Skip to content

Commit f7eedcc

Browse files
committed
fix clones when using username/password urls, take2
1 parent 3638019 commit f7eedcc

File tree

5 files changed

+59
-53
lines changed

5 files changed

+59
-53
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "anysnake2"
3-
version = "2.1.1"
3+
version = "2.1.2"
44
authors = ["Florian Finkernagel <finkernagel@imt.uni-marburg.de>"]
55
edition = "2021"
66

src/config.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -428,45 +428,9 @@ impl TofuPythonPackageSource {
428428
mod test {
429429
use serde_json::json;
430430

431-
use crate::{config::remove_username_from_url, vcs::{ParsedVCS, TofuVCS}};
432-
433-
#[test]
434-
fn test_remove_username_from_url() {
435-
assert_eq!(
436-
remove_username_from_url("https://user@example.com"),
437-
"https://example.com/"
438-
);
439-
assert_eq!(
440-
remove_username_from_url("https://example.com"),
441-
"https://example.com/"
442-
);
443-
assert_eq!(
444-
remove_username_from_url("hg+https://something:passsword@example.com"),
445-
"hg+https://example.com"
446-
);
447-
}
448-
449-
#[test]
450-
fn test_no_usernames_in_vcs_url_to_string_git() {
451-
let git_vcs = TofuVCS::Git {
452-
url: "https://user:password@example.com".to_string(),
453-
branch: "main".to_string(),
454-
rev: "123".to_string(),
455-
};
456-
let str_vcs = serde_json::to_string_pretty(&json!(git_vcs)).unwrap();
457-
assert!(str_vcs.contains("https://example.com"));
458-
}
431+
use crate::{config::remove_username_from_url, vcs::{TofuVCS}};
459432

460-
#[test]
461-
fn test_no_usernames_in_vcs_url_to_string_hg() {
462-
let git_vcs = TofuVCS::Mercurial {
463-
url: "https://user:password@example.com".to_string(),
464-
rev: "123".to_string(),
465-
};
466-
let str_vcs = serde_json::to_string_pretty(&json!(git_vcs)).unwrap();
467-
assert!(str_vcs.contains("https://example.com"));
468-
469-
}
433+
470434

471435
}
472436

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ fn clone(
842842
tofu_vcs.clone_repo(&final_dir.to_string_lossy())?;
843843
}
844844
}
845-
known_clones.insert(name.to_string(), format!("{source:?}"));
845+
known_clones.insert(name.to_string(), source.to_string());
846846
}
847847
Ok(())
848848
}
@@ -889,7 +889,7 @@ fn perform_clones(flake_dir: &Path, parsed_config: &config::TofuConfigToml) -> R
889889
for (name, source) in name_urls {
890890
let known_source = known_clones.get(name).map_or("", String::as_str);
891891
let final_dir: PathBuf = [target_dir, name].iter().collect();
892-
let new_source_str = format!("{:?}", source.without_username_in_url());
892+
let new_source_str = format!("{}", source.without_username_in_url());
893893
if final_dir.exists() && !dir_empty(&final_dir)? && known_source != new_source_str
894894
//empty dir is ok.
895895
{

src/vcs.rs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde::{Serialize, Serializer};
66
use version_compare::Version;
77

88
use crate::{
9-
config::{self, remove_username_from_url},
9+
config::{self, remove_username_from_url, TofuPythonPackageSource},
1010
flake_writer::add_auth,
1111
};
1212
use anysnake2::{run_without_ctrl_c, util::get_proxy_req};
@@ -33,7 +33,6 @@ pub enum ParsedVCS {
3333
#[derive(Serialize, Debug, PartialEq, Eq, Clone)]
3434
pub enum TofuVCS {
3535
Git {
36-
#[serde(serialize_with = "serializer_remove_username_from_url")]
3736
url: String,
3837
branch: String,
3938
rev: String,
@@ -46,22 +45,14 @@ pub enum TofuVCS {
4645
},
4746

4847
Mercurial {
49-
#[serde(serialize_with = "serializer_remove_username_from_url")]
5048
url: String,
5149
rev: String,
5250
},
5351
}
5452

55-
fn serializer_remove_username_from_url<S>(url: &str, serializer: S) -> Result<S::Ok, S::Error>
56-
where
57-
S: Serializer,
58-
{
59-
let url_without_username = remove_username_from_url(url);
60-
serializer.serialize_str(&url_without_username)
61-
}
62-
6353
impl TofuVCS {
6454
pub fn to_nix_string(&self) -> String {
55+
//this must include username:password
6556
match self {
6657
TofuVCS::Git { url, branch, rev } => {
6758
format!("git+{url}?ref={branch}&rev={rev}")
@@ -212,6 +203,17 @@ impl std::fmt::Display for TofuVCS {
212203
})
213204
}
214205
}
206+
impl std::fmt::Display for TofuPythonPackageSource {
207+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
208+
let out = match self {
209+
TofuPythonPackageSource::VersionConstraint(x) => x.to_string(),
210+
TofuPythonPackageSource::Url(url) => remove_username_from_url(url),
211+
TofuPythonPackageSource::Vcs(vcs) => vcs.to_string(),
212+
TofuPythonPackageSource::PyPi { version } => format!("pypi:{version}"),
213+
};
214+
f.write_str(&out)
215+
}
216+
}
215217

216218
#[derive(Debug)]
217219
pub enum BranchOrTag {
@@ -874,4 +876,44 @@ mod test {
874876
);
875877
assert!(vcs.is_err());
876878
}
879+
880+
#[test]
881+
fn test_remove_username_from_url() {
882+
assert_eq!(
883+
remove_username_from_url("https://user@example.com"),
884+
"https://example.com/"
885+
);
886+
assert_eq!(
887+
remove_username_from_url("https://example.com"),
888+
"https://example.com/"
889+
);
890+
assert_eq!(
891+
remove_username_from_url("hg+https://something:passsword@example.com"),
892+
"hg+https://example.com"
893+
);
894+
}
895+
896+
#[test]
897+
fn test_no_usernames_in_vcs_url_to_string_git() {
898+
let git_vcs = TofuVCS::Git {
899+
url: "https://user:password@example.com".to_string(),
900+
branch: "main".to_string(),
901+
rev: "123".to_string(),
902+
};
903+
let str_vcs = format!("{}", git_vcs);
904+
assert!(str_vcs.contains("https://example.com"));
905+
assert!(!str_vcs.contains("user:password"));
906+
}
907+
908+
#[test]
909+
fn test_no_usernames_in_vcs_url_to_string_hg() {
910+
let git_vcs = TofuVCS::Mercurial {
911+
url: "https://user:password@example.com".to_string(),
912+
rev: "123".to_string(),
913+
};
914+
let str_vcs = format!("{}", git_vcs);
915+
dbg!(&str_vcs);
916+
assert!(str_vcs.contains("https://example.com"));
917+
assert!(!str_vcs.contains("user:password"));
918+
}
877919
}

0 commit comments

Comments
 (0)