Skip to content

Commit b0bc672

Browse files
fix(switch): support - shorthand
1 parent 34584a0 commit b0bc672

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

git-branchless-lib/src/git/repo.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,9 @@ impl Repo {
938938
return Err(Error::UnsupportedRevParseSpec(spec.to_owned()));
939939
}
940940

941+
// `libgit2` doesn't understand that `-` is short for `@{-1}`
942+
let spec = if spec == "-" { "@{-1}" } else { spec };
943+
941944
match self.inner.revparse_single(spec) {
942945
Ok(object) => match object.into_commit() {
943946
Ok(commit) => Ok(Some(Commit { inner: commit })),

git-branchless/tests/test_navigation.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,67 @@ fn test_navigation_switch_revset() -> eyre::Result<()> {
909909
"###);
910910
}
911911

912+
{
913+
// switching back to "last checkout"
914+
let (stdout, _stderr) = git.branchless("switch", &["@{-1}"])?;
915+
insta::assert_snapshot!(stdout, @r###"
916+
branchless: running command: <git-executable> checkout @{-1}
917+
O f777ecc (master) create initial.txt
918+
|\
919+
| o 62fc20d create test1.txt
920+
|
921+
@ fe65c1f create test2.txt
922+
"###);
923+
}
924+
925+
{
926+
// switching back to "last checkout"
927+
let (stdout, _stderr) = git.branchless("switch", &["-"])?;
928+
insta::assert_snapshot!(stdout, @r###"
929+
branchless: running command: <git-executable> checkout -
930+
@ f777ecc (master) create initial.txt
931+
|\
932+
| o 62fc20d create test1.txt
933+
|
934+
o fe65c1f create test2.txt
935+
"###);
936+
}
937+
938+
{
939+
// switching back to "last checkout" will also checkout last checked out
940+
// branch, if any
941+
942+
let (stdout, _stderr) = git.branchless("switch", &["master"])?;
943+
insta::assert_snapshot!(stdout, @r###"
944+
branchless: running command: <git-executable> checkout master
945+
@ f777ecc (> master) create initial.txt
946+
|\
947+
| o 62fc20d create test1.txt
948+
|
949+
o fe65c1f create test2.txt
950+
"###);
951+
952+
let (stdout, _stderr) = git.branchless("switch", &["@{-2}"])?;
953+
insta::assert_snapshot!(stdout, @r###"
954+
branchless: running command: <git-executable> checkout @{-2}
955+
O f777ecc (master) create initial.txt
956+
|\
957+
| o 62fc20d create test1.txt
958+
|
959+
@ fe65c1f create test2.txt
960+
"###);
961+
962+
let (stdout, _stderr) = git.branchless("switch", &["-"])?;
963+
insta::assert_snapshot!(stdout, @r###"
964+
branchless: running command: <git-executable> checkout -
965+
@ f777ecc (> master) create initial.txt
966+
|\
967+
| o 62fc20d create test1.txt
968+
|
969+
o fe65c1f create test2.txt
970+
"###);
971+
}
972+
912973
Ok(())
913974
}
914975

0 commit comments

Comments
 (0)