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

Support wholesale directory renames in fast-import

Some source material (e.g. Subversion dump files) perform directory renames without telling us exactly which files in that subdirectory were moved. This makes it hard for a frontend to convert such data formats to a fast-import stream, as all the frontend has on hand is "Rename a/ to b/" with no details about what files are in a/, unless the frontend also kept track of all files. The new 'R' subcommand within a commit allows the frontend to rename either a file or an entire subdirectory, without needing to know the object's SHA-1 or the specific files contained within it. The rename is performed as efficiently as possible internally, making it cheaper than a 'D'/'M' pair for a file rename. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jul 9, 2007 at 22:58 UTC f39a946a1fb0fa4856cd0027b9da3603a1b06fdc
3 files changed +168 -19
Documentation/git-fast-import.txt
+25 -3
@@ -302,7 +302,7 @@ change to the project.
302 data
303 ('from' SP <committish> LF)?
304 ('merge' SP <committish> LF)?
305 - (filemodify | filedelete | filedeleteall)*
305 + (filemodify | filedelete | filerename | filedeleteall)*
306 LF
307 ....
308
@@ -325,11 +325,13 @@ commit message use a 0 length data. Commit messages are free-form
325 and are not interpreted by Git. Currently they must be encoded in
326 UTF-8, as fast-import does not permit other encodings to be specified.
327
328 -Zero or more `filemodify`, `filedelete` and `filedeleteall` commands
328 +Zero or more `filemodify`, `filedelete`, `filename` and
329 +`filedeleteall` commands
330 may be included to update the contents of the branch prior to
331 creating the commit. These commands may be supplied in any order.
332 However it is recommended that a `filedeleteall` command preceed
332 -all `filemodify` commands in the same commit, as `filedeleteall`
333 +all `filemodify` and `filerename` commands in the same commit, as
334 +`filedeleteall`
335 wipes the branch clean (see below).
336
337 `author`
@@ -495,6 +497,26 @@ here `<path>` is the complete path of the file or subdirectory to
497 be removed from the branch.
498 See `filemodify` above for a detailed description of `<path>`.
499
500 +`filerename`
501 +^^^^^^^^^^^^
502 +Renames an existing file or subdirectory to a different location
503 +within the branch. The existing file or directory must exist. If
504 +the destination exists it will be replaced by the source directory.
505 +
506 +....
507 + 'R' SP <path> SP <path> LF
508 +....
509 +
510 +here the first `<path>` is the source location and the second
511 +`<path>` is the destination. See `filemodify` above for a detailed
512 +description of what `<path>` may look like. To use a source path
513 +that contains SP the path must be quoted.
514 +
515 +A `filerename` command takes effect immediately. Once the source
516 +location has been renamed to the destination any future commands
517 +applied to the source location will create new files there and not
518 +impact the destination of the rename.
519 +
520 `filedeleteall`
521 ^^^^^^^^^^^^^^^
522 Included in a `commit` command to remove all files (and also all
fast-import.c
+75 -16
@@ -26,9 +26,10 @@ Format of STDIN stream:
26 lf;
27 commit_msg ::= data;
28
29 - file_change ::= file_clr | file_del | file_obm | file_inm;
29 + file_change ::= file_clr | file_del | file_rnm | file_obm | file_inm;
30 file_clr ::= 'deleteall' lf;
31 file_del ::= 'D' sp path_str lf;
32 + file_rnm ::= 'R' sp path_str sp path_str lf;
33 file_obm ::= 'M' sp mode sp (hexsha1 | idnum) sp path_str lf;
34 file_inm ::= 'M' sp mode sp 'inline' sp path_str lf
35 data;
@@ -1154,7 +1155,8 @@ static int tree_content_set(
1155 struct tree_entry *root,
1156 const char *p,
1157 const unsigned char *sha1,
1157 - const uint16_t mode)
1158 + const uint16_t mode,
1159 + struct tree_content *subtree)
1160 {
1161 struct tree_content *t = root->tree;
1162 const char *slash1;
@@ -1168,20 +1170,22 @@ static int tree_content_set(
1170 n = strlen(p);
1171 if (!n)
1172 die("Empty path component found in input");
1173 + if (!slash1 && !S_ISDIR(mode) && subtree)
1174 + die("Non-directories cannot have subtrees");
1175
1176 for (i = 0; i < t->entry_count; i++) {
1177 e = t->entries[i];
1178 if (e->name->str_len == n && !strncmp(p, e->name->str_dat, n)) {
1179 if (!slash1) {
1176 - if (e->versions[1].mode == mode
1180 + if (!S_ISDIR(mode)
1181 + && e->versions[1].mode == mode
1182 && !hashcmp(e->versions[1].sha1, sha1))
1183 return 0;
1184 e->versions[1].mode = mode;
1185 hashcpy(e->versions[1].sha1, sha1);
1181 - if (e->tree) {
1186 + if (e->tree)
1187 release_tree_content_recursive(e->tree);
1183 - e->tree = NULL;
1184 - }
1188 + e->tree = subtree;
1189 hashclr(root->versions[1].sha1);
1190 return 1;
1191 }
@@ -1191,7 +1195,7 @@ static int tree_content_set(
1195 }
1196 if (!e->tree)
1197 load_tree(e);
1194 - if (tree_content_set(e, slash1 + 1, sha1, mode)) {
1198 + if (tree_content_set(e, slash1 + 1, sha1, mode, subtree)) {
1199 hashclr(root->versions[1].sha1);
1200 return 1;
1201 }
@@ -1209,9 +1213,9 @@ static int tree_content_set(
1213 if (slash1) {
1214 e->tree = new_tree_content(8);
1215 e->versions[1].mode = S_IFDIR;
1212 - tree_content_set(e, slash1 + 1, sha1, mode);
1216 + tree_content_set(e, slash1 + 1, sha1, mode, subtree);
1217 } else {
1214 - e->tree = NULL;
1218 + e->tree = subtree;
1219 e->versions[1].mode = mode;
1220 hashcpy(e->versions[1].sha1, sha1);
1221 }
@@ -1219,7 +1223,10 @@ static int tree_content_set(
1223 return 1;
1224 }
1225
1222 -static int tree_content_remove(struct tree_entry *root, const char *p)
1226 +static int tree_content_remove(
1227 + struct tree_entry *root,
1228 + const char *p,
1229 + struct tree_entry *backup_leaf)
1230 {
1231 struct tree_content *t = root->tree;
1232 const char *slash1;
@@ -1239,13 +1246,14 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
1246 goto del_entry;
1247 if (!e->tree)
1248 load_tree(e);
1242 - if (tree_content_remove(e, slash1 + 1)) {
1249 + if (tree_content_remove(e, slash1 + 1, backup_leaf)) {
1250 for (n = 0; n < e->tree->entry_count; n++) {
1251 if (e->tree->entries[n]->versions[1].mode) {
1252 hashclr(root->versions[1].sha1);
1253 return 1;
1254 }
1255 }
1256 + backup_leaf = NULL;
1257 goto del_entry;
1258 }
1259 return 0;
@@ -1254,10 +1262,11 @@ static int tree_content_remove(struct tree_entry *root, const char *p)
1262 return 0;
1263
1264 del_entry:
1257 - if (e->tree) {
1265 + if (backup_leaf)
1266 + memcpy(backup_leaf, e, sizeof(*backup_leaf));
1267 + else if (e->tree)
1268 release_tree_content_recursive(e->tree);
1259 - e->tree = NULL;
1260 - }
1269 + e->tree = NULL;
1270 e->versions[1].mode = 0;
1271 hashclr(e->versions[1].sha1);
1272 hashclr(root->versions[1].sha1);
@@ -1629,7 +1638,7 @@ static void file_change_m(struct branch *b)
1638 typename(type), command_buf.buf);
1639 }
1640
1632 - tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode);
1641 + tree_content_set(&b->branch_tree, p, sha1, S_IFREG | mode, NULL);
1642 free(p_uq);
1643 }
1644
@@ -1645,10 +1654,58 @@ static void file_change_d(struct branch *b)
1654 die("Garbage after path in: %s", command_buf.buf);
1655 p = p_uq;
1656 }
1648 - tree_content_remove(&b->branch_tree, p);
1657 + tree_content_remove(&b->branch_tree, p, NULL);
1658 free(p_uq);
1659 }
1660
1661 +static void file_change_r(struct branch *b)
1662 +{
1663 + const char *s, *d;
1664 + char *s_uq, *d_uq;
1665 + const char *endp;
1666 + struct tree_entry leaf;
1667 +
1668 + s = command_buf.buf + 2;
1669 + s_uq = unquote_c_style(s, &endp);
1670 + if (s_uq) {
1671 + if (*endp != ' ')
1672 + die("Missing space after source: %s", command_buf.buf);
1673 + }
1674 + else {
1675 + endp = strchr(s, ' ');
1676 + if (!endp)
1677 + die("Missing space after source: %s", command_buf.buf);
1678 + s_uq = xmalloc(endp - s + 1);
1679 + memcpy(s_uq, s, endp - s);
1680 + s_uq[endp - s] = 0;
1681 + }
1682 + s = s_uq;
1683 +
1684 + endp++;
1685 + if (!*endp)
1686 + die("Missing dest: %s", command_buf.buf);
1687 +
1688 + d = endp;
1689 + d_uq = unquote_c_style(d, &endp);
1690 + if (d_uq) {
1691 + if (*endp)
1692 + die("Garbage after dest in: %s", command_buf.buf);
1693 + d = d_uq;
1694 + }
1695 +
1696 + memset(&leaf, 0, sizeof(leaf));
1697 + tree_content_remove(&b->branch_tree, s, &leaf);
1698 + if (!leaf.versions[1].mode)
1699 + die("Path %s not in branch", s);
1700 + tree_content_set(&b->branch_tree, d,
1701 + leaf.versions[1].sha1,
1702 + leaf.versions[1].mode,
1703 + leaf.tree);
1704 +
1705 + free(s_uq);
1706 + free(d_uq);
1707 +}
1708 +
1709 static void file_change_deleteall(struct branch *b)
1710 {
1711 release_tree_content_recursive(b->branch_tree.tree);
@@ -1816,6 +1873,8 @@ static void cmd_new_commit(void)
1873 file_change_m(b);
1874 else if (!prefixcmp(command_buf.buf, "D "))
1875 file_change_d(b);
1876 + else if (!prefixcmp(command_buf.buf, "R "))
1877 + file_change_r(b);
1878 else if (!strcmp("deleteall", command_buf.buf))
1879 file_change_deleteall(b);
1880 else
t/t9300-fast-import.sh
+68
@@ -580,4 +580,72 @@ test_expect_success \
580 git diff --raw L^ L >output &&
581 git diff expect output'
582
583 +###
584 +### series M
585 +###
586 +
587 +test_tick
588 +cat >input <<INPUT_END
589 +commit refs/heads/M1
590 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
591 +data <<COMMIT
592 +file rename
593 +COMMIT
594 +
595 +from refs/heads/branch^0
596 +R file2/newf file2/n.e.w.f
597 +
598 +INPUT_END
599 +
600 +cat >expect <<EOF
601 +:100755 100755 f1fb5da718392694d0076d677d6d0e364c79b0bc f1fb5da718392694d0076d677d6d0e364c79b0bc R100 file2/newf file2/n.e.w.f
602 +EOF
603 +test_expect_success \
604 + 'M: rename file in same subdirectory' \
605 + 'git-fast-import <input &&
606 + git diff-tree -M -r M1^ M1 >actual &&
607 + compare_diff_raw expect actual'
608 +
609 +cat >input <<INPUT_END
610 +commit refs/heads/M2
611 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
612 +data <<COMMIT
613 +file rename
614 +COMMIT
615 +
616 +from refs/heads/branch^0
617 +R file2/newf i/am/new/to/you
618 +
619 +INPUT_END
620 +
621 +cat >expect <<EOF
622 +:100755 100755 f1fb5da718392694d0076d677d6d0e364c79b0bc f1fb5da718392694d0076d677d6d0e364c79b0bc R100 file2/newf i/am/new/to/you
623 +EOF
624 +test_expect_success \
625 + 'M: rename file to new subdirectory' \
626 + 'git-fast-import <input &&
627 + git diff-tree -M -r M2^ M2 >actual &&
628 + compare_diff_raw expect actual'
629 +
630 +cat >input <<INPUT_END
631 +commit refs/heads/M3
632 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
633 +data <<COMMIT
634 +file rename
635 +COMMIT
636 +
637 +from refs/heads/M2^0
638 +R i other/sub
639 +
640 +INPUT_END
641 +
642 +cat >expect <<EOF
643 +:100755 100755 f1fb5da718392694d0076d677d6d0e364c79b0bc f1fb5da718392694d0076d677d6d0e364c79b0bc R100 i/am/new/to/you other/sub/am/new/to/you
644 +EOF
645 +test_expect_success \
646 + 'M: rename subdirectory to new subdirectory' \
647 + 'git-fast-import <input &&
648 + git diff-tree -M -r M3^ M3 >actual &&
649 + compare_diff_raw expect actual'
650 +
651 test_done