1
1
use super :: { CommitId , RepoPath } ;
2
+ use crate :: sync:: sign:: { SignBuilder , SignError } ;
2
3
use crate :: {
3
4
error:: Result ,
4
5
sync:: { repository:: repo, utils:: get_head_repo} ,
@@ -65,7 +66,7 @@ pub fn commit(repo_path: &RepoPath, msg: &str) -> Result<CommitId> {
65
66
scope_time ! ( "commit" ) ;
66
67
67
68
let repo = repo ( repo_path) ?;
68
-
69
+ let config = repo . config ( ) ? ;
69
70
let signature = signature_allow_undefined_name ( & repo) ?;
70
71
let mut index = repo. index ( ) ?;
71
72
let tree_id = index. write_tree ( ) ?;
@@ -79,16 +80,46 @@ pub fn commit(repo_path: &RepoPath, msg: &str) -> Result<CommitId> {
79
80
80
81
let parents = parents. iter ( ) . collect :: < Vec < _ > > ( ) ;
81
82
82
- Ok ( repo
83
- . commit (
83
+ let commit_id = if config
84
+ . get_bool ( "commit.gpgsign" )
85
+ . unwrap_or ( false )
86
+ {
87
+ use crate :: sync:: sign:: Sign ;
88
+
89
+ let buffer = repo. commit_create_buffer (
90
+ & signature,
91
+ & signature,
92
+ msg,
93
+ & tree,
94
+ parents. as_slice ( ) ,
95
+ ) ?;
96
+
97
+ let commit = std:: str:: from_utf8 ( & buffer) . map_err ( |_e| {
98
+ SignError :: Shellout ( "utf8 conversion error" . to_string ( ) )
99
+ } ) ?;
100
+
101
+ let sign = SignBuilder :: from_gitconfig ( & repo, & config) ?;
102
+ let signed_commit = sign. sign ( commit) ?;
103
+ let commit_id =
104
+ repo. commit_signed ( commit, & signed_commit, None ) ?;
105
+
106
+ // manually advance to the new commit ID
107
+ // repo.commit does that on its own, repo.commit_signed does not
108
+ repo. head ( ) ?. set_target ( commit_id, msg) ?;
109
+
110
+ commit_id
111
+ } else {
112
+ repo. commit (
84
113
Some ( "HEAD" ) ,
85
114
& signature,
86
115
& signature,
87
116
msg,
88
117
& tree,
89
118
parents. as_slice ( ) ,
90
119
) ?
91
- . into ( ) )
120
+ } ;
121
+
122
+ Ok ( commit_id. into ( ) )
92
123
}
93
124
94
125
/// Tag a commit.
0 commit comments