@matheusbsbs / matheus-bagliano / commits / 7073e69e38
Fork 0

Don't do non-fastforward updates in fast-import.

If fast-import is being used to update an existing branch of a repository, the user may not want to lose commits if another process updates the same ref at the same time. For example, the user might be using fast-import to make just one or two commits against a live branch. We now perform a fast-forward check during the ref updating process. If updating a branch would cause commits in that branch to be lost, we skip over it and display the new SHA1 to standard error. This new default behavior can be overridden with `--force`, like git-push and git-fetch. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Feb 6, 2007 at 16:08 UTC 7073e69e382bc8247c28859d8b0eda2612cd6b94
3 files changed +143 -17
Documentation/git-fast-import.txt
+17 -5
@@ -38,6 +38,11 @@ OPTIONS
38 See ``Date Formats'' below for details about which formats
39 are supported, and their syntax.
40
41 +--force::
42 + Force updating modified existing branches, even if doing
43 + so would cause commits to be lost (as the new commit does
44 + not contain the old commit).
45 +
46 --max-pack-size=<n>::
47 Maximum size of each output packfile, expressed in MiB.
48 The default is 4096 (4 GiB) as that is the maximum allowed
@@ -92,11 +97,18 @@ run alongside parallel `git repack -a -d` or `git gc` invocations,
97 or any other Git operation (including `git prune`, as loose objects
98 are never used by gfi).
99
95 -However, gfi does not lock the branch or tag refs it is actively
96 -importing. After EOF, during its ref update phase, gfi blindly
97 -overwrites each imported branch or tag ref. Consequently it is not
98 -safe to modify refs that are currently being used by a running gfi
99 -instance, as work could be lost when gfi overwrites the refs.
100 +gfi does not lock the branch or tag refs it is actively importing.
101 +After the import, during its ref update phase, gfi tests each
102 +existing branch ref to verify the update will be a fast-forward
103 +update (the commit stored in the ref is contained in the new
104 +history of the commit to be written). If the update is not a
105 +fast-forward update, gfi will skip updating that ref and instead
106 +prints a warning message. gfi will always attempt to update all
107 +branch refs, and does not stop on the first failure.
108 +
109 +Branch updates can be forced with `--force`, but its recommended that
110 +this only be used on an otherwise quiet repository. Using `--force`
111 +is not necessary for an initial import into an empty repository.
112
113
114 Technical Discussion
fast-import.c
+46 -12
@@ -121,6 +121,7 @@ Format of STDIN stream:
121 #include "object.h"
122 #include "blob.h"
123 #include "tree.h"
124 +#include "commit.h"
125 #include "delta.h"
126 #include "pack.h"
127 #include "refs.h"
@@ -247,6 +248,7 @@ typedef enum {
248 /* Configured limits on output */
249 static unsigned long max_depth = 10;
250 static unsigned long max_packsize = (1LL << 32) - 1;
251 +static int force_update;
252
253 /* Stats and misc. counters */
254 static uintmax_t alloc_count;
@@ -257,6 +259,7 @@ static uintmax_t delta_count_by_type[1 << TYPE_BITS];
259 static unsigned long object_count;
260 static unsigned long branch_count;
261 static unsigned long branch_load_count;
262 +static int failure;
263
264 /* Memory pools */
265 static size_t mem_pool_alloc = 2*1024*1024 - sizeof(struct mem_pool);
@@ -1278,19 +1281,48 @@ del_entry:
1281 return 1;
1282 }
1283
1281 -static void dump_branches(void)
1284 +static int update_branch(struct branch *b)
1285 {
1286 static const char *msg = "fast-import";
1287 + struct ref_lock *lock;
1288 + unsigned char old_sha1[20];
1289 +
1290 + if (read_ref(b->name, old_sha1))
1291 + hashclr(old_sha1);
1292 + lock = lock_any_ref_for_update(b->name, old_sha1);
1293 + if (!lock)
1294 + return error("Unable to lock %s", b->name);
1295 + if (!force_update && !is_null_sha1(old_sha1)) {
1296 + struct commit *old_cmit, *new_cmit;
1297 +
1298 + old_cmit = lookup_commit_reference_gently(old_sha1, 0);
1299 + new_cmit = lookup_commit_reference_gently(b->sha1, 0);
1300 + if (!old_cmit || !new_cmit) {
1301 + unlock_ref(lock);
1302 + return error("Branch %s is missing commits.", b->name);
1303 + }
1304 +
1305 + if (!in_merge_bases(old_cmit, new_cmit)) {
1306 + unlock_ref(lock);
1307 + warn("Not updating %s"
1308 + " (new tip %s does not contain %s)",
1309 + b->name, sha1_to_hex(b->sha1), sha1_to_hex(old_sha1));
1310 + return -1;
1311 + }
1312 + }
1313 + if (write_ref_sha1(lock, b->sha1, msg) < 0)
1314 + return error("Unable to update %s", b->name);
1315 + return 0;
1316 +}
1317 +
1318 +static void dump_branches(void)
1319 +{
1320 unsigned int i;
1321 struct branch *b;
1286 - struct ref_lock *lock;
1322
1323 for (i = 0; i < branch_table_sz; i++) {
1289 - for (b = branch_table[i]; b; b = b->table_next_branch) {
1290 - lock = lock_any_ref_for_update(b->name, NULL);
1291 - if (!lock || write_ref_sha1(lock, b->sha1, msg) < 0)
1292 - die("Can't write %s", b->name);
1293 - }
1324 + for (b = branch_table[i]; b; b = b->table_next_branch)
1325 + failure |= update_branch(b);
1326 }
1327 }
1328
@@ -1299,13 +1331,13 @@ static void dump_tags(void)
1331 static const char *msg = "fast-import";
1332 struct tag *t;
1333 struct ref_lock *lock;
1302 - char path[PATH_MAX];
1334 + char ref_name[PATH_MAX];
1335
1336 for (t = first_tag; t; t = t->next_tag) {
1305 - sprintf(path, "refs/tags/%s", t->name);
1306 - lock = lock_any_ref_for_update(path, NULL);
1337 + sprintf(ref_name, "tags/%s", t->name);
1338 + lock = lock_ref_sha1(ref_name, NULL);
1339 if (!lock || write_ref_sha1(lock, t->sha1, msg) < 0)
1308 - die("Can't write %s", path);
1340 + failure |= error("Unable to update %s", ref_name);
1341 }
1342 }
1343
@@ -1936,6 +1968,8 @@ int main(int argc, const char **argv)
1968 max_active_branches = strtoul(a + 18, NULL, 0);
1969 else if (!strncmp(a, "--export-marks=", 15))
1970 mark_file = a + 15;
1971 + else if (!strcmp(a, "--force"))
1972 + force_update = 1;
1973 else
1974 die("unknown option %s", a);
1975 }
@@ -2001,5 +2035,5 @@ int main(int argc, const char **argv)
2035 fprintf(stderr, "---------------------------------------------------------------------\n");
2036 fprintf(stderr, "\n");
2037
2004 - return 0;
2038 + return failure ? 1 : 0;
2039 }
t/t9300-fast-import.sh
+80
@@ -276,4 +276,84 @@ test_expect_success \
276 'git-cat-file commit branch | sed 1,2d >actual &&
277 diff -u expect actual'
278
279 +###
280 +### series F
281 +###
282 +
283 +old_branch=`git-rev-parse --verify branch^0`
284 +test_tick
285 +cat >input <<INPUT_END
286 +commit refs/heads/branch
287 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
288 +data <<COMMIT
289 +losing things already?
290 +COMMIT
291 +
292 +from refs/heads/branch~1
293 +
294 +reset refs/heads/other
295 +from refs/heads/branch
296 +
297 +INPUT_END
298 +test_expect_success \
299 + 'F: non-fast-forward update skips' \
300 + 'if git-fast-import <input
301 + then
302 + echo BAD gfi did not fail
303 + return 1
304 + else
305 + if test $old_branch = `git-rev-parse --verify branch^0`
306 + then
307 + : branch unaffected and failure returned
308 + return 0
309 + else
310 + echo BAD gfi changed branch $old_branch
311 + return 1
312 + fi
313 + fi
314 + '
315 +test_expect_success \
316 + 'F: verify pack' \
317 + 'for p in .git/objects/pack/*.pack;do git-verify-pack $p||exit;done'
318 +
319 +cat >expect <<EOF
320 +tree `git-rev-parse branch~1^{tree}`
321 +parent `git-rev-parse branch~1`
322 +author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
323 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
324 +
325 +losing things already?
326 +EOF
327 +test_expect_success \
328 + 'F: verify other commit' \
329 + 'git-cat-file commit other >actual &&
330 + diff -u expect actual'
331 +
332 +###
333 +### series G
334 +###
335 +
336 +old_branch=`git-rev-parse --verify branch^0`
337 +test_tick
338 +cat >input <<INPUT_END
339 +commit refs/heads/branch
340 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
341 +data <<COMMIT
342 +losing things already?
343 +COMMIT
344 +
345 +from refs/heads/branch~1
346 +
347 +INPUT_END
348 +test_expect_success \
349 + 'G: non-fast-forward update forced' \
350 + 'git-fast-import --force <input'
351 +test_expect_success \
352 + 'G: verify pack' \
353 + 'for p in .git/objects/pack/*.pack;do git-verify-pack $p||exit;done'
354 +test_expect_success \
355 + 'G: branch changed, but logged' \
356 + 'test $old_branch != `git-rev-parse --verify branch^0` &&
357 + test $old_branch = `git-rev-parse --verify branch@{1}`'
358 +
359 test_done