Class: Apadmi::Grout::GitUtils
- Inherits:
-
Object
- Object
- Apadmi::Grout::GitUtils
- Defined in:
- lib/apadmi/grout/utils/git_utils.rb
Overview
Generic Git related utils
Constant Summary collapse
Class Method Summary collapse
-
.branch(args = "") ⇒ Object
Runs git branch command with given args.
-
.changelog(grep_conditions, merges_only: true) ⇒ Object
Gets commits accessible from the current HEAD which match at least one of the given grep conditions.
-
.commit_hash ⇒ Object
Gets the commit hash of the current HEAD.
-
.fetch_all ⇒ Object
Runs a git fetch all.
- .git_root ⇒ Object
-
.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.
-
.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.
-
.list_tags(args = "") ⇒ String[]
Returns all the tags in the repo.
-
.number_of_commits ⇒ String
Gets the number of commits accessible from HEAD treating the history as a graph.
Instance Method Summary collapse
-
#branch(args = "") ⇒ Object
Runs git branch command with given args.
-
#changelog(grep_conditions, merges_only: true) ⇒ Object
Gets commits accessible from the current HEAD which match at least one of the given grep conditions.
-
#commit_hash ⇒ Object
Gets the commit hash of the current HEAD.
-
#fetch_all ⇒ Object
Runs a git fetch all.
-
#git_root ⇒ String
Gets the root of the Git repo we’re in.
-
#initialize(repo_path = nil) ⇒ GitUtils
constructor
A new instance of GitUtils.
-
#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.
-
#is_shallow_clone? ⇒ Boolean
Helper function to check if a repo is a shallow clone.
-
#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.
- #last_ancestor_tag_hash(pattern) ⇒ Object
-
#list_tags(args = "") ⇒ String[]
Returns all the tags in the repo.
-
#number_of_commits ⇒ String
Gets the number of commits accessible from HEAD treating the history as a graph.
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.
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_hash ⇒ Object
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_all ⇒ Object
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_root ⇒ Object
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.
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
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
41 42 43 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 41 def self.(args = "") @@default.(args) end |
.number_of_commits ⇒ String
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
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.
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_hash ⇒ Object
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_all ⇒ Object
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_root ⇒ String
Gets the root of the Git repo we’re in
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.
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
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
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
32 33 34 35 36 37 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 32 def (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_commits ⇒ String
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
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 |