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 5cc1b7a
Show file tree
Hide file tree
Showing 3 changed files with 31 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
3 changes: 3 additions & 0 deletions git-branchless-lib/src/git/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,9 @@ impl Repo {
return Err(Error::UnsupportedRevParseSpec(spec.to_owned()));
}

// `libgit2` doesn't understand that `-` is short for `@{-1}`
let spec = if spec == "-" { "@{-1}" } else { spec };

match self.inner.revparse_single(spec) {
Ok(object) => match object.into_commit() {
Ok(commit) => Ok(Some(Commit { inner: commit })),
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 5cc1b7a

Please sign in to comment.