@@ -268,6 +268,15 @@ static const char tag_template[] =
268
268
N_ ("\n"
269
269
"#\n"
270
270
"# Write a tag message\n"
271
+ "# Lines starting with '#' will be ignored.\n"
272
+ "#\n" );
273
+
274
+ static const char tag_template_nocleanup [] =
275
+ N_ ("\n"
276
+ "#\n"
277
+ "# Write a tag message\n"
278
+ "# Lines starting with '#' will be kept; you may remove them"
279
+ " yourself if you want to.\n"
271
280
"#\n" );
272
281
273
282
static void set_signingkey (const char * value )
@@ -319,8 +328,18 @@ static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
319
328
return 0 ;
320
329
}
321
330
331
+ struct create_tag_options {
332
+ unsigned int message_given :1 ;
333
+ unsigned int sign ;
334
+ enum {
335
+ CLEANUP_NONE ,
336
+ CLEANUP_SPACE ,
337
+ CLEANUP_ALL
338
+ } cleanup_mode ;
339
+ };
340
+
322
341
static void create_tag (const unsigned char * object , const char * tag ,
323
- struct strbuf * buf , int message , int sign ,
342
+ struct strbuf * buf , struct create_tag_options * opt ,
324
343
unsigned char * prev , unsigned char * result )
325
344
{
326
345
enum object_type type ;
@@ -345,7 +364,7 @@ static void create_tag(const unsigned char *object, const char *tag,
345
364
if (header_len > sizeof (header_buf ) - 1 )
346
365
die (_ ("tag header too big." ));
347
366
348
- if (!message ) {
367
+ if (!opt -> message_given ) {
349
368
int fd ;
350
369
351
370
/* write the template message before editing: */
@@ -356,8 +375,12 @@ static void create_tag(const unsigned char *object, const char *tag,
356
375
357
376
if (!is_null_sha1 (prev ))
358
377
write_tag_body (fd , prev );
378
+ else if (opt -> cleanup_mode == CLEANUP_ALL )
379
+ write_or_die (fd , _ (tag_template ),
380
+ strlen (_ (tag_template )));
359
381
else
360
- write_or_die (fd , _ (tag_template ), strlen (_ (tag_template )));
382
+ write_or_die (fd , _ (tag_template_nocleanup ),
383
+ strlen (_ (tag_template_nocleanup )));
361
384
close (fd );
362
385
363
386
if (launch_editor (path , buf , NULL )) {
@@ -367,14 +390,15 @@ static void create_tag(const unsigned char *object, const char *tag,
367
390
}
368
391
}
369
392
370
- stripspace (buf , 1 );
393
+ if (opt -> cleanup_mode != CLEANUP_NONE )
394
+ stripspace (buf , opt -> cleanup_mode == CLEANUP_ALL );
371
395
372
- if (!message && !buf -> len )
396
+ if (!opt -> message_given && !buf -> len )
373
397
die (_ ("no tag message?" ));
374
398
375
399
strbuf_insert (buf , 0 , header_buf , header_len );
376
400
377
- if (build_tag_object (buf , sign , result ) < 0 ) {
401
+ if (build_tag_object (buf , opt -> sign , result ) < 0 ) {
378
402
if (path )
379
403
fprintf (stderr , _ ("The tag message has been left in %s\n" ),
380
404
path );
@@ -422,9 +446,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
422
446
unsigned char object [20 ], prev [20 ];
423
447
const char * object_ref , * tag ;
424
448
struct ref_lock * lock ;
425
-
426
- int annotate = 0 , sign = 0 , force = 0 , lines = -1 ,
427
- list = 0 , delete = 0 , verify = 0 ;
449
+ struct create_tag_options opt ;
450
+ char * cleanup_arg = NULL ;
451
+ int annotate = 0 , force = 0 , lines = -1 , list = 0 ,
452
+ delete = 0 , verify = 0 ;
428
453
const char * msgfile = NULL , * keyid = NULL ;
429
454
struct msg_arg msg = { 0 , STRBUF_INIT };
430
455
struct commit_list * with_commit = NULL ;
@@ -442,7 +467,9 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
442
467
OPT_CALLBACK ('m' , "message" , & msg , "message" ,
443
468
"tag message" , parse_msg_arg ),
444
469
OPT_FILENAME ('F' , "file" , & msgfile , "read message from file" ),
445
- OPT_BOOLEAN ('s' , "sign" , & sign , "annotated and GPG-signed tag" ),
470
+ OPT_BOOLEAN ('s' , "sign" , & opt .sign , "annotated and GPG-signed tag" ),
471
+ OPT_STRING (0 , "cleanup" , & cleanup_arg , "mode" ,
472
+ "how to strip spaces and #comments from message" ),
446
473
OPT_STRING ('u' , "local-user" , & keyid , "key-id" ,
447
474
"use another key to sign the tag" ),
448
475
OPT__FORCE (& force , "replace the tag if exists" ),
@@ -459,13 +486,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
459
486
460
487
git_config (git_tag_config , NULL );
461
488
489
+ memset (& opt , 0 , sizeof (opt ));
490
+
462
491
argc = parse_options (argc , argv , prefix , options , git_tag_usage , 0 );
463
492
464
493
if (keyid ) {
465
- sign = 1 ;
494
+ opt . sign = 1 ;
466
495
set_signingkey (keyid );
467
496
}
468
- if (sign )
497
+ if (opt . sign )
469
498
annotate = 1 ;
470
499
if (argc == 0 && !(delete || verify ))
471
500
list = 1 ;
@@ -523,9 +552,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
523
552
else if (!force )
524
553
die (_ ("tag '%s' already exists" ), tag );
525
554
555
+ opt .message_given = msg .given || msgfile ;
556
+
557
+ if (!cleanup_arg || !strcmp (cleanup_arg , "strip" ))
558
+ opt .cleanup_mode = CLEANUP_ALL ;
559
+ else if (!strcmp (cleanup_arg , "verbatim" ))
560
+ opt .cleanup_mode = CLEANUP_NONE ;
561
+ else if (!strcmp (cleanup_arg , "whitespace" ))
562
+ opt .cleanup_mode = CLEANUP_SPACE ;
563
+ else
564
+ die (_ ("Invalid cleanup mode %s" ), cleanup_arg );
565
+
526
566
if (annotate )
527
- create_tag (object , tag , & buf , msg .given || msgfile ,
528
- sign , prev , object );
567
+ create_tag (object , tag , & buf , & opt , prev , object );
529
568
530
569
lock = lock_any_ref_for_update (ref .buf , prev , 0 );
531
570
if (!lock )
0 commit comments