Making git-clone clean up when it fails
So instead of just making noise, I'll start actually contributing to git. The first patch here is designed to fix the annoying bug that when git-clone fails it still leaves the created directory around, (so that a subsequent git-clone will be guaranteed to fail since the directory already exists).
The second and third patches provide a new test case for the new behavior. I had to guess at the number for the new test, since the distinction of the various numbers in t/README was too vague for me to know where a git-clone test should go. Feel free to adjust that as you might see fit.
-Carl
Signature · 1 line
[email protected]
PATCH 1/3 Trap exit to clean up created directory if clone fails.
---
git-clone.sh | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
589b68168e3dad89238b879b06680c662a99ad8d
git-clone.sh +4 -0
@@ -118,6 +118,7 @@ dir="$2" [ -e "$dir" ] && echo "$dir already exists." && usage mkdir -p "$dir" && D=$(cd "$dir" && pwd) && +trap 'err=$?; rm -r $D; exit $err' exit case "$bare" in yes) GIT_DIR="$D" ;; *) GIT_DIR="$D/.git" ;;
@@ -255,3 +256,6 @@ Pull: $head_points_at:$origin" && git checkout esac fi + +trap - exit +
Signature · 1 line
1.2.0.gf6e8
PATCH 2/3 Abstract test_create_repo out for use in tests.
---
t/test-lib.sh | 23 +++++++++++++++++------ 1 files changed, 17 insertions(+), 6 deletions(-)
6110a439c2b1962c6f128cdaac3a2ee8450ac22c
t/test-lib.sh +17 -6
@@ -149,6 +149,21 @@ test_expect_code () { fi } +# Most tests can use the created repository, but some amy need to create more. +# Usage: test_create_repo <directory> +test_create_repo () { + test "$#" = 1 || + error "bug in the test script: not 1 parameter to test-create-repo" + owd=`pwd` + repo="$1" + mkdir "$repo" + cd "$repo" || error "Cannot setup test environment" + "$GIT_EXEC_PATH/git" init-db --template=$GIT_EXEC_PATH/templates/blt/ 2>/dev/null || + error "cannot run git init-db -- have you built things yet?" + mv .git/hooks .git/hooks-disabled + cd "$owd" +} + test_done () { trap - exit case "$test_failure" in
@@ -196,9 +211,5 @@ test -d ../templates/blt || { # Test repository test=trash rm -fr "$test" -mkdir "$test" -cd "$test" || error "Cannot setup test environment" -"$GIT_EXEC_PATH/git" init-db --template=../../templates/blt/ 2>/dev/null || -error "cannot run git init-db -- have you built things yet?" - -mv .git/hooks .git/hooks-disabled +test_create_repo $test +cd "$test"
Signature · 1 line
1.2.0.gf6e8
PATCH 3/3 New test to verify that when git-clone fails it cleans up the new directory.
---
t/t5600-clone-fail-cleanup.sh | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) create mode 100755 t/t5600-clone-fail-cleanup.sh
0d8852c105e5f910ba53fc974a5d929a2136aecd
t/t5600-clone-fail-cleanup.sh new file +36 -0
@@ -0,0 +1,36 @@ +#!/bin/sh +# +# Copyright (C) 2006 Carl D. Worth <[email protected]> +# + +test_description='test git-clone to cleanup after failure + +This test covers the fact that if git-clone fails, it should remove +the directory it created, to avoid the user having to manually +remove the directory before attempting a clone again.' + +. ./test-lib.sh + +test_expect_failure \ + 'clone of non-existent source should fail' \ + 'git-clone foo bar' + +test_expect_failure \ + 'failed clone should not leave a directory' \ + 'cd bar' + +# Need a repo to clone +test_create_repo foo + +# clone doesn't like it if there is no HEAD. Is that a bug? +(cd foo && touch file && git add file && git commit -m 'add file' >/dev/null 2>&1) + +test_expect_success \ + 'clone should work now that source exists' \ + 'git-clone foo bar' + +test_expect_success \ + 'successfull clone must leave the directory' \ + 'cd bar' + +test_done
Signature · 1 line
1.2.0.gf6e8