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>
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.
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.
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>
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>
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"
50 5c860e29 2018-03-12 stsp #ifndef nitems
51 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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;
58 99437157 2018-11-11 stsp catch_sigint(int signo)
60 99437157 2018-11-11 stsp sigint_received = 1;
64 99437157 2018-11-11 stsp catch_sigpipe(int signo)
66 99437157 2018-11-11 stsp sigpipe_received = 1;
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;
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);
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 *[]);
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" },
140 ce5b7c56 2019-07-09 stsp static void
141 ce5b7c56 2019-07-09 stsp list_commands(void)
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);
150 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
154 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
156 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
157 5c860e29 2018-03-12 stsp unsigned int i;
159 1b6b95a8 2018-03-12 stsp int hflag = 0;
161 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
163 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
164 5c860e29 2018-03-12 stsp switch (ch) {
169 ce5b7c56 2019-07-09 stsp usage(hflag);
170 5c860e29 2018-03-12 stsp /* NOTREACHED */
174 5c860e29 2018-03-12 stsp argc -= optind;
175 5c860e29 2018-03-12 stsp argv += optind;
176 1e70621d 2018-03-27 stsp optind = 0;
178 5c860e29 2018-03-12 stsp if (argc <= 0)
179 ce5b7c56 2019-07-09 stsp usage(hflag);
181 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
182 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
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;
187 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
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)
194 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
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);
205 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
206 ce5b7c56 2019-07-09 stsp list_commands();
210 4ed7e80c 2018-05-20 stsp __dead static void
211 ce5b7c56 2019-07-09 stsp usage(int hflag)
213 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
215 ce5b7c56 2019-07-09 stsp list_commands();
219 0266afb7 2019-01-04 stsp static const struct got_error *
220 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
222 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
223 e2ba3d07 2019-05-13 stsp const char *editor;
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");
229 0ee7065d 2019-05-13 stsp if (editor) {
230 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
232 0ee7065d 2019-05-13 stsp return err;
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");
241 e2ba3d07 2019-05-13 stsp return NULL;
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)
248 163ce85a 2019-05-13 stsp const struct got_error *err;
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");
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);
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);
260 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
261 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
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;
267 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
268 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
270 0266afb7 2019-01-04 stsp return NULL;
273 2c7829a4 2019-06-17 stsp __dead static void
274 2c7829a4 2019-06-17 stsp usage_init(void)
276 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
280 2c7829a4 2019-06-17 stsp static const struct got_error *
281 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
283 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
284 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
287 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
288 2c7829a4 2019-06-17 stsp switch (ch) {
290 2c7829a4 2019-06-17 stsp usage_init();
291 2c7829a4 2019-06-17 stsp /* NOTREACHED */
295 2c7829a4 2019-06-17 stsp argc -= optind;
296 2c7829a4 2019-06-17 stsp argv += optind;
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");
302 2c7829a4 2019-06-17 stsp if (argc != 1)
303 bc20e173 2019-06-17 stsp usage_init();
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");
309 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
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))
316 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
320 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
321 2c7829a4 2019-06-17 stsp if (error != NULL)
325 3ce1b845 2019-07-15 stsp free(repo_path);
326 3ce1b845 2019-07-15 stsp return error;
329 3ce1b845 2019-07-15 stsp __dead static void
330 3ce1b845 2019-07-15 stsp usage_import(void)
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());
338 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
341 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
342 3ce1b845 2019-07-15 stsp int st = -1;
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);
348 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
350 3ce1b845 2019-07-15 stsp goto doneediting;
352 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
353 3ce1b845 2019-07-15 stsp _exit(127);
356 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
357 3ce1b845 2019-07-15 stsp if (errno != EINTR)
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);
365 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
366 3ce1b845 2019-07-15 stsp errno = EINTR;
370 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
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)
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;
381 3ce1b845 2019-07-15 stsp int content_changed = 0;
382 3ce1b845 2019-07-15 stsp size_t len;
384 3ce1b845 2019-07-15 stsp *logmsg = NULL;
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);
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");
392 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
393 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
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");
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';
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");
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);
417 3ce1b845 2019-07-15 stsp fclose(fp);
419 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
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");
429 3ce1b845 2019-07-15 stsp free(*logmsg);
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
432 3ce1b845 2019-07-15 stsp return err;
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)
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;
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");
448 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
452 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
455 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
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;
462 3ce1b845 2019-07-15 stsp static const struct got_error *
463 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
465 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
466 3ce1b845 2019-07-15 stsp return NULL;
469 3ce1b845 2019-07-15 stsp static const struct got_error *
470 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
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;
482 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
483 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
485 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
487 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
488 3ce1b845 2019-07-15 stsp switch (ch) {
490 3ce1b845 2019-07-15 stsp branch_name = optarg;
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");
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");
507 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
509 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
515 3ce1b845 2019-07-15 stsp usage_init();
516 3ce1b845 2019-07-15 stsp /* NOTREACHED */
520 3ce1b845 2019-07-15 stsp argc -= optind;
521 3ce1b845 2019-07-15 stsp argv += optind;
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");
528 3ce1b845 2019-07-15 stsp if (argc != 1)
529 3ce1b845 2019-07-15 stsp usage_import();
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);
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");
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);
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");
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)
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");
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");
567 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
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.
573 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
574 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
577 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
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);
585 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
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);
594 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
598 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
602 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
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)
611 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
612 3ce1b845 2019-07-15 stsp branch_ref);
616 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
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);
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;
636 4ed7e80c 2018-05-20 stsp __dead static void
637 c09a553d 2018-03-12 stsp usage_checkout(void)
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());
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)
647 92a684f4 2018-03-12 stsp char *worktree_path = arg;
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;
653 92a684f4 2018-03-12 stsp while (path[0] == '/')
656 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
657 1ee397ad 2019-07-12 stsp return NULL;
660 99437157 2018-11-11 stsp static const struct got_error *
661 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
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;
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)
672 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
673 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
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);
678 36a38700 2019-05-10 stsp return err;
680 d5bea539 2019-05-13 stsp if (yca_id == NULL)
681 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
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.
687 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
689 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
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)
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);
705 d5bea539 2019-05-13 stsp free(yca_id);
706 d5bea539 2019-05-13 stsp return NULL;
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)
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;
718 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
722 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
726 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
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);
734 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
735 a367ff0f 2019-05-14 stsp err = NULL;
737 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
739 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
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;
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;
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)
765 04f57cb3 2019-07-25 stsp const struct got_error *err;
766 04f57cb3 2019-07-25 stsp struct got_reference *ref;
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);
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);
778 a367ff0f 2019-05-14 stsp return err;
781 4ed7e80c 2018-05-20 stsp static const struct got_error *
782 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
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;
795 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
796 0bb8a95e 2018-03-12 stsp switch (ch) {
798 08573d5b 2019-05-14 stsp branch_name = optarg;
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");
806 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
809 2deda0b9 2019-03-07 stsp usage_checkout();
810 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
814 0bb8a95e 2018-03-12 stsp argc -= optind;
815 0bb8a95e 2018-03-12 stsp argv += optind;
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");
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");
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);
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",
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");
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]);
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",
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");
876 c09a553d 2018-03-12 stsp usage_checkout();
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);
881 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
882 c09a553d 2018-03-12 stsp if (error != NULL)
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))
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);
897 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
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)
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))
909 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
910 c09a553d 2018-03-12 stsp if (error != NULL)
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)
917 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
918 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
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);
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);
933 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
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);
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)
948 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
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;
957 507dc3bb 2018-12-29 stsp __dead static void
958 507dc3bb 2018-12-29 stsp usage_update(void)
960 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
961 507dc3bb 2018-12-29 stsp getprogname());
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)
968 784955db 2019-01-12 stsp int *did_something = arg;
970 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
971 1ee397ad 2019-07-12 stsp return NULL;
973 1545c615 2019-02-10 stsp *did_something = 1;
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;
979 507dc3bb 2018-12-29 stsp while (path[0] == '/')
981 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
982 1ee397ad 2019-07-12 stsp return NULL;
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)
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;
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);
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;
1010 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1011 a1fb16d8 2019-05-24 stsp return NULL;
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));
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;
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)
1028 0ebf8283 2019-07-24 stsp const struct got_error *err;
1029 0ebf8283 2019-07-24 stsp int in_progress;
1031 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
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);
1037 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
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);
1043 a1fb16d8 2019-05-24 stsp return NULL;
1046 a1fb16d8 2019-05-24 stsp static const struct got_error *
1047 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1049 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1050 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1051 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1052 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
1053 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1054 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1055 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1056 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1057 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1059 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1060 507dc3bb 2018-12-29 stsp switch (ch) {
1062 024e9686 2019-05-14 stsp branch_name = optarg;
1065 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1066 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1067 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1070 2deda0b9 2019-03-07 stsp usage_update();
1071 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1075 507dc3bb 2018-12-29 stsp argc -= optind;
1076 507dc3bb 2018-12-29 stsp argv += optind;
1078 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1079 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1080 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1081 507dc3bb 2018-12-29 stsp err(1, "pledge");
1083 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1084 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1085 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1086 c4cdcb68 2019-04-03 stsp goto done;
1088 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1089 7d5807f4 2019-07-11 stsp if (error)
1090 7d5807f4 2019-07-11 stsp goto done;
1092 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1093 c4cdcb68 2019-04-03 stsp if (error)
1094 7d5807f4 2019-07-11 stsp goto done;
1096 507dc3bb 2018-12-29 stsp if (argc == 0) {
1097 c4cdcb68 2019-04-03 stsp path = strdup("");
1098 c4cdcb68 2019-04-03 stsp if (path == NULL) {
1099 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1100 507dc3bb 2018-12-29 stsp goto done;
1102 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
1103 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1104 c4cdcb68 2019-04-03 stsp if (error)
1105 507dc3bb 2018-12-29 stsp goto done;
1107 507dc3bb 2018-12-29 stsp usage_update();
1109 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1110 507dc3bb 2018-12-29 stsp if (error != NULL)
1111 507dc3bb 2018-12-29 stsp goto done;
1113 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1114 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1115 0266afb7 2019-01-04 stsp if (error)
1116 0266afb7 2019-01-04 stsp goto done;
1118 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1119 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1120 024e9686 2019-05-14 stsp if (error != NULL)
1121 024e9686 2019-05-14 stsp goto done;
1122 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1123 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1124 9c4b8182 2019-01-02 stsp if (error != NULL)
1125 9c4b8182 2019-01-02 stsp goto done;
1126 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1127 507dc3bb 2018-12-29 stsp if (error != NULL)
1128 507dc3bb 2018-12-29 stsp goto done;
1130 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1131 04f57cb3 2019-07-25 stsp free(commit_id_str);
1132 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1133 30837e32 2019-07-25 stsp if (error)
1134 a297e751 2019-07-11 stsp goto done;
1135 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1136 a297e751 2019-07-11 stsp if (error)
1137 507dc3bb 2018-12-29 stsp goto done;
1140 a1fb16d8 2019-05-24 stsp if (branch_name) {
1141 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1142 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
1143 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
1144 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
1145 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
1146 024e9686 2019-05-14 stsp getprogname(), path);
1147 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
1148 024e9686 2019-05-14 stsp goto done;
1150 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1151 024e9686 2019-05-14 stsp if (error)
1152 024e9686 2019-05-14 stsp goto done;
1153 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1154 a367ff0f 2019-05-14 stsp free(head_commit_id);
1155 024e9686 2019-05-14 stsp if (error != NULL)
1156 024e9686 2019-05-14 stsp goto done;
1157 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1158 a367ff0f 2019-05-14 stsp if (error)
1159 a367ff0f 2019-05-14 stsp goto done;
1160 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1161 024e9686 2019-05-14 stsp if (error)
1162 024e9686 2019-05-14 stsp goto done;
1164 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1165 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1166 a367ff0f 2019-05-14 stsp if (error != NULL) {
1167 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1168 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1169 024e9686 2019-05-14 stsp goto done;
1171 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1172 a367ff0f 2019-05-14 stsp if (error)
1173 a367ff0f 2019-05-14 stsp goto done;
1176 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1177 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1178 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1179 507dc3bb 2018-12-29 stsp commit_id);
1180 507dc3bb 2018-12-29 stsp if (error)
1181 507dc3bb 2018-12-29 stsp goto done;
1184 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
1185 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1186 507dc3bb 2018-12-29 stsp if (error != NULL)
1187 507dc3bb 2018-12-29 stsp goto done;
1189 784955db 2019-01-12 stsp if (did_something)
1190 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1192 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1194 c09a553d 2018-03-12 stsp free(worktree_path);
1195 c4cdcb68 2019-04-03 stsp free(path);
1196 507dc3bb 2018-12-29 stsp free(commit_id);
1197 9c4b8182 2019-01-02 stsp free(commit_id_str);
1198 c09a553d 2018-03-12 stsp return error;
1201 f42b1b34 2018-03-12 stsp static const struct got_error *
1202 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1203 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1205 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1206 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1207 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1208 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1209 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1211 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1212 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1214 79109fed 2018-03-27 stsp return err;
1216 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1217 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1218 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1220 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1222 79109fed 2018-03-27 stsp return err;
1224 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1225 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1226 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1228 0f2b3dca 2018-12-22 stsp return err;
1230 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1232 79109fed 2018-03-27 stsp return err;
1235 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1237 0f2b3dca 2018-12-22 stsp goto done;
1239 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1240 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1241 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1242 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1243 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
1245 79109fed 2018-03-27 stsp if (tree1)
1246 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1247 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1248 0f2b3dca 2018-12-22 stsp free(id_str1);
1249 0f2b3dca 2018-12-22 stsp free(id_str2);
1250 79109fed 2018-03-27 stsp return err;
1253 4bb494d5 2018-06-16 stsp static char *
1254 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1256 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1257 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1259 6c281f94 2018-06-11 stsp *p = '\0';
1263 79109fed 2018-03-27 stsp static const struct got_error *
1264 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1265 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1266 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1268 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1269 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1270 6c281f94 2018-06-11 stsp char datebuf[26];
1271 45d799e2 2018-12-23 stsp time_t committer_time;
1272 45d799e2 2018-12-23 stsp const char *author, *committer;
1273 199a4027 2019-02-02 stsp char *refs_str = NULL;
1274 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1276 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1278 199a4027 2019-02-02 stsp const char *name;
1279 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1281 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1282 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1284 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1285 199a4027 2019-02-02 stsp name += 5;
1286 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1288 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1289 e34f9ed6 2019-02-02 stsp name += 6;
1290 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1291 141c2bff 2019-02-04 stsp name += 8;
1292 199a4027 2019-02-02 stsp s = refs_str;
1293 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1294 199a4027 2019-02-02 stsp name) == -1) {
1295 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1301 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1303 8bf5b3c9 2018-03-17 stsp return err;
1305 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1306 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1307 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1308 832c249c 2018-06-10 stsp free(id_str);
1309 d3d493d7 2019-02-21 stsp id_str = NULL;
1310 d3d493d7 2019-02-21 stsp free(refs_str);
1311 d3d493d7 2019-02-21 stsp refs_str = NULL;
1312 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1313 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1314 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1315 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1316 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1317 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1318 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1319 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1320 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1321 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1322 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1323 3fe1abad 2018-06-10 stsp int n = 1;
1324 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1325 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1326 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1328 a0603db2 2018-06-10 stsp return err;
1329 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1330 a0603db2 2018-06-10 stsp free(id_str);
1334 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1335 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1336 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1338 621015ac 2018-07-23 stsp logmsg = logmsg0;
1340 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1342 832c249c 2018-06-10 stsp printf(" %s\n", line);
1343 832c249c 2018-06-10 stsp } while (line);
1344 621015ac 2018-07-23 stsp free(logmsg0);
1346 971751ac 2018-03-27 stsp if (show_patch) {
1347 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1348 971751ac 2018-03-27 stsp if (err == 0)
1349 971751ac 2018-03-27 stsp printf("\n");
1352 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1353 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1354 07862c20 2018-09-15 stsp return err;
1357 f42b1b34 2018-03-12 stsp static const struct got_error *
1358 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1359 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1360 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1362 f42b1b34 2018-03-12 stsp const struct got_error *err;
1363 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1365 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1366 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1368 f42b1b34 2018-03-12 stsp return err;
1369 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1371 fcc85cad 2018-07-22 stsp goto done;
1373 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1374 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1376 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1379 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1380 372ccdbb 2018-06-10 stsp if (err) {
1381 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1382 9ba79e04 2018-06-11 stsp err = NULL;
1385 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1387 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1393 b43fbaa0 2018-06-11 stsp if (id == NULL)
1396 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1399 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1401 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1402 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1406 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1407 f42b1b34 2018-03-12 stsp return err;
1410 4ed7e80c 2018-05-20 stsp __dead static void
1411 6f3d1eb0 2018-03-12 stsp usage_log(void)
1413 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1414 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1418 4ed7e80c 2018-05-20 stsp static const struct got_error *
1419 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1421 f42b1b34 2018-03-12 stsp const struct got_error *error;
1422 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1423 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1424 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1425 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1426 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1427 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1428 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1429 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1430 64a96a6d 2018-04-01 stsp const char *errstr;
1431 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1433 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1435 6715a751 2018-03-16 stsp #ifndef PROFILE
1436 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1439 f42b1b34 2018-03-12 stsp err(1, "pledge");
1442 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1443 79109fed 2018-03-27 stsp switch (ch) {
1445 79109fed 2018-03-27 stsp show_patch = 1;
1448 d142fc45 2018-04-01 stsp start_commit = optarg;
1451 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1453 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1454 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1457 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1458 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1459 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1462 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1465 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1466 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1467 04ca23f4 2018-07-16 stsp err(1, "-r option");
1468 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1471 2deda0b9 2019-03-07 stsp usage_log();
1472 79109fed 2018-03-27 stsp /* NOTREACHED */
1476 79109fed 2018-03-27 stsp argc -= optind;
1477 79109fed 2018-03-27 stsp argv += optind;
1479 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1480 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1481 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1482 04ca23f4 2018-07-16 stsp goto done;
1485 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1486 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1487 cffc0aa4 2019-02-05 stsp goto done;
1488 cffc0aa4 2019-02-05 stsp error = NULL;
1490 e7301579 2019-03-18 stsp if (argc == 0) {
1491 cbd1af7a 2019-03-18 stsp path = strdup("");
1492 e7301579 2019-03-18 stsp if (path == NULL) {
1493 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1494 cbd1af7a 2019-03-18 stsp goto done;
1496 e7301579 2019-03-18 stsp } else if (argc == 1) {
1497 e7301579 2019-03-18 stsp if (worktree) {
1498 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1500 e7301579 2019-03-18 stsp if (error)
1501 e7301579 2019-03-18 stsp goto done;
1503 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1504 e7301579 2019-03-18 stsp if (path == NULL) {
1505 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1506 e7301579 2019-03-18 stsp goto done;
1510 cbd1af7a 2019-03-18 stsp usage_log();
1512 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1513 5486daa2 2019-05-11 stsp repo_path = worktree ?
1514 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1516 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1517 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1518 cffc0aa4 2019-02-05 stsp goto done;
1521 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1522 d7d4f210 2018-03-12 stsp if (error != NULL)
1523 04ca23f4 2018-07-16 stsp goto done;
1525 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1526 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1527 c02c541e 2019-03-29 stsp if (error)
1528 c02c541e 2019-03-29 stsp goto done;
1530 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1531 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1532 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1533 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1534 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1535 3235492e 2018-04-01 stsp if (error != NULL)
1536 3235492e 2018-04-01 stsp return error;
1537 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1538 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1539 3235492e 2018-04-01 stsp if (error != NULL)
1540 3235492e 2018-04-01 stsp return error;
1541 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1543 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1544 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1545 3235492e 2018-04-01 stsp if (error == NULL) {
1546 1caad61b 2019-02-01 stsp int obj_type;
1547 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1548 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1549 d1f2edc9 2018-06-13 stsp if (error != NULL)
1550 1caad61b 2019-02-01 stsp goto done;
1551 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1552 1caad61b 2019-02-01 stsp if (error != NULL)
1553 1caad61b 2019-02-01 stsp goto done;
1554 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1555 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1556 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1557 1caad61b 2019-02-01 stsp if (error != NULL)
1558 1caad61b 2019-02-01 stsp goto done;
1559 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1560 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1561 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1562 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1563 1caad61b 2019-02-01 stsp goto done;
1566 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1567 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1568 1caad61b 2019-02-01 stsp if (id == NULL)
1569 638f9024 2019-05-13 stsp error = got_error_from_errno(
1570 230a42bd 2019-05-11 jcs "got_object_id_dup");
1571 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1572 1caad61b 2019-02-01 stsp if (error)
1573 1caad61b 2019-02-01 stsp goto done;
1574 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1575 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1576 1caad61b 2019-02-01 stsp goto done;
1578 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1579 d1f2edc9 2018-06-13 stsp if (error != NULL)
1580 1caad61b 2019-02-01 stsp goto done;
1582 15a94983 2018-12-23 stsp if (commit == NULL) {
1583 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1584 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1585 d1f2edc9 2018-06-13 stsp if (error != NULL)
1586 d1f2edc9 2018-06-13 stsp return error;
1589 d7d4f210 2018-03-12 stsp if (error != NULL)
1590 04ca23f4 2018-07-16 stsp goto done;
1592 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1593 04ca23f4 2018-07-16 stsp if (error != NULL)
1594 04ca23f4 2018-07-16 stsp goto done;
1595 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1596 04ca23f4 2018-07-16 stsp free(path);
1597 04ca23f4 2018-07-16 stsp path = in_repo_path;
1600 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1601 199a4027 2019-02-02 stsp if (error)
1602 199a4027 2019-02-02 stsp goto done;
1604 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1605 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1607 04ca23f4 2018-07-16 stsp free(path);
1608 04ca23f4 2018-07-16 stsp free(repo_path);
1609 04ca23f4 2018-07-16 stsp free(cwd);
1611 cffc0aa4 2019-02-05 stsp if (worktree)
1612 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1613 ad242220 2018-09-08 stsp if (repo) {
1614 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1615 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1616 ad242220 2018-09-08 stsp if (error == NULL)
1617 ad242220 2018-09-08 stsp error = repo_error;
1619 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1620 8bf5b3c9 2018-03-17 stsp return error;
1623 4ed7e80c 2018-05-20 stsp __dead static void
1624 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1626 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1627 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1631 b72f483a 2019-02-05 stsp struct print_diff_arg {
1632 b72f483a 2019-02-05 stsp struct got_repository *repo;
1633 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1634 b72f483a 2019-02-05 stsp int diff_context;
1635 3fc0c068 2019-02-10 stsp const char *id_str;
1636 3fc0c068 2019-02-10 stsp int header_shown;
1639 4ed7e80c 2018-05-20 stsp static const struct got_error *
1640 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1641 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1643 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1644 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1645 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1646 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1647 b72f483a 2019-02-05 stsp char *abspath = NULL;
1648 b72f483a 2019-02-05 stsp struct stat sb;
1650 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1651 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1652 b72f483a 2019-02-05 stsp return NULL;
1654 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1655 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1656 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1657 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1660 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1661 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1663 d00136be 2019-03-26 stsp goto done;
1667 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1668 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1669 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1670 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1671 2ec1f75b 2019-03-26 stsp goto done;
1674 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1675 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1676 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1677 2ec1f75b 2019-03-26 stsp goto done;
1679 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1680 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1681 2ec1f75b 2019-03-26 stsp goto done;
1684 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1686 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1689 b72f483a 2019-02-05 stsp if (blob1)
1690 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1691 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1693 b72f483a 2019-02-05 stsp free(abspath);
1694 927df6b7 2019-02-10 stsp return err;
1697 927df6b7 2019-02-10 stsp static const struct got_error *
1698 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1700 b00d56cd 2018-04-01 stsp const struct got_error *error;
1701 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1702 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1703 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1704 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1705 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1706 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1707 15a94983 2018-12-23 stsp int type1, type2;
1708 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1709 c0cc5c62 2018-10-18 stsp const char *errstr;
1710 927df6b7 2019-02-10 stsp char *path = NULL;
1712 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1713 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1714 25eccc22 2019-01-04 stsp NULL) == -1)
1715 b00d56cd 2018-04-01 stsp err(1, "pledge");
1718 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1719 b00d56cd 2018-04-01 stsp switch (ch) {
1721 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1722 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1723 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1726 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1727 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1728 b72f483a 2019-02-05 stsp err(1, "-r option");
1729 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1732 2deda0b9 2019-03-07 stsp usage_diff();
1733 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1737 b00d56cd 2018-04-01 stsp argc -= optind;
1738 b00d56cd 2018-04-01 stsp argv += optind;
1740 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1741 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1742 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1743 b72f483a 2019-02-05 stsp goto done;
1745 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1746 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1747 927df6b7 2019-02-10 stsp goto done;
1748 927df6b7 2019-02-10 stsp if (argc <= 1) {
1749 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1750 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1751 927df6b7 2019-02-10 stsp goto done;
1753 b72f483a 2019-02-05 stsp if (repo_path)
1755 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1756 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1757 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1758 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1759 927df6b7 2019-02-10 stsp goto done;
1761 927df6b7 2019-02-10 stsp if (argc == 1) {
1762 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1764 927df6b7 2019-02-10 stsp if (error)
1765 927df6b7 2019-02-10 stsp goto done;
1767 927df6b7 2019-02-10 stsp path = strdup("");
1768 927df6b7 2019-02-10 stsp if (path == NULL) {
1769 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1770 927df6b7 2019-02-10 stsp goto done;
1773 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1774 15a94983 2018-12-23 stsp id_str1 = argv[0];
1775 15a94983 2018-12-23 stsp id_str2 = argv[1];
1776 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1777 30db809c 2019-06-05 stsp repo_path =
1778 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1779 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1780 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1781 30db809c 2019-06-05 stsp goto done;
1785 b00d56cd 2018-04-01 stsp usage_diff();
1787 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1788 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1789 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1790 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1793 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1794 76089277 2018-04-01 stsp free(repo_path);
1795 b00d56cd 2018-04-01 stsp if (error != NULL)
1796 b00d56cd 2018-04-01 stsp goto done;
1798 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1799 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1800 c02c541e 2019-03-29 stsp if (error)
1801 c02c541e 2019-03-29 stsp goto done;
1803 30db809c 2019-06-05 stsp if (argc <= 1) {
1804 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1805 b72f483a 2019-02-05 stsp char *id_str;
1806 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1807 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1808 b72f483a 2019-02-05 stsp if (error)
1809 b72f483a 2019-02-05 stsp goto done;
1810 b72f483a 2019-02-05 stsp arg.repo = repo;
1811 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1812 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1813 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1814 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1816 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1817 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1818 b72f483a 2019-02-05 stsp free(id_str);
1819 b72f483a 2019-02-05 stsp goto done;
1822 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1823 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1824 e02e74af 2019-05-28 stsp if (error) {
1825 e02e74af 2019-05-28 stsp struct got_reference *ref;
1826 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1827 e02e74af 2019-05-28 stsp goto done;
1828 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1829 e02e74af 2019-05-28 stsp if (error != NULL)
1830 e02e74af 2019-05-28 stsp goto done;
1831 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1832 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1833 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1834 5e70831e 2019-05-28 stsp goto done;
1836 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1837 e02e74af 2019-05-28 stsp got_ref_close(ref);
1838 e02e74af 2019-05-28 stsp if (error != NULL)
1839 5e70831e 2019-05-28 stsp goto done;
1841 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1842 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1843 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1844 e02e74af 2019-05-28 stsp goto done;
1848 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1849 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1850 e02e74af 2019-05-28 stsp if (error) {
1851 e02e74af 2019-05-28 stsp struct got_reference *ref;
1852 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1853 e02e74af 2019-05-28 stsp goto done;
1854 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1855 e02e74af 2019-05-28 stsp if (error != NULL)
1856 5e70831e 2019-05-28 stsp goto done;
1857 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1858 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1859 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1860 e02e74af 2019-05-28 stsp goto done;
1862 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1863 e02e74af 2019-05-28 stsp got_ref_close(ref);
1864 e02e74af 2019-05-28 stsp if (error != NULL)
1865 e02e74af 2019-05-28 stsp goto done;
1867 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1868 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1869 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1870 5e70831e 2019-05-28 stsp goto done;
1874 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1875 15a94983 2018-12-23 stsp if (error)
1876 15a94983 2018-12-23 stsp goto done;
1878 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1879 15a94983 2018-12-23 stsp if (error)
1880 15a94983 2018-12-23 stsp goto done;
1882 15a94983 2018-12-23 stsp if (type1 != type2) {
1883 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1884 b00d56cd 2018-04-01 stsp goto done;
1887 15a94983 2018-12-23 stsp switch (type1) {
1888 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1889 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1890 54156555 2018-12-24 stsp diff_context, repo, stdout);
1892 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1893 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1894 54156555 2018-12-24 stsp diff_context, repo, stdout);
1896 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1897 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1898 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1899 c0cc5c62 2018-10-18 stsp repo, stdout);
1902 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1906 5e70831e 2019-05-28 stsp free(label1);
1907 5e70831e 2019-05-28 stsp free(label2);
1908 15a94983 2018-12-23 stsp free(id1);
1909 15a94983 2018-12-23 stsp free(id2);
1910 927df6b7 2019-02-10 stsp free(path);
1911 b72f483a 2019-02-05 stsp if (worktree)
1912 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1913 ad242220 2018-09-08 stsp if (repo) {
1914 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1915 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1916 ad242220 2018-09-08 stsp if (error == NULL)
1917 ad242220 2018-09-08 stsp error = repo_error;
1919 b00d56cd 2018-04-01 stsp return error;
1922 404c43c4 2018-06-21 stsp __dead static void
1923 404c43c4 2018-06-21 stsp usage_blame(void)
1925 9270e621 2019-02-05 stsp fprintf(stderr,
1926 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1927 404c43c4 2018-06-21 stsp getprogname());
1931 404c43c4 2018-06-21 stsp static const struct got_error *
1932 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1934 404c43c4 2018-06-21 stsp const struct got_error *error;
1935 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1936 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1937 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1938 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1939 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1942 404c43c4 2018-06-21 stsp #ifndef PROFILE
1943 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1944 36e2fb66 2019-01-04 stsp NULL) == -1)
1945 404c43c4 2018-06-21 stsp err(1, "pledge");
1948 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1949 404c43c4 2018-06-21 stsp switch (ch) {
1951 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1954 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1955 66bea077 2018-08-02 stsp if (repo_path == NULL)
1956 66bea077 2018-08-02 stsp err(1, "-r option");
1957 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1960 2deda0b9 2019-03-07 stsp usage_blame();
1961 404c43c4 2018-06-21 stsp /* NOTREACHED */
1965 404c43c4 2018-06-21 stsp argc -= optind;
1966 404c43c4 2018-06-21 stsp argv += optind;
1968 a39318fd 2018-08-02 stsp if (argc == 1)
1969 404c43c4 2018-06-21 stsp path = argv[0];
1971 404c43c4 2018-06-21 stsp usage_blame();
1973 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1974 66bea077 2018-08-02 stsp if (cwd == NULL) {
1975 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1976 66bea077 2018-08-02 stsp goto done;
1978 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1979 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1980 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1981 66bea077 2018-08-02 stsp goto done;
1983 0c06baac 2019-02-05 stsp error = NULL;
1984 0c06baac 2019-02-05 stsp if (worktree) {
1985 0c06baac 2019-02-05 stsp repo_path =
1986 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1987 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1988 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1989 0c06baac 2019-02-05 stsp if (error)
1990 0c06baac 2019-02-05 stsp goto done;
1992 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1993 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1994 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1995 0c06baac 2019-02-05 stsp goto done;
2000 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2001 c02c541e 2019-03-29 stsp if (error != NULL)
2002 36e2fb66 2019-01-04 stsp goto done;
2004 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2005 c02c541e 2019-03-29 stsp if (error)
2006 404c43c4 2018-06-21 stsp goto done;
2008 0c06baac 2019-02-05 stsp if (worktree) {
2009 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2010 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2011 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2012 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2013 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2014 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2015 6efaaa2d 2019-02-05 stsp path) == -1) {
2016 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2017 0c06baac 2019-02-05 stsp goto done;
2019 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2022 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2024 0c06baac 2019-02-05 stsp if (error)
2025 66bea077 2018-08-02 stsp goto done;
2027 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2028 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2029 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2030 404c43c4 2018-06-21 stsp if (error != NULL)
2031 66bea077 2018-08-02 stsp goto done;
2032 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2033 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2034 404c43c4 2018-06-21 stsp if (error != NULL)
2035 66bea077 2018-08-02 stsp goto done;
2037 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2038 30837e32 2019-07-25 stsp if (error)
2039 66bea077 2018-08-02 stsp goto done;
2042 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2044 66bea077 2018-08-02 stsp free(in_repo_path);
2045 66bea077 2018-08-02 stsp free(repo_path);
2046 66bea077 2018-08-02 stsp free(cwd);
2047 404c43c4 2018-06-21 stsp free(commit_id);
2048 0c06baac 2019-02-05 stsp if (worktree)
2049 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2050 ad242220 2018-09-08 stsp if (repo) {
2051 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2052 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2053 ad242220 2018-09-08 stsp if (error == NULL)
2054 ad242220 2018-09-08 stsp error = repo_error;
2056 404c43c4 2018-06-21 stsp return error;
2059 5de5890b 2018-10-18 stsp __dead static void
2060 5de5890b 2018-10-18 stsp usage_tree(void)
2062 c1669e2e 2019-01-09 stsp fprintf(stderr,
2063 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2064 5de5890b 2018-10-18 stsp getprogname());
2068 c1669e2e 2019-01-09 stsp static void
2069 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2070 c1669e2e 2019-01-09 stsp const char *root_path)
2072 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2074 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2075 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2078 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2079 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2080 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2083 5de5890b 2018-10-18 stsp static const struct got_error *
2084 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2085 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2086 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2088 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2089 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2090 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2091 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2092 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2094 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2096 5de5890b 2018-10-18 stsp goto done;
2098 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2100 5de5890b 2018-10-18 stsp goto done;
2101 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2102 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2103 5de5890b 2018-10-18 stsp while (te) {
2104 5de5890b 2018-10-18 stsp char *id = NULL;
2106 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2109 5de5890b 2018-10-18 stsp if (show_ids) {
2110 5de5890b 2018-10-18 stsp char *id_str;
2111 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2113 5de5890b 2018-10-18 stsp goto done;
2114 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2115 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2116 5de5890b 2018-10-18 stsp free(id_str);
2117 5de5890b 2018-10-18 stsp goto done;
2119 5de5890b 2018-10-18 stsp free(id_str);
2121 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2124 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2125 c1669e2e 2019-01-09 stsp char *child_path;
2126 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2127 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2128 c1669e2e 2019-01-09 stsp te->name) == -1) {
2129 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2130 c1669e2e 2019-01-09 stsp goto done;
2132 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2133 c1669e2e 2019-01-09 stsp root_path, repo);
2134 c1669e2e 2019-01-09 stsp free(child_path);
2136 c1669e2e 2019-01-09 stsp goto done;
2139 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2143 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2144 5de5890b 2018-10-18 stsp free(tree_id);
2145 5de5890b 2018-10-18 stsp return err;
2148 5de5890b 2018-10-18 stsp static const struct got_error *
2149 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2151 5de5890b 2018-10-18 stsp const struct got_error *error;
2152 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2153 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2154 7a2c19d6 2019-02-05 stsp const char *path;
2155 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2156 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2157 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2158 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2161 5de5890b 2018-10-18 stsp #ifndef PROFILE
2162 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2163 0f8d269b 2019-01-04 stsp NULL) == -1)
2164 5de5890b 2018-10-18 stsp err(1, "pledge");
2167 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2168 5de5890b 2018-10-18 stsp switch (ch) {
2170 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2173 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2174 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2175 5de5890b 2018-10-18 stsp err(1, "-r option");
2176 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2179 5de5890b 2018-10-18 stsp show_ids = 1;
2182 c1669e2e 2019-01-09 stsp recurse = 1;
2185 2deda0b9 2019-03-07 stsp usage_tree();
2186 5de5890b 2018-10-18 stsp /* NOTREACHED */
2190 5de5890b 2018-10-18 stsp argc -= optind;
2191 5de5890b 2018-10-18 stsp argv += optind;
2193 5de5890b 2018-10-18 stsp if (argc == 1)
2194 5de5890b 2018-10-18 stsp path = argv[0];
2195 5de5890b 2018-10-18 stsp else if (argc > 1)
2196 5de5890b 2018-10-18 stsp usage_tree();
2198 7a2c19d6 2019-02-05 stsp path = NULL;
2200 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2201 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2202 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2203 9bf7a39b 2019-02-05 stsp goto done;
2205 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2206 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2207 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2208 7a2c19d6 2019-02-05 stsp goto done;
2210 7a2c19d6 2019-02-05 stsp error = NULL;
2211 7a2c19d6 2019-02-05 stsp if (worktree) {
2212 7a2c19d6 2019-02-05 stsp repo_path =
2213 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2214 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2215 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2216 7a2c19d6 2019-02-05 stsp if (error)
2217 7a2c19d6 2019-02-05 stsp goto done;
2219 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2220 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2221 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2222 7a2c19d6 2019-02-05 stsp goto done;
2227 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2228 5de5890b 2018-10-18 stsp if (error != NULL)
2229 c02c541e 2019-03-29 stsp goto done;
2231 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2232 c02c541e 2019-03-29 stsp if (error)
2233 5de5890b 2018-10-18 stsp goto done;
2235 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2236 9bf7a39b 2019-02-05 stsp if (worktree) {
2237 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2238 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2239 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2240 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2241 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2242 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2243 9bf7a39b 2019-02-05 stsp goto done;
2245 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2247 9bf7a39b 2019-02-05 stsp if (error)
2248 9bf7a39b 2019-02-05 stsp goto done;
2250 9bf7a39b 2019-02-05 stsp path = "/";
2252 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2253 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2254 9bf7a39b 2019-02-05 stsp if (error != NULL)
2255 9bf7a39b 2019-02-05 stsp goto done;
2258 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2259 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2260 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2261 5de5890b 2018-10-18 stsp if (error != NULL)
2262 5de5890b 2018-10-18 stsp goto done;
2263 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2264 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2265 5de5890b 2018-10-18 stsp if (error != NULL)
2266 5de5890b 2018-10-18 stsp goto done;
2268 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2269 30837e32 2019-07-25 stsp if (error)
2270 5de5890b 2018-10-18 stsp goto done;
2273 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2274 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2276 5de5890b 2018-10-18 stsp free(in_repo_path);
2277 5de5890b 2018-10-18 stsp free(repo_path);
2278 5de5890b 2018-10-18 stsp free(cwd);
2279 5de5890b 2018-10-18 stsp free(commit_id);
2280 7a2c19d6 2019-02-05 stsp if (worktree)
2281 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2282 5de5890b 2018-10-18 stsp if (repo) {
2283 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2284 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2285 5de5890b 2018-10-18 stsp if (error == NULL)
2286 5de5890b 2018-10-18 stsp error = repo_error;
2288 5de5890b 2018-10-18 stsp return error;
2291 6bad629b 2019-02-04 stsp __dead static void
2292 6bad629b 2019-02-04 stsp usage_status(void)
2294 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
2298 b72f483a 2019-02-05 stsp static const struct got_error *
2299 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2300 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2302 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2303 b72f483a 2019-02-05 stsp return NULL;
2306 6bad629b 2019-02-04 stsp static const struct got_error *
2307 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2309 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2310 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2311 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2312 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
2315 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2316 6bad629b 2019-02-04 stsp switch (ch) {
2318 2deda0b9 2019-03-07 stsp usage_status();
2319 6bad629b 2019-02-04 stsp /* NOTREACHED */
2323 6bad629b 2019-02-04 stsp argc -= optind;
2324 6bad629b 2019-02-04 stsp argv += optind;
2326 6bad629b 2019-02-04 stsp #ifndef PROFILE
2327 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2328 6bad629b 2019-02-04 stsp NULL) == -1)
2329 6bad629b 2019-02-04 stsp err(1, "pledge");
2331 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2332 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2333 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2334 927df6b7 2019-02-10 stsp goto done;
2337 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2338 927df6b7 2019-02-10 stsp if (error != NULL)
2339 927df6b7 2019-02-10 stsp goto done;
2341 6bad629b 2019-02-04 stsp if (argc == 0) {
2342 927df6b7 2019-02-10 stsp path = strdup("");
2343 927df6b7 2019-02-10 stsp if (path == NULL) {
2344 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2345 6bad629b 2019-02-04 stsp goto done;
2347 6bad629b 2019-02-04 stsp } else if (argc == 1) {
2348 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
2349 927df6b7 2019-02-10 stsp if (error)
2350 6bad629b 2019-02-04 stsp goto done;
2352 6bad629b 2019-02-04 stsp usage_status();
2354 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2355 6bad629b 2019-02-04 stsp if (error != NULL)
2356 6bad629b 2019-02-04 stsp goto done;
2358 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2359 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2360 6bad629b 2019-02-04 stsp if (error)
2361 6bad629b 2019-02-04 stsp goto done;
2363 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
2364 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2366 927df6b7 2019-02-10 stsp free(cwd);
2367 927df6b7 2019-02-10 stsp free(path);
2368 d0eebce4 2019-03-11 stsp return error;
2371 d0eebce4 2019-03-11 stsp __dead static void
2372 d0eebce4 2019-03-11 stsp usage_ref(void)
2374 d0eebce4 2019-03-11 stsp fprintf(stderr,
2375 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2376 d0eebce4 2019-03-11 stsp getprogname());
2380 d0eebce4 2019-03-11 stsp static const struct got_error *
2381 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2383 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2384 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2385 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2387 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2388 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2390 d0eebce4 2019-03-11 stsp return err;
2392 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2393 d0eebce4 2019-03-11 stsp char *refstr;
2394 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2395 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2396 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2397 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2398 d0eebce4 2019-03-11 stsp free(refstr);
2401 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2402 d0eebce4 2019-03-11 stsp return NULL;
2405 d0eebce4 2019-03-11 stsp static const struct got_error *
2406 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2408 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2409 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2411 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2413 d0eebce4 2019-03-11 stsp return err;
2415 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2416 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2417 d0eebce4 2019-03-11 stsp return err;
2420 d0eebce4 2019-03-11 stsp static const struct got_error *
2421 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2423 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2424 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2425 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2428 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2429 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2430 d1644381 2019-07-14 stsp * an unintended typo.
2432 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2433 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2435 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2437 d83d9d5c 2019-05-13 stsp if (err) {
2438 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2440 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2441 d83d9d5c 2019-05-13 stsp return err;
2442 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2444 d83d9d5c 2019-05-13 stsp return err;
2445 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2446 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2448 d83d9d5c 2019-05-13 stsp return err;
2451 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2453 d0eebce4 2019-03-11 stsp goto done;
2455 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2458 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2460 d0eebce4 2019-03-11 stsp return err;
2463 d0eebce4 2019-03-11 stsp static const struct got_error *
2464 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2466 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2467 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2468 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2469 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2470 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2471 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2473 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2474 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2475 d0eebce4 2019-03-11 stsp switch (ch) {
2477 d0eebce4 2019-03-11 stsp delref = optarg;
2480 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2481 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2482 d0eebce4 2019-03-11 stsp err(1, "-r option");
2483 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2486 d0eebce4 2019-03-11 stsp do_list = 1;
2489 d0eebce4 2019-03-11 stsp usage_ref();
2490 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2494 d0eebce4 2019-03-11 stsp if (do_list && delref)
2495 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2497 d0eebce4 2019-03-11 stsp argc -= optind;
2498 d0eebce4 2019-03-11 stsp argv += optind;
2500 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2501 d0eebce4 2019-03-11 stsp if (argc > 0)
2502 d0eebce4 2019-03-11 stsp usage_ref();
2503 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2504 d0eebce4 2019-03-11 stsp usage_ref();
2506 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2507 e0b57350 2019-03-12 stsp if (do_list) {
2508 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2509 e0b57350 2019-03-12 stsp NULL) == -1)
2510 e0b57350 2019-03-12 stsp err(1, "pledge");
2512 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2513 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2514 e0b57350 2019-03-12 stsp err(1, "pledge");
2517 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2518 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2519 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2520 d0eebce4 2019-03-11 stsp goto done;
2523 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2524 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2525 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2526 d0eebce4 2019-03-11 stsp goto done;
2528 d0eebce4 2019-03-11 stsp error = NULL;
2529 d0eebce4 2019-03-11 stsp if (worktree) {
2530 d0eebce4 2019-03-11 stsp repo_path =
2531 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2532 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2533 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2534 d0eebce4 2019-03-11 stsp if (error)
2535 d0eebce4 2019-03-11 stsp goto done;
2537 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2538 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2539 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2540 d0eebce4 2019-03-11 stsp goto done;
2545 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2546 d0eebce4 2019-03-11 stsp if (error != NULL)
2547 d0eebce4 2019-03-11 stsp goto done;
2549 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2550 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2551 c02c541e 2019-03-29 stsp if (error)
2552 c02c541e 2019-03-29 stsp goto done;
2554 d0eebce4 2019-03-11 stsp if (do_list)
2555 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2556 d0eebce4 2019-03-11 stsp else if (delref)
2557 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2559 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2562 4e759de4 2019-06-26 stsp got_repo_close(repo);
2563 4e759de4 2019-06-26 stsp if (worktree)
2564 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2565 4e759de4 2019-06-26 stsp free(cwd);
2566 4e759de4 2019-06-26 stsp free(repo_path);
2567 4e759de4 2019-06-26 stsp return error;
2570 4e759de4 2019-06-26 stsp __dead static void
2571 4e759de4 2019-06-26 stsp usage_branch(void)
2573 4e759de4 2019-06-26 stsp fprintf(stderr,
2574 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2575 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2579 4e759de4 2019-06-26 stsp static const struct got_error *
2580 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2582 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2583 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2584 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2586 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2588 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2590 4e759de4 2019-06-26 stsp return err;
2592 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2593 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2594 4e759de4 2019-06-26 stsp char *refstr;
2595 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2596 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2598 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2599 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2600 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2601 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2603 ba882ee3 2019-07-11 stsp return err;
2604 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2605 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2606 ba882ee3 2019-07-11 stsp marker = "* ";
2608 ba882ee3 2019-07-11 stsp marker = "~ ";
2611 4e759de4 2019-06-26 stsp refname += 11;
2612 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2613 4e759de4 2019-06-26 stsp if (refstr == NULL)
2614 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2615 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2616 4e759de4 2019-06-26 stsp free(refstr);
2619 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2620 4e759de4 2019-06-26 stsp return NULL;
2623 4e759de4 2019-06-26 stsp static const struct got_error *
2624 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2626 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2627 4e759de4 2019-06-26 stsp struct got_reference *ref;
2628 4e759de4 2019-06-26 stsp char *refname;
2630 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2631 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2633 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2635 4e759de4 2019-06-26 stsp goto done;
2637 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2638 4e759de4 2019-06-26 stsp got_ref_close(ref);
2640 4e759de4 2019-06-26 stsp free(refname);
2641 4e759de4 2019-06-26 stsp return err;
2644 4e759de4 2019-06-26 stsp static const struct got_error *
2645 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2646 4e759de4 2019-06-26 stsp const char *base_branch)
2648 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2649 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2650 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2651 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2652 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2655 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2656 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2657 d3f84d51 2019-07-11 stsp * an unintended typo.
2659 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2660 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2662 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2663 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2664 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2665 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2666 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2667 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2669 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2671 4e759de4 2019-06-26 stsp goto done;
2672 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2673 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2675 4e759de4 2019-06-26 stsp goto done;
2677 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2678 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2679 4e759de4 2019-06-26 stsp goto done;
2682 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2683 4e759de4 2019-06-26 stsp if (err == NULL) {
2684 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2685 4e759de4 2019-06-26 stsp goto done;
2686 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2687 4e759de4 2019-06-26 stsp goto done;
2689 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2691 4e759de4 2019-06-26 stsp goto done;
2693 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2696 4e759de4 2019-06-26 stsp got_ref_close(ref);
2698 4e759de4 2019-06-26 stsp free(base_refname);
2699 4e759de4 2019-06-26 stsp free(refname);
2700 4e759de4 2019-06-26 stsp return err;
2703 4e759de4 2019-06-26 stsp static const struct got_error *
2704 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2706 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2707 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2708 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2709 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2710 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2711 4e759de4 2019-06-26 stsp const char *delref = NULL;
2713 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2714 4e759de4 2019-06-26 stsp switch (ch) {
2716 4e759de4 2019-06-26 stsp delref = optarg;
2719 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2720 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2721 4e759de4 2019-06-26 stsp err(1, "-r option");
2722 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2725 4e759de4 2019-06-26 stsp do_list = 1;
2728 4e759de4 2019-06-26 stsp usage_branch();
2729 4e759de4 2019-06-26 stsp /* NOTREACHED */
2733 4e759de4 2019-06-26 stsp if (do_list && delref)
2734 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2736 4e759de4 2019-06-26 stsp argc -= optind;
2737 4e759de4 2019-06-26 stsp argv += optind;
2739 4e759de4 2019-06-26 stsp if (do_list || delref) {
2740 4e759de4 2019-06-26 stsp if (argc > 0)
2741 4e759de4 2019-06-26 stsp usage_branch();
2742 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2743 4e759de4 2019-06-26 stsp usage_branch();
2745 4e759de4 2019-06-26 stsp #ifndef PROFILE
2746 4e759de4 2019-06-26 stsp if (do_list) {
2747 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2748 4e759de4 2019-06-26 stsp NULL) == -1)
2749 4e759de4 2019-06-26 stsp err(1, "pledge");
2751 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2752 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2753 4e759de4 2019-06-26 stsp err(1, "pledge");
2756 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2757 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2758 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2759 4e759de4 2019-06-26 stsp goto done;
2762 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2763 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2764 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2765 4e759de4 2019-06-26 stsp goto done;
2767 4e759de4 2019-06-26 stsp error = NULL;
2768 4e759de4 2019-06-26 stsp if (worktree) {
2769 4e759de4 2019-06-26 stsp repo_path =
2770 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2771 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2772 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2773 4e759de4 2019-06-26 stsp if (error)
2774 4e759de4 2019-06-26 stsp goto done;
2776 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2777 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2778 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2779 4e759de4 2019-06-26 stsp goto done;
2784 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2785 4e759de4 2019-06-26 stsp if (error != NULL)
2786 4e759de4 2019-06-26 stsp goto done;
2788 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2789 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2790 4e759de4 2019-06-26 stsp if (error)
2791 4e759de4 2019-06-26 stsp goto done;
2793 4e759de4 2019-06-26 stsp if (do_list)
2794 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2795 4e759de4 2019-06-26 stsp else if (delref)
2796 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2798 4e759de4 2019-06-26 stsp const char *base_branch;
2799 4e759de4 2019-06-26 stsp if (argc == 1) {
2800 4e759de4 2019-06-26 stsp base_branch = worktree ?
2801 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2802 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2804 4e759de4 2019-06-26 stsp base_branch = argv[1];
2805 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2809 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2810 d0eebce4 2019-03-11 stsp if (worktree)
2811 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2812 d0eebce4 2019-03-11 stsp free(cwd);
2813 d0eebce4 2019-03-11 stsp free(repo_path);
2814 d00136be 2019-03-26 stsp return error;
2817 d00136be 2019-03-26 stsp __dead static void
2818 d00136be 2019-03-26 stsp usage_add(void)
2820 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2824 d00136be 2019-03-26 stsp static const struct got_error *
2825 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2827 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2828 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2829 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2830 1dd54920 2019-05-11 stsp char *cwd = NULL;
2831 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2832 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2835 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2837 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2838 d00136be 2019-03-26 stsp switch (ch) {
2840 d00136be 2019-03-26 stsp usage_add();
2841 d00136be 2019-03-26 stsp /* NOTREACHED */
2845 d00136be 2019-03-26 stsp argc -= optind;
2846 d00136be 2019-03-26 stsp argv += optind;
2848 43012d58 2019-07-14 stsp #ifndef PROFILE
2849 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2850 43012d58 2019-07-14 stsp NULL) == -1)
2851 43012d58 2019-07-14 stsp err(1, "pledge");
2853 723c305c 2019-05-11 jcs if (argc < 1)
2854 d00136be 2019-03-26 stsp usage_add();
2856 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2857 d00136be 2019-03-26 stsp if (cwd == NULL) {
2858 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2859 d00136be 2019-03-26 stsp goto done;
2862 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2863 d00136be 2019-03-26 stsp if (error)
2864 d00136be 2019-03-26 stsp goto done;
2866 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2867 031a5338 2019-03-26 stsp if (error != NULL)
2868 031a5338 2019-03-26 stsp goto done;
2870 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2871 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2872 d00136be 2019-03-26 stsp if (error)
2873 d00136be 2019-03-26 stsp goto done;
2875 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2876 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2877 723c305c 2019-05-11 jcs if (path == NULL) {
2878 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2882 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2883 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2884 1dd54920 2019-05-11 stsp if (error) {
2885 1dd54920 2019-05-11 stsp free(path);
2889 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2890 1dd54920 2019-05-11 stsp NULL, repo);
2893 031a5338 2019-03-26 stsp got_repo_close(repo);
2894 d00136be 2019-03-26 stsp if (worktree)
2895 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2896 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2897 1dd54920 2019-05-11 stsp free((char *)pe->path);
2898 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2899 2ec1f75b 2019-03-26 stsp free(cwd);
2900 2ec1f75b 2019-03-26 stsp return error;
2903 2ec1f75b 2019-03-26 stsp __dead static void
2904 648e4ef7 2019-07-09 stsp usage_remove(void)
2906 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2910 2ec1f75b 2019-03-26 stsp static const struct got_error *
2911 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2913 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2914 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2915 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2916 17ed4618 2019-06-02 stsp char *cwd = NULL;
2917 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2918 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2919 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2921 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2923 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2924 2ec1f75b 2019-03-26 stsp switch (ch) {
2926 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2929 2ec1f75b 2019-03-26 stsp usage_add();
2930 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2934 2ec1f75b 2019-03-26 stsp argc -= optind;
2935 2ec1f75b 2019-03-26 stsp argv += optind;
2937 43012d58 2019-07-14 stsp #ifndef PROFILE
2938 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2939 43012d58 2019-07-14 stsp NULL) == -1)
2940 43012d58 2019-07-14 stsp err(1, "pledge");
2942 17ed4618 2019-06-02 stsp if (argc < 1)
2943 648e4ef7 2019-07-09 stsp usage_remove();
2945 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2946 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2947 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2948 2ec1f75b 2019-03-26 stsp goto done;
2950 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2951 2ec1f75b 2019-03-26 stsp if (error)
2952 2ec1f75b 2019-03-26 stsp goto done;
2954 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2956 2ec1f75b 2019-03-26 stsp goto done;
2958 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2959 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2960 2ec1f75b 2019-03-26 stsp if (error)
2961 2ec1f75b 2019-03-26 stsp goto done;
2963 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2964 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2965 17ed4618 2019-06-02 stsp if (path == NULL) {
2966 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2967 17ed4618 2019-06-02 stsp goto done;
2970 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2971 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2972 17ed4618 2019-06-02 stsp if (error) {
2973 17ed4618 2019-06-02 stsp free(path);
2974 17ed4618 2019-06-02 stsp goto done;
2977 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2978 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2979 a129376b 2019-03-28 stsp if (error)
2980 a129376b 2019-03-28 stsp goto done;
2983 a129376b 2019-03-28 stsp got_repo_close(repo);
2984 a129376b 2019-03-28 stsp if (worktree)
2985 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2986 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2987 17ed4618 2019-06-02 stsp free((char *)pe->path);
2988 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2989 a129376b 2019-03-28 stsp free(cwd);
2990 a129376b 2019-03-28 stsp return error;
2993 a129376b 2019-03-28 stsp __dead static void
2994 a129376b 2019-03-28 stsp usage_revert(void)
2996 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3000 1ee397ad 2019-07-12 stsp static const struct got_error *
3001 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3003 a129376b 2019-03-28 stsp while (path[0] == '/')
3005 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3006 1ee397ad 2019-07-12 stsp return NULL;
3009 a129376b 2019-03-28 stsp static const struct got_error *
3010 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3012 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3013 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3014 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3015 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3016 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3017 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3018 e20a8b6f 2019-06-04 stsp int ch, i;
3020 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3022 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3023 a129376b 2019-03-28 stsp switch (ch) {
3025 a129376b 2019-03-28 stsp usage_revert();
3026 a129376b 2019-03-28 stsp /* NOTREACHED */
3030 a129376b 2019-03-28 stsp argc -= optind;
3031 a129376b 2019-03-28 stsp argv += optind;
3033 43012d58 2019-07-14 stsp #ifndef PROFILE
3034 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3035 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3036 43012d58 2019-07-14 stsp err(1, "pledge");
3038 e20a8b6f 2019-06-04 stsp if (argc < 1)
3039 a129376b 2019-03-28 stsp usage_revert();
3041 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3042 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3043 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3044 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3045 e20a8b6f 2019-06-04 stsp goto done;
3048 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3049 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3050 e20a8b6f 2019-06-04 stsp if (error) {
3051 e20a8b6f 2019-06-04 stsp free(path);
3052 e20a8b6f 2019-06-04 stsp goto done;
3056 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3057 a129376b 2019-03-28 stsp if (cwd == NULL) {
3058 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3059 a129376b 2019-03-28 stsp goto done;
3061 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3062 2ec1f75b 2019-03-26 stsp if (error)
3063 2ec1f75b 2019-03-26 stsp goto done;
3065 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3066 a129376b 2019-03-28 stsp if (error != NULL)
3067 a129376b 2019-03-28 stsp goto done;
3069 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3070 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3071 a129376b 2019-03-28 stsp if (error)
3072 a129376b 2019-03-28 stsp goto done;
3074 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3075 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3076 a129376b 2019-03-28 stsp if (error)
3077 a129376b 2019-03-28 stsp goto done;
3080 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3081 2ec1f75b 2019-03-26 stsp if (worktree)
3082 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3083 2ec1f75b 2019-03-26 stsp free(path);
3084 d00136be 2019-03-26 stsp free(cwd);
3085 6bad629b 2019-02-04 stsp return error;
3088 c4296144 2019-05-09 stsp __dead static void
3089 c4296144 2019-05-09 stsp usage_commit(void)
3091 90e8619e 2019-07-25 stsp fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3095 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3096 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3097 e2ba3d07 2019-05-13 stsp const char *editor;
3098 e0870e44 2019-05-13 stsp const char *worktree_path;
3099 76d98825 2019-06-03 stsp const char *branch_name;
3100 314a6357 2019-05-13 stsp const char *repo_path;
3101 e0870e44 2019-05-13 stsp char *logmsg_path;
3105 33ad4cbe 2019-05-12 jcs static const struct got_error *
3106 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3109 76d98825 2019-06-03 stsp char *initial_content = NULL;
3110 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3111 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3112 e0870e44 2019-05-13 stsp char *template = NULL;
3113 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3115 33ad4cbe 2019-05-12 jcs size_t len;
3117 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3118 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3119 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3120 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3121 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3122 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3123 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3124 33ad4cbe 2019-05-12 jcs return NULL;
3127 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3128 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3130 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3131 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3132 76d98825 2019-06-03 stsp a->branch_name) == -1)
3133 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3135 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3137 e0870e44 2019-05-13 stsp goto done;
3139 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3141 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3142 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3143 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3144 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3145 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3149 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3151 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3152 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3153 76d98825 2019-06-03 stsp free(initial_content);
3154 e0870e44 2019-05-13 stsp free(template);
3156 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3157 3ce1b845 2019-07-15 stsp if (err == NULL) {
3158 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3159 3ce1b845 2019-07-15 stsp if (err) {
3160 3ce1b845 2019-07-15 stsp free(*logmsg);
3161 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3164 33ad4cbe 2019-05-12 jcs return err;
3167 c4296144 2019-05-09 stsp static const struct got_error *
3168 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3170 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3171 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3172 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3173 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
3174 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3175 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3176 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3177 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3178 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3179 a698f62e 2019-07-25 stsp int ch, rebase_in_progress;
3181 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3182 c4296144 2019-05-09 stsp switch (ch) {
3184 c4296144 2019-05-09 stsp logmsg = optarg;
3187 c4296144 2019-05-09 stsp usage_commit();
3188 c4296144 2019-05-09 stsp /* NOTREACHED */
3192 c4296144 2019-05-09 stsp argc -= optind;
3193 c4296144 2019-05-09 stsp argv += optind;
3195 43012d58 2019-07-14 stsp #ifndef PROFILE
3196 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3197 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3198 43012d58 2019-07-14 stsp err(1, "pledge");
3200 c4296144 2019-05-09 stsp if (argc == 1) {
3201 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
3202 c4296144 2019-05-09 stsp if (path == NULL) {
3203 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
3204 c4296144 2019-05-09 stsp goto done;
3206 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
3207 c4296144 2019-05-09 stsp } else if (argc != 0)
3208 c4296144 2019-05-09 stsp usage_commit();
3210 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3211 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3212 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3213 35bd8fed 2019-05-09 stsp goto done;
3216 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3217 c4296144 2019-05-09 stsp if (cwd == NULL) {
3218 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3219 c4296144 2019-05-09 stsp goto done;
3221 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3222 7d5807f4 2019-07-11 stsp if (error)
3223 7d5807f4 2019-07-11 stsp goto done;
3225 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3226 c4296144 2019-05-09 stsp if (error)
3227 a698f62e 2019-07-25 stsp goto done;
3228 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3229 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3230 c4296144 2019-05-09 stsp goto done;
3233 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3234 c4296144 2019-05-09 stsp if (error != NULL)
3235 c4296144 2019-05-09 stsp goto done;
3238 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3239 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3241 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3242 314a6357 2019-05-13 stsp error = get_editor(&editor);
3244 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3245 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3246 c4296144 2019-05-09 stsp if (error)
3247 c4296144 2019-05-09 stsp goto done;
3249 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3250 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3251 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3252 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3253 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3254 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
3255 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3256 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
3257 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3258 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3259 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3260 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3261 e0870e44 2019-05-13 stsp if (error) {
3262 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3263 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3264 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3265 c4296144 2019-05-09 stsp goto done;
3268 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3269 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3271 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3272 c4296144 2019-05-09 stsp if (error)
3273 c4296144 2019-05-09 stsp goto done;
3274 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3277 c4296144 2019-05-09 stsp got_repo_close(repo);
3278 c4296144 2019-05-09 stsp if (worktree)
3279 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3280 c4296144 2019-05-09 stsp free(path);
3281 c4296144 2019-05-09 stsp free(cwd);
3282 c4296144 2019-05-09 stsp free(id_str);
3283 e2ba3d07 2019-05-13 stsp free(editor);
3284 234035bc 2019-06-01 stsp return error;
3287 234035bc 2019-06-01 stsp __dead static void
3288 234035bc 2019-06-01 stsp usage_cherrypick(void)
3290 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3294 234035bc 2019-06-01 stsp static const struct got_error *
3295 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3297 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3298 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3299 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3300 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3301 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3302 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3303 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3304 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3305 234035bc 2019-06-01 stsp int ch, did_something = 0;
3307 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3308 234035bc 2019-06-01 stsp switch (ch) {
3310 234035bc 2019-06-01 stsp usage_cherrypick();
3311 234035bc 2019-06-01 stsp /* NOTREACHED */
3315 234035bc 2019-06-01 stsp argc -= optind;
3316 234035bc 2019-06-01 stsp argv += optind;
3318 43012d58 2019-07-14 stsp #ifndef PROFILE
3319 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3320 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3321 43012d58 2019-07-14 stsp err(1, "pledge");
3323 234035bc 2019-06-01 stsp if (argc != 1)
3324 234035bc 2019-06-01 stsp usage_cherrypick();
3326 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3327 234035bc 2019-06-01 stsp if (cwd == NULL) {
3328 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3329 234035bc 2019-06-01 stsp goto done;
3331 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3332 234035bc 2019-06-01 stsp if (error)
3333 234035bc 2019-06-01 stsp goto done;
3335 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3336 234035bc 2019-06-01 stsp if (error != NULL)
3337 234035bc 2019-06-01 stsp goto done;
3339 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3340 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3341 234035bc 2019-06-01 stsp if (error)
3342 234035bc 2019-06-01 stsp goto done;
3344 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3345 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3346 234035bc 2019-06-01 stsp if (error != NULL) {
3347 234035bc 2019-06-01 stsp struct got_reference *ref;
3348 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3349 234035bc 2019-06-01 stsp goto done;
3350 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3351 234035bc 2019-06-01 stsp if (error != NULL)
3352 234035bc 2019-06-01 stsp goto done;
3353 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3354 234035bc 2019-06-01 stsp got_ref_close(ref);
3355 234035bc 2019-06-01 stsp if (error != NULL)
3356 234035bc 2019-06-01 stsp goto done;
3358 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3359 234035bc 2019-06-01 stsp if (error)
3360 234035bc 2019-06-01 stsp goto done;
3362 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3363 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3364 234035bc 2019-06-01 stsp if (error != NULL)
3365 234035bc 2019-06-01 stsp goto done;
3367 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3368 234035bc 2019-06-01 stsp if (error) {
3369 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3370 234035bc 2019-06-01 stsp goto done;
3371 234035bc 2019-06-01 stsp error = NULL;
3373 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3374 234035bc 2019-06-01 stsp goto done;
3377 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3378 234035bc 2019-06-01 stsp if (error)
3379 234035bc 2019-06-01 stsp goto done;
3380 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3381 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3382 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3384 234035bc 2019-06-01 stsp if (error != NULL)
3385 234035bc 2019-06-01 stsp goto done;
3387 234035bc 2019-06-01 stsp if (did_something)
3388 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3390 234035bc 2019-06-01 stsp if (commit)
3391 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3392 234035bc 2019-06-01 stsp free(commit_id_str);
3393 234035bc 2019-06-01 stsp if (head_ref)
3394 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3395 234035bc 2019-06-01 stsp if (worktree)
3396 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3398 234035bc 2019-06-01 stsp got_repo_close(repo);
3399 c4296144 2019-05-09 stsp return error;
3402 5ef14e63 2019-06-02 stsp __dead static void
3403 5ef14e63 2019-06-02 stsp usage_backout(void)
3405 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3409 5ef14e63 2019-06-02 stsp static const struct got_error *
3410 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3412 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3413 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3414 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3415 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3416 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3417 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3418 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3419 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3420 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3422 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3423 5ef14e63 2019-06-02 stsp switch (ch) {
3425 5ef14e63 2019-06-02 stsp usage_backout();
3426 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3430 5ef14e63 2019-06-02 stsp argc -= optind;
3431 5ef14e63 2019-06-02 stsp argv += optind;
3433 43012d58 2019-07-14 stsp #ifndef PROFILE
3434 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3435 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3436 43012d58 2019-07-14 stsp err(1, "pledge");
3438 5ef14e63 2019-06-02 stsp if (argc != 1)
3439 5ef14e63 2019-06-02 stsp usage_backout();
3441 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3442 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3443 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3444 5ef14e63 2019-06-02 stsp goto done;
3446 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3447 5ef14e63 2019-06-02 stsp if (error)
3448 5ef14e63 2019-06-02 stsp goto done;
3450 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3451 5ef14e63 2019-06-02 stsp if (error != NULL)
3452 5ef14e63 2019-06-02 stsp goto done;
3454 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3455 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3456 5ef14e63 2019-06-02 stsp if (error)
3457 5ef14e63 2019-06-02 stsp goto done;
3459 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3460 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3461 5ef14e63 2019-06-02 stsp if (error != NULL) {
3462 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3463 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3464 5ef14e63 2019-06-02 stsp goto done;
3465 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3466 5ef14e63 2019-06-02 stsp if (error != NULL)
3467 5ef14e63 2019-06-02 stsp goto done;
3468 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3469 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3470 5ef14e63 2019-06-02 stsp if (error != NULL)
3471 5ef14e63 2019-06-02 stsp goto done;
3473 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3474 5ef14e63 2019-06-02 stsp if (error)
3475 5ef14e63 2019-06-02 stsp goto done;
3477 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3478 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3479 5ef14e63 2019-06-02 stsp if (error != NULL)
3480 5ef14e63 2019-06-02 stsp goto done;
3482 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3483 5ef14e63 2019-06-02 stsp if (error)
3484 5ef14e63 2019-06-02 stsp goto done;
3486 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3487 5ef14e63 2019-06-02 stsp if (error)
3488 5ef14e63 2019-06-02 stsp goto done;
3489 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3490 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3491 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3492 5ef14e63 2019-06-02 stsp goto done;
3495 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3496 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3497 5ef14e63 2019-06-02 stsp if (error != NULL)
3498 5ef14e63 2019-06-02 stsp goto done;
3500 5ef14e63 2019-06-02 stsp if (did_something)
3501 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3503 5ef14e63 2019-06-02 stsp if (commit)
3504 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3505 5ef14e63 2019-06-02 stsp free(commit_id_str);
3506 5ef14e63 2019-06-02 stsp if (head_ref)
3507 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3508 818c7501 2019-07-11 stsp if (worktree)
3509 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3511 818c7501 2019-07-11 stsp got_repo_close(repo);
3512 818c7501 2019-07-11 stsp return error;
3515 818c7501 2019-07-11 stsp __dead static void
3516 818c7501 2019-07-11 stsp usage_rebase(void)
3518 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3519 af54c8f8 2019-07-11 stsp getprogname());
3524 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3527 0ebf8283 2019-07-24 stsp size_t len;
3529 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3530 0ebf8283 2019-07-24 stsp if (len > limit)
3531 0ebf8283 2019-07-24 stsp len = limit;
3532 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3533 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3535 0ebf8283 2019-07-24 stsp *nl = '\0';
3538 0ebf8283 2019-07-24 stsp static const struct got_error *
3539 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3541 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3543 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3545 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3546 0ebf8283 2019-07-24 stsp logmsg0++;
3548 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3549 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3550 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3552 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3553 0ebf8283 2019-07-24 stsp return NULL;
3556 818c7501 2019-07-11 stsp static const struct got_error *
3557 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3558 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3560 818c7501 2019-07-11 stsp const struct got_error *err;
3561 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3563 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3565 818c7501 2019-07-11 stsp goto done;
3567 ff0d2220 2019-07-11 stsp if (new_id) {
3568 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3570 ff0d2220 2019-07-11 stsp goto done;
3573 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3574 ff0d2220 2019-07-11 stsp if (new_id_str)
3575 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3577 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3579 0ebf8283 2019-07-24 stsp goto done;
3581 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3582 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3584 818c7501 2019-07-11 stsp free(old_id_str);
3585 818c7501 2019-07-11 stsp free(new_id_str);
3586 818c7501 2019-07-11 stsp return err;
3589 1ee397ad 2019-07-12 stsp static const struct got_error *
3590 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3592 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3594 818c7501 2019-07-11 stsp while (path[0] == '/')
3596 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3598 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3599 1ee397ad 2019-07-12 stsp return NULL;
3600 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3601 818c7501 2019-07-11 stsp *rebase_status = status;
3602 1ee397ad 2019-07-12 stsp return NULL;
3605 818c7501 2019-07-11 stsp static const struct got_error *
3606 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3607 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
3608 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
3610 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3611 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
3612 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3615 818c7501 2019-07-11 stsp static const struct got_error *
3616 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3617 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3618 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
3619 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
3621 ff0d2220 2019-07-11 stsp const struct got_error *error;
3622 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3623 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3625 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3626 ff0d2220 2019-07-11 stsp if (error)
3627 ff0d2220 2019-07-11 stsp return error;
3629 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3630 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
3631 ff0d2220 2019-07-11 stsp if (error) {
3632 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3633 ff0d2220 2019-07-11 stsp goto done;
3634 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3636 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3637 ff0d2220 2019-07-11 stsp free(new_commit_id);
3640 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3641 ff0d2220 2019-07-11 stsp return error;
3644 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3645 64c6d990 2019-07-11 stsp const char *path_prefix;
3646 64c6d990 2019-07-11 stsp size_t len;
3647 8ca9bd68 2019-07-25 stsp int errcode;
3650 64c6d990 2019-07-11 stsp static const struct got_error *
3651 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3652 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3653 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3654 64c6d990 2019-07-11 stsp struct got_repository *repo)
3656 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3658 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3659 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3660 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
3662 64c6d990 2019-07-11 stsp return NULL;
3665 64c6d990 2019-07-11 stsp static const struct got_error *
3666 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
3667 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3668 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
3670 64c6d990 2019-07-11 stsp const struct got_error *err;
3671 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3673 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3675 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3676 64c6d990 2019-07-11 stsp return NULL;
3678 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3680 64c6d990 2019-07-11 stsp goto done;
3682 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3684 64c6d990 2019-07-11 stsp goto done;
3686 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3687 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3689 64c6d990 2019-07-11 stsp goto done;
3691 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3692 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3694 64c6d990 2019-07-11 stsp goto done;
3696 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3697 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
3698 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
3699 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
3700 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
3701 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3702 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff, &cpp_arg);
3704 64c6d990 2019-07-11 stsp if (tree1)
3705 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3706 64c6d990 2019-07-11 stsp if (tree2)
3707 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3708 64c6d990 2019-07-11 stsp if (commit)
3709 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3710 64c6d990 2019-07-11 stsp if (parent_commit)
3711 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3712 64c6d990 2019-07-11 stsp return err;
3715 ff0d2220 2019-07-11 stsp static const struct got_error *
3716 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
3717 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3718 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3719 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
3720 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
3722 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3723 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3724 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3725 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3726 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3728 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3730 af61c510 2019-07-19 stsp return err;
3732 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3734 af61c510 2019-07-19 stsp goto done;
3735 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3736 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3737 af61c510 2019-07-19 stsp if (err) {
3738 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3739 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3740 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3741 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3742 af61c510 2019-07-19 stsp "been reached?!?");
3743 af61c510 2019-07-19 stsp goto done;
3744 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3745 af61c510 2019-07-19 stsp goto done;
3746 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3748 af61c510 2019-07-19 stsp goto done;
3750 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
3751 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
3753 af61c510 2019-07-19 stsp goto done;
3755 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3757 af61c510 2019-07-19 stsp goto done;
3758 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3759 af61c510 2019-07-19 stsp commit_id = parent_id;
3763 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3764 af61c510 2019-07-19 stsp return err;
3767 af61c510 2019-07-19 stsp static const struct got_error *
3768 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3770 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3771 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3772 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3773 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
3774 818c7501 2019-07-11 stsp char *cwd = NULL;
3775 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3776 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3777 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3778 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3779 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3780 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3781 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3782 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3783 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3784 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3785 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3786 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3788 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3789 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3791 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3792 818c7501 2019-07-11 stsp switch (ch) {
3794 818c7501 2019-07-11 stsp abort_rebase = 1;
3797 818c7501 2019-07-11 stsp continue_rebase = 1;
3800 818c7501 2019-07-11 stsp usage_rebase();
3801 818c7501 2019-07-11 stsp /* NOTREACHED */
3805 818c7501 2019-07-11 stsp argc -= optind;
3806 818c7501 2019-07-11 stsp argv += optind;
3808 43012d58 2019-07-14 stsp #ifndef PROFILE
3809 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3811 43012d58 2019-07-14 stsp err(1, "pledge");
3813 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3814 818c7501 2019-07-11 stsp usage_rebase();
3815 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3816 818c7501 2019-07-11 stsp if (argc != 0)
3817 818c7501 2019-07-11 stsp usage_rebase();
3818 818c7501 2019-07-11 stsp } else if (argc != 1)
3819 818c7501 2019-07-11 stsp usage_rebase();
3821 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3822 818c7501 2019-07-11 stsp if (cwd == NULL) {
3823 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3824 818c7501 2019-07-11 stsp goto done;
3826 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3827 818c7501 2019-07-11 stsp if (error)
3828 818c7501 2019-07-11 stsp goto done;
3830 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3831 818c7501 2019-07-11 stsp if (error != NULL)
3832 818c7501 2019-07-11 stsp goto done;
3834 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3835 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3836 818c7501 2019-07-11 stsp if (error)
3837 818c7501 2019-07-11 stsp goto done;
3839 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3840 818c7501 2019-07-11 stsp if (error)
3841 818c7501 2019-07-11 stsp goto done;
3843 f6794adc 2019-07-23 stsp if (abort_rebase) {
3844 818c7501 2019-07-11 stsp int did_something;
3845 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3846 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3847 f6794adc 2019-07-23 stsp goto done;
3849 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3850 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3851 3e3a69f1 2019-07-25 stsp worktree, repo);
3852 818c7501 2019-07-11 stsp if (error)
3853 818c7501 2019-07-11 stsp goto done;
3854 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3855 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3856 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
3857 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3858 818c7501 2019-07-11 stsp if (error)
3859 818c7501 2019-07-11 stsp goto done;
3860 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3861 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3864 818c7501 2019-07-11 stsp if (continue_rebase) {
3865 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3866 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3867 f6794adc 2019-07-23 stsp goto done;
3869 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3870 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3871 3e3a69f1 2019-07-25 stsp worktree, repo);
3872 818c7501 2019-07-11 stsp if (error)
3873 818c7501 2019-07-11 stsp goto done;
3875 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3876 01757395 2019-07-12 stsp resume_commit_id, repo);
3877 818c7501 2019-07-11 stsp if (error)
3878 818c7501 2019-07-11 stsp goto done;
3880 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3881 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3882 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3883 818c7501 2019-07-11 stsp goto done;
3886 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3887 818c7501 2019-07-11 stsp if (error != NULL)
3888 818c7501 2019-07-11 stsp goto done;
3890 818c7501 2019-07-11 stsp error = check_same_branch(
3891 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree), branch, repo);
3892 818c7501 2019-07-11 stsp if (error) {
3893 818c7501 2019-07-11 stsp if (error->code != GOT_ERR_ANCESTRY)
3894 818c7501 2019-07-11 stsp goto done;
3895 818c7501 2019-07-11 stsp error = NULL;
3897 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3898 818c7501 2019-07-11 stsp "specified branch resolves to a commit which "
3899 818c7501 2019-07-11 stsp "is already contained in work tree's branch");
3900 818c7501 2019-07-11 stsp goto done;
3904 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3905 ff0d2220 2019-07-11 stsp if (error)
3906 ff0d2220 2019-07-11 stsp goto done;
3908 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3909 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3910 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree),
3911 818c7501 2019-07-11 stsp branch_head_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 if (yca_id == NULL) {
3915 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3916 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3917 818c7501 2019-07-11 stsp "with work tree's branch");
3918 818c7501 2019-07-11 stsp goto done;
3921 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3922 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
3923 818c7501 2019-07-11 stsp if (error)
3924 818c7501 2019-07-11 stsp goto done;
3927 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3928 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3929 818c7501 2019-07-11 stsp if (error)
3930 818c7501 2019-07-11 stsp goto done;
3932 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3933 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3934 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
3935 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
3936 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
3937 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3938 818c7501 2019-07-11 stsp commit = NULL;
3939 818c7501 2019-07-11 stsp if (error)
3940 818c7501 2019-07-11 stsp goto done;
3942 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3943 ff0d2220 2019-07-11 stsp if (continue_rebase)
3944 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
3945 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
3947 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3948 818c7501 2019-07-11 stsp goto done;
3951 818c7501 2019-07-11 stsp pid = NULL;
3952 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
3953 818c7501 2019-07-11 stsp commit_id = qid->id;
3954 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
3955 818c7501 2019-07-11 stsp pid = qid;
3957 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
3958 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
3959 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
3960 818c7501 2019-07-11 stsp if (error)
3961 818c7501 2019-07-11 stsp goto done;
3963 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3964 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3968 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
3969 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
3970 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3971 818c7501 2019-07-11 stsp if (error)
3972 818c7501 2019-07-11 stsp goto done;
3975 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3976 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
3977 818c7501 2019-07-11 stsp if (error)
3978 818c7501 2019-07-11 stsp goto done;
3979 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
3980 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
3982 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
3983 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
3985 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
3986 818c7501 2019-07-11 stsp free(branch_head_commit_id);
3987 818c7501 2019-07-11 stsp free(resume_commit_id);
3988 818c7501 2019-07-11 stsp free(yca_id);
3989 818c7501 2019-07-11 stsp if (commit)
3990 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3991 818c7501 2019-07-11 stsp if (branch)
3992 818c7501 2019-07-11 stsp got_ref_close(branch);
3993 818c7501 2019-07-11 stsp if (new_base_branch)
3994 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
3995 818c7501 2019-07-11 stsp if (tmp_branch)
3996 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
3997 5ef14e63 2019-06-02 stsp if (worktree)
3998 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4000 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4001 5ef14e63 2019-06-02 stsp return error;
4004 0ebf8283 2019-07-24 stsp __dead static void
4005 0ebf8283 2019-07-24 stsp usage_histedit(void)
4007 0ebf8283 2019-07-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4008 0ebf8283 2019-07-24 stsp getprogname());
4012 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4013 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4014 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4015 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4016 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4018 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4019 0ebf8283 2019-07-24 stsp unsigned char code;
4020 0ebf8283 2019-07-24 stsp const char *name;
4021 0ebf8283 2019-07-24 stsp const char *desc;
4022 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4023 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4024 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4025 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4026 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4027 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4028 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4031 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4032 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4033 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4034 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4035 0ebf8283 2019-07-24 stsp char *logmsg;
4037 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4039 0ebf8283 2019-07-24 stsp static const struct got_error *
4040 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4041 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4043 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4044 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4045 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4048 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4050 0ebf8283 2019-07-24 stsp goto done;
4052 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4054 0ebf8283 2019-07-24 stsp goto done;
4056 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4058 0ebf8283 2019-07-24 stsp goto done;
4060 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4061 0ebf8283 2019-07-24 stsp if (n < 0)
4062 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4064 0ebf8283 2019-07-24 stsp if (commit)
4065 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4066 0ebf8283 2019-07-24 stsp free(id_str);
4067 0ebf8283 2019-07-24 stsp free(logmsg);
4068 0ebf8283 2019-07-24 stsp return err;
4071 0ebf8283 2019-07-24 stsp static const struct got_error *
4072 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4073 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4075 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4076 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4078 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4079 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4081 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4082 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4088 0ebf8283 2019-07-24 stsp return err;
4091 0ebf8283 2019-07-24 stsp static const struct got_error *
4092 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4094 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4097 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4098 0ebf8283 2019-07-24 stsp if (n < 0)
4099 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4101 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4102 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4103 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4104 0ebf8283 2019-07-24 stsp cmd->desc);
4105 0ebf8283 2019-07-24 stsp if (n < 0) {
4106 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4110 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4111 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4112 0ebf8283 2019-07-24 stsp if (n < 0)
4113 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4114 0ebf8283 2019-07-24 stsp return err;
4117 0ebf8283 2019-07-24 stsp static const struct got_error *
4118 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4120 0ebf8283 2019-07-24 stsp static char msg[42];
4123 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4125 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4126 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4128 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4131 0ebf8283 2019-07-24 stsp static const struct got_error *
4132 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4133 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4135 0ebf8283 2019-07-24 stsp const struct got_error *err;
4136 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4137 0ebf8283 2019-07-24 stsp char *id_str;
4139 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4141 0ebf8283 2019-07-24 stsp return err;
4143 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4145 0ebf8283 2019-07-24 stsp goto done;
4147 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4148 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4149 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4150 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4151 0ebf8283 2019-07-24 stsp goto done;
4154 0ebf8283 2019-07-24 stsp if (folded_commit)
4155 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4156 0ebf8283 2019-07-24 stsp free(id_str);
4157 0ebf8283 2019-07-24 stsp return err;
4160 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4161 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4163 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4165 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4166 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4167 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4168 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4169 3f9de99f 2019-07-24 stsp folded = prev;
4170 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4173 0ebf8283 2019-07-24 stsp return folded;
4176 0ebf8283 2019-07-24 stsp static const struct got_error *
4177 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4178 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4180 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4181 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4182 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4183 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4185 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4187 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4189 0ebf8283 2019-07-24 stsp return err;
4191 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4192 0ebf8283 2019-07-24 stsp if (folded) {
4193 0ebf8283 2019-07-24 stsp while (folded != hle) {
4194 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4195 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4198 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4199 0ebf8283 2019-07-24 stsp logmsg, repo);
4201 0ebf8283 2019-07-24 stsp goto done;
4202 0ebf8283 2019-07-24 stsp free(logmsg);
4203 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4204 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4208 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4210 0ebf8283 2019-07-24 stsp goto done;
4211 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4212 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4213 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4214 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4215 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4216 0ebf8283 2019-07-24 stsp goto done;
4218 0ebf8283 2019-07-24 stsp free(logmsg);
4219 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4221 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4223 0ebf8283 2019-07-24 stsp goto done;
4225 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4227 0ebf8283 2019-07-24 stsp goto done;
4229 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4230 0ebf8283 2019-07-24 stsp close(fd);
4232 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4234 0ebf8283 2019-07-24 stsp goto done;
4236 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4237 0ebf8283 2019-07-24 stsp if (err) {
4238 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4239 0ebf8283 2019-07-24 stsp goto done;
4240 0ebf8283 2019-07-24 stsp err = NULL;
4241 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4242 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4243 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4246 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4247 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4248 0ebf8283 2019-07-24 stsp free(logmsg_path);
4249 0ebf8283 2019-07-24 stsp free(logmsg);
4250 0ebf8283 2019-07-24 stsp free(editor);
4251 0ebf8283 2019-07-24 stsp if (commit)
4252 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4253 0ebf8283 2019-07-24 stsp return err;
4256 0ebf8283 2019-07-24 stsp static const struct got_error *
4257 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4258 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4260 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4261 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4262 0ebf8283 2019-07-24 stsp size_t size;
4263 0ebf8283 2019-07-24 stsp ssize_t len;
4264 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4265 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4266 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4267 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4269 0ebf8283 2019-07-24 stsp for (;;) {
4270 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4271 0ebf8283 2019-07-24 stsp if (len == -1) {
4272 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4273 0ebf8283 2019-07-24 stsp if (feof(f))
4275 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4276 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4281 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4283 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4284 0ebf8283 2019-07-24 stsp free(line);
4285 0ebf8283 2019-07-24 stsp line = NULL;
4288 0ebf8283 2019-07-24 stsp cmd = NULL;
4289 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4290 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4291 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4292 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4293 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4296 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4301 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4302 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4305 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4307 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4308 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4309 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4312 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4313 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4317 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4318 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4319 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4323 0ebf8283 2019-07-24 stsp free(line);
4324 0ebf8283 2019-07-24 stsp line = NULL;
4328 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4330 0ebf8283 2019-07-24 stsp *end = '\0';
4332 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4333 0ebf8283 2019-07-24 stsp if (err) {
4334 0ebf8283 2019-07-24 stsp /* override error code */
4335 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4339 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4340 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4341 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4344 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4345 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4346 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4347 0ebf8283 2019-07-24 stsp commit_id = NULL;
4348 0ebf8283 2019-07-24 stsp free(line);
4349 0ebf8283 2019-07-24 stsp line = NULL;
4350 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4353 0ebf8283 2019-07-24 stsp free(line);
4354 0ebf8283 2019-07-24 stsp free(commit_id);
4355 0ebf8283 2019-07-24 stsp return err;
4358 0ebf8283 2019-07-24 stsp static const struct got_error *
4359 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4360 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4362 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4363 0ebf8283 2019-07-24 stsp char *editor;
4364 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4366 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4368 0ebf8283 2019-07-24 stsp return err;
4370 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4371 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4372 0ebf8283 2019-07-24 stsp goto done;
4375 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4376 0ebf8283 2019-07-24 stsp if (f == NULL) {
4377 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4378 0ebf8283 2019-07-24 stsp goto done;
4380 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4382 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4383 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4384 0ebf8283 2019-07-24 stsp free(editor);
4385 0ebf8283 2019-07-24 stsp return err;
4388 0ebf8283 2019-07-24 stsp static const struct got_error *
4389 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *, const char *,
4390 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4392 0ebf8283 2019-07-24 stsp static const struct got_error *
4393 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4394 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4396 0ebf8283 2019-07-24 stsp const struct got_error *err;
4397 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4398 0ebf8283 2019-07-24 stsp char *path = NULL;
4400 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4402 0ebf8283 2019-07-24 stsp return err;
4404 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4406 0ebf8283 2019-07-24 stsp goto done;
4408 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4410 0ebf8283 2019-07-24 stsp goto done;
4412 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4413 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4414 0ebf8283 2019-07-24 stsp goto done;
4418 0ebf8283 2019-07-24 stsp err = histedit_run_editor(histedit_cmds, path, repo);
4419 0ebf8283 2019-07-24 stsp if (err) {
4420 0ebf8283 2019-07-24 stsp const char *errmsg = err->msg;
4421 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4422 0ebf8283 2019-07-24 stsp goto done;
4423 0ebf8283 2019-07-24 stsp err = histedit_edit_list_retry(histedit_cmds, errmsg,
4424 0ebf8283 2019-07-24 stsp commits, path, repo);
4427 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4428 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4429 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4430 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4431 0ebf8283 2019-07-24 stsp free(path);
4432 0ebf8283 2019-07-24 stsp return err;
4435 0ebf8283 2019-07-24 stsp static const struct got_error *
4436 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4437 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4439 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4440 0ebf8283 2019-07-24 stsp char *path = NULL;
4441 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4442 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4443 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4445 0ebf8283 2019-07-24 stsp err = got_worktree_get_histedit_list_path(&path, worktree);
4447 0ebf8283 2019-07-24 stsp return err;
4449 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4450 0ebf8283 2019-07-24 stsp if (f == NULL) {
4451 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4452 0ebf8283 2019-07-24 stsp goto done;
4454 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4455 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4460 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4461 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4462 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4463 0ebf8283 2019-07-24 stsp if (n < 0) {
4464 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4470 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4471 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4472 0ebf8283 2019-07-24 stsp free(path);
4473 0ebf8283 2019-07-24 stsp if (commit)
4474 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4475 0ebf8283 2019-07-24 stsp return err;
4478 0ebf8283 2019-07-24 stsp static const struct got_error *
4479 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4480 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4482 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4483 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4485 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4486 0ebf8283 2019-07-24 stsp if (f == NULL) {
4487 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4488 0ebf8283 2019-07-24 stsp goto done;
4491 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4493 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4494 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4495 0ebf8283 2019-07-24 stsp return err;
4498 0ebf8283 2019-07-24 stsp static const struct got_error *
4499 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4500 0ebf8283 2019-07-24 stsp const char *errmsg, struct got_object_id_queue *commits,
4501 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4503 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4504 0ebf8283 2019-07-24 stsp int resp = ' ';
4506 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4507 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4508 0ebf8283 2019-07-24 stsp "or (a)bort: ", getprogname(), errmsg);
4509 0ebf8283 2019-07-24 stsp resp = getchar();
4510 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4511 0ebf8283 2019-07-24 stsp err = histedit_run_editor(histedit_cmds, path, repo);
4512 426ebf2e 2019-07-25 stsp if (err) {
4513 426ebf2e 2019-07-25 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4515 426ebf2e 2019-07-25 stsp resp = ' ';
4518 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
4519 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4520 0ebf8283 2019-07-24 stsp commits, repo);
4521 426ebf2e 2019-07-25 stsp if (err) {
4522 426ebf2e 2019-07-25 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4524 426ebf2e 2019-07-25 stsp resp = ' ';
4527 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
4528 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4531 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
4534 0ebf8283 2019-07-24 stsp return err;
4537 0ebf8283 2019-07-24 stsp static const struct got_error *
4538 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
4539 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4540 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
4542 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4543 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4544 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4545 3e3a69f1 2019-07-25 stsp branch, repo);
4548 0ebf8283 2019-07-24 stsp static const struct got_error *
4549 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
4550 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4552 0ebf8283 2019-07-24 stsp const struct got_error *err;
4553 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4555 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
4557 0ebf8283 2019-07-24 stsp goto done;
4559 0ebf8283 2019-07-24 stsp if (new_id) {
4560 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
4562 0ebf8283 2019-07-24 stsp goto done;
4565 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
4566 0ebf8283 2019-07-24 stsp if (new_id_str)
4567 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
4569 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4570 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
4571 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
4572 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4573 0ebf8283 2019-07-24 stsp goto done;
4575 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
4577 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4579 0ebf8283 2019-07-24 stsp goto done;
4582 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
4583 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
4584 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
4585 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
4586 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4588 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
4589 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
4590 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4598 0ebf8283 2019-07-24 stsp free(old_id_str);
4599 0ebf8283 2019-07-24 stsp free(new_id_str);
4600 0ebf8283 2019-07-24 stsp return err;
4603 0ebf8283 2019-07-24 stsp static const struct got_error *
4604 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
4605 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4606 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4607 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4609 0ebf8283 2019-07-24 stsp const struct got_error *err;
4610 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4611 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
4613 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4614 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
4615 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4617 0ebf8283 2019-07-24 stsp return err;
4620 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4622 0ebf8283 2019-07-24 stsp return err;
4624 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4625 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
4626 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
4627 0ebf8283 2019-07-24 stsp if (err) {
4628 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4629 0ebf8283 2019-07-24 stsp goto done;
4630 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
4632 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
4633 0ebf8283 2019-07-24 stsp free(new_commit_id);
4636 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4637 0ebf8283 2019-07-24 stsp return err;
4640 0ebf8283 2019-07-24 stsp static const struct got_error *
4641 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
4642 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4644 0ebf8283 2019-07-24 stsp const struct got_error *error;
4645 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4647 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4649 0ebf8283 2019-07-24 stsp if (error)
4650 0ebf8283 2019-07-24 stsp return error;
4652 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4653 0ebf8283 2019-07-24 stsp if (error)
4654 0ebf8283 2019-07-24 stsp return error;
4656 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
4657 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4658 0ebf8283 2019-07-24 stsp return error;
4661 0ebf8283 2019-07-24 stsp static const struct got_error *
4662 0ebf8283 2019-07-24 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4663 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4665 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4666 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4667 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4668 0ebf8283 2019-07-24 stsp static char msg[80];
4669 0ebf8283 2019-07-24 stsp char *id_str;
4671 0ebf8283 2019-07-24 stsp if (TAILQ_EMPTY(histedit_cmds))
4672 ff6aa206 2019-07-25 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4673 ff6aa206 2019-07-25 stsp "histedit script contains no commands");
4675 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4676 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4677 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4680 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4681 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, qid->id);
4683 0ebf8283 2019-07-24 stsp return err;
4684 0ebf8283 2019-07-24 stsp snprintf(msg, sizeof(msg),
4685 0ebf8283 2019-07-24 stsp "commit %s missing from histedit script", id_str);
4686 0ebf8283 2019-07-24 stsp free(id_str);
4687 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4691 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4692 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4693 0ebf8283 2019-07-24 stsp "last commit in histedit script cannot be folded");
4695 0ebf8283 2019-07-24 stsp return NULL;
4698 0ebf8283 2019-07-24 stsp static const struct got_error *
4699 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
4701 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
4702 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
4703 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4704 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
4705 0ebf8283 2019-07-24 stsp char *cwd = NULL;
4706 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
4707 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
4708 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
4709 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
4710 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
4711 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4712 0ebf8283 2019-07-24 stsp int ch, rebase_in_progress = 0;
4713 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4714 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
4715 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4716 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
4717 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
4718 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
4719 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
4720 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
4721 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4723 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
4724 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
4725 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
4727 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
4728 0ebf8283 2019-07-24 stsp switch (ch) {
4730 0ebf8283 2019-07-24 stsp abort_edit = 1;
4733 0ebf8283 2019-07-24 stsp continue_edit = 1;
4736 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
4739 0ebf8283 2019-07-24 stsp usage_histedit();
4740 0ebf8283 2019-07-24 stsp /* NOTREACHED */
4744 0ebf8283 2019-07-24 stsp argc -= optind;
4745 0ebf8283 2019-07-24 stsp argv += optind;
4747 0ebf8283 2019-07-24 stsp #ifndef PROFILE
4748 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4749 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
4750 0ebf8283 2019-07-24 stsp err(1, "pledge");
4752 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
4753 0ebf8283 2019-07-24 stsp usage_histedit();
4754 0ebf8283 2019-07-24 stsp if (argc != 0)
4755 0ebf8283 2019-07-24 stsp usage_histedit();
4758 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
4759 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
4760 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
4761 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
4762 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
4763 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
4766 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
4767 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
4768 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
4769 0ebf8283 2019-07-24 stsp goto done;
4771 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
4772 0ebf8283 2019-07-24 stsp if (error)
4773 0ebf8283 2019-07-24 stsp goto done;
4775 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4776 0ebf8283 2019-07-24 stsp if (error != NULL)
4777 0ebf8283 2019-07-24 stsp goto done;
4779 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4780 0ebf8283 2019-07-24 stsp if (error)
4781 0ebf8283 2019-07-24 stsp goto done;
4782 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
4783 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
4784 0ebf8283 2019-07-24 stsp goto done;
4787 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4788 0ebf8283 2019-07-24 stsp if (error)
4789 0ebf8283 2019-07-24 stsp goto done;
4791 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
4792 0ebf8283 2019-07-24 stsp int did_something;
4793 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4794 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4795 3e3a69f1 2019-07-25 stsp worktree, repo);
4796 0ebf8283 2019-07-24 stsp if (error)
4797 0ebf8283 2019-07-24 stsp goto done;
4798 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4799 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4800 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
4801 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
4802 0ebf8283 2019-07-24 stsp if (error)
4803 0ebf8283 2019-07-24 stsp goto done;
4804 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
4805 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4806 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
4807 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
4808 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4809 0ebf8283 2019-07-24 stsp goto done;
4812 0ebf8283 2019-07-24 stsp if (continue_edit) {
4813 0ebf8283 2019-07-24 stsp char *path;
4815 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
4816 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4817 0ebf8283 2019-07-24 stsp goto done;
4820 0ebf8283 2019-07-24 stsp error = got_worktree_get_histedit_list_path(&path, worktree);
4821 0ebf8283 2019-07-24 stsp if (error)
4822 0ebf8283 2019-07-24 stsp goto done;
4824 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
4825 0ebf8283 2019-07-24 stsp free(path);
4826 0ebf8283 2019-07-24 stsp if (error)
4827 0ebf8283 2019-07-24 stsp goto done;
4829 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4830 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4831 3e3a69f1 2019-07-25 stsp worktree, repo);
4832 0ebf8283 2019-07-24 stsp if (error)
4833 0ebf8283 2019-07-24 stsp goto done;
4835 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4836 0ebf8283 2019-07-24 stsp if (error)
4837 0ebf8283 2019-07-24 stsp goto done;
4839 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4840 0ebf8283 2019-07-24 stsp head_commit_id);
4841 0ebf8283 2019-07-24 stsp if (error)
4842 0ebf8283 2019-07-24 stsp goto done;
4843 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4844 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4845 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4846 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4847 3aac7cf7 2019-07-25 stsp goto done;
4849 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4850 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
4851 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4853 0ebf8283 2019-07-24 stsp commit = NULL;
4854 0ebf8283 2019-07-24 stsp if (error)
4855 0ebf8283 2019-07-24 stsp goto done;
4857 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
4858 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
4859 0ebf8283 2019-07-24 stsp goto done;
4862 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
4863 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
4864 0ebf8283 2019-07-24 stsp if (error != NULL)
4865 0ebf8283 2019-07-24 stsp goto done;
4867 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4868 0ebf8283 2019-07-24 stsp if (error)
4869 0ebf8283 2019-07-24 stsp goto done;
4871 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4872 0ebf8283 2019-07-24 stsp head_commit_id);
4873 0ebf8283 2019-07-24 stsp if (error)
4874 0ebf8283 2019-07-24 stsp goto done;
4875 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4876 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4877 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4878 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4879 3aac7cf7 2019-07-25 stsp goto done;
4881 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4882 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
4883 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
4884 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4885 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4886 0ebf8283 2019-07-24 stsp commit = NULL;
4887 0ebf8283 2019-07-24 stsp if (error)
4888 0ebf8283 2019-07-24 stsp goto done;
4890 0ebf8283 2019-07-24 stsp if (edit_script_path) {
4891 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
4892 0ebf8283 2019-07-24 stsp edit_script_path, repo);
4893 0ebf8283 2019-07-24 stsp if (error)
4894 0ebf8283 2019-07-24 stsp goto done;
4896 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
4898 0ebf8283 2019-07-24 stsp if (error)
4899 0ebf8283 2019-07-24 stsp goto done;
4903 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
4905 0ebf8283 2019-07-24 stsp if (error)
4906 0ebf8283 2019-07-24 stsp goto done;
4908 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4909 3e3a69f1 2019-07-25 stsp &base_commit_id, &fileindex, worktree, repo);
4910 0ebf8283 2019-07-24 stsp if (error)
4911 0ebf8283 2019-07-24 stsp goto done;
4915 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
4916 0ebf8283 2019-07-24 stsp if (error)
4917 0ebf8283 2019-07-24 stsp goto done;
4919 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4920 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
4921 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
4922 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
4925 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
4926 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4927 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
4928 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
4931 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
4932 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
4934 0ebf8283 2019-07-24 stsp if (error)
4935 0ebf8283 2019-07-24 stsp goto done;
4939 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4940 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
4941 0ebf8283 2019-07-24 stsp if (error)
4942 0ebf8283 2019-07-24 stsp goto done;
4946 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4947 0ebf8283 2019-07-24 stsp hle->commit_id);
4948 0ebf8283 2019-07-24 stsp if (error)
4949 0ebf8283 2019-07-24 stsp goto done;
4950 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4951 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4953 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
4954 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
4955 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4956 0ebf8283 2019-07-24 stsp if (error)
4957 0ebf8283 2019-07-24 stsp goto done;
4958 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4959 0ebf8283 2019-07-24 stsp commit = NULL;
4961 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4962 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4966 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4967 0ebf8283 2019-07-24 stsp char *id_str;
4968 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
4969 0ebf8283 2019-07-24 stsp if (error)
4970 0ebf8283 2019-07-24 stsp goto done;
4971 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
4973 0ebf8283 2019-07-24 stsp free(id_str);
4974 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4975 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
4976 3e3a69f1 2019-07-25 stsp fileindex);
4977 0ebf8283 2019-07-24 stsp goto done;
4980 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4981 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
4982 0ebf8283 2019-07-24 stsp if (error)
4983 0ebf8283 2019-07-24 stsp goto done;
4987 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
4988 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
4989 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4990 0ebf8283 2019-07-24 stsp if (error)
4991 0ebf8283 2019-07-24 stsp goto done;
4994 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4995 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
4996 0ebf8283 2019-07-24 stsp if (error)
4997 0ebf8283 2019-07-24 stsp goto done;
4998 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4999 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5001 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5002 3e3a69f1 2019-07-25 stsp branch, repo);
5004 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5005 0ebf8283 2019-07-24 stsp free(head_commit_id);
5006 0ebf8283 2019-07-24 stsp free(base_commit_id);
5007 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5008 0ebf8283 2019-07-24 stsp if (commit)
5009 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5010 0ebf8283 2019-07-24 stsp if (branch)
5011 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5012 0ebf8283 2019-07-24 stsp if (tmp_branch)
5013 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5014 0ebf8283 2019-07-24 stsp if (worktree)
5015 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5017 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5018 0ebf8283 2019-07-24 stsp return error;