@matheusbsbs / matheus-bagliano / commits / ea5e370aa9
Fork 0

fast-import: Support reusing 'from' and brown paper bag fix reset.

It was suggested on the mailing list that being able to use `from` in any commit to reset the current branch is useful in some types of importers, such as a darcs importer. We originally did not permit resetting an existing branch with a new `from` command during a `commit` command, but this restriction was only to help debug the hacked up cvs2svn that Jon Smirl was developing in parallel with git-fast-import. It is probably more of a problem to disallow it than to allow it. So now we permit a `from` during any `commit`. While making the changes required to permit multiple `from` commands on the same branch, I discovered we no longer needed the last_commit field to be set to 0 during a reset, so that was removed. (Reset was originally setting the field to 0 to signal cmd_from() that it was OK to execute on the branch.) While poking around in this section of fast-import I also realized the `reset` command was not working as intended if the corresponding `from` command was omitted (as allowed by the BNF grammar and the code). If `from` was omitted we cleared out the tree but we left the tree SHA-1 and parent commit SHA-1 intact. This is not what the user intended in this case. Instead they would be trying to reset the branch to have no parent and to have no tree, making the branch look new-born during the next commit. We now clear these SHA-1 values during `reset`, ensuring the branch looks new-born if `from` does not get supplied. New test cases for these were also added. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Feb 12, 2007 at 04:08 UTC ea5e370aa9203f999dd8bcd4a9cd29452f1c9181
3 files changed +77 -14
Documentation/git-fast-import.txt
+10 -11
@@ -349,17 +349,16 @@ their syntax.
349
350 `from`
351 ^^^^^^
352 -Only valid for the first commit made on this branch by this
353 -fast-import process. The `from` command is used to specify the commit
354 -to initialize this branch from. This revision will be the first
355 -ancestor of the new commit.
356 -
357 -Omitting the `from` command in the first commit of a new branch will
358 -cause fast-import to create that commit with no ancestor. This tends to be
359 -desired only for the initial commit of a project. Omitting the
360 -`from` command on existing branches is required, as the current
361 -commit on that branch is automatically assumed to be the first
362 -ancestor of the new commit.
352 +The `from` command is used to specify the commit to initialize
353 +this branch from. This revision will be the first ancestor of the
354 +new commit.
355 +
356 +Omitting the `from` command in the first commit of a new branch
357 +will cause fast-import to create that commit with no ancestor. This
358 +tends to be desired only for the initial commit of a project.
359 +Omitting the `from` command on existing branches is usually desired,
360 +as the current commit on that branch is automatically assumed to
361 +be the first ancestor of the new commit.
362
363 As `LF` is not valid in a Git refname or SHA-1 expression, no
364 quoting or escaping syntax is supported within `<committish>`.
fast-import.c
+7 -3
@@ -1667,8 +1667,10 @@ static void cmd_from(struct branch *b)
1667 if (strncmp("from ", command_buf.buf, 5))
1668 return;
1669
1670 - if (b->last_commit)
1671 - die("Can't reinitailize branch %s", b->name);
1670 + if (b->branch_tree.tree) {
1671 + release_tree_content_recursive(b->branch_tree.tree);
1672 + b->branch_tree.tree = NULL;
1673 + }
1674
1675 from = strchr(command_buf.buf, ' ') + 1;
1676 s = lookup_branch(from);
@@ -1936,7 +1938,9 @@ static void cmd_reset_branch(void)
1938 sp = strchr(command_buf.buf, ' ') + 1;
1939 b = lookup_branch(sp);
1940 if (b) {
1939 - b->last_commit = 0;
1941 + hashclr(b->sha1);
1942 + hashclr(b->branch_tree.versions[0].sha1);
1943 + hashclr(b->branch_tree.versions[1].sha1);
1944 if (b->branch_tree.tree) {
1945 release_tree_content_recursive(b->branch_tree.tree);
1946 b->branch_tree.tree = NULL;
t/t9300-fast-import.sh
+60
@@ -433,4 +433,64 @@ test_expect_success \
433 'sed -e s/pack-.*pack/pack-.pack/ edges.list >actual &&
434 diff -u expect actual'
435
436 +###
437 +### series J
438 +###
439 +
440 +cat >input <<INPUT_END
441 +commit refs/heads/J
442 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
443 +data <<COMMIT
444 +create J
445 +COMMIT
446 +
447 +from refs/heads/branch
448 +
449 +reset refs/heads/J
450 +
451 +commit refs/heads/J
452 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
453 +data <<COMMIT
454 +initialize J
455 +COMMIT
456 +
457 +INPUT_END
458 +test_expect_success \
459 + 'J: reset existing branch creates empty commit' \
460 + 'git-fast-import <input'
461 +test_expect_success \
462 + 'J: branch has 1 commit, empty tree' \
463 + 'test 1 = `git-rev-list J | wc -l` &&
464 + test 0 = `git ls-tree J | wc -l`'
465 +
466 +###
467 +### series K
468 +###
469 +
470 +cat >input <<INPUT_END
471 +commit refs/heads/K
472 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
473 +data <<COMMIT
474 +create K
475 +COMMIT
476 +
477 +from refs/heads/branch
478 +
479 +commit refs/heads/K
480 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
481 +data <<COMMIT
482 +redo K
483 +COMMIT
484 +
485 +from refs/heads/branch^1
486 +
487 +INPUT_END
488 +test_expect_success \
489 + 'K: reinit branch with from' \
490 + 'git-fast-import <input'
491 +test_expect_success \
492 + 'K: verify K^1 = branch^1' \
493 + 'test `git-rev-parse --verify branch^1` \
494 + = `git-rev-parse --verify K^1`'
495 +
496 test_done