@@ -21,7 +21,7 @@ public function description(): string
2121
2222 public function usage (): string
2323 {
24- return 'branch [<name>] [-d <name>] [--unset-upstream [<name>]] ' ;
24+ return 'branch [<name>] [-d <name>] [-m <old> <new>] [-a] [--set-upstream-to=<upstream>] [- -unset-upstream [<name>]] ' ;
2525 }
2626
2727 /**
@@ -39,43 +39,61 @@ public function execute(array $args): int
3939 $ repo = Repository::discover ($ cwd );
4040 $ handler = new BranchHandler ($ repo );
4141
42- if ($ this ->isUnsetUpstreamRequest ($ args )) {
43- return $ this ->unsetUpstream ($ handler , $ args [1 ] ?? null );
44- }
45-
46- if ($ this ->isDeleteRequest ($ args )) {
47- return $ this ->deleteBranch ($ handler , $ args [1 ]);
48- }
49-
50- if ($ this ->isCreateRequest ($ args )) {
51- return $ this ->createBranch ($ handler , $ args [0 ]);
52- }
53-
54- return $ this ->listBranches ($ handler );
42+ return $ this ->dispatch ($ handler , $ args );
5543 }
5644
5745 /**
5846 * @param list<string> $args
5947 */
60- private function isUnsetUpstreamRequest ( array $ args ): bool
48+ private function dispatch ( BranchHandler $ handler , array $ args ): int
6149 {
62- return isset ($ args [0 ]) && $ args [0 ] === '--unset-upstream ' ;
50+ if ($ args === []) {
51+ return $ this ->listBranches ($ handler , false );
52+ }
53+
54+ return match ($ args [0 ]) {
55+ '--unset-upstream ' => $ this ->unsetUpstream ($ handler , $ args [1 ] ?? null ),
56+ '-d ' => isset ($ args [1 ]) ? $ this ->deleteBranch ($ handler , $ args [1 ]) : 1 ,
57+ '-m ' => isset ($ args [1 ], $ args [2 ]) ? $ this ->renameBranch ($ handler , $ args [1 ], $ args [2 ]) : 1 ,
58+ '-a ' => $ this ->listBranches ($ handler , true ),
59+ default => $ this ->handleDefault ($ handler , $ args ),
60+ };
6361 }
6462
6563 /**
6664 * @param list<string> $args
6765 */
68- private function isDeleteRequest ( array $ args ): bool
66+ private function handleDefault ( BranchHandler $ handler , array $ args ): int
6967 {
70- return isset ($ args [0 ]) && $ args [0 ] === '-d ' && isset ($ args [1 ]);
68+ $ setUpstream = $ this ->extractSetUpstreamTo ($ args );
69+ if ($ setUpstream !== null ) {
70+ $ handler ->setUpstreamTo ($ setUpstream );
71+ $ current = $ handler ->getCurrentBranch ();
72+ $ branchName = $ current instanceof \Lukasojd \PureGit \Domain \Ref \RefName ? $ current ->shortName () : 'HEAD ' ;
73+ fwrite (STDOUT , sprintf ("branch '%s' set up to track '%s'. \n" , $ branchName , $ setUpstream ));
74+
75+ return 0 ;
76+ }
77+
78+ if (isset ($ args [0 ]) && $ args [0 ] !== '' && $ args [0 ][0 ] !== '- ' ) {
79+ return $ this ->createBranch ($ handler , $ args [0 ]);
80+ }
81+
82+ return $ this ->listBranches ($ handler , false );
7183 }
7284
7385 /**
7486 * @param list<string> $args
7587 */
76- private function isCreateRequest (array $ args ): bool
88+ private function extractSetUpstreamTo (array $ args ): ? string
7789 {
78- return isset ($ args [0 ]) && $ args [0 ] !== '' && $ args [0 ][0 ] !== '- ' ;
90+ foreach ($ args as $ arg ) {
91+ if (str_starts_with ($ arg , '--set-upstream-to= ' )) {
92+ return substr ($ arg , strlen ('--set-upstream-to= ' ));
93+ }
94+ }
95+
96+ return null ;
7997 }
8098
8199 private function unsetUpstream (BranchHandler $ handler , ?string $ name ): int
@@ -93,6 +111,14 @@ private function deleteBranch(BranchHandler $handler, string $name): int
93111 return 0 ;
94112 }
95113
114+ private function renameBranch (BranchHandler $ handler , string $ oldName , string $ newName ): int
115+ {
116+ $ handler ->rename ($ oldName , $ newName );
117+ fwrite (STDOUT , sprintf ("Branch '%s' renamed to '%s' \n" , $ oldName , $ newName ));
118+
119+ return 0 ;
120+ }
121+
96122 private function createBranch (BranchHandler $ handler , string $ name ): int
97123 {
98124 $ handler ->create ($ name );
@@ -101,7 +127,7 @@ private function createBranch(BranchHandler $handler, string $name): int
101127 return 0 ;
102128 }
103129
104- private function listBranches (BranchHandler $ handler ): int
130+ private function listBranches (BranchHandler $ handler, bool $ all ): int
105131 {
106132 $ branches = $ handler ->list ();
107133 $ currentBranch = $ handler ->getCurrentBranch ();
@@ -113,6 +139,14 @@ private function listBranches(BranchHandler $handler): int
113139 fwrite (STDOUT , sprintf ("%s%s \n" , $ prefix , $ short ));
114140 }
115141
142+ if ($ all ) {
143+ $ remotes = $ handler ->listRemote ();
144+ foreach (array_keys ($ remotes ) as $ refName ) {
145+ $ short = str_replace ('refs/ ' , '' , $ refName );
146+ fwrite (STDOUT , sprintf (" %s \n" , $ short ));
147+ }
148+ }
149+
116150 return 0 ;
117151 }
118152}
0 commit comments