Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_worktree.h"
44 #include "got_diff.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
70 struct got_cmd {
71 const char *cmd_name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
75 };
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
138 };
140 static void
141 list_commands(void)
143 int i;
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
150 fputc('\n', stderr);
153 int
154 main(int argc, char *argv[])
156 struct got_cmd *cmd;
157 unsigned int i;
158 int ch;
159 int hflag = 0;
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
164 switch (ch) {
165 case 'h':
166 hflag = 1;
167 break;
168 default:
169 usage(hflag);
170 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
176 optind = 0;
178 if (argc <= 0)
179 usage(hflag);
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
191 continue;
193 if (hflag)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 return 1;
202 return 0;
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
206 list_commands();
207 return 1;
210 __dead static void
211 usage(int hflag)
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
214 if (hflag)
215 list_commands();
216 exit(1);
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
223 const char *editor;
225 editor = getenv("VISUAL");
226 if (editor == NULL)
227 editor = getenv("EDITOR");
229 if (editor) {
230 err = got_path_find_prog(abspath, editor);
231 if (err)
232 return err;
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
241 return NULL;
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
250 #ifdef PROFILE
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
253 #endif
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
264 if (err != NULL)
265 return err;
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
270 return NULL;
273 __dead static void
274 usage_init(void)
276 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
277 exit(1);
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
285 int ch;
287 while ((ch = getopt(argc, argv, "")) != -1) {
288 switch (ch) {
289 default:
290 usage_init();
291 /* NOTREACHED */
295 argc -= optind;
296 argv += optind;
298 #ifndef PROFILE
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
300 err(1, "pledge");
301 #endif
302 if (argc != 1)
303 usage_init();
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
312 if (error &&
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
314 goto done;
316 error = apply_unveil(repo_path, 0, NULL);
317 if (error)
318 goto done;
320 error = got_repo_init(repo_path);
321 if (error != NULL)
322 goto done;
324 done:
325 free(repo_path);
326 return error;
329 __dead static void
330 usage_import(void)
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
334 exit(1);
337 int
338 spawn_editor(const char *editor, const char *file)
340 pid_t pid;
341 sig_t sighup, sigint, sigquit;
342 int st = -1;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
349 case -1:
350 goto doneediting;
351 case 0:
352 execl(editor, editor, file, (char *)NULL);
353 _exit(127);
356 while (waitpid(pid, &st, 0) == -1)
357 if (errno != EINTR)
358 break;
360 doneediting:
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
366 errno = EINTR;
367 return -1;
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
378 char buf[1024];
379 struct stat st, st2;
380 FILE *fp;
381 int content_changed = 0;
382 size_t len;
384 *logmsg = NULL;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
400 if (*logmsg == NULL)
401 return got_error_from_errno("malloc");
402 (*logmsg)[0] = '\0';
403 len = 0;
405 fp = fopen(logmsg_path, "r");
406 if (fp == NULL) {
407 err = got_error_from_errno("fopen");
408 goto done;
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
412 content_changed = 1;
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
417 fclose(fp);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
421 len--;
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
427 done:
428 if (err) {
429 free(*logmsg);
430 *logmsg = NULL;
432 return err;
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
441 int fd;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
445 branch_name) == -1)
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
449 if (err)
450 goto done;
452 dprintf(fd, initial_content);
453 close(fd);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
456 done:
457 free(initial_content);
458 free(logmsg_path);
459 return err;
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
466 return NULL;
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
474 char *editor = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
481 int ch;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
488 switch (ch) {
489 case 'b':
490 branch_name = optarg;
491 break;
492 case 'm':
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
496 goto done;
498 break;
499 case 'r':
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
503 goto done;
505 break;
506 case 'I':
507 if (optarg[0] == '\0')
508 break;
509 error = got_pathlist_insert(&pe, &ignores, optarg,
510 NULL);
511 if (error)
512 goto done;
513 break;
514 default:
515 usage_init();
516 /* NOTREACHED */
520 argc -= optind;
521 argv += optind;
523 #ifndef PROFILE
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
525 NULL) == -1)
526 err(1, "pledge");
527 #endif
528 if (argc != 1)
529 usage_import();
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 goto done;
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
544 if (error)
545 goto done;
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
549 goto done;
552 error = got_ref_open(&branch_ref, repo, refname, 0);
553 if (error) {
554 if (error->code != GOT_ERR_NOT_REF)
555 goto done;
556 } else {
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
559 goto done;
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
565 goto done;
567 got_path_strip_trailing_slashes(path_dir);
569 /*
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
572 */
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
575 if (error)
576 goto done;
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
578 if (error)
579 goto done;
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
586 if (error)
587 goto done;
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
591 if (error)
592 goto done;
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
595 if (error)
596 goto done;
598 error = got_ref_write(branch_ref, repo);
599 if (error)
600 goto done;
602 error = got_object_id_str(&id_str, new_commit_id);
603 if (error)
604 goto done;
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
607 if (error) {
608 if (error->code != GOT_ERR_NOT_REF)
609 goto done;
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
612 branch_ref);
613 if (error)
614 goto done;
616 error = got_ref_write(head_ref, repo);
617 if (error)
618 goto done;
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
623 done:
624 free(repo_path);
625 free(editor);
626 free(refname);
627 free(new_commit_id);
628 free(id_str);
629 if (branch_ref)
630 got_ref_close(branch_ref);
631 if (head_ref)
632 got_ref_close(head_ref);
633 return error;
636 __dead static void
637 usage_checkout(void)
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
641 exit(1);
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
651 return NULL;
653 while (path[0] == '/')
654 path++;
656 printf("%c %s/%s\n", status, worktree_path, path);
657 return NULL;
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
665 return NULL;
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
677 if (err)
678 return err;
680 if (yca_id == NULL)
681 return got_error(GOT_ERR_ANCESTRY);
683 /*
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
690 * \ /
691 * C E
692 * \ /
693 * B (yca)
694 * |
695 * A
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
700 */
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
705 free(yca_id);
706 return NULL;
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_object_id *yca_id,
712 struct got_repository *repo)
714 const struct got_error *err = NULL;
715 struct got_commit_graph *graph = NULL;
716 struct got_object_id *head_commit_id = NULL;
717 int is_same_branch = 0;
719 err = got_ref_resolve(&head_commit_id, repo, head_ref);
720 if (err)
721 goto done;
723 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
724 is_same_branch = 1;
725 goto done;
727 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
728 is_same_branch = 1;
729 goto done;
732 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
733 if (err)
734 goto done;
736 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
737 if (err)
738 goto done;
740 for (;;) {
741 struct got_object_id *id;
742 err = got_commit_graph_iter_next(&id, graph);
743 if (err) {
744 if (err->code == GOT_ERR_ITER_COMPLETED) {
745 err = NULL;
746 break;
747 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
748 break;
749 err = got_commit_graph_fetch_commits(graph, 1,
750 repo);
751 if (err)
752 break;
755 if (id) {
756 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
757 break;
758 if (got_object_id_cmp(id, commit_id) == 0) {
759 is_same_branch = 1;
760 break;
764 done:
765 if (graph)
766 got_commit_graph_close(graph);
767 free(head_commit_id);
768 if (!err && !is_same_branch)
769 err = got_error(GOT_ERR_ANCESTRY);
770 return err;
773 static const struct got_error *
774 resolve_commit_arg(struct got_object_id **commit_id,
775 const char *commit_id_arg, struct got_repository *repo)
777 const struct got_error *err;
778 struct got_reference *ref;
780 err = got_ref_open(&ref, repo, commit_id_arg, 0);
781 if (err == NULL) {
782 err = got_ref_resolve(commit_id, repo, ref);
783 got_ref_close(ref);
784 } else {
785 if (err->code != GOT_ERR_NOT_REF)
786 return err;
787 err = got_repo_match_object_id_prefix(commit_id,
788 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
790 return err;
793 static const struct got_error *
794 cmd_checkout(int argc, char *argv[])
796 const struct got_error *error = NULL;
797 struct got_repository *repo = NULL;
798 struct got_reference *head_ref = NULL;
799 struct got_worktree *worktree = NULL;
800 char *repo_path = NULL;
801 char *worktree_path = NULL;
802 const char *path_prefix = "";
803 const char *branch_name = GOT_REF_HEAD;
804 char *commit_id_str = NULL;
805 int ch, same_path_prefix;
806 struct got_pathlist_head paths;
808 TAILQ_INIT(&paths);
810 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
811 switch (ch) {
812 case 'b':
813 branch_name = optarg;
814 break;
815 case 'c':
816 commit_id_str = strdup(optarg);
817 if (commit_id_str == NULL)
818 return got_error_from_errno("strdup");
819 break;
820 case 'p':
821 path_prefix = optarg;
822 break;
823 default:
824 usage_checkout();
825 /* NOTREACHED */
829 argc -= optind;
830 argv += optind;
832 #ifndef PROFILE
833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
834 "unveil", NULL) == -1)
835 err(1, "pledge");
836 #endif
837 if (argc == 1) {
838 char *cwd, *base, *dotgit;
839 repo_path = realpath(argv[0], NULL);
840 if (repo_path == NULL)
841 return got_error_from_errno2("realpath", argv[0]);
842 cwd = getcwd(NULL, 0);
843 if (cwd == NULL) {
844 error = got_error_from_errno("getcwd");
845 goto done;
847 if (path_prefix[0]) {
848 base = basename(path_prefix);
849 if (base == NULL) {
850 error = got_error_from_errno2("basename",
851 path_prefix);
852 goto done;
854 } else {
855 base = basename(repo_path);
856 if (base == NULL) {
857 error = got_error_from_errno2("basename",
858 repo_path);
859 goto done;
862 dotgit = strstr(base, ".git");
863 if (dotgit)
864 *dotgit = '\0';
865 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
866 error = got_error_from_errno("asprintf");
867 free(cwd);
868 goto done;
870 free(cwd);
871 } else if (argc == 2) {
872 repo_path = realpath(argv[0], NULL);
873 if (repo_path == NULL) {
874 error = got_error_from_errno2("realpath", argv[0]);
875 goto done;
877 worktree_path = realpath(argv[1], NULL);
878 if (worktree_path == NULL) {
879 if (errno != ENOENT) {
880 error = got_error_from_errno2("realpath",
881 argv[1]);
882 goto done;
884 worktree_path = strdup(argv[1]);
885 if (worktree_path == NULL) {
886 error = got_error_from_errno("strdup");
887 goto done;
890 } else
891 usage_checkout();
893 got_path_strip_trailing_slashes(repo_path);
894 got_path_strip_trailing_slashes(worktree_path);
896 error = got_repo_open(&repo, repo_path);
897 if (error != NULL)
898 goto done;
900 /* Pre-create work tree path for unveil(2) */
901 error = got_path_mkdir(worktree_path);
902 if (error) {
903 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
904 goto done;
905 if (!got_path_dir_is_empty(worktree_path)) {
906 error = got_error_path(worktree_path,
907 GOT_ERR_DIR_NOT_EMPTY);
908 goto done;
912 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
913 if (error)
914 goto done;
916 error = got_ref_open(&head_ref, repo, branch_name, 0);
917 if (error != NULL)
918 goto done;
920 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
921 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
922 goto done;
924 error = got_worktree_open(&worktree, worktree_path);
925 if (error != NULL)
926 goto done;
928 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
929 path_prefix);
930 if (error != NULL)
931 goto done;
932 if (!same_path_prefix) {
933 error = got_error(GOT_ERR_PATH_PREFIX);
934 goto done;
937 if (commit_id_str) {
938 struct got_object_id *commit_id;
939 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
940 if (error)
941 goto done;
942 error = check_linear_ancestry(commit_id,
943 got_worktree_get_base_commit_id(worktree), repo);
944 if (error != NULL) {
945 free(commit_id);
946 goto done;
948 error = check_same_branch(commit_id, head_ref, NULL, repo);
949 if (error)
950 goto done;
951 error = got_worktree_set_base_commit_id(worktree, repo,
952 commit_id);
953 free(commit_id);
954 if (error)
955 goto done;
958 error = got_pathlist_append(NULL, &paths, "", NULL);
959 if (error)
960 goto done;
961 error = got_worktree_checkout_files(worktree, &paths, repo,
962 checkout_progress, worktree_path, check_cancelled, NULL);
963 if (error != NULL)
964 goto done;
966 printf("Now shut up and hack\n");
968 done:
969 got_pathlist_free(&paths);
970 free(commit_id_str);
971 free(repo_path);
972 free(worktree_path);
973 return error;
976 __dead static void
977 usage_update(void)
979 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
980 getprogname());
981 exit(1);
984 static const struct got_error *
985 update_progress(void *arg, unsigned char status, const char *path)
987 int *did_something = arg;
989 if (status == GOT_STATUS_EXISTS)
990 return NULL;
992 *did_something = 1;
994 /* Base commit bump happens silently. */
995 if (status == GOT_STATUS_BUMP_BASE)
996 return NULL;
998 while (path[0] == '/')
999 path++;
1000 printf("%c %s\n", status, path);
1001 return NULL;
1004 static const struct got_error *
1005 switch_head_ref(struct got_reference *head_ref,
1006 struct got_object_id *commit_id, struct got_worktree *worktree,
1007 struct got_repository *repo)
1009 const struct got_error *err = NULL;
1010 char *base_id_str;
1011 int ref_has_moved = 0;
1013 /* Trivial case: switching between two different references. */
1014 if (strcmp(got_ref_get_name(head_ref),
1015 got_worktree_get_head_ref_name(worktree)) != 0) {
1016 printf("Switching work tree from %s to %s\n",
1017 got_worktree_get_head_ref_name(worktree),
1018 got_ref_get_name(head_ref));
1019 return got_worktree_set_head_ref(worktree, head_ref);
1022 err = check_linear_ancestry(commit_id,
1023 got_worktree_get_base_commit_id(worktree), repo);
1024 if (err) {
1025 if (err->code != GOT_ERR_ANCESTRY)
1026 return err;
1027 ref_has_moved = 1;
1029 if (!ref_has_moved)
1030 return NULL;
1032 /* Switching to a rebased branch with the same reference name. */
1033 err = got_object_id_str(&base_id_str,
1034 got_worktree_get_base_commit_id(worktree));
1035 if (err)
1036 return err;
1037 printf("Reference %s now points at a different branch\n",
1038 got_worktree_get_head_ref_name(worktree));
1039 printf("Switching work tree from %s to %s\n", base_id_str,
1040 got_worktree_get_head_ref_name(worktree));
1041 return NULL;
1044 static const struct got_error *
1045 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1047 const struct got_error *err;
1048 int in_progress;
1050 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1051 if (err)
1052 return err;
1053 if (in_progress)
1054 return got_error(GOT_ERR_REBASING);
1056 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1057 if (err)
1058 return err;
1059 if (in_progress)
1060 return got_error(GOT_ERR_HISTEDIT_BUSY);
1062 return NULL;
1065 static const struct got_error *
1066 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1067 char *argv[], struct got_worktree *worktree)
1069 const struct got_error *err;
1070 char *path;
1071 int i;
1073 if (argc == 0) {
1074 path = strdup("");
1075 if (path == NULL)
1076 return got_error_from_errno("strdup");
1077 return got_pathlist_append(NULL, paths, path, NULL);
1080 for (i = 0; i < argc; i++) {
1081 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1082 if (err)
1083 break;
1084 err = got_pathlist_append(NULL, paths, path, NULL);
1085 if (err) {
1086 free(path);
1087 break;
1091 return err;
1094 static const struct got_error *
1095 cmd_update(int argc, char *argv[])
1097 const struct got_error *error = NULL;
1098 struct got_repository *repo = NULL;
1099 struct got_worktree *worktree = NULL;
1100 char *worktree_path = NULL;
1101 struct got_object_id *commit_id = NULL;
1102 char *commit_id_str = NULL;
1103 const char *branch_name = NULL;
1104 struct got_reference *head_ref = NULL;
1105 struct got_pathlist_head paths;
1106 struct got_pathlist_entry *pe;
1107 int ch, did_something = 0;
1109 TAILQ_INIT(&paths);
1111 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1112 switch (ch) {
1113 case 'b':
1114 branch_name = optarg;
1115 break;
1116 case 'c':
1117 commit_id_str = strdup(optarg);
1118 if (commit_id_str == NULL)
1119 return got_error_from_errno("strdup");
1120 break;
1121 default:
1122 usage_update();
1123 /* NOTREACHED */
1127 argc -= optind;
1128 argv += optind;
1130 #ifndef PROFILE
1131 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1132 "unveil", NULL) == -1)
1133 err(1, "pledge");
1134 #endif
1135 worktree_path = getcwd(NULL, 0);
1136 if (worktree_path == NULL) {
1137 error = got_error_from_errno("getcwd");
1138 goto done;
1140 error = got_worktree_open(&worktree, worktree_path);
1141 if (error)
1142 goto done;
1144 error = check_rebase_or_histedit_in_progress(worktree);
1145 if (error)
1146 goto done;
1148 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1149 if (error)
1150 goto done;
1152 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1153 if (error != NULL)
1154 goto done;
1156 error = apply_unveil(got_repo_get_path(repo), 0,
1157 got_worktree_get_root_path(worktree));
1158 if (error)
1159 goto done;
1161 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1162 got_worktree_get_head_ref_name(worktree), 0);
1163 if (error != NULL)
1164 goto done;
1165 if (commit_id_str == NULL) {
1166 error = got_ref_resolve(&commit_id, repo, head_ref);
1167 if (error != NULL)
1168 goto done;
1169 error = got_object_id_str(&commit_id_str, commit_id);
1170 if (error != NULL)
1171 goto done;
1172 } else {
1173 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1174 free(commit_id_str);
1175 commit_id_str = NULL;
1176 if (error)
1177 goto done;
1178 error = got_object_id_str(&commit_id_str, commit_id);
1179 if (error)
1180 goto done;
1183 if (branch_name) {
1184 struct got_object_id *head_commit_id;
1185 TAILQ_FOREACH(pe, &paths, entry) {
1186 if (strlen(pe->path) == 0)
1187 continue;
1188 error = got_error_msg(GOT_ERR_BAD_PATH,
1189 "switching between branches requires that "
1190 "the entire work tree gets updated");
1191 goto done;
1193 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1194 if (error)
1195 goto done;
1196 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1197 free(head_commit_id);
1198 if (error != NULL)
1199 goto done;
1200 error = check_same_branch(commit_id, head_ref, NULL, repo);
1201 if (error)
1202 goto done;
1203 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1204 if (error)
1205 goto done;
1206 } else {
1207 error = check_linear_ancestry(commit_id,
1208 got_worktree_get_base_commit_id(worktree), repo);
1209 if (error != NULL) {
1210 if (error->code == GOT_ERR_ANCESTRY)
1211 error = got_error(GOT_ERR_BRANCH_MOVED);
1212 goto done;
1214 error = check_same_branch(commit_id, head_ref, NULL, repo);
1215 if (error)
1216 goto done;
1219 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1220 commit_id) != 0) {
1221 error = got_worktree_set_base_commit_id(worktree, repo,
1222 commit_id);
1223 if (error)
1224 goto done;
1227 error = got_worktree_checkout_files(worktree, &paths, repo,
1228 update_progress, &did_something, check_cancelled, NULL);
1229 if (error != NULL)
1230 goto done;
1232 if (did_something)
1233 printf("Updated to commit %s\n", commit_id_str);
1234 else
1235 printf("Already up-to-date\n");
1236 done:
1237 free(worktree_path);
1238 TAILQ_FOREACH(pe, &paths, entry)
1239 free((char *)pe->path);
1240 got_pathlist_free(&paths);
1241 free(commit_id);
1242 free(commit_id_str);
1243 return error;
1246 static const struct got_error *
1247 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1248 int diff_context, struct got_repository *repo)
1250 const struct got_error *err = NULL;
1251 struct got_tree_object *tree1 = NULL, *tree2;
1252 struct got_object_qid *qid;
1253 char *id_str1 = NULL, *id_str2;
1254 struct got_diff_blob_output_unidiff_arg arg;
1256 err = got_object_open_as_tree(&tree2, repo,
1257 got_object_commit_get_tree_id(commit));
1258 if (err)
1259 return err;
1261 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1262 if (qid != NULL) {
1263 struct got_commit_object *pcommit;
1265 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1266 if (err)
1267 return err;
1269 err = got_object_open_as_tree(&tree1, repo,
1270 got_object_commit_get_tree_id(pcommit));
1271 got_object_commit_close(pcommit);
1272 if (err)
1273 return err;
1275 err = got_object_id_str(&id_str1, qid->id);
1276 if (err)
1277 return err;
1280 err = got_object_id_str(&id_str2, id);
1281 if (err)
1282 goto done;
1284 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1285 arg.diff_context = diff_context;
1286 arg.outfile = stdout;
1287 err = got_diff_tree(tree1, tree2, "", "", repo,
1288 got_diff_blob_output_unidiff, &arg, 1);
1289 done:
1290 if (tree1)
1291 got_object_tree_close(tree1);
1292 got_object_tree_close(tree2);
1293 free(id_str1);
1294 free(id_str2);
1295 return err;
1298 static char *
1299 get_datestr(time_t *time, char *datebuf)
1301 char *p, *s = ctime_r(time, datebuf);
1302 p = strchr(s, '\n');
1303 if (p)
1304 *p = '\0';
1305 return s;
1308 static const struct got_error *
1309 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1310 struct got_repository *repo, int show_patch, int diff_context,
1311 struct got_reflist_head *refs)
1313 const struct got_error *err = NULL;
1314 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1315 char datebuf[26];
1316 time_t committer_time;
1317 const char *author, *committer;
1318 char *refs_str = NULL;
1319 struct got_reflist_entry *re;
1321 SIMPLEQ_FOREACH(re, refs, entry) {
1322 char *s;
1323 const char *name;
1324 if (got_object_id_cmp(re->id, id) != 0)
1325 continue;
1326 name = got_ref_get_name(re->ref);
1327 if (strcmp(name, GOT_REF_HEAD) == 0)
1328 continue;
1329 if (strncmp(name, "refs/", 5) == 0)
1330 name += 5;
1331 if (strncmp(name, "got/", 4) == 0)
1332 continue;
1333 if (strncmp(name, "heads/", 6) == 0)
1334 name += 6;
1335 if (strncmp(name, "remotes/", 8) == 0)
1336 name += 8;
1337 s = refs_str;
1338 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1339 name) == -1) {
1340 err = got_error_from_errno("asprintf");
1341 free(s);
1342 break;
1344 free(s);
1346 err = got_object_id_str(&id_str, id);
1347 if (err)
1348 return err;
1350 printf("-----------------------------------------------\n");
1351 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1352 refs_str ? refs_str : "", refs_str ? ")" : "");
1353 free(id_str);
1354 id_str = NULL;
1355 free(refs_str);
1356 refs_str = NULL;
1357 printf("from: %s\n", got_object_commit_get_author(commit));
1358 committer_time = got_object_commit_get_committer_time(commit);
1359 datestr = get_datestr(&committer_time, datebuf);
1360 printf("date: %s UTC\n", datestr);
1361 author = got_object_commit_get_author(commit);
1362 committer = got_object_commit_get_committer(commit);
1363 if (strcmp(author, committer) != 0)
1364 printf("via: %s\n", committer);
1365 if (got_object_commit_get_nparents(commit) > 1) {
1366 const struct got_object_id_queue *parent_ids;
1367 struct got_object_qid *qid;
1368 int n = 1;
1369 parent_ids = got_object_commit_get_parent_ids(commit);
1370 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1371 err = got_object_id_str(&id_str, qid->id);
1372 if (err)
1373 return err;
1374 printf("parent %d: %s\n", n++, id_str);
1375 free(id_str);
1379 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1380 if (logmsg0 == NULL)
1381 return got_error_from_errno("strdup");
1383 logmsg = logmsg0;
1384 do {
1385 line = strsep(&logmsg, "\n");
1386 if (line)
1387 printf(" %s\n", line);
1388 } while (line);
1389 free(logmsg0);
1391 if (show_patch) {
1392 err = print_patch(commit, id, diff_context, repo);
1393 if (err == 0)
1394 printf("\n");
1397 if (fflush(stdout) != 0 && err == NULL)
1398 err = got_error_from_errno("fflush");
1399 return err;
1402 static const struct got_error *
1403 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1404 char *path, int show_patch, int diff_context, int limit,
1405 int first_parent_traversal, struct got_reflist_head *refs)
1407 const struct got_error *err;
1408 struct got_commit_graph *graph;
1410 err = got_commit_graph_open(&graph, root_id, path,
1411 first_parent_traversal, repo);
1412 if (err)
1413 return err;
1414 err = got_commit_graph_iter_start(graph, root_id, repo);
1415 if (err)
1416 goto done;
1417 for (;;) {
1418 struct got_commit_object *commit;
1419 struct got_object_id *id;
1421 if (sigint_received || sigpipe_received)
1422 break;
1424 err = got_commit_graph_iter_next(&id, graph);
1425 if (err) {
1426 if (err->code == GOT_ERR_ITER_COMPLETED) {
1427 err = NULL;
1428 break;
1430 if (err->code != GOT_ERR_ITER_NEED_MORE)
1431 break;
1432 err = got_commit_graph_fetch_commits(graph, 1, repo);
1433 if (err)
1434 break;
1435 else
1436 continue;
1438 if (id == NULL)
1439 break;
1441 err = got_object_open_as_commit(&commit, repo, id);
1442 if (err)
1443 break;
1444 err = print_commit(commit, id, repo, show_patch, diff_context,
1445 refs);
1446 got_object_commit_close(commit);
1447 if (err || (limit && --limit == 0))
1448 break;
1450 done:
1451 got_commit_graph_close(graph);
1452 return err;
1455 __dead static void
1456 usage_log(void)
1458 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1459 "[-r repository-path] [path]\n", getprogname());
1460 exit(1);
1463 static const struct got_error *
1464 cmd_log(int argc, char *argv[])
1466 const struct got_error *error;
1467 struct got_repository *repo = NULL;
1468 struct got_worktree *worktree = NULL;
1469 struct got_commit_object *commit = NULL;
1470 struct got_object_id *id = NULL;
1471 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1472 char *start_commit = NULL;
1473 int diff_context = 3, ch;
1474 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1475 const char *errstr;
1476 struct got_reflist_head refs;
1478 SIMPLEQ_INIT(&refs);
1480 #ifndef PROFILE
1481 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1482 NULL)
1483 == -1)
1484 err(1, "pledge");
1485 #endif
1487 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1488 switch (ch) {
1489 case 'p':
1490 show_patch = 1;
1491 break;
1492 case 'c':
1493 start_commit = optarg;
1494 break;
1495 case 'C':
1496 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1497 &errstr);
1498 if (errstr != NULL)
1499 err(1, "-C option %s", errstr);
1500 break;
1501 case 'l':
1502 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1503 if (errstr != NULL)
1504 err(1, "-l option %s", errstr);
1505 break;
1506 case 'f':
1507 first_parent_traversal = 1;
1508 break;
1509 case 'r':
1510 repo_path = realpath(optarg, NULL);
1511 if (repo_path == NULL)
1512 err(1, "-r option");
1513 got_path_strip_trailing_slashes(repo_path);
1514 break;
1515 default:
1516 usage_log();
1517 /* NOTREACHED */
1521 argc -= optind;
1522 argv += optind;
1524 cwd = getcwd(NULL, 0);
1525 if (cwd == NULL) {
1526 error = got_error_from_errno("getcwd");
1527 goto done;
1530 error = got_worktree_open(&worktree, cwd);
1531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1532 goto done;
1533 error = NULL;
1535 if (argc == 0) {
1536 path = strdup("");
1537 if (path == NULL) {
1538 error = got_error_from_errno("strdup");
1539 goto done;
1541 } else if (argc == 1) {
1542 if (worktree) {
1543 error = got_worktree_resolve_path(&path, worktree,
1544 argv[0]);
1545 if (error)
1546 goto done;
1547 } else {
1548 path = strdup(argv[0]);
1549 if (path == NULL) {
1550 error = got_error_from_errno("strdup");
1551 goto done;
1554 } else
1555 usage_log();
1557 if (repo_path == NULL) {
1558 repo_path = worktree ?
1559 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1561 if (repo_path == NULL) {
1562 error = got_error_from_errno("strdup");
1563 goto done;
1566 error = got_repo_open(&repo, repo_path);
1567 if (error != NULL)
1568 goto done;
1570 error = apply_unveil(got_repo_get_path(repo), 1,
1571 worktree ? got_worktree_get_root_path(worktree) : NULL);
1572 if (error)
1573 goto done;
1575 if (start_commit == NULL) {
1576 struct got_reference *head_ref;
1577 error = got_ref_open(&head_ref, repo,
1578 worktree ? got_worktree_get_head_ref_name(worktree)
1579 : GOT_REF_HEAD, 0);
1580 if (error != NULL)
1581 return error;
1582 error = got_ref_resolve(&id, repo, head_ref);
1583 got_ref_close(head_ref);
1584 if (error != NULL)
1585 return error;
1586 error = got_object_open_as_commit(&commit, repo, id);
1587 } else {
1588 struct got_reference *ref;
1589 error = got_ref_open(&ref, repo, start_commit, 0);
1590 if (error == NULL) {
1591 int obj_type;
1592 error = got_ref_resolve(&id, repo, ref);
1593 got_ref_close(ref);
1594 if (error != NULL)
1595 goto done;
1596 error = got_object_get_type(&obj_type, repo, id);
1597 if (error != NULL)
1598 goto done;
1599 if (obj_type == GOT_OBJ_TYPE_TAG) {
1600 struct got_tag_object *tag;
1601 error = got_object_open_as_tag(&tag, repo, id);
1602 if (error != NULL)
1603 goto done;
1604 if (got_object_tag_get_object_type(tag) !=
1605 GOT_OBJ_TYPE_COMMIT) {
1606 got_object_tag_close(tag);
1607 error = got_error(GOT_ERR_OBJ_TYPE);
1608 goto done;
1610 free(id);
1611 id = got_object_id_dup(
1612 got_object_tag_get_object_id(tag));
1613 if (id == NULL)
1614 error = got_error_from_errno(
1615 "got_object_id_dup");
1616 got_object_tag_close(tag);
1617 if (error)
1618 goto done;
1619 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1620 error = got_error(GOT_ERR_OBJ_TYPE);
1621 goto done;
1623 error = got_object_open_as_commit(&commit, repo, id);
1624 if (error != NULL)
1625 goto done;
1627 if (commit == NULL) {
1628 error = got_repo_match_object_id_prefix(&id,
1629 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1630 if (error != NULL)
1631 return error;
1634 if (error != NULL)
1635 goto done;
1637 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1638 if (error != NULL)
1639 goto done;
1640 if (in_repo_path) {
1641 free(path);
1642 path = in_repo_path;
1645 error = got_ref_list(&refs, repo);
1646 if (error)
1647 goto done;
1649 error = print_commits(id, repo, path, show_patch,
1650 diff_context, limit, first_parent_traversal, &refs);
1651 done:
1652 free(path);
1653 free(repo_path);
1654 free(cwd);
1655 free(id);
1656 if (worktree)
1657 got_worktree_close(worktree);
1658 if (repo) {
1659 const struct got_error *repo_error;
1660 repo_error = got_repo_close(repo);
1661 if (error == NULL)
1662 error = repo_error;
1664 got_ref_list_free(&refs);
1665 return error;
1668 __dead static void
1669 usage_diff(void)
1671 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1672 "[object1 object2 | path]\n", getprogname());
1673 exit(1);
1676 struct print_diff_arg {
1677 struct got_repository *repo;
1678 struct got_worktree *worktree;
1679 int diff_context;
1680 const char *id_str;
1681 int header_shown;
1684 static const struct got_error *
1685 print_diff(void *arg, unsigned char status, const char *path,
1686 struct got_object_id *blob_id, struct got_object_id *commit_id)
1688 struct print_diff_arg *a = arg;
1689 const struct got_error *err = NULL;
1690 struct got_blob_object *blob1 = NULL;
1691 FILE *f2 = NULL;
1692 char *abspath = NULL;
1693 struct stat sb;
1695 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1696 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1697 return NULL;
1699 if (!a->header_shown) {
1700 printf("diff %s %s\n", a->id_str,
1701 got_worktree_get_root_path(a->worktree));
1702 a->header_shown = 1;
1705 if (status != GOT_STATUS_ADD) {
1706 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1707 if (err)
1708 goto done;
1712 if (status != GOT_STATUS_DELETE) {
1713 if (asprintf(&abspath, "%s/%s",
1714 got_worktree_get_root_path(a->worktree), path) == -1) {
1715 err = got_error_from_errno("asprintf");
1716 goto done;
1719 f2 = fopen(abspath, "r");
1720 if (f2 == NULL) {
1721 err = got_error_from_errno2("fopen", abspath);
1722 goto done;
1724 if (lstat(abspath, &sb) == -1) {
1725 err = got_error_from_errno2("lstat", abspath);
1726 goto done;
1728 } else
1729 sb.st_size = 0;
1731 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1732 stdout);
1733 done:
1734 if (blob1)
1735 got_object_blob_close(blob1);
1736 if (f2 && fclose(f2) != 0 && err == NULL)
1737 err = got_error_from_errno("fclose");
1738 free(abspath);
1739 return err;
1742 static const struct got_error *
1743 cmd_diff(int argc, char *argv[])
1745 const struct got_error *error;
1746 struct got_repository *repo = NULL;
1747 struct got_worktree *worktree = NULL;
1748 char *cwd = NULL, *repo_path = NULL;
1749 struct got_object_id *id1 = NULL, *id2 = NULL;
1750 const char *id_str1 = NULL, *id_str2 = NULL;
1751 char *label1 = NULL, *label2 = NULL;
1752 int type1, type2;
1753 int diff_context = 3, ch;
1754 const char *errstr;
1755 char *path = NULL;
1757 #ifndef PROFILE
1758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1759 NULL) == -1)
1760 err(1, "pledge");
1761 #endif
1763 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1764 switch (ch) {
1765 case 'C':
1766 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1767 if (errstr != NULL)
1768 err(1, "-C option %s", errstr);
1769 break;
1770 case 'r':
1771 repo_path = realpath(optarg, NULL);
1772 if (repo_path == NULL)
1773 err(1, "-r option");
1774 got_path_strip_trailing_slashes(repo_path);
1775 break;
1776 default:
1777 usage_diff();
1778 /* NOTREACHED */
1782 argc -= optind;
1783 argv += optind;
1785 cwd = getcwd(NULL, 0);
1786 if (cwd == NULL) {
1787 error = got_error_from_errno("getcwd");
1788 goto done;
1790 error = got_worktree_open(&worktree, cwd);
1791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1792 goto done;
1793 if (argc <= 1) {
1794 if (worktree == NULL) {
1795 error = got_error(GOT_ERR_NOT_WORKTREE);
1796 goto done;
1798 if (repo_path)
1799 errx(1,
1800 "-r option can't be used when diffing a work tree");
1801 repo_path = strdup(got_worktree_get_repo_path(worktree));
1802 if (repo_path == NULL) {
1803 error = got_error_from_errno("strdup");
1804 goto done;
1806 if (argc == 1) {
1807 error = got_worktree_resolve_path(&path, worktree,
1808 argv[0]);
1809 if (error)
1810 goto done;
1811 } else {
1812 path = strdup("");
1813 if (path == NULL) {
1814 error = got_error_from_errno("strdup");
1815 goto done;
1818 } else if (argc == 2) {
1819 id_str1 = argv[0];
1820 id_str2 = argv[1];
1821 if (worktree && repo_path == NULL) {
1822 repo_path =
1823 strdup(got_worktree_get_repo_path(worktree));
1824 if (repo_path == NULL) {
1825 error = got_error_from_errno("strdup");
1826 goto done;
1829 } else
1830 usage_diff();
1832 if (repo_path == NULL) {
1833 repo_path = getcwd(NULL, 0);
1834 if (repo_path == NULL)
1835 return got_error_from_errno("getcwd");
1838 error = got_repo_open(&repo, repo_path);
1839 free(repo_path);
1840 if (error != NULL)
1841 goto done;
1843 error = apply_unveil(got_repo_get_path(repo), 1,
1844 worktree ? got_worktree_get_root_path(worktree) : NULL);
1845 if (error)
1846 goto done;
1848 if (argc <= 1) {
1849 struct print_diff_arg arg;
1850 struct got_pathlist_head paths;
1851 char *id_str;
1853 TAILQ_INIT(&paths);
1855 error = got_object_id_str(&id_str,
1856 got_worktree_get_base_commit_id(worktree));
1857 if (error)
1858 goto done;
1859 arg.repo = repo;
1860 arg.worktree = worktree;
1861 arg.diff_context = diff_context;
1862 arg.id_str = id_str;
1863 arg.header_shown = 0;
1865 error = got_pathlist_append(NULL, &paths, path, NULL);
1866 if (error)
1867 goto done;
1869 error = got_worktree_status(worktree, &paths, repo, print_diff,
1870 &arg, check_cancelled, NULL);
1871 free(id_str);
1872 got_pathlist_free(&paths);
1873 goto done;
1876 error = got_repo_match_object_id_prefix(&id1, id_str1,
1877 GOT_OBJ_TYPE_ANY, repo);
1878 if (error) {
1879 struct got_reference *ref;
1880 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1881 goto done;
1882 error = got_ref_open(&ref, repo, id_str1, 0);
1883 if (error != NULL)
1884 goto done;
1885 label1 = strdup(got_ref_get_name(ref));
1886 if (label1 == NULL) {
1887 error = got_error_from_errno("strdup");
1888 goto done;
1890 error = got_ref_resolve(&id1, repo, ref);
1891 got_ref_close(ref);
1892 if (error != NULL)
1893 goto done;
1894 } else {
1895 error = got_object_id_str(&label1, id1);
1896 if (label1 == NULL) {
1897 error = got_error_from_errno("strdup");
1898 goto done;
1902 error = got_repo_match_object_id_prefix(&id2, id_str2,
1903 GOT_OBJ_TYPE_ANY, repo);
1904 if (error) {
1905 struct got_reference *ref;
1906 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1907 goto done;
1908 error = got_ref_open(&ref, repo, id_str2, 0);
1909 if (error != NULL)
1910 goto done;
1911 label2 = strdup(got_ref_get_name(ref));
1912 if (label2 == NULL) {
1913 error = got_error_from_errno("strdup");
1914 goto done;
1916 error = got_ref_resolve(&id2, repo, ref);
1917 got_ref_close(ref);
1918 if (error != NULL)
1919 goto done;
1920 } else {
1921 error = got_object_id_str(&label2, id2);
1922 if (label2 == NULL) {
1923 error = got_error_from_errno("strdup");
1924 goto done;
1928 error = got_object_get_type(&type1, repo, id1);
1929 if (error)
1930 goto done;
1932 error = got_object_get_type(&type2, repo, id2);
1933 if (error)
1934 goto done;
1936 if (type1 != type2) {
1937 error = got_error(GOT_ERR_OBJ_TYPE);
1938 goto done;
1941 switch (type1) {
1942 case GOT_OBJ_TYPE_BLOB:
1943 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1944 diff_context, repo, stdout);
1945 break;
1946 case GOT_OBJ_TYPE_TREE:
1947 error = got_diff_objects_as_trees(id1, id2, "", "",
1948 diff_context, repo, stdout);
1949 break;
1950 case GOT_OBJ_TYPE_COMMIT:
1951 printf("diff %s %s\n", label1, label2);
1952 error = got_diff_objects_as_commits(id1, id2, diff_context,
1953 repo, stdout);
1954 break;
1955 default:
1956 error = got_error(GOT_ERR_OBJ_TYPE);
1959 done:
1960 free(label1);
1961 free(label2);
1962 free(id1);
1963 free(id2);
1964 free(path);
1965 if (worktree)
1966 got_worktree_close(worktree);
1967 if (repo) {
1968 const struct got_error *repo_error;
1969 repo_error = got_repo_close(repo);
1970 if (error == NULL)
1971 error = repo_error;
1973 return error;
1976 __dead static void
1977 usage_blame(void)
1979 fprintf(stderr,
1980 "usage: %s blame [-c commit] [-r repository-path] path\n",
1981 getprogname());
1982 exit(1);
1985 static const struct got_error *
1986 cmd_blame(int argc, char *argv[])
1988 const struct got_error *error;
1989 struct got_repository *repo = NULL;
1990 struct got_worktree *worktree = NULL;
1991 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1992 struct got_object_id *commit_id = NULL;
1993 char *commit_id_str = NULL;
1994 int ch;
1996 #ifndef PROFILE
1997 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1998 NULL) == -1)
1999 err(1, "pledge");
2000 #endif
2002 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2003 switch (ch) {
2004 case 'c':
2005 commit_id_str = optarg;
2006 break;
2007 case 'r':
2008 repo_path = realpath(optarg, NULL);
2009 if (repo_path == NULL)
2010 err(1, "-r option");
2011 got_path_strip_trailing_slashes(repo_path);
2012 break;
2013 default:
2014 usage_blame();
2015 /* NOTREACHED */
2019 argc -= optind;
2020 argv += optind;
2022 if (argc == 1)
2023 path = argv[0];
2024 else
2025 usage_blame();
2027 cwd = getcwd(NULL, 0);
2028 if (cwd == NULL) {
2029 error = got_error_from_errno("getcwd");
2030 goto done;
2032 if (repo_path == NULL) {
2033 error = got_worktree_open(&worktree, cwd);
2034 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2035 goto done;
2036 else
2037 error = NULL;
2038 if (worktree) {
2039 repo_path =
2040 strdup(got_worktree_get_repo_path(worktree));
2041 if (repo_path == NULL)
2042 error = got_error_from_errno("strdup");
2043 if (error)
2044 goto done;
2045 } else {
2046 repo_path = strdup(cwd);
2047 if (repo_path == NULL) {
2048 error = got_error_from_errno("strdup");
2049 goto done;
2054 error = got_repo_open(&repo, repo_path);
2055 if (error != NULL)
2056 goto done;
2058 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2059 if (error)
2060 goto done;
2062 if (worktree) {
2063 const char *prefix = got_worktree_get_path_prefix(worktree);
2064 char *p, *worktree_subdir = cwd +
2065 strlen(got_worktree_get_root_path(worktree));
2066 if (asprintf(&p, "%s%s%s%s%s",
2067 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2068 worktree_subdir, worktree_subdir[0] ? "/" : "",
2069 path) == -1) {
2070 error = got_error_from_errno("asprintf");
2071 goto done;
2073 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2074 free(p);
2075 } else {
2076 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2078 if (error)
2079 goto done;
2081 if (commit_id_str == NULL) {
2082 struct got_reference *head_ref;
2083 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2084 if (error != NULL)
2085 goto done;
2086 error = got_ref_resolve(&commit_id, repo, head_ref);
2087 got_ref_close(head_ref);
2088 if (error != NULL)
2089 goto done;
2090 } else {
2091 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2092 if (error)
2093 goto done;
2096 error = got_blame(in_repo_path, commit_id, repo, stdout);
2097 done:
2098 free(in_repo_path);
2099 free(repo_path);
2100 free(cwd);
2101 free(commit_id);
2102 if (worktree)
2103 got_worktree_close(worktree);
2104 if (repo) {
2105 const struct got_error *repo_error;
2106 repo_error = got_repo_close(repo);
2107 if (error == NULL)
2108 error = repo_error;
2110 return error;
2113 __dead static void
2114 usage_tree(void)
2116 fprintf(stderr,
2117 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2118 getprogname());
2119 exit(1);
2122 static void
2123 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2124 const char *root_path)
2126 int is_root_path = (strcmp(path, root_path) == 0);
2128 path += strlen(root_path);
2129 while (path[0] == '/')
2130 path++;
2132 printf("%s%s%s%s%s\n", id ? id : "", path,
2133 is_root_path ? "" : "/", te->name,
2134 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2137 static const struct got_error *
2138 print_tree(const char *path, struct got_object_id *commit_id,
2139 int show_ids, int recurse, const char *root_path,
2140 struct got_repository *repo)
2142 const struct got_error *err = NULL;
2143 struct got_object_id *tree_id = NULL;
2144 struct got_tree_object *tree = NULL;
2145 const struct got_tree_entries *entries;
2146 struct got_tree_entry *te;
2148 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2149 if (err)
2150 goto done;
2152 err = got_object_open_as_tree(&tree, repo, tree_id);
2153 if (err)
2154 goto done;
2155 entries = got_object_tree_get_entries(tree);
2156 te = SIMPLEQ_FIRST(&entries->head);
2157 while (te) {
2158 char *id = NULL;
2160 if (sigint_received || sigpipe_received)
2161 break;
2163 if (show_ids) {
2164 char *id_str;
2165 err = got_object_id_str(&id_str, te->id);
2166 if (err)
2167 goto done;
2168 if (asprintf(&id, "%s ", id_str) == -1) {
2169 err = got_error_from_errno("asprintf");
2170 free(id_str);
2171 goto done;
2173 free(id_str);
2175 print_entry(te, id, path, root_path);
2176 free(id);
2178 if (recurse && S_ISDIR(te->mode)) {
2179 char *child_path;
2180 if (asprintf(&child_path, "%s%s%s", path,
2181 path[0] == '/' && path[1] == '\0' ? "" : "/",
2182 te->name) == -1) {
2183 err = got_error_from_errno("asprintf");
2184 goto done;
2186 err = print_tree(child_path, commit_id, show_ids, 1,
2187 root_path, repo);
2188 free(child_path);
2189 if (err)
2190 goto done;
2193 te = SIMPLEQ_NEXT(te, entry);
2195 done:
2196 if (tree)
2197 got_object_tree_close(tree);
2198 free(tree_id);
2199 return err;
2202 static const struct got_error *
2203 cmd_tree(int argc, char *argv[])
2205 const struct got_error *error;
2206 struct got_repository *repo = NULL;
2207 struct got_worktree *worktree = NULL;
2208 const char *path;
2209 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2210 struct got_object_id *commit_id = NULL;
2211 char *commit_id_str = NULL;
2212 int show_ids = 0, recurse = 0;
2213 int ch;
2215 #ifndef PROFILE
2216 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2217 NULL) == -1)
2218 err(1, "pledge");
2219 #endif
2221 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2222 switch (ch) {
2223 case 'c':
2224 commit_id_str = optarg;
2225 break;
2226 case 'r':
2227 repo_path = realpath(optarg, NULL);
2228 if (repo_path == NULL)
2229 err(1, "-r option");
2230 got_path_strip_trailing_slashes(repo_path);
2231 break;
2232 case 'i':
2233 show_ids = 1;
2234 break;
2235 case 'R':
2236 recurse = 1;
2237 break;
2238 default:
2239 usage_tree();
2240 /* NOTREACHED */
2244 argc -= optind;
2245 argv += optind;
2247 if (argc == 1)
2248 path = argv[0];
2249 else if (argc > 1)
2250 usage_tree();
2251 else
2252 path = NULL;
2254 cwd = getcwd(NULL, 0);
2255 if (cwd == NULL) {
2256 error = got_error_from_errno("getcwd");
2257 goto done;
2259 if (repo_path == NULL) {
2260 error = got_worktree_open(&worktree, cwd);
2261 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2262 goto done;
2263 else
2264 error = NULL;
2265 if (worktree) {
2266 repo_path =
2267 strdup(got_worktree_get_repo_path(worktree));
2268 if (repo_path == NULL)
2269 error = got_error_from_errno("strdup");
2270 if (error)
2271 goto done;
2272 } else {
2273 repo_path = strdup(cwd);
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2276 goto done;
2281 error = got_repo_open(&repo, repo_path);
2282 if (error != NULL)
2283 goto done;
2285 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2286 if (error)
2287 goto done;
2289 if (path == NULL) {
2290 if (worktree) {
2291 char *p, *worktree_subdir = cwd +
2292 strlen(got_worktree_get_root_path(worktree));
2293 if (asprintf(&p, "%s/%s",
2294 got_worktree_get_path_prefix(worktree),
2295 worktree_subdir) == -1) {
2296 error = got_error_from_errno("asprintf");
2297 goto done;
2299 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2300 free(p);
2301 if (error)
2302 goto done;
2303 } else
2304 path = "/";
2306 if (in_repo_path == NULL) {
2307 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2308 if (error != NULL)
2309 goto done;
2312 if (commit_id_str == NULL) {
2313 struct got_reference *head_ref;
2314 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2315 if (error != NULL)
2316 goto done;
2317 error = got_ref_resolve(&commit_id, repo, head_ref);
2318 got_ref_close(head_ref);
2319 if (error != NULL)
2320 goto done;
2321 } else {
2322 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2323 if (error)
2324 goto done;
2327 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2328 in_repo_path, repo);
2329 done:
2330 free(in_repo_path);
2331 free(repo_path);
2332 free(cwd);
2333 free(commit_id);
2334 if (worktree)
2335 got_worktree_close(worktree);
2336 if (repo) {
2337 const struct got_error *repo_error;
2338 repo_error = got_repo_close(repo);
2339 if (error == NULL)
2340 error = repo_error;
2342 return error;
2345 __dead static void
2346 usage_status(void)
2348 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2349 exit(1);
2352 static const struct got_error *
2353 print_status(void *arg, unsigned char status, const char *path,
2354 struct got_object_id *blob_id, struct got_object_id *commit_id)
2356 printf("%c %s\n", status, path);
2357 return NULL;
2360 static const struct got_error *
2361 cmd_status(int argc, char *argv[])
2363 const struct got_error *error = NULL;
2364 struct got_repository *repo = NULL;
2365 struct got_worktree *worktree = NULL;
2366 char *cwd = NULL;
2367 struct got_pathlist_head paths;
2368 struct got_pathlist_entry *pe;
2369 int ch;
2371 TAILQ_INIT(&paths);
2373 while ((ch = getopt(argc, argv, "")) != -1) {
2374 switch (ch) {
2375 default:
2376 usage_status();
2377 /* NOTREACHED */
2381 argc -= optind;
2382 argv += optind;
2384 #ifndef PROFILE
2385 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2386 NULL) == -1)
2387 err(1, "pledge");
2388 #endif
2389 cwd = getcwd(NULL, 0);
2390 if (cwd == NULL) {
2391 error = got_error_from_errno("getcwd");
2392 goto done;
2395 error = got_worktree_open(&worktree, cwd);
2396 if (error != NULL)
2397 goto done;
2399 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2400 if (error)
2401 goto done;
2403 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2404 if (error != NULL)
2405 goto done;
2407 error = apply_unveil(got_repo_get_path(repo), 1,
2408 got_worktree_get_root_path(worktree));
2409 if (error)
2410 goto done;
2412 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2413 check_cancelled, NULL);
2414 done:
2415 TAILQ_FOREACH(pe, &paths, entry)
2416 free((char *)pe->path);
2417 got_pathlist_free(&paths);
2418 free(cwd);
2419 return error;
2422 __dead static void
2423 usage_ref(void)
2425 fprintf(stderr,
2426 "usage: %s ref [-r repository] -l | -d name | name target\n",
2427 getprogname());
2428 exit(1);
2431 static const struct got_error *
2432 list_refs(struct got_repository *repo)
2434 static const struct got_error *err = NULL;
2435 struct got_reflist_head refs;
2436 struct got_reflist_entry *re;
2438 SIMPLEQ_INIT(&refs);
2439 err = got_ref_list(&refs, repo);
2440 if (err)
2441 return err;
2443 SIMPLEQ_FOREACH(re, &refs, entry) {
2444 char *refstr;
2445 refstr = got_ref_to_str(re->ref);
2446 if (refstr == NULL)
2447 return got_error_from_errno("got_ref_to_str");
2448 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2449 free(refstr);
2452 got_ref_list_free(&refs);
2453 return NULL;
2456 static const struct got_error *
2457 delete_ref(struct got_repository *repo, const char *refname)
2459 const struct got_error *err = NULL;
2460 struct got_reference *ref;
2462 err = got_ref_open(&ref, repo, refname, 0);
2463 if (err)
2464 return err;
2466 err = got_ref_delete(ref, repo);
2467 got_ref_close(ref);
2468 return err;
2471 static const struct got_error *
2472 add_ref(struct got_repository *repo, const char *refname, const char *target)
2474 const struct got_error *err = NULL;
2475 struct got_object_id *id;
2476 struct got_reference *ref = NULL;
2479 * Don't let the user create a reference named '-'.
2480 * While technically a valid reference name, this case is usually
2481 * an unintended typo.
2483 if (refname[0] == '-' && refname[1] == '\0')
2484 return got_error(GOT_ERR_BAD_REF_NAME);
2486 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2487 repo);
2488 if (err) {
2489 struct got_reference *target_ref;
2491 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2492 return err;
2493 err = got_ref_open(&target_ref, repo, target, 0);
2494 if (err)
2495 return err;
2496 err = got_ref_resolve(&id, repo, target_ref);
2497 got_ref_close(target_ref);
2498 if (err)
2499 return err;
2502 err = got_ref_alloc(&ref, refname, id);
2503 if (err)
2504 goto done;
2506 err = got_ref_write(ref, repo);
2507 done:
2508 if (ref)
2509 got_ref_close(ref);
2510 free(id);
2511 return err;
2514 static const struct got_error *
2515 cmd_ref(int argc, char *argv[])
2517 const struct got_error *error = NULL;
2518 struct got_repository *repo = NULL;
2519 struct got_worktree *worktree = NULL;
2520 char *cwd = NULL, *repo_path = NULL;
2521 int ch, do_list = 0;
2522 const char *delref = NULL;
2524 /* TODO: Add -s option for adding symbolic references. */
2525 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2526 switch (ch) {
2527 case 'd':
2528 delref = optarg;
2529 break;
2530 case 'r':
2531 repo_path = realpath(optarg, NULL);
2532 if (repo_path == NULL)
2533 err(1, "-r option");
2534 got_path_strip_trailing_slashes(repo_path);
2535 break;
2536 case 'l':
2537 do_list = 1;
2538 break;
2539 default:
2540 usage_ref();
2541 /* NOTREACHED */
2545 if (do_list && delref)
2546 errx(1, "-l and -d options are mutually exclusive\n");
2548 argc -= optind;
2549 argv += optind;
2551 if (do_list || delref) {
2552 if (argc > 0)
2553 usage_ref();
2554 } else if (argc != 2)
2555 usage_ref();
2557 #ifndef PROFILE
2558 if (do_list) {
2559 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2560 NULL) == -1)
2561 err(1, "pledge");
2562 } else {
2563 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2564 "sendfd unveil", NULL) == -1)
2565 err(1, "pledge");
2567 #endif
2568 cwd = getcwd(NULL, 0);
2569 if (cwd == NULL) {
2570 error = got_error_from_errno("getcwd");
2571 goto done;
2574 if (repo_path == NULL) {
2575 error = got_worktree_open(&worktree, cwd);
2576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2577 goto done;
2578 else
2579 error = NULL;
2580 if (worktree) {
2581 repo_path =
2582 strdup(got_worktree_get_repo_path(worktree));
2583 if (repo_path == NULL)
2584 error = got_error_from_errno("strdup");
2585 if (error)
2586 goto done;
2587 } else {
2588 repo_path = strdup(cwd);
2589 if (repo_path == NULL) {
2590 error = got_error_from_errno("strdup");
2591 goto done;
2596 error = got_repo_open(&repo, repo_path);
2597 if (error != NULL)
2598 goto done;
2600 error = apply_unveil(got_repo_get_path(repo), do_list,
2601 worktree ? got_worktree_get_root_path(worktree) : NULL);
2602 if (error)
2603 goto done;
2605 if (do_list)
2606 error = list_refs(repo);
2607 else if (delref)
2608 error = delete_ref(repo, delref);
2609 else
2610 error = add_ref(repo, argv[0], argv[1]);
2611 done:
2612 if (repo)
2613 got_repo_close(repo);
2614 if (worktree)
2615 got_worktree_close(worktree);
2616 free(cwd);
2617 free(repo_path);
2618 return error;
2621 __dead static void
2622 usage_branch(void)
2624 fprintf(stderr,
2625 "usage: %s branch [-r repository] -l | -d name | "
2626 "name [base-branch]\n", getprogname());
2627 exit(1);
2630 static const struct got_error *
2631 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2633 static const struct got_error *err = NULL;
2634 struct got_reflist_head refs;
2635 struct got_reflist_entry *re;
2637 SIMPLEQ_INIT(&refs);
2639 err = got_ref_list(&refs, repo);
2640 if (err)
2641 return err;
2643 SIMPLEQ_FOREACH(re, &refs, entry) {
2644 const char *refname, *marker = " ";
2645 char *refstr;
2646 refname = got_ref_get_name(re->ref);
2647 if (strncmp(refname, "refs/heads/", 11) != 0)
2648 continue;
2649 if (worktree && strcmp(refname,
2650 got_worktree_get_head_ref_name(worktree)) == 0) {
2651 struct got_object_id *id = NULL;
2652 err = got_ref_resolve(&id, repo, re->ref);
2653 if (err)
2654 return err;
2655 if (got_object_id_cmp(id,
2656 got_worktree_get_base_commit_id(worktree)) == 0)
2657 marker = "* ";
2658 else
2659 marker = "~ ";
2660 free(id);
2662 refname += 11;
2663 refstr = got_ref_to_str(re->ref);
2664 if (refstr == NULL)
2665 return got_error_from_errno("got_ref_to_str");
2666 printf("%s%s: %s\n", marker, refname, refstr);
2667 free(refstr);
2670 got_ref_list_free(&refs);
2671 return NULL;
2674 static const struct got_error *
2675 delete_branch(struct got_repository *repo, const char *branch_name)
2677 const struct got_error *err = NULL;
2678 struct got_reference *ref;
2679 char *refname;
2681 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2682 return got_error_from_errno("asprintf");
2684 err = got_ref_open(&ref, repo, refname, 0);
2685 if (err)
2686 goto done;
2688 err = got_ref_delete(ref, repo);
2689 got_ref_close(ref);
2690 done:
2691 free(refname);
2692 return err;
2695 static const struct got_error *
2696 add_branch(struct got_repository *repo, const char *branch_name,
2697 const char *base_branch)
2699 const struct got_error *err = NULL;
2700 struct got_object_id *id = NULL;
2701 struct got_reference *ref = NULL;
2702 char *base_refname = NULL, *refname = NULL;
2703 struct got_reference *base_ref;
2706 * Don't let the user create a branch named '-'.
2707 * While technically a valid reference name, this case is usually
2708 * an unintended typo.
2710 if (branch_name[0] == '-' && branch_name[1] == '\0')
2711 return got_error(GOT_ERR_BAD_REF_NAME);
2713 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2714 base_refname = strdup(GOT_REF_HEAD);
2715 if (base_refname == NULL)
2716 return got_error_from_errno("strdup");
2717 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2718 return got_error_from_errno("asprintf");
2720 err = got_ref_open(&base_ref, repo, base_refname, 0);
2721 if (err)
2722 goto done;
2723 err = got_ref_resolve(&id, repo, base_ref);
2724 got_ref_close(base_ref);
2725 if (err)
2726 goto done;
2728 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2729 err = got_error_from_errno("asprintf");
2730 goto done;
2733 err = got_ref_open(&ref, repo, refname, 0);
2734 if (err == NULL) {
2735 err = got_error(GOT_ERR_BRANCH_EXISTS);
2736 goto done;
2737 } else if (err->code != GOT_ERR_NOT_REF)
2738 goto done;
2740 err = got_ref_alloc(&ref, refname, id);
2741 if (err)
2742 goto done;
2744 err = got_ref_write(ref, repo);
2745 done:
2746 if (ref)
2747 got_ref_close(ref);
2748 free(id);
2749 free(base_refname);
2750 free(refname);
2751 return err;
2754 static const struct got_error *
2755 cmd_branch(int argc, char *argv[])
2757 const struct got_error *error = NULL;
2758 struct got_repository *repo = NULL;
2759 struct got_worktree *worktree = NULL;
2760 char *cwd = NULL, *repo_path = NULL;
2761 int ch, do_list = 0;
2762 const char *delref = NULL;
2764 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2765 switch (ch) {
2766 case 'd':
2767 delref = optarg;
2768 break;
2769 case 'r':
2770 repo_path = realpath(optarg, NULL);
2771 if (repo_path == NULL)
2772 err(1, "-r option");
2773 got_path_strip_trailing_slashes(repo_path);
2774 break;
2775 case 'l':
2776 do_list = 1;
2777 break;
2778 default:
2779 usage_branch();
2780 /* NOTREACHED */
2784 if (do_list && delref)
2785 errx(1, "-l and -d options are mutually exclusive\n");
2787 argc -= optind;
2788 argv += optind;
2790 if (do_list || delref) {
2791 if (argc > 0)
2792 usage_branch();
2793 } else if (argc < 1 || argc > 2)
2794 usage_branch();
2796 #ifndef PROFILE
2797 if (do_list) {
2798 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2799 NULL) == -1)
2800 err(1, "pledge");
2801 } else {
2802 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2803 "sendfd unveil", NULL) == -1)
2804 err(1, "pledge");
2806 #endif
2807 cwd = getcwd(NULL, 0);
2808 if (cwd == NULL) {
2809 error = got_error_from_errno("getcwd");
2810 goto done;
2813 if (repo_path == NULL) {
2814 error = got_worktree_open(&worktree, cwd);
2815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2816 goto done;
2817 else
2818 error = NULL;
2819 if (worktree) {
2820 repo_path =
2821 strdup(got_worktree_get_repo_path(worktree));
2822 if (repo_path == NULL)
2823 error = got_error_from_errno("strdup");
2824 if (error)
2825 goto done;
2826 } else {
2827 repo_path = strdup(cwd);
2828 if (repo_path == NULL) {
2829 error = got_error_from_errno("strdup");
2830 goto done;
2835 error = got_repo_open(&repo, repo_path);
2836 if (error != NULL)
2837 goto done;
2839 error = apply_unveil(got_repo_get_path(repo), do_list,
2840 worktree ? got_worktree_get_root_path(worktree) : NULL);
2841 if (error)
2842 goto done;
2844 if (do_list)
2845 error = list_branches(repo, worktree);
2846 else if (delref)
2847 error = delete_branch(repo, delref);
2848 else {
2849 const char *base_branch;
2850 if (argc == 1) {
2851 base_branch = worktree ?
2852 got_worktree_get_head_ref_name(worktree) :
2853 GOT_REF_HEAD;
2854 } else
2855 base_branch = argv[1];
2856 error = add_branch(repo, argv[0], base_branch);
2858 done:
2859 if (repo)
2860 got_repo_close(repo);
2861 if (worktree)
2862 got_worktree_close(worktree);
2863 free(cwd);
2864 free(repo_path);
2865 return error;
2868 __dead static void
2869 usage_add(void)
2871 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2872 exit(1);
2875 static const struct got_error *
2876 cmd_add(int argc, char *argv[])
2878 const struct got_error *error = NULL;
2879 struct got_repository *repo = NULL;
2880 struct got_worktree *worktree = NULL;
2881 char *cwd = NULL;
2882 struct got_pathlist_head paths;
2883 struct got_pathlist_entry *pe;
2884 int ch, x;
2886 TAILQ_INIT(&paths);
2888 while ((ch = getopt(argc, argv, "")) != -1) {
2889 switch (ch) {
2890 default:
2891 usage_add();
2892 /* NOTREACHED */
2896 argc -= optind;
2897 argv += optind;
2899 #ifndef PROFILE
2900 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2901 NULL) == -1)
2902 err(1, "pledge");
2903 #endif
2904 if (argc < 1)
2905 usage_add();
2907 cwd = getcwd(NULL, 0);
2908 if (cwd == NULL) {
2909 error = got_error_from_errno("getcwd");
2910 goto done;
2913 error = got_worktree_open(&worktree, cwd);
2914 if (error)
2915 goto done;
2917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2918 if (error != NULL)
2919 goto done;
2921 error = apply_unveil(got_repo_get_path(repo), 1,
2922 got_worktree_get_root_path(worktree));
2923 if (error)
2924 goto done;
2926 for (x = 0; x < argc; x++) {
2927 char *path = realpath(argv[x], NULL);
2928 if (path == NULL) {
2929 error = got_error_from_errno2("realpath", argv[x]);
2930 goto done;
2933 got_path_strip_trailing_slashes(path);
2934 error = got_pathlist_insert(&pe, &paths, path, NULL);
2935 if (error) {
2936 free(path);
2937 goto done;
2940 error = got_worktree_schedule_add(worktree, &paths, print_status,
2941 NULL, repo);
2942 done:
2943 if (repo)
2944 got_repo_close(repo);
2945 if (worktree)
2946 got_worktree_close(worktree);
2947 TAILQ_FOREACH(pe, &paths, entry)
2948 free((char *)pe->path);
2949 got_pathlist_free(&paths);
2950 free(cwd);
2951 return error;
2954 __dead static void
2955 usage_remove(void)
2957 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2958 exit(1);
2961 static const struct got_error *
2962 cmd_remove(int argc, char *argv[])
2964 const struct got_error *error = NULL;
2965 struct got_worktree *worktree = NULL;
2966 struct got_repository *repo = NULL;
2967 char *cwd = NULL;
2968 struct got_pathlist_head paths;
2969 struct got_pathlist_entry *pe;
2970 int ch, i, delete_local_mods = 0;
2972 TAILQ_INIT(&paths);
2974 while ((ch = getopt(argc, argv, "f")) != -1) {
2975 switch (ch) {
2976 case 'f':
2977 delete_local_mods = 1;
2978 break;
2979 default:
2980 usage_add();
2981 /* NOTREACHED */
2985 argc -= optind;
2986 argv += optind;
2988 #ifndef PROFILE
2989 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2990 NULL) == -1)
2991 err(1, "pledge");
2992 #endif
2993 if (argc < 1)
2994 usage_remove();
2996 cwd = getcwd(NULL, 0);
2997 if (cwd == NULL) {
2998 error = got_error_from_errno("getcwd");
2999 goto done;
3001 error = got_worktree_open(&worktree, cwd);
3002 if (error)
3003 goto done;
3005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3006 if (error)
3007 goto done;
3009 error = apply_unveil(got_repo_get_path(repo), 1,
3010 got_worktree_get_root_path(worktree));
3011 if (error)
3012 goto done;
3014 for (i = 0; i < argc; i++) {
3015 char *path = realpath(argv[i], NULL);
3016 if (path == NULL) {
3017 error = got_error_from_errno2("realpath", argv[i]);
3018 goto done;
3021 got_path_strip_trailing_slashes(path);
3022 error = got_pathlist_insert(&pe, &paths, path, NULL);
3023 if (error) {
3024 free(path);
3025 goto done;
3028 error = got_worktree_schedule_delete(worktree, &paths,
3029 delete_local_mods, print_status, NULL, repo);
3030 if (error)
3031 goto done;
3032 done:
3033 if (repo)
3034 got_repo_close(repo);
3035 if (worktree)
3036 got_worktree_close(worktree);
3037 TAILQ_FOREACH(pe, &paths, entry)
3038 free((char *)pe->path);
3039 got_pathlist_free(&paths);
3040 free(cwd);
3041 return error;
3044 __dead static void
3045 usage_revert(void)
3047 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3048 exit(1);
3051 static const struct got_error *
3052 revert_progress(void *arg, unsigned char status, const char *path)
3054 while (path[0] == '/')
3055 path++;
3056 printf("%c %s\n", status, path);
3057 return NULL;
3060 static const struct got_error *
3061 cmd_revert(int argc, char *argv[])
3063 const struct got_error *error = NULL;
3064 struct got_worktree *worktree = NULL;
3065 struct got_repository *repo = NULL;
3066 char *cwd = NULL, *path = NULL;
3067 struct got_pathlist_head paths;
3068 struct got_pathlist_entry *pe;
3069 int ch, i;
3071 TAILQ_INIT(&paths);
3073 while ((ch = getopt(argc, argv, "")) != -1) {
3074 switch (ch) {
3075 default:
3076 usage_revert();
3077 /* NOTREACHED */
3081 argc -= optind;
3082 argv += optind;
3084 #ifndef PROFILE
3085 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3086 "unveil", NULL) == -1)
3087 err(1, "pledge");
3088 #endif
3089 if (argc < 1)
3090 usage_revert();
3092 for (i = 0; i < argc; i++) {
3093 char *path = realpath(argv[i], NULL);
3094 if (path == NULL) {
3095 error = got_error_from_errno2("realpath", argv[i]);
3096 goto done;
3099 got_path_strip_trailing_slashes(path);
3100 error = got_pathlist_insert(&pe, &paths, path, NULL);
3101 if (error) {
3102 free(path);
3103 goto done;
3107 cwd = getcwd(NULL, 0);
3108 if (cwd == NULL) {
3109 error = got_error_from_errno("getcwd");
3110 goto done;
3112 error = got_worktree_open(&worktree, cwd);
3113 if (error)
3114 goto done;
3116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3117 if (error != NULL)
3118 goto done;
3120 error = apply_unveil(got_repo_get_path(repo), 1,
3121 got_worktree_get_root_path(worktree));
3122 if (error)
3123 goto done;
3125 error = got_worktree_revert(worktree, &paths,
3126 revert_progress, NULL, repo);
3127 if (error)
3128 goto done;
3129 done:
3130 if (repo)
3131 got_repo_close(repo);
3132 if (worktree)
3133 got_worktree_close(worktree);
3134 free(path);
3135 free(cwd);
3136 return error;
3139 __dead static void
3140 usage_commit(void)
3142 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3143 exit(1);
3146 struct collect_commit_logmsg_arg {
3147 const char *cmdline_log;
3148 const char *editor;
3149 const char *worktree_path;
3150 const char *branch_name;
3151 const char *repo_path;
3152 char *logmsg_path;
3156 static const struct got_error *
3157 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3158 void *arg)
3160 char *initial_content = NULL;
3161 struct got_pathlist_entry *pe;
3162 const struct got_error *err = NULL;
3163 char *template = NULL;
3164 struct collect_commit_logmsg_arg *a = arg;
3165 int fd;
3166 size_t len;
3168 /* if a message was specified on the command line, just use it */
3169 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3170 len = strlen(a->cmdline_log) + 1;
3171 *logmsg = malloc(len + 1);
3172 if (*logmsg == NULL)
3173 return got_error_from_errno("malloc");
3174 strlcpy(*logmsg, a->cmdline_log, len);
3175 return NULL;
3178 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3179 return got_error_from_errno("asprintf");
3181 if (asprintf(&initial_content,
3182 "\n# changes to be committed on branch %s:\n",
3183 a->branch_name) == -1)
3184 return got_error_from_errno("asprintf");
3186 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3187 if (err)
3188 goto done;
3190 dprintf(fd, initial_content);
3192 TAILQ_FOREACH(pe, commitable_paths, entry) {
3193 struct got_commitable *ct = pe->data;
3194 dprintf(fd, "# %c %s\n",
3195 got_commitable_get_status(ct),
3196 got_commitable_get_path(ct));
3198 close(fd);
3200 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3201 done:
3202 unlink(a->logmsg_path);
3203 free(a->logmsg_path);
3204 free(initial_content);
3205 free(template);
3207 /* Editor is done; we can now apply unveil(2) */
3208 if (err == NULL) {
3209 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3210 if (err) {
3211 free(*logmsg);
3212 *logmsg = NULL;
3215 return err;
3218 static const struct got_error *
3219 cmd_commit(int argc, char *argv[])
3221 const struct got_error *error = NULL;
3222 struct got_worktree *worktree = NULL;
3223 struct got_repository *repo = NULL;
3224 char *cwd = NULL, *path = NULL, *id_str = NULL;
3225 struct got_object_id *id = NULL;
3226 const char *logmsg = NULL;
3227 const char *got_author = getenv("GOT_AUTHOR");
3228 struct collect_commit_logmsg_arg cl_arg;
3229 char *editor = NULL;
3230 int ch, rebase_in_progress;
3232 while ((ch = getopt(argc, argv, "m:")) != -1) {
3233 switch (ch) {
3234 case 'm':
3235 logmsg = optarg;
3236 break;
3237 default:
3238 usage_commit();
3239 /* NOTREACHED */
3243 argc -= optind;
3244 argv += optind;
3246 #ifndef PROFILE
3247 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3248 "unveil", NULL) == -1)
3249 err(1, "pledge");
3250 #endif
3251 if (argc == 1) {
3252 path = realpath(argv[0], NULL);
3253 if (path == NULL) {
3254 error = got_error_from_errno2("realpath", argv[0]);
3255 goto done;
3257 got_path_strip_trailing_slashes(path);
3258 } else if (argc != 0)
3259 usage_commit();
3261 if (got_author == NULL) {
3262 /* TODO: Look current user up in password database */
3263 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3264 goto done;
3267 cwd = getcwd(NULL, 0);
3268 if (cwd == NULL) {
3269 error = got_error_from_errno("getcwd");
3270 goto done;
3272 error = got_worktree_open(&worktree, cwd);
3273 if (error)
3274 goto done;
3276 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3277 if (error)
3278 goto done;
3279 if (rebase_in_progress) {
3280 error = got_error(GOT_ERR_REBASING);
3281 goto done;
3284 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3285 if (error != NULL)
3286 goto done;
3289 * unveil(2) traverses exec(2); if an editor is used we have
3290 * to apply unveil after the log message has been written.
3292 if (logmsg == NULL || strlen(logmsg) == 0)
3293 error = get_editor(&editor);
3294 else
3295 error = apply_unveil(got_repo_get_path(repo), 0,
3296 got_worktree_get_root_path(worktree));
3297 if (error)
3298 goto done;
3300 cl_arg.editor = editor;
3301 cl_arg.cmdline_log = logmsg;
3302 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3303 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3304 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3305 cl_arg.branch_name += 5;
3306 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3307 cl_arg.branch_name += 6;
3308 cl_arg.repo_path = got_repo_get_path(repo);
3309 cl_arg.logmsg_path = NULL;
3310 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3311 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3312 if (error) {
3313 if (cl_arg.logmsg_path)
3314 fprintf(stderr, "%s: log message preserved in %s\n",
3315 getprogname(), cl_arg.logmsg_path);
3316 goto done;
3319 if (cl_arg.logmsg_path)
3320 unlink(cl_arg.logmsg_path);
3322 error = got_object_id_str(&id_str, id);
3323 if (error)
3324 goto done;
3325 printf("Created commit %s\n", id_str);
3326 done:
3327 if (repo)
3328 got_repo_close(repo);
3329 if (worktree)
3330 got_worktree_close(worktree);
3331 free(path);
3332 free(cwd);
3333 free(id_str);
3334 free(editor);
3335 return error;
3338 __dead static void
3339 usage_cherrypick(void)
3341 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3342 exit(1);
3345 static const struct got_error *
3346 cmd_cherrypick(int argc, char *argv[])
3348 const struct got_error *error = NULL;
3349 struct got_worktree *worktree = NULL;
3350 struct got_repository *repo = NULL;
3351 char *cwd = NULL, *commit_id_str = NULL;
3352 struct got_object_id *commit_id = NULL;
3353 struct got_commit_object *commit = NULL;
3354 struct got_object_qid *pid;
3355 struct got_reference *head_ref = NULL;
3356 int ch, did_something = 0;
3358 while ((ch = getopt(argc, argv, "")) != -1) {
3359 switch (ch) {
3360 default:
3361 usage_cherrypick();
3362 /* NOTREACHED */
3366 argc -= optind;
3367 argv += optind;
3369 #ifndef PROFILE
3370 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3371 "unveil", NULL) == -1)
3372 err(1, "pledge");
3373 #endif
3374 if (argc != 1)
3375 usage_cherrypick();
3377 cwd = getcwd(NULL, 0);
3378 if (cwd == NULL) {
3379 error = got_error_from_errno("getcwd");
3380 goto done;
3382 error = got_worktree_open(&worktree, cwd);
3383 if (error)
3384 goto done;
3386 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3387 if (error != NULL)
3388 goto done;
3390 error = apply_unveil(got_repo_get_path(repo), 0,
3391 got_worktree_get_root_path(worktree));
3392 if (error)
3393 goto done;
3395 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3396 GOT_OBJ_TYPE_COMMIT, repo);
3397 if (error != NULL) {
3398 struct got_reference *ref;
3399 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3400 goto done;
3401 error = got_ref_open(&ref, repo, argv[0], 0);
3402 if (error != NULL)
3403 goto done;
3404 error = got_ref_resolve(&commit_id, repo, ref);
3405 got_ref_close(ref);
3406 if (error != NULL)
3407 goto done;
3409 error = got_object_id_str(&commit_id_str, commit_id);
3410 if (error)
3411 goto done;
3413 error = got_ref_open(&head_ref, repo,
3414 got_worktree_get_head_ref_name(worktree), 0);
3415 if (error != NULL)
3416 goto done;
3418 error = check_same_branch(commit_id, head_ref, NULL, repo);
3419 if (error) {
3420 if (error->code != GOT_ERR_ANCESTRY)
3421 goto done;
3422 error = NULL;
3423 } else {
3424 error = got_error(GOT_ERR_SAME_BRANCH);
3425 goto done;
3428 error = got_object_open_as_commit(&commit, repo, commit_id);
3429 if (error)
3430 goto done;
3431 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3432 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3433 commit_id, repo, update_progress, &did_something, check_cancelled,
3434 NULL);
3435 if (error != NULL)
3436 goto done;
3438 if (did_something)
3439 printf("Merged commit %s\n", commit_id_str);
3440 done:
3441 if (commit)
3442 got_object_commit_close(commit);
3443 free(commit_id_str);
3444 if (head_ref)
3445 got_ref_close(head_ref);
3446 if (worktree)
3447 got_worktree_close(worktree);
3448 if (repo)
3449 got_repo_close(repo);
3450 return error;
3453 __dead static void
3454 usage_backout(void)
3456 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3457 exit(1);
3460 static const struct got_error *
3461 cmd_backout(int argc, char *argv[])
3463 const struct got_error *error = NULL;
3464 struct got_worktree *worktree = NULL;
3465 struct got_repository *repo = NULL;
3466 char *cwd = NULL, *commit_id_str = NULL;
3467 struct got_object_id *commit_id = NULL;
3468 struct got_commit_object *commit = NULL;
3469 struct got_object_qid *pid;
3470 struct got_reference *head_ref = NULL;
3471 int ch, did_something = 0;
3473 while ((ch = getopt(argc, argv, "")) != -1) {
3474 switch (ch) {
3475 default:
3476 usage_backout();
3477 /* NOTREACHED */
3481 argc -= optind;
3482 argv += optind;
3484 #ifndef PROFILE
3485 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3486 "unveil", NULL) == -1)
3487 err(1, "pledge");
3488 #endif
3489 if (argc != 1)
3490 usage_backout();
3492 cwd = getcwd(NULL, 0);
3493 if (cwd == NULL) {
3494 error = got_error_from_errno("getcwd");
3495 goto done;
3497 error = got_worktree_open(&worktree, cwd);
3498 if (error)
3499 goto done;
3501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3502 if (error != NULL)
3503 goto done;
3505 error = apply_unveil(got_repo_get_path(repo), 0,
3506 got_worktree_get_root_path(worktree));
3507 if (error)
3508 goto done;
3510 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3511 GOT_OBJ_TYPE_COMMIT, repo);
3512 if (error != NULL) {
3513 struct got_reference *ref;
3514 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3515 goto done;
3516 error = got_ref_open(&ref, repo, argv[0], 0);
3517 if (error != NULL)
3518 goto done;
3519 error = got_ref_resolve(&commit_id, repo, ref);
3520 got_ref_close(ref);
3521 if (error != NULL)
3522 goto done;
3524 error = got_object_id_str(&commit_id_str, commit_id);
3525 if (error)
3526 goto done;
3528 error = got_ref_open(&head_ref, repo,
3529 got_worktree_get_head_ref_name(worktree), 0);
3530 if (error != NULL)
3531 goto done;
3533 error = check_same_branch(commit_id, head_ref, NULL, repo);
3534 if (error)
3535 goto done;
3537 error = got_object_open_as_commit(&commit, repo, commit_id);
3538 if (error)
3539 goto done;
3540 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3541 if (pid == NULL) {
3542 error = got_error(GOT_ERR_ROOT_COMMIT);
3543 goto done;
3546 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3547 update_progress, &did_something, check_cancelled, NULL);
3548 if (error != NULL)
3549 goto done;
3551 if (did_something)
3552 printf("Backed out commit %s\n", commit_id_str);
3553 done:
3554 if (commit)
3555 got_object_commit_close(commit);
3556 free(commit_id_str);
3557 if (head_ref)
3558 got_ref_close(head_ref);
3559 if (worktree)
3560 got_worktree_close(worktree);
3561 if (repo)
3562 got_repo_close(repo);
3563 return error;
3566 __dead static void
3567 usage_rebase(void)
3569 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3570 getprogname());
3571 exit(1);
3574 void
3575 trim_logmsg(char *logmsg, int limit)
3577 char *nl;
3578 size_t len;
3580 len = strlen(logmsg);
3581 if (len > limit)
3582 len = limit;
3583 logmsg[len] = '\0';
3584 nl = strchr(logmsg, '\n');
3585 if (nl)
3586 *nl = '\0';
3589 static const struct got_error *
3590 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3592 const char *logmsg0 = NULL;
3594 logmsg0 = got_object_commit_get_logmsg(commit);
3596 while (isspace((unsigned char)logmsg0[0]))
3597 logmsg0++;
3599 *logmsg = strdup(logmsg0);
3600 if (*logmsg == NULL)
3601 return got_error_from_errno("strdup");
3603 trim_logmsg(*logmsg, limit);
3604 return NULL;
3607 static const struct got_error *
3608 show_rebase_progress(struct got_commit_object *commit,
3609 struct got_object_id *old_id, struct got_object_id *new_id)
3611 const struct got_error *err;
3612 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3614 err = got_object_id_str(&old_id_str, old_id);
3615 if (err)
3616 goto done;
3618 if (new_id) {
3619 err = got_object_id_str(&new_id_str, new_id);
3620 if (err)
3621 goto done;
3624 old_id_str[12] = '\0';
3625 if (new_id_str)
3626 new_id_str[12] = '\0';
3628 err = get_short_logmsg(&logmsg, 42, commit);
3629 if (err)
3630 goto done;
3632 printf("%s -> %s: %s\n", old_id_str,
3633 new_id_str ? new_id_str : "no-op change", logmsg);
3634 done:
3635 free(old_id_str);
3636 free(new_id_str);
3637 return err;
3640 static const struct got_error *
3641 rebase_progress(void *arg, unsigned char status, const char *path)
3643 unsigned char *rebase_status = arg;
3645 while (path[0] == '/')
3646 path++;
3647 printf("%c %s\n", status, path);
3649 if (*rebase_status == GOT_STATUS_CONFLICT)
3650 return NULL;
3651 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3652 *rebase_status = status;
3653 return NULL;
3656 static const struct got_error *
3657 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3658 struct got_reference *branch, struct got_reference *new_base_branch,
3659 struct got_reference *tmp_branch, struct got_repository *repo)
3661 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3662 return got_worktree_rebase_complete(worktree, fileindex,
3663 new_base_branch, tmp_branch, branch, repo);
3666 static const struct got_error *
3667 rebase_commit(struct got_pathlist_head *merged_paths,
3668 struct got_worktree *worktree, struct got_fileindex *fileindex,
3669 struct got_reference *tmp_branch,
3670 struct got_object_id *commit_id, struct got_repository *repo)
3672 const struct got_error *error;
3673 struct got_commit_object *commit;
3674 struct got_object_id *new_commit_id;
3676 error = got_object_open_as_commit(&commit, repo, commit_id);
3677 if (error)
3678 return error;
3680 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3681 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3682 if (error) {
3683 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3684 goto done;
3685 error = show_rebase_progress(commit, commit_id, NULL);
3686 } else {
3687 error = show_rebase_progress(commit, commit_id, new_commit_id);
3688 free(new_commit_id);
3690 done:
3691 got_object_commit_close(commit);
3692 return error;
3695 struct check_path_prefix_arg {
3696 const char *path_prefix;
3697 size_t len;
3698 int errcode;
3701 static const struct got_error *
3702 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3703 struct got_blob_object *blob2, struct got_object_id *id1,
3704 struct got_object_id *id2, const char *path1, const char *path2,
3705 struct got_repository *repo)
3707 struct check_path_prefix_arg *a = arg;
3709 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3710 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3711 return got_error(a->errcode);
3713 return NULL;
3716 static const struct got_error *
3717 check_path_prefix(struct got_object_id *parent_id,
3718 struct got_object_id *commit_id, const char *path_prefix,
3719 int errcode, struct got_repository *repo)
3721 const struct got_error *err;
3722 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3723 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3724 struct check_path_prefix_arg cpp_arg;
3726 if (got_path_is_root_dir(path_prefix))
3727 return NULL;
3729 err = got_object_open_as_commit(&commit, repo, commit_id);
3730 if (err)
3731 goto done;
3733 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3734 if (err)
3735 goto done;
3737 err = got_object_open_as_tree(&tree1, repo,
3738 got_object_commit_get_tree_id(parent_commit));
3739 if (err)
3740 goto done;
3742 err = got_object_open_as_tree(&tree2, repo,
3743 got_object_commit_get_tree_id(commit));
3744 if (err)
3745 goto done;
3747 cpp_arg.path_prefix = path_prefix;
3748 while (cpp_arg.path_prefix[0] == '/')
3749 cpp_arg.path_prefix++;
3750 cpp_arg.len = strlen(cpp_arg.path_prefix);
3751 cpp_arg.errcode = errcode;
3752 err = got_diff_tree(tree1, tree2, "", "", repo,
3753 check_path_prefix_in_diff, &cpp_arg, 0);
3754 done:
3755 if (tree1)
3756 got_object_tree_close(tree1);
3757 if (tree2)
3758 got_object_tree_close(tree2);
3759 if (commit)
3760 got_object_commit_close(commit);
3761 if (parent_commit)
3762 got_object_commit_close(parent_commit);
3763 return err;
3766 static const struct got_error *
3767 collect_commits(struct got_object_id_queue *commits,
3768 struct got_object_id *initial_commit_id,
3769 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3770 const char *path_prefix, int path_prefix_errcode,
3771 struct got_repository *repo)
3773 const struct got_error *err = NULL;
3774 struct got_commit_graph *graph = NULL;
3775 struct got_object_id *parent_id = NULL;
3776 struct got_object_qid *qid;
3777 struct got_object_id *commit_id = initial_commit_id;
3779 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3780 if (err)
3781 return err;
3783 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3784 if (err)
3785 goto done;
3786 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3787 err = got_commit_graph_iter_next(&parent_id, graph);
3788 if (err) {
3789 if (err->code == GOT_ERR_ITER_COMPLETED) {
3790 err = got_error_msg(GOT_ERR_ANCESTRY,
3791 "ran out of commits to rebase before "
3792 "youngest common ancestor commit has "
3793 "been reached?!?");
3794 goto done;
3795 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3796 goto done;
3797 err = got_commit_graph_fetch_commits(graph, 1, repo);
3798 if (err)
3799 goto done;
3800 } else {
3801 err = check_path_prefix(parent_id, commit_id,
3802 path_prefix, path_prefix_errcode, repo);
3803 if (err)
3804 goto done;
3806 err = got_object_qid_alloc(&qid, commit_id);
3807 if (err)
3808 goto done;
3809 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3810 commit_id = parent_id;
3813 done:
3814 got_commit_graph_close(graph);
3815 return err;
3818 static const struct got_error *
3819 cmd_rebase(int argc, char *argv[])
3821 const struct got_error *error = NULL;
3822 struct got_worktree *worktree = NULL;
3823 struct got_repository *repo = NULL;
3824 struct got_fileindex *fileindex = NULL;
3825 char *cwd = NULL;
3826 struct got_reference *branch = NULL;
3827 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3828 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3829 struct got_object_id *resume_commit_id = NULL;
3830 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3831 struct got_commit_object *commit = NULL;
3832 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3833 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3834 struct got_object_id_queue commits;
3835 struct got_pathlist_head merged_paths;
3836 const struct got_object_id_queue *parent_ids;
3837 struct got_object_qid *qid, *pid;
3839 SIMPLEQ_INIT(&commits);
3840 TAILQ_INIT(&merged_paths);
3842 while ((ch = getopt(argc, argv, "ac")) != -1) {
3843 switch (ch) {
3844 case 'a':
3845 abort_rebase = 1;
3846 break;
3847 case 'c':
3848 continue_rebase = 1;
3849 break;
3850 default:
3851 usage_rebase();
3852 /* NOTREACHED */
3856 argc -= optind;
3857 argv += optind;
3859 #ifndef PROFILE
3860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3861 "unveil", NULL) == -1)
3862 err(1, "pledge");
3863 #endif
3864 if (abort_rebase && continue_rebase)
3865 usage_rebase();
3866 else if (abort_rebase || continue_rebase) {
3867 if (argc != 0)
3868 usage_rebase();
3869 } else if (argc != 1)
3870 usage_rebase();
3872 cwd = getcwd(NULL, 0);
3873 if (cwd == NULL) {
3874 error = got_error_from_errno("getcwd");
3875 goto done;
3877 error = got_worktree_open(&worktree, cwd);
3878 if (error)
3879 goto done;
3881 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3882 if (error != NULL)
3883 goto done;
3885 error = apply_unveil(got_repo_get_path(repo), 0,
3886 got_worktree_get_root_path(worktree));
3887 if (error)
3888 goto done;
3890 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3891 if (error)
3892 goto done;
3894 if (abort_rebase) {
3895 int did_something;
3896 if (!rebase_in_progress) {
3897 error = got_error(GOT_ERR_NOT_REBASING);
3898 goto done;
3900 error = got_worktree_rebase_continue(&resume_commit_id,
3901 &new_base_branch, &tmp_branch, &branch, &fileindex,
3902 worktree, repo);
3903 if (error)
3904 goto done;
3905 printf("Switching work tree to %s\n",
3906 got_ref_get_symref_target(new_base_branch));
3907 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3908 new_base_branch, update_progress, &did_something);
3909 if (error)
3910 goto done;
3911 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3912 goto done; /* nothing else to do */
3915 if (continue_rebase) {
3916 if (!rebase_in_progress) {
3917 error = got_error(GOT_ERR_NOT_REBASING);
3918 goto done;
3920 error = got_worktree_rebase_continue(&resume_commit_id,
3921 &new_base_branch, &tmp_branch, &branch, &fileindex,
3922 worktree, repo);
3923 if (error)
3924 goto done;
3926 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3927 resume_commit_id, repo);
3928 if (error)
3929 goto done;
3931 yca_id = got_object_id_dup(resume_commit_id);
3932 if (yca_id == NULL) {
3933 error = got_error_from_errno("got_object_id_dup");
3934 goto done;
3936 } else {
3937 error = got_ref_open(&branch, repo, argv[0], 0);
3938 if (error != NULL)
3939 goto done;
3942 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3943 if (error)
3944 goto done;
3946 if (!continue_rebase) {
3947 struct got_object_id *base_commit_id;
3949 base_commit_id = got_worktree_get_base_commit_id(worktree);
3950 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3951 base_commit_id, branch_head_commit_id, repo);
3952 if (error)
3953 goto done;
3954 if (yca_id == NULL) {
3955 error = got_error_msg(GOT_ERR_ANCESTRY,
3956 "specified branch shares no common ancestry "
3957 "with work tree's branch");
3958 goto done;
3961 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3962 if (error) {
3963 if (error->code != GOT_ERR_ANCESTRY)
3964 goto done;
3965 error = NULL;
3966 } else {
3967 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3968 "specified branch resolves to a commit which "
3969 "is already contained in work tree's branch");
3970 goto done;
3972 error = got_worktree_rebase_prepare(&new_base_branch,
3973 &tmp_branch, &fileindex, worktree, branch, repo);
3974 if (error)
3975 goto done;
3978 commit_id = branch_head_commit_id;
3979 error = got_object_open_as_commit(&commit, repo, commit_id);
3980 if (error)
3981 goto done;
3983 parent_ids = got_object_commit_get_parent_ids(commit);
3984 pid = SIMPLEQ_FIRST(parent_ids);
3985 error = collect_commits(&commits, commit_id, pid->id,
3986 yca_id, got_worktree_get_path_prefix(worktree),
3987 GOT_ERR_REBASE_PATH, repo);
3988 got_object_commit_close(commit);
3989 commit = NULL;
3990 if (error)
3991 goto done;
3993 if (SIMPLEQ_EMPTY(&commits)) {
3994 if (continue_rebase)
3995 error = rebase_complete(worktree, fileindex,
3996 branch, new_base_branch, tmp_branch, repo);
3997 else
3998 error = got_error(GOT_ERR_EMPTY_REBASE);
3999 goto done;
4002 pid = NULL;
4003 SIMPLEQ_FOREACH(qid, &commits, entry) {
4004 commit_id = qid->id;
4005 parent_id = pid ? pid->id : yca_id;
4006 pid = qid;
4008 error = got_worktree_rebase_merge_files(&merged_paths,
4009 worktree, fileindex, parent_id, commit_id, repo,
4010 rebase_progress, &rebase_status, check_cancelled, NULL);
4011 if (error)
4012 goto done;
4014 if (rebase_status == GOT_STATUS_CONFLICT) {
4015 got_worktree_rebase_pathlist_free(&merged_paths);
4016 break;
4019 error = rebase_commit(&merged_paths, worktree, fileindex,
4020 tmp_branch, commit_id, repo);
4021 got_worktree_rebase_pathlist_free(&merged_paths);
4022 if (error)
4023 goto done;
4026 if (rebase_status == GOT_STATUS_CONFLICT) {
4027 error = got_worktree_rebase_postpone(worktree, fileindex);
4028 if (error)
4029 goto done;
4030 error = got_error_msg(GOT_ERR_CONFLICTS,
4031 "conflicts must be resolved before rebasing can continue");
4032 } else
4033 error = rebase_complete(worktree, fileindex, branch,
4034 new_base_branch, tmp_branch, repo);
4035 done:
4036 got_object_id_queue_free(&commits);
4037 free(branch_head_commit_id);
4038 free(resume_commit_id);
4039 free(yca_id);
4040 if (commit)
4041 got_object_commit_close(commit);
4042 if (branch)
4043 got_ref_close(branch);
4044 if (new_base_branch)
4045 got_ref_close(new_base_branch);
4046 if (tmp_branch)
4047 got_ref_close(tmp_branch);
4048 if (worktree)
4049 got_worktree_close(worktree);
4050 if (repo)
4051 got_repo_close(repo);
4052 return error;
4055 __dead static void
4056 usage_histedit(void)
4058 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4059 getprogname());
4060 exit(1);
4063 #define GOT_HISTEDIT_PICK 'p'
4064 #define GOT_HISTEDIT_EDIT 'e'
4065 #define GOT_HISTEDIT_FOLD 'f'
4066 #define GOT_HISTEDIT_DROP 'd'
4067 #define GOT_HISTEDIT_MESG 'm'
4069 static struct got_histedit_cmd {
4070 unsigned char code;
4071 const char *name;
4072 const char *desc;
4073 } got_histedit_cmds[] = {
4074 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4075 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4076 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4077 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4078 { GOT_HISTEDIT_MESG, "mesg",
4079 "single-line log message for commit above (open editor if empty)" },
4082 struct got_histedit_list_entry {
4083 TAILQ_ENTRY(got_histedit_list_entry) entry;
4084 struct got_object_id *commit_id;
4085 const struct got_histedit_cmd *cmd;
4086 char *logmsg;
4088 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4090 static const struct got_error *
4091 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4092 FILE *f, struct got_repository *repo)
4094 const struct got_error *err = NULL;
4095 char *logmsg = NULL, *id_str = NULL;
4096 struct got_commit_object *commit = NULL;
4097 size_t n;
4099 err = got_object_open_as_commit(&commit, repo, commit_id);
4100 if (err)
4101 goto done;
4103 err = get_short_logmsg(&logmsg, 34, commit);
4104 if (err)
4105 goto done;
4107 err = got_object_id_str(&id_str, commit_id);
4108 if (err)
4109 goto done;
4111 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4112 if (n < 0)
4113 err = got_ferror(f, GOT_ERR_IO);
4114 done:
4115 if (commit)
4116 got_object_commit_close(commit);
4117 free(id_str);
4118 free(logmsg);
4119 return err;
4122 static const struct got_error *
4123 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4124 struct got_repository *repo)
4126 const struct got_error *err = NULL;
4127 struct got_object_qid *qid;
4129 if (SIMPLEQ_EMPTY(commits))
4130 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4132 SIMPLEQ_FOREACH(qid, commits, entry) {
4133 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4134 f, repo);
4135 if (err)
4136 break;
4139 return err;
4142 static const struct got_error *
4143 write_cmd_list(FILE *f)
4145 const struct got_error *err = NULL;
4146 int n, i;
4148 n = fprintf(f, "# Available histedit commands:\n");
4149 if (n < 0)
4150 return got_ferror(f, GOT_ERR_IO);
4152 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4153 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4154 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4155 cmd->desc);
4156 if (n < 0) {
4157 err = got_ferror(f, GOT_ERR_IO);
4158 break;
4161 n = fprintf(f, "# Commits will be processed in order from top to "
4162 "bottom of this file.\n");
4163 if (n < 0)
4164 return got_ferror(f, GOT_ERR_IO);
4165 return err;
4168 static const struct got_error *
4169 histedit_syntax_error(int lineno)
4171 static char msg[42];
4172 int ret;
4174 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4175 lineno);
4176 if (ret == -1 || ret >= sizeof(msg))
4177 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4179 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4182 static const struct got_error *
4183 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4184 char *logmsg, struct got_repository *repo)
4186 const struct got_error *err;
4187 struct got_commit_object *folded_commit = NULL;
4188 char *id_str;
4190 err = got_object_id_str(&id_str, hle->commit_id);
4191 if (err)
4192 return err;
4194 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4195 if (err)
4196 goto done;
4198 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4199 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4200 got_object_commit_get_logmsg(folded_commit)) == -1) {
4201 err = got_error_from_errno("asprintf");
4202 goto done;
4204 done:
4205 if (folded_commit)
4206 got_object_commit_close(folded_commit);
4207 free(id_str);
4208 return err;
4211 static struct got_histedit_list_entry *
4212 get_folded_commits(struct got_histedit_list_entry *hle)
4214 struct got_histedit_list_entry *prev, *folded = NULL;
4216 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4217 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4218 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4219 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4220 folded = prev;
4221 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4224 return folded;
4227 static const struct got_error *
4228 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4229 struct got_repository *repo)
4231 char *logmsg_path = NULL, *id_str = NULL;
4232 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4233 const struct got_error *err = NULL;
4234 struct got_commit_object *commit = NULL;
4235 int fd;
4236 struct got_histedit_list_entry *folded = NULL;
4238 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4239 if (err)
4240 return err;
4242 folded = get_folded_commits(hle);
4243 if (folded) {
4244 while (folded != hle) {
4245 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4246 folded = TAILQ_NEXT(folded, entry);
4247 continue;
4249 err = append_folded_commit_msg(&new_msg, folded,
4250 logmsg, repo);
4251 if (err)
4252 goto done;
4253 free(logmsg);
4254 logmsg = new_msg;
4255 folded = TAILQ_NEXT(folded, entry);
4259 err = got_object_id_str(&id_str, hle->commit_id);
4260 if (err)
4261 goto done;
4262 if (asprintf(&new_msg,
4263 "%s\n# original log message of commit %s: %s",
4264 logmsg ? logmsg : "", id_str,
4265 got_object_commit_get_logmsg(commit)) == -1) {
4266 err = got_error_from_errno("asprintf");
4267 goto done;
4269 free(logmsg);
4270 logmsg = new_msg;
4272 err = got_object_id_str(&id_str, hle->commit_id);
4273 if (err)
4274 goto done;
4276 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4277 if (err)
4278 goto done;
4280 dprintf(fd, logmsg);
4281 close(fd);
4283 err = get_editor(&editor);
4284 if (err)
4285 goto done;
4287 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4288 if (err) {
4289 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4290 goto done;
4291 err = NULL;
4292 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4293 if (hle->logmsg == NULL)
4294 err = got_error_from_errno("strdup");
4296 done:
4297 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4298 err = got_error_from_errno2("unlink", logmsg_path);
4299 free(logmsg_path);
4300 free(logmsg);
4301 free(editor);
4302 if (commit)
4303 got_object_commit_close(commit);
4304 return err;
4307 static const struct got_error *
4308 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4309 FILE *f, struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 char *line = NULL, *p, *end;
4313 size_t size;
4314 ssize_t len;
4315 int lineno = 0, i;
4316 const struct got_histedit_cmd *cmd;
4317 struct got_object_id *commit_id = NULL;
4318 struct got_histedit_list_entry *hle = NULL;
4320 for (;;) {
4321 len = getline(&line, &size, f);
4322 if (len == -1) {
4323 const struct got_error *getline_err;
4324 if (feof(f))
4325 break;
4326 getline_err = got_error_from_errno("getline");
4327 err = got_ferror(f, getline_err->code);
4328 break;
4330 lineno++;
4331 p = line;
4332 while (isspace((unsigned char)p[0]))
4333 p++;
4334 if (p[0] == '#' || p[0] == '\0') {
4335 free(line);
4336 line = NULL;
4337 continue;
4339 cmd = NULL;
4340 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4341 cmd = &got_histedit_cmds[i];
4342 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4343 isspace((unsigned char)p[strlen(cmd->name)])) {
4344 p += strlen(cmd->name);
4345 break;
4347 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4348 p++;
4349 break;
4352 if (i == nitems(got_histedit_cmds)) {
4353 err = histedit_syntax_error(lineno);
4354 break;
4356 while (isspace((unsigned char)p[0]))
4357 p++;
4358 if (cmd->code == GOT_HISTEDIT_MESG) {
4359 if (hle == NULL || hle->logmsg != NULL) {
4360 err = got_error(GOT_ERR_HISTEDIT_CMD);
4361 break;
4363 if (p[0] == '\0') {
4364 err = histedit_edit_logmsg(hle, repo);
4365 if (err)
4366 break;
4367 } else {
4368 hle->logmsg = strdup(p);
4369 if (hle->logmsg == NULL) {
4370 err = got_error_from_errno("strdup");
4371 break;
4374 free(line);
4375 line = NULL;
4376 continue;
4377 } else {
4378 end = p;
4379 while (end[0] && !isspace((unsigned char)end[0]))
4380 end++;
4381 *end = '\0';
4383 err = got_object_resolve_id_str(&commit_id, repo, p);
4384 if (err) {
4385 /* override error code */
4386 err = histedit_syntax_error(lineno);
4387 break;
4390 hle = malloc(sizeof(*hle));
4391 if (hle == NULL) {
4392 err = got_error_from_errno("malloc");
4393 break;
4395 hle->cmd = cmd;
4396 hle->commit_id = commit_id;
4397 hle->logmsg = NULL;
4398 commit_id = NULL;
4399 free(line);
4400 line = NULL;
4401 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4404 free(line);
4405 free(commit_id);
4406 return err;
4409 static const struct got_error *
4410 histedit_check_script(struct got_histedit_list *histedit_cmds,
4411 struct got_object_id_queue *commits, struct got_repository *repo)
4413 const struct got_error *err = NULL;
4414 struct got_object_qid *qid;
4415 struct got_histedit_list_entry *hle;
4416 static char msg[80];
4417 char *id_str;
4419 if (TAILQ_EMPTY(histedit_cmds))
4420 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4421 "histedit script contains no commands");
4423 SIMPLEQ_FOREACH(qid, commits, entry) {
4424 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4425 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4426 break;
4428 if (hle == NULL) {
4429 err = got_object_id_str(&id_str, qid->id);
4430 if (err)
4431 return err;
4432 snprintf(msg, sizeof(msg),
4433 "commit %s missing from histedit script", id_str);
4434 free(id_str);
4435 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4439 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4440 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4441 "last commit in histedit script cannot be folded");
4443 return NULL;
4446 static const struct got_error *
4447 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4448 const char *path, struct got_object_id_queue *commits,
4449 struct got_repository *repo)
4451 const struct got_error *err = NULL;
4452 char *editor;
4453 FILE *f = NULL;
4455 err = get_editor(&editor);
4456 if (err)
4457 return err;
4459 if (spawn_editor(editor, path) == -1) {
4460 err = got_error_from_errno("failed spawning editor");
4461 goto done;
4464 f = fopen(path, "r");
4465 if (f == NULL) {
4466 err = got_error_from_errno("fopen");
4467 goto done;
4469 err = histedit_parse_list(histedit_cmds, f, repo);
4470 if (err)
4471 goto done;
4473 err = histedit_check_script(histedit_cmds, commits, repo);
4474 done:
4475 if (f && fclose(f) != 0 && err == NULL)
4476 err = got_error_from_errno("fclose");
4477 free(editor);
4478 return err;
4481 static const struct got_error *
4482 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4483 struct got_object_id_queue *, const char *, struct got_repository *);
4485 static const struct got_error *
4486 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4487 struct got_object_id_queue *commits, struct got_repository *repo)
4489 const struct got_error *err;
4490 FILE *f = NULL;
4491 char *path = NULL;
4493 err = got_opentemp_named(&path, &f, "got-histedit");
4494 if (err)
4495 return err;
4497 err = write_cmd_list(f);
4498 if (err)
4499 goto done;
4501 err = histedit_write_commit_list(commits, f, repo);
4502 if (err)
4503 goto done;
4505 if (fclose(f) != 0) {
4506 err = got_error_from_errno("fclose");
4507 goto done;
4509 f = NULL;
4511 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4512 if (err) {
4513 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4514 err->code != GOT_ERR_HISTEDIT_CMD)
4515 goto done;
4516 err = histedit_edit_list_retry(histedit_cmds, err,
4517 commits, path, repo);
4519 done:
4520 if (f && fclose(f) != 0 && err == NULL)
4521 err = got_error_from_errno("fclose");
4522 if (path && unlink(path) != 0 && err == NULL)
4523 err = got_error_from_errno2("unlink", path);
4524 free(path);
4525 return err;
4528 static const struct got_error *
4529 histedit_save_list(struct got_histedit_list *histedit_cmds,
4530 struct got_worktree *worktree, struct got_repository *repo)
4532 const struct got_error *err = NULL;
4533 char *path = NULL;
4534 FILE *f = NULL;
4535 struct got_histedit_list_entry *hle;
4536 struct got_commit_object *commit = NULL;
4538 err = got_worktree_get_histedit_script_path(&path, worktree);
4539 if (err)
4540 return err;
4542 f = fopen(path, "w");
4543 if (f == NULL) {
4544 err = got_error_from_errno2("fopen", path);
4545 goto done;
4547 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4548 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4549 repo);
4550 if (err)
4551 break;
4553 if (hle->logmsg) {
4554 int n = fprintf(f, "%c %s\n",
4555 GOT_HISTEDIT_MESG, hle->logmsg);
4556 if (n < 0) {
4557 err = got_ferror(f, GOT_ERR_IO);
4558 break;
4562 done:
4563 if (f && fclose(f) != 0 && err == NULL)
4564 err = got_error_from_errno("fclose");
4565 free(path);
4566 if (commit)
4567 got_object_commit_close(commit);
4568 return err;
4571 void
4572 histedit_free_list(struct got_histedit_list *histedit_cmds)
4574 struct got_histedit_list_entry *hle;
4576 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4577 TAILQ_REMOVE(histedit_cmds, hle, entry);
4578 free(hle);
4582 static const struct got_error *
4583 histedit_load_list(struct got_histedit_list *histedit_cmds,
4584 const char *path, struct got_repository *repo)
4586 const struct got_error *err = NULL;
4587 FILE *f = NULL;
4589 f = fopen(path, "r");
4590 if (f == NULL) {
4591 err = got_error_from_errno2("fopen", path);
4592 goto done;
4595 err = histedit_parse_list(histedit_cmds, f, repo);
4596 done:
4597 if (f && fclose(f) != 0 && err == NULL)
4598 err = got_error_from_errno("fclose");
4599 return err;
4602 static const struct got_error *
4603 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4604 const struct got_error *edit_err, struct got_object_id_queue *commits,
4605 const char *path, struct got_repository *repo)
4607 const struct got_error *err = NULL, *prev_err = edit_err;
4608 int resp = ' ';
4610 while (resp != 'c' && resp != 'r' && resp != 'a') {
4611 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4612 "or (a)bort: ", getprogname(), prev_err->msg);
4613 resp = getchar();
4614 if (resp == 'c') {
4615 histedit_free_list(histedit_cmds);
4616 err = histedit_run_editor(histedit_cmds, path, commits,
4617 repo);
4618 if (err) {
4619 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4620 err->code != GOT_ERR_HISTEDIT_CMD)
4621 break;
4622 prev_err = err;
4623 resp = ' ';
4624 continue;
4626 break;
4627 } else if (resp == 'r') {
4628 histedit_free_list(histedit_cmds);
4629 err = histedit_edit_script(histedit_cmds,
4630 commits, repo);
4631 if (err) {
4632 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4633 err->code != GOT_ERR_HISTEDIT_CMD)
4634 break;
4635 prev_err = err;
4636 resp = ' ';
4637 continue;
4639 break;
4640 } else if (resp == 'a') {
4641 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4642 break;
4643 } else
4644 printf("invalid response '%c'\n", resp);
4647 return err;
4650 static const struct got_error *
4651 histedit_complete(struct got_worktree *worktree,
4652 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4653 struct got_reference *branch, struct got_repository *repo)
4655 printf("Switching work tree to %s\n",
4656 got_ref_get_symref_target(branch));
4657 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4658 branch, repo);
4661 static const struct got_error *
4662 show_histedit_progress(struct got_commit_object *commit,
4663 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4665 const struct got_error *err;
4666 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4668 err = got_object_id_str(&old_id_str, hle->commit_id);
4669 if (err)
4670 goto done;
4672 if (new_id) {
4673 err = got_object_id_str(&new_id_str, new_id);
4674 if (err)
4675 goto done;
4678 old_id_str[12] = '\0';
4679 if (new_id_str)
4680 new_id_str[12] = '\0';
4682 if (hle->logmsg) {
4683 logmsg = strdup(hle->logmsg);
4684 if (logmsg == NULL) {
4685 err = got_error_from_errno("strdup");
4686 goto done;
4688 trim_logmsg(logmsg, 42);
4689 } else {
4690 err = get_short_logmsg(&logmsg, 42, commit);
4691 if (err)
4692 goto done;
4695 switch (hle->cmd->code) {
4696 case GOT_HISTEDIT_PICK:
4697 case GOT_HISTEDIT_EDIT:
4698 printf("%s -> %s: %s\n", old_id_str,
4699 new_id_str ? new_id_str : "no-op change", logmsg);
4700 break;
4701 case GOT_HISTEDIT_DROP:
4702 case GOT_HISTEDIT_FOLD:
4703 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4704 logmsg);
4705 break;
4706 default:
4707 break;
4710 done:
4711 free(old_id_str);
4712 free(new_id_str);
4713 return err;
4716 static const struct got_error *
4717 histedit_commit(struct got_pathlist_head *merged_paths,
4718 struct got_worktree *worktree, struct got_fileindex *fileindex,
4719 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4720 struct got_repository *repo)
4722 const struct got_error *err;
4723 struct got_commit_object *commit;
4724 struct got_object_id *new_commit_id;
4726 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4727 && hle->logmsg == NULL) {
4728 err = histedit_edit_logmsg(hle, repo);
4729 if (err)
4730 return err;
4733 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4734 if (err)
4735 return err;
4737 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4738 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4739 hle->logmsg, repo);
4740 if (err) {
4741 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4742 goto done;
4743 err = show_histedit_progress(commit, hle, NULL);
4744 } else {
4745 err = show_histedit_progress(commit, hle, new_commit_id);
4746 free(new_commit_id);
4748 done:
4749 got_object_commit_close(commit);
4750 return err;
4753 static const struct got_error *
4754 histedit_skip_commit(struct got_histedit_list_entry *hle,
4755 struct got_worktree *worktree, struct got_repository *repo)
4757 const struct got_error *error;
4758 struct got_commit_object *commit;
4760 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4761 repo);
4762 if (error)
4763 return error;
4765 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4766 if (error)
4767 return error;
4769 error = show_histedit_progress(commit, hle, NULL);
4770 got_object_commit_close(commit);
4771 return error;
4774 static const struct got_error *
4775 cmd_histedit(int argc, char *argv[])
4777 const struct got_error *error = NULL;
4778 struct got_worktree *worktree = NULL;
4779 struct got_fileindex *fileindex = NULL;
4780 struct got_repository *repo = NULL;
4781 char *cwd = NULL;
4782 struct got_reference *branch = NULL;
4783 struct got_reference *tmp_branch = NULL;
4784 struct got_object_id *resume_commit_id = NULL;
4785 struct got_object_id *base_commit_id = NULL;
4786 struct got_object_id *head_commit_id = NULL;
4787 struct got_commit_object *commit = NULL;
4788 int ch, rebase_in_progress = 0, did_something;
4789 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4790 const char *edit_script_path = NULL;
4791 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4792 struct got_object_id_queue commits;
4793 struct got_pathlist_head merged_paths;
4794 const struct got_object_id_queue *parent_ids;
4795 struct got_object_qid *pid;
4796 struct got_histedit_list histedit_cmds;
4797 struct got_histedit_list_entry *hle;
4799 SIMPLEQ_INIT(&commits);
4800 TAILQ_INIT(&histedit_cmds);
4801 TAILQ_INIT(&merged_paths);
4803 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4804 switch (ch) {
4805 case 'a':
4806 abort_edit = 1;
4807 break;
4808 case 'c':
4809 continue_edit = 1;
4810 break;
4811 case 'F':
4812 edit_script_path = optarg;
4813 break;
4814 default:
4815 usage_histedit();
4816 /* NOTREACHED */
4820 argc -= optind;
4821 argv += optind;
4823 #ifndef PROFILE
4824 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4825 "unveil", NULL) == -1)
4826 err(1, "pledge");
4827 #endif
4828 if (abort_edit && continue_edit)
4829 usage_histedit();
4830 if (argc != 0)
4831 usage_histedit();
4834 * This command cannot apply unveil(2) in all cases because the
4835 * user may choose to run an editor to edit the histedit script
4836 * and to edit individual commit log messages.
4837 * unveil(2) traverses exec(2); if an editor is used we have to
4838 * apply unveil after edit script and log messages have been written.
4839 * XXX TODO: Make use of unveil(2) where possible.
4842 cwd = getcwd(NULL, 0);
4843 if (cwd == NULL) {
4844 error = got_error_from_errno("getcwd");
4845 goto done;
4847 error = got_worktree_open(&worktree, cwd);
4848 if (error)
4849 goto done;
4851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4852 if (error != NULL)
4853 goto done;
4855 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4856 if (error)
4857 goto done;
4858 if (rebase_in_progress) {
4859 error = got_error(GOT_ERR_REBASING);
4860 goto done;
4863 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4864 if (error)
4865 goto done;
4867 if (edit_in_progress && abort_edit) {
4868 error = got_worktree_histedit_continue(&resume_commit_id,
4869 &tmp_branch, &branch, &base_commit_id, &fileindex,
4870 worktree, repo);
4871 if (error)
4872 goto done;
4873 printf("Switching work tree to %s\n",
4874 got_ref_get_symref_target(branch));
4875 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4876 branch, base_commit_id, update_progress, &did_something);
4877 if (error)
4878 goto done;
4879 printf("Histedit of %s aborted\n",
4880 got_ref_get_symref_target(branch));
4881 goto done; /* nothing else to do */
4882 } else if (abort_edit) {
4883 error = got_error(GOT_ERR_NOT_HISTEDIT);
4884 goto done;
4887 if (continue_edit) {
4888 char *path;
4890 if (!edit_in_progress) {
4891 error = got_error(GOT_ERR_NOT_HISTEDIT);
4892 goto done;
4895 error = got_worktree_get_histedit_script_path(&path, worktree);
4896 if (error)
4897 goto done;
4899 error = histedit_load_list(&histedit_cmds, path, repo);
4900 free(path);
4901 if (error)
4902 goto done;
4904 error = got_worktree_histedit_continue(&resume_commit_id,
4905 &tmp_branch, &branch, &base_commit_id, &fileindex,
4906 worktree, repo);
4907 if (error)
4908 goto done;
4910 error = got_ref_resolve(&head_commit_id, repo, branch);
4911 if (error)
4912 goto done;
4914 error = got_object_open_as_commit(&commit, repo,
4915 head_commit_id);
4916 if (error)
4917 goto done;
4918 parent_ids = got_object_commit_get_parent_ids(commit);
4919 pid = SIMPLEQ_FIRST(parent_ids);
4920 if (pid == NULL) {
4921 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4922 goto done;
4924 error = collect_commits(&commits, head_commit_id, pid->id,
4925 base_commit_id, got_worktree_get_path_prefix(worktree),
4926 GOT_ERR_HISTEDIT_PATH, repo);
4927 got_object_commit_close(commit);
4928 commit = NULL;
4929 if (error)
4930 goto done;
4931 } else {
4932 if (edit_in_progress) {
4933 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4934 goto done;
4937 error = got_ref_open(&branch, repo,
4938 got_worktree_get_head_ref_name(worktree), 0);
4939 if (error != NULL)
4940 goto done;
4942 error = got_ref_resolve(&head_commit_id, repo, branch);
4943 got_ref_close(branch);
4944 branch = NULL;
4945 if (error)
4946 goto done;
4948 error = got_object_open_as_commit(&commit, repo,
4949 head_commit_id);
4950 if (error)
4951 goto done;
4952 parent_ids = got_object_commit_get_parent_ids(commit);
4953 pid = SIMPLEQ_FIRST(parent_ids);
4954 if (pid == NULL) {
4955 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4956 goto done;
4958 error = collect_commits(&commits, head_commit_id, pid->id,
4959 got_worktree_get_base_commit_id(worktree),
4960 got_worktree_get_path_prefix(worktree),
4961 GOT_ERR_HISTEDIT_PATH, repo);
4962 got_object_commit_close(commit);
4963 commit = NULL;
4964 if (error)
4965 goto done;
4967 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4968 &base_commit_id, &fileindex, worktree, repo);
4969 if (error)
4970 goto done;
4972 if (edit_script_path) {
4973 error = histedit_load_list(&histedit_cmds,
4974 edit_script_path, repo);
4975 if (error) {
4976 got_worktree_histedit_abort(worktree, fileindex,
4977 repo, branch, base_commit_id,
4978 update_progress, &did_something);
4979 goto done;
4981 } else {
4982 error = histedit_edit_script(&histedit_cmds, &commits,
4983 repo);
4984 if (error) {
4985 got_worktree_histedit_abort(worktree, fileindex,
4986 repo, branch, base_commit_id,
4987 update_progress, &did_something);
4988 goto done;
4993 error = histedit_save_list(&histedit_cmds, worktree,
4994 repo);
4995 if (error) {
4996 got_worktree_histedit_abort(worktree, fileindex,
4997 repo, branch, base_commit_id,
4998 update_progress, &did_something);
4999 goto done;
5004 error = histedit_check_script(&histedit_cmds, &commits, repo);
5005 if (error)
5006 goto done;
5008 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5009 if (resume_commit_id) {
5010 if (got_object_id_cmp(hle->commit_id,
5011 resume_commit_id) != 0)
5012 continue;
5014 resume_commit_id = NULL;
5015 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5016 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5017 error = histedit_skip_commit(hle, worktree,
5018 repo);
5019 } else {
5020 error = histedit_commit(NULL, worktree,
5021 fileindex, tmp_branch, hle, repo);
5023 if (error)
5024 goto done;
5025 continue;
5028 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5029 error = histedit_skip_commit(hle, worktree, repo);
5030 if (error)
5031 goto done;
5032 continue;
5035 error = got_object_open_as_commit(&commit, repo,
5036 hle->commit_id);
5037 if (error)
5038 goto done;
5039 parent_ids = got_object_commit_get_parent_ids(commit);
5040 pid = SIMPLEQ_FIRST(parent_ids);
5042 error = got_worktree_histedit_merge_files(&merged_paths,
5043 worktree, fileindex, pid->id, hle->commit_id, repo,
5044 rebase_progress, &rebase_status, check_cancelled, NULL);
5045 if (error)
5046 goto done;
5047 got_object_commit_close(commit);
5048 commit = NULL;
5050 if (rebase_status == GOT_STATUS_CONFLICT) {
5051 got_worktree_rebase_pathlist_free(&merged_paths);
5052 break;
5055 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5056 char *id_str;
5057 error = got_object_id_str(&id_str, hle->commit_id);
5058 if (error)
5059 goto done;
5060 printf("Stopping histedit for amending commit %s\n",
5061 id_str);
5062 free(id_str);
5063 got_worktree_rebase_pathlist_free(&merged_paths);
5064 error = got_worktree_histedit_postpone(worktree,
5065 fileindex);
5066 goto done;
5069 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5070 error = histedit_skip_commit(hle, worktree, repo);
5071 if (error)
5072 goto done;
5073 continue;
5076 error = histedit_commit(&merged_paths, worktree, fileindex,
5077 tmp_branch, hle, repo);
5078 got_worktree_rebase_pathlist_free(&merged_paths);
5079 if (error)
5080 goto done;
5083 if (rebase_status == GOT_STATUS_CONFLICT) {
5084 error = got_worktree_histedit_postpone(worktree, fileindex);
5085 if (error)
5086 goto done;
5087 error = got_error_msg(GOT_ERR_CONFLICTS,
5088 "conflicts must be resolved before rebasing can continue");
5089 } else
5090 error = histedit_complete(worktree, fileindex, tmp_branch,
5091 branch, repo);
5092 done:
5093 got_object_id_queue_free(&commits);
5094 histedit_free_list(&histedit_cmds);
5095 free(head_commit_id);
5096 free(base_commit_id);
5097 free(resume_commit_id);
5098 if (commit)
5099 got_object_commit_close(commit);
5100 if (branch)
5101 got_ref_close(branch);
5102 if (tmp_branch)
5103 got_ref_close(tmp_branch);
5104 if (worktree)
5105 got_worktree_close(worktree);
5106 if (repo)
5107 got_repo_close(repo);
5108 return error;