Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b9f698

Browse files
wip
1 parent d846174 commit 3b9f698

File tree

12 files changed

+196
-249
lines changed

12 files changed

+196
-249
lines changed

‎asyncgit/src/remote_tags.rs‎

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ use crate::{
44
asyncjob::{AsyncJob, RunParams},
55
error::Result,
66
sync::cred::BasicAuthCredential,
7-
sync::{
8-
remotes::{get_default_remote, tags_missing_remote},
9-
RepoPath,
10-
},
7+
sync::{remotes::tags_missing_remote, RepoPath},
118
AsyncGitNotification,
129
};
1310

1411
use std::sync::{Arc, Mutex};
1512

1613
enum JobState {
17-
Request(Option<BasicAuthCredential>),
14+
Request(Option<BasicAuthCredential>,String),
1815
Response(Result<Vec<String>>),
1916
}
2017

@@ -30,12 +27,14 @@ impl AsyncRemoteTagsJob {
3027
///
3128
pub fn new(
3229
repo: RepoPath,
30+
remote: String,
3331
basic_credential: Option<BasicAuthCredential>,
3432
) -> Self {
3533
Self {
3634
repo,
3735
state: Arc::new(Mutex::new(Some(JobState::Request(
3836
basic_credential,
37+
remote,
3938
)))),
4039
}
4140
}
@@ -45,7 +44,7 @@ impl AsyncRemoteTagsJob {
4544
if let Ok(mut state) = self.state.lock() {
4645
if let Some(state) = state.take() {
4746
return match state {
48-
JobState::Request(_) => None,
47+
JobState::Request(_, _) => None,
4948
JobState::Response(result) => Some(result),
5049
};
5150
}
@@ -65,15 +64,12 @@ impl AsyncJob for AsyncRemoteTagsJob {
6564
) -> Result<Self::Notification> {
6665
if let Ok(mut state) = self.state.lock() {
6766
*state = state.take().map(|state| match state {
68-
JobState::Request(basic_credential) => {
69-
let result = get_default_remote(&self.repo)
70-
.and_then(|remote| {
71-
tags_missing_remote(
72-
&self.repo,
73-
&remote,
74-
basic_credential,
75-
)
76-
});
67+
JobState::Request(basic_credential, remote) => {
68+
let result = tags_missing_remote(
69+
&self.repo,
70+
&remote,
71+
basic_credential,
72+
);
7773

7874
JobState::Response(result)
7975
}

‎asyncgit/src/sync/branch/mod.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ pub mod merge_ff;
55
pub mod merge_rebase;
66
pub mod rename;
77

8-
use super::{
9-
remotes::get_default_remote_in_repo, utils::bytes2string,
10-
RepoPath,
11-
};
8+
use super::{utils::bytes2string, RepoPath};
129
use crate::{
1310
error::{Error, Result},
1411
sync::{repository::repo, utils::get_head_repo, CommitId},
@@ -198,14 +195,14 @@ pub struct BranchCompare {
198195
pub(crate) fn branch_set_upstream(
199196
repo: &Repository,
200197
branch_name: &str,
198+
remote: &str,
201199
) -> Result<()> {
202200
scope_time!("branch_set_upstream");
203201

204202
let mut branch =
205203
repo.find_branch(branch_name, BranchType::Local)?;
206204

207205
if branch.upstream().is_err() {
208-
let remote = get_default_remote_in_repo(repo)?;
209206
let upstream_name = format!("{remote}/{branch_name}");
210207
branch.set_upstream(Some(upstream_name.as_str()))?;
211208
}

‎asyncgit/src/sync/cred.rs‎

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! credentials git helper
22
3-
use super::{
4-
remotes::get_default_remote_in_repo, repository::repo, RepoPath,
5-
};
3+
use super::{repository::repo, RepoPath};
64
use crate::error::{Error, Result};
75
use git2::CredentialHelper;
86

@@ -30,10 +28,12 @@ impl BasicAuthCredential {
3028
}
3129

3230
/// know if username and password are needed for this url
33-
pub fn need_username_password(repo_path: &RepoPath) -> Result<bool> {
31+
pub fn need_username_password(
32+
repo_path: &RepoPath,
33+
remote: &str,
34+
) -> Result<bool> {
3435
let repo = repo(repo_path)?;
35-
let remote =
36-
repo.find_remote(&get_default_remote_in_repo(&repo)?)?;
36+
let remote = repo.find_remote(remote)?;
3737
let url = remote
3838
.pushurl()
3939
.or_else(|| remote.url())
@@ -46,10 +46,11 @@ pub fn need_username_password(repo_path: &RepoPath) -> Result<bool> {
4646
/// extract username and password
4747
pub fn extract_username_password(
4848
repo_path: &RepoPath,
49+
remote: &str,
4950
) -> Result<BasicAuthCredential> {
5051
let repo = repo(repo_path)?;
5152
let url = repo
52-
.find_remote(&get_default_remote_in_repo(&repo)?)?
53+
.find_remote(remote)?
5354
.url()
5455
.ok_or(Error::UnknownRemote)?
5556
.to_owned();
@@ -175,7 +176,10 @@ mod tests {
175176
repo.remote(DEFAULT_REMOTE_NAME, "http://user@github.com")
176177
.unwrap();
177178

178-
assert_eq!(need_username_password(repo_path).unwrap(), true);
179+
assert_eq!(
180+
need_username_password(repo_path, "origin").unwrap(),
181+
true
182+
);
179183
}
180184

181185
#[test]
@@ -189,7 +193,10 @@ mod tests {
189193
repo.remote(DEFAULT_REMOTE_NAME, "git@github.com:user/repo")
190194
.unwrap();
191195

192-
assert_eq!(need_username_password(repo_path).unwrap(), false);
196+
assert_eq!(
197+
need_username_password(repo_path, "origin").unwrap(),
198+
false
199+
);
193200
}
194201

195202
#[test]
@@ -208,7 +215,10 @@ mod tests {
208215
)
209216
.unwrap();
210217

211-
assert_eq!(need_username_password(repo_path).unwrap(), false);
218+
assert_eq!(
219+
need_username_password(repo_path, "origin").unwrap(),
220+
false
221+
);
212222
}
213223

214224
#[test]
@@ -221,7 +231,7 @@ mod tests {
221231
let repo_path: &RepoPath =
222232
&root.as_os_str().to_str().unwrap().into();
223233

224-
need_username_password(repo_path).unwrap();
234+
need_username_password(repo_path,"origin").unwrap();
225235
}
226236

227237
#[test]
@@ -239,7 +249,7 @@ mod tests {
239249
.unwrap();
240250

241251
assert_eq!(
242-
extract_username_password(repo_path).unwrap(),
252+
extract_username_password(repo_path,"origin").unwrap(),
243253
BasicAuthCredential::new(
244254
Some("user".to_owned()),
245255
Some("pass".to_owned())
@@ -259,7 +269,7 @@ mod tests {
259269
.unwrap();
260270

261271
assert_eq!(
262-
extract_username_password(repo_path).unwrap(),
272+
extract_username_password(repo_path,"origin").unwrap(),
263273
BasicAuthCredential::new(Some("user".to_owned()), None)
264274
);
265275
}
@@ -274,6 +284,6 @@ mod tests {
274284
let repo_path: &RepoPath =
275285
&root.as_os_str().to_str().unwrap().into();
276286

277-
extract_username_password(repo_path).unwrap();
287+
extract_username_password(repo_path,"origin").unwrap();
278288
}
279289
}

‎asyncgit/src/sync/mod.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub use merge::{
7171
};
7272
pub use rebase::rebase_branch;
7373
pub use remotes::{
74-
get_default_remote, get_remotes, has_default_remote,
74+
get_remotes, get_single_remote, has_single_remote,
7575
push::AsyncProgress, tags::PushTagsProgress,
7676
};
7777
pub(crate) use repository::repo;

‎asyncgit/src/sync/remotes/mod.rs‎

Lines changed: 8 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
ProgressPercent,
1414
};
1515
use crossbeam_channel::Sender;
16-
use git2::{BranchType, FetchOptions, ProxyOptions,Repository};
16+
use git2::{BranchType, FetchOptions, ProxyOptions};
1717
use scopetime::scope_time;
1818
use utils::bytes2string;
1919

@@ -44,29 +44,11 @@ pub fn get_remotes(repo_path: &RepoPath) -> Result<Vec<String>> {
4444
Ok(remotes)
4545
}
4646

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> {
5049
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-
6050
let remotes = repo.remotes()?;
6151

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-
7052
//if only one remote exists pick that
7153
if remotes.len() == 1 {
7254
let first_remote = remotes
@@ -85,12 +67,10 @@ pub(crate) fn get_default_remote_in_repo(
8567
Err(Error::NoDefaultRemoteFound)
8668
}
8769

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)
9474
}
9575

9676
///
@@ -187,9 +167,7 @@ pub(crate) fn fetch(
187167
#[cfg(test)]
188168
mod tests {
189169
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};
193171

194172
#[test]
195173
fn test_smoke() {
@@ -209,107 +187,4 @@ mod tests {
209187

210188
fetch(repo_path, "master", None, None).unwrap();
211189
}
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-
}
315190
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /