Skip to content

Commit ca8d940

Browse files
committed
Allow specifying custom Josh filter
1 parent 64e4604 commit ca8d940

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ $ cargo install --locked --git https://github.com/rust-lang/josh-sync
1313

1414
First, create a configuration file for a given subtree repo using `josh-sync init`. The config will be created under the path `josh-sync.toml`. Modify the file to fill in the name of the subtree repository (e.g. `stdarch`) and its relative path in the main `rust-lang/rust` repository (e.g. `library/stdarch`).
1515

16+
If you need to specify a more complex Josh `filter`, use `filter` field in the configuration file instead of the `path` field.
17+
1618
The `init` command will also create an empty `rust-version` file that stores the last upstream `rustc` SHA that was synced in the subtree.
1719

1820
## Performing pull

src/bin/josh_sync.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ fn main() -> anyhow::Result<()> {
5050
let config = JoshConfig {
5151
org: "rust-lang".to_string(),
5252
repo: "<repository-name>".to_string(),
53-
path: "<relative-subtree-path>".to_string(),
53+
path: Some("<relative-subtree-path>".to_string()),
54+
filter: None,
5455
};
5556
config
5657
.write(Path::new(DEFAULT_CONFIG_PATH))

src/config.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ pub struct JoshConfig {
88
pub repo: String,
99
/// Relative path where the subtree is located in rust-lang/rust.
1010
/// For example `src/doc/rustc-dev-guide`.
11-
pub path: String,
11+
pub path: Option<String>,
12+
/// Optional filter specification for Josh.
13+
/// It cannot be used together with `path`.
14+
pub filter: Option<String>,
1215
}
1316

1417
impl JoshConfig {
1518
pub fn full_repo_name(&self) -> String {
1619
format!("{}/{}", self.org, self.repo)
1720
}
1821

22+
pub fn construct_josh_filter(&self) -> String {
23+
match (&self.path, &self.filter) {
24+
(Some(path), None) => format!(":/{path}"),
25+
(None, Some(filter)) => filter.clone(),
26+
_ => unreachable!("Config contains both path and a filter"),
27+
}
28+
}
29+
1930
pub fn write(&self, path: &Path) -> anyhow::Result<()> {
2031
let config = toml::to_string_pretty(self).context("cannot serialize config")?;
2132
std::fs::write(path, config).context("cannot write config")?;
@@ -31,5 +42,13 @@ pub fn load_config(path: &Path) -> anyhow::Result<JoshConfig> {
3142
let data = std::fs::read_to_string(path)
3243
.with_context(|| format!("cannot load config file from {}", path.display()))?;
3344
let config: JoshConfig = toml::from_str(&data).context("cannot load config as TOML")?;
45+
if config.path.is_some() == config.filter.is_some() {
46+
return if config.path.is_some() {
47+
Err(anyhow::anyhow!("Cannot specify both `path` and `filter`"))
48+
} else {
49+
Err(anyhow::anyhow!("Must specify one of `path` and `filter`"))
50+
};
51+
}
52+
3453
Ok(config)
3554
}

src/sync.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ impl From<anyhow::Error> for RustcPullError {
1919
}
2020
}
2121

22-
/// Constructs a Josh filter for synchronization, like `:/src/foo`.
23-
fn construct_filter(path: &str) -> String {
24-
format!(":/{path}")
25-
}
26-
2722
pub struct PullResult {
2823
pub merge_commit_message: String,
2924
}
@@ -64,7 +59,7 @@ impl GitSync {
6459
let josh_url = josh.git_url(
6560
UPSTREAM_REPO,
6661
Some(&upstream_sha),
67-
&construct_filter(&self.context.config.path),
62+
&self.context.config.construct_josh_filter(),
6863
);
6964

7065
let orig_head = check_output(["git", "rev-parse", "HEAD"])?;
@@ -150,7 +145,7 @@ Upstream ref: {upstream_sha}
150145
Filtered ref: {incoming_ref}
151146
"#,
152147
upstream_head_short = &upstream_sha[..12],
153-
filter = construct_filter(&self.context.config.path)
148+
filter = self.context.config.construct_josh_filter()
154149
);
155150

156151
// Merge the fetched commit.
@@ -203,7 +198,7 @@ Filtered ref: {incoming_ref}
203198
let josh_url = josh.git_url(
204199
&format!("{username}/rust"),
205200
None,
206-
&construct_filter(&self.context.config.path),
201+
&self.context.config.construct_josh_filter(),
207202
);
208203
let user_upstream_url = format!("https://github.com/{username}/rust");
209204

0 commit comments

Comments
 (0)