@@ -4437,7 +4437,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
4437
4437
struct labels_entry * labels_entry ;
4438
4438
struct string_entry * string_entry ;
4439
4439
struct object_id dummy ;
4440
- size_t len ;
4441
4440
int i ;
4442
4441
4443
4442
string_entry = oidmap_get (& state -> commit2label , oid );
@@ -4457,10 +4456,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
4457
4456
* abbreviation for any uninteresting commit's names that does not
4458
4457
* clash with any other label.
4459
4458
*/
4459
+ strbuf_reset (& state -> buf );
4460
4460
if (!label ) {
4461
4461
char * p ;
4462
4462
4463
- strbuf_reset (& state -> buf );
4464
4463
strbuf_grow (& state -> buf , GIT_MAX_HEXSZ );
4465
4464
label = p = state -> buf .buf ;
4466
4465
@@ -4483,32 +4482,55 @@ static const char *label_oid(struct object_id *oid, const char *label,
4483
4482
p [i ] = save ;
4484
4483
}
4485
4484
}
4486
- } else if (((len = strlen (label )) == the_hash_algo -> hexsz &&
4487
- !get_oid_hex (label , & dummy )) ||
4488
- (len == 1 && * label == '#' ) ||
4489
- hashmap_get_from_hash (& state -> labels ,
4490
- strihash (label ), label )) {
4485
+ } else {
4486
+ struct strbuf * buf = & state -> buf ;
4487
+
4491
4488
/*
4492
- * If the label already exists, or if the label is a valid full
4493
- * OID, or the label is a '#' (which we use as a separator
4494
- * between merge heads and oneline), we append a dash and a
4495
- * number to make it unique.
4489
+ * Sanitize labels by replacing non-alpha-numeric characters
4490
+ * (including white-space ones) by dashes, as they might be
4491
+ * illegal in file names (and hence in ref names).
4492
+ *
4493
+ * Note that we retain non-ASCII UTF-8 characters (identified
4494
+ * via the most significant bit). They should be all acceptable
4495
+ * in file names. We do not validate the UTF-8 here, that's not
4496
+ * the job of this function.
4496
4497
*/
4497
- struct strbuf * buf = & state -> buf ;
4498
+ for (; * label ; label ++ )
4499
+ if ((* label & 0x80 ) || isalnum (* label ))
4500
+ strbuf_addch (buf , * label );
4501
+ /* avoid leading dash and double-dashes */
4502
+ else if (buf -> len && buf -> buf [buf -> len - 1 ] != '-' )
4503
+ strbuf_addch (buf , '-' );
4504
+ if (!buf -> len ) {
4505
+ strbuf_addstr (buf , "rev-" );
4506
+ strbuf_add_unique_abbrev (buf , oid , default_abbrev );
4507
+ }
4508
+ label = buf -> buf ;
4498
4509
4499
- strbuf_reset (buf );
4500
- strbuf_add (buf , label , len );
4510
+ if ((buf -> len == the_hash_algo -> hexsz &&
4511
+ !get_oid_hex (label , & dummy )) ||
4512
+ (buf -> len == 1 && * label == '#' ) ||
4513
+ hashmap_get_from_hash (& state -> labels ,
4514
+ strihash (label ), label )) {
4515
+ /*
4516
+ * If the label already exists, or if the label is a
4517
+ * valid full OID, or the label is a '#' (which we use
4518
+ * as a separator between merge heads and oneline), we
4519
+ * append a dash and a number to make it unique.
4520
+ */
4521
+ size_t len = buf -> len ;
4501
4522
4502
- for (i = 2 ; ; i ++ ) {
4503
- strbuf_setlen (buf , len );
4504
- strbuf_addf (buf , "-%d" , i );
4505
- if (!hashmap_get_from_hash (& state -> labels ,
4506
- strihash (buf -> buf ),
4507
- buf -> buf ))
4508
- break ;
4509
- }
4523
+ for (i = 2 ; ; i ++ ) {
4524
+ strbuf_setlen (buf , len );
4525
+ strbuf_addf (buf , "-%d" , i );
4526
+ if (!hashmap_get_from_hash (& state -> labels ,
4527
+ strihash (buf -> buf ),
4528
+ buf -> buf ))
4529
+ break ;
4530
+ }
4510
4531
4511
- label = buf -> buf ;
4532
+ label = buf -> buf ;
4533
+ }
4512
4534
}
4513
4535
4514
4536
FLEX_ALLOC_STR (labels_entry , label , label );
@@ -4610,10 +4632,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
4610
4632
else
4611
4633
strbuf_addbuf (& label , & oneline );
4612
4634
4613
- for (p1 = label .buf ; * p1 ; p1 ++ )
4614
- if (isspace (* p1 ))
4615
- * (char * )p1 = '-' ;
4616
-
4617
4635
strbuf_reset (& buf );
4618
4636
strbuf_addf (& buf , "%s -C %s" ,
4619
4637
cmd_merge , oid_to_hex (& commit -> object .oid ));
0 commit comments