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

.changelog(grep_conditions, merges_only: true) ⇒ Object

Gets commits accessible from the current HEAD which match at least one of the given grep conditions. By default only merge commits are included; set merges_only: false for projects using squash merges.

Parameters:

  • grep_conditions (Array<String>)

    values to be passed in as grep cases (git-scm.com/docs/git-log)

  • merges_only (Boolean) (defaults to: true)

    when true (default) only merge commits are included; when false all commits are included



150
151
152
# File 'lib/apadmi/grout/utils/git_utils.rb', line 150

def self.changelog(grep_conditions, merges_only: true)
  @@default.changelog(grep_conditions, merges_only: merges_only)
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



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

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, merges_only: true) ⇒ Object

Gets commits that are NOT accessible from HEAD which match at least one of the given grep conditions. By default only merge commits are included; set merges_only: false for projects using squash merges.

Parameters:

  • grep_conditions (Array<String>)

    values to be passed in as grep cases (git-scm.com/docs/git-log)

  • merges_only (Boolean) (defaults to: true)

    when true (default) only merge commits are included; when false all commits are included



171
172
173
# File 'lib/apadmi/grout/utils/git_utils.rb', line 171

def self.invert_changelog(grep_conditions, merges_only: true)
  @@default.invert_changelog(grep_conditions, merges_only: merges_only)
end

.last_ancestor_tag_hash(pattern) ⇒ Object

Returns the hash of the last git tag which is an ancestor commit of the current HEAD and matches the given pattern

Parameters:

  • pattern (String)


73
74
75
# File 'lib/apadmi/grout/utils/git_utils.rb', line 73

def self.last_ancestor_tag_hash(pattern)
  @@default.last_ancestor_tag_hash(pattern)
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

.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



116
117
118
# File 'lib/apadmi/grout/utils/git_utils.rb', line 116

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

#changelog(grep_conditions, merges_only: true) ⇒ Object

Gets commits accessible from the current HEAD which match at least one of the given grep conditions. By default only merge commits are included; set merges_only: false for projects using squash merges.

Parameters:

  • grep_conditions (Array<String>)

    values to be passed in as grep cases (git-scm.com/docs/git-log)

  • merges_only (Boolean) (defaults to: true)

    when true (default) only merge commits are included; when false all commits are included



137
138
139
140
141
142
143
144
# File 'lib/apadmi/grout/utils/git_utils.rb', line 137

def changelog(grep_conditions, merges_only: true)
  merges_flag = merges_only ? " --merges" : ""
  command = "git #{@repo_path_arg} log HEAD#{merges_flag} --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

#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



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

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, merges_only: true) ⇒ Object

Gets commits that are NOT accessible from HEAD which match at least one of the given grep conditions. By default only merge commits are included; set merges_only: false for projects using squash merges.

Parameters:

  • grep_conditions (Array<String>)

    values to be passed in as grep cases (git-scm.com/docs/git-log)

  • merges_only (Boolean) (defaults to: true)

    when true (default) only merge commits are included; when false all commits are included



158
159
160
161
162
163
164
165
# File 'lib/apadmi/grout/utils/git_utils.rb', line 158

def invert_changelog(grep_conditions, merges_only: true)
  merges_flag = merges_only ? " --merges" : ""
  command = "git #{@repo_path_arg} log --all ^HEAD#{merges_flag} --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

#is_shallow_clone?Boolean

Helper function to check if a repo is a shallow clone

Returns:

  • (Boolean)

    If the repo is a shallow clone



106
107
108
109
110
111
# File 'lib/apadmi/grout/utils/git_utils.rb', line 106

def is_shallow_clone?
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} rev-parse --is-shallow-repository")
  raise "Failed to check if repo is shallow clone: #{stderr}" unless status.success?

  stdout.strip.downcase == "true"
end

#last_ancestor_tag(pattern) ⇒ Object

Returns the name of the last git tag which is an ancestor commit of the current HEAD and matches the given pattern

Parameters:

  • pattern (String)


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

def last_ancestor_tag(pattern)
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} describe --tags --match '#{pattern}' --abbrev=0")
  raise "Failed to find a tag matching pattern: #{pattern}: #{stderr}" unless status.success?

  stdout.strip
end

#last_ancestor_tag_hash(pattern) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/apadmi/grout/utils/git_utils.rb', line 77

def last_ancestor_tag_hash(pattern)
  tag_name = last_ancestor_tag(pattern)
  stdout, stderr, status = Open3.capture3("git #{@repo_path_arg} rev-list -n 1 #{tag_name}")
  raise "Failed to find a tag: #{tag_name}: #{stderr}" unless status.success?

  stdout.strip
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

#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



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

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