]> git.ipfire.org Git - thirdparty/git.git/blobdiff - remote.c
read_branches_file: simplify string handling
[thirdparty/git.git] / remote.c
index 26504b744786c65ea4d6e1e0abbf5c6409af5358..22a60fcc0bbd82bcc7649a550921758ba73d140f 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -65,7 +65,6 @@ static int valid_remote(const struct remote *remote)
 static const char *alias_url(const char *url, struct rewrites *r)
 {
        int i, j;
-       char *ret;
        struct counted_string *longest;
        int longest_i;
 
@@ -86,11 +85,7 @@ static const char *alias_url(const char *url, struct rewrites *r)
        if (!longest)
                return url;
 
-       ret = xmalloc(r->rewrite[longest_i]->baselen +
-                    (strlen(url) - longest->len) + 1);
-       strcpy(ret, r->rewrite[longest_i]->base);
-       strcpy(ret + r->rewrite[longest_i]->baselen, url + longest->len);
-       return ret;
+       return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
 }
 
 static void add_push_refspec(struct remote *remote, const char *ref)
@@ -298,56 +293,42 @@ static void read_remotes_file(struct remote *remote)
 static void read_branches_file(struct remote *remote)
 {
        char *frag;
-       struct strbuf branch = STRBUF_INIT;
-       int n = 1000;
-       FILE *f = fopen(git_path("branches/%.*s", n, remote->name), "r");
-       char *s, *p;
-       int len;
+       struct strbuf buf = STRBUF_INIT;
+       FILE *f = fopen(git_path("branches/%s", remote->name), "r");
 
        if (!f)
                return;
-       s = fgets(buffer, BUF_SIZE, f);
-       fclose(f);
-       if (!s)
-               return;
-       while (isspace(*s))
-               s++;
-       if (!*s)
+
+       strbuf_getline(&buf, f, '\n');
+       strbuf_trim(&buf);
+       if (!buf.len) {
+               strbuf_release(&buf);
                return;
+       }
+
        remote->origin = REMOTE_BRANCHES;
-       p = s + strlen(s);
-       while (isspace(p[-1]))
-               *--p = 0;
-       len = p - s;
-       p = xmalloc(len + 1);
-       strcpy(p, s);
 
        /*
         * The branches file would have URL and optionally
         * #branch specified.  The "master" (or specified) branch is
-        * fetched and stored in the local branch of the same name.
+        * fetched and stored in the local branch matching the
+        * remote name.
         */
-       frag = strchr(p, '#');
-       if (frag) {
+       frag = strchr(buf.buf, '#');
+       if (frag)
                *(frag++) = '\0';
-               strbuf_addf(&branch, "refs/heads/%s", frag);
-       } else
-               strbuf_addstr(&branch, "refs/heads/master");
+       else
+               frag = "master";
+
+       add_url_alias(remote, strbuf_detach(&buf, NULL));
+       add_fetch_refspec(remote, xstrfmt("refs/heads/%s:refs/heads/%s",
+                                         frag, remote->name));
 
-       strbuf_addf(&branch, ":refs/heads/%s", remote->name);
-       add_url_alias(remote, p);
-       add_fetch_refspec(remote, strbuf_detach(&branch, NULL));
        /*
         * Cogito compatible push: push current HEAD to remote #branch
         * (master if missing)
         */
-       strbuf_init(&branch, 0);
-       strbuf_addstr(&branch, "HEAD");
-       if (frag)
-               strbuf_addf(&branch, ":refs/heads/%s", frag);
-       else
-               strbuf_addstr(&branch, ":refs/heads/master");
-       add_push_refspec(remote, strbuf_detach(&branch, NULL));
+       add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag));
        remote->fetch_tags = 1; /* always auto-follow */
 }