builtin-help: make some internal functions available to other builtins
[gitweb.git] / builtin-prune.c
index bd3d2f67f3eb54cedb1a6c18582c7e10aa3f7bc6..947de8cf258c73d8a327ef3a1daed417ba533f1b 100644 (file)
@@ -7,12 +7,28 @@
 #include "parse-options.h"
 
 static const char * const prune_usage[] = {
-       "git-prune [-n] [--expire <time>] [--] [<head>...]",
+       "git prune [-n] [--expire <time>] [--] [<head>...]",
        NULL
 };
 static int show_only;
 static unsigned long expire;
 
+static int prune_tmp_object(char *path, const char *filename)
+{
+       const char *fullpath = mkpath("%s/%s", path, filename);
+       if (expire) {
+               struct stat st;
+               if (lstat(fullpath, &st))
+                       return error("Could not stat '%s'", fullpath);
+               if (st.st_mtime > expire)
+                       return 0;
+       }
+       printf("Removing stale temporary file %s\n", fullpath);
+       if (!show_only)
+               unlink(fullpath);
+       return 0;
+}
+
 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
 {
        const char *fullpath = mkpath("%s/%s", path, filename);
@@ -53,6 +69,11 @@ static int prune_dir(int i, char *path)
                        if (de->d_name[0] != '.')
                                break;
                        continue;
+               case 14:
+                       if (prefixcmp(de->d_name, "tmp_obj_"))
+                               break;
+                       prune_tmp_object(path, de->d_name);
+                       continue;
                case 38:
                        sprintf(name, "%02x", i);
                        memcpy(name+2, de->d_name, len+1);
@@ -105,23 +126,9 @@ static void remove_temporary_files(void)
                        dirname);
                return;
        }
-       while ((de = readdir(dir)) != NULL) {
-               if (!prefixcmp(de->d_name, "tmp_")) {
-                       char name[PATH_MAX];
-                       int c = snprintf(name, PATH_MAX, "%s/%s",
-                                        dirname, de->d_name);
-                       if (c < 0 || c >= PATH_MAX)
-                               continue;
-                       if (expire) {
-                               struct stat st;
-                               if (stat(name, &st) != 0 || st.st_mtime >= expire)
-                                       continue;
-                       }
-                       printf("Removing stale temporary file %s\n", name);
-                       if (!show_only)
-                               unlink(name);
-               }
-       }
+       while ((de = readdir(dir)) != NULL)
+               if (!prefixcmp(de->d_name, "tmp_"))
+                       prune_tmp_object(dirname, de->d_name);
        closedir(dir);
 }