Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ toml = "0.8"
serde = { version = "1", features = ["derive"] }
urlencoding = "2"
which = "8"
regex = "1.12.3"

[profile.release]
debug = "line-tables-only"
124 changes: 122 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ impl JoshConfig {
}

pub fn construct_josh_filter(&self) -> String {
match (&self.path, &self.filter) {
let filter = match (&self.path, &self.filter) {
(Some(path), None) => format!(":/{path}"),
(None, Some(filter)) => filter.clone(),
_ => unreachable!("Config contains both path and a filter"),
}
};

let filter = convert_rev_syntax(&filter);
let filter = wrap_compat(&filter);
filter
}

pub fn write(&self, path: &Path) -> anyhow::Result<()> {
Expand Down Expand Up @@ -74,3 +78,119 @@ pub fn load_config(path: &Path) -> anyhow::Result<JoshConfig> {

Ok(config)
}

/// Converts filters from old `:rev(sha:filter)` syntax to new
/// `:rev(<=sha:filter)` syntax. Null SHAs (40 zeros) become `_`.
/// Only touches SHAs inside `:rev(...)` blocks.
fn convert_rev_syntax(input: &str) -> String {
let rev_block = regex::Regex::new(r":rev\([^)]*\)").unwrap();
let entry = regex::Regex::new(
r"(?x)
([,(]) # delimiter before entry
(0{40}|[0-9a-f]{40}) # full SHA
: # colon separator
",
)
.unwrap();

rev_block
.replace_all(input, |block: &regex::Captures| {
entry
.replace_all(&block[0], |caps: &regex::Captures| {
let delim = &caps[1];
let sha = &caps[2];
if sha.chars().all(|c| c == '0') {
format!("{delim}_:")
} else {
format!("{delim}<={sha}:")
}
})
.into_owned()
})
.into_owned()
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks plausible but having a test that calls this function on a few representative inputs would probably still be a good idea.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


/// Wraps a filter with the backwards compatibility meta options for
/// trivial merge preservation and CRLF normalization in gpgsig headers.
///
/// `:your/filter` becomes
/// `:~(history="keep-trivial-merges",gpgsig="norm-lf")[:your/filter]`
fn wrap_compat(filter: &str) -> String {
format!(":~(history=\"keep-trivial-merges\",gpgsig=\"norm-lf\")[{filter}]")
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn no_rev_block_unchanged() {
assert_eq!(convert_rev_syntax(":/some/path"), ":/some/path");
}

#[test]
fn single_sha_gets_prefix() {
assert_eq!(
convert_rev_syntax(":rev(3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path)"),
":rev(<=3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path)",
);
}

#[test]
fn null_sha_becomes_underscore() {
assert_eq!(
convert_rev_syntax(":rev(0000000000000000000000000000000000000000:/some/path)"),
":rev(_:/some/path)",
);
}

#[test]
fn multiple_entries_in_rev_block() {
assert_eq!(
convert_rev_syntax(
":rev(3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/p1,\
e4c7a2d8f1b3e5a9d6c0f2b4a7e1d3c5f8a0b6e9:/p2,\
0000000000000000000000000000000000000000:/p3)"
),
":rev(<=3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/p1,\
<=e4c7a2d8f1b3e5a9d6c0f2b4a7e1d3c5f8a0b6e9:/p2,\
_:/p3)",
);
}

#[test]
fn already_converted_syntax_unchanged() {
assert_eq!(
convert_rev_syntax(":rev(<=3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path)"),
":rev(<=3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path)",
);
}

#[test]
fn underscore_syntax_unchanged() {
assert_eq!(
convert_rev_syntax(":rev(_:/some/path)"),
":rev(_:/some/path)",
);
}

#[test]
fn sha_outside_rev_block_unchanged() {
assert_eq!(
convert_rev_syntax("3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path"),
"3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/some/path",
);
}

#[test]
fn multiple_rev_blocks() {
assert_eq!(
convert_rev_syntax(
":rev(3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/p1)\
:rev(e4c7a2d8f1b3e5a9d6c0f2b4a7e1d3c5f8a0b6e9:/p2)"
),
":rev(<=3a1f5e2b9c8d4e7f6a0b1c2d3e4f5a6b7c8d9e0f:/p1)\
:rev(<=e4c7a2d8f1b3e5a9d6c0f2b4a7e1d3c5f8a0b6e9:/p2)",
);
}
}
3 changes: 2 additions & 1 deletion src/josh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::time::Duration;

const JOSH_PORT: u16 = 42042;
/// Version of `josh-proxy` that should be downloaded for the user.
const JOSH_VERSION: &str = "r24.10.04";
const JOSH_VERSION: &str = "r26.05.08";

pub struct JoshProxy {
path: PathBuf,
Expand Down Expand Up @@ -137,6 +137,7 @@ pub struct RunningJoshProxy {
impl RunningJoshProxy {
pub fn git_url(&self, repo: &str, commit: Option<&str>, filter: &str) -> String {
let commit = commit.map(|c| format!("@{c}")).unwrap_or_default();
let filter = urlencoding::encode(filter);
format!(
"http://localhost:{}/{repo}.git{commit}{filter}.git",
self.port
Expand Down
Loading