Skip to content

Commit 7fea6bc

Browse files
committed
Merge branch 'jt/rev-list-z' into seen
* jt/rev-list-z: rev-list: support NUL-delimited --missing 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 4d16673 + 57834dc commit 7fea6bc

File tree

6 files changed

+155
-31
lines changed

6 files changed

+155
-31
lines changed

Documentation/rev-list-options.adoc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,32 @@ 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 is delimited
367+
with two NUL bytes in the following form:
368+
+
369+
-----------------------------------------------------------------------
370+
<OID> NUL NUL
371+
-----------------------------------------------------------------------
372+
+
373+
When the `--objects` option is also present, available object name information
374+
is printed in the following form without any truncation for object names
375+
containing newline characters:
376+
+
377+
-----------------------------------------------------------------------
378+
<OID> [NUL <object-name>] NUL NUL
379+
-----------------------------------------------------------------------
380+
+
381+
When the `--missing` option is provided, missing objects are printed in the
382+
following form where value is printed as-is without any token specific
383+
encoding:
384+
+
385+
-----------------------------------------------------------------------
386+
?<OID> [NUL <token>=<value>]... NUL NUL
387+
-----------------------------------------------------------------------
388+
+
389+
This option is only compatible with `--objects` and `--missing`.
364390
endif::git-rev-list[]
365391

366392
History Simplification

builtin/rev-list.c

Lines changed: 65 additions & 21 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,10 +98,23 @@ static int arg_show_object_names = 1;
9698

9799
#define DEFAULT_OIDSET_SIZE (16*1024)
98100

101+
static int nul_delim;
99102
static int show_disk_usage;
100103
static off_t total_disk_usage;
101104
static int human_readable;
102105

106+
static void print_object_term(int nul_delim)
107+
{
108+
char line_sep = '\n';
109+
110+
if (nul_delim)
111+
line_sep = '\0';
112+
113+
putchar(line_sep);
114+
if (nul_delim)
115+
putchar(line_sep);
116+
}
117+
103118
static off_t get_object_disk_usage(struct object *obj)
104119
{
105120
off_t size;
@@ -130,25 +145,38 @@ static void print_missing_object(struct missing_objects_map_entry *entry,
130145
int print_missing_info)
131146
{
132147
struct strbuf sb = STRBUF_INIT;
148+
char info_sep = ' ';
149+
150+
if (nul_delim)
151+
info_sep = '\0';
152+
153+
printf("?%s", oid_to_hex(&entry->entry.oid));
133154

134155
if (!print_missing_info) {
135-
printf("?%s\n", oid_to_hex(&entry->entry.oid));
156+
print_object_term(nul_delim);
136157
return;
137158
}
138159

139160
if (entry->path && *entry->path) {
140161
struct strbuf path = STRBUF_INIT;
141162

142-
strbuf_addstr(&sb, " path=");
143-
quote_path(entry->path, NULL, &path, QUOTE_PATH_QUOTE_SP);
144-
strbuf_addbuf(&sb, &path);
163+
strbuf_addf(&sb, "%cpath=", info_sep);
164+
165+
if (nul_delim) {
166+
strbuf_addstr(&sb, entry->path);
167+
} else {
168+
quote_path(entry->path, NULL, &path, QUOTE_PATH_QUOTE_SP);
169+
strbuf_addbuf(&sb, &path);
170+
}
145171

146172
strbuf_release(&path);
147173
}
148174
if (entry->type)
149-
strbuf_addf(&sb, " type=%s", type_name(entry->type));
175+
strbuf_addf(&sb, "%ctype=%s", info_sep, type_name(entry->type));
176+
177+
fwrite(sb.buf, sizeof(char), sb.len, stdout);
178+
print_object_term(nul_delim);
150179

151-
printf("?%s%s\n", oid_to_hex(&entry->entry.oid), sb.buf);
152180
strbuf_release(&sb);
153181
}
154182

@@ -263,7 +291,7 @@ static void show_commit(struct commit *commit, void *data)
263291
if (revs->commit_format == CMIT_FMT_ONELINE)
264292
putchar(' ');
265293
else if (revs->include_header)
266-
putchar('\n');
294+
print_object_term(nul_delim);
267295

268296
if (revs->verbose_header) {
269297
struct strbuf buf = STRBUF_INIT;
@@ -357,10 +385,20 @@ static void show_object(struct object *obj, const char *name, void *cb_data)
357385
return;
358386
}
359387

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));
388+
printf("%s", oid_to_hex(&obj->oid));
389+
390+
if (arg_show_object_names) {
391+
if (nul_delim && *name) {
392+
putchar('\0');
393+
printf("%s", name);
394+
} else if (!nul_delim) {
395+
putchar(' ');
396+
for (const char *p = name; *p && *p != '\n'; p++)
397+
putchar(*p);
398+
}
399+
}
400+
401+
print_object_term(nul_delim);
364402
}
365403

366404
static void show_edge(struct commit *commit)
@@ -634,19 +672,17 @@ int cmd_rev_list(int argc,
634672
if (!strcmp(arg, "--exclude-promisor-objects")) {
635673
fetch_if_missing = 0;
636674
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;
675+
} else if (skip_prefix(arg, "--missing=", &arg)) {
676+
parse_missing_action_value(arg);
677+
} else if (!strcmp(arg, "-z")) {
678+
nul_delim = 1;
647679
}
648680
}
649681

682+
die_for_incompatible_opt2(revs.exclude_promisor_objects,
683+
"--exclude_promisor_objects",
684+
arg_missing_action, "--missing");
685+
650686
if (arg_missing_action)
651687
revs.do_not_die_on_missing_objects = 1;
652688

@@ -755,6 +791,14 @@ int cmd_rev_list(int argc,
755791
usage(rev_list_usage);
756792

757793
}
794+
795+
if (nul_delim) {
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)
799+
die(_("-z option used with unsupported option"));
800+
}
801+
758802
if (revs.commit_format != CMIT_FMT_USERFORMAT)
759803
revs.include_header = 1;
760804
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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,38 @@ 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\0%s\0\0" "$oid1" "$oid2" >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\0%s\0\0%s\0%s\0\0" "$oid1" "$path1" "$oid2" "$path2" >expect &&
214+
git -C repo rev-list -z --objects HEAD:1.t HEAD:2.t >actual &&
215+
216+
test_cmp expect actual
217+
'
218+
185219
test_done

t/t6022-rev-list-missing.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,34 @@ 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\0path=%s\0type=%s\0\0" "$oid" "$path" "$type" >>expect &&
222+
223+
mv "$obj_path" "$obj_path.hidden" &&
224+
git rev-list -z --objects --no-object-names \
225+
--missing=print-info HEAD >actual &&
226+
227+
test_cmp expect actual
228+
)
229+
'
230+
201231
test_done

0 commit comments

Comments
 (0)