Skip to content

Commit e1b3a2c

Browse files
iabervongitster
authored andcommitted
Build-in merge-recursive
This makes write_tree_from_memory(), which writes the active cache as a tree and returns the struct tree for it, available to other code. It also makes available merge_trees(), which does the internal merge of two trees with a known base, and merge_recursive(), which does the recursive internal merge of two commits with a list of common ancestors. The first two of these will be used by checkout -m, and the third is presumably useful in general, although the implementation of checkout -m which entirely matches the behavior of the shell version does not use it (since it ignores the difference of ancestry between the old branch and the new branch). Signed-off-by: Daniel Barkalow <[email protected]>
1 parent 4e7c457 commit e1b3a2c

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ PROGRAMS = \
256256
git-upload-pack$X \
257257
git-pack-redundant$X git-var$X \
258258
git-merge-tree$X git-imap-send$X \
259-
git-merge-recursive$X \
260259
$(EXTRA_PROGRAMS)
261260

262261
# Empty...
@@ -360,6 +359,7 @@ BUILTIN_OBJS = \
360359
builtin-merge-base.o \
361360
builtin-merge-file.o \
362361
builtin-merge-ours.o \
362+
builtin-merge-recursive.o \
363363
builtin-mv.o \
364364
builtin-name-rev.o \
365365
builtin-pack-objects.o \

merge-recursive.c renamed to builtin-merge-recursive.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cache-tree.h"
88
#include "commit.h"
99
#include "blob.h"
10+
#include "builtin.h"
1011
#include "tree-walk.h"
1112
#include "diff.h"
1213
#include "diffcore.h"
@@ -17,6 +18,7 @@
1718
#include "xdiff-interface.h"
1819
#include "interpolate.h"
1920
#include "attr.h"
21+
#include "merge-recursive.h"
2022

2123
static int subtree_merge;
2224

@@ -232,7 +234,7 @@ static int unmerged_index(void)
232234
return 0;
233235
}
234236

235-
static struct tree *git_write_tree(void)
237+
struct tree *write_tree_from_memory(void)
236238
{
237239
struct tree *result = NULL;
238240

@@ -1495,12 +1497,12 @@ static int process_entry(const char *path, struct stage_data *entry,
14951497
return clean_merge;
14961498
}
14971499

1498-
static int merge_trees(struct tree *head,
1499-
struct tree *merge,
1500-
struct tree *common,
1501-
const char *branch1,
1502-
const char *branch2,
1503-
struct tree **result)
1500+
int merge_trees(struct tree *head,
1501+
struct tree *merge,
1502+
struct tree *common,
1503+
const char *branch1,
1504+
const char *branch2,
1505+
struct tree **result)
15041506
{
15051507
int code, clean;
15061508

@@ -1552,7 +1554,7 @@ static int merge_trees(struct tree *head,
15521554
clean = 1;
15531555

15541556
if (index_only)
1555-
*result = git_write_tree();
1557+
*result = write_tree_from_memory();
15561558

15571559
return clean;
15581560
}
@@ -1572,12 +1574,12 @@ static struct commit_list *reverse_commit_list(struct commit_list *list)
15721574
* Merge the commits h1 and h2, return the resulting virtual
15731575
* commit object and a flag indicating the cleanness of the merge.
15741576
*/
1575-
static int merge(struct commit *h1,
1576-
struct commit *h2,
1577-
const char *branch1,
1578-
const char *branch2,
1579-
struct commit_list *ca,
1580-
struct commit **result)
1577+
int merge_recursive(struct commit *h1,
1578+
struct commit *h2,
1579+
const char *branch1,
1580+
const char *branch2,
1581+
struct commit_list *ca,
1582+
struct commit **result)
15811583
{
15821584
struct commit_list *iter;
15831585
struct commit *merged_common_ancestors;
@@ -1622,11 +1624,11 @@ static int merge(struct commit *h1,
16221624
* "conflicts" were already resolved.
16231625
*/
16241626
discard_cache();
1625-
merge(merged_common_ancestors, iter->item,
1626-
"Temporary merge branch 1",
1627-
"Temporary merge branch 2",
1628-
NULL,
1629-
&merged_common_ancestors);
1627+
merge_recursive(merged_common_ancestors, iter->item,
1628+
"Temporary merge branch 1",
1629+
"Temporary merge branch 2",
1630+
NULL,
1631+
&merged_common_ancestors);
16301632
call_depth--;
16311633

16321634
if (!merged_common_ancestors)
@@ -1695,7 +1697,7 @@ static int merge_config(const char *var, const char *value)
16951697
return git_default_config(var, value);
16961698
}
16971699

1698-
int main(int argc, char *argv[])
1700+
int cmd_merge_recursive(int argc, const char **argv, const char *prefix)
16991701
{
17001702
static const char *bases[20];
17011703
static unsigned bases_count = 0;
@@ -1749,7 +1751,7 @@ int main(int argc, char *argv[])
17491751
struct commit *ancestor = get_ref(bases[i]);
17501752
ca = commit_list_insert(ancestor, &ca);
17511753
}
1752-
clean = merge(h1, h2, branch1, branch2, ca, &result);
1754+
clean = merge_recursive(h1, h2, branch1, branch2, ca, &result);
17531755

17541756
if (active_cache_changed &&
17551757
(write_cache(index_fd, active_cache, active_nr) ||

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ extern int cmd_mailsplit(int argc, const char **argv, const char *prefix);
5757
extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
5858
extern int cmd_merge_ours(int argc, const char **argv, const char *prefix);
5959
extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
60+
extern int cmd_merge_recursive(int argc, const char **argv, const char *prefix);
6061
extern int cmd_mv(int argc, const char **argv, const char *prefix);
6162
extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
6263
extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ static void handle_internal_command(int argc, const char **argv)
330330
{ "merge-base", cmd_merge_base, RUN_SETUP },
331331
{ "merge-file", cmd_merge_file },
332332
{ "merge-ours", cmd_merge_ours, RUN_SETUP },
333+
{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
333334
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
334335
{ "name-rev", cmd_name_rev, RUN_SETUP },
335336
{ "pack-objects", cmd_pack_objects, RUN_SETUP },

merge-recursive.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef MERGE_RECURSIVE_H
2+
#define MERGE_RECURSIVE_H
3+
4+
int merge_recursive(struct commit *h1,
5+
struct commit *h2,
6+
const char *branch1,
7+
const char *branch2,
8+
struct commit_list *ancestors,
9+
struct commit **result);
10+
11+
int merge_trees(struct tree *head,
12+
struct tree *merge,
13+
struct tree *common,
14+
const char *branch1,
15+
const char *branch2,
16+
struct tree **result);
17+
18+
struct tree *write_tree_from_memory(void);
19+
20+
#endif

0 commit comments

Comments
 (0)