Skip to content

Commit 80f98d6

Browse files
author
Stephan Dilly
committed
move commit into its own mod
# Conflicts: # asyncgit/src/sync/utils.rs
1 parent c856c23 commit 80f98d6

File tree

4 files changed

+109
-85
lines changed

4 files changed

+109
-85
lines changed

asyncgit/src/sync/commit.rs

+103-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use super::{utils::repo, CommitId};
1+
use super::{get_head, utils::repo, CommitId};
22
use crate::error::Result;
3+
use git2::{ErrorCode, Repository, Signature};
34
use scopetime::scope_time;
45

56
///
@@ -29,13 +30,65 @@ pub fn amend(
2930
Ok(CommitId::new(new_id))
3031
}
3132

33+
/// Wrap Repository::signature to allow unknown user.name.
34+
///
35+
/// See <https://github.com/extrawurst/gitui/issues/79>.
36+
fn signature_allow_undefined_name(
37+
repo: &Repository,
38+
) -> std::result::Result<Signature<'_>, git2::Error> {
39+
match repo.signature() {
40+
Err(e) if e.code() == ErrorCode::NotFound => {
41+
let config = repo.config()?;
42+
Signature::now(
43+
config.get_str("user.name").unwrap_or("unknown"),
44+
config.get_str("user.email")?,
45+
)
46+
}
47+
48+
v => v,
49+
}
50+
}
51+
52+
/// this does not run any git hooks
53+
pub fn commit(repo_path: &str, msg: &str) -> Result<CommitId> {
54+
scope_time!("commit");
55+
56+
let repo = repo(repo_path)?;
57+
58+
let signature = signature_allow_undefined_name(&repo)?;
59+
let mut index = repo.index()?;
60+
let tree_id = index.write_tree()?;
61+
let tree = repo.find_tree(tree_id)?;
62+
63+
let parents = if let Ok(id) = get_head(repo_path) {
64+
vec![repo.find_commit(id.into())?]
65+
} else {
66+
Vec::new()
67+
};
68+
69+
let parents = parents.iter().collect::<Vec<_>>();
70+
71+
Ok(repo
72+
.commit(
73+
Some("HEAD"),
74+
&signature,
75+
&signature,
76+
msg,
77+
&tree,
78+
parents.as_slice(),
79+
)?
80+
.into())
81+
}
82+
3283
#[cfg(test)]
3384
mod tests {
3485

3586
use crate::error::Result;
3687
use crate::sync::{
3788
commit, get_commit_details, get_commit_files, stage_add_file,
38-
tests::repo_init_empty, utils::get_head, LogWalker,
89+
tests::{get_statuses, repo_init, repo_init_empty},
90+
utils::get_head,
91+
LogWalker,
3992
};
4093
use commit::amend;
4194
use git2::Repository;
@@ -48,6 +101,54 @@ mod tests {
48101
items.len()
49102
}
50103

104+
#[test]
105+
fn test_commit() {
106+
let file_path = Path::new("foo");
107+
let (_td, repo) = repo_init().unwrap();
108+
let root = repo.path().parent().unwrap();
109+
let repo_path = root.as_os_str().to_str().unwrap();
110+
111+
File::create(&root.join(file_path))
112+
.unwrap()
113+
.write_all(b"test\nfoo")
114+
.unwrap();
115+
116+
assert_eq!(get_statuses(repo_path), (1, 0));
117+
118+
stage_add_file(repo_path, file_path).unwrap();
119+
120+
assert_eq!(get_statuses(repo_path), (0, 1));
121+
122+
commit(repo_path, "commit msg").unwrap();
123+
124+
assert_eq!(get_statuses(repo_path), (0, 0));
125+
}
126+
127+
#[test]
128+
fn test_commit_in_empty_repo() {
129+
let file_path = Path::new("foo");
130+
let (_td, repo) = repo_init_empty().unwrap();
131+
let root = repo.path().parent().unwrap();
132+
let repo_path = root.as_os_str().to_str().unwrap();
133+
134+
assert_eq!(get_statuses(repo_path), (0, 0));
135+
136+
File::create(&root.join(file_path))
137+
.unwrap()
138+
.write_all(b"test\nfoo")
139+
.unwrap();
140+
141+
assert_eq!(get_statuses(repo_path), (1, 0));
142+
143+
stage_add_file(repo_path, file_path).unwrap();
144+
145+
assert_eq!(get_statuses(repo_path), (0, 1));
146+
147+
commit(repo_path, "commit msg").unwrap();
148+
149+
assert_eq!(get_statuses(repo_path), (0, 0));
150+
}
151+
51152
#[test]
52153
fn test_amend() -> Result<()> {
53154
let file_path1 = Path::new("foo");

asyncgit/src/sync/mod.rs

+3-3
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;
20+
pub use commit::{amend, commit};
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, get_head, is_bare_repo, is_repo, stage_add_all,
34-
stage_add_file, stage_addremoved,
33+
get_head, is_bare_repo, is_repo, stage_add_all, stage_add_file,
34+
stage_addremoved,
3535
};
3636

3737
#[cfg(test)]

asyncgit/src/sync/reset.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ mod tests {
4343
use super::{reset_stage, reset_workdir};
4444
use crate::error::Result;
4545
use crate::sync::{
46+
commit,
4647
status::{get_status, StatusType},
4748
tests::{
4849
debug_cmd_print, get_statuses, repo_init, repo_init_empty,
4950
},
50-
utils::{commit, stage_add_all, stage_add_file},
51+
utils::{stage_add_all, stage_add_file},
5152
};
5253
use std::{
5354
fs::{self, File},

asyncgit/src/sync/utils.rs

+1-79
Original file line numberDiff line numberDiff line change
@@ -66,37 +66,6 @@ pub fn get_head_repo(repo: &Repository) -> Result<CommitId> {
6666
}
6767
}
6868

69-
/// this does not run any git hooks
70-
pub fn commit(repo_path: &str, msg: &str) -> Result<CommitId> {
71-
scope_time!("commit");
72-
73-
let repo = repo(repo_path)?;
74-
75-
let signature = repo.signature()?;
76-
let mut index = repo.index()?;
77-
let tree_id = index.write_tree()?;
78-
let tree = repo.find_tree(tree_id)?;
79-
80-
let parents = if let Ok(id) = get_head(repo_path) {
81-
vec![repo.find_commit(id.into())?]
82-
} else {
83-
Vec::new()
84-
};
85-
86-
let parents = parents.iter().collect::<Vec<_>>();
87-
88-
Ok(repo
89-
.commit(
90-
Some("HEAD"),
91-
&signature,
92-
&signature,
93-
msg,
94-
&tree,
95-
parents.as_slice(),
96-
)?
97-
.into())
98-
}
99-
10069
/// add a file diff from workingdir to stage (will not add removed files see `stage_addremoved`)
10170
pub fn stage_add_file(repo_path: &str, path: &Path) -> Result<()> {
10271
scope_time!("stage_add_file");
@@ -143,6 +112,7 @@ pub fn stage_addremoved(repo_path: &str, path: &Path) -> Result<()> {
143112
mod tests {
144113
use super::*;
145114
use crate::sync::{
115+
commit,
146116
status::{get_status, StatusType},
147117
tests::{
148118
debug_cmd_print, get_statuses, repo_init, repo_init_empty,
@@ -154,54 +124,6 @@ mod tests {
154124
path::Path,
155125
};
156126

157-
#[test]
158-
fn test_commit() {
159-
let file_path = Path::new("foo");
160-
let (_td, repo) = repo_init().unwrap();
161-
let root = repo.path().parent().unwrap();
162-
let repo_path = root.as_os_str().to_str().unwrap();
163-
164-
File::create(&root.join(file_path))
165-
.unwrap()
166-
.write_all(b"test\nfoo")
167-
.unwrap();
168-
169-
assert_eq!(get_statuses(repo_path), (1, 0));
170-
171-
stage_add_file(repo_path, file_path).unwrap();
172-
173-
assert_eq!(get_statuses(repo_path), (0, 1));
174-
175-
commit(repo_path, "commit msg").unwrap();
176-
177-
assert_eq!(get_statuses(repo_path), (0, 0));
178-
}
179-
180-
#[test]
181-
fn test_commit_in_empty_repo() {
182-
let file_path = Path::new("foo");
183-
let (_td, repo) = repo_init_empty().unwrap();
184-
let root = repo.path().parent().unwrap();
185-
let repo_path = root.as_os_str().to_str().unwrap();
186-
187-
assert_eq!(get_statuses(repo_path), (0, 0));
188-
189-
File::create(&root.join(file_path))
190-
.unwrap()
191-
.write_all(b"test\nfoo")
192-
.unwrap();
193-
194-
assert_eq!(get_statuses(repo_path), (1, 0));
195-
196-
stage_add_file(repo_path, file_path).unwrap();
197-
198-
assert_eq!(get_statuses(repo_path), (0, 1));
199-
200-
commit(repo_path, "commit msg").unwrap();
201-
202-
assert_eq!(get_statuses(repo_path), (0, 0));
203-
}
204-
205127
#[test]
206128
fn test_stage_add_smoke() {
207129
let file_path = Path::new("foo");

0 commit comments

Comments
 (0)