]> git.armaanb.net Git - stagit.git/commitdiff
resolve absolute paths to repodir, remove basename just use strrchr.
authorHiltjo Posthuma <hiltjo@codemadness.org>
Wed, 27 Apr 2016 17:19:50 +0000 (19:19 +0200)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Wed, 27 Apr 2016 17:19:50 +0000 (19:19 +0200)
- resolve repodir, for example: stagit-index ../ used to use ".." as the name,
  now it will resolve to the real directory name.
- just use strrchr(path, '/') instead of basename, '/' path separator is now
  used.

stagit-index.c
stagit.c

index 4faa264e6d6b2e322db1397cd733fd2d066662f0..e2cac598910bf687a314ba14e38d66d452e02b34 100644 (file)
@@ -3,7 +3,6 @@
 #include <err.h>
 #include <errno.h>
 #include <inttypes.h>
-#include <libgen.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -21,7 +20,7 @@ static const char *relpath = "";
 static const char *repodir;
 
 static char description[255] = "Repositories";
-static char name[255];
+static char *name = "";
 static char owner[255];
 
 /* Escape characters below as HTML 2.0 / XML 1.0. */
@@ -42,27 +41,6 @@ xmlencode(FILE *fp, const char *s, size_t len)
        }
 }
 
-/* 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;
-}
-
 void
 printtimeformat(FILE *fp, const git_time *intime, const char *fmt)
 {
@@ -166,7 +144,7 @@ main(int argc, char *argv[])
 {
        const git_error *e = NULL;
        FILE *fp;
-       char path[PATH_MAX], *p;
+       char path[PATH_MAX], repodirabs[PATH_MAX + 1];
        int i, r, ret = 0;
 
        if (argc < 2) {
@@ -179,6 +157,8 @@ main(int argc, char *argv[])
 
        for (i = 1; i < argc; i++) {
                repodir = argv[i];
+               if (!realpath(repodir, repodirabs))
+                       err(1, "realpath");
 
                if (git_repository_open_ext(&repo, repodir,
                    GIT_REPOSITORY_OPEN_NO_SEARCH, NULL)) {
@@ -188,10 +168,11 @@ main(int argc, char *argv[])
                        continue;
                }
 
-               /* use directory name as name, truncation of name is no problem. */
-               p = xbasename(repodir);
-               snprintf(name, sizeof(name), "%s", p);
-               free(p);
+               /* use directory name as name */
+               if ((name = strrchr(repodirabs, '/')))
+                       name++;
+               else
+                       name = "";
 
                /* read description or .git/description */
                description[0] = '\0';
index e3c921d3bef5d51c164d1faaf3df3c64cf02f741..cbb0a7790e2851885ca9833b2c929a7763414c8b 100644 (file)
--- a/stagit.c
+++ b/stagit.c
@@ -42,7 +42,7 @@ static git_repository *repo;
 static const char *relpath = "";
 static const char *repodir;
 
-static char *name;
+static char *name = "";
 static char *stripped_name;
 static char description[255];
 static char cloneurl[1024];
@@ -162,27 +162,6 @@ xdirname(const char *path)
        return b;
 }
 
-/* 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;
-}
-
 int
 mkdirp(const char *path)
 {
@@ -879,7 +858,7 @@ main(int argc, char *argv[])
        const git_oid *head = NULL;
        const git_error *e = NULL;
        FILE *fp, *fpread;
-       char path[PATH_MAX], *p;
+       char path[PATH_MAX], repodirabs[PATH_MAX + 1], *p;
        int r, status;
 
        if (argc != 2) {
@@ -887,6 +866,8 @@ main(int argc, char *argv[])
                return 1;
        }
        repodir = argv[1];
+       if (!realpath(repodir, repodirabs))
+               err(1, "realpath");
 
        git_libgit2_init();
 
@@ -904,7 +885,10 @@ main(int argc, char *argv[])
        git_object_free(obj);
 
        /* use directory name as name */
-       name = xbasename(repodir);
+       if ((name = strrchr(repodirabs, '/')))
+               name++;
+       else
+               name = "";
 
        /* strip .git suffix */
        if (!(stripped_name = strdup(name)))