1
+ use colored:: * ;
1
2
use std:: fs;
2
3
use std:: path:: Path ;
3
4
use std:: path:: PathBuf ;
@@ -28,7 +29,7 @@ pub fn get_repositories(path: &Path) -> Vec<PathBuf> {
28
29
repositories
29
30
}
30
31
31
- pub fn git_current_branch ( repository : PathBuf ) -> String {
32
+ pub fn git_current_branch ( repository : & Path ) -> String {
32
33
let command_string = "git rev-parse --abbrev-ref HEAD" ;
33
34
let ( command, args) = build_command ( command_string) ;
34
35
let ( error, output) = run_command ( command, args, repository) ;
@@ -46,16 +47,33 @@ pub fn sync_repository_to_branch(repository: PathBuf, branch: &str) {
46
47
println ! ( "!> Syncing repository: [{}]" , repo_name) ;
47
48
48
49
println ! ( "\t !> Running git fetch" ) ;
49
- git_fetch ( repository. to_path_buf ( ) ) ;
50
+ git_fetch ( & repository) ;
50
51
51
- println ! ( "\t !> Running git stash" ) ;
52
- git_stash ( repository. to_path_buf ( ) ) ;
52
+ let should_stash = git_has_changes ( & repository) ;
53
+
54
+ if should_stash {
55
+ println ! ( "\t !> Running git stash" ) ;
56
+ git_stash ( & repository) ;
57
+ }
53
58
54
59
println ! ( "\t !> Running git checkout {}" , & branch) ;
55
- git_checkout ( repository. to_path_buf ( ) , branch. to_string ( ) ) ;
60
+ let ( err, _) = git_checkout ( & repository, branch. to_string ( ) ) ;
61
+
62
+ if !err. is_empty ( ) && err. contains ( "did not match any file(s) known to git" ) {
63
+ println ! ( "\t {}" , "!!> Branch does not exist on this repository" . red( ) ) ;
64
+ }
56
65
57
66
println ! ( "\t !> Running git pull" ) ;
58
- git_pull ( repository. to_path_buf ( ) ) ;
67
+ let ( err, _) = git_pull ( & repository) ;
68
+
69
+ if !err. is_empty ( ) && err. contains ( "but no such ref was fetched" ) {
70
+ println ! (
71
+ "\t {}" ,
72
+ "!!> Branch does not exist on the remote, most probably remote branch was removed"
73
+ . red( )
74
+ ) ;
75
+ }
76
+
59
77
println ! ( "\n " ) ;
60
78
}
61
79
@@ -72,7 +90,7 @@ fn build_command(command: &str) -> (String, Vec<&str>) {
72
90
( command. to_string ( ) , args. to_vec ( ) )
73
91
}
74
92
75
- fn run_command ( command : String , args : Vec < & str > , cwd : PathBuf ) -> ( String , String ) {
93
+ fn run_command ( command : String , args : Vec < & str > , cwd : & Path ) -> ( String , String ) {
76
94
let command = match Command :: new ( command) . current_dir ( cwd) . args ( args) . output ( ) {
77
95
Err ( err) => panic ! ( "Error running command [{}]" , err) ,
78
96
Ok ( cmd) => cmd,
@@ -86,26 +104,33 @@ fn run_command(command: String, args: Vec<&str>, cwd: PathBuf) -> (String, Strin
86
104
( error, output)
87
105
}
88
106
89
- fn git_checkout ( repository : PathBuf , branch : String ) {
107
+ fn git_checkout ( repository : & Path , branch : String ) -> ( String , String ) {
90
108
let command_string = format ! ( "git checkout {}" , branch) ;
91
109
let ( command, args) = build_command ( & command_string) ;
92
- run_command ( command, args, repository) ;
110
+ run_command ( command, args, repository)
93
111
}
94
112
95
- fn git_pull ( repository : PathBuf ) {
113
+ fn git_pull ( repository : & Path ) -> ( String , String ) {
96
114
let ( command, args) = build_command ( "git pull" ) ;
97
- run_command ( command, args, repository) ;
115
+ run_command ( command, args, repository)
98
116
}
99
117
100
- fn git_fetch ( repository : PathBuf ) {
118
+ fn git_fetch ( repository : & Path ) -> ( String , String ) {
101
119
let command_string = "git fetch" ;
102
120
let ( command, args) = build_command ( command_string) ;
103
- run_command ( command, args, repository) ;
121
+ run_command ( command, args, repository)
104
122
}
105
123
106
- fn git_stash ( repository : PathBuf ) {
107
- // TODO save unique stash message
124
+ fn git_stash ( repository : & Path ) -> ( String , String ) {
108
125
let command_string = "git stash save 'switcher:: changes'" ;
109
126
let ( command, args) = build_command ( command_string) ;
110
- run_command ( command, args, repository) ;
127
+ run_command ( command, args, repository)
128
+ }
129
+
130
+ fn git_has_changes ( repository : & Path ) -> bool {
131
+ let command_string = "git status -s" ;
132
+ let ( command, args) = build_command ( command_string) ;
133
+ let ( _, output) = run_command ( command, args, repository) ;
134
+
135
+ !output. is_empty ( )
111
136
}
0 commit comments