Merge branch 'mh/fsck-reflog-entries'
authorJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2015 19:21:48 +0000 (12:21 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jun 2015 19:21:48 +0000 (12:21 -0700)
"git fsck" used to ignore missing or invalid objects recorded in reflog.

* mh/fsck-reflog-entries:
fsck: report errors if reflog entries point at invalid objects
fsck_handle_reflog_sha1(): new function

1  2 
builtin/fsck.c
diff --combined builtin/fsck.c
index 4e8e2ee5b739379e387c3875a1d71de8a22a6db6,6b6f31997ccb8b00d4bb7f1a41fb831238ee7554..267979304994158c491b2306388b932c50d7438b
@@@ -25,7 -25,7 +25,7 @@@ static int include_reflogs = 1
  static int check_full = 1;
  static int check_strict;
  static int keep_cache_objects;
 -static unsigned char head_sha1[20];
 +static struct object_id head_oid;
  static const char *head_points_at;
  static int errors_found;
  static int write_lost_and_found;
@@@ -225,12 -225,12 +225,12 @@@ static void check_unreachable_object(st
                        printf("dangling %s %s\n", typename(obj->type),
                               sha1_to_hex(obj->sha1));
                if (write_lost_and_found) {
 -                      char *filename = git_path("lost-found/%s/%s",
 +                      const char *filename = git_path("lost-found/%s/%s",
                                obj->type == OBJ_COMMIT ? "commit" : "other",
                                sha1_to_hex(obj->sha1));
                        FILE *f;
  
 -                      if (safe_create_leading_directories(filename)) {
 +                      if (safe_create_leading_directories_const(filename)) {
                                error("Could not create lost-found");
                                return;
                        }
@@@ -451,46 -451,50 +451,52 @@@ static void fsck_dir(int i, char *path
  
  static int default_refs;
  
+ static void fsck_handle_reflog_sha1(const char *refname, unsigned char *sha1)
+ {
+       struct object *obj;
+       if (!is_null_sha1(sha1)) {
+               obj = lookup_object(sha1);
+               if (obj) {
+                       obj->used = 1;
+                       mark_object_reachable(obj);
+               } else {
+                       error("%s: invalid reflog entry %s", refname, sha1_to_hex(sha1));
+                       errors_found |= ERROR_REACHABLE;
+               }
+       }
+ }
  static int fsck_handle_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
                const char *email, unsigned long timestamp, int tz,
                const char *message, void *cb_data)
  {
-       struct object *obj;
+       const char *refname = cb_data;
  
        if (verbose)
                fprintf(stderr, "Checking reflog %s->%s\n",
                        sha1_to_hex(osha1), sha1_to_hex(nsha1));
  
-       if (!is_null_sha1(osha1)) {
-               obj = lookup_object(osha1);
-               if (obj) {
-                       obj->used = 1;
-                       mark_object_reachable(obj);
-               }
-       }
-       obj = lookup_object(nsha1);
-       if (obj) {
-               obj->used = 1;
-               mark_object_reachable(obj);
-       }
+       fsck_handle_reflog_sha1(refname, osha1);
+       fsck_handle_reflog_sha1(refname, nsha1);
        return 0;
  }
  
 -static int fsck_handle_reflog(const char *logname, const unsigned char *sha1, int flag, void *cb_data)
 +static int fsck_handle_reflog(const char *logname, const struct object_id *oid,
 +                            int flag, void *cb_data)
  {
-       for_each_reflog_ent(logname, fsck_handle_reflog_ent, NULL);
+       for_each_reflog_ent(logname, fsck_handle_reflog_ent, (void *)logname);
        return 0;
  }
  
 -static int fsck_handle_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 +static int fsck_handle_ref(const char *refname, const struct object_id *oid,
 +                         int flag, void *cb_data)
  {
        struct object *obj;
  
 -      obj = parse_object(sha1);
 +      obj = parse_object(oid->hash);
        if (!obj) {
 -              error("%s: invalid sha1 pointer %s", refname, sha1_to_hex(sha1));
 +              error("%s: invalid sha1 pointer %s", refname, oid_to_hex(oid));
                errors_found |= ERROR_REACHABLE;
                /* We'll continue with the rest despite the error.. */
                return 0;
  
  static void get_default_heads(void)
  {
 -      if (head_points_at && !is_null_sha1(head_sha1))
 -              fsck_handle_ref("HEAD", head_sha1, 0, NULL);
 +      if (head_points_at && !is_null_oid(&head_oid))
 +              fsck_handle_ref("HEAD", &head_oid, 0, NULL);
        for_each_rawref(fsck_handle_ref, NULL);
        if (include_reflogs)
                for_each_reflog(fsck_handle_reflog, NULL);
@@@ -558,7 -562,7 +564,7 @@@ static int fsck_head_link(void
        if (verbose)
                fprintf(stderr, "Checking HEAD link\n");
  
 -      head_points_at = resolve_ref_unsafe("HEAD", 0, head_sha1, &flag);
 +      head_points_at = resolve_ref_unsafe("HEAD", 0, head_oid.hash, &flag);
        if (!head_points_at)
                return error("Invalid HEAD");
        if (!strcmp(head_points_at, "HEAD"))
        else if (!starts_with(head_points_at, "refs/heads/"))
                return error("HEAD points to something strange (%s)",
                             head_points_at);
 -      if (is_null_sha1(head_sha1)) {
 +      if (is_null_oid(&head_oid)) {
                if (null_is_error)
                        return error("HEAD: detached HEAD points at nothing");
                fprintf(stderr, "notice: HEAD points to an unborn branch (%s)\n",