[PATCH] gen-version.sh: check if git is available before trying to call it

Fabien C. t74jgwb88tli9ch at jetable.org
Sat Feb 1 16:10:44 CET 2014


On 01/02/2014 15:06, Peter Wu wrote:
> On Saturday 01 February 2014 13:56:46 John Keeping wrote:
>>> diff --git a/gen-version.sh b/gen-version.sh
>>> index 3a08015..13ff979 100755
>>> --- a/gen-version.sh
>>> +++ b/gen-version.sh
>>> @@ -4,7 +4,7 @@
>>>
>>>  V=$1
>>>  
>>>  # Use `git describe` to get current version if we're inside a git repo
>>>
>>> -if test -d .git
>>> +if test -d .git && command -v git > /dev/null
>>
>> Style: no space between redirect and file: >/dev/null
>>
>> I'm not sure command is the most portable way to achieve this, how about
>> this instead:
>>
>>     git --version >/dev/null 2>&1

"command -v" is POSIX compliant [1] but your version is nice, maybe nicer because POSIX is not that universal. Up to you. 

> If you are testing for git anyway, what about testing the result rather than 
> the command:
> 
>     gitver=$(git describe --abbrev=4 HEAD 2>/dev/null)
>     [ -z "$gitver" ] || V=$gitver
> 
>>>  then
>>>       V=$(git describe --abbrev=4 HEAD 2>/dev/null)
>>>  fi

Not so nice I guess because git might find a .git folder in some parent directory and produce a (wrong) result. 

Here is yet another version (+ patch file): 

if test "$(git rev-parse --git-dir 2>/dev/null)" = '.git'
then
        V=$(git describe --abbrev=4 HEAD 2>/dev/null)
fi

I think this one solves the above problems. 

Regards, 
Fabien 

[1]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-gen-version.sh-check-if-git-is-available-before-tryi.patch
Type: text/x-patch
Size: 910 bytes
Desc: not available
URL: <http://lists.zx2c4.com/pipermail/cgit/attachments/20140201/6c49bd69/attachment.bin>


More information about the CGit mailing list