Skip to content

Commit 4341460

Browse files
sunshinecogitster
authored andcommitted
checkout: generalize die_if_checked_out() branch name argument
The plan is to publish die_if_checked_out() so that callers other than git-checkout can take advantage of it, however, those callers won't have access to git-checkout's "struct branch_info". Therefore, change it to accept the full name of the branch as a simple string instead. While here, also give the argument a more meaningful name ("branch" instead of "new"). Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e07815 commit 4341460

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

builtin/checkout.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ static const char *unique_tracking_name(const char *name, unsigned char *sha1)
873873
return NULL;
874874
}
875875

876-
static void check_linked_checkout(struct branch_info *new, const char *id)
876+
static void check_linked_checkout(const char *branch, const char *id)
877877
{
878878
struct strbuf sb = STRBUF_INIT;
879879
struct strbuf path = STRBUF_INIT;
@@ -898,7 +898,7 @@ static void check_linked_checkout(struct branch_info *new, const char *id)
898898
end = start;
899899
while (*end && !isspace(*end))
900900
end++;
901-
if (strncmp(start, new->path, end - start) || new->path[end - start] != '\0')
901+
if (strncmp(start, branch, end - start) || branch[end - start] != '\0')
902902
goto done;
903903
if (id) {
904904
strbuf_reset(&path);
@@ -908,20 +908,21 @@ static void check_linked_checkout(struct branch_info *new, const char *id)
908908
strbuf_rtrim(&gitdir);
909909
} else
910910
strbuf_addstr(&gitdir, get_git_common_dir());
911-
die(_("'%s' is already checked out at '%s'"), new->name, gitdir.buf);
911+
skip_prefix(branch, "refs/heads/", &branch);
912+
die(_("'%s' is already checked out at '%s'"), branch, gitdir.buf);
912913
done:
913914
strbuf_release(&path);
914915
strbuf_release(&sb);
915916
strbuf_release(&gitdir);
916917
}
917918

918-
static void die_if_checked_out(struct branch_info *new)
919+
static void die_if_checked_out(const char *branch)
919920
{
920921
struct strbuf path = STRBUF_INIT;
921922
DIR *dir;
922923
struct dirent *d;
923924

924-
check_linked_checkout(new, NULL);
925+
check_linked_checkout(branch, NULL);
925926

926927
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
927928
dir = opendir(path.buf);
@@ -932,7 +933,7 @@ static void die_if_checked_out(struct branch_info *new)
932933
while ((d = readdir(dir)) != NULL) {
933934
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
934935
continue;
935-
check_linked_checkout(new, d->d_name);
936+
check_linked_checkout(branch, d->d_name);
936937
}
937938
closedir(dir);
938939
}
@@ -1151,7 +1152,7 @@ static int checkout_branch(struct checkout_opts *opts,
11511152
char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
11521153
if (head_ref &&
11531154
(!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
1154-
die_if_checked_out(new);
1155+
die_if_checked_out(new->path);
11551156
free(head_ref);
11561157
}
11571158

0 commit comments

Comments
 (0)