Skip to content

Commit c68fa3e

Browse files
authored
Jump to commit via sha (#1818)
1 parent 005047f commit c68fa3e

File tree

6 files changed

+366
-108
lines changed

6 files changed

+366
-108
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3333
* added to [anaconda](https://anaconda.org/conda-forge/gitui) [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1626](https://github.com/extrawurst/gitui/issues/1626))
3434
* visualize empty line substituted with content in diff better ([#1359](https://github.com/extrawurst/gitui/issues/1359))
3535
* checkout branch works with non-empty status report [[@lightsnowball](https://github.com/lightsnowball)] ([#1399](https://github.com/extrawurst/gitui/issues/1399))
36+
* jump to commit by SHA [[@AmmarAbouZor](https://github.com/AmmarAbouZor)] ([#1818](https://github.com/extrawurst/gitui/pull/1818))
3637

3738
### Fixes
3839
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))

asyncgit/src/sync/commits_info.rs

+42-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ impl CommitId {
3131
pub fn get_short_string(&self) -> String {
3232
self.to_string().chars().take(7).collect()
3333
}
34+
35+
/// Tries to retrieve the `CommitId` form the revision if exists in the given repository
36+
pub fn from_revision(
37+
repo_path: &RepoPath,
38+
revision: &str,
39+
) -> Result<Self> {
40+
scope_time!("CommitId::from_revision");
41+
42+
let repo = repo(repo_path)?;
43+
44+
let commit_obj = repo.revparse_single(revision)?;
45+
Ok(commit_obj.id().into())
46+
}
3447
}
3548

3649
impl ToString for CommitId {
@@ -144,7 +157,7 @@ mod tests {
144157
error::Result,
145158
sync::{
146159
commit, stage_add_file, tests::repo_init_empty,
147-
utils::get_head_repo, RepoPath,
160+
utils::get_head_repo, CommitId, RepoPath,
148161
},
149162
};
150163
use std::{fs::File, io::Write, path::Path};
@@ -221,4 +234,32 @@ mod tests {
221234

222235
Ok(())
223236
}
237+
238+
#[test]
239+
fn test_get_commit_from_revision() -> Result<()> {
240+
let (_td, repo) = repo_init_empty().unwrap();
241+
let root = repo.path().parent().unwrap();
242+
let repo_path: &RepoPath =
243+
&root.as_os_str().to_str().unwrap().into();
244+
245+
let foo_file = Path::new("foo");
246+
File::create(root.join(foo_file))?.write_all(b"a")?;
247+
stage_add_file(repo_path, foo_file).unwrap();
248+
let c1 = commit(repo_path, "subject: foo\nbody").unwrap();
249+
let c1_rev = c1.get_short_string();
250+
251+
assert_eq!(
252+
CommitId::from_revision(repo_path, c1_rev.as_str())
253+
.unwrap(),
254+
c1
255+
);
256+
257+
const FOREIGN_HASH: &str =
258+
"d6d7d55cb6e4ba7301d6a11a657aab4211e5777e";
259+
assert!(
260+
CommitId::from_revision(repo_path, FOREIGN_HASH).is_err()
261+
);
262+
263+
Ok(())
264+
}
224265
}

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl App {
273273
key_config.clone(),
274274
),
275275
log_search_popup: LogSearchPopupComponent::new(
276+
repo.clone(),
276277
&queue,
277278
theme.clone(),
278279
key_config.clone(),

0 commit comments

Comments
 (0)