Skip to content

Commit dbf668a

Browse files
committed
Merge branch 'ps/pseudo-refs'
Assorted changes around pseudoref handling. * ps/pseudo-refs: bisect: consistently write BISECT_EXPECTED_REV via the refdb refs: complete list of special refs refs: propagate errno when reading special refs fails wt-status: read HEAD and ORIG_HEAD via the refdb
2 parents 601b157 + 0a06892 commit dbf668a

File tree

6 files changed

+87
-39
lines changed

6 files changed

+87
-39
lines changed

bisect.c

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ static int read_bisect_refs(void)
471471
}
472472

473473
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
474-
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
475474
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
476475
static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
477476
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
@@ -707,26 +706,10 @@ static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
707706

708707
static int is_expected_rev(const struct object_id *oid)
709708
{
710-
const char *filename = git_path_bisect_expected_rev();
711-
struct stat st;
712-
struct strbuf str = STRBUF_INIT;
713-
FILE *fp;
714-
int res = 0;
715-
716-
if (stat(filename, &st) || !S_ISREG(st.st_mode))
709+
struct object_id expected_oid;
710+
if (read_ref("BISECT_EXPECTED_REV", &expected_oid))
717711
return 0;
718-
719-
fp = fopen_or_warn(filename, "r");
720-
if (!fp)
721-
return 0;
722-
723-
if (strbuf_getline_lf(&str, fp) != EOF)
724-
res = !strcmp(str.buf, oid_to_hex(oid));
725-
726-
strbuf_release(&str);
727-
fclose(fp);
728-
729-
return res;
712+
return oideq(oid, &expected_oid);
730713
}
731714

732715
enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
@@ -1185,10 +1168,10 @@ int bisect_clean_state(void)
11851168
struct string_list refs_for_removal = STRING_LIST_INIT_NODUP;
11861169
for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal);
11871170
string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD"));
1171+
string_list_append(&refs_for_removal, xstrdup("BISECT_EXPECTED_REV"));
11881172
result = delete_refs("bisect: remove", &refs_for_removal, REF_NO_DEREF);
11891173
refs_for_removal.strdup_strings = 1;
11901174
string_list_clear(&refs_for_removal, 0);
1191-
unlink_or_warn(git_path_bisect_expected_rev());
11921175
unlink_or_warn(git_path_bisect_ancestors_ok());
11931176
unlink_or_warn(git_path_bisect_log());
11941177
unlink_or_warn(git_path_bisect_names());

builtin/bisect.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "revision.h"
1818

1919
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
20-
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
2120
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
2221
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
2322
static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
@@ -920,7 +919,6 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
920919
const char *state;
921920
int i, verify_expected = 1;
922921
struct object_id oid, expected;
923-
struct strbuf buf = STRBUF_INIT;
924922
struct oid_array revs = OID_ARRAY_INIT;
925923

926924
if (!argc)
@@ -975,10 +973,8 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
975973
oid_array_append(&revs, &commit->object.oid);
976974
}
977975

978-
if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
979-
get_oid_hex(buf.buf, &expected) < 0)
976+
if (read_ref("BISECT_EXPECTED_REV", &expected))
980977
verify_expected = 0; /* Ignore invalid file contents */
981-
strbuf_release(&buf);
982978

983979
for (i = 0; i < revs.nr; i++) {
984980
if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
@@ -987,7 +983,7 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc,
987983
}
988984
if (verify_expected && !oideq(&revs.oid[i], &expected)) {
989985
unlink_or_warn(git_path_bisect_ancestors_ok());
990-
unlink_or_warn(git_path_bisect_expected_rev());
986+
delete_ref(NULL, "BISECT_EXPECTED_REV", NULL, REF_NO_DEREF);
991987
verify_expected = 0;
992988
}
993989
}

refs.c

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,8 +1806,10 @@ static int refs_read_special_head(struct ref_store *ref_store,
18061806
int result = -1;
18071807
strbuf_addf(&full_path, "%s/%s", ref_store->gitdir, refname);
18081808

1809-
if (strbuf_read_file(&content, full_path.buf, 0) < 0)
1809+
if (strbuf_read_file(&content, full_path.buf, 0) < 0) {
1810+
*failure_errno = errno;
18101811
goto done;
1812+
}
18111813

18121814
result = parse_loose_ref_contents(content.buf, oid, referent, type,
18131815
failure_errno);
@@ -1818,15 +1820,66 @@ static int refs_read_special_head(struct ref_store *ref_store,
18181820
return result;
18191821
}
18201822

1823+
static int is_special_ref(const char *refname)
1824+
{
1825+
/*
1826+
* Special references get written and read directly via the filesystem
1827+
* by the subsystems that create them. Thus, they must not go through
1828+
* the reference backend but must instead be read directly. It is
1829+
* arguable whether this behaviour is sensible, or whether it's simply
1830+
* a leaky abstraction enabled by us only having a single reference
1831+
* backend implementation. But at least for a subset of references it
1832+
* indeed does make sense to treat them specially:
1833+
*
1834+
* - FETCH_HEAD may contain multiple object IDs, and each one of them
1835+
* carries additional metadata like where it came from.
1836+
*
1837+
* - MERGE_HEAD may contain multiple object IDs when merging multiple
1838+
* heads.
1839+
*
1840+
* There are some exceptions that you might expect to see on this list
1841+
* but which are handled exclusively via the reference backend:
1842+
*
1843+
* - BISECT_EXPECTED_REV
1844+
*
1845+
* - CHERRY_PICK_HEAD
1846+
*
1847+
* - HEAD
1848+
*
1849+
* - ORIG_HEAD
1850+
*
1851+
* - "rebase-apply/" and "rebase-merge/" contain all of the state for
1852+
* rebases, including some reference-like files. These are
1853+
* exclusively read and written via the filesystem and never go
1854+
* through the refdb.
1855+
*
1856+
* Writing or deleting references must consistently go either through
1857+
* the filesystem (special refs) or through the reference backend
1858+
* (normal ones).
1859+
*/
1860+
static const char * const special_refs[] = {
1861+
"AUTO_MERGE",
1862+
"FETCH_HEAD",
1863+
"MERGE_AUTOSTASH",
1864+
"MERGE_HEAD",
1865+
};
1866+
size_t i;
1867+
1868+
for (i = 0; i < ARRAY_SIZE(special_refs); i++)
1869+
if (!strcmp(refname, special_refs[i]))
1870+
return 1;
1871+
1872+
return 0;
1873+
}
1874+
18211875
int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
18221876
struct object_id *oid, struct strbuf *referent,
18231877
unsigned int *type, int *failure_errno)
18241878
{
18251879
assert(failure_errno);
1826-
if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
1880+
if (is_special_ref(refname))
18271881
return refs_read_special_head(ref_store, refname, oid, referent,
18281882
type, failure_errno);
1829-
}
18301883

18311884
return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
18321885
type, failure_errno);

t/t1403-show-ref.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,14 @@ test_expect_success '--exists with directory fails with generic error' '
268268
test_cmp expect err
269269
'
270270

271+
test_expect_success '--exists with non-existent special ref' '
272+
test_expect_code 2 git show-ref --exists FETCH_HEAD
273+
'
274+
275+
test_expect_success '--exists with existing special ref' '
276+
test_when_finished "rm .git/FETCH_HEAD" &&
277+
git rev-parse HEAD >.git/FETCH_HEAD &&
278+
git show-ref --exists FETCH_HEAD
279+
'
280+
271281
test_done

t/t6030-bisect-porcelain.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ test_expect_success 'git bisect reset cleans bisection state properly' '
11821182
git bisect bad $HASH4 &&
11831183
git bisect reset &&
11841184
test -z "$(git for-each-ref "refs/bisect/*")" &&
1185-
test_path_is_missing ".git/BISECT_EXPECTED_REV" &&
1185+
test_ref_missing BISECT_EXPECTED_REV &&
11861186
test_path_is_missing ".git/BISECT_ANCESTORS_OK" &&
11871187
test_path_is_missing ".git/BISECT_LOG" &&
11881188
test_path_is_missing ".git/BISECT_RUN" &&

wt-status.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,26 +1296,32 @@ static char *read_line_from_git_path(const char *filename)
12961296
static int split_commit_in_progress(struct wt_status *s)
12971297
{
12981298
int split_in_progress = 0;
1299-
char *head, *orig_head, *rebase_amend, *rebase_orig_head;
1299+
struct object_id head_oid, orig_head_oid;
1300+
char *rebase_amend, *rebase_orig_head;
1301+
int head_flags, orig_head_flags;
13001302

13011303
if ((!s->amend && !s->nowarn && !s->workdir_dirty) ||
13021304
!s->branch || strcmp(s->branch, "HEAD"))
13031305
return 0;
13041306

1305-
head = read_line_from_git_path("HEAD");
1306-
orig_head = read_line_from_git_path("ORIG_HEAD");
1307+
if (read_ref_full("HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1308+
&head_oid, &head_flags) ||
1309+
read_ref_full("ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
1310+
&orig_head_oid, &orig_head_flags))
1311+
return 0;
1312+
if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF)
1313+
return 0;
1314+
13071315
rebase_amend = read_line_from_git_path("rebase-merge/amend");
13081316
rebase_orig_head = read_line_from_git_path("rebase-merge/orig-head");
13091317

1310-
if (!head || !orig_head || !rebase_amend || !rebase_orig_head)
1318+
if (!rebase_amend || !rebase_orig_head)
13111319
; /* fall through, no split in progress */
13121320
else if (!strcmp(rebase_amend, rebase_orig_head))
1313-
split_in_progress = !!strcmp(head, rebase_amend);
1314-
else if (strcmp(orig_head, rebase_orig_head))
1321+
split_in_progress = !!strcmp(oid_to_hex(&head_oid), rebase_amend);
1322+
else if (strcmp(oid_to_hex(&orig_head_oid), rebase_orig_head))
13151323
split_in_progress = 1;
13161324

1317-
free(head);
1318-
free(orig_head);
13191325
free(rebase_amend);
13201326
free(rebase_orig_head);
13211327

0 commit comments

Comments
 (0)