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