Module: Git::Commands

Included in:
ReleaseNotes, Tagger
Defined in:
lib/git/commands.rb

Instance Method Summary collapse

Instance Method Details

#assert_is_git_repoObject



4
5
6
# File 'lib/git/commands.rb', line 4

def assert_is_git_repo
  error "Not a git repository" unless is_git_repo?
end

#get_branchObject

figure out what branch the pwd’s repo is on



13
14
15
16
# File 'lib/git/commands.rb', line 13

def get_branch
  match = Regexp.new("^# On branch (.*)").match(`git status`)
  match && match[1]
end

#get_tagsObject

Find all version tags



24
25
26
27
# File 'lib/git/commands.rb', line 24

def get_tags
  version_regexp = valid_version_regexp
  %x{git tag}.split.grep(version_regexp).sort_by{|v| v.split('.').map{|nbr| nbr[/\d+/].to_i}}.map{|tag| tag.strip}
end

#is_git_repo?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/git/commands.rb', line 8

def is_git_repo?
  File.directory?('.git')
end

#needs_commit?(dir = Dir.pwd, file = nil) ⇒ Boolean

based on ‘git status’ output, does this repo contain changes that need to be committed?

optional second argument is a specific file (or directory) in the repo.

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git/commands.rb', line 57

def needs_commit?(dir = Dir.pwd, file = nil)
  rval = false
  Dir.chdir(dir) do
    status = %x{git status}
    if file.nil?
      rval = true unless status =~ /nothing to commit \(working directory clean\)|nothing added to commit but untracked files present/
      if status =~ /nothing added to commit but untracked files present/
        warn "untracked files present in #{dir}"
        show_changed_files(status)
      end
    else
      rval = true if status =~ /^#\t.*modified:   #{file}/
    end
  end
  rval
end

#next_version(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/git/commands.rb', line 29

def next_version(options={})
  latest_tag = get_tags.last
  return 'v0.0.1' unless latest_tag

  unless latest_tag.match valid_version_regexp
    warn "invalid version number"
    return latest_tag
  end

  major, minor, point = latest_tag.split('.')
  major = major[1..-1]
  if options[:major]
    major.succ!
    minor, point = '0', '0'
  elsif options[:minor]
    minor.succ!
    point = '0'
  elsif options[:point]
    point.succ!
  else
    warn "unable to increment version number."
  end

  'v' + [major, minor, point].join('.')
end

#valid_version_regexpObject

e.g. v1.4.3



19
20
21
# File 'lib/git/commands.rb', line 19

def valid_version_regexp
  /^v\d+\.\d+\.\d+/
end