Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> --- builtin/checkout.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 95a1a61..6353557 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -20,6 +20,7 @@ #include "resolve-undo.h" #include "submodule.h" #include "argv-array.h" +#include "sigchain.h" static const char * const checkout_usage[] = { N_("git checkout [options] <branch>"), @@ -826,6 +827,35 @@ static void write_to_file(const char *path, const char *fmt, ...) fclose(fp); } +static const char *junk_work_tree; +static const char *junk_git_dir; +static int is_junk; +static pid_t junk_pid; + +static void remove_junk(void) +{ + struct strbuf sb = STRBUF_INIT; + if (!is_junk || getpid() != junk_pid) + return; + if (junk_git_dir) { + strbuf_addstr(&sb, junk_git_dir); + remove_dir_recursively(&sb, 0); + strbuf_reset(&sb); + } + if (junk_work_tree) { + strbuf_addstr(&sb, junk_work_tree); + remove_dir_recursively(&sb, 0); + strbuf_reset(&sb); + } +} + +static void remove_junk_on_signal(int signo) +{ + remove_junk(); + sigchain_pop(signo); + raise(signo); +} + static int checkout_new_worktree(const struct checkout_opts *opts, struct branch_info *new) { @@ -835,7 +865,7 @@ static int checkout_new_worktree(const struct checkout_opts *opts, struct stat st; const char *name; struct child_process cp; - int counter = 0, len; + int counter = 0, len, ret; if (!new->commit) die(_("no branch specified")); @@ -860,13 +890,21 @@ static int checkout_new_worktree(const struct checkout_opts *opts, strbuf_addf(&sb_repo, "%d", counter); } name = sb_repo.buf + len - strlen(name); + + junk_pid = getpid(); + atexit(remove_junk); + sigchain_push_common(remove_junk_on_signal); + if (mkdir(sb_repo.buf, 0777)) die_errno(_("could not create directory of '%s'"), sb_repo.buf); + junk_git_dir = sb_repo.buf; + is_junk = 1; strbuf_addf(&sb_git, "%s/.git", path); if (safe_create_leading_directories_const(sb_git.buf)) die_errno(_("could not create leading directories of '%s'"), sb_git.buf); + junk_work_tree = path; write_to_file(sb_git.buf, "gitsuper: %s\ngitdir: %s\n", real_path(get_git_dir()), name); @@ -887,7 +925,14 @@ static int checkout_new_worktree(const struct checkout_opts *opts, memset(&cp, 0, sizeof(cp)); cp.git_cmd = 1; cp.argv = opts->saved_argv; - return run_command(&cp); + ret = run_command(&cp); + if (!ret) + is_junk = 0; + strbuf_release(&sb); + strbuf_release(&sb_repo); + strbuf_release(&sb_git); + return ret; + } static int git_checkout_config(const char *var, const char *value, void *cb) -- 1.8.5.1.77.g42c48fa -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html