kubernetes / git-sync

A sidecar app which clones a git repo and keeps it in sync with the upstream.
Apache License 2.0
2.14k stars 409 forks source link

Simplify inner loop: just fetch $ref #845

Closed thockin closed 7 months ago

thockin commented 7 months ago

Fixes #844

Old way:

New way:

Side-effect: fixed a bug where the remote had multiple refs with the same name, for example "main" could resolve to:

244999b795d4a7890f237ef3c8035d68ad56515d    refs/heads/main
be2c0aec052e300028d9c6d919787624290505b6    refs/remotes/upstream/main
k8s-ci-robot commented 7 months ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: thockin

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes/git-sync/blob/master/OWNERS)~~ [thockin] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
nan-yu commented 4 months ago

Not only does it simplifies the inner loop, but it also coincidentally fixes an issue with fetching the remote SHA. It may be worth mentioning in the release note.

Here is an example:

In v4.1.0, it gets the remote SHA with git ls-remote $ref $ref^{} and parses the last line. The remote SHA is be2c0aec052e300028d9c6d919787624290505b6.

$ git ls-remote -q https://github.com/janetkuo/anthos-config-management-samples main  main^{}
244999b795d4a7890f237ef3c8035d68ad56515d    refs/heads/main
be2c0aec052e300028d9c6d919787624290505b6    refs/remotes/upstream/main

In v4.2.0, it gets the remote SHA with git fetch $ref, and the remote SHA is 244999b795d4a7890f237ef3c8035d68ad56515d.

$ git fetch https://github.com/janetkuo/anthos-config-management-samples main --verbose --no-progress --prune --no-auto-gc --depth 1
POST git-upload-pack (326 bytes)
POST git-upload-pack (991 bytes)
POST git-upload-pack (gzip 1791 to 952 bytes)
POST git-upload-pack (gzip 3391 to 1759 bytes)
POST git-upload-pack (gzip 6591 to 3357 bytes)
POST git-upload-pack (gzip 12991 to 6566 bytes)
POST git-upload-pack (gzip 25791 to 12989 bytes)
POST git-upload-pack (gzip 51391 to 25936 bytes)
POST git-upload-pack (gzip 6341 to 3235 bytes)
POST git-upload-pack (200 bytes)
From https://github.com/janetkuo/anthos-config-management-samples
 * branch                main       -> FETCH_HEAD
 * 

$ git rev-parse FETCH_HEAD^{}
244999b795d4a7890f237ef3c8035d68ad56515d
thockin commented 4 months ago

That's odd. It looks like 244999b795d4a7890f237ef3c8035d68ad56515d is a merge commit and be2c0aec052e300028d9c6d919787624290505b6 is a regular commit BUT NOT the immediately previous commit!

In fact, the ^{} does nothing:

$ git ls-remote -q https://github.com/janetkuo/anthos-config-management-samples main
244999b795d4a7890f237ef3c8035d68ad56515d    refs/heads/main
be2c0aec052e300028d9c6d919787624290505b6    refs/remotes/upstream/main

$ git ls-remote -q https://github.com/janetkuo/anthos-config-management-samples main^{}
# no output

It looks like git is being oh so helpful and returning all the refs named "main", not just the branch. It seems I should have been passing -h -t to avoid this. I guess it is not needed now. :)

Thanks for the info!