Module: Pkg::Util::Git

Defined in:
lib/packaging/util/git.rb

Overview

Utility methods for handling git

Class Method Summary collapse

Class Method Details

.branch_nameObject

Return the name of the current branch



131
132
133
134
135
136
# File 'lib/packaging/util/git.rb', line 131

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



25
26
27
28
29
30
31
32
33
# File 'lib/packaging/util/git.rb', line 25

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



58
59
60
61
62
63
# File 'lib/packaging/util/git.rb', line 58

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, message = 'changes')
  fail_unless_repo
  puts 'Committing 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 #{message} 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



67
68
69
70
71
72
73
74
75
76
# File 'lib/packaging/util/git.rb', line 67

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_sourceObject



142
143
144
145
146
147
# File 'lib/packaging/util/git.rb', line 142

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_repoObject



115
116
117
118
119
120
# File 'lib/packaging/util/git.rb', line 115

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



162
163
164
165
# File 'lib/packaging/util/git.rb', line 162

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



152
153
154
155
# File 'lib/packaging/util/git.rb', line 152

def git_commit_file(file, message = "changes")
  Pkg::Util.deprecate('Pkg::Util::Git.git_commit_file', 'Pkg::Util::Git.commit_file')
  Pkg::Util::Git.commit_file(file, message)
end

.git_pull(remote, branch) ⇒ Object



167
168
169
170
# File 'lib/packaging/util/git.rb', line 167

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



157
158
159
160
# File 'lib/packaging/util/git.rb', line 157

def git_tag(version)
  Pkg::Util.deprecate('Pkg::Util::Git.git_tag', 'Pkg::Util::Git.tag')
  Pkg::Util::Git.tag(version)
end

.project_nameObject

Return the basename of the project repo



123
124
125
126
127
128
# File 'lib/packaging/util/git.rb', line 123

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



35
36
37
38
39
# File 'lib/packaging/util/git.rb', line 35

def pull(remote, branch)
  fail_unless_repo
  stdout, = Pkg::Util::Execution.capture3("#{Pkg::Util::Tool::GIT} pull #{remote} #{branch}")
  stdout
end

.ref_typeObject

Return the ref type of HEAD on the current branch



91
92
93
94
95
96
# File 'lib/packaging/util/git.rb', line 91

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.

Parameters:

  • url (string)

    url of repo grabbed from json file

  • ref (string)

    ref grabbed from json file

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/packaging/util/git.rb', line 51

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

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/packaging/util/git.rb', line 108

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.



83
84
85
86
87
88
# File 'lib/packaging/util/git.rb', line 83

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.



99
100
101
102
103
104
105
# File 'lib/packaging/util/git.rb', line 99

def sha_or_tag(length = 40)
  if ref_type == 'tag'
    describe
  else
    sha(length)
  end
end

.source_dirty?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/packaging/util/git.rb', line 138

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.

Returns:

  • (Boolean)


42
43
44
# File 'lib/packaging/util/git.rb', line 42

def tagged?
  ref_type == 'tag'
end