3 #include "run-command.h"
4 #include "cache-tree.h"
6 #include "merge-recursive.h"
8 static struct commit
*commit
;
10 static char *get_oneline(const char *message
)
13 const char *p
= message
, *abbrev
, *eol
;
14 int abbrev_len
, oneline_len
;
18 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
23 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
27 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
28 abbrev_len
= strlen(abbrev
);
29 oneline_len
= eol
- p
;
30 result
= xmalloc(abbrev_len
+ 5 + oneline_len
);
31 memcpy(result
, abbrev
, abbrev_len
);
32 memcpy(result
+ abbrev_len
, "... ", 4);
33 memcpy(result
+ abbrev_len
+ 4, p
, oneline_len
);
34 result
[abbrev_len
+ 4 + oneline_len
] = '\0';
38 static void add_message_to_msg(struct strbuf
*msg
, const char *message
)
40 const char *p
= message
;
41 while (*p
&& (*p
!= '\n' || p
[1] != '\n'))
45 strbuf_addstr(msg
, sha1_to_hex(commit
->object
.sha1
));
48 strbuf_addstr(msg
, p
);
52 static struct tree
*empty_tree(void)
54 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
56 tree
->object
.parsed
= 1;
57 tree
->object
.type
= OBJ_TREE
;
58 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
63 * Pick changes introduced by "commit" argument into current working
66 * It starts from the current index (not HEAD), and allow the effect
67 * of one commit replayed (either forward or backward) to that state,
68 * leaving the result in the index.
70 * You do not have to start from a commit, so you can replay many
71 * commits to the index in sequence without commiting in between to
72 * squash multiple steps if you wanted to.
74 * Return 0 on success.
75 * Return negative value on error before picking,
76 * and a positive value after picking,
77 * and return 1 if and only if a conflict occurs but no other error.
79 int pick_commit(struct commit
*pick_commit
, int mainline
, int flags
,
82 unsigned char head
[20];
83 struct commit
*base
, *next
, *parent
;
84 int i
, index_fd
, clean
;
88 struct merge_options o
;
89 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
90 static struct lock_file index_lock
;
96 * Let's compute the tree that represents the "current" state
97 * for merge-recursive to work on.
99 if (write_cache_as_tree(head
, 0, NULL
))
100 return error("Your index file is unmerged.");
103 index_fd
= hold_locked_index(&index_lock
, 0);
105 return error("Unable to create locked index: %s",
108 if (!commit
->parents
) {
109 if (flags
& PICK_REVERSE
)
110 return error("Cannot revert a root commit");
113 else if (commit
->parents
->next
) {
114 /* Reverting or cherry-picking a merge commit */
116 struct commit_list
*p
;
119 return error("Commit %s is a merge but no mainline was given.",
120 sha1_to_hex(commit
->object
.sha1
));
122 for (cnt
= 1, p
= commit
->parents
;
123 cnt
!= mainline
&& p
;
126 if (cnt
!= mainline
|| !p
)
127 return error("Commit %s does not have parent %d",
128 sha1_to_hex(commit
->object
.sha1
),
131 } else if (0 < mainline
)
132 return error("Mainline was specified but commit %s is not a merge.",
133 sha1_to_hex(commit
->object
.sha1
));
135 parent
= commit
->parents
->item
;
137 if (!(message
= commit
->buffer
))
138 return error("Cannot get commit message for %s",
139 sha1_to_hex(commit
->object
.sha1
));
141 if (parent
&& parse_commit(parent
) < 0)
142 return error("Cannot parse parent commit %s",
143 sha1_to_hex(parent
->object
.sha1
));
145 oneline
= get_oneline(message
);
147 if (flags
& PICK_REVERSE
) {
148 char *oneline_body
= strchr(oneline
, ' ');
152 strbuf_addstr(msg
, "Revert \"");
153 strbuf_addstr(msg
, oneline_body
+ 1);
154 strbuf_addstr(msg
, "\"\n\nThis reverts commit ");
155 strbuf_addstr(msg
, sha1_to_hex(commit
->object
.sha1
));
157 if (commit
->parents
->next
) {
158 strbuf_addstr(msg
, ", reversing\nchanges made to ");
159 strbuf_addstr(msg
, sha1_to_hex(parent
->object
.sha1
));
161 strbuf_addstr(msg
, ".\n");
165 add_message_to_msg(msg
, message
);
166 if (flags
& PICK_ADD_NOTE
) {
167 strbuf_addstr(msg
, "(cherry picked from commit ");
168 strbuf_addstr(msg
, sha1_to_hex(commit
->object
.sha1
));
169 strbuf_addstr(msg
, ")\n");
174 init_merge_options(&o
);
178 head_tree
= parse_tree_indirect(head
);
179 next_tree
= next
? next
->tree
: empty_tree();
180 base_tree
= base
? base
->tree
: empty_tree();
182 clean
= merge_trees(&o
,
184 next_tree
, base_tree
, &result
);
185 free_merge_options(&o
);
187 if (active_cache_changed
&&
188 (write_cache(index_fd
, active_cache
, active_nr
) ||
189 commit_locked_index(&index_lock
))) {
190 error("Unable to write new index file");
193 rollback_lock_file(&index_lock
);
196 strbuf_addstr(msg
, "\nConflicts:\n\n");
197 for (i
= 0; i
< active_nr
;) {
198 struct cache_entry
*ce
= active_cache
[i
++];
200 strbuf_addstr(msg
, "\t");
201 strbuf_addstr(msg
, ce
->name
);
202 strbuf_addstr(msg
, "\n");
203 while (i
< active_nr
&& !strcmp(ce
->name
,
204 active_cache
[i
]->name
))
213 if (read_cache() < 0) {
214 error("Cannot read the index");