@@ -8,49 +8,46 @@ pub fn run(name: Option<String>) -> Result<()> {
88 bail ! ( "Not a git repository" ) ;
99 }
1010
11- match name {
11+ if let Some ( branch ) = name {
1212 // Switch to branch
13- Some ( branch) => {
14- util:: info ( & format ! ( "Switching to {branch}..." ) ) ;
13+ util:: info ( & format ! ( "Switching to {branch}..." ) ) ;
1514
16- let status = Command :: new ( "git" ) . args ( [ "checkout" , & branch] ) . status ( ) ?;
15+ let status = Command :: new ( "git" ) . args ( [ "checkout" , & branch] ) . status ( ) ?;
16+
17+ if status. success ( ) {
18+ util:: ok ( & format ! ( "Now on {branch}" ) ) ;
19+ } else {
20+ // Maybe it's a new branch?
21+ util:: info ( "Branch not found, creating it..." ) ;
22+ let status = Command :: new ( "git" )
23+ . args ( [ "checkout" , "-b" , & branch] )
24+ . status ( ) ?;
1725
1826 if status. success ( ) {
19- util:: ok ( & format ! ( "Now on {branch}" ) ) ;
27+ util:: ok ( & format ! ( "Created and switched to {branch}" ) ) ;
2028 } else {
21- // Maybe it's a new branch?
22- util:: info ( "Branch not found, creating it..." ) ;
23- let status = Command :: new ( "git" )
24- . args ( [ "checkout" , "-b" , & branch] )
25- . status ( ) ?;
26-
27- if status. success ( ) {
28- util:: ok ( & format ! ( "Created and switched to {branch}" ) ) ;
29- } else {
30- util:: err ( "Could not switch branch" ) ;
31- }
29+ util:: err ( "Could not switch branch" ) ;
3230 }
3331 }
32+ } else {
3433 // List branches
35- None => {
36- let current = git:: currentbranch ( ) . unwrap_or_default ( ) ;
37-
38- let output = Command :: new ( "git" ) . args ( [ "branch" , "--list" ] ) . output ( ) ?;
39-
40- let text = String :: from_utf8_lossy ( & output. stdout ) ;
41-
42- println ! ( ) ;
43- util:: info ( "Branches:" ) ;
44- for line in text. lines ( ) {
45- let name = line. trim ( ) . trim_start_matches ( "* " ) ;
46- if name == current {
47- println ! ( " \x1b [32m▶ {}\x1b [0m (current)" , name) ;
48- } else {
49- util:: dim ( & format ! ( " {name}" ) ) ;
50- }
34+ let current = git:: currentbranch ( ) . unwrap_or_default ( ) ;
35+
36+ let output = Command :: new ( "git" ) . args ( [ "branch" , "--list" ] ) . output ( ) ?;
37+
38+ let text = String :: from_utf8_lossy ( & output. stdout ) ;
39+
40+ println ! ( ) ;
41+ util:: info ( "Branches:" ) ;
42+ for line in text. lines ( ) {
43+ let name = line. trim ( ) . trim_start_matches ( "* " ) ;
44+ if name == current {
45+ println ! ( " \x1b [32m▶ {name}\x1b [0m (current)" ) ;
46+ } else {
47+ util:: dim ( & format ! ( " {name}" ) ) ;
5148 }
52- println ! ( ) ;
5349 }
50+ println ! ( ) ;
5451 }
5552
5653 Ok ( ( ) )
0 commit comments