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.
-
.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) ⇒ Object
Gets all the merges that are NOT accessible from HEAD which matches 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.
-
.merge_changelog(grep_conditions) ⇒ Object
Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions.
-
.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.
-
#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) ⇒ Object
Gets all the merges that are NOT accessible from HEAD which matches 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.
-
#merge_changelog(grep_conditions) ⇒ Object
Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions.
-
#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 |
.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
131 132 133 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 131 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) ⇒ Object
Gets all the merges that are NOT accessible from HEAD which matches at least one of the given grep conditions
163 164 165 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 163 def self.invert_changelog(grep_conditions) @@default.invert_changelog(grep_conditions) 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 |
.merge_changelog(grep_conditions) ⇒ Object
Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions
147 148 149 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 147 def self.merge_changelog(grep_conditions) @@default.merge_changelog(grep_conditions) 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
118 119 120 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 118 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_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
123 124 125 126 127 128 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 123 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) ⇒ Object
Gets all the merges that are NOT accessible from HEAD which matches at least one of the given grep conditions
153 154 155 156 157 158 159 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 153 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 |
#is_shallow_clone ⇒ Boolean
Helper function to check if a repo is a shallow clone
108 109 110 111 112 113 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 108 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 |
#merge_changelog(grep_conditions) ⇒ Object
Gets all the merges accessible from the current HEAD which matches at least one of the given grep conditions
137 138 139 140 141 142 143 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 137 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_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 103 104 |
# File 'lib/apadmi/grout/utils/git_utils.rb', line 97 def number_of_commits raise "Repo is a shallow clone so this method will always return 1" if is_shallow_clone 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 |