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",
3143 getprogname());
3144 exit(1);
3147 struct collect_commit_logmsg_arg {
3148 const char *cmdline_log;
3149 const char *editor;
3150 const char *worktree_path;
3151 const char *branch_name;
3152 const char *repo_path;
3153 char *logmsg_path;
3157 static const struct got_error *
3158 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3159 void *arg)
3161 char *initial_content = NULL;
3162 struct got_pathlist_entry *pe;
3163 const struct got_error *err = NULL;
3164 char *template = NULL;
3165 struct collect_commit_logmsg_arg *a = arg;
3166 int fd;
3167 size_t len;
3169 /* if a message was specified on the command line, just use it */
3170 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3171 len = strlen(a->cmdline_log) + 1;
3172 *logmsg = malloc(len + 1);
3173 if (*logmsg == NULL)
3174 return got_error_from_errno("malloc");
3175 strlcpy(*logmsg, a->cmdline_log, len);
3176 return NULL;
3179 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3180 return got_error_from_errno("asprintf");
3182 if (asprintf(&initial_content,
3183 "\n# changes to be committed on branch %s:\n",
3184 a->branch_name) == -1)
3185 return got_error_from_errno("asprintf");
3187 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3188 if (err)
3189 goto done;
3191 dprintf(fd, initial_content);
3193 TAILQ_FOREACH(pe, commitable_paths, entry) {
3194 struct got_commitable *ct = pe->data;
3195 dprintf(fd, "# %c %s\n",
3196 got_commitable_get_status(ct),
3197 got_commitable_get_path(ct));
3199 close(fd);
3201 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3202 done:
3203 unlink(a->logmsg_path);
3204 free(a->logmsg_path);
3205 free(initial_content);
3206 free(template);
3208 /* Editor is done; we can now apply unveil(2) */
3209 if (err == NULL) {
3210 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3211 if (err) {
3212 free(*logmsg);
3213 *logmsg = NULL;
3216 return err;
3219 static const struct got_error *
3220 cmd_commit(int argc, char *argv[])
3222 const struct got_error *error = NULL;
3223 struct got_worktree *worktree = NULL;
3224 struct got_repository *repo = NULL;
3225 char *cwd = NULL, *id_str = NULL;
3226 struct got_object_id *id = NULL;
3227 const char *logmsg = NULL;
3228 const char *got_author = getenv("GOT_AUTHOR");
3229 struct collect_commit_logmsg_arg cl_arg;
3230 char *editor = NULL;
3231 int ch, rebase_in_progress;
3232 struct got_pathlist_head paths;
3234 TAILQ_INIT(&paths);
3236 while ((ch = getopt(argc, argv, "m:")) != -1) {
3237 switch (ch) {
3238 case 'm':
3239 logmsg = optarg;
3240 break;
3241 default:
3242 usage_commit();
3243 /* NOTREACHED */
3247 argc -= optind;
3248 argv += optind;
3250 #ifndef PROFILE
3251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3252 "unveil", NULL) == -1)
3253 err(1, "pledge");
3254 #endif
3255 if (got_author == NULL) {
3256 /* TODO: Look current user up in password database */
3257 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3258 goto done;
3261 cwd = getcwd(NULL, 0);
3262 if (cwd == NULL) {
3263 error = got_error_from_errno("getcwd");
3264 goto done;
3266 error = got_worktree_open(&worktree, cwd);
3267 if (error)
3268 goto done;
3270 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3271 if (error)
3272 goto done;
3273 if (rebase_in_progress) {
3274 error = got_error(GOT_ERR_REBASING);
3275 goto done;
3278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3279 if (error)
3280 goto done;
3282 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3283 if (error != NULL)
3284 goto done;
3287 * unveil(2) traverses exec(2); if an editor is used we have
3288 * to apply unveil after the log message has been written.
3290 if (logmsg == NULL || strlen(logmsg) == 0)
3291 error = get_editor(&editor);
3292 else
3293 error = apply_unveil(got_repo_get_path(repo), 0,
3294 got_worktree_get_root_path(worktree));
3295 if (error)
3296 goto done;
3298 cl_arg.editor = editor;
3299 cl_arg.cmdline_log = logmsg;
3300 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3301 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3302 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3303 cl_arg.branch_name += 5;
3304 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3305 cl_arg.branch_name += 6;
3306 cl_arg.repo_path = got_repo_get_path(repo);
3307 cl_arg.logmsg_path = NULL;
3308 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3309 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3310 if (error) {
3311 if (cl_arg.logmsg_path)
3312 fprintf(stderr, "%s: log message preserved in %s\n",
3313 getprogname(), cl_arg.logmsg_path);
3314 goto done;
3317 if (cl_arg.logmsg_path)
3318 unlink(cl_arg.logmsg_path);
3320 error = got_object_id_str(&id_str, id);
3321 if (error)
3322 goto done;
3323 printf("Created commit %s\n", id_str);
3324 done:
3325 if (repo)
3326 got_repo_close(repo);
3327 if (worktree)
3328 got_worktree_close(worktree);
3329 free(cwd);
3330 free(id_str);
3331 free(editor);
3332 return error;
3335 __dead static void
3336 usage_cherrypick(void)
3338 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3339 exit(1);
3342 static const struct got_error *
3343 cmd_cherrypick(int argc, char *argv[])
3345 const struct got_error *error = NULL;
3346 struct got_worktree *worktree = NULL;
3347 struct got_repository *repo = NULL;
3348 char *cwd = NULL, *commit_id_str = NULL;
3349 struct got_object_id *commit_id = NULL;
3350 struct got_commit_object *commit = NULL;
3351 struct got_object_qid *pid;
3352 struct got_reference *head_ref = NULL;
3353 int ch, did_something = 0;
3355 while ((ch = getopt(argc, argv, "")) != -1) {
3356 switch (ch) {
3357 default:
3358 usage_cherrypick();
3359 /* NOTREACHED */
3363 argc -= optind;
3364 argv += optind;
3366 #ifndef PROFILE
3367 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3368 "unveil", NULL) == -1)
3369 err(1, "pledge");
3370 #endif
3371 if (argc != 1)
3372 usage_cherrypick();
3374 cwd = getcwd(NULL, 0);
3375 if (cwd == NULL) {
3376 error = got_error_from_errno("getcwd");
3377 goto done;
3379 error = got_worktree_open(&worktree, cwd);
3380 if (error)
3381 goto done;
3383 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3384 if (error != NULL)
3385 goto done;
3387 error = apply_unveil(got_repo_get_path(repo), 0,
3388 got_worktree_get_root_path(worktree));
3389 if (error)
3390 goto done;
3392 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3393 GOT_OBJ_TYPE_COMMIT, repo);
3394 if (error != NULL) {
3395 struct got_reference *ref;
3396 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3397 goto done;
3398 error = got_ref_open(&ref, repo, argv[0], 0);
3399 if (error != NULL)
3400 goto done;
3401 error = got_ref_resolve(&commit_id, repo, ref);
3402 got_ref_close(ref);
3403 if (error != NULL)
3404 goto done;
3406 error = got_object_id_str(&commit_id_str, commit_id);
3407 if (error)
3408 goto done;
3410 error = got_ref_open(&head_ref, repo,
3411 got_worktree_get_head_ref_name(worktree), 0);
3412 if (error != NULL)
3413 goto done;
3415 error = check_same_branch(commit_id, head_ref, NULL, repo);
3416 if (error) {
3417 if (error->code != GOT_ERR_ANCESTRY)
3418 goto done;
3419 error = NULL;
3420 } else {
3421 error = got_error(GOT_ERR_SAME_BRANCH);
3422 goto done;
3425 error = got_object_open_as_commit(&commit, repo, commit_id);
3426 if (error)
3427 goto done;
3428 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3429 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3430 commit_id, repo, update_progress, &did_something, check_cancelled,
3431 NULL);
3432 if (error != NULL)
3433 goto done;
3435 if (did_something)
3436 printf("Merged commit %s\n", commit_id_str);
3437 done:
3438 if (commit)
3439 got_object_commit_close(commit);
3440 free(commit_id_str);
3441 if (head_ref)
3442 got_ref_close(head_ref);
3443 if (worktree)
3444 got_worktree_close(worktree);
3445 if (repo)
3446 got_repo_close(repo);
3447 return error;
3450 __dead static void
3451 usage_backout(void)
3453 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3454 exit(1);
3457 static const struct got_error *
3458 cmd_backout(int argc, char *argv[])
3460 const struct got_error *error = NULL;
3461 struct got_worktree *worktree = NULL;
3462 struct got_repository *repo = NULL;
3463 char *cwd = NULL, *commit_id_str = NULL;
3464 struct got_object_id *commit_id = NULL;
3465 struct got_commit_object *commit = NULL;
3466 struct got_object_qid *pid;
3467 struct got_reference *head_ref = NULL;
3468 int ch, did_something = 0;
3470 while ((ch = getopt(argc, argv, "")) != -1) {
3471 switch (ch) {
3472 default:
3473 usage_backout();
3474 /* NOTREACHED */
3478 argc -= optind;
3479 argv += optind;
3481 #ifndef PROFILE
3482 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3483 "unveil", NULL) == -1)
3484 err(1, "pledge");
3485 #endif
3486 if (argc != 1)
3487 usage_backout();
3489 cwd = getcwd(NULL, 0);
3490 if (cwd == NULL) {
3491 error = got_error_from_errno("getcwd");
3492 goto done;
3494 error = got_worktree_open(&worktree, cwd);
3495 if (error)
3496 goto done;
3498 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3499 if (error != NULL)
3500 goto done;
3502 error = apply_unveil(got_repo_get_path(repo), 0,
3503 got_worktree_get_root_path(worktree));
3504 if (error)
3505 goto done;
3507 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3508 GOT_OBJ_TYPE_COMMIT, repo);
3509 if (error != NULL) {
3510 struct got_reference *ref;
3511 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3512 goto done;
3513 error = got_ref_open(&ref, repo, argv[0], 0);
3514 if (error != NULL)
3515 goto done;
3516 error = got_ref_resolve(&commit_id, repo, ref);
3517 got_ref_close(ref);
3518 if (error != NULL)
3519 goto done;
3521 error = got_object_id_str(&commit_id_str, commit_id);
3522 if (error)
3523 goto done;
3525 error = got_ref_open(&head_ref, repo,
3526 got_worktree_get_head_ref_name(worktree), 0);
3527 if (error != NULL)
3528 goto done;
3530 error = check_same_branch(commit_id, head_ref, NULL, repo);
3531 if (error)
3532 goto done;
3534 error = got_object_open_as_commit(&commit, repo, commit_id);
3535 if (error)
3536 goto done;
3537 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3538 if (pid == NULL) {
3539 error = got_error(GOT_ERR_ROOT_COMMIT);
3540 goto done;
3543 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3544 update_progress, &did_something, check_cancelled, NULL);
3545 if (error != NULL)
3546 goto done;
3548 if (did_something)
3549 printf("Backed out commit %s\n", commit_id_str);
3550 done:
3551 if (commit)
3552 got_object_commit_close(commit);
3553 free(commit_id_str);
3554 if (head_ref)
3555 got_ref_close(head_ref);
3556 if (worktree)
3557 got_worktree_close(worktree);
3558 if (repo)
3559 got_repo_close(repo);
3560 return error;
3563 __dead static void
3564 usage_rebase(void)
3566 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3567 getprogname());
3568 exit(1);
3571 void
3572 trim_logmsg(char *logmsg, int limit)
3574 char *nl;
3575 size_t len;
3577 len = strlen(logmsg);
3578 if (len > limit)
3579 len = limit;
3580 logmsg[len] = '\0';
3581 nl = strchr(logmsg, '\n');
3582 if (nl)
3583 *nl = '\0';
3586 static const struct got_error *
3587 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3589 const char *logmsg0 = NULL;
3591 logmsg0 = got_object_commit_get_logmsg(commit);
3593 while (isspace((unsigned char)logmsg0[0]))
3594 logmsg0++;
3596 *logmsg = strdup(logmsg0);
3597 if (*logmsg == NULL)
3598 return got_error_from_errno("strdup");
3600 trim_logmsg(*logmsg, limit);
3601 return NULL;
3604 static const struct got_error *
3605 show_rebase_progress(struct got_commit_object *commit,
3606 struct got_object_id *old_id, struct got_object_id *new_id)
3608 const struct got_error *err;
3609 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3611 err = got_object_id_str(&old_id_str, old_id);
3612 if (err)
3613 goto done;
3615 if (new_id) {
3616 err = got_object_id_str(&new_id_str, new_id);
3617 if (err)
3618 goto done;
3621 old_id_str[12] = '\0';
3622 if (new_id_str)
3623 new_id_str[12] = '\0';
3625 err = get_short_logmsg(&logmsg, 42, commit);
3626 if (err)
3627 goto done;
3629 printf("%s -> %s: %s\n", old_id_str,
3630 new_id_str ? new_id_str : "no-op change", logmsg);
3631 done:
3632 free(old_id_str);
3633 free(new_id_str);
3634 return err;
3637 static const struct got_error *
3638 rebase_progress(void *arg, unsigned char status, const char *path)
3640 unsigned char *rebase_status = arg;
3642 while (path[0] == '/')
3643 path++;
3644 printf("%c %s\n", status, path);
3646 if (*rebase_status == GOT_STATUS_CONFLICT)
3647 return NULL;
3648 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3649 *rebase_status = status;
3650 return NULL;
3653 static const struct got_error *
3654 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3655 struct got_reference *branch, struct got_reference *new_base_branch,
3656 struct got_reference *tmp_branch, struct got_repository *repo)
3658 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3659 return got_worktree_rebase_complete(worktree, fileindex,
3660 new_base_branch, tmp_branch, branch, repo);
3663 static const struct got_error *
3664 rebase_commit(struct got_pathlist_head *merged_paths,
3665 struct got_worktree *worktree, struct got_fileindex *fileindex,
3666 struct got_reference *tmp_branch,
3667 struct got_object_id *commit_id, struct got_repository *repo)
3669 const struct got_error *error;
3670 struct got_commit_object *commit;
3671 struct got_object_id *new_commit_id;
3673 error = got_object_open_as_commit(&commit, repo, commit_id);
3674 if (error)
3675 return error;
3677 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3678 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3679 if (error) {
3680 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3681 goto done;
3682 error = show_rebase_progress(commit, commit_id, NULL);
3683 } else {
3684 error = show_rebase_progress(commit, commit_id, new_commit_id);
3685 free(new_commit_id);
3687 done:
3688 got_object_commit_close(commit);
3689 return error;
3692 struct check_path_prefix_arg {
3693 const char *path_prefix;
3694 size_t len;
3695 int errcode;
3698 static const struct got_error *
3699 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3700 struct got_blob_object *blob2, struct got_object_id *id1,
3701 struct got_object_id *id2, const char *path1, const char *path2,
3702 struct got_repository *repo)
3704 struct check_path_prefix_arg *a = arg;
3706 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3707 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3708 return got_error(a->errcode);
3710 return NULL;
3713 static const struct got_error *
3714 check_path_prefix(struct got_object_id *parent_id,
3715 struct got_object_id *commit_id, const char *path_prefix,
3716 int errcode, struct got_repository *repo)
3718 const struct got_error *err;
3719 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3720 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3721 struct check_path_prefix_arg cpp_arg;
3723 if (got_path_is_root_dir(path_prefix))
3724 return NULL;
3726 err = got_object_open_as_commit(&commit, repo, commit_id);
3727 if (err)
3728 goto done;
3730 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3731 if (err)
3732 goto done;
3734 err = got_object_open_as_tree(&tree1, repo,
3735 got_object_commit_get_tree_id(parent_commit));
3736 if (err)
3737 goto done;
3739 err = got_object_open_as_tree(&tree2, repo,
3740 got_object_commit_get_tree_id(commit));
3741 if (err)
3742 goto done;
3744 cpp_arg.path_prefix = path_prefix;
3745 while (cpp_arg.path_prefix[0] == '/')
3746 cpp_arg.path_prefix++;
3747 cpp_arg.len = strlen(cpp_arg.path_prefix);
3748 cpp_arg.errcode = errcode;
3749 err = got_diff_tree(tree1, tree2, "", "", repo,
3750 check_path_prefix_in_diff, &cpp_arg, 0);
3751 done:
3752 if (tree1)
3753 got_object_tree_close(tree1);
3754 if (tree2)
3755 got_object_tree_close(tree2);
3756 if (commit)
3757 got_object_commit_close(commit);
3758 if (parent_commit)
3759 got_object_commit_close(parent_commit);
3760 return err;
3763 static const struct got_error *
3764 collect_commits(struct got_object_id_queue *commits,
3765 struct got_object_id *initial_commit_id,
3766 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3767 const char *path_prefix, int path_prefix_errcode,
3768 struct got_repository *repo)
3770 const struct got_error *err = NULL;
3771 struct got_commit_graph *graph = NULL;
3772 struct got_object_id *parent_id = NULL;
3773 struct got_object_qid *qid;
3774 struct got_object_id *commit_id = initial_commit_id;
3776 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3777 if (err)
3778 return err;
3780 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3781 if (err)
3782 goto done;
3783 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3784 err = got_commit_graph_iter_next(&parent_id, graph);
3785 if (err) {
3786 if (err->code == GOT_ERR_ITER_COMPLETED) {
3787 err = got_error_msg(GOT_ERR_ANCESTRY,
3788 "ran out of commits to rebase before "
3789 "youngest common ancestor commit has "
3790 "been reached?!?");
3791 goto done;
3792 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3793 goto done;
3794 err = got_commit_graph_fetch_commits(graph, 1, repo);
3795 if (err)
3796 goto done;
3797 } else {
3798 err = check_path_prefix(parent_id, commit_id,
3799 path_prefix, path_prefix_errcode, repo);
3800 if (err)
3801 goto done;
3803 err = got_object_qid_alloc(&qid, commit_id);
3804 if (err)
3805 goto done;
3806 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3807 commit_id = parent_id;
3810 done:
3811 got_commit_graph_close(graph);
3812 return err;
3815 static const struct got_error *
3816 cmd_rebase(int argc, char *argv[])
3818 const struct got_error *error = NULL;
3819 struct got_worktree *worktree = NULL;
3820 struct got_repository *repo = NULL;
3821 struct got_fileindex *fileindex = NULL;
3822 char *cwd = NULL;
3823 struct got_reference *branch = NULL;
3824 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3825 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3826 struct got_object_id *resume_commit_id = NULL;
3827 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3828 struct got_commit_object *commit = NULL;
3829 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3830 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3831 struct got_object_id_queue commits;
3832 struct got_pathlist_head merged_paths;
3833 const struct got_object_id_queue *parent_ids;
3834 struct got_object_qid *qid, *pid;
3836 SIMPLEQ_INIT(&commits);
3837 TAILQ_INIT(&merged_paths);
3839 while ((ch = getopt(argc, argv, "ac")) != -1) {
3840 switch (ch) {
3841 case 'a':
3842 abort_rebase = 1;
3843 break;
3844 case 'c':
3845 continue_rebase = 1;
3846 break;
3847 default:
3848 usage_rebase();
3849 /* NOTREACHED */
3853 argc -= optind;
3854 argv += optind;
3856 #ifndef PROFILE
3857 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3858 "unveil", NULL) == -1)
3859 err(1, "pledge");
3860 #endif
3861 if (abort_rebase && continue_rebase)
3862 usage_rebase();
3863 else if (abort_rebase || continue_rebase) {
3864 if (argc != 0)
3865 usage_rebase();
3866 } else if (argc != 1)
3867 usage_rebase();
3869 cwd = getcwd(NULL, 0);
3870 if (cwd == NULL) {
3871 error = got_error_from_errno("getcwd");
3872 goto done;
3874 error = got_worktree_open(&worktree, cwd);
3875 if (error)
3876 goto done;
3878 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3879 if (error != NULL)
3880 goto done;
3882 error = apply_unveil(got_repo_get_path(repo), 0,
3883 got_worktree_get_root_path(worktree));
3884 if (error)
3885 goto done;
3887 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3888 if (error)
3889 goto done;
3891 if (abort_rebase) {
3892 int did_something;
3893 if (!rebase_in_progress) {
3894 error = got_error(GOT_ERR_NOT_REBASING);
3895 goto done;
3897 error = got_worktree_rebase_continue(&resume_commit_id,
3898 &new_base_branch, &tmp_branch, &branch, &fileindex,
3899 worktree, repo);
3900 if (error)
3901 goto done;
3902 printf("Switching work tree to %s\n",
3903 got_ref_get_symref_target(new_base_branch));
3904 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3905 new_base_branch, update_progress, &did_something);
3906 if (error)
3907 goto done;
3908 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3909 goto done; /* nothing else to do */
3912 if (continue_rebase) {
3913 if (!rebase_in_progress) {
3914 error = got_error(GOT_ERR_NOT_REBASING);
3915 goto done;
3917 error = got_worktree_rebase_continue(&resume_commit_id,
3918 &new_base_branch, &tmp_branch, &branch, &fileindex,
3919 worktree, repo);
3920 if (error)
3921 goto done;
3923 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3924 resume_commit_id, repo);
3925 if (error)
3926 goto done;
3928 yca_id = got_object_id_dup(resume_commit_id);
3929 if (yca_id == NULL) {
3930 error = got_error_from_errno("got_object_id_dup");
3931 goto done;
3933 } else {
3934 error = got_ref_open(&branch, repo, argv[0], 0);
3935 if (error != NULL)
3936 goto done;
3939 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3940 if (error)
3941 goto done;
3943 if (!continue_rebase) {
3944 struct got_object_id *base_commit_id;
3946 base_commit_id = got_worktree_get_base_commit_id(worktree);
3947 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3948 base_commit_id, branch_head_commit_id, repo);
3949 if (error)
3950 goto done;
3951 if (yca_id == NULL) {
3952 error = got_error_msg(GOT_ERR_ANCESTRY,
3953 "specified branch shares no common ancestry "
3954 "with work tree's branch");
3955 goto done;
3958 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3959 if (error) {
3960 if (error->code != GOT_ERR_ANCESTRY)
3961 goto done;
3962 error = NULL;
3963 } else {
3964 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3965 "specified branch resolves to a commit which "
3966 "is already contained in work tree's branch");
3967 goto done;
3969 error = got_worktree_rebase_prepare(&new_base_branch,
3970 &tmp_branch, &fileindex, worktree, branch, repo);
3971 if (error)
3972 goto done;
3975 commit_id = branch_head_commit_id;
3976 error = got_object_open_as_commit(&commit, repo, commit_id);
3977 if (error)
3978 goto done;
3980 parent_ids = got_object_commit_get_parent_ids(commit);
3981 pid = SIMPLEQ_FIRST(parent_ids);
3982 error = collect_commits(&commits, commit_id, pid->id,
3983 yca_id, got_worktree_get_path_prefix(worktree),
3984 GOT_ERR_REBASE_PATH, repo);
3985 got_object_commit_close(commit);
3986 commit = NULL;
3987 if (error)
3988 goto done;
3990 if (SIMPLEQ_EMPTY(&commits)) {
3991 if (continue_rebase)
3992 error = rebase_complete(worktree, fileindex,
3993 branch, new_base_branch, tmp_branch, repo);
3994 else
3995 error = got_error(GOT_ERR_EMPTY_REBASE);
3996 goto done;
3999 pid = NULL;
4000 SIMPLEQ_FOREACH(qid, &commits, entry) {
4001 commit_id = qid->id;
4002 parent_id = pid ? pid->id : yca_id;
4003 pid = qid;
4005 error = got_worktree_rebase_merge_files(&merged_paths,
4006 worktree, fileindex, parent_id, commit_id, repo,
4007 rebase_progress, &rebase_status, check_cancelled, NULL);
4008 if (error)
4009 goto done;
4011 if (rebase_status == GOT_STATUS_CONFLICT) {
4012 got_worktree_rebase_pathlist_free(&merged_paths);
4013 break;
4016 error = rebase_commit(&merged_paths, worktree, fileindex,
4017 tmp_branch, commit_id, repo);
4018 got_worktree_rebase_pathlist_free(&merged_paths);
4019 if (error)
4020 goto done;
4023 if (rebase_status == GOT_STATUS_CONFLICT) {
4024 error = got_worktree_rebase_postpone(worktree, fileindex);
4025 if (error)
4026 goto done;
4027 error = got_error_msg(GOT_ERR_CONFLICTS,
4028 "conflicts must be resolved before rebasing can continue");
4029 } else
4030 error = rebase_complete(worktree, fileindex, branch,
4031 new_base_branch, tmp_branch, repo);
4032 done:
4033 got_object_id_queue_free(&commits);
4034 free(branch_head_commit_id);
4035 free(resume_commit_id);
4036 free(yca_id);
4037 if (commit)
4038 got_object_commit_close(commit);
4039 if (branch)
4040 got_ref_close(branch);
4041 if (new_base_branch)
4042 got_ref_close(new_base_branch);
4043 if (tmp_branch)
4044 got_ref_close(tmp_branch);
4045 if (worktree)
4046 got_worktree_close(worktree);
4047 if (repo)
4048 got_repo_close(repo);
4049 return error;
4052 __dead static void
4053 usage_histedit(void)
4055 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4056 getprogname());
4057 exit(1);
4060 #define GOT_HISTEDIT_PICK 'p'
4061 #define GOT_HISTEDIT_EDIT 'e'
4062 #define GOT_HISTEDIT_FOLD 'f'
4063 #define GOT_HISTEDIT_DROP 'd'
4064 #define GOT_HISTEDIT_MESG 'm'
4066 static struct got_histedit_cmd {
4067 unsigned char code;
4068 const char *name;
4069 const char *desc;
4070 } got_histedit_cmds[] = {
4071 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4072 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4073 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4074 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4075 { GOT_HISTEDIT_MESG, "mesg",
4076 "single-line log message for commit above (open editor if empty)" },
4079 struct got_histedit_list_entry {
4080 TAILQ_ENTRY(got_histedit_list_entry) entry;
4081 struct got_object_id *commit_id;
4082 const struct got_histedit_cmd *cmd;
4083 char *logmsg;
4085 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4087 static const struct got_error *
4088 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4089 FILE *f, struct got_repository *repo)
4091 const struct got_error *err = NULL;
4092 char *logmsg = NULL, *id_str = NULL;
4093 struct got_commit_object *commit = NULL;
4094 size_t n;
4096 err = got_object_open_as_commit(&commit, repo, commit_id);
4097 if (err)
4098 goto done;
4100 err = get_short_logmsg(&logmsg, 34, commit);
4101 if (err)
4102 goto done;
4104 err = got_object_id_str(&id_str, commit_id);
4105 if (err)
4106 goto done;
4108 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4109 if (n < 0)
4110 err = got_ferror(f, GOT_ERR_IO);
4111 done:
4112 if (commit)
4113 got_object_commit_close(commit);
4114 free(id_str);
4115 free(logmsg);
4116 return err;
4119 static const struct got_error *
4120 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4121 struct got_repository *repo)
4123 const struct got_error *err = NULL;
4124 struct got_object_qid *qid;
4126 if (SIMPLEQ_EMPTY(commits))
4127 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4129 SIMPLEQ_FOREACH(qid, commits, entry) {
4130 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4131 f, repo);
4132 if (err)
4133 break;
4136 return err;
4139 static const struct got_error *
4140 write_cmd_list(FILE *f)
4142 const struct got_error *err = NULL;
4143 int n, i;
4145 n = fprintf(f, "# Available histedit commands:\n");
4146 if (n < 0)
4147 return got_ferror(f, GOT_ERR_IO);
4149 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4150 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4151 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4152 cmd->desc);
4153 if (n < 0) {
4154 err = got_ferror(f, GOT_ERR_IO);
4155 break;
4158 n = fprintf(f, "# Commits will be processed in order from top to "
4159 "bottom of this file.\n");
4160 if (n < 0)
4161 return got_ferror(f, GOT_ERR_IO);
4162 return err;
4165 static const struct got_error *
4166 histedit_syntax_error(int lineno)
4168 static char msg[42];
4169 int ret;
4171 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4172 lineno);
4173 if (ret == -1 || ret >= sizeof(msg))
4174 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4176 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4179 static const struct got_error *
4180 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4181 char *logmsg, struct got_repository *repo)
4183 const struct got_error *err;
4184 struct got_commit_object *folded_commit = NULL;
4185 char *id_str;
4187 err = got_object_id_str(&id_str, hle->commit_id);
4188 if (err)
4189 return err;
4191 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4192 if (err)
4193 goto done;
4195 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4196 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4197 got_object_commit_get_logmsg(folded_commit)) == -1) {
4198 err = got_error_from_errno("asprintf");
4199 goto done;
4201 done:
4202 if (folded_commit)
4203 got_object_commit_close(folded_commit);
4204 free(id_str);
4205 return err;
4208 static struct got_histedit_list_entry *
4209 get_folded_commits(struct got_histedit_list_entry *hle)
4211 struct got_histedit_list_entry *prev, *folded = NULL;
4213 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4214 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4215 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4216 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4217 folded = prev;
4218 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4221 return folded;
4224 static const struct got_error *
4225 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4226 struct got_repository *repo)
4228 char *logmsg_path = NULL, *id_str = NULL;
4229 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4230 const struct got_error *err = NULL;
4231 struct got_commit_object *commit = NULL;
4232 int fd;
4233 struct got_histedit_list_entry *folded = NULL;
4235 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4236 if (err)
4237 return err;
4239 folded = get_folded_commits(hle);
4240 if (folded) {
4241 while (folded != hle) {
4242 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4243 folded = TAILQ_NEXT(folded, entry);
4244 continue;
4246 err = append_folded_commit_msg(&new_msg, folded,
4247 logmsg, repo);
4248 if (err)
4249 goto done;
4250 free(logmsg);
4251 logmsg = new_msg;
4252 folded = TAILQ_NEXT(folded, entry);
4256 err = got_object_id_str(&id_str, hle->commit_id);
4257 if (err)
4258 goto done;
4259 if (asprintf(&new_msg,
4260 "%s\n# original log message of commit %s: %s",
4261 logmsg ? logmsg : "", id_str,
4262 got_object_commit_get_logmsg(commit)) == -1) {
4263 err = got_error_from_errno("asprintf");
4264 goto done;
4266 free(logmsg);
4267 logmsg = new_msg;
4269 err = got_object_id_str(&id_str, hle->commit_id);
4270 if (err)
4271 goto done;
4273 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4274 if (err)
4275 goto done;
4277 dprintf(fd, logmsg);
4278 close(fd);
4280 err = get_editor(&editor);
4281 if (err)
4282 goto done;
4284 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4285 if (err) {
4286 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4287 goto done;
4288 err = NULL;
4289 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4290 if (hle->logmsg == NULL)
4291 err = got_error_from_errno("strdup");
4293 done:
4294 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4295 err = got_error_from_errno2("unlink", logmsg_path);
4296 free(logmsg_path);
4297 free(logmsg);
4298 free(editor);
4299 if (commit)
4300 got_object_commit_close(commit);
4301 return err;
4304 static const struct got_error *
4305 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4306 FILE *f, struct got_repository *repo)
4308 const struct got_error *err = NULL;
4309 char *line = NULL, *p, *end;
4310 size_t size;
4311 ssize_t len;
4312 int lineno = 0, i;
4313 const struct got_histedit_cmd *cmd;
4314 struct got_object_id *commit_id = NULL;
4315 struct got_histedit_list_entry *hle = NULL;
4317 for (;;) {
4318 len = getline(&line, &size, f);
4319 if (len == -1) {
4320 const struct got_error *getline_err;
4321 if (feof(f))
4322 break;
4323 getline_err = got_error_from_errno("getline");
4324 err = got_ferror(f, getline_err->code);
4325 break;
4327 lineno++;
4328 p = line;
4329 while (isspace((unsigned char)p[0]))
4330 p++;
4331 if (p[0] == '#' || p[0] == '\0') {
4332 free(line);
4333 line = NULL;
4334 continue;
4336 cmd = NULL;
4337 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4338 cmd = &got_histedit_cmds[i];
4339 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4340 isspace((unsigned char)p[strlen(cmd->name)])) {
4341 p += strlen(cmd->name);
4342 break;
4344 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4345 p++;
4346 break;
4349 if (i == nitems(got_histedit_cmds)) {
4350 err = histedit_syntax_error(lineno);
4351 break;
4353 while (isspace((unsigned char)p[0]))
4354 p++;
4355 if (cmd->code == GOT_HISTEDIT_MESG) {
4356 if (hle == NULL || hle->logmsg != NULL) {
4357 err = got_error(GOT_ERR_HISTEDIT_CMD);
4358 break;
4360 if (p[0] == '\0') {
4361 err = histedit_edit_logmsg(hle, repo);
4362 if (err)
4363 break;
4364 } else {
4365 hle->logmsg = strdup(p);
4366 if (hle->logmsg == NULL) {
4367 err = got_error_from_errno("strdup");
4368 break;
4371 free(line);
4372 line = NULL;
4373 continue;
4374 } else {
4375 end = p;
4376 while (end[0] && !isspace((unsigned char)end[0]))
4377 end++;
4378 *end = '\0';
4380 err = got_object_resolve_id_str(&commit_id, repo, p);
4381 if (err) {
4382 /* override error code */
4383 err = histedit_syntax_error(lineno);
4384 break;
4387 hle = malloc(sizeof(*hle));
4388 if (hle == NULL) {
4389 err = got_error_from_errno("malloc");
4390 break;
4392 hle->cmd = cmd;
4393 hle->commit_id = commit_id;
4394 hle->logmsg = NULL;
4395 commit_id = NULL;
4396 free(line);
4397 line = NULL;
4398 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4401 free(line);
4402 free(commit_id);
4403 return err;
4406 static const struct got_error *
4407 histedit_check_script(struct got_histedit_list *histedit_cmds,
4408 struct got_object_id_queue *commits, struct got_repository *repo)
4410 const struct got_error *err = NULL;
4411 struct got_object_qid *qid;
4412 struct got_histedit_list_entry *hle;
4413 static char msg[80];
4414 char *id_str;
4416 if (TAILQ_EMPTY(histedit_cmds))
4417 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4418 "histedit script contains no commands");
4420 SIMPLEQ_FOREACH(qid, commits, entry) {
4421 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4422 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4423 break;
4425 if (hle == NULL) {
4426 err = got_object_id_str(&id_str, qid->id);
4427 if (err)
4428 return err;
4429 snprintf(msg, sizeof(msg),
4430 "commit %s missing from histedit script", id_str);
4431 free(id_str);
4432 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4436 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4437 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4438 "last commit in histedit script cannot be folded");
4440 return NULL;
4443 static const struct got_error *
4444 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4445 const char *path, struct got_object_id_queue *commits,
4446 struct got_repository *repo)
4448 const struct got_error *err = NULL;
4449 char *editor;
4450 FILE *f = NULL;
4452 err = get_editor(&editor);
4453 if (err)
4454 return err;
4456 if (spawn_editor(editor, path) == -1) {
4457 err = got_error_from_errno("failed spawning editor");
4458 goto done;
4461 f = fopen(path, "r");
4462 if (f == NULL) {
4463 err = got_error_from_errno("fopen");
4464 goto done;
4466 err = histedit_parse_list(histedit_cmds, f, repo);
4467 if (err)
4468 goto done;
4470 err = histedit_check_script(histedit_cmds, commits, repo);
4471 done:
4472 if (f && fclose(f) != 0 && err == NULL)
4473 err = got_error_from_errno("fclose");
4474 free(editor);
4475 return err;
4478 static const struct got_error *
4479 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4480 struct got_object_id_queue *, const char *, struct got_repository *);
4482 static const struct got_error *
4483 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4484 struct got_object_id_queue *commits, struct got_repository *repo)
4486 const struct got_error *err;
4487 FILE *f = NULL;
4488 char *path = NULL;
4490 err = got_opentemp_named(&path, &f, "got-histedit");
4491 if (err)
4492 return err;
4494 err = write_cmd_list(f);
4495 if (err)
4496 goto done;
4498 err = histedit_write_commit_list(commits, f, repo);
4499 if (err)
4500 goto done;
4502 if (fclose(f) != 0) {
4503 err = got_error_from_errno("fclose");
4504 goto done;
4506 f = NULL;
4508 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4509 if (err) {
4510 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4511 err->code != GOT_ERR_HISTEDIT_CMD)
4512 goto done;
4513 err = histedit_edit_list_retry(histedit_cmds, err,
4514 commits, path, repo);
4516 done:
4517 if (f && fclose(f) != 0 && err == NULL)
4518 err = got_error_from_errno("fclose");
4519 if (path && unlink(path) != 0 && err == NULL)
4520 err = got_error_from_errno2("unlink", path);
4521 free(path);
4522 return err;
4525 static const struct got_error *
4526 histedit_save_list(struct got_histedit_list *histedit_cmds,
4527 struct got_worktree *worktree, struct got_repository *repo)
4529 const struct got_error *err = NULL;
4530 char *path = NULL;
4531 FILE *f = NULL;
4532 struct got_histedit_list_entry *hle;
4533 struct got_commit_object *commit = NULL;
4535 err = got_worktree_get_histedit_script_path(&path, worktree);
4536 if (err)
4537 return err;
4539 f = fopen(path, "w");
4540 if (f == NULL) {
4541 err = got_error_from_errno2("fopen", path);
4542 goto done;
4544 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4545 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4546 repo);
4547 if (err)
4548 break;
4550 if (hle->logmsg) {
4551 int n = fprintf(f, "%c %s\n",
4552 GOT_HISTEDIT_MESG, hle->logmsg);
4553 if (n < 0) {
4554 err = got_ferror(f, GOT_ERR_IO);
4555 break;
4559 done:
4560 if (f && fclose(f) != 0 && err == NULL)
4561 err = got_error_from_errno("fclose");
4562 free(path);
4563 if (commit)
4564 got_object_commit_close(commit);
4565 return err;
4568 void
4569 histedit_free_list(struct got_histedit_list *histedit_cmds)
4571 struct got_histedit_list_entry *hle;
4573 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4574 TAILQ_REMOVE(histedit_cmds, hle, entry);
4575 free(hle);
4579 static const struct got_error *
4580 histedit_load_list(struct got_histedit_list *histedit_cmds,
4581 const char *path, struct got_repository *repo)
4583 const struct got_error *err = NULL;
4584 FILE *f = NULL;
4586 f = fopen(path, "r");
4587 if (f == NULL) {
4588 err = got_error_from_errno2("fopen", path);
4589 goto done;
4592 err = histedit_parse_list(histedit_cmds, f, repo);
4593 done:
4594 if (f && fclose(f) != 0 && err == NULL)
4595 err = got_error_from_errno("fclose");
4596 return err;
4599 static const struct got_error *
4600 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4601 const struct got_error *edit_err, struct got_object_id_queue *commits,
4602 const char *path, struct got_repository *repo)
4604 const struct got_error *err = NULL, *prev_err = edit_err;
4605 int resp = ' ';
4607 while (resp != 'c' && resp != 'r' && resp != 'a') {
4608 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4609 "or (a)bort: ", getprogname(), prev_err->msg);
4610 resp = getchar();
4611 if (resp == 'c') {
4612 histedit_free_list(histedit_cmds);
4613 err = histedit_run_editor(histedit_cmds, path, commits,
4614 repo);
4615 if (err) {
4616 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4617 err->code != GOT_ERR_HISTEDIT_CMD)
4618 break;
4619 prev_err = err;
4620 resp = ' ';
4621 continue;
4623 break;
4624 } else if (resp == 'r') {
4625 histedit_free_list(histedit_cmds);
4626 err = histedit_edit_script(histedit_cmds,
4627 commits, repo);
4628 if (err) {
4629 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4630 err->code != GOT_ERR_HISTEDIT_CMD)
4631 break;
4632 prev_err = err;
4633 resp = ' ';
4634 continue;
4636 break;
4637 } else if (resp == 'a') {
4638 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4639 break;
4640 } else
4641 printf("invalid response '%c'\n", resp);
4644 return err;
4647 static const struct got_error *
4648 histedit_complete(struct got_worktree *worktree,
4649 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4650 struct got_reference *branch, struct got_repository *repo)
4652 printf("Switching work tree to %s\n",
4653 got_ref_get_symref_target(branch));
4654 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4655 branch, repo);
4658 static const struct got_error *
4659 show_histedit_progress(struct got_commit_object *commit,
4660 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4662 const struct got_error *err;
4663 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4665 err = got_object_id_str(&old_id_str, hle->commit_id);
4666 if (err)
4667 goto done;
4669 if (new_id) {
4670 err = got_object_id_str(&new_id_str, new_id);
4671 if (err)
4672 goto done;
4675 old_id_str[12] = '\0';
4676 if (new_id_str)
4677 new_id_str[12] = '\0';
4679 if (hle->logmsg) {
4680 logmsg = strdup(hle->logmsg);
4681 if (logmsg == NULL) {
4682 err = got_error_from_errno("strdup");
4683 goto done;
4685 trim_logmsg(logmsg, 42);
4686 } else {
4687 err = get_short_logmsg(&logmsg, 42, commit);
4688 if (err)
4689 goto done;
4692 switch (hle->cmd->code) {
4693 case GOT_HISTEDIT_PICK:
4694 case GOT_HISTEDIT_EDIT:
4695 printf("%s -> %s: %s\n", old_id_str,
4696 new_id_str ? new_id_str : "no-op change", logmsg);
4697 break;
4698 case GOT_HISTEDIT_DROP:
4699 case GOT_HISTEDIT_FOLD:
4700 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4701 logmsg);
4702 break;
4703 default:
4704 break;
4707 done:
4708 free(old_id_str);
4709 free(new_id_str);
4710 return err;
4713 static const struct got_error *
4714 histedit_commit(struct got_pathlist_head *merged_paths,
4715 struct got_worktree *worktree, struct got_fileindex *fileindex,
4716 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4717 struct got_repository *repo)
4719 const struct got_error *err;
4720 struct got_commit_object *commit;
4721 struct got_object_id *new_commit_id;
4723 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4724 && hle->logmsg == NULL) {
4725 err = histedit_edit_logmsg(hle, repo);
4726 if (err)
4727 return err;
4730 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4731 if (err)
4732 return err;
4734 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4735 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4736 hle->logmsg, repo);
4737 if (err) {
4738 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4739 goto done;
4740 err = show_histedit_progress(commit, hle, NULL);
4741 } else {
4742 err = show_histedit_progress(commit, hle, new_commit_id);
4743 free(new_commit_id);
4745 done:
4746 got_object_commit_close(commit);
4747 return err;
4750 static const struct got_error *
4751 histedit_skip_commit(struct got_histedit_list_entry *hle,
4752 struct got_worktree *worktree, struct got_repository *repo)
4754 const struct got_error *error;
4755 struct got_commit_object *commit;
4757 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4758 repo);
4759 if (error)
4760 return error;
4762 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4763 if (error)
4764 return error;
4766 error = show_histedit_progress(commit, hle, NULL);
4767 got_object_commit_close(commit);
4768 return error;
4771 static const struct got_error *
4772 cmd_histedit(int argc, char *argv[])
4774 const struct got_error *error = NULL;
4775 struct got_worktree *worktree = NULL;
4776 struct got_fileindex *fileindex = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL;
4779 struct got_reference *branch = NULL;
4780 struct got_reference *tmp_branch = NULL;
4781 struct got_object_id *resume_commit_id = NULL;
4782 struct got_object_id *base_commit_id = NULL;
4783 struct got_object_id *head_commit_id = NULL;
4784 struct got_commit_object *commit = NULL;
4785 int ch, rebase_in_progress = 0, did_something;
4786 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4787 const char *edit_script_path = NULL;
4788 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4789 struct got_object_id_queue commits;
4790 struct got_pathlist_head merged_paths;
4791 const struct got_object_id_queue *parent_ids;
4792 struct got_object_qid *pid;
4793 struct got_histedit_list histedit_cmds;
4794 struct got_histedit_list_entry *hle;
4796 SIMPLEQ_INIT(&commits);
4797 TAILQ_INIT(&histedit_cmds);
4798 TAILQ_INIT(&merged_paths);
4800 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4801 switch (ch) {
4802 case 'a':
4803 abort_edit = 1;
4804 break;
4805 case 'c':
4806 continue_edit = 1;
4807 break;
4808 case 'F':
4809 edit_script_path = optarg;
4810 break;
4811 default:
4812 usage_histedit();
4813 /* NOTREACHED */
4817 argc -= optind;
4818 argv += optind;
4820 #ifndef PROFILE
4821 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4822 "unveil", NULL) == -1)
4823 err(1, "pledge");
4824 #endif
4825 if (abort_edit && continue_edit)
4826 usage_histedit();
4827 if (argc != 0)
4828 usage_histedit();
4831 * This command cannot apply unveil(2) in all cases because the
4832 * user may choose to run an editor to edit the histedit script
4833 * and to edit individual commit log messages.
4834 * unveil(2) traverses exec(2); if an editor is used we have to
4835 * apply unveil after edit script and log messages have been written.
4836 * XXX TODO: Make use of unveil(2) where possible.
4839 cwd = getcwd(NULL, 0);
4840 if (cwd == NULL) {
4841 error = got_error_from_errno("getcwd");
4842 goto done;
4844 error = got_worktree_open(&worktree, cwd);
4845 if (error)
4846 goto done;
4848 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4849 if (error != NULL)
4850 goto done;
4852 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4853 if (error)
4854 goto done;
4855 if (rebase_in_progress) {
4856 error = got_error(GOT_ERR_REBASING);
4857 goto done;
4860 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4861 if (error)
4862 goto done;
4864 if (edit_in_progress && abort_edit) {
4865 error = got_worktree_histedit_continue(&resume_commit_id,
4866 &tmp_branch, &branch, &base_commit_id, &fileindex,
4867 worktree, repo);
4868 if (error)
4869 goto done;
4870 printf("Switching work tree to %s\n",
4871 got_ref_get_symref_target(branch));
4872 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4873 branch, base_commit_id, update_progress, &did_something);
4874 if (error)
4875 goto done;
4876 printf("Histedit of %s aborted\n",
4877 got_ref_get_symref_target(branch));
4878 goto done; /* nothing else to do */
4879 } else if (abort_edit) {
4880 error = got_error(GOT_ERR_NOT_HISTEDIT);
4881 goto done;
4884 if (continue_edit) {
4885 char *path;
4887 if (!edit_in_progress) {
4888 error = got_error(GOT_ERR_NOT_HISTEDIT);
4889 goto done;
4892 error = got_worktree_get_histedit_script_path(&path, worktree);
4893 if (error)
4894 goto done;
4896 error = histedit_load_list(&histedit_cmds, path, repo);
4897 free(path);
4898 if (error)
4899 goto done;
4901 error = got_worktree_histedit_continue(&resume_commit_id,
4902 &tmp_branch, &branch, &base_commit_id, &fileindex,
4903 worktree, repo);
4904 if (error)
4905 goto done;
4907 error = got_ref_resolve(&head_commit_id, repo, branch);
4908 if (error)
4909 goto done;
4911 error = got_object_open_as_commit(&commit, repo,
4912 head_commit_id);
4913 if (error)
4914 goto done;
4915 parent_ids = got_object_commit_get_parent_ids(commit);
4916 pid = SIMPLEQ_FIRST(parent_ids);
4917 if (pid == NULL) {
4918 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4919 goto done;
4921 error = collect_commits(&commits, head_commit_id, pid->id,
4922 base_commit_id, got_worktree_get_path_prefix(worktree),
4923 GOT_ERR_HISTEDIT_PATH, repo);
4924 got_object_commit_close(commit);
4925 commit = NULL;
4926 if (error)
4927 goto done;
4928 } else {
4929 if (edit_in_progress) {
4930 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4931 goto done;
4934 error = got_ref_open(&branch, repo,
4935 got_worktree_get_head_ref_name(worktree), 0);
4936 if (error != NULL)
4937 goto done;
4939 error = got_ref_resolve(&head_commit_id, repo, branch);
4940 got_ref_close(branch);
4941 branch = NULL;
4942 if (error)
4943 goto done;
4945 error = got_object_open_as_commit(&commit, repo,
4946 head_commit_id);
4947 if (error)
4948 goto done;
4949 parent_ids = got_object_commit_get_parent_ids(commit);
4950 pid = SIMPLEQ_FIRST(parent_ids);
4951 if (pid == NULL) {
4952 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4953 goto done;
4955 error = collect_commits(&commits, head_commit_id, pid->id,
4956 got_worktree_get_base_commit_id(worktree),
4957 got_worktree_get_path_prefix(worktree),
4958 GOT_ERR_HISTEDIT_PATH, repo);
4959 got_object_commit_close(commit);
4960 commit = NULL;
4961 if (error)
4962 goto done;
4964 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4965 &base_commit_id, &fileindex, worktree, repo);
4966 if (error)
4967 goto done;
4969 if (edit_script_path) {
4970 error = histedit_load_list(&histedit_cmds,
4971 edit_script_path, repo);
4972 if (error) {
4973 got_worktree_histedit_abort(worktree, fileindex,
4974 repo, branch, base_commit_id,
4975 update_progress, &did_something);
4976 goto done;
4978 } else {
4979 error = histedit_edit_script(&histedit_cmds, &commits,
4980 repo);
4981 if (error) {
4982 got_worktree_histedit_abort(worktree, fileindex,
4983 repo, branch, base_commit_id,
4984 update_progress, &did_something);
4985 goto done;
4990 error = histedit_save_list(&histedit_cmds, worktree,
4991 repo);
4992 if (error) {
4993 got_worktree_histedit_abort(worktree, fileindex,
4994 repo, branch, base_commit_id,
4995 update_progress, &did_something);
4996 goto done;
5001 error = histedit_check_script(&histedit_cmds, &commits, repo);
5002 if (error)
5003 goto done;
5005 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5006 if (resume_commit_id) {
5007 if (got_object_id_cmp(hle->commit_id,
5008 resume_commit_id) != 0)
5009 continue;
5011 resume_commit_id = NULL;
5012 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5013 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5014 error = histedit_skip_commit(hle, worktree,
5015 repo);
5016 } else {
5017 error = histedit_commit(NULL, worktree,
5018 fileindex, tmp_branch, hle, repo);
5020 if (error)
5021 goto done;
5022 continue;
5025 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5026 error = histedit_skip_commit(hle, worktree, repo);
5027 if (error)
5028 goto done;
5029 continue;
5032 error = got_object_open_as_commit(&commit, repo,
5033 hle->commit_id);
5034 if (error)
5035 goto done;
5036 parent_ids = got_object_commit_get_parent_ids(commit);
5037 pid = SIMPLEQ_FIRST(parent_ids);
5039 error = got_worktree_histedit_merge_files(&merged_paths,
5040 worktree, fileindex, pid->id, hle->commit_id, repo,
5041 rebase_progress, &rebase_status, check_cancelled, NULL);
5042 if (error)
5043 goto done;
5044 got_object_commit_close(commit);
5045 commit = NULL;
5047 if (rebase_status == GOT_STATUS_CONFLICT) {
5048 got_worktree_rebase_pathlist_free(&merged_paths);
5049 break;
5052 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5053 char *id_str;
5054 error = got_object_id_str(&id_str, hle->commit_id);
5055 if (error)
5056 goto done;
5057 printf("Stopping histedit for amending commit %s\n",
5058 id_str);
5059 free(id_str);
5060 got_worktree_rebase_pathlist_free(&merged_paths);
5061 error = got_worktree_histedit_postpone(worktree,
5062 fileindex);
5063 goto done;
5066 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5067 error = histedit_skip_commit(hle, worktree, repo);
5068 if (error)
5069 goto done;
5070 continue;
5073 error = histedit_commit(&merged_paths, worktree, fileindex,
5074 tmp_branch, hle, repo);
5075 got_worktree_rebase_pathlist_free(&merged_paths);
5076 if (error)
5077 goto done;
5080 if (rebase_status == GOT_STATUS_CONFLICT) {
5081 error = got_worktree_histedit_postpone(worktree, fileindex);
5082 if (error)
5083 goto done;
5084 error = got_error_msg(GOT_ERR_CONFLICTS,
5085 "conflicts must be resolved before rebasing can continue");
5086 } else
5087 error = histedit_complete(worktree, fileindex, tmp_branch,
5088 branch, repo);
5089 done:
5090 got_object_id_queue_free(&commits);
5091 histedit_free_list(&histedit_cmds);
5092 free(head_commit_id);
5093 free(base_commit_id);
5094 free(resume_commit_id);
5095 if (commit)
5096 got_object_commit_close(commit);
5097 if (branch)
5098 got_ref_close(branch);
5099 if (tmp_branch)
5100 got_ref_close(tmp_branch);
5101 if (worktree)
5102 got_worktree_close(worktree);
5103 if (repo)
5104 got_repo_close(repo);
5105 return error;