Skip to content

Commit b91e20f

Browse files
authored
Merge pull request #10708 from gitbutlerapp/parse-gerrit-wip-tag
Handle [WIP] tag in Gerrit push parsing
2 parents c7d0c7a + a64c5ec commit b91e20f

File tree

1 file changed

+89
-9
lines changed

1 file changed

+89
-9
lines changed

crates/but-gerrit/src/parse.rs

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct ChangeInfo {
1515
pub url: String,
1616
pub commit_title: String,
1717
pub is_new: bool,
18+
pub is_wip: bool,
1819
}
1920

2021
#[derive(Clone, Debug, PartialEq)]
@@ -106,6 +107,7 @@ enum ChangeSection {
106107
fn parse_change_info(line: &str) -> Option<ChangeInfo> {
107108
// Parse lines like:
108109
// "http://15a45d4cba1a/c/gerrit-test/+/42 aaaaaaa [NEW]"
110+
// "http://15a45d4cba1a/c/gerrit-test/+/47 hello [WIP] [NEW]"
109111
// or "http://15a45d4cba1a/c/gerrit-test/+/41 sup5"
110112

111113
let parts: Vec<&str> = line.splitn(2, ' ').collect();
@@ -115,24 +117,32 @@ fn parse_change_info(line: &str) -> Option<ChangeInfo> {
115117
url: line.to_string(),
116118
commit_title: String::new(),
117119
is_new: false,
120+
is_wip: false,
118121
});
119122
}
120123

121124
let url = parts[0].to_string();
122125
let rest = parts[1];
123126

124-
// Check if it ends with [NEW]
125-
let is_new = rest.ends_with("[NEW]");
126-
let commit_title = if is_new {
127-
rest.trim_end_matches(" [NEW]").trim().to_string()
128-
} else {
129-
rest.trim().to_string()
130-
};
127+
// Check for [WIP] and [NEW] tags
128+
let is_wip = rest.contains("[WIP]");
129+
let is_new = rest.contains("[NEW]");
130+
131+
// Remove tags to get clean commit title
132+
let mut commit_title = rest.to_string();
133+
if is_wip {
134+
commit_title = commit_title.replace(" [WIP]", "");
135+
}
136+
if is_new {
137+
commit_title = commit_title.replace(" [NEW]", "");
138+
}
139+
let commit_title = commit_title.trim().to_string();
131140

132141
Some(ChangeInfo {
133142
url,
134143
commit_title,
135144
is_new,
145+
is_wip,
136146
})
137147
}
138148

@@ -338,6 +348,7 @@ remote:"#;
338348
url: "http://example.com/change/123".to_string(),
339349
commit_title: "my commit message".to_string(),
340350
is_new: false,
351+
is_wip: false,
341352
};
342353
assert_eq!(result, Some(expected));
343354

@@ -347,6 +358,7 @@ remote:"#;
347358
url: "http://gerrit.local/c/project/+/41".to_string(),
348359
commit_title: "fix bug".to_string(),
349360
is_new: true,
361+
is_wip: false,
350362
};
351363
assert_eq!(result, Some(expected));
352364

@@ -356,6 +368,7 @@ remote:"#;
356368
url: "http://example.com/change/123".to_string(),
357369
commit_title: String::new(),
358370
is_new: false,
371+
is_wip: false,
359372
};
360373
assert_eq!(result, Some(expected));
361374

@@ -366,13 +379,80 @@ remote:"#;
366379
url: "http://gerrit.local/c/project/+/41".to_string(),
367380
commit_title: "fix: update dependencies".to_string(),
368381
is_new: true,
382+
is_wip: false,
369383
};
370384
assert_eq!(result, Some(expected));
371385
}
372386

387+
#[test]
388+
fn test_parse_wip_changes() {
389+
// Test WIP tag only
390+
let result = parse_change_info("http://gerrit.local/c/project/+/47 hello [WIP]");
391+
let expected = ChangeInfo {
392+
url: "http://gerrit.local/c/project/+/47".to_string(),
393+
commit_title: "hello".to_string(),
394+
is_new: false,
395+
is_wip: true,
396+
};
397+
assert_eq!(result, Some(expected));
398+
399+
// Test both WIP and NEW tags
400+
let result = parse_change_info("http://15a45d4cba1a/c/gerrit-test/+/47 hello [WIP] [NEW]");
401+
let expected = ChangeInfo {
402+
url: "http://15a45d4cba1a/c/gerrit-test/+/47".to_string(),
403+
commit_title: "hello".to_string(),
404+
is_new: true,
405+
is_wip: true,
406+
};
407+
assert_eq!(result, Some(expected));
408+
409+
// Test NEW and WIP in different order
410+
let result =
411+
parse_change_info("http://gerrit.local/c/project/+/48 multi word title [NEW] [WIP]");
412+
let expected = ChangeInfo {
413+
url: "http://gerrit.local/c/project/+/48".to_string(),
414+
commit_title: "multi word title".to_string(),
415+
is_new: true,
416+
is_wip: true,
417+
};
418+
assert_eq!(result, Some(expected));
419+
}
420+
421+
#[test]
422+
fn test_parse_wip_push_output() {
423+
let output = r#"remote:
424+
remote: Processing changes: refs: 1, new: 1
425+
remote: Processing changes: refs: 1, new: 1
426+
remote: Processing changes: refs: 1, new: 1
427+
remote: Processing changes: refs: 1, new: 1, done
428+
remote:
429+
remote: SUCCESS
430+
remote:
431+
remote: http://15a45d4cba1a/c/gerrit-test/+/47 hello [WIP] [NEW]
432+
remote:"#;
433+
434+
let result = push_output(output).unwrap();
435+
436+
assert!(result.success);
437+
assert_eq!(result.warnings.len(), 0);
438+
assert_eq!(result.changes.len(), 1);
439+
440+
let change = &result.changes[0];
441+
assert_eq!(change.url, "http://15a45d4cba1a/c/gerrit-test/+/47");
442+
assert_eq!(change.commit_title, "hello");
443+
assert!(change.is_new);
444+
assert!(change.is_wip);
445+
446+
assert!(result.processing_info.is_some());
447+
let processing = result.processing_info.unwrap();
448+
assert_eq!(processing.refs_count, 1);
449+
assert_eq!(processing.updated_count, None);
450+
assert_eq!(processing.new_count, Some(1));
451+
}
452+
373453
#[test]
374454
fn test_parse_new_changes_section() {
375-
let output = r#"remote: Processing changes: new: 1, done
455+
let output = r#"remote: Processing changes: refs: 1, new: 1, done
376456
remote:
377457
remote: New Changes:
378458
remote: http://gerrithost/#/c/RecipeBook/+/702 Change to a proper, yeast based pizza dough.
@@ -401,7 +481,7 @@ remote:"#;
401481

402482
#[test]
403483
fn test_parse_updated_changes_section() {
404-
let output = r#"remote: Processing changes: updated: 1, done
484+
let output = r#"remote: Processing changes: refs: 1, updated: 1, done
405485
remote:
406486
remote: Updated Changes:
407487
remote: http://gerrithost/#/c/RecipeBook/+/702 Change to a proper, yeast based pizza dough.

0 commit comments

Comments
 (0)