Merge branch 'ab/ref-filter-no-contains' into maint
[gitweb.git] / builtin / worktree.c
index 57caa0855e0e8078d6f100f41cab76aac7784887..ff5dfd2b1022fd94314f2cefe3ca91a4820e8abf 100644 (file)
@@ -24,6 +24,7 @@ struct add_opts {
        int force;
        int detach;
        int checkout;
+       int keep_locked;
        const char *new_branch;
        int force_new_branch;
 };
@@ -240,7 +241,10 @@ static int add_worktree(const char *path, const char *refname,
         * after the preparation is over.
         */
        strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-       write_file(sb.buf, "initializing");
+       if (!opts->keep_locked)
+               write_file(sb.buf, "initializing");
+       else
+               write_file(sb.buf, "added with --lock");
 
        strbuf_addf(&sb_git, "%s/.git", path);
        if (safe_create_leading_directories_const(sb_git.buf))
@@ -301,9 +305,11 @@ static int add_worktree(const char *path, const char *refname,
        junk_git_dir = NULL;
 
 done:
-       strbuf_reset(&sb);
-       strbuf_addf(&sb, "%s/locked", sb_repo.buf);
-       unlink_or_warn(sb.buf);
+       if (ret || !opts->keep_locked) {
+               strbuf_reset(&sb);
+               strbuf_addf(&sb, "%s/locked", sb_repo.buf);
+               unlink_or_warn(sb.buf);
+       }
        argv_array_clear(&child_env);
        strbuf_release(&sb);
        strbuf_release(&symref);
@@ -326,6 +332,7 @@ static int add(int ac, const char **av, const char *prefix)
                           N_("create or reset a branch")),
                OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
                OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
+               OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
                OPT_END()
        };
 
@@ -407,9 +414,11 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
                                find_unique_abbrev(wt->head_sha1, DEFAULT_ABBREV));
                if (wt->is_detached)
                        strbuf_addstr(&sb, "(detached HEAD)");
-               else if (wt->head_ref)
-                       strbuf_addf(&sb, "[%s]", shorten_unambiguous_ref(wt->head_ref, 0));
-               else
+               else if (wt->head_ref) {
+                       char *ref = shorten_unambiguous_ref(wt->head_ref, 0);
+                       strbuf_addf(&sb, "[%s]", ref);
+                       free(ref);
+               } else
                        strbuf_addstr(&sb, "(error)");
        }
        printf("%s\n", sb.buf);