@@ -47,6 +47,7 @@ static enum {
4747
4848static char * logfile , * force_author , * template_file ;
4949static char * edit_message , * use_message ;
50+ static char * author_name , * author_email , * author_date ;
5051static int all , edit_flag , also , interactive , only , amend , signoff ;
5152static int quiet , verbose , untracked_files , no_verify , allow_empty ;
5253/*
@@ -397,6 +398,47 @@ static int is_a_merge(const unsigned char *sha1)
397398
398399static const char sign_off_header [] = "Signed-off-by: " ;
399400
401+ static void determine_author_info (void )
402+ {
403+ char * name , * email , * date ;
404+
405+ name = getenv ("GIT_AUTHOR_NAME" );
406+ email = getenv ("GIT_AUTHOR_EMAIL" );
407+ date = getenv ("GIT_AUTHOR_DATE" );
408+
409+ if (use_message ) {
410+ const char * a , * lb , * rb , * eol ;
411+
412+ a = strstr (use_message_buffer , "\nauthor " );
413+ if (!a )
414+ die ("invalid commit: %s" , use_message );
415+
416+ lb = strstr (a + 8 , " <" );
417+ rb = strstr (a + 8 , "> " );
418+ eol = strchr (a + 8 , '\n' );
419+ if (!lb || !rb || !eol )
420+ die ("invalid commit: %s" , use_message );
421+
422+ name = xstrndup (a + 8 , lb - (a + 8 ));
423+ email = xstrndup (lb + 2 , rb - (lb + 2 ));
424+ date = xstrndup (rb + 2 , eol - (rb + 2 ));
425+ }
426+
427+ if (force_author ) {
428+ const char * lb = strstr (force_author , " <" );
429+ const char * rb = strchr (force_author , '>' );
430+
431+ if (!lb || !rb )
432+ die ("malformed --author parameter" );
433+ name = xstrndup (force_author , lb - force_author );
434+ email = xstrndup (lb + 2 , rb - (lb + 2 ));
435+ }
436+
437+ author_name = name ;
438+ author_email = email ;
439+ author_date = date ;
440+ }
441+
400442static int prepare_to_commit (const char * index_file , const char * prefix )
401443{
402444 struct stat statbuf ;
@@ -406,6 +448,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
406448 FILE * fp ;
407449 const char * hook_arg1 = NULL ;
408450 const char * hook_arg2 = NULL ;
451+ int ident_shown = 0 ;
409452
410453 if (!no_verify && run_hook (index_file , "pre-commit" , NULL ))
411454 return 0 ;
@@ -485,7 +528,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
485528
486529 strbuf_release (& sb );
487530
531+ determine_author_info ();
532+
533+ /* This checks if committer ident is explicitly given */
534+ git_committer_info (0 );
488535 if (use_editor ) {
536+ char * author_ident ;
537+ const char * committer_ident ;
538+
489539 if (in_merge )
490540 fprintf (fp ,
491541 "#\n"
@@ -508,6 +558,27 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
508558 if (only_include_assumed )
509559 fprintf (fp , "# %s\n" , only_include_assumed );
510560
561+ author_ident = xstrdup (fmt_name (author_name , author_email ));
562+ committer_ident = fmt_name (getenv ("GIT_COMMITTER_NAME" ),
563+ getenv ("GIT_COMMITTER_EMAIL" ));
564+ if (strcmp (author_ident , committer_ident ))
565+ fprintf (fp ,
566+ "%s"
567+ "# Author: %s\n" ,
568+ ident_shown ++ ? "" : "#\n" ,
569+ author_ident );
570+ free (author_ident );
571+
572+ if (!user_ident_explicitly_given )
573+ fprintf (fp ,
574+ "%s"
575+ "# Committer: %s\n" ,
576+ ident_shown ++ ? "" : "#\n" ,
577+ committer_ident );
578+
579+ if (ident_shown )
580+ fprintf (fp , "#\n" );
581+
511582 saved_color_setting = wt_status_use_color ;
512583 wt_status_use_color = 0 ;
513584 commitable = run_status (fp , index_file , prefix , 1 );
@@ -624,45 +695,6 @@ static int message_is_empty(struct strbuf *sb, int start)
624695 return 1 ;
625696}
626697
627- static void determine_author_info (struct strbuf * sb )
628- {
629- char * name , * email , * date ;
630-
631- name = getenv ("GIT_AUTHOR_NAME" );
632- email = getenv ("GIT_AUTHOR_EMAIL" );
633- date = getenv ("GIT_AUTHOR_DATE" );
634-
635- if (use_message ) {
636- const char * a , * lb , * rb , * eol ;
637-
638- a = strstr (use_message_buffer , "\nauthor " );
639- if (!a )
640- die ("invalid commit: %s" , use_message );
641-
642- lb = strstr (a + 8 , " <" );
643- rb = strstr (a + 8 , "> " );
644- eol = strchr (a + 8 , '\n' );
645- if (!lb || !rb || !eol )
646- die ("invalid commit: %s" , use_message );
647-
648- name = xstrndup (a + 8 , lb - (a + 8 ));
649- email = xstrndup (lb + 2 , rb - (lb + 2 ));
650- date = xstrndup (rb + 2 , eol - (rb + 2 ));
651- }
652-
653- if (force_author ) {
654- const char * lb = strstr (force_author , " <" );
655- const char * rb = strchr (force_author , '>' );
656-
657- if (!lb || !rb )
658- die ("malformed --author parameter" );
659- name = xstrndup (force_author , lb - force_author );
660- email = xstrndup (lb + 2 , rb - (lb + 2 ));
661- }
662-
663- strbuf_addf (sb , "author %s\n" , fmt_ident (name , email , date , IDENT_ERROR_ON_NO_NAME ));
664- }
665-
666698static int parse_and_validate_options (int argc , const char * argv [],
667699 const char * const usage [])
668700{
@@ -922,7 +954,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
922954 strbuf_addf (& sb , "parent %s\n" , sha1_to_hex (head_sha1 ));
923955 }
924956
925- determine_author_info (& sb );
957+ strbuf_addf (& sb , "author %s\n" ,
958+ fmt_ident (author_name , author_email , author_date , IDENT_ERROR_ON_NO_NAME ));
926959 strbuf_addf (& sb , "committer %s\n" , git_committer_info (IDENT_ERROR_ON_NO_NAME ));
927960 if (!is_encoding_utf8 (git_commit_encoding ))
928961 strbuf_addf (& sb , "encoding %s\n" , git_commit_encoding );
0 commit comments