@@ -13,7 +13,7 @@ use crate::{
13
13
ProgressPercent ,
14
14
} ;
15
15
use crossbeam_channel:: Sender ;
16
- use git2:: { BranchType , FetchOptions , ProxyOptions , Repository } ;
16
+ use git2:: { BranchType , FetchOptions , ProxyOptions } ;
17
17
use scopetime:: scope_time;
18
18
use utils:: bytes2string;
19
19
@@ -44,29 +44,11 @@ pub fn get_remotes(repo_path: &RepoPath) -> Result<Vec<String>> {
44
44
Ok ( remotes)
45
45
}
46
46
47
- /// tries to find origin or the only remote that is defined if any
48
- /// in case of multiple remotes and none named *origin* we fail
49
- pub fn get_default_remote ( repo_path : & RepoPath ) -> Result < String > {
47
+ /// returns remote name if there is only one single remote
48
+ pub fn get_single_remote ( repo_path : & RepoPath ) -> Result < String > {
50
49
let repo = repo ( repo_path) ?;
51
- get_default_remote_in_repo ( & repo)
52
- }
53
-
54
- /// see `get_default_remote`
55
- pub ( crate ) fn get_default_remote_in_repo (
56
- repo : & Repository ,
57
- ) -> Result < String > {
58
- scope_time ! ( "get_default_remote_in_repo" ) ;
59
-
60
50
let remotes = repo. remotes ( ) ?;
61
51
62
- // if `origin` exists return that
63
- let found_origin = remotes. iter ( ) . any ( |r| {
64
- r. map ( |r| r == DEFAULT_REMOTE_NAME ) . unwrap_or_default ( )
65
- } ) ;
66
- if found_origin {
67
- return Ok ( DEFAULT_REMOTE_NAME . into ( ) ) ;
68
- }
69
-
70
52
//if only one remote exists pick that
71
53
if remotes. len ( ) == 1 {
72
54
let first_remote = remotes
@@ -85,12 +67,10 @@ pub(crate) fn get_default_remote_in_repo(
85
67
Err ( Error :: NoDefaultRemoteFound )
86
68
}
87
69
88
- /// returns true based on result of `get_default_remote_in_repo` being anything but `NoDefaultRemoteFound`
89
- pub fn has_default_remote ( repo : & Repository ) -> bool {
90
- !matches ! (
91
- get_default_remote_in_repo( repo) ,
92
- Err ( Error :: NoDefaultRemoteFound )
93
- )
70
+ /// returns true if there is only a single remote
71
+ pub fn has_single_remote ( repo_path : & RepoPath ) -> Result < bool > {
72
+ let repo = repo ( repo_path) ?;
73
+ Ok ( repo. remotes ( ) ?. len ( ) == 1 )
94
74
}
95
75
96
76
///
@@ -187,9 +167,7 @@ pub(crate) fn fetch(
187
167
#[ cfg( test) ]
188
168
mod tests {
189
169
use super :: * ;
190
- use crate :: sync:: tests:: {
191
- debug_cmd_print, repo_clone, repo_init,
192
- } ;
170
+ use crate :: sync:: tests:: { repo_clone, repo_init} ;
193
171
194
172
#[ test]
195
173
fn test_smoke ( ) {
@@ -209,107 +187,4 @@ mod tests {
209
187
210
188
fetch ( repo_path, "master" , None , None ) . unwrap ( ) ;
211
189
}
212
-
213
- #[ test]
214
- fn test_default_remote ( ) {
215
- let ( remote_dir, _remote) = repo_init ( ) . unwrap ( ) ;
216
- let remote_path = remote_dir. path ( ) . to_str ( ) . unwrap ( ) ;
217
- let ( repo_dir, _repo) = repo_clone ( remote_path) . unwrap ( ) ;
218
- let repo_path: & RepoPath = & repo_dir
219
- . into_path ( )
220
- . as_os_str ( )
221
- . to_str ( )
222
- . unwrap ( )
223
- . into ( ) ;
224
-
225
- debug_cmd_print (
226
- repo_path,
227
- & format ! ( "git remote add second {remote_path}" ) [ ..] ,
228
- ) ;
229
-
230
- let remotes = get_remotes ( repo_path) . unwrap ( ) ;
231
-
232
- assert_eq ! (
233
- remotes,
234
- vec![ String :: from( "origin" ) , String :: from( "second" ) ]
235
- ) ;
236
-
237
- let first =
238
- get_default_remote_in_repo ( & repo ( repo_path) . unwrap ( ) )
239
- . unwrap ( ) ;
240
- assert_eq ! ( first, String :: from( "origin" ) ) ;
241
- }
242
-
243
- #[ test]
244
- fn test_default_remote_out_of_order ( ) {
245
- let ( remote_dir, _remote) = repo_init ( ) . unwrap ( ) ;
246
- let remote_path = remote_dir. path ( ) . to_str ( ) . unwrap ( ) ;
247
- let ( repo_dir, _repo) = repo_clone ( remote_path) . unwrap ( ) ;
248
- let repo_path: & RepoPath = & repo_dir
249
- . into_path ( )
250
- . as_os_str ( )
251
- . to_str ( )
252
- . unwrap ( )
253
- . into ( ) ;
254
-
255
- debug_cmd_print (
256
- repo_path,
257
- "git remote rename origin alternate" ,
258
- ) ;
259
-
260
- debug_cmd_print (
261
- repo_path,
262
- & format ! ( "git remote add origin {remote_path}" ) [ ..] ,
263
- ) ;
264
-
265
- //NOTE: aparently remotes are not chronolically sorted but alphabetically
266
- let remotes = get_remotes ( repo_path) . unwrap ( ) ;
267
-
268
- assert_eq ! (
269
- remotes,
270
- vec![ String :: from( "alternate" ) , String :: from( "origin" ) ]
271
- ) ;
272
-
273
- let first =
274
- get_default_remote_in_repo ( & repo ( repo_path) . unwrap ( ) )
275
- . unwrap ( ) ;
276
- assert_eq ! ( first, String :: from( "origin" ) ) ;
277
- }
278
-
279
- #[ test]
280
- fn test_default_remote_inconclusive ( ) {
281
- let ( remote_dir, _remote) = repo_init ( ) . unwrap ( ) ;
282
- let remote_path = remote_dir. path ( ) . to_str ( ) . unwrap ( ) ;
283
- let ( repo_dir, _repo) = repo_clone ( remote_path) . unwrap ( ) ;
284
- let repo_path: & RepoPath = & repo_dir
285
- . into_path ( )
286
- . as_os_str ( )
287
- . to_str ( )
288
- . unwrap ( )
289
- . into ( ) ;
290
-
291
- debug_cmd_print (
292
- repo_path,
293
- "git remote rename origin alternate" ,
294
- ) ;
295
-
296
- debug_cmd_print (
297
- repo_path,
298
- & format ! ( "git remote add someremote {remote_path}" ) [ ..] ,
299
- ) ;
300
-
301
- let remotes = get_remotes ( repo_path) . unwrap ( ) ;
302
- assert_eq ! (
303
- remotes,
304
- vec![
305
- String :: from( "alternate" ) ,
306
- String :: from( "someremote" )
307
- ]
308
- ) ;
309
-
310
- let res =
311
- get_default_remote_in_repo ( & repo ( repo_path) . unwrap ( ) ) ;
312
- assert_eq ! ( res. is_err( ) , true ) ;
313
- assert ! ( matches!( res, Err ( Error :: NoDefaultRemoteFound ) ) ) ;
314
- }
315
190
}
0 commit comments