Reputation: 71
Im not sure if this is a bash issue or git issue but I am completely stumped.
I have the following bash script:
#!/bin/bash
FILE=testfile.c
sha1=`git log --pretty=oneline --branches -- $FILE | tail -1 | cut -f 1 -d' '`
sha2=044ec18c3c4efe9ef44daf7479b9f71d89720690
echo "sha1=.$sha1."
echo "sha2=.$sha2."
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch '"$FILE"'' -- ${sha1}^..
Which fails with the following output:
$> ./script.sh
sha1=.044ec18c3c4efe9ef44daf7479b9f71d89720690.
sha2=.044ec18c3c4efe9ef44daf7479b9f71d89720690.
fatal: ambiguous argument '044ec18c3c4efe9ef44daf7479b9f71d89720690^..': unknown
revision or path not in the working tree.
Use '--' to separate paths from revisions
But if I simply change the script by using sha2
instead of sha1
on the git filter-branch
line, it works and generates the following output:
$> ./script.sh
sha1=.044ec18c3c4efe9ef44daf7479b9f71d89720690.
sha2=.044ec18c3c4efe9ef44daf7479b9f71d89720690.
Rewrite 044ec18c3c4efe9ef44daf7479b9f71d89720690 (1/1139)rm 'talairach_avi/SVIP_Child_Comp_N24_as_orig.4dfp.img'
Rewrite d30e314d7b6e2f62fa26e62dc83fc5083b92085b (2/1139)rm 'talairach_avi/SVIP_Child_Comp_N24_as_orig.4dfp.img'
Rewrite f2897cd0d17b75de70aca1baa70e6cb40243d0a1 (3/1139)rm 'talairach_avi/SVIP_Child_Comp_N24_as_orig.4dfp.img'
...
sha1
and sha2
have the exact same value. Why does using the hard-coded sha2
variable work, but the assigned variable of sha1
not work? How can I get it to work with sha1
? Ive tried every possible permutation/combination of single and double quotes to no avail.
I would very greatly appreciate any input on the matter. Thanks.
EDIT1:
Although the echo statements alone appear identical, piping the output of echo through od
produces different output:
$> echo $sha1 | od -x
0000000 5b1b 3333 306d 3434 6365 3831 3363 3463
0000020 6665 3965 6665 3434 6164 3766 3734 6239
0000040 6639 3137 3864 3739 3032 3936 1b30 6d5b
0000060 000a
0000061
$> eco $sha2 | od -x
0000000 3430 6534 3163 6338 6333 6534 6566 6539
0000020 3466 6434 6661 3437 3937 3962 3766 6431
0000040 3938 3237 3630 3039 000a
0000051
EDIT2: SOLVED As can be seen by the comments below, the issue was solved with git configuration for coloured output. Unsetting this configuration solved the issue. git config --global color.ui false
Upvotes: 3
Views: 826
Reputation: 488193
The problem turned out to be color strings (%C(yellow)
and a corresponding %C(reset)
, more or less) getting forced into the git log
output. This seems odd since color.ui
is supposed to automatically switch off when piping output.
In any case, though, to get just the desired commit SHA1, you could sidestep the entire issue by using git rev-list
rather than git log --pretty=oneline
and piping through commands to extract the SHA-1s. That is, instead of:
git log --pretty=oneline --branches -- $FILE | tail -1 | cut -f 1 -d' '
you can use the simpler:
git rev-list --branches -- $FILE | tail -1
(unfortunately, --reverse -n 1
to eliminate the tail
step fails: git rev-list
generates the list, limits it to one commit, then reverses it, which just gets you the first commit instead of the last).
Upvotes: 2