Skip to content

Commit d3b3462

Browse files
peffgitster
authored andcommitted
clone: leave repo in place after checkout errors
If we manage to clone a remote repository but run into an error in the checkout, it is probably sane to leave the repo directory in place. That lets the user examine the situation without spending time to re-clone from the remote (which may be a lengthy process). Rather than try to convert each die() from the checkout code path into an error(), we simply set a flag that tells the "remove_junk" atexit function to print a helpful message and leave the repo in place. Note that the test added in this patch actually passes without the code change. The reason is that the cleanup code is buggy; we chdir into the working tree for the checkout, but still may use relative paths to remove the directories (which means if you cloned into "foo", we would accidentally remove "foo" from the working tree!). There's no point in fixing it now, since this patch means we will never try to remove anything after the chdir, anyway. [jc: replaced the message with a more succinct version from Jonathan] Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0433ad1 commit d3b3462

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

builtin/clone.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,32 @@ static void clone_local(const char *src_repo, const char *dest_repo)
377377
static const char *junk_work_tree;
378378
static const char *junk_git_dir;
379379
static pid_t junk_pid;
380+
enum {
381+
JUNK_LEAVE_NONE,
382+
JUNK_LEAVE_REPO,
383+
JUNK_LEAVE_ALL
384+
} junk_mode = JUNK_LEAVE_NONE;
385+
386+
static const char junk_leave_repo_msg[] =
387+
N_("Clone succeeded, but checkout failed.\n"
388+
"You can inspect what was checked out with 'git status'\n"
389+
"and retry the checkout with 'git checkout -f HEAD'\n");
380390

381391
static void remove_junk(void)
382392
{
383393
struct strbuf sb = STRBUF_INIT;
394+
395+
switch (junk_mode) {
396+
case JUNK_LEAVE_REPO:
397+
warning("%s", _(junk_leave_repo_msg));
398+
/* fall-through */
399+
case JUNK_LEAVE_ALL:
400+
return;
401+
default:
402+
/* proceed to removal */
403+
break;
404+
}
405+
384406
if (getpid() != junk_pid)
385407
return;
386408
if (junk_git_dir) {
@@ -925,12 +947,13 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
925947
transport_unlock_pack(transport);
926948
transport_disconnect(transport);
927949

950+
junk_mode = JUNK_LEAVE_REPO;
928951
err = checkout();
929952

930953
strbuf_release(&reflog_msg);
931954
strbuf_release(&branch_top);
932955
strbuf_release(&key);
933956
strbuf_release(&value);
934-
junk_pid = 0;
957+
junk_mode = JUNK_LEAVE_ALL;
935958
return err;
936959
}

t/t1060-object-corruption.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ test_expect_success 'clone --local detects corruption' '
8989
test_must_fail git clone --local bit-error corrupt-checkout
9090
'
9191

92+
test_expect_success 'error detected during checkout leaves repo intact' '
93+
test_path_is_dir corrupt-checkout/.git
94+
'
95+
9296
test_expect_success 'clone --local detects missing objects' '
9397
test_must_fail git clone --local missing missing-checkout
9498
'

0 commit comments

Comments
 (0)