Skip to content

Commit dfe284a

Browse files
author
Stephan Dilly
committed
some cleanup
1 parent 6be72c7 commit dfe284a

File tree

5 files changed

+56
-76
lines changed

5 files changed

+56
-76
lines changed

asyncgit/src/sync/commit.rs

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,6 @@ use super::{utils::repo, CommitId};
22
use crate::error::Result;
33
use scopetime::scope_time;
44

5-
///
6-
pub fn get_head(repo_path: &str) -> Result<CommitId> {
7-
scope_time!("get_head");
8-
9-
let repo = repo(repo_path)?;
10-
11-
let head_id = repo.head()?.target().expect("head target error");
12-
13-
Ok(CommitId::new(head_id))
14-
}
15-
165
///
176
pub fn amend(
187
repo_path: &str,
@@ -46,10 +35,9 @@ mod tests {
4635
use crate::error::Result;
4736
use crate::sync::{
4837
commit, get_commit_details, get_commit_files, stage_add_file,
49-
tests::{repo_init, repo_init_empty},
50-
CommitId, LogWalker,
38+
tests::repo_init_empty, utils::get_head, CommitId, LogWalker,
5139
};
52-
use commit::{amend, get_head};
40+
use commit::amend;
5341
use git2::Repository;
5442
use std::{fs::File, io::Write, path::Path};
5543

@@ -96,26 +84,4 @@ mod tests {
9684

9785
Ok(())
9886
}
99-
100-
#[test]
101-
fn test_head_empty() -> Result<()> {
102-
let (_td, repo) = repo_init_empty()?;
103-
let root = repo.path().parent().unwrap();
104-
let repo_path = root.as_os_str().to_str().unwrap();
105-
106-
assert_eq!(get_head(repo_path).is_ok(), false);
107-
108-
Ok(())
109-
}
110-
111-
#[test]
112-
fn test_head() -> Result<()> {
113-
let (_td, repo) = repo_init()?;
114-
let root = repo.path().parent().unwrap();
115-
let repo_path = root.as_os_str().to_str().unwrap();
116-
117-
assert_eq!(get_head(repo_path).is_ok(), true);
118-
119-
Ok(())
120-
}
12187
}

asyncgit/src/sync/diff.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use git2::{
88
};
99
use scopetime::scope_time;
1010
use std::{fs, path::Path};
11-
use utils::work_dir;
11+
use utils::{get_head_repo, work_dir};
1212

1313
/// type of diff of a single line
1414
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
@@ -89,16 +89,8 @@ pub(crate) fn get_diff_raw<'a>(
8989

9090
let diff = if stage {
9191
// diff against head
92-
if let Ok(ref_head) = repo.head() {
93-
let parent = repo.find_commit(
94-
//TODO: use new NoHead Error
95-
ref_head.target().ok_or_else(|| {
96-
let name = ref_head.name().unwrap_or("??");
97-
Error::Generic(
98-
format!("can not find the target of symbolic references: {}", name)
99-
)
100-
})?,
101-
)?;
92+
if let Ok(id) = get_head_repo(&repo) {
93+
let parent = repo.find_commit(id.into())?;
10294

10395
let tree = parent.tree()?;
10496
repo.diff_tree_to_index(

asyncgit/src/sync/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod tags;
1717
pub mod utils;
1818

1919
pub use branch::get_branch_name;
20-
pub use commit::{amend, get_head};
20+
pub use commit::amend;
2121
pub use commit_details::{get_commit_details, CommitDetails};
2222
pub use commit_files::get_commit_files;
2323
pub use commits_info::{get_commits_info, CommitId, CommitInfo};
@@ -30,8 +30,8 @@ pub use reset::{reset_stage, reset_workdir};
3030
pub use stash::{get_stashes, stash_apply, stash_drop, stash_save};
3131
pub use tags::{get_tags, Tags};
3232
pub use utils::{
33-
commit, commit_new, is_bare_repo, is_repo, stage_add_all,
34-
stage_add_file, stage_addremoved,
33+
commit, commit_new, get_head, is_bare_repo, is_repo,
34+
stage_add_all, stage_add_file, stage_addremoved,
3535
};
3636

3737
#[cfg(test)]

asyncgit/src/sync/reset.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::utils::repo;
2-
use crate::error::{Error, Result};
1+
use super::utils::{get_head_repo, repo};
2+
use crate::error::Result;
33
use git2::{build::CheckoutBuilder, ObjectType};
44
use scopetime::scope_time;
55

@@ -9,19 +9,9 @@ pub fn reset_stage(repo_path: &str, path: &str) -> Result<()> {
99

1010
let repo = repo(repo_path)?;
1111

12-
let head = repo.head();
13-
14-
if let Ok(reference) = head {
15-
let obj = repo.find_object(
16-
//TODO: use NoHead error type
17-
reference.target().ok_or_else(|| {
18-
Error::Generic(
19-
"can't get reference to symbolic reference,"
20-
.to_string(),
21-
)
22-
})?,
23-
Some(ObjectType::Commit),
24-
)?;
12+
if let Ok(id) = get_head_repo(&repo) {
13+
let obj =
14+
repo.find_object(id.into(), Some(ObjectType::Commit))?;
2515

2616
repo.reset_default(Some(&obj), &[path])?;
2717
} else {

asyncgit/src/sync/utils.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@ pub fn work_dir(repo: &Repository) -> &Path {
4747
repo.workdir().expect("unable to query workdir")
4848
}
4949

50+
///
51+
pub fn get_head(repo_path: &str) -> Result<CommitId> {
52+
let repo = repo(repo_path)?;
53+
get_head_repo(&repo)
54+
}
55+
56+
///
57+
pub fn get_head_repo(repo: &Repository) -> Result<CommitId> {
58+
scope_time!("get_head_repo");
59+
60+
let head = repo.head()?.target();
61+
62+
if let Some(head_id) = head {
63+
Ok(CommitId::new(head_id))
64+
} else {
65+
Err(Error::NoHead)
66+
}
67+
}
68+
5069
/// ditto
5170
pub fn commit_new(repo_path: &str, msg: &str) -> Result<CommitId> {
5271
commit(repo_path, msg).map(CommitId::new)
@@ -63,17 +82,8 @@ pub fn commit(repo_path: &str, msg: &str) -> Result<Oid> {
6382
let tree_id = index.write_tree()?;
6483
let tree = repo.find_tree(tree_id)?;
6584

66-
//TODO: use NoHead error
67-
let parents = if let Ok(reference) = repo.head() {
68-
let parent = repo.find_commit(
69-
reference.target().ok_or_else(|| {
70-
Error::Generic(
71-
"failed to get the target for reference"
72-
.to_string(),
73-
)
74-
})?,
75-
)?;
76-
vec![parent]
85+
let parents = if let Ok(id) = get_head(repo_path) {
86+
vec![repo.find_commit(id.into())?]
7787
} else {
7888
Vec::new()
7989
};
@@ -323,4 +333,26 @@ mod tests {
323333

324334
Ok(())
325335
}
336+
337+
#[test]
338+
fn test_head_empty() -> Result<()> {
339+
let (_td, repo) = repo_init_empty()?;
340+
let root = repo.path().parent().unwrap();
341+
let repo_path = root.as_os_str().to_str().unwrap();
342+
343+
assert_eq!(get_head(repo_path).is_ok(), false);
344+
345+
Ok(())
346+
}
347+
348+
#[test]
349+
fn test_head() -> Result<()> {
350+
let (_td, repo) = repo_init()?;
351+
let root = repo.path().parent().unwrap();
352+
let repo_path = root.as_os_str().to_str().unwrap();
353+
354+
assert_eq!(get_head(repo_path).is_ok(), true);
355+
356+
Ok(())
357+
}
326358
}

0 commit comments

Comments
 (0)