Clean up failed clones

If an initial clone crashes, it can leave the git-root in a bad state
such that git can't retry the clone.  This change forces it to clean up
the mess and retry.
This commit is contained in:
Tim Hockin 2018-08-20 16:07:04 -07:00
parent 2e46b74cf8
commit 296093b9df
2 changed files with 49 additions and 2 deletions

View File

@ -335,9 +335,22 @@ func cloneRepo(repo, branch, rev string, depth int, gitRoot string) error {
}
args = append(args, repo, gitRoot)
_, err := runCommand("", "git", args...)
if err != nil {
if strings.Contains(err.Error(), "already exists and is not an empty directory") {
// Maybe a previous run crashed? Git won't use this dir.
log.V(0).Infof("%s exists and is not empty (previous crash?), cleaning up", gitRoot)
err := os.RemoveAll(gitRoot)
if err != nil {
return err
}
_, err = runCommand("", "git", args...)
if err != nil {
return err
}
} else {
return err
}
}
log.V(0).Infof("cloned %s", repo)
return nil

View File

@ -89,7 +89,8 @@ function GIT_SYNC() {
function remove_sync_container() {
# Verify the container is running using 'docker top' before removing
docker top $CONTAINER_NAME >/dev/null 2>&1 && docker rm -f $CONTAINER_NAME
docker top $CONTAINER_NAME >/dev/null 2>&1 && \
docker rm -f $CONTAINER_NAME >/dev/null 2>&1
}
REPO="$DIR/repo"
@ -408,5 +409,38 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass
# Test syncing after a crash
testcase "crash-cleanup-retry"
# First sync
echo "$TESTCASE 1" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE 1"
GIT_SYNC \
--logtostderr \
--v=5 \
--one-time \
--repo="$REPO" \
--root="$ROOT" \
--dest="link" > "$DIR"/log."$TESTCASE" 2>&1 &
sleep 3
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
# Corrupt it
rm -f "$ROOT"/link
# Try again
GIT_SYNC \
--logtostderr \
--v=5 \
--one-time \
--repo="$REPO" \
--root="$ROOT" \
--dest="link" > "$DIR"/log."$TESTCASE" 2>&1 &
sleep 3
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE 1"
# Wrap up
pass
echo "cleaning up $DIR"
rm -rf "$DIR"