@@ -4425,7 +4425,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
4425
4425
struct labels_entry * labels_entry ;
4426
4426
struct string_entry * string_entry ;
4427
4427
struct object_id dummy ;
4428
- size_t len ;
4429
4428
int i ;
4430
4429
4431
4430
string_entry = oidmap_get (& state -> commit2label , oid );
@@ -4445,10 +4444,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
4445
4444
* abbreviation for any uninteresting commit's names that does not
4446
4445
* clash with any other label.
4447
4446
*/
4447
+ strbuf_reset (& state -> buf );
4448
4448
if (!label ) {
4449
4449
char * p ;
4450
4450
4451
- strbuf_reset (& state -> buf );
4452
4451
strbuf_grow (& state -> buf , GIT_MAX_HEXSZ );
4453
4452
label = p = state -> buf .buf ;
4454
4453
@@ -4471,32 +4470,55 @@ static const char *label_oid(struct object_id *oid, const char *label,
4471
4470
p [i ] = save ;
4472
4471
}
4473
4472
}
4474
- } else if (((len = strlen (label )) == the_hash_algo -> hexsz &&
4475
- !get_oid_hex (label , & dummy )) ||
4476
- (len == 1 && * label == '#' ) ||
4477
- hashmap_get_from_hash (& state -> labels ,
4478
- strihash (label ), label )) {
4473
+ } else {
4474
+ struct strbuf * buf = & state -> buf ;
4475
+
4479
4476
/*
4480
- * If the label already exists, or if the label is a valid full
4481
- * OID, or the label is a '#' (which we use as a separator
4482
- * between merge heads and oneline), we append a dash and a
4483
- * number to make it unique.
4477
+ * Sanitize labels by replacing non-alpha-numeric characters
4478
+ * (including white-space ones) by dashes, as they might be
4479
+ * illegal in file names (and hence in ref names).
4480
+ *
4481
+ * Note that we retain non-ASCII UTF-8 characters (identified
4482
+ * via the most significant bit). They should be all acceptable
4483
+ * in file names. We do not validate the UTF-8 here, that's not
4484
+ * the job of this function.
4484
4485
*/
4485
- struct strbuf * buf = & state -> buf ;
4486
+ for (; * label ; label ++ )
4487
+ if ((* label & 0x80 ) || isalnum (* label ))
4488
+ strbuf_addch (buf , * label );
4489
+ /* avoid leading dash and double-dashes */
4490
+ else if (buf -> len && buf -> buf [buf -> len - 1 ] != '-' )
4491
+ strbuf_addch (buf , '-' );
4492
+ if (!buf -> len ) {
4493
+ strbuf_addstr (buf , "rev-" );
4494
+ strbuf_add_unique_abbrev (buf , oid , default_abbrev );
4495
+ }
4496
+ label = buf -> buf ;
4486
4497
4487
- strbuf_reset (buf );
4488
- strbuf_add (buf , label , len );
4498
+ if ((buf -> len == the_hash_algo -> hexsz &&
4499
+ !get_oid_hex (label , & dummy )) ||
4500
+ (buf -> len == 1 && * label == '#' ) ||
4501
+ hashmap_get_from_hash (& state -> labels ,
4502
+ strihash (label ), label )) {
4503
+ /*
4504
+ * If the label already exists, or if the label is a
4505
+ * valid full OID, or the label is a '#' (which we use
4506
+ * as a separator between merge heads and oneline), we
4507
+ * append a dash and a number to make it unique.
4508
+ */
4509
+ size_t len = buf -> len ;
4489
4510
4490
- for (i = 2 ; ; i ++ ) {
4491
- strbuf_setlen (buf , len );
4492
- strbuf_addf (buf , "-%d" , i );
4493
- if (!hashmap_get_from_hash (& state -> labels ,
4494
- strihash (buf -> buf ),
4495
- buf -> buf ))
4496
- break ;
4497
- }
4511
+ for (i = 2 ; ; i ++ ) {
4512
+ strbuf_setlen (buf , len );
4513
+ strbuf_addf (buf , "-%d" , i );
4514
+ if (!hashmap_get_from_hash (& state -> labels ,
4515
+ strihash (buf -> buf ),
4516
+ buf -> buf ))
4517
+ break ;
4518
+ }
4498
4519
4499
- label = buf -> buf ;
4520
+ label = buf -> buf ;
4521
+ }
4500
4522
}
4501
4523
4502
4524
FLEX_ALLOC_STR (labels_entry , label , label );
@@ -4598,10 +4620,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
4598
4620
else
4599
4621
strbuf_addbuf (& label , & oneline );
4600
4622
4601
- for (p1 = label .buf ; * p1 ; p1 ++ )
4602
- if (isspace (* p1 ))
4603
- * (char * )p1 = '-' ;
4604
-
4605
4623
strbuf_reset (& buf );
4606
4624
strbuf_addf (& buf , "%s -C %s" ,
4607
4625
cmd_merge , oid_to_hex (& commit -> object .oid ));
0 commit comments