Module: Maximus::GitHelper

Included in:
GitControl
Defined in:
lib/maximus/git_helper.rb

Overview

Methods used for git commands

Since:

  • 0.1.7

Instance Method Summary collapse

Instance Method Details

#branchString

Get current branch name

Returns:

  • (String)

Since:

  • 0.1.7



30
31
32
# File 'lib/maximus/git_helper.rb', line 30

def branch
  `env -i git rev-parse --abbrev-ref HEAD`.strip!
end

#commit_information(commit_sha) ⇒ Object

A commit’s insertions, deletions, and file names

Since:

  • 0.1.7



66
67
68
69
# File 'lib/maximus/git_helper.rb', line 66

def commit_information(commit_sha)
  # Start after the commit message
  `git -C #{@config.working_dir} log --numstat --oneline #{commit_sha}`.split("\n")[1..-1]
end

#files_by_sha(commit_sha) ⇒ Object

Grab files by sha

Since:

  • 0.1.7



54
55
56
# File 'lib/maximus/git_helper.rb', line 54

def files_by_sha(commit_sha)
  `git -C #{@config.working_dir} show --pretty="format:" --name-only #{commit_sha}`
end

#first_commitString

Find first commit

Returns:

  • (String)

Since:

  • 0.1.5



9
10
11
# File 'lib/maximus/git_helper.rb', line 9

def first_commit
  `git -C #{@config.working_dir} rev-list --max-parents=0 HEAD`.strip!
end

#head_shaString

Get last commit on current branch

Returns:

  • (String)

    sha

Since:

  • 0.1.7



24
25
26
# File 'lib/maximus/git_helper.rb', line 24

def head_sha
  @g.object('HEAD').sha
end

#lines_by_sha(commit_sha) ⇒ Array

Retrieve insertions by commit with a custom script

Returns:

  • (Array)

Since:

  • 0.1.7



74
75
76
# File 'lib/maximus/git_helper.rb', line 74

def lines_by_sha(commit_sha)
  `#{File.join(File.dirname(__FILE__), 'reporter', 'git-lines.sh')} #{@config.working_dir} #{commit_sha}`.split("\n")
end

#master_commit_shaString

Get last commit sha on the master branch

Returns:

  • (String)

Since:

  • 0.1.7



36
37
38
# File 'lib/maximus/git_helper.rb', line 36

def master_commit_sha
  @g.branches[:master].blank? ? head_sha : @g.branches[:master].gcommit.sha
end

#previous_commit(current_commit = head_sha, previous_by = 1) ⇒ String

Get commit before current

Parameters:

  • current_commit (String) (defaults to: head_sha)

    (head_sha) commit to start at

  • previous_by (Integer) (defaults to: 1)

    (1) commit n commits ago

Returns:

  • (String)

Since:

  • 0.1.5



18
19
20
# File 'lib/maximus/git_helper.rb', line 18

def previous_commit(current_commit = head_sha, previous_by = 1)
  `git -C #{@config.working_dir} rev-list --max-count=#{previous_by + 1} #{current_commit} --reverse | head -n1`.strip!
end

#remoteString?

Get remote URL

Returns:

  • (String, nil)

    nil returns if remotes is blank

Since:

  • 0.1.7



42
43
44
# File 'lib/maximus/git_helper.rb', line 42

def remote
  @g.remotes.first.url unless @g.remotes.blank?
end

#sha_range(sha1, sha2) ⇒ Object

Retrieve list of shas between two commits

Since:

  • 0.1.7



60
61
62
# File 'lib/maximus/git_helper.rb', line 60

def sha_range(sha1, sha2)
  `git -C #{@config.working_dir} rev-list #{sha1}..#{sha2} --no-merges`
end

#working_copy_filesObject

Return file names of working copy files

Since:

  • 0.1.7



48
49
50
# File 'lib/maximus/git_helper.rb', line 48

def working_copy_files
  `git -C #{@config.working_dir} diff --name-only`
end