Git fork

refs: move ref name helpers around

strbuf_branchname(), strbuf_check_{branch,tag}_ref() are helper
functions to deal with branch and tag names, and the fact that they
happen to use strbuf to hold the name of a branch or a tag is not
essential. These functions fit better in the refs API than strbuf
API, the latter of which is about string manipulations.

Signed-off-by: Junio C Hamano <gitster@pobox.com>

+76 -69
-11
builtin/tag.c
··· 447 447 return 0; 448 448 } 449 449 450 - static int strbuf_check_tag_ref(struct strbuf *sb, const char *name) 451 - { 452 - if (name[0] == '-') 453 - return -1; 454 - 455 - strbuf_reset(sb); 456 - strbuf_addf(sb, "refs/tags/%s", name); 457 - 458 - return check_refname_format(sb->buf, 0); 459 - } 460 - 461 450 int cmd_tag(int argc, 462 451 const char **argv, 463 452 const char *prefix,
-36
object-name.c
··· 1734 1734 return -1; 1735 1735 } 1736 1736 1737 - void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) 1738 - { 1739 - int len = strlen(name); 1740 - struct interpret_branch_name_options options = { 1741 - .allowed = allowed 1742 - }; 1743 - int used = repo_interpret_branch_name(the_repository, name, len, sb, 1744 - &options); 1745 - 1746 - if (used < 0) 1747 - used = 0; 1748 - strbuf_add(sb, name + used, len - used); 1749 - } 1750 - 1751 - int strbuf_check_branch_ref(struct strbuf *sb, const char *name) 1752 - { 1753 - if (startup_info->have_repository) 1754 - strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); 1755 - else 1756 - strbuf_addstr(sb, name); 1757 - 1758 - /* 1759 - * This splice must be done even if we end up rejecting the 1760 - * name; builtin/branch.c::copy_or_rename_branch() still wants 1761 - * to see what the name expanded to so that "branch -m" can be 1762 - * used as a tool to correct earlier mistakes. 1763 - */ 1764 - strbuf_splice(sb, 0, 0, "refs/heads/", 11); 1765 - 1766 - if (*name == '-' || 1767 - !strcmp(sb->buf, "refs/heads/HEAD")) 1768 - return -1; 1769 - 1770 - return check_refname_format(sb->buf, 0); 1771 - } 1772 - 1773 1737 void object_context_release(struct object_context *ctx) 1774 1738 { 1775 1739 free(ctx->path);
+47
refs.c
··· 697 697 return NULL; 698 698 } 699 699 700 + void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed) 701 + { 702 + int len = strlen(name); 703 + struct interpret_branch_name_options options = { 704 + .allowed = allowed 705 + }; 706 + int used = repo_interpret_branch_name(the_repository, name, len, sb, 707 + &options); 708 + 709 + if (used < 0) 710 + used = 0; 711 + strbuf_add(sb, name + used, len - used); 712 + } 713 + 714 + int strbuf_check_branch_ref(struct strbuf *sb, const char *name) 715 + { 716 + if (startup_info->have_repository) 717 + strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL); 718 + else 719 + strbuf_addstr(sb, name); 720 + 721 + /* 722 + * This splice must be done even if we end up rejecting the 723 + * name; builtin/branch.c::copy_or_rename_branch() still wants 724 + * to see what the name expanded to so that "branch -m" can be 725 + * used as a tool to correct earlier mistakes. 726 + */ 727 + strbuf_splice(sb, 0, 0, "refs/heads/", 11); 728 + 729 + if (*name == '-' || 730 + !strcmp(sb->buf, "refs/heads/HEAD")) 731 + return -1; 732 + 733 + return check_refname_format(sb->buf, 0); 734 + } 735 + 736 + int strbuf_check_tag_ref(struct strbuf *sb, const char *name) 737 + { 738 + if (name[0] == '-') 739 + return -1; 740 + 741 + strbuf_reset(sb); 742 + strbuf_addf(sb, "refs/tags/%s", name); 743 + 744 + return check_refname_format(sb->buf, 0); 745 + } 746 + 700 747 int repo_dwim_ref(struct repository *r, const char *str, int len, 701 748 struct object_id *oid, char **ref, int nonfatal_dangling_mark) 702 749 {
+29
refs.h
··· 181 181 char *repo_default_branch_name(struct repository *r, int quiet); 182 182 183 183 /* 184 + * Copy "name" to "sb", expanding any special @-marks as handled by 185 + * repo_interpret_branch_name(). The result is a non-qualified branch name 186 + * (so "foo" or "origin/master" instead of "refs/heads/foo" or 187 + * "refs/remotes/origin/master"). 188 + * 189 + * Note that the resulting name may not be a syntactically valid refname. 190 + * 191 + * If "allowed" is non-zero, restrict the set of allowed expansions. See 192 + * repo_interpret_branch_name() for details. 193 + */ 194 + void strbuf_branchname(struct strbuf *sb, const char *name, 195 + unsigned allowed); 196 + 197 + /* 198 + * Like strbuf_branchname() above, but confirm that the result is 199 + * syntactically valid to be used as a local branch name in refs/heads/. 200 + * 201 + * The return value is "0" if the result is valid, and "-1" otherwise. 202 + */ 203 + int strbuf_check_branch_ref(struct strbuf *sb, const char *name); 204 + 205 + /* 206 + * Similar for a tag name in refs/tags/. 207 + * 208 + * The return value is "0" if the result is valid, and "-1" otherwise. 209 + */ 210 + int strbuf_check_tag_ref(struct strbuf *sb, const char *name); 211 + 212 + /* 184 213 * A ref_transaction represents a collection of reference updates that 185 214 * should succeed or fail together. 186 215 *
-22
strbuf.h
··· 637 637 strbuf_complete(sb, '\n'); 638 638 } 639 639 640 - /* 641 - * Copy "name" to "sb", expanding any special @-marks as handled by 642 - * repo_interpret_branch_name(). The result is a non-qualified branch name 643 - * (so "foo" or "origin/master" instead of "refs/heads/foo" or 644 - * "refs/remotes/origin/master"). 645 - * 646 - * Note that the resulting name may not be a syntactically valid refname. 647 - * 648 - * If "allowed" is non-zero, restrict the set of allowed expansions. See 649 - * repo_interpret_branch_name() for details. 650 - */ 651 - void strbuf_branchname(struct strbuf *sb, const char *name, 652 - unsigned allowed); 653 - 654 - /* 655 - * Like strbuf_branchname() above, but confirm that the result is 656 - * syntactically valid to be used as a local branch name in refs/heads/. 657 - * 658 - * The return value is "0" if the result is valid, and "-1" otherwise. 659 - */ 660 - int strbuf_check_branch_ref(struct strbuf *sb, const char *name); 661 - 662 640 typedef int (*char_predicate)(char ch); 663 641 664 642 void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,