get_ref_dir(): use a strbuf to hold refname

This simplifies the bookkeeping and allows an (artificial) restriction on refname component length to be removed. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 25, 2012 at 00:45 UTC 72b64b44e77112b93e7b046e54c62a01e69cef3d
1 file changed +26 -28
refs.c
+26 -28
@@ -756,7 +756,7 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
756 const char *path;
757 struct dirent *de;
758 int baselen;
759 - char *refname;
759 + struct strbuf refname;
760
761 if (*refs->name)
762 path = git_path_submodule(refs->name, "%s", base);
@@ -768,50 +768,48 @@ static void get_ref_dir(struct ref_cache *refs, const char *base,
768 return;
769
770 baselen = strlen(base);
771 - refname = xmalloc(baselen + 257);
772 -
773 - memcpy(refname, base, baselen);
774 - if (baselen && base[baselen-1] != '/')
775 - refname[baselen++] = '/';
771 + strbuf_init(&refname, baselen + 257);
772 + strbuf_add(&refname, base, baselen);
773 + if (baselen && base[baselen-1] != '/') {
774 + strbuf_addch(&refname, '/');
775 + baselen++;
776 + }
777
778 while ((de = readdir(d)) != NULL) {
779 unsigned char sha1[20];
780 struct stat st;
781 int flag;
781 - int namelen;
782 const char *refdir;
783
784 if (de->d_name[0] == '.')
785 continue;
786 - namelen = strlen(de->d_name);
787 - if (namelen > 255)
788 - continue;
786 if (has_extension(de->d_name, ".lock"))
787 continue;
791 - memcpy(refname + baselen, de->d_name, namelen+1);
788 + strbuf_addstr(&refname, de->d_name);
789 refdir = *refs->name
793 - ? git_path_submodule(refs->name, "%s", refname)
794 - : git_path("%s", refname);
795 - if (stat(refdir, &st) < 0)
796 - continue;
797 - if (S_ISDIR(st.st_mode)) {
798 - get_ref_dir(refs, refname, dir);
799 - continue;
800 - }
801 - if (*refs->name) {
802 - hashclr(sha1);
803 - flag = 0;
804 - if (resolve_gitlink_ref(refs->name, refname, sha1) < 0) {
790 + ? git_path_submodule(refs->name, "%s", refname.buf)
791 + : git_path("%s", refname.buf);
792 + if (stat(refdir, &st) < 0) {
793 + ; /* silently ignore */
794 + } else if (S_ISDIR(st.st_mode)) {
795 + get_ref_dir(refs, refname.buf, dir);
796 + } else {
797 + if (*refs->name) {
798 + hashclr(sha1);
799 + flag = 0;
800 + if (resolve_gitlink_ref(refs->name, refname.buf, sha1) < 0) {
801 + hashclr(sha1);
802 + flag |= REF_ISBROKEN;
803 + }
804 + } else if (read_ref_full(refname.buf, sha1, 1, &flag)) {
805 hashclr(sha1);
806 flag |= REF_ISBROKEN;
807 }
808 - } else if (read_ref_full(refname, sha1, 1, &flag)) {
809 - hashclr(sha1);
810 - flag |= REF_ISBROKEN;
808 + add_ref(dir, create_ref_entry(refname.buf, sha1, flag, 1));
809 }
812 - add_ref(dir, create_ref_entry(refname, sha1, flag, 1));
810 + strbuf_setlen(&refname, baselen);
811 }
814 - free(refname);
812 + strbuf_release(&refname);
813 closedir(d);
814 }
815