@@ -4425,7 +4425,6 @@ static const char *label_oid(struct object_id *oid, const char *label,
44254425 struct labels_entry * labels_entry ;
44264426 struct string_entry * string_entry ;
44274427 struct object_id dummy ;
4428- size_t len ;
44294428 int i ;
44304429
44314430 string_entry = oidmap_get (& state -> commit2label , oid );
@@ -4445,10 +4444,10 @@ static const char *label_oid(struct object_id *oid, const char *label,
44454444 * abbreviation for any uninteresting commit's names that does not
44464445 * clash with any other label.
44474446 */
4447+ strbuf_reset (& state -> buf );
44484448 if (!label ) {
44494449 char * p ;
44504450
4451- strbuf_reset (& state -> buf );
44524451 strbuf_grow (& state -> buf , GIT_MAX_HEXSZ );
44534452 label = p = state -> buf .buf ;
44544453
@@ -4471,32 +4470,55 @@ static const char *label_oid(struct object_id *oid, const char *label,
44714470 p [i ] = save ;
44724471 }
44734472 }
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+
44794476 /*
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.
44844485 */
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 ;
44864497
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 ;
44894510
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+ }
44984519
4499- label = buf -> buf ;
4520+ label = buf -> buf ;
4521+ }
45004522 }
45014523
45024524 FLEX_ALLOC_STR (labels_entry , label , label );
@@ -4598,10 +4620,6 @@ static int make_script_with_merges(struct pretty_print_context *pp,
45984620 else
45994621 strbuf_addbuf (& label , & oneline );
46004622
4601- for (p1 = label .buf ; * p1 ; p1 ++ )
4602- if (isspace (* p1 ))
4603- * (char * )p1 = '-' ;
4604-
46054623 strbuf_reset (& buf );
46064624 strbuf_addf (& buf , "%s -C %s" ,
46074625 cmd_merge , oid_to_hex (& commit -> object .oid ));
0 commit comments