-
Notifications
You must be signed in to change notification settings - Fork 64
Description
My code does need the git repository but only needs the current commit. So a clone_depth of 1 works. However, I noticed in my build output that these commands are issued during the clone:
git clone -q --depth=1 --branch=appveyor-testing https://bitbucket.org/«redacted»/main.git C:\projects\main
git checkout -qf 26daa5ae6672c2a1ce97f0cef8ff9e0a7dce83bd
I realized that this meant that the checkout step would fail if multiple pushes were performed to the same branch at once. As I searched around, I saw this mentioned in https://www.appveyor.com/blog/2014/06/04/shallow-clone-for-git-repositories/ :
Note: Be aware that if you do a lot of commits producing queued builds and depth number is too small, git checkout operation following git clone can fail because the requested commit is not present in a cloned repository.
It surprised me that git works this way because it seems you should be able to just directly clone a revision. However, git doesn’t make it that simple. Apparently git didn’t even support this until 2.5.0. And it is not straight forward, but as mentioned at https://stackoverflow.com/a/3489576, modern versions of git have solved this issue. I experimented and came up with the following commands which seem to do the right thing for this scenario:
git -c advice.detachedHead=false clone -q --depth=1 --revision=26daa5ae6672c2a1ce97f0cef8ff9e0a7dce83bd https://bitbucket.org/«redacted»/main.git C:\projects\main
git remote set-branches --add origin appveyor-testing
git update-ref refs/remotes/origin/appveyor-testing HEAD
git switch -q appveyor-testing
This leaves you at the specified commit with the fetch upstream set correctly and the current branch set correctly (which are important side-effects of the git clone -b that should be preserved). This should do the correct thing without relying on guesswork to find a good value for clone_depth which might not account for high queue lengths during peak usage and without wasting the bandwidth and time of downloading unused commits.