diff --git a/builtin-fsck.c b/builtin-fsck.c
index 44ce629a498f986f239e008a61d32e10968907e0..e4874f64a3742b0b54ba2c25f398bece41a5015e 100644 (file)
--- a/builtin-fsck.c
+++ b/builtin-fsck.c
+#include "builtin.h"
#include "cache.h"
#include "commit.h"
#include "tree.h"
#include "cache.h"
#include "commit.h"
#include "tree.h"
#include "pack.h"
#include "cache-tree.h"
#include "tree-walk.h"
#include "pack.h"
#include "cache-tree.h"
#include "tree-walk.h"
+#include "parse-options.h"
#define REACHABLE 0x0001
#define SEEN 0x0002
#define REACHABLE 0x0001
#define SEEN 0x0002
static int keep_cache_objects;
static unsigned char head_sha1[20];
static int errors_found;
static int keep_cache_objects;
static unsigned char head_sha1[20];
static int errors_found;
+static int write_lost_and_found;
+static int verbose;
#define ERROR_OBJECT 01
#define ERROR_REACHABLE 02
#define ERROR_OBJECT 01
#define ERROR_REACHABLE 02
if (!obj->used) {
printf("dangling %s %s\n", typename(obj->type),
sha1_to_hex(obj->sha1));
if (!obj->used) {
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",
+ obj->type == OBJ_COMMIT ? "commit" : "other",
+ sha1_to_hex(obj->sha1));
+ FILE *f;
+
+ if (safe_create_leading_directories(filename)) {
+ error("Could not create lost-found");
+ return;
+ }
+ if (!(f = fopen(filename, "w")))
+ die("Could not open %s", filename);
+ if (obj->type == OBJ_BLOB) {
+ enum object_type type;
+ unsigned long size;
+ char *buf = read_sha1_file(obj->sha1,
+ &type, &size);
+ if (buf) {
+ fwrite(buf, size, 1, f);
+ free(buf);
+ }
+ } else
+ fprintf(f, "%s\n", sha1_to_hex(obj->sha1));
+ fclose(f);
+ }
return;
}
return;
}
static void check_object(struct object *obj)
{
static void check_object(struct object *obj)
{
+ if (verbose)
+ fprintf(stderr, "Checking %s\n", sha1_to_hex(obj->sha1));
+
if (obj->flags & REACHABLE)
check_reachable_object(obj);
else
if (obj->flags & REACHABLE)
check_reachable_object(obj);
else
/* Look up all the requirements, warn about missing objects.. */
max = get_max_object_index();
/* Look up all the requirements, warn about missing objects.. */
max = get_max_object_index();
+ if (verbose)
+ fprintf(stderr, "Checking connectivity (%d objects)\n", max);
+
for (i = 0; i < max; i++) {
struct object *obj = get_indexed_object(i);
for (i = 0; i < max; i++) {
struct object *obj = get_indexed_object(i);
const char *o_name;
const unsigned char *o_sha1;
const char *o_name;
const unsigned char *o_sha1;
+ if (verbose)
+ fprintf(stderr, "Checking tree %s\n",
+ sha1_to_hex(item->object.sha1));
+
init_tree_desc(&desc, item->buffer, item->size);
o_mode = 0;
init_tree_desc(&desc, item->buffer, item->size);
o_mode = 0;
case S_IFREG | 0644:
case S_IFLNK:
case S_IFDIR:
case S_IFREG | 0644:
case S_IFLNK:
case S_IFDIR:
- case S_IFDIRLNK:
+ case S_IFGITLINK:
break;
/*
* This is nonstandard, but we had a few of these
break;
/*
* This is nonstandard, but we had a few of these
char *buffer = commit->buffer;
unsigned char tree_sha1[20], sha1[20];
char *buffer = commit->buffer;
unsigned char tree_sha1[20], sha1[20];
+ if (verbose)
+ fprintf(stderr, "Checking commit %s\n",
+ sha1_to_hex(commit->object.sha1));
+
if (memcmp(buffer, "tree ", 5))
return objerror(&commit->object, "invalid format - expected 'tree' line");
if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
if (memcmp(buffer, "tree ", 5))
return objerror(&commit->object, "invalid format - expected 'tree' line");
if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
if (!commit->parents && show_root)
printf("root %s\n", sha1_to_hex(commit->object.sha1));
if (!commit->date)
if (!commit->parents && show_root)
printf("root %s\n", sha1_to_hex(commit->object.sha1));
if (!commit->date)
- printf("bad commit date in %s\n",
+ printf("bad commit date in %s\n",
sha1_to_hex(commit->object.sha1));
return 0;
}
sha1_to_hex(commit->object.sha1));
return 0;
}
{
struct object *tagged = tag->tagged;
{
struct object *tagged = tag->tagged;
+ if (verbose)
+ fprintf(stderr, "Checking tag %s\n",
+ sha1_to_hex(tag->object.sha1));
+
if (!tagged) {
return objerror(&tag->object, "could not load tagged object");
}
if (!tagged) {
return objerror(&tag->object, "could not load tagged object");
}
if (!dir)
return;
if (!dir)
return;
+ if (verbose)
+ fprintf(stderr, "Checking directory %s\n", path);
+
while ((de = readdir(dir)) != NULL) {
char name[100];
unsigned char sha1[20];
while ((de = readdir(dir)) != NULL) {
char name[100];
unsigned char sha1[20];
{
struct object *obj;
{
struct object *obj;
+ 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) {
if (!is_null_sha1(osha1)) {
obj = lookup_object(osha1);
if (obj) {
static void fsck_object_dir(const char *path)
{
int i;
static void fsck_object_dir(const char *path)
{
int i;
+
+ if (verbose)
+ fprintf(stderr, "Checking object directory\n");
+
for (i = 0; i < 256; i++) {
static char dir[4096];
sprintf(dir, "%s/%02x", path, i);
for (i = 0; i < 256; i++) {
static char dir[4096];
sprintf(dir, "%s/%02x", path, i);
int null_is_error = 0;
const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
int null_is_error = 0;
const char *head_points_at = resolve_ref("HEAD", sha1, 0, &flag);
+ if (verbose)
+ fprintf(stderr, "Checking HEAD link\n");
+
if (!head_points_at)
return error("Invalid HEAD");
if (!strcmp(head_points_at, "HEAD"))
if (!head_points_at)
return error("Invalid HEAD");
if (!strcmp(head_points_at, "HEAD"))
int i;
int err = 0;
int i;
int err = 0;
+ if (verbose)
+ fprintf(stderr, "Checking cache tree\n");
+
if (0 <= it->entry_count) {
struct object *obj = parse_object(it->sha1);
if (!obj) {
if (0 <= it->entry_count) {
struct object *obj = parse_object(it->sha1);
if (!obj) {
return err;
}
return err;
}
-static const char fsck_usage[] =
-"git-fsck [--tags] [--root] [[--unreachable] [--cache] [--full] "
-"[--strict] <head-sha1>*]";
+static char const * const fsck_usage[] = {
+ "git-fsck [options] [<object>...]",
+ NULL
+};
-int cmd_fsck(int argc, char **argv, const char *prefix)
+static struct option fsck_opts[] = {
+ OPT__VERBOSE(&verbose),
+ OPT_BOOLEAN(0, "unreachable", &show_unreachable, "show unreachable objects"),
+ OPT_BOOLEAN(0, "tags", &show_tags, "report tags"),
+ OPT_BOOLEAN(0, "root", &show_root, "report root nodes"),
+ OPT_BOOLEAN(0, "cache", &keep_cache_objects, "make index objects head nodes"),
+ OPT_BOOLEAN(0, "reflogs", &include_reflogs, "make reflogs head nodes (default)"),
+ OPT_BOOLEAN(0, "full", &check_full, "also consider alternate objects"),
+ OPT_BOOLEAN(0, "strict", &check_strict, "enable more strict checking"),
+ OPT_BOOLEAN(0, "lost-found", &write_lost_and_found,
+ "write dangling objects in .git/lost-found"),
+ OPT_END(),
+};
+
+int cmd_fsck(int argc, const char **argv, const char *prefix)
{
int i, heads;
track_object_refs = 1;
errors_found = 0;
{
int i, heads;
track_object_refs = 1;
errors_found = 0;
- for (i = 1; i < argc; i++) {
- const char *arg = argv[i];
-
- if (!strcmp(arg, "--unreachable")) {
- show_unreachable = 1;
- continue;
- }
- if (!strcmp(arg, "--tags")) {
- show_tags = 1;
- continue;
- }
- if (!strcmp(arg, "--root")) {
- show_root = 1;
- continue;
- }
- if (!strcmp(arg, "--cache")) {
- keep_cache_objects = 1;
- continue;
- }
- if (!strcmp(arg, "--no-reflogs")) {
- include_reflogs = 0;
- continue;
- }
- if (!strcmp(arg, "--full")) {
- check_full = 1;
- continue;
- }
- if (!strcmp(arg, "--strict")) {
- check_strict = 1;
- continue;
- }
- if (*arg == '-')
- usage(fsck_usage);
+ argc = parse_options(argc, argv, fsck_opts, fsck_usage, 0);
+ if (write_lost_and_found) {
+ check_full = 1;
+ include_reflogs = 0;
}
fsck_head_link();
}
fsck_head_link();
verify_pack(p, 0);
for (p = packed_git; p; p = p->next) {
verify_pack(p, 0);
for (p = packed_git; p; p = p->next) {
- uint32_t i, num = p->num_objects;
- for (i = 0; i < num; i++)
- fsck_sha1(nth_packed_object_sha1(p, i));
+ uint32_t j, num;
+ if (open_pack_index(p))
+ continue;
+ num = p->num_objects;
+ for (j = 0; j < num; j++)
+ fsck_sha1(nth_packed_object_sha1(p, j));
}
}
heads = 0;
for (i = 1; i < argc; i++) {
}
}
heads = 0;
for (i = 1; i < argc; i++) {
- const char *arg = argv[i];
-
- if (*arg == '-')
- continue;
-
+ const char *arg = argv[i];
if (!get_sha1(arg, head_sha1)) {
struct object *obj = lookup_object(head_sha1);
if (!get_sha1(arg, head_sha1)) {
struct object *obj = lookup_object(head_sha1);
}
if (keep_cache_objects) {
}
if (keep_cache_objects) {
- int i;
read_cache();
for (i = 0; i < active_nr; i++) {
unsigned int mode;
read_cache();
for (i = 0; i < active_nr; i++) {
unsigned int mode;
struct object *obj;
mode = ntohl(active_cache[i]->ce_mode);
struct object *obj;
mode = ntohl(active_cache[i]->ce_mode);
- if (S_ISDIRLNK(mode))
+ if (S_ISGITLINK(mode))
continue;
blob = lookup_blob(active_cache[i]->sha1);
if (!blob)
continue;
blob = lookup_blob(active_cache[i]->sha1);
if (!blob)