@@ -575,35 +575,37 @@ static int add(int ac, const char **av, const char *prefix)
575575 return add_worktree (path , branch , & opts );
576576}
577577
578- static void show_worktree_porcelain (struct worktree * wt )
578+ static void show_worktree_porcelain (struct worktree * wt , int line_terminator )
579579{
580580 const char * reason ;
581581
582- printf ("worktree %s\n " , wt -> path );
582+ printf ("worktree %s%c " , wt -> path , line_terminator );
583583 if (wt -> is_bare )
584- printf ("bare\n" );
584+ printf ("bare%c" , line_terminator );
585585 else {
586- printf ("HEAD %s\n " , oid_to_hex (& wt -> head_oid ));
586+ printf ("HEAD %s%c " , oid_to_hex (& wt -> head_oid ), line_terminator );
587587 if (wt -> is_detached )
588- printf ("detached\n" );
588+ printf ("detached%c" , line_terminator );
589589 else if (wt -> head_ref )
590- printf ("branch %s\n " , wt -> head_ref );
590+ printf ("branch %s%c " , wt -> head_ref , line_terminator );
591591 }
592592
593593 reason = worktree_lock_reason (wt );
594- if (reason && * reason ) {
595- struct strbuf sb = STRBUF_INIT ;
596- quote_c_style (reason , & sb , NULL , 0 );
597- printf ("locked %s\n" , sb .buf );
598- strbuf_release (& sb );
599- } else if (reason )
600- printf ("locked\n" );
594+ if (reason ) {
595+ fputs ("locked" , stdout );
596+ if (* reason ) {
597+ fputc (' ' , stdout );
598+ write_name_quoted (reason , stdout , line_terminator );
599+ } else {
600+ fputc (line_terminator , stdout );
601+ }
602+ }
601603
602604 reason = worktree_prune_reason (wt , expire );
603605 if (reason )
604- printf ("prunable %s\n " , reason );
606+ printf ("prunable %s%c " , reason , line_terminator );
605607
606- printf ( "\n" );
608+ fputc ( line_terminator , stdout );
607609}
608610
609611static void show_worktree (struct worktree * wt , int path_maxlen , int abbrev_len )
@@ -681,12 +683,15 @@ static void pathsort(struct worktree **wt)
681683static int list (int ac , const char * * av , const char * prefix )
682684{
683685 int porcelain = 0 ;
686+ int line_terminator = '\n' ;
684687
685688 struct option options [] = {
686689 OPT_BOOL (0 , "porcelain" , & porcelain , N_ ("machine-readable output" )),
687690 OPT__VERBOSE (& verbose , N_ ("show extended annotations and reasons, if available" )),
688691 OPT_EXPIRY_DATE (0 , "expire" , & expire ,
689692 N_ ("add 'prunable' annotation to worktrees older than <time>" )),
693+ OPT_SET_INT ('z' , NULL , & line_terminator ,
694+ N_ ("terminate records with a NUL character" ), '\0' ),
690695 OPT_END ()
691696 };
692697
@@ -696,6 +701,8 @@ static int list(int ac, const char **av, const char *prefix)
696701 usage_with_options (worktree_usage , options );
697702 else if (verbose && porcelain )
698703 die (_ ("options '%s' and '%s' cannot be used together" ), "--verbose" , "--porcelain" );
704+ else if (!line_terminator && !porcelain )
705+ die (_ ("the option '%s' requires '%s'" ), "-z" , "--porcelain" );
699706 else {
700707 struct worktree * * worktrees = get_worktrees ();
701708 int path_maxlen = 0 , abbrev = DEFAULT_ABBREV , i ;
@@ -708,7 +715,8 @@ static int list(int ac, const char **av, const char *prefix)
708715
709716 for (i = 0 ; worktrees [i ]; i ++ ) {
710717 if (porcelain )
711- show_worktree_porcelain (worktrees [i ]);
718+ show_worktree_porcelain (worktrees [i ],
719+ line_terminator );
712720 else
713721 show_worktree (worktrees [i ], path_maxlen , abbrev );
714722 }
0 commit comments