Skip to content

Commit a1f9595

Browse files
committed
Merge branch 'en/merge-ort-api-null-impl'
Preparation for a new merge strategy. * en/merge-ort-api-null-impl: merge,rebase,revert: select ort or recursive by config or environment fast-rebase: demonstrate merge-ort's API via new test-tool command merge-ort-wrappers: new convience wrappers to mimic the old merge API merge-ort: barebones API of new merge strategy with empty implementation
2 parents 7660da1 + 14c4586 commit a1f9595

File tree

13 files changed

+517
-15
lines changed

13 files changed

+517
-15
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,7 @@ TEST_BUILTINS_OBJS += test-dump-fsmonitor.o
705705
TEST_BUILTINS_OBJS += test-dump-split-index.o
706706
TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
707707
TEST_BUILTINS_OBJS += test-example-decorate.o
708+
TEST_BUILTINS_OBJS += test-fast-rebase.o
708709
TEST_BUILTINS_OBJS += test-genrandom.o
709710
TEST_BUILTINS_OBJS += test-genzeros.o
710711
TEST_BUILTINS_OBJS += test-hash-speed.o
@@ -922,6 +923,8 @@ LIB_OBJS += mailmap.o
922923
LIB_OBJS += match-trees.o
923924
LIB_OBJS += mem-pool.o
924925
LIB_OBJS += merge-blobs.o
926+
LIB_OBJS += merge-ort.o
927+
LIB_OBJS += merge-ort-wrappers.o
925928
LIB_OBJS += merge-recursive.o
926929
LIB_OBJS += merge.o
927930
LIB_OBJS += mergesort.o

builtin/merge.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "rerere.h"
2929
#include "help.h"
3030
#include "merge-recursive.h"
31+
#include "merge-ort-wrappers.h"
3132
#include "resolve-undo.h"
3233
#include "remote.h"
3334
#include "fmt-merge-msg.h"
@@ -88,6 +89,7 @@ static int no_verify;
8889
static struct strategy all_strategy[] = {
8990
{ "recursive", DEFAULT_TWOHEAD | NO_TRIVIAL },
9091
{ "octopus", DEFAULT_OCTOPUS },
92+
{ "ort", NO_TRIVIAL },
9193
{ "resolve", 0 },
9294
{ "ours", NO_FAST_FORWARD | NO_TRIVIAL },
9395
{ "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
@@ -159,10 +161,17 @@ static struct strategy *get_strategy(const char *name)
159161
struct strategy *ret;
160162
static struct cmdnames main_cmds, other_cmds;
161163
static int loaded;
164+
char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
162165

163166
if (!name)
164167
return NULL;
165168

169+
if (default_strategy &&
170+
!strcmp(default_strategy, "ort") &&
171+
!strcmp(name, "recursive")) {
172+
name = "ort";
173+
}
174+
166175
for (i = 0; i < ARRAY_SIZE(all_strategy); i++)
167176
if (!strcmp(name, all_strategy[i].name))
168177
return &all_strategy[i];
@@ -701,7 +710,8 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
701710
if (refresh_and_write_cache(REFRESH_QUIET, SKIP_IF_UNCHANGED, 0) < 0)
702711
return error(_("Unable to write index."));
703712

704-
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree")) {
713+
if (!strcmp(strategy, "recursive") || !strcmp(strategy, "subtree") ||
714+
!strcmp(strategy, "ort")) {
705715
struct lock_file lock = LOCK_INIT;
706716
int clean, x;
707717
struct commit *result;
@@ -732,8 +742,12 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
732742
commit_list_insert(j->item, &reversed);
733743

734744
hold_locked_index(&lock, LOCK_DIE_ON_ERROR);
735-
clean = merge_recursive(&o, head,
736-
remoteheads->item, reversed, &result);
745+
if (!strcmp(strategy, "ort"))
746+
clean = merge_ort_recursive(&o, head, remoteheads->item,
747+
reversed, &result);
748+
else
749+
clean = merge_recursive(&o, head, remoteheads->item,
750+
reversed, &result);
737751
if (clean < 0)
738752
exit(128);
739753
if (write_locked_index(&the_index, &lock,
@@ -1264,6 +1278,12 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12641278
if (branch)
12651279
skip_prefix(branch, "refs/heads/", &branch);
12661280

1281+
if (!pull_twohead) {
1282+
char *default_strategy = getenv("GIT_TEST_MERGE_ALGORITHM");
1283+
if (default_strategy && !strcmp(default_strategy, "ort"))
1284+
pull_twohead = "ort";
1285+
}
1286+
12671287
init_diff_ui_defaults();
12681288
git_config(git_merge_config, NULL);
12691289

builtin/rebase.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
119119
struct replay_opts replay = REPLAY_OPTS_INIT;
120120

121121
replay.action = REPLAY_INTERACTIVE_REBASE;
122+
replay.strategy = NULL;
122123
sequencer_init_config(&replay);
123124

124125
replay.signoff = opts->signoff;
@@ -136,7 +137,12 @@ static struct replay_opts get_replay_opts(const struct rebase_options *opts)
136137
opts->committer_date_is_author_date;
137138
replay.ignore_date = opts->ignore_date;
138139
replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
139-
replay.strategy = opts->strategy;
140+
if (opts->strategy)
141+
replay.strategy = opts->strategy;
142+
else if (!replay.strategy && replay.default_strategy) {
143+
replay.strategy = replay.default_strategy;
144+
replay.default_strategy = NULL;
145+
}
140146

141147
if (opts->strategy_opts)
142148
parse_strategy_opts(&replay, opts->strategy_opts);
@@ -1771,6 +1777,11 @@ int cmd_rebase(int argc, const char **argv, const char *prefix)
17711777
options.default_backend);
17721778
}
17731779

1780+
if (options.type == REBASE_MERGE &&
1781+
!options.strategy &&
1782+
getenv("GIT_TEST_MERGE_ALGORITHM"))
1783+
options.strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1784+
17741785
switch (options.type) {
17751786
case REBASE_MERGE:
17761787
case REBASE_PRESERVE_MERGES:

builtin/revert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
172172
NULL);
173173
}
174174

175+
if (!opts->strategy && opts->default_strategy) {
176+
opts->strategy = opts->default_strategy;
177+
opts->default_strategy = NULL;
178+
}
179+
175180
if (opts->allow_ff)
176181
verify_opt_compatible(me, "--ff",
177182
"--signoff", opts->signoff,
@@ -202,6 +207,8 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
202207
/* These option values will be free()d */
203208
opts->gpg_sign = xstrdup_or_null(opts->gpg_sign);
204209
opts->strategy = xstrdup_or_null(opts->strategy);
210+
if (!opts->strategy && getenv("GIT_TEST_MERGE_ALGORITHM"))
211+
opts->strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
205212

206213
if (cmd == 'q') {
207214
int ret = sequencer_remove_state(opts);

merge-ort-wrappers.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "cache.h"
2+
#include "merge-ort.h"
3+
#include "merge-ort-wrappers.h"
4+
5+
#include "commit.h"
6+
7+
static int unclean(struct merge_options *opt, struct tree *head)
8+
{
9+
/* Sanity check on repo state; index must match head */
10+
struct strbuf sb = STRBUF_INIT;
11+
12+
if (head && repo_index_has_changes(opt->repo, head, &sb)) {
13+
fprintf(stderr, _("Your local changes to the following files would be overwritten by merge:\n %s"),
14+
sb.buf);
15+
strbuf_release(&sb);
16+
return -1;
17+
}
18+
19+
return 0;
20+
}
21+
22+
int merge_ort_nonrecursive(struct merge_options *opt,
23+
struct tree *head,
24+
struct tree *merge,
25+
struct tree *merge_base)
26+
{
27+
struct merge_result result;
28+
29+
if (unclean(opt, head))
30+
return -1;
31+
32+
if (oideq(&merge_base->object.oid, &merge->object.oid)) {
33+
printf(_("Already up to date!"));
34+
return 1;
35+
}
36+
37+
memset(&result, 0, sizeof(result));
38+
merge_incore_nonrecursive(opt, merge_base, head, merge, &result);
39+
merge_switch_to_result(opt, head, &result, 1, 1);
40+
41+
return result.clean;
42+
}
43+
44+
int merge_ort_recursive(struct merge_options *opt,
45+
struct commit *side1,
46+
struct commit *side2,
47+
struct commit_list *merge_bases,
48+
struct commit **result)
49+
{
50+
struct tree *head = repo_get_commit_tree(opt->repo, side1);
51+
struct merge_result tmp;
52+
53+
if (unclean(opt, head))
54+
return -1;
55+
56+
memset(&tmp, 0, sizeof(tmp));
57+
merge_incore_recursive(opt, merge_bases, side1, side2, &tmp);
58+
merge_switch_to_result(opt, head, &tmp, 1, 1);
59+
*result = NULL;
60+
61+
return tmp.clean;
62+
}

merge-ort-wrappers.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef MERGE_ORT_WRAPPERS_H
2+
#define MERGE_ORT_WRAPPERS_H
3+
4+
#include "merge-recursive.h"
5+
6+
/*
7+
* rename-detecting three-way merge, no recursion.
8+
* Wrapper mimicking the old merge_trees() function.
9+
*/
10+
int merge_ort_nonrecursive(struct merge_options *opt,
11+
struct tree *head,
12+
struct tree *merge,
13+
struct tree *common);
14+
15+
/*
16+
* rename-detecting three-way merge with recursive ancestor consolidation.
17+
* Wrapper mimicking the old merge_recursive() function.
18+
*/
19+
int merge_ort_recursive(struct merge_options *opt,
20+
struct commit *h1,
21+
struct commit *h2,
22+
struct commit_list *ancestors,
23+
struct commit **result);
24+
25+
#endif

merge-ort.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* "Ostensibly Recursive's Twin" merge strategy, or "ort" for short. Meant
3+
* as a drop-in replacement for the "recursive" merge strategy, allowing one
4+
* to replace
5+
*
6+
* git merge [-s recursive]
7+
*
8+
* with
9+
*
10+
* git merge -s ort
11+
*
12+
* Note: git's parser allows the space between '-s' and its argument to be
13+
* missing. (Should I have backronymed "ham", "alsa", "kip", "nap, "alvo",
14+
* "cale", "peedy", or "ins" instead of "ort"?)
15+
*/
16+
17+
#include "cache.h"
18+
#include "merge-ort.h"
19+
20+
void merge_switch_to_result(struct merge_options *opt,
21+
struct tree *head,
22+
struct merge_result *result,
23+
int update_worktree_and_index,
24+
int display_update_msgs)
25+
{
26+
die("Not yet implemented");
27+
merge_finalize(opt, result);
28+
}
29+
30+
void merge_finalize(struct merge_options *opt,
31+
struct merge_result *result)
32+
{
33+
die("Not yet implemented");
34+
}
35+
36+
void merge_incore_nonrecursive(struct merge_options *opt,
37+
struct tree *merge_base,
38+
struct tree *side1,
39+
struct tree *side2,
40+
struct merge_result *result)
41+
{
42+
die("Not yet implemented");
43+
}
44+
45+
void merge_incore_recursive(struct merge_options *opt,
46+
struct commit_list *merge_bases,
47+
struct commit *side1,
48+
struct commit *side2,
49+
struct merge_result *result)
50+
{
51+
die("Not yet implemented");
52+
}

merge-ort.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef MERGE_ORT_H
2+
#define MERGE_ORT_H
3+
4+
#include "merge-recursive.h"
5+
6+
struct commit;
7+
struct tree;
8+
9+
struct merge_result {
10+
/* Whether the merge is clean */
11+
int clean;
12+
13+
/*
14+
* Result of merge. If !clean, represents what would go in worktree
15+
* (thus possibly including files containing conflict markers).
16+
*/
17+
struct tree *tree;
18+
19+
/*
20+
* Additional metadata used by merge_switch_to_result() or future calls
21+
* to merge_incore_*(). Includes data needed to update the index (if
22+
* !clean) and to print "CONFLICT" messages. Not for external use.
23+
*/
24+
void *priv;
25+
};
26+
27+
/*
28+
* rename-detecting three-way merge with recursive ancestor consolidation.
29+
* working tree and index are untouched.
30+
*/
31+
void merge_incore_recursive(struct merge_options *opt,
32+
struct commit_list *merge_bases,
33+
struct commit *side1,
34+
struct commit *side2,
35+
struct merge_result *result);
36+
37+
/*
38+
* rename-detecting three-way merge, no recursion.
39+
* working tree and index are untouched.
40+
*/
41+
void merge_incore_nonrecursive(struct merge_options *opt,
42+
struct tree *merge_base,
43+
struct tree *side1,
44+
struct tree *side2,
45+
struct merge_result *result);
46+
47+
/* Update the working tree and index from head to result after incore merge */
48+
void merge_switch_to_result(struct merge_options *opt,
49+
struct tree *head,
50+
struct merge_result *result,
51+
int update_worktree_and_index,
52+
int display_update_msgs);
53+
54+
/* Do needed cleanup when not calling merge_switch_to_result() */
55+
void merge_finalize(struct merge_options *opt,
56+
struct merge_result *result);
57+
58+
#endif

0 commit comments

Comments
 (0)