commit 1e6ed1007a3a94c01fcc0a1442911f689cbe862b
parent cda8e4de2a97bdc852163cca54bcbc1f7d011ad7
Author: krasjet
Date: 2020-07-15 01:35Z

use a function to read meta instead of boilerplate

Diffstat:
Mstagit.c | 81+++++++++++++++++++++++++++----------------------------------------------------
1 file changed, 28 insertions(+), 53 deletions(-)

diff --git a/stagit.c b/stagit.c @@ -85,6 +85,28 @@ joinpath(char *buf, size_t bufsiz, const char *path, const char *path2) path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2); } +/* read metadata from name or .git/name to buf */ +void +readmeta(const char * name, char *buf, size_t bufsiz, const char * repodir) +{ + char path[PATH_MAX]; + FILE * fp; + + /* read name or .git/name */ + joinpath(path, sizeof(path), repodir, name); + if (!(fp = fopen(path, "r"))) { + joinpath(path, sizeof(path), repodir, ".git/"); + strcat(path, name); + fp = fopen(path, "r"); + } + if (fp) { + if (!fgets(buf, bufsiz, fp)) + buf[0] = '\0'; + buf[strcspn(buf, "\r\n")] = '\0'; + fclose(fp); + } +} + void deltainfo_free(struct deltainfo *di) { @@ -1058,8 +1080,8 @@ main(int argc, char *argv[]) git_object *obj = NULL; const git_oid *head = NULL; mode_t mask; - FILE *fp, *fpread; - char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p; + FILE *fp; + char repodirabs[PATH_MAX + 1], *p; char tmppath[64] = "cache.XXXXXXXXXXXX", buf[BUFSIZ]; size_t n; int i, fd; @@ -1132,57 +1154,10 @@ main(int argc, char *argv[]) if (!strcmp(p, ".git")) *p = '\0'; - /* read description or .git/description */ - joinpath(path, sizeof(path), repodir, "description"); - if (!(fpread = fopen(path, "r"))) { - joinpath(path, sizeof(path), repodir, ".git/description"); - fpread = fopen(path, "r"); - } - if (fpread) { - if (!fgets(description, sizeof(description), fpread)) - description[0] = '\0'; - description[strcspn(description, "\r\n")] = '\0'; - fclose(fpread); - } - - /* read url or .git/url */ - joinpath(path, sizeof(path), repodir, "url"); - if (!(fpread = fopen(path, "r"))) { - joinpath(path, sizeof(path), repodir, ".git/url"); - fpread = fopen(path, "r"); - } - if (fpread) { - if (!fgets(cloneurl, sizeof(cloneurl), fpread)) - cloneurl[0] = '\0'; - cloneurl[strcspn(cloneurl, "\r\n")] = '\0'; - fclose(fpread); - } - - /* read upstream_url or .git/upstream_url */ - joinpath(path, sizeof(path), repodir, "upstream_url"); - if (!(fpread = fopen(path, "r"))) { - joinpath(path, sizeof(path), repodir, ".git/upstream_url"); - fpread = fopen(path, "r"); - } - if (fpread) { - if (!fgets(upstreamurl, sizeof(upstreamurl), fpread)) - upstreamurl[0] = '\0'; - upstreamurl[strcspn(upstreamurl, "\r\n")] = '\0'; - fclose(fpread); - } - - /* read home_url or .git/home_url */ - joinpath(path, sizeof(path), repodir, "home_url"); - if (!(fpread = fopen(path, "r"))) { - joinpath(path, sizeof(path), repodir, ".git/home_url"); - fpread = fopen(path, "r"); - } - if (fpread) { - if (!fgets(homeurl, sizeof(homeurl), fpread)) - homeurl[0] = '\0'; - homeurl[strcspn(homeurl, "\r\n")] = '\0'; - fclose(fpread); - } + readmeta("description", description, sizeof(description), repodir); + readmeta("url", cloneurl, sizeof(cloneurl), repodir); + readmeta("upstream_url", upstreamurl, sizeof(upstreamurl), repodir); + readmeta("home_url", homeurl, sizeof(homeurl), repodir); /* check LICENSE */ for (i = 0; i < sizeof(licensefiles) / sizeof(*licensefiles) && !license; i++) {