Class: Origen::RevisionControl::Git
- Defined in:
- lib/origen/revision_control/git.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #build(options = {}) ⇒ Object
-
#can_checkin? ⇒ Boolean
Returns true if the current user can checkin to the given repo (means has permission to push in Git terms).
- #changes(dir = nil, options = {}) ⇒ Object
- #checkin(path = nil, options = {}) ⇒ Object
- #checkout(path = nil, options = {}) ⇒ Object
- #current_branch ⇒ Object
-
#delete_all(dir = nil, options = {}) ⇒ Object
Delete everything in the given directory, or the whole repo.
- #diff_cmd(file, version) ⇒ Object
- #initialized? ⇒ Boolean
- #local_modifications(dir = nil, options = {}) ⇒ Object
- #root ⇒ Object
- #tag(id, options = {}) ⇒ Object
-
#tag_exists?(tag) ⇒ Boolean
Returns true if the given tag already exists.
- #unmanaged(dir = nil, options = {}) ⇒ Object
Methods inherited from Base
#dssc?, #git?, #initialize, #svn?
Constructor Details
This class inherits a constructor from Origen::RevisionControl::Base
Instance Method Details
#build(options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/origen/revision_control/git.rb', line 4 def build( = {}) if Dir["#{local}/*"].empty? || [:force] FileUtils.rm_rf(local.to_s) # Not using the regular 'git' method here since the local dir doesn't exist to CD into system "git clone #{remote} #{local}" else fail "The requested workspace is not empty: #{local}" end end |
#can_checkin? ⇒ Boolean
Returns true if the current user can checkin to the given repo (means has permission to push in Git terms)
112 113 114 115 116 117 |
# File 'lib/origen/revision_control/git.rb', line 112 def can_checkin? git('push --dry-run', verbose: false) true rescue false end |
#changes(dir = nil, options = {}) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/origen/revision_control/git.rb', line 119 def changes(dir = nil, = {}) paths, = clean_path(dir, ) = { verbose: false }.merge() # Pulls latest metadata from server, does not change workspace git 'fetch', version = [:version] || 'HEAD' objects = {} objects[:added] = git("diff --name-only --diff-filter=A #{version} #{paths.first}", ).map(&:strip) objects[:removed] = git("diff --name-only --diff-filter=D #{version} #{paths.first}", ).map(&:strip) objects[:changed] = git("diff --name-only --diff-filter=M #{version} #{paths.first}", ).map(&:strip) objects[:present] = !objects[:added].empty? || !objects[:removed].empty? || !objects[:changed].empty? # Return full paths objects[:added].map! { |i| "#{paths.first}/" + i } objects[:removed].map! { |i| "#{paths.first}/" + i } objects[:changed].map! { |i| "#{paths.first}/" + i } objects end |
#checkin(path = nil, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/origen/revision_control/git.rb', line 61 def checkin(path = nil, = {}) paths, = clean_path(path, ) # Can't check in unless we have the latest if [:force] && ![:initial] # Locally check in the given files checkin(paths.join(' '), local: true, verbose: false, comment: [:comment]) local_rev = current_commit # Pull latest checkout # Restore the given files to our previous version # Errors are ignored here since this can fail if the given file didn't exist until now, # in that case we already implicitly have the previous version git("checkout #{local_rev} -- #{paths.join(' ')}", check_errors: false) # Then proceed with checking them in as latest else checkout unless [:initial] end cmd = 'add' if [:unmanaged] cmd += ' -A' else cmd += ' -u' unless [:unmanaged] end cmd += " #{paths.join(' ')}" git cmd, if changes_pending_commit? cmd = 'commit' if [:comment] && ![:comment].strip.empty? cmd += " -m \"#{[:comment].strip}\"" else cmd += " -m \"No comment!\"" end if [:author] if [:author].respond_to?(:name_and_email) = [:author].name_and_email else = "#{[:author]} <>" end cmd += " --author=\"#{}\"" end if [:time] cmd += " --date=\"#{[:time].strftime('%a %b %e %H:%M:%S %Y %z')}\"" end git cmd, end git "push origin #{current_branch}" unless [:local] paths end |
#checkout(path = nil, options = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/origen/revision_control/git.rb', line 14 def checkout(path = nil, = {}) paths, = clean_path(path, ) # Pulls latest metadata from server, does not change workspace git 'fetch', version = [:version] || current_branch if version == 'HEAD' puts "Sorry, but you are not currently on a branch and I don't know which branch you want to checkout" puts 'Please supply a branch name as the version to checkout the latest version of it, e.g. origen rc co -v develop' exit 1 end if [:force] git 'reset HEAD' git 'pull', git "checkout #{version} #{paths.join(' ')}", else if paths.size > 1 || paths.first != local.to_s fail 'The Git driver does not support partial merge checkout, it has to be the whole workspace' end git 'reset HEAD' res = git 'stash', stashed = !res.any? { |l| l =~ /^No local changes to save/ } git 'pull', git "checkout #{version}", if stashed result = git 'stash pop', { check_errors: false }.merge() conflicts = [] result.each do |line| if line =~ /CONFLICT.* (.*)$/ conflicts << Regexp.last_match(1) end end git 'reset HEAD' unless conflicts.empty? Origen.log.info '' Origen.log.error 'Your local changes could not automatically merged into the following files, open them to fix the conflicts:' conflicts.each do |conflict| Origen.log.error " #{conflict}" end end end end paths end |
#current_branch ⇒ Object
183 184 185 |
# File 'lib/origen/revision_control/git.rb', line 183 def current_branch git('rev-parse --abbrev-ref HEAD', verbose: false).first end |
#delete_all(dir = nil, options = {}) ⇒ Object
Delete everything in the given directory, or the whole repo
201 202 203 204 205 |
# File 'lib/origen/revision_control/git.rb', line 201 def delete_all(dir = nil, = {}) paths, = clean_path(dir, ) files = git("ls-files #{paths.first}") FileUtils.rm_f files end |
#diff_cmd(file, version) ⇒ Object
164 165 166 |
# File 'lib/origen/revision_control/git.rb', line 164 def diff_cmd(file, version) "git difftool --tool tkdiff -y #{prefix_tag(version)} #{file}" end |
#initialized? ⇒ Boolean
194 195 196 197 198 |
# File 'lib/origen/revision_control/git.rb', line 194 def initialized? File.exist?("#{local}/.git") && git('remote -v', verbose: false).any? { |r| r =~ /#{remote_without_protocol}/ } && !git('status', verbose: false).any? { |l| l =~ /^#? ?Initial commit$/ } end |
#local_modifications(dir = nil, options = {}) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/origen/revision_control/git.rb', line 142 def local_modifications(dir = nil, = {}) paths, = clean_path(dir, ) = { verbose: false }.merge() cmd = 'diff --name-only' dir = " #{paths.first}" unstaged = git(cmd + dir, ).map(&:strip) staged = git(cmd + ' --cached' + dir, ).map(&:strip) unstaged + staged end |
#root ⇒ Object
179 180 181 |
# File 'lib/origen/revision_control/git.rb', line 179 def root Pathname.new(git('rev-parse --show-toplevel', verbose: false).first.strip) end |
#tag(id, options = {}) ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/origen/revision_control/git.rb', line 168 def tag(id, = {}) id = VersionString.new(id) id = id.prefixed if id.semantic? if [:comment] git "tag -a #{id} -m \"#{[:comment]}\"" else git "tag #{id}" end git "push origin #{id}" end |
#tag_exists?(tag) ⇒ Boolean
Returns true if the given tag already exists
188 189 190 191 192 |
# File 'lib/origen/revision_control/git.rb', line 188 def tag_exists?(tag) git('fetch', verbose: false) unless @all_tags_fetched @all_tags_fetched = true git('tag', verbose: false).include?(tag.to_s) end |
#unmanaged(dir = nil, options = {}) ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/origen/revision_control/git.rb', line 155 def unmanaged(dir = nil, = {}) paths, = clean_path(dir, ) = { verbose: false }.merge() cmd = "ls-files #{paths.first} --exclude-standard --others" git(cmd, ).map(&:strip) end |