Skip to content

Commit d82ff46

Browse files
committed
use gitconfig user.signingKey to sign commit
1 parent 1d84f61 commit d82ff46

File tree

19 files changed

+97
-125
lines changed

19 files changed

+97
-125
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

asyncgit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ keywords = ["git"]
1414
[dependencies]
1515
bitflags = "2"
1616
crossbeam-channel = "0.5"
17+
dirs = "5.0"
1718
easy-cast = "0.5"
1819
fuzzy-matcher = "0.3"
1920
git2 = "0.18"

asyncgit/src/sync/blame.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ mod tests {
175175
File::create(root.join(file_path))?.write_all(b"line 1\n")?;
176176

177177
stage_add_file(repo_path, file_path)?;
178-
commit(repo_path, "first commit", None)?;
178+
commit(repo_path, "first commit")?;
179179

180180
let blame = blame_file(repo_path, "foo", None)?;
181181

@@ -199,7 +199,7 @@ mod tests {
199199
file.write(b"line 2\n")?;
200200

201201
stage_add_file(repo_path, file_path)?;
202-
commit(repo_path, "second commit", None)?;
202+
commit(repo_path, "second commit")?;
203203

204204
let blame = blame_file(repo_path, "foo", None)?;
205205

@@ -233,7 +233,7 @@ mod tests {
233233
assert_eq!(blame.lines.len(), 2);
234234

235235
stage_add_file(repo_path, file_path)?;
236-
commit(repo_path, "third commit", None)?;
236+
commit(repo_path, "third commit")?;
237237

238238
let blame = blame_file(repo_path, "foo", None)?;
239239

@@ -258,7 +258,7 @@ mod tests {
258258
.unwrap();
259259

260260
stage_add_file(repo_path, file_path).unwrap();
261-
commit(repo_path, "first commit", None).unwrap();
261+
commit(repo_path, "first commit").unwrap();
262262

263263
assert!(blame_file(repo_path, "bar\\foo", None).is_ok());
264264
}

asyncgit/src/sync/commit.rs

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::{
66
use git2::{ErrorCode, ObjectType, Repository, Signature};
77
use scopetime::scope_time;
88
use ssh_key::{HashAlg, LineEnding, PrivateKey};
9+
use std::path::PathBuf;
910

1011
///
1112
pub fn amend(
@@ -62,15 +63,42 @@ pub(crate) fn signature_allow_undefined_name(
6263
}
6364

6465
/// this does not run any git hooks, git-hooks have to be executed manually, checkout `hooks_commit_msg` for example
65-
/// If ssh private key is provided, the commit will be signed.
66-
pub fn commit(
67-
repo_path: &RepoPath,
68-
msg: &str,
69-
sk: Option<&PrivateKey>,
70-
) -> Result<CommitId> {
66+
pub fn commit(repo_path: &RepoPath, msg: &str) -> Result<CommitId> {
7167
scope_time!("commit");
7268

7369
let repo = repo(repo_path)?;
70+
let ssh_secret_key = {
71+
let config = repo.config()?;
72+
config
73+
.get_entry("user.signingKey")
74+
.ok()
75+
.and_then(|entry| {
76+
entry.value().map(|key_path| {
77+
key_path.strip_prefix('~').map_or_else(
78+
|| Some(PathBuf::from(&key_path)),
79+
|ssh_key_path| {
80+
dirs::home_dir().map(|home| {
81+
home.join(
82+
ssh_key_path
83+
.strip_prefix('/')
84+
.unwrap_or(ssh_key_path),
85+
)
86+
})
87+
},
88+
)
89+
})
90+
})
91+
.and_then(|key_file| {
92+
key_file
93+
.and_then(|mut p| {
94+
p.set_extension("");
95+
std::fs::read(p).ok()
96+
})
97+
.and_then(|bytes| {
98+
PrivateKey::from_openssh(bytes).ok()
99+
})
100+
})
101+
};
74102

75103
let signature = signature_allow_undefined_name(&repo)?;
76104
let mut index = repo.index()?;
@@ -84,7 +112,7 @@ pub fn commit(
84112
};
85113

86114
let parents = parents.iter().collect::<Vec<_>>();
87-
if let Some(sk) = sk {
115+
if let Some(sk) = ssh_secret_key {
88116
let buffer = repo.commit_create_buffer(
89117
&signature,
90118
&signature,
@@ -197,7 +225,7 @@ mod tests {
197225

198226
assert_eq!(get_statuses(repo_path), (0, 1));
199227

200-
commit(repo_path, "commit msg", None).unwrap();
228+
commit(repo_path, "commit msg").unwrap();
201229

202230
assert_eq!(get_statuses(repo_path), (0, 0));
203231
}
@@ -223,7 +251,7 @@ mod tests {
223251

224252
assert_eq!(get_statuses(repo_path), (0, 1));
225253

226-
commit(repo_path, "commit msg", None).unwrap();
254+
commit(repo_path, "commit msg").unwrap();
227255

228256
assert_eq!(get_statuses(repo_path), (0, 0));
229257
}
@@ -240,7 +268,7 @@ mod tests {
240268
File::create(root.join(file_path1))?.write_all(b"test1")?;
241269

242270
stage_add_file(repo_path, file_path1)?;
243-
let id = commit(repo_path, "commit msg", None)?;
271+
let id = commit(repo_path, "commit msg")?;
244272

245273
assert_eq!(count_commits(&repo, 10), 1);
246274

@@ -279,7 +307,7 @@ mod tests {
279307

280308
stage_add_file(repo_path, file_path)?;
281309

282-
let new_id = commit(repo_path, "commit msg", None)?;
310+
let new_id = commit(repo_path, "commit msg")?;
283311

284312
tag_commit(repo_path, &new_id, "tag", None)?;
285313

@@ -321,7 +349,7 @@ mod tests {
321349

322350
stage_add_file(repo_path, file_path)?;
323351

324-
let new_id = commit(repo_path, "commit msg", None)?;
352+
let new_id = commit(repo_path, "commit msg")?;
325353

326354
tag_commit(repo_path, &new_id, "tag", Some("tag-message"))?;
327355

@@ -357,13 +385,13 @@ mod tests {
357385

358386
repo.config()?.remove("user.email")?;
359387

360-
let error = commit(repo_path, "commit msg", None);
388+
let error = commit(repo_path, "commit msg");
361389

362390
assert!(matches!(error, Err(_)));
363391

364392
repo.config()?.set_str("user.email", "email")?;
365393

366-
let success = commit(repo_path, "commit msg", None);
394+
let success = commit(repo_path, "commit msg");
367395

368396
assert!(matches!(success, Ok(_)));
369397
assert_eq!(count_commits(&repo, 10), 1);
@@ -393,7 +421,7 @@ mod tests {
393421

394422
repo.config()?.remove("user.name")?;
395423

396-
let mut success = commit(repo_path, "commit msg", None);
424+
let mut success = commit(repo_path, "commit msg");
397425

398426
assert!(matches!(success, Ok(_)));
399427
assert_eq!(count_commits(&repo, 10), 1);
@@ -406,7 +434,7 @@ mod tests {
406434

407435
repo.config()?.set_str("user.name", "name")?;
408436

409-
success = commit(repo_path, "commit msg", None);
437+
success = commit(repo_path, "commit msg");
410438

411439
assert!(matches!(success, Ok(_)));
412440
assert_eq!(count_commits(&repo, 10), 2);

asyncgit/src/sync/commit_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod tests {
144144
stage_add_file(repo_path, file_path).unwrap();
145145

146146
let msg = invalidstring::invalid_utf8("test msg");
147-
let id = commit(repo_path, msg.as_str(), None).unwrap();
147+
let id = commit(repo_path, msg.as_str()).unwrap();
148148

149149
let res = get_commit_details(repo_path, id).unwrap();
150150

asyncgit/src/sync/commit_files.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ mod tests {
209209

210210
stage_add_file(repo_path, file_path)?;
211211

212-
let id = commit(repo_path, "commit msg", None)?;
212+
let id = commit(repo_path, "commit msg")?;
213213

214214
let diff = get_commit_files(repo_path, id, None)?;
215215

@@ -251,7 +251,7 @@ mod tests {
251251

252252
File::create(root.join(file_path1))?.write_all(b"test")?;
253253
stage_add_file(repo_path, file_path1)?;
254-
commit(repo_path, "c1", None)?;
254+
commit(repo_path, "c1")?;
255255

256256
File::create(root.join(file_path1))?
257257
.write_all(b"modified")?;

asyncgit/src/sync/commit_revert.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::{
44
sync::{repository::repo, utils::read_file},
55
};
66
use scopetime::scope_time;
7-
use ssh_key::PrivateKey;
87

98
const GIT_REVERT_HEAD_FILE: &str = "REVERT_HEAD";
109

@@ -41,11 +40,10 @@ pub fn revert_head(repo_path: &RepoPath) -> Result<CommitId> {
4140
pub fn commit_revert(
4241
repo_path: &RepoPath,
4342
msg: &str,
44-
sk: Option<&PrivateKey>,
4543
) -> Result<CommitId> {
4644
scope_time!("commit_revert");
4745

48-
let id = crate::sync::commit(repo_path, msg, sk)?;
46+
let id = crate::sync::commit(repo_path, msg)?;
4947

5048
repo(repo_path)?.cleanup_state()?;
5149

asyncgit/src/sync/commits_info.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ mod tests {
172172

173173
File::create(root.join(file_path))?.write_all(b"a")?;
174174
stage_add_file(repo_path, file_path).unwrap();
175-
let c1 = commit(repo_path, "commit1", None).unwrap();
175+
let c1 = commit(repo_path, "commit1").unwrap();
176176
File::create(root.join(file_path))?.write_all(b"a")?;
177177
stage_add_file(repo_path, file_path).unwrap();
178-
let c2 = commit(repo_path, "commit2", None).unwrap();
178+
let c2 = commit(repo_path, "commit2").unwrap();
179179

180180
let res = get_commits_info(repo_path, &[c2, c1], 50).unwrap();
181181

@@ -197,7 +197,7 @@ mod tests {
197197

198198
File::create(root.join(file_path))?.write_all(b"a")?;
199199
stage_add_file(repo_path, file_path).unwrap();
200-
let c1 = commit(repo_path, "subject\nbody", None).unwrap();
200+
let c1 = commit(repo_path, "subject\nbody").unwrap();
201201

202202
let res = get_commits_info(repo_path, &[c1], 50).unwrap();
203203

@@ -219,7 +219,7 @@ mod tests {
219219
stage_add_file(repo_path, file_path).unwrap();
220220

221221
let msg = invalidstring::invalid_utf8("test msg");
222-
commit(repo_path, msg.as_str(), None).unwrap();
222+
commit(repo_path, msg.as_str()).unwrap();
223223

224224
let res = get_commits_info(
225225
repo_path,
@@ -245,8 +245,7 @@ mod tests {
245245
let foo_file = Path::new("foo");
246246
File::create(root.join(foo_file))?.write_all(b"a")?;
247247
stage_add_file(repo_path, foo_file).unwrap();
248-
let c1 =
249-
commit(repo_path, "subject: foo\nbody", None).unwrap();
248+
let c1 = commit(repo_path, "subject: foo\nbody").unwrap();
250249
let c1_rev = c1.get_short_string();
251250

252251
assert_eq!(

asyncgit/src/sync/diff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ mod tests {
597597

598598
stage_add_file(repo_path, file_path).unwrap();
599599

600-
commit(repo_path, "commit", None).unwrap();
600+
commit(repo_path, "commit").unwrap();
601601

602602
File::create(root.join(file_path))?.write_all(b"\x00\x02")?;
603603

@@ -653,13 +653,13 @@ mod tests {
653653

654654
stage_add_file(repo_path, file_path).unwrap();
655655

656-
commit(repo_path, "", None).unwrap();
656+
commit(repo_path, "").unwrap();
657657

658658
File::create(root.join(file_path))?.write_all(b"\x00\x02")?;
659659

660660
stage_add_file(repo_path, file_path).unwrap();
661661

662-
let id = commit(repo_path, "", None).unwrap();
662+
let id = commit(repo_path, "").unwrap();
663663

664664
let diff =
665665
get_diff_commit(repo_path, id, String::new(), None)

asyncgit/src/sync/logwalker.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ mod tests {
135135

136136
File::create(root.join(file_path))?.write_all(b"a")?;
137137
stage_add_file(repo_path, file_path).unwrap();
138-
commit(repo_path, "commit1", None).unwrap();
138+
commit(repo_path, "commit1").unwrap();
139139
File::create(root.join(file_path))?.write_all(b"a")?;
140140
stage_add_file(repo_path, file_path).unwrap();
141-
let oid2 = commit(repo_path, "commit2", None).unwrap();
141+
let oid2 = commit(repo_path, "commit2").unwrap();
142142

143143
let mut items = Vec::new();
144144
let mut walk = LogWalker::new(&repo, 1)?;
@@ -160,10 +160,10 @@ mod tests {
160160

161161
File::create(root.join(file_path))?.write_all(b"a")?;
162162
stage_add_file(repo_path, file_path).unwrap();
163-
commit(repo_path, "commit1", None).unwrap();
163+
commit(repo_path, "commit1").unwrap();
164164
File::create(root.join(file_path))?.write_all(b"a")?;
165165
stage_add_file(repo_path, file_path).unwrap();
166-
let oid2 = commit(repo_path, "commit2", None).unwrap();
166+
let oid2 = commit(repo_path, "commit2").unwrap();
167167

168168
let mut items = Vec::new();
169169
let mut walk = LogWalker::new(&repo, 100)?;
@@ -195,20 +195,17 @@ mod tests {
195195
File::create(root.join(file_path))?.write_all(b"a")?;
196196
stage_add_file(&repo_path, file_path).unwrap();
197197

198-
let _first_commit_id =
199-
commit(&repo_path, "commit1", None).unwrap();
198+
let _first_commit_id = commit(&repo_path, "commit1").unwrap();
200199

201200
File::create(root.join(second_file_path))?.write_all(b"a")?;
202201
stage_add_file(&repo_path, second_file_path).unwrap();
203202

204-
let second_commit_id =
205-
commit(&repo_path, "commit2", None).unwrap();
203+
let second_commit_id = commit(&repo_path, "commit2").unwrap();
206204

207205
File::create(root.join(file_path))?.write_all(b"b")?;
208206
stage_add_file(&repo_path, file_path).unwrap();
209207

210-
let _third_commit_id =
211-
commit(&repo_path, "commit3", None).unwrap();
208+
let _third_commit_id = commit(&repo_path, "commit3").unwrap();
212209

213210
let diff_contains_baz = diff_contains_file("baz".into());
214211

0 commit comments

Comments
 (0)