Skip to content

Commit faab5b7

Browse files
committed
Don't error if HEAD has no upstream
1 parent f25e507 commit faab5b7

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/main.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn run(squash: bool, max_commits: usize) -> Result<(), Box<dyn Error>> {
8181
let diff = repo.diff_tree_to_index(Some(&head_tree), None, None)?;
8282
let upstream = get_upstream(&repo, &head_branch)?;
8383
let commit_to_amend =
84-
create_fixup_commit(&repo, &head_branch, &upstream, &diff, squash, max_commits)?;
84+
create_fixup_commit(&repo, &head_branch, upstream, &diff, squash, max_commits)?;
8585
println!(
8686
"selected: {} {}",
8787
&commit_to_amend.id().to_string()[0..10],
@@ -100,7 +100,7 @@ fn run(squash: bool, max_commits: usize) -> Result<(), Box<dyn Error>> {
100100
fn get_upstream<'a>(
101101
repo: &'a Repository,
102102
head_branch: &'a Branch,
103-
) -> Result<Object<'a>, Box<dyn Error>> {
103+
) -> Result<Option<Object<'a>>, Box<dyn Error>> {
104104
let upstream = if let Ok(upstream_name) = env::var(UPSTREAM_VAR) {
105105
let branch = repo
106106
.branches(None)?
@@ -124,20 +124,20 @@ fn get_upstream<'a>(
124124

125125
commit
126126
} else {
127-
head_branch
128-
.upstream()
129-
.unwrap()
130-
.into_reference()
131-
.peel(ObjectType::Commit)?
127+
if let Ok(upstream) = head_branch.upstream() {
128+
upstream.into_reference().peel(ObjectType::Commit)?
129+
} else {
130+
return Ok(None);
131+
}
132132
};
133133

134-
Ok(upstream)
134+
Ok(Some(upstream))
135135
}
136136

137137
fn create_fixup_commit<'a>(
138138
repo: &'a Repository,
139139
head_branch: &'a Branch,
140-
upstream: &'a Object,
140+
upstream: Option<Object<'a>>,
141141
diff: &'a Diff,
142142
squash: bool,
143143
max_commits: usize,
@@ -164,7 +164,7 @@ fn create_fixup_commit<'a>(
164164
println!("Staged changes:");
165165
print_diff(Changes::Staged)?;
166166
}
167-
let commit_to_amend = select_commit_to_amend(&repo, Some(upstream), max_commits)?;
167+
let commit_to_amend = select_commit_to_amend(&repo, upstream, max_commits)?;
168168
do_fixup_commit(&repo, &head_branch, &commit_to_amend, squash)?;
169169
Ok(commit_to_amend)
170170
}
@@ -191,12 +191,12 @@ fn do_fixup_commit<'a>(
191191

192192
fn select_commit_to_amend<'a>(
193193
repo: &'a Repository,
194-
upstream: Option<&Object<'a>>,
194+
upstream: Option<Object<'a>>,
195195
max_commits: usize,
196196
) -> Result<Commit<'a>, Box<dyn Error>> {
197197
let mut walker = repo.revwalk()?;
198198
walker.push_head()?;
199-
let commits = if let Some(upstream) = upstream {
199+
let commits = if let Some(upstream) = upstream.as_ref() {
200200
let upstream_oid = upstream.id();
201201
walker
202202
.flat_map(|r| r)
@@ -221,7 +221,11 @@ fn select_commit_to_amend<'a>(
221221
)
222222
})
223223
.collect::<Vec<_>>();
224-
eprintln!("Select a commit to amend:");
224+
if upstream.is_none() {
225+
eprintln!("Select a commit to amend (no upstream for HEAD):");
226+
} else {
227+
eprintln!("Select a commit to amend:");
228+
}
225229
let selected = Select::new().items(&rev_aliases).default(0).interact();
226230
Ok(repo.find_commit(commits[selected?].id())?)
227231
}

0 commit comments

Comments
 (0)