xuwupeng2000/capistrano-scm-gitcopy

Testing branch name and making archive

jbaptperez opened this issue · 0 comments

Hello,

I have the following problem : my git branch name is already part of other branch names.
Example :
My branch name is foo, but I have also 2 branches which names are foobar and barfoobar.
So, stdout will show me :

d00c52294e78ce782eb12cfa47249d3523286879    refs/heads/foo
ee5e21f46d9d35ef362ed8df80d95b121a9a1482    refs/heads/foobar
304bd988585c5507198075cdbafebc2de2fd339a    refs/heads/barfoobar
fatal: ambiguous argument 'foo': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

In gitcopy.rake, I can see this code:

system "git ls-remote #{fetch(:repo_url)} | grep #{fetch(:branch)}"

-> This call produces the first 3 lines above.
As the exit status of this code is then tested to make or not the archive, I can easily produce a bug:
Specifying a branch like fo or ooba. Both don't exists but the test will pass!

Note that this code just bellow:

system "git archive #{no_repo_url ? '' : "--remote #{fetch(:repo_url)}" } --format=tar #{fetch(:branch)}:#{fetch(:sub_directory)} | gzip > #{ archive_name }"

will produce the last three lines: the archive which is being built can be built from another reference!

Idea of resolution :
First, using exact match with a regular expression. Something like:

system "git ls-remote #{fetch(:repo_url)} | grep -P '^.{40}\t.*#{fetch(:branch)}$'"

Then, count results lines (| wc -l), getting stdout in a variable instead of showing it and test the variable's value:

  • "0" = error
  • "1" = OK
  • More than "1" = error message like "Branch name is ambiguous, please type exact ref (ie refs/heads/fooinstead of foo)"

So the last 3 lines will have no chances to be produced and the archive will always be the good one.

I am a noob in ruby language, but if you have no time to fix it, I can try to find the code for that and then send you a pull request.

Thanks.