@@ -325,7 +325,7 @@ static int add_worktree(const char *path, const char *refname,
325325 struct strbuf sb_name = STRBUF_INIT ;
326326 struct worktree * * worktrees ;
327327
328- worktrees = get_worktrees (0 );
328+ worktrees = get_worktrees ();
329329 check_candidate_path (path , opts -> force , worktrees , "add" );
330330 free_worktrees (worktrees );
331331 worktrees = NULL ;
@@ -697,6 +697,23 @@ static void measure_widths(struct worktree **wt, int *abbrev, int *maxlen)
697697 }
698698}
699699
700+ static int pathcmp (const void * a_ , const void * b_ )
701+ {
702+ const struct worktree * const * a = a_ ;
703+ const struct worktree * const * b = b_ ;
704+ return fspathcmp ((* a )-> path , (* b )-> path );
705+ }
706+
707+ static void pathsort (struct worktree * * wt )
708+ {
709+ int n = 0 ;
710+ struct worktree * * p = wt ;
711+
712+ while (* p ++ )
713+ n ++ ;
714+ QSORT (wt , n , pathcmp );
715+ }
716+
700717static int list (int ac , const char * * av , const char * prefix )
701718{
702719 int porcelain = 0 ;
@@ -710,9 +727,12 @@ static int list(int ac, const char **av, const char *prefix)
710727 if (ac )
711728 usage_with_options (worktree_usage , options );
712729 else {
713- struct worktree * * worktrees = get_worktrees (GWT_SORT_LINKED );
730+ struct worktree * * worktrees = get_worktrees ();
714731 int path_maxlen = 0 , abbrev = DEFAULT_ABBREV , i ;
715732
733+ /* sort worktrees by path but keep main worktree at top */
734+ pathsort (worktrees + 1 );
735+
716736 if (!porcelain )
717737 measure_widths (worktrees , & abbrev , & path_maxlen );
718738
@@ -741,7 +761,7 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
741761 if (ac != 1 )
742762 usage_with_options (worktree_usage , options );
743763
744- worktrees = get_worktrees (0 );
764+ worktrees = get_worktrees ();
745765 wt = find_worktree (worktrees , prefix , av [0 ]);
746766 if (!wt )
747767 die (_ ("'%s' is not a working tree" ), av [0 ]);
@@ -774,7 +794,7 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
774794 if (ac != 1 )
775795 usage_with_options (worktree_usage , options );
776796
777- worktrees = get_worktrees (0 );
797+ worktrees = get_worktrees ();
778798 wt = find_worktree (worktrees , prefix , av [0 ]);
779799 if (!wt )
780800 die (_ ("'%s' is not a working tree" ), av [0 ]);
@@ -848,7 +868,7 @@ static int move_worktree(int ac, const char **av, const char *prefix)
848868 strbuf_addstr (& dst , path );
849869 free (path );
850870
851- worktrees = get_worktrees (0 );
871+ worktrees = get_worktrees ();
852872 wt = find_worktree (worktrees , prefix , av [0 ]);
853873 if (!wt )
854874 die (_ ("'%s' is not a working tree" ), av [0 ]);
@@ -974,7 +994,7 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
974994 if (ac != 1 )
975995 usage_with_options (worktree_usage , options );
976996
977- worktrees = get_worktrees (0 );
997+ worktrees = get_worktrees ();
978998 wt = find_worktree (worktrees , prefix , av [0 ]);
979999 if (!wt )
9801000 die (_ ("'%s' is not a working tree" ), av [0 ]);
0 commit comments