Support creation of merge commits in fast-import.
Some importers are able to determine when branch merges occurred within their source data. In these cases they will want to supply the correct commits to fast-import so that a proper merge commit will exist in Git. This is now supported by supplying a 'merge ' command after the commit message and optional from command. A merge is not actually performed by fast-import, its assumed that the frontend performed any sort of merging activity already and that fast-import should simply be storing its result. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Jan 11, 2007 at 22:21 UTC
62b6f48388faf0ac2432a07cfc53aa904c591f8f
1 file changed
+58
fast-import.c
+58
@@ -20,6 +20,7 @@ Format of STDIN stream:
20
'committer' sp name '<' email '>' ts tz lf
21
commit_msg
22
('from' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)?
23
+ ('merge' sp (ref_str | hexsha1 | sha1exp_str | idnum) lf)*
24
file_change*
25
lf;
26
commit_msg ::= data;
@@ -202,6 +203,11 @@ struct dbuf
203
size_t capacity;
204
};
205
206
+struct hash_list
207
+{
208
+ struct hash_list *next;
209
+ unsigned char sha1[20];
210
+};
211
212
/* Stats and misc. counters */
213
static unsigned long max_depth = 10;
@@ -1502,6 +1508,48 @@ static void cmd_from(struct branch *b)
1508
read_next_command();
1509
}
1510
1511
+static struct hash_list* cmd_merge(unsigned int *count)
1512
+{
1513
+ struct hash_list *list = NULL, *n, *e;
1514
+ const char *from, *endp;
1515
+ char *str_uq;
1516
+ struct branch *s;
1517
+
1518
+ *count = 0;
1519
+ while (!strncmp("merge ", command_buf.buf, 6)) {
1520
+ from = strchr(command_buf.buf, ' ') + 1;
1521
+ str_uq = unquote_c_style(from, &endp);
1522
+ if (str_uq) {
1523
+ if (*endp)
1524
+ die("Garbage after string in: %s", command_buf.buf);
1525
+ from = str_uq;
1526
+ }
1527
+
1528
+ n = xmalloc(sizeof(*n));
1529
+ s = lookup_branch(from);
1530
+ if (s)
1531
+ hashcpy(n->sha1, s->sha1);
1532
+ else if (*from == ':') {
1533
+ unsigned long idnum = strtoul(from + 1, NULL, 10);
1534
+ struct object_entry *oe = find_mark(idnum);
1535
+ if (oe->type != OBJ_COMMIT)
1536
+ die("Mark :%lu not a commit", idnum);
1537
+ hashcpy(n->sha1, oe->sha1);
1538
+ } else if (get_sha1(from, n->sha1))
1539
+ die("Invalid ref name or SHA1 expression: %s", from);
1540
+
1541
+ n->next = NULL;
1542
+ if (list)
1543
+ e->next = n;
1544
+ else
1545
+ list = n;
1546
+ e = n;
1547
+ *count++;
1548
+ read_next_command();
1549
+ }
1550
+ return list;
1551
+}
1552
+
1553
static void cmd_new_commit()
1554
{
1555
struct branch *b;
@@ -1512,6 +1560,8 @@ static void cmd_new_commit()
1560
char *sp;
1561
char *author = NULL;
1562
char *committer = NULL;
1563
+ struct hash_list *merge_list = NULL;
1564
+ unsigned int merge_count;
1565
1566
/* Obtain the branch name from the rest of our command */
1567
sp = strchr(command_buf.buf, ' ') + 1;
@@ -1542,6 +1592,7 @@ static void cmd_new_commit()
1592
msg = cmd_data(&msglen);
1593
read_next_command();
1594
cmd_from(b);
1595
+ merge_list = cmd_merge(&merge_count);
1596
1597
/* ensure the branch is active/loaded */
1598
if (!b->branch_tree.tree || !max_active_branches) {
@@ -1567,6 +1618,7 @@ static void cmd_new_commit()
1618
hashcpy(b->branch_tree.versions[0].sha1,
1619
b->branch_tree.versions[1].sha1);
1620
size_dbuf(&new_data, 97 + msglen
1621
+ + merge_count * 49
1622
+ (author
1623
? strlen(author) + strlen(committer)
1624
: 2 * strlen(committer)));
@@ -1575,6 +1627,12 @@ static void cmd_new_commit()
1627
sha1_to_hex(b->branch_tree.versions[1].sha1));
1628
if (!is_null_sha1(b->sha1))
1629
sp += sprintf(sp, "parent %s\n", sha1_to_hex(b->sha1));
1630
+ while (merge_list) {
1631
+ struct hash_list *next = merge_list->next;
1632
+ sp += sprintf(sp, "parent %s\n", sha1_to_hex(merge_list->sha1));
1633
+ free(merge_list);
1634
+ merge_list = next;
1635
+ }
1636
if (author)
1637
sp += sprintf(sp, "%s\n", author);
1638
else