Skip to content

Commit f24c30e

Browse files
jonathantanmygitster
authored andcommitted
wt-status: tolerate dangling marks
When a user checks out the upstream branch of HEAD, the upstream branch not being a local branch, and then runs "git status", like this: git clone $URL client cd client git checkout @{u} git status no status is printed, but instead an error message: fatal: HEAD does not point to a branch (This error message when running "git branch" persists even after checking out other things - it only stops after checking out a branch.) This is because "git status" reads the reflog when determining the "HEAD detached" message, and thus attempts to DWIM "@{u}", but that doesn't work because HEAD no longer points to a branch. Therefore, when calculating the status of a worktree, tolerate dangling marks. This is done by adding an additional parameter to dwim_ref() and repo_dwim_ref(). Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ec06b05 commit f24c30e

19 files changed

+60
-29
lines changed

archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ static void parse_treeish_arg(const char **argv,
397397
const char *colon = strchrnul(name, ':');
398398
int refnamelen = colon - name;
399399

400-
if (!dwim_ref(name, refnamelen, &oid, &ref))
400+
if (!dwim_ref(name, refnamelen, &oid, &ref, 0))
401401
die(_("no such ref: %.*s"), refnamelen, name);
402402
} else {
403-
dwim_ref(name, strlen(name), &oid, &ref);
403+
dwim_ref(name, strlen(name), &oid, &ref, 0);
404404
}
405405

406406
if (get_oid(name, &oid))

branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void create_branch(struct repository *r,
281281
die(_("Not a valid object name: '%s'."), start_name);
282282
}
283283

284-
switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref)) {
284+
switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
285285
case 0:
286286
/* Not branching from any existing branch */
287287
if (explicit_tracking)

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ static void setup_branch_path(struct branch_info *branch)
650650
* If this is a ref, resolve it; otherwise, look up the OID for our
651651
* expression. Failure here is okay.
652652
*/
653-
if (!dwim_ref(branch->name, strlen(branch->name), &branch->oid, &branch->refname))
653+
if (!dwim_ref(branch->name, strlen(branch->name), &branch->oid, &branch->refname, 0))
654654
repo_get_oid_committish(the_repository, branch->name, &branch->oid);
655655

656656
strbuf_branchname(&buf, branch->name, INTERPRET_BRANCH_LOCAL);
@@ -1349,7 +1349,7 @@ static void die_expecting_a_branch(const struct branch_info *branch_info)
13491349
struct object_id oid;
13501350
char *to_free;
13511351

1352-
if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free) == 1) {
1352+
if (dwim_ref(branch_info->name, strlen(branch_info->name), &oid, &to_free, 0) == 1) {
13531353
const char *ref = to_free;
13541354

13551355
if (skip_prefix(ref, "refs/tags/", &ref))

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ static void get_tags_and_duplicates(struct rev_cmdline_info *info)
943943
if (e->flags & UNINTERESTING)
944944
continue;
945945

946-
if (dwim_ref(e->name, strlen(e->name), &oid, &full_name) != 1)
946+
if (dwim_ref(e->name, strlen(e->name), &oid, &full_name, 0) != 1)
947947
continue;
948948

949949
if (refspecs.nr) {

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ static char *find_branch_name(struct rev_info *rev)
10621062
return NULL;
10631063
ref = rev->cmdline.rev[positive].name;
10641064
tip_oid = &rev->cmdline.rev[positive].item->oid;
1065-
if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref) &&
1065+
if (dwim_ref(ref, strlen(ref), &branch_oid, &full_ref, 0) &&
10661066
skip_prefix(full_ref, "refs/heads/", &v) &&
10671067
oideq(tip_oid, &branch_oid))
10681068
branch = xstrdup(v);

builtin/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ static void merge_name(const char *remote, struct strbuf *msg)
501501
if (!remote_head)
502502
die(_("'%s' does not point to a commit"), remote);
503503

504-
if (dwim_ref(remote, strlen(remote), &branch_head, &found_ref) > 0) {
504+
if (dwim_ref(remote, strlen(remote), &branch_head, &found_ref, 0) > 0) {
505505
if (starts_with(found_ref, "refs/heads/")) {
506506
strbuf_addf(msg, "%s\t\tbranch '%s' of .\n",
507507
oid_to_hex(&branch_head), remote);

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
423423
char *ref = NULL;
424424
int err;
425425

426-
dwim_ref(rev, strlen(rev), &dummy, &ref);
426+
dwim_ref(rev, strlen(rev), &dummy, &ref, 0);
427427
if (ref && !starts_with(ref, "refs/"))
428428
ref = NULL;
429429

builtin/rev-parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static void show_rev(int type, const struct object_id *oid, const char *name)
136136
struct object_id discard;
137137
char *full;
138138

139-
switch (dwim_ref(name, strlen(name), &discard, &full)) {
139+
switch (dwim_ref(name, strlen(name), &discard, &full, 0)) {
140140
case 0:
141141
/*
142142
* Not found -- not a ref. We could

builtin/show-branch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
741741
die(Q_("only %d entry can be shown at one time.",
742742
"only %d entries can be shown at one time.",
743743
MAX_REVS), MAX_REVS);
744-
if (!dwim_ref(*av, strlen(*av), &oid, &ref))
744+
if (!dwim_ref(*av, strlen(*av), &oid, &ref, 0))
745745
die(_("no such ref %s"), *av);
746746

747747
/* Has the base been specified? */

builtin/stash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv)
185185
end_of_rev = strchrnul(revision, '@');
186186
strbuf_add(&symbolic, revision, end_of_rev - revision);
187187

188-
ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref);
188+
ret = dwim_ref(symbolic.buf, symbolic.len, &dummy, &expanded_ref, 0);
189189
strbuf_release(&symbolic);
190190
switch (ret) {
191191
case 0: /* Not found, but valid ref */

0 commit comments

Comments
 (0)