send-email: fix broken transferEncoding tests
[git] / merge.c
1 #include "cache.h"
2 #include "diff.h"
3 #include "diffcore.h"
4 #include "lockfile.h"
5 #include "commit.h"
6 #include "run-command.h"
7 #include "resolve-undo.h"
8 #include "tree-walk.h"
9 #include "unpack-trees.h"
10 #include "dir.h"
11
12 static const char *merge_argument(struct commit *commit)
13 {
14         return oid_to_hex(commit ? &commit->object.oid : the_hash_algo->empty_tree);
15 }
16
17 int index_has_changes(struct strbuf *sb)
18 {
19         struct object_id head;
20         int i;
21
22         if (!get_oid_tree("HEAD", &head)) {
23                 struct diff_options opt;
24
25                 diff_setup(&opt);
26                 opt.flags.exit_with_status = 1;
27                 if (!sb)
28                         opt.flags.quick = 1;
29                 do_diff_cache(&head, &opt);
30                 diffcore_std(&opt);
31                 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
32                         if (i)
33                                 strbuf_addch(sb, ' ');
34                         strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
35                 }
36                 diff_flush(&opt);
37                 return opt.flags.has_changes != 0;
38         } else {
39                 for (i = 0; sb && i < active_nr; i++) {
40                         if (i)
41                                 strbuf_addch(sb, ' ');
42                         strbuf_addstr(sb, active_cache[i]->name);
43                 }
44                 return !!active_nr;
45         }
46 }
47
48 int try_merge_command(const char *strategy, size_t xopts_nr,
49                       const char **xopts, struct commit_list *common,
50                       const char *head_arg, struct commit_list *remotes)
51 {
52         struct argv_array args = ARGV_ARRAY_INIT;
53         int i, ret;
54         struct commit_list *j;
55
56         argv_array_pushf(&args, "merge-%s", strategy);
57         for (i = 0; i < xopts_nr; i++)
58                 argv_array_pushf(&args, "--%s", xopts[i]);
59         for (j = common; j; j = j->next)
60                 argv_array_push(&args, merge_argument(j->item));
61         argv_array_push(&args, "--");
62         argv_array_push(&args, head_arg);
63         for (j = remotes; j; j = j->next)
64                 argv_array_push(&args, merge_argument(j->item));
65
66         ret = run_command_v_opt(args.argv, RUN_GIT_CMD);
67         argv_array_clear(&args);
68
69         discard_cache();
70         if (read_cache() < 0)
71                 die(_("failed to read the cache"));
72         resolve_undo_clear();
73
74         return ret;
75 }
76
77 int checkout_fast_forward(const struct object_id *head,
78                           const struct object_id *remote,
79                           int overwrite_ignore)
80 {
81         struct tree *trees[MAX_UNPACK_TREES];
82         struct unpack_trees_options opts;
83         struct tree_desc t[MAX_UNPACK_TREES];
84         int i, nr_trees = 0;
85         struct dir_struct dir;
86         struct lock_file lock_file = LOCK_INIT;
87
88         refresh_cache(REFRESH_QUIET);
89
90         if (hold_locked_index(&lock_file, LOCK_REPORT_ON_ERROR) < 0)
91                 return -1;
92
93         memset(&trees, 0, sizeof(trees));
94         memset(&t, 0, sizeof(t));
95
96         trees[nr_trees] = parse_tree_indirect(head);
97         if (!trees[nr_trees++]) {
98                 rollback_lock_file(&lock_file);
99                 return -1;
100         }
101         trees[nr_trees] = parse_tree_indirect(remote);
102         if (!trees[nr_trees++]) {
103                 rollback_lock_file(&lock_file);
104                 return -1;
105         }
106         for (i = 0; i < nr_trees; i++) {
107                 parse_tree(trees[i]);
108                 init_tree_desc(t+i, trees[i]->buffer, trees[i]->size);
109         }
110
111         memset(&opts, 0, sizeof(opts));
112         if (overwrite_ignore) {
113                 memset(&dir, 0, sizeof(dir));
114                 dir.flags |= DIR_SHOW_IGNORED;
115                 setup_standard_excludes(&dir);
116                 opts.dir = &dir;
117         }
118
119         opts.head_idx = 1;
120         opts.src_index = &the_index;
121         opts.dst_index = &the_index;
122         opts.update = 1;
123         opts.verbose_update = 1;
124         opts.merge = 1;
125         opts.fn = twoway_merge;
126         setup_unpack_trees_porcelain(&opts, "merge");
127
128         if (unpack_trees(nr_trees, t, &opts)) {
129                 rollback_lock_file(&lock_file);
130                 clear_unpack_trees_porcelain(&opts);
131                 return -1;
132         }
133         clear_unpack_trees_porcelain(&opts);
134
135         if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
136                 return error(_("unable to write new index file"));
137         return 0;
138 }