@@ -28,7 +28,8 @@ impl crate::Repository {
28
28
Remote :: from_fetch_url ( url, false , self )
29
29
}
30
30
31
- /// Find the remote with the given `name_or_url` or report an error, similar to [`try_find_remote(…)`][Self::try_find_remote()].
31
+ /// Find the configured remote with the given `name_or_url` or report an error,
32
+ /// similar to [`try_find_remote(…)`][Self::try_find_remote()].
32
33
///
33
34
/// Note that we will obtain remotes only if we deem them [trustworthy][crate::open::Options::filter_config_section()].
34
35
pub fn find_remote < ' a > ( & self , name_or_url : impl Into < & ' a BStr > ) -> Result < Remote < ' _ > , find:: existing:: Error > {
@@ -42,7 +43,7 @@ impl crate::Repository {
42
43
43
44
/// Find the default remote as configured, or `None` if no such configuration could be found.
44
45
///
45
- /// See [`remote_default_name()`][ Self::remote_default_name()] for more information on the `direction` parameter.
46
+ /// See [`remote_default_name()`]( Self::remote_default_name()) for more information on the `direction` parameter.
46
47
pub fn find_default_remote (
47
48
& self ,
48
49
direction : remote:: Direction ,
@@ -51,8 +52,8 @@ impl crate::Repository {
51
52
. map ( |name| self . find_remote ( name. as_ref ( ) ) )
52
53
}
53
54
54
- /// Find the remote with the given `name_or_url` or return `None` if it doesn't exist, for the purpose of fetching or pushing
55
- /// data to a remote .
55
+ /// Find the configured remote with the given `name_or_url` or return `None` if it doesn't exist,
56
+ /// for the purpose of fetching or pushing data .
56
57
///
57
58
/// There are various error kinds related to partial information or incorrectly formatted URLs or ref-specs.
58
59
/// Also note that the created `Remote` may have neither fetch nor push ref-specs set at all.
@@ -65,6 +66,35 @@ impl crate::Repository {
65
66
self . try_find_remote_inner ( name_or_url, true )
66
67
}
67
68
69
+ /// This method emulate what `git fetch <remote>` does in order to obtain a remote to fetch from.
70
+ ///
71
+ /// As such, with `name_or_url` being `Some`, it will:
72
+ ///
73
+ /// * use `name_or_url` verbatim if it is a URL, creating a new remote in memory as needed.
74
+ /// * find the named remote if `name_or_url` is a remote name
75
+ ///
76
+ /// If `name_or_url` is `None`:
77
+ ///
78
+ /// * use the current `HEAD` branch to find a configured remote
79
+ /// * fall back to either a generally configured remote or the only configured remote.
80
+ ///
81
+ /// Fail if no remote could be found despite all of the above.
82
+ pub fn find_fetch_remote ( & self , name_or_url : Option < & BStr > ) -> Result < Remote < ' _ > , find:: for_fetch:: Error > {
83
+ Ok ( match name_or_url {
84
+ Some ( name) => match self . try_find_remote ( name) . and_then ( Result :: ok) {
85
+ Some ( remote) => remote,
86
+ None => self . remote_at ( gix_url:: parse ( name) ?) ?,
87
+ } ,
88
+ None => self
89
+ . head ( ) ?
90
+ . into_remote ( remote:: Direction :: Fetch )
91
+ . transpose ( ) ?
92
+ . map ( Ok )
93
+ . or_else ( || self . find_default_remote ( remote:: Direction :: Fetch ) )
94
+ . ok_or_else ( || find:: for_fetch:: Error :: ExactlyOneRemoteNotAvailable ) ??,
95
+ } )
96
+ }
97
+
68
98
/// Similar to [`try_find_remote()`][Self::try_find_remote()], but removes a failure mode if rewritten URLs turn out to be invalid
69
99
/// as it skips rewriting them.
70
100
/// Use this in conjunction with [`Remote::rewrite_urls()`] to non-destructively apply the rules and keep the failed urls unchanged.
0 commit comments