fsck: git receive-pack: support excluding objects from fsck'ing

The optional new config option `receive.fsck.skipList` specifies the path to a file listing the names, i.e. SHA-1s, one per line, of objects that are to be ignored by `git receive-pack` when `receive.fsckObjects = true`. This is extremely handy in case of legacy repositories where it would cause more pain to change incorrect objects than to live with them (e.g. a duplicate 'author' line in an early commit object). The intended use case is for server administrators to inspect objects that are reported by `git push` as being too problematic to enter the repository, and to add the objects' SHA-1 to a (preferably sorted) file when the objects are legitimate, i.e. when it is determined that those problematic objects should be allowed to enter the server. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jun 22, 2015 at 17:27 UTC cd94c6f91ee92ffb0592696170fa84f426b839ab
5 files changed +82
Documentation/config.txt
+8
@@ -2222,6 +2222,14 @@ which would not pass pushing when `receive.fsckObjects = true`, allowing
2222 the host to accept repositories with certain known issues but still catch
2223 other issues.
2224
2225 +receive.fsck.skipList::
2226 + The path to a sorted list of object names (i.e. one SHA-1 per
2227 + line) that are known to be broken in a non-fatal way and should
2228 + be ignored. This feature is useful when an established project
2229 + should be accepted despite early commits containing errors that
2230 + can be safely ignored such as invalid committer email addresses.
2231 + Note: corrupt objects cannot be skipped with this setting.
2232 +
2233 receive.unpackLimit::
2234 If the number of objects received in a push is below this
2235 limit then the objects will be unpacked into loose object
builtin/receive-pack.c
+11
@@ -117,6 +117,17 @@ static int receive_pack_config(const char *var, const char *value, void *cb)
117 return 0;
118 }
119
120 + if (strcmp(var, "receive.fsck.skiplist") == 0) {
121 + const char *path;
122 +
123 + if (git_config_pathname(&path, var, value))
124 + return 1;
125 + strbuf_addf(&fsck_msg_types, "%cskiplist=%s",
126 + fsck_msg_types.len ? ',' : '=', path);
127 + free((char *)path);
128 + return 0;
129 + }
130 +
131 if (skip_prefix(var, "receive.fsck.", &var)) {
132 if (is_valid_msg_type(var, value))
133 strbuf_addf(&fsck_msg_types, "%c%s=%s",
fsck.c
+50
@@ -8,6 +8,7 @@
8 #include "fsck.h"
9 #include "refs.h"
10 #include "utf8.h"
11 +#include "sha1-array.h"
12
13 #define FSCK_FATAL -1
14 #define FSCK_INFO -2
@@ -127,6 +128,43 @@ static int fsck_msg_type(enum fsck_msg_id msg_id,
128 return msg_type;
129 }
130
131 +static void init_skiplist(struct fsck_options *options, const char *path)
132 +{
133 + static struct sha1_array skiplist = SHA1_ARRAY_INIT;
134 + int sorted, fd;
135 + char buffer[41];
136 + unsigned char sha1[20];
137 +
138 + if (options->skiplist)
139 + sorted = options->skiplist->sorted;
140 + else {
141 + sorted = 1;
142 + options->skiplist = &skiplist;
143 + }
144 +
145 + fd = open(path, O_RDONLY);
146 + if (fd < 0)
147 + die("Could not open skip list: %s", path);
148 + for (;;) {
149 + int result = read_in_full(fd, buffer, sizeof(buffer));
150 + if (result < 0)
151 + die_errno("Could not read '%s'", path);
152 + if (!result)
153 + break;
154 + if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
155 + die("Invalid SHA-1: %s", buffer);
156 + sha1_array_append(&skiplist, sha1);
157 + if (sorted && skiplist.nr > 1 &&
158 + hashcmp(skiplist.sha1[skiplist.nr - 2],
159 + sha1) > 0)
160 + sorted = 0;
161 + }
162 + close(fd);
163 +
164 + if (sorted)
165 + skiplist.sorted = 1;
166 +}
167 +
168 static int parse_msg_type(const char *str)
169 {
170 if (!strcmp(str, "error"))
@@ -191,6 +229,14 @@ void fsck_set_msg_types(struct fsck_options *options, const char *values)
229 buf[equal] = tolower(buf[equal]);
230 buf[equal] = '\0';
231
232 + if (!strcmp(buf, "skiplist")) {
233 + if (equal == len)
234 + die("skiplist requires a path");
235 + init_skiplist(options, buf + equal + 1);
236 + buf += len + 1;
237 + continue;
238 + }
239 +
240 if (equal == len)
241 die("Missing '=': '%s'", buf);
242
@@ -229,6 +275,10 @@ static int report(struct fsck_options *options, struct object *object,
275 if (msg_type == FSCK_IGNORE)
276 return 0;
277
278 + if (options->skiplist && object &&
279 + sha1_array_lookup(options->skiplist, object->sha1) >= 0)
280 + return 0;
281 +
282 if (msg_type == FSCK_FATAL)
283 msg_type = FSCK_ERROR;
284 else if (msg_type == FSCK_INFO)
fsck.h
+1
@@ -32,6 +32,7 @@ struct fsck_options {
32 fsck_error error_func;
33 unsigned strict:1;
34 int *msg_type;
35 + struct sha1_array *skiplist;
36 };
37
38 #define FSCK_OPTIONS_DEFAULT { NULL, fsck_error_function, 0, NULL }
t/t5504-fetch-receive-strict.sh
+12
@@ -123,6 +123,18 @@ committer Bugs Bunny <bugs@bun.ni> 1234567890 +0000
123 This commit object intentionally broken
124 EOF
125
126 +test_expect_success 'push with receive.fsck.skipList' '
127 + commit="$(git hash-object -t commit -w --stdin <bogus-commit)" &&
128 + git push . $commit:refs/heads/bogus &&
129 + rm -rf dst &&
130 + git init dst &&
131 + git --git-dir=dst/.git config receive.fsckObjects true &&
132 + test_must_fail git push --porcelain dst bogus &&
133 + git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
134 + echo $commit >dst/.git/SKIP &&
135 + git push --porcelain dst bogus
136 +'
137 +
138 test_expect_success 'push with receive.fsck.missingEmail=warn' '
139 commit="$(git hash-object -t commit -w --stdin <bogus-commit)" &&
140 git push . $commit:refs/heads/bogus &&