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_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
719 if (err)
720 goto done;
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
723 if (err)
724 goto done;
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
727 if (err)
728 goto done;
730 for (;;) {
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
733 if (err) {
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
735 err = NULL;
736 break;
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
738 break;
739 err = got_commit_graph_fetch_commits(graph, 1,
740 repo);
741 if (err)
742 break;
745 if (id) {
746 if (got_object_id_cmp(id, commit_id) == 0) {
747 is_same_branch = 1;
748 break;
752 done:
753 if (graph)
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
758 return err;
761 static const struct got_error *
762 resolve_commit_arg(struct got_object_id **commit_id,
763 const char *commit_id_arg, struct got_repository *repo)
765 const struct got_error *err;
766 struct got_reference *ref;
768 err = got_ref_open(&ref, repo, commit_id_arg, 0);
769 if (err == NULL) {
770 err = got_ref_resolve(commit_id, repo, ref);
771 got_ref_close(ref);
772 } else {
773 if (err->code != GOT_ERR_NOT_REF)
774 return err;
775 err = got_repo_match_object_id_prefix(commit_id,
776 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
778 return err;
781 static const struct got_error *
782 cmd_checkout(int argc, char *argv[])
784 const struct got_error *error = NULL;
785 struct got_repository *repo = NULL;
786 struct got_reference *head_ref = NULL;
787 struct got_worktree *worktree = NULL;
788 char *repo_path = NULL;
789 char *worktree_path = NULL;
790 const char *path_prefix = "";
791 const char *branch_name = GOT_REF_HEAD;
792 char *commit_id_str = NULL;
793 int ch, same_path_prefix;
795 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
796 switch (ch) {
797 case 'b':
798 branch_name = optarg;
799 break;
800 case 'c':
801 commit_id_str = strdup(optarg);
802 if (commit_id_str == NULL)
803 return got_error_from_errno("strdup");
804 break;
805 case 'p':
806 path_prefix = optarg;
807 break;
808 default:
809 usage_checkout();
810 /* NOTREACHED */
814 argc -= optind;
815 argv += optind;
817 #ifndef PROFILE
818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
819 "unveil", NULL) == -1)
820 err(1, "pledge");
821 #endif
822 if (argc == 1) {
823 char *cwd, *base, *dotgit;
824 repo_path = realpath(argv[0], NULL);
825 if (repo_path == NULL)
826 return got_error_from_errno2("realpath", argv[0]);
827 cwd = getcwd(NULL, 0);
828 if (cwd == NULL) {
829 error = got_error_from_errno("getcwd");
830 goto done;
832 if (path_prefix[0]) {
833 base = basename(path_prefix);
834 if (base == NULL) {
835 error = got_error_from_errno2("basename",
836 path_prefix);
837 goto done;
839 } else {
840 base = basename(repo_path);
841 if (base == NULL) {
842 error = got_error_from_errno2("basename",
843 repo_path);
844 goto done;
847 dotgit = strstr(base, ".git");
848 if (dotgit)
849 *dotgit = '\0';
850 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
851 error = got_error_from_errno("asprintf");
852 free(cwd);
853 goto done;
855 free(cwd);
856 } else if (argc == 2) {
857 repo_path = realpath(argv[0], NULL);
858 if (repo_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[0]);
860 goto done;
862 worktree_path = realpath(argv[1], NULL);
863 if (worktree_path == NULL) {
864 if (errno != ENOENT) {
865 error = got_error_from_errno2("realpath",
866 argv[1]);
867 goto done;
869 worktree_path = strdup(argv[1]);
870 if (worktree_path == NULL) {
871 error = got_error_from_errno("strdup");
872 goto done;
875 } else
876 usage_checkout();
878 got_path_strip_trailing_slashes(repo_path);
879 got_path_strip_trailing_slashes(worktree_path);
881 error = got_repo_open(&repo, repo_path);
882 if (error != NULL)
883 goto done;
885 /* Pre-create work tree path for unveil(2) */
886 error = got_path_mkdir(worktree_path);
887 if (error) {
888 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
889 goto done;
890 if (!got_path_dir_is_empty(worktree_path)) {
891 error = got_error_path(worktree_path,
892 GOT_ERR_DIR_NOT_EMPTY);
893 goto done;
897 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
898 if (error)
899 goto done;
901 error = got_ref_open(&head_ref, repo, branch_name, 0);
902 if (error != NULL)
903 goto done;
905 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
906 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
907 goto done;
909 error = got_worktree_open(&worktree, worktree_path);
910 if (error != NULL)
911 goto done;
913 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
914 path_prefix);
915 if (error != NULL)
916 goto done;
917 if (!same_path_prefix) {
918 error = got_error(GOT_ERR_PATH_PREFIX);
919 goto done;
922 if (commit_id_str) {
923 struct got_object_id *commit_id;
924 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
925 if (error)
926 goto done;
927 error = check_linear_ancestry(commit_id,
928 got_worktree_get_base_commit_id(worktree), repo);
929 if (error != NULL) {
930 free(commit_id);
931 goto done;
933 error = check_same_branch(commit_id, head_ref, repo);
934 if (error)
935 goto done;
936 error = got_worktree_set_base_commit_id(worktree, repo,
937 commit_id);
938 free(commit_id);
939 if (error)
940 goto done;
943 error = got_worktree_checkout_files(worktree, "", repo,
944 checkout_progress, worktree_path, check_cancelled, NULL);
945 if (error != NULL)
946 goto done;
948 printf("Now shut up and hack\n");
950 done:
951 free(commit_id_str);
952 free(repo_path);
953 free(worktree_path);
954 return error;
957 __dead static void
958 usage_update(void)
960 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
961 getprogname());
962 exit(1);
965 static const struct got_error *
966 update_progress(void *arg, unsigned char status, const char *path)
968 int *did_something = arg;
970 if (status == GOT_STATUS_EXISTS)
971 return NULL;
973 *did_something = 1;
975 /* Base commit bump happens silently. */
976 if (status == GOT_STATUS_BUMP_BASE)
977 return NULL;
979 while (path[0] == '/')
980 path++;
981 printf("%c %s\n", status, path);
982 return NULL;
985 static const struct got_error *
986 switch_head_ref(struct got_reference *head_ref,
987 struct got_object_id *commit_id, struct got_worktree *worktree,
988 struct got_repository *repo)
990 const struct got_error *err = NULL;
991 char *base_id_str;
992 int ref_has_moved = 0;
994 /* Trivial case: switching between two different references. */
995 if (strcmp(got_ref_get_name(head_ref),
996 got_worktree_get_head_ref_name(worktree)) != 0) {
997 printf("Switching work tree from %s to %s\n",
998 got_worktree_get_head_ref_name(worktree),
999 got_ref_get_name(head_ref));
1000 return got_worktree_set_head_ref(worktree, head_ref);
1003 err = check_linear_ancestry(commit_id,
1004 got_worktree_get_base_commit_id(worktree), repo);
1005 if (err) {
1006 if (err->code != GOT_ERR_ANCESTRY)
1007 return err;
1008 ref_has_moved = 1;
1010 if (!ref_has_moved)
1011 return NULL;
1013 /* Switching to a rebased branch with the same reference name. */
1014 err = got_object_id_str(&base_id_str,
1015 got_worktree_get_base_commit_id(worktree));
1016 if (err)
1017 return err;
1018 printf("Reference %s now points at a different branch\n",
1019 got_worktree_get_head_ref_name(worktree));
1020 printf("Switching work tree from %s to %s\n", base_id_str,
1021 got_worktree_get_head_ref_name(worktree));
1022 return NULL;
1025 static const struct got_error *
1026 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1028 const struct got_error *err;
1029 int in_progress;
1031 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1032 if (err)
1033 return err;
1034 if (in_progress)
1035 return got_error(GOT_ERR_REBASING);
1037 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1038 if (err)
1039 return err;
1040 if (in_progress)
1041 return got_error(GOT_ERR_HISTEDIT_BUSY);
1043 return NULL;
1046 static const struct got_error *
1047 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1048 char *argv[], struct got_worktree *worktree)
1050 const struct got_error *err;
1051 char *path;
1052 int i;
1054 if (argc == 0) {
1055 path = strdup("");
1056 if (path == NULL)
1057 return got_error_from_errno("strdup");
1058 return got_pathlist_append(NULL, paths, path, NULL);
1061 for (i = 0; i < argc; i++) {
1062 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1063 if (err)
1064 break;
1065 err = got_pathlist_append(NULL, paths, path, NULL);
1066 if (err) {
1067 free(path);
1068 break;
1072 return err;
1075 static const struct got_error *
1076 cmd_update(int argc, char *argv[])
1078 const struct got_error *error = NULL;
1079 struct got_repository *repo = NULL;
1080 struct got_worktree *worktree = NULL;
1081 char *worktree_path = NULL, *path = NULL;
1082 struct got_object_id *commit_id = NULL;
1083 char *commit_id_str = NULL;
1084 const char *branch_name = NULL;
1085 struct got_reference *head_ref = NULL;
1086 int ch, did_something = 0;
1088 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1089 switch (ch) {
1090 case 'b':
1091 branch_name = optarg;
1092 break;
1093 case 'c':
1094 commit_id_str = strdup(optarg);
1095 if (commit_id_str == NULL)
1096 return got_error_from_errno("strdup");
1097 break;
1098 default:
1099 usage_update();
1100 /* NOTREACHED */
1104 argc -= optind;
1105 argv += optind;
1107 #ifndef PROFILE
1108 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1109 "unveil", NULL) == -1)
1110 err(1, "pledge");
1111 #endif
1112 worktree_path = getcwd(NULL, 0);
1113 if (worktree_path == NULL) {
1114 error = got_error_from_errno("getcwd");
1115 goto done;
1117 error = got_worktree_open(&worktree, worktree_path);
1118 if (error)
1119 goto done;
1121 error = check_rebase_or_histedit_in_progress(worktree);
1122 if (error)
1123 goto done;
1125 if (argc == 0) {
1126 path = strdup("");
1127 if (path == NULL) {
1128 error = got_error_from_errno("strdup");
1129 goto done;
1131 } else if (argc == 1) {
1132 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1133 if (error)
1134 goto done;
1135 } else
1136 usage_update();
1138 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1139 if (error != NULL)
1140 goto done;
1142 error = apply_unveil(got_repo_get_path(repo), 0,
1143 got_worktree_get_root_path(worktree));
1144 if (error)
1145 goto done;
1147 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1148 got_worktree_get_head_ref_name(worktree), 0);
1149 if (error != NULL)
1150 goto done;
1151 if (commit_id_str == NULL) {
1152 error = got_ref_resolve(&commit_id, repo, head_ref);
1153 if (error != NULL)
1154 goto done;
1155 error = got_object_id_str(&commit_id_str, commit_id);
1156 if (error != NULL)
1157 goto done;
1158 } else {
1159 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1160 free(commit_id_str);
1161 commit_id_str = NULL;
1162 if (error)
1163 goto done;
1164 error = got_object_id_str(&commit_id_str, commit_id);
1165 if (error)
1166 goto done;
1169 if (branch_name) {
1170 struct got_object_id *head_commit_id;
1171 if (strlen(path) != 0) {
1172 fprintf(stderr, "%s: switching between branches "
1173 "requires that the entire work tree "
1174 "gets updated, not just '%s'\n",
1175 getprogname(), path);
1176 error = got_error(GOT_ERR_BAD_PATH);
1177 goto done;
1179 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1180 if (error)
1181 goto done;
1182 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1183 free(head_commit_id);
1184 if (error != NULL)
1185 goto done;
1186 error = check_same_branch(commit_id, head_ref, repo);
1187 if (error)
1188 goto done;
1189 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1190 if (error)
1191 goto done;
1192 } else {
1193 error = check_linear_ancestry(commit_id,
1194 got_worktree_get_base_commit_id(worktree), repo);
1195 if (error != NULL) {
1196 if (error->code == GOT_ERR_ANCESTRY)
1197 error = got_error(GOT_ERR_BRANCH_MOVED);
1198 goto done;
1200 error = check_same_branch(commit_id, head_ref, repo);
1201 if (error)
1202 goto done;
1205 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1206 commit_id) != 0) {
1207 error = got_worktree_set_base_commit_id(worktree, repo,
1208 commit_id);
1209 if (error)
1210 goto done;
1213 error = got_worktree_checkout_files(worktree, path, repo,
1214 update_progress, &did_something, check_cancelled, NULL);
1215 if (error != NULL)
1216 goto done;
1218 if (did_something)
1219 printf("Updated to commit %s\n", commit_id_str);
1220 else
1221 printf("Already up-to-date\n");
1222 done:
1223 free(worktree_path);
1224 free(path);
1225 free(commit_id);
1226 free(commit_id_str);
1227 return error;
1230 static const struct got_error *
1231 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1232 int diff_context, struct got_repository *repo)
1234 const struct got_error *err = NULL;
1235 struct got_tree_object *tree1 = NULL, *tree2;
1236 struct got_object_qid *qid;
1237 char *id_str1 = NULL, *id_str2;
1238 struct got_diff_blob_output_unidiff_arg arg;
1240 err = got_object_open_as_tree(&tree2, repo,
1241 got_object_commit_get_tree_id(commit));
1242 if (err)
1243 return err;
1245 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1246 if (qid != NULL) {
1247 struct got_commit_object *pcommit;
1249 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1250 if (err)
1251 return err;
1253 err = got_object_open_as_tree(&tree1, repo,
1254 got_object_commit_get_tree_id(pcommit));
1255 got_object_commit_close(pcommit);
1256 if (err)
1257 return err;
1259 err = got_object_id_str(&id_str1, qid->id);
1260 if (err)
1261 return err;
1264 err = got_object_id_str(&id_str2, id);
1265 if (err)
1266 goto done;
1268 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1269 arg.diff_context = diff_context;
1270 arg.outfile = stdout;
1271 err = got_diff_tree(tree1, tree2, "", "", repo,
1272 got_diff_blob_output_unidiff, &arg);
1273 done:
1274 if (tree1)
1275 got_object_tree_close(tree1);
1276 got_object_tree_close(tree2);
1277 free(id_str1);
1278 free(id_str2);
1279 return err;
1282 static char *
1283 get_datestr(time_t *time, char *datebuf)
1285 char *p, *s = ctime_r(time, datebuf);
1286 p = strchr(s, '\n');
1287 if (p)
1288 *p = '\0';
1289 return s;
1292 static const struct got_error *
1293 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1294 struct got_repository *repo, int show_patch, int diff_context,
1295 struct got_reflist_head *refs)
1297 const struct got_error *err = NULL;
1298 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1299 char datebuf[26];
1300 time_t committer_time;
1301 const char *author, *committer;
1302 char *refs_str = NULL;
1303 struct got_reflist_entry *re;
1305 SIMPLEQ_FOREACH(re, refs, entry) {
1306 char *s;
1307 const char *name;
1308 if (got_object_id_cmp(re->id, id) != 0)
1309 continue;
1310 name = got_ref_get_name(re->ref);
1311 if (strcmp(name, GOT_REF_HEAD) == 0)
1312 continue;
1313 if (strncmp(name, "refs/", 5) == 0)
1314 name += 5;
1315 if (strncmp(name, "got/", 4) == 0)
1316 continue;
1317 if (strncmp(name, "heads/", 6) == 0)
1318 name += 6;
1319 if (strncmp(name, "remotes/", 8) == 0)
1320 name += 8;
1321 s = refs_str;
1322 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1323 name) == -1) {
1324 err = got_error_from_errno("asprintf");
1325 free(s);
1326 break;
1328 free(s);
1330 err = got_object_id_str(&id_str, id);
1331 if (err)
1332 return err;
1334 printf("-----------------------------------------------\n");
1335 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1336 refs_str ? refs_str : "", refs_str ? ")" : "");
1337 free(id_str);
1338 id_str = NULL;
1339 free(refs_str);
1340 refs_str = NULL;
1341 printf("from: %s\n", got_object_commit_get_author(commit));
1342 committer_time = got_object_commit_get_committer_time(commit);
1343 datestr = get_datestr(&committer_time, datebuf);
1344 printf("date: %s UTC\n", datestr);
1345 author = got_object_commit_get_author(commit);
1346 committer = got_object_commit_get_committer(commit);
1347 if (strcmp(author, committer) != 0)
1348 printf("via: %s\n", committer);
1349 if (got_object_commit_get_nparents(commit) > 1) {
1350 const struct got_object_id_queue *parent_ids;
1351 struct got_object_qid *qid;
1352 int n = 1;
1353 parent_ids = got_object_commit_get_parent_ids(commit);
1354 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1355 err = got_object_id_str(&id_str, qid->id);
1356 if (err)
1357 return err;
1358 printf("parent %d: %s\n", n++, id_str);
1359 free(id_str);
1363 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1364 if (logmsg0 == NULL)
1365 return got_error_from_errno("strdup");
1367 logmsg = logmsg0;
1368 do {
1369 line = strsep(&logmsg, "\n");
1370 if (line)
1371 printf(" %s\n", line);
1372 } while (line);
1373 free(logmsg0);
1375 if (show_patch) {
1376 err = print_patch(commit, id, diff_context, repo);
1377 if (err == 0)
1378 printf("\n");
1381 if (fflush(stdout) != 0 && err == NULL)
1382 err = got_error_from_errno("fflush");
1383 return err;
1386 static const struct got_error *
1387 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1388 char *path, int show_patch, int diff_context, int limit,
1389 int first_parent_traversal, struct got_reflist_head *refs)
1391 const struct got_error *err;
1392 struct got_commit_graph *graph;
1394 err = got_commit_graph_open(&graph, root_id, path,
1395 first_parent_traversal, repo);
1396 if (err)
1397 return err;
1398 err = got_commit_graph_iter_start(graph, root_id, repo);
1399 if (err)
1400 goto done;
1401 for (;;) {
1402 struct got_commit_object *commit;
1403 struct got_object_id *id;
1405 if (sigint_received || sigpipe_received)
1406 break;
1408 err = got_commit_graph_iter_next(&id, graph);
1409 if (err) {
1410 if (err->code == GOT_ERR_ITER_COMPLETED) {
1411 err = NULL;
1412 break;
1414 if (err->code != GOT_ERR_ITER_NEED_MORE)
1415 break;
1416 err = got_commit_graph_fetch_commits(graph, 1, repo);
1417 if (err)
1418 break;
1419 else
1420 continue;
1422 if (id == NULL)
1423 break;
1425 err = got_object_open_as_commit(&commit, repo, id);
1426 if (err)
1427 break;
1428 err = print_commit(commit, id, repo, show_patch, diff_context,
1429 refs);
1430 got_object_commit_close(commit);
1431 if (err || (limit && --limit == 0))
1432 break;
1434 done:
1435 got_commit_graph_close(graph);
1436 return err;
1439 __dead static void
1440 usage_log(void)
1442 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1443 "[-r repository-path] [path]\n", getprogname());
1444 exit(1);
1447 static const struct got_error *
1448 cmd_log(int argc, char *argv[])
1450 const struct got_error *error;
1451 struct got_repository *repo = NULL;
1452 struct got_worktree *worktree = NULL;
1453 struct got_commit_object *commit = NULL;
1454 struct got_object_id *id = NULL;
1455 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1456 char *start_commit = NULL;
1457 int diff_context = 3, ch;
1458 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1459 const char *errstr;
1460 struct got_reflist_head refs;
1462 SIMPLEQ_INIT(&refs);
1464 #ifndef PROFILE
1465 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1466 NULL)
1467 == -1)
1468 err(1, "pledge");
1469 #endif
1471 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1472 switch (ch) {
1473 case 'p':
1474 show_patch = 1;
1475 break;
1476 case 'c':
1477 start_commit = optarg;
1478 break;
1479 case 'C':
1480 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1481 &errstr);
1482 if (errstr != NULL)
1483 err(1, "-C option %s", errstr);
1484 break;
1485 case 'l':
1486 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1487 if (errstr != NULL)
1488 err(1, "-l option %s", errstr);
1489 break;
1490 case 'f':
1491 first_parent_traversal = 1;
1492 break;
1493 case 'r':
1494 repo_path = realpath(optarg, NULL);
1495 if (repo_path == NULL)
1496 err(1, "-r option");
1497 got_path_strip_trailing_slashes(repo_path);
1498 break;
1499 default:
1500 usage_log();
1501 /* NOTREACHED */
1505 argc -= optind;
1506 argv += optind;
1508 cwd = getcwd(NULL, 0);
1509 if (cwd == NULL) {
1510 error = got_error_from_errno("getcwd");
1511 goto done;
1514 error = got_worktree_open(&worktree, cwd);
1515 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1516 goto done;
1517 error = NULL;
1519 if (argc == 0) {
1520 path = strdup("");
1521 if (path == NULL) {
1522 error = got_error_from_errno("strdup");
1523 goto done;
1525 } else if (argc == 1) {
1526 if (worktree) {
1527 error = got_worktree_resolve_path(&path, worktree,
1528 argv[0]);
1529 if (error)
1530 goto done;
1531 } else {
1532 path = strdup(argv[0]);
1533 if (path == NULL) {
1534 error = got_error_from_errno("strdup");
1535 goto done;
1538 } else
1539 usage_log();
1541 if (repo_path == NULL) {
1542 repo_path = worktree ?
1543 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1545 if (repo_path == NULL) {
1546 error = got_error_from_errno("strdup");
1547 goto done;
1550 error = got_repo_open(&repo, repo_path);
1551 if (error != NULL)
1552 goto done;
1554 error = apply_unveil(got_repo_get_path(repo), 1,
1555 worktree ? got_worktree_get_root_path(worktree) : NULL);
1556 if (error)
1557 goto done;
1559 if (start_commit == NULL) {
1560 struct got_reference *head_ref;
1561 error = got_ref_open(&head_ref, repo,
1562 worktree ? got_worktree_get_head_ref_name(worktree)
1563 : GOT_REF_HEAD, 0);
1564 if (error != NULL)
1565 return error;
1566 error = got_ref_resolve(&id, repo, head_ref);
1567 got_ref_close(head_ref);
1568 if (error != NULL)
1569 return error;
1570 error = got_object_open_as_commit(&commit, repo, id);
1571 } else {
1572 struct got_reference *ref;
1573 error = got_ref_open(&ref, repo, start_commit, 0);
1574 if (error == NULL) {
1575 int obj_type;
1576 error = got_ref_resolve(&id, repo, ref);
1577 got_ref_close(ref);
1578 if (error != NULL)
1579 goto done;
1580 error = got_object_get_type(&obj_type, repo, id);
1581 if (error != NULL)
1582 goto done;
1583 if (obj_type == GOT_OBJ_TYPE_TAG) {
1584 struct got_tag_object *tag;
1585 error = got_object_open_as_tag(&tag, repo, id);
1586 if (error != NULL)
1587 goto done;
1588 if (got_object_tag_get_object_type(tag) !=
1589 GOT_OBJ_TYPE_COMMIT) {
1590 got_object_tag_close(tag);
1591 error = got_error(GOT_ERR_OBJ_TYPE);
1592 goto done;
1594 free(id);
1595 id = got_object_id_dup(
1596 got_object_tag_get_object_id(tag));
1597 if (id == NULL)
1598 error = got_error_from_errno(
1599 "got_object_id_dup");
1600 got_object_tag_close(tag);
1601 if (error)
1602 goto done;
1603 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1604 error = got_error(GOT_ERR_OBJ_TYPE);
1605 goto done;
1607 error = got_object_open_as_commit(&commit, repo, id);
1608 if (error != NULL)
1609 goto done;
1611 if (commit == NULL) {
1612 error = got_repo_match_object_id_prefix(&id,
1613 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1614 if (error != NULL)
1615 return error;
1618 if (error != NULL)
1619 goto done;
1621 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1622 if (error != NULL)
1623 goto done;
1624 if (in_repo_path) {
1625 free(path);
1626 path = in_repo_path;
1629 error = got_ref_list(&refs, repo);
1630 if (error)
1631 goto done;
1633 error = print_commits(id, repo, path, show_patch,
1634 diff_context, limit, first_parent_traversal, &refs);
1635 done:
1636 free(path);
1637 free(repo_path);
1638 free(cwd);
1639 free(id);
1640 if (worktree)
1641 got_worktree_close(worktree);
1642 if (repo) {
1643 const struct got_error *repo_error;
1644 repo_error = got_repo_close(repo);
1645 if (error == NULL)
1646 error = repo_error;
1648 got_ref_list_free(&refs);
1649 return error;
1652 __dead static void
1653 usage_diff(void)
1655 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1656 "[object1 object2 | path]\n", getprogname());
1657 exit(1);
1660 struct print_diff_arg {
1661 struct got_repository *repo;
1662 struct got_worktree *worktree;
1663 int diff_context;
1664 const char *id_str;
1665 int header_shown;
1668 static const struct got_error *
1669 print_diff(void *arg, unsigned char status, const char *path,
1670 struct got_object_id *blob_id, struct got_object_id *commit_id)
1672 struct print_diff_arg *a = arg;
1673 const struct got_error *err = NULL;
1674 struct got_blob_object *blob1 = NULL;
1675 FILE *f2 = NULL;
1676 char *abspath = NULL;
1677 struct stat sb;
1679 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1680 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1681 return NULL;
1683 if (!a->header_shown) {
1684 printf("diff %s %s\n", a->id_str,
1685 got_worktree_get_root_path(a->worktree));
1686 a->header_shown = 1;
1689 if (status != GOT_STATUS_ADD) {
1690 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1691 if (err)
1692 goto done;
1696 if (status != GOT_STATUS_DELETE) {
1697 if (asprintf(&abspath, "%s/%s",
1698 got_worktree_get_root_path(a->worktree), path) == -1) {
1699 err = got_error_from_errno("asprintf");
1700 goto done;
1703 f2 = fopen(abspath, "r");
1704 if (f2 == NULL) {
1705 err = got_error_from_errno2("fopen", abspath);
1706 goto done;
1708 if (lstat(abspath, &sb) == -1) {
1709 err = got_error_from_errno2("lstat", abspath);
1710 goto done;
1712 } else
1713 sb.st_size = 0;
1715 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1716 stdout);
1717 done:
1718 if (blob1)
1719 got_object_blob_close(blob1);
1720 if (f2 && fclose(f2) != 0 && err == NULL)
1721 err = got_error_from_errno("fclose");
1722 free(abspath);
1723 return err;
1726 static const struct got_error *
1727 cmd_diff(int argc, char *argv[])
1729 const struct got_error *error;
1730 struct got_repository *repo = NULL;
1731 struct got_worktree *worktree = NULL;
1732 char *cwd = NULL, *repo_path = NULL;
1733 struct got_object_id *id1 = NULL, *id2 = NULL;
1734 const char *id_str1 = NULL, *id_str2 = NULL;
1735 char *label1 = NULL, *label2 = NULL;
1736 int type1, type2;
1737 int diff_context = 3, ch;
1738 const char *errstr;
1739 char *path = NULL;
1741 #ifndef PROFILE
1742 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1743 NULL) == -1)
1744 err(1, "pledge");
1745 #endif
1747 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1748 switch (ch) {
1749 case 'C':
1750 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1751 if (errstr != NULL)
1752 err(1, "-C option %s", errstr);
1753 break;
1754 case 'r':
1755 repo_path = realpath(optarg, NULL);
1756 if (repo_path == NULL)
1757 err(1, "-r option");
1758 got_path_strip_trailing_slashes(repo_path);
1759 break;
1760 default:
1761 usage_diff();
1762 /* NOTREACHED */
1766 argc -= optind;
1767 argv += optind;
1769 cwd = getcwd(NULL, 0);
1770 if (cwd == NULL) {
1771 error = got_error_from_errno("getcwd");
1772 goto done;
1774 error = got_worktree_open(&worktree, cwd);
1775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1776 goto done;
1777 if (argc <= 1) {
1778 if (worktree == NULL) {
1779 error = got_error(GOT_ERR_NOT_WORKTREE);
1780 goto done;
1782 if (repo_path)
1783 errx(1,
1784 "-r option can't be used when diffing a work tree");
1785 repo_path = strdup(got_worktree_get_repo_path(worktree));
1786 if (repo_path == NULL) {
1787 error = got_error_from_errno("strdup");
1788 goto done;
1790 if (argc == 1) {
1791 error = got_worktree_resolve_path(&path, worktree,
1792 argv[0]);
1793 if (error)
1794 goto done;
1795 } else {
1796 path = strdup("");
1797 if (path == NULL) {
1798 error = got_error_from_errno("strdup");
1799 goto done;
1802 } else if (argc == 2) {
1803 id_str1 = argv[0];
1804 id_str2 = argv[1];
1805 if (worktree && repo_path == NULL) {
1806 repo_path =
1807 strdup(got_worktree_get_repo_path(worktree));
1808 if (repo_path == NULL) {
1809 error = got_error_from_errno("strdup");
1810 goto done;
1813 } else
1814 usage_diff();
1816 if (repo_path == NULL) {
1817 repo_path = getcwd(NULL, 0);
1818 if (repo_path == NULL)
1819 return got_error_from_errno("getcwd");
1822 error = got_repo_open(&repo, repo_path);
1823 free(repo_path);
1824 if (error != NULL)
1825 goto done;
1827 error = apply_unveil(got_repo_get_path(repo), 1,
1828 worktree ? got_worktree_get_root_path(worktree) : NULL);
1829 if (error)
1830 goto done;
1832 if (argc <= 1) {
1833 struct print_diff_arg arg;
1834 struct got_pathlist_head paths;
1835 char *id_str;
1837 TAILQ_INIT(&paths);
1839 error = got_object_id_str(&id_str,
1840 got_worktree_get_base_commit_id(worktree));
1841 if (error)
1842 goto done;
1843 arg.repo = repo;
1844 arg.worktree = worktree;
1845 arg.diff_context = diff_context;
1846 arg.id_str = id_str;
1847 arg.header_shown = 0;
1849 error = got_pathlist_append(NULL, &paths, path, NULL);
1850 if (error)
1851 goto done;
1853 error = got_worktree_status(worktree, &paths, repo, print_diff,
1854 &arg, check_cancelled, NULL);
1855 free(id_str);
1856 got_pathlist_free(&paths);
1857 goto done;
1860 error = got_repo_match_object_id_prefix(&id1, id_str1,
1861 GOT_OBJ_TYPE_ANY, repo);
1862 if (error) {
1863 struct got_reference *ref;
1864 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1865 goto done;
1866 error = got_ref_open(&ref, repo, id_str1, 0);
1867 if (error != NULL)
1868 goto done;
1869 label1 = strdup(got_ref_get_name(ref));
1870 if (label1 == NULL) {
1871 error = got_error_from_errno("strdup");
1872 goto done;
1874 error = got_ref_resolve(&id1, repo, ref);
1875 got_ref_close(ref);
1876 if (error != NULL)
1877 goto done;
1878 } else {
1879 error = got_object_id_str(&label1, id1);
1880 if (label1 == NULL) {
1881 error = got_error_from_errno("strdup");
1882 goto done;
1886 error = got_repo_match_object_id_prefix(&id2, id_str2,
1887 GOT_OBJ_TYPE_ANY, repo);
1888 if (error) {
1889 struct got_reference *ref;
1890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1891 goto done;
1892 error = got_ref_open(&ref, repo, id_str2, 0);
1893 if (error != NULL)
1894 goto done;
1895 label2 = strdup(got_ref_get_name(ref));
1896 if (label2 == NULL) {
1897 error = got_error_from_errno("strdup");
1898 goto done;
1900 error = got_ref_resolve(&id2, repo, ref);
1901 got_ref_close(ref);
1902 if (error != NULL)
1903 goto done;
1904 } else {
1905 error = got_object_id_str(&label2, id2);
1906 if (label2 == NULL) {
1907 error = got_error_from_errno("strdup");
1908 goto done;
1912 error = got_object_get_type(&type1, repo, id1);
1913 if (error)
1914 goto done;
1916 error = got_object_get_type(&type2, repo, id2);
1917 if (error)
1918 goto done;
1920 if (type1 != type2) {
1921 error = got_error(GOT_ERR_OBJ_TYPE);
1922 goto done;
1925 switch (type1) {
1926 case GOT_OBJ_TYPE_BLOB:
1927 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1928 diff_context, repo, stdout);
1929 break;
1930 case GOT_OBJ_TYPE_TREE:
1931 error = got_diff_objects_as_trees(id1, id2, "", "",
1932 diff_context, repo, stdout);
1933 break;
1934 case GOT_OBJ_TYPE_COMMIT:
1935 printf("diff %s %s\n", label1, label2);
1936 error = got_diff_objects_as_commits(id1, id2, diff_context,
1937 repo, stdout);
1938 break;
1939 default:
1940 error = got_error(GOT_ERR_OBJ_TYPE);
1943 done:
1944 free(label1);
1945 free(label2);
1946 free(id1);
1947 free(id2);
1948 free(path);
1949 if (worktree)
1950 got_worktree_close(worktree);
1951 if (repo) {
1952 const struct got_error *repo_error;
1953 repo_error = got_repo_close(repo);
1954 if (error == NULL)
1955 error = repo_error;
1957 return error;
1960 __dead static void
1961 usage_blame(void)
1963 fprintf(stderr,
1964 "usage: %s blame [-c commit] [-r repository-path] path\n",
1965 getprogname());
1966 exit(1);
1969 static const struct got_error *
1970 cmd_blame(int argc, char *argv[])
1972 const struct got_error *error;
1973 struct got_repository *repo = NULL;
1974 struct got_worktree *worktree = NULL;
1975 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1976 struct got_object_id *commit_id = NULL;
1977 char *commit_id_str = NULL;
1978 int ch;
1980 #ifndef PROFILE
1981 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1982 NULL) == -1)
1983 err(1, "pledge");
1984 #endif
1986 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1987 switch (ch) {
1988 case 'c':
1989 commit_id_str = optarg;
1990 break;
1991 case 'r':
1992 repo_path = realpath(optarg, NULL);
1993 if (repo_path == NULL)
1994 err(1, "-r option");
1995 got_path_strip_trailing_slashes(repo_path);
1996 break;
1997 default:
1998 usage_blame();
1999 /* NOTREACHED */
2003 argc -= optind;
2004 argv += optind;
2006 if (argc == 1)
2007 path = argv[0];
2008 else
2009 usage_blame();
2011 cwd = getcwd(NULL, 0);
2012 if (cwd == NULL) {
2013 error = got_error_from_errno("getcwd");
2014 goto done;
2016 if (repo_path == NULL) {
2017 error = got_worktree_open(&worktree, cwd);
2018 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2019 goto done;
2020 else
2021 error = NULL;
2022 if (worktree) {
2023 repo_path =
2024 strdup(got_worktree_get_repo_path(worktree));
2025 if (repo_path == NULL)
2026 error = got_error_from_errno("strdup");
2027 if (error)
2028 goto done;
2029 } else {
2030 repo_path = strdup(cwd);
2031 if (repo_path == NULL) {
2032 error = got_error_from_errno("strdup");
2033 goto done;
2038 error = got_repo_open(&repo, repo_path);
2039 if (error != NULL)
2040 goto done;
2042 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2043 if (error)
2044 goto done;
2046 if (worktree) {
2047 const char *prefix = got_worktree_get_path_prefix(worktree);
2048 char *p, *worktree_subdir = cwd +
2049 strlen(got_worktree_get_root_path(worktree));
2050 if (asprintf(&p, "%s%s%s%s%s",
2051 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2052 worktree_subdir, worktree_subdir[0] ? "/" : "",
2053 path) == -1) {
2054 error = got_error_from_errno("asprintf");
2055 goto done;
2057 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2058 free(p);
2059 } else {
2060 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2062 if (error)
2063 goto done;
2065 if (commit_id_str == NULL) {
2066 struct got_reference *head_ref;
2067 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2068 if (error != NULL)
2069 goto done;
2070 error = got_ref_resolve(&commit_id, repo, head_ref);
2071 got_ref_close(head_ref);
2072 if (error != NULL)
2073 goto done;
2074 } else {
2075 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2076 if (error)
2077 goto done;
2080 error = got_blame(in_repo_path, commit_id, repo, stdout);
2081 done:
2082 free(in_repo_path);
2083 free(repo_path);
2084 free(cwd);
2085 free(commit_id);
2086 if (worktree)
2087 got_worktree_close(worktree);
2088 if (repo) {
2089 const struct got_error *repo_error;
2090 repo_error = got_repo_close(repo);
2091 if (error == NULL)
2092 error = repo_error;
2094 return error;
2097 __dead static void
2098 usage_tree(void)
2100 fprintf(stderr,
2101 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2102 getprogname());
2103 exit(1);
2106 static void
2107 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2108 const char *root_path)
2110 int is_root_path = (strcmp(path, root_path) == 0);
2112 path += strlen(root_path);
2113 while (path[0] == '/')
2114 path++;
2116 printf("%s%s%s%s%s\n", id ? id : "", path,
2117 is_root_path ? "" : "/", te->name,
2118 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2121 static const struct got_error *
2122 print_tree(const char *path, struct got_object_id *commit_id,
2123 int show_ids, int recurse, const char *root_path,
2124 struct got_repository *repo)
2126 const struct got_error *err = NULL;
2127 struct got_object_id *tree_id = NULL;
2128 struct got_tree_object *tree = NULL;
2129 const struct got_tree_entries *entries;
2130 struct got_tree_entry *te;
2132 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2133 if (err)
2134 goto done;
2136 err = got_object_open_as_tree(&tree, repo, tree_id);
2137 if (err)
2138 goto done;
2139 entries = got_object_tree_get_entries(tree);
2140 te = SIMPLEQ_FIRST(&entries->head);
2141 while (te) {
2142 char *id = NULL;
2144 if (sigint_received || sigpipe_received)
2145 break;
2147 if (show_ids) {
2148 char *id_str;
2149 err = got_object_id_str(&id_str, te->id);
2150 if (err)
2151 goto done;
2152 if (asprintf(&id, "%s ", id_str) == -1) {
2153 err = got_error_from_errno("asprintf");
2154 free(id_str);
2155 goto done;
2157 free(id_str);
2159 print_entry(te, id, path, root_path);
2160 free(id);
2162 if (recurse && S_ISDIR(te->mode)) {
2163 char *child_path;
2164 if (asprintf(&child_path, "%s%s%s", path,
2165 path[0] == '/' && path[1] == '\0' ? "" : "/",
2166 te->name) == -1) {
2167 err = got_error_from_errno("asprintf");
2168 goto done;
2170 err = print_tree(child_path, commit_id, show_ids, 1,
2171 root_path, repo);
2172 free(child_path);
2173 if (err)
2174 goto done;
2177 te = SIMPLEQ_NEXT(te, entry);
2179 done:
2180 if (tree)
2181 got_object_tree_close(tree);
2182 free(tree_id);
2183 return err;
2186 static const struct got_error *
2187 cmd_tree(int argc, char *argv[])
2189 const struct got_error *error;
2190 struct got_repository *repo = NULL;
2191 struct got_worktree *worktree = NULL;
2192 const char *path;
2193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2194 struct got_object_id *commit_id = NULL;
2195 char *commit_id_str = NULL;
2196 int show_ids = 0, recurse = 0;
2197 int ch;
2199 #ifndef PROFILE
2200 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2201 NULL) == -1)
2202 err(1, "pledge");
2203 #endif
2205 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2206 switch (ch) {
2207 case 'c':
2208 commit_id_str = optarg;
2209 break;
2210 case 'r':
2211 repo_path = realpath(optarg, NULL);
2212 if (repo_path == NULL)
2213 err(1, "-r option");
2214 got_path_strip_trailing_slashes(repo_path);
2215 break;
2216 case 'i':
2217 show_ids = 1;
2218 break;
2219 case 'R':
2220 recurse = 1;
2221 break;
2222 default:
2223 usage_tree();
2224 /* NOTREACHED */
2228 argc -= optind;
2229 argv += optind;
2231 if (argc == 1)
2232 path = argv[0];
2233 else if (argc > 1)
2234 usage_tree();
2235 else
2236 path = NULL;
2238 cwd = getcwd(NULL, 0);
2239 if (cwd == NULL) {
2240 error = got_error_from_errno("getcwd");
2241 goto done;
2243 if (repo_path == NULL) {
2244 error = got_worktree_open(&worktree, cwd);
2245 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2246 goto done;
2247 else
2248 error = NULL;
2249 if (worktree) {
2250 repo_path =
2251 strdup(got_worktree_get_repo_path(worktree));
2252 if (repo_path == NULL)
2253 error = got_error_from_errno("strdup");
2254 if (error)
2255 goto done;
2256 } else {
2257 repo_path = strdup(cwd);
2258 if (repo_path == NULL) {
2259 error = got_error_from_errno("strdup");
2260 goto done;
2265 error = got_repo_open(&repo, repo_path);
2266 if (error != NULL)
2267 goto done;
2269 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2270 if (error)
2271 goto done;
2273 if (path == NULL) {
2274 if (worktree) {
2275 char *p, *worktree_subdir = cwd +
2276 strlen(got_worktree_get_root_path(worktree));
2277 if (asprintf(&p, "%s/%s",
2278 got_worktree_get_path_prefix(worktree),
2279 worktree_subdir) == -1) {
2280 error = got_error_from_errno("asprintf");
2281 goto done;
2283 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2284 free(p);
2285 if (error)
2286 goto done;
2287 } else
2288 path = "/";
2290 if (in_repo_path == NULL) {
2291 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2292 if (error != NULL)
2293 goto done;
2296 if (commit_id_str == NULL) {
2297 struct got_reference *head_ref;
2298 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2299 if (error != NULL)
2300 goto done;
2301 error = got_ref_resolve(&commit_id, repo, head_ref);
2302 got_ref_close(head_ref);
2303 if (error != NULL)
2304 goto done;
2305 } else {
2306 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2307 if (error)
2308 goto done;
2311 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2312 in_repo_path, repo);
2313 done:
2314 free(in_repo_path);
2315 free(repo_path);
2316 free(cwd);
2317 free(commit_id);
2318 if (worktree)
2319 got_worktree_close(worktree);
2320 if (repo) {
2321 const struct got_error *repo_error;
2322 repo_error = got_repo_close(repo);
2323 if (error == NULL)
2324 error = repo_error;
2326 return error;
2329 __dead static void
2330 usage_status(void)
2332 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2333 exit(1);
2336 static const struct got_error *
2337 print_status(void *arg, unsigned char status, const char *path,
2338 struct got_object_id *blob_id, struct got_object_id *commit_id)
2340 printf("%c %s\n", status, path);
2341 return NULL;
2344 static const struct got_error *
2345 cmd_status(int argc, char *argv[])
2347 const struct got_error *error = NULL;
2348 struct got_repository *repo = NULL;
2349 struct got_worktree *worktree = NULL;
2350 char *cwd = NULL;
2351 struct got_pathlist_head paths;
2352 struct got_pathlist_entry *pe;
2353 int ch;
2355 TAILQ_INIT(&paths);
2357 while ((ch = getopt(argc, argv, "")) != -1) {
2358 switch (ch) {
2359 default:
2360 usage_status();
2361 /* NOTREACHED */
2365 argc -= optind;
2366 argv += optind;
2368 #ifndef PROFILE
2369 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2370 NULL) == -1)
2371 err(1, "pledge");
2372 #endif
2373 cwd = getcwd(NULL, 0);
2374 if (cwd == NULL) {
2375 error = got_error_from_errno("getcwd");
2376 goto done;
2379 error = got_worktree_open(&worktree, cwd);
2380 if (error != NULL)
2381 goto done;
2383 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2384 if (error)
2385 goto done;
2387 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2388 if (error != NULL)
2389 goto done;
2391 error = apply_unveil(got_repo_get_path(repo), 1,
2392 got_worktree_get_root_path(worktree));
2393 if (error)
2394 goto done;
2396 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2397 check_cancelled, NULL);
2398 done:
2399 TAILQ_FOREACH(pe, &paths, entry)
2400 free((char *)pe->path);
2401 got_pathlist_free(&paths);
2402 free(cwd);
2403 return error;
2406 __dead static void
2407 usage_ref(void)
2409 fprintf(stderr,
2410 "usage: %s ref [-r repository] -l | -d name | name target\n",
2411 getprogname());
2412 exit(1);
2415 static const struct got_error *
2416 list_refs(struct got_repository *repo)
2418 static const struct got_error *err = NULL;
2419 struct got_reflist_head refs;
2420 struct got_reflist_entry *re;
2422 SIMPLEQ_INIT(&refs);
2423 err = got_ref_list(&refs, repo);
2424 if (err)
2425 return err;
2427 SIMPLEQ_FOREACH(re, &refs, entry) {
2428 char *refstr;
2429 refstr = got_ref_to_str(re->ref);
2430 if (refstr == NULL)
2431 return got_error_from_errno("got_ref_to_str");
2432 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2433 free(refstr);
2436 got_ref_list_free(&refs);
2437 return NULL;
2440 static const struct got_error *
2441 delete_ref(struct got_repository *repo, const char *refname)
2443 const struct got_error *err = NULL;
2444 struct got_reference *ref;
2446 err = got_ref_open(&ref, repo, refname, 0);
2447 if (err)
2448 return err;
2450 err = got_ref_delete(ref, repo);
2451 got_ref_close(ref);
2452 return err;
2455 static const struct got_error *
2456 add_ref(struct got_repository *repo, const char *refname, const char *target)
2458 const struct got_error *err = NULL;
2459 struct got_object_id *id;
2460 struct got_reference *ref = NULL;
2463 * Don't let the user create a reference named '-'.
2464 * While technically a valid reference name, this case is usually
2465 * an unintended typo.
2467 if (refname[0] == '-' && refname[1] == '\0')
2468 return got_error(GOT_ERR_BAD_REF_NAME);
2470 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2471 repo);
2472 if (err) {
2473 struct got_reference *target_ref;
2475 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2476 return err;
2477 err = got_ref_open(&target_ref, repo, target, 0);
2478 if (err)
2479 return err;
2480 err = got_ref_resolve(&id, repo, target_ref);
2481 got_ref_close(target_ref);
2482 if (err)
2483 return err;
2486 err = got_ref_alloc(&ref, refname, id);
2487 if (err)
2488 goto done;
2490 err = got_ref_write(ref, repo);
2491 done:
2492 if (ref)
2493 got_ref_close(ref);
2494 free(id);
2495 return err;
2498 static const struct got_error *
2499 cmd_ref(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 char *cwd = NULL, *repo_path = NULL;
2505 int ch, do_list = 0;
2506 const char *delref = NULL;
2508 /* TODO: Add -s option for adding symbolic references. */
2509 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2510 switch (ch) {
2511 case 'd':
2512 delref = optarg;
2513 break;
2514 case 'r':
2515 repo_path = realpath(optarg, NULL);
2516 if (repo_path == NULL)
2517 err(1, "-r option");
2518 got_path_strip_trailing_slashes(repo_path);
2519 break;
2520 case 'l':
2521 do_list = 1;
2522 break;
2523 default:
2524 usage_ref();
2525 /* NOTREACHED */
2529 if (do_list && delref)
2530 errx(1, "-l and -d options are mutually exclusive\n");
2532 argc -= optind;
2533 argv += optind;
2535 if (do_list || delref) {
2536 if (argc > 0)
2537 usage_ref();
2538 } else if (argc != 2)
2539 usage_ref();
2541 #ifndef PROFILE
2542 if (do_list) {
2543 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2544 NULL) == -1)
2545 err(1, "pledge");
2546 } else {
2547 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2548 "sendfd unveil", NULL) == -1)
2549 err(1, "pledge");
2551 #endif
2552 cwd = getcwd(NULL, 0);
2553 if (cwd == NULL) {
2554 error = got_error_from_errno("getcwd");
2555 goto done;
2558 if (repo_path == NULL) {
2559 error = got_worktree_open(&worktree, cwd);
2560 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2561 goto done;
2562 else
2563 error = NULL;
2564 if (worktree) {
2565 repo_path =
2566 strdup(got_worktree_get_repo_path(worktree));
2567 if (repo_path == NULL)
2568 error = got_error_from_errno("strdup");
2569 if (error)
2570 goto done;
2571 } else {
2572 repo_path = strdup(cwd);
2573 if (repo_path == NULL) {
2574 error = got_error_from_errno("strdup");
2575 goto done;
2580 error = got_repo_open(&repo, repo_path);
2581 if (error != NULL)
2582 goto done;
2584 error = apply_unveil(got_repo_get_path(repo), do_list,
2585 worktree ? got_worktree_get_root_path(worktree) : NULL);
2586 if (error)
2587 goto done;
2589 if (do_list)
2590 error = list_refs(repo);
2591 else if (delref)
2592 error = delete_ref(repo, delref);
2593 else
2594 error = add_ref(repo, argv[0], argv[1]);
2595 done:
2596 if (repo)
2597 got_repo_close(repo);
2598 if (worktree)
2599 got_worktree_close(worktree);
2600 free(cwd);
2601 free(repo_path);
2602 return error;
2605 __dead static void
2606 usage_branch(void)
2608 fprintf(stderr,
2609 "usage: %s branch [-r repository] -l | -d name | "
2610 "name [base-branch]\n", getprogname());
2611 exit(1);
2614 static const struct got_error *
2615 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2617 static const struct got_error *err = NULL;
2618 struct got_reflist_head refs;
2619 struct got_reflist_entry *re;
2621 SIMPLEQ_INIT(&refs);
2623 err = got_ref_list(&refs, repo);
2624 if (err)
2625 return err;
2627 SIMPLEQ_FOREACH(re, &refs, entry) {
2628 const char *refname, *marker = " ";
2629 char *refstr;
2630 refname = got_ref_get_name(re->ref);
2631 if (strncmp(refname, "refs/heads/", 11) != 0)
2632 continue;
2633 if (worktree && strcmp(refname,
2634 got_worktree_get_head_ref_name(worktree)) == 0) {
2635 struct got_object_id *id = NULL;
2636 err = got_ref_resolve(&id, repo, re->ref);
2637 if (err)
2638 return err;
2639 if (got_object_id_cmp(id,
2640 got_worktree_get_base_commit_id(worktree)) == 0)
2641 marker = "* ";
2642 else
2643 marker = "~ ";
2644 free(id);
2646 refname += 11;
2647 refstr = got_ref_to_str(re->ref);
2648 if (refstr == NULL)
2649 return got_error_from_errno("got_ref_to_str");
2650 printf("%s%s: %s\n", marker, refname, refstr);
2651 free(refstr);
2654 got_ref_list_free(&refs);
2655 return NULL;
2658 static const struct got_error *
2659 delete_branch(struct got_repository *repo, const char *branch_name)
2661 const struct got_error *err = NULL;
2662 struct got_reference *ref;
2663 char *refname;
2665 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2666 return got_error_from_errno("asprintf");
2668 err = got_ref_open(&ref, repo, refname, 0);
2669 if (err)
2670 goto done;
2672 err = got_ref_delete(ref, repo);
2673 got_ref_close(ref);
2674 done:
2675 free(refname);
2676 return err;
2679 static const struct got_error *
2680 add_branch(struct got_repository *repo, const char *branch_name,
2681 const char *base_branch)
2683 const struct got_error *err = NULL;
2684 struct got_object_id *id = NULL;
2685 struct got_reference *ref = NULL;
2686 char *base_refname = NULL, *refname = NULL;
2687 struct got_reference *base_ref;
2690 * Don't let the user create a branch named '-'.
2691 * While technically a valid reference name, this case is usually
2692 * an unintended typo.
2694 if (branch_name[0] == '-' && branch_name[1] == '\0')
2695 return got_error(GOT_ERR_BAD_REF_NAME);
2697 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2698 base_refname = strdup(GOT_REF_HEAD);
2699 if (base_refname == NULL)
2700 return got_error_from_errno("strdup");
2701 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2702 return got_error_from_errno("asprintf");
2704 err = got_ref_open(&base_ref, repo, base_refname, 0);
2705 if (err)
2706 goto done;
2707 err = got_ref_resolve(&id, repo, base_ref);
2708 got_ref_close(base_ref);
2709 if (err)
2710 goto done;
2712 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2713 err = got_error_from_errno("asprintf");
2714 goto done;
2717 err = got_ref_open(&ref, repo, refname, 0);
2718 if (err == NULL) {
2719 err = got_error(GOT_ERR_BRANCH_EXISTS);
2720 goto done;
2721 } else if (err->code != GOT_ERR_NOT_REF)
2722 goto done;
2724 err = got_ref_alloc(&ref, refname, id);
2725 if (err)
2726 goto done;
2728 err = got_ref_write(ref, repo);
2729 done:
2730 if (ref)
2731 got_ref_close(ref);
2732 free(id);
2733 free(base_refname);
2734 free(refname);
2735 return err;
2738 static const struct got_error *
2739 cmd_branch(int argc, char *argv[])
2741 const struct got_error *error = NULL;
2742 struct got_repository *repo = NULL;
2743 struct got_worktree *worktree = NULL;
2744 char *cwd = NULL, *repo_path = NULL;
2745 int ch, do_list = 0;
2746 const char *delref = NULL;
2748 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2749 switch (ch) {
2750 case 'd':
2751 delref = optarg;
2752 break;
2753 case 'r':
2754 repo_path = realpath(optarg, NULL);
2755 if (repo_path == NULL)
2756 err(1, "-r option");
2757 got_path_strip_trailing_slashes(repo_path);
2758 break;
2759 case 'l':
2760 do_list = 1;
2761 break;
2762 default:
2763 usage_branch();
2764 /* NOTREACHED */
2768 if (do_list && delref)
2769 errx(1, "-l and -d options are mutually exclusive\n");
2771 argc -= optind;
2772 argv += optind;
2774 if (do_list || delref) {
2775 if (argc > 0)
2776 usage_branch();
2777 } else if (argc < 1 || argc > 2)
2778 usage_branch();
2780 #ifndef PROFILE
2781 if (do_list) {
2782 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2783 NULL) == -1)
2784 err(1, "pledge");
2785 } else {
2786 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2787 "sendfd unveil", NULL) == -1)
2788 err(1, "pledge");
2790 #endif
2791 cwd = getcwd(NULL, 0);
2792 if (cwd == NULL) {
2793 error = got_error_from_errno("getcwd");
2794 goto done;
2797 if (repo_path == NULL) {
2798 error = got_worktree_open(&worktree, cwd);
2799 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2800 goto done;
2801 else
2802 error = NULL;
2803 if (worktree) {
2804 repo_path =
2805 strdup(got_worktree_get_repo_path(worktree));
2806 if (repo_path == NULL)
2807 error = got_error_from_errno("strdup");
2808 if (error)
2809 goto done;
2810 } else {
2811 repo_path = strdup(cwd);
2812 if (repo_path == NULL) {
2813 error = got_error_from_errno("strdup");
2814 goto done;
2819 error = got_repo_open(&repo, repo_path);
2820 if (error != NULL)
2821 goto done;
2823 error = apply_unveil(got_repo_get_path(repo), do_list,
2824 worktree ? got_worktree_get_root_path(worktree) : NULL);
2825 if (error)
2826 goto done;
2828 if (do_list)
2829 error = list_branches(repo, worktree);
2830 else if (delref)
2831 error = delete_branch(repo, delref);
2832 else {
2833 const char *base_branch;
2834 if (argc == 1) {
2835 base_branch = worktree ?
2836 got_worktree_get_head_ref_name(worktree) :
2837 GOT_REF_HEAD;
2838 } else
2839 base_branch = argv[1];
2840 error = add_branch(repo, argv[0], base_branch);
2842 done:
2843 if (repo)
2844 got_repo_close(repo);
2845 if (worktree)
2846 got_worktree_close(worktree);
2847 free(cwd);
2848 free(repo_path);
2849 return error;
2852 __dead static void
2853 usage_add(void)
2855 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2856 exit(1);
2859 static const struct got_error *
2860 cmd_add(int argc, char *argv[])
2862 const struct got_error *error = NULL;
2863 struct got_repository *repo = NULL;
2864 struct got_worktree *worktree = NULL;
2865 char *cwd = NULL;
2866 struct got_pathlist_head paths;
2867 struct got_pathlist_entry *pe;
2868 int ch, x;
2870 TAILQ_INIT(&paths);
2872 while ((ch = getopt(argc, argv, "")) != -1) {
2873 switch (ch) {
2874 default:
2875 usage_add();
2876 /* NOTREACHED */
2880 argc -= optind;
2881 argv += optind;
2883 #ifndef PROFILE
2884 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2885 NULL) == -1)
2886 err(1, "pledge");
2887 #endif
2888 if (argc < 1)
2889 usage_add();
2891 cwd = getcwd(NULL, 0);
2892 if (cwd == NULL) {
2893 error = got_error_from_errno("getcwd");
2894 goto done;
2897 error = got_worktree_open(&worktree, cwd);
2898 if (error)
2899 goto done;
2901 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2902 if (error != NULL)
2903 goto done;
2905 error = apply_unveil(got_repo_get_path(repo), 1,
2906 got_worktree_get_root_path(worktree));
2907 if (error)
2908 goto done;
2910 for (x = 0; x < argc; x++) {
2911 char *path = realpath(argv[x], NULL);
2912 if (path == NULL) {
2913 error = got_error_from_errno2("realpath", argv[x]);
2914 goto done;
2917 got_path_strip_trailing_slashes(path);
2918 error = got_pathlist_insert(&pe, &paths, path, NULL);
2919 if (error) {
2920 free(path);
2921 goto done;
2924 error = got_worktree_schedule_add(worktree, &paths, print_status,
2925 NULL, repo);
2926 done:
2927 if (repo)
2928 got_repo_close(repo);
2929 if (worktree)
2930 got_worktree_close(worktree);
2931 TAILQ_FOREACH(pe, &paths, entry)
2932 free((char *)pe->path);
2933 got_pathlist_free(&paths);
2934 free(cwd);
2935 return error;
2938 __dead static void
2939 usage_remove(void)
2941 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2942 exit(1);
2945 static const struct got_error *
2946 cmd_remove(int argc, char *argv[])
2948 const struct got_error *error = NULL;
2949 struct got_worktree *worktree = NULL;
2950 struct got_repository *repo = NULL;
2951 char *cwd = NULL;
2952 struct got_pathlist_head paths;
2953 struct got_pathlist_entry *pe;
2954 int ch, i, delete_local_mods = 0;
2956 TAILQ_INIT(&paths);
2958 while ((ch = getopt(argc, argv, "f")) != -1) {
2959 switch (ch) {
2960 case 'f':
2961 delete_local_mods = 1;
2962 break;
2963 default:
2964 usage_add();
2965 /* NOTREACHED */
2969 argc -= optind;
2970 argv += optind;
2972 #ifndef PROFILE
2973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2974 NULL) == -1)
2975 err(1, "pledge");
2976 #endif
2977 if (argc < 1)
2978 usage_remove();
2980 cwd = getcwd(NULL, 0);
2981 if (cwd == NULL) {
2982 error = got_error_from_errno("getcwd");
2983 goto done;
2985 error = got_worktree_open(&worktree, cwd);
2986 if (error)
2987 goto done;
2989 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2990 if (error)
2991 goto done;
2993 error = apply_unveil(got_repo_get_path(repo), 1,
2994 got_worktree_get_root_path(worktree));
2995 if (error)
2996 goto done;
2998 for (i = 0; i < argc; i++) {
2999 char *path = realpath(argv[i], NULL);
3000 if (path == NULL) {
3001 error = got_error_from_errno2("realpath", argv[i]);
3002 goto done;
3005 got_path_strip_trailing_slashes(path);
3006 error = got_pathlist_insert(&pe, &paths, path, NULL);
3007 if (error) {
3008 free(path);
3009 goto done;
3012 error = got_worktree_schedule_delete(worktree, &paths,
3013 delete_local_mods, print_status, NULL, repo);
3014 if (error)
3015 goto done;
3016 done:
3017 if (repo)
3018 got_repo_close(repo);
3019 if (worktree)
3020 got_worktree_close(worktree);
3021 TAILQ_FOREACH(pe, &paths, entry)
3022 free((char *)pe->path);
3023 got_pathlist_free(&paths);
3024 free(cwd);
3025 return error;
3028 __dead static void
3029 usage_revert(void)
3031 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3032 exit(1);
3035 static const struct got_error *
3036 revert_progress(void *arg, unsigned char status, const char *path)
3038 while (path[0] == '/')
3039 path++;
3040 printf("%c %s\n", status, path);
3041 return NULL;
3044 static const struct got_error *
3045 cmd_revert(int argc, char *argv[])
3047 const struct got_error *error = NULL;
3048 struct got_worktree *worktree = NULL;
3049 struct got_repository *repo = NULL;
3050 char *cwd = NULL, *path = NULL;
3051 struct got_pathlist_head paths;
3052 struct got_pathlist_entry *pe;
3053 int ch, i;
3055 TAILQ_INIT(&paths);
3057 while ((ch = getopt(argc, argv, "")) != -1) {
3058 switch (ch) {
3059 default:
3060 usage_revert();
3061 /* NOTREACHED */
3065 argc -= optind;
3066 argv += optind;
3068 #ifndef PROFILE
3069 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3070 "unveil", NULL) == -1)
3071 err(1, "pledge");
3072 #endif
3073 if (argc < 1)
3074 usage_revert();
3076 for (i = 0; i < argc; i++) {
3077 char *path = realpath(argv[i], NULL);
3078 if (path == NULL) {
3079 error = got_error_from_errno2("realpath", argv[i]);
3080 goto done;
3083 got_path_strip_trailing_slashes(path);
3084 error = got_pathlist_insert(&pe, &paths, path, NULL);
3085 if (error) {
3086 free(path);
3087 goto done;
3091 cwd = getcwd(NULL, 0);
3092 if (cwd == NULL) {
3093 error = got_error_from_errno("getcwd");
3094 goto done;
3096 error = got_worktree_open(&worktree, cwd);
3097 if (error)
3098 goto done;
3100 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3101 if (error != NULL)
3102 goto done;
3104 error = apply_unveil(got_repo_get_path(repo), 1,
3105 got_worktree_get_root_path(worktree));
3106 if (error)
3107 goto done;
3109 error = got_worktree_revert(worktree, &paths,
3110 revert_progress, NULL, repo);
3111 if (error)
3112 goto done;
3113 done:
3114 if (repo)
3115 got_repo_close(repo);
3116 if (worktree)
3117 got_worktree_close(worktree);
3118 free(path);
3119 free(cwd);
3120 return error;
3123 __dead static void
3124 usage_commit(void)
3126 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3127 exit(1);
3130 struct collect_commit_logmsg_arg {
3131 const char *cmdline_log;
3132 const char *editor;
3133 const char *worktree_path;
3134 const char *branch_name;
3135 const char *repo_path;
3136 char *logmsg_path;
3140 static const struct got_error *
3141 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3142 void *arg)
3144 char *initial_content = NULL;
3145 struct got_pathlist_entry *pe;
3146 const struct got_error *err = NULL;
3147 char *template = NULL;
3148 struct collect_commit_logmsg_arg *a = arg;
3149 int fd;
3150 size_t len;
3152 /* if a message was specified on the command line, just use it */
3153 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3154 len = strlen(a->cmdline_log) + 1;
3155 *logmsg = malloc(len + 1);
3156 if (*logmsg == NULL)
3157 return got_error_from_errno("malloc");
3158 strlcpy(*logmsg, a->cmdline_log, len);
3159 return NULL;
3162 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3163 return got_error_from_errno("asprintf");
3165 if (asprintf(&initial_content,
3166 "\n# changes to be committed on branch %s:\n",
3167 a->branch_name) == -1)
3168 return got_error_from_errno("asprintf");
3170 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3171 if (err)
3172 goto done;
3174 dprintf(fd, initial_content);
3176 TAILQ_FOREACH(pe, commitable_paths, entry) {
3177 struct got_commitable *ct = pe->data;
3178 dprintf(fd, "# %c %s\n",
3179 got_commitable_get_status(ct),
3180 got_commitable_get_path(ct));
3182 close(fd);
3184 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3185 done:
3186 unlink(a->logmsg_path);
3187 free(a->logmsg_path);
3188 free(initial_content);
3189 free(template);
3191 /* Editor is done; we can now apply unveil(2) */
3192 if (err == NULL) {
3193 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3194 if (err) {
3195 free(*logmsg);
3196 *logmsg = NULL;
3199 return err;
3202 static const struct got_error *
3203 cmd_commit(int argc, char *argv[])
3205 const struct got_error *error = NULL;
3206 struct got_worktree *worktree = NULL;
3207 struct got_repository *repo = NULL;
3208 char *cwd = NULL, *path = NULL, *id_str = NULL;
3209 struct got_object_id *id = NULL;
3210 const char *logmsg = NULL;
3211 const char *got_author = getenv("GOT_AUTHOR");
3212 struct collect_commit_logmsg_arg cl_arg;
3213 char *editor = NULL;
3214 int ch, rebase_in_progress;
3216 while ((ch = getopt(argc, argv, "m:")) != -1) {
3217 switch (ch) {
3218 case 'm':
3219 logmsg = optarg;
3220 break;
3221 default:
3222 usage_commit();
3223 /* NOTREACHED */
3227 argc -= optind;
3228 argv += optind;
3230 #ifndef PROFILE
3231 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3232 "unveil", NULL) == -1)
3233 err(1, "pledge");
3234 #endif
3235 if (argc == 1) {
3236 path = realpath(argv[0], NULL);
3237 if (path == NULL) {
3238 error = got_error_from_errno2("realpath", argv[0]);
3239 goto done;
3241 got_path_strip_trailing_slashes(path);
3242 } else if (argc != 0)
3243 usage_commit();
3245 if (got_author == NULL) {
3246 /* TODO: Look current user up in password database */
3247 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3248 goto done;
3251 cwd = getcwd(NULL, 0);
3252 if (cwd == NULL) {
3253 error = got_error_from_errno("getcwd");
3254 goto done;
3256 error = got_worktree_open(&worktree, cwd);
3257 if (error)
3258 goto done;
3260 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3261 if (error)
3262 goto done;
3263 if (rebase_in_progress) {
3264 error = got_error(GOT_ERR_REBASING);
3265 goto done;
3268 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3269 if (error != NULL)
3270 goto done;
3273 * unveil(2) traverses exec(2); if an editor is used we have
3274 * to apply unveil after the log message has been written.
3276 if (logmsg == NULL || strlen(logmsg) == 0)
3277 error = get_editor(&editor);
3278 else
3279 error = apply_unveil(got_repo_get_path(repo), 0,
3280 got_worktree_get_root_path(worktree));
3281 if (error)
3282 goto done;
3284 cl_arg.editor = editor;
3285 cl_arg.cmdline_log = logmsg;
3286 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3287 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3288 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3289 cl_arg.branch_name += 5;
3290 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3291 cl_arg.branch_name += 6;
3292 cl_arg.repo_path = got_repo_get_path(repo);
3293 cl_arg.logmsg_path = NULL;
3294 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3295 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3296 if (error) {
3297 if (cl_arg.logmsg_path)
3298 fprintf(stderr, "%s: log message preserved in %s\n",
3299 getprogname(), cl_arg.logmsg_path);
3300 goto done;
3303 if (cl_arg.logmsg_path)
3304 unlink(cl_arg.logmsg_path);
3306 error = got_object_id_str(&id_str, id);
3307 if (error)
3308 goto done;
3309 printf("Created commit %s\n", id_str);
3310 done:
3311 if (repo)
3312 got_repo_close(repo);
3313 if (worktree)
3314 got_worktree_close(worktree);
3315 free(path);
3316 free(cwd);
3317 free(id_str);
3318 free(editor);
3319 return error;
3322 __dead static void
3323 usage_cherrypick(void)
3325 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3326 exit(1);
3329 static const struct got_error *
3330 cmd_cherrypick(int argc, char *argv[])
3332 const struct got_error *error = NULL;
3333 struct got_worktree *worktree = NULL;
3334 struct got_repository *repo = NULL;
3335 char *cwd = NULL, *commit_id_str = NULL;
3336 struct got_object_id *commit_id = NULL;
3337 struct got_commit_object *commit = NULL;
3338 struct got_object_qid *pid;
3339 struct got_reference *head_ref = NULL;
3340 int ch, did_something = 0;
3342 while ((ch = getopt(argc, argv, "")) != -1) {
3343 switch (ch) {
3344 default:
3345 usage_cherrypick();
3346 /* NOTREACHED */
3350 argc -= optind;
3351 argv += optind;
3353 #ifndef PROFILE
3354 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3355 "unveil", NULL) == -1)
3356 err(1, "pledge");
3357 #endif
3358 if (argc != 1)
3359 usage_cherrypick();
3361 cwd = getcwd(NULL, 0);
3362 if (cwd == NULL) {
3363 error = got_error_from_errno("getcwd");
3364 goto done;
3366 error = got_worktree_open(&worktree, cwd);
3367 if (error)
3368 goto done;
3370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3371 if (error != NULL)
3372 goto done;
3374 error = apply_unveil(got_repo_get_path(repo), 0,
3375 got_worktree_get_root_path(worktree));
3376 if (error)
3377 goto done;
3379 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3380 GOT_OBJ_TYPE_COMMIT, repo);
3381 if (error != NULL) {
3382 struct got_reference *ref;
3383 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3384 goto done;
3385 error = got_ref_open(&ref, repo, argv[0], 0);
3386 if (error != NULL)
3387 goto done;
3388 error = got_ref_resolve(&commit_id, repo, ref);
3389 got_ref_close(ref);
3390 if (error != NULL)
3391 goto done;
3393 error = got_object_id_str(&commit_id_str, commit_id);
3394 if (error)
3395 goto done;
3397 error = got_ref_open(&head_ref, repo,
3398 got_worktree_get_head_ref_name(worktree), 0);
3399 if (error != NULL)
3400 goto done;
3402 error = check_same_branch(commit_id, head_ref, repo);
3403 if (error) {
3404 if (error->code != GOT_ERR_ANCESTRY)
3405 goto done;
3406 error = NULL;
3407 } else {
3408 error = got_error(GOT_ERR_SAME_BRANCH);
3409 goto done;
3412 error = got_object_open_as_commit(&commit, repo, commit_id);
3413 if (error)
3414 goto done;
3415 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3416 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3417 commit_id, repo, update_progress, &did_something, check_cancelled,
3418 NULL);
3419 if (error != NULL)
3420 goto done;
3422 if (did_something)
3423 printf("Merged commit %s\n", commit_id_str);
3424 done:
3425 if (commit)
3426 got_object_commit_close(commit);
3427 free(commit_id_str);
3428 if (head_ref)
3429 got_ref_close(head_ref);
3430 if (worktree)
3431 got_worktree_close(worktree);
3432 if (repo)
3433 got_repo_close(repo);
3434 return error;
3437 __dead static void
3438 usage_backout(void)
3440 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3441 exit(1);
3444 static const struct got_error *
3445 cmd_backout(int argc, char *argv[])
3447 const struct got_error *error = NULL;
3448 struct got_worktree *worktree = NULL;
3449 struct got_repository *repo = NULL;
3450 char *cwd = NULL, *commit_id_str = NULL;
3451 struct got_object_id *commit_id = NULL;
3452 struct got_commit_object *commit = NULL;
3453 struct got_object_qid *pid;
3454 struct got_reference *head_ref = NULL;
3455 int ch, did_something = 0;
3457 while ((ch = getopt(argc, argv, "")) != -1) {
3458 switch (ch) {
3459 default:
3460 usage_backout();
3461 /* NOTREACHED */
3465 argc -= optind;
3466 argv += optind;
3468 #ifndef PROFILE
3469 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3470 "unveil", NULL) == -1)
3471 err(1, "pledge");
3472 #endif
3473 if (argc != 1)
3474 usage_backout();
3476 cwd = getcwd(NULL, 0);
3477 if (cwd == NULL) {
3478 error = got_error_from_errno("getcwd");
3479 goto done;
3481 error = got_worktree_open(&worktree, cwd);
3482 if (error)
3483 goto done;
3485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3486 if (error != NULL)
3487 goto done;
3489 error = apply_unveil(got_repo_get_path(repo), 0,
3490 got_worktree_get_root_path(worktree));
3491 if (error)
3492 goto done;
3494 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3495 GOT_OBJ_TYPE_COMMIT, repo);
3496 if (error != NULL) {
3497 struct got_reference *ref;
3498 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3499 goto done;
3500 error = got_ref_open(&ref, repo, argv[0], 0);
3501 if (error != NULL)
3502 goto done;
3503 error = got_ref_resolve(&commit_id, repo, ref);
3504 got_ref_close(ref);
3505 if (error != NULL)
3506 goto done;
3508 error = got_object_id_str(&commit_id_str, commit_id);
3509 if (error)
3510 goto done;
3512 error = got_ref_open(&head_ref, repo,
3513 got_worktree_get_head_ref_name(worktree), 0);
3514 if (error != NULL)
3515 goto done;
3517 error = check_same_branch(commit_id, head_ref, repo);
3518 if (error)
3519 goto done;
3521 error = got_object_open_as_commit(&commit, repo, commit_id);
3522 if (error)
3523 goto done;
3524 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3525 if (pid == NULL) {
3526 error = got_error(GOT_ERR_ROOT_COMMIT);
3527 goto done;
3530 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3531 update_progress, &did_something, check_cancelled, NULL);
3532 if (error != NULL)
3533 goto done;
3535 if (did_something)
3536 printf("Backed out commit %s\n", commit_id_str);
3537 done:
3538 if (commit)
3539 got_object_commit_close(commit);
3540 free(commit_id_str);
3541 if (head_ref)
3542 got_ref_close(head_ref);
3543 if (worktree)
3544 got_worktree_close(worktree);
3545 if (repo)
3546 got_repo_close(repo);
3547 return error;
3550 __dead static void
3551 usage_rebase(void)
3553 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3554 getprogname());
3555 exit(1);
3558 void
3559 trim_logmsg(char *logmsg, int limit)
3561 char *nl;
3562 size_t len;
3564 len = strlen(logmsg);
3565 if (len > limit)
3566 len = limit;
3567 logmsg[len] = '\0';
3568 nl = strchr(logmsg, '\n');
3569 if (nl)
3570 *nl = '\0';
3573 static const struct got_error *
3574 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3576 const char *logmsg0 = NULL;
3578 logmsg0 = got_object_commit_get_logmsg(commit);
3580 while (isspace((unsigned char)logmsg0[0]))
3581 logmsg0++;
3583 *logmsg = strdup(logmsg0);
3584 if (*logmsg == NULL)
3585 return got_error_from_errno("strdup");
3587 trim_logmsg(*logmsg, limit);
3588 return NULL;
3591 static const struct got_error *
3592 show_rebase_progress(struct got_commit_object *commit,
3593 struct got_object_id *old_id, struct got_object_id *new_id)
3595 const struct got_error *err;
3596 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3598 err = got_object_id_str(&old_id_str, old_id);
3599 if (err)
3600 goto done;
3602 if (new_id) {
3603 err = got_object_id_str(&new_id_str, new_id);
3604 if (err)
3605 goto done;
3608 old_id_str[12] = '\0';
3609 if (new_id_str)
3610 new_id_str[12] = '\0';
3612 err = get_short_logmsg(&logmsg, 42, commit);
3613 if (err)
3614 goto done;
3616 printf("%s -> %s: %s\n", old_id_str,
3617 new_id_str ? new_id_str : "no-op change", logmsg);
3618 done:
3619 free(old_id_str);
3620 free(new_id_str);
3621 return err;
3624 static const struct got_error *
3625 rebase_progress(void *arg, unsigned char status, const char *path)
3627 unsigned char *rebase_status = arg;
3629 while (path[0] == '/')
3630 path++;
3631 printf("%c %s\n", status, path);
3633 if (*rebase_status == GOT_STATUS_CONFLICT)
3634 return NULL;
3635 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3636 *rebase_status = status;
3637 return NULL;
3640 static const struct got_error *
3641 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3642 struct got_reference *branch, struct got_reference *new_base_branch,
3643 struct got_reference *tmp_branch, struct got_repository *repo)
3645 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3646 return got_worktree_rebase_complete(worktree, fileindex,
3647 new_base_branch, tmp_branch, branch, repo);
3650 static const struct got_error *
3651 rebase_commit(struct got_pathlist_head *merged_paths,
3652 struct got_worktree *worktree, struct got_fileindex *fileindex,
3653 struct got_reference *tmp_branch,
3654 struct got_object_id *commit_id, struct got_repository *repo)
3656 const struct got_error *error;
3657 struct got_commit_object *commit;
3658 struct got_object_id *new_commit_id;
3660 error = got_object_open_as_commit(&commit, repo, commit_id);
3661 if (error)
3662 return error;
3664 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3665 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3666 if (error) {
3667 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3668 goto done;
3669 error = show_rebase_progress(commit, commit_id, NULL);
3670 } else {
3671 error = show_rebase_progress(commit, commit_id, new_commit_id);
3672 free(new_commit_id);
3674 done:
3675 got_object_commit_close(commit);
3676 return error;
3679 struct check_path_prefix_arg {
3680 const char *path_prefix;
3681 size_t len;
3682 int errcode;
3685 static const struct got_error *
3686 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3687 struct got_blob_object *blob2, struct got_object_id *id1,
3688 struct got_object_id *id2, const char *path1, const char *path2,
3689 struct got_repository *repo)
3691 struct check_path_prefix_arg *a = arg;
3693 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3694 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3695 return got_error(a->errcode);
3697 return NULL;
3700 static const struct got_error *
3701 check_path_prefix(struct got_object_id *parent_id,
3702 struct got_object_id *commit_id, const char *path_prefix,
3703 int errcode, struct got_repository *repo)
3705 const struct got_error *err;
3706 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3707 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3708 struct check_path_prefix_arg cpp_arg;
3710 if (got_path_is_root_dir(path_prefix))
3711 return NULL;
3713 err = got_object_open_as_commit(&commit, repo, commit_id);
3714 if (err)
3715 goto done;
3717 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3718 if (err)
3719 goto done;
3721 err = got_object_open_as_tree(&tree1, repo,
3722 got_object_commit_get_tree_id(parent_commit));
3723 if (err)
3724 goto done;
3726 err = got_object_open_as_tree(&tree2, repo,
3727 got_object_commit_get_tree_id(commit));
3728 if (err)
3729 goto done;
3731 cpp_arg.path_prefix = path_prefix;
3732 while (cpp_arg.path_prefix[0] == '/')
3733 cpp_arg.path_prefix++;
3734 cpp_arg.len = strlen(cpp_arg.path_prefix);
3735 cpp_arg.errcode = errcode;
3736 err = got_diff_tree(tree1, tree2, "", "", repo,
3737 check_path_prefix_in_diff, &cpp_arg);
3738 done:
3739 if (tree1)
3740 got_object_tree_close(tree1);
3741 if (tree2)
3742 got_object_tree_close(tree2);
3743 if (commit)
3744 got_object_commit_close(commit);
3745 if (parent_commit)
3746 got_object_commit_close(parent_commit);
3747 return err;
3750 static const struct got_error *
3751 collect_commits(struct got_object_id_queue *commits,
3752 struct got_object_id *initial_commit_id,
3753 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3754 const char *path_prefix, int path_prefix_errcode,
3755 struct got_repository *repo)
3757 const struct got_error *err = NULL;
3758 struct got_commit_graph *graph = NULL;
3759 struct got_object_id *parent_id = NULL;
3760 struct got_object_qid *qid;
3761 struct got_object_id *commit_id = initial_commit_id;
3763 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3764 if (err)
3765 return err;
3767 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3768 if (err)
3769 goto done;
3770 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3771 err = got_commit_graph_iter_next(&parent_id, graph);
3772 if (err) {
3773 if (err->code == GOT_ERR_ITER_COMPLETED) {
3774 err = got_error_msg(GOT_ERR_ANCESTRY,
3775 "ran out of commits to rebase before "
3776 "youngest common ancestor commit has "
3777 "been reached?!?");
3778 goto done;
3779 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3780 goto done;
3781 err = got_commit_graph_fetch_commits(graph, 1, repo);
3782 if (err)
3783 goto done;
3784 } else {
3785 err = check_path_prefix(parent_id, commit_id,
3786 path_prefix, path_prefix_errcode, repo);
3787 if (err)
3788 goto done;
3790 err = got_object_qid_alloc(&qid, commit_id);
3791 if (err)
3792 goto done;
3793 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3794 commit_id = parent_id;
3797 done:
3798 got_commit_graph_close(graph);
3799 return err;
3802 static const struct got_error *
3803 cmd_rebase(int argc, char *argv[])
3805 const struct got_error *error = NULL;
3806 struct got_worktree *worktree = NULL;
3807 struct got_repository *repo = NULL;
3808 struct got_fileindex *fileindex = NULL;
3809 char *cwd = NULL;
3810 struct got_reference *branch = NULL;
3811 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3812 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3813 struct got_object_id *resume_commit_id = NULL;
3814 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3815 struct got_commit_object *commit = NULL;
3816 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3817 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3818 struct got_object_id_queue commits;
3819 struct got_pathlist_head merged_paths;
3820 const struct got_object_id_queue *parent_ids;
3821 struct got_object_qid *qid, *pid;
3823 SIMPLEQ_INIT(&commits);
3824 TAILQ_INIT(&merged_paths);
3826 while ((ch = getopt(argc, argv, "ac")) != -1) {
3827 switch (ch) {
3828 case 'a':
3829 abort_rebase = 1;
3830 break;
3831 case 'c':
3832 continue_rebase = 1;
3833 break;
3834 default:
3835 usage_rebase();
3836 /* NOTREACHED */
3840 argc -= optind;
3841 argv += optind;
3843 #ifndef PROFILE
3844 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3845 "unveil", NULL) == -1)
3846 err(1, "pledge");
3847 #endif
3848 if (abort_rebase && continue_rebase)
3849 usage_rebase();
3850 else if (abort_rebase || continue_rebase) {
3851 if (argc != 0)
3852 usage_rebase();
3853 } else if (argc != 1)
3854 usage_rebase();
3856 cwd = getcwd(NULL, 0);
3857 if (cwd == NULL) {
3858 error = got_error_from_errno("getcwd");
3859 goto done;
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error)
3863 goto done;
3865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3866 if (error != NULL)
3867 goto done;
3869 error = apply_unveil(got_repo_get_path(repo), 0,
3870 got_worktree_get_root_path(worktree));
3871 if (error)
3872 goto done;
3874 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3875 if (error)
3876 goto done;
3878 if (abort_rebase) {
3879 int did_something;
3880 if (!rebase_in_progress) {
3881 error = got_error(GOT_ERR_NOT_REBASING);
3882 goto done;
3884 error = got_worktree_rebase_continue(&resume_commit_id,
3885 &new_base_branch, &tmp_branch, &branch, &fileindex,
3886 worktree, repo);
3887 if (error)
3888 goto done;
3889 printf("Switching work tree to %s\n",
3890 got_ref_get_symref_target(new_base_branch));
3891 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3892 new_base_branch, update_progress, &did_something);
3893 if (error)
3894 goto done;
3895 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3896 goto done; /* nothing else to do */
3899 if (continue_rebase) {
3900 if (!rebase_in_progress) {
3901 error = got_error(GOT_ERR_NOT_REBASING);
3902 goto done;
3904 error = got_worktree_rebase_continue(&resume_commit_id,
3905 &new_base_branch, &tmp_branch, &branch, &fileindex,
3906 worktree, repo);
3907 if (error)
3908 goto done;
3910 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3911 resume_commit_id, repo);
3912 if (error)
3913 goto done;
3915 yca_id = got_object_id_dup(resume_commit_id);
3916 if (yca_id == NULL) {
3917 error = got_error_from_errno("got_object_id_dup");
3918 goto done;
3920 } else {
3921 error = got_ref_open(&branch, repo, argv[0], 0);
3922 if (error != NULL)
3923 goto done;
3925 error = check_same_branch(
3926 got_worktree_get_base_commit_id(worktree), branch, repo);
3927 if (error) {
3928 if (error->code != GOT_ERR_ANCESTRY)
3929 goto done;
3930 error = NULL;
3931 } else {
3932 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3933 "specified branch resolves to a commit which "
3934 "is already contained in work tree's branch");
3935 goto done;
3939 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3940 if (error)
3941 goto done;
3943 if (!continue_rebase) {
3944 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3945 got_worktree_get_base_commit_id(worktree),
3946 branch_head_commit_id, repo);
3947 if (error)
3948 goto done;
3949 if (yca_id == NULL) {
3950 error = got_error_msg(GOT_ERR_ANCESTRY,
3951 "specified branch shares no common ancestry "
3952 "with work tree's branch");
3953 goto done;
3956 error = got_worktree_rebase_prepare(&new_base_branch,
3957 &tmp_branch, &fileindex, worktree, branch, repo);
3958 if (error)
3959 goto done;
3962 commit_id = branch_head_commit_id;
3963 error = got_object_open_as_commit(&commit, repo, commit_id);
3964 if (error)
3965 goto done;
3967 parent_ids = got_object_commit_get_parent_ids(commit);
3968 pid = SIMPLEQ_FIRST(parent_ids);
3969 error = collect_commits(&commits, commit_id, pid->id,
3970 yca_id, got_worktree_get_path_prefix(worktree),
3971 GOT_ERR_REBASE_PATH, repo);
3972 got_object_commit_close(commit);
3973 commit = NULL;
3974 if (error)
3975 goto done;
3977 if (SIMPLEQ_EMPTY(&commits)) {
3978 if (continue_rebase)
3979 error = rebase_complete(worktree, fileindex,
3980 branch, new_base_branch, tmp_branch, repo);
3981 else
3982 error = got_error(GOT_ERR_EMPTY_REBASE);
3983 goto done;
3986 pid = NULL;
3987 SIMPLEQ_FOREACH(qid, &commits, entry) {
3988 commit_id = qid->id;
3989 parent_id = pid ? pid->id : yca_id;
3990 pid = qid;
3992 error = got_worktree_rebase_merge_files(&merged_paths,
3993 worktree, fileindex, parent_id, commit_id, repo,
3994 rebase_progress, &rebase_status, check_cancelled, NULL);
3995 if (error)
3996 goto done;
3998 if (rebase_status == GOT_STATUS_CONFLICT) {
3999 got_worktree_rebase_pathlist_free(&merged_paths);
4000 break;
4003 error = rebase_commit(&merged_paths, worktree, fileindex,
4004 tmp_branch, commit_id, repo);
4005 got_worktree_rebase_pathlist_free(&merged_paths);
4006 if (error)
4007 goto done;
4010 if (rebase_status == GOT_STATUS_CONFLICT) {
4011 error = got_worktree_rebase_postpone(worktree, fileindex);
4012 if (error)
4013 goto done;
4014 error = got_error_msg(GOT_ERR_CONFLICTS,
4015 "conflicts must be resolved before rebasing can continue");
4016 } else
4017 error = rebase_complete(worktree, fileindex, branch,
4018 new_base_branch, tmp_branch, repo);
4019 done:
4020 got_object_id_queue_free(&commits);
4021 free(branch_head_commit_id);
4022 free(resume_commit_id);
4023 free(yca_id);
4024 if (commit)
4025 got_object_commit_close(commit);
4026 if (branch)
4027 got_ref_close(branch);
4028 if (new_base_branch)
4029 got_ref_close(new_base_branch);
4030 if (tmp_branch)
4031 got_ref_close(tmp_branch);
4032 if (worktree)
4033 got_worktree_close(worktree);
4034 if (repo)
4035 got_repo_close(repo);
4036 return error;
4039 __dead static void
4040 usage_histedit(void)
4042 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4043 getprogname());
4044 exit(1);
4047 #define GOT_HISTEDIT_PICK 'p'
4048 #define GOT_HISTEDIT_EDIT 'e'
4049 #define GOT_HISTEDIT_FOLD 'f'
4050 #define GOT_HISTEDIT_DROP 'd'
4051 #define GOT_HISTEDIT_MESG 'm'
4053 static struct got_histedit_cmd {
4054 unsigned char code;
4055 const char *name;
4056 const char *desc;
4057 } got_histedit_cmds[] = {
4058 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4059 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4060 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4061 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4062 { GOT_HISTEDIT_MESG, "mesg",
4063 "single-line log message for commit above (open editor if empty)" },
4066 struct got_histedit_list_entry {
4067 TAILQ_ENTRY(got_histedit_list_entry) entry;
4068 struct got_object_id *commit_id;
4069 const struct got_histedit_cmd *cmd;
4070 char *logmsg;
4072 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4074 static const struct got_error *
4075 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4076 FILE *f, struct got_repository *repo)
4078 const struct got_error *err = NULL;
4079 char *logmsg = NULL, *id_str = NULL;
4080 struct got_commit_object *commit = NULL;
4081 size_t n;
4083 err = got_object_open_as_commit(&commit, repo, commit_id);
4084 if (err)
4085 goto done;
4087 err = get_short_logmsg(&logmsg, 34, commit);
4088 if (err)
4089 goto done;
4091 err = got_object_id_str(&id_str, commit_id);
4092 if (err)
4093 goto done;
4095 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4096 if (n < 0)
4097 err = got_ferror(f, GOT_ERR_IO);
4098 done:
4099 if (commit)
4100 got_object_commit_close(commit);
4101 free(id_str);
4102 free(logmsg);
4103 return err;
4106 static const struct got_error *
4107 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4108 struct got_repository *repo)
4110 const struct got_error *err = NULL;
4111 struct got_object_qid *qid;
4113 if (SIMPLEQ_EMPTY(commits))
4114 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4116 SIMPLEQ_FOREACH(qid, commits, entry) {
4117 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4118 f, repo);
4119 if (err)
4120 break;
4123 return err;
4126 static const struct got_error *
4127 write_cmd_list(FILE *f)
4129 const struct got_error *err = NULL;
4130 int n, i;
4132 n = fprintf(f, "# Available histedit commands:\n");
4133 if (n < 0)
4134 return got_ferror(f, GOT_ERR_IO);
4136 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4137 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4138 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4139 cmd->desc);
4140 if (n < 0) {
4141 err = got_ferror(f, GOT_ERR_IO);
4142 break;
4145 n = fprintf(f, "# Commits will be processed in order from top to "
4146 "bottom of this file.\n");
4147 if (n < 0)
4148 return got_ferror(f, GOT_ERR_IO);
4149 return err;
4152 static const struct got_error *
4153 histedit_syntax_error(int lineno)
4155 static char msg[42];
4156 int ret;
4158 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4159 lineno);
4160 if (ret == -1 || ret >= sizeof(msg))
4161 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4163 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4166 static const struct got_error *
4167 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4168 char *logmsg, struct got_repository *repo)
4170 const struct got_error *err;
4171 struct got_commit_object *folded_commit = NULL;
4172 char *id_str;
4174 err = got_object_id_str(&id_str, hle->commit_id);
4175 if (err)
4176 return err;
4178 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4179 if (err)
4180 goto done;
4182 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4183 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4184 got_object_commit_get_logmsg(folded_commit)) == -1) {
4185 err = got_error_from_errno("asprintf");
4186 goto done;
4188 done:
4189 if (folded_commit)
4190 got_object_commit_close(folded_commit);
4191 free(id_str);
4192 return err;
4195 static struct got_histedit_list_entry *
4196 get_folded_commits(struct got_histedit_list_entry *hle)
4198 struct got_histedit_list_entry *prev, *folded = NULL;
4200 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4201 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4202 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4203 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4204 folded = prev;
4205 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4208 return folded;
4211 static const struct got_error *
4212 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4213 struct got_repository *repo)
4215 char *logmsg_path = NULL, *id_str = NULL;
4216 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4217 const struct got_error *err = NULL;
4218 struct got_commit_object *commit = NULL;
4219 int fd;
4220 struct got_histedit_list_entry *folded = NULL;
4222 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4223 if (err)
4224 return err;
4226 folded = get_folded_commits(hle);
4227 if (folded) {
4228 while (folded != hle) {
4229 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4230 folded = TAILQ_NEXT(folded, entry);
4231 continue;
4233 err = append_folded_commit_msg(&new_msg, folded,
4234 logmsg, repo);
4235 if (err)
4236 goto done;
4237 free(logmsg);
4238 logmsg = new_msg;
4239 folded = TAILQ_NEXT(folded, entry);
4243 err = got_object_id_str(&id_str, hle->commit_id);
4244 if (err)
4245 goto done;
4246 if (asprintf(&new_msg,
4247 "%s\n# original log message of commit %s: %s",
4248 logmsg ? logmsg : "", id_str,
4249 got_object_commit_get_logmsg(commit)) == -1) {
4250 err = got_error_from_errno("asprintf");
4251 goto done;
4253 free(logmsg);
4254 logmsg = new_msg;
4256 err = got_object_id_str(&id_str, hle->commit_id);
4257 if (err)
4258 goto done;
4260 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4261 if (err)
4262 goto done;
4264 dprintf(fd, logmsg);
4265 close(fd);
4267 err = get_editor(&editor);
4268 if (err)
4269 goto done;
4271 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4272 if (err) {
4273 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4274 goto done;
4275 err = NULL;
4276 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4277 if (hle->logmsg == NULL)
4278 err = got_error_from_errno("strdup");
4280 done:
4281 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4282 err = got_error_from_errno2("unlink", logmsg_path);
4283 free(logmsg_path);
4284 free(logmsg);
4285 free(editor);
4286 if (commit)
4287 got_object_commit_close(commit);
4288 return err;
4291 static const struct got_error *
4292 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4293 FILE *f, struct got_repository *repo)
4295 const struct got_error *err = NULL;
4296 char *line = NULL, *p, *end;
4297 size_t size;
4298 ssize_t len;
4299 int lineno = 0, i;
4300 const struct got_histedit_cmd *cmd;
4301 struct got_object_id *commit_id = NULL;
4302 struct got_histedit_list_entry *hle = NULL;
4304 for (;;) {
4305 len = getline(&line, &size, f);
4306 if (len == -1) {
4307 const struct got_error *getline_err;
4308 if (feof(f))
4309 break;
4310 getline_err = got_error_from_errno("getline");
4311 err = got_ferror(f, getline_err->code);
4312 break;
4314 lineno++;
4315 p = line;
4316 while (isspace((unsigned char)p[0]))
4317 p++;
4318 if (p[0] == '#' || p[0] == '\0') {
4319 free(line);
4320 line = NULL;
4321 continue;
4323 cmd = NULL;
4324 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4325 cmd = &got_histedit_cmds[i];
4326 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4327 isspace((unsigned char)p[strlen(cmd->name)])) {
4328 p += strlen(cmd->name);
4329 break;
4331 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4332 p++;
4333 break;
4336 if (i == nitems(got_histedit_cmds)) {
4337 err = histedit_syntax_error(lineno);
4338 break;
4340 while (isspace((unsigned char)p[0]))
4341 p++;
4342 if (cmd->code == GOT_HISTEDIT_MESG) {
4343 if (hle == NULL || hle->logmsg != NULL) {
4344 err = got_error(GOT_ERR_HISTEDIT_CMD);
4345 break;
4347 if (p[0] == '\0') {
4348 err = histedit_edit_logmsg(hle, repo);
4349 if (err)
4350 break;
4351 } else {
4352 hle->logmsg = strdup(p);
4353 if (hle->logmsg == NULL) {
4354 err = got_error_from_errno("strdup");
4355 break;
4358 free(line);
4359 line = NULL;
4360 continue;
4361 } else {
4362 end = p;
4363 while (end[0] && !isspace((unsigned char)end[0]))
4364 end++;
4365 *end = '\0';
4367 err = got_object_resolve_id_str(&commit_id, repo, p);
4368 if (err) {
4369 /* override error code */
4370 err = histedit_syntax_error(lineno);
4371 break;
4374 hle = malloc(sizeof(*hle));
4375 if (hle == NULL) {
4376 err = got_error_from_errno("malloc");
4377 break;
4379 hle->cmd = cmd;
4380 hle->commit_id = commit_id;
4381 hle->logmsg = NULL;
4382 commit_id = NULL;
4383 free(line);
4384 line = NULL;
4385 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4388 free(line);
4389 free(commit_id);
4390 return err;
4393 static const struct got_error *
4394 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4395 const char *path, struct got_repository *repo)
4397 const struct got_error *err = NULL;
4398 char *editor;
4399 FILE *f = NULL;
4401 err = get_editor(&editor);
4402 if (err)
4403 return err;
4405 if (spawn_editor(editor, path) == -1) {
4406 err = got_error_from_errno("failed spawning editor");
4407 goto done;
4410 f = fopen(path, "r");
4411 if (f == NULL) {
4412 err = got_error_from_errno("fopen");
4413 goto done;
4415 err = histedit_parse_list(histedit_cmds, f, repo);
4416 done:
4417 if (f && fclose(f) != 0 && err == NULL)
4418 err = got_error_from_errno("fclose");
4419 free(editor);
4420 return err;
4423 static const struct got_error *
4424 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4425 struct got_object_id_queue *, const char *, struct got_repository *);
4427 static const struct got_error *
4428 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4429 struct got_object_id_queue *commits, struct got_repository *repo)
4431 const struct got_error *err;
4432 FILE *f = NULL;
4433 char *path = NULL;
4435 err = got_opentemp_named(&path, &f, "got-histedit");
4436 if (err)
4437 return err;
4439 err = write_cmd_list(f);
4440 if (err)
4441 goto done;
4443 err = histedit_write_commit_list(commits, f, repo);
4444 if (err)
4445 goto done;
4447 if (fclose(f) != 0) {
4448 err = got_error_from_errno("fclose");
4449 goto done;
4451 f = NULL;
4453 err = histedit_run_editor(histedit_cmds, path, repo);
4454 if (err) {
4455 const char *errmsg = err->msg;
4456 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4457 goto done;
4458 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4459 commits, path, repo);
4461 done:
4462 if (f && fclose(f) != 0 && err == NULL)
4463 err = got_error_from_errno("fclose");
4464 if (path && unlink(path) != 0 && err == NULL)
4465 err = got_error_from_errno2("unlink", path);
4466 free(path);
4467 return err;
4470 static const struct got_error *
4471 histedit_save_list(struct got_histedit_list *histedit_cmds,
4472 struct got_worktree *worktree, struct got_repository *repo)
4474 const struct got_error *err = NULL;
4475 char *path = NULL;
4476 FILE *f = NULL;
4477 struct got_histedit_list_entry *hle;
4478 struct got_commit_object *commit = NULL;
4480 err = got_worktree_get_histedit_list_path(&path, worktree);
4481 if (err)
4482 return err;
4484 f = fopen(path, "w");
4485 if (f == NULL) {
4486 err = got_error_from_errno2("fopen", path);
4487 goto done;
4489 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4490 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4491 repo);
4492 if (err)
4493 break;
4495 if (hle->logmsg) {
4496 int n = fprintf(f, "%c %s\n",
4497 GOT_HISTEDIT_MESG, hle->logmsg);
4498 if (n < 0) {
4499 err = got_ferror(f, GOT_ERR_IO);
4500 break;
4504 done:
4505 if (f && fclose(f) != 0 && err == NULL)
4506 err = got_error_from_errno("fclose");
4507 free(path);
4508 if (commit)
4509 got_object_commit_close(commit);
4510 return err;
4513 static const struct got_error *
4514 histedit_load_list(struct got_histedit_list *histedit_cmds,
4515 const char *path, struct got_repository *repo)
4517 const struct got_error *err = NULL;
4518 FILE *f = NULL;
4520 f = fopen(path, "r");
4521 if (f == NULL) {
4522 err = got_error_from_errno2("fopen", path);
4523 goto done;
4526 err = histedit_parse_list(histedit_cmds, f, repo);
4527 done:
4528 if (f && fclose(f) != 0 && err == NULL)
4529 err = got_error_from_errno("fclose");
4530 return err;
4533 static const struct got_error *
4534 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4535 const char *errmsg, struct got_object_id_queue *commits,
4536 const char *path, struct got_repository *repo)
4538 const struct got_error *err = NULL;
4539 int resp = ' ';
4541 while (resp != 'c' && resp != 'r' && resp != 'a') {
4542 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4543 "or (a)bort: ", getprogname(), errmsg);
4544 resp = getchar();
4545 if (resp == 'c') {
4546 err = histedit_run_editor(histedit_cmds, path, repo);
4547 if (err) {
4548 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4549 break;
4550 resp = ' ';
4551 continue;
4553 } else if (resp == 'r') {
4554 err = histedit_edit_script(histedit_cmds,
4555 commits, repo);
4556 if (err) {
4557 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4558 break;
4559 resp = ' ';
4560 continue;
4562 } else if (resp == 'a') {
4563 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4564 break;
4565 } else
4566 printf("invalid response '%c'\n", resp);
4569 return err;
4572 static const struct got_error *
4573 histedit_complete(struct got_worktree *worktree,
4574 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4575 struct got_reference *branch, struct got_repository *repo)
4577 printf("Switching work tree to %s\n",
4578 got_ref_get_symref_target(branch));
4579 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4580 branch, repo);
4583 static const struct got_error *
4584 show_histedit_progress(struct got_commit_object *commit,
4585 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4587 const struct got_error *err;
4588 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4590 err = got_object_id_str(&old_id_str, hle->commit_id);
4591 if (err)
4592 goto done;
4594 if (new_id) {
4595 err = got_object_id_str(&new_id_str, new_id);
4596 if (err)
4597 goto done;
4600 old_id_str[12] = '\0';
4601 if (new_id_str)
4602 new_id_str[12] = '\0';
4604 if (hle->logmsg) {
4605 logmsg = strdup(hle->logmsg);
4606 if (logmsg == NULL) {
4607 err = got_error_from_errno("strdup");
4608 goto done;
4610 trim_logmsg(logmsg, 42);
4611 } else {
4612 err = get_short_logmsg(&logmsg, 42, commit);
4613 if (err)
4614 goto done;
4617 switch (hle->cmd->code) {
4618 case GOT_HISTEDIT_PICK:
4619 case GOT_HISTEDIT_EDIT:
4620 printf("%s -> %s: %s\n", old_id_str,
4621 new_id_str ? new_id_str : "no-op change", logmsg);
4622 break;
4623 case GOT_HISTEDIT_DROP:
4624 case GOT_HISTEDIT_FOLD:
4625 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4626 logmsg);
4627 break;
4628 default:
4629 break;
4632 done:
4633 free(old_id_str);
4634 free(new_id_str);
4635 return err;
4638 static const struct got_error *
4639 histedit_commit(struct got_pathlist_head *merged_paths,
4640 struct got_worktree *worktree, struct got_fileindex *fileindex,
4641 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4642 struct got_repository *repo)
4644 const struct got_error *err;
4645 struct got_commit_object *commit;
4646 struct got_object_id *new_commit_id;
4648 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4649 && hle->logmsg == NULL) {
4650 err = histedit_edit_logmsg(hle, repo);
4651 if (err)
4652 return err;
4655 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4656 if (err)
4657 return err;
4659 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4660 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4661 hle->logmsg, repo);
4662 if (err) {
4663 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4664 goto done;
4665 err = show_histedit_progress(commit, hle, NULL);
4666 } else {
4667 err = show_histedit_progress(commit, hle, new_commit_id);
4668 free(new_commit_id);
4670 done:
4671 got_object_commit_close(commit);
4672 return err;
4675 static const struct got_error *
4676 histedit_skip_commit(struct got_histedit_list_entry *hle,
4677 struct got_worktree *worktree, struct got_repository *repo)
4679 const struct got_error *error;
4680 struct got_commit_object *commit;
4682 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4683 repo);
4684 if (error)
4685 return error;
4687 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4688 if (error)
4689 return error;
4691 error = show_histedit_progress(commit, hle, NULL);
4692 got_object_commit_close(commit);
4693 return error;
4696 static const struct got_error *
4697 histedit_check_script(struct got_histedit_list *histedit_cmds,
4698 struct got_object_id_queue *commits, struct got_repository *repo)
4700 const struct got_error *err = NULL;
4701 struct got_object_qid *qid;
4702 struct got_histedit_list_entry *hle;
4703 static char msg[80];
4704 char *id_str;
4706 if (TAILQ_EMPTY(histedit_cmds))
4707 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4708 "histedit script contains no commands");
4710 SIMPLEQ_FOREACH(qid, commits, entry) {
4711 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4712 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4713 break;
4715 if (hle == NULL) {
4716 err = got_object_id_str(&id_str, qid->id);
4717 if (err)
4718 return err;
4719 snprintf(msg, sizeof(msg),
4720 "commit %s missing from histedit script", id_str);
4721 free(id_str);
4722 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4726 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4727 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4728 "last commit in histedit script cannot be folded");
4730 return NULL;
4733 static const struct got_error *
4734 cmd_histedit(int argc, char *argv[])
4736 const struct got_error *error = NULL;
4737 struct got_worktree *worktree = NULL;
4738 struct got_fileindex *fileindex = NULL;
4739 struct got_repository *repo = NULL;
4740 char *cwd = NULL;
4741 struct got_reference *branch = NULL;
4742 struct got_reference *tmp_branch = NULL;
4743 struct got_object_id *resume_commit_id = NULL;
4744 struct got_object_id *base_commit_id = NULL;
4745 struct got_object_id *head_commit_id = NULL;
4746 struct got_commit_object *commit = NULL;
4747 int ch, rebase_in_progress = 0;
4748 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4749 const char *edit_script_path = NULL;
4750 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4751 struct got_object_id_queue commits;
4752 struct got_pathlist_head merged_paths;
4753 const struct got_object_id_queue *parent_ids;
4754 struct got_object_qid *pid;
4755 struct got_histedit_list histedit_cmds;
4756 struct got_histedit_list_entry *hle;
4758 SIMPLEQ_INIT(&commits);
4759 TAILQ_INIT(&histedit_cmds);
4760 TAILQ_INIT(&merged_paths);
4762 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4763 switch (ch) {
4764 case 'a':
4765 abort_edit = 1;
4766 break;
4767 case 'c':
4768 continue_edit = 1;
4769 break;
4770 case 'F':
4771 edit_script_path = optarg;
4772 break;
4773 default:
4774 usage_histedit();
4775 /* NOTREACHED */
4779 argc -= optind;
4780 argv += optind;
4782 #ifndef PROFILE
4783 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4784 "unveil", NULL) == -1)
4785 err(1, "pledge");
4786 #endif
4787 if (abort_edit && continue_edit)
4788 usage_histedit();
4789 if (argc != 0)
4790 usage_histedit();
4793 * This command cannot apply unveil(2) in all cases because the
4794 * user may choose to run an editor to edit the histedit script
4795 * and to edit individual commit log messages.
4796 * unveil(2) traverses exec(2); if an editor is used we have to
4797 * apply unveil after edit script and log messages have been written.
4798 * XXX TODO: Make use of unveil(2) where possible.
4801 cwd = getcwd(NULL, 0);
4802 if (cwd == NULL) {
4803 error = got_error_from_errno("getcwd");
4804 goto done;
4806 error = got_worktree_open(&worktree, cwd);
4807 if (error)
4808 goto done;
4810 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4811 if (error != NULL)
4812 goto done;
4814 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4815 if (error)
4816 goto done;
4817 if (rebase_in_progress) {
4818 error = got_error(GOT_ERR_REBASING);
4819 goto done;
4822 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4823 if (error)
4824 goto done;
4826 if (edit_in_progress && abort_edit) {
4827 int did_something;
4828 error = got_worktree_histedit_continue(&resume_commit_id,
4829 &tmp_branch, &branch, &base_commit_id, &fileindex,
4830 worktree, repo);
4831 if (error)
4832 goto done;
4833 printf("Switching work tree to %s\n",
4834 got_ref_get_symref_target(branch));
4835 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4836 branch, base_commit_id, update_progress, &did_something);
4837 if (error)
4838 goto done;
4839 printf("Histedit of %s aborted\n",
4840 got_ref_get_symref_target(branch));
4841 goto done; /* nothing else to do */
4842 } else if (abort_edit) {
4843 error = got_error(GOT_ERR_NOT_HISTEDIT);
4844 goto done;
4847 if (continue_edit) {
4848 char *path;
4850 if (!edit_in_progress) {
4851 error = got_error(GOT_ERR_NOT_HISTEDIT);
4852 goto done;
4855 error = got_worktree_get_histedit_list_path(&path, worktree);
4856 if (error)
4857 goto done;
4859 error = histedit_load_list(&histedit_cmds, path, repo);
4860 free(path);
4861 if (error)
4862 goto done;
4864 error = got_worktree_histedit_continue(&resume_commit_id,
4865 &tmp_branch, &branch, &base_commit_id, &fileindex,
4866 worktree, repo);
4867 if (error)
4868 goto done;
4870 error = got_ref_resolve(&head_commit_id, repo, branch);
4871 if (error)
4872 goto done;
4874 error = got_object_open_as_commit(&commit, repo,
4875 head_commit_id);
4876 if (error)
4877 goto done;
4878 parent_ids = got_object_commit_get_parent_ids(commit);
4879 pid = SIMPLEQ_FIRST(parent_ids);
4880 if (pid == NULL) {
4881 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4882 goto done;
4884 error = collect_commits(&commits, head_commit_id, pid->id,
4885 base_commit_id, got_worktree_get_path_prefix(worktree),
4886 GOT_ERR_HISTEDIT_PATH, repo);
4887 got_object_commit_close(commit);
4888 commit = NULL;
4889 if (error)
4890 goto done;
4891 } else {
4892 if (edit_in_progress) {
4893 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4894 goto done;
4897 error = got_ref_open(&branch, repo,
4898 got_worktree_get_head_ref_name(worktree), 0);
4899 if (error != NULL)
4900 goto done;
4902 error = got_ref_resolve(&head_commit_id, repo, branch);
4903 if (error)
4904 goto done;
4906 error = got_object_open_as_commit(&commit, repo,
4907 head_commit_id);
4908 if (error)
4909 goto done;
4910 parent_ids = got_object_commit_get_parent_ids(commit);
4911 pid = SIMPLEQ_FIRST(parent_ids);
4912 if (pid == NULL) {
4913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4914 goto done;
4916 error = collect_commits(&commits, head_commit_id, pid->id,
4917 got_worktree_get_base_commit_id(worktree),
4918 got_worktree_get_path_prefix(worktree),
4919 GOT_ERR_HISTEDIT_PATH, repo);
4920 got_object_commit_close(commit);
4921 commit = NULL;
4922 if (error)
4923 goto done;
4925 if (edit_script_path) {
4926 error = histedit_load_list(&histedit_cmds,
4927 edit_script_path, repo);
4928 if (error)
4929 goto done;
4930 } else {
4931 error = histedit_edit_script(&histedit_cmds, &commits,
4932 repo);
4933 if (error)
4934 goto done;
4938 error = histedit_save_list(&histedit_cmds, worktree,
4939 repo);
4940 if (error)
4941 goto done;
4943 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4944 &base_commit_id, &fileindex, worktree, repo);
4945 if (error)
4946 goto done;
4950 error = histedit_check_script(&histedit_cmds, &commits, repo);
4951 if (error)
4952 goto done;
4954 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4955 if (resume_commit_id) {
4956 if (got_object_id_cmp(hle->commit_id,
4957 resume_commit_id) != 0)
4958 continue;
4960 resume_commit_id = NULL;
4961 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4962 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4963 error = histedit_skip_commit(hle, worktree,
4964 repo);
4965 } else {
4966 error = histedit_commit(NULL, worktree,
4967 fileindex, tmp_branch, hle, repo);
4969 if (error)
4970 goto done;
4971 continue;
4974 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4975 error = histedit_skip_commit(hle, worktree, repo);
4976 if (error)
4977 goto done;
4978 continue;
4981 error = got_object_open_as_commit(&commit, repo,
4982 hle->commit_id);
4983 if (error)
4984 goto done;
4985 parent_ids = got_object_commit_get_parent_ids(commit);
4986 pid = SIMPLEQ_FIRST(parent_ids);
4988 error = got_worktree_histedit_merge_files(&merged_paths,
4989 worktree, fileindex, pid->id, hle->commit_id, repo,
4990 rebase_progress, &rebase_status, check_cancelled, NULL);
4991 if (error)
4992 goto done;
4993 got_object_commit_close(commit);
4994 commit = NULL;
4996 if (rebase_status == GOT_STATUS_CONFLICT) {
4997 got_worktree_rebase_pathlist_free(&merged_paths);
4998 break;
5001 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5002 char *id_str;
5003 error = got_object_id_str(&id_str, hle->commit_id);
5004 if (error)
5005 goto done;
5006 printf("Stopping histedit for amending commit %s\n",
5007 id_str);
5008 free(id_str);
5009 got_worktree_rebase_pathlist_free(&merged_paths);
5010 error = got_worktree_histedit_postpone(worktree,
5011 fileindex);
5012 goto done;
5015 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5016 error = histedit_skip_commit(hle, worktree, repo);
5017 if (error)
5018 goto done;
5019 continue;
5022 error = histedit_commit(&merged_paths, worktree, fileindex,
5023 tmp_branch, hle, repo);
5024 got_worktree_rebase_pathlist_free(&merged_paths);
5025 if (error)
5026 goto done;
5029 if (rebase_status == GOT_STATUS_CONFLICT) {
5030 error = got_worktree_histedit_postpone(worktree, fileindex);
5031 if (error)
5032 goto done;
5033 error = got_error_msg(GOT_ERR_CONFLICTS,
5034 "conflicts must be resolved before rebasing can continue");
5035 } else
5036 error = histedit_complete(worktree, fileindex, tmp_branch,
5037 branch, repo);
5038 done:
5039 got_object_id_queue_free(&commits);
5040 free(head_commit_id);
5041 free(base_commit_id);
5042 free(resume_commit_id);
5043 if (commit)
5044 got_object_commit_close(commit);
5045 if (branch)
5046 got_ref_close(branch);
5047 if (tmp_branch)
5048 got_ref_close(tmp_branch);
5049 if (worktree)
5050 got_worktree_close(worktree);
5051 if (repo)
5052 got_repo_close(repo);
5053 return error;