Skip to content

Commit

Permalink
fix(switch): support - shorthand
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonrcarter committed Dec 8, 2024
1 parent 93a7c3b commit e9386a7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

- (#1463): `git switch` now accepts a revset that evaluates to a set with a single head, and that head will be checked out

## [v0.10.0] - 2024-10-10

### Added
Expand Down
5 changes: 5 additions & 0 deletions git-branchless-navigation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,11 @@ pub fn switch(
(true, None) => Target::Interactive(String::new()),
(false, Some(target)) => match repo.revparse_single_commit(target.to_string().as_ref()) {
Ok(Some(_)) => Target::Revision(target.to_string()),
// special case: - is shorthand for @{-1} but isn't supported as
// such by revparse_single_commit() or as a revset
Ok(None) | Err(_) if target.to_string().as_str() == "-" => {
Target::Revision(target.to_string())
}
Ok(None) | Err(_) => Target::Revset(target.clone()),
},
(false, None) => Target::None,
Expand Down
26 changes: 26 additions & 0 deletions git-branchless/tests/test_navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,32 @@ fn test_navigation_switch_revset() -> eyre::Result<()> {
"###);
}

{
// switching back to "last checkout"
let (stdout, _stderr) = git.branchless("switch", &["@{-1}"])?;
insta::assert_snapshot!(stdout, @r###"
branchless: running command: <git-executable> checkout @{-1}
O f777ecc (master) create initial.txt
|\
| o 62fc20d create test1.txt
|
@ fe65c1f create test2.txt
"###);
}

{
// switching back to "last checkout"
let (stdout, _stderr) = git.branchless("switch", &["-"])?;
insta::assert_snapshot!(stdout, @r###"
branchless: running command: <git-executable> checkout -
@ f777ecc (master) create initial.txt
|\
| o 62fc20d create test1.txt
|
o fe65c1f create test2.txt
"###);
}

Ok(())
}

Expand Down

0 comments on commit e9386a7

Please sign in to comment.