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_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
58 static void
59 catch_sigint(int signo)
60 {
61 sigint_received = 1;
62 }
64 static void
65 catch_sigpipe(int signo)
66 {
67 sigpipe_received = 1;
68 }
71 struct got_cmd {
72 const char *cmd_name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
76 };
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
99 static const struct got_error* cmd_init(int, char *[]);
100 static const struct got_error* cmd_import(int, char *[]);
101 static const struct got_error* cmd_checkout(int, char *[]);
102 static const struct got_error* cmd_update(int, char *[]);
103 static const struct got_error* cmd_log(int, char *[]);
104 static const struct got_error* cmd_diff(int, char *[]);
105 static const struct got_error* cmd_blame(int, char *[]);
106 static const struct got_error* cmd_tree(int, char *[]);
107 static const struct got_error* cmd_status(int, char *[]);
108 static const struct got_error* cmd_ref(int, char *[]);
109 static const struct got_error* cmd_branch(int, char *[]);
110 static const struct got_error* cmd_add(int, char *[]);
111 static const struct got_error* cmd_remove(int, char *[]);
112 static const struct got_error* cmd_revert(int, char *[]);
113 static const struct got_error* cmd_commit(int, char *[]);
114 static const struct got_error* cmd_cherrypick(int, char *[]);
115 static const struct got_error* cmd_backout(int, char *[]);
116 static const struct got_error* cmd_rebase(int, char *[]);
117 static const struct got_error* cmd_histedit(int, char *[]);
119 static struct got_cmd got_commands[] = {
120 { "init", cmd_init, usage_init, "" },
121 { "import", cmd_import, usage_import, "" },
122 { "checkout", cmd_checkout, usage_checkout, "co" },
123 { "update", cmd_update, usage_update, "up" },
124 { "log", cmd_log, usage_log, "" },
125 { "diff", cmd_diff, usage_diff, "" },
126 { "blame", cmd_blame, usage_blame, "" },
127 { "tree", cmd_tree, usage_tree, "" },
128 { "status", cmd_status, usage_status, "st" },
129 { "ref", cmd_ref, usage_ref, "" },
130 { "branch", cmd_branch, usage_branch, "br" },
131 { "add", cmd_add, usage_add, "" },
132 { "remove", cmd_remove, usage_remove, "rm" },
133 { "revert", cmd_revert, usage_revert, "rv" },
134 { "commit", cmd_commit, usage_commit, "ci" },
135 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
136 { "backout", cmd_backout, usage_backout, "bo" },
137 { "rebase", cmd_rebase, usage_rebase, "rb" },
138 { "histedit", cmd_histedit, usage_histedit, "he" },
139 };
141 static void
142 list_commands(void)
144 int i;
146 fprintf(stderr, "commands:");
147 for (i = 0; i < nitems(got_commands); i++) {
148 struct got_cmd *cmd = &got_commands[i];
149 fprintf(stderr, " %s", cmd->cmd_name);
151 fputc('\n', stderr);
154 int
155 main(int argc, char *argv[])
157 struct got_cmd *cmd;
158 unsigned int i;
159 int ch;
160 int hflag = 0, Vflag = 0;
162 setlocale(LC_CTYPE, "");
164 while ((ch = getopt(argc, argv, "hV")) != -1) {
165 switch (ch) {
166 case 'h':
167 hflag = 1;
168 break;
169 case 'V':
170 Vflag = 1;
171 break;
172 default:
173 usage(hflag);
174 /* NOTREACHED */
178 argc -= optind;
179 argv += optind;
180 optind = 0;
182 if (Vflag) {
183 got_version_print_str();
184 return 1;
187 if (argc <= 0)
188 usage(hflag);
190 signal(SIGINT, catch_sigint);
191 signal(SIGPIPE, catch_sigpipe);
193 for (i = 0; i < nitems(got_commands); i++) {
194 const struct got_error *error;
196 cmd = &got_commands[i];
198 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
199 strcmp(cmd->cmd_alias, argv[0]) != 0)
200 continue;
202 if (hflag)
203 got_commands[i].cmd_usage();
205 error = got_commands[i].cmd_main(argc, argv);
206 if (error && !(sigint_received || sigpipe_received)) {
207 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
208 return 1;
211 return 0;
214 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
215 list_commands();
216 return 1;
219 __dead static void
220 usage(int hflag)
222 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
223 getprogname());
224 if (hflag)
225 list_commands();
226 exit(1);
229 static const struct got_error *
230 get_editor(char **abspath)
232 const struct got_error *err = NULL;
233 const char *editor;
235 editor = getenv("VISUAL");
236 if (editor == NULL)
237 editor = getenv("EDITOR");
239 if (editor) {
240 err = got_path_find_prog(abspath, editor);
241 if (err)
242 return err;
245 if (*abspath == NULL) {
246 *abspath = strdup("/bin/ed");
247 if (*abspath == NULL)
248 return got_error_from_errno("strdup");
251 return NULL;
254 static const struct got_error *
255 apply_unveil(const char *repo_path, int repo_read_only,
256 const char *worktree_path)
258 const struct got_error *err;
260 #ifdef PROFILE
261 if (unveil("gmon.out", "rwc") != 0)
262 return got_error_from_errno2("unveil", "gmon.out");
263 #endif
264 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 return got_error_from_errno2("unveil", repo_path);
267 if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 return got_error_from_errno2("unveil", worktree_path);
270 if (unveil("/tmp", "rwc") != 0)
271 return got_error_from_errno2("unveil", "/tmp");
273 err = got_privsep_unveil_exec_helpers();
274 if (err != NULL)
275 return err;
277 if (unveil(NULL, NULL) != 0)
278 return got_error_from_errno("unveil");
280 return NULL;
283 __dead static void
284 usage_init(void)
286 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
287 exit(1);
290 static const struct got_error *
291 cmd_init(int argc, char *argv[])
293 const struct got_error *error = NULL;
294 char *repo_path = NULL;
295 int ch;
297 while ((ch = getopt(argc, argv, "")) != -1) {
298 switch (ch) {
299 default:
300 usage_init();
301 /* NOTREACHED */
305 argc -= optind;
306 argv += optind;
308 #ifndef PROFILE
309 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
310 err(1, "pledge");
311 #endif
312 if (argc != 1)
313 usage_init();
315 repo_path = strdup(argv[0]);
316 if (repo_path == NULL)
317 return got_error_from_errno("strdup");
319 got_path_strip_trailing_slashes(repo_path);
321 error = got_path_mkdir(repo_path);
322 if (error &&
323 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
324 goto done;
326 error = apply_unveil(repo_path, 0, NULL);
327 if (error)
328 goto done;
330 error = got_repo_init(repo_path);
331 if (error != NULL)
332 goto done;
334 done:
335 free(repo_path);
336 return error;
339 __dead static void
340 usage_import(void)
342 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
343 "[-r repository-path] [-I pattern] path\n", getprogname());
344 exit(1);
347 int
348 spawn_editor(const char *editor, const char *file)
350 pid_t pid;
351 sig_t sighup, sigint, sigquit;
352 int st = -1;
354 sighup = signal(SIGHUP, SIG_IGN);
355 sigint = signal(SIGINT, SIG_IGN);
356 sigquit = signal(SIGQUIT, SIG_IGN);
358 switch (pid = fork()) {
359 case -1:
360 goto doneediting;
361 case 0:
362 execl(editor, editor, file, (char *)NULL);
363 _exit(127);
366 while (waitpid(pid, &st, 0) == -1)
367 if (errno != EINTR)
368 break;
370 doneediting:
371 (void)signal(SIGHUP, sighup);
372 (void)signal(SIGINT, sigint);
373 (void)signal(SIGQUIT, sigquit);
375 if (!WIFEXITED(st)) {
376 errno = EINTR;
377 return -1;
380 return WEXITSTATUS(st);
383 static const struct got_error *
384 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
385 const char *initial_content)
387 const struct got_error *err = NULL;
388 char buf[1024];
389 struct stat st, st2;
390 FILE *fp;
391 int content_changed = 0;
392 size_t len;
394 *logmsg = NULL;
396 if (stat(logmsg_path, &st) == -1)
397 return got_error_from_errno2("stat", logmsg_path);
399 if (spawn_editor(editor, logmsg_path) == -1)
400 return got_error_from_errno("failed spawning editor");
402 if (stat(logmsg_path, &st2) == -1)
403 return got_error_from_errno("stat");
405 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
406 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
407 "no changes made to commit message, aborting");
409 *logmsg = malloc(st2.st_size + 1);
410 if (*logmsg == NULL)
411 return got_error_from_errno("malloc");
412 (*logmsg)[0] = '\0';
413 len = 0;
415 fp = fopen(logmsg_path, "r");
416 if (fp == NULL) {
417 err = got_error_from_errno("fopen");
418 goto done;
420 while (fgets(buf, sizeof(buf), fp) != NULL) {
421 if (!content_changed && strcmp(buf, initial_content) != 0)
422 content_changed = 1;
423 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
424 continue; /* remove comments and leading empty lines */
425 len = strlcat(*logmsg, buf, st2.st_size);
427 fclose(fp);
429 while (len > 0 && (*logmsg)[len - 1] == '\n') {
430 (*logmsg)[len - 1] = '\0';
431 len--;
434 if (len == 0 || !content_changed)
435 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
436 "commit message cannot be empty, aborting");
437 done:
438 if (err) {
439 free(*logmsg);
440 *logmsg = NULL;
442 return err;
445 static const struct got_error *
446 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
447 const char *branch_name)
449 char *initial_content = NULL, *logmsg_path = NULL;
450 const struct got_error *err = NULL;
451 int fd;
453 if (asprintf(&initial_content,
454 "\n# %s to be imported to branch %s\n", path_dir,
455 branch_name) == -1)
456 return got_error_from_errno("asprintf");
458 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
459 if (err)
460 goto done;
462 dprintf(fd, initial_content);
463 close(fd);
465 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
466 done:
467 free(initial_content);
468 free(logmsg_path);
469 return err;
472 static const struct got_error *
473 import_progress(void *arg, const char *path)
475 printf("A %s\n", path);
476 return NULL;
479 static const struct got_error *
480 cmd_import(int argc, char *argv[])
482 const struct got_error *error = NULL;
483 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
484 char *editor = NULL;
485 const char *got_author = getenv("GOT_AUTHOR");
486 const char *branch_name = "master";
487 char *refname = NULL, *id_str = NULL;
488 struct got_repository *repo = NULL;
489 struct got_reference *branch_ref = NULL, *head_ref = NULL;
490 struct got_object_id *new_commit_id = NULL;
491 int ch;
492 struct got_pathlist_head ignores;
493 struct got_pathlist_entry *pe;
495 TAILQ_INIT(&ignores);
497 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
498 switch (ch) {
499 case 'b':
500 branch_name = optarg;
501 break;
502 case 'm':
503 logmsg = strdup(optarg);
504 if (logmsg == NULL) {
505 error = got_error_from_errno("strdup");
506 goto done;
508 break;
509 case 'r':
510 repo_path = realpath(optarg, NULL);
511 if (repo_path == NULL) {
512 error = got_error_from_errno("realpath");
513 goto done;
515 break;
516 case 'I':
517 if (optarg[0] == '\0')
518 break;
519 error = got_pathlist_insert(&pe, &ignores, optarg,
520 NULL);
521 if (error)
522 goto done;
523 break;
524 default:
525 usage_init();
526 /* NOTREACHED */
530 argc -= optind;
531 argv += optind;
533 #ifndef PROFILE
534 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
535 NULL) == -1)
536 err(1, "pledge");
537 #endif
538 if (argc != 1)
539 usage_import();
541 if (got_author == NULL) {
542 /* TODO: Look current user up in password database */
543 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 goto done;
547 if (repo_path == NULL) {
548 repo_path = getcwd(NULL, 0);
549 if (repo_path == NULL)
550 return got_error_from_errno("getcwd");
552 got_path_strip_trailing_slashes(repo_path);
553 error = got_repo_open(&repo, repo_path);
554 if (error)
555 goto done;
557 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
558 error = got_error_from_errno("asprintf");
559 goto done;
562 error = got_ref_open(&branch_ref, repo, refname, 0);
563 if (error) {
564 if (error->code != GOT_ERR_NOT_REF)
565 goto done;
566 } else {
567 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
568 "import target branch already exists");
569 goto done;
572 path_dir = realpath(argv[0], NULL);
573 if (path_dir == NULL) {
574 error = got_error_from_errno("realpath");
575 goto done;
577 got_path_strip_trailing_slashes(path_dir);
579 /*
580 * unveil(2) traverses exec(2); if an editor is used we have
581 * to apply unveil after the log message has been written.
582 */
583 if (logmsg == NULL || strlen(logmsg) == 0) {
584 error = get_editor(&editor);
585 if (error)
586 goto done;
587 error = collect_import_msg(&logmsg, editor, path_dir, refname);
588 if (error)
589 goto done;
592 if (unveil(path_dir, "r") != 0)
593 return got_error_from_errno2("unveil", path_dir);
595 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
596 if (error)
597 goto done;
599 error = got_repo_import(&new_commit_id, path_dir, logmsg,
600 got_author, &ignores, repo, import_progress, NULL);
601 if (error)
602 goto done;
604 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
605 if (error)
606 goto done;
608 error = got_ref_write(branch_ref, repo);
609 if (error)
610 goto done;
612 error = got_object_id_str(&id_str, new_commit_id);
613 if (error)
614 goto done;
616 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
617 if (error) {
618 if (error->code != GOT_ERR_NOT_REF)
619 goto done;
621 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
622 branch_ref);
623 if (error)
624 goto done;
626 error = got_ref_write(head_ref, repo);
627 if (error)
628 goto done;
631 printf("Created branch %s with commit %s\n",
632 got_ref_get_name(branch_ref), id_str);
633 done:
634 free(repo_path);
635 free(editor);
636 free(refname);
637 free(new_commit_id);
638 free(id_str);
639 if (branch_ref)
640 got_ref_close(branch_ref);
641 if (head_ref)
642 got_ref_close(head_ref);
643 return error;
646 __dead static void
647 usage_checkout(void)
649 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
650 "[-p prefix] repository-path [worktree-path]\n", getprogname());
651 exit(1);
654 static const struct got_error *
655 checkout_progress(void *arg, unsigned char status, const char *path)
657 char *worktree_path = arg;
659 /* Base commit bump happens silently. */
660 if (status == GOT_STATUS_BUMP_BASE)
661 return NULL;
663 while (path[0] == '/')
664 path++;
666 printf("%c %s/%s\n", status, worktree_path, path);
667 return NULL;
670 static const struct got_error *
671 check_cancelled(void *arg)
673 if (sigint_received || sigpipe_received)
674 return got_error(GOT_ERR_CANCELLED);
675 return NULL;
678 static const struct got_error *
679 check_linear_ancestry(struct got_object_id *commit_id,
680 struct got_object_id *base_commit_id, struct got_repository *repo)
682 const struct got_error *err = NULL;
683 struct got_object_id *yca_id;
685 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
686 commit_id, base_commit_id, repo);
687 if (err)
688 return err;
690 if (yca_id == NULL)
691 return got_error(GOT_ERR_ANCESTRY);
693 /*
694 * Require a straight line of history between the target commit
695 * and the work tree's base commit.
697 * Non-linear situations such as this require a rebase:
699 * (commit) D F (base_commit)
700 * \ /
701 * C E
702 * \ /
703 * B (yca)
704 * |
705 * A
707 * 'got update' only handles linear cases:
708 * Update forwards in time: A (base/yca) - B - C - D (commit)
709 * Update backwards in time: D (base) - C - B - A (commit/yca)
710 */
711 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
712 got_object_id_cmp(base_commit_id, yca_id) != 0)
713 return got_error(GOT_ERR_ANCESTRY);
715 free(yca_id);
716 return NULL;
719 static const struct got_error *
720 check_same_branch(struct got_object_id *commit_id,
721 struct got_reference *head_ref, struct got_object_id *yca_id,
722 struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_commit_graph *graph = NULL;
726 struct got_object_id *head_commit_id = NULL;
727 int is_same_branch = 0;
729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
730 if (err)
731 goto done;
733 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
734 is_same_branch = 1;
735 goto done;
737 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
738 is_same_branch = 1;
739 goto done;
742 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
743 if (err)
744 goto done;
746 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
747 if (err)
748 goto done;
750 for (;;) {
751 struct got_object_id *id;
752 err = got_commit_graph_iter_next(&id, graph);
753 if (err) {
754 if (err->code == GOT_ERR_ITER_COMPLETED) {
755 err = NULL;
756 break;
757 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
758 break;
759 err = got_commit_graph_fetch_commits(graph, 1,
760 repo);
761 if (err)
762 break;
765 if (id) {
766 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
767 break;
768 if (got_object_id_cmp(id, commit_id) == 0) {
769 is_same_branch = 1;
770 break;
774 done:
775 if (graph)
776 got_commit_graph_close(graph);
777 free(head_commit_id);
778 if (!err && !is_same_branch)
779 err = got_error(GOT_ERR_ANCESTRY);
780 return err;
783 static const struct got_error *
784 resolve_commit_arg(struct got_object_id **commit_id,
785 const char *commit_id_arg, struct got_repository *repo)
787 const struct got_error *err;
788 struct got_reference *ref;
790 err = got_ref_open(&ref, repo, commit_id_arg, 0);
791 if (err == NULL) {
792 err = got_ref_resolve(commit_id, repo, ref);
793 got_ref_close(ref);
794 } else {
795 if (err->code != GOT_ERR_NOT_REF)
796 return err;
797 err = got_repo_match_object_id_prefix(commit_id,
798 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
800 return err;
803 static const struct got_error *
804 cmd_checkout(int argc, char *argv[])
806 const struct got_error *error = NULL;
807 struct got_repository *repo = NULL;
808 struct got_reference *head_ref = NULL;
809 struct got_worktree *worktree = NULL;
810 char *repo_path = NULL;
811 char *worktree_path = NULL;
812 const char *path_prefix = "";
813 const char *branch_name = GOT_REF_HEAD;
814 char *commit_id_str = NULL;
815 int ch, same_path_prefix;
816 struct got_pathlist_head paths;
818 TAILQ_INIT(&paths);
820 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
821 switch (ch) {
822 case 'b':
823 branch_name = optarg;
824 break;
825 case 'c':
826 commit_id_str = strdup(optarg);
827 if (commit_id_str == NULL)
828 return got_error_from_errno("strdup");
829 break;
830 case 'p':
831 path_prefix = optarg;
832 break;
833 default:
834 usage_checkout();
835 /* NOTREACHED */
839 argc -= optind;
840 argv += optind;
842 #ifndef PROFILE
843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
844 "unveil", NULL) == -1)
845 err(1, "pledge");
846 #endif
847 if (argc == 1) {
848 char *cwd, *base, *dotgit;
849 repo_path = realpath(argv[0], NULL);
850 if (repo_path == NULL)
851 return got_error_from_errno2("realpath", argv[0]);
852 cwd = getcwd(NULL, 0);
853 if (cwd == NULL) {
854 error = got_error_from_errno("getcwd");
855 goto done;
857 if (path_prefix[0]) {
858 base = basename(path_prefix);
859 if (base == NULL) {
860 error = got_error_from_errno2("basename",
861 path_prefix);
862 goto done;
864 } else {
865 base = basename(repo_path);
866 if (base == NULL) {
867 error = got_error_from_errno2("basename",
868 repo_path);
869 goto done;
872 dotgit = strstr(base, ".git");
873 if (dotgit)
874 *dotgit = '\0';
875 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
876 error = got_error_from_errno("asprintf");
877 free(cwd);
878 goto done;
880 free(cwd);
881 } else if (argc == 2) {
882 repo_path = realpath(argv[0], NULL);
883 if (repo_path == NULL) {
884 error = got_error_from_errno2("realpath", argv[0]);
885 goto done;
887 worktree_path = realpath(argv[1], NULL);
888 if (worktree_path == NULL) {
889 if (errno != ENOENT) {
890 error = got_error_from_errno2("realpath",
891 argv[1]);
892 goto done;
894 worktree_path = strdup(argv[1]);
895 if (worktree_path == NULL) {
896 error = got_error_from_errno("strdup");
897 goto done;
900 } else
901 usage_checkout();
903 got_path_strip_trailing_slashes(repo_path);
904 got_path_strip_trailing_slashes(worktree_path);
906 error = got_repo_open(&repo, repo_path);
907 if (error != NULL)
908 goto done;
910 /* Pre-create work tree path for unveil(2) */
911 error = got_path_mkdir(worktree_path);
912 if (error) {
913 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
914 goto done;
915 if (!got_path_dir_is_empty(worktree_path)) {
916 error = got_error_path(worktree_path,
917 GOT_ERR_DIR_NOT_EMPTY);
918 goto done;
922 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
923 if (error)
924 goto done;
926 error = got_ref_open(&head_ref, repo, branch_name, 0);
927 if (error != NULL)
928 goto done;
930 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
931 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
932 goto done;
934 error = got_worktree_open(&worktree, worktree_path);
935 if (error != NULL)
936 goto done;
938 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
939 path_prefix);
940 if (error != NULL)
941 goto done;
942 if (!same_path_prefix) {
943 error = got_error(GOT_ERR_PATH_PREFIX);
944 goto done;
947 if (commit_id_str) {
948 struct got_object_id *commit_id;
949 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
950 if (error)
951 goto done;
952 error = check_linear_ancestry(commit_id,
953 got_worktree_get_base_commit_id(worktree), repo);
954 if (error != NULL) {
955 free(commit_id);
956 goto done;
958 error = check_same_branch(commit_id, head_ref, NULL, repo);
959 if (error)
960 goto done;
961 error = got_worktree_set_base_commit_id(worktree, repo,
962 commit_id);
963 free(commit_id);
964 if (error)
965 goto done;
968 error = got_pathlist_append(&paths, "", NULL);
969 if (error)
970 goto done;
971 error = got_worktree_checkout_files(worktree, &paths, repo,
972 checkout_progress, worktree_path, check_cancelled, NULL);
973 if (error != NULL)
974 goto done;
976 printf("Now shut up and hack\n");
978 done:
979 got_pathlist_free(&paths);
980 free(commit_id_str);
981 free(repo_path);
982 free(worktree_path);
983 return error;
986 __dead static void
987 usage_update(void)
989 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
990 getprogname());
991 exit(1);
994 static const struct got_error *
995 update_progress(void *arg, unsigned char status, const char *path)
997 int *did_something = arg;
999 if (status == GOT_STATUS_EXISTS)
1000 return NULL;
1002 *did_something = 1;
1004 /* Base commit bump happens silently. */
1005 if (status == GOT_STATUS_BUMP_BASE)
1006 return NULL;
1008 while (path[0] == '/')
1009 path++;
1010 printf("%c %s\n", status, path);
1011 return NULL;
1014 static const struct got_error *
1015 switch_head_ref(struct got_reference *head_ref,
1016 struct got_object_id *commit_id, struct got_worktree *worktree,
1017 struct got_repository *repo)
1019 const struct got_error *err = NULL;
1020 char *base_id_str;
1021 int ref_has_moved = 0;
1023 /* Trivial case: switching between two different references. */
1024 if (strcmp(got_ref_get_name(head_ref),
1025 got_worktree_get_head_ref_name(worktree)) != 0) {
1026 printf("Switching work tree from %s to %s\n",
1027 got_worktree_get_head_ref_name(worktree),
1028 got_ref_get_name(head_ref));
1029 return got_worktree_set_head_ref(worktree, head_ref);
1032 err = check_linear_ancestry(commit_id,
1033 got_worktree_get_base_commit_id(worktree), repo);
1034 if (err) {
1035 if (err->code != GOT_ERR_ANCESTRY)
1036 return err;
1037 ref_has_moved = 1;
1039 if (!ref_has_moved)
1040 return NULL;
1042 /* Switching to a rebased branch with the same reference name. */
1043 err = got_object_id_str(&base_id_str,
1044 got_worktree_get_base_commit_id(worktree));
1045 if (err)
1046 return err;
1047 printf("Reference %s now points at a different branch\n",
1048 got_worktree_get_head_ref_name(worktree));
1049 printf("Switching work tree from %s to %s\n", base_id_str,
1050 got_worktree_get_head_ref_name(worktree));
1051 return NULL;
1054 static const struct got_error *
1055 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1057 const struct got_error *err;
1058 int in_progress;
1060 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1061 if (err)
1062 return err;
1063 if (in_progress)
1064 return got_error(GOT_ERR_REBASING);
1066 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1067 if (err)
1068 return err;
1069 if (in_progress)
1070 return got_error(GOT_ERR_HISTEDIT_BUSY);
1072 return NULL;
1075 static const struct got_error *
1076 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1077 char *argv[], struct got_worktree *worktree)
1079 const struct got_error *err;
1080 char *path;
1081 int i;
1083 if (argc == 0) {
1084 path = strdup("");
1085 if (path == NULL)
1086 return got_error_from_errno("strdup");
1087 return got_pathlist_append(paths, path, NULL);
1090 for (i = 0; i < argc; i++) {
1091 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1092 if (err)
1093 break;
1094 err = got_pathlist_append(paths, path, NULL);
1095 if (err) {
1096 free(path);
1097 break;
1101 return err;
1104 static const struct got_error *
1105 cmd_update(int argc, char *argv[])
1107 const struct got_error *error = NULL;
1108 struct got_repository *repo = NULL;
1109 struct got_worktree *worktree = NULL;
1110 char *worktree_path = NULL;
1111 struct got_object_id *commit_id = NULL;
1112 char *commit_id_str = NULL;
1113 const char *branch_name = NULL;
1114 struct got_reference *head_ref = NULL;
1115 struct got_pathlist_head paths;
1116 struct got_pathlist_entry *pe;
1117 int ch, did_something = 0;
1119 TAILQ_INIT(&paths);
1121 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1122 switch (ch) {
1123 case 'b':
1124 branch_name = optarg;
1125 break;
1126 case 'c':
1127 commit_id_str = strdup(optarg);
1128 if (commit_id_str == NULL)
1129 return got_error_from_errno("strdup");
1130 break;
1131 default:
1132 usage_update();
1133 /* NOTREACHED */
1137 argc -= optind;
1138 argv += optind;
1140 #ifndef PROFILE
1141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1142 "unveil", NULL) == -1)
1143 err(1, "pledge");
1144 #endif
1145 worktree_path = getcwd(NULL, 0);
1146 if (worktree_path == NULL) {
1147 error = got_error_from_errno("getcwd");
1148 goto done;
1150 error = got_worktree_open(&worktree, worktree_path);
1151 if (error)
1152 goto done;
1154 error = check_rebase_or_histedit_in_progress(worktree);
1155 if (error)
1156 goto done;
1158 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1159 if (error)
1160 goto done;
1162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1163 if (error != NULL)
1164 goto done;
1166 error = apply_unveil(got_repo_get_path(repo), 0,
1167 got_worktree_get_root_path(worktree));
1168 if (error)
1169 goto done;
1171 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1172 got_worktree_get_head_ref_name(worktree), 0);
1173 if (error != NULL)
1174 goto done;
1175 if (commit_id_str == NULL) {
1176 error = got_ref_resolve(&commit_id, repo, head_ref);
1177 if (error != NULL)
1178 goto done;
1179 error = got_object_id_str(&commit_id_str, commit_id);
1180 if (error != NULL)
1181 goto done;
1182 } else {
1183 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1184 free(commit_id_str);
1185 commit_id_str = NULL;
1186 if (error)
1187 goto done;
1188 error = got_object_id_str(&commit_id_str, commit_id);
1189 if (error)
1190 goto done;
1193 if (branch_name) {
1194 struct got_object_id *head_commit_id;
1195 TAILQ_FOREACH(pe, &paths, entry) {
1196 if (strlen(pe->path) == 0)
1197 continue;
1198 error = got_error_msg(GOT_ERR_BAD_PATH,
1199 "switching between branches requires that "
1200 "the entire work tree gets updated");
1201 goto done;
1203 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1204 if (error)
1205 goto done;
1206 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1207 free(head_commit_id);
1208 if (error != NULL)
1209 goto done;
1210 error = check_same_branch(commit_id, head_ref, NULL, repo);
1211 if (error)
1212 goto done;
1213 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1214 if (error)
1215 goto done;
1216 } else {
1217 error = check_linear_ancestry(commit_id,
1218 got_worktree_get_base_commit_id(worktree), repo);
1219 if (error != NULL) {
1220 if (error->code == GOT_ERR_ANCESTRY)
1221 error = got_error(GOT_ERR_BRANCH_MOVED);
1222 goto done;
1224 error = check_same_branch(commit_id, head_ref, NULL, repo);
1225 if (error)
1226 goto done;
1229 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1230 commit_id) != 0) {
1231 error = got_worktree_set_base_commit_id(worktree, repo,
1232 commit_id);
1233 if (error)
1234 goto done;
1237 error = got_worktree_checkout_files(worktree, &paths, repo,
1238 update_progress, &did_something, check_cancelled, NULL);
1239 if (error != NULL)
1240 goto done;
1242 if (did_something)
1243 printf("Updated to commit %s\n", commit_id_str);
1244 else
1245 printf("Already up-to-date\n");
1246 done:
1247 free(worktree_path);
1248 TAILQ_FOREACH(pe, &paths, entry)
1249 free((char *)pe->path);
1250 got_pathlist_free(&paths);
1251 free(commit_id);
1252 free(commit_id_str);
1253 return error;
1256 static const struct got_error *
1257 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1258 int diff_context, struct got_repository *repo)
1260 const struct got_error *err = NULL;
1261 struct got_tree_object *tree1 = NULL, *tree2;
1262 struct got_object_qid *qid;
1263 char *id_str1 = NULL, *id_str2;
1264 struct got_diff_blob_output_unidiff_arg arg;
1266 err = got_object_open_as_tree(&tree2, repo,
1267 got_object_commit_get_tree_id(commit));
1268 if (err)
1269 return err;
1271 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1272 if (qid != NULL) {
1273 struct got_commit_object *pcommit;
1275 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1276 if (err)
1277 return err;
1279 err = got_object_open_as_tree(&tree1, repo,
1280 got_object_commit_get_tree_id(pcommit));
1281 got_object_commit_close(pcommit);
1282 if (err)
1283 return err;
1285 err = got_object_id_str(&id_str1, qid->id);
1286 if (err)
1287 return err;
1290 err = got_object_id_str(&id_str2, id);
1291 if (err)
1292 goto done;
1294 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1295 arg.diff_context = diff_context;
1296 arg.outfile = stdout;
1297 err = got_diff_tree(tree1, tree2, "", "", repo,
1298 got_diff_blob_output_unidiff, &arg, 1);
1299 done:
1300 if (tree1)
1301 got_object_tree_close(tree1);
1302 got_object_tree_close(tree2);
1303 free(id_str1);
1304 free(id_str2);
1305 return err;
1308 static char *
1309 get_datestr(time_t *time, char *datebuf)
1311 char *p, *s = ctime_r(time, datebuf);
1312 p = strchr(s, '\n');
1313 if (p)
1314 *p = '\0';
1315 return s;
1318 static const struct got_error *
1319 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1320 struct got_repository *repo, int show_patch, int diff_context,
1321 struct got_reflist_head *refs)
1323 const struct got_error *err = NULL;
1324 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1325 char datebuf[26];
1326 time_t committer_time;
1327 const char *author, *committer;
1328 char *refs_str = NULL;
1329 struct got_reflist_entry *re;
1331 SIMPLEQ_FOREACH(re, refs, entry) {
1332 char *s;
1333 const char *name;
1334 if (got_object_id_cmp(re->id, id) != 0)
1335 continue;
1336 name = got_ref_get_name(re->ref);
1337 if (strcmp(name, GOT_REF_HEAD) == 0)
1338 continue;
1339 if (strncmp(name, "refs/", 5) == 0)
1340 name += 5;
1341 if (strncmp(name, "got/", 4) == 0)
1342 continue;
1343 if (strncmp(name, "heads/", 6) == 0)
1344 name += 6;
1345 if (strncmp(name, "remotes/", 8) == 0)
1346 name += 8;
1347 s = refs_str;
1348 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1349 name) == -1) {
1350 err = got_error_from_errno("asprintf");
1351 free(s);
1352 break;
1354 free(s);
1356 err = got_object_id_str(&id_str, id);
1357 if (err)
1358 return err;
1360 printf("-----------------------------------------------\n");
1361 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1362 refs_str ? refs_str : "", refs_str ? ")" : "");
1363 free(id_str);
1364 id_str = NULL;
1365 free(refs_str);
1366 refs_str = NULL;
1367 printf("from: %s\n", got_object_commit_get_author(commit));
1368 committer_time = got_object_commit_get_committer_time(commit);
1369 datestr = get_datestr(&committer_time, datebuf);
1370 printf("date: %s UTC\n", datestr);
1371 author = got_object_commit_get_author(commit);
1372 committer = got_object_commit_get_committer(commit);
1373 if (strcmp(author, committer) != 0)
1374 printf("via: %s\n", committer);
1375 if (got_object_commit_get_nparents(commit) > 1) {
1376 const struct got_object_id_queue *parent_ids;
1377 struct got_object_qid *qid;
1378 int n = 1;
1379 parent_ids = got_object_commit_get_parent_ids(commit);
1380 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1381 err = got_object_id_str(&id_str, qid->id);
1382 if (err)
1383 return err;
1384 printf("parent %d: %s\n", n++, id_str);
1385 free(id_str);
1389 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1390 if (logmsg0 == NULL)
1391 return got_error_from_errno("strdup");
1393 logmsg = logmsg0;
1394 do {
1395 line = strsep(&logmsg, "\n");
1396 if (line)
1397 printf(" %s\n", line);
1398 } while (line);
1399 free(logmsg0);
1401 if (show_patch) {
1402 err = print_patch(commit, id, diff_context, repo);
1403 if (err == 0)
1404 printf("\n");
1407 if (fflush(stdout) != 0 && err == NULL)
1408 err = got_error_from_errno("fflush");
1409 return err;
1412 static const struct got_error *
1413 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1414 char *path, int show_patch, int diff_context, int limit,
1415 int first_parent_traversal, struct got_reflist_head *refs)
1417 const struct got_error *err;
1418 struct got_commit_graph *graph;
1420 err = got_commit_graph_open(&graph, root_id, path,
1421 first_parent_traversal, repo);
1422 if (err)
1423 return err;
1424 err = got_commit_graph_iter_start(graph, root_id, repo);
1425 if (err)
1426 goto done;
1427 for (;;) {
1428 struct got_commit_object *commit;
1429 struct got_object_id *id;
1431 if (sigint_received || sigpipe_received)
1432 break;
1434 err = got_commit_graph_iter_next(&id, graph);
1435 if (err) {
1436 if (err->code == GOT_ERR_ITER_COMPLETED) {
1437 err = NULL;
1438 break;
1440 if (err->code != GOT_ERR_ITER_NEED_MORE)
1441 break;
1442 err = got_commit_graph_fetch_commits(graph, 1, repo);
1443 if (err)
1444 break;
1445 else
1446 continue;
1448 if (id == NULL)
1449 break;
1451 err = got_object_open_as_commit(&commit, repo, id);
1452 if (err)
1453 break;
1454 err = print_commit(commit, id, repo, show_patch, diff_context,
1455 refs);
1456 got_object_commit_close(commit);
1457 if (err || (limit && --limit == 0))
1458 break;
1460 done:
1461 got_commit_graph_close(graph);
1462 return err;
1465 __dead static void
1466 usage_log(void)
1468 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1469 "[-r repository-path] [path]\n", getprogname());
1470 exit(1);
1473 static const struct got_error *
1474 cmd_log(int argc, char *argv[])
1476 const struct got_error *error;
1477 struct got_repository *repo = NULL;
1478 struct got_worktree *worktree = NULL;
1479 struct got_commit_object *commit = NULL;
1480 struct got_object_id *id = NULL;
1481 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1482 char *start_commit = NULL;
1483 int diff_context = 3, ch;
1484 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1485 const char *errstr;
1486 struct got_reflist_head refs;
1488 SIMPLEQ_INIT(&refs);
1490 #ifndef PROFILE
1491 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1492 NULL)
1493 == -1)
1494 err(1, "pledge");
1495 #endif
1497 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1498 switch (ch) {
1499 case 'p':
1500 show_patch = 1;
1501 break;
1502 case 'c':
1503 start_commit = optarg;
1504 break;
1505 case 'C':
1506 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1507 &errstr);
1508 if (errstr != NULL)
1509 err(1, "-C option %s", errstr);
1510 break;
1511 case 'l':
1512 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1513 if (errstr != NULL)
1514 err(1, "-l option %s", errstr);
1515 break;
1516 case 'f':
1517 first_parent_traversal = 1;
1518 break;
1519 case 'r':
1520 repo_path = realpath(optarg, NULL);
1521 if (repo_path == NULL)
1522 err(1, "-r option");
1523 got_path_strip_trailing_slashes(repo_path);
1524 break;
1525 default:
1526 usage_log();
1527 /* NOTREACHED */
1531 argc -= optind;
1532 argv += optind;
1534 cwd = getcwd(NULL, 0);
1535 if (cwd == NULL) {
1536 error = got_error_from_errno("getcwd");
1537 goto done;
1540 error = got_worktree_open(&worktree, cwd);
1541 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1542 goto done;
1543 error = NULL;
1545 if (argc == 0) {
1546 path = strdup("");
1547 if (path == NULL) {
1548 error = got_error_from_errno("strdup");
1549 goto done;
1551 } else if (argc == 1) {
1552 if (worktree) {
1553 error = got_worktree_resolve_path(&path, worktree,
1554 argv[0]);
1555 if (error)
1556 goto done;
1557 } else {
1558 path = strdup(argv[0]);
1559 if (path == NULL) {
1560 error = got_error_from_errno("strdup");
1561 goto done;
1564 } else
1565 usage_log();
1567 if (repo_path == NULL) {
1568 repo_path = worktree ?
1569 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1571 if (repo_path == NULL) {
1572 error = got_error_from_errno("strdup");
1573 goto done;
1576 error = got_repo_open(&repo, repo_path);
1577 if (error != NULL)
1578 goto done;
1580 error = apply_unveil(got_repo_get_path(repo), 1,
1581 worktree ? got_worktree_get_root_path(worktree) : NULL);
1582 if (error)
1583 goto done;
1585 if (start_commit == NULL) {
1586 struct got_reference *head_ref;
1587 error = got_ref_open(&head_ref, repo,
1588 worktree ? got_worktree_get_head_ref_name(worktree)
1589 : GOT_REF_HEAD, 0);
1590 if (error != NULL)
1591 return error;
1592 error = got_ref_resolve(&id, repo, head_ref);
1593 got_ref_close(head_ref);
1594 if (error != NULL)
1595 return error;
1596 error = got_object_open_as_commit(&commit, repo, id);
1597 } else {
1598 struct got_reference *ref;
1599 error = got_ref_open(&ref, repo, start_commit, 0);
1600 if (error == NULL) {
1601 int obj_type;
1602 error = got_ref_resolve(&id, repo, ref);
1603 got_ref_close(ref);
1604 if (error != NULL)
1605 goto done;
1606 error = got_object_get_type(&obj_type, repo, id);
1607 if (error != NULL)
1608 goto done;
1609 if (obj_type == GOT_OBJ_TYPE_TAG) {
1610 struct got_tag_object *tag;
1611 error = got_object_open_as_tag(&tag, repo, id);
1612 if (error != NULL)
1613 goto done;
1614 if (got_object_tag_get_object_type(tag) !=
1615 GOT_OBJ_TYPE_COMMIT) {
1616 got_object_tag_close(tag);
1617 error = got_error(GOT_ERR_OBJ_TYPE);
1618 goto done;
1620 free(id);
1621 id = got_object_id_dup(
1622 got_object_tag_get_object_id(tag));
1623 if (id == NULL)
1624 error = got_error_from_errno(
1625 "got_object_id_dup");
1626 got_object_tag_close(tag);
1627 if (error)
1628 goto done;
1629 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1630 error = got_error(GOT_ERR_OBJ_TYPE);
1631 goto done;
1633 error = got_object_open_as_commit(&commit, repo, id);
1634 if (error != NULL)
1635 goto done;
1637 if (commit == NULL) {
1638 error = got_repo_match_object_id_prefix(&id,
1639 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1640 if (error != NULL)
1641 return error;
1644 if (error != NULL)
1645 goto done;
1647 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1648 if (error != NULL)
1649 goto done;
1650 if (in_repo_path) {
1651 free(path);
1652 path = in_repo_path;
1655 error = got_ref_list(&refs, repo);
1656 if (error)
1657 goto done;
1659 error = print_commits(id, repo, path, show_patch,
1660 diff_context, limit, first_parent_traversal, &refs);
1661 done:
1662 free(path);
1663 free(repo_path);
1664 free(cwd);
1665 free(id);
1666 if (worktree)
1667 got_worktree_close(worktree);
1668 if (repo) {
1669 const struct got_error *repo_error;
1670 repo_error = got_repo_close(repo);
1671 if (error == NULL)
1672 error = repo_error;
1674 got_ref_list_free(&refs);
1675 return error;
1678 __dead static void
1679 usage_diff(void)
1681 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1682 "[object1 object2 | path]\n", getprogname());
1683 exit(1);
1686 struct print_diff_arg {
1687 struct got_repository *repo;
1688 struct got_worktree *worktree;
1689 int diff_context;
1690 const char *id_str;
1691 int header_shown;
1694 static const struct got_error *
1695 print_diff(void *arg, unsigned char status, const char *path,
1696 struct got_object_id *blob_id, struct got_object_id *commit_id)
1698 struct print_diff_arg *a = arg;
1699 const struct got_error *err = NULL;
1700 struct got_blob_object *blob1 = NULL;
1701 FILE *f2 = NULL;
1702 char *abspath = NULL;
1703 struct stat sb;
1705 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1706 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1707 return NULL;
1709 if (!a->header_shown) {
1710 printf("diff %s %s\n", a->id_str,
1711 got_worktree_get_root_path(a->worktree));
1712 a->header_shown = 1;
1715 if (status != GOT_STATUS_ADD) {
1716 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1717 if (err)
1718 goto done;
1722 if (status != GOT_STATUS_DELETE) {
1723 if (asprintf(&abspath, "%s/%s",
1724 got_worktree_get_root_path(a->worktree), path) == -1) {
1725 err = got_error_from_errno("asprintf");
1726 goto done;
1729 f2 = fopen(abspath, "r");
1730 if (f2 == NULL) {
1731 err = got_error_from_errno2("fopen", abspath);
1732 goto done;
1734 if (lstat(abspath, &sb) == -1) {
1735 err = got_error_from_errno2("lstat", abspath);
1736 goto done;
1738 } else
1739 sb.st_size = 0;
1741 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1742 stdout);
1743 done:
1744 if (blob1)
1745 got_object_blob_close(blob1);
1746 if (f2 && fclose(f2) != 0 && err == NULL)
1747 err = got_error_from_errno("fclose");
1748 free(abspath);
1749 return err;
1752 static const struct got_error *
1753 cmd_diff(int argc, char *argv[])
1755 const struct got_error *error;
1756 struct got_repository *repo = NULL;
1757 struct got_worktree *worktree = NULL;
1758 char *cwd = NULL, *repo_path = NULL;
1759 struct got_object_id *id1 = NULL, *id2 = NULL;
1760 const char *id_str1 = NULL, *id_str2 = NULL;
1761 char *label1 = NULL, *label2 = NULL;
1762 int type1, type2;
1763 int diff_context = 3, ch;
1764 const char *errstr;
1765 char *path = NULL;
1767 #ifndef PROFILE
1768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1769 NULL) == -1)
1770 err(1, "pledge");
1771 #endif
1773 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1774 switch (ch) {
1775 case 'C':
1776 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1777 if (errstr != NULL)
1778 err(1, "-C option %s", errstr);
1779 break;
1780 case 'r':
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1785 break;
1786 default:
1787 usage_diff();
1788 /* NOTREACHED */
1792 argc -= optind;
1793 argv += optind;
1795 cwd = getcwd(NULL, 0);
1796 if (cwd == NULL) {
1797 error = got_error_from_errno("getcwd");
1798 goto done;
1800 error = got_worktree_open(&worktree, cwd);
1801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1802 goto done;
1803 if (argc <= 1) {
1804 if (worktree == NULL) {
1805 error = got_error(GOT_ERR_NOT_WORKTREE);
1806 goto done;
1808 if (repo_path)
1809 errx(1,
1810 "-r option can't be used when diffing a work tree");
1811 repo_path = strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL) {
1813 error = got_error_from_errno("strdup");
1814 goto done;
1816 if (argc == 1) {
1817 error = got_worktree_resolve_path(&path, worktree,
1818 argv[0]);
1819 if (error)
1820 goto done;
1821 } else {
1822 path = strdup("");
1823 if (path == NULL) {
1824 error = got_error_from_errno("strdup");
1825 goto done;
1828 } else if (argc == 2) {
1829 id_str1 = argv[0];
1830 id_str2 = argv[1];
1831 if (worktree && repo_path == NULL) {
1832 repo_path =
1833 strdup(got_worktree_get_repo_path(worktree));
1834 if (repo_path == NULL) {
1835 error = got_error_from_errno("strdup");
1836 goto done;
1839 } else
1840 usage_diff();
1842 if (repo_path == NULL) {
1843 repo_path = getcwd(NULL, 0);
1844 if (repo_path == NULL)
1845 return got_error_from_errno("getcwd");
1848 error = got_repo_open(&repo, repo_path);
1849 free(repo_path);
1850 if (error != NULL)
1851 goto done;
1853 error = apply_unveil(got_repo_get_path(repo), 1,
1854 worktree ? got_worktree_get_root_path(worktree) : NULL);
1855 if (error)
1856 goto done;
1858 if (argc <= 1) {
1859 struct print_diff_arg arg;
1860 struct got_pathlist_head paths;
1861 char *id_str;
1863 TAILQ_INIT(&paths);
1865 error = got_object_id_str(&id_str,
1866 got_worktree_get_base_commit_id(worktree));
1867 if (error)
1868 goto done;
1869 arg.repo = repo;
1870 arg.worktree = worktree;
1871 arg.diff_context = diff_context;
1872 arg.id_str = id_str;
1873 arg.header_shown = 0;
1875 error = got_pathlist_append(&paths, path, NULL);
1876 if (error)
1877 goto done;
1879 error = got_worktree_status(worktree, &paths, repo, print_diff,
1880 &arg, check_cancelled, NULL);
1881 free(id_str);
1882 got_pathlist_free(&paths);
1883 goto done;
1886 error = got_repo_match_object_id_prefix(&id1, id_str1,
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_str1, 0);
1893 if (error != NULL)
1894 goto done;
1895 label1 = strdup(got_ref_get_name(ref));
1896 if (label1 == NULL) {
1897 error = got_error_from_errno("strdup");
1898 goto done;
1900 error = got_ref_resolve(&id1, repo, ref);
1901 got_ref_close(ref);
1902 if (error != NULL)
1903 goto done;
1904 } else {
1905 error = got_object_id_str(&label1, id1);
1906 if (label1 == NULL) {
1907 error = got_error_from_errno("strdup");
1908 goto done;
1912 error = got_repo_match_object_id_prefix(&id2, id_str2,
1913 GOT_OBJ_TYPE_ANY, repo);
1914 if (error) {
1915 struct got_reference *ref;
1916 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1917 goto done;
1918 error = got_ref_open(&ref, repo, id_str2, 0);
1919 if (error != NULL)
1920 goto done;
1921 label2 = strdup(got_ref_get_name(ref));
1922 if (label2 == NULL) {
1923 error = got_error_from_errno("strdup");
1924 goto done;
1926 error = got_ref_resolve(&id2, repo, ref);
1927 got_ref_close(ref);
1928 if (error != NULL)
1929 goto done;
1930 } else {
1931 error = got_object_id_str(&label2, id2);
1932 if (label2 == NULL) {
1933 error = got_error_from_errno("strdup");
1934 goto done;
1938 error = got_object_get_type(&type1, repo, id1);
1939 if (error)
1940 goto done;
1942 error = got_object_get_type(&type2, repo, id2);
1943 if (error)
1944 goto done;
1946 if (type1 != type2) {
1947 error = got_error(GOT_ERR_OBJ_TYPE);
1948 goto done;
1951 switch (type1) {
1952 case GOT_OBJ_TYPE_BLOB:
1953 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1954 diff_context, repo, stdout);
1955 break;
1956 case GOT_OBJ_TYPE_TREE:
1957 error = got_diff_objects_as_trees(id1, id2, "", "",
1958 diff_context, repo, stdout);
1959 break;
1960 case GOT_OBJ_TYPE_COMMIT:
1961 printf("diff %s %s\n", label1, label2);
1962 error = got_diff_objects_as_commits(id1, id2, diff_context,
1963 repo, stdout);
1964 break;
1965 default:
1966 error = got_error(GOT_ERR_OBJ_TYPE);
1969 done:
1970 free(label1);
1971 free(label2);
1972 free(id1);
1973 free(id2);
1974 free(path);
1975 if (worktree)
1976 got_worktree_close(worktree);
1977 if (repo) {
1978 const struct got_error *repo_error;
1979 repo_error = got_repo_close(repo);
1980 if (error == NULL)
1981 error = repo_error;
1983 return error;
1986 __dead static void
1987 usage_blame(void)
1989 fprintf(stderr,
1990 "usage: %s blame [-c commit] [-r repository-path] path\n",
1991 getprogname());
1992 exit(1);
1995 static const struct got_error *
1996 cmd_blame(int argc, char *argv[])
1998 const struct got_error *error;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2002 struct got_object_id *commit_id = NULL;
2003 char *commit_id_str = NULL;
2004 int ch;
2006 #ifndef PROFILE
2007 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2008 NULL) == -1)
2009 err(1, "pledge");
2010 #endif
2012 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2013 switch (ch) {
2014 case 'c':
2015 commit_id_str = optarg;
2016 break;
2017 case 'r':
2018 repo_path = realpath(optarg, NULL);
2019 if (repo_path == NULL)
2020 err(1, "-r option");
2021 got_path_strip_trailing_slashes(repo_path);
2022 break;
2023 default:
2024 usage_blame();
2025 /* NOTREACHED */
2029 argc -= optind;
2030 argv += optind;
2032 if (argc == 1)
2033 path = argv[0];
2034 else
2035 usage_blame();
2037 cwd = getcwd(NULL, 0);
2038 if (cwd == NULL) {
2039 error = got_error_from_errno("getcwd");
2040 goto done;
2042 if (repo_path == NULL) {
2043 error = got_worktree_open(&worktree, cwd);
2044 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2045 goto done;
2046 else
2047 error = NULL;
2048 if (worktree) {
2049 repo_path =
2050 strdup(got_worktree_get_repo_path(worktree));
2051 if (repo_path == NULL)
2052 error = got_error_from_errno("strdup");
2053 if (error)
2054 goto done;
2055 } else {
2056 repo_path = strdup(cwd);
2057 if (repo_path == NULL) {
2058 error = got_error_from_errno("strdup");
2059 goto done;
2064 error = got_repo_open(&repo, repo_path);
2065 if (error != NULL)
2066 goto done;
2068 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2069 if (error)
2070 goto done;
2072 if (worktree) {
2073 const char *prefix = got_worktree_get_path_prefix(worktree);
2074 char *p, *worktree_subdir = cwd +
2075 strlen(got_worktree_get_root_path(worktree));
2076 if (asprintf(&p, "%s%s%s%s%s",
2077 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2078 worktree_subdir, worktree_subdir[0] ? "/" : "",
2079 path) == -1) {
2080 error = got_error_from_errno("asprintf");
2081 goto done;
2083 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2084 free(p);
2085 } else {
2086 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2088 if (error)
2089 goto done;
2091 if (commit_id_str == NULL) {
2092 struct got_reference *head_ref;
2093 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2094 if (error != NULL)
2095 goto done;
2096 error = got_ref_resolve(&commit_id, repo, head_ref);
2097 got_ref_close(head_ref);
2098 if (error != NULL)
2099 goto done;
2100 } else {
2101 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2102 if (error)
2103 goto done;
2106 error = got_blame(in_repo_path, commit_id, repo, stdout);
2107 done:
2108 free(in_repo_path);
2109 free(repo_path);
2110 free(cwd);
2111 free(commit_id);
2112 if (worktree)
2113 got_worktree_close(worktree);
2114 if (repo) {
2115 const struct got_error *repo_error;
2116 repo_error = got_repo_close(repo);
2117 if (error == NULL)
2118 error = repo_error;
2120 return error;
2123 __dead static void
2124 usage_tree(void)
2126 fprintf(stderr,
2127 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2128 getprogname());
2129 exit(1);
2132 static void
2133 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2134 const char *root_path)
2136 int is_root_path = (strcmp(path, root_path) == 0);
2138 path += strlen(root_path);
2139 while (path[0] == '/')
2140 path++;
2142 printf("%s%s%s%s%s\n", id ? id : "", path,
2143 is_root_path ? "" : "/", te->name,
2144 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2147 static const struct got_error *
2148 print_tree(const char *path, struct got_object_id *commit_id,
2149 int show_ids, int recurse, const char *root_path,
2150 struct got_repository *repo)
2152 const struct got_error *err = NULL;
2153 struct got_object_id *tree_id = NULL;
2154 struct got_tree_object *tree = NULL;
2155 const struct got_tree_entries *entries;
2156 struct got_tree_entry *te;
2158 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2159 if (err)
2160 goto done;
2162 err = got_object_open_as_tree(&tree, repo, tree_id);
2163 if (err)
2164 goto done;
2165 entries = got_object_tree_get_entries(tree);
2166 te = SIMPLEQ_FIRST(&entries->head);
2167 while (te) {
2168 char *id = NULL;
2170 if (sigint_received || sigpipe_received)
2171 break;
2173 if (show_ids) {
2174 char *id_str;
2175 err = got_object_id_str(&id_str, te->id);
2176 if (err)
2177 goto done;
2178 if (asprintf(&id, "%s ", id_str) == -1) {
2179 err = got_error_from_errno("asprintf");
2180 free(id_str);
2181 goto done;
2183 free(id_str);
2185 print_entry(te, id, path, root_path);
2186 free(id);
2188 if (recurse && S_ISDIR(te->mode)) {
2189 char *child_path;
2190 if (asprintf(&child_path, "%s%s%s", path,
2191 path[0] == '/' && path[1] == '\0' ? "" : "/",
2192 te->name) == -1) {
2193 err = got_error_from_errno("asprintf");
2194 goto done;
2196 err = print_tree(child_path, commit_id, show_ids, 1,
2197 root_path, repo);
2198 free(child_path);
2199 if (err)
2200 goto done;
2203 te = SIMPLEQ_NEXT(te, entry);
2205 done:
2206 if (tree)
2207 got_object_tree_close(tree);
2208 free(tree_id);
2209 return err;
2212 static const struct got_error *
2213 cmd_tree(int argc, char *argv[])
2215 const struct got_error *error;
2216 struct got_repository *repo = NULL;
2217 struct got_worktree *worktree = NULL;
2218 const char *path;
2219 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2220 struct got_object_id *commit_id = NULL;
2221 char *commit_id_str = NULL;
2222 int show_ids = 0, recurse = 0;
2223 int ch;
2225 #ifndef PROFILE
2226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2227 NULL) == -1)
2228 err(1, "pledge");
2229 #endif
2231 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2232 switch (ch) {
2233 case 'c':
2234 commit_id_str = optarg;
2235 break;
2236 case 'r':
2237 repo_path = realpath(optarg, NULL);
2238 if (repo_path == NULL)
2239 err(1, "-r option");
2240 got_path_strip_trailing_slashes(repo_path);
2241 break;
2242 case 'i':
2243 show_ids = 1;
2244 break;
2245 case 'R':
2246 recurse = 1;
2247 break;
2248 default:
2249 usage_tree();
2250 /* NOTREACHED */
2254 argc -= optind;
2255 argv += optind;
2257 if (argc == 1)
2258 path = argv[0];
2259 else if (argc > 1)
2260 usage_tree();
2261 else
2262 path = NULL;
2264 cwd = getcwd(NULL, 0);
2265 if (cwd == NULL) {
2266 error = got_error_from_errno("getcwd");
2267 goto done;
2269 if (repo_path == NULL) {
2270 error = got_worktree_open(&worktree, cwd);
2271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2272 goto done;
2273 else
2274 error = NULL;
2275 if (worktree) {
2276 repo_path =
2277 strdup(got_worktree_get_repo_path(worktree));
2278 if (repo_path == NULL)
2279 error = got_error_from_errno("strdup");
2280 if (error)
2281 goto done;
2282 } else {
2283 repo_path = strdup(cwd);
2284 if (repo_path == NULL) {
2285 error = got_error_from_errno("strdup");
2286 goto done;
2291 error = got_repo_open(&repo, repo_path);
2292 if (error != NULL)
2293 goto done;
2295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2296 if (error)
2297 goto done;
2299 if (path == NULL) {
2300 if (worktree) {
2301 char *p, *worktree_subdir = cwd +
2302 strlen(got_worktree_get_root_path(worktree));
2303 if (asprintf(&p, "%s/%s",
2304 got_worktree_get_path_prefix(worktree),
2305 worktree_subdir) == -1) {
2306 error = got_error_from_errno("asprintf");
2307 goto done;
2309 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2310 free(p);
2311 if (error)
2312 goto done;
2313 } else
2314 path = "/";
2316 if (in_repo_path == NULL) {
2317 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2318 if (error != NULL)
2319 goto done;
2322 if (commit_id_str == NULL) {
2323 struct got_reference *head_ref;
2324 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2325 if (error != NULL)
2326 goto done;
2327 error = got_ref_resolve(&commit_id, repo, head_ref);
2328 got_ref_close(head_ref);
2329 if (error != NULL)
2330 goto done;
2331 } else {
2332 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2333 if (error)
2334 goto done;
2337 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2338 in_repo_path, repo);
2339 done:
2340 free(in_repo_path);
2341 free(repo_path);
2342 free(cwd);
2343 free(commit_id);
2344 if (worktree)
2345 got_worktree_close(worktree);
2346 if (repo) {
2347 const struct got_error *repo_error;
2348 repo_error = got_repo_close(repo);
2349 if (error == NULL)
2350 error = repo_error;
2352 return error;
2355 __dead static void
2356 usage_status(void)
2358 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2359 exit(1);
2362 static const struct got_error *
2363 print_status(void *arg, unsigned char status, const char *path,
2364 struct got_object_id *blob_id, struct got_object_id *commit_id)
2366 printf("%c %s\n", status, path);
2367 return NULL;
2370 static const struct got_error *
2371 cmd_status(int argc, char *argv[])
2373 const struct got_error *error = NULL;
2374 struct got_repository *repo = NULL;
2375 struct got_worktree *worktree = NULL;
2376 char *cwd = NULL;
2377 struct got_pathlist_head paths;
2378 struct got_pathlist_entry *pe;
2379 int ch;
2381 TAILQ_INIT(&paths);
2383 while ((ch = getopt(argc, argv, "")) != -1) {
2384 switch (ch) {
2385 default:
2386 usage_status();
2387 /* NOTREACHED */
2391 argc -= optind;
2392 argv += optind;
2394 #ifndef PROFILE
2395 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2396 NULL) == -1)
2397 err(1, "pledge");
2398 #endif
2399 cwd = getcwd(NULL, 0);
2400 if (cwd == NULL) {
2401 error = got_error_from_errno("getcwd");
2402 goto done;
2405 error = got_worktree_open(&worktree, cwd);
2406 if (error != NULL)
2407 goto done;
2409 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2410 if (error)
2411 goto done;
2413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2414 if (error != NULL)
2415 goto done;
2417 error = apply_unveil(got_repo_get_path(repo), 1,
2418 got_worktree_get_root_path(worktree));
2419 if (error)
2420 goto done;
2422 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2423 check_cancelled, NULL);
2424 done:
2425 TAILQ_FOREACH(pe, &paths, entry)
2426 free((char *)pe->path);
2427 got_pathlist_free(&paths);
2428 free(cwd);
2429 return error;
2432 __dead static void
2433 usage_ref(void)
2435 fprintf(stderr,
2436 "usage: %s ref [-r repository] -l | -d name | name target\n",
2437 getprogname());
2438 exit(1);
2441 static const struct got_error *
2442 list_refs(struct got_repository *repo)
2444 static const struct got_error *err = NULL;
2445 struct got_reflist_head refs;
2446 struct got_reflist_entry *re;
2448 SIMPLEQ_INIT(&refs);
2449 err = got_ref_list(&refs, repo);
2450 if (err)
2451 return err;
2453 SIMPLEQ_FOREACH(re, &refs, entry) {
2454 char *refstr;
2455 refstr = got_ref_to_str(re->ref);
2456 if (refstr == NULL)
2457 return got_error_from_errno("got_ref_to_str");
2458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2459 free(refstr);
2462 got_ref_list_free(&refs);
2463 return NULL;
2466 static const struct got_error *
2467 delete_ref(struct got_repository *repo, const char *refname)
2469 const struct got_error *err = NULL;
2470 struct got_reference *ref;
2472 err = got_ref_open(&ref, repo, refname, 0);
2473 if (err)
2474 return err;
2476 err = got_ref_delete(ref, repo);
2477 got_ref_close(ref);
2478 return err;
2481 static const struct got_error *
2482 add_ref(struct got_repository *repo, const char *refname, const char *target)
2484 const struct got_error *err = NULL;
2485 struct got_object_id *id;
2486 struct got_reference *ref = NULL;
2489 * Don't let the user create a reference named '-'.
2490 * While technically a valid reference name, this case is usually
2491 * an unintended typo.
2493 if (refname[0] == '-' && refname[1] == '\0')
2494 return got_error(GOT_ERR_BAD_REF_NAME);
2496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2497 repo);
2498 if (err) {
2499 struct got_reference *target_ref;
2501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2502 return err;
2503 err = got_ref_open(&target_ref, repo, target, 0);
2504 if (err)
2505 return err;
2506 err = got_ref_resolve(&id, repo, target_ref);
2507 got_ref_close(target_ref);
2508 if (err)
2509 return err;
2512 err = got_ref_alloc(&ref, refname, id);
2513 if (err)
2514 goto done;
2516 err = got_ref_write(ref, repo);
2517 done:
2518 if (ref)
2519 got_ref_close(ref);
2520 free(id);
2521 return err;
2524 static const struct got_error *
2525 cmd_ref(int argc, char *argv[])
2527 const struct got_error *error = NULL;
2528 struct got_repository *repo = NULL;
2529 struct got_worktree *worktree = NULL;
2530 char *cwd = NULL, *repo_path = NULL;
2531 int ch, do_list = 0;
2532 const char *delref = NULL;
2534 /* TODO: Add -s option for adding symbolic references. */
2535 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2536 switch (ch) {
2537 case 'd':
2538 delref = optarg;
2539 break;
2540 case 'r':
2541 repo_path = realpath(optarg, NULL);
2542 if (repo_path == NULL)
2543 err(1, "-r option");
2544 got_path_strip_trailing_slashes(repo_path);
2545 break;
2546 case 'l':
2547 do_list = 1;
2548 break;
2549 default:
2550 usage_ref();
2551 /* NOTREACHED */
2555 if (do_list && delref)
2556 errx(1, "-l and -d options are mutually exclusive\n");
2558 argc -= optind;
2559 argv += optind;
2561 if (do_list || delref) {
2562 if (argc > 0)
2563 usage_ref();
2564 } else if (argc != 2)
2565 usage_ref();
2567 #ifndef PROFILE
2568 if (do_list) {
2569 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2570 NULL) == -1)
2571 err(1, "pledge");
2572 } else {
2573 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2574 "sendfd unveil", NULL) == -1)
2575 err(1, "pledge");
2577 #endif
2578 cwd = getcwd(NULL, 0);
2579 if (cwd == NULL) {
2580 error = got_error_from_errno("getcwd");
2581 goto done;
2584 if (repo_path == NULL) {
2585 error = got_worktree_open(&worktree, cwd);
2586 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2587 goto done;
2588 else
2589 error = NULL;
2590 if (worktree) {
2591 repo_path =
2592 strdup(got_worktree_get_repo_path(worktree));
2593 if (repo_path == NULL)
2594 error = got_error_from_errno("strdup");
2595 if (error)
2596 goto done;
2597 } else {
2598 repo_path = strdup(cwd);
2599 if (repo_path == NULL) {
2600 error = got_error_from_errno("strdup");
2601 goto done;
2606 error = got_repo_open(&repo, repo_path);
2607 if (error != NULL)
2608 goto done;
2610 error = apply_unveil(got_repo_get_path(repo), do_list,
2611 worktree ? got_worktree_get_root_path(worktree) : NULL);
2612 if (error)
2613 goto done;
2615 if (do_list)
2616 error = list_refs(repo);
2617 else if (delref)
2618 error = delete_ref(repo, delref);
2619 else
2620 error = add_ref(repo, argv[0], argv[1]);
2621 done:
2622 if (repo)
2623 got_repo_close(repo);
2624 if (worktree)
2625 got_worktree_close(worktree);
2626 free(cwd);
2627 free(repo_path);
2628 return error;
2631 __dead static void
2632 usage_branch(void)
2634 fprintf(stderr,
2635 "usage: %s branch [-r repository] -l | -d name | "
2636 "name [base-branch]\n", getprogname());
2637 exit(1);
2640 static const struct got_error *
2641 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2643 static const struct got_error *err = NULL;
2644 struct got_reflist_head refs;
2645 struct got_reflist_entry *re;
2647 SIMPLEQ_INIT(&refs);
2649 err = got_ref_list(&refs, repo);
2650 if (err)
2651 return err;
2653 SIMPLEQ_FOREACH(re, &refs, entry) {
2654 const char *refname, *marker = " ";
2655 char *refstr;
2656 refname = got_ref_get_name(re->ref);
2657 if (strncmp(refname, "refs/heads/", 11) != 0)
2658 continue;
2659 if (worktree && strcmp(refname,
2660 got_worktree_get_head_ref_name(worktree)) == 0) {
2661 struct got_object_id *id = NULL;
2662 err = got_ref_resolve(&id, repo, re->ref);
2663 if (err)
2664 return err;
2665 if (got_object_id_cmp(id,
2666 got_worktree_get_base_commit_id(worktree)) == 0)
2667 marker = "* ";
2668 else
2669 marker = "~ ";
2670 free(id);
2672 refname += 11;
2673 refstr = got_ref_to_str(re->ref);
2674 if (refstr == NULL)
2675 return got_error_from_errno("got_ref_to_str");
2676 printf("%s%s: %s\n", marker, refname, refstr);
2677 free(refstr);
2680 got_ref_list_free(&refs);
2681 return NULL;
2684 static const struct got_error *
2685 delete_branch(struct got_repository *repo, const char *branch_name)
2687 const struct got_error *err = NULL;
2688 struct got_reference *ref;
2689 char *refname;
2691 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2692 return got_error_from_errno("asprintf");
2694 err = got_ref_open(&ref, repo, refname, 0);
2695 if (err)
2696 goto done;
2698 err = got_ref_delete(ref, repo);
2699 got_ref_close(ref);
2700 done:
2701 free(refname);
2702 return err;
2705 static const struct got_error *
2706 add_branch(struct got_repository *repo, const char *branch_name,
2707 const char *base_branch)
2709 const struct got_error *err = NULL;
2710 struct got_object_id *id = NULL;
2711 struct got_reference *ref = NULL;
2712 char *base_refname = NULL, *refname = NULL;
2713 struct got_reference *base_ref;
2716 * Don't let the user create a branch named '-'.
2717 * While technically a valid reference name, this case is usually
2718 * an unintended typo.
2720 if (branch_name[0] == '-' && branch_name[1] == '\0')
2721 return got_error(GOT_ERR_BAD_REF_NAME);
2723 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2724 base_refname = strdup(GOT_REF_HEAD);
2725 if (base_refname == NULL)
2726 return got_error_from_errno("strdup");
2727 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2728 return got_error_from_errno("asprintf");
2730 err = got_ref_open(&base_ref, repo, base_refname, 0);
2731 if (err)
2732 goto done;
2733 err = got_ref_resolve(&id, repo, base_ref);
2734 got_ref_close(base_ref);
2735 if (err)
2736 goto done;
2738 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2739 err = got_error_from_errno("asprintf");
2740 goto done;
2743 err = got_ref_open(&ref, repo, refname, 0);
2744 if (err == NULL) {
2745 err = got_error(GOT_ERR_BRANCH_EXISTS);
2746 goto done;
2747 } else if (err->code != GOT_ERR_NOT_REF)
2748 goto done;
2750 err = got_ref_alloc(&ref, refname, id);
2751 if (err)
2752 goto done;
2754 err = got_ref_write(ref, repo);
2755 done:
2756 if (ref)
2757 got_ref_close(ref);
2758 free(id);
2759 free(base_refname);
2760 free(refname);
2761 return err;
2764 static const struct got_error *
2765 cmd_branch(int argc, char *argv[])
2767 const struct got_error *error = NULL;
2768 struct got_repository *repo = NULL;
2769 struct got_worktree *worktree = NULL;
2770 char *cwd = NULL, *repo_path = NULL;
2771 int ch, do_list = 0;
2772 const char *delref = NULL;
2774 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2775 switch (ch) {
2776 case 'd':
2777 delref = optarg;
2778 break;
2779 case 'r':
2780 repo_path = realpath(optarg, NULL);
2781 if (repo_path == NULL)
2782 err(1, "-r option");
2783 got_path_strip_trailing_slashes(repo_path);
2784 break;
2785 case 'l':
2786 do_list = 1;
2787 break;
2788 default:
2789 usage_branch();
2790 /* NOTREACHED */
2794 if (do_list && delref)
2795 errx(1, "-l and -d options are mutually exclusive\n");
2797 argc -= optind;
2798 argv += optind;
2800 if (do_list || delref) {
2801 if (argc > 0)
2802 usage_branch();
2803 } else if (argc < 1 || argc > 2)
2804 usage_branch();
2806 #ifndef PROFILE
2807 if (do_list) {
2808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2809 NULL) == -1)
2810 err(1, "pledge");
2811 } else {
2812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2813 "sendfd unveil", NULL) == -1)
2814 err(1, "pledge");
2816 #endif
2817 cwd = getcwd(NULL, 0);
2818 if (cwd == NULL) {
2819 error = got_error_from_errno("getcwd");
2820 goto done;
2823 if (repo_path == NULL) {
2824 error = got_worktree_open(&worktree, cwd);
2825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2826 goto done;
2827 else
2828 error = NULL;
2829 if (worktree) {
2830 repo_path =
2831 strdup(got_worktree_get_repo_path(worktree));
2832 if (repo_path == NULL)
2833 error = got_error_from_errno("strdup");
2834 if (error)
2835 goto done;
2836 } else {
2837 repo_path = strdup(cwd);
2838 if (repo_path == NULL) {
2839 error = got_error_from_errno("strdup");
2840 goto done;
2845 error = got_repo_open(&repo, repo_path);
2846 if (error != NULL)
2847 goto done;
2849 error = apply_unveil(got_repo_get_path(repo), do_list,
2850 worktree ? got_worktree_get_root_path(worktree) : NULL);
2851 if (error)
2852 goto done;
2854 if (do_list)
2855 error = list_branches(repo, worktree);
2856 else if (delref)
2857 error = delete_branch(repo, delref);
2858 else {
2859 const char *base_branch;
2860 if (argc == 1) {
2861 base_branch = worktree ?
2862 got_worktree_get_head_ref_name(worktree) :
2863 GOT_REF_HEAD;
2864 } else
2865 base_branch = argv[1];
2866 error = add_branch(repo, argv[0], base_branch);
2868 done:
2869 if (repo)
2870 got_repo_close(repo);
2871 if (worktree)
2872 got_worktree_close(worktree);
2873 free(cwd);
2874 free(repo_path);
2875 return error;
2878 __dead static void
2879 usage_add(void)
2881 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2882 exit(1);
2885 static const struct got_error *
2886 cmd_add(int argc, char *argv[])
2888 const struct got_error *error = NULL;
2889 struct got_repository *repo = NULL;
2890 struct got_worktree *worktree = NULL;
2891 char *cwd = NULL;
2892 struct got_pathlist_head paths;
2893 struct got_pathlist_entry *pe;
2894 int ch, x;
2896 TAILQ_INIT(&paths);
2898 while ((ch = getopt(argc, argv, "")) != -1) {
2899 switch (ch) {
2900 default:
2901 usage_add();
2902 /* NOTREACHED */
2906 argc -= optind;
2907 argv += optind;
2909 #ifndef PROFILE
2910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2911 NULL) == -1)
2912 err(1, "pledge");
2913 #endif
2914 if (argc < 1)
2915 usage_add();
2917 cwd = getcwd(NULL, 0);
2918 if (cwd == NULL) {
2919 error = got_error_from_errno("getcwd");
2920 goto done;
2923 error = got_worktree_open(&worktree, cwd);
2924 if (error)
2925 goto done;
2927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2928 if (error != NULL)
2929 goto done;
2931 error = apply_unveil(got_repo_get_path(repo), 1,
2932 got_worktree_get_root_path(worktree));
2933 if (error)
2934 goto done;
2936 for (x = 0; x < argc; x++) {
2937 char *path = realpath(argv[x], NULL);
2938 if (path == NULL) {
2939 error = got_error_from_errno2("realpath", argv[x]);
2940 goto done;
2943 got_path_strip_trailing_slashes(path);
2944 error = got_pathlist_insert(&pe, &paths, path, NULL);
2945 if (error) {
2946 free(path);
2947 goto done;
2950 error = got_worktree_schedule_add(worktree, &paths, print_status,
2951 NULL, repo);
2952 done:
2953 if (repo)
2954 got_repo_close(repo);
2955 if (worktree)
2956 got_worktree_close(worktree);
2957 TAILQ_FOREACH(pe, &paths, entry)
2958 free((char *)pe->path);
2959 got_pathlist_free(&paths);
2960 free(cwd);
2961 return error;
2964 __dead static void
2965 usage_remove(void)
2967 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2968 exit(1);
2971 static const struct got_error *
2972 cmd_remove(int argc, char *argv[])
2974 const struct got_error *error = NULL;
2975 struct got_worktree *worktree = NULL;
2976 struct got_repository *repo = NULL;
2977 char *cwd = NULL;
2978 struct got_pathlist_head paths;
2979 struct got_pathlist_entry *pe;
2980 int ch, i, delete_local_mods = 0;
2982 TAILQ_INIT(&paths);
2984 while ((ch = getopt(argc, argv, "f")) != -1) {
2985 switch (ch) {
2986 case 'f':
2987 delete_local_mods = 1;
2988 break;
2989 default:
2990 usage_add();
2991 /* NOTREACHED */
2995 argc -= optind;
2996 argv += optind;
2998 #ifndef PROFILE
2999 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3000 NULL) == -1)
3001 err(1, "pledge");
3002 #endif
3003 if (argc < 1)
3004 usage_remove();
3006 cwd = getcwd(NULL, 0);
3007 if (cwd == NULL) {
3008 error = got_error_from_errno("getcwd");
3009 goto done;
3011 error = got_worktree_open(&worktree, cwd);
3012 if (error)
3013 goto done;
3015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3016 if (error)
3017 goto done;
3019 error = apply_unveil(got_repo_get_path(repo), 1,
3020 got_worktree_get_root_path(worktree));
3021 if (error)
3022 goto done;
3024 for (i = 0; i < argc; i++) {
3025 char *path = realpath(argv[i], NULL);
3026 if (path == NULL) {
3027 error = got_error_from_errno2("realpath", argv[i]);
3028 goto done;
3031 got_path_strip_trailing_slashes(path);
3032 error = got_pathlist_insert(&pe, &paths, path, NULL);
3033 if (error) {
3034 free(path);
3035 goto done;
3038 error = got_worktree_schedule_delete(worktree, &paths,
3039 delete_local_mods, print_status, NULL, repo);
3040 if (error)
3041 goto done;
3042 done:
3043 if (repo)
3044 got_repo_close(repo);
3045 if (worktree)
3046 got_worktree_close(worktree);
3047 TAILQ_FOREACH(pe, &paths, entry)
3048 free((char *)pe->path);
3049 got_pathlist_free(&paths);
3050 free(cwd);
3051 return error;
3054 __dead static void
3055 usage_revert(void)
3057 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3058 exit(1);
3061 static const struct got_error *
3062 revert_progress(void *arg, unsigned char status, const char *path)
3064 while (path[0] == '/')
3065 path++;
3066 printf("%c %s\n", status, path);
3067 return NULL;
3070 static const struct got_error *
3071 cmd_revert(int argc, char *argv[])
3073 const struct got_error *error = NULL;
3074 struct got_worktree *worktree = NULL;
3075 struct got_repository *repo = NULL;
3076 char *cwd = NULL, *path = NULL;
3077 struct got_pathlist_head paths;
3078 struct got_pathlist_entry *pe;
3079 int ch, i;
3081 TAILQ_INIT(&paths);
3083 while ((ch = getopt(argc, argv, "")) != -1) {
3084 switch (ch) {
3085 default:
3086 usage_revert();
3087 /* NOTREACHED */
3091 argc -= optind;
3092 argv += optind;
3094 #ifndef PROFILE
3095 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3096 "unveil", NULL) == -1)
3097 err(1, "pledge");
3098 #endif
3099 if (argc < 1)
3100 usage_revert();
3102 for (i = 0; i < argc; i++) {
3103 char *path = realpath(argv[i], NULL);
3104 if (path == NULL) {
3105 error = got_error_from_errno2("realpath", argv[i]);
3106 goto done;
3109 got_path_strip_trailing_slashes(path);
3110 error = got_pathlist_insert(&pe, &paths, path, NULL);
3111 if (error) {
3112 free(path);
3113 goto done;
3117 cwd = getcwd(NULL, 0);
3118 if (cwd == NULL) {
3119 error = got_error_from_errno("getcwd");
3120 goto done;
3122 error = got_worktree_open(&worktree, cwd);
3123 if (error)
3124 goto done;
3126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3127 if (error != NULL)
3128 goto done;
3130 error = apply_unveil(got_repo_get_path(repo), 1,
3131 got_worktree_get_root_path(worktree));
3132 if (error)
3133 goto done;
3135 error = got_worktree_revert(worktree, &paths,
3136 revert_progress, NULL, repo);
3137 if (error)
3138 goto done;
3139 done:
3140 if (repo)
3141 got_repo_close(repo);
3142 if (worktree)
3143 got_worktree_close(worktree);
3144 free(path);
3145 free(cwd);
3146 return error;
3149 __dead static void
3150 usage_commit(void)
3152 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3153 getprogname());
3154 exit(1);
3157 struct collect_commit_logmsg_arg {
3158 const char *cmdline_log;
3159 const char *editor;
3160 const char *worktree_path;
3161 const char *branch_name;
3162 const char *repo_path;
3163 char *logmsg_path;
3167 static const struct got_error *
3168 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3169 void *arg)
3171 char *initial_content = NULL;
3172 struct got_pathlist_entry *pe;
3173 const struct got_error *err = NULL;
3174 char *template = NULL;
3175 struct collect_commit_logmsg_arg *a = arg;
3176 int fd;
3177 size_t len;
3179 /* if a message was specified on the command line, just use it */
3180 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3181 len = strlen(a->cmdline_log) + 1;
3182 *logmsg = malloc(len + 1);
3183 if (*logmsg == NULL)
3184 return got_error_from_errno("malloc");
3185 strlcpy(*logmsg, a->cmdline_log, len);
3186 return NULL;
3189 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3190 return got_error_from_errno("asprintf");
3192 if (asprintf(&initial_content,
3193 "\n# changes to be committed on branch %s:\n",
3194 a->branch_name) == -1)
3195 return got_error_from_errno("asprintf");
3197 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3198 if (err)
3199 goto done;
3201 dprintf(fd, initial_content);
3203 TAILQ_FOREACH(pe, commitable_paths, entry) {
3204 struct got_commitable *ct = pe->data;
3205 dprintf(fd, "# %c %s\n",
3206 got_commitable_get_status(ct),
3207 got_commitable_get_path(ct));
3209 close(fd);
3211 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3212 done:
3213 unlink(a->logmsg_path);
3214 free(a->logmsg_path);
3215 free(initial_content);
3216 free(template);
3218 /* Editor is done; we can now apply unveil(2) */
3219 if (err == NULL) {
3220 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3221 if (err) {
3222 free(*logmsg);
3223 *logmsg = NULL;
3226 return err;
3229 static const struct got_error *
3230 cmd_commit(int argc, char *argv[])
3232 const struct got_error *error = NULL;
3233 struct got_worktree *worktree = NULL;
3234 struct got_repository *repo = NULL;
3235 char *cwd = NULL, *id_str = NULL;
3236 struct got_object_id *id = NULL;
3237 const char *logmsg = NULL;
3238 const char *got_author = getenv("GOT_AUTHOR");
3239 struct collect_commit_logmsg_arg cl_arg;
3240 char *editor = NULL;
3241 int ch, rebase_in_progress, histedit_in_progress;
3242 struct got_pathlist_head paths;
3244 TAILQ_INIT(&paths);
3246 while ((ch = getopt(argc, argv, "m:")) != -1) {
3247 switch (ch) {
3248 case 'm':
3249 logmsg = optarg;
3250 break;
3251 default:
3252 usage_commit();
3253 /* NOTREACHED */
3257 argc -= optind;
3258 argv += optind;
3260 #ifndef PROFILE
3261 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3262 "unveil", NULL) == -1)
3263 err(1, "pledge");
3264 #endif
3265 if (got_author == NULL) {
3266 /* TODO: Look current user up in password database */
3267 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3268 goto done;
3271 cwd = getcwd(NULL, 0);
3272 if (cwd == NULL) {
3273 error = got_error_from_errno("getcwd");
3274 goto done;
3276 error = got_worktree_open(&worktree, cwd);
3277 if (error)
3278 goto done;
3280 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3281 if (error)
3282 goto done;
3283 if (rebase_in_progress) {
3284 error = got_error(GOT_ERR_REBASING);
3285 goto done;
3288 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3289 worktree);
3290 if (error)
3291 goto done;
3293 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3294 if (error)
3295 goto done;
3297 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3298 if (error != NULL)
3299 goto done;
3302 * unveil(2) traverses exec(2); if an editor is used we have
3303 * to apply unveil after the log message has been written.
3305 if (logmsg == NULL || strlen(logmsg) == 0)
3306 error = get_editor(&editor);
3307 else
3308 error = apply_unveil(got_repo_get_path(repo), 0,
3309 got_worktree_get_root_path(worktree));
3310 if (error)
3311 goto done;
3313 cl_arg.editor = editor;
3314 cl_arg.cmdline_log = logmsg;
3315 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3316 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3317 if (!histedit_in_progress) {
3318 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3319 error = got_error(GOT_ERR_COMMIT_BRANCH);
3320 goto done;
3322 cl_arg.branch_name += 11;
3324 cl_arg.repo_path = got_repo_get_path(repo);
3325 cl_arg.logmsg_path = NULL;
3326 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3327 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3328 if (error) {
3329 if (cl_arg.logmsg_path)
3330 fprintf(stderr, "%s: log message preserved in %s\n",
3331 getprogname(), cl_arg.logmsg_path);
3332 goto done;
3335 if (cl_arg.logmsg_path)
3336 unlink(cl_arg.logmsg_path);
3338 error = got_object_id_str(&id_str, id);
3339 if (error)
3340 goto done;
3341 printf("Created commit %s\n", id_str);
3342 done:
3343 if (repo)
3344 got_repo_close(repo);
3345 if (worktree)
3346 got_worktree_close(worktree);
3347 free(cwd);
3348 free(id_str);
3349 free(editor);
3350 return error;
3353 __dead static void
3354 usage_cherrypick(void)
3356 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3357 exit(1);
3360 static const struct got_error *
3361 cmd_cherrypick(int argc, char *argv[])
3363 const struct got_error *error = NULL;
3364 struct got_worktree *worktree = NULL;
3365 struct got_repository *repo = NULL;
3366 char *cwd = NULL, *commit_id_str = NULL;
3367 struct got_object_id *commit_id = NULL;
3368 struct got_commit_object *commit = NULL;
3369 struct got_object_qid *pid;
3370 struct got_reference *head_ref = NULL;
3371 int ch, did_something = 0;
3373 while ((ch = getopt(argc, argv, "")) != -1) {
3374 switch (ch) {
3375 default:
3376 usage_cherrypick();
3377 /* NOTREACHED */
3381 argc -= optind;
3382 argv += optind;
3384 #ifndef PROFILE
3385 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3386 "unveil", NULL) == -1)
3387 err(1, "pledge");
3388 #endif
3389 if (argc != 1)
3390 usage_cherrypick();
3392 cwd = getcwd(NULL, 0);
3393 if (cwd == NULL) {
3394 error = got_error_from_errno("getcwd");
3395 goto done;
3397 error = got_worktree_open(&worktree, cwd);
3398 if (error)
3399 goto done;
3401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3402 if (error != NULL)
3403 goto done;
3405 error = apply_unveil(got_repo_get_path(repo), 0,
3406 got_worktree_get_root_path(worktree));
3407 if (error)
3408 goto done;
3410 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3411 GOT_OBJ_TYPE_COMMIT, repo);
3412 if (error != NULL) {
3413 struct got_reference *ref;
3414 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3415 goto done;
3416 error = got_ref_open(&ref, repo, argv[0], 0);
3417 if (error != NULL)
3418 goto done;
3419 error = got_ref_resolve(&commit_id, repo, ref);
3420 got_ref_close(ref);
3421 if (error != NULL)
3422 goto done;
3424 error = got_object_id_str(&commit_id_str, commit_id);
3425 if (error)
3426 goto done;
3428 error = got_ref_open(&head_ref, repo,
3429 got_worktree_get_head_ref_name(worktree), 0);
3430 if (error != NULL)
3431 goto done;
3433 error = check_same_branch(commit_id, head_ref, NULL, repo);
3434 if (error) {
3435 if (error->code != GOT_ERR_ANCESTRY)
3436 goto done;
3437 error = NULL;
3438 } else {
3439 error = got_error(GOT_ERR_SAME_BRANCH);
3440 goto done;
3443 error = got_object_open_as_commit(&commit, repo, commit_id);
3444 if (error)
3445 goto done;
3446 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3447 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3448 commit_id, repo, update_progress, &did_something, check_cancelled,
3449 NULL);
3450 if (error != NULL)
3451 goto done;
3453 if (did_something)
3454 printf("Merged commit %s\n", commit_id_str);
3455 done:
3456 if (commit)
3457 got_object_commit_close(commit);
3458 free(commit_id_str);
3459 if (head_ref)
3460 got_ref_close(head_ref);
3461 if (worktree)
3462 got_worktree_close(worktree);
3463 if (repo)
3464 got_repo_close(repo);
3465 return error;
3468 __dead static void
3469 usage_backout(void)
3471 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3472 exit(1);
3475 static const struct got_error *
3476 cmd_backout(int argc, char *argv[])
3478 const struct got_error *error = NULL;
3479 struct got_worktree *worktree = NULL;
3480 struct got_repository *repo = NULL;
3481 char *cwd = NULL, *commit_id_str = NULL;
3482 struct got_object_id *commit_id = NULL;
3483 struct got_commit_object *commit = NULL;
3484 struct got_object_qid *pid;
3485 struct got_reference *head_ref = NULL;
3486 int ch, did_something = 0;
3488 while ((ch = getopt(argc, argv, "")) != -1) {
3489 switch (ch) {
3490 default:
3491 usage_backout();
3492 /* NOTREACHED */
3496 argc -= optind;
3497 argv += optind;
3499 #ifndef PROFILE
3500 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3501 "unveil", NULL) == -1)
3502 err(1, "pledge");
3503 #endif
3504 if (argc != 1)
3505 usage_backout();
3507 cwd = getcwd(NULL, 0);
3508 if (cwd == NULL) {
3509 error = got_error_from_errno("getcwd");
3510 goto done;
3512 error = got_worktree_open(&worktree, cwd);
3513 if (error)
3514 goto done;
3516 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3517 if (error != NULL)
3518 goto done;
3520 error = apply_unveil(got_repo_get_path(repo), 0,
3521 got_worktree_get_root_path(worktree));
3522 if (error)
3523 goto done;
3525 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3526 GOT_OBJ_TYPE_COMMIT, repo);
3527 if (error != NULL) {
3528 struct got_reference *ref;
3529 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3530 goto done;
3531 error = got_ref_open(&ref, repo, argv[0], 0);
3532 if (error != NULL)
3533 goto done;
3534 error = got_ref_resolve(&commit_id, repo, ref);
3535 got_ref_close(ref);
3536 if (error != NULL)
3537 goto done;
3539 error = got_object_id_str(&commit_id_str, commit_id);
3540 if (error)
3541 goto done;
3543 error = got_ref_open(&head_ref, repo,
3544 got_worktree_get_head_ref_name(worktree), 0);
3545 if (error != NULL)
3546 goto done;
3548 error = check_same_branch(commit_id, head_ref, NULL, repo);
3549 if (error)
3550 goto done;
3552 error = got_object_open_as_commit(&commit, repo, commit_id);
3553 if (error)
3554 goto done;
3555 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3556 if (pid == NULL) {
3557 error = got_error(GOT_ERR_ROOT_COMMIT);
3558 goto done;
3561 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3562 update_progress, &did_something, check_cancelled, NULL);
3563 if (error != NULL)
3564 goto done;
3566 if (did_something)
3567 printf("Backed out commit %s\n", commit_id_str);
3568 done:
3569 if (commit)
3570 got_object_commit_close(commit);
3571 free(commit_id_str);
3572 if (head_ref)
3573 got_ref_close(head_ref);
3574 if (worktree)
3575 got_worktree_close(worktree);
3576 if (repo)
3577 got_repo_close(repo);
3578 return error;
3581 __dead static void
3582 usage_rebase(void)
3584 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3585 getprogname());
3586 exit(1);
3589 void
3590 trim_logmsg(char *logmsg, int limit)
3592 char *nl;
3593 size_t len;
3595 len = strlen(logmsg);
3596 if (len > limit)
3597 len = limit;
3598 logmsg[len] = '\0';
3599 nl = strchr(logmsg, '\n');
3600 if (nl)
3601 *nl = '\0';
3604 static const struct got_error *
3605 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3607 const char *logmsg0 = NULL;
3609 logmsg0 = got_object_commit_get_logmsg(commit);
3611 while (isspace((unsigned char)logmsg0[0]))
3612 logmsg0++;
3614 *logmsg = strdup(logmsg0);
3615 if (*logmsg == NULL)
3616 return got_error_from_errno("strdup");
3618 trim_logmsg(*logmsg, limit);
3619 return NULL;
3622 static const struct got_error *
3623 show_rebase_progress(struct got_commit_object *commit,
3624 struct got_object_id *old_id, struct got_object_id *new_id)
3626 const struct got_error *err;
3627 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3629 err = got_object_id_str(&old_id_str, old_id);
3630 if (err)
3631 goto done;
3633 if (new_id) {
3634 err = got_object_id_str(&new_id_str, new_id);
3635 if (err)
3636 goto done;
3639 old_id_str[12] = '\0';
3640 if (new_id_str)
3641 new_id_str[12] = '\0';
3643 err = get_short_logmsg(&logmsg, 42, commit);
3644 if (err)
3645 goto done;
3647 printf("%s -> %s: %s\n", old_id_str,
3648 new_id_str ? new_id_str : "no-op change", logmsg);
3649 done:
3650 free(old_id_str);
3651 free(new_id_str);
3652 return err;
3655 static const struct got_error *
3656 rebase_progress(void *arg, unsigned char status, const char *path)
3658 unsigned char *rebase_status = arg;
3660 while (path[0] == '/')
3661 path++;
3662 printf("%c %s\n", status, path);
3664 if (*rebase_status == GOT_STATUS_CONFLICT)
3665 return NULL;
3666 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3667 *rebase_status = status;
3668 return NULL;
3671 static const struct got_error *
3672 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3673 struct got_reference *branch, struct got_reference *new_base_branch,
3674 struct got_reference *tmp_branch, struct got_repository *repo)
3676 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3677 return got_worktree_rebase_complete(worktree, fileindex,
3678 new_base_branch, tmp_branch, branch, repo);
3681 static const struct got_error *
3682 rebase_commit(struct got_pathlist_head *merged_paths,
3683 struct got_worktree *worktree, struct got_fileindex *fileindex,
3684 struct got_reference *tmp_branch,
3685 struct got_object_id *commit_id, struct got_repository *repo)
3687 const struct got_error *error;
3688 struct got_commit_object *commit;
3689 struct got_object_id *new_commit_id;
3691 error = got_object_open_as_commit(&commit, repo, commit_id);
3692 if (error)
3693 return error;
3695 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3696 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3697 if (error) {
3698 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3699 goto done;
3700 error = show_rebase_progress(commit, commit_id, NULL);
3701 } else {
3702 error = show_rebase_progress(commit, commit_id, new_commit_id);
3703 free(new_commit_id);
3705 done:
3706 got_object_commit_close(commit);
3707 return error;
3710 struct check_path_prefix_arg {
3711 const char *path_prefix;
3712 size_t len;
3713 int errcode;
3716 static const struct got_error *
3717 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3718 struct got_blob_object *blob2, struct got_object_id *id1,
3719 struct got_object_id *id2, const char *path1, const char *path2,
3720 struct got_repository *repo)
3722 struct check_path_prefix_arg *a = arg;
3724 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3725 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3726 return got_error(a->errcode);
3728 return NULL;
3731 static const struct got_error *
3732 check_path_prefix(struct got_object_id *parent_id,
3733 struct got_object_id *commit_id, const char *path_prefix,
3734 int errcode, struct got_repository *repo)
3736 const struct got_error *err;
3737 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3738 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3739 struct check_path_prefix_arg cpp_arg;
3741 if (got_path_is_root_dir(path_prefix))
3742 return NULL;
3744 err = got_object_open_as_commit(&commit, repo, commit_id);
3745 if (err)
3746 goto done;
3748 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3749 if (err)
3750 goto done;
3752 err = got_object_open_as_tree(&tree1, repo,
3753 got_object_commit_get_tree_id(parent_commit));
3754 if (err)
3755 goto done;
3757 err = got_object_open_as_tree(&tree2, repo,
3758 got_object_commit_get_tree_id(commit));
3759 if (err)
3760 goto done;
3762 cpp_arg.path_prefix = path_prefix;
3763 while (cpp_arg.path_prefix[0] == '/')
3764 cpp_arg.path_prefix++;
3765 cpp_arg.len = strlen(cpp_arg.path_prefix);
3766 cpp_arg.errcode = errcode;
3767 err = got_diff_tree(tree1, tree2, "", "", repo,
3768 check_path_prefix_in_diff, &cpp_arg, 0);
3769 done:
3770 if (tree1)
3771 got_object_tree_close(tree1);
3772 if (tree2)
3773 got_object_tree_close(tree2);
3774 if (commit)
3775 got_object_commit_close(commit);
3776 if (parent_commit)
3777 got_object_commit_close(parent_commit);
3778 return err;
3781 static const struct got_error *
3782 collect_commits(struct got_object_id_queue *commits,
3783 struct got_object_id *initial_commit_id,
3784 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3785 const char *path_prefix, int path_prefix_errcode,
3786 struct got_repository *repo)
3788 const struct got_error *err = NULL;
3789 struct got_commit_graph *graph = NULL;
3790 struct got_object_id *parent_id = NULL;
3791 struct got_object_qid *qid;
3792 struct got_object_id *commit_id = initial_commit_id;
3794 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3795 if (err)
3796 return err;
3798 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3799 if (err)
3800 goto done;
3801 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3802 err = got_commit_graph_iter_next(&parent_id, graph);
3803 if (err) {
3804 if (err->code == GOT_ERR_ITER_COMPLETED) {
3805 err = got_error_msg(GOT_ERR_ANCESTRY,
3806 "ran out of commits to rebase before "
3807 "youngest common ancestor commit has "
3808 "been reached?!?");
3809 goto done;
3810 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3811 goto done;
3812 err = got_commit_graph_fetch_commits(graph, 1, repo);
3813 if (err)
3814 goto done;
3815 } else {
3816 err = check_path_prefix(parent_id, commit_id,
3817 path_prefix, path_prefix_errcode, repo);
3818 if (err)
3819 goto done;
3821 err = got_object_qid_alloc(&qid, commit_id);
3822 if (err)
3823 goto done;
3824 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3825 commit_id = parent_id;
3828 done:
3829 got_commit_graph_close(graph);
3830 return err;
3833 static const struct got_error *
3834 cmd_rebase(int argc, char *argv[])
3836 const struct got_error *error = NULL;
3837 struct got_worktree *worktree = NULL;
3838 struct got_repository *repo = NULL;
3839 struct got_fileindex *fileindex = NULL;
3840 char *cwd = NULL;
3841 struct got_reference *branch = NULL;
3842 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3843 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3844 struct got_object_id *resume_commit_id = NULL;
3845 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3846 struct got_commit_object *commit = NULL;
3847 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3848 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3849 struct got_object_id_queue commits;
3850 struct got_pathlist_head merged_paths;
3851 const struct got_object_id_queue *parent_ids;
3852 struct got_object_qid *qid, *pid;
3854 SIMPLEQ_INIT(&commits);
3855 TAILQ_INIT(&merged_paths);
3857 while ((ch = getopt(argc, argv, "ac")) != -1) {
3858 switch (ch) {
3859 case 'a':
3860 abort_rebase = 1;
3861 break;
3862 case 'c':
3863 continue_rebase = 1;
3864 break;
3865 default:
3866 usage_rebase();
3867 /* NOTREACHED */
3871 argc -= optind;
3872 argv += optind;
3874 #ifndef PROFILE
3875 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3876 "unveil", NULL) == -1)
3877 err(1, "pledge");
3878 #endif
3879 if (abort_rebase && continue_rebase)
3880 usage_rebase();
3881 else if (abort_rebase || continue_rebase) {
3882 if (argc != 0)
3883 usage_rebase();
3884 } else if (argc != 1)
3885 usage_rebase();
3887 cwd = getcwd(NULL, 0);
3888 if (cwd == NULL) {
3889 error = got_error_from_errno("getcwd");
3890 goto done;
3892 error = got_worktree_open(&worktree, cwd);
3893 if (error)
3894 goto done;
3896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3897 if (error != NULL)
3898 goto done;
3900 error = apply_unveil(got_repo_get_path(repo), 0,
3901 got_worktree_get_root_path(worktree));
3902 if (error)
3903 goto done;
3905 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3906 if (error)
3907 goto done;
3909 if (abort_rebase) {
3910 int did_something;
3911 if (!rebase_in_progress) {
3912 error = got_error(GOT_ERR_NOT_REBASING);
3913 goto done;
3915 error = got_worktree_rebase_continue(&resume_commit_id,
3916 &new_base_branch, &tmp_branch, &branch, &fileindex,
3917 worktree, repo);
3918 if (error)
3919 goto done;
3920 printf("Switching work tree to %s\n",
3921 got_ref_get_symref_target(new_base_branch));
3922 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3923 new_base_branch, update_progress, &did_something);
3924 if (error)
3925 goto done;
3926 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3927 goto done; /* nothing else to do */
3930 if (continue_rebase) {
3931 if (!rebase_in_progress) {
3932 error = got_error(GOT_ERR_NOT_REBASING);
3933 goto done;
3935 error = got_worktree_rebase_continue(&resume_commit_id,
3936 &new_base_branch, &tmp_branch, &branch, &fileindex,
3937 worktree, repo);
3938 if (error)
3939 goto done;
3941 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3942 resume_commit_id, repo);
3943 if (error)
3944 goto done;
3946 yca_id = got_object_id_dup(resume_commit_id);
3947 if (yca_id == NULL) {
3948 error = got_error_from_errno("got_object_id_dup");
3949 goto done;
3951 } else {
3952 error = got_ref_open(&branch, repo, argv[0], 0);
3953 if (error != NULL)
3954 goto done;
3957 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3958 if (error)
3959 goto done;
3961 if (!continue_rebase) {
3962 struct got_object_id *base_commit_id;
3964 base_commit_id = got_worktree_get_base_commit_id(worktree);
3965 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3966 base_commit_id, branch_head_commit_id, repo);
3967 if (error)
3968 goto done;
3969 if (yca_id == NULL) {
3970 error = got_error_msg(GOT_ERR_ANCESTRY,
3971 "specified branch shares no common ancestry "
3972 "with work tree's branch");
3973 goto done;
3976 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3977 if (error) {
3978 if (error->code != GOT_ERR_ANCESTRY)
3979 goto done;
3980 error = NULL;
3981 } else {
3982 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3983 "specified branch resolves to a commit which "
3984 "is already contained in work tree's branch");
3985 goto done;
3987 error = got_worktree_rebase_prepare(&new_base_branch,
3988 &tmp_branch, &fileindex, worktree, branch, repo);
3989 if (error)
3990 goto done;
3993 commit_id = branch_head_commit_id;
3994 error = got_object_open_as_commit(&commit, repo, commit_id);
3995 if (error)
3996 goto done;
3998 parent_ids = got_object_commit_get_parent_ids(commit);
3999 pid = SIMPLEQ_FIRST(parent_ids);
4000 error = collect_commits(&commits, commit_id, pid->id,
4001 yca_id, got_worktree_get_path_prefix(worktree),
4002 GOT_ERR_REBASE_PATH, repo);
4003 got_object_commit_close(commit);
4004 commit = NULL;
4005 if (error)
4006 goto done;
4008 if (SIMPLEQ_EMPTY(&commits)) {
4009 if (continue_rebase)
4010 error = rebase_complete(worktree, fileindex,
4011 branch, new_base_branch, tmp_branch, repo);
4012 else
4013 error = got_error(GOT_ERR_EMPTY_REBASE);
4014 goto done;
4017 pid = NULL;
4018 SIMPLEQ_FOREACH(qid, &commits, entry) {
4019 commit_id = qid->id;
4020 parent_id = pid ? pid->id : yca_id;
4021 pid = qid;
4023 error = got_worktree_rebase_merge_files(&merged_paths,
4024 worktree, fileindex, parent_id, commit_id, repo,
4025 rebase_progress, &rebase_status, check_cancelled, NULL);
4026 if (error)
4027 goto done;
4029 if (rebase_status == GOT_STATUS_CONFLICT) {
4030 got_worktree_rebase_pathlist_free(&merged_paths);
4031 break;
4034 error = rebase_commit(&merged_paths, worktree, fileindex,
4035 tmp_branch, commit_id, repo);
4036 got_worktree_rebase_pathlist_free(&merged_paths);
4037 if (error)
4038 goto done;
4041 if (rebase_status == GOT_STATUS_CONFLICT) {
4042 error = got_worktree_rebase_postpone(worktree, fileindex);
4043 if (error)
4044 goto done;
4045 error = got_error_msg(GOT_ERR_CONFLICTS,
4046 "conflicts must be resolved before rebasing can continue");
4047 } else
4048 error = rebase_complete(worktree, fileindex, branch,
4049 new_base_branch, tmp_branch, repo);
4050 done:
4051 got_object_id_queue_free(&commits);
4052 free(branch_head_commit_id);
4053 free(resume_commit_id);
4054 free(yca_id);
4055 if (commit)
4056 got_object_commit_close(commit);
4057 if (branch)
4058 got_ref_close(branch);
4059 if (new_base_branch)
4060 got_ref_close(new_base_branch);
4061 if (tmp_branch)
4062 got_ref_close(tmp_branch);
4063 if (worktree)
4064 got_worktree_close(worktree);
4065 if (repo)
4066 got_repo_close(repo);
4067 return error;
4070 __dead static void
4071 usage_histedit(void)
4073 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4074 getprogname());
4075 exit(1);
4078 #define GOT_HISTEDIT_PICK 'p'
4079 #define GOT_HISTEDIT_EDIT 'e'
4080 #define GOT_HISTEDIT_FOLD 'f'
4081 #define GOT_HISTEDIT_DROP 'd'
4082 #define GOT_HISTEDIT_MESG 'm'
4084 static struct got_histedit_cmd {
4085 unsigned char code;
4086 const char *name;
4087 const char *desc;
4088 } got_histedit_cmds[] = {
4089 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4090 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4091 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4092 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4093 { GOT_HISTEDIT_MESG, "mesg",
4094 "single-line log message for commit above (open editor if empty)" },
4097 struct got_histedit_list_entry {
4098 TAILQ_ENTRY(got_histedit_list_entry) entry;
4099 struct got_object_id *commit_id;
4100 const struct got_histedit_cmd *cmd;
4101 char *logmsg;
4103 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4105 static const struct got_error *
4106 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4107 FILE *f, struct got_repository *repo)
4109 const struct got_error *err = NULL;
4110 char *logmsg = NULL, *id_str = NULL;
4111 struct got_commit_object *commit = NULL;
4112 size_t n;
4114 err = got_object_open_as_commit(&commit, repo, commit_id);
4115 if (err)
4116 goto done;
4118 err = get_short_logmsg(&logmsg, 34, commit);
4119 if (err)
4120 goto done;
4122 err = got_object_id_str(&id_str, commit_id);
4123 if (err)
4124 goto done;
4126 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4127 if (n < 0)
4128 err = got_ferror(f, GOT_ERR_IO);
4129 done:
4130 if (commit)
4131 got_object_commit_close(commit);
4132 free(id_str);
4133 free(logmsg);
4134 return err;
4137 static const struct got_error *
4138 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4139 struct got_repository *repo)
4141 const struct got_error *err = NULL;
4142 struct got_object_qid *qid;
4144 if (SIMPLEQ_EMPTY(commits))
4145 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4147 SIMPLEQ_FOREACH(qid, commits, entry) {
4148 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4149 f, repo);
4150 if (err)
4151 break;
4154 return err;
4157 static const struct got_error *
4158 write_cmd_list(FILE *f)
4160 const struct got_error *err = NULL;
4161 int n, i;
4163 n = fprintf(f, "# Available histedit commands:\n");
4164 if (n < 0)
4165 return got_ferror(f, GOT_ERR_IO);
4167 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4168 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4169 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4170 cmd->desc);
4171 if (n < 0) {
4172 err = got_ferror(f, GOT_ERR_IO);
4173 break;
4176 n = fprintf(f, "# Commits will be processed in order from top to "
4177 "bottom of this file.\n");
4178 if (n < 0)
4179 return got_ferror(f, GOT_ERR_IO);
4180 return err;
4183 static const struct got_error *
4184 histedit_syntax_error(int lineno)
4186 static char msg[42];
4187 int ret;
4189 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4190 lineno);
4191 if (ret == -1 || ret >= sizeof(msg))
4192 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4194 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4197 static const struct got_error *
4198 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4199 char *logmsg, struct got_repository *repo)
4201 const struct got_error *err;
4202 struct got_commit_object *folded_commit = NULL;
4203 char *id_str;
4205 err = got_object_id_str(&id_str, hle->commit_id);
4206 if (err)
4207 return err;
4209 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4210 if (err)
4211 goto done;
4213 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4214 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4215 got_object_commit_get_logmsg(folded_commit)) == -1) {
4216 err = got_error_from_errno("asprintf");
4217 goto done;
4219 done:
4220 if (folded_commit)
4221 got_object_commit_close(folded_commit);
4222 free(id_str);
4223 return err;
4226 static struct got_histedit_list_entry *
4227 get_folded_commits(struct got_histedit_list_entry *hle)
4229 struct got_histedit_list_entry *prev, *folded = NULL;
4231 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4232 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4233 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4234 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4235 folded = prev;
4236 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4239 return folded;
4242 static const struct got_error *
4243 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4244 struct got_repository *repo)
4246 char *logmsg_path = NULL, *id_str = NULL;
4247 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4248 const struct got_error *err = NULL;
4249 struct got_commit_object *commit = NULL;
4250 int fd;
4251 struct got_histedit_list_entry *folded = NULL;
4253 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4254 if (err)
4255 return err;
4257 folded = get_folded_commits(hle);
4258 if (folded) {
4259 while (folded != hle) {
4260 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4261 folded = TAILQ_NEXT(folded, entry);
4262 continue;
4264 err = append_folded_commit_msg(&new_msg, folded,
4265 logmsg, repo);
4266 if (err)
4267 goto done;
4268 free(logmsg);
4269 logmsg = new_msg;
4270 folded = TAILQ_NEXT(folded, entry);
4274 err = got_object_id_str(&id_str, hle->commit_id);
4275 if (err)
4276 goto done;
4277 if (asprintf(&new_msg,
4278 "%s\n# original log message of commit %s: %s",
4279 logmsg ? logmsg : "", id_str,
4280 got_object_commit_get_logmsg(commit)) == -1) {
4281 err = got_error_from_errno("asprintf");
4282 goto done;
4284 free(logmsg);
4285 logmsg = new_msg;
4287 err = got_object_id_str(&id_str, hle->commit_id);
4288 if (err)
4289 goto done;
4291 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4292 if (err)
4293 goto done;
4295 dprintf(fd, logmsg);
4296 close(fd);
4298 err = get_editor(&editor);
4299 if (err)
4300 goto done;
4302 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4303 if (err) {
4304 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4305 goto done;
4306 err = NULL;
4307 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4308 if (hle->logmsg == NULL)
4309 err = got_error_from_errno("strdup");
4311 done:
4312 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4313 err = got_error_from_errno2("unlink", logmsg_path);
4314 free(logmsg_path);
4315 free(logmsg);
4316 free(editor);
4317 if (commit)
4318 got_object_commit_close(commit);
4319 return err;
4322 static const struct got_error *
4323 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4324 FILE *f, struct got_repository *repo)
4326 const struct got_error *err = NULL;
4327 char *line = NULL, *p, *end;
4328 size_t size;
4329 ssize_t len;
4330 int lineno = 0, i;
4331 const struct got_histedit_cmd *cmd;
4332 struct got_object_id *commit_id = NULL;
4333 struct got_histedit_list_entry *hle = NULL;
4335 for (;;) {
4336 len = getline(&line, &size, f);
4337 if (len == -1) {
4338 const struct got_error *getline_err;
4339 if (feof(f))
4340 break;
4341 getline_err = got_error_from_errno("getline");
4342 err = got_ferror(f, getline_err->code);
4343 break;
4345 lineno++;
4346 p = line;
4347 while (isspace((unsigned char)p[0]))
4348 p++;
4349 if (p[0] == '#' || p[0] == '\0') {
4350 free(line);
4351 line = NULL;
4352 continue;
4354 cmd = NULL;
4355 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4356 cmd = &got_histedit_cmds[i];
4357 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4358 isspace((unsigned char)p[strlen(cmd->name)])) {
4359 p += strlen(cmd->name);
4360 break;
4362 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4363 p++;
4364 break;
4367 if (i == nitems(got_histedit_cmds)) {
4368 err = histedit_syntax_error(lineno);
4369 break;
4371 while (isspace((unsigned char)p[0]))
4372 p++;
4373 if (cmd->code == GOT_HISTEDIT_MESG) {
4374 if (hle == NULL || hle->logmsg != NULL) {
4375 err = got_error(GOT_ERR_HISTEDIT_CMD);
4376 break;
4378 if (p[0] == '\0') {
4379 err = histedit_edit_logmsg(hle, repo);
4380 if (err)
4381 break;
4382 } else {
4383 hle->logmsg = strdup(p);
4384 if (hle->logmsg == NULL) {
4385 err = got_error_from_errno("strdup");
4386 break;
4389 free(line);
4390 line = NULL;
4391 continue;
4392 } else {
4393 end = p;
4394 while (end[0] && !isspace((unsigned char)end[0]))
4395 end++;
4396 *end = '\0';
4398 err = got_object_resolve_id_str(&commit_id, repo, p);
4399 if (err) {
4400 /* override error code */
4401 err = histedit_syntax_error(lineno);
4402 break;
4405 hle = malloc(sizeof(*hle));
4406 if (hle == NULL) {
4407 err = got_error_from_errno("malloc");
4408 break;
4410 hle->cmd = cmd;
4411 hle->commit_id = commit_id;
4412 hle->logmsg = NULL;
4413 commit_id = NULL;
4414 free(line);
4415 line = NULL;
4416 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4419 free(line);
4420 free(commit_id);
4421 return err;
4424 static const struct got_error *
4425 histedit_check_script(struct got_histedit_list *histedit_cmds,
4426 struct got_object_id_queue *commits, struct got_repository *repo)
4428 const struct got_error *err = NULL;
4429 struct got_object_qid *qid;
4430 struct got_histedit_list_entry *hle;
4431 static char msg[80];
4432 char *id_str;
4434 if (TAILQ_EMPTY(histedit_cmds))
4435 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4436 "histedit script contains no commands");
4438 SIMPLEQ_FOREACH(qid, commits, entry) {
4439 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4440 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4441 break;
4443 if (hle == NULL) {
4444 err = got_object_id_str(&id_str, qid->id);
4445 if (err)
4446 return err;
4447 snprintf(msg, sizeof(msg),
4448 "commit %s missing from histedit script", id_str);
4449 free(id_str);
4450 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4454 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4455 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4456 "last commit in histedit script cannot be folded");
4458 return NULL;
4461 static const struct got_error *
4462 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4463 const char *path, struct got_object_id_queue *commits,
4464 struct got_repository *repo)
4466 const struct got_error *err = NULL;
4467 char *editor;
4468 FILE *f = NULL;
4470 err = get_editor(&editor);
4471 if (err)
4472 return err;
4474 if (spawn_editor(editor, path) == -1) {
4475 err = got_error_from_errno("failed spawning editor");
4476 goto done;
4479 f = fopen(path, "r");
4480 if (f == NULL) {
4481 err = got_error_from_errno("fopen");
4482 goto done;
4484 err = histedit_parse_list(histedit_cmds, f, repo);
4485 if (err)
4486 goto done;
4488 err = histedit_check_script(histedit_cmds, commits, repo);
4489 done:
4490 if (f && fclose(f) != 0 && err == NULL)
4491 err = got_error_from_errno("fclose");
4492 free(editor);
4493 return err;
4496 static const struct got_error *
4497 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4498 struct got_object_id_queue *, const char *, struct got_repository *);
4500 static const struct got_error *
4501 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4502 struct got_object_id_queue *commits, struct got_repository *repo)
4504 const struct got_error *err;
4505 FILE *f = NULL;
4506 char *path = NULL;
4508 err = got_opentemp_named(&path, &f, "got-histedit");
4509 if (err)
4510 return err;
4512 err = write_cmd_list(f);
4513 if (err)
4514 goto done;
4516 err = histedit_write_commit_list(commits, f, repo);
4517 if (err)
4518 goto done;
4520 if (fclose(f) != 0) {
4521 err = got_error_from_errno("fclose");
4522 goto done;
4524 f = NULL;
4526 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4527 if (err) {
4528 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4529 err->code != GOT_ERR_HISTEDIT_CMD)
4530 goto done;
4531 err = histedit_edit_list_retry(histedit_cmds, err,
4532 commits, path, repo);
4534 done:
4535 if (f && fclose(f) != 0 && err == NULL)
4536 err = got_error_from_errno("fclose");
4537 if (path && unlink(path) != 0 && err == NULL)
4538 err = got_error_from_errno2("unlink", path);
4539 free(path);
4540 return err;
4543 static const struct got_error *
4544 histedit_save_list(struct got_histedit_list *histedit_cmds,
4545 struct got_worktree *worktree, struct got_repository *repo)
4547 const struct got_error *err = NULL;
4548 char *path = NULL;
4549 FILE *f = NULL;
4550 struct got_histedit_list_entry *hle;
4551 struct got_commit_object *commit = NULL;
4553 err = got_worktree_get_histedit_script_path(&path, worktree);
4554 if (err)
4555 return err;
4557 f = fopen(path, "w");
4558 if (f == NULL) {
4559 err = got_error_from_errno2("fopen", path);
4560 goto done;
4562 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4563 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4564 repo);
4565 if (err)
4566 break;
4568 if (hle->logmsg) {
4569 int n = fprintf(f, "%c %s\n",
4570 GOT_HISTEDIT_MESG, hle->logmsg);
4571 if (n < 0) {
4572 err = got_ferror(f, GOT_ERR_IO);
4573 break;
4577 done:
4578 if (f && fclose(f) != 0 && err == NULL)
4579 err = got_error_from_errno("fclose");
4580 free(path);
4581 if (commit)
4582 got_object_commit_close(commit);
4583 return err;
4586 void
4587 histedit_free_list(struct got_histedit_list *histedit_cmds)
4589 struct got_histedit_list_entry *hle;
4591 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4592 TAILQ_REMOVE(histedit_cmds, hle, entry);
4593 free(hle);
4597 static const struct got_error *
4598 histedit_load_list(struct got_histedit_list *histedit_cmds,
4599 const char *path, struct got_repository *repo)
4601 const struct got_error *err = NULL;
4602 FILE *f = NULL;
4604 f = fopen(path, "r");
4605 if (f == NULL) {
4606 err = got_error_from_errno2("fopen", path);
4607 goto done;
4610 err = histedit_parse_list(histedit_cmds, f, repo);
4611 done:
4612 if (f && fclose(f) != 0 && err == NULL)
4613 err = got_error_from_errno("fclose");
4614 return err;
4617 static const struct got_error *
4618 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4619 const struct got_error *edit_err, struct got_object_id_queue *commits,
4620 const char *path, struct got_repository *repo)
4622 const struct got_error *err = NULL, *prev_err = edit_err;
4623 int resp = ' ';
4625 while (resp != 'c' && resp != 'r' && resp != 'a') {
4626 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4627 "or (a)bort: ", getprogname(), prev_err->msg);
4628 resp = getchar();
4629 if (resp == 'c') {
4630 histedit_free_list(histedit_cmds);
4631 err = histedit_run_editor(histedit_cmds, path, commits,
4632 repo);
4633 if (err) {
4634 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4635 err->code != GOT_ERR_HISTEDIT_CMD)
4636 break;
4637 prev_err = err;
4638 resp = ' ';
4639 continue;
4641 break;
4642 } else if (resp == 'r') {
4643 histedit_free_list(histedit_cmds);
4644 err = histedit_edit_script(histedit_cmds,
4645 commits, repo);
4646 if (err) {
4647 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4648 err->code != GOT_ERR_HISTEDIT_CMD)
4649 break;
4650 prev_err = err;
4651 resp = ' ';
4652 continue;
4654 break;
4655 } else if (resp == 'a') {
4656 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4657 break;
4658 } else
4659 printf("invalid response '%c'\n", resp);
4662 return err;
4665 static const struct got_error *
4666 histedit_complete(struct got_worktree *worktree,
4667 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4668 struct got_reference *branch, struct got_repository *repo)
4670 printf("Switching work tree to %s\n",
4671 got_ref_get_symref_target(branch));
4672 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4673 branch, repo);
4676 static const struct got_error *
4677 show_histedit_progress(struct got_commit_object *commit,
4678 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4680 const struct got_error *err;
4681 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4683 err = got_object_id_str(&old_id_str, hle->commit_id);
4684 if (err)
4685 goto done;
4687 if (new_id) {
4688 err = got_object_id_str(&new_id_str, new_id);
4689 if (err)
4690 goto done;
4693 old_id_str[12] = '\0';
4694 if (new_id_str)
4695 new_id_str[12] = '\0';
4697 if (hle->logmsg) {
4698 logmsg = strdup(hle->logmsg);
4699 if (logmsg == NULL) {
4700 err = got_error_from_errno("strdup");
4701 goto done;
4703 trim_logmsg(logmsg, 42);
4704 } else {
4705 err = get_short_logmsg(&logmsg, 42, commit);
4706 if (err)
4707 goto done;
4710 switch (hle->cmd->code) {
4711 case GOT_HISTEDIT_PICK:
4712 case GOT_HISTEDIT_EDIT:
4713 printf("%s -> %s: %s\n", old_id_str,
4714 new_id_str ? new_id_str : "no-op change", logmsg);
4715 break;
4716 case GOT_HISTEDIT_DROP:
4717 case GOT_HISTEDIT_FOLD:
4718 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4719 logmsg);
4720 break;
4721 default:
4722 break;
4725 done:
4726 free(old_id_str);
4727 free(new_id_str);
4728 return err;
4731 static const struct got_error *
4732 histedit_commit(struct got_pathlist_head *merged_paths,
4733 struct got_worktree *worktree, struct got_fileindex *fileindex,
4734 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4735 struct got_repository *repo)
4737 const struct got_error *err;
4738 struct got_commit_object *commit;
4739 struct got_object_id *new_commit_id;
4741 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4742 && hle->logmsg == NULL) {
4743 err = histedit_edit_logmsg(hle, repo);
4744 if (err)
4745 return err;
4748 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4749 if (err)
4750 return err;
4752 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4753 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4754 hle->logmsg, repo);
4755 if (err) {
4756 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4757 goto done;
4758 err = show_histedit_progress(commit, hle, NULL);
4759 } else {
4760 err = show_histedit_progress(commit, hle, new_commit_id);
4761 free(new_commit_id);
4763 done:
4764 got_object_commit_close(commit);
4765 return err;
4768 static const struct got_error *
4769 histedit_skip_commit(struct got_histedit_list_entry *hle,
4770 struct got_worktree *worktree, struct got_repository *repo)
4772 const struct got_error *error;
4773 struct got_commit_object *commit;
4775 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4776 repo);
4777 if (error)
4778 return error;
4780 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4781 if (error)
4782 return error;
4784 error = show_histedit_progress(commit, hle, NULL);
4785 got_object_commit_close(commit);
4786 return error;
4789 static const struct got_error *
4790 cmd_histedit(int argc, char *argv[])
4792 const struct got_error *error = NULL;
4793 struct got_worktree *worktree = NULL;
4794 struct got_fileindex *fileindex = NULL;
4795 struct got_repository *repo = NULL;
4796 char *cwd = NULL;
4797 struct got_reference *branch = NULL;
4798 struct got_reference *tmp_branch = NULL;
4799 struct got_object_id *resume_commit_id = NULL;
4800 struct got_object_id *base_commit_id = NULL;
4801 struct got_object_id *head_commit_id = NULL;
4802 struct got_commit_object *commit = NULL;
4803 int ch, rebase_in_progress = 0, did_something;
4804 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4805 const char *edit_script_path = NULL;
4806 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4807 struct got_object_id_queue commits;
4808 struct got_pathlist_head merged_paths;
4809 const struct got_object_id_queue *parent_ids;
4810 struct got_object_qid *pid;
4811 struct got_histedit_list histedit_cmds;
4812 struct got_histedit_list_entry *hle;
4814 SIMPLEQ_INIT(&commits);
4815 TAILQ_INIT(&histedit_cmds);
4816 TAILQ_INIT(&merged_paths);
4818 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4819 switch (ch) {
4820 case 'a':
4821 abort_edit = 1;
4822 break;
4823 case 'c':
4824 continue_edit = 1;
4825 break;
4826 case 'F':
4827 edit_script_path = optarg;
4828 break;
4829 default:
4830 usage_histedit();
4831 /* NOTREACHED */
4835 argc -= optind;
4836 argv += optind;
4838 #ifndef PROFILE
4839 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4840 "unveil", NULL) == -1)
4841 err(1, "pledge");
4842 #endif
4843 if (abort_edit && continue_edit)
4844 usage_histedit();
4845 if (argc != 0)
4846 usage_histedit();
4849 * This command cannot apply unveil(2) in all cases because the
4850 * user may choose to run an editor to edit the histedit script
4851 * and to edit individual commit log messages.
4852 * unveil(2) traverses exec(2); if an editor is used we have to
4853 * apply unveil after edit script and log messages have been written.
4854 * XXX TODO: Make use of unveil(2) where possible.
4857 cwd = getcwd(NULL, 0);
4858 if (cwd == NULL) {
4859 error = got_error_from_errno("getcwd");
4860 goto done;
4862 error = got_worktree_open(&worktree, cwd);
4863 if (error)
4864 goto done;
4866 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4867 if (error != NULL)
4868 goto done;
4870 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4871 if (error)
4872 goto done;
4873 if (rebase_in_progress) {
4874 error = got_error(GOT_ERR_REBASING);
4875 goto done;
4878 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4879 if (error)
4880 goto done;
4882 if (edit_in_progress && abort_edit) {
4883 error = got_worktree_histedit_continue(&resume_commit_id,
4884 &tmp_branch, &branch, &base_commit_id, &fileindex,
4885 worktree, repo);
4886 if (error)
4887 goto done;
4888 printf("Switching work tree to %s\n",
4889 got_ref_get_symref_target(branch));
4890 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4891 branch, base_commit_id, update_progress, &did_something);
4892 if (error)
4893 goto done;
4894 printf("Histedit of %s aborted\n",
4895 got_ref_get_symref_target(branch));
4896 goto done; /* nothing else to do */
4897 } else if (abort_edit) {
4898 error = got_error(GOT_ERR_NOT_HISTEDIT);
4899 goto done;
4902 if (continue_edit) {
4903 char *path;
4905 if (!edit_in_progress) {
4906 error = got_error(GOT_ERR_NOT_HISTEDIT);
4907 goto done;
4910 error = got_worktree_get_histedit_script_path(&path, worktree);
4911 if (error)
4912 goto done;
4914 error = histedit_load_list(&histedit_cmds, path, repo);
4915 free(path);
4916 if (error)
4917 goto done;
4919 error = got_worktree_histedit_continue(&resume_commit_id,
4920 &tmp_branch, &branch, &base_commit_id, &fileindex,
4921 worktree, repo);
4922 if (error)
4923 goto done;
4925 error = got_ref_resolve(&head_commit_id, repo, branch);
4926 if (error)
4927 goto done;
4929 error = got_object_open_as_commit(&commit, repo,
4930 head_commit_id);
4931 if (error)
4932 goto done;
4933 parent_ids = got_object_commit_get_parent_ids(commit);
4934 pid = SIMPLEQ_FIRST(parent_ids);
4935 if (pid == NULL) {
4936 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4937 goto done;
4939 error = collect_commits(&commits, head_commit_id, pid->id,
4940 base_commit_id, got_worktree_get_path_prefix(worktree),
4941 GOT_ERR_HISTEDIT_PATH, repo);
4942 got_object_commit_close(commit);
4943 commit = NULL;
4944 if (error)
4945 goto done;
4946 } else {
4947 if (edit_in_progress) {
4948 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4949 goto done;
4952 error = got_ref_open(&branch, repo,
4953 got_worktree_get_head_ref_name(worktree), 0);
4954 if (error != NULL)
4955 goto done;
4957 error = got_ref_resolve(&head_commit_id, repo, branch);
4958 got_ref_close(branch);
4959 branch = NULL;
4960 if (error)
4961 goto done;
4963 error = got_object_open_as_commit(&commit, repo,
4964 head_commit_id);
4965 if (error)
4966 goto done;
4967 parent_ids = got_object_commit_get_parent_ids(commit);
4968 pid = SIMPLEQ_FIRST(parent_ids);
4969 if (pid == NULL) {
4970 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4971 goto done;
4973 error = collect_commits(&commits, head_commit_id, pid->id,
4974 got_worktree_get_base_commit_id(worktree),
4975 got_worktree_get_path_prefix(worktree),
4976 GOT_ERR_HISTEDIT_PATH, repo);
4977 got_object_commit_close(commit);
4978 commit = NULL;
4979 if (error)
4980 goto done;
4982 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4983 &base_commit_id, &fileindex, worktree, repo);
4984 if (error)
4985 goto done;
4987 if (edit_script_path) {
4988 error = histedit_load_list(&histedit_cmds,
4989 edit_script_path, repo);
4990 if (error) {
4991 got_worktree_histedit_abort(worktree, fileindex,
4992 repo, branch, base_commit_id,
4993 update_progress, &did_something);
4994 goto done;
4996 } else {
4997 error = histedit_edit_script(&histedit_cmds, &commits,
4998 repo);
4999 if (error) {
5000 got_worktree_histedit_abort(worktree, fileindex,
5001 repo, branch, base_commit_id,
5002 update_progress, &did_something);
5003 goto done;
5008 error = histedit_save_list(&histedit_cmds, worktree,
5009 repo);
5010 if (error) {
5011 got_worktree_histedit_abort(worktree, fileindex,
5012 repo, branch, base_commit_id,
5013 update_progress, &did_something);
5014 goto done;
5019 error = histedit_check_script(&histedit_cmds, &commits, repo);
5020 if (error)
5021 goto done;
5023 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5024 if (resume_commit_id) {
5025 if (got_object_id_cmp(hle->commit_id,
5026 resume_commit_id) != 0)
5027 continue;
5029 resume_commit_id = NULL;
5030 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5031 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5032 error = histedit_skip_commit(hle, worktree,
5033 repo);
5034 } else {
5035 error = histedit_commit(NULL, worktree,
5036 fileindex, tmp_branch, hle, repo);
5038 if (error)
5039 goto done;
5040 continue;
5043 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5044 error = histedit_skip_commit(hle, worktree, repo);
5045 if (error)
5046 goto done;
5047 continue;
5050 error = got_object_open_as_commit(&commit, repo,
5051 hle->commit_id);
5052 if (error)
5053 goto done;
5054 parent_ids = got_object_commit_get_parent_ids(commit);
5055 pid = SIMPLEQ_FIRST(parent_ids);
5057 error = got_worktree_histedit_merge_files(&merged_paths,
5058 worktree, fileindex, pid->id, hle->commit_id, repo,
5059 rebase_progress, &rebase_status, check_cancelled, NULL);
5060 if (error)
5061 goto done;
5062 got_object_commit_close(commit);
5063 commit = NULL;
5065 if (rebase_status == GOT_STATUS_CONFLICT) {
5066 got_worktree_rebase_pathlist_free(&merged_paths);
5067 break;
5070 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5071 char *id_str;
5072 error = got_object_id_str(&id_str, hle->commit_id);
5073 if (error)
5074 goto done;
5075 printf("Stopping histedit for amending commit %s\n",
5076 id_str);
5077 free(id_str);
5078 got_worktree_rebase_pathlist_free(&merged_paths);
5079 error = got_worktree_histedit_postpone(worktree,
5080 fileindex);
5081 goto done;
5084 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5085 error = histedit_skip_commit(hle, worktree, repo);
5086 if (error)
5087 goto done;
5088 continue;
5091 error = histedit_commit(&merged_paths, worktree, fileindex,
5092 tmp_branch, hle, repo);
5093 got_worktree_rebase_pathlist_free(&merged_paths);
5094 if (error)
5095 goto done;
5098 if (rebase_status == GOT_STATUS_CONFLICT) {
5099 error = got_worktree_histedit_postpone(worktree, fileindex);
5100 if (error)
5101 goto done;
5102 error = got_error_msg(GOT_ERR_CONFLICTS,
5103 "conflicts must be resolved before rebasing can continue");
5104 } else
5105 error = histedit_complete(worktree, fileindex, tmp_branch,
5106 branch, repo);
5107 done:
5108 got_object_id_queue_free(&commits);
5109 histedit_free_list(&histedit_cmds);
5110 free(head_commit_id);
5111 free(base_commit_id);
5112 free(resume_commit_id);
5113 if (commit)
5114 got_object_commit_close(commit);
5115 if (branch)
5116 got_ref_close(branch);
5117 if (tmp_branch)
5118 got_ref_close(tmp_branch);
5119 if (worktree)
5120 got_worktree_close(worktree);
5121 if (repo)
5122 got_repo_close(repo);
5123 return error;