Skip to content
Browse files

Better updating of cached git sources.

Make sure to hard-reset to get rid of any potential problems from a previous run. This should clear the way for using checkout safely.

Do not merge remote branches into local branches. Instead, just fetch all branches and tags from the remote and just keep the branches as mere remote branches.

Checkout the SHA directly. No need to open concerns about checking out branches that could possibly be updated.

In the case of a branch being specified on the source, this requires looking up the SHA for the remote branch, rather than for the local branch, because remote branches are not merged into local branches. In the case of a tag or SHA being specified on the source, such a lookup appears unnecessary.

Fixes #44. Fixes #49. Thanks @TylerRick and @bradmontgomery.

Replaces original fixes for #36, #37, and #39.
  • Loading branch information...
1 parent a19f6ed commit ad17a1e6e23d536f47c3c64ef559797a1d3468cd @yfeldblum yfeldblum committed Apr 6, 2012
Showing with 45 additions and 24 deletions.
  1. +9 −5 lib/librarian/source/git.rb
  2. +36 −19 lib/librarian/source/git/repository.rb
View
14 lib/librarian/source/git.rb
@@ -79,12 +79,16 @@ def cache!(dependencies)
repository.path.mkpath
repository.clone!(uri)
end
+ repository.reset_hard!
unless sha == repository.current_commit_hash
- repository.fetch!(:tags => true)
- repository.fetch!
- repository.merge_all_remote_branches!
- repository.checkout!(repository.hash_from(sha || ref), :force => true)
- @sha ||= repository.current_commit_hash
+ remote = repository.default_remote
+ repository.fetch!(remote)
+ repository.fetch!(remote, :tags => true)
+
+ new_sha = repository.hash_from(remote, sha || ref)
+ repository.checkout!(new_sha)
+
+ @sha ||= new_sha
end
end
View
55 lib/librarian/source/git/repository.rb
@@ -33,6 +33,10 @@ def git?
path.join('.git').exist?
end
+ def default_remote
+ "origin"
+ end
+
def clone!(repository_url)
within do
command = "clone #{repository_url} ."
@@ -48,48 +52,61 @@ def checkout!(reference, options ={ })
end
end
- def fetch!(options = { })
+ def fetch!(remote, options = { })
within do
- command = "fetch"
+ command = "fetch #{remote}"
command << " --tags" if options[:tags]
run!(command)
end
end
- def merge!(reference)
+ def reset_hard!
within do
- command = "merge #{reference}"
+ command = "reset --hard"
run!(command)
end
end
- def hash_from(reference)
+ def remote_names
within do
- command = "rev-parse #{reference}"
- run!(command).strip
+ command = "remote"
+ run!(command, false).strip.lines.map(&:strip)
end
end
- def current_commit_hash
+ def remote_branch_names
+ remotes = remote_names.sort_by(&:length).reverse
+
within do
- command = "rev-parse HEAD"
- run!(command).strip!
+ command = "branch -r"
+ names = run!(command, false).strip.lines.map(&:strip).to_a
+ names.each{|n| n.gsub!(/\s*->.*$/, "")}
+ names.reject!{|n| n =~ /\/HEAD$/}
+ Hash[remotes.map do |r|
+ matching_names = names.select{|n| n.start_with?("#{r}/")}
+ matching_names.each{|n| names.delete(n)}
+ matching_names.each{|n| n.slice!(0, r.size + 1)}
+ [r, matching_names]
+ end]
end
end
- def merge_all_remote_branches!
- remote_branches.each do |branch|
- checkout!(branch.slice(%r{[^/]+$}), :force => true)
- merge! branch
+ def hash_from(remote, reference)
+ branch_names = remote_branch_names[remote]
+ if branch_names.include?(reference)
+ reference = "#{remote}/#{reference}"
+ end
+
+ within do
+ command = "rev-parse #{reference}"
+ run!(command).strip
end
end
- def remote_branches
+ def current_commit_hash
within do
- command ="branch -r --no-color"
- run!(command, false).split("\n ").reject do |r|
- r.include? '->' #delete pointers like origin/HEAD -> origin/master
- end.collect {|r|r.strip}
+ command = "rev-parse HEAD"
+ run!(command).strip!
end
end
private

3 comments on commit ad17a1e

@yfeldblum

@databus23 Does this commit introduce any regressions for you (#36, #37, #39)?

@databus23

No. Everything works fine. Some nice updates!

@yfeldblum

@databus23 Thanks for verifying.

Please sign in to comment.
Something went wrong with that request. Please try again.