Skip to content

Commit d037212

Browse files
committed
Merge branch 'kn/for-all-refs'
"git for-each-ref" learned "--include-root-refs" option to show even the stuff outside the 'refs/' hierarchy. * kn/for-all-refs: for-each-ref: add new option to include root refs ref-filter: rename 'FILTER_REFS_ALL' to 'FILTER_REFS_REGULAR' refs: introduce `refs_for_each_include_root_refs()` refs: extract out `loose_fill_ref_dir_regular_file()` refs: introduce `is_pseudoref()` and `is_headref()`
2 parents 661f379 + 33d15b5 commit d037212

File tree

11 files changed

+243
-53
lines changed

11 files changed

+243
-53
lines changed

Documentation/git-for-each-ref.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
1212
[(--sort=<key>)...] [--format=<format>]
13-
[ --stdin | <pattern>... ]
13+
[--include-root-refs] [ --stdin | <pattern>... ]
1414
[--points-at=<object>]
1515
[--merged[=<object>]] [--no-merged[=<object>]]
1616
[--contains[=<object>]] [--no-contains[=<object>]]
@@ -105,6 +105,9 @@ TAB %(refname)`.
105105
any excluded pattern(s) are shown. Matching is done using the
106106
same rules as `<pattern>` above.
107107

108+
--include-root-refs::
109+
List root refs (HEAD and pseudorefs) apart from regular refs.
110+
108111
FIELD NAMES
109112
-----------
110113

builtin/for-each-ref.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
2020
{
2121
struct ref_sorting *sorting;
2222
struct string_list sorting_options = STRING_LIST_INIT_DUP;
23-
int icase = 0;
23+
int icase = 0, include_root_refs = 0, from_stdin = 0;
2424
struct ref_filter filter = REF_FILTER_INIT;
2525
struct ref_format format = REF_FORMAT_INIT;
26-
int from_stdin = 0;
26+
unsigned int flags = FILTER_REFS_REGULAR;
2727
struct strvec vec = STRVEC_INIT;
2828

2929
struct option opts[] = {
@@ -53,6 +53,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
5353
OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
5454
OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
5555
OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
56+
OPT_BOOL(0, "include-root-refs", &include_root_refs, N_("also include HEAD ref and pseudorefs")),
5657
OPT_END(),
5758
};
5859

@@ -96,8 +97,11 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
9697
filter.name_patterns = argv;
9798
}
9899

100+
if (include_root_refs)
101+
flags |= FILTER_REFS_ROOT_REFS;
102+
99103
filter.match_as_path = 1;
100-
filter_and_format_refs(&filter, FILTER_REFS_ALL, sorting, &format);
104+
filter_and_format_refs(&filter, flags, sorting, &format);
101105

102106
ref_filter_clear(&filter);
103107
ref_sorting_release(sorting);

ref-filter.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,12 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
26282628
each_ref_fn cb,
26292629
void *cb_data)
26302630
{
2631+
if (filter->kind == FILTER_REFS_KIND_MASK) {
2632+
/* In this case, we want to print all refs including root refs. */
2633+
return refs_for_each_include_root_refs(get_main_ref_store(the_repository),
2634+
cb, cb_data);
2635+
}
2636+
26312637
if (!filter->match_as_path) {
26322638
/*
26332639
* in this case, the patterns are applied after
@@ -2750,6 +2756,9 @@ static int ref_kind_from_refname(const char *refname)
27502756
return ref_kind[i].kind;
27512757
}
27522758

2759+
if (is_pseudoref(get_main_ref_store(the_repository), refname))
2760+
return FILTER_REFS_PSEUDOREFS;
2761+
27532762
return FILTER_REFS_OTHERS;
27542763
}
27552764

@@ -2781,7 +2790,16 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
27812790

27822791
/* Obtain the current ref kind from filter_ref_kind() and ignore unwanted refs. */
27832792
kind = filter_ref_kind(filter, refname);
2784-
if (!(kind & filter->kind))
2793+
2794+
/*
2795+
* Generally HEAD refs are printed with special description denoting a rebase,
2796+
* detached state and so forth. This is useful when only printing the HEAD ref
2797+
* But when it is being printed along with other pseudorefs, it makes sense to
2798+
* keep the formatting consistent. So we mask the type to act like a pseudoref.
2799+
*/
2800+
if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD)
2801+
kind = FILTER_REFS_PSEUDOREFS;
2802+
else if (!(kind & filter->kind))
27852803
return NULL;
27862804

27872805
if (!filter_pattern_match(filter, refname))
@@ -3047,9 +3065,15 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref
30473065
ret = for_each_fullref_in("refs/remotes/", fn, cb_data);
30483066
else if (filter->kind == FILTER_REFS_TAGS)
30493067
ret = for_each_fullref_in("refs/tags/", fn, cb_data);
3050-
else if (filter->kind & FILTER_REFS_ALL)
3068+
else if (filter->kind & FILTER_REFS_REGULAR)
30513069
ret = for_each_fullref_in_pattern(filter, fn, cb_data);
3052-
if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
3070+
3071+
/*
3072+
* When printing all ref types, HEAD is already included,
3073+
* so we don't want to print HEAD again.
3074+
*/
3075+
if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) &&
3076+
(filter->kind & FILTER_REFS_DETACHED_HEAD))
30533077
head_ref(fn, cb_data);
30543078
}
30553079

ref-filter.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
#define FILTER_REFS_BRANCHES 0x0004
2020
#define FILTER_REFS_REMOTES 0x0008
2121
#define FILTER_REFS_OTHERS 0x0010
22-
#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
22+
#define FILTER_REFS_REGULAR (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
2323
FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
2424
#define FILTER_REFS_DETACHED_HEAD 0x0020
25-
#define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
25+
#define FILTER_REFS_PSEUDOREFS 0x0040
26+
#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS)
27+
#define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \
28+
FILTER_REFS_PSEUDOREFS)
2629

2730
struct atom_value;
2831
struct ref_sorting;

refs.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,47 @@ static int is_pseudoref_syntax(const char *refname)
860860
return 1;
861861
}
862862

863+
int is_pseudoref(struct ref_store *refs, const char *refname)
864+
{
865+
static const char *const irregular_pseudorefs[] = {
866+
"AUTO_MERGE",
867+
"BISECT_EXPECTED_REV",
868+
"NOTES_MERGE_PARTIAL",
869+
"NOTES_MERGE_REF",
870+
"MERGE_AUTOSTASH",
871+
};
872+
struct object_id oid;
873+
size_t i;
874+
875+
if (!is_pseudoref_syntax(refname))
876+
return 0;
877+
878+
if (ends_with(refname, "_HEAD")) {
879+
refs_resolve_ref_unsafe(refs, refname,
880+
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
881+
&oid, NULL);
882+
return !is_null_oid(&oid);
883+
}
884+
885+
for (i = 0; i < ARRAY_SIZE(irregular_pseudorefs); i++)
886+
if (!strcmp(refname, irregular_pseudorefs[i])) {
887+
refs_resolve_ref_unsafe(refs, refname,
888+
RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE,
889+
&oid, NULL);
890+
return !is_null_oid(&oid);
891+
}
892+
893+
return 0;
894+
}
895+
896+
int is_headref(struct ref_store *refs, const char *refname)
897+
{
898+
if (!strcmp(refname, "HEAD"))
899+
return refs_ref_exists(refs, refname);
900+
901+
return 0;
902+
}
903+
863904
static int is_current_worktree_ref(const char *ref) {
864905
return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
865906
}
@@ -1715,6 +1756,13 @@ int for_each_rawref(each_ref_fn fn, void *cb_data)
17151756
return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data);
17161757
}
17171758

1759+
int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
1760+
void *cb_data)
1761+
{
1762+
return do_for_each_ref(refs, "", NULL, fn, 0,
1763+
DO_FOR_EACH_INCLUDE_ROOT_REFS, cb_data);
1764+
}
1765+
17181766
static int qsort_strcmp(const void *va, const void *vb)
17191767
{
17201768
const char *a = *(const char **)va;

refs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ int for_each_namespaced_ref(const char **exclude_patterns,
398398
int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data);
399399
int for_each_rawref(each_ref_fn fn, void *cb_data);
400400

401+
/*
402+
* Iterates over all refs including root refs, i.e. pseudorefs and HEAD.
403+
*/
404+
int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn,
405+
void *cb_data);
406+
401407
/*
402408
* Normalizes partial refs to their fully qualified form.
403409
* Will prepend <prefix> to the <pattern> if it doesn't start with 'refs/'.
@@ -1043,4 +1049,7 @@ extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT];
10431049
*/
10441050
void update_ref_namespace(enum ref_namespace namespace, char *ref);
10451051

1052+
int is_pseudoref(struct ref_store *refs, const char *refname);
1053+
int is_headref(struct ref_store *refs, const char *refname);
1054+
10461055
#endif /* REFS_H */

refs/files-backend.c

Lines changed: 93 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,38 @@ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dir
229229
}
230230
}
231231

232+
static void loose_fill_ref_dir_regular_file(struct files_ref_store *refs,
233+
const char *refname,
234+
struct ref_dir *dir)
235+
{
236+
struct object_id oid;
237+
int flag;
238+
239+
if (!refs_resolve_ref_unsafe(&refs->base, refname, RESOLVE_REF_READING,
240+
&oid, &flag)) {
241+
oidclr(&oid);
242+
flag |= REF_ISBROKEN;
243+
} else if (is_null_oid(&oid)) {
244+
/*
245+
* It is so astronomically unlikely
246+
* that null_oid is the OID of an
247+
* actual object that we consider its
248+
* appearance in a loose reference
249+
* file to be repo corruption
250+
* (probably due to a software bug).
251+
*/
252+
flag |= REF_ISBROKEN;
253+
}
254+
255+
if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
256+
if (!refname_is_safe(refname))
257+
die("loose refname is dangerous: %s", refname);
258+
oidclr(&oid);
259+
flag |= REF_BAD_NAME | REF_ISBROKEN;
260+
}
261+
add_entry_to_dir(dir, create_ref_entry(refname, &oid, flag));
262+
}
263+
232264
/*
233265
* Read the loose references from the namespace dirname into dir
234266
* (without recursing). dirname must end with '/'. dir must be the
@@ -257,8 +289,6 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
257289
strbuf_add(&refname, dirname, dirnamelen);
258290

259291
while ((de = readdir(d)) != NULL) {
260-
struct object_id oid;
261-
int flag;
262292
unsigned char dtype;
263293

264294
if (de->d_name[0] == '.')
@@ -274,33 +304,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
274304
create_dir_entry(dir->cache, refname.buf,
275305
refname.len));
276306
} else if (dtype == DT_REG) {
277-
if (!refs_resolve_ref_unsafe(&refs->base,
278-
refname.buf,
279-
RESOLVE_REF_READING,
280-
&oid, &flag)) {
281-
oidclr(&oid);
282-
flag |= REF_ISBROKEN;
283-
} else if (is_null_oid(&oid)) {
284-
/*
285-
* It is so astronomically unlikely
286-
* that null_oid is the OID of an
287-
* actual object that we consider its
288-
* appearance in a loose reference
289-
* file to be repo corruption
290-
* (probably due to a software bug).
291-
*/
292-
flag |= REF_ISBROKEN;
293-
}
294-
295-
if (check_refname_format(refname.buf,
296-
REFNAME_ALLOW_ONELEVEL)) {
297-
if (!refname_is_safe(refname.buf))
298-
die("loose refname is dangerous: %s", refname.buf);
299-
oidclr(&oid);
300-
flag |= REF_BAD_NAME | REF_ISBROKEN;
301-
}
302-
add_entry_to_dir(dir,
303-
create_ref_entry(refname.buf, &oid, flag));
307+
loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
304308
}
305309
strbuf_setlen(&refname, dirnamelen);
306310
}
@@ -311,9 +315,59 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
311315
add_per_worktree_entries_to_dir(dir, dirname);
312316
}
313317

314-
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
318+
/*
319+
* Add pseudorefs to the ref dir by parsing the directory for any files
320+
* which follow the pseudoref syntax.
321+
*/
322+
static void add_pseudoref_and_head_entries(struct ref_store *ref_store,
323+
struct ref_dir *dir,
324+
const char *dirname)
325+
{
326+
struct files_ref_store *refs =
327+
files_downcast(ref_store, REF_STORE_READ, "fill_ref_dir");
328+
struct strbuf path = STRBUF_INIT, refname = STRBUF_INIT;
329+
struct dirent *de;
330+
size_t dirnamelen;
331+
DIR *d;
332+
333+
files_ref_path(refs, &path, dirname);
334+
335+
d = opendir(path.buf);
336+
if (!d) {
337+
strbuf_release(&path);
338+
return;
339+
}
340+
341+
strbuf_addstr(&refname, dirname);
342+
dirnamelen = refname.len;
343+
344+
while ((de = readdir(d)) != NULL) {
345+
unsigned char dtype;
346+
347+
if (de->d_name[0] == '.')
348+
continue;
349+
if (ends_with(de->d_name, ".lock"))
350+
continue;
351+
strbuf_addstr(&refname, de->d_name);
352+
353+
dtype = get_dtype(de, &path, 1);
354+
if (dtype == DT_REG && (is_pseudoref(ref_store, de->d_name) ||
355+
is_headref(ref_store, de->d_name)))
356+
loose_fill_ref_dir_regular_file(refs, refname.buf, dir);
357+
358+
strbuf_setlen(&refname, dirnamelen);
359+
}
360+
strbuf_release(&refname);
361+
strbuf_release(&path);
362+
closedir(d);
363+
}
364+
365+
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs,
366+
unsigned int flags)
315367
{
316368
if (!refs->loose) {
369+
struct ref_dir *dir;
370+
317371
/*
318372
* Mark the top-level directory complete because we
319373
* are about to read the only subdirectory that can
@@ -324,12 +378,17 @@ static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)
324378
/* We're going to fill the top level ourselves: */
325379
refs->loose->root->flag &= ~REF_INCOMPLETE;
326380

381+
dir = get_ref_dir(refs->loose->root);
382+
383+
if (flags & DO_FOR_EACH_INCLUDE_ROOT_REFS)
384+
add_pseudoref_and_head_entries(dir->cache->ref_store, dir,
385+
refs->loose->root->name);
386+
327387
/*
328388
* Add an incomplete entry for "refs/" (to be filled
329389
* lazily):
330390
*/
331-
add_entry_to_dir(get_ref_dir(refs->loose->root),
332-
create_dir_entry(refs->loose, "refs/", 5));
391+
add_entry_to_dir(dir, create_dir_entry(refs->loose, "refs/", 5));
333392
}
334393
return refs->loose;
335394
}
@@ -857,7 +916,7 @@ static struct ref_iterator *files_ref_iterator_begin(
857916
* disk, and re-reads it if not.
858917
*/
859918

860-
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs),
919+
loose_iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, flags),
861920
prefix, ref_store->repo, 1);
862921

863922
/*
@@ -1217,7 +1276,7 @@ static int files_pack_refs(struct ref_store *ref_store,
12171276

12181277
packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err);
12191278

1220-
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs), NULL,
1279+
iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL,
12211280
the_repository, 0);
12221281
while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
12231282
/*

0 commit comments

Comments
 (0)