Skip to content

Commit a5ee1b1

Browse files
authored
Merge pull request #413 from barney-s/412
Check for rev to be present after a fetch
2 parents 9cd1e24 + 6baccb4 commit a5ee1b1

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

cmd/git-sync/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@ func addWorktreeAndSwap(ctx context.Context, gitRoot, dest, branch, rev string,
700700
return err
701701
}
702702

703+
// With shallow fetches, it's possible to race with the upstream repo and
704+
// end up NOT fetching the hash we wanted. If we can't resolve that hash
705+
// to a commit we can just end early and leave it for the next sync period.
706+
if _, err := revIsHash(ctx, hash, gitRoot); err != nil {
707+
log.Error(err, "can't resolve commit, will retry", "rev", rev, "hash", hash)
708+
return nil
709+
}
710+
703711
// GC clone
704712
if _, err := runCommand(ctx, gitRoot, *flGitCmd, "gc", "--prune=all"); err != nil {
705713
return err
File renamed without changes.

slow_git_fetch.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
#
3+
# Copyright 2020 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
18+
if [ "$1" != "fetch" ]; then
19+
git "$@"
20+
exit $?
21+
fi
22+
23+
sleep 5
24+
git "$@"

test_e2e.sh

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ function finish() {
161161
}
162162
trap finish INT EXIT
163163

164-
SLOW_GIT=/slow_git.sh
164+
SLOW_GIT_CLONE=/slow_git_clone.sh
165+
SLOW_GIT_FETCH=/slow_git_fetch.sh
165166
ASKPASS_GIT=/askpass_git.sh
166167
SYNC_HOOK_COMMAND=/test_sync_hook_command.sh
167168

@@ -174,7 +175,8 @@ function GIT_SYNC() {
174175
--network="host" \
175176
-u $(id -u):$(id -g) \
176177
-v "$DIR":"$DIR":rw \
177-
-v "$(pwd)/slow_git.sh":"$SLOW_GIT":ro \
178+
-v "$(pwd)/slow_git_clone.sh":"$SLOW_GIT_CLONE":ro \
179+
-v "$(pwd)/slow_git_fetch.sh":"$SLOW_GIT_FETCH":ro \
178180
-v "$(pwd)/askpass_git.sh":"$ASKPASS_GIT":ro \
179181
-v "$(pwd)/test_sync_hook_command.sh":"$SYNC_HOOK_COMMAND":ro \
180182
-v "$DOT_SSH/id_test":"/etc/git-secret/ssh":ro \
@@ -609,7 +611,7 @@ testcase "sync-loop-timeout"
609611
echo "$TESTCASE 1" > "$REPO"/file
610612
git -C "$REPO" commit -qam "$TESTCASE 1"
611613
GIT_SYNC \
612-
--git="$SLOW_GIT" \
614+
--git="$SLOW_GIT_CLONE" \
613615
--one-time \
614616
--timeout=1 \
615617
--repo="file://$REPO" \
@@ -621,7 +623,7 @@ GIT_SYNC \
621623
assert_file_absent "$ROOT"/link/file
622624
# run with slow_git but without timing out
623625
GIT_SYNC \
624-
--git="$SLOW_GIT" \
626+
--git="$SLOW_GIT_CLONE" \
625627
--wait=0.1 \
626628
--timeout=16 \
627629
--repo="file://$REPO" \
@@ -691,6 +693,48 @@ fi
691693
# Wrap up
692694
pass
693695

696+
##############################################
697+
# Test fetch skipping commit
698+
##############################################
699+
testcase "fetch-skip-depth-1"
700+
echo "$TESTCASE" > "$REPO"/file
701+
git -C "$REPO" commit -qam "$TESTCASE"
702+
GIT_SYNC \
703+
--git="$SLOW_GIT_FETCH" \
704+
--wait=0.1 \
705+
--depth=1 \
706+
--repo="file://$REPO" \
707+
--branch=e2e-branch \
708+
--rev=HEAD \
709+
--root="$ROOT" \
710+
--dest="link" \
711+
> "$DIR"/log."$TESTCASE" 2>&1 &
712+
713+
# wait for first sync which does a clone followed by an artifically slowed fetch
714+
sleep 8
715+
assert_link_exists "$ROOT"/link
716+
assert_file_exists "$ROOT"/link/file
717+
assert_file_eq "$ROOT"/link/file "$TESTCASE"
718+
719+
# make a second commit to trigger a sync with shallow fetch
720+
echo "$TESTCASE-ok" > "$REPO"/file2
721+
git -C "$REPO" add file2
722+
git -C "$REPO" commit -qam "$TESTCASE new file"
723+
724+
# Give time for ls-remote to detect the commit and slowed fetch to start
725+
sleep 2
726+
727+
# make a third commit to insert the commit between ls-remote and fetch
728+
echo "$TESTCASE-ok" > "$REPO"/file3
729+
git -C "$REPO" add file3
730+
git -C "$REPO" commit -qam "$TESTCASE third file"
731+
sleep 10
732+
assert_link_exists "$ROOT"/link
733+
assert_file_exists "$ROOT"/link/file3
734+
assert_file_eq "$ROOT"/link/file3 "$TESTCASE-ok"
735+
736+
pass
737+
694738
##############################################
695739
# Test password
696740
##############################################
@@ -945,7 +989,7 @@ BINDPORT=8888
945989
echo "$TESTCASE 1" > "$REPO"/file
946990
git -C "$REPO" commit -qam "$TESTCASE 1"
947991
GIT_SYNC \
948-
--git="$SLOW_GIT" \
992+
--git="$SLOW_GIT_CLONE" \
949993
--repo="file://$REPO" \
950994
--branch=e2e-branch \
951995
--root="$ROOT" \

0 commit comments

Comments
 (0)