Skip to content

Commit

Permalink
Jump to commit via sha (#1818)
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarAbouZor authored Aug 27, 2023
1 parent 005047f commit c68fa3e
Show file tree
Hide file tree
Showing 6 changed files with 366 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* added to [anaconda](https://anaconda.org/conda-forge/gitui) [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1626](https://github.com/extrawurst/gitui/issues/1626))
* visualize empty line substituted with content in diff better ([#1359](https://github.com/extrawurst/gitui/issues/1359))
* checkout branch works with non-empty status report [[@lightsnowball](https://github.com/lightsnowball)] ([#1399](https://github.com/extrawurst/gitui/issues/1399))
* jump to commit by SHA [[@AmmarAbouZor](https://github.com/AmmarAbouZor)] ([#1818](https://github.com/extrawurst/gitui/pull/1818))

### Fixes
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))
Expand Down
43 changes: 42 additions & 1 deletion asyncgit/src/sync/commits_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ impl CommitId {
pub fn get_short_string(&self) -> String {
self.to_string().chars().take(7).collect()
}

/// Tries to retrieve the `CommitId` form the revision if exists in the given repository
pub fn from_revision(
repo_path: &RepoPath,
revision: &str,
) -> Result<Self> {
scope_time!("CommitId::from_revision");

let repo = repo(repo_path)?;

let commit_obj = repo.revparse_single(revision)?;
Ok(commit_obj.id().into())
}
}

impl ToString for CommitId {
Expand Down Expand Up @@ -144,7 +157,7 @@ mod tests {
error::Result,
sync::{
commit, stage_add_file, tests::repo_init_empty,
utils::get_head_repo, RepoPath,
utils::get_head_repo, CommitId, RepoPath,
},
};
use std::{fs::File, io::Write, path::Path};
Expand Down Expand Up @@ -221,4 +234,32 @@ mod tests {

Ok(())
}

#[test]
fn test_get_commit_from_revision() -> Result<()> {
let (_td, repo) = repo_init_empty().unwrap();
let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();

let foo_file = Path::new("foo");
File::create(root.join(foo_file))?.write_all(b"a")?;
stage_add_file(repo_path, foo_file).unwrap();
let c1 = commit(repo_path, "subject: foo\nbody").unwrap();
let c1_rev = c1.get_short_string();

assert_eq!(
CommitId::from_revision(repo_path, c1_rev.as_str())
.unwrap(),
c1
);

const FOREIGN_HASH: &str =
"d6d7d55cb6e4ba7301d6a11a657aab4211e5777e";
assert!(
CommitId::from_revision(repo_path, FOREIGN_HASH).is_err()
);

Ok(())
}
}
1 change: 1 addition & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ impl App {
key_config.clone(),
),
log_search_popup: LogSearchPopupComponent::new(
repo.clone(),
&queue,
theme.clone(),
key_config.clone(),
Expand Down
Loading

0 comments on commit c68fa3e

Please sign in to comment.