@@ -60,38 +60,11 @@ pub fn git_clone(repo_url: &str, target_dir: &Path, revision: Option<&str>) -> R
60
60
. status ( )
61
61
. with_context ( || format ! ( "Failed to clone repository from {}" , repo_url) ) ?;
62
62
63
-
64
63
let revision_str = revision. unwrap_or ( "main" ) ;
65
- return git_to_revision ( target_dir, "origin" , revision_str)
66
- }
67
-
68
- pub fn git_pull ( repo_path : & Path ) -> Result < ( ) > {
69
- if !repo_path. is_dir ( ) {
70
- return Err ( anyhow ! (
71
- "Error with git pull. {} is not a directory" ,
72
- repo_path. display( )
73
- ) ) ;
74
- }
75
-
76
- let command = "git pull" ;
77
- let command_vec = shell_words:: split ( command) . map_err ( anyhow:: Error :: new) ?;
78
-
79
- let status = Command :: new ( & command_vec[ 0 ] )
80
- . args ( & command_vec[ 1 ..] )
81
- . current_dir ( repo_path)
82
- . stdout ( Stdio :: null ( ) )
83
- . status ( )
84
- . with_context ( || format ! ( "Failed to execute process in {}" , repo_path. display( ) ) ) ?;
85
-
86
- if status. success ( ) {
87
- Ok ( ( ) )
88
- } else {
89
- Err ( anyhow ! ( "Error wth git pull in {}" , repo_path. display( ) ) )
90
- }
64
+ return git_to_revision ( target_dir, "origin" , revision_str) ;
91
65
}
92
66
93
67
pub fn git_update ( repo_path : & Path , repo_url : & str , revision : Option < & str > ) -> Result < ( ) > {
94
-
95
68
if !repo_path. is_dir ( ) {
96
69
return Err ( anyhow ! (
97
70
"Error with updating. {} is not a directory" ,
@@ -121,7 +94,14 @@ pub fn git_update(repo_path: &Path, repo_url: &str, revision: Option<&str>) -> R
121
94
. current_dir ( repo_path)
122
95
. stdout ( Stdio :: null ( ) )
123
96
. status ( )
124
- . with_context ( || format ! ( "Error with adding {} as a remote named {} in {}" , repo_url, tmp_remote_name, repo_path. display( ) ) ) ?;
97
+ . with_context ( || {
98
+ format ! (
99
+ "Error with adding {} as a remote named {} in {}" ,
100
+ repo_url,
101
+ tmp_remote_name,
102
+ repo_path. display( )
103
+ )
104
+ } ) ?;
125
105
126
106
// Attempt to switch to the revision on temporary remote
127
107
let revision_str = revision. unwrap_or ( "main" ) ;
@@ -147,25 +127,35 @@ pub fn git_update(repo_path: &Path, repo_url: &str, revision: Option<&str>) -> R
147
127
. args ( & command_vec[ 1 ..] )
148
128
. stdout ( Stdio :: null ( ) )
149
129
. status ( )
150
- . with_context ( || format ! ( "Failed to set origin remote to {} in {}" , repo_url, repo_path. display( ) ) ) ?;
130
+ . with_context ( || {
131
+ format ! (
132
+ "Failed to set origin remote to {} in {}" ,
133
+ repo_url,
134
+ repo_path. display( )
135
+ )
136
+ } ) ?;
151
137
Command :: new ( "git" )
152
138
. current_dir ( repo_path)
153
139
. args ( vec ! [ "remote" , "rm" , & tmp_remote_name] )
154
140
. stdout ( Stdio :: null ( ) )
155
141
. status ( )
156
- . with_context ( || format ! ( "Failed to remove temporary remote {} in {}" , tmp_remote_name, repo_path. display( ) ) ) ?;
157
- return Ok ( ( ) )
142
+ . with_context ( || {
143
+ format ! (
144
+ "Failed to remove temporary remote {} in {}" ,
145
+ tmp_remote_name,
146
+ repo_path. display( )
147
+ )
148
+ } ) ?;
149
+ return Ok ( ( ) ) ;
158
150
}
159
151
160
-
161
152
fn random_remote_name ( ) -> String {
162
153
let mut rng = rand:: thread_rng ( ) ;
163
154
let random_number: u32 = rng. gen ( ) ;
164
155
format ! ( "tinty-remote-{}" , random_number)
165
156
}
166
157
167
158
fn git_to_revision ( repo_path : & Path , remote_name : & str , revision : & str ) -> Result < ( ) > {
168
-
169
159
let command = format ! ( "git fetch \" {}\" \" {}\" " , remote_name, revision) ;
170
160
let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
171
161
@@ -174,7 +164,12 @@ fn git_to_revision(repo_path: &Path, remote_name: &str, revision: &str) -> Resul
174
164
. current_dir ( repo_path)
175
165
. stdout ( Stdio :: null ( ) )
176
166
. status ( )
177
- . with_context ( || format ! ( "fetch: Failed to execute process in {}" , repo_path. display( ) ) ) ?;
167
+ . with_context ( || {
168
+ format ! (
169
+ "fetch: Failed to execute process in {}" ,
170
+ repo_path. display( )
171
+ )
172
+ } ) ?;
178
173
179
174
if !fetch. success ( ) {
180
175
return Err ( anyhow ! ( "Error with fetching \" {}\" " , revision) ) ;
@@ -189,23 +184,44 @@ fn git_to_revision(repo_path: &Path, remote_name: &str, revision: &str) -> Resul
189
184
. args ( & command_vec[ 1 ..] )
190
185
. current_dir ( repo_path)
191
186
. output ( )
192
- . with_context ( || format ! ( "Unable to parse revision {} in {}" , revision, repo_path. display( ) ) ) ?;
187
+ . with_context ( || {
188
+ format ! (
189
+ "Unable to parse revision {} in {}" ,
190
+ revision,
191
+ repo_path. display( )
192
+ )
193
+ } ) ?;
193
194
194
195
let stdout = String :: from_utf8_lossy ( & parse_out. stdout ) ;
195
196
196
197
let commit_sha = match stdout. lines ( ) . next ( ) {
197
198
Some ( sha) => sha,
198
- None => return Err ( anyhow ! ( "Unable to parse revision {} in {}" , revision, repo_path. display( ) ) )
199
+ None => {
200
+ return Err ( anyhow ! (
201
+ "Unable to parse revision {} in {}" ,
202
+ revision,
203
+ repo_path. display( )
204
+ ) )
205
+ }
199
206
} ;
200
207
201
- let command = format ! ( "git -c advice.detachedHead=false checkout \" {}\" " , commit_sha) ;
208
+ let command = format ! (
209
+ "git -c advice.detachedHead=false checkout \" {}\" " ,
210
+ commit_sha
211
+ ) ;
202
212
let command_vec = shell_words:: split ( & command) . map_err ( anyhow:: Error :: new) ?;
203
213
204
214
Command :: new ( & command_vec[ 0 ] )
205
215
. args ( & command_vec[ 1 ..] )
206
216
. current_dir ( repo_path)
207
217
. status ( )
208
- . with_context ( || format ! ( "Failed to checkout SHA {} in {}" , commit_sha, repo_path. display( ) ) ) ?;
218
+ . with_context ( || {
219
+ format ! (
220
+ "Failed to checkout SHA {} in {}" ,
221
+ commit_sha,
222
+ repo_path. display( )
223
+ )
224
+ } ) ?;
209
225
210
226
Ok ( ( ) )
211
227
}
0 commit comments