@@ -651,35 +651,37 @@ static int add(int ac, const char **av, const char *prefix)
651651 return add_worktree (path , branch , & opts );
652652}
653653
654- static void show_worktree_porcelain (struct worktree * wt )
654+ static void show_worktree_porcelain (struct worktree * wt , int line_terminator )
655655{
656656 const char * reason ;
657657
658- printf ("worktree %s\n " , wt -> path );
658+ printf ("worktree %s%c " , wt -> path , line_terminator );
659659 if (wt -> is_bare )
660- printf ("bare\n" );
660+ printf ("bare%c" , line_terminator );
661661 else {
662- printf ("HEAD %s\n " , oid_to_hex (& wt -> head_oid ));
662+ printf ("HEAD %s%c " , oid_to_hex (& wt -> head_oid ), line_terminator );
663663 if (wt -> is_detached )
664- printf ("detached\n" );
664+ printf ("detached%c" , line_terminator );
665665 else if (wt -> head_ref )
666- printf ("branch %s\n " , wt -> head_ref );
666+ printf ("branch %s%c " , wt -> head_ref , line_terminator );
667667 }
668668
669669 reason = worktree_lock_reason (wt );
670- if (reason && * reason ) {
671- struct strbuf sb = STRBUF_INIT ;
672- quote_c_style (reason , & sb , NULL , 0 );
673- printf ("locked %s\n" , sb .buf );
674- strbuf_release (& sb );
675- } else if (reason )
676- printf ("locked\n" );
670+ if (reason ) {
671+ fputs ("locked" , stdout );
672+ if (* reason ) {
673+ fputc (' ' , stdout );
674+ write_name_quoted (reason , stdout , line_terminator );
675+ } else {
676+ fputc (line_terminator , stdout );
677+ }
678+ }
677679
678680 reason = worktree_prune_reason (wt , expire );
679681 if (reason )
680- printf ("prunable %s\n " , reason );
682+ printf ("prunable %s%c " , reason , line_terminator );
681683
682- printf ( "\n" );
684+ fputc ( line_terminator , stdout );
683685}
684686
685687static void show_worktree (struct worktree * wt , int path_maxlen , int abbrev_len )
@@ -757,12 +759,15 @@ static void pathsort(struct worktree **wt)
757759static int list (int ac , const char * * av , const char * prefix )
758760{
759761 int porcelain = 0 ;
762+ int line_terminator = '\n' ;
760763
761764 struct option options [] = {
762765 OPT_BOOL (0 , "porcelain" , & porcelain , N_ ("machine-readable output" )),
763766 OPT__VERBOSE (& verbose , N_ ("show extended annotations and reasons, if available" )),
764767 OPT_EXPIRY_DATE (0 , "expire" , & expire ,
765768 N_ ("add 'prunable' annotation to worktrees older than <time>" )),
769+ OPT_SET_INT ('z' , NULL , & line_terminator ,
770+ N_ ("terminate records with a NUL character" ), '\0' ),
766771 OPT_END ()
767772 };
768773
@@ -772,6 +777,8 @@ static int list(int ac, const char **av, const char *prefix)
772777 usage_with_options (worktree_usage , options );
773778 else if (verbose && porcelain )
774779 die (_ ("options '%s' and '%s' cannot be used together" ), "--verbose" , "--porcelain" );
780+ else if (!line_terminator && !porcelain )
781+ die (_ ("the option '%s' requires '%s'" ), "-z" , "--porcelain" );
775782 else {
776783 struct worktree * * worktrees = get_worktrees ();
777784 int path_maxlen = 0 , abbrev = DEFAULT_ABBREV , i ;
@@ -784,7 +791,8 @@ static int list(int ac, const char **av, const char *prefix)
784791
785792 for (i = 0 ; worktrees [i ]; i ++ ) {
786793 if (porcelain )
787- show_worktree_porcelain (worktrees [i ]);
794+ show_worktree_porcelain (worktrees [i ],
795+ line_terminator );
788796 else
789797 show_worktree (worktrees [i ], path_maxlen , abbrev );
790798 }
0 commit comments