Skip to content

Commit 2c27002

Browse files
tgummerergitster
authored andcommitted
worktree: improve message when creating a new worktree
Currently 'git worktree add' produces output like the following: Preparing ../foo (identifier foo) HEAD is now at 26da330922 <title> The '../foo' is the path where the worktree is created, which the user has just given on the command line. The identifier is an internal implementation detail, which is not particularly relevant for the user and indeed isn't mentioned explicitly anywhere in the man page. Instead of this message, print a message that gives the user a bit more detail of what exactly 'git worktree' is doing. There are various dwim modes which perform some magic under the hood, which should be helpful to users. Just from the output of the command it is not always visible to users what exactly has happened. Help the users a bit more by modifying the "Preparing ..." message and adding some additional information of what 'git worktree add' did under the hood, while not displaying the identifier anymore. Currently there are several different cases: - 'git worktree add -b ...' or 'git worktree add <path>', both of which create a new branch, either through the user explicitly requesting it, or through 'git worktree add' implicitly creating it. This will end up with the following output: Preparing worktree (new branch '<branch>') HEAD is now at 26da330922 <title> - 'git worktree add -B ...', which may either create a new branch if the branch with the given name does not exist yet, or resets an existing branch to the current HEAD, or the commit-ish given. Depending on which action is taken, we'll end up with the following output: Preparing worktree (resetting branch '<branch>'; was at caa68db) HEAD is now at 26da330922 <title> or: Preparing worktree (new branch '<branch>') HEAD is now at 26da330922 <title> - 'git worktree add --detach' or 'git worktree add <path> <commit-ish>', both of which create a new worktree with a detached HEAD, for which we will print the following output: Preparing worktree (detached HEAD 26da330922) HEAD is now at 26da330922 <title> - 'git worktree add <path> <local-branch>', which checks out the branch and prints the following output: Preparing worktree (checking out '<local-branch>') HEAD is now at 47007d5 <title> Additionally currently the "Preparing ..." line is printed to stderr, while the "HEAD is now at ..." line is printed to stdout by 'git reset --hard', which is used internally by 'git worktree add'. Fix this inconsistency by printing the "Preparing ..." message to stdout as well. As "Preparing ..." is not an error, stdout also seems like the more appropriate output stream. Helped-by: Eric Sunshine <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Reviewed-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d861d34 commit 2c27002

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

builtin/worktree.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ static int add_worktree(const char *path, const char *refname,
301301
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
302302
write_file(sb.buf, "../..");
303303

304-
fprintf_ln(stderr, _("Preparing %s (identifier %s)"), path, name);
305-
306304
argv_array_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
307305
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
308306
cp.git_cmd = 1;
@@ -355,6 +353,40 @@ static int add_worktree(const char *path, const char *refname,
355353
return ret;
356354
}
357355

356+
static void print_preparing_worktree_line(int detach,
357+
const char *branch,
358+
const char *new_branch,
359+
int force_new_branch)
360+
{
361+
if (force_new_branch) {
362+
struct commit *commit = lookup_commit_reference_by_name(new_branch);
363+
if (!commit)
364+
printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
365+
else
366+
printf_ln(_("Preparing worktree (resetting branch '%s'; was at %s)"),
367+
new_branch,
368+
find_unique_abbrev(commit->object.oid.hash,
369+
DEFAULT_ABBREV));
370+
} else if (new_branch) {
371+
printf_ln(_("Preparing worktree (new branch '%s')"), new_branch);
372+
} else {
373+
struct strbuf s = STRBUF_INIT;
374+
if (!detach && !strbuf_check_branch_ref(&s, branch) &&
375+
ref_exists(s.buf))
376+
printf_ln(_("Preparing worktree (checking out '%s')"),
377+
branch);
378+
else {
379+
struct commit *commit = lookup_commit_reference_by_name(branch);
380+
if (!commit)
381+
die(_("invalid reference: %s"), branch);
382+
printf_ln(_("Preparing worktree (detached HEAD %s)"),
383+
find_unique_abbrev(commit->object.oid.hash,
384+
DEFAULT_ABBREV));
385+
}
386+
strbuf_release(&s);
387+
}
388+
}
389+
358390
static int add(int ac, const char **av, const char *prefix)
359391
{
360392
struct add_opts opts;
@@ -435,6 +467,8 @@ static int add(int ac, const char **av, const char *prefix)
435467
}
436468
}
437469

470+
print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
471+
438472
if (new_branch) {
439473
struct child_process cp = CHILD_PROCESS_INIT;
440474
cp.git_cmd = 1;

0 commit comments

Comments
 (0)