]> asedeno.scripts.mit.edu Git - git.git/blobdiff - fsck-objects.c
Protect commits recorded in reflog from pruning.
[git.git] / fsck-objects.c
index 456c17e2f6c2d226248156065b1a47dad487c402..1cc3b399bcf2b8ef875ded8dfcff5aef0f345811 100644 (file)
@@ -1,6 +1,3 @@
-#include <sys/types.h>
-#include <dirent.h>
-
 #include "cache.h"
 #include "commit.h"
 #include "tree.h"
@@ -402,7 +399,26 @@ static void fsck_dir(int i, char *path)
 
 static int default_refs;
 
-static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
+static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1, char *datail, void *cb_data)
+{
+       struct object *obj;
+
+       if (!is_null_sha1(osha1)) {
+               obj = lookup_object(osha1);
+               if (obj) {
+                       obj->used = 1;
+                       mark_reachable(obj, REACHABLE);
+               }
+       }
+       obj = lookup_object(nsha1);
+       if (obj) {
+               obj->used = 1;
+               mark_reachable(obj, REACHABLE);
+       }
+       return 0;
+}
+
+static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 {
        struct object *obj;
 
@@ -419,12 +435,15 @@ static int fsck_handle_ref(const char *refname, const unsigned char *sha1)
        default_refs++;
        obj->used = 1;
        mark_reachable(obj, REACHABLE);
+
+       for_each_reflog_ent(refname, fsck_handle_reflog_ent, NULL);
+
        return 0;
 }
 
 static void get_default_heads(void)
 {
-       for_each_ref(fsck_handle_ref);
+       for_each_ref(fsck_handle_ref, NULL);
 
        /*
         * Not having any default heads isn't really fatal, but
@@ -458,9 +477,10 @@ static void fsck_object_dir(const char *path)
 static int fsck_head_link(void)
 {
        unsigned char sha1[20];
-       const char *head_points_at = resolve_ref("HEAD", sha1, 1);
+       int flag;
+       const char *head_points_at = resolve_ref("HEAD", sha1, 1, &flag);
 
-       if (!head_points_at)
+       if (!head_points_at || !(flag & REF_ISSYMREF))
                return error("HEAD is not a symbolic ref");
        if (strncmp(head_points_at, "refs/heads/", 11))
                return error("HEAD points to something strange (%s)",