Documentation: color.* = true means "auto"
[gitweb.git] / builtin-prune.c
index 7929af1aacd515bcc49bb79e9e3d8ee616397b4f..b5e768421ba548efaf0dd62ca876c15911df55d2 100644 (file)
@@ -5,25 +5,26 @@
 #include "builtin.h"
 #include "reachable.h"
 
-static const char prune_usage[] = "git-prune [-n] [--grace=time]";
+static const char prune_usage[] = "git-prune [-n]";
 static int show_only;
-static int prune_grace_period;
+static unsigned long expire;
 
 static int prune_object(char *path, const char *filename, const unsigned char *sha1)
 {
-       char buf[20];
-       const char *type;
-
-       if (show_only) {
-               if (sha1_object_info(sha1, buf, NULL))
-                       type = "unknown";
-               else
-                       type = buf;
-               printf("%s %s\n", sha1_to_hex(sha1), type);
-               return 0;
+       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;
        }
-       unlink(mkpath("%s/%s", path, filename));
-       rmdir(path);
+       if (show_only) {
+               enum object_type type = sha1_object_info(sha1, NULL);
+               printf("%s %s\n", sha1_to_hex(sha1),
+                      (type > 0) ? typename(type) : "unknown");
+       } else
+               unlink(fullpath);
        return 0;
 }
 
@@ -39,7 +40,6 @@ static int prune_dir(int i, char *path)
                char name[100];
                unsigned char sha1[20];
                int len = strlen(de->d_name);
-               struct stat st;
 
                switch (len) {
                case 2:
@@ -62,16 +62,13 @@ static int prune_dir(int i, char *path)
                        if (lookup_object(sha1))
                                continue;
 
-                       if (prune_grace_period > 0 &&
-                           !stat(mkpath("%s/%s", path, de->d_name), &st) &&
-                           st.st_mtime > prune_grace_period)
-                               continue;
-
                        prune_object(path, de->d_name, sha1);
                        continue;
                }
                fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
        }
+       if (!show_only)
+               rmdir(path);
        closedir(dir);
        return 0;
 }
@@ -86,25 +83,10 @@ static void prune_object_dir(const char *path)
        }
 }
 
-static int git_prune_config(const char *var, const char *value)
-{
-       if (!strcmp(var, "gc.prunegrace")) {
-               if (!strcmp(value, "off"))
-                       prune_grace_period = 0;
-               else
-                       prune_grace_period = approxidate(value);
-               return 0;
-       }
-       return git_default_config(var, value);
-}
-
 int cmd_prune(int argc, const char **argv, const char *prefix)
 {
        int i;
        struct rev_info revs;
-       prune_grace_period = time(NULL)-24*60*60;
-
-       git_config(git_prune_config);
 
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
@@ -112,11 +94,14 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
                        show_only = 1;
                        continue;
                }
-               if (!strncmp(arg, "--grace=", 8)) {
-                       if (!strcmp(arg+8, "off"))
-                               prune_grace_period = 0;
-                       else
-                               prune_grace_period = approxidate(arg+8);
+               if (!strcmp(arg, "--expire")) {
+                       if (++i < argc) {
+                               expire = approxidate(argv[i]);
+                               continue;
+                       }
+               }
+               else if (!prefixcmp(arg, "--expire=")) {
+                       expire = approxidate(arg + 9);
                        continue;
                }
                usage(prune_usage);