worktree: factor out dwim_branch function

Factor out a dwim_branch function, which takes care of the dwim'ery in 'git worktree add <path>'. It's not too much code currently, but we're adding a new kind of dwim in a subsequent patch, at which point it makes more sense to have it as a separate function. Factor it out now to reduce the patch noise in the next patch. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Reviewed-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Apr 24, 2018 at 22:56 UTC 6427f87186e53d9d4319d43e4efbe46bb93b7440
1 file changed +18 -11
builtin/worktree.c
+18 -11
@@ -387,6 +387,21 @@ static void print_preparing_worktree_line(int detach,
387 }
388 }
389
390 +static const char *dwim_branch(const char *path, const char **new_branch)
391 +{
392 + int n;
393 + const char *s = worktree_basename(path, &n);
394 + *new_branch = xstrndup(s, n);
395 + UNLEAK(*new_branch);
396 + if (guess_remote) {
397 + struct object_id oid;
398 + const char *remote =
399 + unique_tracking_name(*new_branch, &oid);
400 + return remote;
401 + }
402 + return NULL;
403 +}
404 +
405 static int add(int ac, const char **av, const char *prefix)
406 {
407 struct add_opts opts;
@@ -439,17 +454,9 @@ static int add(int ac, const char **av, const char *prefix)
454 }
455
456 if (ac < 2 && !new_branch && !opts.detach) {
442 - int n;
443 - const char *s = worktree_basename(path, &n);
444 - new_branch = xstrndup(s, n);
445 - UNLEAK(new_branch);
446 - if (guess_remote) {
447 - struct object_id oid;
448 - const char *remote =
449 - unique_tracking_name(new_branch, &oid);
450 - if (remote)
451 - branch = remote;
452 - }
457 + const char *s = dwim_branch(path, &new_branch);
458 + if (s)
459 + branch = s;
460 }
461
462 if (ac == 2 && !new_branch && !opts.detach) {