Class: Plus2GitTagger::Git

Inherits:
Struct
  • Object
show all
Defined in:
lib/plus2_git_tagger/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGit

Returns a new instance of Git.



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

def initialize(*)
  super
  self.repo = Pathname( repo ).expand_path
end

Instance Attribute Details

#repoObject

Returns the value of attribute repo

Returns:

  • (Object)

    the current value of repo



2
3
4
# File 'lib/plus2_git_tagger/git.rb', line 2

def repo
  @repo
end

Instance Method Details

#commit_if_required(subject) ⇒ Object

Try to commit, but don’t worry if it doesn’t work because it wasn’t needed.



24
25
26
27
28
29
30
# File 'lib/plus2_git_tagger/git.rb', line 24

def commit_if_required(subject)
  result = git_s("commit -uno -m '#{ subject }' 2>&1")

  unless $?.success? || result[/nothing to commit/i] || result[/no changes added to commit/i]
    abort result
  end
end

#current_subjectObject

Queries git for HEAD’s subject.

Returns a string containing HEAD’s subject.



12
13
14
# File 'lib/plus2_git_tagger/git.rb', line 12

def current_subject
  git_s('log --pretty="format:%s" -1').chomp
end

#escaped_current_subjectObject

Naive: remove quotes.



18
19
20
# File 'lib/plus2_git_tagger/git.rb', line 18

def escaped_current_subject
  current_subject.gsub(/["']/, '')
end

#git(cmd) ⇒ Object Also known as: run

Run git.



40
41
42
43
44
45
46
# File 'lib/plus2_git_tagger/git.rb', line 40

def git( cmd )
  Dir.chdir( repo ) do
    system "git #{cmd}"

    abort("'git #{cmd}' failed") unless $?.success?
  end
end

#git_s(cmd) ⇒ Object

Run git, capture stdout.



51
52
53
54
55
# File 'lib/plus2_git_tagger/git.rb', line 51

def git_s( cmd )
  Dir.chdir( repo ) do
    `git #{cmd}`
  end
end

#tagsObject

Returns a list of all tags for this repo.



34
35
36
# File 'lib/plus2_git_tagger/git.rb', line 34

def tags
  git_s('show-ref --tags --abbrev=1').split("\n").map {|line| line.split(/\s/).last.chomp.sub(%r[^refs/tags/],'')}
end