Class: Apadmi::Grout::GitUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/apadmi/grout/utils/git_utils.rb

Overview

Generic Git related utils

Constant Summary collapse

@@default =

rubocop:disable Style/ClassVars:

Apadmi::Grout::GitUtils.new(nil)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_path = nil) ⇒ GitUtils

Returns a new instance of GitUtils.



9
10
11
# File 'lib/apadmi/grout/utils/git_utils.rb', line 9

def initialize(repo_path = nil)
  @repo_path_arg = repo_path.nil? ? "" : " -C #{repo_path} "
end

Class Method Details

.branch(args = "") ⇒ Object

Runs git branch command with given args



54
55
56
# File 'lib/apadmi/grout/utils/git_utils.rb', line 54

def self.branch(args = "")
  @@default.branch(args)
end

.commit_hashObject

Gets the commit hash of the current HEAD



67
68
69
# File 'lib/apadmi/grout/utils/git_utils.rb', line 67

def self.commit_hash
  @@default.commit_hash
end

.fetch_allObject

Runs a git fetch all



97
98
99
# File 'lib/apadmi/grout/utils/git_utils.rb', line 97

def self.fetch_all
  @@default.fetch_all
end

.git_rootObject



26
27
28
# File 'lib/apadmi/grout/utils/git_utils.rb', line 26

def self.git_root
  @@default.git_root
end

.invert_changelog(grep_conditions) ⇒ Object

Gets all the merges that are NOT accessible from HEAD which matches at least one of the given grep conditions

Parameters:



129
130
131
# File 'lib/apadmi/grout/utils/git_utils.rb', line 129

def self.invert_changelog(grep_conditions)
  @@default.invert_changelog(grep_conditions)
end

.list_tags(args = "") ⇒ String[]

Returns all the tags in the repo

Returns:

  • (String[])

    The tags



41
42
43
# File 'lib/apadmi/grout/utils/git_utils.rb', line 41

def self.list_tags(args = "")
  @@default.list_tags(args)
end

.merge_changelog(grep_conditions) ⇒ Object

Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions

Parameters:



113
114
115
# File 'lib/apadmi/grout/utils/git_utils.rb', line 113

def self.merge_changelog(grep_conditions)
  @@default.merge_changelog(grep_conditions)
end

.number_of_commitsString

Gets the number of commits accessible from HEAD treating the history as a graph. See more details here: git-scm.com/docs/git-rev-list

Returns:

  • (String)

    The number of commits



84
85
86
# File 'lib/apadmi/grout/utils/git_utils.rb', line 84

def self.number_of_commits
  @@default.number_of_commits
end

Instance Method Details

#branch(args = "") ⇒ Object

Runs git branch command with given args



46
47
48
49
50
51
# File 'lib/apadmi/grout/utils/git_utils.rb', line 46

def branch(args = "")
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} branch #{args}")
  raise "Failed to run branch: #{stderr}" unless status.success?

  stdout.strip
end

#commit_hashObject

Gets the commit hash of the current HEAD



59
60
61
62
63
64
# File 'lib/apadmi/grout/utils/git_utils.rb', line 59

def commit_hash
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} rev-parse HEAD")
  raise "Failed to get hash: #{stderr}" unless status.success?

  stdout.strip
end

#fetch_allObject

Runs a git fetch all



89
90
91
92
93
94
# File 'lib/apadmi/grout/utils/git_utils.rb', line 89

def fetch_all
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} fetch --all")
  raise "Failed to fetch #{stderr}" unless status.success?

  stdout.strip
end

#git_rootString

Gets the root of the Git repo we're in

Returns:

  • (String)

    The full path for the root of this Git repo



19
20
21
22
23
24
# File 'lib/apadmi/grout/utils/git_utils.rb', line 19

def git_root
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} rev-parse --show-toplevel")
  raise "Failed to get git root: #{stderr}" unless status.success?

  stdout.strip
end

#invert_changelog(grep_conditions) ⇒ Object

Gets all the merges that are NOT accessible from HEAD which matches at least one of the given grep conditions

Parameters:



119
120
121
122
123
124
125
# File 'lib/apadmi/grout/utils/git_utils.rb', line 119

def invert_changelog(grep_conditions)
  command = "git #{@repo_path_arg} log --all ^HEAD --merges --format=%s#{grep_conditions.map { |c| " --grep #{c}" }.join(" ")}"
  stdout, stderr, status = Open3.capture3(command)
  raise "Failed to get changelog: #{stderr}" unless status.success?

  stdout
end

#list_tags(args = "") ⇒ String[]

Returns all the tags in the repo

Returns:

  • (String[])

    The tags



32
33
34
35
36
37
# File 'lib/apadmi/grout/utils/git_utils.rb', line 32

def list_tags(args = "")
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} tag --list #{args}")
  raise "Failed to list tags: #{stderr}" unless status.success?

  stdout.strip.split("\n")
end

#merge_changelog(grep_conditions) ⇒ Object

Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions

Parameters:



103
104
105
106
107
108
109
# File 'lib/apadmi/grout/utils/git_utils.rb', line 103

def merge_changelog(grep_conditions)
  command = "git #{@repo_path_arg} log HEAD --merges --format=%s#{grep_conditions.map { |c| " --grep #{c}" }.join(" ")}"
  stdout, stderr, status = Open3.capture3(command)
  raise "Failed to get changelog: #{stderr}" unless status.success?

  stdout
end

#number_of_commitsString

Gets the number of commits accessible from HEAD treating the history as a graph. See more details here: git-scm.com/docs/git-rev-list

Returns:

  • (String)

    The number of commits



74
75
76
77
78
79
# File 'lib/apadmi/grout/utils/git_utils.rb', line 74

def number_of_commits
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} rev-list HEAD --count")
  raise "Failed to get commit number: #{stderr}" unless status.success?

  stdout.strip
end