resolve_ref_1(): eliminate local variable
[gitweb.git] / refs / files-backend.c
index c0cf6fd6c069d3cfa3f4506a5f691a111e31211d..69ec9036e0c8b70304e57f30a2f94d675340e914 100644 (file)
@@ -1383,8 +1383,7 @@ static int resolve_missing_loose_ref(const char *refname,
        entry = get_packed_ref(refname);
        if (entry) {
                hashcpy(sha1, entry->u.value.oid.hash);
-               if (flags)
-                       *flags |= REF_ISPACKED;
+               *flags |= REF_ISPACKED;
                return 0;
        }
        /* refname is not a packed reference. */
@@ -1400,15 +1399,13 @@ static const char *resolve_ref_1(const char *refname,
                                 struct strbuf *sb_path,
                                 struct strbuf *sb_contents)
 {
-       int depth = MAXDEPTH;
        int bad_name = 0;
+       int symref_count;
 
-       if (flags)
-               *flags = 0;
+       *flags = 0;
 
        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
-               if (flags)
-                       *flags |= REF_BAD_NAME;
+               *flags |= REF_BAD_NAME;
 
                if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
                    !refname_is_safe(refname)) {
@@ -1425,17 +1422,12 @@ static const char *resolve_ref_1(const char *refname,
                 */
                bad_name = 1;
        }
-       for (;;) {
+
+       for (symref_count = 0; symref_count < MAXDEPTH; symref_count++) {
                const char *path;
                struct stat st;
-               char *buf;
                int fd;
 
-               if (--depth < 0) {
-                       errno = ELOOP;
-                       return NULL;
-               }
-
                strbuf_reset(sb_path);
                strbuf_git_path(sb_path, "%s", refname);
                path = sb_path->buf;
@@ -1462,8 +1454,7 @@ static const char *resolve_ref_1(const char *refname,
                        }
                        if (bad_name) {
                                hashclr(sha1);
-                               if (flags)
-                                       *flags |= REF_ISBROKEN;
+                               *flags |= REF_ISBROKEN;
                        }
                        return refname;
                }
@@ -1482,8 +1473,7 @@ static const char *resolve_ref_1(const char *refname,
                            !check_refname_format(sb_contents->buf, 0)) {
                                strbuf_swap(sb_refname, sb_contents);
                                refname = sb_refname->buf;
-                               if (flags)
-                                       *flags |= REF_ISSYMREF;
+                               *flags |= REF_ISSYMREF;
                                if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
                                        hashclr(sha1);
                                        return refname;
@@ -1530,42 +1520,41 @@ static const char *resolve_ref_1(const char *refname,
                         */
                        if (get_sha1_hex(sb_contents->buf, sha1) ||
                            (sb_contents->buf[40] != '\0' && !isspace(sb_contents->buf[40]))) {
-                               if (flags)
-                                       *flags |= REF_ISBROKEN;
+                               *flags |= REF_ISBROKEN;
                                errno = EINVAL;
                                return NULL;
                        }
                        if (bad_name) {
                                hashclr(sha1);
-                               if (flags)
-                                       *flags |= REF_ISBROKEN;
+                               *flags |= REF_ISBROKEN;
                        }
                        return refname;
                }
-               if (flags)
-                       *flags |= REF_ISSYMREF;
-               buf = sb_contents->buf + 4;
-               while (isspace(*buf))
-                       buf++;
+               *flags |= REF_ISSYMREF;
+               refname = sb_contents->buf + 4;
+               while (isspace(*refname))
+                       refname++;
                strbuf_reset(sb_refname);
-               strbuf_addstr(sb_refname, buf);
+               strbuf_addstr(sb_refname, refname);
                refname = sb_refname->buf;
                if (resolve_flags & RESOLVE_REF_NO_RECURSE) {
                        hashclr(sha1);
                        return refname;
                }
-               if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
-                       if (flags)
-                               *flags |= REF_ISBROKEN;
+               if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
+                       *flags |= REF_ISBROKEN;
 
                        if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
-                           !refname_is_safe(buf)) {
+                           !refname_is_safe(refname)) {
                                errno = EINVAL;
                                return NULL;
                        }
                        bad_name = 1;
                }
        }
+
+       errno = ELOOP;
+       return NULL;
 }
 
 const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
@@ -1574,8 +1563,12 @@ const char *resolve_ref_unsafe(const char *refname, int resolve_flags,
        static struct strbuf sb_refname = STRBUF_INIT;
        struct strbuf sb_contents = STRBUF_INIT;
        struct strbuf sb_path = STRBUF_INIT;
+       int unused_flags;
        const char *ret;
 
+       if (!flags)
+               flags = &unused_flags;
+
        ret = resolve_ref_1(refname, resolve_flags, sha1, flags,
                            &sb_refname, &sb_path, &sb_contents);
        strbuf_release(&sb_path);