Skip to content

Commit cd8abc3

Browse files
refactor: add force_detach checkout option
This will be used to circumvent the "autocheckout" setting in `git split` (in the next commit), so that we can preserve the current detached/attached state. FIXME is this really needed? Maybe we don't need to force-re-checkout?
1 parent b1f7ae5 commit cd8abc3

File tree

9 files changed

+18
-1
lines changed

9 files changed

+18
-1
lines changed

git-branchless-lib/src/core/check_out.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ pub struct CheckOutCommitOptions {
4444
/// Additional arguments to pass to `git checkout`.
4545
pub additional_args: Vec<OsString>,
4646

47+
/// Ignore the `autoSwitchBranches` setting?
48+
pub force_detach: bool,
49+
4750
/// Use `git reset` rather than `git checkout`; that is, leave the index and
4851
/// working copy unchanged, and just adjust the `HEAD` pointer.
4952
pub reset: bool,
@@ -56,6 +59,7 @@ impl Default for CheckOutCommitOptions {
5659
fn default() -> Self {
5760
Self {
5861
additional_args: Default::default(),
62+
force_detach: false,
5963
reset: false,
6064
render_smartlog: true,
6165
}
@@ -116,6 +120,7 @@ pub fn check_out_commit(
116120
) -> EyreExitOr<()> {
117121
let CheckOutCommitOptions {
118122
additional_args,
123+
force_detach,
119124
reset,
120125
render_smartlog,
121126
} = options;
@@ -134,7 +139,7 @@ pub fn check_out_commit(
134139
create_snapshot(effects, git_run_info, repo, event_log_db, event_tx_id)?;
135140
}
136141

137-
let target = if get_auto_switch_branches(repo)? && !reset {
142+
let target = if get_auto_switch_branches(repo)? && !reset && !force_detach {
138143
maybe_get_branch_name(target, oid, repo)?
139144
} else {
140145
target

git-branchless-lib/tests/test_rewrite_plan.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ fn create_and_execute_plan(
762762
resolve_merge_conflicts: true,
763763
check_out_commit_options: CheckOutCommitOptions {
764764
additional_args: Default::default(),
765+
force_detach: false,
765766
reset: false,
766767
render_smartlog: false,
767768
},

git-branchless-navigation/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ pub fn switch(
617617
target,
618618
&CheckOutCommitOptions {
619619
additional_args,
620+
force_detach: false,
620621
reset: false,
621622
render_smartlog: true,
622623
},

git-branchless-record/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ fn record(
129129
None,
130130
&CheckOutCommitOptions {
131131
additional_args: vec![OsString::from("-b"), OsString::from(branch_name)],
132+
force_detach: false,
132133
reset: false,
133134
render_smartlog: false,
134135
},
@@ -227,6 +228,7 @@ fn record(
227228
checkout_target,
228229
&CheckOutCommitOptions {
229230
additional_args: vec![],
231+
force_detach: false,
230232
reset: false,
231233
render_smartlog: false,
232234
},

git-branchless-reword/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ pub fn reword(
292292
resolve_merge_conflicts: false,
293293
check_out_commit_options: CheckOutCommitOptions {
294294
additional_args: Default::default(),
295+
force_detach: false,
295296
reset: false,
296297
render_smartlog: false,
297298
},

git-branchless-undo/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ fn extract_checkout_target(
743743
target: CheckoutTarget::Oid(*new_oid),
744744
options: CheckOutCommitOptions {
745745
additional_args: vec!["--detach".into()],
746+
force_detach: false,
746747
reset: false,
747748
render_smartlog: true,
748749
},
@@ -766,6 +767,7 @@ fn extract_checkout_target(
766767
}
767768
None => Default::default(),
768769
},
770+
force_detach: false,
769771
reset: false,
770772
render_smartlog: true,
771773
},
@@ -1077,6 +1079,7 @@ mod tests {
10771079
additional_args: [
10781080
"--detach",
10791081
],
1082+
force_detach: false,
10801083
reset: false,
10811084
render_smartlog: true,
10821085
},

git-branchless/src/commands/amend.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ pub fn amend(
203203
Some(target),
204204
&CheckOutCommitOptions {
205205
additional_args: Default::default(),
206+
force_detach: false,
206207
reset: true,
207208
render_smartlog: false,
208209
},
@@ -297,6 +298,7 @@ pub fn amend(
297298
resolve_merge_conflicts: move_options.resolve_merge_conflicts,
298299
check_out_commit_options: CheckOutCommitOptions {
299300
additional_args: Default::default(),
301+
force_detach: false,
300302
reset: false,
301303
render_smartlog: false,
302304
},

git-branchless/src/commands/restack.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ pub fn restack(
328328
resolve_merge_conflicts,
329329
check_out_commit_options: CheckOutCommitOptions {
330330
additional_args: Default::default(),
331+
force_detach: false,
331332
reset: false,
332333
render_smartlog: false,
333334
},

git-branchless/src/commands/sync.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub fn sync(
9494
resolve_merge_conflicts,
9595
check_out_commit_options: CheckOutCommitOptions {
9696
additional_args: Default::default(),
97+
force_detach: false,
9798
reset: false,
9899
render_smartlog: false,
99100
},

0 commit comments

Comments
 (0)