8 #include "run-command.h"
9 #include "resolve-undo.h"
10 #include "tree-walk.h"
11 #include "unpack-trees.h"
14 static const char *merge_argument(struct commit
*commit
)
16 return oid_to_hex(commit
? &commit
->object
.oid
: the_hash_algo
->empty_tree
);
19 int try_merge_command(struct repository
*r
,
20 const char *strategy
, size_t xopts_nr
,
21 const char **xopts
, struct commit_list
*common
,
22 const char *head_arg
, struct commit_list
*remotes
)
24 struct child_process cmd
= CHILD_PROCESS_INIT
;
26 struct commit_list
*j
;
28 strvec_pushf(&cmd
.args
, "merge-%s", strategy
);
29 for (i
= 0; i
< xopts_nr
; i
++)
30 strvec_pushf(&cmd
.args
, "--%s", xopts
[i
]);
31 for (j
= common
; j
; j
= j
->next
)
32 strvec_push(&cmd
.args
, merge_argument(j
->item
));
33 strvec_push(&cmd
.args
, "--");
34 strvec_push(&cmd
.args
, head_arg
);
35 for (j
= remotes
; j
; j
= j
->next
)
36 strvec_push(&cmd
.args
, merge_argument(j
->item
));
39 ret
= run_command(&cmd
);
41 discard_index(r
->index
);
42 if (repo_read_index(r
) < 0)
43 die(_("failed to read the cache"));
44 resolve_undo_clear_index(r
->index
);
49 int checkout_fast_forward(struct repository
*r
,
50 const struct object_id
*head
,
51 const struct object_id
*remote
,
54 struct tree
*trees
[MAX_UNPACK_TREES
];
55 struct unpack_trees_options opts
;
56 struct tree_desc t
[MAX_UNPACK_TREES
];
58 struct lock_file lock_file
= LOCK_INIT
;
60 refresh_index(r
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
62 if (repo_hold_locked_index(r
, &lock_file
, LOCK_REPORT_ON_ERROR
) < 0)
65 memset(&trees
, 0, sizeof(trees
));
66 memset(&t
, 0, sizeof(t
));
68 trees
[nr_trees
] = parse_tree_indirect(head
);
69 if (!trees
[nr_trees
++]) {
70 rollback_lock_file(&lock_file
);
73 trees
[nr_trees
] = parse_tree_indirect(remote
);
74 if (!trees
[nr_trees
++]) {
75 rollback_lock_file(&lock_file
);
78 for (i
= 0; i
< nr_trees
; i
++) {
80 init_tree_desc(t
+i
, trees
[i
]->buffer
, trees
[i
]->size
);
83 memset(&opts
, 0, sizeof(opts
));
84 opts
.preserve_ignored
= !overwrite_ignore
;
87 opts
.src_index
= r
->index
;
88 opts
.dst_index
= r
->index
;
90 opts
.verbose_update
= 1;
92 opts
.fn
= twoway_merge
;
93 init_checkout_metadata(&opts
.meta
, NULL
, remote
, NULL
);
94 setup_unpack_trees_porcelain(&opts
, "merge");
96 if (unpack_trees(nr_trees
, t
, &opts
)) {
97 rollback_lock_file(&lock_file
);
98 clear_unpack_trees_porcelain(&opts
);
101 clear_unpack_trees_porcelain(&opts
);
103 if (write_locked_index(r
->index
, &lock_file
, COMMIT_LOCK
))
104 return error(_("unable to write new index file"));