Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db361a3

Browse files
author
Stephan Dilly
committed
move commit into its own mod
1 parent 37b0644 commit db361a3

File tree

4 files changed

+109
-104
lines changed

4 files changed

+109
-104
lines changed

‎asyncgit/src/sync/commit.rs‎

Lines changed: 103 additions & 2 deletions
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‎

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;
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‎

Lines changed: 2 additions & 1 deletion
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‎

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -66,56 +66,6 @@ pub fn get_head_repo(repo: &Repository) -> Result<CommitId> {
6666
}
6767
}
6868

69-
/// Wrap Repository::signature to allow unknown user.name.
70-
///
71-
/// See <https://github.com/extrawurst/gitui/issues/79>.
72-
pub fn signature_allow_undefined_name(
73-
repo: &Repository,
74-
) -> std::result::Result<git2::Signature<'static>, git2::Error> {
75-
match repo.signature() {
76-
Err(e) if e.code() == git2::ErrorCode::NotFound => {
77-
let config = repo.config()?;
78-
git2::Signature::now(
79-
config.get_str("user.name").unwrap_or("unknown"),
80-
config.get_str("user.email")?,
81-
)
82-
}
83-
84-
v => v,
85-
}
86-
}
87-
88-
/// this does not run any git hooks
89-
pub fn commit(repo_path: &str, msg: &str) -> Result<CommitId> {
90-
scope_time!("commit");
91-
92-
let repo = repo(repo_path)?;
93-
94-
let signature = signature_allow_undefined_name(&repo)?;
95-
let mut index = repo.index()?;
96-
let tree_id = index.write_tree()?;
97-
let tree = repo.find_tree(tree_id)?;
98-
99-
let parents = if let Ok(id) = get_head(repo_path) {
100-
vec![repo.find_commit(id.into())?]
101-
} else {
102-
Vec::new()
103-
};
104-
105-
let parents = parents.iter().collect::<Vec<_>>();
106-
107-
Ok(repo
108-
.commit(
109-
Some("HEAD"),
110-
&signature,
111-
&signature,
112-
msg,
113-
&tree,
114-
parents.as_slice(),
115-
)?
116-
.into())
117-
}
118-
11969
/// add a file diff from workingdir to stage (will not add removed files see `stage_addremoved`)
12070
pub fn stage_add_file(repo_path: &str, path: &Path) -> Result<()> {
12171
scope_time!("stage_add_file");
@@ -162,6 +112,7 @@ pub fn stage_addremoved(repo_path: &str, path: &Path) -> Result<()> {
162112
mod tests {
163113
use super::*;
164114
use crate::sync::{
115+
commit,
165116
status::{get_status, StatusType},
166117
tests::{
167118
debug_cmd_print, get_statuses, repo_init, repo_init_empty,
@@ -173,54 +124,6 @@ mod tests {
173124
path::Path,
174125
};
175126

176-
#[test]
177-
fn test_commit() {
178-
let file_path = Path::new("foo");
179-
let (_td, repo) = repo_init().unwrap();
180-
let root = repo.path().parent().unwrap();
181-
let repo_path = root.as_os_str().to_str().unwrap();
182-
183-
File::create(&root.join(file_path))
184-
.unwrap()
185-
.write_all(b"test\nfoo")
186-
.unwrap();
187-
188-
assert_eq!(get_statuses(repo_path), (1, 0));
189-
190-
stage_add_file(repo_path, file_path).unwrap();
191-
192-
assert_eq!(get_statuses(repo_path), (0, 1));
193-
194-
commit(repo_path, "commit msg").unwrap();
195-
196-
assert_eq!(get_statuses(repo_path), (0, 0));
197-
}
198-
199-
#[test]
200-
fn test_commit_in_empty_repo() {
201-
let file_path = Path::new("foo");
202-
let (_td, repo) = repo_init_empty().unwrap();
203-
let root = repo.path().parent().unwrap();
204-
let repo_path = root.as_os_str().to_str().unwrap();
205-
206-
assert_eq!(get_statuses(repo_path), (0, 0));
207-
208-
File::create(&root.join(file_path))
209-
.unwrap()
210-
.write_all(b"test\nfoo")
211-
.unwrap();
212-
213-
assert_eq!(get_statuses(repo_path), (1, 0));
214-
215-
stage_add_file(repo_path, file_path).unwrap();
216-
217-
assert_eq!(get_statuses(repo_path), (0, 1));
218-
219-
commit(repo_path, "commit msg").unwrap();
220-
221-
assert_eq!(get_statuses(repo_path), (0, 0));
222-
}
223-
224127
#[test]
225128
fn test_stage_add_smoke() {
226129
let file_path = Path::new("foo");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /