tag: disallow '-' as tag name
Disallow '-' as tag name, as well as tag names starting with '-', as it would be cumbersome to "git checkout tags/-" because "git checkout -" is to switch to the previous branch. Add strbuf_check_tag_ref() as helper to check a refname for a tag. Signed-off-by: Michael Schubert <mschub@elegosoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Schubert committed
May 10, 2011 at 01:36 UTC
4f0accd638b8d27c44066e7a866315ca67cb07ec
1 file changed
+18
-8
builtin/tag.c
+18
-8
@@ -352,11 +352,22 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
352
return 0;
353
}
354
355
+static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
356
+{
357
+ if (name[0] == '-')
358
+ return CHECK_REF_FORMAT_ERROR;
359
+
360
+ strbuf_reset(sb);
361
+ strbuf_addf(sb, "refs/tags/%s", name);
362
+
363
+ return check_ref_format(sb->buf);
364
+}
365
+
366
int cmd_tag(int argc, const char **argv, const char *prefix)
367
{
368
struct strbuf buf = STRBUF_INIT;
369
+ struct strbuf ref = STRBUF_INIT;
370
unsigned char object[20], prev[20];
359
- char ref[PATH_MAX];
371
const char *object_ref, *tag;
372
struct ref_lock *lock;
373
@@ -452,12 +463,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
463
if (get_sha1(object_ref, object))
464
die(_("Failed to resolve '%s' as a valid ref."), object_ref);
465
455
- if (snprintf(ref, sizeof(ref), "refs/tags/%s", tag) > sizeof(ref) - 1)
456
- die(_("tag name too long: %.*s..."), 50, tag);
457
- if (check_ref_format(ref))
466
+ if (strbuf_check_tag_ref(&ref, tag))
467
die(_("'%s' is not a valid tag name."), tag);
468
460
- if (!resolve_ref(ref, prev, 1, NULL))
469
+ if (!resolve_ref(ref.buf, prev, 1, NULL))
470
hashclr(prev);
471
else if (!force)
472
die(_("tag '%s' already exists"), tag);
@@ -466,14 +475,15 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
475
create_tag(object, tag, &buf, msg.given || msgfile,
476
sign, prev, object);
477
469
- lock = lock_any_ref_for_update(ref, prev, 0);
478
+ lock = lock_any_ref_for_update(ref.buf, prev, 0);
479
if (!lock)
471
- die(_("%s: cannot lock the ref"), ref);
480
+ die(_("%s: cannot lock the ref"), ref.buf);
481
if (write_ref_sha1(lock, object, NULL) < 0)
473
- die(_("%s: cannot update the ref"), ref);
482
+ die(_("%s: cannot update the ref"), ref.buf);
483
if (force && hashcmp(prev, object))
484
printf(_("Updated tag '%s' (was %s)\n"), tag, find_unique_abbrev(prev, DEFAULT_ABBREV));
485
486
strbuf_release(&buf);
487
+ strbuf_release(&ref);
488
return 0;
489
}