@@ -47,8 +47,21 @@ static char *logfile, *force_author, *template_file;
4747static char * edit_message , * use_message ;
4848static int all , edit_flag , also , interactive , only , amend , signoff ;
4949static int quiet , verbose , untracked_files , no_verify , allow_empty ;
50+ /*
51+ * The default commit message cleanup mode will remove the lines
52+ * beginning with # (shell comments) and leading and trailing
53+ * whitespaces (empty lines or containing only whitespaces)
54+ * if editor is used, and only the whitespaces if the message
55+ * is specified explicitly.
56+ */
57+ static enum {
58+ CLEANUP_SPACE ,
59+ CLEANUP_NONE ,
60+ CLEANUP_ALL ,
61+ } cleanup_mode ;
62+ static char * cleanup_arg ;
5063
51- static int no_edit , initial_commit , in_merge ;
64+ static int use_editor = 1 , initial_commit , in_merge ;
5265const char * only_include_assumed ;
5366struct strbuf message ;
5467
@@ -88,6 +101,7 @@ static struct option builtin_commit_options[] = {
88101 OPT_BOOLEAN (0 , "amend" , & amend , "amend previous commit" ),
89102 OPT_BOOLEAN (0 , "untracked-files" , & untracked_files , "show all untracked files" ),
90103 OPT_BOOLEAN (0 , "allow-empty" , & allow_empty , "ok to record an empty change" ),
104+ OPT_STRING (0 , "cleanup" , & cleanup_arg , "default" , "how to strip spaces and #comments from message" ),
91105
92106 OPT_END ()
93107};
@@ -346,7 +360,8 @@ static int prepare_log_message(const char *index_file, const char *prefix)
346360 if (fp == NULL )
347361 die ("could not open %s" , git_path (commit_editmsg ));
348362
349- stripspace (& sb , 0 );
363+ if (cleanup_mode != CLEANUP_NONE )
364+ stripspace (& sb , 0 );
350365
351366 if (signoff ) {
352367 struct strbuf sob ;
@@ -372,22 +387,22 @@ static int prepare_log_message(const char *index_file, const char *prefix)
372387
373388 strbuf_release (& sb );
374389
375- if (no_edit ) {
390+ if (! use_editor ) {
376391 struct rev_info rev ;
377- unsigned char sha1 [40 ];
392+ unsigned char sha1 [20 ];
378393 const char * parent = "HEAD" ;
379394
380395 fclose (fp );
381396
382397 if (!active_nr && read_cache () < 0 )
383398 die ("Cannot read index" );
384399
385- if (get_sha1 ("HEAD" , sha1 ) != 0 )
386- return !!active_nr ;
387-
388400 if (amend )
389401 parent = "HEAD^1" ;
390402
403+ if (get_sha1 (parent , sha1 ))
404+ return !!active_nr ;
405+
391406 init_revisions (& rev , "" );
392407 rev .abbrev = 0 ;
393408 setup_revisions (0 , NULL , & rev , parent );
@@ -398,7 +413,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
398413 return !!DIFF_OPT_TST (& rev .diffopt , HAS_CHANGES );
399414 }
400415
401- if (in_merge && ! no_edit )
416+ if (in_merge )
402417 fprintf (fp ,
403418 "#\n"
404419 "# It looks like you may be committing a MERGE.\n"
@@ -411,7 +426,12 @@ static int prepare_log_message(const char *index_file, const char *prefix)
411426 fprintf (fp ,
412427 "\n"
413428 "# Please enter the commit message for your changes.\n"
414- "# (Comment lines starting with '#' will not be included)\n" );
429+ "# (Comment lines starting with '#' will " );
430+ if (cleanup_mode == CLEANUP_ALL )
431+ fprintf (fp , "not be included)\n" );
432+ else /* CLEANUP_SPACE, that is. */
433+ fprintf (fp , "be kept.\n"
434+ "# You can remove them yourself if you want to)\n" );
415435 if (only_include_assumed )
416436 fprintf (fp , "# %s\n" , only_include_assumed );
417437
@@ -435,10 +455,13 @@ static int message_is_empty(struct strbuf *sb, int start)
435455 const char * nl ;
436456 int eol , i ;
437457
458+ if (cleanup_mode == CLEANUP_NONE && sb -> len )
459+ return 0 ;
460+
438461 /* See if the template is just a prefix of the message. */
439462 strbuf_init (& tmpl , 0 );
440463 if (template_file && strbuf_read_file (& tmpl , template_file , 0 ) > 0 ) {
441- stripspace (& tmpl , 1 );
464+ stripspace (& tmpl , cleanup_mode == CLEANUP_ALL );
442465 if (start + tmpl .len <= sb -> len &&
443466 memcmp (tmpl .buf , sb -> buf + start , tmpl .len ) == 0 )
444467 start += tmpl .len ;
@@ -513,9 +536,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
513536 argc = parse_options (argc , argv , builtin_commit_options , usage , 0 );
514537
515538 if (logfile || message .len || use_message )
516- no_edit = 1 ;
539+ use_editor = 0 ;
517540 if (edit_flag )
518- no_edit = 0 ;
541+ use_editor = 1 ;
519542
520543 if (get_sha1 ("HEAD" , head_sha1 ))
521544 initial_commit = 1 ;
@@ -591,6 +614,16 @@ static int parse_and_validate_options(int argc, const char *argv[],
591614 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths..." ;
592615 also = 0 ;
593616 }
617+ if (!cleanup_arg || !strcmp (cleanup_arg , "default" ))
618+ cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE ;
619+ else if (!strcmp (cleanup_arg , "verbatim" ))
620+ cleanup_mode = CLEANUP_NONE ;
621+ else if (!strcmp (cleanup_arg , "whitespace" ))
622+ cleanup_mode = CLEANUP_SPACE ;
623+ else if (!strcmp (cleanup_arg , "strip" ))
624+ cleanup_mode = CLEANUP_ALL ;
625+ else
626+ die ("Invalid cleanup mode %s" , cleanup_arg );
594627
595628 if (all && argc > 0 )
596629 die ("Paths with -a does not make sense." );
@@ -796,7 +829,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
796829
797830 /* Get the commit message and validate it */
798831 header_len = sb .len ;
799- if (! no_edit ) {
832+ if (use_editor ) {
800833 char index [PATH_MAX ];
801834 const char * env [2 ] = { index , NULL };
802835 snprintf (index , sizeof (index ), "GIT_INDEX_FILE=%s" , index_file );
@@ -817,7 +850,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
817850 if (p != NULL )
818851 strbuf_setlen (& sb , p - sb .buf + 1 );
819852
820- stripspace (& sb , 1 );
853+ if (cleanup_mode != CLEANUP_NONE )
854+ stripspace (& sb , cleanup_mode == CLEANUP_ALL );
821855 if (sb .len < header_len || message_is_empty (& sb , header_len )) {
822856 rollback_index_files ();
823857 die ("no commit message? aborting commit." );
0 commit comments