Merge branch 'seq-builtin-dev' into mymaster
[git/sbeyer.git] / builtin-revert.c
blob4797ac53e7a4bf5f82a1e6f9ba24132469d2ef09
1 #include "cache.h"
2 #include "builtin.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "exec_cmd.h"
6 #include "utf8.h"
7 #include "parse-options.h"
8 #include "diff.h"
9 #include "revision.h"
10 #include "rerere.h"
11 #include "pick.h"
14 * This implements the builtins revert and cherry-pick.
16 * Copyright (c) 2007 Johannes E. Schindelin
18 * Based on git-revert.sh, which is
20 * Copyright (c) 2005 Linus Torvalds
21 * Copyright (c) 2005 Junio C Hamano
24 static const char * const revert_usage[] = {
25 "git revert [options] <commit-ish>",
26 NULL
29 static const char * const cherry_pick_usage[] = {
30 "git cherry-pick [options] <commit-ish>",
31 NULL
34 static int edit, no_commit, mainline, signoff;
35 static int flags;
36 static struct commit *commit;
38 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
40 static void parse_args(int argc, const char **argv)
42 const char * const * usage_str =
43 flags & PICK_REVERSE ? revert_usage : cherry_pick_usage;
44 unsigned char sha1[20];
45 const char *arg;
46 int noop;
47 struct option options[] = {
48 OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
49 OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
50 OPT_BIT('x', NULL, &flags, "append commit name when cherry-picking", PICK_ADD_NOTE),
51 OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
52 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
53 OPT_INTEGER('m', "mainline", &mainline, "parent number"),
54 OPT_END(),
57 if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1)
58 usage_with_options(usage_str, options);
59 arg = argv[0];
61 if (get_sha1(arg, sha1))
62 die ("Cannot find '%s'", arg);
63 commit = (struct commit *)parse_object(sha1);
64 if (!commit)
65 die ("Could not find %s", sha1_to_hex(sha1));
66 if (commit->object.type == OBJ_TAG) {
67 commit = (struct commit *)
68 deref_tag((struct object *)commit, arg, strlen(arg));
70 if (commit->object.type != OBJ_COMMIT)
71 die ("'%s' does not point to a commit", arg);
74 static char *get_encoding(const char *message)
76 const char *p = message, *eol;
78 if (!p)
79 return NULL;
80 while (*p && *p != '\n') {
81 for (eol = p + 1; *eol && *eol != '\n'; eol++)
82 ; /* do nothing */
83 if (!prefixcmp(p, "encoding ")) {
84 char *result = xmalloc(eol - 8 - p);
85 strlcpy(result, p + 9, eol - 8 - p);
86 return result;
88 p = eol;
89 if (*p == '\n')
90 p++;
92 return NULL;
95 static void set_author_ident_env(const char *message)
97 const char *p = message;
98 if (!p)
99 die ("Could not read commit message of %s",
100 sha1_to_hex(commit->object.sha1));
101 while (*p && *p != '\n') {
102 const char *eol;
104 for (eol = p; *eol && *eol != '\n'; eol++)
105 ; /* do nothing */
106 if (!prefixcmp(p, "author ")) {
107 char *line, *pend, *email, *timestamp;
109 p += 7;
110 line = xmemdupz(p, eol - p);
111 email = strchr(line, '<');
112 if (!email)
113 die ("Could not extract author email from %s",
114 sha1_to_hex(commit->object.sha1));
115 if (email == line)
116 pend = line;
117 else
118 for (pend = email; pend != line + 1 &&
119 isspace(pend[-1]); pend--);
120 ; /* do nothing */
121 *pend = '\0';
122 email++;
123 timestamp = strchr(email, '>');
124 if (!timestamp)
125 die ("Could not extract author time from %s",
126 sha1_to_hex(commit->object.sha1));
127 *timestamp = '\0';
128 for (timestamp++; *timestamp && isspace(*timestamp);
129 timestamp++)
130 ; /* do nothing */
131 setenv("GIT_AUTHOR_NAME", line, 1);
132 setenv("GIT_AUTHOR_EMAIL", email, 1);
133 setenv("GIT_AUTHOR_DATE", timestamp, 1);
134 free(line);
135 return;
137 p = eol;
138 if (*p == '\n')
139 p++;
141 die ("No author information found in %s",
142 sha1_to_hex(commit->object.sha1));
145 static char *help_msg(const unsigned char *sha1)
147 static char helpbuf[1024];
148 char *msg = getenv("GIT_CHERRY_PICK_HELP");
150 if (msg)
151 return msg;
153 strcpy(helpbuf, " After resolving the conflicts,\n"
154 "mark the corrected paths with 'git add <paths>' "
155 "or 'git rm <paths>' and commit the result.");
157 if (!(flags & PICK_REVERSE)) {
158 sprintf(helpbuf + strlen(helpbuf),
159 "\nWhen commiting, use the option "
160 "'-c %s' to retain authorship and message.",
161 find_unique_abbrev(sha1, DEFAULT_ABBREV));
163 return helpbuf;
166 static void write_message(struct strbuf *msgbuf, const char *filename)
168 struct lock_file msg_file;
169 int msg_fd;
170 msg_fd = hold_lock_file_for_update(&msg_file, filename,
171 LOCK_DIE_ON_ERROR);
172 if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
173 die_errno("Could not write to %s.", filename);
174 strbuf_release(msgbuf);
175 if (commit_lock_file(&msg_file) < 0)
176 die("Error wrapping up %s", filename);
179 static int revert_or_cherry_pick(int argc, const char **argv)
181 const char *me;
182 struct strbuf msgbuf;
183 char *reencoded_message = NULL;
184 const char *encoding;
185 int failed;
187 git_config(git_default_config, NULL);
188 me = flags & PICK_REVERSE ? "revert" : "cherry-pick";
189 setenv(GIT_REFLOG_ACTION, me, 0);
190 parse_args(argc, argv);
192 if (read_cache() < 0)
193 die("git %s: failed to read the index", me);
194 if (!no_commit && index_differs_from("HEAD", 0))
195 die ("Dirty index: cannot %s", me);
197 if (!commit->buffer)
198 return error("Cannot get commit message for %s",
199 sha1_to_hex(commit->object.sha1));
200 encoding = get_encoding(commit->buffer);
201 if (!encoding)
202 encoding = "UTF-8";
203 if (!git_commit_encoding)
204 git_commit_encoding = "UTF-8";
205 if ((reencoded_message = reencode_string(commit->buffer,
206 git_commit_encoding, encoding)))
207 commit->buffer = reencoded_message;
209 failed = pick_commit(commit, mainline, flags, &msgbuf);
210 if (failed < 0) {
211 exit(1);
212 } else if (failed > 0) {
213 fprintf(stderr, "Automatic %s failed.%s\n",
214 me, help_msg(commit->object.sha1));
215 write_message(&msgbuf, git_path("MERGE_MSG"));
216 rerere();
217 exit(1);
219 if (!(flags & PICK_REVERSE))
220 set_author_ident_env(commit->buffer);
221 free(reencoded_message);
223 fprintf(stderr, "Finished one %s.\n", me);
225 write_message(&msgbuf, git_path("MERGE_MSG"));
228 * If we are cherry-pick, and if the merge did not result in
229 * hand-editing, we will hit this commit and inherit the original
230 * author date and name.
231 * If we are revert, or if our cherry-pick results in a hand merge,
232 * we had better say that the current user is responsible for that.
235 if (!no_commit) {
236 /* 6 is max possible length of our args array including NULL */
237 const char *args[6];
238 int i = 0;
239 args[i++] = "commit";
240 args[i++] = "-n";
241 if (signoff)
242 args[i++] = "-s";
243 if (!edit) {
244 args[i++] = "-F";
245 args[i++] = git_path("MERGE_MSG");
247 args[i] = NULL;
248 return execv_git_cmd(args);
250 return 0;
253 int cmd_revert(int argc, const char **argv, const char *prefix)
255 if (isatty(0))
256 edit = 1;
257 flags = PICK_REVERSE | PICK_ADD_NOTE;
258 return revert_or_cherry_pick(argc, argv);
261 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
263 flags = 0;
264 return revert_or_cherry_pick(argc, argv);