Skip to content

Commit c618c07

Browse files
committed
Use get_entry instead of get_str
1 parent 17de5a9 commit c618c07

File tree

2 files changed

+107
-8
lines changed

2 files changed

+107
-8
lines changed

asyncgit/src/sync/commit.rs

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ pub fn amend(
3636
fn signature_allow_undefined_name(
3737
repo: &Repository,
3838
) -> std::result::Result<Signature<'_>, git2::Error> {
39-
match repo.signature() {
40-
Err(e) if e.code() == ErrorCode::NotFound => {
39+
let signature = repo.signature();
40+
41+
if let Err(ref e) = signature {
42+
if e.code() == ErrorCode::NotFound {
4143
let config = repo.config()?;
42-
Signature::now(
43-
config.get_str("user.name").unwrap_or("unknown"),
44-
config.get_str("user.email")?,
45-
)
46-
}
4744

48-
v => v,
45+
if let (Err(_), Ok(email_entry)) = (
46+
config.get_entry("user.name"),
47+
config.get_entry("user.email"),
48+
) {
49+
if let Some(email) = email_entry.value() {
50+
return Signature::now("unknown", email);
51+
}
52+
};
53+
}
4954
}
55+
56+
signature
5057
}
5158

5259
/// this does not run any git hooks
@@ -245,4 +252,91 @@ mod tests {
245252

246253
Ok(())
247254
}
255+
256+
/// Beware: this test has to be run with a `$HOME/.gitconfig` that has
257+
/// `user.email` not set. Otherwise, `git` falls back to the value of
258+
/// `user.email` in `$HOME/.gitconfig` and this test fails.
259+
///
260+
/// As of September 2020, `repo_init_empty` sets `$HOME` to an empty
261+
/// temporary directory, so this constraint is met.
262+
#[test]
263+
fn test_empty_email() -> Result<()> {
264+
let file_path = Path::new("foo");
265+
let (_td, repo) = repo_init_empty().unwrap();
266+
let root = repo.path().parent().unwrap();
267+
let repo_path = root.as_os_str().to_str().unwrap();
268+
269+
File::create(&root.join(file_path))?
270+
.write_all(b"test\nfoo")?;
271+
272+
stage_add_file(repo_path, file_path)?;
273+
274+
repo.config()?.remove("user.email")?;
275+
276+
let error = commit(repo_path, "commit msg");
277+
278+
assert!(matches!(error, Err(_)));
279+
280+
repo.config()?.set_str("user.email", "email")?;
281+
282+
let success = commit(repo_path, "commit msg");
283+
284+
assert!(matches!(success, Ok(_)));
285+
assert_eq!(count_commits(&repo, 10), 1);
286+
287+
let details =
288+
get_commit_details(repo_path, success.unwrap()).unwrap();
289+
290+
assert_eq!(details.author.name, "name");
291+
assert_eq!(details.author.email, "email");
292+
293+
Ok(())
294+
}
295+
296+
/// Beware: this test has to be run with a `$HOME/.gitconfig` that has
297+
/// `user.name` not set. Otherwise, `git` falls back to the value of
298+
/// `user.name` in `$HOME/.gitconfig` and this test fails.
299+
///
300+
/// As of September 2020, `repo_init_empty` sets `$HOME` to an empty
301+
/// temporary directory, so this constraint is met.
302+
#[test]
303+
fn test_empty_name() -> Result<()> {
304+
let file_path = Path::new("foo");
305+
let (_td, repo) = repo_init_empty().unwrap();
306+
let root = repo.path().parent().unwrap();
307+
let repo_path = root.as_os_str().to_str().unwrap();
308+
309+
File::create(&root.join(file_path))?
310+
.write_all(b"test\nfoo")?;
311+
312+
stage_add_file(repo_path, file_path)?;
313+
314+
repo.config()?.remove("user.name")?;
315+
316+
let mut success = commit(repo_path, "commit msg");
317+
318+
assert!(matches!(success, Ok(_)));
319+
assert_eq!(count_commits(&repo, 10), 1);
320+
321+
let mut details =
322+
get_commit_details(repo_path, success.unwrap()).unwrap();
323+
324+
assert_eq!(details.author.name, "unknown");
325+
assert_eq!(details.author.email, "email");
326+
327+
repo.config()?.set_str("user.name", "name")?;
328+
329+
success = commit(repo_path, "commit msg");
330+
331+
assert!(matches!(success, Ok(_)));
332+
assert_eq!(count_commits(&repo, 10), 2);
333+
334+
details =
335+
get_commit_details(repo_path, success.unwrap()).unwrap();
336+
337+
assert_eq!(details.author.name, "name");
338+
assert_eq!(details.author.email, "email");
339+
340+
Ok(())
341+
}
248342
}

asyncgit/src/sync/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ mod tests {
4949

5050
///
5151
pub fn repo_init_empty() -> Result<(TempDir, Repository)> {
52+
// Setting `$HOME` to an empty directory makes sure that there is no
53+
// user-local `.gitconfig` or `.git/config` interfering with out tests.
54+
let temp_home = TempDir::new()?;
55+
std::env::set_var("HOME", temp_home.path());
56+
5257
let td = TempDir::new()?;
5358
let repo = Repository::init(td.path())?;
5459
{

0 commit comments

Comments
 (0)