diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0a77d5b..3b64def14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/git-branchless-navigation/src/lib.rs b/git-branchless-navigation/src/lib.rs index 30beff614..98110efb0 100644 --- a/git-branchless-navigation/src/lib.rs +++ b/git-branchless-navigation/src/lib.rs @@ -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, diff --git a/git-branchless/tests/test_navigation.rs b/git-branchless/tests/test_navigation.rs index 81115274b..ca5790d50 100644 --- a/git-branchless/tests/test_navigation.rs +++ b/git-branchless/tests/test_navigation.rs @@ -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: 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: checkout - + @ f777ecc (master) create initial.txt + |\ + | o 62fc20d create test1.txt + | + o fe65c1f create test2.txt + "###); + } + Ok(()) }