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 a367ff0f 2019-05-14 stsp return err;
761 4ed7e80c 2018-05-20 stsp static const struct got_error *
762 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
764 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
765 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
766 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
767 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
768 c09a553d 2018-03-12 stsp char *repo_path = NULL;
769 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
770 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
771 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
772 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
773 72151b04 2019-05-11 stsp int ch, same_path_prefix;
775 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
776 0bb8a95e 2018-03-12 stsp switch (ch) {
778 08573d5b 2019-05-14 stsp branch_name = optarg;
781 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
782 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
783 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
786 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
789 2deda0b9 2019-03-07 stsp usage_checkout();
790 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
794 0bb8a95e 2018-03-12 stsp argc -= optind;
795 0bb8a95e 2018-03-12 stsp argv += optind;
797 6715a751 2018-03-16 stsp #ifndef PROFILE
798 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
799 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
800 c09a553d 2018-03-12 stsp err(1, "pledge");
802 0bb8a95e 2018-03-12 stsp if (argc == 1) {
803 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
804 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
805 76089277 2018-04-01 stsp if (repo_path == NULL)
806 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
807 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
808 76089277 2018-04-01 stsp if (cwd == NULL) {
809 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
812 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
813 230a42bd 2019-05-11 jcs base = basename(path_prefix);
814 230a42bd 2019-05-11 jcs if (base == NULL) {
815 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
816 230a42bd 2019-05-11 jcs path_prefix);
820 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
821 230a42bd 2019-05-11 jcs if (base == NULL) {
822 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
827 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
828 c09a553d 2018-03-12 stsp if (dotgit)
829 c09a553d 2018-03-12 stsp *dotgit = '\0';
830 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
831 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
836 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
837 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
838 76089277 2018-04-01 stsp if (repo_path == NULL) {
839 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
842 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
843 76089277 2018-04-01 stsp if (worktree_path == NULL) {
844 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
845 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
849 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
850 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
851 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
856 c09a553d 2018-03-12 stsp usage_checkout();
858 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
859 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
861 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
862 c09a553d 2018-03-12 stsp if (error != NULL)
865 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
866 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
867 c530dc23 2019-07-23 stsp if (error) {
868 c530dc23 2019-07-23 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
870 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
871 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
872 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
877 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
881 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
882 c09a553d 2018-03-12 stsp if (error != NULL)
885 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
886 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
889 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
890 c09a553d 2018-03-12 stsp if (error != NULL)
893 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
894 e5dc7198 2018-12-29 stsp path_prefix);
895 e5dc7198 2018-12-29 stsp if (error != NULL)
897 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
898 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
902 8069f636 2019-01-12 stsp if (commit_id_str) {
903 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
904 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
905 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
906 8069f636 2019-01-12 stsp if (error != NULL)
908 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
909 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
910 8069f636 2019-01-12 stsp if (error != NULL) {
911 8069f636 2019-01-12 stsp free(commit_id);
914 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
917 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
918 8069f636 2019-01-12 stsp commit_id);
919 8069f636 2019-01-12 stsp free(commit_id);
924 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
925 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
926 c09a553d 2018-03-12 stsp if (error != NULL)
929 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
932 8069f636 2019-01-12 stsp free(commit_id_str);
933 76089277 2018-04-01 stsp free(repo_path);
934 507dc3bb 2018-12-29 stsp free(worktree_path);
935 507dc3bb 2018-12-29 stsp return error;
938 507dc3bb 2018-12-29 stsp __dead static void
939 507dc3bb 2018-12-29 stsp usage_update(void)
941 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
942 507dc3bb 2018-12-29 stsp getprogname());
946 1ee397ad 2019-07-12 stsp static const struct got_error *
947 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
949 784955db 2019-01-12 stsp int *did_something = arg;
951 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
952 1ee397ad 2019-07-12 stsp return NULL;
954 1545c615 2019-02-10 stsp *did_something = 1;
956 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
957 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
958 1ee397ad 2019-07-12 stsp return NULL;
960 507dc3bb 2018-12-29 stsp while (path[0] == '/')
962 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
963 1ee397ad 2019-07-12 stsp return NULL;
966 be7061eb 2018-12-30 stsp static const struct got_error *
967 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
968 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
969 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
971 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
972 a1fb16d8 2019-05-24 stsp char *base_id_str;
973 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
975 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
976 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
977 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
978 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
979 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
980 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
981 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
984 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
985 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
987 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
988 a1fb16d8 2019-05-24 stsp return err;
989 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
991 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
992 a1fb16d8 2019-05-24 stsp return NULL;
994 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
995 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
996 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
998 a1fb16d8 2019-05-24 stsp return err;
999 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1000 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1001 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1002 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1003 0ebf8283 2019-07-24 stsp return NULL;
1006 0ebf8283 2019-07-24 stsp static const struct got_error *
1007 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1009 0ebf8283 2019-07-24 stsp const struct got_error *err;
1010 0ebf8283 2019-07-24 stsp int in_progress;
1012 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1014 0ebf8283 2019-07-24 stsp return err;
1015 0ebf8283 2019-07-24 stsp if (in_progress)
1016 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1018 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1020 0ebf8283 2019-07-24 stsp return err;
1021 0ebf8283 2019-07-24 stsp if (in_progress)
1022 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1024 a1fb16d8 2019-05-24 stsp return NULL;
1027 a1fb16d8 2019-05-24 stsp static const struct got_error *
1028 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1030 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1031 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1032 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1033 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
1034 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1035 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1036 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1037 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1038 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1040 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1041 507dc3bb 2018-12-29 stsp switch (ch) {
1043 024e9686 2019-05-14 stsp branch_name = optarg;
1046 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1047 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1048 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1051 2deda0b9 2019-03-07 stsp usage_update();
1052 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1056 507dc3bb 2018-12-29 stsp argc -= optind;
1057 507dc3bb 2018-12-29 stsp argv += optind;
1059 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1060 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1061 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1062 507dc3bb 2018-12-29 stsp err(1, "pledge");
1064 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1065 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1066 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1067 c4cdcb68 2019-04-03 stsp goto done;
1069 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1070 7d5807f4 2019-07-11 stsp if (error)
1071 7d5807f4 2019-07-11 stsp goto done;
1073 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1074 c4cdcb68 2019-04-03 stsp if (error)
1075 7d5807f4 2019-07-11 stsp goto done;
1077 507dc3bb 2018-12-29 stsp if (argc == 0) {
1078 c4cdcb68 2019-04-03 stsp path = strdup("");
1079 c4cdcb68 2019-04-03 stsp if (path == NULL) {
1080 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1081 507dc3bb 2018-12-29 stsp goto done;
1083 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
1084 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1085 c4cdcb68 2019-04-03 stsp if (error)
1086 507dc3bb 2018-12-29 stsp goto done;
1088 507dc3bb 2018-12-29 stsp usage_update();
1090 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1091 507dc3bb 2018-12-29 stsp if (error != NULL)
1092 507dc3bb 2018-12-29 stsp goto done;
1094 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1095 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1096 0266afb7 2019-01-04 stsp if (error)
1097 0266afb7 2019-01-04 stsp goto done;
1099 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1100 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1101 024e9686 2019-05-14 stsp if (error != NULL)
1102 024e9686 2019-05-14 stsp goto done;
1103 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1104 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1105 9c4b8182 2019-01-02 stsp if (error != NULL)
1106 9c4b8182 2019-01-02 stsp goto done;
1107 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1108 507dc3bb 2018-12-29 stsp if (error != NULL)
1109 507dc3bb 2018-12-29 stsp goto done;
1111 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1112 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1113 507dc3bb 2018-12-29 stsp if (error != NULL)
1114 a297e751 2019-07-11 stsp goto done;
1115 a297e751 2019-07-11 stsp free(commit_id_str);
1116 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1117 a297e751 2019-07-11 stsp if (error)
1118 507dc3bb 2018-12-29 stsp goto done;
1121 a1fb16d8 2019-05-24 stsp if (branch_name) {
1122 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1123 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
1124 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
1125 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
1126 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
1127 024e9686 2019-05-14 stsp getprogname(), path);
1128 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
1129 024e9686 2019-05-14 stsp goto done;
1131 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1132 024e9686 2019-05-14 stsp if (error)
1133 024e9686 2019-05-14 stsp goto done;
1134 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1135 a367ff0f 2019-05-14 stsp free(head_commit_id);
1136 024e9686 2019-05-14 stsp if (error != NULL)
1137 024e9686 2019-05-14 stsp goto done;
1138 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1139 a367ff0f 2019-05-14 stsp if (error)
1140 a367ff0f 2019-05-14 stsp goto done;
1141 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1142 024e9686 2019-05-14 stsp if (error)
1143 024e9686 2019-05-14 stsp goto done;
1145 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1146 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1147 a367ff0f 2019-05-14 stsp if (error != NULL) {
1148 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1149 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1150 024e9686 2019-05-14 stsp goto done;
1152 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1153 a367ff0f 2019-05-14 stsp if (error)
1154 a367ff0f 2019-05-14 stsp goto done;
1157 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1158 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1159 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1160 507dc3bb 2018-12-29 stsp commit_id);
1161 507dc3bb 2018-12-29 stsp if (error)
1162 507dc3bb 2018-12-29 stsp goto done;
1165 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
1166 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1167 507dc3bb 2018-12-29 stsp if (error != NULL)
1168 507dc3bb 2018-12-29 stsp goto done;
1170 784955db 2019-01-12 stsp if (did_something)
1171 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1173 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1175 c09a553d 2018-03-12 stsp free(worktree_path);
1176 c4cdcb68 2019-04-03 stsp free(path);
1177 507dc3bb 2018-12-29 stsp free(commit_id);
1178 9c4b8182 2019-01-02 stsp free(commit_id_str);
1179 c09a553d 2018-03-12 stsp return error;
1182 f42b1b34 2018-03-12 stsp static const struct got_error *
1183 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1184 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1186 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1187 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1188 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1189 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1190 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1192 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1193 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1195 79109fed 2018-03-27 stsp return err;
1197 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1198 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1199 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1201 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1203 79109fed 2018-03-27 stsp return err;
1205 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1206 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1207 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1209 0f2b3dca 2018-12-22 stsp return err;
1211 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1213 79109fed 2018-03-27 stsp return err;
1216 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1218 0f2b3dca 2018-12-22 stsp goto done;
1220 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1221 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1222 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1223 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1224 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
1226 79109fed 2018-03-27 stsp if (tree1)
1227 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1228 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1229 0f2b3dca 2018-12-22 stsp free(id_str1);
1230 0f2b3dca 2018-12-22 stsp free(id_str2);
1231 79109fed 2018-03-27 stsp return err;
1234 4bb494d5 2018-06-16 stsp static char *
1235 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1237 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1238 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1240 6c281f94 2018-06-11 stsp *p = '\0';
1244 79109fed 2018-03-27 stsp static const struct got_error *
1245 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1246 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1247 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1249 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1250 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1251 6c281f94 2018-06-11 stsp char datebuf[26];
1252 45d799e2 2018-12-23 stsp time_t committer_time;
1253 45d799e2 2018-12-23 stsp const char *author, *committer;
1254 199a4027 2019-02-02 stsp char *refs_str = NULL;
1255 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1257 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1259 199a4027 2019-02-02 stsp const char *name;
1260 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1262 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1263 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1265 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1266 199a4027 2019-02-02 stsp name += 5;
1267 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1269 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1270 e34f9ed6 2019-02-02 stsp name += 6;
1271 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1272 141c2bff 2019-02-04 stsp name += 8;
1273 199a4027 2019-02-02 stsp s = refs_str;
1274 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1275 199a4027 2019-02-02 stsp name) == -1) {
1276 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1282 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1284 8bf5b3c9 2018-03-17 stsp return err;
1286 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1287 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1288 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1289 832c249c 2018-06-10 stsp free(id_str);
1290 d3d493d7 2019-02-21 stsp id_str = NULL;
1291 d3d493d7 2019-02-21 stsp free(refs_str);
1292 d3d493d7 2019-02-21 stsp refs_str = NULL;
1293 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1294 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1295 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1296 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1297 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1298 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1299 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1300 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1301 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1302 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1303 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1304 3fe1abad 2018-06-10 stsp int n = 1;
1305 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1306 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1307 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1309 a0603db2 2018-06-10 stsp return err;
1310 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1311 a0603db2 2018-06-10 stsp free(id_str);
1315 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1316 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1317 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1319 621015ac 2018-07-23 stsp logmsg = logmsg0;
1321 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1323 832c249c 2018-06-10 stsp printf(" %s\n", line);
1324 832c249c 2018-06-10 stsp } while (line);
1325 621015ac 2018-07-23 stsp free(logmsg0);
1327 971751ac 2018-03-27 stsp if (show_patch) {
1328 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1329 971751ac 2018-03-27 stsp if (err == 0)
1330 971751ac 2018-03-27 stsp printf("\n");
1333 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1334 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1335 07862c20 2018-09-15 stsp return err;
1338 f42b1b34 2018-03-12 stsp static const struct got_error *
1339 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1340 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1341 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1343 f42b1b34 2018-03-12 stsp const struct got_error *err;
1344 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1346 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1347 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1349 f42b1b34 2018-03-12 stsp return err;
1350 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1352 fcc85cad 2018-07-22 stsp goto done;
1354 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1355 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1357 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1360 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1361 372ccdbb 2018-06-10 stsp if (err) {
1362 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1363 9ba79e04 2018-06-11 stsp err = NULL;
1366 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1368 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1374 b43fbaa0 2018-06-11 stsp if (id == NULL)
1377 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1380 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1382 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1383 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1387 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1388 f42b1b34 2018-03-12 stsp return err;
1391 4ed7e80c 2018-05-20 stsp __dead static void
1392 6f3d1eb0 2018-03-12 stsp usage_log(void)
1394 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1395 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1399 4ed7e80c 2018-05-20 stsp static const struct got_error *
1400 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1402 f42b1b34 2018-03-12 stsp const struct got_error *error;
1403 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1404 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1405 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1406 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1407 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1408 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1409 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1410 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1411 64a96a6d 2018-04-01 stsp const char *errstr;
1412 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1414 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1416 6715a751 2018-03-16 stsp #ifndef PROFILE
1417 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1420 f42b1b34 2018-03-12 stsp err(1, "pledge");
1423 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1424 79109fed 2018-03-27 stsp switch (ch) {
1426 79109fed 2018-03-27 stsp show_patch = 1;
1429 d142fc45 2018-04-01 stsp start_commit = optarg;
1432 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1434 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1435 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1438 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1439 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1440 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1443 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1446 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1447 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1448 04ca23f4 2018-07-16 stsp err(1, "-r option");
1449 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1452 2deda0b9 2019-03-07 stsp usage_log();
1453 79109fed 2018-03-27 stsp /* NOTREACHED */
1457 79109fed 2018-03-27 stsp argc -= optind;
1458 79109fed 2018-03-27 stsp argv += optind;
1460 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1461 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1462 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1463 04ca23f4 2018-07-16 stsp goto done;
1466 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1467 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1468 cffc0aa4 2019-02-05 stsp goto done;
1469 cffc0aa4 2019-02-05 stsp error = NULL;
1471 e7301579 2019-03-18 stsp if (argc == 0) {
1472 cbd1af7a 2019-03-18 stsp path = strdup("");
1473 e7301579 2019-03-18 stsp if (path == NULL) {
1474 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1475 cbd1af7a 2019-03-18 stsp goto done;
1477 e7301579 2019-03-18 stsp } else if (argc == 1) {
1478 e7301579 2019-03-18 stsp if (worktree) {
1479 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1481 e7301579 2019-03-18 stsp if (error)
1482 e7301579 2019-03-18 stsp goto done;
1484 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1485 e7301579 2019-03-18 stsp if (path == NULL) {
1486 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1487 e7301579 2019-03-18 stsp goto done;
1491 cbd1af7a 2019-03-18 stsp usage_log();
1493 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1494 5486daa2 2019-05-11 stsp repo_path = worktree ?
1495 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1497 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1498 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1499 cffc0aa4 2019-02-05 stsp goto done;
1502 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1503 d7d4f210 2018-03-12 stsp if (error != NULL)
1504 04ca23f4 2018-07-16 stsp goto done;
1506 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1507 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1508 c02c541e 2019-03-29 stsp if (error)
1509 c02c541e 2019-03-29 stsp goto done;
1511 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1512 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1513 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1514 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1515 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1516 3235492e 2018-04-01 stsp if (error != NULL)
1517 3235492e 2018-04-01 stsp return error;
1518 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1519 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1520 3235492e 2018-04-01 stsp if (error != NULL)
1521 3235492e 2018-04-01 stsp return error;
1522 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1524 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1525 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1526 3235492e 2018-04-01 stsp if (error == NULL) {
1527 1caad61b 2019-02-01 stsp int obj_type;
1528 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1529 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1530 d1f2edc9 2018-06-13 stsp if (error != NULL)
1531 1caad61b 2019-02-01 stsp goto done;
1532 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1533 1caad61b 2019-02-01 stsp if (error != NULL)
1534 1caad61b 2019-02-01 stsp goto done;
1535 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1536 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1537 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1538 1caad61b 2019-02-01 stsp if (error != NULL)
1539 1caad61b 2019-02-01 stsp goto done;
1540 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1541 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1542 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1543 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1544 1caad61b 2019-02-01 stsp goto done;
1547 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1548 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1549 1caad61b 2019-02-01 stsp if (id == NULL)
1550 638f9024 2019-05-13 stsp error = got_error_from_errno(
1551 230a42bd 2019-05-11 jcs "got_object_id_dup");
1552 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1553 1caad61b 2019-02-01 stsp if (error)
1554 1caad61b 2019-02-01 stsp goto done;
1555 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1556 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1557 1caad61b 2019-02-01 stsp goto done;
1559 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1560 d1f2edc9 2018-06-13 stsp if (error != NULL)
1561 1caad61b 2019-02-01 stsp goto done;
1563 15a94983 2018-12-23 stsp if (commit == NULL) {
1564 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1565 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1566 d1f2edc9 2018-06-13 stsp if (error != NULL)
1567 d1f2edc9 2018-06-13 stsp return error;
1570 d7d4f210 2018-03-12 stsp if (error != NULL)
1571 04ca23f4 2018-07-16 stsp goto done;
1573 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1574 04ca23f4 2018-07-16 stsp if (error != NULL)
1575 04ca23f4 2018-07-16 stsp goto done;
1576 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1577 04ca23f4 2018-07-16 stsp free(path);
1578 04ca23f4 2018-07-16 stsp path = in_repo_path;
1581 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1582 199a4027 2019-02-02 stsp if (error)
1583 199a4027 2019-02-02 stsp goto done;
1585 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1586 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1588 04ca23f4 2018-07-16 stsp free(path);
1589 04ca23f4 2018-07-16 stsp free(repo_path);
1590 04ca23f4 2018-07-16 stsp free(cwd);
1592 cffc0aa4 2019-02-05 stsp if (worktree)
1593 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1594 ad242220 2018-09-08 stsp if (repo) {
1595 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1596 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1597 ad242220 2018-09-08 stsp if (error == NULL)
1598 ad242220 2018-09-08 stsp error = repo_error;
1600 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1601 8bf5b3c9 2018-03-17 stsp return error;
1604 4ed7e80c 2018-05-20 stsp __dead static void
1605 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1607 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1608 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1612 b72f483a 2019-02-05 stsp struct print_diff_arg {
1613 b72f483a 2019-02-05 stsp struct got_repository *repo;
1614 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1615 b72f483a 2019-02-05 stsp int diff_context;
1616 3fc0c068 2019-02-10 stsp const char *id_str;
1617 3fc0c068 2019-02-10 stsp int header_shown;
1620 4ed7e80c 2018-05-20 stsp static const struct got_error *
1621 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1622 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1624 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1625 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1626 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1627 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1628 b72f483a 2019-02-05 stsp char *abspath = NULL;
1629 b72f483a 2019-02-05 stsp struct stat sb;
1631 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1632 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1633 b72f483a 2019-02-05 stsp return NULL;
1635 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1636 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1637 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1638 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1641 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1642 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1644 d00136be 2019-03-26 stsp goto done;
1648 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1649 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1650 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1651 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1652 2ec1f75b 2019-03-26 stsp goto done;
1655 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1656 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1657 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1658 2ec1f75b 2019-03-26 stsp goto done;
1660 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1661 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1662 2ec1f75b 2019-03-26 stsp goto done;
1665 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1667 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1670 b72f483a 2019-02-05 stsp if (blob1)
1671 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1672 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1673 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1674 b72f483a 2019-02-05 stsp free(abspath);
1675 927df6b7 2019-02-10 stsp return err;
1678 927df6b7 2019-02-10 stsp static const struct got_error *
1679 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1681 b00d56cd 2018-04-01 stsp const struct got_error *error;
1682 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1683 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1684 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1685 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1686 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1687 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1688 15a94983 2018-12-23 stsp int type1, type2;
1689 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1690 c0cc5c62 2018-10-18 stsp const char *errstr;
1691 927df6b7 2019-02-10 stsp char *path = NULL;
1693 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1694 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1695 25eccc22 2019-01-04 stsp NULL) == -1)
1696 b00d56cd 2018-04-01 stsp err(1, "pledge");
1699 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1700 b00d56cd 2018-04-01 stsp switch (ch) {
1702 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1703 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1704 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1707 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1708 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1709 b72f483a 2019-02-05 stsp err(1, "-r option");
1710 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1713 2deda0b9 2019-03-07 stsp usage_diff();
1714 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1718 b00d56cd 2018-04-01 stsp argc -= optind;
1719 b00d56cd 2018-04-01 stsp argv += optind;
1721 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1722 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1723 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1724 b72f483a 2019-02-05 stsp goto done;
1726 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1727 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1728 927df6b7 2019-02-10 stsp goto done;
1729 927df6b7 2019-02-10 stsp if (argc <= 1) {
1730 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1731 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1732 927df6b7 2019-02-10 stsp goto done;
1734 b72f483a 2019-02-05 stsp if (repo_path)
1736 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1737 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1738 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1739 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1740 927df6b7 2019-02-10 stsp goto done;
1742 927df6b7 2019-02-10 stsp if (argc == 1) {
1743 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1745 927df6b7 2019-02-10 stsp if (error)
1746 927df6b7 2019-02-10 stsp goto done;
1748 927df6b7 2019-02-10 stsp path = strdup("");
1749 927df6b7 2019-02-10 stsp if (path == NULL) {
1750 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1751 927df6b7 2019-02-10 stsp goto done;
1754 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1755 15a94983 2018-12-23 stsp id_str1 = argv[0];
1756 15a94983 2018-12-23 stsp id_str2 = argv[1];
1757 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1758 30db809c 2019-06-05 stsp repo_path =
1759 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1760 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1761 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1762 30db809c 2019-06-05 stsp goto done;
1766 b00d56cd 2018-04-01 stsp usage_diff();
1768 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1769 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1770 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1771 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1774 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1775 76089277 2018-04-01 stsp free(repo_path);
1776 b00d56cd 2018-04-01 stsp if (error != NULL)
1777 b00d56cd 2018-04-01 stsp goto done;
1779 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1780 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1781 c02c541e 2019-03-29 stsp if (error)
1782 c02c541e 2019-03-29 stsp goto done;
1784 30db809c 2019-06-05 stsp if (argc <= 1) {
1785 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1786 b72f483a 2019-02-05 stsp char *id_str;
1787 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1788 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1789 b72f483a 2019-02-05 stsp if (error)
1790 b72f483a 2019-02-05 stsp goto done;
1791 b72f483a 2019-02-05 stsp arg.repo = repo;
1792 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1793 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1794 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1795 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1797 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1798 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1799 b72f483a 2019-02-05 stsp free(id_str);
1800 b72f483a 2019-02-05 stsp goto done;
1803 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1804 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1805 e02e74af 2019-05-28 stsp if (error) {
1806 e02e74af 2019-05-28 stsp struct got_reference *ref;
1807 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1808 e02e74af 2019-05-28 stsp goto done;
1809 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1810 e02e74af 2019-05-28 stsp if (error != NULL)
1811 e02e74af 2019-05-28 stsp goto done;
1812 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1813 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1814 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1815 5e70831e 2019-05-28 stsp goto done;
1817 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1818 e02e74af 2019-05-28 stsp got_ref_close(ref);
1819 e02e74af 2019-05-28 stsp if (error != NULL)
1820 5e70831e 2019-05-28 stsp goto done;
1822 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1823 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1824 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1825 e02e74af 2019-05-28 stsp goto done;
1829 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1830 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1831 e02e74af 2019-05-28 stsp if (error) {
1832 e02e74af 2019-05-28 stsp struct got_reference *ref;
1833 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1834 e02e74af 2019-05-28 stsp goto done;
1835 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1836 e02e74af 2019-05-28 stsp if (error != NULL)
1837 5e70831e 2019-05-28 stsp goto done;
1838 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1839 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1840 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1841 e02e74af 2019-05-28 stsp goto done;
1843 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1844 e02e74af 2019-05-28 stsp got_ref_close(ref);
1845 e02e74af 2019-05-28 stsp if (error != NULL)
1846 e02e74af 2019-05-28 stsp goto done;
1848 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1849 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1850 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1851 5e70831e 2019-05-28 stsp goto done;
1855 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1856 15a94983 2018-12-23 stsp if (error)
1857 15a94983 2018-12-23 stsp goto done;
1859 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1860 15a94983 2018-12-23 stsp if (error)
1861 15a94983 2018-12-23 stsp goto done;
1863 15a94983 2018-12-23 stsp if (type1 != type2) {
1864 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1865 b00d56cd 2018-04-01 stsp goto done;
1868 15a94983 2018-12-23 stsp switch (type1) {
1869 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1870 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1871 54156555 2018-12-24 stsp diff_context, repo, stdout);
1873 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1874 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1875 54156555 2018-12-24 stsp diff_context, repo, stdout);
1877 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1878 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1879 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1880 c0cc5c62 2018-10-18 stsp repo, stdout);
1883 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1887 5e70831e 2019-05-28 stsp free(label1);
1888 5e70831e 2019-05-28 stsp free(label2);
1889 15a94983 2018-12-23 stsp free(id1);
1890 15a94983 2018-12-23 stsp free(id2);
1891 927df6b7 2019-02-10 stsp free(path);
1892 b72f483a 2019-02-05 stsp if (worktree)
1893 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1894 ad242220 2018-09-08 stsp if (repo) {
1895 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1896 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1897 ad242220 2018-09-08 stsp if (error == NULL)
1898 ad242220 2018-09-08 stsp error = repo_error;
1900 b00d56cd 2018-04-01 stsp return error;
1903 404c43c4 2018-06-21 stsp __dead static void
1904 404c43c4 2018-06-21 stsp usage_blame(void)
1906 9270e621 2019-02-05 stsp fprintf(stderr,
1907 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1908 404c43c4 2018-06-21 stsp getprogname());
1912 404c43c4 2018-06-21 stsp static const struct got_error *
1913 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1915 404c43c4 2018-06-21 stsp const struct got_error *error;
1916 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1917 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1918 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1919 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1920 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1923 404c43c4 2018-06-21 stsp #ifndef PROFILE
1924 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1925 36e2fb66 2019-01-04 stsp NULL) == -1)
1926 404c43c4 2018-06-21 stsp err(1, "pledge");
1929 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1930 404c43c4 2018-06-21 stsp switch (ch) {
1932 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1935 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1936 66bea077 2018-08-02 stsp if (repo_path == NULL)
1937 66bea077 2018-08-02 stsp err(1, "-r option");
1938 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1941 2deda0b9 2019-03-07 stsp usage_blame();
1942 404c43c4 2018-06-21 stsp /* NOTREACHED */
1946 404c43c4 2018-06-21 stsp argc -= optind;
1947 404c43c4 2018-06-21 stsp argv += optind;
1949 a39318fd 2018-08-02 stsp if (argc == 1)
1950 404c43c4 2018-06-21 stsp path = argv[0];
1952 404c43c4 2018-06-21 stsp usage_blame();
1954 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1955 66bea077 2018-08-02 stsp if (cwd == NULL) {
1956 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1957 66bea077 2018-08-02 stsp goto done;
1959 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1960 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1961 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1962 66bea077 2018-08-02 stsp goto done;
1964 0c06baac 2019-02-05 stsp error = NULL;
1965 0c06baac 2019-02-05 stsp if (worktree) {
1966 0c06baac 2019-02-05 stsp repo_path =
1967 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1968 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1969 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1970 0c06baac 2019-02-05 stsp if (error)
1971 0c06baac 2019-02-05 stsp goto done;
1973 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1974 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1975 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1976 0c06baac 2019-02-05 stsp goto done;
1981 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1982 c02c541e 2019-03-29 stsp if (error != NULL)
1983 36e2fb66 2019-01-04 stsp goto done;
1985 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1986 c02c541e 2019-03-29 stsp if (error)
1987 404c43c4 2018-06-21 stsp goto done;
1989 0c06baac 2019-02-05 stsp if (worktree) {
1990 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1991 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1992 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1993 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1994 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1995 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1996 6efaaa2d 2019-02-05 stsp path) == -1) {
1997 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1998 0c06baac 2019-02-05 stsp goto done;
2000 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2003 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2005 0c06baac 2019-02-05 stsp if (error)
2006 66bea077 2018-08-02 stsp goto done;
2008 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2009 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2010 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2011 404c43c4 2018-06-21 stsp if (error != NULL)
2012 66bea077 2018-08-02 stsp goto done;
2013 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2014 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2015 404c43c4 2018-06-21 stsp if (error != NULL)
2016 66bea077 2018-08-02 stsp goto done;
2018 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
2019 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2020 404c43c4 2018-06-21 stsp if (error != NULL)
2021 66bea077 2018-08-02 stsp goto done;
2024 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2026 66bea077 2018-08-02 stsp free(in_repo_path);
2027 66bea077 2018-08-02 stsp free(repo_path);
2028 66bea077 2018-08-02 stsp free(cwd);
2029 404c43c4 2018-06-21 stsp free(commit_id);
2030 0c06baac 2019-02-05 stsp if (worktree)
2031 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2032 ad242220 2018-09-08 stsp if (repo) {
2033 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2034 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2035 ad242220 2018-09-08 stsp if (error == NULL)
2036 ad242220 2018-09-08 stsp error = repo_error;
2038 404c43c4 2018-06-21 stsp return error;
2041 5de5890b 2018-10-18 stsp __dead static void
2042 5de5890b 2018-10-18 stsp usage_tree(void)
2044 c1669e2e 2019-01-09 stsp fprintf(stderr,
2045 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2046 5de5890b 2018-10-18 stsp getprogname());
2050 c1669e2e 2019-01-09 stsp static void
2051 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2052 c1669e2e 2019-01-09 stsp const char *root_path)
2054 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2056 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2057 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2060 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2061 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2062 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2065 5de5890b 2018-10-18 stsp static const struct got_error *
2066 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2067 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2068 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2070 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2071 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2072 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2073 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2074 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2076 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2078 5de5890b 2018-10-18 stsp goto done;
2080 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2082 5de5890b 2018-10-18 stsp goto done;
2083 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2084 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2085 5de5890b 2018-10-18 stsp while (te) {
2086 5de5890b 2018-10-18 stsp char *id = NULL;
2088 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2091 5de5890b 2018-10-18 stsp if (show_ids) {
2092 5de5890b 2018-10-18 stsp char *id_str;
2093 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2095 5de5890b 2018-10-18 stsp goto done;
2096 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2097 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2098 5de5890b 2018-10-18 stsp free(id_str);
2099 5de5890b 2018-10-18 stsp goto done;
2101 5de5890b 2018-10-18 stsp free(id_str);
2103 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2106 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2107 c1669e2e 2019-01-09 stsp char *child_path;
2108 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2109 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2110 c1669e2e 2019-01-09 stsp te->name) == -1) {
2111 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2112 c1669e2e 2019-01-09 stsp goto done;
2114 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2115 c1669e2e 2019-01-09 stsp root_path, repo);
2116 c1669e2e 2019-01-09 stsp free(child_path);
2118 c1669e2e 2019-01-09 stsp goto done;
2121 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2125 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2126 5de5890b 2018-10-18 stsp free(tree_id);
2127 5de5890b 2018-10-18 stsp return err;
2130 5de5890b 2018-10-18 stsp static const struct got_error *
2131 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2133 5de5890b 2018-10-18 stsp const struct got_error *error;
2134 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2135 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2136 7a2c19d6 2019-02-05 stsp const char *path;
2137 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2138 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2139 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2140 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2143 5de5890b 2018-10-18 stsp #ifndef PROFILE
2144 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2145 0f8d269b 2019-01-04 stsp NULL) == -1)
2146 5de5890b 2018-10-18 stsp err(1, "pledge");
2149 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2150 5de5890b 2018-10-18 stsp switch (ch) {
2152 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2155 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2156 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2157 5de5890b 2018-10-18 stsp err(1, "-r option");
2158 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2161 5de5890b 2018-10-18 stsp show_ids = 1;
2164 c1669e2e 2019-01-09 stsp recurse = 1;
2167 2deda0b9 2019-03-07 stsp usage_tree();
2168 5de5890b 2018-10-18 stsp /* NOTREACHED */
2172 5de5890b 2018-10-18 stsp argc -= optind;
2173 5de5890b 2018-10-18 stsp argv += optind;
2175 5de5890b 2018-10-18 stsp if (argc == 1)
2176 5de5890b 2018-10-18 stsp path = argv[0];
2177 5de5890b 2018-10-18 stsp else if (argc > 1)
2178 5de5890b 2018-10-18 stsp usage_tree();
2180 7a2c19d6 2019-02-05 stsp path = NULL;
2182 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2183 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2184 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2185 9bf7a39b 2019-02-05 stsp goto done;
2187 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2188 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2189 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2190 7a2c19d6 2019-02-05 stsp goto done;
2192 7a2c19d6 2019-02-05 stsp error = NULL;
2193 7a2c19d6 2019-02-05 stsp if (worktree) {
2194 7a2c19d6 2019-02-05 stsp repo_path =
2195 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2196 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2197 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2198 7a2c19d6 2019-02-05 stsp if (error)
2199 7a2c19d6 2019-02-05 stsp goto done;
2201 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2202 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2203 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2204 7a2c19d6 2019-02-05 stsp goto done;
2209 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2210 5de5890b 2018-10-18 stsp if (error != NULL)
2211 c02c541e 2019-03-29 stsp goto done;
2213 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2214 c02c541e 2019-03-29 stsp if (error)
2215 5de5890b 2018-10-18 stsp goto done;
2217 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2218 9bf7a39b 2019-02-05 stsp if (worktree) {
2219 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2220 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2221 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2222 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2223 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2224 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2225 9bf7a39b 2019-02-05 stsp goto done;
2227 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2229 9bf7a39b 2019-02-05 stsp if (error)
2230 9bf7a39b 2019-02-05 stsp goto done;
2232 9bf7a39b 2019-02-05 stsp path = "/";
2234 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2235 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2236 9bf7a39b 2019-02-05 stsp if (error != NULL)
2237 9bf7a39b 2019-02-05 stsp goto done;
2240 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2241 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2242 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2243 5de5890b 2018-10-18 stsp if (error != NULL)
2244 5de5890b 2018-10-18 stsp goto done;
2245 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2246 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2247 5de5890b 2018-10-18 stsp if (error != NULL)
2248 5de5890b 2018-10-18 stsp goto done;
2250 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
2251 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2252 5de5890b 2018-10-18 stsp if (error != NULL)
2253 5de5890b 2018-10-18 stsp goto done;
2256 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2257 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2259 5de5890b 2018-10-18 stsp free(in_repo_path);
2260 5de5890b 2018-10-18 stsp free(repo_path);
2261 5de5890b 2018-10-18 stsp free(cwd);
2262 5de5890b 2018-10-18 stsp free(commit_id);
2263 7a2c19d6 2019-02-05 stsp if (worktree)
2264 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2265 5de5890b 2018-10-18 stsp if (repo) {
2266 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2267 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2268 5de5890b 2018-10-18 stsp if (error == NULL)
2269 5de5890b 2018-10-18 stsp error = repo_error;
2271 5de5890b 2018-10-18 stsp return error;
2274 6bad629b 2019-02-04 stsp __dead static void
2275 6bad629b 2019-02-04 stsp usage_status(void)
2277 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
2281 b72f483a 2019-02-05 stsp static const struct got_error *
2282 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2283 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2285 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2286 b72f483a 2019-02-05 stsp return NULL;
2289 6bad629b 2019-02-04 stsp static const struct got_error *
2290 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2292 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2293 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2294 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2295 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
2298 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2299 6bad629b 2019-02-04 stsp switch (ch) {
2301 2deda0b9 2019-03-07 stsp usage_status();
2302 6bad629b 2019-02-04 stsp /* NOTREACHED */
2306 6bad629b 2019-02-04 stsp argc -= optind;
2307 6bad629b 2019-02-04 stsp argv += optind;
2309 6bad629b 2019-02-04 stsp #ifndef PROFILE
2310 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2311 6bad629b 2019-02-04 stsp NULL) == -1)
2312 6bad629b 2019-02-04 stsp err(1, "pledge");
2314 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2315 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2316 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2317 927df6b7 2019-02-10 stsp goto done;
2320 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2321 927df6b7 2019-02-10 stsp if (error != NULL)
2322 927df6b7 2019-02-10 stsp goto done;
2324 6bad629b 2019-02-04 stsp if (argc == 0) {
2325 927df6b7 2019-02-10 stsp path = strdup("");
2326 927df6b7 2019-02-10 stsp if (path == NULL) {
2327 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2328 6bad629b 2019-02-04 stsp goto done;
2330 6bad629b 2019-02-04 stsp } else if (argc == 1) {
2331 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
2332 927df6b7 2019-02-10 stsp if (error)
2333 6bad629b 2019-02-04 stsp goto done;
2335 6bad629b 2019-02-04 stsp usage_status();
2337 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2338 6bad629b 2019-02-04 stsp if (error != NULL)
2339 6bad629b 2019-02-04 stsp goto done;
2341 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2342 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2343 6bad629b 2019-02-04 stsp if (error)
2344 6bad629b 2019-02-04 stsp goto done;
2346 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
2347 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2349 927df6b7 2019-02-10 stsp free(cwd);
2350 927df6b7 2019-02-10 stsp free(path);
2351 d0eebce4 2019-03-11 stsp return error;
2354 d0eebce4 2019-03-11 stsp __dead static void
2355 d0eebce4 2019-03-11 stsp usage_ref(void)
2357 d0eebce4 2019-03-11 stsp fprintf(stderr,
2358 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2359 d0eebce4 2019-03-11 stsp getprogname());
2363 d0eebce4 2019-03-11 stsp static const struct got_error *
2364 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2366 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2367 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2368 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2370 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2371 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2373 d0eebce4 2019-03-11 stsp return err;
2375 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2376 d0eebce4 2019-03-11 stsp char *refstr;
2377 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2378 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2379 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2380 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2381 d0eebce4 2019-03-11 stsp free(refstr);
2384 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2385 d0eebce4 2019-03-11 stsp return NULL;
2388 d0eebce4 2019-03-11 stsp static const struct got_error *
2389 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2391 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2392 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2394 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2396 d0eebce4 2019-03-11 stsp return err;
2398 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2399 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2400 d0eebce4 2019-03-11 stsp return err;
2403 d0eebce4 2019-03-11 stsp static const struct got_error *
2404 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2406 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2407 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2408 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2411 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2412 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2413 d1644381 2019-07-14 stsp * an unintended typo.
2415 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2416 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2418 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2420 d83d9d5c 2019-05-13 stsp if (err) {
2421 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2423 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2424 d83d9d5c 2019-05-13 stsp return err;
2425 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2427 d83d9d5c 2019-05-13 stsp return err;
2428 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2429 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2431 d83d9d5c 2019-05-13 stsp return err;
2434 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2436 d0eebce4 2019-03-11 stsp goto done;
2438 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2441 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2443 d0eebce4 2019-03-11 stsp return err;
2446 d0eebce4 2019-03-11 stsp static const struct got_error *
2447 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2449 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2450 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2451 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2452 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2453 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2454 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2456 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2457 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2458 d0eebce4 2019-03-11 stsp switch (ch) {
2460 d0eebce4 2019-03-11 stsp delref = optarg;
2463 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2464 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2465 d0eebce4 2019-03-11 stsp err(1, "-r option");
2466 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2469 d0eebce4 2019-03-11 stsp do_list = 1;
2472 d0eebce4 2019-03-11 stsp usage_ref();
2473 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2477 d0eebce4 2019-03-11 stsp if (do_list && delref)
2478 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2480 d0eebce4 2019-03-11 stsp argc -= optind;
2481 d0eebce4 2019-03-11 stsp argv += optind;
2483 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2484 d0eebce4 2019-03-11 stsp if (argc > 0)
2485 d0eebce4 2019-03-11 stsp usage_ref();
2486 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2487 d0eebce4 2019-03-11 stsp usage_ref();
2489 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2490 e0b57350 2019-03-12 stsp if (do_list) {
2491 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2492 e0b57350 2019-03-12 stsp NULL) == -1)
2493 e0b57350 2019-03-12 stsp err(1, "pledge");
2495 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2496 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2497 e0b57350 2019-03-12 stsp err(1, "pledge");
2500 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2501 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2502 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2503 d0eebce4 2019-03-11 stsp goto done;
2506 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2507 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2508 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2509 d0eebce4 2019-03-11 stsp goto done;
2511 d0eebce4 2019-03-11 stsp error = NULL;
2512 d0eebce4 2019-03-11 stsp if (worktree) {
2513 d0eebce4 2019-03-11 stsp repo_path =
2514 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2515 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2516 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2517 d0eebce4 2019-03-11 stsp if (error)
2518 d0eebce4 2019-03-11 stsp goto done;
2520 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2521 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2522 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2523 d0eebce4 2019-03-11 stsp goto done;
2528 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2529 d0eebce4 2019-03-11 stsp if (error != NULL)
2530 d0eebce4 2019-03-11 stsp goto done;
2532 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2533 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2534 c02c541e 2019-03-29 stsp if (error)
2535 c02c541e 2019-03-29 stsp goto done;
2537 d0eebce4 2019-03-11 stsp if (do_list)
2538 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2539 d0eebce4 2019-03-11 stsp else if (delref)
2540 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2542 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2545 4e759de4 2019-06-26 stsp got_repo_close(repo);
2546 4e759de4 2019-06-26 stsp if (worktree)
2547 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2548 4e759de4 2019-06-26 stsp free(cwd);
2549 4e759de4 2019-06-26 stsp free(repo_path);
2550 4e759de4 2019-06-26 stsp return error;
2553 4e759de4 2019-06-26 stsp __dead static void
2554 4e759de4 2019-06-26 stsp usage_branch(void)
2556 4e759de4 2019-06-26 stsp fprintf(stderr,
2557 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2558 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2562 4e759de4 2019-06-26 stsp static const struct got_error *
2563 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2565 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2566 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2567 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2569 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2571 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2573 4e759de4 2019-06-26 stsp return err;
2575 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2576 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2577 4e759de4 2019-06-26 stsp char *refstr;
2578 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2579 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2581 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2582 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2583 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2584 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2586 ba882ee3 2019-07-11 stsp return err;
2587 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2588 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2589 ba882ee3 2019-07-11 stsp marker = "* ";
2591 ba882ee3 2019-07-11 stsp marker = "~ ";
2594 4e759de4 2019-06-26 stsp refname += 11;
2595 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2596 4e759de4 2019-06-26 stsp if (refstr == NULL)
2597 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2598 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2599 4e759de4 2019-06-26 stsp free(refstr);
2602 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2603 4e759de4 2019-06-26 stsp return NULL;
2606 4e759de4 2019-06-26 stsp static const struct got_error *
2607 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2609 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2610 4e759de4 2019-06-26 stsp struct got_reference *ref;
2611 4e759de4 2019-06-26 stsp char *refname;
2613 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2614 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2616 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2618 4e759de4 2019-06-26 stsp goto done;
2620 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2621 4e759de4 2019-06-26 stsp got_ref_close(ref);
2623 4e759de4 2019-06-26 stsp free(refname);
2624 4e759de4 2019-06-26 stsp return err;
2627 4e759de4 2019-06-26 stsp static const struct got_error *
2628 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2629 4e759de4 2019-06-26 stsp const char *base_branch)
2631 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2632 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2633 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2634 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2635 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2638 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2639 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2640 d3f84d51 2019-07-11 stsp * an unintended typo.
2642 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2643 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2645 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2646 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2647 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2648 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2649 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2650 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2652 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2654 4e759de4 2019-06-26 stsp goto done;
2655 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2656 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2658 4e759de4 2019-06-26 stsp goto done;
2660 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2661 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2662 4e759de4 2019-06-26 stsp goto done;
2665 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2666 4e759de4 2019-06-26 stsp if (err == NULL) {
2667 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2668 4e759de4 2019-06-26 stsp goto done;
2669 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2670 4e759de4 2019-06-26 stsp goto done;
2672 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2674 4e759de4 2019-06-26 stsp goto done;
2676 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2679 4e759de4 2019-06-26 stsp got_ref_close(ref);
2681 4e759de4 2019-06-26 stsp free(base_refname);
2682 4e759de4 2019-06-26 stsp free(refname);
2683 4e759de4 2019-06-26 stsp return err;
2686 4e759de4 2019-06-26 stsp static const struct got_error *
2687 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2689 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2690 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2691 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2692 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2693 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2694 4e759de4 2019-06-26 stsp const char *delref = NULL;
2696 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2697 4e759de4 2019-06-26 stsp switch (ch) {
2699 4e759de4 2019-06-26 stsp delref = optarg;
2702 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2703 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2704 4e759de4 2019-06-26 stsp err(1, "-r option");
2705 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2708 4e759de4 2019-06-26 stsp do_list = 1;
2711 4e759de4 2019-06-26 stsp usage_branch();
2712 4e759de4 2019-06-26 stsp /* NOTREACHED */
2716 4e759de4 2019-06-26 stsp if (do_list && delref)
2717 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2719 4e759de4 2019-06-26 stsp argc -= optind;
2720 4e759de4 2019-06-26 stsp argv += optind;
2722 4e759de4 2019-06-26 stsp if (do_list || delref) {
2723 4e759de4 2019-06-26 stsp if (argc > 0)
2724 4e759de4 2019-06-26 stsp usage_branch();
2725 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2726 4e759de4 2019-06-26 stsp usage_branch();
2728 4e759de4 2019-06-26 stsp #ifndef PROFILE
2729 4e759de4 2019-06-26 stsp if (do_list) {
2730 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2731 4e759de4 2019-06-26 stsp NULL) == -1)
2732 4e759de4 2019-06-26 stsp err(1, "pledge");
2734 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2735 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2736 4e759de4 2019-06-26 stsp err(1, "pledge");
2739 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2740 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2741 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2742 4e759de4 2019-06-26 stsp goto done;
2745 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2746 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2747 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2748 4e759de4 2019-06-26 stsp goto done;
2750 4e759de4 2019-06-26 stsp error = NULL;
2751 4e759de4 2019-06-26 stsp if (worktree) {
2752 4e759de4 2019-06-26 stsp repo_path =
2753 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2754 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2755 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2756 4e759de4 2019-06-26 stsp if (error)
2757 4e759de4 2019-06-26 stsp goto done;
2759 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2760 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2761 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2762 4e759de4 2019-06-26 stsp goto done;
2767 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2768 4e759de4 2019-06-26 stsp if (error != NULL)
2769 4e759de4 2019-06-26 stsp goto done;
2771 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2772 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2773 4e759de4 2019-06-26 stsp if (error)
2774 4e759de4 2019-06-26 stsp goto done;
2776 4e759de4 2019-06-26 stsp if (do_list)
2777 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2778 4e759de4 2019-06-26 stsp else if (delref)
2779 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2781 4e759de4 2019-06-26 stsp const char *base_branch;
2782 4e759de4 2019-06-26 stsp if (argc == 1) {
2783 4e759de4 2019-06-26 stsp base_branch = worktree ?
2784 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2785 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2787 4e759de4 2019-06-26 stsp base_branch = argv[1];
2788 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2792 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2793 d0eebce4 2019-03-11 stsp if (worktree)
2794 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2795 d0eebce4 2019-03-11 stsp free(cwd);
2796 d0eebce4 2019-03-11 stsp free(repo_path);
2797 d00136be 2019-03-26 stsp return error;
2800 d00136be 2019-03-26 stsp __dead static void
2801 d00136be 2019-03-26 stsp usage_add(void)
2803 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2807 d00136be 2019-03-26 stsp static const struct got_error *
2808 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2810 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2811 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2812 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2813 1dd54920 2019-05-11 stsp char *cwd = NULL;
2814 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2815 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2818 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2820 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2821 d00136be 2019-03-26 stsp switch (ch) {
2823 d00136be 2019-03-26 stsp usage_add();
2824 d00136be 2019-03-26 stsp /* NOTREACHED */
2828 d00136be 2019-03-26 stsp argc -= optind;
2829 d00136be 2019-03-26 stsp argv += optind;
2831 43012d58 2019-07-14 stsp #ifndef PROFILE
2832 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2833 43012d58 2019-07-14 stsp NULL) == -1)
2834 43012d58 2019-07-14 stsp err(1, "pledge");
2836 723c305c 2019-05-11 jcs if (argc < 1)
2837 d00136be 2019-03-26 stsp usage_add();
2839 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2840 d00136be 2019-03-26 stsp if (cwd == NULL) {
2841 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2842 d00136be 2019-03-26 stsp goto done;
2845 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2846 d00136be 2019-03-26 stsp if (error)
2847 d00136be 2019-03-26 stsp goto done;
2849 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2850 031a5338 2019-03-26 stsp if (error != NULL)
2851 031a5338 2019-03-26 stsp goto done;
2853 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2854 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2855 d00136be 2019-03-26 stsp if (error)
2856 d00136be 2019-03-26 stsp goto done;
2858 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2859 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2860 723c305c 2019-05-11 jcs if (path == NULL) {
2861 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2865 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2866 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2867 1dd54920 2019-05-11 stsp if (error) {
2868 1dd54920 2019-05-11 stsp free(path);
2872 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2873 1dd54920 2019-05-11 stsp NULL, repo);
2876 031a5338 2019-03-26 stsp got_repo_close(repo);
2877 d00136be 2019-03-26 stsp if (worktree)
2878 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2879 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2880 1dd54920 2019-05-11 stsp free((char *)pe->path);
2881 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2882 2ec1f75b 2019-03-26 stsp free(cwd);
2883 2ec1f75b 2019-03-26 stsp return error;
2886 2ec1f75b 2019-03-26 stsp __dead static void
2887 648e4ef7 2019-07-09 stsp usage_remove(void)
2889 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2893 2ec1f75b 2019-03-26 stsp static const struct got_error *
2894 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2896 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2897 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2898 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2899 17ed4618 2019-06-02 stsp char *cwd = NULL;
2900 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2901 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2902 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2904 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2906 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2907 2ec1f75b 2019-03-26 stsp switch (ch) {
2909 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2912 2ec1f75b 2019-03-26 stsp usage_add();
2913 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2917 2ec1f75b 2019-03-26 stsp argc -= optind;
2918 2ec1f75b 2019-03-26 stsp argv += optind;
2920 43012d58 2019-07-14 stsp #ifndef PROFILE
2921 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2922 43012d58 2019-07-14 stsp NULL) == -1)
2923 43012d58 2019-07-14 stsp err(1, "pledge");
2925 17ed4618 2019-06-02 stsp if (argc < 1)
2926 648e4ef7 2019-07-09 stsp usage_remove();
2928 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2929 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2930 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2931 2ec1f75b 2019-03-26 stsp goto done;
2933 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2934 2ec1f75b 2019-03-26 stsp if (error)
2935 2ec1f75b 2019-03-26 stsp goto done;
2937 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2939 2ec1f75b 2019-03-26 stsp goto done;
2941 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2942 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2943 2ec1f75b 2019-03-26 stsp if (error)
2944 2ec1f75b 2019-03-26 stsp goto done;
2946 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2947 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2948 17ed4618 2019-06-02 stsp if (path == NULL) {
2949 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2950 17ed4618 2019-06-02 stsp goto done;
2953 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2954 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2955 17ed4618 2019-06-02 stsp if (error) {
2956 17ed4618 2019-06-02 stsp free(path);
2957 17ed4618 2019-06-02 stsp goto done;
2960 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2961 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2962 a129376b 2019-03-28 stsp if (error)
2963 a129376b 2019-03-28 stsp goto done;
2966 a129376b 2019-03-28 stsp got_repo_close(repo);
2967 a129376b 2019-03-28 stsp if (worktree)
2968 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2969 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2970 17ed4618 2019-06-02 stsp free((char *)pe->path);
2971 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2972 a129376b 2019-03-28 stsp free(cwd);
2973 a129376b 2019-03-28 stsp return error;
2976 a129376b 2019-03-28 stsp __dead static void
2977 a129376b 2019-03-28 stsp usage_revert(void)
2979 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2983 1ee397ad 2019-07-12 stsp static const struct got_error *
2984 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2986 a129376b 2019-03-28 stsp while (path[0] == '/')
2988 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2989 1ee397ad 2019-07-12 stsp return NULL;
2992 a129376b 2019-03-28 stsp static const struct got_error *
2993 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2995 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2996 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2997 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2998 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2999 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3000 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3001 e20a8b6f 2019-06-04 stsp int ch, i;
3003 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3005 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3006 a129376b 2019-03-28 stsp switch (ch) {
3008 a129376b 2019-03-28 stsp usage_revert();
3009 a129376b 2019-03-28 stsp /* NOTREACHED */
3013 a129376b 2019-03-28 stsp argc -= optind;
3014 a129376b 2019-03-28 stsp argv += optind;
3016 43012d58 2019-07-14 stsp #ifndef PROFILE
3017 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3018 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3019 43012d58 2019-07-14 stsp err(1, "pledge");
3021 e20a8b6f 2019-06-04 stsp if (argc < 1)
3022 a129376b 2019-03-28 stsp usage_revert();
3024 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3025 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3026 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3027 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3028 e20a8b6f 2019-06-04 stsp goto done;
3031 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3032 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3033 e20a8b6f 2019-06-04 stsp if (error) {
3034 e20a8b6f 2019-06-04 stsp free(path);
3035 e20a8b6f 2019-06-04 stsp goto done;
3039 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3040 a129376b 2019-03-28 stsp if (cwd == NULL) {
3041 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3042 a129376b 2019-03-28 stsp goto done;
3044 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3045 2ec1f75b 2019-03-26 stsp if (error)
3046 2ec1f75b 2019-03-26 stsp goto done;
3048 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3049 a129376b 2019-03-28 stsp if (error != NULL)
3050 a129376b 2019-03-28 stsp goto done;
3052 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3053 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3054 a129376b 2019-03-28 stsp if (error)
3055 a129376b 2019-03-28 stsp goto done;
3057 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3058 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3059 a129376b 2019-03-28 stsp if (error)
3060 a129376b 2019-03-28 stsp goto done;
3063 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3064 2ec1f75b 2019-03-26 stsp if (worktree)
3065 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3066 2ec1f75b 2019-03-26 stsp free(path);
3067 d00136be 2019-03-26 stsp free(cwd);
3068 6bad629b 2019-02-04 stsp return error;
3071 c4296144 2019-05-09 stsp __dead static void
3072 c4296144 2019-05-09 stsp usage_commit(void)
3074 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3078 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3079 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3080 e2ba3d07 2019-05-13 stsp const char *editor;
3081 e0870e44 2019-05-13 stsp const char *worktree_path;
3082 76d98825 2019-06-03 stsp const char *branch_name;
3083 314a6357 2019-05-13 stsp const char *repo_path;
3084 e0870e44 2019-05-13 stsp char *logmsg_path;
3088 33ad4cbe 2019-05-12 jcs static const struct got_error *
3089 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3092 76d98825 2019-06-03 stsp char *initial_content = NULL;
3093 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3094 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3095 e0870e44 2019-05-13 stsp char *template = NULL;
3096 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3098 33ad4cbe 2019-05-12 jcs size_t len;
3100 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3101 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3102 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3103 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3104 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3105 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3106 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3107 33ad4cbe 2019-05-12 jcs return NULL;
3110 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3111 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3113 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3114 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3115 76d98825 2019-06-03 stsp a->branch_name) == -1)
3116 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3118 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3120 e0870e44 2019-05-13 stsp goto done;
3122 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3124 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3125 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3126 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3127 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3128 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3132 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3134 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3135 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3136 76d98825 2019-06-03 stsp free(initial_content);
3137 e0870e44 2019-05-13 stsp free(template);
3139 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3140 3ce1b845 2019-07-15 stsp if (err == NULL) {
3141 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3142 3ce1b845 2019-07-15 stsp if (err) {
3143 3ce1b845 2019-07-15 stsp free(*logmsg);
3144 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3147 33ad4cbe 2019-05-12 jcs return err;
3150 c4296144 2019-05-09 stsp static const struct got_error *
3151 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3153 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3154 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3155 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3156 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
3157 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3158 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3159 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3160 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3161 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3164 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3165 c4296144 2019-05-09 stsp switch (ch) {
3167 c4296144 2019-05-09 stsp logmsg = optarg;
3170 c4296144 2019-05-09 stsp usage_commit();
3171 c4296144 2019-05-09 stsp /* NOTREACHED */
3175 c4296144 2019-05-09 stsp argc -= optind;
3176 c4296144 2019-05-09 stsp argv += optind;
3178 43012d58 2019-07-14 stsp #ifndef PROFILE
3179 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3180 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3181 43012d58 2019-07-14 stsp err(1, "pledge");
3183 c4296144 2019-05-09 stsp if (argc == 1) {
3184 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
3185 c4296144 2019-05-09 stsp if (path == NULL) {
3186 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
3187 c4296144 2019-05-09 stsp goto done;
3189 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
3190 c4296144 2019-05-09 stsp } else if (argc != 0)
3191 c4296144 2019-05-09 stsp usage_commit();
3193 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3194 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3195 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3196 35bd8fed 2019-05-09 stsp goto done;
3199 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3200 c4296144 2019-05-09 stsp if (cwd == NULL) {
3201 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3202 c4296144 2019-05-09 stsp goto done;
3204 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3205 7d5807f4 2019-07-11 stsp if (error)
3206 7d5807f4 2019-07-11 stsp goto done;
3208 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
3209 c4296144 2019-05-09 stsp if (error)
3210 c4296144 2019-05-09 stsp goto done;
3212 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3213 c4296144 2019-05-09 stsp if (error != NULL)
3214 c4296144 2019-05-09 stsp goto done;
3217 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3218 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3220 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3221 314a6357 2019-05-13 stsp error = get_editor(&editor);
3223 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3224 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3225 c4296144 2019-05-09 stsp if (error)
3226 c4296144 2019-05-09 stsp goto done;
3228 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3229 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3230 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3231 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3232 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3233 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
3234 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3235 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
3236 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3237 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3238 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3239 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3240 e0870e44 2019-05-13 stsp if (error) {
3241 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3242 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3243 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3244 c4296144 2019-05-09 stsp goto done;
3247 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3248 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3250 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3251 c4296144 2019-05-09 stsp if (error)
3252 c4296144 2019-05-09 stsp goto done;
3253 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3256 c4296144 2019-05-09 stsp got_repo_close(repo);
3257 c4296144 2019-05-09 stsp if (worktree)
3258 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3259 c4296144 2019-05-09 stsp free(path);
3260 c4296144 2019-05-09 stsp free(cwd);
3261 c4296144 2019-05-09 stsp free(id_str);
3262 e2ba3d07 2019-05-13 stsp free(editor);
3263 234035bc 2019-06-01 stsp return error;
3266 234035bc 2019-06-01 stsp __dead static void
3267 234035bc 2019-06-01 stsp usage_cherrypick(void)
3269 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3273 234035bc 2019-06-01 stsp static const struct got_error *
3274 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3276 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3277 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3278 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3279 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3280 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3281 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3282 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3283 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3284 234035bc 2019-06-01 stsp int ch, did_something = 0;
3286 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3287 234035bc 2019-06-01 stsp switch (ch) {
3289 234035bc 2019-06-01 stsp usage_cherrypick();
3290 234035bc 2019-06-01 stsp /* NOTREACHED */
3294 234035bc 2019-06-01 stsp argc -= optind;
3295 234035bc 2019-06-01 stsp argv += optind;
3297 43012d58 2019-07-14 stsp #ifndef PROFILE
3298 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3299 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3300 43012d58 2019-07-14 stsp err(1, "pledge");
3302 234035bc 2019-06-01 stsp if (argc != 1)
3303 234035bc 2019-06-01 stsp usage_cherrypick();
3305 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3306 234035bc 2019-06-01 stsp if (cwd == NULL) {
3307 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3308 234035bc 2019-06-01 stsp goto done;
3310 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3311 234035bc 2019-06-01 stsp if (error)
3312 234035bc 2019-06-01 stsp goto done;
3314 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3315 234035bc 2019-06-01 stsp if (error != NULL)
3316 234035bc 2019-06-01 stsp goto done;
3318 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3319 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3320 234035bc 2019-06-01 stsp if (error)
3321 234035bc 2019-06-01 stsp goto done;
3323 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3324 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3325 234035bc 2019-06-01 stsp if (error != NULL) {
3326 234035bc 2019-06-01 stsp struct got_reference *ref;
3327 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3328 234035bc 2019-06-01 stsp goto done;
3329 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3330 234035bc 2019-06-01 stsp if (error != NULL)
3331 234035bc 2019-06-01 stsp goto done;
3332 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3333 234035bc 2019-06-01 stsp got_ref_close(ref);
3334 234035bc 2019-06-01 stsp if (error != NULL)
3335 234035bc 2019-06-01 stsp goto done;
3337 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3338 234035bc 2019-06-01 stsp if (error)
3339 234035bc 2019-06-01 stsp goto done;
3341 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3342 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3343 234035bc 2019-06-01 stsp if (error != NULL)
3344 234035bc 2019-06-01 stsp goto done;
3346 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3347 234035bc 2019-06-01 stsp if (error) {
3348 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3349 234035bc 2019-06-01 stsp goto done;
3350 234035bc 2019-06-01 stsp error = NULL;
3352 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3353 234035bc 2019-06-01 stsp goto done;
3356 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3357 234035bc 2019-06-01 stsp if (error)
3358 234035bc 2019-06-01 stsp goto done;
3359 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3360 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3361 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3363 234035bc 2019-06-01 stsp if (error != NULL)
3364 234035bc 2019-06-01 stsp goto done;
3366 234035bc 2019-06-01 stsp if (did_something)
3367 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3369 234035bc 2019-06-01 stsp if (commit)
3370 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3371 234035bc 2019-06-01 stsp free(commit_id_str);
3372 234035bc 2019-06-01 stsp if (head_ref)
3373 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3374 234035bc 2019-06-01 stsp if (worktree)
3375 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3377 234035bc 2019-06-01 stsp got_repo_close(repo);
3378 c4296144 2019-05-09 stsp return error;
3381 5ef14e63 2019-06-02 stsp __dead static void
3382 5ef14e63 2019-06-02 stsp usage_backout(void)
3384 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3388 5ef14e63 2019-06-02 stsp static const struct got_error *
3389 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3391 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3392 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3393 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3394 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3395 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3396 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3397 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3398 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3399 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3401 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3402 5ef14e63 2019-06-02 stsp switch (ch) {
3404 5ef14e63 2019-06-02 stsp usage_backout();
3405 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3409 5ef14e63 2019-06-02 stsp argc -= optind;
3410 5ef14e63 2019-06-02 stsp argv += optind;
3412 43012d58 2019-07-14 stsp #ifndef PROFILE
3413 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3414 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3415 43012d58 2019-07-14 stsp err(1, "pledge");
3417 5ef14e63 2019-06-02 stsp if (argc != 1)
3418 5ef14e63 2019-06-02 stsp usage_backout();
3420 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3421 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3422 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3423 5ef14e63 2019-06-02 stsp goto done;
3425 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3426 5ef14e63 2019-06-02 stsp if (error)
3427 5ef14e63 2019-06-02 stsp goto done;
3429 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3430 5ef14e63 2019-06-02 stsp if (error != NULL)
3431 5ef14e63 2019-06-02 stsp goto done;
3433 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3434 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3435 5ef14e63 2019-06-02 stsp if (error)
3436 5ef14e63 2019-06-02 stsp goto done;
3438 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3439 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3440 5ef14e63 2019-06-02 stsp if (error != NULL) {
3441 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3442 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3443 5ef14e63 2019-06-02 stsp goto done;
3444 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3445 5ef14e63 2019-06-02 stsp if (error != NULL)
3446 5ef14e63 2019-06-02 stsp goto done;
3447 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3448 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3449 5ef14e63 2019-06-02 stsp if (error != NULL)
3450 5ef14e63 2019-06-02 stsp goto done;
3452 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3453 5ef14e63 2019-06-02 stsp if (error)
3454 5ef14e63 2019-06-02 stsp goto done;
3456 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3457 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3458 5ef14e63 2019-06-02 stsp if (error != NULL)
3459 5ef14e63 2019-06-02 stsp goto done;
3461 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3462 5ef14e63 2019-06-02 stsp if (error)
3463 5ef14e63 2019-06-02 stsp goto done;
3465 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3466 5ef14e63 2019-06-02 stsp if (error)
3467 5ef14e63 2019-06-02 stsp goto done;
3468 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3469 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3470 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3471 5ef14e63 2019-06-02 stsp goto done;
3474 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3475 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3476 5ef14e63 2019-06-02 stsp if (error != NULL)
3477 5ef14e63 2019-06-02 stsp goto done;
3479 5ef14e63 2019-06-02 stsp if (did_something)
3480 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3482 5ef14e63 2019-06-02 stsp if (commit)
3483 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3484 5ef14e63 2019-06-02 stsp free(commit_id_str);
3485 5ef14e63 2019-06-02 stsp if (head_ref)
3486 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3487 818c7501 2019-07-11 stsp if (worktree)
3488 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3490 818c7501 2019-07-11 stsp got_repo_close(repo);
3491 818c7501 2019-07-11 stsp return error;
3494 818c7501 2019-07-11 stsp __dead static void
3495 818c7501 2019-07-11 stsp usage_rebase(void)
3497 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3498 af54c8f8 2019-07-11 stsp getprogname());
3503 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3506 0ebf8283 2019-07-24 stsp size_t len;
3508 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3509 0ebf8283 2019-07-24 stsp if (len > limit)
3510 0ebf8283 2019-07-24 stsp len = limit;
3511 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3512 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3514 0ebf8283 2019-07-24 stsp *nl = '\0';
3517 0ebf8283 2019-07-24 stsp static const struct got_error *
3518 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3520 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3522 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3524 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3525 0ebf8283 2019-07-24 stsp logmsg0++;
3527 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3528 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3529 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3531 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3532 0ebf8283 2019-07-24 stsp return NULL;
3535 818c7501 2019-07-11 stsp static const struct got_error *
3536 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3537 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3539 818c7501 2019-07-11 stsp const struct got_error *err;
3540 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3542 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3544 818c7501 2019-07-11 stsp goto done;
3546 ff0d2220 2019-07-11 stsp if (new_id) {
3547 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3549 ff0d2220 2019-07-11 stsp goto done;
3552 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3553 ff0d2220 2019-07-11 stsp if (new_id_str)
3554 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3556 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3558 0ebf8283 2019-07-24 stsp goto done;
3560 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3561 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3563 818c7501 2019-07-11 stsp free(old_id_str);
3564 818c7501 2019-07-11 stsp free(new_id_str);
3565 818c7501 2019-07-11 stsp return err;
3568 1ee397ad 2019-07-12 stsp static const struct got_error *
3569 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3571 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3573 818c7501 2019-07-11 stsp while (path[0] == '/')
3575 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3577 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3578 1ee397ad 2019-07-12 stsp return NULL;
3579 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3580 818c7501 2019-07-11 stsp *rebase_status = status;
3581 1ee397ad 2019-07-12 stsp return NULL;
3584 818c7501 2019-07-11 stsp static const struct got_error *
3585 818c7501 2019-07-11 stsp rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3586 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3587 818c7501 2019-07-11 stsp struct got_repository *repo)
3589 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3590 818c7501 2019-07-11 stsp return got_worktree_rebase_complete(worktree,
3591 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3594 818c7501 2019-07-11 stsp static const struct got_error *
3595 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3596 01757395 2019-07-12 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
3597 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
3599 ff0d2220 2019-07-11 stsp const struct got_error *error;
3600 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3601 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3603 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3604 ff0d2220 2019-07-11 stsp if (error)
3605 ff0d2220 2019-07-11 stsp return error;
3607 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3608 01757395 2019-07-12 stsp worktree, tmp_branch, commit, commit_id, repo);
3609 ff0d2220 2019-07-11 stsp if (error) {
3610 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3611 ff0d2220 2019-07-11 stsp goto done;
3612 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3614 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3615 ff0d2220 2019-07-11 stsp free(new_commit_id);
3618 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3619 ff0d2220 2019-07-11 stsp return error;
3622 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3623 64c6d990 2019-07-11 stsp const char *path_prefix;
3624 64c6d990 2019-07-11 stsp size_t len;
3627 64c6d990 2019-07-11 stsp static const struct got_error *
3628 64c6d990 2019-07-11 stsp check_path_prefix(void *arg, struct got_blob_object *blob1,
3629 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3630 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3631 64c6d990 2019-07-11 stsp struct got_repository *repo)
3633 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3635 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3636 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3637 64c6d990 2019-07-11 stsp return got_error(GOT_ERR_REBASE_PATH);
3639 64c6d990 2019-07-11 stsp return NULL;
3642 64c6d990 2019-07-11 stsp static const struct got_error *
3643 64c6d990 2019-07-11 stsp rebase_check_path_prefix(struct got_object_id *parent_id,
3644 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3645 64c6d990 2019-07-11 stsp struct got_repository *repo)
3647 64c6d990 2019-07-11 stsp const struct got_error *err;
3648 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3649 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3650 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3652 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3653 64c6d990 2019-07-11 stsp return NULL;
3655 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3657 64c6d990 2019-07-11 stsp goto done;
3659 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3661 64c6d990 2019-07-11 stsp goto done;
3663 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3664 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3666 64c6d990 2019-07-11 stsp goto done;
3668 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3669 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3671 64c6d990 2019-07-11 stsp goto done;
3673 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3674 64c6d990 2019-07-11 stsp cpp_arg.len = strlen(path_prefix);
3675 64c6d990 2019-07-11 stsp err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3676 64c6d990 2019-07-11 stsp &cpp_arg);
3678 64c6d990 2019-07-11 stsp if (tree1)
3679 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3680 64c6d990 2019-07-11 stsp if (tree2)
3681 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3682 64c6d990 2019-07-11 stsp if (commit)
3683 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3684 64c6d990 2019-07-11 stsp if (parent_commit)
3685 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3686 64c6d990 2019-07-11 stsp return err;
3689 ff0d2220 2019-07-11 stsp static const struct got_error *
3690 af61c510 2019-07-19 stsp collect_commits_to_rebase(struct got_object_id_queue *commits,
3691 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3692 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3693 af61c510 2019-07-19 stsp const char *path_prefix, struct got_repository *repo)
3695 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3696 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3697 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3698 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3699 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3701 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3703 af61c510 2019-07-19 stsp return err;
3705 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3707 af61c510 2019-07-19 stsp goto done;
3708 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3709 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3710 af61c510 2019-07-19 stsp if (err) {
3711 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3712 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3713 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3714 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3715 af61c510 2019-07-19 stsp "been reached?!?");
3716 af61c510 2019-07-19 stsp goto done;
3717 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3718 af61c510 2019-07-19 stsp goto done;
3719 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3721 af61c510 2019-07-19 stsp goto done;
3723 af61c510 2019-07-19 stsp err = rebase_check_path_prefix(parent_id, commit_id,
3724 af61c510 2019-07-19 stsp path_prefix, repo);
3726 af61c510 2019-07-19 stsp goto done;
3728 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3730 af61c510 2019-07-19 stsp goto done;
3731 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3732 af61c510 2019-07-19 stsp commit_id = parent_id;
3736 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3737 af61c510 2019-07-19 stsp return err;
3740 af61c510 2019-07-19 stsp static const struct got_error *
3741 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3743 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3744 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3745 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3746 818c7501 2019-07-11 stsp char *cwd = NULL;
3747 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3748 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3749 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3750 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3751 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3752 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3753 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3754 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3755 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3756 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3757 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3758 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3760 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3761 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3763 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3764 818c7501 2019-07-11 stsp switch (ch) {
3766 818c7501 2019-07-11 stsp abort_rebase = 1;
3769 818c7501 2019-07-11 stsp continue_rebase = 1;
3772 818c7501 2019-07-11 stsp usage_rebase();
3773 818c7501 2019-07-11 stsp /* NOTREACHED */
3777 818c7501 2019-07-11 stsp argc -= optind;
3778 818c7501 2019-07-11 stsp argv += optind;
3780 43012d58 2019-07-14 stsp #ifndef PROFILE
3781 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3782 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3783 43012d58 2019-07-14 stsp err(1, "pledge");
3785 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3786 818c7501 2019-07-11 stsp usage_rebase();
3787 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3788 818c7501 2019-07-11 stsp if (argc != 0)
3789 818c7501 2019-07-11 stsp usage_rebase();
3790 818c7501 2019-07-11 stsp } else if (argc != 1)
3791 818c7501 2019-07-11 stsp usage_rebase();
3793 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3794 818c7501 2019-07-11 stsp if (cwd == NULL) {
3795 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3796 818c7501 2019-07-11 stsp goto done;
3798 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3799 818c7501 2019-07-11 stsp if (error)
3800 818c7501 2019-07-11 stsp goto done;
3802 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3803 818c7501 2019-07-11 stsp if (error != NULL)
3804 818c7501 2019-07-11 stsp goto done;
3806 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3807 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3808 818c7501 2019-07-11 stsp if (error)
3809 818c7501 2019-07-11 stsp goto done;
3811 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3812 818c7501 2019-07-11 stsp if (error)
3813 818c7501 2019-07-11 stsp goto done;
3815 f6794adc 2019-07-23 stsp if (abort_rebase) {
3816 818c7501 2019-07-11 stsp int did_something;
3817 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3818 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3819 f6794adc 2019-07-23 stsp goto done;
3821 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3822 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3823 818c7501 2019-07-11 stsp if (error)
3824 818c7501 2019-07-11 stsp goto done;
3825 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3826 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3827 818c7501 2019-07-11 stsp error = got_worktree_rebase_abort(worktree, repo,
3828 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3829 818c7501 2019-07-11 stsp if (error)
3830 818c7501 2019-07-11 stsp goto done;
3831 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3832 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3835 818c7501 2019-07-11 stsp if (continue_rebase) {
3836 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3837 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3838 f6794adc 2019-07-23 stsp goto done;
3840 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3841 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3842 818c7501 2019-07-11 stsp if (error)
3843 818c7501 2019-07-11 stsp goto done;
3845 01757395 2019-07-12 stsp error = rebase_commit(NULL, worktree, tmp_branch,
3846 01757395 2019-07-12 stsp resume_commit_id, repo);
3847 818c7501 2019-07-11 stsp if (error)
3848 818c7501 2019-07-11 stsp goto done;
3850 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3851 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3852 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3853 818c7501 2019-07-11 stsp goto done;
3856 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3857 818c7501 2019-07-11 stsp if (error != NULL)
3858 818c7501 2019-07-11 stsp goto done;
3860 818c7501 2019-07-11 stsp error = check_same_branch(
3861 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree), branch, repo);
3862 818c7501 2019-07-11 stsp if (error) {
3863 818c7501 2019-07-11 stsp if (error->code != GOT_ERR_ANCESTRY)
3864 818c7501 2019-07-11 stsp goto done;
3865 818c7501 2019-07-11 stsp error = NULL;
3867 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3868 818c7501 2019-07-11 stsp "specified branch resolves to a commit which "
3869 818c7501 2019-07-11 stsp "is already contained in work tree's branch");
3870 818c7501 2019-07-11 stsp goto done;
3874 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3875 ff0d2220 2019-07-11 stsp if (error)
3876 ff0d2220 2019-07-11 stsp goto done;
3878 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3879 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3880 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree),
3881 818c7501 2019-07-11 stsp branch_head_commit_id, repo);
3882 818c7501 2019-07-11 stsp if (error)
3883 818c7501 2019-07-11 stsp goto done;
3884 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3885 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3886 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3887 818c7501 2019-07-11 stsp "with work tree's branch");
3888 818c7501 2019-07-11 stsp goto done;
3891 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3892 818c7501 2019-07-11 stsp &tmp_branch, worktree, branch, repo);
3893 818c7501 2019-07-11 stsp if (error)
3894 818c7501 2019-07-11 stsp goto done;
3897 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3898 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3899 818c7501 2019-07-11 stsp if (error)
3900 818c7501 2019-07-11 stsp goto done;
3902 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3903 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3904 af61c510 2019-07-19 stsp error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3905 af61c510 2019-07-19 stsp yca_id, got_worktree_get_path_prefix(worktree), repo);
3906 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3907 818c7501 2019-07-11 stsp commit = NULL;
3908 818c7501 2019-07-11 stsp if (error)
3909 818c7501 2019-07-11 stsp goto done;
3911 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3912 ff0d2220 2019-07-11 stsp if (continue_rebase)
3913 ff0d2220 2019-07-11 stsp error = rebase_complete(worktree, branch,
3914 ff0d2220 2019-07-11 stsp new_base_branch, tmp_branch, repo);
3916 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3917 818c7501 2019-07-11 stsp goto done;
3920 818c7501 2019-07-11 stsp pid = NULL;
3921 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
3922 818c7501 2019-07-11 stsp commit_id = qid->id;
3923 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
3924 818c7501 2019-07-11 stsp pid = qid;
3926 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
3927 01757395 2019-07-12 stsp worktree, parent_id, commit_id, repo, rebase_progress,
3928 01757395 2019-07-12 stsp &rebase_status, check_cancelled, NULL);
3929 818c7501 2019-07-11 stsp if (error)
3930 818c7501 2019-07-11 stsp goto done;
3932 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3933 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3937 01757395 2019-07-12 stsp error = rebase_commit(&merged_paths, worktree, tmp_branch,
3938 01757395 2019-07-12 stsp commit_id, repo);
3939 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3940 818c7501 2019-07-11 stsp if (error)
3941 818c7501 2019-07-11 stsp goto done;
3944 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3945 818c7501 2019-07-11 stsp error = got_worktree_rebase_postpone(worktree);
3946 818c7501 2019-07-11 stsp if (error)
3947 818c7501 2019-07-11 stsp goto done;
3948 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
3949 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
3951 818c7501 2019-07-11 stsp error = rebase_complete(worktree, branch, new_base_branch,
3952 818c7501 2019-07-11 stsp tmp_branch, repo);
3954 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
3955 818c7501 2019-07-11 stsp free(branch_head_commit_id);
3956 818c7501 2019-07-11 stsp free(resume_commit_id);
3957 818c7501 2019-07-11 stsp free(yca_id);
3958 818c7501 2019-07-11 stsp if (commit)
3959 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3960 818c7501 2019-07-11 stsp if (branch)
3961 818c7501 2019-07-11 stsp got_ref_close(branch);
3962 818c7501 2019-07-11 stsp if (new_base_branch)
3963 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
3964 818c7501 2019-07-11 stsp if (tmp_branch)
3965 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
3966 5ef14e63 2019-06-02 stsp if (worktree)
3967 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3969 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3970 5ef14e63 2019-06-02 stsp return error;
3973 0ebf8283 2019-07-24 stsp __dead static void
3974 0ebf8283 2019-07-24 stsp usage_histedit(void)
3976 0ebf8283 2019-07-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
3977 0ebf8283 2019-07-24 stsp getprogname());
3981 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
3982 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
3983 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
3984 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
3985 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
3987 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
3988 0ebf8283 2019-07-24 stsp unsigned char code;
3989 0ebf8283 2019-07-24 stsp const char *name;
3990 0ebf8283 2019-07-24 stsp const char *desc;
3991 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
3992 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
3993 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
3994 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
3995 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
3996 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
3997 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4000 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4001 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4002 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4003 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4004 0ebf8283 2019-07-24 stsp char *logmsg;
4006 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4008 0ebf8283 2019-07-24 stsp static const struct got_error *
4009 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4010 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4012 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4013 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4014 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4017 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4019 0ebf8283 2019-07-24 stsp goto done;
4021 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4023 0ebf8283 2019-07-24 stsp goto done;
4025 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4027 0ebf8283 2019-07-24 stsp goto done;
4029 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4030 0ebf8283 2019-07-24 stsp if (n < 0)
4031 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4033 0ebf8283 2019-07-24 stsp if (commit)
4034 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4035 0ebf8283 2019-07-24 stsp free(id_str);
4036 0ebf8283 2019-07-24 stsp free(logmsg);
4037 0ebf8283 2019-07-24 stsp return err;
4040 0ebf8283 2019-07-24 stsp static const struct got_error *
4041 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4042 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4044 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4045 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4047 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4048 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4050 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4051 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4057 0ebf8283 2019-07-24 stsp return err;
4060 0ebf8283 2019-07-24 stsp static const struct got_error *
4061 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4063 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4066 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4067 0ebf8283 2019-07-24 stsp if (n < 0)
4068 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4070 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4071 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4072 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4073 0ebf8283 2019-07-24 stsp cmd->desc);
4074 0ebf8283 2019-07-24 stsp if (n < 0) {
4075 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4079 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4080 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4081 0ebf8283 2019-07-24 stsp if (n < 0)
4082 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4083 0ebf8283 2019-07-24 stsp return err;
4086 0ebf8283 2019-07-24 stsp static const struct got_error *
4087 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4089 0ebf8283 2019-07-24 stsp static char msg[42];
4092 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4094 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4095 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4097 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4100 0ebf8283 2019-07-24 stsp static const struct got_error *
4101 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4102 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4104 0ebf8283 2019-07-24 stsp const struct got_error *err;
4105 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4106 0ebf8283 2019-07-24 stsp char *id_str;
4108 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4110 0ebf8283 2019-07-24 stsp return err;
4112 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4114 0ebf8283 2019-07-24 stsp goto done;
4116 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4117 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4118 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4119 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4120 0ebf8283 2019-07-24 stsp goto done;
4123 0ebf8283 2019-07-24 stsp if (folded_commit)
4124 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4125 0ebf8283 2019-07-24 stsp free(id_str);
4126 0ebf8283 2019-07-24 stsp return err;
4129 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4130 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4132 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4134 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4135 0ebf8283 2019-07-24 stsp while (prev && prev->cmd->code == GOT_HISTEDIT_FOLD) {
4136 0ebf8283 2019-07-24 stsp folded = prev;
4137 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4140 0ebf8283 2019-07-24 stsp return folded;
4143 0ebf8283 2019-07-24 stsp static const struct got_error *
4144 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4145 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4147 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4148 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4149 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4150 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4152 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4154 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4156 0ebf8283 2019-07-24 stsp return err;
4158 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4159 0ebf8283 2019-07-24 stsp if (folded) {
4160 0ebf8283 2019-07-24 stsp while (folded != hle) {
4161 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4162 0ebf8283 2019-07-24 stsp logmsg, repo);
4164 0ebf8283 2019-07-24 stsp goto done;
4165 0ebf8283 2019-07-24 stsp free(logmsg);
4166 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4167 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4171 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4173 0ebf8283 2019-07-24 stsp goto done;
4174 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4175 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4176 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4177 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4178 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4179 0ebf8283 2019-07-24 stsp goto done;
4181 0ebf8283 2019-07-24 stsp free(logmsg);
4182 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4184 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4186 0ebf8283 2019-07-24 stsp goto done;
4188 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4190 0ebf8283 2019-07-24 stsp goto done;
4192 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4193 0ebf8283 2019-07-24 stsp close(fd);
4195 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4197 0ebf8283 2019-07-24 stsp goto done;
4199 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4200 0ebf8283 2019-07-24 stsp if (err) {
4201 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4202 0ebf8283 2019-07-24 stsp goto done;
4203 0ebf8283 2019-07-24 stsp err = NULL;
4204 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4205 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4206 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4209 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4210 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4211 0ebf8283 2019-07-24 stsp free(logmsg_path);
4212 0ebf8283 2019-07-24 stsp free(logmsg);
4213 0ebf8283 2019-07-24 stsp free(editor);
4214 0ebf8283 2019-07-24 stsp if (commit)
4215 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4216 0ebf8283 2019-07-24 stsp return err;
4219 0ebf8283 2019-07-24 stsp static const struct got_error *
4220 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4221 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4223 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4224 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4225 0ebf8283 2019-07-24 stsp size_t size;
4226 0ebf8283 2019-07-24 stsp ssize_t len;
4227 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4228 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4229 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4230 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4232 0ebf8283 2019-07-24 stsp for (;;) {
4233 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4234 0ebf8283 2019-07-24 stsp if (len == -1) {
4235 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4236 0ebf8283 2019-07-24 stsp if (feof(f))
4238 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4239 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4244 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4246 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4247 0ebf8283 2019-07-24 stsp free(line);
4248 0ebf8283 2019-07-24 stsp line = NULL;
4251 0ebf8283 2019-07-24 stsp cmd = NULL;
4252 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4253 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4254 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4255 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4256 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4259 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4264 0ebf8283 2019-07-24 stsp if (cmd == NULL) {
4265 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4268 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4270 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4271 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4272 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4275 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4276 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4280 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4281 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4282 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4286 0ebf8283 2019-07-24 stsp free(line);
4287 0ebf8283 2019-07-24 stsp line = NULL;
4291 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4293 0ebf8283 2019-07-24 stsp *end = '\0';
4295 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4296 0ebf8283 2019-07-24 stsp if (err) {
4297 0ebf8283 2019-07-24 stsp /* override error code */
4298 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4302 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4303 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4304 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4307 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4308 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4309 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4310 0ebf8283 2019-07-24 stsp commit_id = NULL;
4311 0ebf8283 2019-07-24 stsp free(line);
4312 0ebf8283 2019-07-24 stsp line = NULL;
4313 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4316 0ebf8283 2019-07-24 stsp free(line);
4317 0ebf8283 2019-07-24 stsp free(commit_id);
4318 0ebf8283 2019-07-24 stsp return err;
4321 0ebf8283 2019-07-24 stsp static const struct got_error *
4322 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4323 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4325 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4326 0ebf8283 2019-07-24 stsp char *editor;
4327 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4329 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4331 0ebf8283 2019-07-24 stsp return err;
4333 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4334 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4335 0ebf8283 2019-07-24 stsp goto done;
4338 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4339 0ebf8283 2019-07-24 stsp if (f == NULL) {
4340 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4341 0ebf8283 2019-07-24 stsp goto done;
4343 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4345 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4346 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4347 0ebf8283 2019-07-24 stsp free(editor);
4348 0ebf8283 2019-07-24 stsp return err;
4351 0ebf8283 2019-07-24 stsp static const struct got_error *
4352 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *, const char *,
4353 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4355 0ebf8283 2019-07-24 stsp static const struct got_error *
4356 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4357 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4359 0ebf8283 2019-07-24 stsp const struct got_error *err;
4360 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4361 0ebf8283 2019-07-24 stsp char *path = NULL;
4363 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4365 0ebf8283 2019-07-24 stsp return err;
4367 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4369 0ebf8283 2019-07-24 stsp goto done;
4371 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4373 0ebf8283 2019-07-24 stsp goto done;
4375 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4376 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4377 0ebf8283 2019-07-24 stsp goto done;
4381 0ebf8283 2019-07-24 stsp err = histedit_run_editor(histedit_cmds, path, repo);
4382 0ebf8283 2019-07-24 stsp if (err) {
4383 0ebf8283 2019-07-24 stsp const char *errmsg = err->msg;
4384 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4385 0ebf8283 2019-07-24 stsp goto done;
4386 0ebf8283 2019-07-24 stsp err = histedit_edit_list_retry(histedit_cmds, errmsg,
4387 0ebf8283 2019-07-24 stsp commits, path, repo);
4390 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4391 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4392 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4393 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4394 0ebf8283 2019-07-24 stsp free(path);
4395 0ebf8283 2019-07-24 stsp return err;
4398 0ebf8283 2019-07-24 stsp static const struct got_error *
4399 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4400 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4402 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4403 0ebf8283 2019-07-24 stsp char *path = NULL;
4404 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4405 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4406 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4408 0ebf8283 2019-07-24 stsp err = got_worktree_get_histedit_list_path(&path, worktree);
4410 0ebf8283 2019-07-24 stsp return err;
4412 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4413 0ebf8283 2019-07-24 stsp if (f == NULL) {
4414 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4415 0ebf8283 2019-07-24 stsp goto done;
4417 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4418 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4423 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4424 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4425 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4426 0ebf8283 2019-07-24 stsp if (n < 0) {
4427 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4433 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4434 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4435 0ebf8283 2019-07-24 stsp free(path);
4436 0ebf8283 2019-07-24 stsp if (commit)
4437 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4438 0ebf8283 2019-07-24 stsp return err;
4441 0ebf8283 2019-07-24 stsp static const struct got_error *
4442 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4443 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4445 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4446 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4448 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4449 0ebf8283 2019-07-24 stsp if (f == NULL) {
4450 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4451 0ebf8283 2019-07-24 stsp goto done;
4454 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4456 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4457 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4458 0ebf8283 2019-07-24 stsp return err;
4461 0ebf8283 2019-07-24 stsp static const struct got_error *
4462 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4463 0ebf8283 2019-07-24 stsp const char *errmsg, struct got_object_id_queue *commits,
4464 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4466 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4467 0ebf8283 2019-07-24 stsp int resp = ' ';
4469 0ebf8283 2019-07-24 stsp while (resp != 'c' && resp != 'r' && resp != 'q') {
4470 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4471 0ebf8283 2019-07-24 stsp "or (a)bort: ", getprogname(), errmsg);
4472 0ebf8283 2019-07-24 stsp resp = getchar();
4473 0ebf8283 2019-07-24 stsp switch (resp) {
4475 0ebf8283 2019-07-24 stsp err = histedit_run_editor(histedit_cmds, path, repo);
4478 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4479 0ebf8283 2019-07-24 stsp commits, repo);
4482 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4485 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
4490 0ebf8283 2019-07-24 stsp return err;
4493 0ebf8283 2019-07-24 stsp static const struct got_error *
4494 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
4495 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch, struct got_reference *branch,
4496 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4498 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4499 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4500 0ebf8283 2019-07-24 stsp return got_worktree_histedit_complete(worktree, tmp_branch, branch,
4504 0ebf8283 2019-07-24 stsp static const struct got_error *
4505 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
4506 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4508 0ebf8283 2019-07-24 stsp const struct got_error *err;
4509 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4511 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
4513 0ebf8283 2019-07-24 stsp goto done;
4515 0ebf8283 2019-07-24 stsp if (new_id) {
4516 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
4518 0ebf8283 2019-07-24 stsp goto done;
4521 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
4522 0ebf8283 2019-07-24 stsp if (new_id_str)
4523 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
4525 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4526 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
4527 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
4528 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4529 0ebf8283 2019-07-24 stsp goto done;
4531 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
4533 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4535 0ebf8283 2019-07-24 stsp goto done;
4538 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
4539 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
4540 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
4541 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
4542 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4544 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
4545 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
4546 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4554 0ebf8283 2019-07-24 stsp free(old_id_str);
4555 0ebf8283 2019-07-24 stsp free(new_id_str);
4556 0ebf8283 2019-07-24 stsp return err;
4559 0ebf8283 2019-07-24 stsp static const struct got_error *
4560 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
4561 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
4562 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_repository *repo)
4564 0ebf8283 2019-07-24 stsp const struct got_error *err;
4565 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4566 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
4568 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4569 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
4570 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4572 0ebf8283 2019-07-24 stsp return err;
4575 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4577 0ebf8283 2019-07-24 stsp return err;
4579 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4580 0ebf8283 2019-07-24 stsp worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
4581 0ebf8283 2019-07-24 stsp if (err) {
4582 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4583 0ebf8283 2019-07-24 stsp goto done;
4584 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
4586 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
4587 0ebf8283 2019-07-24 stsp free(new_commit_id);
4590 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4591 0ebf8283 2019-07-24 stsp return err;
4594 0ebf8283 2019-07-24 stsp static const struct got_error *
4595 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
4596 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4598 0ebf8283 2019-07-24 stsp const struct got_error *error;
4599 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4601 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4603 0ebf8283 2019-07-24 stsp if (error)
4604 0ebf8283 2019-07-24 stsp return error;
4606 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4607 0ebf8283 2019-07-24 stsp if (error)
4608 0ebf8283 2019-07-24 stsp return error;
4610 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
4611 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4612 0ebf8283 2019-07-24 stsp return error;
4615 0ebf8283 2019-07-24 stsp static const struct got_error *
4616 0ebf8283 2019-07-24 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4617 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4619 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4620 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4621 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4622 0ebf8283 2019-07-24 stsp static char msg[80];
4623 0ebf8283 2019-07-24 stsp char *id_str;
4625 0ebf8283 2019-07-24 stsp if (TAILQ_EMPTY(histedit_cmds))
4626 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4628 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4629 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4630 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4633 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4634 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, qid->id);
4636 0ebf8283 2019-07-24 stsp return err;
4637 0ebf8283 2019-07-24 stsp snprintf(msg, sizeof(msg),
4638 0ebf8283 2019-07-24 stsp "commit %s missing from histedit script", id_str);
4639 0ebf8283 2019-07-24 stsp free(id_str);
4640 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4644 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4645 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4646 0ebf8283 2019-07-24 stsp "last commit in histedit script cannot be folded");
4648 0ebf8283 2019-07-24 stsp return NULL;
4651 0ebf8283 2019-07-24 stsp static const struct got_error *
4652 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
4654 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
4655 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
4656 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
4657 0ebf8283 2019-07-24 stsp char *cwd = NULL;
4658 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
4659 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
4660 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
4661 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
4662 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
4663 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4664 0ebf8283 2019-07-24 stsp int ch, rebase_in_progress = 0;
4665 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4666 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
4667 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4668 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
4669 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
4670 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
4671 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
4672 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
4673 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4675 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
4676 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
4677 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
4679 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
4680 0ebf8283 2019-07-24 stsp switch (ch) {
4682 0ebf8283 2019-07-24 stsp abort_edit = 1;
4685 0ebf8283 2019-07-24 stsp continue_edit = 1;
4688 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
4691 0ebf8283 2019-07-24 stsp usage_histedit();
4692 0ebf8283 2019-07-24 stsp /* NOTREACHED */
4696 0ebf8283 2019-07-24 stsp argc -= optind;
4697 0ebf8283 2019-07-24 stsp argv += optind;
4699 0ebf8283 2019-07-24 stsp #ifndef PROFILE
4700 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4701 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
4702 0ebf8283 2019-07-24 stsp err(1, "pledge");
4704 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
4705 0ebf8283 2019-07-24 stsp usage_histedit();
4706 0ebf8283 2019-07-24 stsp if (argc != 0)
4707 0ebf8283 2019-07-24 stsp usage_histedit();
4710 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
4711 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
4712 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
4713 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
4714 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
4715 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
4718 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
4719 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
4720 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
4721 0ebf8283 2019-07-24 stsp goto done;
4723 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
4724 0ebf8283 2019-07-24 stsp if (error)
4725 0ebf8283 2019-07-24 stsp goto done;
4727 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4728 0ebf8283 2019-07-24 stsp if (error != NULL)
4729 0ebf8283 2019-07-24 stsp goto done;
4731 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4732 0ebf8283 2019-07-24 stsp if (error)
4733 0ebf8283 2019-07-24 stsp goto done;
4734 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
4735 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
4736 0ebf8283 2019-07-24 stsp goto done;
4739 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4740 0ebf8283 2019-07-24 stsp if (error)
4741 0ebf8283 2019-07-24 stsp goto done;
4743 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
4744 0ebf8283 2019-07-24 stsp int did_something;
4745 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4746 0ebf8283 2019-07-24 stsp &tmp_branch, &branch, &base_commit_id, worktree, repo);
4747 0ebf8283 2019-07-24 stsp if (error)
4748 0ebf8283 2019-07-24 stsp goto done;
4749 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4750 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4751 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_abort(worktree, repo,
4752 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
4753 0ebf8283 2019-07-24 stsp if (error)
4754 0ebf8283 2019-07-24 stsp goto done;
4755 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
4756 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4757 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
4758 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
4759 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4760 0ebf8283 2019-07-24 stsp goto done;
4763 0ebf8283 2019-07-24 stsp if (continue_edit) {
4764 0ebf8283 2019-07-24 stsp char *path;
4766 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
4767 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4768 0ebf8283 2019-07-24 stsp goto done;
4771 0ebf8283 2019-07-24 stsp error = got_worktree_get_histedit_list_path(&path, worktree);
4772 0ebf8283 2019-07-24 stsp if (error)
4773 0ebf8283 2019-07-24 stsp goto done;
4775 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
4776 0ebf8283 2019-07-24 stsp free(path);
4777 0ebf8283 2019-07-24 stsp if (error)
4778 0ebf8283 2019-07-24 stsp goto done;
4780 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4781 0ebf8283 2019-07-24 stsp &tmp_branch, &branch, &base_commit_id, worktree, repo);
4782 0ebf8283 2019-07-24 stsp if (error)
4783 0ebf8283 2019-07-24 stsp goto done;
4785 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4786 0ebf8283 2019-07-24 stsp if (error)
4787 0ebf8283 2019-07-24 stsp goto done;
4789 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4790 0ebf8283 2019-07-24 stsp head_commit_id);
4791 0ebf8283 2019-07-24 stsp if (error)
4792 0ebf8283 2019-07-24 stsp goto done;
4793 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4794 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4795 0ebf8283 2019-07-24 stsp error = collect_commits_to_rebase(&commits, head_commit_id,
4796 0ebf8283 2019-07-24 stsp pid->id, base_commit_id,
4797 0ebf8283 2019-07-24 stsp got_worktree_get_path_prefix(worktree), repo);
4798 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4799 0ebf8283 2019-07-24 stsp commit = NULL;
4800 0ebf8283 2019-07-24 stsp if (error)
4801 0ebf8283 2019-07-24 stsp goto done;
4803 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
4804 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
4805 0ebf8283 2019-07-24 stsp goto done;
4808 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
4809 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
4810 0ebf8283 2019-07-24 stsp if (error != NULL)
4811 0ebf8283 2019-07-24 stsp goto done;
4813 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4814 0ebf8283 2019-07-24 stsp if (error)
4815 0ebf8283 2019-07-24 stsp goto done;
4817 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4818 0ebf8283 2019-07-24 stsp head_commit_id);
4819 0ebf8283 2019-07-24 stsp if (error)
4820 0ebf8283 2019-07-24 stsp goto done;
4821 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4822 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4823 0ebf8283 2019-07-24 stsp error = collect_commits_to_rebase(&commits, head_commit_id,
4824 0ebf8283 2019-07-24 stsp pid->id, got_worktree_get_base_commit_id(worktree),
4825 0ebf8283 2019-07-24 stsp got_worktree_get_path_prefix(worktree), repo);
4826 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4827 0ebf8283 2019-07-24 stsp commit = NULL;
4828 0ebf8283 2019-07-24 stsp if (error)
4829 0ebf8283 2019-07-24 stsp goto done;
4831 0ebf8283 2019-07-24 stsp if (edit_script_path) {
4832 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
4833 0ebf8283 2019-07-24 stsp edit_script_path, repo);
4834 0ebf8283 2019-07-24 stsp if (error)
4835 0ebf8283 2019-07-24 stsp goto done;
4837 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
4839 0ebf8283 2019-07-24 stsp if (error)
4840 0ebf8283 2019-07-24 stsp goto done;
4844 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
4846 0ebf8283 2019-07-24 stsp if (error)
4847 0ebf8283 2019-07-24 stsp goto done;
4849 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4850 0ebf8283 2019-07-24 stsp &base_commit_id, worktree, repo);
4851 0ebf8283 2019-07-24 stsp if (error)
4852 0ebf8283 2019-07-24 stsp goto done;
4856 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
4857 0ebf8283 2019-07-24 stsp if (error)
4858 0ebf8283 2019-07-24 stsp goto done;
4860 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4861 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
4862 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
4863 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
4866 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
4867 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4868 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
4869 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
4872 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
4873 0ebf8283 2019-07-24 stsp tmp_branch, hle, repo);
4875 0ebf8283 2019-07-24 stsp if (error)
4876 0ebf8283 2019-07-24 stsp goto done;
4880 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4881 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
4882 0ebf8283 2019-07-24 stsp if (error)
4883 0ebf8283 2019-07-24 stsp goto done;
4887 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4888 0ebf8283 2019-07-24 stsp hle->commit_id);
4889 0ebf8283 2019-07-24 stsp if (error)
4890 0ebf8283 2019-07-24 stsp goto done;
4891 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4892 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4894 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
4895 0ebf8283 2019-07-24 stsp worktree, pid->id, hle->commit_id, repo, rebase_progress,
4896 0ebf8283 2019-07-24 stsp &rebase_status, check_cancelled, NULL);
4897 0ebf8283 2019-07-24 stsp if (error)
4898 0ebf8283 2019-07-24 stsp goto done;
4899 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4900 0ebf8283 2019-07-24 stsp commit = NULL;
4902 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4903 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4907 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4908 0ebf8283 2019-07-24 stsp char *id_str;
4909 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
4910 0ebf8283 2019-07-24 stsp if (error)
4911 0ebf8283 2019-07-24 stsp goto done;
4912 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
4914 0ebf8283 2019-07-24 stsp free(id_str);
4915 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4916 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_postpone(worktree);
4917 0ebf8283 2019-07-24 stsp goto done;
4920 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4921 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
4922 0ebf8283 2019-07-24 stsp if (error)
4923 0ebf8283 2019-07-24 stsp goto done;
4927 0ebf8283 2019-07-24 stsp error = histedit_commit(&merged_paths, worktree, tmp_branch,
4928 0ebf8283 2019-07-24 stsp hle, repo);
4929 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4930 0ebf8283 2019-07-24 stsp if (error)
4931 0ebf8283 2019-07-24 stsp goto done;
4934 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4935 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_postpone(worktree);
4936 0ebf8283 2019-07-24 stsp if (error)
4937 0ebf8283 2019-07-24 stsp goto done;
4938 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4939 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
4941 0ebf8283 2019-07-24 stsp error = histedit_complete(worktree, tmp_branch, branch, repo);
4943 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
4944 0ebf8283 2019-07-24 stsp free(head_commit_id);
4945 0ebf8283 2019-07-24 stsp free(base_commit_id);
4946 0ebf8283 2019-07-24 stsp free(resume_commit_id);
4947 0ebf8283 2019-07-24 stsp if (commit)
4948 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4949 0ebf8283 2019-07-24 stsp if (branch)
4950 0ebf8283 2019-07-24 stsp got_ref_close(branch);
4951 0ebf8283 2019-07-24 stsp if (tmp_branch)
4952 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
4953 0ebf8283 2019-07-24 stsp if (worktree)
4954 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
4956 0ebf8283 2019-07-24 stsp got_repo_close(repo);
4957 0ebf8283 2019-07-24 stsp return error;