Skip to content

Commit 7579e96

Browse files
committed
Improve error message if upstream is the same commit as HEAD
1 parent a348518 commit 7579e96

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ authors = ["Brandon W Maister <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8+
anyhow = "1.0"
89
git2 = { version = "0.13.0", default_features = false }
910
dialoguer = "0.5.0"
1011
structopt = "0.3"

src/main.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::env;
1717
use std::error::Error;
1818
use std::process::Command;
1919

20+
use anyhow::bail;
2021
use console::style;
2122
use dialoguer::{Confirmation, Select};
2223
use git2::{Branch, Commit, Diff, Object, ObjectType, Oid, Repository};
@@ -194,7 +195,7 @@ fn select_commit_to_amend<'a>(
194195
repo: &'a Repository,
195196
upstream: Option<Object<'a>>,
196197
max_commits: usize,
197-
) -> Result<Commit<'a>, Box<dyn Error>> {
198+
) -> Result<Commit<'a>, anyhow::Error> {
198199
let mut walker = repo.revwalk()?;
199200
walker.push_head()?;
200201
let commits = if let Some(upstream) = upstream.as_ref() {
@@ -212,6 +213,13 @@ fn select_commit_to_amend<'a>(
212213
.map(|rev| repo.find_commit(rev))
213214
.collect::<Result<Vec<_>, _>>()?
214215
};
216+
if commits.len() == 0 {
217+
bail!(
218+
"No commits between {} and {:?}",
219+
format_ref(&repo.head()?)?,
220+
upstream.map(|u| u.id()).unwrap()
221+
);
222+
}
215223
let branches: HashMap<Oid, String> = repo
216224
.branches(None)?
217225
.filter_map(|b| {
@@ -251,6 +259,12 @@ fn select_commit_to_amend<'a>(
251259
Ok(repo.find_commit(commits[selected?].id())?)
252260
}
253261

262+
fn format_ref(rf: &git2::Reference<'_>) -> Result<String, anyhow::Error> {
263+
let shorthand = rf.shorthand().unwrap_or("<unnamed>");
264+
let sha = rf.peel_to_commit()?.id().to_string();
265+
Ok(format!("{} ({})", shorthand, &sha[..10]))
266+
}
267+
254268
fn print_diff(kind: Changes) -> Result<(), Box<dyn Error>> {
255269
let mut args = vec!["diff", "--stat"];
256270
if kind == Changes::Staged {

0 commit comments

Comments
 (0)