@matheusbsbs / matheus-bagliano / commits / 825769a8fe
Fork 0

Teach fast-import how to clear the internal branch content.

Some frontends may not be able to (easily) keep track of which files are included in the branch, and which aren't. Performing this tracking can be tedious and error prone for the frontend to do, especially if its foreign data source cannot supply the changed path list on a per-commit basis. fast-import now allows a frontend to request that a branch's tree be wiped clean (reset to the empty tree) at the start of a commit, allowing the frontend to feed in all paths which belong on the branch. This is ideal for a tar-file importer frontend, for example, as the frontend just needs to reformat the tar data stream into a gfi data stream, which may be something a few Perl regexps can take care of. :) Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Feb 7, 2007 at 02:03 UTC 825769a8fe4d0529268fee7e763bc8f24ed4218e
3 files changed +94 -6
Documentation/git-fast-import.txt
+31 -5
@@ -269,7 +269,7 @@ change to the project.
269 data
270 ('from' SP <committish> LF)?
271 ('merge' SP <committish> LF)?
272 - (filemodify | filedelete)*
272 + (filemodify | filedelete | filedeleteall)*
273 LF
274 ....
275
@@ -292,10 +292,12 @@ commit message use a 0 length data. Commit messages are free-form
292 and are not interpreted by Git. Currently they must be encoded in
293 UTF-8, as gfi does not permit other encodings to be specified.
294
295 -Zero or more `filemodify` and `filedelete` commands may be
296 -included to update the contents of the branch prior to the commit.
297 -These commands can be supplied in any order, gfi is not sensitive
298 -to pathname or operation ordering.
295 +Zero or more `filemodify`, `filedelete` and `filedeleteall` commands
296 +may be included to update the contents of the branch prior to
297 +creating the commit. These commands may be supplied in any order.
298 +However it is recommended that a `filedeleteall` command preceed
299 +all `filemodify` commands in the same commit, as `filedeleteall`
300 +wipes the branch clean (see below).
301
302 `author`
303 ^^^^^^^^
@@ -459,6 +461,30 @@ first non-empty directory or the root is reached.
461 here `<path>` is the complete path of the file to be removed.
462 See `filemodify` above for a detailed description of `<path>`.
463
464 +`filedeleteall`
465 +^^^^^^^^^^^^^^^
466 +Included in a `commit` command to remove all files (and also all
467 +directories) from the branch. This command resets the internal
468 +branch structure to have no files in it, allowing the frontend
469 +to subsequently add all interesting files from scratch.
470 +
471 +....
472 + 'deleteall' LF
473 +....
474 +
475 +This command is extremely useful if the frontend does not know
476 +(or does not care to know) what files are currently on the branch,
477 +and therefore cannot generate the proper `filedelete` commands to
478 +update the content.
479 +
480 +Issuing a `filedeleteall` followed by the needed `filemodify`
481 +commands to set the correct content will produce the same results
482 +as sending only the needed `filemodify` and `filedelete` commands.
483 +The `filedeleteall` approach may however require gfi to use slightly
484 +more memory per active branch (less than 1 MiB for even most large
485 +projects); so frontends that can easily obtain only the affected
486 +paths for a commit are encouraged to do so.
487 +
488 `mark`
489 ~~~~~~
490 Arranges for gfi to save a reference to the current object, allowing
fast-import.c
+12 -1
@@ -26,7 +26,8 @@ Format of STDIN stream:
26 lf;
27 commit_msg ::= data;
28
29 - file_change ::= file_del | file_obm | file_inm;
29 + file_change ::= file_clr | file_del | file_obm | file_inm;
30 + file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
33 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
@@ -1640,6 +1641,14 @@ static void file_change_d(struct branch *b)
1641 free(p_uq);
1642 }
1643
1644 +static void file_change_deleteall(struct branch *b)
1645 +{
1646 + release_tree_content_recursive(b->branch_tree.tree);
1647 + hashclr(b->branch_tree.versions[0].sha1);
1648 + hashclr(b->branch_tree.versions[1].sha1);
1649 + load_tree(&b->branch_tree);
1650 +}
1651 +
1652 static void cmd_from(struct branch *b)
1653 {
1654 const char *from;
@@ -1784,6 +1793,8 @@ static void cmd_new_commit(void)
1793 file_change_m(b);
1794 else if (!strncmp("D ", command_buf.buf, 2))
1795 file_change_d(b);
1796 + else if (!strcmp("deleteall", command_buf.buf))
1797 + file_change_deleteall(b);
1798 else
1799 die("Unsupported file_change: %s", command_buf.buf);
1800 read_next_command();
t/t9300-fast-import.sh
+51
@@ -356,4 +356,55 @@ test_expect_success \
356 'test $old_branch != `git-rev-parse --verify branch^0` &&
357 test $old_branch = `git-rev-parse --verify branch@{1}`'
358
359 +###
360 +### series H
361 +###
362 +
363 +test_tick
364 +cat >input <<INPUT_END
365 +commit refs/heads/H
366 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
367 +data <<COMMIT
368 +third
369 +COMMIT
370 +
371 +from refs/heads/branch^0
372 +M 644 inline i-will-die
373 +data <<EOF
374 +this file will never exist.
375 +EOF
376 +
377 +deleteall
378 +M 644 inline h/e/l/lo
379 +data <<EOF
380 +$file5_data
381 +EOF
382 +
383 +INPUT_END
384 +test_expect_success \
385 + 'H: deletall, add 1' \
386 + 'git-fast-import <input &&
387 + git-whatchanged H'
388 +test_expect_success \
389 + 'H: verify pack' \
390 + 'for p in .git/objects/pack/*.pack;do git-verify-pack $p||exit;done'
391 +
392 +cat >expect <<EOF
393 +:100755 000000 f1fb5da718392694d0076d677d6d0e364c79b0bc 0000000000000000000000000000000000000000 D file2/newf
394 +:100644 000000 7123f7f44e39be127c5eb701e5968176ee9d78b1 0000000000000000000000000000000000000000 D file2/oldf
395 +:100755 000000 85df50785d62d3b05ab03d9cbf7e4a0b49449730 0000000000000000000000000000000000000000 D file4
396 +:100644 100644 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 fcf778cda181eaa1cbc9e9ce3a2e15ee9f9fe791 R100 newdir/interesting h/e/l/lo
397 +:100755 000000 e74b7d465e52746be2b4bae983670711e6e66657 0000000000000000000000000000000000000000 D newdir/exec.sh
398 +EOF
399 +git-diff-tree -M -r H^ H >actual
400 +test_expect_success \
401 + 'H: validate old files removed, new files added' \
402 + 'compare_diff_raw expect actual'
403 +
404 +echo "$file5_data" >expect
405 +test_expect_success \
406 + 'H: verify file' \
407 + 'git-cat-file blob H:h/e/l/lo >actual &&
408 + diff -u expect actual'
409 +
410 test_done