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

classic Classic list List threaded Threaded
5 messages Options
sberg sberg
Reply | Threaded
Open this post in threaded view
|

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

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?
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
Jan-Marek Glogowski Jan-Marek Glogowski
Reply | Threaded
Open this post in threaded view
|

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

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?}"

Just remember that at this point the branch is "fake".

HTH

Jan-Marek
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
sberg sberg
Reply | Threaded
Open this post in threaded view
|

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

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.
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice
Miklos Vajna-4 Miklos Vajna-4
Reply | Threaded
Open this post in threaded view
|

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

Hi,

On Fri, Sep 15, 2017 at 06:33:17PM +0200, Stephan Bergmann <[hidden email]> wrote:
> >if [ ... ]; then
> >   git fetch --prune --tags origin
> >   # cleanup all patched stuff - probably not needed
> >   git reset --hard
> >   git checkout --detached "origin/${my_gitbranch?}"

Whether the above git fetch will put origin's master branch to
origin/master or master (the later case is what you call "tags and
branches behave the same way") depends on the refspec of the remote
named "origin", which is configured when the "origin" remote is created.

Depending on 'git remote add' was invoked with the --mirror=fetch
option (either directly or via 'git clone --mirror') .git/config
contains something like:

[remote "origin"]
        fetch = +refs/heads/*:refs/remotes/origin/*

(normal case, master is available as origin/master)

or

[remote "origin"]
        fetch = +refs/*:refs/*

(mirror case)

So if the refspec is set up correctly, you should be able to
transparently use tags/branches the same way.

(Normally it's valuable that origin's master is written to
origin/master, so your local master branch does not change when you do
'git fetch', that's why --mirror=fetch is not the default.)

Regards,

Miklos

_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice

signature.asc (188 bytes) Download Attachment
sberg sberg
Reply | Threaded
Open this post in threaded view
|

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

On 09/18/2017 11:20 AM, Miklos Vajna wrote:
> Depending on 'git remote add' was invoked with the --mirror=fetch
> option (either directly or via 'git clone --mirror') .git/config
> contains something like:

Excellent.  Thanks!
_______________________________________________
LibreOffice mailing list
[hidden email]
https://lists.freedesktop.org/mailman/listinfo/libreoffice