Module: Pkg::Util::Git
- Defined in:
- lib/packaging/util/git.rb
Overview
Utility methods for handling git
Class Method Summary collapse
-
.branch_name ⇒ Object
Return the name of the current branch.
-
.bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) ⇒ Object
Git utility to create a new git bundle rubocop:disable Metrics/AbcSize.
-
.checkout(ref) ⇒ Object
Checks out a specified ref.
-
.commit_file(file, message = 'changes') ⇒ Object
Git utility to create a new git commit.
-
.describe(extra_opts = ['--tags', '--dirty']) ⇒ Object
Returns the value of ‘git describe`.
- .fail_on_dirty_source ⇒ Object
-
.fail_unless_repo ⇒ Object
rubocop:disable Style/GuardClause.
- .git_bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) ⇒ Object
-
.git_commit_file(file, message = "changes") ⇒ Object
DEPRECATED METHODS.
- .git_pull(remote, branch) ⇒ Object
- .git_tag(version) ⇒ Object
-
.project_name ⇒ Object
Return the basename of the project repo.
- .pull(remote, branch) ⇒ Object
-
.ref_type ⇒ Object
Return the ref type of HEAD on the current branch.
-
.remote_tagged?(url, ref) ⇒ Boolean
Reports if a ref and its corresponding git repo points to a git tag.
-
.repo? ⇒ Boolean
Return true if we’re in a git repo, otherwise false.
-
.sha(length = 40) ⇒ Object
return the sha of HEAD on the current branch You can specify the length you want from the sha.
-
.sha_or_tag(length = 40) ⇒ Object
If HEAD is a tag, return the tag.
- .source_dirty? ⇒ Boolean
-
.tag(version) ⇒ Object
Git utility to create a new git tag.
-
.tagged? ⇒ Boolean
Check if we are currently working on a tagged commit.
Class Method Details
.branch_name ⇒ Object
Return the name of the current branch
133 134 135 136 137 138 |
# File 'lib/packaging/util/git.rb', line 133 def branch_name Pkg::Util.in_project_root do stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} rev-parse --abbrev-ref HEAD") stdout.strip end end |
.bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) ⇒ Object
Git utility to create a new git bundle rubocop:disable Metrics/AbcSize
26 27 28 29 30 31 32 33 34 |
# File 'lib/packaging/util/git.rb', line 26 def bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) fail_unless_repo Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} bundle create #{temp}/#{Pkg::Config.project}-#{Pkg::Config.version}-#{appendix} #{treeish} --tags") Dir.chdir(temp) do Pkg::Util::Execution.capture3("#{Pkg::Util::Tool.find_tool('tar')} -czf #{Pkg::Config.project}-#{Pkg::Config.version}-#{appendix}.tar.gz #{Pkg::Config.project}-#{Pkg::Config.version}-#{appendix}") FileUtils.rm_rf("#{Pkg::Config.project}-#{Pkg::Config.version}-#{appendix}") end "#{temp}/#{Pkg::Config.project}-#{Pkg::Config.version}-#{appendix}.tar.gz" end |
.checkout(ref) ⇒ Object
Checks out a specified ref. The ref must exist in the current repo. This also removes any uncommitted changes
59 60 61 62 63 64 |
# File 'lib/packaging/util/git.rb', line 59 def checkout(ref) Pkg::Util.in_project_root do _, _, ret = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} reset --hard ; #{Pkg::Util::Tool::GIT} checkout #{ref}") Pkg::Util::Execution.success?(ret) || raise("Could not checkout #{ref} git branch to build package from...exiting") end end |
.commit_file(file, message = 'changes') ⇒ Object
Git utility to create a new git commit
7 8 9 10 11 12 13 14 15 |
# File 'lib/packaging/util/git.rb', line 7 def commit_file(file, = 'changes') fail_unless_repo puts 'Commiting changes:' puts diff, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} diff HEAD #{file}") puts diff stdout, = Pkg::Util::Execution.capture3(%(#{Pkg::Util::Tool::GIT} commit #{file} -m "Commit #{} in #{file}" &> #{Pkg::Util::OS::DEVNULL})) stdout end |
.describe(extra_opts = ['--tags', '--dirty']) ⇒ Object
Returns the value of ‘git describe`. If this is not a git repo or `git describe` fails because there is no tag, this will return false
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/packaging/util/git.rb', line 68 def describe(extra_opts = ['--tags', '--dirty']) Pkg::Util.in_project_root do stdout, _, ret = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} describe #{Array(extra_opts).join(' ')}") if Pkg::Util::Execution.success?(ret) stdout.strip else false end end end |
.fail_on_dirty_source ⇒ Object
144 145 146 147 148 149 |
# File 'lib/packaging/util/git.rb', line 144 def fail_on_dirty_source if source_dirty? raise "The source tree is dirty, e.g. there are uncommited changes. \ Please commit/discard changes and try again." end end |
.fail_unless_repo ⇒ Object
rubocop:disable Style/GuardClause
117 118 119 120 121 122 |
# File 'lib/packaging/util/git.rb', line 117 def fail_unless_repo unless repo? raise "Pkg::Config.project_root (#{Pkg::Config.project_root}) is not \ a valid git repository" end end |
.git_bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) ⇒ Object
164 165 166 167 |
# File 'lib/packaging/util/git.rb', line 164 def git_bundle(treeish, appendix = Pkg::Util.rand_string, temp = Pkg::Util::File.mktemp) Pkg::Util.deprecate('Pkg::Util::Git.git_bundle', 'Pkg::Util::Git.bundle') Pkg::Util::Git.bundle(treeish, appendix, temp) end |
.git_commit_file(file, message = "changes") ⇒ Object
DEPRECATED METHODS
154 155 156 157 |
# File 'lib/packaging/util/git.rb', line 154 def git_commit_file(file, = "changes") Pkg::Util.deprecate('Pkg::Util::Git.git_commit_file', 'Pkg::Util::Git.commit_file') Pkg::Util::Git.commit_file(file, ) end |
.git_pull(remote, branch) ⇒ Object
169 170 171 172 |
# File 'lib/packaging/util/git.rb', line 169 def git_pull(remote, branch) Pkg::Util.deprecate('Pkg::Util::Git.git_pull', 'Pkg::Util::Git.pull') Pkg::Util::Git.pull(remote, branch) end |
.git_tag(version) ⇒ Object
159 160 161 162 |
# File 'lib/packaging/util/git.rb', line 159 def git_tag(version) Pkg::Util.deprecate('Pkg::Util::Git.git_tag', 'Pkg::Util::Git.tag') Pkg::Util::Git.tag(version) end |
.project_name ⇒ Object
Return the basename of the project repo
125 126 127 128 129 130 |
# File 'lib/packaging/util/git.rb', line 125 def project_name Pkg::Util.in_project_root do stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} config --get remote.origin.url") stdout.split('/')[-1].chomp('.git').chomp end end |
.pull(remote, branch) ⇒ Object
36 37 38 39 40 |
# File 'lib/packaging/util/git.rb', line 36 def pull(remote, branch) fail_unless_repo stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} pull #{remote} #{branch}") stdout end |
.ref_type ⇒ Object
Return the ref type of HEAD on the current branch
92 93 94 95 96 97 |
# File 'lib/packaging/util/git.rb', line 92 def ref_type Pkg::Util.in_project_root do stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} cat-file -t #{describe('')}") stdout.strip end end |
.remote_tagged?(url, ref) ⇒ Boolean
Reports if a ref and its corresponding git repo points to a git tag.
52 53 54 55 |
# File 'lib/packaging/util/git.rb', line 52 def remote_tagged?(url, ref) reference = Pkg::Util::Git_tag.new(url, ref) reference.tag? end |
.repo? ⇒ Boolean
Return true if we’re in a git repo, otherwise false
109 110 111 112 113 114 |
# File 'lib/packaging/util/git.rb', line 109 def repo? Pkg::Util.in_project_root do _, _, ret = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} rev-parse --git-dir") Pkg::Util::Execution.success?(ret) end end |
.sha(length = 40) ⇒ Object
return the sha of HEAD on the current branch You can specify the length you want from the sha. Default is 40, the length for sha1. If you specify anything higher, it will still return 40 characters. Ideally, you’re not going to specify anything under 7 characters, but I’ll leave that discretion up to you.
84 85 86 87 88 89 |
# File 'lib/packaging/util/git.rb', line 84 def sha(length = 40) Pkg::Util.in_project_root do stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} rev-parse --short=#{length} HEAD") stdout.strip end end |
.sha_or_tag(length = 40) ⇒ Object
If HEAD is a tag, return the tag. Otherwise return the sha of HEAD.
100 101 102 103 104 105 106 |
# File 'lib/packaging/util/git.rb', line 100 def sha_or_tag(length = 40) if ref_type == 'tag' describe else sha(length) end end |
.source_dirty? ⇒ Boolean
140 141 142 |
# File 'lib/packaging/util/git.rb', line 140 def source_dirty? describe.include?('dirty') end |
.tag(version) ⇒ Object
Git utility to create a new git tag
18 19 20 21 22 |
# File 'lib/packaging/util/git.rb', line 18 def tag(version) fail_unless_repo stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} tag -s -u #{Pkg::Util::Gpg.key} -m '#{version}' #{version}") stdout end |
.tagged? ⇒ Boolean
Check if we are currently working on a tagged commit.
43 44 45 |
# File 'lib/packaging/util/git.rb', line 43 def tagged? ref_type == 'tag' end |