Git help needed: update local repo to specific branch or tag

Stephan Bergmann sbergman at redhat.com
Fri Sep 15 16:33:17 UTC 2017


On 09/15/2017 04:20 PM, Jan-Marek Glogowski wrote:
> Am 15.09.2017 um 08:54 schrieb Stephan Bergmann:
>> At
>> <https://gerrit.libreoffice.org/gitweb?p=dev-tools.git;a=blob;f=flatpak/build.sh;h=fab02d7f71f38c1f30174a534627db2bcc211ebd;hb=HEAD#l50>,
>>
>>
>>> if [ -e "${my_dir?}"/lo ]; then
>>>   git -C "${my_dir?}"/lo fetch --tags
>>>   git -C "${my_dir?}"/lo checkout "${my_gitbranch?}"
>>> else
>>>   git clone --branch "${my_gitbranch?}"
>>> git://gerrit.libreoffice.org/core \
>>>    "${my_dir?}"/lo
>>> fi
>>
>> I want to get a local checkout of $my_gitbranch (which will either
>> denote a branch like libreoffice-5-4 or a tag like libreoffice-5.4.1.2)
>> of our LO repo.  The "else" case is straightforward.
>>
>> But if I already have the repo checked out from a previous run of the
>> script (with a potentially different value for $my_gitbranch), I don't
>> want to clone again, but re-use the existing repo and just update it
>> accordingly (the "then" branch).  The sequence of git commands works
>> fine if $my_gitbranch is a tag.
>>
>> However, if $my_gitbranch is a branch, and the existing repo happens to
>> already have that branch checked out, then the 'git checkout' will
>> report that I'm behind the remote repo by N commits and should
>> fast-forward.
>>
>> So adding a 'git pull' after the 'git checkout' would help the case
>> where $my_gitbranch is a branch.  But when it is a tag, such a 'git
>> pull' would fail, stating that I'm not on a branch.
>>
>> Is there a magic git incantation that does what I want?
> 
> Use detached heads and don't use local branches at all. Untested, but
> should work:
> 
> if [ ... ]; then
>    git fetch --prune --tags origin
>    # cleanup all patched stuff - probably not needed
>    git reset --hard
>    git checkout --detached "origin/${my_gitbranch?}"
> else
>    git clone --no-checkout git://gerrit.libreoffice.org/core
>    git checkout --detached "origin/${my_gitbranch?}"
> fi
> 
> Alternatively to checkouts you can just reset your local branch to your
> preferred state using:
> 
> git reset --hard "origin/${my_gitbranch?}"

The problem with both those approaches is that branches must be written 
as "origin/$my_gitbranch" while tags must be written as just 
"$my_gitbranch".  (Whereas the original 'git clone --branch' and 'git 
checkout' approach is also happy with branches written as just 
"$my_gitbranch".)

But yeah, one way to fix that would be to require that branches are 
actually passed as "origin/libreoffice-5-4" to the script.  But then, 
flatpak-builder, called further down in the script, is unhappy with 
$my_gitbranch containing "origin/libreoffice-5-4" instead of 
"libreoffice-5-4".

But thanks anyway; if nobody comes up with a better solution, I'd 
probably hack something up based on this.


More information about the LibreOffice mailing list