@@ -190,6 +190,7 @@ static const char *global_prefix;
190190
191191static enum sign_mode signed_tag_mode = SIGN_VERBATIM ;
192192static enum sign_mode signed_commit_mode = SIGN_VERBATIM ;
193+ static const char * signed_commit_keyid ;
193194
194195/* Memory pools */
195196static struct mem_pool fi_mem_pool = {
@@ -2836,10 +2837,11 @@ static void finalize_commit_buffer(struct strbuf *new_data,
28362837 strbuf_addbuf (new_data , msg );
28372838}
28382839
2839- static void handle_strip_if_invalid (struct strbuf * new_data ,
2840- struct signature_data * sig_sha1 ,
2841- struct signature_data * sig_sha256 ,
2842- struct strbuf * msg )
2840+ static void handle_signature_if_invalid (struct strbuf * new_data ,
2841+ struct signature_data * sig_sha1 ,
2842+ struct signature_data * sig_sha256 ,
2843+ struct strbuf * msg ,
2844+ enum sign_mode mode )
28432845{
28442846 struct strbuf tmp_buf = STRBUF_INIT ;
28452847 struct signature_check signature_check = { 0 };
@@ -2856,15 +2858,52 @@ static void handle_strip_if_invalid(struct strbuf *new_data,
28562858 const char * subject ;
28572859 int subject_len = find_commit_subject (msg -> buf , & subject );
28582860
2859- if (subject_len > 100 )
2860- warning (_ ("stripping invalid signature for commit '%.100s...'\n"
2861- " allegedly by %s" ), subject , signer );
2862- else if (subject_len > 0 )
2863- warning (_ ("stripping invalid signature for commit '%.*s'\n"
2864- " allegedly by %s" ), subject_len , subject , signer );
2865- else
2866- warning (_ ("stripping invalid signature for commit\n"
2867- " allegedly by %s" ), signer );
2861+ if (mode == SIGN_STRIP_IF_INVALID ) {
2862+ if (subject_len > 100 )
2863+ warning (_ ("stripping invalid signature for commit '%.100s...'\n"
2864+ " allegedly by %s" ), subject , signer );
2865+ else if (subject_len > 0 )
2866+ warning (_ ("stripping invalid signature for commit '%.*s'\n"
2867+ " allegedly by %s" ), subject_len , subject , signer );
2868+ else
2869+ warning (_ ("stripping invalid signature for commit\n"
2870+ " allegedly by %s" ), signer );
2871+ } else if (mode == SIGN_RESIGN_IF_INVALID ) {
2872+ struct strbuf signature = STRBUF_INIT ;
2873+ struct strbuf payload = STRBUF_INIT ;
2874+
2875+ if (subject_len > 100 )
2876+ warning (_ ("re-signing invalid signature for commit '%.100s...'\n"
2877+ " allegedly by %s" ), subject , signer );
2878+ else if (subject_len > 0 )
2879+ warning (_ ("re-signing invalid signature for commit '%.*s'\n"
2880+ " allegedly by %s" ), subject_len , subject , signer );
2881+ else
2882+ warning (_ ("re-signing invalid signature for commit\n"
2883+ " allegedly by %s" ), signer );
2884+
2885+ /*
2886+ * NEEDSWORK: To properly support interoperability mode
2887+ * when re-signing commit signatures, the commit buffer
2888+ * must be provided in both the repository and
2889+ * compatability object formats. As currently
2890+ * implemented, only the repository object format is
2891+ * considered meaning compatability signatures cannot be
2892+ * generated. Thus, attempting to re-sign commit
2893+ * signatures in interoperability mode is currently
2894+ * unsupported.
2895+ */
2896+ if (the_repository -> compat_hash_algo )
2897+ die (_ ("re-signing signatures in interoperability mode is unsupported" ));
2898+
2899+ strbuf_addstr (& payload , signature_check .payload );
2900+ if (sign_buffer_with_key (& payload , & signature , signed_commit_keyid ))
2901+ die (_ ("failed to sign commit object" ));
2902+ add_header_signature (new_data , & signature , the_hash_algo );
2903+
2904+ strbuf_release (& signature );
2905+ strbuf_release (& payload );
2906+ }
28682907
28692908 finalize_commit_buffer (new_data , NULL , NULL , msg );
28702909 } else {
@@ -2927,6 +2966,7 @@ static void parse_new_commit(const char *arg)
29272966 /* fallthru */
29282967 case SIGN_VERBATIM :
29292968 case SIGN_STRIP_IF_INVALID :
2969+ case SIGN_RESIGN_IF_INVALID :
29302970 import_one_signature (& sig_sha1 , & sig_sha256 , v );
29312971 break ;
29322972
@@ -3011,9 +3051,11 @@ static void parse_new_commit(const char *arg)
30113051 "encoding %s\n" ,
30123052 encoding );
30133053
3014- if (signed_commit_mode == SIGN_STRIP_IF_INVALID &&
3054+ if ((signed_commit_mode == SIGN_STRIP_IF_INVALID ||
3055+ signed_commit_mode == SIGN_RESIGN_IF_INVALID ) &&
30153056 (sig_sha1 .hash_algo || sig_sha256 .hash_algo ))
3016- handle_strip_if_invalid (& new_data , & sig_sha1 , & sig_sha256 , & msg );
3057+ handle_signature_if_invalid (& new_data , & sig_sha1 , & sig_sha256 ,
3058+ & msg , signed_commit_mode );
30173059 else
30183060 finalize_commit_buffer (& new_data , & sig_sha1 , & sig_sha256 , & msg );
30193061
@@ -3060,6 +3102,9 @@ static void handle_tag_signature(struct strbuf *msg, const char *name)
30603102 case SIGN_STRIP_IF_INVALID :
30613103 die (_ ("'strip-if-invalid' is not a valid mode for "
30623104 "git fast-import with --signed-tags=<mode>" ));
3105+ case SIGN_RESIGN_IF_INVALID :
3106+ die (_ ("'re-sign-if-invalid' is not a valid mode for "
3107+ "git fast-import with --signed-tags=<mode>" ));
30633108 default :
30643109 BUG ("invalid signed_tag_mode value %d from tag '%s'" ,
30653110 signed_tag_mode , name );
@@ -3649,10 +3694,10 @@ static int parse_one_option(const char *option)
36493694 } else if (skip_prefix (option , "export-pack-edges=" , & option )) {
36503695 option_export_pack_edges (option );
36513696 } else if (skip_prefix (option , "signed-commits=" , & option )) {
3652- if (parse_sign_mode (option , & signed_commit_mode ))
3697+ if (parse_sign_mode (option , & signed_commit_mode , & signed_commit_keyid ))
36533698 usagef (_ ("unknown --signed-commits mode '%s'" ), option );
36543699 } else if (skip_prefix (option , "signed-tags=" , & option )) {
3655- if (parse_sign_mode (option , & signed_tag_mode ))
3700+ if (parse_sign_mode (option , & signed_tag_mode , NULL ))
36563701 usagef (_ ("unknown --signed-tags mode '%s'" ), option );
36573702 } else if (!strcmp (option , "quiet" )) {
36583703 show_stats = 0 ;
0 commit comments