From 3872d9e902722b0c85c7f5c6ef8975389167e7d9 Mon Sep 17 00:00:00 2001 From: Hiltjo Posthuma Date: Sat, 5 Dec 2015 20:59:14 +0100 Subject: [PATCH] detect name and description --- urmoms.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/urmoms.c b/urmoms.c index 2d385f1..cebe1b6 100644 --- a/urmoms.c +++ b/urmoms.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,12 +9,11 @@ static git_repository *repo; -static const char *relpath = ""; -static const char *name = ""; -static const char *description = ""; - +static const char *relpath; static const char *repodir = "."; +static char name[255]; +static char description[255]; static int hasreadme, haslicense; FILE * @@ -28,6 +28,41 @@ efopen(const char *name, const char *flags) return fp; } +void +concat(FILE *fp1, FILE *fp2) +{ + char buf[BUFSIZ]; + size_t n; + + while ((n = fread(buf, 1, sizeof(buf), fp1))) { + fwrite(buf, 1, n, fp2); + + if (feof(fp1) || ferror(fp1) || ferror(fp2)) + break; + } +} + +/* Some implementations of basename(3) return a pointer to a static + * internal buffer (OpenBSD). Others modify the contents of `path` (POSIX). + * This is a wrapper function that is compatible with both versions. + * The program will error out if basename(3) failed, this can only happen + * with the OpenBSD version. */ +char * +xbasename(const char *path) +{ + char *p, *b; + + if (!(p = strdup(path))) + err(1, "strdup"); + if (!(b = basename(p))) + err(1, "basename"); + if (!(b = strdup(b))) + err(1, "strdup"); + free(p); + + return b; +} + static void printtime(FILE *fp, const git_time * intime, const char *prefix) { @@ -98,7 +133,7 @@ writeheader(FILE *fp) "" "" ""); - fprintf(fp, "%s - %s", name, description); + fprintf(fp, "%s%s%s", name, description[0] ? " - " : "", description); fprintf(fp, "" "
"); fprintf(fp, "

\"\" %s

", relpath, name); @@ -187,26 +222,12 @@ writebranches(FILE *fp) } #endif -void -concat(FILE *fp1, FILE *fp2) -{ - char buf[BUFSIZ]; - size_t n; - - while ((n = fread(buf, 1, sizeof(buf), fp1))) { - fwrite(buf, 1, n, fp2); - - if (feof(fp1) || ferror(fp1) || ferror(fp2)) - break; - } -} - int main(int argc, char *argv[]) { const git_error *e = NULL; FILE *fp, *fpread; - char path[PATH_MAX]; + char path[PATH_MAX], *p; int status; if (argc != 2) { @@ -223,6 +244,26 @@ main(int argc, char *argv[]) exit(status); } + /* use directory name as name */ + p = xbasename(repodir); + snprintf(name, sizeof(name), "%s", p); + free(p); + + /* read description or .git/description */ + snprintf(path, sizeof(path), "%s%s%s", + repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "description"); + if (!(fpread = fopen(path, "r+b"))) { + snprintf(path, sizeof(path), "%s%s%s", + repodir, repodir[strlen(repodir)] == '/' ? "" : "/", ".git/description"); + fpread = fopen(path, "r+b"); + } + if (fpread) { + if (!fgets(description, sizeof(description), fpread)) + description[0] = '\0'; + fclose(fpread); + } + + /* read LICENSE */ snprintf(path, sizeof(path), "%s%s%s", repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "LICENSE"); if ((fpread = fopen(path, "r+b"))) { @@ -239,6 +280,7 @@ main(int argc, char *argv[]) haslicense = 1; } + /* read README */ snprintf(path, sizeof(path), "%s%s%s", repodir, repodir[strlen(repodir)] == '/' ? "" : "/", "README"); if ((fpread = fopen(path, "r+b"))) { -- libgit2 1.1.1