Skip to content

Commit edc2f71

Browse files
committed
Merge branch 'dt/pre-refs-backend'
Code restructuring around the "refs" area to prepare for pluggable refs backends. * dt/pre-refs-backend: (24 commits) refs: on symref reflog expire, lock symref not referrent refs: move resolve_ref_unsafe into common code show_head_ref(): check the result of resolve_ref_namespace() check_aliased_update(): check that dst_name is non-NULL checkout_paths(): remove unneeded flag variable cmd_merge(): remove unneeded flag variable fsck_head_link(): remove unneeded flag variable read_raw_ref(): change flags parameter to unsigned int files-backend: inline resolve_ref_1() into resolve_ref_unsafe() read_raw_ref(): manage own scratch space files-backend: break out ref reading resolve_ref_1(): eliminate local variable "bad_name" resolve_ref_1(): reorder code resolve_ref_1(): eliminate local variable resolve_ref_unsafe(): ensure flags is always set resolve_ref_unsafe(): use for loop to count up to MAXDEPTH resolve_missing_loose_ref(): simplify semantics t1430: improve test coverage of deletion of badly-named refs t1430: test for-each-ref in the presence of badly-named refs t1430: don't rely on symbolic-ref for creating broken symrefs ...
2 parents 5b715ec + 41d796e commit edc2f71

File tree

10 files changed

+440
-288
lines changed

10 files changed

+440
-288
lines changed

builtin/checkout.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ static int checkout_paths(const struct checkout_opts *opts,
242242
struct checkout state;
243243
static char *ps_matched;
244244
unsigned char rev[20];
245-
int flag;
246245
struct commit *head;
247246
int errs = 0;
248247
struct lock_file *lock_file;
@@ -375,7 +374,7 @@ static int checkout_paths(const struct checkout_opts *opts,
375374
if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
376375
die(_("unable to write new index file"));
377376

378-
read_ref_full("HEAD", 0, rev, &flag);
377+
read_ref_full("HEAD", 0, rev, NULL);
379378
head = lookup_commit_reference_gently(rev, 1);
380379

381380
errs |= post_checkout_hook(head, head, 0);

builtin/fsck.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,13 +493,12 @@ static void fsck_object_dir(const char *path)
493493

494494
static int fsck_head_link(void)
495495
{
496-
int flag;
497496
int null_is_error = 0;
498497

499498
if (verbose)
500499
fprintf(stderr, "Checking HEAD link\n");
501500

502-
head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
501+
head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, NULL);
503502
if (!head_points_at) {
504503
errors_found |= ERROR_REFS;
505504
return error("Invalid HEAD");

builtin/merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11681168
struct commit *head_commit;
11691169
struct strbuf buf = STRBUF_INIT;
11701170
const char *head_arg;
1171-
int flag, i, ret = 0, head_subsumed;
1171+
int i, ret = 0, head_subsumed;
11721172
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
11731173
struct commit_list *common = NULL;
11741174
const char *best_strategy = NULL, *wt_strategy = NULL;
@@ -1182,7 +1182,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11821182
* Check if we are _not_ on a detached HEAD, i.e. if there is a
11831183
* current branch.
11841184
*/
1185-
branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, &flag);
1185+
branch = branch_to_free = resolve_refdup("HEAD", 0, head_sha1, NULL);
11861186
if (branch && starts_with(branch, "refs/heads/"))
11871187
branch += 11;
11881188
if (!branch || is_null_sha1(head_sha1))

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1084,13 +1084,13 @@ static void check_aliased_update(struct command *cmd, struct string_list *list)
10841084
if (!(flag & REF_ISSYMREF))
10851085
return;
10861086

1087-
dst_name = strip_namespace(dst_name);
10881087
if (!dst_name) {
10891088
rp_error("refusing update to broken symref '%s'", cmd->ref_name);
10901089
cmd->skip_update = 1;
10911090
cmd->error_string = "broken symref";
10921091
return;
10931092
}
1093+
dst_name = strip_namespace(dst_name);
10941094

10951095
if ((item = string_list_lookup(list, dst_name)) == NULL)
10961096
return;

http-backend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ static int show_head_ref(const char *refname, const struct object_id *oid,
484484
const char *target = resolve_ref_unsafe(refname,
485485
RESOLVE_REF_READING,
486486
unused.hash, NULL);
487-
const char *target_nons = strip_namespace(target);
488487

489-
strbuf_addf(buf, "ref: %s\n", target_nons);
488+
if (target)
489+
strbuf_addf(buf, "ref: %s\n", strip_namespace(target));
490490
} else {
491491
strbuf_addf(buf, "%s\n", oid_to_hex(oid));
492492
}

refs.c

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,3 +1080,152 @@ int rename_ref_available(const char *oldname, const char *newname)
10801080
strbuf_release(&err);
10811081
return ret;
10821082
}
1083+
1084+
int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1085+
{
1086+
struct object_id oid;
1087+
int flag;
1088+
1089+
if (submodule) {
1090+
if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
1091+
return fn("HEAD", &oid, 0, cb_data);
1092+
1093+
return 0;
1094+
}
1095+
1096+
if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
1097+
return fn("HEAD", &oid, flag, cb_data);
1098+
1099+
return 0;
1100+
}
1101+
1102+
int head_ref(each_ref_fn fn, void *cb_data)
1103+
{
1104+
return head_ref_submodule(NULL, fn, cb_data);
1105+
}
1106+
1107+
int for_each_ref(each_ref_fn fn, void *cb_data)
1108+
{
1109+
return do_for_each_ref(NULL, "", fn, 0, 0, cb_data);
1110+
}
1111+
1112+
int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
1113+
{
1114+
return do_for_each_ref(submodule, "", fn, 0, 0, cb_data);
1115+
}
1116+
1117+
int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
1118+
{
1119+
return do_for_each_ref(NULL, prefix, fn, strlen(prefix), 0, cb_data);
1120+
}
1121+
1122+
int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
1123+
{
1124+
unsigned int flag = 0;
1125+
1126+
if (broken)
1127+
flag = DO_FOR_EACH_INCLUDE_BROKEN;
1128+
return do_for_each_ref(NULL, prefix, fn, 0, flag, cb_data);
1129+
}
1130+
1131+
int for_each_ref_in_submodule(const char *submodule, const char *prefix,
1132+
each_ref_fn fn, void *cb_data)
1133+
{
1134+
return do_for_each_ref(submodule, prefix, fn, strlen(prefix), 0, cb_data);
1135+
}
1136+
1137+
int for_each_replace_ref(each_ref_fn fn, void *cb_data)
1138+
{
1139+
return do_for_each_ref(NULL, git_replace_ref_base, fn,
1140+
strlen(git_replace_ref_base), 0, cb_data);
1141+
}
1142+
1143+
int for_each_namespaced_ref(each_ref_fn fn, void *cb_data)
1144+
{
1145+
struct strbuf buf = STRBUF_INIT;
1146+
int ret;
1147+
strbuf_addf(&buf, "%srefs/", get_git_namespace());
1148+
ret = do_for_each_ref(NULL, buf.buf, fn, 0, 0, cb_data);
1149+
strbuf_release(&buf);
1150+
return ret;
1151+
}
1152+
1153+
int for_each_rawref(each_ref_fn fn, void *cb_data)
1154+
{
1155+
return do_for_each_ref(NULL, "", fn, 0,
1156+
DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
1157+
}
1158+
1159+
/* This function needs to return a meaningful errno on failure */
1160+
const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
1161+
unsigned char *sha1, int *flags)
1162+
{
1163+
static struct strbuf sb_refname = STRBUF_INIT;
1164+
int unused_flags;
1165+
int symref_count;
1166+
1167+
if (!flags)
1168+
flags = &unused_flags;
1169+
1170+
*flags = 0;
1171+
1172+
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1173+
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1174+
!refname_is_safe(refname)) {
1175+
errno = EINVAL;
1176+
return NULL;
1177+
}
1178+
1179+
/*
1180+
* dwim_ref() uses REF_ISBROKEN to distinguish between
1181+
* missing refs and refs that were present but invalid,
1182+
* to complain about the latter to stderr.
1183+
*
1184+
* We don't know whether the ref exists, so don't set
1185+
* REF_ISBROKEN yet.
1186+
*/
1187+
*flags |= REF_BAD_NAME;
1188+
}
1189+
1190+
for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
1191+
unsigned int read_flags = 0;
1192+
1193+
if (read_raw_ref(refname, sha1, &sb_refname, &read_flags)) {
1194+
*flags |= read_flags;
1195+
if (errno != ENOENT || (resolve_flags & RESOLVE_REF_READING))
1196+
return NULL;
1197+
hashclr(sha1);
1198+
if (*flags & REF_BAD_NAME)
1199+
*flags |= REF_ISBROKEN;
1200+
return refname;
1201+
}
1202+
1203+
*flags |= read_flags;
1204+
1205+
if (!(read_flags & REF_ISSYMREF)) {
1206+
if (*flags & REF_BAD_NAME) {
1207+
hashclr(sha1);
1208+
*flags |= REF_ISBROKEN;
1209+
}
1210+
return refname;
1211+
}
1212+
1213+
refname = sb_refname.buf;
1214+
if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
1215+
hashclr(sha1);
1216+
return refname;
1217+
}
1218+
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
1219+
if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
1220+
!refname_is_safe(refname)) {
1221+
errno = EINVAL;
1222+
return NULL;
1223+
}
1224+
1225+
*flags |= REF_ISBROKEN | REF_BAD_NAME;
1226+
}
1227+
}
1228+
1229+
errno = ELOOP;
1230+
return NULL;
1231+
}

0 commit comments

Comments
 (0)