Skip to content

Commit 843a6cb

Browse files
refactor(record): combine detach and stash logic
1 parent 8a760d4 commit 843a6cb

File tree

1 file changed

+54
-41
lines changed
  • git-branchless-record/src

1 file changed

+54
-41
lines changed

git-branchless-record/src/lib.rs

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -177,64 +177,77 @@ fn record(
177177

178178
if detach || stash {
179179
let head_info = repo.get_head_info()?;
180-
if let ResolvedReferenceInfo {
181-
oid: Some(oid),
182-
reference_name: Some(reference_name),
183-
} = &head_info
184-
{
185-
let head_commit = repo.find_commit_or_fail(*oid)?;
186-
match head_commit.get_parents().as_slice() {
187-
[] => try_exit_code!(git_run_info.run(
188-
effects,
189-
Some(event_tx_id),
190-
&[
191-
"update-ref",
192-
"-d",
193-
reference_name.as_str(),
194-
&oid.to_string(),
195-
],
196-
)?),
197-
[parent_commit] => {
198-
let branch_name = CategorizedReferenceName::new(reference_name).render_suffix();
199-
repo.detach_head(&head_info)?;
200-
try_exit_code!(git_run_info.run(
180+
let checkout_target = match &head_info {
181+
ResolvedReferenceInfo {
182+
oid: None,
183+
reference_name: Some(reference_name),
184+
} => {
185+
// FIXME: unborn HEAD, what to do?
186+
stash.then(|| CheckoutTarget::Reference(reference_name.clone()))
187+
}
188+
189+
ResolvedReferenceInfo {
190+
oid: Some(oid),
191+
reference_name: Some(reference_name),
192+
} => {
193+
let head_commit = repo.find_commit_or_fail(*oid)?;
194+
match head_commit.get_parents().as_slice() {
195+
[] => try_exit_code!(git_run_info.run(
201196
effects,
202197
Some(event_tx_id),
203198
&[
204-
"branch",
205-
"-f",
206-
&branch_name,
207-
&parent_commit.get_oid().to_string(),
199+
"update-ref",
200+
"-d",
201+
reference_name.as_str(),
202+
&oid.to_string(),
208203
],
209-
)?);
210-
}
211-
parent_commits => {
212-
eyre::bail!("git-branchless record --detach called on a merge commit, but it should only be capable of creating zero- or one-parent commits. Parents: {parent_commits:?}");
204+
)?),
205+
[parent_commit] => {
206+
let branch_name =
207+
CategorizedReferenceName::new(reference_name).render_suffix();
208+
repo.detach_head(&head_info)?;
209+
try_exit_code!(git_run_info.run(
210+
effects,
211+
Some(event_tx_id),
212+
&[
213+
"branch",
214+
"-f",
215+
&branch_name,
216+
&parent_commit.get_oid().to_string(),
217+
],
218+
)?);
219+
}
220+
parent_commits => {
221+
eyre::bail!("git-branchless record --detach called on a merge commit, but it should only be capable of creating zero- or one-parent commits. Parents: {parent_commits:?}");
222+
}
213223
}
224+
225+
stash.then(|| CheckoutTarget::Reference(reference_name.clone()))
214226
}
215-
}
216-
let checkout_target = match head_info {
217-
ResolvedReferenceInfo {
218-
oid: _,
219-
reference_name: Some(reference_name),
220-
} => Some(CheckoutTarget::Reference(reference_name.clone())),
227+
221228
ResolvedReferenceInfo {
222229
oid: Some(oid),
223-
reference_name: _,
230+
reference_name: None,
224231
} => {
225-
let head_commit = repo.find_commit_or_fail(oid)?;
232+
let head_commit = repo.find_commit_or_fail(*oid)?;
226233
match head_commit.get_parents().as_slice() {
227-
[] => None,
228-
[parent_commit] => Some(CheckoutTarget::Oid(parent_commit.get_oid())),
234+
[] => {
235+
eyre::bail!("git-branchless record --stash seems to have created a root commit (commit without parents), but this should be impossible.");
236+
}
237+
[parent_commit] => stash.then(|| CheckoutTarget::Oid(parent_commit.get_oid())),
229238
parent_commits => {
230239
eyre::bail!("git-branchless record --stash seems to have created a merge commit, but this should be impossible. Parents: {parent_commits:?}");
231240
}
232241
}
233242
}
234-
_ => None,
243+
244+
ResolvedReferenceInfo {
245+
oid: None,
246+
reference_name: None,
247+
} => None,
235248
};
236249

237-
if stash && checkout_target.is_some() {
250+
if checkout_target.is_some() {
238251
try_exit_code!(check_out_commit(
239252
effects,
240253
git_run_info,

0 commit comments

Comments
 (0)