@@ -39,7 +39,7 @@ static void expand_objectsize(struct strbuf *line, const struct object_id *oid,
39
39
}
40
40
41
41
struct ls_tree_options {
42
- int line_termination ;
42
+ unsigned null_termination : 1 ;
43
43
int abbrev ;
44
44
enum ls_tree_path_options {
45
45
LS_RECURSIVE = 1 << 0 ,
@@ -166,7 +166,7 @@ static int show_tree_fmt(const struct object_id *oid, struct strbuf *base,
166
166
167
167
baselen = base -> len ;
168
168
strbuf_expand (& sb , options -> format , expand_show_tree , & cb_data );
169
- strbuf_addch (& sb , options -> line_termination );
169
+ strbuf_addch (& sb , options -> null_termination ? '\0' : '\n' );
170
170
fwrite (sb .buf , sb .len , 1 , stdout );
171
171
strbuf_release (& sb );
172
172
strbuf_setlen (base , baselen );
@@ -198,10 +198,22 @@ static void show_tree_common_default_long(struct ls_tree_options *options,
198
198
const char * pathname ,
199
199
const size_t baselen )
200
200
{
201
+ const char * prefix = options -> chomp_prefix ? options -> ls_tree_prefix : NULL ;
202
+
201
203
strbuf_addstr (base , pathname );
202
- write_name_quoted_relative (base -> buf ,
203
- options -> chomp_prefix ? options -> ls_tree_prefix : NULL , stdout ,
204
- options -> line_termination );
204
+
205
+ if (options -> null_termination ) {
206
+ struct strbuf sb = STRBUF_INIT ;
207
+ const char * name = relative_path (base -> buf , prefix , & sb );
208
+
209
+ fputs (name , stdout );
210
+ fputc ('\0' , stdout );
211
+
212
+ strbuf_release (& sb );
213
+ } else {
214
+ write_name_quoted_relative (base -> buf , prefix , stdout , '\n' );
215
+ }
216
+
205
217
strbuf_setlen (base , baselen );
206
218
}
207
219
@@ -264,15 +276,25 @@ static int show_tree_name_only(const struct object_id *oid, struct strbuf *base,
264
276
int recurse ;
265
277
const size_t baselen = base -> len ;
266
278
enum object_type type = object_type (mode );
279
+ const char * prefix ;
267
280
268
281
early = show_tree_common (options , & recurse , base , pathname , type );
269
282
if (early >= 0 )
270
283
return early ;
271
284
285
+ prefix = options -> chomp_prefix ? options -> ls_tree_prefix : NULL ;
272
286
strbuf_addstr (base , pathname );
273
- write_name_quoted_relative (base -> buf ,
274
- options -> chomp_prefix ? options -> ls_tree_prefix : NULL ,
275
- stdout , options -> line_termination );
287
+ if (options -> null_termination ) {
288
+ struct strbuf sb = STRBUF_INIT ;
289
+ const char * name = relative_path (base -> buf , prefix , & sb );
290
+
291
+ fputs (name , stdout );
292
+ fputc ('\0' , stdout );
293
+
294
+ strbuf_release (& sb );
295
+ } else {
296
+ write_name_quoted_relative (base -> buf , prefix , stdout , '\n' );
297
+ }
276
298
strbuf_setlen (base , baselen );
277
299
return recurse ;
278
300
}
@@ -285,12 +307,19 @@ static int show_tree_object(const struct object_id *oid, struct strbuf *base,
285
307
int early ;
286
308
int recurse ;
287
309
enum object_type type = object_type (mode );
310
+ const char * str ;
288
311
289
312
early = show_tree_common (options , & recurse , base , pathname , type );
290
313
if (early >= 0 )
291
314
return early ;
292
315
293
- printf ("%s%c" , find_unique_abbrev (oid , options -> abbrev ), options -> line_termination );
316
+ str = find_unique_abbrev (oid , options -> abbrev );
317
+ if (options -> null_termination ) {
318
+ fputs (str , stdout );
319
+ fputc ('\0' , stdout );
320
+ } else {
321
+ puts (str );
322
+ }
294
323
return recurse ;
295
324
}
296
325
@@ -342,18 +371,17 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
342
371
int i , full_tree = 0 ;
343
372
read_tree_fn_t fn = NULL ;
344
373
enum ls_tree_cmdmode cmdmode = MODE_DEFAULT ;
345
- struct ls_tree_options options = {
346
- .line_termination = '\n' ,
347
- };
374
+ int null_termination = 0 ;
375
+ struct ls_tree_options options = { 0 };
348
376
const struct option ls_tree_options [] = {
349
377
OPT_BIT ('d' , NULL , & options .ls_options , N_ ("only show trees" ),
350
378
LS_TREE_ONLY ),
351
379
OPT_BIT ('r' , NULL , & options .ls_options , N_ ("recurse into subtrees" ),
352
380
LS_RECURSIVE ),
353
381
OPT_BIT ('t' , NULL , & options .ls_options , N_ ("show trees when recursing" ),
354
382
LS_SHOW_TREES ),
355
- OPT_SET_INT ('z' , NULL , & options . line_termination ,
356
- N_ ("terminate entries with NUL byte" ), 0 ),
383
+ OPT_BOOL ('z' , NULL , & null_termination ,
384
+ N_ ("terminate entries with NUL byte" )),
357
385
OPT_CMDMODE ('l' , "long" , & cmdmode , N_ ("include object size" ),
358
386
MODE_LONG ),
359
387
OPT_CMDMODE (0 , "name-only" , & cmdmode , N_ ("list only filenames" ),
@@ -383,6 +411,8 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
383
411
384
412
argc = parse_options (argc , argv , prefix , ls_tree_options ,
385
413
ls_tree_usage , 0 );
414
+ options .null_termination = null_termination ;
415
+
386
416
if (full_tree ) {
387
417
options .ls_tree_prefix = prefix = NULL ;
388
418
options .chomp_prefix = 0 ;
0 commit comments