Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 5c860e29 2018-03-12 stsp
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 c09a553d 2018-03-12 stsp #include "got_worktree.h"
45 79109fed 2018-03-27 stsp #include "got_diff.h"
46 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
47 404c43c4 2018-06-21 stsp #include "got_blame.h"
48 63219cd2 2019-01-04 stsp #include "got_privsep.h"
49 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
50 5c860e29 2018-03-12 stsp
51 5c860e29 2018-03-12 stsp #ifndef nitems
52 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 5c860e29 2018-03-12 stsp #endif
54 99437157 2018-11-11 stsp
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
57 99437157 2018-11-11 stsp
58 99437157 2018-11-11 stsp static void
59 99437157 2018-11-11 stsp catch_sigint(int signo)
60 99437157 2018-11-11 stsp {
61 99437157 2018-11-11 stsp sigint_received = 1;
62 99437157 2018-11-11 stsp }
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static void
65 99437157 2018-11-11 stsp catch_sigpipe(int signo)
66 99437157 2018-11-11 stsp {
67 99437157 2018-11-11 stsp sigpipe_received = 1;
68 99437157 2018-11-11 stsp }
69 5c860e29 2018-03-12 stsp
70 99437157 2018-11-11 stsp
71 8cfb4057 2019-07-09 stsp struct got_cmd {
72 5c860e29 2018-03-12 stsp const char *cmd_name;
73 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
75 97b3a7be 2019-07-09 stsp const char *cmd_alias;
76 5c860e29 2018-03-12 stsp };
77 5c860e29 2018-03-12 stsp
78 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
79 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
80 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
82 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
85 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
86 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
87 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
88 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
89 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
90 d00136be 2019-03-26 stsp __dead static void usage_add(void);
91 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
92 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
93 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
94 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
95 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
96 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
97 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
98 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
99 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
100 5c860e29 2018-03-12 stsp
101 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
102 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
104 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
106 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
107 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
108 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
110 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
111 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
112 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
113 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
114 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
115 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
116 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
117 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
118 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
119 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
120 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
121 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
122 5c860e29 2018-03-12 stsp
123 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
124 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
125 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
126 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
127 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
128 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
129 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
130 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
131 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
132 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
133 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
134 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
135 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
136 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
137 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
138 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
139 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
141 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
142 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
143 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
144 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
145 5c860e29 2018-03-12 stsp };
146 ce5b7c56 2019-07-09 stsp
147 ce5b7c56 2019-07-09 stsp static void
148 ce5b7c56 2019-07-09 stsp list_commands(void)
149 ce5b7c56 2019-07-09 stsp {
150 ce5b7c56 2019-07-09 stsp int i;
151 ce5b7c56 2019-07-09 stsp
152 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
153 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
154 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
155 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
156 ce5b7c56 2019-07-09 stsp }
157 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
158 ce5b7c56 2019-07-09 stsp }
159 5c860e29 2018-03-12 stsp
160 5c860e29 2018-03-12 stsp int
161 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
162 5c860e29 2018-03-12 stsp {
163 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
164 5c860e29 2018-03-12 stsp unsigned int i;
165 5c860e29 2018-03-12 stsp int ch;
166 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
167 5c860e29 2018-03-12 stsp
168 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
169 5c860e29 2018-03-12 stsp
170 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
171 5c860e29 2018-03-12 stsp switch (ch) {
172 1b6b95a8 2018-03-12 stsp case 'h':
173 1b6b95a8 2018-03-12 stsp hflag = 1;
174 53ccebc2 2019-07-30 stsp break;
175 53ccebc2 2019-07-30 stsp case 'V':
176 53ccebc2 2019-07-30 stsp Vflag = 1;
177 1b6b95a8 2018-03-12 stsp break;
178 5c860e29 2018-03-12 stsp default:
179 ce5b7c56 2019-07-09 stsp usage(hflag);
180 5c860e29 2018-03-12 stsp /* NOTREACHED */
181 5c860e29 2018-03-12 stsp }
182 5c860e29 2018-03-12 stsp }
183 5c860e29 2018-03-12 stsp
184 5c860e29 2018-03-12 stsp argc -= optind;
185 5c860e29 2018-03-12 stsp argv += optind;
186 1e70621d 2018-03-27 stsp optind = 0;
187 53ccebc2 2019-07-30 stsp
188 53ccebc2 2019-07-30 stsp if (Vflag) {
189 53ccebc2 2019-07-30 stsp got_version_print_str();
190 53ccebc2 2019-07-30 stsp return 1;
191 53ccebc2 2019-07-30 stsp }
192 5c860e29 2018-03-12 stsp
193 5c860e29 2018-03-12 stsp if (argc <= 0)
194 ce5b7c56 2019-07-09 stsp usage(hflag);
195 5c860e29 2018-03-12 stsp
196 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
197 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
198 99437157 2018-11-11 stsp
199 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
200 d7d4f210 2018-03-12 stsp const struct got_error *error;
201 d7d4f210 2018-03-12 stsp
202 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
203 5c860e29 2018-03-12 stsp
204 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
206 5c860e29 2018-03-12 stsp continue;
207 5c860e29 2018-03-12 stsp
208 1b6b95a8 2018-03-12 stsp if (hflag)
209 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
210 1b6b95a8 2018-03-12 stsp
211 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
212 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
213 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
214 d7d4f210 2018-03-12 stsp return 1;
215 d7d4f210 2018-03-12 stsp }
216 d7d4f210 2018-03-12 stsp
217 d7d4f210 2018-03-12 stsp return 0;
218 5c860e29 2018-03-12 stsp }
219 5c860e29 2018-03-12 stsp
220 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
221 ce5b7c56 2019-07-09 stsp list_commands();
222 5c860e29 2018-03-12 stsp return 1;
223 5c860e29 2018-03-12 stsp }
224 5c860e29 2018-03-12 stsp
225 4ed7e80c 2018-05-20 stsp __dead static void
226 ce5b7c56 2019-07-09 stsp usage(int hflag)
227 5c860e29 2018-03-12 stsp {
228 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 53ccebc2 2019-07-30 stsp getprogname());
230 ce5b7c56 2019-07-09 stsp if (hflag)
231 ce5b7c56 2019-07-09 stsp list_commands();
232 5c860e29 2018-03-12 stsp exit(1);
233 5c860e29 2018-03-12 stsp }
234 5c860e29 2018-03-12 stsp
235 0266afb7 2019-01-04 stsp static const struct got_error *
236 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
237 e2ba3d07 2019-05-13 stsp {
238 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
239 e2ba3d07 2019-05-13 stsp const char *editor;
240 e2ba3d07 2019-05-13 stsp
241 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
242 e2ba3d07 2019-05-13 stsp if (editor == NULL)
243 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
244 e2ba3d07 2019-05-13 stsp
245 0ee7065d 2019-05-13 stsp if (editor) {
246 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
247 0ee7065d 2019-05-13 stsp if (err)
248 0ee7065d 2019-05-13 stsp return err;
249 0ee7065d 2019-05-13 stsp }
250 e2ba3d07 2019-05-13 stsp
251 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
252 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
253 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
254 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
255 0ee7065d 2019-05-13 stsp }
256 0ee7065d 2019-05-13 stsp
257 e2ba3d07 2019-05-13 stsp return NULL;
258 e2ba3d07 2019-05-13 stsp }
259 e2ba3d07 2019-05-13 stsp
260 e2ba3d07 2019-05-13 stsp static const struct got_error *
261 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
262 c530dc23 2019-07-23 stsp const char *worktree_path)
263 0266afb7 2019-01-04 stsp {
264 163ce85a 2019-05-13 stsp const struct got_error *err;
265 0266afb7 2019-01-04 stsp
266 37c06ea4 2019-07-15 stsp #ifdef PROFILE
267 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
268 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
269 37c06ea4 2019-07-15 stsp #endif
270 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
272 0266afb7 2019-01-04 stsp
273 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
275 0266afb7 2019-01-04 stsp
276 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
277 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
278 0266afb7 2019-01-04 stsp
279 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
280 163ce85a 2019-05-13 stsp if (err != NULL)
281 163ce85a 2019-05-13 stsp return err;
282 0266afb7 2019-01-04 stsp
283 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
284 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
285 0266afb7 2019-01-04 stsp
286 0266afb7 2019-01-04 stsp return NULL;
287 2c7829a4 2019-06-17 stsp }
288 2c7829a4 2019-06-17 stsp
289 2c7829a4 2019-06-17 stsp __dead static void
290 2c7829a4 2019-06-17 stsp usage_init(void)
291 2c7829a4 2019-06-17 stsp {
292 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
293 2c7829a4 2019-06-17 stsp exit(1);
294 2c7829a4 2019-06-17 stsp }
295 2c7829a4 2019-06-17 stsp
296 2c7829a4 2019-06-17 stsp static const struct got_error *
297 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
298 2c7829a4 2019-06-17 stsp {
299 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
300 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
301 2c7829a4 2019-06-17 stsp int ch;
302 2c7829a4 2019-06-17 stsp
303 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
304 2c7829a4 2019-06-17 stsp switch (ch) {
305 2c7829a4 2019-06-17 stsp default:
306 2c7829a4 2019-06-17 stsp usage_init();
307 2c7829a4 2019-06-17 stsp /* NOTREACHED */
308 2c7829a4 2019-06-17 stsp }
309 2c7829a4 2019-06-17 stsp }
310 2c7829a4 2019-06-17 stsp
311 2c7829a4 2019-06-17 stsp argc -= optind;
312 2c7829a4 2019-06-17 stsp argv += optind;
313 2c7829a4 2019-06-17 stsp
314 2c7829a4 2019-06-17 stsp #ifndef PROFILE
315 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
316 2c7829a4 2019-06-17 stsp err(1, "pledge");
317 2c7829a4 2019-06-17 stsp #endif
318 2c7829a4 2019-06-17 stsp if (argc != 1)
319 bc20e173 2019-06-17 stsp usage_init();
320 2c7829a4 2019-06-17 stsp
321 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
322 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
323 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
324 2c7829a4 2019-06-17 stsp
325 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
326 2c7829a4 2019-06-17 stsp
327 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
328 2c7829a4 2019-06-17 stsp if (error &&
329 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
330 2c7829a4 2019-06-17 stsp goto done;
331 2c7829a4 2019-06-17 stsp
332 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
333 2c7829a4 2019-06-17 stsp if (error)
334 2c7829a4 2019-06-17 stsp goto done;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
337 2c7829a4 2019-06-17 stsp if (error != NULL)
338 3ce1b845 2019-07-15 stsp goto done;
339 3ce1b845 2019-07-15 stsp
340 3ce1b845 2019-07-15 stsp done:
341 3ce1b845 2019-07-15 stsp free(repo_path);
342 3ce1b845 2019-07-15 stsp return error;
343 3ce1b845 2019-07-15 stsp }
344 3ce1b845 2019-07-15 stsp
345 3ce1b845 2019-07-15 stsp __dead static void
346 3ce1b845 2019-07-15 stsp usage_import(void)
347 3ce1b845 2019-07-15 stsp {
348 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
350 3ce1b845 2019-07-15 stsp exit(1);
351 3ce1b845 2019-07-15 stsp }
352 3ce1b845 2019-07-15 stsp
353 3ce1b845 2019-07-15 stsp int
354 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
355 3ce1b845 2019-07-15 stsp {
356 3ce1b845 2019-07-15 stsp pid_t pid;
357 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
358 3ce1b845 2019-07-15 stsp int st = -1;
359 3ce1b845 2019-07-15 stsp
360 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
361 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
362 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
363 3ce1b845 2019-07-15 stsp
364 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
365 3ce1b845 2019-07-15 stsp case -1:
366 3ce1b845 2019-07-15 stsp goto doneediting;
367 3ce1b845 2019-07-15 stsp case 0:
368 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
369 3ce1b845 2019-07-15 stsp _exit(127);
370 3ce1b845 2019-07-15 stsp }
371 3ce1b845 2019-07-15 stsp
372 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
373 3ce1b845 2019-07-15 stsp if (errno != EINTR)
374 3ce1b845 2019-07-15 stsp break;
375 3ce1b845 2019-07-15 stsp
376 3ce1b845 2019-07-15 stsp doneediting:
377 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
378 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
379 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
380 3ce1b845 2019-07-15 stsp
381 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
382 3ce1b845 2019-07-15 stsp errno = EINTR;
383 3ce1b845 2019-07-15 stsp return -1;
384 3ce1b845 2019-07-15 stsp }
385 3ce1b845 2019-07-15 stsp
386 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
387 3ce1b845 2019-07-15 stsp }
388 3ce1b845 2019-07-15 stsp
389 3ce1b845 2019-07-15 stsp static const struct got_error *
390 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 3ce1b845 2019-07-15 stsp const char *initial_content)
392 3ce1b845 2019-07-15 stsp {
393 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
394 3ce1b845 2019-07-15 stsp char buf[1024];
395 3ce1b845 2019-07-15 stsp struct stat st, st2;
396 3ce1b845 2019-07-15 stsp FILE *fp;
397 3ce1b845 2019-07-15 stsp int content_changed = 0;
398 3ce1b845 2019-07-15 stsp size_t len;
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp *logmsg = NULL;
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
403 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
404 3ce1b845 2019-07-15 stsp
405 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
406 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
407 3ce1b845 2019-07-15 stsp
408 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
409 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
416 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
417 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
418 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
419 3ce1b845 2019-07-15 stsp len = 0;
420 3ce1b845 2019-07-15 stsp
421 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
422 3ce1b845 2019-07-15 stsp if (fp == NULL) {
423 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
424 3ce1b845 2019-07-15 stsp goto done;
425 3ce1b845 2019-07-15 stsp }
426 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
427 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
428 3ce1b845 2019-07-15 stsp content_changed = 1;
429 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
431 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
432 3ce1b845 2019-07-15 stsp }
433 3ce1b845 2019-07-15 stsp fclose(fp);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
437 3ce1b845 2019-07-15 stsp len--;
438 3ce1b845 2019-07-15 stsp }
439 3ce1b845 2019-07-15 stsp
440 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
441 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
443 3ce1b845 2019-07-15 stsp done:
444 3ce1b845 2019-07-15 stsp if (err) {
445 3ce1b845 2019-07-15 stsp free(*logmsg);
446 3ce1b845 2019-07-15 stsp *logmsg = NULL;
447 3ce1b845 2019-07-15 stsp }
448 3ce1b845 2019-07-15 stsp return err;
449 3ce1b845 2019-07-15 stsp }
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp static const struct got_error *
452 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 3ce1b845 2019-07-15 stsp const char *branch_name)
454 3ce1b845 2019-07-15 stsp {
455 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
456 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
457 3ce1b845 2019-07-15 stsp int fd;
458 3ce1b845 2019-07-15 stsp
459 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
460 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
461 3ce1b845 2019-07-15 stsp branch_name) == -1)
462 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
463 3ce1b845 2019-07-15 stsp
464 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
465 3ce1b845 2019-07-15 stsp if (err)
466 3ce1b845 2019-07-15 stsp goto done;
467 3ce1b845 2019-07-15 stsp
468 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
469 3ce1b845 2019-07-15 stsp close(fd);
470 3ce1b845 2019-07-15 stsp
471 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
472 3ce1b845 2019-07-15 stsp done:
473 3ce1b845 2019-07-15 stsp free(initial_content);
474 3ce1b845 2019-07-15 stsp free(logmsg_path);
475 3ce1b845 2019-07-15 stsp return err;
476 3ce1b845 2019-07-15 stsp }
477 3ce1b845 2019-07-15 stsp
478 3ce1b845 2019-07-15 stsp static const struct got_error *
479 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
480 3ce1b845 2019-07-15 stsp {
481 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
482 3ce1b845 2019-07-15 stsp return NULL;
483 3ce1b845 2019-07-15 stsp }
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 84792843 2019-08-09 stsp get_author(const char **author)
487 84792843 2019-08-09 stsp {
488 84792843 2019-08-09 stsp const char *got_author;
489 84792843 2019-08-09 stsp
490 84792843 2019-08-09 stsp *author = NULL;
491 84792843 2019-08-09 stsp
492 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
493 84792843 2019-08-09 stsp if (got_author == NULL) {
494 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
495 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
496 84792843 2019-08-09 stsp }
497 84792843 2019-08-09 stsp
498 84792843 2019-08-09 stsp *author = got_author;
499 84792843 2019-08-09 stsp
500 84792843 2019-08-09 stsp /*
501 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
502 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
503 84792843 2019-08-09 stsp */
504 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
505 84792843 2019-08-09 stsp got_author++;
506 84792843 2019-08-09 stsp if (*got_author != '<')
507 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
509 84792843 2019-08-09 stsp got_author++;
510 84792843 2019-08-09 stsp if (*got_author != '@')
511 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
513 84792843 2019-08-09 stsp got_author++;
514 84792843 2019-08-09 stsp if (*got_author != '>')
515 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_EMAIL);
516 84792843 2019-08-09 stsp
517 84792843 2019-08-09 stsp return NULL;
518 84792843 2019-08-09 stsp }
519 84792843 2019-08-09 stsp
520 84792843 2019-08-09 stsp static const struct got_error *
521 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
522 3ce1b845 2019-07-15 stsp {
523 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
524 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
525 3ce1b845 2019-07-15 stsp char *editor = NULL;
526 84792843 2019-08-09 stsp const char *author;
527 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
528 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
529 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
530 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
532 3ce1b845 2019-07-15 stsp int ch;
533 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
534 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
535 3ce1b845 2019-07-15 stsp
536 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
537 3ce1b845 2019-07-15 stsp
538 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
539 3ce1b845 2019-07-15 stsp switch (ch) {
540 3ce1b845 2019-07-15 stsp case 'b':
541 3ce1b845 2019-07-15 stsp branch_name = optarg;
542 3ce1b845 2019-07-15 stsp break;
543 3ce1b845 2019-07-15 stsp case 'm':
544 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
545 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
546 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
547 3ce1b845 2019-07-15 stsp goto done;
548 3ce1b845 2019-07-15 stsp }
549 3ce1b845 2019-07-15 stsp break;
550 3ce1b845 2019-07-15 stsp case 'r':
551 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
552 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
553 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
554 3ce1b845 2019-07-15 stsp goto done;
555 3ce1b845 2019-07-15 stsp }
556 3ce1b845 2019-07-15 stsp break;
557 3ce1b845 2019-07-15 stsp case 'I':
558 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
559 3ce1b845 2019-07-15 stsp break;
560 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
561 3ce1b845 2019-07-15 stsp NULL);
562 3ce1b845 2019-07-15 stsp if (error)
563 3ce1b845 2019-07-15 stsp goto done;
564 3ce1b845 2019-07-15 stsp break;
565 3ce1b845 2019-07-15 stsp default:
566 3ce1b845 2019-07-15 stsp usage_init();
567 3ce1b845 2019-07-15 stsp /* NOTREACHED */
568 3ce1b845 2019-07-15 stsp }
569 3ce1b845 2019-07-15 stsp }
570 3ce1b845 2019-07-15 stsp
571 3ce1b845 2019-07-15 stsp argc -= optind;
572 3ce1b845 2019-07-15 stsp argv += optind;
573 3ce1b845 2019-07-15 stsp
574 3ce1b845 2019-07-15 stsp #ifndef PROFILE
575 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
576 3ce1b845 2019-07-15 stsp NULL) == -1)
577 3ce1b845 2019-07-15 stsp err(1, "pledge");
578 3ce1b845 2019-07-15 stsp #endif
579 3ce1b845 2019-07-15 stsp if (argc != 1)
580 3ce1b845 2019-07-15 stsp usage_import();
581 3ce1b845 2019-07-15 stsp
582 84792843 2019-08-09 stsp error = get_author(&author);
583 84792843 2019-08-09 stsp if (error)
584 84792843 2019-08-09 stsp return error;
585 2c7829a4 2019-06-17 stsp
586 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
587 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
588 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
589 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
590 3ce1b845 2019-07-15 stsp }
591 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
592 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
593 3ce1b845 2019-07-15 stsp if (error)
594 3ce1b845 2019-07-15 stsp goto done;
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
598 3ce1b845 2019-07-15 stsp goto done;
599 3ce1b845 2019-07-15 stsp }
600 3ce1b845 2019-07-15 stsp
601 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
602 3ce1b845 2019-07-15 stsp if (error) {
603 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
604 3ce1b845 2019-07-15 stsp goto done;
605 3ce1b845 2019-07-15 stsp } else {
606 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 3ce1b845 2019-07-15 stsp "import target branch already exists");
608 3ce1b845 2019-07-15 stsp goto done;
609 3ce1b845 2019-07-15 stsp }
610 3ce1b845 2019-07-15 stsp
611 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
612 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
613 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp }
616 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
617 3ce1b845 2019-07-15 stsp
618 3ce1b845 2019-07-15 stsp /*
619 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
620 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
621 3ce1b845 2019-07-15 stsp */
622 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
623 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
624 3ce1b845 2019-07-15 stsp if (error)
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
627 3ce1b845 2019-07-15 stsp if (error)
628 3ce1b845 2019-07-15 stsp goto done;
629 3ce1b845 2019-07-15 stsp }
630 3ce1b845 2019-07-15 stsp
631 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
632 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
633 3ce1b845 2019-07-15 stsp
634 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
635 3ce1b845 2019-07-15 stsp if (error)
636 3ce1b845 2019-07-15 stsp goto done;
637 3ce1b845 2019-07-15 stsp
638 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
640 3ce1b845 2019-07-15 stsp if (error)
641 3ce1b845 2019-07-15 stsp goto done;
642 3ce1b845 2019-07-15 stsp
643 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
644 3ce1b845 2019-07-15 stsp if (error)
645 3ce1b845 2019-07-15 stsp goto done;
646 3ce1b845 2019-07-15 stsp
647 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
648 3ce1b845 2019-07-15 stsp if (error)
649 3ce1b845 2019-07-15 stsp goto done;
650 3ce1b845 2019-07-15 stsp
651 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
652 3ce1b845 2019-07-15 stsp if (error)
653 3ce1b845 2019-07-15 stsp goto done;
654 3ce1b845 2019-07-15 stsp
655 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
656 3ce1b845 2019-07-15 stsp if (error) {
657 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
658 3ce1b845 2019-07-15 stsp goto done;
659 3ce1b845 2019-07-15 stsp
660 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
661 3ce1b845 2019-07-15 stsp branch_ref);
662 3ce1b845 2019-07-15 stsp if (error)
663 3ce1b845 2019-07-15 stsp goto done;
664 3ce1b845 2019-07-15 stsp
665 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
666 3ce1b845 2019-07-15 stsp if (error)
667 3ce1b845 2019-07-15 stsp goto done;
668 3ce1b845 2019-07-15 stsp }
669 3ce1b845 2019-07-15 stsp
670 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
671 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
672 2c7829a4 2019-06-17 stsp done:
673 2c7829a4 2019-06-17 stsp free(repo_path);
674 3ce1b845 2019-07-15 stsp free(editor);
675 3ce1b845 2019-07-15 stsp free(refname);
676 3ce1b845 2019-07-15 stsp free(new_commit_id);
677 3ce1b845 2019-07-15 stsp free(id_str);
678 3ce1b845 2019-07-15 stsp if (branch_ref)
679 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
680 3ce1b845 2019-07-15 stsp if (head_ref)
681 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
682 2c7829a4 2019-06-17 stsp return error;
683 0266afb7 2019-01-04 stsp }
684 0266afb7 2019-01-04 stsp
685 4ed7e80c 2018-05-20 stsp __dead static void
686 c09a553d 2018-03-12 stsp usage_checkout(void)
687 c09a553d 2018-03-12 stsp {
688 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
690 c09a553d 2018-03-12 stsp exit(1);
691 92a684f4 2018-03-12 stsp }
692 92a684f4 2018-03-12 stsp
693 1ee397ad 2019-07-12 stsp static const struct got_error *
694 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
695 92a684f4 2018-03-12 stsp {
696 92a684f4 2018-03-12 stsp char *worktree_path = arg;
697 a484d721 2019-06-10 stsp
698 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
699 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
700 1ee397ad 2019-07-12 stsp return NULL;
701 92a684f4 2018-03-12 stsp
702 92a684f4 2018-03-12 stsp while (path[0] == '/')
703 92a684f4 2018-03-12 stsp path++;
704 92a684f4 2018-03-12 stsp
705 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
706 1ee397ad 2019-07-12 stsp return NULL;
707 99437157 2018-11-11 stsp }
708 99437157 2018-11-11 stsp
709 99437157 2018-11-11 stsp static const struct got_error *
710 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
711 99437157 2018-11-11 stsp {
712 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
713 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
714 99437157 2018-11-11 stsp return NULL;
715 8069f636 2019-01-12 stsp }
716 8069f636 2019-01-12 stsp
717 8069f636 2019-01-12 stsp static const struct got_error *
718 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
719 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
720 8069f636 2019-01-12 stsp {
721 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
722 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
723 8069f636 2019-01-12 stsp
724 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
726 36a38700 2019-05-10 stsp if (err)
727 36a38700 2019-05-10 stsp return err;
728 8069f636 2019-01-12 stsp
729 d5bea539 2019-05-13 stsp if (yca_id == NULL)
730 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
731 8069f636 2019-01-12 stsp
732 d5bea539 2019-05-13 stsp /*
733 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
734 d5bea539 2019-05-13 stsp * and the work tree's base commit.
735 d5bea539 2019-05-13 stsp *
736 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
737 d5bea539 2019-05-13 stsp *
738 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
739 d5bea539 2019-05-13 stsp * \ /
740 d5bea539 2019-05-13 stsp * C E
741 d5bea539 2019-05-13 stsp * \ /
742 d5bea539 2019-05-13 stsp * B (yca)
743 d5bea539 2019-05-13 stsp * |
744 d5bea539 2019-05-13 stsp * A
745 d5bea539 2019-05-13 stsp *
746 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
747 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
748 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
749 d5bea539 2019-05-13 stsp */
750 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
752 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
753 8069f636 2019-01-12 stsp
754 d5bea539 2019-05-13 stsp free(yca_id);
755 d5bea539 2019-05-13 stsp return NULL;
756 c09a553d 2018-03-12 stsp }
757 a367ff0f 2019-05-14 stsp
758 a367ff0f 2019-05-14 stsp static const struct got_error *
759 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
760 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
761 a51a74b3 2019-07-27 stsp struct got_repository *repo)
762 a367ff0f 2019-05-14 stsp {
763 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
764 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
765 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
766 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
767 a367ff0f 2019-05-14 stsp
768 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
769 a367ff0f 2019-05-14 stsp if (err)
770 a51a74b3 2019-07-27 stsp goto done;
771 a51a74b3 2019-07-27 stsp
772 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
773 a51a74b3 2019-07-27 stsp is_same_branch = 1;
774 a51a74b3 2019-07-27 stsp goto done;
775 a51a74b3 2019-07-27 stsp }
776 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
777 a51a74b3 2019-07-27 stsp is_same_branch = 1;
778 a367ff0f 2019-05-14 stsp goto done;
779 a51a74b3 2019-07-27 stsp }
780 a367ff0f 2019-05-14 stsp
781 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
782 a367ff0f 2019-05-14 stsp if (err)
783 a367ff0f 2019-05-14 stsp goto done;
784 a367ff0f 2019-05-14 stsp
785 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
786 a367ff0f 2019-05-14 stsp if (err)
787 a367ff0f 2019-05-14 stsp goto done;
788 a367ff0f 2019-05-14 stsp
789 a367ff0f 2019-05-14 stsp for (;;) {
790 a367ff0f 2019-05-14 stsp struct got_object_id *id;
791 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
792 a367ff0f 2019-05-14 stsp if (err) {
793 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
794 a367ff0f 2019-05-14 stsp err = NULL;
795 a367ff0f 2019-05-14 stsp break;
796 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
797 a367ff0f 2019-05-14 stsp break;
798 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
799 a367ff0f 2019-05-14 stsp repo);
800 a367ff0f 2019-05-14 stsp if (err)
801 a367ff0f 2019-05-14 stsp break;
802 a367ff0f 2019-05-14 stsp }
803 c09a553d 2018-03-12 stsp
804 a367ff0f 2019-05-14 stsp if (id) {
805 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
806 a51a74b3 2019-07-27 stsp break;
807 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
808 a367ff0f 2019-05-14 stsp is_same_branch = 1;
809 a367ff0f 2019-05-14 stsp break;
810 a367ff0f 2019-05-14 stsp }
811 a367ff0f 2019-05-14 stsp }
812 a367ff0f 2019-05-14 stsp }
813 a367ff0f 2019-05-14 stsp done:
814 a367ff0f 2019-05-14 stsp if (graph)
815 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
816 a367ff0f 2019-05-14 stsp free(head_commit_id);
817 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
818 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
819 04f57cb3 2019-07-25 stsp return err;
820 04f57cb3 2019-07-25 stsp }
821 04f57cb3 2019-07-25 stsp
822 04f57cb3 2019-07-25 stsp static const struct got_error *
823 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
824 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
825 04f57cb3 2019-07-25 stsp {
826 04f57cb3 2019-07-25 stsp const struct got_error *err;
827 04f57cb3 2019-07-25 stsp struct got_reference *ref;
828 303e2782 2019-08-09 stsp struct got_tag_object *tag;
829 04f57cb3 2019-07-25 stsp
830 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
831 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
832 303e2782 2019-08-09 stsp if (err == NULL) {
833 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
834 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
835 303e2782 2019-08-09 stsp if (*commit_id == NULL)
836 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
837 303e2782 2019-08-09 stsp got_object_tag_close(tag);
838 303e2782 2019-08-09 stsp return err;
839 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
840 303e2782 2019-08-09 stsp return err;
841 303e2782 2019-08-09 stsp
842 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
843 04f57cb3 2019-07-25 stsp if (err == NULL) {
844 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
845 04f57cb3 2019-07-25 stsp got_ref_close(ref);
846 04f57cb3 2019-07-25 stsp } else {
847 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
848 04f57cb3 2019-07-25 stsp return err;
849 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
850 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
851 04f57cb3 2019-07-25 stsp }
852 a367ff0f 2019-05-14 stsp return err;
853 a367ff0f 2019-05-14 stsp }
854 8069f636 2019-01-12 stsp
855 4ed7e80c 2018-05-20 stsp static const struct got_error *
856 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
857 c09a553d 2018-03-12 stsp {
858 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
859 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
860 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
861 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
862 c09a553d 2018-03-12 stsp char *repo_path = NULL;
863 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
864 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
865 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
866 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
867 72151b04 2019-05-11 stsp int ch, same_path_prefix;
868 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
869 f2ea84fa 2019-07-27 stsp
870 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
871 c09a553d 2018-03-12 stsp
872 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
873 0bb8a95e 2018-03-12 stsp switch (ch) {
874 08573d5b 2019-05-14 stsp case 'b':
875 08573d5b 2019-05-14 stsp branch_name = optarg;
876 08573d5b 2019-05-14 stsp break;
877 8069f636 2019-01-12 stsp case 'c':
878 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
879 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
880 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
881 8069f636 2019-01-12 stsp break;
882 0bb8a95e 2018-03-12 stsp case 'p':
883 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
884 0bb8a95e 2018-03-12 stsp break;
885 0bb8a95e 2018-03-12 stsp default:
886 2deda0b9 2019-03-07 stsp usage_checkout();
887 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
888 0bb8a95e 2018-03-12 stsp }
889 0bb8a95e 2018-03-12 stsp }
890 0bb8a95e 2018-03-12 stsp
891 0bb8a95e 2018-03-12 stsp argc -= optind;
892 0bb8a95e 2018-03-12 stsp argv += optind;
893 0bb8a95e 2018-03-12 stsp
894 6715a751 2018-03-16 stsp #ifndef PROFILE
895 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
897 c09a553d 2018-03-12 stsp err(1, "pledge");
898 6715a751 2018-03-16 stsp #endif
899 0bb8a95e 2018-03-12 stsp if (argc == 1) {
900 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
901 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
902 76089277 2018-04-01 stsp if (repo_path == NULL)
903 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
904 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
905 76089277 2018-04-01 stsp if (cwd == NULL) {
906 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
907 76089277 2018-04-01 stsp goto done;
908 76089277 2018-04-01 stsp }
909 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
910 230a42bd 2019-05-11 jcs base = basename(path_prefix);
911 230a42bd 2019-05-11 jcs if (base == NULL) {
912 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
913 230a42bd 2019-05-11 jcs path_prefix);
914 230a42bd 2019-05-11 jcs goto done;
915 230a42bd 2019-05-11 jcs }
916 230a42bd 2019-05-11 jcs } else {
917 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
918 230a42bd 2019-05-11 jcs if (base == NULL) {
919 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
920 230a42bd 2019-05-11 jcs repo_path);
921 230a42bd 2019-05-11 jcs goto done;
922 230a42bd 2019-05-11 jcs }
923 76089277 2018-04-01 stsp }
924 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
925 c09a553d 2018-03-12 stsp if (dotgit)
926 c09a553d 2018-03-12 stsp *dotgit = '\0';
927 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
929 c09a553d 2018-03-12 stsp free(cwd);
930 76089277 2018-04-01 stsp goto done;
931 c09a553d 2018-03-12 stsp }
932 c09a553d 2018-03-12 stsp free(cwd);
933 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
934 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
935 76089277 2018-04-01 stsp if (repo_path == NULL) {
936 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
937 76089277 2018-04-01 stsp goto done;
938 76089277 2018-04-01 stsp }
939 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
940 76089277 2018-04-01 stsp if (worktree_path == NULL) {
941 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
942 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
943 b4b3a7dd 2019-07-22 stsp argv[1]);
944 b4b3a7dd 2019-07-22 stsp goto done;
945 b4b3a7dd 2019-07-22 stsp }
946 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
947 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
948 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
949 b4b3a7dd 2019-07-22 stsp goto done;
950 b4b3a7dd 2019-07-22 stsp }
951 76089277 2018-04-01 stsp }
952 c09a553d 2018-03-12 stsp } else
953 c09a553d 2018-03-12 stsp usage_checkout();
954 c09a553d 2018-03-12 stsp
955 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
956 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
957 13bfb272 2019-05-10 jcs
958 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
959 c09a553d 2018-03-12 stsp if (error != NULL)
960 c02c541e 2019-03-29 stsp goto done;
961 c02c541e 2019-03-29 stsp
962 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
963 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
964 c530dc23 2019-07-23 stsp if (error) {
965 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
967 c530dc23 2019-07-23 stsp goto done;
968 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
969 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
970 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
971 c530dc23 2019-07-23 stsp goto done;
972 c530dc23 2019-07-23 stsp }
973 c530dc23 2019-07-23 stsp }
974 c530dc23 2019-07-23 stsp
975 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
976 c02c541e 2019-03-29 stsp if (error)
977 c09a553d 2018-03-12 stsp goto done;
978 8069f636 2019-01-12 stsp
979 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
980 c09a553d 2018-03-12 stsp if (error != NULL)
981 c09a553d 2018-03-12 stsp goto done;
982 c09a553d 2018-03-12 stsp
983 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
985 c09a553d 2018-03-12 stsp goto done;
986 c09a553d 2018-03-12 stsp
987 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
988 c09a553d 2018-03-12 stsp if (error != NULL)
989 c09a553d 2018-03-12 stsp goto done;
990 c09a553d 2018-03-12 stsp
991 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
992 e5dc7198 2018-12-29 stsp path_prefix);
993 e5dc7198 2018-12-29 stsp if (error != NULL)
994 e5dc7198 2018-12-29 stsp goto done;
995 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
996 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
997 49520a32 2018-12-29 stsp goto done;
998 49520a32 2018-12-29 stsp }
999 49520a32 2018-12-29 stsp
1000 8069f636 2019-01-12 stsp if (commit_id_str) {
1001 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1002 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1003 30837e32 2019-07-25 stsp if (error)
1004 8069f636 2019-01-12 stsp goto done;
1005 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1006 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1007 8069f636 2019-01-12 stsp if (error != NULL) {
1008 8069f636 2019-01-12 stsp free(commit_id);
1009 8069f636 2019-01-12 stsp goto done;
1010 8069f636 2019-01-12 stsp }
1011 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1012 45d344f6 2019-05-14 stsp if (error)
1013 45d344f6 2019-05-14 stsp goto done;
1014 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1015 8069f636 2019-01-12 stsp commit_id);
1016 8069f636 2019-01-12 stsp free(commit_id);
1017 8069f636 2019-01-12 stsp if (error)
1018 8069f636 2019-01-12 stsp goto done;
1019 8069f636 2019-01-12 stsp }
1020 8069f636 2019-01-12 stsp
1021 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1022 f2ea84fa 2019-07-27 stsp if (error)
1023 f2ea84fa 2019-07-27 stsp goto done;
1024 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1025 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1026 c09a553d 2018-03-12 stsp if (error != NULL)
1027 c09a553d 2018-03-12 stsp goto done;
1028 c09a553d 2018-03-12 stsp
1029 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1030 c09a553d 2018-03-12 stsp
1031 c09a553d 2018-03-12 stsp done:
1032 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1033 8069f636 2019-01-12 stsp free(commit_id_str);
1034 76089277 2018-04-01 stsp free(repo_path);
1035 507dc3bb 2018-12-29 stsp free(worktree_path);
1036 507dc3bb 2018-12-29 stsp return error;
1037 507dc3bb 2018-12-29 stsp }
1038 507dc3bb 2018-12-29 stsp
1039 507dc3bb 2018-12-29 stsp __dead static void
1040 507dc3bb 2018-12-29 stsp usage_update(void)
1041 507dc3bb 2018-12-29 stsp {
1042 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1043 507dc3bb 2018-12-29 stsp getprogname());
1044 507dc3bb 2018-12-29 stsp exit(1);
1045 507dc3bb 2018-12-29 stsp }
1046 507dc3bb 2018-12-29 stsp
1047 1ee397ad 2019-07-12 stsp static const struct got_error *
1048 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1049 507dc3bb 2018-12-29 stsp {
1050 784955db 2019-01-12 stsp int *did_something = arg;
1051 784955db 2019-01-12 stsp
1052 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1053 1ee397ad 2019-07-12 stsp return NULL;
1054 507dc3bb 2018-12-29 stsp
1055 1545c615 2019-02-10 stsp *did_something = 1;
1056 a484d721 2019-06-10 stsp
1057 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1058 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1059 1ee397ad 2019-07-12 stsp return NULL;
1060 a484d721 2019-06-10 stsp
1061 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1062 507dc3bb 2018-12-29 stsp path++;
1063 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1064 1ee397ad 2019-07-12 stsp return NULL;
1065 be7061eb 2018-12-30 stsp }
1066 be7061eb 2018-12-30 stsp
1067 be7061eb 2018-12-30 stsp static const struct got_error *
1068 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1069 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1070 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1071 a1fb16d8 2019-05-24 stsp {
1072 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1073 a1fb16d8 2019-05-24 stsp char *base_id_str;
1074 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1075 a1fb16d8 2019-05-24 stsp
1076 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1077 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1078 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1079 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1080 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1081 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1082 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1083 a1fb16d8 2019-05-24 stsp }
1084 a1fb16d8 2019-05-24 stsp
1085 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1086 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1087 a1fb16d8 2019-05-24 stsp if (err) {
1088 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1089 a1fb16d8 2019-05-24 stsp return err;
1090 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1091 a1fb16d8 2019-05-24 stsp }
1092 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1093 a1fb16d8 2019-05-24 stsp return NULL;
1094 a1fb16d8 2019-05-24 stsp
1095 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1096 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1097 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1098 a1fb16d8 2019-05-24 stsp if (err)
1099 a1fb16d8 2019-05-24 stsp return err;
1100 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1101 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1102 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1103 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1104 0ebf8283 2019-07-24 stsp return NULL;
1105 0ebf8283 2019-07-24 stsp }
1106 0ebf8283 2019-07-24 stsp
1107 0ebf8283 2019-07-24 stsp static const struct got_error *
1108 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1109 0ebf8283 2019-07-24 stsp {
1110 0ebf8283 2019-07-24 stsp const struct got_error *err;
1111 0ebf8283 2019-07-24 stsp int in_progress;
1112 0ebf8283 2019-07-24 stsp
1113 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1114 0ebf8283 2019-07-24 stsp if (err)
1115 0ebf8283 2019-07-24 stsp return err;
1116 0ebf8283 2019-07-24 stsp if (in_progress)
1117 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1118 0ebf8283 2019-07-24 stsp
1119 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1120 0ebf8283 2019-07-24 stsp if (err)
1121 0ebf8283 2019-07-24 stsp return err;
1122 0ebf8283 2019-07-24 stsp if (in_progress)
1123 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1124 0ebf8283 2019-07-24 stsp
1125 a1fb16d8 2019-05-24 stsp return NULL;
1126 a5edda0a 2019-07-27 stsp }
1127 a5edda0a 2019-07-27 stsp
1128 a5edda0a 2019-07-27 stsp static const struct got_error *
1129 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1131 a5edda0a 2019-07-27 stsp {
1132 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1133 a5edda0a 2019-07-27 stsp char *path;
1134 a5edda0a 2019-07-27 stsp int i;
1135 a5edda0a 2019-07-27 stsp
1136 a5edda0a 2019-07-27 stsp if (argc == 0) {
1137 a5edda0a 2019-07-27 stsp path = strdup("");
1138 a5edda0a 2019-07-27 stsp if (path == NULL)
1139 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1140 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1141 a5edda0a 2019-07-27 stsp }
1142 a5edda0a 2019-07-27 stsp
1143 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1144 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1145 a5edda0a 2019-07-27 stsp if (err)
1146 a5edda0a 2019-07-27 stsp break;
1147 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1148 a5edda0a 2019-07-27 stsp if (err) {
1149 a5edda0a 2019-07-27 stsp free(path);
1150 a5edda0a 2019-07-27 stsp break;
1151 a5edda0a 2019-07-27 stsp }
1152 a5edda0a 2019-07-27 stsp }
1153 a5edda0a 2019-07-27 stsp
1154 a5edda0a 2019-07-27 stsp return err;
1155 a1fb16d8 2019-05-24 stsp }
1156 a1fb16d8 2019-05-24 stsp
1157 a1fb16d8 2019-05-24 stsp static const struct got_error *
1158 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1159 507dc3bb 2018-12-29 stsp {
1160 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1161 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1162 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1163 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1164 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1165 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1166 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1167 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1168 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1169 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1170 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1171 507dc3bb 2018-12-29 stsp
1172 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1173 f2ea84fa 2019-07-27 stsp
1174 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1175 507dc3bb 2018-12-29 stsp switch (ch) {
1176 024e9686 2019-05-14 stsp case 'b':
1177 024e9686 2019-05-14 stsp branch_name = optarg;
1178 024e9686 2019-05-14 stsp break;
1179 507dc3bb 2018-12-29 stsp case 'c':
1180 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1181 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1182 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1183 507dc3bb 2018-12-29 stsp break;
1184 507dc3bb 2018-12-29 stsp default:
1185 2deda0b9 2019-03-07 stsp usage_update();
1186 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1187 507dc3bb 2018-12-29 stsp }
1188 507dc3bb 2018-12-29 stsp }
1189 507dc3bb 2018-12-29 stsp
1190 507dc3bb 2018-12-29 stsp argc -= optind;
1191 507dc3bb 2018-12-29 stsp argv += optind;
1192 507dc3bb 2018-12-29 stsp
1193 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1194 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1196 507dc3bb 2018-12-29 stsp err(1, "pledge");
1197 507dc3bb 2018-12-29 stsp #endif
1198 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1199 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1200 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1201 c4cdcb68 2019-04-03 stsp goto done;
1202 c4cdcb68 2019-04-03 stsp }
1203 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1204 7d5807f4 2019-07-11 stsp if (error)
1205 7d5807f4 2019-07-11 stsp goto done;
1206 7d5807f4 2019-07-11 stsp
1207 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1208 f2ea84fa 2019-07-27 stsp if (error)
1209 f2ea84fa 2019-07-27 stsp goto done;
1210 507dc3bb 2018-12-29 stsp
1211 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1212 507dc3bb 2018-12-29 stsp if (error != NULL)
1213 507dc3bb 2018-12-29 stsp goto done;
1214 507dc3bb 2018-12-29 stsp
1215 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1216 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1217 087fb88c 2019-08-04 stsp if (error)
1218 087fb88c 2019-08-04 stsp goto done;
1219 087fb88c 2019-08-04 stsp
1220 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1221 0266afb7 2019-01-04 stsp if (error)
1222 0266afb7 2019-01-04 stsp goto done;
1223 0266afb7 2019-01-04 stsp
1224 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1226 024e9686 2019-05-14 stsp if (error != NULL)
1227 024e9686 2019-05-14 stsp goto done;
1228 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1229 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1230 9c4b8182 2019-01-02 stsp if (error != NULL)
1231 9c4b8182 2019-01-02 stsp goto done;
1232 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1233 507dc3bb 2018-12-29 stsp if (error != NULL)
1234 507dc3bb 2018-12-29 stsp goto done;
1235 507dc3bb 2018-12-29 stsp } else {
1236 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 04f57cb3 2019-07-25 stsp free(commit_id_str);
1238 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1239 30837e32 2019-07-25 stsp if (error)
1240 a297e751 2019-07-11 stsp goto done;
1241 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1242 a297e751 2019-07-11 stsp if (error)
1243 507dc3bb 2018-12-29 stsp goto done;
1244 507dc3bb 2018-12-29 stsp }
1245 35c965b2 2018-12-29 stsp
1246 a1fb16d8 2019-05-24 stsp if (branch_name) {
1247 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1248 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1249 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1250 f2ea84fa 2019-07-27 stsp continue;
1251 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1252 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1253 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1254 024e9686 2019-05-14 stsp goto done;
1255 024e9686 2019-05-14 stsp }
1256 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1257 024e9686 2019-05-14 stsp if (error)
1258 024e9686 2019-05-14 stsp goto done;
1259 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 a367ff0f 2019-05-14 stsp free(head_commit_id);
1261 024e9686 2019-05-14 stsp if (error != NULL)
1262 024e9686 2019-05-14 stsp goto done;
1263 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1264 a367ff0f 2019-05-14 stsp if (error)
1265 a367ff0f 2019-05-14 stsp goto done;
1266 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1267 024e9686 2019-05-14 stsp if (error)
1268 024e9686 2019-05-14 stsp goto done;
1269 024e9686 2019-05-14 stsp } else {
1270 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1271 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1272 a367ff0f 2019-05-14 stsp if (error != NULL) {
1273 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1274 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1275 024e9686 2019-05-14 stsp goto done;
1276 a367ff0f 2019-05-14 stsp }
1277 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1278 a367ff0f 2019-05-14 stsp if (error)
1279 a367ff0f 2019-05-14 stsp goto done;
1280 024e9686 2019-05-14 stsp }
1281 507dc3bb 2018-12-29 stsp
1282 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1283 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1284 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1285 507dc3bb 2018-12-29 stsp commit_id);
1286 507dc3bb 2018-12-29 stsp if (error)
1287 507dc3bb 2018-12-29 stsp goto done;
1288 507dc3bb 2018-12-29 stsp }
1289 507dc3bb 2018-12-29 stsp
1290 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1291 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1292 507dc3bb 2018-12-29 stsp if (error != NULL)
1293 507dc3bb 2018-12-29 stsp goto done;
1294 9c4b8182 2019-01-02 stsp
1295 784955db 2019-01-12 stsp if (did_something)
1296 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1297 784955db 2019-01-12 stsp else
1298 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1299 507dc3bb 2018-12-29 stsp done:
1300 c09a553d 2018-03-12 stsp free(worktree_path);
1301 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1302 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1303 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1304 507dc3bb 2018-12-29 stsp free(commit_id);
1305 9c4b8182 2019-01-02 stsp free(commit_id_str);
1306 c09a553d 2018-03-12 stsp return error;
1307 c09a553d 2018-03-12 stsp }
1308 c09a553d 2018-03-12 stsp
1309 f42b1b34 2018-03-12 stsp static const struct got_error *
1310 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1312 5c860e29 2018-03-12 stsp {
1313 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1314 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1315 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1316 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1317 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1318 79109fed 2018-03-27 stsp
1319 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1320 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1321 79109fed 2018-03-27 stsp if (err)
1322 79109fed 2018-03-27 stsp return err;
1323 79109fed 2018-03-27 stsp
1324 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1325 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1326 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1327 79109fed 2018-03-27 stsp
1328 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1329 79109fed 2018-03-27 stsp if (err)
1330 79109fed 2018-03-27 stsp return err;
1331 79109fed 2018-03-27 stsp
1332 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1333 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1334 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1335 0f2b3dca 2018-12-22 stsp if (err)
1336 0f2b3dca 2018-12-22 stsp return err;
1337 0f2b3dca 2018-12-22 stsp
1338 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1339 79109fed 2018-03-27 stsp if (err)
1340 79109fed 2018-03-27 stsp return err;
1341 79109fed 2018-03-27 stsp }
1342 79109fed 2018-03-27 stsp
1343 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1344 0f2b3dca 2018-12-22 stsp if (err)
1345 0f2b3dca 2018-12-22 stsp goto done;
1346 0f2b3dca 2018-12-22 stsp
1347 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1349 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1350 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1351 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1352 0f2b3dca 2018-12-22 stsp done:
1353 79109fed 2018-03-27 stsp if (tree1)
1354 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1355 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1356 0f2b3dca 2018-12-22 stsp free(id_str1);
1357 0f2b3dca 2018-12-22 stsp free(id_str2);
1358 79109fed 2018-03-27 stsp return err;
1359 79109fed 2018-03-27 stsp }
1360 79109fed 2018-03-27 stsp
1361 4bb494d5 2018-06-16 stsp static char *
1362 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1363 6c281f94 2018-06-11 stsp {
1364 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1365 09867e48 2019-08-13 stsp char *p, *s;
1366 09867e48 2019-08-13 stsp
1367 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1368 09867e48 2019-08-13 stsp if (tm == NULL)
1369 09867e48 2019-08-13 stsp return NULL;
1370 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1371 09867e48 2019-08-13 stsp if (s == NULL)
1372 09867e48 2019-08-13 stsp return NULL;
1373 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1374 6c281f94 2018-06-11 stsp if (p)
1375 6c281f94 2018-06-11 stsp *p = '\0';
1376 6c281f94 2018-06-11 stsp return s;
1377 6c281f94 2018-06-11 stsp }
1378 dc424a06 2019-08-07 stsp
1379 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1380 6c281f94 2018-06-11 stsp
1381 79109fed 2018-03-27 stsp static const struct got_error *
1382 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1383 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1384 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1385 79109fed 2018-03-27 stsp {
1386 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1387 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1388 6c281f94 2018-06-11 stsp char datebuf[26];
1389 45d799e2 2018-12-23 stsp time_t committer_time;
1390 45d799e2 2018-12-23 stsp const char *author, *committer;
1391 199a4027 2019-02-02 stsp char *refs_str = NULL;
1392 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1393 5c860e29 2018-03-12 stsp
1394 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1395 199a4027 2019-02-02 stsp char *s;
1396 199a4027 2019-02-02 stsp const char *name;
1397 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1398 a436ad14 2019-08-13 stsp int cmp;
1399 a436ad14 2019-08-13 stsp
1400 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1401 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1402 d9498b20 2019-02-05 stsp continue;
1403 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1404 199a4027 2019-02-02 stsp name += 5;
1405 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1406 7143d404 2019-03-12 stsp continue;
1407 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1408 e34f9ed6 2019-02-02 stsp name += 6;
1409 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1410 141c2bff 2019-02-04 stsp name += 8;
1411 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1412 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1413 5d844a1e 2019-08-13 stsp if (err) {
1414 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1415 5d844a1e 2019-08-13 stsp return err;
1416 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1417 5d844a1e 2019-08-13 stsp err = NULL;
1418 5d844a1e 2019-08-13 stsp tag = NULL;
1419 5d844a1e 2019-08-13 stsp }
1420 a436ad14 2019-08-13 stsp }
1421 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1422 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1423 a436ad14 2019-08-13 stsp if (tag)
1424 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1425 a436ad14 2019-08-13 stsp if (cmp != 0)
1426 a436ad14 2019-08-13 stsp continue;
1427 199a4027 2019-02-02 stsp s = refs_str;
1428 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1429 199a4027 2019-02-02 stsp name) == -1) {
1430 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1431 199a4027 2019-02-02 stsp free(s);
1432 34ca4898 2019-08-13 stsp return err;
1433 199a4027 2019-02-02 stsp }
1434 199a4027 2019-02-02 stsp free(s);
1435 199a4027 2019-02-02 stsp }
1436 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1437 8bf5b3c9 2018-03-17 stsp if (err)
1438 8bf5b3c9 2018-03-17 stsp return err;
1439 788c352e 2018-06-16 stsp
1440 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1441 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1442 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1443 832c249c 2018-06-10 stsp free(id_str);
1444 d3d493d7 2019-02-21 stsp id_str = NULL;
1445 d3d493d7 2019-02-21 stsp free(refs_str);
1446 d3d493d7 2019-02-21 stsp refs_str = NULL;
1447 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1448 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1449 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1450 09867e48 2019-08-13 stsp if (datestr)
1451 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1452 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1453 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1454 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1455 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1456 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1457 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1458 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1459 3fe1abad 2018-06-10 stsp int n = 1;
1460 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1461 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1462 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1463 a0603db2 2018-06-10 stsp if (err)
1464 a0603db2 2018-06-10 stsp return err;
1465 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1466 a0603db2 2018-06-10 stsp free(id_str);
1467 a0603db2 2018-06-10 stsp }
1468 a0603db2 2018-06-10 stsp }
1469 832c249c 2018-06-10 stsp
1470 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1471 5943eee2 2019-08-13 stsp if (err)
1472 5943eee2 2019-08-13 stsp return err;
1473 8bf5b3c9 2018-03-17 stsp
1474 621015ac 2018-07-23 stsp logmsg = logmsg0;
1475 832c249c 2018-06-10 stsp do {
1476 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1477 832c249c 2018-06-10 stsp if (line)
1478 832c249c 2018-06-10 stsp printf(" %s\n", line);
1479 832c249c 2018-06-10 stsp } while (line);
1480 621015ac 2018-07-23 stsp free(logmsg0);
1481 832c249c 2018-06-10 stsp
1482 971751ac 2018-03-27 stsp if (show_patch) {
1483 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1484 971751ac 2018-03-27 stsp if (err == 0)
1485 971751ac 2018-03-27 stsp printf("\n");
1486 971751ac 2018-03-27 stsp }
1487 07862c20 2018-09-15 stsp
1488 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1489 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1490 07862c20 2018-09-15 stsp return err;
1491 f42b1b34 2018-03-12 stsp }
1492 5c860e29 2018-03-12 stsp
1493 f42b1b34 2018-03-12 stsp static const struct got_error *
1494 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1495 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1496 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1497 f42b1b34 2018-03-12 stsp {
1498 f42b1b34 2018-03-12 stsp const struct got_error *err;
1499 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1500 372ccdbb 2018-06-10 stsp
1501 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1502 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1503 f42b1b34 2018-03-12 stsp if (err)
1504 f42b1b34 2018-03-12 stsp return err;
1505 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1506 372ccdbb 2018-06-10 stsp if (err)
1507 fcc85cad 2018-07-22 stsp goto done;
1508 656b1f76 2019-05-11 jcs for (;;) {
1509 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1510 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1511 8bf5b3c9 2018-03-17 stsp
1512 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1513 84453469 2018-11-11 stsp break;
1514 84453469 2018-11-11 stsp
1515 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1516 372ccdbb 2018-06-10 stsp if (err) {
1517 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1518 9ba79e04 2018-06-11 stsp err = NULL;
1519 9ba79e04 2018-06-11 stsp break;
1520 9ba79e04 2018-06-11 stsp }
1521 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1522 372ccdbb 2018-06-10 stsp break;
1523 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1524 372ccdbb 2018-06-10 stsp if (err)
1525 372ccdbb 2018-06-10 stsp break;
1526 372ccdbb 2018-06-10 stsp else
1527 372ccdbb 2018-06-10 stsp continue;
1528 7e665116 2018-04-02 stsp }
1529 b43fbaa0 2018-06-11 stsp if (id == NULL)
1530 7e665116 2018-04-02 stsp break;
1531 b43fbaa0 2018-06-11 stsp
1532 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1533 b43fbaa0 2018-06-11 stsp if (err)
1534 fcc85cad 2018-07-22 stsp break;
1535 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1536 199a4027 2019-02-02 stsp refs);
1537 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1538 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1539 7e665116 2018-04-02 stsp break;
1540 31cedeaf 2018-09-15 stsp }
1541 fcc85cad 2018-07-22 stsp done:
1542 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1543 f42b1b34 2018-03-12 stsp return err;
1544 f42b1b34 2018-03-12 stsp }
1545 5c860e29 2018-03-12 stsp
1546 4ed7e80c 2018-05-20 stsp __dead static void
1547 6f3d1eb0 2018-03-12 stsp usage_log(void)
1548 6f3d1eb0 2018-03-12 stsp {
1549 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1550 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1551 6f3d1eb0 2018-03-12 stsp exit(1);
1552 6f3d1eb0 2018-03-12 stsp }
1553 6f3d1eb0 2018-03-12 stsp
1554 4ed7e80c 2018-05-20 stsp static const struct got_error *
1555 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1556 f42b1b34 2018-03-12 stsp {
1557 f42b1b34 2018-03-12 stsp const struct got_error *error;
1558 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1559 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1560 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1561 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1562 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1563 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1564 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1565 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1566 64a96a6d 2018-04-01 stsp const char *errstr;
1567 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1568 1b3893a2 2019-03-18 stsp
1569 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1570 5c860e29 2018-03-12 stsp
1571 6715a751 2018-03-16 stsp #ifndef PROFILE
1572 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1573 6098196c 2019-01-04 stsp NULL)
1574 ad242220 2018-09-08 stsp == -1)
1575 f42b1b34 2018-03-12 stsp err(1, "pledge");
1576 6715a751 2018-03-16 stsp #endif
1577 79109fed 2018-03-27 stsp
1578 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1579 79109fed 2018-03-27 stsp switch (ch) {
1580 79109fed 2018-03-27 stsp case 'p':
1581 79109fed 2018-03-27 stsp show_patch = 1;
1582 d142fc45 2018-04-01 stsp break;
1583 d142fc45 2018-04-01 stsp case 'c':
1584 d142fc45 2018-04-01 stsp start_commit = optarg;
1585 64a96a6d 2018-04-01 stsp break;
1586 c0cc5c62 2018-10-18 stsp case 'C':
1587 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1588 4a8520aa 2018-10-18 stsp &errstr);
1589 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1590 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1591 c0cc5c62 2018-10-18 stsp break;
1592 64a96a6d 2018-04-01 stsp case 'l':
1593 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1594 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1595 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1596 cc54c501 2019-07-15 stsp break;
1597 cc54c501 2019-07-15 stsp case 'f':
1598 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1599 a0603db2 2018-06-10 stsp break;
1600 04ca23f4 2018-07-16 stsp case 'r':
1601 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1602 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1603 04ca23f4 2018-07-16 stsp err(1, "-r option");
1604 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1605 04ca23f4 2018-07-16 stsp break;
1606 79109fed 2018-03-27 stsp default:
1607 2deda0b9 2019-03-07 stsp usage_log();
1608 79109fed 2018-03-27 stsp /* NOTREACHED */
1609 79109fed 2018-03-27 stsp }
1610 79109fed 2018-03-27 stsp }
1611 79109fed 2018-03-27 stsp
1612 79109fed 2018-03-27 stsp argc -= optind;
1613 79109fed 2018-03-27 stsp argv += optind;
1614 f42b1b34 2018-03-12 stsp
1615 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1616 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1617 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1618 04ca23f4 2018-07-16 stsp goto done;
1619 04ca23f4 2018-07-16 stsp }
1620 cffc0aa4 2019-02-05 stsp
1621 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1622 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1623 cffc0aa4 2019-02-05 stsp goto done;
1624 cffc0aa4 2019-02-05 stsp error = NULL;
1625 cffc0aa4 2019-02-05 stsp
1626 e7301579 2019-03-18 stsp if (argc == 0) {
1627 cbd1af7a 2019-03-18 stsp path = strdup("");
1628 e7301579 2019-03-18 stsp if (path == NULL) {
1629 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1630 cbd1af7a 2019-03-18 stsp goto done;
1631 e7301579 2019-03-18 stsp }
1632 e7301579 2019-03-18 stsp } else if (argc == 1) {
1633 e7301579 2019-03-18 stsp if (worktree) {
1634 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1635 e7301579 2019-03-18 stsp argv[0]);
1636 e7301579 2019-03-18 stsp if (error)
1637 e7301579 2019-03-18 stsp goto done;
1638 e7301579 2019-03-18 stsp } else {
1639 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1640 e7301579 2019-03-18 stsp if (path == NULL) {
1641 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1642 e7301579 2019-03-18 stsp goto done;
1643 e7301579 2019-03-18 stsp }
1644 e7301579 2019-03-18 stsp }
1645 cbd1af7a 2019-03-18 stsp } else
1646 cbd1af7a 2019-03-18 stsp usage_log();
1647 cbd1af7a 2019-03-18 stsp
1648 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1649 5486daa2 2019-05-11 stsp repo_path = worktree ?
1650 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1651 5486daa2 2019-05-11 stsp }
1652 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1653 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1654 cffc0aa4 2019-02-05 stsp goto done;
1655 04ca23f4 2018-07-16 stsp }
1656 6098196c 2019-01-04 stsp
1657 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1658 d7d4f210 2018-03-12 stsp if (error != NULL)
1659 04ca23f4 2018-07-16 stsp goto done;
1660 f42b1b34 2018-03-12 stsp
1661 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1662 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1663 c02c541e 2019-03-29 stsp if (error)
1664 c02c541e 2019-03-29 stsp goto done;
1665 c02c541e 2019-03-29 stsp
1666 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1667 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1668 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1669 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1670 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1671 3235492e 2018-04-01 stsp if (error != NULL)
1672 3235492e 2018-04-01 stsp return error;
1673 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1674 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1675 3235492e 2018-04-01 stsp if (error != NULL)
1676 3235492e 2018-04-01 stsp return error;
1677 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1678 3235492e 2018-04-01 stsp } else {
1679 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1680 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1681 3235492e 2018-04-01 stsp if (error == NULL) {
1682 1caad61b 2019-02-01 stsp int obj_type;
1683 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1684 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1685 d1f2edc9 2018-06-13 stsp if (error != NULL)
1686 1caad61b 2019-02-01 stsp goto done;
1687 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1688 1caad61b 2019-02-01 stsp if (error != NULL)
1689 1caad61b 2019-02-01 stsp goto done;
1690 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1691 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1692 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1693 1caad61b 2019-02-01 stsp if (error != NULL)
1694 1caad61b 2019-02-01 stsp goto done;
1695 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1696 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1697 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1698 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1699 1caad61b 2019-02-01 stsp goto done;
1700 1caad61b 2019-02-01 stsp }
1701 1caad61b 2019-02-01 stsp free(id);
1702 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1703 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1704 1caad61b 2019-02-01 stsp if (id == NULL)
1705 638f9024 2019-05-13 stsp error = got_error_from_errno(
1706 230a42bd 2019-05-11 jcs "got_object_id_dup");
1707 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1708 1caad61b 2019-02-01 stsp if (error)
1709 1caad61b 2019-02-01 stsp goto done;
1710 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1711 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1712 1caad61b 2019-02-01 stsp goto done;
1713 1caad61b 2019-02-01 stsp }
1714 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1715 d1f2edc9 2018-06-13 stsp if (error != NULL)
1716 1caad61b 2019-02-01 stsp goto done;
1717 d1f2edc9 2018-06-13 stsp }
1718 15a94983 2018-12-23 stsp if (commit == NULL) {
1719 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1720 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1721 d1f2edc9 2018-06-13 stsp if (error != NULL)
1722 d1f2edc9 2018-06-13 stsp return error;
1723 3235492e 2018-04-01 stsp }
1724 3235492e 2018-04-01 stsp }
1725 d7d4f210 2018-03-12 stsp if (error != NULL)
1726 04ca23f4 2018-07-16 stsp goto done;
1727 04ca23f4 2018-07-16 stsp
1728 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1729 04ca23f4 2018-07-16 stsp if (error != NULL)
1730 04ca23f4 2018-07-16 stsp goto done;
1731 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1732 04ca23f4 2018-07-16 stsp free(path);
1733 04ca23f4 2018-07-16 stsp path = in_repo_path;
1734 04ca23f4 2018-07-16 stsp }
1735 199a4027 2019-02-02 stsp
1736 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1737 199a4027 2019-02-02 stsp if (error)
1738 199a4027 2019-02-02 stsp goto done;
1739 04ca23f4 2018-07-16 stsp
1740 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1741 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1742 04ca23f4 2018-07-16 stsp done:
1743 04ca23f4 2018-07-16 stsp free(path);
1744 04ca23f4 2018-07-16 stsp free(repo_path);
1745 04ca23f4 2018-07-16 stsp free(cwd);
1746 f42b1b34 2018-03-12 stsp free(id);
1747 cffc0aa4 2019-02-05 stsp if (worktree)
1748 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1749 ad242220 2018-09-08 stsp if (repo) {
1750 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1751 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1752 ad242220 2018-09-08 stsp if (error == NULL)
1753 ad242220 2018-09-08 stsp error = repo_error;
1754 ad242220 2018-09-08 stsp }
1755 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1756 8bf5b3c9 2018-03-17 stsp return error;
1757 5c860e29 2018-03-12 stsp }
1758 5c860e29 2018-03-12 stsp
1759 4ed7e80c 2018-05-20 stsp __dead static void
1760 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1761 3f8b7d6a 2018-04-01 stsp {
1762 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1763 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1764 3f8b7d6a 2018-04-01 stsp exit(1);
1765 b00d56cd 2018-04-01 stsp }
1766 b00d56cd 2018-04-01 stsp
1767 b72f483a 2019-02-05 stsp struct print_diff_arg {
1768 b72f483a 2019-02-05 stsp struct got_repository *repo;
1769 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1770 b72f483a 2019-02-05 stsp int diff_context;
1771 3fc0c068 2019-02-10 stsp const char *id_str;
1772 3fc0c068 2019-02-10 stsp int header_shown;
1773 98eaaa12 2019-08-03 stsp int diff_staged;
1774 b72f483a 2019-02-05 stsp };
1775 b72f483a 2019-02-05 stsp
1776 4ed7e80c 2018-05-20 stsp static const struct got_error *
1777 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1778 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1779 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1780 b72f483a 2019-02-05 stsp {
1781 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1782 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1783 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1784 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1785 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1786 b72f483a 2019-02-05 stsp struct stat sb;
1787 b72f483a 2019-02-05 stsp
1788 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1789 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1790 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1791 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1792 98eaaa12 2019-08-03 stsp return NULL;
1793 98eaaa12 2019-08-03 stsp } else {
1794 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1795 98eaaa12 2019-08-03 stsp return NULL;
1796 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1797 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1798 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1799 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1800 98eaaa12 2019-08-03 stsp return NULL;
1801 98eaaa12 2019-08-03 stsp }
1802 3fc0c068 2019-02-10 stsp
1803 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1804 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1805 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
1806 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
1807 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1808 3fc0c068 2019-02-10 stsp }
1809 b72f483a 2019-02-05 stsp
1810 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1811 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
1812 98eaaa12 2019-08-03 stsp switch (staged_status) {
1813 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
1814 98eaaa12 2019-08-03 stsp label1 = path;
1815 98eaaa12 2019-08-03 stsp label2 = path;
1816 98eaaa12 2019-08-03 stsp break;
1817 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
1818 98eaaa12 2019-08-03 stsp label2 = path;
1819 98eaaa12 2019-08-03 stsp break;
1820 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
1821 98eaaa12 2019-08-03 stsp label1 = path;
1822 98eaaa12 2019-08-03 stsp break;
1823 98eaaa12 2019-08-03 stsp default:
1824 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
1825 98eaaa12 2019-08-03 stsp }
1826 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1827 98eaaa12 2019-08-03 stsp label1, label2, a->diff_context, a->repo, stdout);
1828 98eaaa12 2019-08-03 stsp }
1829 98eaaa12 2019-08-03 stsp
1830 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
1831 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
1832 4ce46740 2019-08-08 stsp char *id_str;
1833 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1834 408b4ebc 2019-08-03 stsp 8192);
1835 4ce46740 2019-08-08 stsp if (err)
1836 4ce46740 2019-08-08 stsp goto done;
1837 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
1838 4ce46740 2019-08-08 stsp if (err)
1839 4ce46740 2019-08-08 stsp goto done;
1840 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1841 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
1842 4ce46740 2019-08-08 stsp free(id_str);
1843 4ce46740 2019-08-08 stsp goto done;
1844 4ce46740 2019-08-08 stsp }
1845 4ce46740 2019-08-08 stsp free(id_str);
1846 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
1847 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1848 4ce46740 2019-08-08 stsp if (err)
1849 4ce46740 2019-08-08 stsp goto done;
1850 4ce46740 2019-08-08 stsp }
1851 d00136be 2019-03-26 stsp
1852 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1853 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1854 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1855 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1856 2ec1f75b 2019-03-26 stsp goto done;
1857 2ec1f75b 2019-03-26 stsp }
1858 b72f483a 2019-02-05 stsp
1859 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1860 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1861 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1862 2ec1f75b 2019-03-26 stsp goto done;
1863 2ec1f75b 2019-03-26 stsp }
1864 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1865 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1866 2ec1f75b 2019-03-26 stsp goto done;
1867 2ec1f75b 2019-03-26 stsp }
1868 2ec1f75b 2019-03-26 stsp } else
1869 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1870 b72f483a 2019-02-05 stsp
1871 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1872 4ce46740 2019-08-08 stsp a->diff_context, stdout);
1873 b72f483a 2019-02-05 stsp done:
1874 b72f483a 2019-02-05 stsp if (blob1)
1875 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1876 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1877 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1878 b72f483a 2019-02-05 stsp free(abspath);
1879 927df6b7 2019-02-10 stsp return err;
1880 927df6b7 2019-02-10 stsp }
1881 927df6b7 2019-02-10 stsp
1882 927df6b7 2019-02-10 stsp static const struct got_error *
1883 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
1884 0adb7196 2019-08-11 stsp const char *id_str, int obj_type, struct got_repository *repo)
1885 0adb7196 2019-08-11 stsp {
1886 0adb7196 2019-08-11 stsp const struct got_error *err;
1887 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
1888 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
1889 0adb7196 2019-08-11 stsp
1890 0adb7196 2019-08-11 stsp *id = NULL;
1891 0adb7196 2019-08-11 stsp *label = NULL;
1892 d24820bf 2019-08-11 stsp
1893 d24820bf 2019-08-11 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1894 d24820bf 2019-08-11 stsp if (err == NULL) {
1895 d24820bf 2019-08-11 stsp *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1896 d24820bf 2019-08-11 stsp if (*id == NULL)
1897 d24820bf 2019-08-11 stsp err = got_error_from_errno("got_object_id_dup");
1898 d24820bf 2019-08-11 stsp if (asprintf(label, "refs/tags/%s",
1899 1b7a69d3 2019-08-11 stsp got_object_tag_get_name(tag)) == -1)
1900 d24820bf 2019-08-11 stsp err = got_error_from_errno("asprintf");
1901 d24820bf 2019-08-11 stsp got_object_tag_close(tag);
1902 d24820bf 2019-08-11 stsp return err;
1903 d24820bf 2019-08-11 stsp } else if (err->code != GOT_ERR_NO_OBJ)
1904 d24820bf 2019-08-11 stsp return err;
1905 0adb7196 2019-08-11 stsp
1906 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1907 0adb7196 2019-08-11 stsp if (err) {
1908 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1909 0adb7196 2019-08-11 stsp return err;
1910 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
1911 0adb7196 2019-08-11 stsp if (err != NULL)
1912 0adb7196 2019-08-11 stsp goto done;
1913 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
1914 0adb7196 2019-08-11 stsp if (*label == NULL) {
1915 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1916 0adb7196 2019-08-11 stsp goto done;
1917 0adb7196 2019-08-11 stsp }
1918 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
1919 0adb7196 2019-08-11 stsp } else {
1920 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
1921 0adb7196 2019-08-11 stsp if (*label == NULL) {
1922 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
1923 0adb7196 2019-08-11 stsp goto done;
1924 0adb7196 2019-08-11 stsp }
1925 0adb7196 2019-08-11 stsp }
1926 0adb7196 2019-08-11 stsp done:
1927 0adb7196 2019-08-11 stsp if (ref)
1928 0adb7196 2019-08-11 stsp got_ref_close(ref);
1929 0adb7196 2019-08-11 stsp return err;
1930 0adb7196 2019-08-11 stsp }
1931 0adb7196 2019-08-11 stsp
1932 0adb7196 2019-08-11 stsp
1933 0adb7196 2019-08-11 stsp static const struct got_error *
1934 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1935 b00d56cd 2018-04-01 stsp {
1936 b00d56cd 2018-04-01 stsp const struct got_error *error;
1937 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1938 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1939 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1940 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1941 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1942 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1943 15a94983 2018-12-23 stsp int type1, type2;
1944 98eaaa12 2019-08-03 stsp int diff_context = 3, diff_staged = 0, ch;
1945 c0cc5c62 2018-10-18 stsp const char *errstr;
1946 927df6b7 2019-02-10 stsp char *path = NULL;
1947 b00d56cd 2018-04-01 stsp
1948 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1949 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1950 25eccc22 2019-01-04 stsp NULL) == -1)
1951 b00d56cd 2018-04-01 stsp err(1, "pledge");
1952 b00d56cd 2018-04-01 stsp #endif
1953 b00d56cd 2018-04-01 stsp
1954 98eaaa12 2019-08-03 stsp while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1955 b00d56cd 2018-04-01 stsp switch (ch) {
1956 c0cc5c62 2018-10-18 stsp case 'C':
1957 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1958 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1959 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1960 c0cc5c62 2018-10-18 stsp break;
1961 b72f483a 2019-02-05 stsp case 'r':
1962 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1963 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1964 b72f483a 2019-02-05 stsp err(1, "-r option");
1965 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1966 b72f483a 2019-02-05 stsp break;
1967 98eaaa12 2019-08-03 stsp case 's':
1968 98eaaa12 2019-08-03 stsp diff_staged = 1;
1969 98eaaa12 2019-08-03 stsp break;
1970 b00d56cd 2018-04-01 stsp default:
1971 2deda0b9 2019-03-07 stsp usage_diff();
1972 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1973 b00d56cd 2018-04-01 stsp }
1974 b00d56cd 2018-04-01 stsp }
1975 b00d56cd 2018-04-01 stsp
1976 b00d56cd 2018-04-01 stsp argc -= optind;
1977 b00d56cd 2018-04-01 stsp argv += optind;
1978 b00d56cd 2018-04-01 stsp
1979 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1980 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1981 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1982 b72f483a 2019-02-05 stsp goto done;
1983 b72f483a 2019-02-05 stsp }
1984 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1985 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1986 927df6b7 2019-02-10 stsp goto done;
1987 927df6b7 2019-02-10 stsp if (argc <= 1) {
1988 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1989 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1990 927df6b7 2019-02-10 stsp goto done;
1991 927df6b7 2019-02-10 stsp }
1992 b72f483a 2019-02-05 stsp if (repo_path)
1993 b72f483a 2019-02-05 stsp errx(1,
1994 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1995 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1996 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1997 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1998 927df6b7 2019-02-10 stsp goto done;
1999 927df6b7 2019-02-10 stsp }
2000 927df6b7 2019-02-10 stsp if (argc == 1) {
2001 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2002 cbd1af7a 2019-03-18 stsp argv[0]);
2003 927df6b7 2019-02-10 stsp if (error)
2004 927df6b7 2019-02-10 stsp goto done;
2005 927df6b7 2019-02-10 stsp } else {
2006 927df6b7 2019-02-10 stsp path = strdup("");
2007 927df6b7 2019-02-10 stsp if (path == NULL) {
2008 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2009 927df6b7 2019-02-10 stsp goto done;
2010 927df6b7 2019-02-10 stsp }
2011 927df6b7 2019-02-10 stsp }
2012 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2013 98eaaa12 2019-08-03 stsp if (diff_staged)
2014 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2015 98eaaa12 2019-08-03 stsp "objects in repository");
2016 15a94983 2018-12-23 stsp id_str1 = argv[0];
2017 15a94983 2018-12-23 stsp id_str2 = argv[1];
2018 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2019 30db809c 2019-06-05 stsp repo_path =
2020 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2021 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2022 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2023 30db809c 2019-06-05 stsp goto done;
2024 30db809c 2019-06-05 stsp }
2025 30db809c 2019-06-05 stsp }
2026 b00d56cd 2018-04-01 stsp } else
2027 b00d56cd 2018-04-01 stsp usage_diff();
2028 25eccc22 2019-01-04 stsp
2029 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2030 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2031 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2032 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2033 b72f483a 2019-02-05 stsp }
2034 b00d56cd 2018-04-01 stsp
2035 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
2036 76089277 2018-04-01 stsp free(repo_path);
2037 b00d56cd 2018-04-01 stsp if (error != NULL)
2038 b00d56cd 2018-04-01 stsp goto done;
2039 b00d56cd 2018-04-01 stsp
2040 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2041 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2042 c02c541e 2019-03-29 stsp if (error)
2043 c02c541e 2019-03-29 stsp goto done;
2044 c02c541e 2019-03-29 stsp
2045 30db809c 2019-06-05 stsp if (argc <= 1) {
2046 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2047 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2048 b72f483a 2019-02-05 stsp char *id_str;
2049 72ea6654 2019-07-27 stsp
2050 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2051 72ea6654 2019-07-27 stsp
2052 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2053 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2054 b72f483a 2019-02-05 stsp if (error)
2055 b72f483a 2019-02-05 stsp goto done;
2056 b72f483a 2019-02-05 stsp arg.repo = repo;
2057 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2058 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2059 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2060 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2061 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2062 b72f483a 2019-02-05 stsp
2063 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2064 72ea6654 2019-07-27 stsp if (error)
2065 72ea6654 2019-07-27 stsp goto done;
2066 72ea6654 2019-07-27 stsp
2067 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2068 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2069 b72f483a 2019-02-05 stsp free(id_str);
2070 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2071 b72f483a 2019-02-05 stsp goto done;
2072 b72f483a 2019-02-05 stsp }
2073 b72f483a 2019-02-05 stsp
2074 0adb7196 2019-08-11 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2075 0adb7196 2019-08-11 stsp if (error)
2076 0adb7196 2019-08-11 stsp goto done;
2077 b00d56cd 2018-04-01 stsp
2078 0adb7196 2019-08-11 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2079 0adb7196 2019-08-11 stsp if (error)
2080 0adb7196 2019-08-11 stsp goto done;
2081 b00d56cd 2018-04-01 stsp
2082 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2083 15a94983 2018-12-23 stsp if (error)
2084 15a94983 2018-12-23 stsp goto done;
2085 15a94983 2018-12-23 stsp
2086 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2087 15a94983 2018-12-23 stsp if (error)
2088 15a94983 2018-12-23 stsp goto done;
2089 15a94983 2018-12-23 stsp
2090 15a94983 2018-12-23 stsp if (type1 != type2) {
2091 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2092 b00d56cd 2018-04-01 stsp goto done;
2093 b00d56cd 2018-04-01 stsp }
2094 b00d56cd 2018-04-01 stsp
2095 15a94983 2018-12-23 stsp switch (type1) {
2096 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2097 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2098 54156555 2018-12-24 stsp diff_context, repo, stdout);
2099 b00d56cd 2018-04-01 stsp break;
2100 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2101 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2102 54156555 2018-12-24 stsp diff_context, repo, stdout);
2103 b00d56cd 2018-04-01 stsp break;
2104 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2105 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2106 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2107 c0cc5c62 2018-10-18 stsp repo, stdout);
2108 b00d56cd 2018-04-01 stsp break;
2109 b00d56cd 2018-04-01 stsp default:
2110 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2111 b00d56cd 2018-04-01 stsp }
2112 b00d56cd 2018-04-01 stsp
2113 b00d56cd 2018-04-01 stsp done:
2114 5e70831e 2019-05-28 stsp free(label1);
2115 5e70831e 2019-05-28 stsp free(label2);
2116 15a94983 2018-12-23 stsp free(id1);
2117 15a94983 2018-12-23 stsp free(id2);
2118 927df6b7 2019-02-10 stsp free(path);
2119 b72f483a 2019-02-05 stsp if (worktree)
2120 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2121 ad242220 2018-09-08 stsp if (repo) {
2122 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2123 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2124 ad242220 2018-09-08 stsp if (error == NULL)
2125 ad242220 2018-09-08 stsp error = repo_error;
2126 ad242220 2018-09-08 stsp }
2127 b00d56cd 2018-04-01 stsp return error;
2128 404c43c4 2018-06-21 stsp }
2129 404c43c4 2018-06-21 stsp
2130 404c43c4 2018-06-21 stsp __dead static void
2131 404c43c4 2018-06-21 stsp usage_blame(void)
2132 404c43c4 2018-06-21 stsp {
2133 9270e621 2019-02-05 stsp fprintf(stderr,
2134 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2135 404c43c4 2018-06-21 stsp getprogname());
2136 404c43c4 2018-06-21 stsp exit(1);
2137 b00d56cd 2018-04-01 stsp }
2138 b00d56cd 2018-04-01 stsp
2139 404c43c4 2018-06-21 stsp static const struct got_error *
2140 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2141 404c43c4 2018-06-21 stsp {
2142 404c43c4 2018-06-21 stsp const struct got_error *error;
2143 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2144 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2145 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2146 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2147 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2148 404c43c4 2018-06-21 stsp int ch;
2149 404c43c4 2018-06-21 stsp
2150 404c43c4 2018-06-21 stsp #ifndef PROFILE
2151 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2152 36e2fb66 2019-01-04 stsp NULL) == -1)
2153 404c43c4 2018-06-21 stsp err(1, "pledge");
2154 404c43c4 2018-06-21 stsp #endif
2155 404c43c4 2018-06-21 stsp
2156 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2157 404c43c4 2018-06-21 stsp switch (ch) {
2158 404c43c4 2018-06-21 stsp case 'c':
2159 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2160 404c43c4 2018-06-21 stsp break;
2161 66bea077 2018-08-02 stsp case 'r':
2162 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2163 66bea077 2018-08-02 stsp if (repo_path == NULL)
2164 66bea077 2018-08-02 stsp err(1, "-r option");
2165 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2166 66bea077 2018-08-02 stsp break;
2167 404c43c4 2018-06-21 stsp default:
2168 2deda0b9 2019-03-07 stsp usage_blame();
2169 404c43c4 2018-06-21 stsp /* NOTREACHED */
2170 404c43c4 2018-06-21 stsp }
2171 404c43c4 2018-06-21 stsp }
2172 404c43c4 2018-06-21 stsp
2173 404c43c4 2018-06-21 stsp argc -= optind;
2174 404c43c4 2018-06-21 stsp argv += optind;
2175 404c43c4 2018-06-21 stsp
2176 a39318fd 2018-08-02 stsp if (argc == 1)
2177 404c43c4 2018-06-21 stsp path = argv[0];
2178 a39318fd 2018-08-02 stsp else
2179 404c43c4 2018-06-21 stsp usage_blame();
2180 404c43c4 2018-06-21 stsp
2181 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2182 66bea077 2018-08-02 stsp if (cwd == NULL) {
2183 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2184 66bea077 2018-08-02 stsp goto done;
2185 66bea077 2018-08-02 stsp }
2186 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2187 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2188 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2189 66bea077 2018-08-02 stsp goto done;
2190 0c06baac 2019-02-05 stsp else
2191 0c06baac 2019-02-05 stsp error = NULL;
2192 0c06baac 2019-02-05 stsp if (worktree) {
2193 0c06baac 2019-02-05 stsp repo_path =
2194 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2195 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2196 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2197 0c06baac 2019-02-05 stsp if (error)
2198 0c06baac 2019-02-05 stsp goto done;
2199 0c06baac 2019-02-05 stsp } else {
2200 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2201 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2202 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2203 0c06baac 2019-02-05 stsp goto done;
2204 0c06baac 2019-02-05 stsp }
2205 66bea077 2018-08-02 stsp }
2206 66bea077 2018-08-02 stsp }
2207 36e2fb66 2019-01-04 stsp
2208 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2209 c02c541e 2019-03-29 stsp if (error != NULL)
2210 36e2fb66 2019-01-04 stsp goto done;
2211 66bea077 2018-08-02 stsp
2212 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2213 c02c541e 2019-03-29 stsp if (error)
2214 404c43c4 2018-06-21 stsp goto done;
2215 404c43c4 2018-06-21 stsp
2216 0c06baac 2019-02-05 stsp if (worktree) {
2217 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2218 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2219 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2220 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2221 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2222 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2223 6efaaa2d 2019-02-05 stsp path) == -1) {
2224 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2225 0c06baac 2019-02-05 stsp goto done;
2226 0c06baac 2019-02-05 stsp }
2227 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2228 0c06baac 2019-02-05 stsp free(p);
2229 0c06baac 2019-02-05 stsp } else {
2230 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2231 0c06baac 2019-02-05 stsp }
2232 0c06baac 2019-02-05 stsp if (error)
2233 66bea077 2018-08-02 stsp goto done;
2234 66bea077 2018-08-02 stsp
2235 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2236 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2237 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2238 404c43c4 2018-06-21 stsp if (error != NULL)
2239 66bea077 2018-08-02 stsp goto done;
2240 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2241 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2242 404c43c4 2018-06-21 stsp if (error != NULL)
2243 66bea077 2018-08-02 stsp goto done;
2244 404c43c4 2018-06-21 stsp } else {
2245 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2246 30837e32 2019-07-25 stsp if (error)
2247 66bea077 2018-08-02 stsp goto done;
2248 404c43c4 2018-06-21 stsp }
2249 404c43c4 2018-06-21 stsp
2250 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2251 404c43c4 2018-06-21 stsp done:
2252 66bea077 2018-08-02 stsp free(in_repo_path);
2253 66bea077 2018-08-02 stsp free(repo_path);
2254 66bea077 2018-08-02 stsp free(cwd);
2255 404c43c4 2018-06-21 stsp free(commit_id);
2256 0c06baac 2019-02-05 stsp if (worktree)
2257 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2258 ad242220 2018-09-08 stsp if (repo) {
2259 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2260 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2261 ad242220 2018-09-08 stsp if (error == NULL)
2262 ad242220 2018-09-08 stsp error = repo_error;
2263 ad242220 2018-09-08 stsp }
2264 404c43c4 2018-06-21 stsp return error;
2265 5de5890b 2018-10-18 stsp }
2266 5de5890b 2018-10-18 stsp
2267 5de5890b 2018-10-18 stsp __dead static void
2268 5de5890b 2018-10-18 stsp usage_tree(void)
2269 5de5890b 2018-10-18 stsp {
2270 c1669e2e 2019-01-09 stsp fprintf(stderr,
2271 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2272 5de5890b 2018-10-18 stsp getprogname());
2273 5de5890b 2018-10-18 stsp exit(1);
2274 5de5890b 2018-10-18 stsp }
2275 5de5890b 2018-10-18 stsp
2276 c1669e2e 2019-01-09 stsp static void
2277 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2278 c1669e2e 2019-01-09 stsp const char *root_path)
2279 c1669e2e 2019-01-09 stsp {
2280 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2281 848d6979 2019-08-12 stsp const char *modestr = "";
2282 5de5890b 2018-10-18 stsp
2283 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2284 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2285 c1669e2e 2019-01-09 stsp path++;
2286 c1669e2e 2019-01-09 stsp
2287 848d6979 2019-08-12 stsp if (S_ISLNK(te->mode))
2288 848d6979 2019-08-12 stsp modestr = "@";
2289 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2290 848d6979 2019-08-12 stsp modestr = "/";
2291 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2292 848d6979 2019-08-12 stsp modestr = "*";
2293 848d6979 2019-08-12 stsp
2294 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2295 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2296 c1669e2e 2019-01-09 stsp }
2297 c1669e2e 2019-01-09 stsp
2298 5de5890b 2018-10-18 stsp static const struct got_error *
2299 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2300 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2301 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2302 5de5890b 2018-10-18 stsp {
2303 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2304 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2305 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2306 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2307 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2308 5de5890b 2018-10-18 stsp
2309 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2310 5de5890b 2018-10-18 stsp if (err)
2311 5de5890b 2018-10-18 stsp goto done;
2312 5de5890b 2018-10-18 stsp
2313 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2314 5de5890b 2018-10-18 stsp if (err)
2315 5de5890b 2018-10-18 stsp goto done;
2316 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2317 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2318 5de5890b 2018-10-18 stsp while (te) {
2319 5de5890b 2018-10-18 stsp char *id = NULL;
2320 84453469 2018-11-11 stsp
2321 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2322 84453469 2018-11-11 stsp break;
2323 84453469 2018-11-11 stsp
2324 5de5890b 2018-10-18 stsp if (show_ids) {
2325 5de5890b 2018-10-18 stsp char *id_str;
2326 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2327 5de5890b 2018-10-18 stsp if (err)
2328 5de5890b 2018-10-18 stsp goto done;
2329 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2330 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2331 5de5890b 2018-10-18 stsp free(id_str);
2332 5de5890b 2018-10-18 stsp goto done;
2333 5de5890b 2018-10-18 stsp }
2334 5de5890b 2018-10-18 stsp free(id_str);
2335 5de5890b 2018-10-18 stsp }
2336 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2337 5de5890b 2018-10-18 stsp free(id);
2338 c1669e2e 2019-01-09 stsp
2339 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2340 c1669e2e 2019-01-09 stsp char *child_path;
2341 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2342 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2343 c1669e2e 2019-01-09 stsp te->name) == -1) {
2344 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2345 c1669e2e 2019-01-09 stsp goto done;
2346 c1669e2e 2019-01-09 stsp }
2347 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2348 c1669e2e 2019-01-09 stsp root_path, repo);
2349 c1669e2e 2019-01-09 stsp free(child_path);
2350 c1669e2e 2019-01-09 stsp if (err)
2351 c1669e2e 2019-01-09 stsp goto done;
2352 c1669e2e 2019-01-09 stsp }
2353 c1669e2e 2019-01-09 stsp
2354 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2355 5de5890b 2018-10-18 stsp }
2356 5de5890b 2018-10-18 stsp done:
2357 5de5890b 2018-10-18 stsp if (tree)
2358 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2359 5de5890b 2018-10-18 stsp free(tree_id);
2360 5de5890b 2018-10-18 stsp return err;
2361 404c43c4 2018-06-21 stsp }
2362 404c43c4 2018-06-21 stsp
2363 5de5890b 2018-10-18 stsp static const struct got_error *
2364 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2365 5de5890b 2018-10-18 stsp {
2366 5de5890b 2018-10-18 stsp const struct got_error *error;
2367 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2368 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2369 7a2c19d6 2019-02-05 stsp const char *path;
2370 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2371 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2372 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2373 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2374 5de5890b 2018-10-18 stsp int ch;
2375 5de5890b 2018-10-18 stsp
2376 5de5890b 2018-10-18 stsp #ifndef PROFILE
2377 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2378 0f8d269b 2019-01-04 stsp NULL) == -1)
2379 5de5890b 2018-10-18 stsp err(1, "pledge");
2380 5de5890b 2018-10-18 stsp #endif
2381 5de5890b 2018-10-18 stsp
2382 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2383 5de5890b 2018-10-18 stsp switch (ch) {
2384 5de5890b 2018-10-18 stsp case 'c':
2385 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2386 5de5890b 2018-10-18 stsp break;
2387 5de5890b 2018-10-18 stsp case 'r':
2388 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2389 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2390 5de5890b 2018-10-18 stsp err(1, "-r option");
2391 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2392 5de5890b 2018-10-18 stsp break;
2393 5de5890b 2018-10-18 stsp case 'i':
2394 5de5890b 2018-10-18 stsp show_ids = 1;
2395 5de5890b 2018-10-18 stsp break;
2396 c1669e2e 2019-01-09 stsp case 'R':
2397 c1669e2e 2019-01-09 stsp recurse = 1;
2398 c1669e2e 2019-01-09 stsp break;
2399 5de5890b 2018-10-18 stsp default:
2400 2deda0b9 2019-03-07 stsp usage_tree();
2401 5de5890b 2018-10-18 stsp /* NOTREACHED */
2402 5de5890b 2018-10-18 stsp }
2403 5de5890b 2018-10-18 stsp }
2404 5de5890b 2018-10-18 stsp
2405 5de5890b 2018-10-18 stsp argc -= optind;
2406 5de5890b 2018-10-18 stsp argv += optind;
2407 5de5890b 2018-10-18 stsp
2408 5de5890b 2018-10-18 stsp if (argc == 1)
2409 5de5890b 2018-10-18 stsp path = argv[0];
2410 5de5890b 2018-10-18 stsp else if (argc > 1)
2411 5de5890b 2018-10-18 stsp usage_tree();
2412 5de5890b 2018-10-18 stsp else
2413 7a2c19d6 2019-02-05 stsp path = NULL;
2414 7a2c19d6 2019-02-05 stsp
2415 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2416 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2417 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2418 9bf7a39b 2019-02-05 stsp goto done;
2419 9bf7a39b 2019-02-05 stsp }
2420 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2421 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2422 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2423 7a2c19d6 2019-02-05 stsp goto done;
2424 7a2c19d6 2019-02-05 stsp else
2425 7a2c19d6 2019-02-05 stsp error = NULL;
2426 7a2c19d6 2019-02-05 stsp if (worktree) {
2427 7a2c19d6 2019-02-05 stsp repo_path =
2428 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2429 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2430 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2431 7a2c19d6 2019-02-05 stsp if (error)
2432 7a2c19d6 2019-02-05 stsp goto done;
2433 7a2c19d6 2019-02-05 stsp } else {
2434 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2435 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2436 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2437 7a2c19d6 2019-02-05 stsp goto done;
2438 7a2c19d6 2019-02-05 stsp }
2439 5de5890b 2018-10-18 stsp }
2440 5de5890b 2018-10-18 stsp }
2441 5de5890b 2018-10-18 stsp
2442 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2443 5de5890b 2018-10-18 stsp if (error != NULL)
2444 c02c541e 2019-03-29 stsp goto done;
2445 c02c541e 2019-03-29 stsp
2446 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2447 c02c541e 2019-03-29 stsp if (error)
2448 5de5890b 2018-10-18 stsp goto done;
2449 5de5890b 2018-10-18 stsp
2450 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2451 9bf7a39b 2019-02-05 stsp if (worktree) {
2452 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2453 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2454 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2455 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2456 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2457 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2458 9bf7a39b 2019-02-05 stsp goto done;
2459 9bf7a39b 2019-02-05 stsp }
2460 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2461 9bf7a39b 2019-02-05 stsp free(p);
2462 9bf7a39b 2019-02-05 stsp if (error)
2463 9bf7a39b 2019-02-05 stsp goto done;
2464 9bf7a39b 2019-02-05 stsp } else
2465 9bf7a39b 2019-02-05 stsp path = "/";
2466 9bf7a39b 2019-02-05 stsp }
2467 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2468 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2469 9bf7a39b 2019-02-05 stsp if (error != NULL)
2470 9bf7a39b 2019-02-05 stsp goto done;
2471 9bf7a39b 2019-02-05 stsp }
2472 5de5890b 2018-10-18 stsp
2473 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2474 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2475 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2476 5de5890b 2018-10-18 stsp if (error != NULL)
2477 5de5890b 2018-10-18 stsp goto done;
2478 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2479 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2480 5de5890b 2018-10-18 stsp if (error != NULL)
2481 5de5890b 2018-10-18 stsp goto done;
2482 5de5890b 2018-10-18 stsp } else {
2483 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2484 30837e32 2019-07-25 stsp if (error)
2485 5de5890b 2018-10-18 stsp goto done;
2486 5de5890b 2018-10-18 stsp }
2487 5de5890b 2018-10-18 stsp
2488 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2489 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2490 5de5890b 2018-10-18 stsp done:
2491 5de5890b 2018-10-18 stsp free(in_repo_path);
2492 5de5890b 2018-10-18 stsp free(repo_path);
2493 5de5890b 2018-10-18 stsp free(cwd);
2494 5de5890b 2018-10-18 stsp free(commit_id);
2495 7a2c19d6 2019-02-05 stsp if (worktree)
2496 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2497 5de5890b 2018-10-18 stsp if (repo) {
2498 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2499 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2500 5de5890b 2018-10-18 stsp if (error == NULL)
2501 5de5890b 2018-10-18 stsp error = repo_error;
2502 5de5890b 2018-10-18 stsp }
2503 5de5890b 2018-10-18 stsp return error;
2504 5de5890b 2018-10-18 stsp }
2505 5de5890b 2018-10-18 stsp
2506 6bad629b 2019-02-04 stsp __dead static void
2507 6bad629b 2019-02-04 stsp usage_status(void)
2508 6bad629b 2019-02-04 stsp {
2509 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2510 6bad629b 2019-02-04 stsp exit(1);
2511 6bad629b 2019-02-04 stsp }
2512 5c860e29 2018-03-12 stsp
2513 b72f483a 2019-02-05 stsp static const struct got_error *
2514 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2515 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2516 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2517 6bad629b 2019-02-04 stsp {
2518 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2519 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2520 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2521 b72f483a 2019-02-05 stsp return NULL;
2522 6bad629b 2019-02-04 stsp }
2523 5c860e29 2018-03-12 stsp
2524 6bad629b 2019-02-04 stsp static const struct got_error *
2525 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2526 6bad629b 2019-02-04 stsp {
2527 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2528 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2529 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2530 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2531 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2532 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2533 a5edda0a 2019-07-27 stsp int ch;
2534 5c860e29 2018-03-12 stsp
2535 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2536 72ea6654 2019-07-27 stsp
2537 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2538 6bad629b 2019-02-04 stsp switch (ch) {
2539 5c860e29 2018-03-12 stsp default:
2540 2deda0b9 2019-03-07 stsp usage_status();
2541 6bad629b 2019-02-04 stsp /* NOTREACHED */
2542 5c860e29 2018-03-12 stsp }
2543 5c860e29 2018-03-12 stsp }
2544 5c860e29 2018-03-12 stsp
2545 6bad629b 2019-02-04 stsp argc -= optind;
2546 6bad629b 2019-02-04 stsp argv += optind;
2547 5c860e29 2018-03-12 stsp
2548 6bad629b 2019-02-04 stsp #ifndef PROFILE
2549 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2550 6bad629b 2019-02-04 stsp NULL) == -1)
2551 6bad629b 2019-02-04 stsp err(1, "pledge");
2552 f42b1b34 2018-03-12 stsp #endif
2553 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2554 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2555 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2556 927df6b7 2019-02-10 stsp goto done;
2557 927df6b7 2019-02-10 stsp }
2558 927df6b7 2019-02-10 stsp
2559 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2560 927df6b7 2019-02-10 stsp if (error != NULL)
2561 a5edda0a 2019-07-27 stsp goto done;
2562 6bad629b 2019-02-04 stsp
2563 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2564 6bad629b 2019-02-04 stsp if (error != NULL)
2565 6bad629b 2019-02-04 stsp goto done;
2566 6bad629b 2019-02-04 stsp
2567 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2568 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2569 087fb88c 2019-08-04 stsp if (error)
2570 087fb88c 2019-08-04 stsp goto done;
2571 087fb88c 2019-08-04 stsp
2572 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2573 6bad629b 2019-02-04 stsp if (error)
2574 6bad629b 2019-02-04 stsp goto done;
2575 6bad629b 2019-02-04 stsp
2576 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2577 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2578 6bad629b 2019-02-04 stsp done:
2579 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2580 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2581 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2582 927df6b7 2019-02-10 stsp free(cwd);
2583 d0eebce4 2019-03-11 stsp return error;
2584 d0eebce4 2019-03-11 stsp }
2585 d0eebce4 2019-03-11 stsp
2586 d0eebce4 2019-03-11 stsp __dead static void
2587 d0eebce4 2019-03-11 stsp usage_ref(void)
2588 d0eebce4 2019-03-11 stsp {
2589 d0eebce4 2019-03-11 stsp fprintf(stderr,
2590 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2591 d0eebce4 2019-03-11 stsp getprogname());
2592 d0eebce4 2019-03-11 stsp exit(1);
2593 d0eebce4 2019-03-11 stsp }
2594 d0eebce4 2019-03-11 stsp
2595 d0eebce4 2019-03-11 stsp static const struct got_error *
2596 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2597 d0eebce4 2019-03-11 stsp {
2598 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2599 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2600 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2601 d0eebce4 2019-03-11 stsp
2602 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2603 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2604 d0eebce4 2019-03-11 stsp if (err)
2605 d0eebce4 2019-03-11 stsp return err;
2606 d0eebce4 2019-03-11 stsp
2607 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2608 d0eebce4 2019-03-11 stsp char *refstr;
2609 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2610 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2611 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2612 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2613 d0eebce4 2019-03-11 stsp free(refstr);
2614 d0eebce4 2019-03-11 stsp }
2615 d0eebce4 2019-03-11 stsp
2616 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2617 d0eebce4 2019-03-11 stsp return NULL;
2618 d0eebce4 2019-03-11 stsp }
2619 d0eebce4 2019-03-11 stsp
2620 d0eebce4 2019-03-11 stsp static const struct got_error *
2621 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2622 d0eebce4 2019-03-11 stsp {
2623 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2624 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2625 d0eebce4 2019-03-11 stsp
2626 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2627 d0eebce4 2019-03-11 stsp if (err)
2628 d0eebce4 2019-03-11 stsp return err;
2629 d0eebce4 2019-03-11 stsp
2630 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2631 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2632 d0eebce4 2019-03-11 stsp return err;
2633 d0eebce4 2019-03-11 stsp }
2634 d0eebce4 2019-03-11 stsp
2635 d0eebce4 2019-03-11 stsp static const struct got_error *
2636 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2637 d0eebce4 2019-03-11 stsp {
2638 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2639 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2640 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2641 d1644381 2019-07-14 stsp
2642 d1644381 2019-07-14 stsp /*
2643 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2644 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2645 d1644381 2019-07-14 stsp * an unintended typo.
2646 d1644381 2019-07-14 stsp */
2647 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2648 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2649 d0eebce4 2019-03-11 stsp
2650 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2651 dd88155e 2019-06-29 stsp repo);
2652 d83d9d5c 2019-05-13 stsp if (err) {
2653 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2654 d0eebce4 2019-03-11 stsp
2655 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2656 d83d9d5c 2019-05-13 stsp return err;
2657 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2658 d83d9d5c 2019-05-13 stsp if (err)
2659 d83d9d5c 2019-05-13 stsp return err;
2660 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2661 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2662 d83d9d5c 2019-05-13 stsp if (err)
2663 d83d9d5c 2019-05-13 stsp return err;
2664 d83d9d5c 2019-05-13 stsp }
2665 d83d9d5c 2019-05-13 stsp
2666 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2667 d0eebce4 2019-03-11 stsp if (err)
2668 d0eebce4 2019-03-11 stsp goto done;
2669 d0eebce4 2019-03-11 stsp
2670 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2671 d0eebce4 2019-03-11 stsp done:
2672 d0eebce4 2019-03-11 stsp if (ref)
2673 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2674 d0eebce4 2019-03-11 stsp free(id);
2675 d1c1ae5f 2019-08-12 stsp return err;
2676 d1c1ae5f 2019-08-12 stsp }
2677 d1c1ae5f 2019-08-12 stsp
2678 d1c1ae5f 2019-08-12 stsp static const struct got_error *
2679 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
2680 d1c1ae5f 2019-08-12 stsp {
2681 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
2682 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
2683 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
2684 d1c1ae5f 2019-08-12 stsp
2685 d1c1ae5f 2019-08-12 stsp /*
2686 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
2687 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
2688 d1c1ae5f 2019-08-12 stsp * an unintended typo.
2689 d1c1ae5f 2019-08-12 stsp */
2690 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
2691 d1c1ae5f 2019-08-12 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2692 d1c1ae5f 2019-08-12 stsp
2693 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
2694 d1c1ae5f 2019-08-12 stsp if (err)
2695 d1c1ae5f 2019-08-12 stsp return err;
2696 d1c1ae5f 2019-08-12 stsp
2697 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
2698 d1c1ae5f 2019-08-12 stsp if (err)
2699 d1c1ae5f 2019-08-12 stsp goto done;
2700 d1c1ae5f 2019-08-12 stsp
2701 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
2702 d1c1ae5f 2019-08-12 stsp done:
2703 d1c1ae5f 2019-08-12 stsp if (target_ref)
2704 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
2705 d1c1ae5f 2019-08-12 stsp if (ref)
2706 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
2707 d0eebce4 2019-03-11 stsp return err;
2708 d0eebce4 2019-03-11 stsp }
2709 d0eebce4 2019-03-11 stsp
2710 d0eebce4 2019-03-11 stsp static const struct got_error *
2711 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2712 d0eebce4 2019-03-11 stsp {
2713 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2714 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2715 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2716 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2717 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
2718 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2719 d0eebce4 2019-03-11 stsp
2720 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2721 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2722 d0eebce4 2019-03-11 stsp switch (ch) {
2723 d0eebce4 2019-03-11 stsp case 'd':
2724 d0eebce4 2019-03-11 stsp delref = optarg;
2725 d0eebce4 2019-03-11 stsp break;
2726 d0eebce4 2019-03-11 stsp case 'r':
2727 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2728 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2729 d0eebce4 2019-03-11 stsp err(1, "-r option");
2730 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2731 d0eebce4 2019-03-11 stsp break;
2732 d0eebce4 2019-03-11 stsp case 'l':
2733 d0eebce4 2019-03-11 stsp do_list = 1;
2734 d1c1ae5f 2019-08-12 stsp break;
2735 d1c1ae5f 2019-08-12 stsp case 's':
2736 d1c1ae5f 2019-08-12 stsp create_symref = 1;
2737 d0eebce4 2019-03-11 stsp break;
2738 d0eebce4 2019-03-11 stsp default:
2739 d0eebce4 2019-03-11 stsp usage_ref();
2740 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2741 d0eebce4 2019-03-11 stsp }
2742 d0eebce4 2019-03-11 stsp }
2743 d0eebce4 2019-03-11 stsp
2744 d0eebce4 2019-03-11 stsp if (do_list && delref)
2745 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2746 d0eebce4 2019-03-11 stsp
2747 d0eebce4 2019-03-11 stsp argc -= optind;
2748 d0eebce4 2019-03-11 stsp argv += optind;
2749 d0eebce4 2019-03-11 stsp
2750 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2751 d1c1ae5f 2019-08-12 stsp if (create_symref)
2752 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
2753 d1c1ae5f 2019-08-12 stsp "-l or -d options");
2754 d0eebce4 2019-03-11 stsp if (argc > 0)
2755 d0eebce4 2019-03-11 stsp usage_ref();
2756 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2757 d0eebce4 2019-03-11 stsp usage_ref();
2758 d0eebce4 2019-03-11 stsp
2759 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2760 e0b57350 2019-03-12 stsp if (do_list) {
2761 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2762 e0b57350 2019-03-12 stsp NULL) == -1)
2763 e0b57350 2019-03-12 stsp err(1, "pledge");
2764 e0b57350 2019-03-12 stsp } else {
2765 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2766 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2767 e0b57350 2019-03-12 stsp err(1, "pledge");
2768 e0b57350 2019-03-12 stsp }
2769 d0eebce4 2019-03-11 stsp #endif
2770 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2771 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2772 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2773 d0eebce4 2019-03-11 stsp goto done;
2774 d0eebce4 2019-03-11 stsp }
2775 d0eebce4 2019-03-11 stsp
2776 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2777 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2778 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2779 d0eebce4 2019-03-11 stsp goto done;
2780 d0eebce4 2019-03-11 stsp else
2781 d0eebce4 2019-03-11 stsp error = NULL;
2782 d0eebce4 2019-03-11 stsp if (worktree) {
2783 d0eebce4 2019-03-11 stsp repo_path =
2784 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2785 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2786 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2787 d0eebce4 2019-03-11 stsp if (error)
2788 d0eebce4 2019-03-11 stsp goto done;
2789 d0eebce4 2019-03-11 stsp } else {
2790 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2791 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2792 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2793 d0eebce4 2019-03-11 stsp goto done;
2794 d0eebce4 2019-03-11 stsp }
2795 d0eebce4 2019-03-11 stsp }
2796 d0eebce4 2019-03-11 stsp }
2797 d0eebce4 2019-03-11 stsp
2798 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2799 d0eebce4 2019-03-11 stsp if (error != NULL)
2800 d0eebce4 2019-03-11 stsp goto done;
2801 d0eebce4 2019-03-11 stsp
2802 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2803 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2804 c02c541e 2019-03-29 stsp if (error)
2805 c02c541e 2019-03-29 stsp goto done;
2806 c02c541e 2019-03-29 stsp
2807 d0eebce4 2019-03-11 stsp if (do_list)
2808 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2809 d0eebce4 2019-03-11 stsp else if (delref)
2810 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2811 d1c1ae5f 2019-08-12 stsp else if (create_symref)
2812 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
2813 d0eebce4 2019-03-11 stsp else
2814 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2815 4e759de4 2019-06-26 stsp done:
2816 4e759de4 2019-06-26 stsp if (repo)
2817 4e759de4 2019-06-26 stsp got_repo_close(repo);
2818 4e759de4 2019-06-26 stsp if (worktree)
2819 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2820 4e759de4 2019-06-26 stsp free(cwd);
2821 4e759de4 2019-06-26 stsp free(repo_path);
2822 4e759de4 2019-06-26 stsp return error;
2823 4e759de4 2019-06-26 stsp }
2824 4e759de4 2019-06-26 stsp
2825 4e759de4 2019-06-26 stsp __dead static void
2826 4e759de4 2019-06-26 stsp usage_branch(void)
2827 4e759de4 2019-06-26 stsp {
2828 4e759de4 2019-06-26 stsp fprintf(stderr,
2829 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2830 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2831 4e759de4 2019-06-26 stsp exit(1);
2832 4e759de4 2019-06-26 stsp }
2833 4e759de4 2019-06-26 stsp
2834 4e759de4 2019-06-26 stsp static const struct got_error *
2835 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2836 4e759de4 2019-06-26 stsp {
2837 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2838 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2839 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2840 4e759de4 2019-06-26 stsp
2841 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2842 ba882ee3 2019-07-11 stsp
2843 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2844 4e759de4 2019-06-26 stsp if (err)
2845 4e759de4 2019-06-26 stsp return err;
2846 4e759de4 2019-06-26 stsp
2847 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2848 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2849 4e759de4 2019-06-26 stsp char *refstr;
2850 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2851 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2852 4e759de4 2019-06-26 stsp continue;
2853 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2854 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2855 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2856 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2857 ba882ee3 2019-07-11 stsp if (err)
2858 ba882ee3 2019-07-11 stsp return err;
2859 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2860 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2861 ba882ee3 2019-07-11 stsp marker = "* ";
2862 ba882ee3 2019-07-11 stsp else
2863 ba882ee3 2019-07-11 stsp marker = "~ ";
2864 ba882ee3 2019-07-11 stsp free(id);
2865 ba882ee3 2019-07-11 stsp }
2866 4e759de4 2019-06-26 stsp refname += 11;
2867 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2868 4e759de4 2019-06-26 stsp if (refstr == NULL)
2869 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2870 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2871 4e759de4 2019-06-26 stsp free(refstr);
2872 4e759de4 2019-06-26 stsp }
2873 4e759de4 2019-06-26 stsp
2874 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2875 4e759de4 2019-06-26 stsp return NULL;
2876 4e759de4 2019-06-26 stsp }
2877 4e759de4 2019-06-26 stsp
2878 4e759de4 2019-06-26 stsp static const struct got_error *
2879 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2880 4e759de4 2019-06-26 stsp {
2881 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2882 4e759de4 2019-06-26 stsp struct got_reference *ref;
2883 4e759de4 2019-06-26 stsp char *refname;
2884 4e759de4 2019-06-26 stsp
2885 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2886 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2887 4e759de4 2019-06-26 stsp
2888 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2889 4e759de4 2019-06-26 stsp if (err)
2890 4e759de4 2019-06-26 stsp goto done;
2891 4e759de4 2019-06-26 stsp
2892 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2893 4e759de4 2019-06-26 stsp got_ref_close(ref);
2894 4e759de4 2019-06-26 stsp done:
2895 4e759de4 2019-06-26 stsp free(refname);
2896 4e759de4 2019-06-26 stsp return err;
2897 4e759de4 2019-06-26 stsp }
2898 4e759de4 2019-06-26 stsp
2899 4e759de4 2019-06-26 stsp static const struct got_error *
2900 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2901 4e759de4 2019-06-26 stsp const char *base_branch)
2902 4e759de4 2019-06-26 stsp {
2903 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2904 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2905 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2906 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2907 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2908 d3f84d51 2019-07-11 stsp
2909 d3f84d51 2019-07-11 stsp /*
2910 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2911 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2912 d3f84d51 2019-07-11 stsp * an unintended typo.
2913 d3f84d51 2019-07-11 stsp */
2914 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2915 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2916 4e759de4 2019-06-26 stsp
2917 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2918 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2919 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2920 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2921 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2922 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2923 4e759de4 2019-06-26 stsp
2924 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2925 4e759de4 2019-06-26 stsp if (err)
2926 4e759de4 2019-06-26 stsp goto done;
2927 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2928 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2929 4e759de4 2019-06-26 stsp if (err)
2930 4e759de4 2019-06-26 stsp goto done;
2931 4e759de4 2019-06-26 stsp
2932 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2933 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2934 4e759de4 2019-06-26 stsp goto done;
2935 4e759de4 2019-06-26 stsp }
2936 4e759de4 2019-06-26 stsp
2937 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2938 4e759de4 2019-06-26 stsp if (err == NULL) {
2939 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2940 4e759de4 2019-06-26 stsp goto done;
2941 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2942 4e759de4 2019-06-26 stsp goto done;
2943 4e759de4 2019-06-26 stsp
2944 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2945 4e759de4 2019-06-26 stsp if (err)
2946 4e759de4 2019-06-26 stsp goto done;
2947 4e759de4 2019-06-26 stsp
2948 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2949 d0eebce4 2019-03-11 stsp done:
2950 4e759de4 2019-06-26 stsp if (ref)
2951 4e759de4 2019-06-26 stsp got_ref_close(ref);
2952 4e759de4 2019-06-26 stsp free(id);
2953 4e759de4 2019-06-26 stsp free(base_refname);
2954 4e759de4 2019-06-26 stsp free(refname);
2955 4e759de4 2019-06-26 stsp return err;
2956 4e759de4 2019-06-26 stsp }
2957 4e759de4 2019-06-26 stsp
2958 4e759de4 2019-06-26 stsp static const struct got_error *
2959 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2960 4e759de4 2019-06-26 stsp {
2961 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2962 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2963 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2964 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2965 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2966 4e759de4 2019-06-26 stsp const char *delref = NULL;
2967 4e759de4 2019-06-26 stsp
2968 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2969 4e759de4 2019-06-26 stsp switch (ch) {
2970 4e759de4 2019-06-26 stsp case 'd':
2971 4e759de4 2019-06-26 stsp delref = optarg;
2972 4e759de4 2019-06-26 stsp break;
2973 4e759de4 2019-06-26 stsp case 'r':
2974 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2975 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2976 4e759de4 2019-06-26 stsp err(1, "-r option");
2977 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2978 4e759de4 2019-06-26 stsp break;
2979 4e759de4 2019-06-26 stsp case 'l':
2980 4e759de4 2019-06-26 stsp do_list = 1;
2981 4e759de4 2019-06-26 stsp break;
2982 4e759de4 2019-06-26 stsp default:
2983 4e759de4 2019-06-26 stsp usage_branch();
2984 4e759de4 2019-06-26 stsp /* NOTREACHED */
2985 4e759de4 2019-06-26 stsp }
2986 4e759de4 2019-06-26 stsp }
2987 4e759de4 2019-06-26 stsp
2988 4e759de4 2019-06-26 stsp if (do_list && delref)
2989 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2990 4e759de4 2019-06-26 stsp
2991 4e759de4 2019-06-26 stsp argc -= optind;
2992 4e759de4 2019-06-26 stsp argv += optind;
2993 4e759de4 2019-06-26 stsp
2994 4e759de4 2019-06-26 stsp if (do_list || delref) {
2995 4e759de4 2019-06-26 stsp if (argc > 0)
2996 4e759de4 2019-06-26 stsp usage_branch();
2997 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2998 4e759de4 2019-06-26 stsp usage_branch();
2999 4e759de4 2019-06-26 stsp
3000 4e759de4 2019-06-26 stsp #ifndef PROFILE
3001 4e759de4 2019-06-26 stsp if (do_list) {
3002 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3003 4e759de4 2019-06-26 stsp NULL) == -1)
3004 4e759de4 2019-06-26 stsp err(1, "pledge");
3005 4e759de4 2019-06-26 stsp } else {
3006 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3007 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3008 4e759de4 2019-06-26 stsp err(1, "pledge");
3009 4e759de4 2019-06-26 stsp }
3010 4e759de4 2019-06-26 stsp #endif
3011 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3012 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3013 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3014 4e759de4 2019-06-26 stsp goto done;
3015 4e759de4 2019-06-26 stsp }
3016 4e759de4 2019-06-26 stsp
3017 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3018 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3019 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3020 4e759de4 2019-06-26 stsp goto done;
3021 4e759de4 2019-06-26 stsp else
3022 4e759de4 2019-06-26 stsp error = NULL;
3023 4e759de4 2019-06-26 stsp if (worktree) {
3024 4e759de4 2019-06-26 stsp repo_path =
3025 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3026 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3027 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3028 4e759de4 2019-06-26 stsp if (error)
3029 4e759de4 2019-06-26 stsp goto done;
3030 4e759de4 2019-06-26 stsp } else {
3031 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3032 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3033 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3034 4e759de4 2019-06-26 stsp goto done;
3035 4e759de4 2019-06-26 stsp }
3036 4e759de4 2019-06-26 stsp }
3037 4e759de4 2019-06-26 stsp }
3038 4e759de4 2019-06-26 stsp
3039 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
3040 4e759de4 2019-06-26 stsp if (error != NULL)
3041 4e759de4 2019-06-26 stsp goto done;
3042 4e759de4 2019-06-26 stsp
3043 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3044 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3045 4e759de4 2019-06-26 stsp if (error)
3046 4e759de4 2019-06-26 stsp goto done;
3047 4e759de4 2019-06-26 stsp
3048 4e759de4 2019-06-26 stsp if (do_list)
3049 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3050 4e759de4 2019-06-26 stsp else if (delref)
3051 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
3052 4e759de4 2019-06-26 stsp else {
3053 4e759de4 2019-06-26 stsp const char *base_branch;
3054 4e759de4 2019-06-26 stsp if (argc == 1) {
3055 4e759de4 2019-06-26 stsp base_branch = worktree ?
3056 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3057 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3058 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3059 dc5351b4 2019-07-30 stsp base_branch += 11;
3060 4e759de4 2019-06-26 stsp } else
3061 4e759de4 2019-06-26 stsp base_branch = argv[1];
3062 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3063 4e759de4 2019-06-26 stsp }
3064 4e759de4 2019-06-26 stsp done:
3065 d0eebce4 2019-03-11 stsp if (repo)
3066 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3067 d0eebce4 2019-03-11 stsp if (worktree)
3068 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3069 d0eebce4 2019-03-11 stsp free(cwd);
3070 d0eebce4 2019-03-11 stsp free(repo_path);
3071 d00136be 2019-03-26 stsp return error;
3072 d00136be 2019-03-26 stsp }
3073 d00136be 2019-03-26 stsp
3074 d00136be 2019-03-26 stsp __dead static void
3075 d00136be 2019-03-26 stsp usage_add(void)
3076 d00136be 2019-03-26 stsp {
3077 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3078 d00136be 2019-03-26 stsp exit(1);
3079 d00136be 2019-03-26 stsp }
3080 d00136be 2019-03-26 stsp
3081 d00136be 2019-03-26 stsp static const struct got_error *
3082 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
3083 d00136be 2019-03-26 stsp {
3084 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
3085 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
3086 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
3087 1dd54920 2019-05-11 stsp char *cwd = NULL;
3088 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
3089 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
3090 6d022e97 2019-08-04 stsp int ch;
3091 1dd54920 2019-05-11 stsp
3092 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
3093 d00136be 2019-03-26 stsp
3094 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3095 d00136be 2019-03-26 stsp switch (ch) {
3096 d00136be 2019-03-26 stsp default:
3097 d00136be 2019-03-26 stsp usage_add();
3098 d00136be 2019-03-26 stsp /* NOTREACHED */
3099 d00136be 2019-03-26 stsp }
3100 d00136be 2019-03-26 stsp }
3101 d00136be 2019-03-26 stsp
3102 d00136be 2019-03-26 stsp argc -= optind;
3103 d00136be 2019-03-26 stsp argv += optind;
3104 d00136be 2019-03-26 stsp
3105 43012d58 2019-07-14 stsp #ifndef PROFILE
3106 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3107 43012d58 2019-07-14 stsp NULL) == -1)
3108 43012d58 2019-07-14 stsp err(1, "pledge");
3109 43012d58 2019-07-14 stsp #endif
3110 723c305c 2019-05-11 jcs if (argc < 1)
3111 d00136be 2019-03-26 stsp usage_add();
3112 d00136be 2019-03-26 stsp
3113 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
3114 d00136be 2019-03-26 stsp if (cwd == NULL) {
3115 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3116 d00136be 2019-03-26 stsp goto done;
3117 d00136be 2019-03-26 stsp }
3118 723c305c 2019-05-11 jcs
3119 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3120 d00136be 2019-03-26 stsp if (error)
3121 d00136be 2019-03-26 stsp goto done;
3122 d00136be 2019-03-26 stsp
3123 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3124 031a5338 2019-03-26 stsp if (error != NULL)
3125 031a5338 2019-03-26 stsp goto done;
3126 031a5338 2019-03-26 stsp
3127 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3128 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3129 d00136be 2019-03-26 stsp if (error)
3130 d00136be 2019-03-26 stsp goto done;
3131 d00136be 2019-03-26 stsp
3132 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3133 6d022e97 2019-08-04 stsp if (error)
3134 6d022e97 2019-08-04 stsp goto done;
3135 723c305c 2019-05-11 jcs
3136 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
3137 1dd54920 2019-05-11 stsp NULL, repo);
3138 d00136be 2019-03-26 stsp done:
3139 031a5338 2019-03-26 stsp if (repo)
3140 031a5338 2019-03-26 stsp got_repo_close(repo);
3141 d00136be 2019-03-26 stsp if (worktree)
3142 d00136be 2019-03-26 stsp got_worktree_close(worktree);
3143 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
3144 1dd54920 2019-05-11 stsp free((char *)pe->path);
3145 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
3146 2ec1f75b 2019-03-26 stsp free(cwd);
3147 2ec1f75b 2019-03-26 stsp return error;
3148 2ec1f75b 2019-03-26 stsp }
3149 2ec1f75b 2019-03-26 stsp
3150 2ec1f75b 2019-03-26 stsp __dead static void
3151 648e4ef7 2019-07-09 stsp usage_remove(void)
3152 2ec1f75b 2019-03-26 stsp {
3153 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3154 2ec1f75b 2019-03-26 stsp exit(1);
3155 2ec1f75b 2019-03-26 stsp }
3156 2ec1f75b 2019-03-26 stsp
3157 2ec1f75b 2019-03-26 stsp static const struct got_error *
3158 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
3159 2ec1f75b 2019-03-26 stsp {
3160 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
3161 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
3162 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
3163 17ed4618 2019-06-02 stsp char *cwd = NULL;
3164 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
3165 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
3166 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
3167 2ec1f75b 2019-03-26 stsp
3168 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
3169 17ed4618 2019-06-02 stsp
3170 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
3171 2ec1f75b 2019-03-26 stsp switch (ch) {
3172 2ec1f75b 2019-03-26 stsp case 'f':
3173 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
3174 2ec1f75b 2019-03-26 stsp break;
3175 2ec1f75b 2019-03-26 stsp default:
3176 2ec1f75b 2019-03-26 stsp usage_add();
3177 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
3178 2ec1f75b 2019-03-26 stsp }
3179 2ec1f75b 2019-03-26 stsp }
3180 2ec1f75b 2019-03-26 stsp
3181 2ec1f75b 2019-03-26 stsp argc -= optind;
3182 2ec1f75b 2019-03-26 stsp argv += optind;
3183 2ec1f75b 2019-03-26 stsp
3184 43012d58 2019-07-14 stsp #ifndef PROFILE
3185 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3186 43012d58 2019-07-14 stsp NULL) == -1)
3187 43012d58 2019-07-14 stsp err(1, "pledge");
3188 43012d58 2019-07-14 stsp #endif
3189 17ed4618 2019-06-02 stsp if (argc < 1)
3190 648e4ef7 2019-07-09 stsp usage_remove();
3191 2ec1f75b 2019-03-26 stsp
3192 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3193 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3194 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3195 2ec1f75b 2019-03-26 stsp goto done;
3196 2ec1f75b 2019-03-26 stsp }
3197 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3198 2ec1f75b 2019-03-26 stsp if (error)
3199 2ec1f75b 2019-03-26 stsp goto done;
3200 2ec1f75b 2019-03-26 stsp
3201 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3202 2af4a041 2019-05-11 jcs if (error)
3203 2ec1f75b 2019-03-26 stsp goto done;
3204 2ec1f75b 2019-03-26 stsp
3205 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3206 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3207 2ec1f75b 2019-03-26 stsp if (error)
3208 2ec1f75b 2019-03-26 stsp goto done;
3209 2ec1f75b 2019-03-26 stsp
3210 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3211 6d022e97 2019-08-04 stsp if (error)
3212 6d022e97 2019-08-04 stsp goto done;
3213 17ed4618 2019-06-02 stsp
3214 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3215 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3216 a129376b 2019-03-28 stsp if (error)
3217 a129376b 2019-03-28 stsp goto done;
3218 a129376b 2019-03-28 stsp done:
3219 a129376b 2019-03-28 stsp if (repo)
3220 a129376b 2019-03-28 stsp got_repo_close(repo);
3221 a129376b 2019-03-28 stsp if (worktree)
3222 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3223 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3224 17ed4618 2019-06-02 stsp free((char *)pe->path);
3225 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3226 a129376b 2019-03-28 stsp free(cwd);
3227 a129376b 2019-03-28 stsp return error;
3228 a129376b 2019-03-28 stsp }
3229 a129376b 2019-03-28 stsp
3230 a129376b 2019-03-28 stsp __dead static void
3231 a129376b 2019-03-28 stsp usage_revert(void)
3232 a129376b 2019-03-28 stsp {
3233 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3234 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
3235 a129376b 2019-03-28 stsp exit(1);
3236 a129376b 2019-03-28 stsp }
3237 a129376b 2019-03-28 stsp
3238 1ee397ad 2019-07-12 stsp static const struct got_error *
3239 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3240 a129376b 2019-03-28 stsp {
3241 a129376b 2019-03-28 stsp while (path[0] == '/')
3242 a129376b 2019-03-28 stsp path++;
3243 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3244 33aa809d 2019-08-08 stsp return NULL;
3245 33aa809d 2019-08-08 stsp }
3246 33aa809d 2019-08-08 stsp
3247 33aa809d 2019-08-08 stsp struct choose_patch_arg {
3248 33aa809d 2019-08-08 stsp FILE *patch_script_file;
3249 33aa809d 2019-08-08 stsp const char *action;
3250 33aa809d 2019-08-08 stsp };
3251 33aa809d 2019-08-08 stsp
3252 33aa809d 2019-08-08 stsp static const struct got_error *
3253 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3254 33aa809d 2019-08-08 stsp int nchanges, const char *action)
3255 33aa809d 2019-08-08 stsp {
3256 33aa809d 2019-08-08 stsp char *line = NULL;
3257 33aa809d 2019-08-08 stsp size_t linesize = 0;
3258 33aa809d 2019-08-08 stsp ssize_t linelen;
3259 33aa809d 2019-08-08 stsp
3260 33aa809d 2019-08-08 stsp switch (status) {
3261 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
3262 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
3263 33aa809d 2019-08-08 stsp break;
3264 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
3265 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
3266 33aa809d 2019-08-08 stsp break;
3267 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
3268 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
3269 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
3270 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3271 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3272 33aa809d 2019-08-08 stsp printf("%s", line);
3273 33aa809d 2019-08-08 stsp if (ferror(patch_file))
3274 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3275 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
3276 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3277 33aa809d 2019-08-08 stsp path, n, nchanges, action);
3278 33aa809d 2019-08-08 stsp break;
3279 33aa809d 2019-08-08 stsp default:
3280 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
3281 33aa809d 2019-08-08 stsp }
3282 33aa809d 2019-08-08 stsp
3283 33aa809d 2019-08-08 stsp return NULL;
3284 33aa809d 2019-08-08 stsp }
3285 33aa809d 2019-08-08 stsp
3286 33aa809d 2019-08-08 stsp static const struct got_error *
3287 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3288 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
3289 33aa809d 2019-08-08 stsp {
3290 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
3291 33aa809d 2019-08-08 stsp char *line = NULL;
3292 33aa809d 2019-08-08 stsp size_t linesize = 0;
3293 33aa809d 2019-08-08 stsp ssize_t linelen;
3294 33aa809d 2019-08-08 stsp int resp = ' ';
3295 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
3296 33aa809d 2019-08-08 stsp
3297 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
3298 33aa809d 2019-08-08 stsp
3299 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
3300 33aa809d 2019-08-08 stsp char *nl;
3301 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3302 33aa809d 2019-08-08 stsp a->action);
3303 33aa809d 2019-08-08 stsp if (err)
3304 33aa809d 2019-08-08 stsp return err;
3305 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
3306 33aa809d 2019-08-08 stsp if (linelen == -1) {
3307 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
3308 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
3309 33aa809d 2019-08-08 stsp return NULL;
3310 33aa809d 2019-08-08 stsp }
3311 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
3312 33aa809d 2019-08-08 stsp if (nl)
3313 33aa809d 2019-08-08 stsp *nl = '\0';
3314 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
3315 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3316 33aa809d 2019-08-08 stsp printf("y\n");
3317 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
3318 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3319 33aa809d 2019-08-08 stsp printf("n\n");
3320 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
3321 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
3322 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3323 33aa809d 2019-08-08 stsp printf("q\n");
3324 33aa809d 2019-08-08 stsp } else
3325 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
3326 33aa809d 2019-08-08 stsp free(line);
3327 33aa809d 2019-08-08 stsp return NULL;
3328 33aa809d 2019-08-08 stsp }
3329 33aa809d 2019-08-08 stsp
3330 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
3331 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
3332 33aa809d 2019-08-08 stsp a->action);
3333 33aa809d 2019-08-08 stsp if (err)
3334 33aa809d 2019-08-08 stsp return err;
3335 33aa809d 2019-08-08 stsp resp = getchar();
3336 33aa809d 2019-08-08 stsp if (resp == '\n')
3337 33aa809d 2019-08-08 stsp resp = getchar();
3338 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
3339 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
3340 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3341 33aa809d 2019-08-08 stsp resp = ' ';
3342 33aa809d 2019-08-08 stsp }
3343 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
3344 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
3345 33aa809d 2019-08-08 stsp resp = ' ';
3346 33aa809d 2019-08-08 stsp }
3347 33aa809d 2019-08-08 stsp }
3348 33aa809d 2019-08-08 stsp
3349 33aa809d 2019-08-08 stsp if (resp == 'y')
3350 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
3351 33aa809d 2019-08-08 stsp else if (resp == 'n')
3352 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
3353 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3354 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
3355 33aa809d 2019-08-08 stsp
3356 1ee397ad 2019-07-12 stsp return NULL;
3357 a129376b 2019-03-28 stsp }
3358 a129376b 2019-03-28 stsp
3359 33aa809d 2019-08-08 stsp
3360 a129376b 2019-03-28 stsp static const struct got_error *
3361 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3362 a129376b 2019-03-28 stsp {
3363 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3364 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3365 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3366 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3367 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3368 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
3369 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
3370 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
3371 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
3372 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
3373 a129376b 2019-03-28 stsp
3374 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3375 e20a8b6f 2019-06-04 stsp
3376 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3377 a129376b 2019-03-28 stsp switch (ch) {
3378 33aa809d 2019-08-08 stsp case 'p':
3379 33aa809d 2019-08-08 stsp pflag = 1;
3380 33aa809d 2019-08-08 stsp break;
3381 33aa809d 2019-08-08 stsp case 'F':
3382 33aa809d 2019-08-08 stsp patch_script_path = optarg;
3383 33aa809d 2019-08-08 stsp break;
3384 0f6d7415 2019-08-08 stsp case 'R':
3385 0f6d7415 2019-08-08 stsp can_recurse = 1;
3386 0f6d7415 2019-08-08 stsp break;
3387 a129376b 2019-03-28 stsp default:
3388 a129376b 2019-03-28 stsp usage_revert();
3389 a129376b 2019-03-28 stsp /* NOTREACHED */
3390 a129376b 2019-03-28 stsp }
3391 a129376b 2019-03-28 stsp }
3392 a129376b 2019-03-28 stsp
3393 a129376b 2019-03-28 stsp argc -= optind;
3394 a129376b 2019-03-28 stsp argv += optind;
3395 a129376b 2019-03-28 stsp
3396 43012d58 2019-07-14 stsp #ifndef PROFILE
3397 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3398 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3399 43012d58 2019-07-14 stsp err(1, "pledge");
3400 43012d58 2019-07-14 stsp #endif
3401 e20a8b6f 2019-06-04 stsp if (argc < 1)
3402 a129376b 2019-03-28 stsp usage_revert();
3403 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
3404 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
3405 a129376b 2019-03-28 stsp
3406 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3407 a129376b 2019-03-28 stsp if (cwd == NULL) {
3408 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3409 a129376b 2019-03-28 stsp goto done;
3410 a129376b 2019-03-28 stsp }
3411 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3412 2ec1f75b 2019-03-26 stsp if (error)
3413 2ec1f75b 2019-03-26 stsp goto done;
3414 a129376b 2019-03-28 stsp
3415 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3416 a129376b 2019-03-28 stsp if (error != NULL)
3417 a129376b 2019-03-28 stsp goto done;
3418 a129376b 2019-03-28 stsp
3419 33aa809d 2019-08-08 stsp if (patch_script_path) {
3420 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
3421 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
3422 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
3423 33aa809d 2019-08-08 stsp patch_script_path);
3424 33aa809d 2019-08-08 stsp goto done;
3425 33aa809d 2019-08-08 stsp }
3426 33aa809d 2019-08-08 stsp }
3427 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3428 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3429 a129376b 2019-03-28 stsp if (error)
3430 a129376b 2019-03-28 stsp goto done;
3431 a129376b 2019-03-28 stsp
3432 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3433 6d022e97 2019-08-04 stsp if (error)
3434 6d022e97 2019-08-04 stsp goto done;
3435 00db391e 2019-08-03 stsp
3436 0f6d7415 2019-08-08 stsp if (!can_recurse) {
3437 0f6d7415 2019-08-08 stsp char *ondisk_path;
3438 0f6d7415 2019-08-08 stsp struct stat sb;
3439 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
3440 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
3441 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
3442 0f6d7415 2019-08-08 stsp pe->path) == -1) {
3443 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
3444 0f6d7415 2019-08-08 stsp goto done;
3445 0f6d7415 2019-08-08 stsp }
3446 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
3447 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
3448 0f6d7415 2019-08-08 stsp free(ondisk_path);
3449 0f6d7415 2019-08-08 stsp continue;
3450 0f6d7415 2019-08-08 stsp }
3451 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
3452 0f6d7415 2019-08-08 stsp ondisk_path);
3453 0f6d7415 2019-08-08 stsp free(ondisk_path);
3454 0f6d7415 2019-08-08 stsp goto done;
3455 0f6d7415 2019-08-08 stsp }
3456 0f6d7415 2019-08-08 stsp free(ondisk_path);
3457 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
3458 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3459 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
3460 0f6d7415 2019-08-08 stsp goto done;
3461 0f6d7415 2019-08-08 stsp }
3462 0f6d7415 2019-08-08 stsp }
3463 0f6d7415 2019-08-08 stsp }
3464 0f6d7415 2019-08-08 stsp
3465 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
3466 33aa809d 2019-08-08 stsp cpa.action = "revert";
3467 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3468 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
3469 a129376b 2019-03-28 stsp if (error)
3470 a129376b 2019-03-28 stsp goto done;
3471 2ec1f75b 2019-03-26 stsp done:
3472 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
3473 33aa809d 2019-08-08 stsp error == NULL)
3474 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
3475 2ec1f75b 2019-03-26 stsp if (repo)
3476 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3477 2ec1f75b 2019-03-26 stsp if (worktree)
3478 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3479 2ec1f75b 2019-03-26 stsp free(path);
3480 d00136be 2019-03-26 stsp free(cwd);
3481 6bad629b 2019-02-04 stsp return error;
3482 c4296144 2019-05-09 stsp }
3483 c4296144 2019-05-09 stsp
3484 c4296144 2019-05-09 stsp __dead static void
3485 c4296144 2019-05-09 stsp usage_commit(void)
3486 c4296144 2019-05-09 stsp {
3487 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3488 5c1e53bc 2019-07-28 stsp getprogname());
3489 c4296144 2019-05-09 stsp exit(1);
3490 33ad4cbe 2019-05-12 jcs }
3491 33ad4cbe 2019-05-12 jcs
3492 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3493 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3494 e2ba3d07 2019-05-13 stsp const char *editor;
3495 e0870e44 2019-05-13 stsp const char *worktree_path;
3496 76d98825 2019-06-03 stsp const char *branch_name;
3497 314a6357 2019-05-13 stsp const char *repo_path;
3498 e0870e44 2019-05-13 stsp char *logmsg_path;
3499 e2ba3d07 2019-05-13 stsp
3500 e2ba3d07 2019-05-13 stsp };
3501 e0870e44 2019-05-13 stsp
3502 33ad4cbe 2019-05-12 jcs static const struct got_error *
3503 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3504 33ad4cbe 2019-05-12 jcs void *arg)
3505 33ad4cbe 2019-05-12 jcs {
3506 76d98825 2019-06-03 stsp char *initial_content = NULL;
3507 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3508 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3509 e0870e44 2019-05-13 stsp char *template = NULL;
3510 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3511 3ce1b845 2019-07-15 stsp int fd;
3512 33ad4cbe 2019-05-12 jcs size_t len;
3513 33ad4cbe 2019-05-12 jcs
3514 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3515 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3516 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3517 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3518 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3519 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3520 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3521 33ad4cbe 2019-05-12 jcs return NULL;
3522 33ad4cbe 2019-05-12 jcs }
3523 33ad4cbe 2019-05-12 jcs
3524 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3525 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3526 76d98825 2019-06-03 stsp
3527 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3528 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3529 76d98825 2019-06-03 stsp a->branch_name) == -1)
3530 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3531 e0870e44 2019-05-13 stsp
3532 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3533 793c30b5 2019-05-13 stsp if (err)
3534 e0870e44 2019-05-13 stsp goto done;
3535 33ad4cbe 2019-05-12 jcs
3536 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3537 33ad4cbe 2019-05-12 jcs
3538 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3539 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3540 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3541 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3542 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3543 33ad4cbe 2019-05-12 jcs }
3544 33ad4cbe 2019-05-12 jcs close(fd);
3545 33ad4cbe 2019-05-12 jcs
3546 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3547 33ad4cbe 2019-05-12 jcs done:
3548 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3549 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
3550 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
3551 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
3552 e2af0fd1 2019-08-08 stsp }
3553 76d98825 2019-06-03 stsp free(initial_content);
3554 e0870e44 2019-05-13 stsp free(template);
3555 314a6357 2019-05-13 stsp
3556 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3557 3ce1b845 2019-07-15 stsp if (err == NULL) {
3558 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3559 3ce1b845 2019-07-15 stsp if (err) {
3560 3ce1b845 2019-07-15 stsp free(*logmsg);
3561 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3562 3ce1b845 2019-07-15 stsp }
3563 3ce1b845 2019-07-15 stsp }
3564 33ad4cbe 2019-05-12 jcs return err;
3565 6bad629b 2019-02-04 stsp }
3566 c4296144 2019-05-09 stsp
3567 c4296144 2019-05-09 stsp static const struct got_error *
3568 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3569 c4296144 2019-05-09 stsp {
3570 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3571 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3572 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3573 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3574 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3575 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3576 84792843 2019-08-09 stsp const char *author;
3577 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3578 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3579 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3580 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3581 5c1e53bc 2019-07-28 stsp
3582 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3583 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
3584 c4296144 2019-05-09 stsp
3585 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3586 c4296144 2019-05-09 stsp switch (ch) {
3587 c4296144 2019-05-09 stsp case 'm':
3588 c4296144 2019-05-09 stsp logmsg = optarg;
3589 c4296144 2019-05-09 stsp break;
3590 c4296144 2019-05-09 stsp default:
3591 c4296144 2019-05-09 stsp usage_commit();
3592 c4296144 2019-05-09 stsp /* NOTREACHED */
3593 c4296144 2019-05-09 stsp }
3594 c4296144 2019-05-09 stsp }
3595 c4296144 2019-05-09 stsp
3596 c4296144 2019-05-09 stsp argc -= optind;
3597 c4296144 2019-05-09 stsp argv += optind;
3598 c4296144 2019-05-09 stsp
3599 43012d58 2019-07-14 stsp #ifndef PROFILE
3600 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3601 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3602 43012d58 2019-07-14 stsp err(1, "pledge");
3603 43012d58 2019-07-14 stsp #endif
3604 84792843 2019-08-09 stsp error = get_author(&author);
3605 84792843 2019-08-09 stsp if (error)
3606 84792843 2019-08-09 stsp return error;
3607 c4296144 2019-05-09 stsp
3608 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3609 c4296144 2019-05-09 stsp if (cwd == NULL) {
3610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3611 c4296144 2019-05-09 stsp goto done;
3612 c4296144 2019-05-09 stsp }
3613 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3614 7d5807f4 2019-07-11 stsp if (error)
3615 7d5807f4 2019-07-11 stsp goto done;
3616 7d5807f4 2019-07-11 stsp
3617 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3618 c4296144 2019-05-09 stsp if (error)
3619 a698f62e 2019-07-25 stsp goto done;
3620 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3621 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3622 c4296144 2019-05-09 stsp goto done;
3623 a698f62e 2019-07-25 stsp }
3624 5c1e53bc 2019-07-28 stsp
3625 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3626 916f288c 2019-07-30 stsp worktree);
3627 5c1e53bc 2019-07-28 stsp if (error)
3628 5c1e53bc 2019-07-28 stsp goto done;
3629 c4296144 2019-05-09 stsp
3630 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3631 c4296144 2019-05-09 stsp if (error != NULL)
3632 c4296144 2019-05-09 stsp goto done;
3633 c4296144 2019-05-09 stsp
3634 314a6357 2019-05-13 stsp /*
3635 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3636 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3637 314a6357 2019-05-13 stsp */
3638 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3639 314a6357 2019-05-13 stsp error = get_editor(&editor);
3640 314a6357 2019-05-13 stsp else
3641 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3642 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3643 c4296144 2019-05-09 stsp if (error)
3644 c4296144 2019-05-09 stsp goto done;
3645 c4296144 2019-05-09 stsp
3646 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3647 087fb88c 2019-08-04 stsp if (error)
3648 087fb88c 2019-08-04 stsp goto done;
3649 087fb88c 2019-08-04 stsp
3650 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3651 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3652 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3653 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3654 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3655 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3656 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3657 916f288c 2019-07-30 stsp goto done;
3658 916f288c 2019-07-30 stsp }
3659 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3660 916f288c 2019-07-30 stsp }
3661 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3662 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3663 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3664 e0870e44 2019-05-13 stsp if (error) {
3665 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3666 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3667 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3668 c4296144 2019-05-09 stsp goto done;
3669 e0870e44 2019-05-13 stsp }
3670 c4296144 2019-05-09 stsp
3671 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3672 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3673 e0870e44 2019-05-13 stsp
3674 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3675 c4296144 2019-05-09 stsp if (error)
3676 c4296144 2019-05-09 stsp goto done;
3677 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3678 c4296144 2019-05-09 stsp done:
3679 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
3680 c4296144 2019-05-09 stsp if (repo)
3681 c4296144 2019-05-09 stsp got_repo_close(repo);
3682 c4296144 2019-05-09 stsp if (worktree)
3683 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3684 c4296144 2019-05-09 stsp free(cwd);
3685 c4296144 2019-05-09 stsp free(id_str);
3686 e2ba3d07 2019-05-13 stsp free(editor);
3687 234035bc 2019-06-01 stsp return error;
3688 234035bc 2019-06-01 stsp }
3689 234035bc 2019-06-01 stsp
3690 234035bc 2019-06-01 stsp __dead static void
3691 234035bc 2019-06-01 stsp usage_cherrypick(void)
3692 234035bc 2019-06-01 stsp {
3693 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3694 234035bc 2019-06-01 stsp exit(1);
3695 234035bc 2019-06-01 stsp }
3696 234035bc 2019-06-01 stsp
3697 234035bc 2019-06-01 stsp static const struct got_error *
3698 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3699 234035bc 2019-06-01 stsp {
3700 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3701 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3702 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3703 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3704 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3705 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3706 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3707 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3708 234035bc 2019-06-01 stsp int ch, did_something = 0;
3709 234035bc 2019-06-01 stsp
3710 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3711 234035bc 2019-06-01 stsp switch (ch) {
3712 234035bc 2019-06-01 stsp default:
3713 234035bc 2019-06-01 stsp usage_cherrypick();
3714 234035bc 2019-06-01 stsp /* NOTREACHED */
3715 234035bc 2019-06-01 stsp }
3716 234035bc 2019-06-01 stsp }
3717 234035bc 2019-06-01 stsp
3718 234035bc 2019-06-01 stsp argc -= optind;
3719 234035bc 2019-06-01 stsp argv += optind;
3720 234035bc 2019-06-01 stsp
3721 43012d58 2019-07-14 stsp #ifndef PROFILE
3722 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3723 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3724 43012d58 2019-07-14 stsp err(1, "pledge");
3725 43012d58 2019-07-14 stsp #endif
3726 234035bc 2019-06-01 stsp if (argc != 1)
3727 234035bc 2019-06-01 stsp usage_cherrypick();
3728 234035bc 2019-06-01 stsp
3729 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3730 234035bc 2019-06-01 stsp if (cwd == NULL) {
3731 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3732 234035bc 2019-06-01 stsp goto done;
3733 234035bc 2019-06-01 stsp }
3734 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3735 234035bc 2019-06-01 stsp if (error)
3736 234035bc 2019-06-01 stsp goto done;
3737 234035bc 2019-06-01 stsp
3738 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3739 234035bc 2019-06-01 stsp if (error != NULL)
3740 234035bc 2019-06-01 stsp goto done;
3741 234035bc 2019-06-01 stsp
3742 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3743 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3744 234035bc 2019-06-01 stsp if (error)
3745 234035bc 2019-06-01 stsp goto done;
3746 234035bc 2019-06-01 stsp
3747 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3748 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3749 234035bc 2019-06-01 stsp if (error != NULL) {
3750 234035bc 2019-06-01 stsp struct got_reference *ref;
3751 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3752 234035bc 2019-06-01 stsp goto done;
3753 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3754 234035bc 2019-06-01 stsp if (error != NULL)
3755 234035bc 2019-06-01 stsp goto done;
3756 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3757 234035bc 2019-06-01 stsp got_ref_close(ref);
3758 234035bc 2019-06-01 stsp if (error != NULL)
3759 234035bc 2019-06-01 stsp goto done;
3760 234035bc 2019-06-01 stsp }
3761 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3762 234035bc 2019-06-01 stsp if (error)
3763 234035bc 2019-06-01 stsp goto done;
3764 234035bc 2019-06-01 stsp
3765 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3766 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3767 234035bc 2019-06-01 stsp if (error != NULL)
3768 234035bc 2019-06-01 stsp goto done;
3769 234035bc 2019-06-01 stsp
3770 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3771 234035bc 2019-06-01 stsp if (error) {
3772 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3773 234035bc 2019-06-01 stsp goto done;
3774 234035bc 2019-06-01 stsp error = NULL;
3775 234035bc 2019-06-01 stsp } else {
3776 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3777 234035bc 2019-06-01 stsp goto done;
3778 234035bc 2019-06-01 stsp }
3779 234035bc 2019-06-01 stsp
3780 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3781 234035bc 2019-06-01 stsp if (error)
3782 234035bc 2019-06-01 stsp goto done;
3783 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3784 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3785 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3786 03415a1a 2019-06-02 stsp NULL);
3787 234035bc 2019-06-01 stsp if (error != NULL)
3788 234035bc 2019-06-01 stsp goto done;
3789 234035bc 2019-06-01 stsp
3790 234035bc 2019-06-01 stsp if (did_something)
3791 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3792 234035bc 2019-06-01 stsp done:
3793 234035bc 2019-06-01 stsp if (commit)
3794 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3795 234035bc 2019-06-01 stsp free(commit_id_str);
3796 234035bc 2019-06-01 stsp if (head_ref)
3797 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3798 234035bc 2019-06-01 stsp if (worktree)
3799 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3800 234035bc 2019-06-01 stsp if (repo)
3801 234035bc 2019-06-01 stsp got_repo_close(repo);
3802 c4296144 2019-05-09 stsp return error;
3803 c4296144 2019-05-09 stsp }
3804 5ef14e63 2019-06-02 stsp
3805 5ef14e63 2019-06-02 stsp __dead static void
3806 5ef14e63 2019-06-02 stsp usage_backout(void)
3807 5ef14e63 2019-06-02 stsp {
3808 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3809 5ef14e63 2019-06-02 stsp exit(1);
3810 5ef14e63 2019-06-02 stsp }
3811 5ef14e63 2019-06-02 stsp
3812 5ef14e63 2019-06-02 stsp static const struct got_error *
3813 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3814 5ef14e63 2019-06-02 stsp {
3815 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3816 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3817 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3818 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3819 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3820 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3821 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3822 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3823 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3824 5ef14e63 2019-06-02 stsp
3825 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3826 5ef14e63 2019-06-02 stsp switch (ch) {
3827 5ef14e63 2019-06-02 stsp default:
3828 5ef14e63 2019-06-02 stsp usage_backout();
3829 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3830 5ef14e63 2019-06-02 stsp }
3831 5ef14e63 2019-06-02 stsp }
3832 5ef14e63 2019-06-02 stsp
3833 5ef14e63 2019-06-02 stsp argc -= optind;
3834 5ef14e63 2019-06-02 stsp argv += optind;
3835 5ef14e63 2019-06-02 stsp
3836 43012d58 2019-07-14 stsp #ifndef PROFILE
3837 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3838 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3839 43012d58 2019-07-14 stsp err(1, "pledge");
3840 43012d58 2019-07-14 stsp #endif
3841 5ef14e63 2019-06-02 stsp if (argc != 1)
3842 5ef14e63 2019-06-02 stsp usage_backout();
3843 5ef14e63 2019-06-02 stsp
3844 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3845 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3846 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3847 5ef14e63 2019-06-02 stsp goto done;
3848 5ef14e63 2019-06-02 stsp }
3849 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3850 5ef14e63 2019-06-02 stsp if (error)
3851 5ef14e63 2019-06-02 stsp goto done;
3852 5ef14e63 2019-06-02 stsp
3853 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3854 5ef14e63 2019-06-02 stsp if (error != NULL)
3855 5ef14e63 2019-06-02 stsp goto done;
3856 5ef14e63 2019-06-02 stsp
3857 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3858 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3859 5ef14e63 2019-06-02 stsp if (error)
3860 5ef14e63 2019-06-02 stsp goto done;
3861 5ef14e63 2019-06-02 stsp
3862 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3863 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3864 5ef14e63 2019-06-02 stsp if (error != NULL) {
3865 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3866 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3867 5ef14e63 2019-06-02 stsp goto done;
3868 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3869 5ef14e63 2019-06-02 stsp if (error != NULL)
3870 5ef14e63 2019-06-02 stsp goto done;
3871 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3872 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3873 5ef14e63 2019-06-02 stsp if (error != NULL)
3874 5ef14e63 2019-06-02 stsp goto done;
3875 5ef14e63 2019-06-02 stsp }
3876 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3877 5ef14e63 2019-06-02 stsp if (error)
3878 5ef14e63 2019-06-02 stsp goto done;
3879 5ef14e63 2019-06-02 stsp
3880 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3881 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3882 5ef14e63 2019-06-02 stsp if (error != NULL)
3883 5ef14e63 2019-06-02 stsp goto done;
3884 5ef14e63 2019-06-02 stsp
3885 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3886 5ef14e63 2019-06-02 stsp if (error)
3887 5ef14e63 2019-06-02 stsp goto done;
3888 5ef14e63 2019-06-02 stsp
3889 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3890 5ef14e63 2019-06-02 stsp if (error)
3891 5ef14e63 2019-06-02 stsp goto done;
3892 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3893 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3894 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3895 5ef14e63 2019-06-02 stsp goto done;
3896 5ef14e63 2019-06-02 stsp }
3897 5ef14e63 2019-06-02 stsp
3898 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3899 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3900 5ef14e63 2019-06-02 stsp if (error != NULL)
3901 5ef14e63 2019-06-02 stsp goto done;
3902 5ef14e63 2019-06-02 stsp
3903 5ef14e63 2019-06-02 stsp if (did_something)
3904 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3905 5ef14e63 2019-06-02 stsp done:
3906 5ef14e63 2019-06-02 stsp if (commit)
3907 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3908 5ef14e63 2019-06-02 stsp free(commit_id_str);
3909 5ef14e63 2019-06-02 stsp if (head_ref)
3910 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3911 818c7501 2019-07-11 stsp if (worktree)
3912 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3913 818c7501 2019-07-11 stsp if (repo)
3914 818c7501 2019-07-11 stsp got_repo_close(repo);
3915 818c7501 2019-07-11 stsp return error;
3916 818c7501 2019-07-11 stsp }
3917 818c7501 2019-07-11 stsp
3918 818c7501 2019-07-11 stsp __dead static void
3919 818c7501 2019-07-11 stsp usage_rebase(void)
3920 818c7501 2019-07-11 stsp {
3921 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3922 af54c8f8 2019-07-11 stsp getprogname());
3923 818c7501 2019-07-11 stsp exit(1);
3924 0ebf8283 2019-07-24 stsp }
3925 0ebf8283 2019-07-24 stsp
3926 0ebf8283 2019-07-24 stsp void
3927 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3928 0ebf8283 2019-07-24 stsp {
3929 0ebf8283 2019-07-24 stsp char *nl;
3930 0ebf8283 2019-07-24 stsp size_t len;
3931 0ebf8283 2019-07-24 stsp
3932 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3933 0ebf8283 2019-07-24 stsp if (len > limit)
3934 0ebf8283 2019-07-24 stsp len = limit;
3935 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3936 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3937 0ebf8283 2019-07-24 stsp if (nl)
3938 0ebf8283 2019-07-24 stsp *nl = '\0';
3939 0ebf8283 2019-07-24 stsp }
3940 0ebf8283 2019-07-24 stsp
3941 0ebf8283 2019-07-24 stsp static const struct got_error *
3942 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3943 0ebf8283 2019-07-24 stsp {
3944 5943eee2 2019-08-13 stsp const struct got_error *err;
3945 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
3946 5943eee2 2019-08-13 stsp const char *s;
3947 0ebf8283 2019-07-24 stsp
3948 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3949 5943eee2 2019-08-13 stsp if (err)
3950 5943eee2 2019-08-13 stsp return err;
3951 0ebf8283 2019-07-24 stsp
3952 5943eee2 2019-08-13 stsp s = logmsg0;
3953 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
3954 5943eee2 2019-08-13 stsp s++;
3955 5943eee2 2019-08-13 stsp
3956 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
3957 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
3958 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
3959 5943eee2 2019-08-13 stsp goto done;
3960 5943eee2 2019-08-13 stsp }
3961 0ebf8283 2019-07-24 stsp
3962 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3963 5943eee2 2019-08-13 stsp done:
3964 5943eee2 2019-08-13 stsp free(logmsg0);
3965 5943eee2 2019-08-13 stsp return err;
3966 818c7501 2019-07-11 stsp }
3967 818c7501 2019-07-11 stsp
3968 818c7501 2019-07-11 stsp static const struct got_error *
3969 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3970 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3971 818c7501 2019-07-11 stsp {
3972 818c7501 2019-07-11 stsp const struct got_error *err;
3973 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3974 818c7501 2019-07-11 stsp
3975 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3976 818c7501 2019-07-11 stsp if (err)
3977 818c7501 2019-07-11 stsp goto done;
3978 818c7501 2019-07-11 stsp
3979 ff0d2220 2019-07-11 stsp if (new_id) {
3980 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3981 ff0d2220 2019-07-11 stsp if (err)
3982 ff0d2220 2019-07-11 stsp goto done;
3983 ff0d2220 2019-07-11 stsp }
3984 818c7501 2019-07-11 stsp
3985 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3986 ff0d2220 2019-07-11 stsp if (new_id_str)
3987 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3988 0ebf8283 2019-07-24 stsp
3989 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3990 0ebf8283 2019-07-24 stsp if (err)
3991 0ebf8283 2019-07-24 stsp goto done;
3992 0ebf8283 2019-07-24 stsp
3993 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3994 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3995 818c7501 2019-07-11 stsp done:
3996 818c7501 2019-07-11 stsp free(old_id_str);
3997 818c7501 2019-07-11 stsp free(new_id_str);
3998 818c7501 2019-07-11 stsp return err;
3999 818c7501 2019-07-11 stsp }
4000 818c7501 2019-07-11 stsp
4001 1ee397ad 2019-07-12 stsp static const struct got_error *
4002 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4003 818c7501 2019-07-11 stsp {
4004 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4005 818c7501 2019-07-11 stsp
4006 818c7501 2019-07-11 stsp while (path[0] == '/')
4007 818c7501 2019-07-11 stsp path++;
4008 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4009 818c7501 2019-07-11 stsp
4010 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4011 1ee397ad 2019-07-12 stsp return NULL;
4012 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4013 818c7501 2019-07-11 stsp *rebase_status = status;
4014 1ee397ad 2019-07-12 stsp return NULL;
4015 818c7501 2019-07-11 stsp }
4016 818c7501 2019-07-11 stsp
4017 818c7501 2019-07-11 stsp static const struct got_error *
4018 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4019 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4020 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4021 818c7501 2019-07-11 stsp {
4022 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4023 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4024 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4025 818c7501 2019-07-11 stsp }
4026 818c7501 2019-07-11 stsp
4027 818c7501 2019-07-11 stsp static const struct got_error *
4028 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4029 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4030 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4031 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4032 ff0d2220 2019-07-11 stsp {
4033 ff0d2220 2019-07-11 stsp const struct got_error *error;
4034 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4035 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4036 ff0d2220 2019-07-11 stsp
4037 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4038 ff0d2220 2019-07-11 stsp if (error)
4039 ff0d2220 2019-07-11 stsp return error;
4040 ff0d2220 2019-07-11 stsp
4041 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4042 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
4043 ff0d2220 2019-07-11 stsp if (error) {
4044 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4045 ff0d2220 2019-07-11 stsp goto done;
4046 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
4047 ff0d2220 2019-07-11 stsp } else {
4048 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
4049 ff0d2220 2019-07-11 stsp free(new_commit_id);
4050 ff0d2220 2019-07-11 stsp }
4051 ff0d2220 2019-07-11 stsp done:
4052 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
4053 ff0d2220 2019-07-11 stsp return error;
4054 64c6d990 2019-07-11 stsp }
4055 64c6d990 2019-07-11 stsp
4056 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
4057 64c6d990 2019-07-11 stsp const char *path_prefix;
4058 64c6d990 2019-07-11 stsp size_t len;
4059 8ca9bd68 2019-07-25 stsp int errcode;
4060 64c6d990 2019-07-11 stsp };
4061 64c6d990 2019-07-11 stsp
4062 64c6d990 2019-07-11 stsp static const struct got_error *
4063 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4064 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
4065 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
4066 64c6d990 2019-07-11 stsp struct got_repository *repo)
4067 64c6d990 2019-07-11 stsp {
4068 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
4069 64c6d990 2019-07-11 stsp
4070 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4071 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4072 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
4073 64c6d990 2019-07-11 stsp
4074 64c6d990 2019-07-11 stsp return NULL;
4075 64c6d990 2019-07-11 stsp }
4076 64c6d990 2019-07-11 stsp
4077 64c6d990 2019-07-11 stsp static const struct got_error *
4078 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
4079 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
4080 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
4081 64c6d990 2019-07-11 stsp {
4082 64c6d990 2019-07-11 stsp const struct got_error *err;
4083 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4084 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
4085 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
4086 64c6d990 2019-07-11 stsp
4087 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
4088 64c6d990 2019-07-11 stsp return NULL;
4089 64c6d990 2019-07-11 stsp
4090 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4091 64c6d990 2019-07-11 stsp if (err)
4092 64c6d990 2019-07-11 stsp goto done;
4093 64c6d990 2019-07-11 stsp
4094 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4095 64c6d990 2019-07-11 stsp if (err)
4096 64c6d990 2019-07-11 stsp goto done;
4097 64c6d990 2019-07-11 stsp
4098 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
4099 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
4100 64c6d990 2019-07-11 stsp if (err)
4101 64c6d990 2019-07-11 stsp goto done;
4102 64c6d990 2019-07-11 stsp
4103 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
4104 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
4105 64c6d990 2019-07-11 stsp if (err)
4106 64c6d990 2019-07-11 stsp goto done;
4107 64c6d990 2019-07-11 stsp
4108 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
4109 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
4110 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
4111 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
4112 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
4113 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
4114 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
4115 64c6d990 2019-07-11 stsp done:
4116 64c6d990 2019-07-11 stsp if (tree1)
4117 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
4118 64c6d990 2019-07-11 stsp if (tree2)
4119 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
4120 64c6d990 2019-07-11 stsp if (commit)
4121 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
4122 64c6d990 2019-07-11 stsp if (parent_commit)
4123 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
4124 64c6d990 2019-07-11 stsp return err;
4125 ff0d2220 2019-07-11 stsp }
4126 ff0d2220 2019-07-11 stsp
4127 ff0d2220 2019-07-11 stsp static const struct got_error *
4128 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
4129 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
4130 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4131 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
4132 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
4133 af61c510 2019-07-19 stsp {
4134 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
4135 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
4136 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
4137 af61c510 2019-07-19 stsp struct got_object_qid *qid;
4138 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
4139 af61c510 2019-07-19 stsp
4140 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4141 af61c510 2019-07-19 stsp if (err)
4142 af61c510 2019-07-19 stsp return err;
4143 af61c510 2019-07-19 stsp
4144 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4145 af61c510 2019-07-19 stsp if (err)
4146 af61c510 2019-07-19 stsp goto done;
4147 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4148 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
4149 af61c510 2019-07-19 stsp if (err) {
4150 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
4151 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
4152 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
4153 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
4154 af61c510 2019-07-19 stsp "been reached?!?");
4155 af61c510 2019-07-19 stsp goto done;
4156 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4157 af61c510 2019-07-19 stsp goto done;
4158 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
4159 af61c510 2019-07-19 stsp if (err)
4160 af61c510 2019-07-19 stsp goto done;
4161 af61c510 2019-07-19 stsp } else {
4162 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
4163 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
4164 af61c510 2019-07-19 stsp if (err)
4165 af61c510 2019-07-19 stsp goto done;
4166 af61c510 2019-07-19 stsp
4167 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
4168 af61c510 2019-07-19 stsp if (err)
4169 af61c510 2019-07-19 stsp goto done;
4170 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4171 af61c510 2019-07-19 stsp commit_id = parent_id;
4172 af61c510 2019-07-19 stsp }
4173 af61c510 2019-07-19 stsp }
4174 af61c510 2019-07-19 stsp done:
4175 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
4176 af61c510 2019-07-19 stsp return err;
4177 af61c510 2019-07-19 stsp }
4178 af61c510 2019-07-19 stsp
4179 af61c510 2019-07-19 stsp static const struct got_error *
4180 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
4181 818c7501 2019-07-11 stsp {
4182 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
4183 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
4184 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
4185 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4186 818c7501 2019-07-11 stsp char *cwd = NULL;
4187 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
4188 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4189 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
4190 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
4191 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4192 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
4193 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4194 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4195 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
4196 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
4197 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
4198 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
4199 818c7501 2019-07-11 stsp
4200 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
4201 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
4202 818c7501 2019-07-11 stsp
4203 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
4204 818c7501 2019-07-11 stsp switch (ch) {
4205 818c7501 2019-07-11 stsp case 'a':
4206 818c7501 2019-07-11 stsp abort_rebase = 1;
4207 818c7501 2019-07-11 stsp break;
4208 818c7501 2019-07-11 stsp case 'c':
4209 818c7501 2019-07-11 stsp continue_rebase = 1;
4210 818c7501 2019-07-11 stsp break;
4211 818c7501 2019-07-11 stsp default:
4212 818c7501 2019-07-11 stsp usage_rebase();
4213 818c7501 2019-07-11 stsp /* NOTREACHED */
4214 818c7501 2019-07-11 stsp }
4215 818c7501 2019-07-11 stsp }
4216 818c7501 2019-07-11 stsp
4217 818c7501 2019-07-11 stsp argc -= optind;
4218 818c7501 2019-07-11 stsp argv += optind;
4219 818c7501 2019-07-11 stsp
4220 43012d58 2019-07-14 stsp #ifndef PROFILE
4221 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4222 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4223 43012d58 2019-07-14 stsp err(1, "pledge");
4224 43012d58 2019-07-14 stsp #endif
4225 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
4226 818c7501 2019-07-11 stsp usage_rebase();
4227 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
4228 818c7501 2019-07-11 stsp if (argc != 0)
4229 818c7501 2019-07-11 stsp usage_rebase();
4230 818c7501 2019-07-11 stsp } else if (argc != 1)
4231 818c7501 2019-07-11 stsp usage_rebase();
4232 818c7501 2019-07-11 stsp
4233 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
4234 818c7501 2019-07-11 stsp if (cwd == NULL) {
4235 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
4236 818c7501 2019-07-11 stsp goto done;
4237 818c7501 2019-07-11 stsp }
4238 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
4239 818c7501 2019-07-11 stsp if (error)
4240 818c7501 2019-07-11 stsp goto done;
4241 818c7501 2019-07-11 stsp
4242 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4243 818c7501 2019-07-11 stsp if (error != NULL)
4244 818c7501 2019-07-11 stsp goto done;
4245 818c7501 2019-07-11 stsp
4246 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4247 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4248 818c7501 2019-07-11 stsp if (error)
4249 818c7501 2019-07-11 stsp goto done;
4250 818c7501 2019-07-11 stsp
4251 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4252 818c7501 2019-07-11 stsp if (error)
4253 818c7501 2019-07-11 stsp goto done;
4254 818c7501 2019-07-11 stsp
4255 f6794adc 2019-07-23 stsp if (abort_rebase) {
4256 818c7501 2019-07-11 stsp int did_something;
4257 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4258 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4259 f6794adc 2019-07-23 stsp goto done;
4260 f6794adc 2019-07-23 stsp }
4261 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4262 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4263 3e3a69f1 2019-07-25 stsp worktree, repo);
4264 818c7501 2019-07-11 stsp if (error)
4265 818c7501 2019-07-11 stsp goto done;
4266 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
4267 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
4268 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
4269 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
4270 818c7501 2019-07-11 stsp if (error)
4271 818c7501 2019-07-11 stsp goto done;
4272 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4273 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
4274 818c7501 2019-07-11 stsp }
4275 818c7501 2019-07-11 stsp
4276 818c7501 2019-07-11 stsp if (continue_rebase) {
4277 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
4278 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
4279 f6794adc 2019-07-23 stsp goto done;
4280 f6794adc 2019-07-23 stsp }
4281 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
4282 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
4283 3e3a69f1 2019-07-25 stsp worktree, repo);
4284 818c7501 2019-07-11 stsp if (error)
4285 818c7501 2019-07-11 stsp goto done;
4286 818c7501 2019-07-11 stsp
4287 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4288 01757395 2019-07-12 stsp resume_commit_id, repo);
4289 818c7501 2019-07-11 stsp if (error)
4290 818c7501 2019-07-11 stsp goto done;
4291 818c7501 2019-07-11 stsp
4292 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
4293 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
4294 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
4295 818c7501 2019-07-11 stsp goto done;
4296 818c7501 2019-07-11 stsp }
4297 818c7501 2019-07-11 stsp } else {
4298 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
4299 818c7501 2019-07-11 stsp if (error != NULL)
4300 818c7501 2019-07-11 stsp goto done;
4301 ff0d2220 2019-07-11 stsp }
4302 818c7501 2019-07-11 stsp
4303 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4304 ff0d2220 2019-07-11 stsp if (error)
4305 ff0d2220 2019-07-11 stsp goto done;
4306 ff0d2220 2019-07-11 stsp
4307 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
4308 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
4309 a51a74b3 2019-07-27 stsp
4310 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
4311 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4312 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
4313 818c7501 2019-07-11 stsp if (error)
4314 818c7501 2019-07-11 stsp goto done;
4315 818c7501 2019-07-11 stsp if (yca_id == NULL) {
4316 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
4317 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
4318 818c7501 2019-07-11 stsp "with work tree's branch");
4319 818c7501 2019-07-11 stsp goto done;
4320 818c7501 2019-07-11 stsp }
4321 818c7501 2019-07-11 stsp
4322 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
4323 a51a74b3 2019-07-27 stsp if (error) {
4324 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
4325 a51a74b3 2019-07-27 stsp goto done;
4326 a51a74b3 2019-07-27 stsp error = NULL;
4327 a51a74b3 2019-07-27 stsp } else {
4328 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
4329 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
4330 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
4331 a51a74b3 2019-07-27 stsp goto done;
4332 a51a74b3 2019-07-27 stsp }
4333 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
4334 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
4335 818c7501 2019-07-11 stsp if (error)
4336 818c7501 2019-07-11 stsp goto done;
4337 818c7501 2019-07-11 stsp }
4338 818c7501 2019-07-11 stsp
4339 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
4340 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4341 818c7501 2019-07-11 stsp if (error)
4342 818c7501 2019-07-11 stsp goto done;
4343 818c7501 2019-07-11 stsp
4344 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4345 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4346 fc66b545 2019-08-12 stsp if (pid == NULL) {
4347 fc66b545 2019-08-12 stsp if (!continue_rebase) {
4348 fc66b545 2019-08-12 stsp int did_something;
4349 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
4350 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
4351 fc66b545 2019-08-12 stsp &did_something);
4352 fc66b545 2019-08-12 stsp if (error)
4353 fc66b545 2019-08-12 stsp goto done;
4354 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
4355 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
4356 fc66b545 2019-08-12 stsp }
4357 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4358 fc66b545 2019-08-12 stsp goto done;
4359 fc66b545 2019-08-12 stsp }
4360 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4361 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4362 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4363 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4364 818c7501 2019-07-11 stsp commit = NULL;
4365 818c7501 2019-07-11 stsp if (error)
4366 818c7501 2019-07-11 stsp goto done;
4367 64c6d990 2019-07-11 stsp
4368 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4369 ff0d2220 2019-07-11 stsp if (continue_rebase)
4370 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4371 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4372 ff0d2220 2019-07-11 stsp else
4373 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4374 818c7501 2019-07-11 stsp goto done;
4375 818c7501 2019-07-11 stsp }
4376 818c7501 2019-07-11 stsp
4377 818c7501 2019-07-11 stsp pid = NULL;
4378 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4379 818c7501 2019-07-11 stsp commit_id = qid->id;
4380 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4381 818c7501 2019-07-11 stsp pid = qid;
4382 818c7501 2019-07-11 stsp
4383 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4384 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4385 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4386 818c7501 2019-07-11 stsp if (error)
4387 818c7501 2019-07-11 stsp goto done;
4388 818c7501 2019-07-11 stsp
4389 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4390 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4391 818c7501 2019-07-11 stsp break;
4392 01757395 2019-07-12 stsp }
4393 818c7501 2019-07-11 stsp
4394 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4395 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4396 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4397 818c7501 2019-07-11 stsp if (error)
4398 818c7501 2019-07-11 stsp goto done;
4399 818c7501 2019-07-11 stsp }
4400 818c7501 2019-07-11 stsp
4401 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4402 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4403 818c7501 2019-07-11 stsp if (error)
4404 818c7501 2019-07-11 stsp goto done;
4405 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4406 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4407 818c7501 2019-07-11 stsp } else
4408 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4409 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4410 818c7501 2019-07-11 stsp done:
4411 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4412 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4413 818c7501 2019-07-11 stsp free(resume_commit_id);
4414 818c7501 2019-07-11 stsp free(yca_id);
4415 818c7501 2019-07-11 stsp if (commit)
4416 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4417 818c7501 2019-07-11 stsp if (branch)
4418 818c7501 2019-07-11 stsp got_ref_close(branch);
4419 818c7501 2019-07-11 stsp if (new_base_branch)
4420 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4421 818c7501 2019-07-11 stsp if (tmp_branch)
4422 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4423 5ef14e63 2019-06-02 stsp if (worktree)
4424 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4425 5ef14e63 2019-06-02 stsp if (repo)
4426 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4427 5ef14e63 2019-06-02 stsp return error;
4428 0ebf8283 2019-07-24 stsp }
4429 0ebf8283 2019-07-24 stsp
4430 0ebf8283 2019-07-24 stsp __dead static void
4431 0ebf8283 2019-07-24 stsp usage_histedit(void)
4432 0ebf8283 2019-07-24 stsp {
4433 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4434 0ebf8283 2019-07-24 stsp getprogname());
4435 0ebf8283 2019-07-24 stsp exit(1);
4436 0ebf8283 2019-07-24 stsp }
4437 0ebf8283 2019-07-24 stsp
4438 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4439 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4440 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4441 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4442 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4443 0ebf8283 2019-07-24 stsp
4444 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4445 0ebf8283 2019-07-24 stsp unsigned char code;
4446 0ebf8283 2019-07-24 stsp const char *name;
4447 0ebf8283 2019-07-24 stsp const char *desc;
4448 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4449 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4450 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4451 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4452 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4453 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4454 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4455 0ebf8283 2019-07-24 stsp };
4456 0ebf8283 2019-07-24 stsp
4457 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4458 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4459 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4460 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4461 0ebf8283 2019-07-24 stsp char *logmsg;
4462 0ebf8283 2019-07-24 stsp };
4463 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4464 0ebf8283 2019-07-24 stsp
4465 0ebf8283 2019-07-24 stsp static const struct got_error *
4466 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4467 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4468 0ebf8283 2019-07-24 stsp {
4469 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4470 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4471 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4472 8138f3e1 2019-08-11 stsp int n;
4473 0ebf8283 2019-07-24 stsp
4474 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4475 0ebf8283 2019-07-24 stsp if (err)
4476 0ebf8283 2019-07-24 stsp goto done;
4477 0ebf8283 2019-07-24 stsp
4478 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4479 0ebf8283 2019-07-24 stsp if (err)
4480 0ebf8283 2019-07-24 stsp goto done;
4481 0ebf8283 2019-07-24 stsp
4482 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4483 0ebf8283 2019-07-24 stsp if (err)
4484 0ebf8283 2019-07-24 stsp goto done;
4485 0ebf8283 2019-07-24 stsp
4486 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4487 0ebf8283 2019-07-24 stsp if (n < 0)
4488 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4489 0ebf8283 2019-07-24 stsp done:
4490 0ebf8283 2019-07-24 stsp if (commit)
4491 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4492 0ebf8283 2019-07-24 stsp free(id_str);
4493 0ebf8283 2019-07-24 stsp free(logmsg);
4494 0ebf8283 2019-07-24 stsp return err;
4495 0ebf8283 2019-07-24 stsp }
4496 0ebf8283 2019-07-24 stsp
4497 0ebf8283 2019-07-24 stsp static const struct got_error *
4498 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4499 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4500 0ebf8283 2019-07-24 stsp {
4501 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4502 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4503 0ebf8283 2019-07-24 stsp
4504 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4505 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4506 0ebf8283 2019-07-24 stsp
4507 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4508 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4509 0ebf8283 2019-07-24 stsp f, repo);
4510 0ebf8283 2019-07-24 stsp if (err)
4511 0ebf8283 2019-07-24 stsp break;
4512 0ebf8283 2019-07-24 stsp }
4513 0ebf8283 2019-07-24 stsp
4514 0ebf8283 2019-07-24 stsp return err;
4515 0ebf8283 2019-07-24 stsp }
4516 0ebf8283 2019-07-24 stsp
4517 0ebf8283 2019-07-24 stsp static const struct got_error *
4518 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4519 0ebf8283 2019-07-24 stsp {
4520 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4521 0ebf8283 2019-07-24 stsp int n, i;
4522 0ebf8283 2019-07-24 stsp
4523 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4524 0ebf8283 2019-07-24 stsp if (n < 0)
4525 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4526 0ebf8283 2019-07-24 stsp
4527 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4528 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4529 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4530 0ebf8283 2019-07-24 stsp cmd->desc);
4531 0ebf8283 2019-07-24 stsp if (n < 0) {
4532 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4533 0ebf8283 2019-07-24 stsp break;
4534 0ebf8283 2019-07-24 stsp }
4535 0ebf8283 2019-07-24 stsp }
4536 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4537 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4538 0ebf8283 2019-07-24 stsp if (n < 0)
4539 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4540 0ebf8283 2019-07-24 stsp return err;
4541 0ebf8283 2019-07-24 stsp }
4542 0ebf8283 2019-07-24 stsp
4543 0ebf8283 2019-07-24 stsp static const struct got_error *
4544 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4545 0ebf8283 2019-07-24 stsp {
4546 0ebf8283 2019-07-24 stsp static char msg[42];
4547 0ebf8283 2019-07-24 stsp int ret;
4548 0ebf8283 2019-07-24 stsp
4549 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4550 0ebf8283 2019-07-24 stsp lineno);
4551 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4552 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4553 0ebf8283 2019-07-24 stsp
4554 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4555 0ebf8283 2019-07-24 stsp }
4556 0ebf8283 2019-07-24 stsp
4557 0ebf8283 2019-07-24 stsp static const struct got_error *
4558 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4559 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4560 0ebf8283 2019-07-24 stsp {
4561 0ebf8283 2019-07-24 stsp const struct got_error *err;
4562 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4563 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
4564 0ebf8283 2019-07-24 stsp
4565 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4566 0ebf8283 2019-07-24 stsp if (err)
4567 0ebf8283 2019-07-24 stsp return err;
4568 0ebf8283 2019-07-24 stsp
4569 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4570 0ebf8283 2019-07-24 stsp if (err)
4571 0ebf8283 2019-07-24 stsp goto done;
4572 0ebf8283 2019-07-24 stsp
4573 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4574 5943eee2 2019-08-13 stsp if (err)
4575 5943eee2 2019-08-13 stsp goto done;
4576 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4577 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4578 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
4579 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4580 0ebf8283 2019-07-24 stsp goto done;
4581 0ebf8283 2019-07-24 stsp }
4582 0ebf8283 2019-07-24 stsp done:
4583 0ebf8283 2019-07-24 stsp if (folded_commit)
4584 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4585 0ebf8283 2019-07-24 stsp free(id_str);
4586 5943eee2 2019-08-13 stsp free(folded_logmsg);
4587 0ebf8283 2019-07-24 stsp return err;
4588 0ebf8283 2019-07-24 stsp }
4589 0ebf8283 2019-07-24 stsp
4590 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4591 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4592 0ebf8283 2019-07-24 stsp {
4593 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4594 0ebf8283 2019-07-24 stsp
4595 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4596 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4597 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4598 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4599 3f9de99f 2019-07-24 stsp folded = prev;
4600 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4601 0ebf8283 2019-07-24 stsp }
4602 0ebf8283 2019-07-24 stsp
4603 0ebf8283 2019-07-24 stsp return folded;
4604 0ebf8283 2019-07-24 stsp }
4605 0ebf8283 2019-07-24 stsp
4606 0ebf8283 2019-07-24 stsp static const struct got_error *
4607 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4608 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4609 0ebf8283 2019-07-24 stsp {
4610 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4611 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4612 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4613 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4614 0ebf8283 2019-07-24 stsp int fd;
4615 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4616 0ebf8283 2019-07-24 stsp
4617 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4618 0ebf8283 2019-07-24 stsp if (err)
4619 0ebf8283 2019-07-24 stsp return err;
4620 0ebf8283 2019-07-24 stsp
4621 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4622 0ebf8283 2019-07-24 stsp if (folded) {
4623 0ebf8283 2019-07-24 stsp while (folded != hle) {
4624 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4625 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4626 3f9de99f 2019-07-24 stsp continue;
4627 3f9de99f 2019-07-24 stsp }
4628 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4629 0ebf8283 2019-07-24 stsp logmsg, repo);
4630 0ebf8283 2019-07-24 stsp if (err)
4631 0ebf8283 2019-07-24 stsp goto done;
4632 0ebf8283 2019-07-24 stsp free(logmsg);
4633 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4634 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4635 0ebf8283 2019-07-24 stsp }
4636 0ebf8283 2019-07-24 stsp }
4637 0ebf8283 2019-07-24 stsp
4638 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4639 0ebf8283 2019-07-24 stsp if (err)
4640 0ebf8283 2019-07-24 stsp goto done;
4641 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4642 5943eee2 2019-08-13 stsp if (err)
4643 5943eee2 2019-08-13 stsp goto done;
4644 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4645 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4646 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4647 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4648 0ebf8283 2019-07-24 stsp goto done;
4649 0ebf8283 2019-07-24 stsp }
4650 0ebf8283 2019-07-24 stsp free(logmsg);
4651 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4652 0ebf8283 2019-07-24 stsp
4653 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4654 0ebf8283 2019-07-24 stsp if (err)
4655 0ebf8283 2019-07-24 stsp goto done;
4656 0ebf8283 2019-07-24 stsp
4657 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4658 0ebf8283 2019-07-24 stsp if (err)
4659 0ebf8283 2019-07-24 stsp goto done;
4660 0ebf8283 2019-07-24 stsp
4661 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4662 0ebf8283 2019-07-24 stsp close(fd);
4663 0ebf8283 2019-07-24 stsp
4664 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4665 0ebf8283 2019-07-24 stsp if (err)
4666 0ebf8283 2019-07-24 stsp goto done;
4667 0ebf8283 2019-07-24 stsp
4668 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4669 0ebf8283 2019-07-24 stsp if (err) {
4670 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4671 0ebf8283 2019-07-24 stsp goto done;
4672 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4673 0ebf8283 2019-07-24 stsp }
4674 0ebf8283 2019-07-24 stsp done:
4675 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4676 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4677 0ebf8283 2019-07-24 stsp free(logmsg_path);
4678 0ebf8283 2019-07-24 stsp free(logmsg);
4679 5943eee2 2019-08-13 stsp free(orig_logmsg);
4680 0ebf8283 2019-07-24 stsp free(editor);
4681 0ebf8283 2019-07-24 stsp if (commit)
4682 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4683 0ebf8283 2019-07-24 stsp return err;
4684 5ef14e63 2019-06-02 stsp }
4685 0ebf8283 2019-07-24 stsp
4686 0ebf8283 2019-07-24 stsp static const struct got_error *
4687 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4688 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4689 0ebf8283 2019-07-24 stsp {
4690 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4691 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4692 0ebf8283 2019-07-24 stsp size_t size;
4693 0ebf8283 2019-07-24 stsp ssize_t len;
4694 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4695 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4696 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4697 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4698 0ebf8283 2019-07-24 stsp
4699 0ebf8283 2019-07-24 stsp for (;;) {
4700 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4701 0ebf8283 2019-07-24 stsp if (len == -1) {
4702 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4703 0ebf8283 2019-07-24 stsp if (feof(f))
4704 0ebf8283 2019-07-24 stsp break;
4705 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4706 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4707 0ebf8283 2019-07-24 stsp break;
4708 0ebf8283 2019-07-24 stsp }
4709 0ebf8283 2019-07-24 stsp lineno++;
4710 0ebf8283 2019-07-24 stsp p = line;
4711 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4712 0ebf8283 2019-07-24 stsp p++;
4713 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4714 0ebf8283 2019-07-24 stsp free(line);
4715 0ebf8283 2019-07-24 stsp line = NULL;
4716 0ebf8283 2019-07-24 stsp continue;
4717 0ebf8283 2019-07-24 stsp }
4718 0ebf8283 2019-07-24 stsp cmd = NULL;
4719 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4720 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4721 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4722 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4723 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4724 0ebf8283 2019-07-24 stsp break;
4725 0ebf8283 2019-07-24 stsp }
4726 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4727 0ebf8283 2019-07-24 stsp p++;
4728 0ebf8283 2019-07-24 stsp break;
4729 0ebf8283 2019-07-24 stsp }
4730 0ebf8283 2019-07-24 stsp }
4731 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4732 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4733 0ebf8283 2019-07-24 stsp break;
4734 0ebf8283 2019-07-24 stsp }
4735 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4736 0ebf8283 2019-07-24 stsp p++;
4737 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4738 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4739 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4740 0ebf8283 2019-07-24 stsp break;
4741 0ebf8283 2019-07-24 stsp }
4742 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4743 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4744 0ebf8283 2019-07-24 stsp if (err)
4745 0ebf8283 2019-07-24 stsp break;
4746 0ebf8283 2019-07-24 stsp } else {
4747 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4748 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4749 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4750 0ebf8283 2019-07-24 stsp break;
4751 0ebf8283 2019-07-24 stsp }
4752 0ebf8283 2019-07-24 stsp }
4753 0ebf8283 2019-07-24 stsp free(line);
4754 0ebf8283 2019-07-24 stsp line = NULL;
4755 0ebf8283 2019-07-24 stsp continue;
4756 0ebf8283 2019-07-24 stsp } else {
4757 0ebf8283 2019-07-24 stsp end = p;
4758 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4759 0ebf8283 2019-07-24 stsp end++;
4760 0ebf8283 2019-07-24 stsp *end = '\0';
4761 0ebf8283 2019-07-24 stsp
4762 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4763 0ebf8283 2019-07-24 stsp if (err) {
4764 0ebf8283 2019-07-24 stsp /* override error code */
4765 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4766 0ebf8283 2019-07-24 stsp break;
4767 0ebf8283 2019-07-24 stsp }
4768 0ebf8283 2019-07-24 stsp }
4769 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4770 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4771 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4772 0ebf8283 2019-07-24 stsp break;
4773 0ebf8283 2019-07-24 stsp }
4774 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4775 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4776 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4777 0ebf8283 2019-07-24 stsp commit_id = NULL;
4778 0ebf8283 2019-07-24 stsp free(line);
4779 0ebf8283 2019-07-24 stsp line = NULL;
4780 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4781 0ebf8283 2019-07-24 stsp }
4782 0ebf8283 2019-07-24 stsp
4783 0ebf8283 2019-07-24 stsp free(line);
4784 0ebf8283 2019-07-24 stsp free(commit_id);
4785 0ebf8283 2019-07-24 stsp return err;
4786 0ebf8283 2019-07-24 stsp }
4787 0ebf8283 2019-07-24 stsp
4788 0ebf8283 2019-07-24 stsp static const struct got_error *
4789 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4790 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4791 bfce7f83 2019-07-27 stsp {
4792 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4793 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4794 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4795 bfce7f83 2019-07-27 stsp static char msg[80];
4796 bfce7f83 2019-07-27 stsp char *id_str;
4797 bfce7f83 2019-07-27 stsp
4798 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
4799 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4800 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
4801 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
4802 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4803 bfce7f83 2019-07-27 stsp
4804 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4805 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4806 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4807 bfce7f83 2019-07-27 stsp break;
4808 bfce7f83 2019-07-27 stsp }
4809 bfce7f83 2019-07-27 stsp if (hle == NULL) {
4810 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
4811 bfce7f83 2019-07-27 stsp if (err)
4812 bfce7f83 2019-07-27 stsp return err;
4813 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
4814 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
4815 bfce7f83 2019-07-27 stsp free(id_str);
4816 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4817 bfce7f83 2019-07-27 stsp }
4818 bfce7f83 2019-07-27 stsp }
4819 bfce7f83 2019-07-27 stsp
4820 bfce7f83 2019-07-27 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4821 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4822 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
4823 bfce7f83 2019-07-27 stsp
4824 bfce7f83 2019-07-27 stsp return NULL;
4825 bfce7f83 2019-07-27 stsp }
4826 bfce7f83 2019-07-27 stsp
4827 bfce7f83 2019-07-27 stsp static const struct got_error *
4828 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4829 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
4830 bfce7f83 2019-07-27 stsp struct got_repository *repo)
4831 0ebf8283 2019-07-24 stsp {
4832 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4833 0ebf8283 2019-07-24 stsp char *editor;
4834 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4835 0ebf8283 2019-07-24 stsp
4836 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4837 0ebf8283 2019-07-24 stsp if (err)
4838 0ebf8283 2019-07-24 stsp return err;
4839 0ebf8283 2019-07-24 stsp
4840 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4841 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4842 0ebf8283 2019-07-24 stsp goto done;
4843 0ebf8283 2019-07-24 stsp }
4844 0ebf8283 2019-07-24 stsp
4845 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4846 0ebf8283 2019-07-24 stsp if (f == NULL) {
4847 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4848 0ebf8283 2019-07-24 stsp goto done;
4849 0ebf8283 2019-07-24 stsp }
4850 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4851 bfce7f83 2019-07-27 stsp if (err)
4852 bfce7f83 2019-07-27 stsp goto done;
4853 bfce7f83 2019-07-27 stsp
4854 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
4855 0ebf8283 2019-07-24 stsp done:
4856 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4857 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4858 0ebf8283 2019-07-24 stsp free(editor);
4859 0ebf8283 2019-07-24 stsp return err;
4860 0ebf8283 2019-07-24 stsp }
4861 0ebf8283 2019-07-24 stsp
4862 0ebf8283 2019-07-24 stsp static const struct got_error *
4863 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4864 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4865 0ebf8283 2019-07-24 stsp
4866 0ebf8283 2019-07-24 stsp static const struct got_error *
4867 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4868 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4869 0ebf8283 2019-07-24 stsp {
4870 0ebf8283 2019-07-24 stsp const struct got_error *err;
4871 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4872 0ebf8283 2019-07-24 stsp char *path = NULL;
4873 0ebf8283 2019-07-24 stsp
4874 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4875 0ebf8283 2019-07-24 stsp if (err)
4876 0ebf8283 2019-07-24 stsp return err;
4877 0ebf8283 2019-07-24 stsp
4878 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4879 0ebf8283 2019-07-24 stsp if (err)
4880 0ebf8283 2019-07-24 stsp goto done;
4881 0ebf8283 2019-07-24 stsp
4882 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4883 0ebf8283 2019-07-24 stsp if (err)
4884 0ebf8283 2019-07-24 stsp goto done;
4885 0ebf8283 2019-07-24 stsp
4886 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4887 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4888 0ebf8283 2019-07-24 stsp goto done;
4889 0ebf8283 2019-07-24 stsp }
4890 0ebf8283 2019-07-24 stsp f = NULL;
4891 0ebf8283 2019-07-24 stsp
4892 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
4893 0ebf8283 2019-07-24 stsp if (err) {
4894 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4895 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4896 0ebf8283 2019-07-24 stsp goto done;
4897 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
4898 0ebf8283 2019-07-24 stsp commits, path, repo);
4899 0ebf8283 2019-07-24 stsp }
4900 0ebf8283 2019-07-24 stsp done:
4901 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4902 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4903 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4904 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4905 0ebf8283 2019-07-24 stsp free(path);
4906 0ebf8283 2019-07-24 stsp return err;
4907 0ebf8283 2019-07-24 stsp }
4908 0ebf8283 2019-07-24 stsp
4909 0ebf8283 2019-07-24 stsp static const struct got_error *
4910 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4911 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4912 0ebf8283 2019-07-24 stsp {
4913 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4914 0ebf8283 2019-07-24 stsp char *path = NULL;
4915 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4916 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4917 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4918 0ebf8283 2019-07-24 stsp
4919 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
4920 0ebf8283 2019-07-24 stsp if (err)
4921 0ebf8283 2019-07-24 stsp return err;
4922 0ebf8283 2019-07-24 stsp
4923 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4924 0ebf8283 2019-07-24 stsp if (f == NULL) {
4925 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4926 0ebf8283 2019-07-24 stsp goto done;
4927 0ebf8283 2019-07-24 stsp }
4928 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4929 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4930 0ebf8283 2019-07-24 stsp repo);
4931 0ebf8283 2019-07-24 stsp if (err)
4932 0ebf8283 2019-07-24 stsp break;
4933 0ebf8283 2019-07-24 stsp
4934 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4935 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4936 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4937 0ebf8283 2019-07-24 stsp if (n < 0) {
4938 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4939 0ebf8283 2019-07-24 stsp break;
4940 0ebf8283 2019-07-24 stsp }
4941 0ebf8283 2019-07-24 stsp }
4942 0ebf8283 2019-07-24 stsp }
4943 0ebf8283 2019-07-24 stsp done:
4944 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4945 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4946 0ebf8283 2019-07-24 stsp free(path);
4947 0ebf8283 2019-07-24 stsp if (commit)
4948 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4949 0ebf8283 2019-07-24 stsp return err;
4950 0ebf8283 2019-07-24 stsp }
4951 0ebf8283 2019-07-24 stsp
4952 bfce7f83 2019-07-27 stsp void
4953 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
4954 bfce7f83 2019-07-27 stsp {
4955 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4956 bfce7f83 2019-07-27 stsp
4957 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
4958 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
4959 bfce7f83 2019-07-27 stsp free(hle);
4960 bfce7f83 2019-07-27 stsp }
4961 bfce7f83 2019-07-27 stsp }
4962 bfce7f83 2019-07-27 stsp
4963 0ebf8283 2019-07-24 stsp static const struct got_error *
4964 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4965 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4966 0ebf8283 2019-07-24 stsp {
4967 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4968 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4969 0ebf8283 2019-07-24 stsp
4970 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4971 0ebf8283 2019-07-24 stsp if (f == NULL) {
4972 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4973 0ebf8283 2019-07-24 stsp goto done;
4974 0ebf8283 2019-07-24 stsp }
4975 0ebf8283 2019-07-24 stsp
4976 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4977 0ebf8283 2019-07-24 stsp done:
4978 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4979 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4980 0ebf8283 2019-07-24 stsp return err;
4981 0ebf8283 2019-07-24 stsp }
4982 0ebf8283 2019-07-24 stsp
4983 0ebf8283 2019-07-24 stsp static const struct got_error *
4984 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4985 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
4986 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4987 0ebf8283 2019-07-24 stsp {
4988 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
4989 0ebf8283 2019-07-24 stsp int resp = ' ';
4990 0ebf8283 2019-07-24 stsp
4991 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4992 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4993 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
4994 0ebf8283 2019-07-24 stsp resp = getchar();
4995 a61a4414 2019-08-07 stsp if (resp == '\n')
4996 a61a4414 2019-08-07 stsp resp = getchar();
4997 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4998 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4999 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5000 bfce7f83 2019-07-27 stsp repo);
5001 426ebf2e 2019-07-25 stsp if (err) {
5002 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5003 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5004 426ebf2e 2019-07-25 stsp break;
5005 bfce7f83 2019-07-27 stsp prev_err = err;
5006 426ebf2e 2019-07-25 stsp resp = ' ';
5007 426ebf2e 2019-07-25 stsp continue;
5008 426ebf2e 2019-07-25 stsp }
5009 bfce7f83 2019-07-27 stsp break;
5010 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5011 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5012 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5013 0ebf8283 2019-07-24 stsp commits, repo);
5014 426ebf2e 2019-07-25 stsp if (err) {
5015 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5016 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5017 426ebf2e 2019-07-25 stsp break;
5018 bfce7f83 2019-07-27 stsp prev_err = err;
5019 426ebf2e 2019-07-25 stsp resp = ' ';
5020 426ebf2e 2019-07-25 stsp continue;
5021 426ebf2e 2019-07-25 stsp }
5022 bfce7f83 2019-07-27 stsp break;
5023 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5024 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5025 0ebf8283 2019-07-24 stsp break;
5026 426ebf2e 2019-07-25 stsp } else
5027 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5028 0ebf8283 2019-07-24 stsp }
5029 0ebf8283 2019-07-24 stsp
5030 0ebf8283 2019-07-24 stsp return err;
5031 0ebf8283 2019-07-24 stsp }
5032 0ebf8283 2019-07-24 stsp
5033 0ebf8283 2019-07-24 stsp static const struct got_error *
5034 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
5035 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5036 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
5037 0ebf8283 2019-07-24 stsp {
5038 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5039 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5040 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5041 3e3a69f1 2019-07-25 stsp branch, repo);
5042 0ebf8283 2019-07-24 stsp }
5043 0ebf8283 2019-07-24 stsp
5044 0ebf8283 2019-07-24 stsp static const struct got_error *
5045 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
5046 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5047 0ebf8283 2019-07-24 stsp {
5048 0ebf8283 2019-07-24 stsp const struct got_error *err;
5049 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5050 0ebf8283 2019-07-24 stsp
5051 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
5052 0ebf8283 2019-07-24 stsp if (err)
5053 0ebf8283 2019-07-24 stsp goto done;
5054 0ebf8283 2019-07-24 stsp
5055 0ebf8283 2019-07-24 stsp if (new_id) {
5056 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
5057 0ebf8283 2019-07-24 stsp if (err)
5058 0ebf8283 2019-07-24 stsp goto done;
5059 0ebf8283 2019-07-24 stsp }
5060 0ebf8283 2019-07-24 stsp
5061 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
5062 0ebf8283 2019-07-24 stsp if (new_id_str)
5063 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
5064 0ebf8283 2019-07-24 stsp
5065 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5066 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
5067 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
5068 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5069 0ebf8283 2019-07-24 stsp goto done;
5070 0ebf8283 2019-07-24 stsp }
5071 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
5072 0ebf8283 2019-07-24 stsp } else {
5073 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5074 0ebf8283 2019-07-24 stsp if (err)
5075 0ebf8283 2019-07-24 stsp goto done;
5076 0ebf8283 2019-07-24 stsp }
5077 0ebf8283 2019-07-24 stsp
5078 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
5079 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
5080 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
5081 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
5082 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5083 0ebf8283 2019-07-24 stsp break;
5084 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
5085 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
5086 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5087 0ebf8283 2019-07-24 stsp logmsg);
5088 0ebf8283 2019-07-24 stsp break;
5089 0ebf8283 2019-07-24 stsp default:
5090 0ebf8283 2019-07-24 stsp break;
5091 0ebf8283 2019-07-24 stsp }
5092 0ebf8283 2019-07-24 stsp
5093 0ebf8283 2019-07-24 stsp done:
5094 0ebf8283 2019-07-24 stsp free(old_id_str);
5095 0ebf8283 2019-07-24 stsp free(new_id_str);
5096 0ebf8283 2019-07-24 stsp return err;
5097 0ebf8283 2019-07-24 stsp }
5098 0ebf8283 2019-07-24 stsp
5099 0ebf8283 2019-07-24 stsp static const struct got_error *
5100 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
5101 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5102 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5103 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
5104 0ebf8283 2019-07-24 stsp {
5105 0ebf8283 2019-07-24 stsp const struct got_error *err;
5106 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5107 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
5108 0ebf8283 2019-07-24 stsp
5109 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5110 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
5111 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5112 0ebf8283 2019-07-24 stsp if (err)
5113 0ebf8283 2019-07-24 stsp return err;
5114 0ebf8283 2019-07-24 stsp }
5115 0ebf8283 2019-07-24 stsp
5116 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5117 0ebf8283 2019-07-24 stsp if (err)
5118 0ebf8283 2019-07-24 stsp return err;
5119 0ebf8283 2019-07-24 stsp
5120 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5121 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
5122 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
5123 0ebf8283 2019-07-24 stsp if (err) {
5124 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5125 0ebf8283 2019-07-24 stsp goto done;
5126 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
5127 0ebf8283 2019-07-24 stsp } else {
5128 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
5129 0ebf8283 2019-07-24 stsp free(new_commit_id);
5130 0ebf8283 2019-07-24 stsp }
5131 0ebf8283 2019-07-24 stsp done:
5132 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5133 0ebf8283 2019-07-24 stsp return err;
5134 0ebf8283 2019-07-24 stsp }
5135 0ebf8283 2019-07-24 stsp
5136 0ebf8283 2019-07-24 stsp static const struct got_error *
5137 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
5138 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5139 0ebf8283 2019-07-24 stsp {
5140 0ebf8283 2019-07-24 stsp const struct got_error *error;
5141 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
5142 0ebf8283 2019-07-24 stsp
5143 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5144 0ebf8283 2019-07-24 stsp repo);
5145 0ebf8283 2019-07-24 stsp if (error)
5146 0ebf8283 2019-07-24 stsp return error;
5147 0ebf8283 2019-07-24 stsp
5148 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5149 0ebf8283 2019-07-24 stsp if (error)
5150 0ebf8283 2019-07-24 stsp return error;
5151 0ebf8283 2019-07-24 stsp
5152 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
5153 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5154 0ebf8283 2019-07-24 stsp return error;
5155 0ebf8283 2019-07-24 stsp }
5156 0ebf8283 2019-07-24 stsp
5157 0ebf8283 2019-07-24 stsp static const struct got_error *
5158 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
5159 0ebf8283 2019-07-24 stsp {
5160 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
5161 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
5162 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5163 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
5164 0ebf8283 2019-07-24 stsp char *cwd = NULL;
5165 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
5166 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
5167 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
5168 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
5169 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
5170 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5171 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
5172 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5173 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
5174 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5175 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
5176 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
5177 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
5178 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
5179 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
5180 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5181 0ebf8283 2019-07-24 stsp
5182 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
5183 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
5184 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
5185 0ebf8283 2019-07-24 stsp
5186 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
5187 0ebf8283 2019-07-24 stsp switch (ch) {
5188 0ebf8283 2019-07-24 stsp case 'a':
5189 0ebf8283 2019-07-24 stsp abort_edit = 1;
5190 0ebf8283 2019-07-24 stsp break;
5191 0ebf8283 2019-07-24 stsp case 'c':
5192 0ebf8283 2019-07-24 stsp continue_edit = 1;
5193 0ebf8283 2019-07-24 stsp break;
5194 0ebf8283 2019-07-24 stsp case 'F':
5195 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
5196 0ebf8283 2019-07-24 stsp break;
5197 0ebf8283 2019-07-24 stsp default:
5198 0ebf8283 2019-07-24 stsp usage_histedit();
5199 0ebf8283 2019-07-24 stsp /* NOTREACHED */
5200 0ebf8283 2019-07-24 stsp }
5201 0ebf8283 2019-07-24 stsp }
5202 0ebf8283 2019-07-24 stsp
5203 0ebf8283 2019-07-24 stsp argc -= optind;
5204 0ebf8283 2019-07-24 stsp argv += optind;
5205 0ebf8283 2019-07-24 stsp
5206 0ebf8283 2019-07-24 stsp #ifndef PROFILE
5207 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5208 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
5209 0ebf8283 2019-07-24 stsp err(1, "pledge");
5210 0ebf8283 2019-07-24 stsp #endif
5211 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
5212 0ebf8283 2019-07-24 stsp usage_histedit();
5213 0ebf8283 2019-07-24 stsp if (argc != 0)
5214 0ebf8283 2019-07-24 stsp usage_histedit();
5215 0ebf8283 2019-07-24 stsp
5216 0ebf8283 2019-07-24 stsp /*
5217 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
5218 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
5219 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
5220 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
5221 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
5222 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
5223 0ebf8283 2019-07-24 stsp */
5224 0ebf8283 2019-07-24 stsp
5225 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
5226 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
5227 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
5228 0ebf8283 2019-07-24 stsp goto done;
5229 0ebf8283 2019-07-24 stsp }
5230 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
5231 0ebf8283 2019-07-24 stsp if (error)
5232 0ebf8283 2019-07-24 stsp goto done;
5233 0ebf8283 2019-07-24 stsp
5234 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5235 0ebf8283 2019-07-24 stsp if (error != NULL)
5236 0ebf8283 2019-07-24 stsp goto done;
5237 0ebf8283 2019-07-24 stsp
5238 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5239 0ebf8283 2019-07-24 stsp if (error)
5240 0ebf8283 2019-07-24 stsp goto done;
5241 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
5242 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
5243 0ebf8283 2019-07-24 stsp goto done;
5244 0ebf8283 2019-07-24 stsp }
5245 0ebf8283 2019-07-24 stsp
5246 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5247 0ebf8283 2019-07-24 stsp if (error)
5248 0ebf8283 2019-07-24 stsp goto done;
5249 0ebf8283 2019-07-24 stsp
5250 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
5251 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5252 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5253 3e3a69f1 2019-07-25 stsp worktree, repo);
5254 0ebf8283 2019-07-24 stsp if (error)
5255 0ebf8283 2019-07-24 stsp goto done;
5256 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
5257 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5258 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
5259 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
5260 0ebf8283 2019-07-24 stsp if (error)
5261 0ebf8283 2019-07-24 stsp goto done;
5262 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
5263 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
5264 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
5265 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
5266 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5267 0ebf8283 2019-07-24 stsp goto done;
5268 0ebf8283 2019-07-24 stsp }
5269 0ebf8283 2019-07-24 stsp
5270 0ebf8283 2019-07-24 stsp if (continue_edit) {
5271 0ebf8283 2019-07-24 stsp char *path;
5272 0ebf8283 2019-07-24 stsp
5273 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
5274 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
5275 0ebf8283 2019-07-24 stsp goto done;
5276 0ebf8283 2019-07-24 stsp }
5277 0ebf8283 2019-07-24 stsp
5278 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
5279 0ebf8283 2019-07-24 stsp if (error)
5280 0ebf8283 2019-07-24 stsp goto done;
5281 0ebf8283 2019-07-24 stsp
5282 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
5283 0ebf8283 2019-07-24 stsp free(path);
5284 0ebf8283 2019-07-24 stsp if (error)
5285 0ebf8283 2019-07-24 stsp goto done;
5286 0ebf8283 2019-07-24 stsp
5287 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
5288 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
5289 3e3a69f1 2019-07-25 stsp worktree, repo);
5290 0ebf8283 2019-07-24 stsp if (error)
5291 0ebf8283 2019-07-24 stsp goto done;
5292 0ebf8283 2019-07-24 stsp
5293 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5294 0ebf8283 2019-07-24 stsp if (error)
5295 0ebf8283 2019-07-24 stsp goto done;
5296 0ebf8283 2019-07-24 stsp
5297 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5298 0ebf8283 2019-07-24 stsp head_commit_id);
5299 0ebf8283 2019-07-24 stsp if (error)
5300 0ebf8283 2019-07-24 stsp goto done;
5301 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5302 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5303 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5304 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5305 3aac7cf7 2019-07-25 stsp goto done;
5306 3aac7cf7 2019-07-25 stsp }
5307 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5308 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
5309 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5310 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5311 0ebf8283 2019-07-24 stsp commit = NULL;
5312 0ebf8283 2019-07-24 stsp if (error)
5313 0ebf8283 2019-07-24 stsp goto done;
5314 0ebf8283 2019-07-24 stsp } else {
5315 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
5316 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5317 0ebf8283 2019-07-24 stsp goto done;
5318 0ebf8283 2019-07-24 stsp }
5319 0ebf8283 2019-07-24 stsp
5320 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
5321 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
5322 0ebf8283 2019-07-24 stsp if (error != NULL)
5323 0ebf8283 2019-07-24 stsp goto done;
5324 0ebf8283 2019-07-24 stsp
5325 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5326 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5327 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
5328 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
5329 c7d20a3f 2019-07-30 stsp goto done;
5330 c7d20a3f 2019-07-30 stsp }
5331 c7d20a3f 2019-07-30 stsp
5332 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
5333 bfce7f83 2019-07-27 stsp got_ref_close(branch);
5334 bfce7f83 2019-07-27 stsp branch = NULL;
5335 0ebf8283 2019-07-24 stsp if (error)
5336 0ebf8283 2019-07-24 stsp goto done;
5337 0ebf8283 2019-07-24 stsp
5338 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5339 0ebf8283 2019-07-24 stsp head_commit_id);
5340 0ebf8283 2019-07-24 stsp if (error)
5341 0ebf8283 2019-07-24 stsp goto done;
5342 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5343 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5344 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
5345 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5346 3aac7cf7 2019-07-25 stsp goto done;
5347 3aac7cf7 2019-07-25 stsp }
5348 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
5349 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
5350 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
5351 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
5352 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5353 0ebf8283 2019-07-24 stsp commit = NULL;
5354 0ebf8283 2019-07-24 stsp if (error)
5355 0ebf8283 2019-07-24 stsp goto done;
5356 0ebf8283 2019-07-24 stsp
5357 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5358 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
5359 bfce7f83 2019-07-27 stsp if (error)
5360 bfce7f83 2019-07-27 stsp goto done;
5361 bfce7f83 2019-07-27 stsp
5362 0ebf8283 2019-07-24 stsp if (edit_script_path) {
5363 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
5364 0ebf8283 2019-07-24 stsp edit_script_path, repo);
5365 6ba2bea2 2019-07-27 stsp if (error) {
5366 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5367 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5368 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5369 0ebf8283 2019-07-24 stsp goto done;
5370 6ba2bea2 2019-07-27 stsp }
5371 0ebf8283 2019-07-24 stsp } else {
5372 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
5373 0ebf8283 2019-07-24 stsp repo);
5374 6ba2bea2 2019-07-27 stsp if (error) {
5375 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5376 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5377 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5378 0ebf8283 2019-07-24 stsp goto done;
5379 6ba2bea2 2019-07-27 stsp }
5380 0ebf8283 2019-07-24 stsp
5381 0ebf8283 2019-07-24 stsp }
5382 0ebf8283 2019-07-24 stsp
5383 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5384 0ebf8283 2019-07-24 stsp repo);
5385 6ba2bea2 2019-07-27 stsp if (error) {
5386 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5387 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5388 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5389 0ebf8283 2019-07-24 stsp goto done;
5390 6ba2bea2 2019-07-27 stsp }
5391 0ebf8283 2019-07-24 stsp
5392 0ebf8283 2019-07-24 stsp }
5393 0ebf8283 2019-07-24 stsp
5394 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5395 0ebf8283 2019-07-24 stsp if (error)
5396 0ebf8283 2019-07-24 stsp goto done;
5397 0ebf8283 2019-07-24 stsp
5398 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5399 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5400 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5401 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5402 0ebf8283 2019-07-24 stsp continue;
5403 0ebf8283 2019-07-24 stsp
5404 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5405 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5406 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5407 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5408 0ebf8283 2019-07-24 stsp repo);
5409 0ebf8283 2019-07-24 stsp } else {
5410 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5411 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5412 0ebf8283 2019-07-24 stsp }
5413 0ebf8283 2019-07-24 stsp if (error)
5414 0ebf8283 2019-07-24 stsp goto done;
5415 0ebf8283 2019-07-24 stsp continue;
5416 0ebf8283 2019-07-24 stsp }
5417 0ebf8283 2019-07-24 stsp
5418 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5419 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5420 0ebf8283 2019-07-24 stsp if (error)
5421 0ebf8283 2019-07-24 stsp goto done;
5422 0ebf8283 2019-07-24 stsp continue;
5423 0ebf8283 2019-07-24 stsp }
5424 0ebf8283 2019-07-24 stsp
5425 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5426 0ebf8283 2019-07-24 stsp hle->commit_id);
5427 0ebf8283 2019-07-24 stsp if (error)
5428 0ebf8283 2019-07-24 stsp goto done;
5429 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5430 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5431 0ebf8283 2019-07-24 stsp
5432 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5433 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5434 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5435 0ebf8283 2019-07-24 stsp if (error)
5436 0ebf8283 2019-07-24 stsp goto done;
5437 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5438 0ebf8283 2019-07-24 stsp commit = NULL;
5439 0ebf8283 2019-07-24 stsp
5440 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5441 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5442 0ebf8283 2019-07-24 stsp break;
5443 0ebf8283 2019-07-24 stsp }
5444 0ebf8283 2019-07-24 stsp
5445 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5446 0ebf8283 2019-07-24 stsp char *id_str;
5447 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5448 0ebf8283 2019-07-24 stsp if (error)
5449 0ebf8283 2019-07-24 stsp goto done;
5450 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5451 0ebf8283 2019-07-24 stsp id_str);
5452 0ebf8283 2019-07-24 stsp free(id_str);
5453 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5454 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5455 3e3a69f1 2019-07-25 stsp fileindex);
5456 0ebf8283 2019-07-24 stsp goto done;
5457 0ebf8283 2019-07-24 stsp }
5458 0ebf8283 2019-07-24 stsp
5459 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5460 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5461 0ebf8283 2019-07-24 stsp if (error)
5462 0ebf8283 2019-07-24 stsp goto done;
5463 0ebf8283 2019-07-24 stsp continue;
5464 0ebf8283 2019-07-24 stsp }
5465 0ebf8283 2019-07-24 stsp
5466 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5467 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5468 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5469 0ebf8283 2019-07-24 stsp if (error)
5470 0ebf8283 2019-07-24 stsp goto done;
5471 0ebf8283 2019-07-24 stsp }
5472 0ebf8283 2019-07-24 stsp
5473 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5474 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5475 0ebf8283 2019-07-24 stsp if (error)
5476 0ebf8283 2019-07-24 stsp goto done;
5477 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5478 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5479 0ebf8283 2019-07-24 stsp } else
5480 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5481 3e3a69f1 2019-07-25 stsp branch, repo);
5482 0ebf8283 2019-07-24 stsp done:
5483 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5484 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5485 0ebf8283 2019-07-24 stsp free(head_commit_id);
5486 0ebf8283 2019-07-24 stsp free(base_commit_id);
5487 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5488 0ebf8283 2019-07-24 stsp if (commit)
5489 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5490 0ebf8283 2019-07-24 stsp if (branch)
5491 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5492 0ebf8283 2019-07-24 stsp if (tmp_branch)
5493 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5494 0ebf8283 2019-07-24 stsp if (worktree)
5495 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5496 715dc77e 2019-08-03 stsp if (repo)
5497 715dc77e 2019-08-03 stsp got_repo_close(repo);
5498 715dc77e 2019-08-03 stsp return error;
5499 715dc77e 2019-08-03 stsp }
5500 715dc77e 2019-08-03 stsp
5501 715dc77e 2019-08-03 stsp __dead static void
5502 715dc77e 2019-08-03 stsp usage_stage(void)
5503 715dc77e 2019-08-03 stsp {
5504 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5505 f3055044 2019-08-07 stsp "[file-path ...]\n",
5506 715dc77e 2019-08-03 stsp getprogname());
5507 715dc77e 2019-08-03 stsp exit(1);
5508 715dc77e 2019-08-03 stsp }
5509 715dc77e 2019-08-03 stsp
5510 715dc77e 2019-08-03 stsp static const struct got_error *
5511 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
5512 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
5513 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5514 408b4ebc 2019-08-03 stsp {
5515 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
5516 408b4ebc 2019-08-03 stsp char *id_str = NULL;
5517 408b4ebc 2019-08-03 stsp
5518 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
5519 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
5520 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
5521 408b4ebc 2019-08-03 stsp return NULL;
5522 408b4ebc 2019-08-03 stsp
5523 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
5524 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
5525 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
5526 408b4ebc 2019-08-03 stsp else
5527 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
5528 408b4ebc 2019-08-03 stsp if (err)
5529 408b4ebc 2019-08-03 stsp return err;
5530 408b4ebc 2019-08-03 stsp
5531 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
5532 408b4ebc 2019-08-03 stsp free(id_str);
5533 dc424a06 2019-08-07 stsp return NULL;
5534 dc424a06 2019-08-07 stsp }
5535 2e1f37b0 2019-08-08 stsp
5536 dc424a06 2019-08-07 stsp static const struct got_error *
5537 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
5538 715dc77e 2019-08-03 stsp {
5539 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
5540 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
5541 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
5542 715dc77e 2019-08-03 stsp char *cwd = NULL;
5543 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
5544 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
5545 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
5546 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
5547 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5548 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5549 715dc77e 2019-08-03 stsp
5550 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
5551 715dc77e 2019-08-03 stsp
5552 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5553 715dc77e 2019-08-03 stsp switch (ch) {
5554 408b4ebc 2019-08-03 stsp case 'l':
5555 408b4ebc 2019-08-03 stsp list_stage = 1;
5556 408b4ebc 2019-08-03 stsp break;
5557 dc424a06 2019-08-07 stsp case 'p':
5558 dc424a06 2019-08-07 stsp pflag = 1;
5559 dc424a06 2019-08-07 stsp break;
5560 dc424a06 2019-08-07 stsp case 'F':
5561 dc424a06 2019-08-07 stsp patch_script_path = optarg;
5562 dc424a06 2019-08-07 stsp break;
5563 715dc77e 2019-08-03 stsp default:
5564 715dc77e 2019-08-03 stsp usage_stage();
5565 715dc77e 2019-08-03 stsp /* NOTREACHED */
5566 715dc77e 2019-08-03 stsp }
5567 715dc77e 2019-08-03 stsp }
5568 715dc77e 2019-08-03 stsp
5569 715dc77e 2019-08-03 stsp argc -= optind;
5570 715dc77e 2019-08-03 stsp argv += optind;
5571 715dc77e 2019-08-03 stsp
5572 715dc77e 2019-08-03 stsp #ifndef PROFILE
5573 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5574 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
5575 715dc77e 2019-08-03 stsp err(1, "pledge");
5576 715dc77e 2019-08-03 stsp #endif
5577 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
5578 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
5579 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
5580 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
5581 715dc77e 2019-08-03 stsp
5582 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
5583 715dc77e 2019-08-03 stsp if (cwd == NULL) {
5584 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
5585 715dc77e 2019-08-03 stsp goto done;
5586 715dc77e 2019-08-03 stsp }
5587 715dc77e 2019-08-03 stsp
5588 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5589 715dc77e 2019-08-03 stsp if (error)
5590 715dc77e 2019-08-03 stsp goto done;
5591 715dc77e 2019-08-03 stsp
5592 715dc77e 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5593 715dc77e 2019-08-03 stsp if (error != NULL)
5594 715dc77e 2019-08-03 stsp goto done;
5595 715dc77e 2019-08-03 stsp
5596 dc424a06 2019-08-07 stsp if (patch_script_path) {
5597 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
5598 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
5599 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
5600 dc424a06 2019-08-07 stsp patch_script_path);
5601 dc424a06 2019-08-07 stsp goto done;
5602 dc424a06 2019-08-07 stsp }
5603 dc424a06 2019-08-07 stsp }
5604 715dc77e 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5605 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
5606 715dc77e 2019-08-03 stsp if (error)
5607 715dc77e 2019-08-03 stsp goto done;
5608 715dc77e 2019-08-03 stsp
5609 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5610 6d022e97 2019-08-04 stsp if (error)
5611 6d022e97 2019-08-04 stsp goto done;
5612 715dc77e 2019-08-03 stsp
5613 6d022e97 2019-08-04 stsp if (list_stage)
5614 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
5615 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
5616 2e1f37b0 2019-08-08 stsp else {
5617 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5618 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
5619 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
5620 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
5621 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5622 2e1f37b0 2019-08-08 stsp }
5623 ad493afc 2019-08-03 stsp done:
5624 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5625 2e1f37b0 2019-08-08 stsp error == NULL)
5626 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5627 ad493afc 2019-08-03 stsp if (repo)
5628 ad493afc 2019-08-03 stsp got_repo_close(repo);
5629 ad493afc 2019-08-03 stsp if (worktree)
5630 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
5631 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5632 ad493afc 2019-08-03 stsp free((char *)pe->path);
5633 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
5634 ad493afc 2019-08-03 stsp free(cwd);
5635 ad493afc 2019-08-03 stsp return error;
5636 ad493afc 2019-08-03 stsp }
5637 ad493afc 2019-08-03 stsp
5638 ad493afc 2019-08-03 stsp __dead static void
5639 ad493afc 2019-08-03 stsp usage_unstage(void)
5640 ad493afc 2019-08-03 stsp {
5641 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5642 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
5643 ad493afc 2019-08-03 stsp getprogname());
5644 ad493afc 2019-08-03 stsp exit(1);
5645 ad493afc 2019-08-03 stsp }
5646 ad493afc 2019-08-03 stsp
5647 ad493afc 2019-08-03 stsp
5648 ad493afc 2019-08-03 stsp static const struct got_error *
5649 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
5650 ad493afc 2019-08-03 stsp {
5651 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
5652 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
5653 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
5654 ad493afc 2019-08-03 stsp char *cwd = NULL;
5655 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
5656 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
5657 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
5658 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
5659 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
5660 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
5661 ad493afc 2019-08-03 stsp
5662 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
5663 ad493afc 2019-08-03 stsp
5664 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
5665 ad493afc 2019-08-03 stsp switch (ch) {
5666 2e1f37b0 2019-08-08 stsp case 'p':
5667 2e1f37b0 2019-08-08 stsp pflag = 1;
5668 2e1f37b0 2019-08-08 stsp break;
5669 2e1f37b0 2019-08-08 stsp case 'F':
5670 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
5671 2e1f37b0 2019-08-08 stsp break;
5672 ad493afc 2019-08-03 stsp default:
5673 ad493afc 2019-08-03 stsp usage_unstage();
5674 ad493afc 2019-08-03 stsp /* NOTREACHED */
5675 ad493afc 2019-08-03 stsp }
5676 ad493afc 2019-08-03 stsp }
5677 ad493afc 2019-08-03 stsp
5678 ad493afc 2019-08-03 stsp argc -= optind;
5679 ad493afc 2019-08-03 stsp argv += optind;
5680 ad493afc 2019-08-03 stsp
5681 ad493afc 2019-08-03 stsp #ifndef PROFILE
5682 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5683 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
5684 ad493afc 2019-08-03 stsp err(1, "pledge");
5685 ad493afc 2019-08-03 stsp #endif
5686 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
5687 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5688 2e1f37b0 2019-08-08 stsp
5689 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
5690 ad493afc 2019-08-03 stsp if (cwd == NULL) {
5691 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
5692 ad493afc 2019-08-03 stsp goto done;
5693 ad493afc 2019-08-03 stsp }
5694 ad493afc 2019-08-03 stsp
5695 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
5696 ad493afc 2019-08-03 stsp if (error)
5697 ad493afc 2019-08-03 stsp goto done;
5698 ad493afc 2019-08-03 stsp
5699 ad493afc 2019-08-03 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5700 ad493afc 2019-08-03 stsp if (error != NULL)
5701 ad493afc 2019-08-03 stsp goto done;
5702 ad493afc 2019-08-03 stsp
5703 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
5704 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5705 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
5706 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
5707 2e1f37b0 2019-08-08 stsp patch_script_path);
5708 2e1f37b0 2019-08-08 stsp goto done;
5709 2e1f37b0 2019-08-08 stsp }
5710 2e1f37b0 2019-08-08 stsp }
5711 2e1f37b0 2019-08-08 stsp
5712 ad493afc 2019-08-03 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5713 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
5714 ad493afc 2019-08-03 stsp if (error)
5715 ad493afc 2019-08-03 stsp goto done;
5716 ad493afc 2019-08-03 stsp
5717 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5718 ad493afc 2019-08-03 stsp if (error)
5719 ad493afc 2019-08-03 stsp goto done;
5720 ad493afc 2019-08-03 stsp
5721 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5722 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
5723 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
5724 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5725 715dc77e 2019-08-03 stsp done:
5726 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5727 2e1f37b0 2019-08-08 stsp error == NULL)
5728 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5729 0ebf8283 2019-07-24 stsp if (repo)
5730 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5731 715dc77e 2019-08-03 stsp if (worktree)
5732 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
5733 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
5734 715dc77e 2019-08-03 stsp free((char *)pe->path);
5735 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
5736 715dc77e 2019-08-03 stsp free(cwd);
5737 0ebf8283 2019-07-24 stsp return error;
5738 0ebf8283 2019-07-24 stsp }