Skip to content

Commit 1a1661b

Browse files
committed
Merge branch 'jt/rev-list-z'
"git rev-list" learns machine-parsable output format that delimits each field with NUL. * jt/rev-list-z: rev-list: support NUL-delimited --missing option rev-list: support NUL-delimited --boundary option rev-list: support delimiting objects with NUL bytes rev-list: refactor early option parsing rev-list: inline `show_object_with_name()` in `show_object()`
2 parents 1f1e219 + 340e752 commit 1a1661b

File tree

6 files changed

+175
-34
lines changed

6 files changed

+175
-34
lines changed

Documentation/rev-list-options.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,30 @@ ifdef::git-rev-list[]
361361
--progress=<header>::
362362
Show progress reports on stderr as objects are considered. The
363363
`<header>` text will be printed with each progress update.
364+
365+
-z::
366+
Instead of being newline-delimited, each outputted object and its
367+
accompanying metadata is delimited using NUL bytes. Output is printed
368+
in the following form:
369+
+
370+
-----------------------------------------------------------------------
371+
<OID> NUL [<token>=<value> NUL]...
372+
-----------------------------------------------------------------------
373+
+
374+
Additional object metadata, such as object paths or boundary objects, is
375+
printed using the `<token>=<value>` form. Token values are printed as-is
376+
without any encoding/truncation. An OID entry never contains a '=' character
377+
and thus is used to signal the start of a new object record. Examples:
378+
+
379+
-----------------------------------------------------------------------
380+
<OID> NUL
381+
<OID> NUL path=<path> NUL
382+
<OID> NUL boundary=yes NUL
383+
<OID> NUL missing=yes NUL [<token>=<value> NUL]...
384+
-----------------------------------------------------------------------
385+
+
386+
This mode is only compatible with the `--objects`, `--boundary`, and
387+
`--missing` output options.
364388
endif::git-rev-list[]
365389

366390
History Simplification

builtin/rev-list.c

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "object-file.h"
1717
#include "object-store-ll.h"
1818
#include "pack-bitmap.h"
19+
#include "parse-options.h"
1920
#include "log-tree.h"
2021
#include "graph.h"
2122
#include "bisect.h"
@@ -64,6 +65,7 @@ static const char rev_list_usage[] =
6465
" --abbrev-commit\n"
6566
" --left-right\n"
6667
" --count\n"
68+
" -z\n"
6769
" special purpose:\n"
6870
" --bisect\n"
6971
" --bisect-vars\n"
@@ -96,6 +98,9 @@ static int arg_show_object_names = 1;
9698

9799
#define DEFAULT_OIDSET_SIZE (16*1024)
98100

101+
static char line_term = '\n';
102+
static char info_term = ' ';
103+
99104
static int show_disk_usage;
100105
static off_t total_disk_usage;
101106
static int human_readable;
@@ -131,24 +136,37 @@ static void print_missing_object(struct missing_objects_map_entry *entry,
131136
{
132137
struct strbuf sb = STRBUF_INIT;
133138

139+
if (line_term)
140+
printf("?%s", oid_to_hex(&entry->entry.oid));
141+
else
142+
printf("%s%cmissing=yes", oid_to_hex(&entry->entry.oid),
143+
info_term);
144+
134145
if (!print_missing_info) {
135-
printf("?%s\n", oid_to_hex(&entry->entry.oid));
146+
putchar(line_term);
136147
return;
137148
}
138149

139150
if (entry->path && *entry->path) {
140-
struct strbuf path = STRBUF_INIT;
151+
strbuf_addf(&sb, "%cpath=", info_term);
141152

142-
strbuf_addstr(&sb, " path=");
143-
quote_path(entry->path, NULL, &path, QUOTE_PATH_QUOTE_SP);
144-
strbuf_addbuf(&sb, &path);
153+
if (line_term) {
154+
struct strbuf path = STRBUF_INIT;
145155

146-
strbuf_release(&path);
156+
quote_path(entry->path, NULL, &path, QUOTE_PATH_QUOTE_SP);
157+
strbuf_addbuf(&sb, &path);
158+
159+
strbuf_release(&path);
160+
} else {
161+
strbuf_addstr(&sb, entry->path);
162+
}
147163
}
148164
if (entry->type)
149-
strbuf_addf(&sb, " type=%s", type_name(entry->type));
165+
strbuf_addf(&sb, "%ctype=%s", info_term, type_name(entry->type));
166+
167+
fwrite(sb.buf, sizeof(char), sb.len, stdout);
168+
putchar(line_term);
150169

151-
printf("?%s%s\n", oid_to_hex(&entry->entry.oid), sb.buf);
152170
strbuf_release(&sb);
153171
}
154172

@@ -235,13 +253,18 @@ static void show_commit(struct commit *commit, void *data)
235253
fputs(info->header_prefix, stdout);
236254

237255
if (revs->include_header) {
238-
if (!revs->graph)
256+
if (!revs->graph && line_term)
239257
fputs(get_revision_mark(revs, commit), stdout);
240258
if (revs->abbrev_commit && revs->abbrev)
241259
fputs(repo_find_unique_abbrev(the_repository, &commit->object.oid, revs->abbrev),
242260
stdout);
243261
else
244262
fputs(oid_to_hex(&commit->object.oid), stdout);
263+
264+
if (!line_term) {
265+
if (commit->object.flags & BOUNDARY)
266+
printf("%cboundary=yes", info_term);
267+
}
245268
}
246269
if (revs->print_parents) {
247270
struct commit_list *parents = commit->parents;
@@ -263,7 +286,7 @@ static void show_commit(struct commit *commit, void *data)
263286
if (revs->commit_format == CMIT_FMT_ONELINE)
264287
putchar(' ');
265288
else if (revs->include_header)
266-
putchar('\n');
289+
putchar(line_term);
267290

268291
if (revs->verbose_header) {
269292
struct strbuf buf = STRBUF_INIT;
@@ -357,10 +380,19 @@ static void show_object(struct object *obj, const char *name, void *cb_data)
357380
return;
358381
}
359382

360-
if (arg_show_object_names)
361-
show_object_with_name(stdout, obj, name);
362-
else
363-
printf("%s\n", oid_to_hex(&obj->oid));
383+
printf("%s", oid_to_hex(&obj->oid));
384+
385+
if (arg_show_object_names) {
386+
if (line_term) {
387+
putchar(info_term);
388+
for (const char *p = name; *p && *p != '\n'; p++)
389+
putchar(*p);
390+
} else if (*name) {
391+
printf("%cpath=%s", info_term, name);
392+
}
393+
}
394+
395+
putchar(line_term);
364396
}
365397

366398
static void show_edge(struct commit *commit)
@@ -634,19 +666,18 @@ int cmd_rev_list(int argc,
634666
if (!strcmp(arg, "--exclude-promisor-objects")) {
635667
fetch_if_missing = 0;
636668
revs.exclude_promisor_objects = 1;
637-
break;
638-
}
639-
}
640-
for (i = 1; i < argc; i++) {
641-
const char *arg = argv[i];
642-
if (skip_prefix(arg, "--missing=", &arg)) {
643-
if (revs.exclude_promisor_objects)
644-
die(_("options '%s' and '%s' cannot be used together"), "--exclude-promisor-objects", "--missing");
645-
if (parse_missing_action_value(arg))
646-
break;
669+
} else if (skip_prefix(arg, "--missing=", &arg)) {
670+
parse_missing_action_value(arg);
671+
} else if (!strcmp(arg, "-z")) {
672+
line_term = '\0';
673+
info_term = '\0';
647674
}
648675
}
649676

677+
die_for_incompatible_opt2(revs.exclude_promisor_objects,
678+
"--exclude_promisor_objects",
679+
arg_missing_action, "--missing");
680+
650681
if (arg_missing_action)
651682
revs.do_not_die_on_missing_objects = 1;
652683

@@ -755,6 +786,20 @@ int cmd_rev_list(int argc,
755786
usage(rev_list_usage);
756787

757788
}
789+
790+
/*
791+
* Reject options currently incompatible with -z. For some options, this
792+
* is not an inherent limitation and support may be implemented in the
793+
* future.
794+
*/
795+
if (!line_term) {
796+
if (revs.graph || revs.verbose_header || show_disk_usage ||
797+
info.show_timestamp || info.header_prefix || bisect_list ||
798+
use_bitmap_index || revs.edge_hint || revs.left_right ||
799+
revs.cherry_mark)
800+
die(_("-z option used with unsupported option"));
801+
}
802+
758803
if (revs.commit_format != CMIT_FMT_USERFORMAT)
759804
revs.include_header = 1;
760805
if (revs.commit_format != CMIT_FMT_UNSPECIFIED) {

revision.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ implement_shared_commit_slab(revision_sources, char *);
5959

6060
static inline int want_ancestry(const struct rev_info *revs);
6161

62-
void show_object_with_name(FILE *out, struct object *obj, const char *name)
63-
{
64-
fprintf(out, "%s ", oid_to_hex(&obj->oid));
65-
for (const char *p = name; *p && *p != '\n'; p++)
66-
fputc(*p, out);
67-
fputc('\n', out);
68-
}
69-
7062
static void mark_blob_uninteresting(struct blob *blob)
7163
{
7264
if (!blob)

revision.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,6 @@ void mark_parents_uninteresting(struct rev_info *revs, struct commit *commit);
489489
void mark_tree_uninteresting(struct repository *r, struct tree *tree);
490490
void mark_trees_uninteresting_sparse(struct repository *r, struct oidset *trees);
491491

492-
void show_object_with_name(FILE *, struct object *, const char *);
493-
494492
/**
495493
* Helpers to check if a reference should be excluded.
496494
*/

t/t6000-rev-list-misc.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,55 @@ test_expect_success 'rev-list --unpacked' '
182182
test_cmp expect actual
183183
'
184184

185+
test_expect_success 'rev-list -z' '
186+
test_when_finished rm -rf repo &&
187+
188+
git init repo &&
189+
test_commit -C repo 1 &&
190+
test_commit -C repo 2 &&
191+
192+
oid1=$(git -C repo rev-parse HEAD~) &&
193+
oid2=$(git -C repo rev-parse HEAD) &&
194+
195+
printf "%s\0%s\0" "$oid2" "$oid1" >expect &&
196+
git -C repo rev-list -z HEAD >actual &&
197+
198+
test_cmp expect actual
199+
'
200+
201+
test_expect_success 'rev-list -z --objects' '
202+
test_when_finished rm -rf repo &&
203+
204+
git init repo &&
205+
test_commit -C repo 1 &&
206+
test_commit -C repo 2 &&
207+
208+
oid1=$(git -C repo rev-parse HEAD:1.t) &&
209+
oid2=$(git -C repo rev-parse HEAD:2.t) &&
210+
path1=1.t &&
211+
path2=2.t &&
212+
213+
printf "%s\0path=%s\0%s\0path=%s\0" "$oid1" "$path1" "$oid2" "$path2" \
214+
>expect &&
215+
git -C repo rev-list -z --objects HEAD:1.t HEAD:2.t >actual &&
216+
217+
test_cmp expect actual
218+
'
219+
220+
test_expect_success 'rev-list -z --boundary' '
221+
test_when_finished rm -rf repo &&
222+
223+
git init repo &&
224+
test_commit -C repo 1 &&
225+
test_commit -C repo 2 &&
226+
227+
oid1=$(git -C repo rev-parse HEAD~) &&
228+
oid2=$(git -C repo rev-parse HEAD) &&
229+
230+
printf "%s\0%s\0boundary=yes\0" "$oid2" "$oid1" >expect &&
231+
git -C repo rev-list -z --boundary HEAD~.. >actual &&
232+
233+
test_cmp expect actual
234+
'
235+
185236
test_done

t/t6022-rev-list-missing.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,35 @@ do
198198
'
199199
done
200200

201+
test_expect_success "-z nul-delimited --missing" '
202+
test_when_finished rm -rf repo &&
203+
204+
git init repo &&
205+
(
206+
cd repo &&
207+
git commit --allow-empty -m first &&
208+
209+
path="foo bar" &&
210+
echo foobar >"$path" &&
211+
git add -A &&
212+
git commit -m second &&
213+
214+
oid=$(git rev-parse "HEAD:$path") &&
215+
type="$(git cat-file -t $oid)" &&
216+
217+
obj_path=".git/objects/$(test_oid_to_path $oid)" &&
218+
219+
git rev-list -z --objects --no-object-names \
220+
HEAD ^"$oid" >expect &&
221+
printf "%s\0missing=yes\0path=%s\0type=%s\0" "$oid" "$path" \
222+
"$type" >>expect &&
223+
224+
mv "$obj_path" "$obj_path.hidden" &&
225+
git rev-list -z --objects --no-object-names \
226+
--missing=print-info HEAD >actual &&
227+
228+
test_cmp expect actual
229+
)
230+
'
231+
201232
test_done

0 commit comments

Comments
 (0)