unpack-objects desperately salvages objects from a corrupt pack
The command unpack-objects dies upon the first error. This is probably considered a feature -- if a pack is corrupt, instead of trying to extract from it and possibly risking to contaminate a good repository with objects whose validity is dubious, we should seek a good copy of the pack and retry. However, we may not have any good copy anywhere. This implements the last resort effort to extract what are salvageable from such a corrupt pack. This flag might have helped Sergio when recovering from a corrupt pack. In my test, it managed to salvage 247 objects out of a pack that had 251 objects but without it the command stopped after extracting 73 objects. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 3, 2006 at 22:55 UTC
f986f2c830b24a0f56f2bf4b3f30353ca1e372a9
2 files changed
+45
-22
Documentation/git-unpack-objects.txt
+7
-1
@@ -8,7 +8,7 @@ git-unpack-objects - Unpack objects from a packed archive
8
9
SYNOPSIS
10
--------
11
-'git-unpack-objects' [-n] [-q] <pack-file
11
+'git-unpack-objects' [-n] [-q] [-r] <pack-file
12
13
14
DESCRIPTION
@@ -34,6 +34,12 @@ OPTIONS
34
The command usually shows percentage progress. This
35
flag suppresses it.
36
37
+-r::
38
+ When unpacking a corrupt packfile, the command dies at
39
+ the first corruption. This flag tells it to keep going
40
+ and make the best effort to salvage as many objects as
41
+ possible.
42
+
43
44
Author
45
------
builtin-unpack-objects.c
+38
-21
@@ -10,7 +10,7 @@
10
11
#include <sys/time.h>
12
13
-static int dry_run, quiet;
13
+static int dry_run, quiet, desperate, has_errors;
14
static const char unpack_usage[] = "git-unpack-objects [-n] [-q] < pack-file";
15
16
/* We always read in 4kB chunks. */
@@ -71,8 +71,15 @@ static void *get_data(unsigned long size)
71
use(len - stream.avail_in);
72
if (stream.total_out == size && ret == Z_STREAM_END)
73
break;
74
- if (ret != Z_OK)
75
- die("inflate returned %d\n", ret);
74
+ if (ret != Z_OK) {
75
+ error("inflate returned %d\n", ret);
76
+ free(buf);
77
+ buf = NULL;
78
+ if (!desperate)
79
+ exit(1);
80
+ has_errors = 1;
81
+ break;
82
+ }
83
stream.next_in = fill(1);
84
stream.avail_in = len;
85
}
@@ -110,9 +117,9 @@ static void write_object(void *buf, unsigned long size, const char *type)
117
added_object(sha1, type, buf, size);
118
}
119
113
-static int resolve_delta(const char *type,
114
- void *base, unsigned long base_size,
115
- void *delta, unsigned long delta_size)
120
+static void resolve_delta(const char *type,
121
+ void *base, unsigned long base_size,
122
+ void *delta, unsigned long delta_size)
123
{
124
void *result;
125
unsigned long result_size;
@@ -125,7 +132,6 @@ static int resolve_delta(const char *type,
132
free(delta);
133
write_object(result, result_size, type);
134
free(result);
128
- return 0;
135
}
136
137
static void added_object(unsigned char *sha1, const char *type, void *data, unsigned long size)
@@ -145,7 +151,7 @@ static void added_object(unsigned char *sha1, const char *type, void *data, unsi
151
}
152
}
153
148
-static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
154
+static void unpack_non_delta_entry(enum object_type kind, unsigned long size)
155
{
156
void *buf = get_data(size);
157
const char *type;
@@ -157,39 +163,42 @@ static int unpack_non_delta_entry(enum object_type kind, unsigned long size)
163
case OBJ_TAG: type = tag_type; break;
164
default: die("bad type %d", kind);
165
}
160
- if (!dry_run)
166
+ if (!dry_run && buf)
167
write_object(buf, size, type);
168
free(buf);
163
- return 0;
169
}
170
166
-static int unpack_delta_entry(unsigned long delta_size)
171
+static void unpack_delta_entry(unsigned long delta_size)
172
{
173
void *delta_data, *base;
174
unsigned long base_size;
175
char type[20];
176
unsigned char base_sha1[20];
172
- int result;
177
178
hashcpy(base_sha1, fill(20));
179
use(20);
180
181
delta_data = get_data(delta_size);
178
- if (dry_run) {
182
+ if (dry_run || !delta_data) {
183
free(delta_data);
180
- return 0;
184
+ return;
185
}
186
187
if (!has_sha1_file(base_sha1)) {
188
add_delta_to_list(base_sha1, delta_data, delta_size);
185
- return 0;
189
+ return;
190
}
191
base = read_sha1_file(base_sha1, type, &base_size);
188
- if (!base)
189
- die("failed to read delta-pack base object %s", sha1_to_hex(base_sha1));
190
- result = resolve_delta(type, base, base_size, delta_data, delta_size);
192
+ if (!base) {
193
+ error("failed to read delta-pack base object %s",
194
+ sha1_to_hex(base_sha1));
195
+ if (!desperate)
196
+ exit(1);
197
+ has_errors = 1;
198
+ return;
199
+ }
200
+ resolve_delta(type, base, base_size, delta_data, delta_size);
201
free(base);
192
- return result;
202
}
203
204
static void unpack_one(unsigned nr, unsigned total)
@@ -236,7 +245,11 @@ static void unpack_one(unsigned nr, unsigned total)
245
unpack_delta_entry(size);
246
return;
247
default:
239
- die("bad object type %d", type);
248
+ error("bad object type %d", type);
249
+ has_errors = 1;
250
+ if (desperate)
251
+ return;
252
+ exit(1);
253
}
254
}
255
@@ -280,6 +293,10 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
293
quiet = 1;
294
continue;
295
}
296
+ if (!strcmp(arg, "-r")) {
297
+ desperate = 1;
298
+ continue;
299
+ }
300
usage(unpack_usage);
301
}
302
@@ -306,5 +323,5 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
323
/* All done */
324
if (!quiet)
325
fprintf(stderr, "\n");
309
- return 0;
326
+ return has_errors;
327
}