Skip to content

Commit 50d720c

Browse files
committed
Add a test that merge-base is found correctly
And fix the fact that it wasn't :-(
1 parent 863631a commit 50d720c

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

src/main.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ fn main() {
7474
if !msg.is_empty() {
7575
println!("Error: {}", e);
7676
}
77+
std::process::exit(1);
7778
}
7879
}
7980

@@ -128,18 +129,7 @@ fn get_upstream<'a>(
128129
.unwrap_or(false)
129130
})
130131
.ok_or_else(|| format!("cannot find branch with name {:?}", upstream_name))?;
131-
let result = Command::new("git")
132-
.args(&[
133-
"merge-base",
134-
head_branch.name().unwrap().unwrap(),
135-
branch.name().unwrap().unwrap(),
136-
])
137-
.output()?
138-
.stdout;
139-
let oid = Oid::from_str(std::str::from_utf8(&result)?.trim())?;
140-
let commit = repo.find_object(oid, None).unwrap();
141-
142-
commit
132+
branch.into_reference().peel(ObjectType::Commit)?
143133
} else {
144134
if let Ok(upstream) = head_branch.upstream() {
145135
upstream.into_reference().peel(ObjectType::Commit)?
@@ -148,7 +138,18 @@ fn get_upstream<'a>(
148138
}
149139
};
150140

151-
Ok(Some(upstream))
141+
let result = Command::new("git")
142+
.args(&[
143+
"merge-base",
144+
head_branch.name().unwrap().unwrap(),
145+
&upstream.id().to_string(),
146+
])
147+
.output()?
148+
.stdout;
149+
let oid = Oid::from_str(std::str::from_utf8(&result)?.trim())?;
150+
let commit = repo.find_object(oid, None).unwrap();
151+
152+
Ok(Some(commit))
152153
}
153154

154155
fn create_fixup_commit<'a>(

tests/basic.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,66 @@ new
7070
err
7171
);
7272
}
73+
74+
#[test]
75+
fn test_no_commit_in_range() {
76+
let td = assert_fs::TempDir::new().unwrap();
77+
git_init(&td);
78+
79+
for n in &["a", "b", "c", "d"] {
80+
git_file_commit(n, &td);
81+
}
82+
git(&["checkout", "-b", "changes", ":/c"], &td);
83+
git(&["branch", "-u", "main"], &td);
84+
for n in &["e", "f", "g"] {
85+
git_file_commit(&n, &td);
86+
}
87+
88+
let out = git_log(&td);
89+
assert_eq!(
90+
out,
91+
"\
92+
* g HEAD -> changes
93+
* f
94+
* e
95+
| * d main
96+
|/
97+
* c
98+
* b
99+
* a
100+
",
101+
"log:\n{}",
102+
out
103+
);
104+
105+
td.child("new").touch().unwrap();
106+
git(&["add", "new"], &td);
107+
108+
let assertion = fixup(&td).args(&["-P", "b"]).assert().failure();
109+
let out = string(assertion.get_output().stdout.clone());
110+
assert!(out.contains("No commit contains the pattern"), out);
111+
112+
fixup(&td).args(&["-P", "e"]).assert().success();
113+
114+
let shown = git_out(
115+
&["diff-tree", "--no-commit-id", "--name-only", "-r", ":/e"],
116+
&td,
117+
);
118+
let files = string(shown.stdout);
119+
let err = string(shown.stderr);
120+
121+
assert_eq!(
122+
files,
123+
"\
124+
file_e
125+
new
126+
",
127+
"out: {} err: {}",
128+
files,
129+
err
130+
);
131+
}
132+
73133
///////////////////////////////////////////////////////////////////////////////
74134
// Helpers
75135

0 commit comments

Comments
 (0)