@@ -975,14 +975,18 @@ static void show_worktree_porcelain(struct worktree *wt, int line_terminator)
975975 fputc (line_terminator , stdout );
976976}
977977
978- static void show_worktree (struct worktree * wt , int path_maxlen , int abbrev_len )
978+ struct worktree_display {
979+ char * path ;
980+ int width ;
981+ };
982+
983+ static void show_worktree (struct worktree * wt , struct worktree_display * display ,
984+ int path_maxwidth , int abbrev_len )
979985{
980986 struct strbuf sb = STRBUF_INIT ;
981- int cur_path_len = strlen (wt -> path );
982- int path_adj = cur_path_len - utf8_strwidth (wt -> path );
983987 const char * reason ;
984988
985- strbuf_addf (& sb , "%-*s " , 1 + path_maxlen + path_adj , wt -> path );
989+ strbuf_addf (& sb , "%s%*s " , display -> path , 1 + path_maxwidth - display -> width , "" );
986990 if (wt -> is_bare )
987991 strbuf_addstr (& sb , "(bare)" );
988992 else {
@@ -1016,20 +1020,27 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len)
10161020 strbuf_release (& sb );
10171021}
10181022
1019- static void measure_widths (struct worktree * * wt , int * abbrev , int * maxlen )
1023+ static void measure_widths (struct worktree * * wt , int * abbrev ,
1024+ struct worktree_display * * d , int * maxwidth )
10201025{
1021- int i ;
1026+ int i , display_alloc = 0 ;
1027+ struct worktree_display * display = NULL ;
1028+ struct strbuf buf = STRBUF_INIT ;
10221029
10231030 for (i = 0 ; wt [i ]; i ++ ) {
10241031 int sha1_len ;
1025- int path_len = strlen (wt [i ]-> path );
1032+ ALLOC_GROW (display , i + 1 , display_alloc );
1033+ quote_path (wt [i ]-> path , NULL , & buf , 0 );
1034+ display [i ].width = utf8_strwidth (buf .buf );
1035+ display [i ].path = strbuf_detach (& buf , NULL );
10261036
1027- if (path_len > * maxlen )
1028- * maxlen = path_len ;
1037+ if (display [ i ]. width > * maxwidth )
1038+ * maxwidth = display [ i ]. width ;
10291039 sha1_len = strlen (repo_find_unique_abbrev (the_repository , & wt [i ]-> head_oid , * abbrev ));
10301040 if (sha1_len > * abbrev )
10311041 * abbrev = sha1_len ;
10321042 }
1043+ * d = display ;
10331044}
10341045
10351046static int pathcmp (const void * a_ , const void * b_ )
@@ -1075,21 +1086,27 @@ static int list(int ac, const char **av, const char *prefix,
10751086 die (_ ("the option '%s' requires '%s'" ), "-z" , "--porcelain" );
10761087 else {
10771088 struct worktree * * worktrees = get_worktrees ();
1078- int path_maxlen = 0 , abbrev = DEFAULT_ABBREV , i ;
1089+ int path_maxwidth = 0 , abbrev = DEFAULT_ABBREV , i ;
1090+ struct worktree_display * display = NULL ;
10791091
10801092 /* sort worktrees by path but keep main worktree at top */
10811093 pathsort (worktrees + 1 );
10821094
10831095 if (!porcelain )
1084- measure_widths (worktrees , & abbrev , & path_maxlen );
1096+ measure_widths (worktrees , & abbrev ,
1097+ & display , & path_maxwidth );
10851098
10861099 for (i = 0 ; worktrees [i ]; i ++ ) {
10871100 if (porcelain )
10881101 show_worktree_porcelain (worktrees [i ],
10891102 line_terminator );
10901103 else
1091- show_worktree (worktrees [i ], path_maxlen , abbrev );
1104+ show_worktree (worktrees [i ],
1105+ & display [i ], path_maxwidth , abbrev );
10921106 }
1107+ for (i = 0 ; display && worktrees [i ]; i ++ )
1108+ free (display [i ].path );
1109+ free (display );
10931110 free_worktrees (worktrees );
10941111 }
10951112 return 0 ;
0 commit comments