Skip to content

Commit 92e6f88

Browse files
Remove Change: footer on split push
Some projects don't like extra metadata in the commit messages, and after the push it is not needed anyway.
1 parent 50481f5 commit 92e6f88

File tree

3 files changed

+95
-21
lines changed

3 files changed

+95
-21
lines changed

josh-core/src/filter/text.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use crate::JoshResult;
2+
use regex::Regex;
3+
use std::cell::RefCell;
4+
use std::collections::HashMap;
5+
6+
pub fn transform_with_template(
7+
re: &Regex,
8+
template: &str,
9+
input: &str,
10+
globals: &HashMap<String, String>,
11+
) -> JoshResult<String> {
12+
let first_error: RefCell<Option<crate::JoshError>> = RefCell::new(None);
13+
14+
let result = re
15+
.replace_all(input, |caps: &regex::Captures| {
16+
// Build a HashMap with all named captures and globals
17+
// We need to store the string values to keep them alive for the HashMap references
18+
let mut string_storage: HashMap<String, String> = HashMap::new();
19+
20+
// Collect all named capture values
21+
for name in re.capture_names().flatten() {
22+
if let Some(m) = caps.name(name) {
23+
string_storage.insert(name.to_string(), m.as_str().to_string());
24+
}
25+
}
26+
27+
// Build the HashMap for strfmt with references to the stored strings
28+
let mut vars: HashMap<String, &dyn strfmt::DisplayStr> = HashMap::new();
29+
30+
// Add all globals first (lower priority)
31+
for (key, value) in globals {
32+
vars.insert(key.clone(), value as &dyn strfmt::DisplayStr);
33+
}
34+
35+
// Add all named captures (higher priority - will overwrite globals if there's a conflict)
36+
for (key, value) in &string_storage {
37+
vars.insert(key.clone(), value as &dyn strfmt::DisplayStr);
38+
}
39+
40+
// Format the template, propagating errors
41+
match strfmt::strfmt(template, &vars) {
42+
Ok(s) => s,
43+
Err(e) => {
44+
let mut error = first_error.borrow_mut();
45+
if error.is_none() {
46+
*error = Some(e.into());
47+
}
48+
caps[0].to_string()
49+
}
50+
}
51+
})
52+
.into_owned();
53+
54+
match first_error.into_inner() {
55+
Some(e) => Err(e),
56+
None => Ok(result),
57+
}
58+
}

josh-core/src/history.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,23 @@ pub fn unapply_filter(
577577
}
578578
};
579579

580+
let mut apply = filter::Apply::from_tree(new_tree.clone());
581+
582+
if change_ids.is_some() {
583+
let new_message = filter::text::transform_with_template(
584+
&regex::Regex::new(&"(?m)^Change: [^ ]+")?,
585+
&"",
586+
module_commit.message_raw().unwrap(),
587+
&std::collections::HashMap::new(),
588+
)?;
589+
apply = apply.with_message(new_message);
590+
}
591+
580592
ret = rewrite_commit(
581593
transaction.repo(),
582594
&module_commit,
583595
&original_parents,
584-
filter::Apply::from_tree(new_tree.clone()),
596+
apply,
585597
false,
586598
)?;
587599

tests/cli/push_stacked_split.t

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ Make multiple changes with Change-Ids for split testing
4949
$ git commit -q -m "Change-Id: 1234"
5050
$ echo "contents2" > file7
5151
$ git add file7
52-
$ git commit -q -m "Change-Id: foo7"
52+
$ git commit -q -m "Change: foo7"
5353
$ echo "contents3" > file2
5454
$ git add file2
5555
$ git commit -q -m "Change-Id: 1235"
5656
$ git log --decorate --graph --pretty="%s %d"
5757
* Change-Id: 1235 (HEAD -> master)
58-
* Change-Id: foo7
58+
* Change: foo7
5959
* Change-Id: 1234
6060
* add file1 (origin/master, origin/HEAD)
6161

@@ -72,32 +72,32 @@ Push with split mode (should create multiple refs for each change)
7272

7373
Pushed c61c37f4a3d5eb447f41dde15620eee1a181d60b to origin/refs/heads/@changes/master/josh@example.com/1234
7474
To $TESTTMP/remote
75-
* [new branch] c1b55ea7e5f27f82d3565c1f5d64113adf635c2c -> @changes/master/josh@example.com/foo7
75+
* [new branch] 9da166dcfa8650e04c7e39c54a61b7fa0b69ef4f -> @changes/master/josh@example.com/foo7
7676

77-
Pushed c1b55ea7e5f27f82d3565c1f5d64113adf635c2c to origin/refs/heads/@changes/master/josh@example.com/foo7
77+
Pushed 9da166dcfa8650e04c7e39c54a61b7fa0b69ef4f to origin/refs/heads/@changes/master/josh@example.com/foo7
7878
To $TESTTMP/remote
7979
* [new branch] ef7c3c85ad4c5875f308003d42a6e11d9b14aeb9 -> @changes/master/josh@example.com/1235
8080

8181
Pushed ef7c3c85ad4c5875f308003d42a6e11d9b14aeb9 to origin/refs/heads/@changes/master/josh@example.com/1235
8282
To $TESTTMP/remote
83-
* [new branch] 02796cbb12e05f3be9f16c82e4d26542af7e700c -> @heads/master/josh@example.com
83+
* [new branch] 52310103e5d9a8e55df1a7766789a1af04d8601b -> @heads/master/josh@example.com
8484

85-
Pushed 02796cbb12e05f3be9f16c82e4d26542af7e700c to origin/refs/heads/@heads/master/josh@example.com
85+
Pushed 52310103e5d9a8e55df1a7766789a1af04d8601b to origin/refs/heads/@heads/master/josh@example.com
8686

8787
Verify the refs were created in the remote
8888

8989
$ cd ${TESTTMP}/remote
9090
$ git ls-remote . | grep "@" | sort
91-
02796cbb12e05f3be9f16c82e4d26542af7e700c\trefs/heads/@heads/master/josh@example.com (esc)
92-
c1b55ea7e5f27f82d3565c1f5d64113adf635c2c\trefs/heads/@changes/master/josh@example.com/foo7 (esc)
91+
52310103e5d9a8e55df1a7766789a1af04d8601b\trefs/heads/@heads/master/josh@example.com (esc)
92+
9da166dcfa8650e04c7e39c54a61b7fa0b69ef4f\trefs/heads/@changes/master/josh@example.com/foo7 (esc)
9393
c61c37f4a3d5eb447f41dde15620eee1a181d60b\trefs/heads/@changes/master/josh@example.com/1234 (esc)
9494
ef7c3c85ad4c5875f308003d42a6e11d9b14aeb9\trefs/heads/@changes/master/josh@example.com/1235 (esc)
9595

9696
$ git log --all --decorate --graph --pretty="%s %d %H"
9797
* Change-Id: 1235 (@changes/master/josh@example.com/1235) ef7c3c85ad4c5875f308003d42a6e11d9b14aeb9
98-
| * Change-Id: foo7 (@changes/master/josh@example.com/foo7) c1b55ea7e5f27f82d3565c1f5d64113adf635c2c
99-
| | * Change-Id: 1235 (@heads/master/josh@example.com) 02796cbb12e05f3be9f16c82e4d26542af7e700c
100-
| | * Change-Id: foo7 2cbfa8cb8d9a9f1de029fcba547a6e56c742733f
98+
| * (@changes/master/josh@example.com/foo7) 9da166dcfa8650e04c7e39c54a61b7fa0b69ef4f
99+
| | * Change-Id: 1235 (@heads/master/josh@example.com) 52310103e5d9a8e55df1a7766789a1af04d8601b
100+
| | * 48f307ad20210547fdf339d0b0d7ee02bc702c3d
101101
| |/
102102
|/|
103103
* | Change-Id: 1234 (@changes/master/josh@example.com/1234) c61c37f4a3d5eb447f41dde15620eee1a181d60b
@@ -123,21 +123,25 @@ Test that we can fetch the split refs back
123123
Fetched from remote: origin
124124

125125
$ git log --all --decorate --graph --pretty="%s %d %H"
126-
* Change-Id: 1235 (HEAD -> master, origin/@heads/master/josh@example.com) 3faa5b51d4600be54a2b32e84697e7b32a781a03
127-
* Change-Id: foo7 da80e49d24d110866ce2ec7a5c21112696fd165b
126+
* Change-Id: 1235 (HEAD -> master) 8fee494b5170edb463fc623d03d562118cebe88e
127+
* Change: foo7 cadc8f164b24465285d8ec413e0325a6341e4453
128128
| * Change-Id: 1235 ef7c3c85ad4c5875f308003d42a6e11d9b14aeb9
129-
| | * Change-Id: foo7 c1b55ea7e5f27f82d3565c1f5d64113adf635c2c
130-
| | | * Change-Id: 1235 02796cbb12e05f3be9f16c82e4d26542af7e700c
131-
| | | * Change-Id: foo7 2cbfa8cb8d9a9f1de029fcba547a6e56c742733f
129+
| | * 9da166dcfa8650e04c7e39c54a61b7fa0b69ef4f
130+
| | | * Change-Id: 1235 52310103e5d9a8e55df1a7766789a1af04d8601b
131+
| | | * 48f307ad20210547fdf339d0b0d7ee02bc702c3d
132132
| | |/
133133
| |/|
134134
| * | Change-Id: 1234 c61c37f4a3d5eb447f41dde15620eee1a181d60b
135135
| |/
136136
| * add file1 6ed6c1ca90cb15fe4edf8d133f0e2e44562aa77d
137137
| * Change-Id: 1235 (origin/@changes/master/josh@example.com/1235) 96da92a9021ee186e1e9dd82305ddebfd1153ed5
138138
|/
139-
* Change-Id: 1234 (origin/@changes/master/josh@example.com/1234) 43d6fcc9e7a81452d7343c78c0102f76027717fb
140-
| * Change-Id: foo7 (origin/@changes/master/josh@example.com/foo7) ecb19ea4b4fbfb6afff253ec719909e80a480a18
139+
| * (origin/@changes/master/josh@example.com/foo7) 7a54645bd1415d8b911ea129f90fb962799846d2
140+
| | * Change-Id: 1235 (origin/@heads/master/josh@example.com) 7fc3588b28a7c0e18be92f3e8303ccf632072804
141+
| | * bc99e4e2bb4f77e86e65630057da2cea96110852
142+
| |/
143+
|/|
144+
* | Change-Id: 1234 (origin/@changes/master/josh@example.com/1234) 43d6fcc9e7a81452d7343c78c0102f76027717fb
141145
|/
142146
* add file1 (origin/master, origin/HEAD) 5f2928c89c4dcc7f5a8c59ef65734a83620cefee
143147
* Notes added by 'git_note_create' from libgit2 725a17751b9dc03b1696fb894d0643c5b6f0397d
@@ -150,9 +154,9 @@ Test normal push still works
150154
$ git commit -q -m "add file4" -m "Change-Id: 1236"
151155
$ josh push
152156
To $TESTTMP/remote
153-
6ed6c1c..84f0380 84f0380f63011c5432945683f8f79426cc6bd180 -> master
157+
6ed6c1c..46af19d 46af19d75e628e41acb704f2fcae3973ed780d4a -> master
154158

155-
Pushed 84f0380f63011c5432945683f8f79426cc6bd180 to origin/master
159+
Pushed 46af19d75e628e41acb704f2fcae3973ed780d4a to origin/master
156160

157161
Verify normal push worked
158162

0 commit comments

Comments
 (0)