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/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <limits.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_cancel.h"
45 #include "got_worktree.h"
46 #include "got_diff.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
59 static void
60 catch_sigint(int signo)
61 {
62 sigint_received = 1;
63 }
65 static void
66 catch_sigpipe(int signo)
67 {
68 sigpipe_received = 1;
69 }
72 struct got_cmd {
73 const char *cmd_name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
77 };
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_add(void);
92 __dead static void usage_remove(void);
93 __dead static void usage_revert(void);
94 __dead static void usage_commit(void);
95 __dead static void usage_cherrypick(void);
96 __dead static void usage_backout(void);
97 __dead static void usage_rebase(void);
98 __dead static void usage_histedit(void);
99 __dead static void usage_stage(void);
100 __dead static void usage_unstage(void);
101 __dead static void usage_cat(void);
103 static const struct got_error* cmd_init(int, char *[]);
104 static const struct got_error* cmd_import(int, char *[]);
105 static const struct got_error* cmd_checkout(int, char *[]);
106 static const struct got_error* cmd_update(int, char *[]);
107 static const struct got_error* cmd_log(int, char *[]);
108 static const struct got_error* cmd_diff(int, char *[]);
109 static const struct got_error* cmd_blame(int, char *[]);
110 static const struct got_error* cmd_tree(int, char *[]);
111 static const struct got_error* cmd_status(int, char *[]);
112 static const struct got_error* cmd_ref(int, char *[]);
113 static const struct got_error* cmd_branch(int, char *[]);
114 static const struct got_error* cmd_add(int, char *[]);
115 static const struct got_error* cmd_remove(int, char *[]);
116 static const struct got_error* cmd_revert(int, char *[]);
117 static const struct got_error* cmd_commit(int, char *[]);
118 static const struct got_error* cmd_cherrypick(int, char *[]);
119 static const struct got_error* cmd_backout(int, char *[]);
120 static const struct got_error* cmd_rebase(int, char *[]);
121 static const struct got_error* cmd_histedit(int, char *[]);
122 static const struct got_error* cmd_stage(int, char *[]);
123 static const struct got_error* cmd_unstage(int, char *[]);
124 static const struct got_error* cmd_cat(int, char *[]);
126 static struct got_cmd got_commands[] = {
127 { "init", cmd_init, usage_init, "in" },
128 { "import", cmd_import, usage_import, "im" },
129 { "checkout", cmd_checkout, usage_checkout, "co" },
130 { "update", cmd_update, usage_update, "up" },
131 { "log", cmd_log, usage_log, "" },
132 { "diff", cmd_diff, usage_diff, "di" },
133 { "blame", cmd_blame, usage_blame, "bl" },
134 { "tree", cmd_tree, usage_tree, "tr" },
135 { "status", cmd_status, usage_status, "st" },
136 { "ref", cmd_ref, usage_ref, "" },
137 { "branch", cmd_branch, usage_branch, "br" },
138 { "add", cmd_add, usage_add, "" },
139 { "remove", cmd_remove, usage_remove, "rm" },
140 { "revert", cmd_revert, usage_revert, "rv" },
141 { "commit", cmd_commit, usage_commit, "ci" },
142 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
143 { "backout", cmd_backout, usage_backout, "bo" },
144 { "rebase", cmd_rebase, usage_rebase, "rb" },
145 { "histedit", cmd_histedit, usage_histedit, "he" },
146 { "stage", cmd_stage, usage_stage, "sg" },
147 { "unstage", cmd_unstage, usage_unstage, "ug" },
148 { "cat", cmd_cat, usage_cat, "" },
149 };
151 static void
152 list_commands(void)
154 int i;
156 fprintf(stderr, "commands:");
157 for (i = 0; i < nitems(got_commands); i++) {
158 struct got_cmd *cmd = &got_commands[i];
159 fprintf(stderr, " %s", cmd->cmd_name);
161 fputc('\n', stderr);
164 int
165 main(int argc, char *argv[])
167 struct got_cmd *cmd;
168 unsigned int i;
169 int ch;
170 int hflag = 0, Vflag = 0;
172 setlocale(LC_CTYPE, "");
174 while ((ch = getopt(argc, argv, "hV")) != -1) {
175 switch (ch) {
176 case 'h':
177 hflag = 1;
178 break;
179 case 'V':
180 Vflag = 1;
181 break;
182 default:
183 usage(hflag);
184 /* NOTREACHED */
188 argc -= optind;
189 argv += optind;
190 optind = 0;
192 if (Vflag) {
193 got_version_print_str();
194 return 1;
197 if (argc <= 0)
198 usage(hflag);
200 signal(SIGINT, catch_sigint);
201 signal(SIGPIPE, catch_sigpipe);
203 for (i = 0; i < nitems(got_commands); i++) {
204 const struct got_error *error;
206 cmd = &got_commands[i];
208 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
209 strcmp(cmd->cmd_alias, argv[0]) != 0)
210 continue;
212 if (hflag)
213 got_commands[i].cmd_usage();
215 error = got_commands[i].cmd_main(argc, argv);
216 if (error && !(sigint_received || sigpipe_received)) {
217 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
218 return 1;
221 return 0;
224 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
225 list_commands();
226 return 1;
229 __dead static void
230 usage(int hflag)
232 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
233 getprogname());
234 if (hflag)
235 list_commands();
236 exit(1);
239 static const struct got_error *
240 get_editor(char **abspath)
242 const struct got_error *err = NULL;
243 const char *editor;
245 *abspath = NULL;
247 editor = getenv("VISUAL");
248 if (editor == NULL)
249 editor = getenv("EDITOR");
251 if (editor) {
252 err = got_path_find_prog(abspath, editor);
253 if (err)
254 return err;
257 if (*abspath == NULL) {
258 *abspath = strdup("/bin/ed");
259 if (*abspath == NULL)
260 return got_error_from_errno("strdup");
263 return NULL;
266 static const struct got_error *
267 apply_unveil(const char *repo_path, int repo_read_only,
268 const char *worktree_path)
270 const struct got_error *err;
272 #ifdef PROFILE
273 if (unveil("gmon.out", "rwc") != 0)
274 return got_error_from_errno2("unveil", "gmon.out");
275 #endif
276 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
277 return got_error_from_errno2("unveil", repo_path);
279 if (worktree_path && unveil(worktree_path, "rwc") != 0)
280 return got_error_from_errno2("unveil", worktree_path);
282 if (unveil("/tmp", "rwc") != 0)
283 return got_error_from_errno2("unveil", "/tmp");
285 err = got_privsep_unveil_exec_helpers();
286 if (err != NULL)
287 return err;
289 if (unveil(NULL, NULL) != 0)
290 return got_error_from_errno("unveil");
292 return NULL;
295 __dead static void
296 usage_init(void)
298 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
299 exit(1);
302 static const struct got_error *
303 cmd_init(int argc, char *argv[])
305 const struct got_error *error = NULL;
306 char *repo_path = NULL;
307 int ch;
309 while ((ch = getopt(argc, argv, "")) != -1) {
310 switch (ch) {
311 default:
312 usage_init();
313 /* NOTREACHED */
317 argc -= optind;
318 argv += optind;
320 #ifndef PROFILE
321 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
322 err(1, "pledge");
323 #endif
324 if (argc != 1)
325 usage_init();
327 repo_path = strdup(argv[0]);
328 if (repo_path == NULL)
329 return got_error_from_errno("strdup");
331 got_path_strip_trailing_slashes(repo_path);
333 error = got_path_mkdir(repo_path);
334 if (error &&
335 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
336 goto done;
338 error = apply_unveil(repo_path, 0, NULL);
339 if (error)
340 goto done;
342 error = got_repo_init(repo_path);
343 if (error != NULL)
344 goto done;
346 done:
347 free(repo_path);
348 return error;
351 __dead static void
352 usage_import(void)
354 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
355 "[-r repository-path] [-I pattern] path\n", getprogname());
356 exit(1);
359 int
360 spawn_editor(const char *editor, const char *file)
362 pid_t pid;
363 sig_t sighup, sigint, sigquit;
364 int st = -1;
366 sighup = signal(SIGHUP, SIG_IGN);
367 sigint = signal(SIGINT, SIG_IGN);
368 sigquit = signal(SIGQUIT, SIG_IGN);
370 switch (pid = fork()) {
371 case -1:
372 goto doneediting;
373 case 0:
374 execl(editor, editor, file, (char *)NULL);
375 _exit(127);
378 while (waitpid(pid, &st, 0) == -1)
379 if (errno != EINTR)
380 break;
382 doneediting:
383 (void)signal(SIGHUP, sighup);
384 (void)signal(SIGINT, sigint);
385 (void)signal(SIGQUIT, sigquit);
387 if (!WIFEXITED(st)) {
388 errno = EINTR;
389 return -1;
392 return WEXITSTATUS(st);
395 static const struct got_error *
396 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
397 const char *initial_content)
399 const struct got_error *err = NULL;
400 char buf[1024];
401 struct stat st, st2;
402 FILE *fp;
403 int content_changed = 0;
404 size_t len;
406 *logmsg = NULL;
408 if (stat(logmsg_path, &st) == -1)
409 return got_error_from_errno2("stat", logmsg_path);
411 if (spawn_editor(editor, logmsg_path) == -1)
412 return got_error_from_errno("failed spawning editor");
414 if (stat(logmsg_path, &st2) == -1)
415 return got_error_from_errno("stat");
417 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
421 *logmsg = malloc(st2.st_size + 1);
422 if (*logmsg == NULL)
423 return got_error_from_errno("malloc");
424 (*logmsg)[0] = '\0';
425 len = 0;
427 fp = fopen(logmsg_path, "r");
428 if (fp == NULL) {
429 err = got_error_from_errno("fopen");
430 goto done;
432 while (fgets(buf, sizeof(buf), fp) != NULL) {
433 if (!content_changed && strcmp(buf, initial_content) != 0)
434 content_changed = 1;
435 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
436 continue; /* remove comments and leading empty lines */
437 len = strlcat(*logmsg, buf, st2.st_size);
439 fclose(fp);
441 while (len > 0 && (*logmsg)[len - 1] == '\n') {
442 (*logmsg)[len - 1] = '\0';
443 len--;
446 if (len == 0 || !content_changed)
447 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
448 "commit message cannot be empty, aborting");
449 done:
450 if (err) {
451 free(*logmsg);
452 *logmsg = NULL;
454 return err;
457 static const struct got_error *
458 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
459 const char *branch_name)
461 char *initial_content = NULL, *logmsg_path = NULL;
462 const struct got_error *err = NULL;
463 int fd;
465 if (asprintf(&initial_content,
466 "\n# %s to be imported to branch %s\n", path_dir,
467 branch_name) == -1)
468 return got_error_from_errno("asprintf");
470 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
471 if (err)
472 goto done;
474 dprintf(fd, initial_content);
475 close(fd);
477 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
478 done:
479 free(initial_content);
480 free(logmsg_path);
481 return err;
484 static const struct got_error *
485 import_progress(void *arg, const char *path)
487 printf("A %s\n", path);
488 return NULL;
491 static const struct got_error *
492 get_author(const char **author)
494 const char *got_author;
496 *author = NULL;
498 got_author = getenv("GOT_AUTHOR");
499 if (got_author == NULL) {
500 /* TODO: Look up user in password database? */
501 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
504 *author = got_author;
506 /*
507 * Really dumb email address check; we're only doing this to
508 * avoid git's object parser breaking on commits we create.
509 */
510 while (*got_author && *got_author != '<')
511 got_author++;
512 if (*got_author != '<')
513 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
514 while (*got_author && *got_author != '@')
515 got_author++;
516 if (*got_author != '@')
517 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
518 while (*got_author && *got_author != '>')
519 got_author++;
520 if (*got_author != '>')
521 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
523 return NULL;
526 static const struct got_error *
527 cmd_import(int argc, char *argv[])
529 const struct got_error *error = NULL;
530 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
531 char *editor = NULL;
532 const char *author;
533 const char *branch_name = "master";
534 char *refname = NULL, *id_str = NULL;
535 struct got_repository *repo = NULL;
536 struct got_reference *branch_ref = NULL, *head_ref = NULL;
537 struct got_object_id *new_commit_id = NULL;
538 int ch;
539 struct got_pathlist_head ignores;
540 struct got_pathlist_entry *pe;
542 TAILQ_INIT(&ignores);
544 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
545 switch (ch) {
546 case 'b':
547 branch_name = optarg;
548 break;
549 case 'm':
550 logmsg = strdup(optarg);
551 if (logmsg == NULL) {
552 error = got_error_from_errno("strdup");
553 goto done;
555 break;
556 case 'r':
557 repo_path = realpath(optarg, NULL);
558 if (repo_path == NULL) {
559 error = got_error_from_errno("realpath");
560 goto done;
562 break;
563 case 'I':
564 if (optarg[0] == '\0')
565 break;
566 error = got_pathlist_insert(&pe, &ignores, optarg,
567 NULL);
568 if (error)
569 goto done;
570 break;
571 default:
572 usage_init();
573 /* NOTREACHED */
577 argc -= optind;
578 argv += optind;
580 #ifndef PROFILE
581 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
582 NULL) == -1)
583 err(1, "pledge");
584 #endif
585 if (argc != 1)
586 usage_import();
588 error = get_author(&author);
589 if (error)
590 return error;
592 if (repo_path == NULL) {
593 repo_path = getcwd(NULL, 0);
594 if (repo_path == NULL)
595 return got_error_from_errno("getcwd");
597 got_path_strip_trailing_slashes(repo_path);
598 error = got_repo_open(&repo, repo_path);
599 if (error)
600 goto done;
602 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
603 error = got_error_from_errno("asprintf");
604 goto done;
607 error = got_ref_open(&branch_ref, repo, refname, 0);
608 if (error) {
609 if (error->code != GOT_ERR_NOT_REF)
610 goto done;
611 } else {
612 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
613 "import target branch already exists");
614 goto done;
617 path_dir = realpath(argv[0], NULL);
618 if (path_dir == NULL) {
619 error = got_error_from_errno("realpath");
620 goto done;
622 got_path_strip_trailing_slashes(path_dir);
624 /*
625 * unveil(2) traverses exec(2); if an editor is used we have
626 * to apply unveil after the log message has been written.
627 */
628 if (logmsg == NULL || strlen(logmsg) == 0) {
629 error = get_editor(&editor);
630 if (error)
631 goto done;
632 error = collect_import_msg(&logmsg, editor, path_dir, refname);
633 if (error)
634 goto done;
637 if (unveil(path_dir, "r") != 0)
638 return got_error_from_errno2("unveil", path_dir);
640 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
641 if (error)
642 goto done;
644 error = got_repo_import(&new_commit_id, path_dir, logmsg,
645 author, &ignores, repo, import_progress, NULL);
646 if (error)
647 goto done;
649 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
650 if (error)
651 goto done;
653 error = got_ref_write(branch_ref, repo);
654 if (error)
655 goto done;
657 error = got_object_id_str(&id_str, new_commit_id);
658 if (error)
659 goto done;
661 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
662 if (error) {
663 if (error->code != GOT_ERR_NOT_REF)
664 goto done;
666 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
667 branch_ref);
668 if (error)
669 goto done;
671 error = got_ref_write(head_ref, repo);
672 if (error)
673 goto done;
676 printf("Created branch %s with commit %s\n",
677 got_ref_get_name(branch_ref), id_str);
678 done:
679 free(repo_path);
680 free(editor);
681 free(refname);
682 free(new_commit_id);
683 free(id_str);
684 if (branch_ref)
685 got_ref_close(branch_ref);
686 if (head_ref)
687 got_ref_close(head_ref);
688 return error;
691 __dead static void
692 usage_checkout(void)
694 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
695 "[-p prefix] repository-path [worktree-path]\n", getprogname());
696 exit(1);
699 static const struct got_error *
700 checkout_progress(void *arg, unsigned char status, const char *path)
702 char *worktree_path = arg;
704 /* Base commit bump happens silently. */
705 if (status == GOT_STATUS_BUMP_BASE)
706 return NULL;
708 while (path[0] == '/')
709 path++;
711 printf("%c %s/%s\n", status, worktree_path, path);
712 return NULL;
715 static const struct got_error *
716 check_cancelled(void *arg)
718 if (sigint_received || sigpipe_received)
719 return got_error(GOT_ERR_CANCELLED);
720 return NULL;
723 static const struct got_error *
724 check_linear_ancestry(struct got_object_id *commit_id,
725 struct got_object_id *base_commit_id, struct got_repository *repo)
727 const struct got_error *err = NULL;
728 struct got_object_id *yca_id;
730 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
731 commit_id, base_commit_id, repo, check_cancelled, NULL);
732 if (err)
733 return err;
735 if (yca_id == NULL)
736 return got_error(GOT_ERR_ANCESTRY);
738 /*
739 * Require a straight line of history between the target commit
740 * and the work tree's base commit.
742 * Non-linear situations such as this require a rebase:
744 * (commit) D F (base_commit)
745 * \ /
746 * C E
747 * \ /
748 * B (yca)
749 * |
750 * A
752 * 'got update' only handles linear cases:
753 * Update forwards in time: A (base/yca) - B - C - D (commit)
754 * Update backwards in time: D (base) - C - B - A (commit/yca)
755 */
756 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
757 got_object_id_cmp(base_commit_id, yca_id) != 0)
758 return got_error(GOT_ERR_ANCESTRY);
760 free(yca_id);
761 return NULL;
764 static const struct got_error *
765 check_same_branch(struct got_object_id *commit_id,
766 struct got_reference *head_ref, struct got_object_id *yca_id,
767 struct got_repository *repo)
769 const struct got_error *err = NULL;
770 struct got_commit_graph *graph = NULL;
771 struct got_object_id *head_commit_id = NULL;
772 int is_same_branch = 0;
774 err = got_ref_resolve(&head_commit_id, repo, head_ref);
775 if (err)
776 goto done;
778 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
779 is_same_branch = 1;
780 goto done;
782 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
783 is_same_branch = 1;
784 goto done;
787 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
788 if (err)
789 goto done;
791 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
792 check_cancelled, NULL);
793 if (err)
794 goto done;
796 for (;;) {
797 struct got_object_id *id;
798 err = got_commit_graph_iter_next(&id, graph);
799 if (err) {
800 if (err->code == GOT_ERR_ITER_COMPLETED) {
801 err = NULL;
802 break;
803 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
804 break;
805 err = got_commit_graph_fetch_commits(graph, 1,
806 repo, check_cancelled, NULL);
807 if (err)
808 break;
811 if (id) {
812 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
813 break;
814 if (got_object_id_cmp(id, commit_id) == 0) {
815 is_same_branch = 1;
816 break;
820 done:
821 if (graph)
822 got_commit_graph_close(graph);
823 free(head_commit_id);
824 if (!err && !is_same_branch)
825 err = got_error(GOT_ERR_ANCESTRY);
826 return err;
829 static const struct got_error *
830 resolve_commit_arg(struct got_object_id **commit_id,
831 const char *commit_id_arg, struct got_repository *repo)
833 const struct got_error *err;
834 struct got_reference *ref;
835 struct got_tag_object *tag;
837 err = got_repo_object_match_tag(&tag, commit_id_arg,
838 GOT_OBJ_TYPE_COMMIT, repo);
839 if (err == NULL) {
840 *commit_id = got_object_id_dup(
841 got_object_tag_get_object_id(tag));
842 if (*commit_id == NULL)
843 err = got_error_from_errno("got_object_id_dup");
844 got_object_tag_close(tag);
845 return err;
846 } else if (err->code != GOT_ERR_NO_OBJ)
847 return err;
849 err = got_ref_open(&ref, repo, commit_id_arg, 0);
850 if (err == NULL) {
851 err = got_ref_resolve(commit_id, repo, ref);
852 got_ref_close(ref);
853 } else {
854 if (err->code != GOT_ERR_NOT_REF)
855 return err;
856 err = got_repo_match_object_id_prefix(commit_id,
857 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
859 return err;
862 static const struct got_error *
863 cmd_checkout(int argc, char *argv[])
865 const struct got_error *error = NULL;
866 struct got_repository *repo = NULL;
867 struct got_reference *head_ref = NULL;
868 struct got_worktree *worktree = NULL;
869 char *repo_path = NULL;
870 char *worktree_path = NULL;
871 const char *path_prefix = "";
872 const char *branch_name = GOT_REF_HEAD;
873 char *commit_id_str = NULL;
874 int ch, same_path_prefix;
875 struct got_pathlist_head paths;
877 TAILQ_INIT(&paths);
879 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
880 switch (ch) {
881 case 'b':
882 branch_name = optarg;
883 break;
884 case 'c':
885 commit_id_str = strdup(optarg);
886 if (commit_id_str == NULL)
887 return got_error_from_errno("strdup");
888 break;
889 case 'p':
890 path_prefix = optarg;
891 break;
892 default:
893 usage_checkout();
894 /* NOTREACHED */
898 argc -= optind;
899 argv += optind;
901 #ifndef PROFILE
902 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
903 "unveil", NULL) == -1)
904 err(1, "pledge");
905 #endif
906 if (argc == 1) {
907 char *cwd, *base, *dotgit;
908 repo_path = realpath(argv[0], NULL);
909 if (repo_path == NULL)
910 return got_error_from_errno2("realpath", argv[0]);
911 cwd = getcwd(NULL, 0);
912 if (cwd == NULL) {
913 error = got_error_from_errno("getcwd");
914 goto done;
916 if (path_prefix[0]) {
917 base = basename(path_prefix);
918 if (base == NULL) {
919 error = got_error_from_errno2("basename",
920 path_prefix);
921 goto done;
923 } else {
924 base = basename(repo_path);
925 if (base == NULL) {
926 error = got_error_from_errno2("basename",
927 repo_path);
928 goto done;
931 dotgit = strstr(base, ".git");
932 if (dotgit)
933 *dotgit = '\0';
934 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
935 error = got_error_from_errno("asprintf");
936 free(cwd);
937 goto done;
939 free(cwd);
940 } else if (argc == 2) {
941 repo_path = realpath(argv[0], NULL);
942 if (repo_path == NULL) {
943 error = got_error_from_errno2("realpath", argv[0]);
944 goto done;
946 worktree_path = realpath(argv[1], NULL);
947 if (worktree_path == NULL) {
948 if (errno != ENOENT) {
949 error = got_error_from_errno2("realpath",
950 argv[1]);
951 goto done;
953 worktree_path = strdup(argv[1]);
954 if (worktree_path == NULL) {
955 error = got_error_from_errno("strdup");
956 goto done;
959 } else
960 usage_checkout();
962 got_path_strip_trailing_slashes(repo_path);
963 got_path_strip_trailing_slashes(worktree_path);
965 error = got_repo_open(&repo, repo_path);
966 if (error != NULL)
967 goto done;
969 /* Pre-create work tree path for unveil(2) */
970 error = got_path_mkdir(worktree_path);
971 if (error) {
972 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
973 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
974 goto done;
975 if (!got_path_dir_is_empty(worktree_path)) {
976 error = got_error_path(worktree_path,
977 GOT_ERR_DIR_NOT_EMPTY);
978 goto done;
982 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
983 if (error)
984 goto done;
986 error = got_ref_open(&head_ref, repo, branch_name, 0);
987 if (error != NULL)
988 goto done;
990 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
991 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
992 goto done;
994 error = got_worktree_open(&worktree, worktree_path);
995 if (error != NULL)
996 goto done;
998 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
999 path_prefix);
1000 if (error != NULL)
1001 goto done;
1002 if (!same_path_prefix) {
1003 error = got_error(GOT_ERR_PATH_PREFIX);
1004 goto done;
1007 if (commit_id_str) {
1008 struct got_object_id *commit_id;
1009 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1010 if (error)
1011 goto done;
1012 error = check_linear_ancestry(commit_id,
1013 got_worktree_get_base_commit_id(worktree), repo);
1014 if (error != NULL) {
1015 free(commit_id);
1016 goto done;
1018 error = check_same_branch(commit_id, head_ref, NULL, repo);
1019 if (error)
1020 goto done;
1021 error = got_worktree_set_base_commit_id(worktree, repo,
1022 commit_id);
1023 free(commit_id);
1024 if (error)
1025 goto done;
1028 error = got_pathlist_append(&paths, "", NULL);
1029 if (error)
1030 goto done;
1031 error = got_worktree_checkout_files(worktree, &paths, repo,
1032 checkout_progress, worktree_path, check_cancelled, NULL);
1033 if (error != NULL)
1034 goto done;
1036 printf("Now shut up and hack\n");
1038 done:
1039 got_pathlist_free(&paths);
1040 free(commit_id_str);
1041 free(repo_path);
1042 free(worktree_path);
1043 return error;
1046 __dead static void
1047 usage_update(void)
1049 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1050 getprogname());
1051 exit(1);
1054 static const struct got_error *
1055 update_progress(void *arg, unsigned char status, const char *path)
1057 int *did_something = arg;
1059 if (status == GOT_STATUS_EXISTS)
1060 return NULL;
1062 *did_something = 1;
1064 /* Base commit bump happens silently. */
1065 if (status == GOT_STATUS_BUMP_BASE)
1066 return NULL;
1068 while (path[0] == '/')
1069 path++;
1070 printf("%c %s\n", status, path);
1071 return NULL;
1074 static const struct got_error *
1075 switch_head_ref(struct got_reference *head_ref,
1076 struct got_object_id *commit_id, struct got_worktree *worktree,
1077 struct got_repository *repo)
1079 const struct got_error *err = NULL;
1080 char *base_id_str;
1081 int ref_has_moved = 0;
1083 /* Trivial case: switching between two different references. */
1084 if (strcmp(got_ref_get_name(head_ref),
1085 got_worktree_get_head_ref_name(worktree)) != 0) {
1086 printf("Switching work tree from %s to %s\n",
1087 got_worktree_get_head_ref_name(worktree),
1088 got_ref_get_name(head_ref));
1089 return got_worktree_set_head_ref(worktree, head_ref);
1092 err = check_linear_ancestry(commit_id,
1093 got_worktree_get_base_commit_id(worktree), repo);
1094 if (err) {
1095 if (err->code != GOT_ERR_ANCESTRY)
1096 return err;
1097 ref_has_moved = 1;
1099 if (!ref_has_moved)
1100 return NULL;
1102 /* Switching to a rebased branch with the same reference name. */
1103 err = got_object_id_str(&base_id_str,
1104 got_worktree_get_base_commit_id(worktree));
1105 if (err)
1106 return err;
1107 printf("Reference %s now points at a different branch\n",
1108 got_worktree_get_head_ref_name(worktree));
1109 printf("Switching work tree from %s to %s\n", base_id_str,
1110 got_worktree_get_head_ref_name(worktree));
1111 return NULL;
1114 static const struct got_error *
1115 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1117 const struct got_error *err;
1118 int in_progress;
1120 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1121 if (err)
1122 return err;
1123 if (in_progress)
1124 return got_error(GOT_ERR_REBASING);
1126 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1127 if (err)
1128 return err;
1129 if (in_progress)
1130 return got_error(GOT_ERR_HISTEDIT_BUSY);
1132 return NULL;
1135 static const struct got_error *
1136 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1137 char *argv[], struct got_worktree *worktree)
1139 const struct got_error *err = NULL;
1140 char *path;
1141 int i;
1143 if (argc == 0) {
1144 path = strdup("");
1145 if (path == NULL)
1146 return got_error_from_errno("strdup");
1147 return got_pathlist_append(paths, path, NULL);
1150 for (i = 0; i < argc; i++) {
1151 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1152 if (err)
1153 break;
1154 err = got_pathlist_append(paths, path, NULL);
1155 if (err) {
1156 free(path);
1157 break;
1161 return err;
1164 static const struct got_error *
1165 cmd_update(int argc, char *argv[])
1167 const struct got_error *error = NULL;
1168 struct got_repository *repo = NULL;
1169 struct got_worktree *worktree = NULL;
1170 char *worktree_path = NULL;
1171 struct got_object_id *commit_id = NULL;
1172 char *commit_id_str = NULL;
1173 const char *branch_name = NULL;
1174 struct got_reference *head_ref = NULL;
1175 struct got_pathlist_head paths;
1176 struct got_pathlist_entry *pe;
1177 int ch, did_something = 0;
1179 TAILQ_INIT(&paths);
1181 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1182 switch (ch) {
1183 case 'b':
1184 branch_name = optarg;
1185 break;
1186 case 'c':
1187 commit_id_str = strdup(optarg);
1188 if (commit_id_str == NULL)
1189 return got_error_from_errno("strdup");
1190 break;
1191 default:
1192 usage_update();
1193 /* NOTREACHED */
1197 argc -= optind;
1198 argv += optind;
1200 #ifndef PROFILE
1201 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1202 "unveil", NULL) == -1)
1203 err(1, "pledge");
1204 #endif
1205 worktree_path = getcwd(NULL, 0);
1206 if (worktree_path == NULL) {
1207 error = got_error_from_errno("getcwd");
1208 goto done;
1210 error = got_worktree_open(&worktree, worktree_path);
1211 if (error)
1212 goto done;
1214 error = check_rebase_or_histedit_in_progress(worktree);
1215 if (error)
1216 goto done;
1218 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1219 if (error != NULL)
1220 goto done;
1222 error = apply_unveil(got_repo_get_path(repo), 0,
1223 got_worktree_get_root_path(worktree));
1224 if (error)
1225 goto done;
1227 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1228 if (error)
1229 goto done;
1231 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1232 got_worktree_get_head_ref_name(worktree), 0);
1233 if (error != NULL)
1234 goto done;
1235 if (commit_id_str == NULL) {
1236 error = got_ref_resolve(&commit_id, repo, head_ref);
1237 if (error != NULL)
1238 goto done;
1239 error = got_object_id_str(&commit_id_str, commit_id);
1240 if (error != NULL)
1241 goto done;
1242 } else {
1243 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1244 free(commit_id_str);
1245 commit_id_str = NULL;
1246 if (error)
1247 goto done;
1248 error = got_object_id_str(&commit_id_str, commit_id);
1249 if (error)
1250 goto done;
1253 if (branch_name) {
1254 struct got_object_id *head_commit_id;
1255 TAILQ_FOREACH(pe, &paths, entry) {
1256 if (pe->path_len == 0)
1257 continue;
1258 error = got_error_msg(GOT_ERR_BAD_PATH,
1259 "switching between branches requires that "
1260 "the entire work tree gets updated");
1261 goto done;
1263 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1264 if (error)
1265 goto done;
1266 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1267 free(head_commit_id);
1268 if (error != NULL)
1269 goto done;
1270 error = check_same_branch(commit_id, head_ref, NULL, repo);
1271 if (error)
1272 goto done;
1273 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1274 if (error)
1275 goto done;
1276 } else {
1277 error = check_linear_ancestry(commit_id,
1278 got_worktree_get_base_commit_id(worktree), repo);
1279 if (error != NULL) {
1280 if (error->code == GOT_ERR_ANCESTRY)
1281 error = got_error(GOT_ERR_BRANCH_MOVED);
1282 goto done;
1284 error = check_same_branch(commit_id, head_ref, NULL, repo);
1285 if (error)
1286 goto done;
1289 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1290 commit_id) != 0) {
1291 error = got_worktree_set_base_commit_id(worktree, repo,
1292 commit_id);
1293 if (error)
1294 goto done;
1297 error = got_worktree_checkout_files(worktree, &paths, repo,
1298 update_progress, &did_something, check_cancelled, NULL);
1299 if (error != NULL)
1300 goto done;
1302 if (did_something)
1303 printf("Updated to commit %s\n", commit_id_str);
1304 else
1305 printf("Already up-to-date\n");
1306 done:
1307 free(worktree_path);
1308 TAILQ_FOREACH(pe, &paths, entry)
1309 free((char *)pe->path);
1310 got_pathlist_free(&paths);
1311 free(commit_id);
1312 free(commit_id_str);
1313 return error;
1316 static const struct got_error *
1317 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1318 int diff_context, struct got_repository *repo)
1320 const struct got_error *err = NULL;
1321 struct got_tree_object *tree1 = NULL, *tree2;
1322 struct got_object_qid *qid;
1323 char *id_str1 = NULL, *id_str2;
1324 struct got_diff_blob_output_unidiff_arg arg;
1326 err = got_object_open_as_tree(&tree2, repo,
1327 got_object_commit_get_tree_id(commit));
1328 if (err)
1329 return err;
1331 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1332 if (qid != NULL) {
1333 struct got_commit_object *pcommit;
1335 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1336 if (err)
1337 return err;
1339 err = got_object_open_as_tree(&tree1, repo,
1340 got_object_commit_get_tree_id(pcommit));
1341 got_object_commit_close(pcommit);
1342 if (err)
1343 return err;
1345 err = got_object_id_str(&id_str1, qid->id);
1346 if (err)
1347 return err;
1350 err = got_object_id_str(&id_str2, id);
1351 if (err)
1352 goto done;
1354 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1355 arg.diff_context = diff_context;
1356 arg.outfile = stdout;
1357 err = got_diff_tree(tree1, tree2, "", "", repo,
1358 got_diff_blob_output_unidiff, &arg, 1);
1359 done:
1360 if (tree1)
1361 got_object_tree_close(tree1);
1362 got_object_tree_close(tree2);
1363 free(id_str1);
1364 free(id_str2);
1365 return err;
1368 static char *
1369 get_datestr(time_t *time, char *datebuf)
1371 struct tm mytm, *tm;
1372 char *p, *s;
1374 tm = gmtime_r(time, &mytm);
1375 if (tm == NULL)
1376 return NULL;
1377 s = asctime_r(tm, datebuf);
1378 if (s == NULL)
1379 return NULL;
1380 p = strchr(s, '\n');
1381 if (p)
1382 *p = '\0';
1383 return s;
1386 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1388 static const struct got_error *
1389 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1390 struct got_repository *repo, int show_patch, int diff_context,
1391 struct got_reflist_head *refs)
1393 const struct got_error *err = NULL;
1394 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1395 char datebuf[26];
1396 time_t committer_time;
1397 const char *author, *committer;
1398 char *refs_str = NULL;
1399 struct got_reflist_entry *re;
1401 SIMPLEQ_FOREACH(re, refs, entry) {
1402 char *s;
1403 const char *name;
1404 struct got_tag_object *tag = NULL;
1405 int cmp;
1407 name = got_ref_get_name(re->ref);
1408 if (strcmp(name, GOT_REF_HEAD) == 0)
1409 continue;
1410 if (strncmp(name, "refs/", 5) == 0)
1411 name += 5;
1412 if (strncmp(name, "got/", 4) == 0)
1413 continue;
1414 if (strncmp(name, "heads/", 6) == 0)
1415 name += 6;
1416 if (strncmp(name, "remotes/", 8) == 0)
1417 name += 8;
1418 if (strncmp(name, "tags/", 5) == 0) {
1419 err = got_object_open_as_tag(&tag, repo, re->id);
1420 if (err) {
1421 if (err->code != GOT_ERR_OBJ_TYPE)
1422 return err;
1423 /* Ref points at something other than a tag. */
1424 err = NULL;
1425 tag = NULL;
1428 cmp = got_object_id_cmp(tag ?
1429 got_object_tag_get_object_id(tag) : re->id, id);
1430 if (tag)
1431 got_object_tag_close(tag);
1432 if (cmp != 0)
1433 continue;
1434 s = refs_str;
1435 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1436 name) == -1) {
1437 err = got_error_from_errno("asprintf");
1438 free(s);
1439 return err;
1441 free(s);
1443 err = got_object_id_str(&id_str, id);
1444 if (err)
1445 return err;
1447 printf(GOT_COMMIT_SEP_STR);
1448 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1449 refs_str ? refs_str : "", refs_str ? ")" : "");
1450 free(id_str);
1451 id_str = NULL;
1452 free(refs_str);
1453 refs_str = NULL;
1454 printf("from: %s\n", got_object_commit_get_author(commit));
1455 committer_time = got_object_commit_get_committer_time(commit);
1456 datestr = get_datestr(&committer_time, datebuf);
1457 if (datestr)
1458 printf("date: %s UTC\n", datestr);
1459 author = got_object_commit_get_author(commit);
1460 committer = got_object_commit_get_committer(commit);
1461 if (strcmp(author, committer) != 0)
1462 printf("via: %s\n", committer);
1463 if (got_object_commit_get_nparents(commit) > 1) {
1464 const struct got_object_id_queue *parent_ids;
1465 struct got_object_qid *qid;
1466 int n = 1;
1467 parent_ids = got_object_commit_get_parent_ids(commit);
1468 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1469 err = got_object_id_str(&id_str, qid->id);
1470 if (err)
1471 return err;
1472 printf("parent %d: %s\n", n++, id_str);
1473 free(id_str);
1477 err = got_object_commit_get_logmsg(&logmsg0, commit);
1478 if (err)
1479 return err;
1481 logmsg = logmsg0;
1482 do {
1483 line = strsep(&logmsg, "\n");
1484 if (line)
1485 printf(" %s\n", line);
1486 } while (line);
1487 free(logmsg0);
1489 if (show_patch) {
1490 err = print_patch(commit, id, diff_context, repo);
1491 if (err == 0)
1492 printf("\n");
1495 if (fflush(stdout) != 0 && err == NULL)
1496 err = got_error_from_errno("fflush");
1497 return err;
1500 static const struct got_error *
1501 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1502 char *path, int show_patch, int diff_context, int limit,
1503 int first_parent_traversal, struct got_reflist_head *refs)
1505 const struct got_error *err;
1506 struct got_commit_graph *graph;
1508 err = got_commit_graph_open(&graph, root_id, path,
1509 first_parent_traversal, repo);
1510 if (err)
1511 return err;
1512 err = got_commit_graph_iter_start(graph, root_id, repo,
1513 check_cancelled, NULL);
1514 if (err)
1515 goto done;
1516 for (;;) {
1517 struct got_commit_object *commit;
1518 struct got_object_id *id;
1520 if (sigint_received || sigpipe_received)
1521 break;
1523 err = got_commit_graph_iter_next(&id, graph);
1524 if (err) {
1525 if (err->code == GOT_ERR_ITER_COMPLETED) {
1526 err = NULL;
1527 break;
1529 if (err->code != GOT_ERR_ITER_NEED_MORE)
1530 break;
1531 err = got_commit_graph_fetch_commits(graph, 1, repo,
1532 check_cancelled, NULL);
1533 if (err)
1534 break;
1535 else
1536 continue;
1538 if (id == NULL)
1539 break;
1541 err = got_object_open_as_commit(&commit, repo, id);
1542 if (err)
1543 break;
1544 err = print_commit(commit, id, repo, show_patch, diff_context,
1545 refs);
1546 got_object_commit_close(commit);
1547 if (err || (limit && --limit == 0))
1548 break;
1550 done:
1551 got_commit_graph_close(graph);
1552 return err;
1555 __dead static void
1556 usage_log(void)
1558 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1559 "[-r repository-path] [path]\n", getprogname());
1560 exit(1);
1563 static int
1564 get_default_log_limit(void)
1566 const char *got_default_log_limit;
1567 long long n;
1568 const char *errstr;
1570 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1571 if (got_default_log_limit == NULL)
1572 return 0;
1573 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1574 if (errstr != NULL)
1575 return 0;
1576 return n;
1579 static const struct got_error *
1580 cmd_log(int argc, char *argv[])
1582 const struct got_error *error;
1583 struct got_repository *repo = NULL;
1584 struct got_worktree *worktree = NULL;
1585 struct got_commit_object *commit = NULL;
1586 struct got_object_id *id = NULL;
1587 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1588 char *start_commit = NULL;
1589 int diff_context = 3, ch;
1590 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1591 const char *errstr;
1592 struct got_reflist_head refs;
1594 SIMPLEQ_INIT(&refs);
1596 #ifndef PROFILE
1597 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1598 NULL)
1599 == -1)
1600 err(1, "pledge");
1601 #endif
1603 limit = get_default_log_limit();
1605 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1606 switch (ch) {
1607 case 'p':
1608 show_patch = 1;
1609 break;
1610 case 'c':
1611 start_commit = optarg;
1612 break;
1613 case 'C':
1614 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1615 &errstr);
1616 if (errstr != NULL)
1617 err(1, "-C option %s", errstr);
1618 break;
1619 case 'l':
1620 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1621 if (errstr != NULL)
1622 err(1, "-l option %s", errstr);
1623 break;
1624 case 'f':
1625 first_parent_traversal = 1;
1626 break;
1627 case 'r':
1628 repo_path = realpath(optarg, NULL);
1629 if (repo_path == NULL)
1630 err(1, "-r option");
1631 got_path_strip_trailing_slashes(repo_path);
1632 break;
1633 default:
1634 usage_log();
1635 /* NOTREACHED */
1639 argc -= optind;
1640 argv += optind;
1642 cwd = getcwd(NULL, 0);
1643 if (cwd == NULL) {
1644 error = got_error_from_errno("getcwd");
1645 goto done;
1648 error = got_worktree_open(&worktree, cwd);
1649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1650 goto done;
1651 error = NULL;
1653 if (argc == 0) {
1654 path = strdup("");
1655 if (path == NULL) {
1656 error = got_error_from_errno("strdup");
1657 goto done;
1659 } else if (argc == 1) {
1660 if (worktree) {
1661 error = got_worktree_resolve_path(&path, worktree,
1662 argv[0]);
1663 if (error)
1664 goto done;
1665 } else {
1666 path = strdup(argv[0]);
1667 if (path == NULL) {
1668 error = got_error_from_errno("strdup");
1669 goto done;
1672 } else
1673 usage_log();
1675 if (repo_path == NULL) {
1676 repo_path = worktree ?
1677 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1679 if (repo_path == NULL) {
1680 error = got_error_from_errno("strdup");
1681 goto done;
1684 error = got_repo_open(&repo, repo_path);
1685 if (error != NULL)
1686 goto done;
1688 error = apply_unveil(got_repo_get_path(repo), 1,
1689 worktree ? got_worktree_get_root_path(worktree) : NULL);
1690 if (error)
1691 goto done;
1693 if (start_commit == NULL) {
1694 struct got_reference *head_ref;
1695 error = got_ref_open(&head_ref, repo,
1696 worktree ? got_worktree_get_head_ref_name(worktree)
1697 : GOT_REF_HEAD, 0);
1698 if (error != NULL)
1699 return error;
1700 error = got_ref_resolve(&id, repo, head_ref);
1701 got_ref_close(head_ref);
1702 if (error != NULL)
1703 return error;
1704 error = got_object_open_as_commit(&commit, repo, id);
1705 } else {
1706 struct got_reference *ref;
1707 error = got_ref_open(&ref, repo, start_commit, 0);
1708 if (error == NULL) {
1709 int obj_type;
1710 error = got_ref_resolve(&id, repo, ref);
1711 got_ref_close(ref);
1712 if (error != NULL)
1713 goto done;
1714 error = got_object_get_type(&obj_type, repo, id);
1715 if (error != NULL)
1716 goto done;
1717 if (obj_type == GOT_OBJ_TYPE_TAG) {
1718 struct got_tag_object *tag;
1719 error = got_object_open_as_tag(&tag, repo, id);
1720 if (error != NULL)
1721 goto done;
1722 if (got_object_tag_get_object_type(tag) !=
1723 GOT_OBJ_TYPE_COMMIT) {
1724 got_object_tag_close(tag);
1725 error = got_error(GOT_ERR_OBJ_TYPE);
1726 goto done;
1728 free(id);
1729 id = got_object_id_dup(
1730 got_object_tag_get_object_id(tag));
1731 if (id == NULL)
1732 error = got_error_from_errno(
1733 "got_object_id_dup");
1734 got_object_tag_close(tag);
1735 if (error)
1736 goto done;
1737 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1738 error = got_error(GOT_ERR_OBJ_TYPE);
1739 goto done;
1741 error = got_object_open_as_commit(&commit, repo, id);
1742 if (error != NULL)
1743 goto done;
1745 if (commit == NULL) {
1746 error = got_repo_match_object_id_prefix(&id,
1747 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1748 if (error != NULL)
1749 return error;
1752 if (error != NULL)
1753 goto done;
1755 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1756 if (error != NULL)
1757 goto done;
1758 if (in_repo_path) {
1759 free(path);
1760 path = in_repo_path;
1763 error = got_ref_list(&refs, repo);
1764 if (error)
1765 goto done;
1767 error = print_commits(id, repo, path, show_patch,
1768 diff_context, limit, first_parent_traversal, &refs);
1769 done:
1770 free(path);
1771 free(repo_path);
1772 free(cwd);
1773 free(id);
1774 if (worktree)
1775 got_worktree_close(worktree);
1776 if (repo) {
1777 const struct got_error *repo_error;
1778 repo_error = got_repo_close(repo);
1779 if (error == NULL)
1780 error = repo_error;
1782 got_ref_list_free(&refs);
1783 return error;
1786 __dead static void
1787 usage_diff(void)
1789 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1790 "[object1 object2 | path]\n", getprogname());
1791 exit(1);
1794 struct print_diff_arg {
1795 struct got_repository *repo;
1796 struct got_worktree *worktree;
1797 int diff_context;
1798 const char *id_str;
1799 int header_shown;
1800 int diff_staged;
1803 static const struct got_error *
1804 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1805 const char *path, struct got_object_id *blob_id,
1806 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1808 struct print_diff_arg *a = arg;
1809 const struct got_error *err = NULL;
1810 struct got_blob_object *blob1 = NULL;
1811 FILE *f2 = NULL;
1812 char *abspath = NULL, *label1 = NULL;
1813 struct stat sb;
1815 if (a->diff_staged) {
1816 if (staged_status != GOT_STATUS_MODIFY &&
1817 staged_status != GOT_STATUS_ADD &&
1818 staged_status != GOT_STATUS_DELETE)
1819 return NULL;
1820 } else {
1821 if (staged_status == GOT_STATUS_DELETE)
1822 return NULL;
1823 if (status != GOT_STATUS_MODIFY &&
1824 status != GOT_STATUS_ADD &&
1825 status != GOT_STATUS_DELETE &&
1826 status != GOT_STATUS_CONFLICT)
1827 return NULL;
1830 if (!a->header_shown) {
1831 printf("diff %s %s%s\n", a->id_str,
1832 got_worktree_get_root_path(a->worktree),
1833 a->diff_staged ? " (staged changes)" : "");
1834 a->header_shown = 1;
1837 if (a->diff_staged) {
1838 const char *label1 = NULL, *label2 = NULL;
1839 switch (staged_status) {
1840 case GOT_STATUS_MODIFY:
1841 label1 = path;
1842 label2 = path;
1843 break;
1844 case GOT_STATUS_ADD:
1845 label2 = path;
1846 break;
1847 case GOT_STATUS_DELETE:
1848 label1 = path;
1849 break;
1850 default:
1851 return got_error(GOT_ERR_FILE_STATUS);
1853 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1854 label1, label2, a->diff_context, a->repo, stdout);
1857 if (staged_status == GOT_STATUS_ADD ||
1858 staged_status == GOT_STATUS_MODIFY) {
1859 char *id_str;
1860 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1861 8192);
1862 if (err)
1863 goto done;
1864 err = got_object_id_str(&id_str, staged_blob_id);
1865 if (err)
1866 goto done;
1867 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1868 err = got_error_from_errno("asprintf");
1869 free(id_str);
1870 goto done;
1872 free(id_str);
1873 } else if (status != GOT_STATUS_ADD) {
1874 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1875 if (err)
1876 goto done;
1879 if (status != GOT_STATUS_DELETE) {
1880 if (asprintf(&abspath, "%s/%s",
1881 got_worktree_get_root_path(a->worktree), path) == -1) {
1882 err = got_error_from_errno("asprintf");
1883 goto done;
1886 f2 = fopen(abspath, "r");
1887 if (f2 == NULL) {
1888 err = got_error_from_errno2("fopen", abspath);
1889 goto done;
1891 if (lstat(abspath, &sb) == -1) {
1892 err = got_error_from_errno2("lstat", abspath);
1893 goto done;
1895 } else
1896 sb.st_size = 0;
1898 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1899 a->diff_context, stdout);
1900 done:
1901 if (blob1)
1902 got_object_blob_close(blob1);
1903 if (f2 && fclose(f2) != 0 && err == NULL)
1904 err = got_error_from_errno("fclose");
1905 free(abspath);
1906 return err;
1909 static const struct got_error *
1910 match_object_id(struct got_object_id **id, char **label,
1911 const char *id_str, int obj_type, int resolve_tags,
1912 struct got_repository *repo)
1914 const struct got_error *err;
1915 struct got_tag_object *tag;
1916 struct got_reference *ref = NULL;
1918 *id = NULL;
1919 *label = NULL;
1921 if (resolve_tags) {
1922 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
1923 repo);
1924 if (err == NULL) {
1925 *id = got_object_id_dup(
1926 got_object_tag_get_object_id(tag));
1927 if (*id == NULL)
1928 err = got_error_from_errno("got_object_id_dup");
1929 else if (asprintf(label, "refs/tags/%s",
1930 got_object_tag_get_name(tag)) == -1) {
1931 err = got_error_from_errno("asprintf");
1932 free(id);
1933 *id = NULL;
1935 got_object_tag_close(tag);
1936 return err;
1937 } else if (err->code != GOT_ERR_NO_OBJ)
1938 return err;
1941 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1942 if (err) {
1943 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1944 return err;
1945 err = got_ref_open(&ref, repo, id_str, 0);
1946 if (err != NULL)
1947 goto done;
1948 *label = strdup(got_ref_get_name(ref));
1949 if (*label == NULL) {
1950 err = got_error_from_errno("strdup");
1951 goto done;
1953 err = got_ref_resolve(id, repo, ref);
1954 } else {
1955 err = got_object_id_str(label, *id);
1956 if (*label == NULL) {
1957 err = got_error_from_errno("strdup");
1958 goto done;
1961 done:
1962 if (ref)
1963 got_ref_close(ref);
1964 return err;
1968 static const struct got_error *
1969 cmd_diff(int argc, char *argv[])
1971 const struct got_error *error;
1972 struct got_repository *repo = NULL;
1973 struct got_worktree *worktree = NULL;
1974 char *cwd = NULL, *repo_path = NULL;
1975 struct got_object_id *id1 = NULL, *id2 = NULL;
1976 const char *id_str1 = NULL, *id_str2 = NULL;
1977 char *label1 = NULL, *label2 = NULL;
1978 int type1, type2;
1979 int diff_context = 3, diff_staged = 0, ch;
1980 const char *errstr;
1981 char *path = NULL;
1983 #ifndef PROFILE
1984 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1985 NULL) == -1)
1986 err(1, "pledge");
1987 #endif
1989 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1990 switch (ch) {
1991 case 'C':
1992 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1993 if (errstr != NULL)
1994 err(1, "-C option %s", errstr);
1995 break;
1996 case 'r':
1997 repo_path = realpath(optarg, NULL);
1998 if (repo_path == NULL)
1999 err(1, "-r option");
2000 got_path_strip_trailing_slashes(repo_path);
2001 break;
2002 case 's':
2003 diff_staged = 1;
2004 break;
2005 default:
2006 usage_diff();
2007 /* NOTREACHED */
2011 argc -= optind;
2012 argv += optind;
2014 cwd = getcwd(NULL, 0);
2015 if (cwd == NULL) {
2016 error = got_error_from_errno("getcwd");
2017 goto done;
2019 error = got_worktree_open(&worktree, cwd);
2020 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2021 goto done;
2022 if (argc <= 1) {
2023 if (worktree == NULL) {
2024 error = got_error(GOT_ERR_NOT_WORKTREE);
2025 goto done;
2027 if (repo_path)
2028 errx(1,
2029 "-r option can't be used when diffing a work tree");
2030 repo_path = strdup(got_worktree_get_repo_path(worktree));
2031 if (repo_path == NULL) {
2032 error = got_error_from_errno("strdup");
2033 goto done;
2035 if (argc == 1) {
2036 error = got_worktree_resolve_path(&path, worktree,
2037 argv[0]);
2038 if (error)
2039 goto done;
2040 } else {
2041 path = strdup("");
2042 if (path == NULL) {
2043 error = got_error_from_errno("strdup");
2044 goto done;
2047 } else if (argc == 2) {
2048 if (diff_staged)
2049 errx(1, "-s option can't be used when diffing "
2050 "objects in repository");
2051 id_str1 = argv[0];
2052 id_str2 = argv[1];
2053 if (worktree && repo_path == NULL) {
2054 repo_path =
2055 strdup(got_worktree_get_repo_path(worktree));
2056 if (repo_path == NULL) {
2057 error = got_error_from_errno("strdup");
2058 goto done;
2061 } else
2062 usage_diff();
2064 if (repo_path == NULL) {
2065 repo_path = getcwd(NULL, 0);
2066 if (repo_path == NULL)
2067 return got_error_from_errno("getcwd");
2070 error = got_repo_open(&repo, repo_path);
2071 free(repo_path);
2072 if (error != NULL)
2073 goto done;
2075 error = apply_unveil(got_repo_get_path(repo), 1,
2076 worktree ? got_worktree_get_root_path(worktree) : NULL);
2077 if (error)
2078 goto done;
2080 if (argc <= 1) {
2081 struct print_diff_arg arg;
2082 struct got_pathlist_head paths;
2083 char *id_str;
2085 TAILQ_INIT(&paths);
2087 error = got_object_id_str(&id_str,
2088 got_worktree_get_base_commit_id(worktree));
2089 if (error)
2090 goto done;
2091 arg.repo = repo;
2092 arg.worktree = worktree;
2093 arg.diff_context = diff_context;
2094 arg.id_str = id_str;
2095 arg.header_shown = 0;
2096 arg.diff_staged = diff_staged;
2098 error = got_pathlist_append(&paths, path, NULL);
2099 if (error)
2100 goto done;
2102 error = got_worktree_status(worktree, &paths, repo, print_diff,
2103 &arg, check_cancelled, NULL);
2104 free(id_str);
2105 got_pathlist_free(&paths);
2106 goto done;
2109 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2110 repo);
2111 if (error)
2112 goto done;
2114 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2115 repo);
2116 if (error)
2117 goto done;
2119 error = got_object_get_type(&type1, repo, id1);
2120 if (error)
2121 goto done;
2123 error = got_object_get_type(&type2, repo, id2);
2124 if (error)
2125 goto done;
2127 if (type1 != type2) {
2128 error = got_error(GOT_ERR_OBJ_TYPE);
2129 goto done;
2132 switch (type1) {
2133 case GOT_OBJ_TYPE_BLOB:
2134 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2135 diff_context, repo, stdout);
2136 break;
2137 case GOT_OBJ_TYPE_TREE:
2138 error = got_diff_objects_as_trees(id1, id2, "", "",
2139 diff_context, repo, stdout);
2140 break;
2141 case GOT_OBJ_TYPE_COMMIT:
2142 printf("diff %s %s\n", label1, label2);
2143 error = got_diff_objects_as_commits(id1, id2, diff_context,
2144 repo, stdout);
2145 break;
2146 default:
2147 error = got_error(GOT_ERR_OBJ_TYPE);
2150 done:
2151 free(label1);
2152 free(label2);
2153 free(id1);
2154 free(id2);
2155 free(path);
2156 if (worktree)
2157 got_worktree_close(worktree);
2158 if (repo) {
2159 const struct got_error *repo_error;
2160 repo_error = got_repo_close(repo);
2161 if (error == NULL)
2162 error = repo_error;
2164 return error;
2167 __dead static void
2168 usage_blame(void)
2170 fprintf(stderr,
2171 "usage: %s blame [-c commit] [-r repository-path] path\n",
2172 getprogname());
2173 exit(1);
2176 struct blame_line {
2177 int annotated;
2178 char *id_str;
2179 char *committer;
2180 char datebuf[9]; /* YY-MM-DD + NUL */
2183 struct blame_cb_args {
2184 struct blame_line *lines;
2185 int nlines;
2186 int nlines_prec;
2187 int lineno_cur;
2188 off_t *line_offsets;
2189 FILE *f;
2190 struct got_repository *repo;
2193 static const struct got_error *
2194 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2196 const struct got_error *err = NULL;
2197 struct blame_cb_args *a = arg;
2198 struct blame_line *bline;
2199 char *line = NULL;
2200 size_t linesize = 0;
2201 struct got_commit_object *commit = NULL;
2202 off_t offset;
2203 struct tm tm;
2204 time_t committer_time;
2206 if (nlines != a->nlines ||
2207 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2208 return got_error(GOT_ERR_RANGE);
2210 if (sigint_received)
2211 return got_error(GOT_ERR_ITER_COMPLETED);
2213 if (lineno == -1)
2214 return NULL; /* no change in this commit */
2216 /* Annotate this line. */
2217 bline = &a->lines[lineno - 1];
2218 if (bline->annotated)
2219 return NULL;
2220 err = got_object_id_str(&bline->id_str, id);
2221 if (err)
2222 return err;
2224 err = got_object_open_as_commit(&commit, a->repo, id);
2225 if (err)
2226 goto done;
2228 bline->committer = strdup(got_object_commit_get_committer(commit));
2229 if (bline->committer == NULL) {
2230 err = got_error_from_errno("strdup");
2231 goto done;
2234 committer_time = got_object_commit_get_committer_time(commit);
2235 if (localtime_r(&committer_time, &tm) == NULL)
2236 return got_error_from_errno("localtime_r");
2237 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2238 &tm) >= sizeof(bline->datebuf)) {
2239 err = got_error(GOT_ERR_NO_SPACE);
2240 goto done;
2242 bline->annotated = 1;
2244 /* Print lines annotated so far. */
2245 bline = &a->lines[a->lineno_cur - 1];
2246 if (!bline->annotated)
2247 goto done;
2249 offset = a->line_offsets[a->lineno_cur - 1];
2250 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2251 err = got_error_from_errno("fseeko");
2252 goto done;
2255 while (bline->annotated) {
2256 char *smallerthan, *at, *nl, *committer;
2257 size_t len;
2259 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2260 if (ferror(a->f))
2261 err = got_error_from_errno("getline");
2262 break;
2265 committer = bline->committer;
2266 smallerthan = strchr(committer, '<');
2267 if (smallerthan && smallerthan[1] != '\0')
2268 committer = smallerthan + 1;
2269 at = strchr(committer, '@');
2270 if (at)
2271 *at = '\0';
2272 len = strlen(committer);
2273 if (len >= 9)
2274 committer[8] = '\0';
2276 nl = strchr(line, '\n');
2277 if (nl)
2278 *nl = '\0';
2279 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2280 bline->id_str, bline->datebuf, committer, line);
2282 a->lineno_cur++;
2283 bline = &a->lines[a->lineno_cur - 1];
2285 done:
2286 if (commit)
2287 got_object_commit_close(commit);
2288 free(line);
2289 return err;
2292 static const struct got_error *
2293 cmd_blame(int argc, char *argv[])
2295 const struct got_error *error;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2299 struct got_object_id *obj_id = NULL;
2300 struct got_object_id *commit_id = NULL;
2301 struct got_blob_object *blob = NULL;
2302 char *commit_id_str = NULL;
2303 struct blame_cb_args bca;
2304 int ch, obj_type, i;
2305 size_t filesize;
2307 memset(&bca, 0, sizeof(bca));
2309 #ifndef PROFILE
2310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2311 NULL) == -1)
2312 err(1, "pledge");
2313 #endif
2315 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2316 switch (ch) {
2317 case 'c':
2318 commit_id_str = optarg;
2319 break;
2320 case 'r':
2321 repo_path = realpath(optarg, NULL);
2322 if (repo_path == NULL)
2323 err(1, "-r option");
2324 got_path_strip_trailing_slashes(repo_path);
2325 break;
2326 default:
2327 usage_blame();
2328 /* NOTREACHED */
2332 argc -= optind;
2333 argv += optind;
2335 if (argc == 1)
2336 path = argv[0];
2337 else
2338 usage_blame();
2340 cwd = getcwd(NULL, 0);
2341 if (cwd == NULL) {
2342 error = got_error_from_errno("getcwd");
2343 goto done;
2345 if (repo_path == NULL) {
2346 error = got_worktree_open(&worktree, cwd);
2347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2348 goto done;
2349 else
2350 error = NULL;
2351 if (worktree) {
2352 repo_path =
2353 strdup(got_worktree_get_repo_path(worktree));
2354 if (repo_path == NULL)
2355 error = got_error_from_errno("strdup");
2356 if (error)
2357 goto done;
2358 } else {
2359 repo_path = strdup(cwd);
2360 if (repo_path == NULL) {
2361 error = got_error_from_errno("strdup");
2362 goto done;
2367 error = got_repo_open(&repo, repo_path);
2368 if (error != NULL)
2369 goto done;
2371 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2372 if (error)
2373 goto done;
2375 if (worktree) {
2376 const char *prefix = got_worktree_get_path_prefix(worktree);
2377 char *p, *worktree_subdir = cwd +
2378 strlen(got_worktree_get_root_path(worktree));
2379 if (asprintf(&p, "%s%s%s%s%s",
2380 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2381 worktree_subdir, worktree_subdir[0] ? "/" : "",
2382 path) == -1) {
2383 error = got_error_from_errno("asprintf");
2384 goto done;
2386 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2387 free(p);
2388 } else {
2389 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2391 if (error)
2392 goto done;
2394 if (commit_id_str == NULL) {
2395 struct got_reference *head_ref;
2396 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2397 if (error != NULL)
2398 goto done;
2399 error = got_ref_resolve(&commit_id, repo, head_ref);
2400 got_ref_close(head_ref);
2401 if (error != NULL)
2402 goto done;
2403 } else {
2404 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2405 if (error)
2406 goto done;
2409 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2410 if (error)
2411 goto done;
2412 if (obj_id == NULL) {
2413 error = got_error(GOT_ERR_NO_OBJ);
2414 goto done;
2417 error = got_object_get_type(&obj_type, repo, obj_id);
2418 if (error)
2419 goto done;
2421 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2422 error = got_error(GOT_ERR_OBJ_TYPE);
2423 goto done;
2426 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2427 if (error)
2428 goto done;
2429 bca.f = got_opentemp();
2430 if (bca.f == NULL) {
2431 error = got_error_from_errno("got_opentemp");
2432 goto done;
2434 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2435 &bca.line_offsets, bca.f, blob);
2436 if (error || bca.nlines == 0)
2437 goto done;
2439 /* Don't include \n at EOF in the blame line count. */
2440 if (bca.line_offsets[bca.nlines - 1] == filesize)
2441 bca.nlines--;
2443 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2444 if (bca.lines == NULL) {
2445 error = got_error_from_errno("calloc");
2446 goto done;
2448 bca.lineno_cur = 1;
2449 bca.nlines_prec = 0;
2450 i = bca.nlines;
2451 while (i > 0) {
2452 i /= 10;
2453 bca.nlines_prec++;
2455 bca.repo = repo;
2457 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2458 check_cancelled, NULL);
2459 if (error)
2460 goto done;
2461 done:
2462 free(in_repo_path);
2463 free(repo_path);
2464 free(cwd);
2465 free(commit_id);
2466 free(obj_id);
2467 if (blob)
2468 got_object_blob_close(blob);
2469 if (worktree)
2470 got_worktree_close(worktree);
2471 if (repo) {
2472 const struct got_error *repo_error;
2473 repo_error = got_repo_close(repo);
2474 if (error == NULL)
2475 error = repo_error;
2477 for (i = 0; i < bca.nlines; i++) {
2478 struct blame_line *bline = &bca.lines[i];
2479 free(bline->id_str);
2480 free(bline->committer);
2482 free(bca.lines);
2483 free(bca.line_offsets);
2484 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2485 error = got_error_from_errno("fclose");
2486 return error;
2489 __dead static void
2490 usage_tree(void)
2492 fprintf(stderr,
2493 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2494 getprogname());
2495 exit(1);
2498 static void
2499 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2500 const char *root_path)
2502 int is_root_path = (strcmp(path, root_path) == 0);
2503 const char *modestr = "";
2505 path += strlen(root_path);
2506 while (path[0] == '/')
2507 path++;
2509 if (S_ISLNK(te->mode))
2510 modestr = "@";
2511 else if (S_ISDIR(te->mode))
2512 modestr = "/";
2513 else if (te->mode & S_IXUSR)
2514 modestr = "*";
2516 printf("%s%s%s%s%s\n", id ? id : "", path,
2517 is_root_path ? "" : "/", te->name, modestr);
2520 static const struct got_error *
2521 print_tree(const char *path, struct got_object_id *commit_id,
2522 int show_ids, int recurse, const char *root_path,
2523 struct got_repository *repo)
2525 const struct got_error *err = NULL;
2526 struct got_object_id *tree_id = NULL;
2527 struct got_tree_object *tree = NULL;
2528 const struct got_tree_entries *entries;
2529 struct got_tree_entry *te;
2531 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2532 if (err)
2533 goto done;
2535 err = got_object_open_as_tree(&tree, repo, tree_id);
2536 if (err)
2537 goto done;
2538 entries = got_object_tree_get_entries(tree);
2539 te = SIMPLEQ_FIRST(&entries->head);
2540 while (te) {
2541 char *id = NULL;
2543 if (sigint_received || sigpipe_received)
2544 break;
2546 if (show_ids) {
2547 char *id_str;
2548 err = got_object_id_str(&id_str, te->id);
2549 if (err)
2550 goto done;
2551 if (asprintf(&id, "%s ", id_str) == -1) {
2552 err = got_error_from_errno("asprintf");
2553 free(id_str);
2554 goto done;
2556 free(id_str);
2558 print_entry(te, id, path, root_path);
2559 free(id);
2561 if (recurse && S_ISDIR(te->mode)) {
2562 char *child_path;
2563 if (asprintf(&child_path, "%s%s%s", path,
2564 path[0] == '/' && path[1] == '\0' ? "" : "/",
2565 te->name) == -1) {
2566 err = got_error_from_errno("asprintf");
2567 goto done;
2569 err = print_tree(child_path, commit_id, show_ids, 1,
2570 root_path, repo);
2571 free(child_path);
2572 if (err)
2573 goto done;
2576 te = SIMPLEQ_NEXT(te, entry);
2578 done:
2579 if (tree)
2580 got_object_tree_close(tree);
2581 free(tree_id);
2582 return err;
2585 static const struct got_error *
2586 cmd_tree(int argc, char *argv[])
2588 const struct got_error *error;
2589 struct got_repository *repo = NULL;
2590 struct got_worktree *worktree = NULL;
2591 const char *path;
2592 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2593 struct got_object_id *commit_id = NULL;
2594 char *commit_id_str = NULL;
2595 int show_ids = 0, recurse = 0;
2596 int ch;
2598 #ifndef PROFILE
2599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2600 NULL) == -1)
2601 err(1, "pledge");
2602 #endif
2604 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2605 switch (ch) {
2606 case 'c':
2607 commit_id_str = optarg;
2608 break;
2609 case 'r':
2610 repo_path = realpath(optarg, NULL);
2611 if (repo_path == NULL)
2612 err(1, "-r option");
2613 got_path_strip_trailing_slashes(repo_path);
2614 break;
2615 case 'i':
2616 show_ids = 1;
2617 break;
2618 case 'R':
2619 recurse = 1;
2620 break;
2621 default:
2622 usage_tree();
2623 /* NOTREACHED */
2627 argc -= optind;
2628 argv += optind;
2630 if (argc == 1)
2631 path = argv[0];
2632 else if (argc > 1)
2633 usage_tree();
2634 else
2635 path = NULL;
2637 cwd = getcwd(NULL, 0);
2638 if (cwd == NULL) {
2639 error = got_error_from_errno("getcwd");
2640 goto done;
2642 if (repo_path == NULL) {
2643 error = got_worktree_open(&worktree, cwd);
2644 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2645 goto done;
2646 else
2647 error = NULL;
2648 if (worktree) {
2649 repo_path =
2650 strdup(got_worktree_get_repo_path(worktree));
2651 if (repo_path == NULL)
2652 error = got_error_from_errno("strdup");
2653 if (error)
2654 goto done;
2655 } else {
2656 repo_path = strdup(cwd);
2657 if (repo_path == NULL) {
2658 error = got_error_from_errno("strdup");
2659 goto done;
2664 error = got_repo_open(&repo, repo_path);
2665 if (error != NULL)
2666 goto done;
2668 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2669 if (error)
2670 goto done;
2672 if (path == NULL) {
2673 if (worktree) {
2674 char *p, *worktree_subdir = cwd +
2675 strlen(got_worktree_get_root_path(worktree));
2676 if (asprintf(&p, "%s/%s",
2677 got_worktree_get_path_prefix(worktree),
2678 worktree_subdir) == -1) {
2679 error = got_error_from_errno("asprintf");
2680 goto done;
2682 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2683 free(p);
2684 if (error)
2685 goto done;
2686 } else
2687 path = "/";
2689 if (in_repo_path == NULL) {
2690 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2691 if (error != NULL)
2692 goto done;
2695 if (commit_id_str == NULL) {
2696 struct got_reference *head_ref;
2697 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2698 if (error != NULL)
2699 goto done;
2700 error = got_ref_resolve(&commit_id, repo, head_ref);
2701 got_ref_close(head_ref);
2702 if (error != NULL)
2703 goto done;
2704 } else {
2705 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2706 if (error)
2707 goto done;
2710 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2711 in_repo_path, repo);
2712 done:
2713 free(in_repo_path);
2714 free(repo_path);
2715 free(cwd);
2716 free(commit_id);
2717 if (worktree)
2718 got_worktree_close(worktree);
2719 if (repo) {
2720 const struct got_error *repo_error;
2721 repo_error = got_repo_close(repo);
2722 if (error == NULL)
2723 error = repo_error;
2725 return error;
2728 __dead static void
2729 usage_status(void)
2731 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2732 exit(1);
2735 static const struct got_error *
2736 print_status(void *arg, unsigned char status, unsigned char staged_status,
2737 const char *path, struct got_object_id *blob_id,
2738 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2740 if (status == staged_status && (status == GOT_STATUS_DELETE))
2741 status = GOT_STATUS_NO_CHANGE;
2742 printf("%c%c %s\n", status, staged_status, path);
2743 return NULL;
2746 static const struct got_error *
2747 cmd_status(int argc, char *argv[])
2749 const struct got_error *error = NULL;
2750 struct got_repository *repo = NULL;
2751 struct got_worktree *worktree = NULL;
2752 char *cwd = NULL;
2753 struct got_pathlist_head paths;
2754 struct got_pathlist_entry *pe;
2755 int ch;
2757 TAILQ_INIT(&paths);
2759 while ((ch = getopt(argc, argv, "")) != -1) {
2760 switch (ch) {
2761 default:
2762 usage_status();
2763 /* NOTREACHED */
2767 argc -= optind;
2768 argv += optind;
2770 #ifndef PROFILE
2771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2772 NULL) == -1)
2773 err(1, "pledge");
2774 #endif
2775 cwd = getcwd(NULL, 0);
2776 if (cwd == NULL) {
2777 error = got_error_from_errno("getcwd");
2778 goto done;
2781 error = got_worktree_open(&worktree, cwd);
2782 if (error != NULL)
2783 goto done;
2785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2786 if (error != NULL)
2787 goto done;
2789 error = apply_unveil(got_repo_get_path(repo), 1,
2790 got_worktree_get_root_path(worktree));
2791 if (error)
2792 goto done;
2794 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2795 if (error)
2796 goto done;
2798 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2799 check_cancelled, NULL);
2800 done:
2801 TAILQ_FOREACH(pe, &paths, entry)
2802 free((char *)pe->path);
2803 got_pathlist_free(&paths);
2804 free(cwd);
2805 return error;
2808 __dead static void
2809 usage_ref(void)
2811 fprintf(stderr,
2812 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2813 getprogname());
2814 exit(1);
2817 static const struct got_error *
2818 list_refs(struct got_repository *repo)
2820 static const struct got_error *err = NULL;
2821 struct got_reflist_head refs;
2822 struct got_reflist_entry *re;
2824 SIMPLEQ_INIT(&refs);
2825 err = got_ref_list(&refs, repo);
2826 if (err)
2827 return err;
2829 SIMPLEQ_FOREACH(re, &refs, entry) {
2830 char *refstr;
2831 refstr = got_ref_to_str(re->ref);
2832 if (refstr == NULL)
2833 return got_error_from_errno("got_ref_to_str");
2834 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2835 free(refstr);
2838 got_ref_list_free(&refs);
2839 return NULL;
2842 static const struct got_error *
2843 delete_ref(struct got_repository *repo, const char *refname)
2845 const struct got_error *err = NULL;
2846 struct got_reference *ref;
2848 err = got_ref_open(&ref, repo, refname, 0);
2849 if (err)
2850 return err;
2852 err = got_ref_delete(ref, repo);
2853 got_ref_close(ref);
2854 return err;
2857 static const struct got_error *
2858 add_ref(struct got_repository *repo, const char *refname, const char *target)
2860 const struct got_error *err = NULL;
2861 struct got_object_id *id;
2862 struct got_reference *ref = NULL;
2865 * Don't let the user create a reference named '-'.
2866 * While technically a valid reference name, this case is usually
2867 * an unintended typo.
2869 if (refname[0] == '-' && refname[1] == '\0')
2870 return got_error(GOT_ERR_BAD_REF_NAME);
2872 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2873 repo);
2874 if (err) {
2875 struct got_reference *target_ref;
2877 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2878 return err;
2879 err = got_ref_open(&target_ref, repo, target, 0);
2880 if (err)
2881 return err;
2882 err = got_ref_resolve(&id, repo, target_ref);
2883 got_ref_close(target_ref);
2884 if (err)
2885 return err;
2888 err = got_ref_alloc(&ref, refname, id);
2889 if (err)
2890 goto done;
2892 err = got_ref_write(ref, repo);
2893 done:
2894 if (ref)
2895 got_ref_close(ref);
2896 free(id);
2897 return err;
2900 static const struct got_error *
2901 add_symref(struct got_repository *repo, const char *refname, const char *target)
2903 const struct got_error *err = NULL;
2904 struct got_reference *ref = NULL;
2905 struct got_reference *target_ref = NULL;
2908 * Don't let the user create a reference named '-'.
2909 * While technically a valid reference name, this case is usually
2910 * an unintended typo.
2912 if (refname[0] == '-' && refname[1] == '\0')
2913 return got_error(GOT_ERR_BAD_REF_NAME);
2915 err = got_ref_open(&target_ref, repo, target, 0);
2916 if (err)
2917 return err;
2919 err = got_ref_alloc_symref(&ref, refname, target_ref);
2920 if (err)
2921 goto done;
2923 err = got_ref_write(ref, repo);
2924 done:
2925 if (target_ref)
2926 got_ref_close(target_ref);
2927 if (ref)
2928 got_ref_close(ref);
2929 return err;
2932 static const struct got_error *
2933 cmd_ref(int argc, char *argv[])
2935 const struct got_error *error = NULL;
2936 struct got_repository *repo = NULL;
2937 struct got_worktree *worktree = NULL;
2938 char *cwd = NULL, *repo_path = NULL;
2939 int ch, do_list = 0, create_symref = 0;
2940 const char *delref = NULL;
2942 /* TODO: Add -s option for adding symbolic references. */
2943 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2944 switch (ch) {
2945 case 'd':
2946 delref = optarg;
2947 break;
2948 case 'r':
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 err(1, "-r option");
2952 got_path_strip_trailing_slashes(repo_path);
2953 break;
2954 case 'l':
2955 do_list = 1;
2956 break;
2957 case 's':
2958 create_symref = 1;
2959 break;
2960 default:
2961 usage_ref();
2962 /* NOTREACHED */
2966 if (do_list && delref)
2967 errx(1, "-l and -d options are mutually exclusive\n");
2969 argc -= optind;
2970 argv += optind;
2972 if (do_list || delref) {
2973 if (create_symref)
2974 errx(1, "-s option cannot be used together with the "
2975 "-l or -d options");
2976 if (argc > 0)
2977 usage_ref();
2978 } else if (argc != 2)
2979 usage_ref();
2981 #ifndef PROFILE
2982 if (do_list) {
2983 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2984 NULL) == -1)
2985 err(1, "pledge");
2986 } else {
2987 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2988 "sendfd unveil", NULL) == -1)
2989 err(1, "pledge");
2991 #endif
2992 cwd = getcwd(NULL, 0);
2993 if (cwd == NULL) {
2994 error = got_error_from_errno("getcwd");
2995 goto done;
2998 if (repo_path == NULL) {
2999 error = got_worktree_open(&worktree, cwd);
3000 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3001 goto done;
3002 else
3003 error = NULL;
3004 if (worktree) {
3005 repo_path =
3006 strdup(got_worktree_get_repo_path(worktree));
3007 if (repo_path == NULL)
3008 error = got_error_from_errno("strdup");
3009 if (error)
3010 goto done;
3011 } else {
3012 repo_path = strdup(cwd);
3013 if (repo_path == NULL) {
3014 error = got_error_from_errno("strdup");
3015 goto done;
3020 error = got_repo_open(&repo, repo_path);
3021 if (error != NULL)
3022 goto done;
3024 error = apply_unveil(got_repo_get_path(repo), do_list,
3025 worktree ? got_worktree_get_root_path(worktree) : NULL);
3026 if (error)
3027 goto done;
3029 if (do_list)
3030 error = list_refs(repo);
3031 else if (delref)
3032 error = delete_ref(repo, delref);
3033 else if (create_symref)
3034 error = add_symref(repo, argv[0], argv[1]);
3035 else
3036 error = add_ref(repo, argv[0], argv[1]);
3037 done:
3038 if (repo)
3039 got_repo_close(repo);
3040 if (worktree)
3041 got_worktree_close(worktree);
3042 free(cwd);
3043 free(repo_path);
3044 return error;
3047 __dead static void
3048 usage_branch(void)
3050 fprintf(stderr,
3051 "usage: %s branch [-r repository] -l | -d name | "
3052 "name [base-branch]\n", getprogname());
3053 exit(1);
3056 static const struct got_error *
3057 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3059 static const struct got_error *err = NULL;
3060 struct got_reflist_head refs;
3061 struct got_reflist_entry *re;
3063 SIMPLEQ_INIT(&refs);
3065 err = got_ref_list(&refs, repo);
3066 if (err)
3067 return err;
3069 SIMPLEQ_FOREACH(re, &refs, entry) {
3070 const char *refname, *marker = " ";
3071 char *refstr;
3072 refname = got_ref_get_name(re->ref);
3073 if (strncmp(refname, "refs/heads/", 11) != 0)
3074 continue;
3075 if (worktree && strcmp(refname,
3076 got_worktree_get_head_ref_name(worktree)) == 0) {
3077 struct got_object_id *id = NULL;
3078 err = got_ref_resolve(&id, repo, re->ref);
3079 if (err)
3080 return err;
3081 if (got_object_id_cmp(id,
3082 got_worktree_get_base_commit_id(worktree)) == 0)
3083 marker = "* ";
3084 else
3085 marker = "~ ";
3086 free(id);
3088 refname += 11;
3089 refstr = got_ref_to_str(re->ref);
3090 if (refstr == NULL)
3091 return got_error_from_errno("got_ref_to_str");
3092 printf("%s%s: %s\n", marker, refname, refstr);
3093 free(refstr);
3096 got_ref_list_free(&refs);
3097 return NULL;
3100 static const struct got_error *
3101 delete_branch(struct got_repository *repo, const char *branch_name)
3103 const struct got_error *err = NULL;
3104 struct got_reference *ref;
3105 char *refname;
3107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3108 return got_error_from_errno("asprintf");
3110 err = got_ref_open(&ref, repo, refname, 0);
3111 if (err)
3112 goto done;
3114 err = got_ref_delete(ref, repo);
3115 got_ref_close(ref);
3116 done:
3117 free(refname);
3118 return err;
3121 static const struct got_error *
3122 add_branch(struct got_repository *repo, const char *branch_name,
3123 const char *base_branch)
3125 const struct got_error *err = NULL;
3126 struct got_object_id *id = NULL;
3127 struct got_reference *ref = NULL;
3128 char *base_refname = NULL, *refname = NULL;
3129 struct got_reference *base_ref;
3132 * Don't let the user create a branch named '-'.
3133 * While technically a valid reference name, this case is usually
3134 * an unintended typo.
3136 if (branch_name[0] == '-' && branch_name[1] == '\0')
3137 return got_error(GOT_ERR_BAD_REF_NAME);
3139 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3140 base_refname = strdup(GOT_REF_HEAD);
3141 if (base_refname == NULL)
3142 return got_error_from_errno("strdup");
3143 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3144 return got_error_from_errno("asprintf");
3146 err = got_ref_open(&base_ref, repo, base_refname, 0);
3147 if (err)
3148 goto done;
3149 err = got_ref_resolve(&id, repo, base_ref);
3150 got_ref_close(base_ref);
3151 if (err)
3152 goto done;
3154 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3155 err = got_error_from_errno("asprintf");
3156 goto done;
3159 err = got_ref_open(&ref, repo, refname, 0);
3160 if (err == NULL) {
3161 err = got_error(GOT_ERR_BRANCH_EXISTS);
3162 goto done;
3163 } else if (err->code != GOT_ERR_NOT_REF)
3164 goto done;
3166 err = got_ref_alloc(&ref, refname, id);
3167 if (err)
3168 goto done;
3170 err = got_ref_write(ref, repo);
3171 done:
3172 if (ref)
3173 got_ref_close(ref);
3174 free(id);
3175 free(base_refname);
3176 free(refname);
3177 return err;
3180 static const struct got_error *
3181 cmd_branch(int argc, char *argv[])
3183 const struct got_error *error = NULL;
3184 struct got_repository *repo = NULL;
3185 struct got_worktree *worktree = NULL;
3186 char *cwd = NULL, *repo_path = NULL;
3187 int ch, do_list = 0;
3188 const char *delref = NULL;
3190 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3191 switch (ch) {
3192 case 'd':
3193 delref = optarg;
3194 break;
3195 case 'r':
3196 repo_path = realpath(optarg, NULL);
3197 if (repo_path == NULL)
3198 err(1, "-r option");
3199 got_path_strip_trailing_slashes(repo_path);
3200 break;
3201 case 'l':
3202 do_list = 1;
3203 break;
3204 default:
3205 usage_branch();
3206 /* NOTREACHED */
3210 if (do_list && delref)
3211 errx(1, "-l and -d options are mutually exclusive\n");
3213 argc -= optind;
3214 argv += optind;
3216 if (do_list || delref) {
3217 if (argc > 0)
3218 usage_branch();
3219 } else if (argc < 1 || argc > 2)
3220 usage_branch();
3222 #ifndef PROFILE
3223 if (do_list) {
3224 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3225 NULL) == -1)
3226 err(1, "pledge");
3227 } else {
3228 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3229 "sendfd unveil", NULL) == -1)
3230 err(1, "pledge");
3232 #endif
3233 cwd = getcwd(NULL, 0);
3234 if (cwd == NULL) {
3235 error = got_error_from_errno("getcwd");
3236 goto done;
3239 if (repo_path == NULL) {
3240 error = got_worktree_open(&worktree, cwd);
3241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3242 goto done;
3243 else
3244 error = NULL;
3245 if (worktree) {
3246 repo_path =
3247 strdup(got_worktree_get_repo_path(worktree));
3248 if (repo_path == NULL)
3249 error = got_error_from_errno("strdup");
3250 if (error)
3251 goto done;
3252 } else {
3253 repo_path = strdup(cwd);
3254 if (repo_path == NULL) {
3255 error = got_error_from_errno("strdup");
3256 goto done;
3261 error = got_repo_open(&repo, repo_path);
3262 if (error != NULL)
3263 goto done;
3265 error = apply_unveil(got_repo_get_path(repo), do_list,
3266 worktree ? got_worktree_get_root_path(worktree) : NULL);
3267 if (error)
3268 goto done;
3270 if (do_list)
3271 error = list_branches(repo, worktree);
3272 else if (delref)
3273 error = delete_branch(repo, delref);
3274 else {
3275 const char *base_branch;
3276 if (argc == 1) {
3277 base_branch = worktree ?
3278 got_worktree_get_head_ref_name(worktree) :
3279 GOT_REF_HEAD;
3280 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3281 base_branch += 11;
3282 } else
3283 base_branch = argv[1];
3284 error = add_branch(repo, argv[0], base_branch);
3286 done:
3287 if (repo)
3288 got_repo_close(repo);
3289 if (worktree)
3290 got_worktree_close(worktree);
3291 free(cwd);
3292 free(repo_path);
3293 return error;
3296 __dead static void
3297 usage_add(void)
3299 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3300 exit(1);
3303 static const struct got_error *
3304 cmd_add(int argc, char *argv[])
3306 const struct got_error *error = NULL;
3307 struct got_repository *repo = NULL;
3308 struct got_worktree *worktree = NULL;
3309 char *cwd = NULL;
3310 struct got_pathlist_head paths;
3311 struct got_pathlist_entry *pe;
3312 int ch;
3314 TAILQ_INIT(&paths);
3316 while ((ch = getopt(argc, argv, "")) != -1) {
3317 switch (ch) {
3318 default:
3319 usage_add();
3320 /* NOTREACHED */
3324 argc -= optind;
3325 argv += optind;
3327 #ifndef PROFILE
3328 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3329 NULL) == -1)
3330 err(1, "pledge");
3331 #endif
3332 if (argc < 1)
3333 usage_add();
3335 cwd = getcwd(NULL, 0);
3336 if (cwd == NULL) {
3337 error = got_error_from_errno("getcwd");
3338 goto done;
3341 error = got_worktree_open(&worktree, cwd);
3342 if (error)
3343 goto done;
3345 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3346 if (error != NULL)
3347 goto done;
3349 error = apply_unveil(got_repo_get_path(repo), 1,
3350 got_worktree_get_root_path(worktree));
3351 if (error)
3352 goto done;
3354 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3355 if (error)
3356 goto done;
3358 error = got_worktree_schedule_add(worktree, &paths, print_status,
3359 NULL, repo);
3360 done:
3361 if (repo)
3362 got_repo_close(repo);
3363 if (worktree)
3364 got_worktree_close(worktree);
3365 TAILQ_FOREACH(pe, &paths, entry)
3366 free((char *)pe->path);
3367 got_pathlist_free(&paths);
3368 free(cwd);
3369 return error;
3372 __dead static void
3373 usage_remove(void)
3375 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3376 exit(1);
3379 static const struct got_error *
3380 cmd_remove(int argc, char *argv[])
3382 const struct got_error *error = NULL;
3383 struct got_worktree *worktree = NULL;
3384 struct got_repository *repo = NULL;
3385 char *cwd = NULL;
3386 struct got_pathlist_head paths;
3387 struct got_pathlist_entry *pe;
3388 int ch, delete_local_mods = 0;
3390 TAILQ_INIT(&paths);
3392 while ((ch = getopt(argc, argv, "f")) != -1) {
3393 switch (ch) {
3394 case 'f':
3395 delete_local_mods = 1;
3396 break;
3397 default:
3398 usage_add();
3399 /* NOTREACHED */
3403 argc -= optind;
3404 argv += optind;
3406 #ifndef PROFILE
3407 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3408 NULL) == -1)
3409 err(1, "pledge");
3410 #endif
3411 if (argc < 1)
3412 usage_remove();
3414 cwd = getcwd(NULL, 0);
3415 if (cwd == NULL) {
3416 error = got_error_from_errno("getcwd");
3417 goto done;
3419 error = got_worktree_open(&worktree, cwd);
3420 if (error)
3421 goto done;
3423 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3424 if (error)
3425 goto done;
3427 error = apply_unveil(got_repo_get_path(repo), 1,
3428 got_worktree_get_root_path(worktree));
3429 if (error)
3430 goto done;
3432 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3433 if (error)
3434 goto done;
3436 error = got_worktree_schedule_delete(worktree, &paths,
3437 delete_local_mods, print_status, NULL, repo);
3438 if (error)
3439 goto done;
3440 done:
3441 if (repo)
3442 got_repo_close(repo);
3443 if (worktree)
3444 got_worktree_close(worktree);
3445 TAILQ_FOREACH(pe, &paths, entry)
3446 free((char *)pe->path);
3447 got_pathlist_free(&paths);
3448 free(cwd);
3449 return error;
3452 __dead static void
3453 usage_revert(void)
3455 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3456 "path ...\n", getprogname());
3457 exit(1);
3460 static const struct got_error *
3461 revert_progress(void *arg, unsigned char status, const char *path)
3463 while (path[0] == '/')
3464 path++;
3465 printf("%c %s\n", status, path);
3466 return NULL;
3469 struct choose_patch_arg {
3470 FILE *patch_script_file;
3471 const char *action;
3474 static const struct got_error *
3475 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3476 int nchanges, const char *action)
3478 char *line = NULL;
3479 size_t linesize = 0;
3480 ssize_t linelen;
3482 switch (status) {
3483 case GOT_STATUS_ADD:
3484 printf("A %s\n%s this addition? [y/n] ", path, action);
3485 break;
3486 case GOT_STATUS_DELETE:
3487 printf("D %s\n%s this deletion? [y/n] ", path, action);
3488 break;
3489 case GOT_STATUS_MODIFY:
3490 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3491 return got_error_from_errno("fseek");
3492 printf(GOT_COMMIT_SEP_STR);
3493 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3494 printf("%s", line);
3495 if (ferror(patch_file))
3496 return got_error_from_errno("getline");
3497 printf(GOT_COMMIT_SEP_STR);
3498 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3499 path, n, nchanges, action);
3500 break;
3501 default:
3502 return got_error_path(path, GOT_ERR_FILE_STATUS);
3505 return NULL;
3508 static const struct got_error *
3509 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3510 FILE *patch_file, int n, int nchanges)
3512 const struct got_error *err = NULL;
3513 char *line = NULL;
3514 size_t linesize = 0;
3515 ssize_t linelen;
3516 int resp = ' ';
3517 struct choose_patch_arg *a = arg;
3519 *choice = GOT_PATCH_CHOICE_NONE;
3521 if (a->patch_script_file) {
3522 char *nl;
3523 err = show_change(status, path, patch_file, n, nchanges,
3524 a->action);
3525 if (err)
3526 return err;
3527 linelen = getline(&line, &linesize, a->patch_script_file);
3528 if (linelen == -1) {
3529 if (ferror(a->patch_script_file))
3530 return got_error_from_errno("getline");
3531 return NULL;
3533 nl = strchr(line, '\n');
3534 if (nl)
3535 *nl = '\0';
3536 if (strcmp(line, "y") == 0) {
3537 *choice = GOT_PATCH_CHOICE_YES;
3538 printf("y\n");
3539 } else if (strcmp(line, "n") == 0) {
3540 *choice = GOT_PATCH_CHOICE_NO;
3541 printf("n\n");
3542 } else if (strcmp(line, "q") == 0 &&
3543 status == GOT_STATUS_MODIFY) {
3544 *choice = GOT_PATCH_CHOICE_QUIT;
3545 printf("q\n");
3546 } else
3547 printf("invalid response '%s'\n", line);
3548 free(line);
3549 return NULL;
3552 while (resp != 'y' && resp != 'n' && resp != 'q') {
3553 err = show_change(status, path, patch_file, n, nchanges,
3554 a->action);
3555 if (err)
3556 return err;
3557 resp = getchar();
3558 if (resp == '\n')
3559 resp = getchar();
3560 if (status == GOT_STATUS_MODIFY) {
3561 if (resp != 'y' && resp != 'n' && resp != 'q') {
3562 printf("invalid response '%c'\n", resp);
3563 resp = ' ';
3565 } else if (resp != 'y' && resp != 'n') {
3566 printf("invalid response '%c'\n", resp);
3567 resp = ' ';
3571 if (resp == 'y')
3572 *choice = GOT_PATCH_CHOICE_YES;
3573 else if (resp == 'n')
3574 *choice = GOT_PATCH_CHOICE_NO;
3575 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3576 *choice = GOT_PATCH_CHOICE_QUIT;
3578 return NULL;
3582 static const struct got_error *
3583 cmd_revert(int argc, char *argv[])
3585 const struct got_error *error = NULL;
3586 struct got_worktree *worktree = NULL;
3587 struct got_repository *repo = NULL;
3588 char *cwd = NULL, *path = NULL;
3589 struct got_pathlist_head paths;
3590 struct got_pathlist_entry *pe;
3591 int ch, can_recurse = 0, pflag = 0;
3592 FILE *patch_script_file = NULL;
3593 const char *patch_script_path = NULL;
3594 struct choose_patch_arg cpa;
3596 TAILQ_INIT(&paths);
3598 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3599 switch (ch) {
3600 case 'p':
3601 pflag = 1;
3602 break;
3603 case 'F':
3604 patch_script_path = optarg;
3605 break;
3606 case 'R':
3607 can_recurse = 1;
3608 break;
3609 default:
3610 usage_revert();
3611 /* NOTREACHED */
3615 argc -= optind;
3616 argv += optind;
3618 #ifndef PROFILE
3619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3620 "unveil", NULL) == -1)
3621 err(1, "pledge");
3622 #endif
3623 if (argc < 1)
3624 usage_revert();
3625 if (patch_script_path && !pflag)
3626 errx(1, "-F option can only be used together with -p option");
3628 cwd = getcwd(NULL, 0);
3629 if (cwd == NULL) {
3630 error = got_error_from_errno("getcwd");
3631 goto done;
3633 error = got_worktree_open(&worktree, cwd);
3634 if (error)
3635 goto done;
3637 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3638 if (error != NULL)
3639 goto done;
3641 if (patch_script_path) {
3642 patch_script_file = fopen(patch_script_path, "r");
3643 if (patch_script_file == NULL) {
3644 error = got_error_from_errno2("fopen",
3645 patch_script_path);
3646 goto done;
3649 error = apply_unveil(got_repo_get_path(repo), 1,
3650 got_worktree_get_root_path(worktree));
3651 if (error)
3652 goto done;
3654 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3655 if (error)
3656 goto done;
3658 if (!can_recurse) {
3659 char *ondisk_path;
3660 struct stat sb;
3661 TAILQ_FOREACH(pe, &paths, entry) {
3662 if (asprintf(&ondisk_path, "%s/%s",
3663 got_worktree_get_root_path(worktree),
3664 pe->path) == -1) {
3665 error = got_error_from_errno("asprintf");
3666 goto done;
3668 if (lstat(ondisk_path, &sb) == -1) {
3669 if (errno == ENOENT) {
3670 free(ondisk_path);
3671 continue;
3673 error = got_error_from_errno2("lstat",
3674 ondisk_path);
3675 free(ondisk_path);
3676 goto done;
3678 free(ondisk_path);
3679 if (S_ISDIR(sb.st_mode)) {
3680 error = got_error_msg(GOT_ERR_BAD_PATH,
3681 "reverting directories requires -R option");
3682 goto done;
3687 cpa.patch_script_file = patch_script_file;
3688 cpa.action = "revert";
3689 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3690 pflag ? choose_patch : NULL, &cpa, repo);
3691 if (error)
3692 goto done;
3693 done:
3694 if (patch_script_file && fclose(patch_script_file) == EOF &&
3695 error == NULL)
3696 error = got_error_from_errno2("fclose", patch_script_path);
3697 if (repo)
3698 got_repo_close(repo);
3699 if (worktree)
3700 got_worktree_close(worktree);
3701 free(path);
3702 free(cwd);
3703 return error;
3706 __dead static void
3707 usage_commit(void)
3709 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3710 getprogname());
3711 exit(1);
3714 struct collect_commit_logmsg_arg {
3715 const char *cmdline_log;
3716 const char *editor;
3717 const char *worktree_path;
3718 const char *branch_name;
3719 const char *repo_path;
3720 char *logmsg_path;
3724 static const struct got_error *
3725 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3726 void *arg)
3728 char *initial_content = NULL;
3729 struct got_pathlist_entry *pe;
3730 const struct got_error *err = NULL;
3731 char *template = NULL;
3732 struct collect_commit_logmsg_arg *a = arg;
3733 int fd;
3734 size_t len;
3736 /* if a message was specified on the command line, just use it */
3737 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3738 len = strlen(a->cmdline_log) + 1;
3739 *logmsg = malloc(len + 1);
3740 if (*logmsg == NULL)
3741 return got_error_from_errno("malloc");
3742 strlcpy(*logmsg, a->cmdline_log, len);
3743 return NULL;
3746 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3747 return got_error_from_errno("asprintf");
3749 if (asprintf(&initial_content,
3750 "\n# changes to be committed on branch %s:\n",
3751 a->branch_name) == -1)
3752 return got_error_from_errno("asprintf");
3754 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3755 if (err)
3756 goto done;
3758 dprintf(fd, initial_content);
3760 TAILQ_FOREACH(pe, commitable_paths, entry) {
3761 struct got_commitable *ct = pe->data;
3762 dprintf(fd, "# %c %s\n",
3763 got_commitable_get_status(ct),
3764 got_commitable_get_path(ct));
3766 close(fd);
3768 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3769 done:
3770 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3771 unlink(a->logmsg_path);
3772 free(a->logmsg_path);
3773 a->logmsg_path = NULL;
3775 free(initial_content);
3776 free(template);
3778 /* Editor is done; we can now apply unveil(2) */
3779 if (err == NULL) {
3780 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3781 if (err) {
3782 free(*logmsg);
3783 *logmsg = NULL;
3786 return err;
3789 static const struct got_error *
3790 cmd_commit(int argc, char *argv[])
3792 const struct got_error *error = NULL;
3793 struct got_worktree *worktree = NULL;
3794 struct got_repository *repo = NULL;
3795 char *cwd = NULL, *id_str = NULL;
3796 struct got_object_id *id = NULL;
3797 const char *logmsg = NULL;
3798 const char *author;
3799 struct collect_commit_logmsg_arg cl_arg;
3800 char *editor = NULL;
3801 int ch, rebase_in_progress, histedit_in_progress;
3802 struct got_pathlist_head paths;
3804 TAILQ_INIT(&paths);
3805 cl_arg.logmsg_path = NULL;
3807 while ((ch = getopt(argc, argv, "m:")) != -1) {
3808 switch (ch) {
3809 case 'm':
3810 logmsg = optarg;
3811 break;
3812 default:
3813 usage_commit();
3814 /* NOTREACHED */
3818 argc -= optind;
3819 argv += optind;
3821 #ifndef PROFILE
3822 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3823 "unveil", NULL) == -1)
3824 err(1, "pledge");
3825 #endif
3826 error = get_author(&author);
3827 if (error)
3828 return error;
3830 cwd = getcwd(NULL, 0);
3831 if (cwd == NULL) {
3832 error = got_error_from_errno("getcwd");
3833 goto done;
3835 error = got_worktree_open(&worktree, cwd);
3836 if (error)
3837 goto done;
3839 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3840 if (error)
3841 goto done;
3842 if (rebase_in_progress) {
3843 error = got_error(GOT_ERR_REBASING);
3844 goto done;
3847 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3848 worktree);
3849 if (error)
3850 goto done;
3852 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3853 if (error != NULL)
3854 goto done;
3857 * unveil(2) traverses exec(2); if an editor is used we have
3858 * to apply unveil after the log message has been written.
3860 if (logmsg == NULL || strlen(logmsg) == 0)
3861 error = get_editor(&editor);
3862 else
3863 error = apply_unveil(got_repo_get_path(repo), 0,
3864 got_worktree_get_root_path(worktree));
3865 if (error)
3866 goto done;
3868 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3869 if (error)
3870 goto done;
3872 cl_arg.editor = editor;
3873 cl_arg.cmdline_log = logmsg;
3874 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3875 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3876 if (!histedit_in_progress) {
3877 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3878 error = got_error(GOT_ERR_COMMIT_BRANCH);
3879 goto done;
3881 cl_arg.branch_name += 11;
3883 cl_arg.repo_path = got_repo_get_path(repo);
3884 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3885 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3886 if (error) {
3887 if (cl_arg.logmsg_path)
3888 fprintf(stderr, "%s: log message preserved in %s\n",
3889 getprogname(), cl_arg.logmsg_path);
3890 goto done;
3893 if (cl_arg.logmsg_path)
3894 unlink(cl_arg.logmsg_path);
3896 error = got_object_id_str(&id_str, id);
3897 if (error)
3898 goto done;
3899 printf("Created commit %s\n", id_str);
3900 done:
3901 free(cl_arg.logmsg_path);
3902 if (repo)
3903 got_repo_close(repo);
3904 if (worktree)
3905 got_worktree_close(worktree);
3906 free(cwd);
3907 free(id_str);
3908 free(editor);
3909 return error;
3912 __dead static void
3913 usage_cherrypick(void)
3915 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3916 exit(1);
3919 static const struct got_error *
3920 cmd_cherrypick(int argc, char *argv[])
3922 const struct got_error *error = NULL;
3923 struct got_worktree *worktree = NULL;
3924 struct got_repository *repo = NULL;
3925 char *cwd = NULL, *commit_id_str = NULL;
3926 struct got_object_id *commit_id = NULL;
3927 struct got_commit_object *commit = NULL;
3928 struct got_object_qid *pid;
3929 struct got_reference *head_ref = NULL;
3930 int ch, did_something = 0;
3932 while ((ch = getopt(argc, argv, "")) != -1) {
3933 switch (ch) {
3934 default:
3935 usage_cherrypick();
3936 /* NOTREACHED */
3940 argc -= optind;
3941 argv += optind;
3943 #ifndef PROFILE
3944 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3945 "unveil", NULL) == -1)
3946 err(1, "pledge");
3947 #endif
3948 if (argc != 1)
3949 usage_cherrypick();
3951 cwd = getcwd(NULL, 0);
3952 if (cwd == NULL) {
3953 error = got_error_from_errno("getcwd");
3954 goto done;
3956 error = got_worktree_open(&worktree, cwd);
3957 if (error)
3958 goto done;
3960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3961 if (error != NULL)
3962 goto done;
3964 error = apply_unveil(got_repo_get_path(repo), 0,
3965 got_worktree_get_root_path(worktree));
3966 if (error)
3967 goto done;
3969 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3970 GOT_OBJ_TYPE_COMMIT, repo);
3971 if (error != NULL) {
3972 struct got_reference *ref;
3973 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3974 goto done;
3975 error = got_ref_open(&ref, repo, argv[0], 0);
3976 if (error != NULL)
3977 goto done;
3978 error = got_ref_resolve(&commit_id, repo, ref);
3979 got_ref_close(ref);
3980 if (error != NULL)
3981 goto done;
3983 error = got_object_id_str(&commit_id_str, commit_id);
3984 if (error)
3985 goto done;
3987 error = got_ref_open(&head_ref, repo,
3988 got_worktree_get_head_ref_name(worktree), 0);
3989 if (error != NULL)
3990 goto done;
3992 error = check_same_branch(commit_id, head_ref, NULL, repo);
3993 if (error) {
3994 if (error->code != GOT_ERR_ANCESTRY)
3995 goto done;
3996 error = NULL;
3997 } else {
3998 error = got_error(GOT_ERR_SAME_BRANCH);
3999 goto done;
4002 error = got_object_open_as_commit(&commit, repo, commit_id);
4003 if (error)
4004 goto done;
4005 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4006 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4007 commit_id, repo, update_progress, &did_something, check_cancelled,
4008 NULL);
4009 if (error != NULL)
4010 goto done;
4012 if (did_something)
4013 printf("Merged commit %s\n", commit_id_str);
4014 done:
4015 if (commit)
4016 got_object_commit_close(commit);
4017 free(commit_id_str);
4018 if (head_ref)
4019 got_ref_close(head_ref);
4020 if (worktree)
4021 got_worktree_close(worktree);
4022 if (repo)
4023 got_repo_close(repo);
4024 return error;
4027 __dead static void
4028 usage_backout(void)
4030 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4031 exit(1);
4034 static const struct got_error *
4035 cmd_backout(int argc, char *argv[])
4037 const struct got_error *error = NULL;
4038 struct got_worktree *worktree = NULL;
4039 struct got_repository *repo = NULL;
4040 char *cwd = NULL, *commit_id_str = NULL;
4041 struct got_object_id *commit_id = NULL;
4042 struct got_commit_object *commit = NULL;
4043 struct got_object_qid *pid;
4044 struct got_reference *head_ref = NULL;
4045 int ch, did_something = 0;
4047 while ((ch = getopt(argc, argv, "")) != -1) {
4048 switch (ch) {
4049 default:
4050 usage_backout();
4051 /* NOTREACHED */
4055 argc -= optind;
4056 argv += optind;
4058 #ifndef PROFILE
4059 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4060 "unveil", NULL) == -1)
4061 err(1, "pledge");
4062 #endif
4063 if (argc != 1)
4064 usage_backout();
4066 cwd = getcwd(NULL, 0);
4067 if (cwd == NULL) {
4068 error = got_error_from_errno("getcwd");
4069 goto done;
4071 error = got_worktree_open(&worktree, cwd);
4072 if (error)
4073 goto done;
4075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4076 if (error != NULL)
4077 goto done;
4079 error = apply_unveil(got_repo_get_path(repo), 0,
4080 got_worktree_get_root_path(worktree));
4081 if (error)
4082 goto done;
4084 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4085 GOT_OBJ_TYPE_COMMIT, repo);
4086 if (error != NULL) {
4087 struct got_reference *ref;
4088 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4089 goto done;
4090 error = got_ref_open(&ref, repo, argv[0], 0);
4091 if (error != NULL)
4092 goto done;
4093 error = got_ref_resolve(&commit_id, repo, ref);
4094 got_ref_close(ref);
4095 if (error != NULL)
4096 goto done;
4098 error = got_object_id_str(&commit_id_str, commit_id);
4099 if (error)
4100 goto done;
4102 error = got_ref_open(&head_ref, repo,
4103 got_worktree_get_head_ref_name(worktree), 0);
4104 if (error != NULL)
4105 goto done;
4107 error = check_same_branch(commit_id, head_ref, NULL, repo);
4108 if (error)
4109 goto done;
4111 error = got_object_open_as_commit(&commit, repo, commit_id);
4112 if (error)
4113 goto done;
4114 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4115 if (pid == NULL) {
4116 error = got_error(GOT_ERR_ROOT_COMMIT);
4117 goto done;
4120 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4121 update_progress, &did_something, check_cancelled, NULL);
4122 if (error != NULL)
4123 goto done;
4125 if (did_something)
4126 printf("Backed out commit %s\n", commit_id_str);
4127 done:
4128 if (commit)
4129 got_object_commit_close(commit);
4130 free(commit_id_str);
4131 if (head_ref)
4132 got_ref_close(head_ref);
4133 if (worktree)
4134 got_worktree_close(worktree);
4135 if (repo)
4136 got_repo_close(repo);
4137 return error;
4140 __dead static void
4141 usage_rebase(void)
4143 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4144 getprogname());
4145 exit(1);
4148 void
4149 trim_logmsg(char *logmsg, int limit)
4151 char *nl;
4152 size_t len;
4154 len = strlen(logmsg);
4155 if (len > limit)
4156 len = limit;
4157 logmsg[len] = '\0';
4158 nl = strchr(logmsg, '\n');
4159 if (nl)
4160 *nl = '\0';
4163 static const struct got_error *
4164 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4166 const struct got_error *err;
4167 char *logmsg0 = NULL;
4168 const char *s;
4170 err = got_object_commit_get_logmsg(&logmsg0, commit);
4171 if (err)
4172 return err;
4174 s = logmsg0;
4175 while (isspace((unsigned char)s[0]))
4176 s++;
4178 *logmsg = strdup(s);
4179 if (*logmsg == NULL) {
4180 err = got_error_from_errno("strdup");
4181 goto done;
4184 trim_logmsg(*logmsg, limit);
4185 done:
4186 free(logmsg0);
4187 return err;
4190 static const struct got_error *
4191 show_rebase_progress(struct got_commit_object *commit,
4192 struct got_object_id *old_id, struct got_object_id *new_id)
4194 const struct got_error *err;
4195 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4197 err = got_object_id_str(&old_id_str, old_id);
4198 if (err)
4199 goto done;
4201 if (new_id) {
4202 err = got_object_id_str(&new_id_str, new_id);
4203 if (err)
4204 goto done;
4207 old_id_str[12] = '\0';
4208 if (new_id_str)
4209 new_id_str[12] = '\0';
4211 err = get_short_logmsg(&logmsg, 42, commit);
4212 if (err)
4213 goto done;
4215 printf("%s -> %s: %s\n", old_id_str,
4216 new_id_str ? new_id_str : "no-op change", logmsg);
4217 done:
4218 free(old_id_str);
4219 free(new_id_str);
4220 return err;
4223 static const struct got_error *
4224 rebase_progress(void *arg, unsigned char status, const char *path)
4226 unsigned char *rebase_status = arg;
4228 while (path[0] == '/')
4229 path++;
4230 printf("%c %s\n", status, path);
4232 if (*rebase_status == GOT_STATUS_CONFLICT)
4233 return NULL;
4234 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4235 *rebase_status = status;
4236 return NULL;
4239 static const struct got_error *
4240 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4241 struct got_reference *branch, struct got_reference *new_base_branch,
4242 struct got_reference *tmp_branch, struct got_repository *repo)
4244 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4245 return got_worktree_rebase_complete(worktree, fileindex,
4246 new_base_branch, tmp_branch, branch, repo);
4249 static const struct got_error *
4250 rebase_commit(struct got_pathlist_head *merged_paths,
4251 struct got_worktree *worktree, struct got_fileindex *fileindex,
4252 struct got_reference *tmp_branch,
4253 struct got_object_id *commit_id, struct got_repository *repo)
4255 const struct got_error *error;
4256 struct got_commit_object *commit;
4257 struct got_object_id *new_commit_id;
4259 error = got_object_open_as_commit(&commit, repo, commit_id);
4260 if (error)
4261 return error;
4263 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4264 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4265 if (error) {
4266 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4267 goto done;
4268 error = show_rebase_progress(commit, commit_id, NULL);
4269 } else {
4270 error = show_rebase_progress(commit, commit_id, new_commit_id);
4271 free(new_commit_id);
4273 done:
4274 got_object_commit_close(commit);
4275 return error;
4278 struct check_path_prefix_arg {
4279 const char *path_prefix;
4280 size_t len;
4281 int errcode;
4284 static const struct got_error *
4285 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4286 struct got_blob_object *blob2, struct got_object_id *id1,
4287 struct got_object_id *id2, const char *path1, const char *path2,
4288 struct got_repository *repo)
4290 struct check_path_prefix_arg *a = arg;
4292 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4293 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4294 return got_error(a->errcode);
4296 return NULL;
4299 static const struct got_error *
4300 check_path_prefix(struct got_object_id *parent_id,
4301 struct got_object_id *commit_id, const char *path_prefix,
4302 int errcode, struct got_repository *repo)
4304 const struct got_error *err;
4305 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4306 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4307 struct check_path_prefix_arg cpp_arg;
4309 if (got_path_is_root_dir(path_prefix))
4310 return NULL;
4312 err = got_object_open_as_commit(&commit, repo, commit_id);
4313 if (err)
4314 goto done;
4316 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4317 if (err)
4318 goto done;
4320 err = got_object_open_as_tree(&tree1, repo,
4321 got_object_commit_get_tree_id(parent_commit));
4322 if (err)
4323 goto done;
4325 err = got_object_open_as_tree(&tree2, repo,
4326 got_object_commit_get_tree_id(commit));
4327 if (err)
4328 goto done;
4330 cpp_arg.path_prefix = path_prefix;
4331 while (cpp_arg.path_prefix[0] == '/')
4332 cpp_arg.path_prefix++;
4333 cpp_arg.len = strlen(cpp_arg.path_prefix);
4334 cpp_arg.errcode = errcode;
4335 err = got_diff_tree(tree1, tree2, "", "", repo,
4336 check_path_prefix_in_diff, &cpp_arg, 0);
4337 done:
4338 if (tree1)
4339 got_object_tree_close(tree1);
4340 if (tree2)
4341 got_object_tree_close(tree2);
4342 if (commit)
4343 got_object_commit_close(commit);
4344 if (parent_commit)
4345 got_object_commit_close(parent_commit);
4346 return err;
4349 static const struct got_error *
4350 collect_commits(struct got_object_id_queue *commits,
4351 struct got_object_id *initial_commit_id,
4352 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4353 const char *path_prefix, int path_prefix_errcode,
4354 struct got_repository *repo)
4356 const struct got_error *err = NULL;
4357 struct got_commit_graph *graph = NULL;
4358 struct got_object_id *parent_id = NULL;
4359 struct got_object_qid *qid;
4360 struct got_object_id *commit_id = initial_commit_id;
4362 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4363 if (err)
4364 return err;
4366 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4367 check_cancelled, NULL);
4368 if (err)
4369 goto done;
4370 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4371 err = got_commit_graph_iter_next(&parent_id, graph);
4372 if (err) {
4373 if (err->code == GOT_ERR_ITER_COMPLETED) {
4374 err = got_error_msg(GOT_ERR_ANCESTRY,
4375 "ran out of commits to rebase before "
4376 "youngest common ancestor commit has "
4377 "been reached?!?");
4378 goto done;
4379 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4380 goto done;
4381 err = got_commit_graph_fetch_commits(graph, 1, repo,
4382 check_cancelled, NULL);
4383 if (err)
4384 goto done;
4385 } else {
4386 err = check_path_prefix(parent_id, commit_id,
4387 path_prefix, path_prefix_errcode, repo);
4388 if (err)
4389 goto done;
4391 err = got_object_qid_alloc(&qid, commit_id);
4392 if (err)
4393 goto done;
4394 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4395 commit_id = parent_id;
4398 done:
4399 got_commit_graph_close(graph);
4400 return err;
4403 static const struct got_error *
4404 cmd_rebase(int argc, char *argv[])
4406 const struct got_error *error = NULL;
4407 struct got_worktree *worktree = NULL;
4408 struct got_repository *repo = NULL;
4409 struct got_fileindex *fileindex = NULL;
4410 char *cwd = NULL;
4411 struct got_reference *branch = NULL;
4412 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4413 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4414 struct got_object_id *resume_commit_id = NULL;
4415 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4416 struct got_commit_object *commit = NULL;
4417 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4418 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4419 struct got_object_id_queue commits;
4420 struct got_pathlist_head merged_paths;
4421 const struct got_object_id_queue *parent_ids;
4422 struct got_object_qid *qid, *pid;
4424 SIMPLEQ_INIT(&commits);
4425 TAILQ_INIT(&merged_paths);
4427 while ((ch = getopt(argc, argv, "ac")) != -1) {
4428 switch (ch) {
4429 case 'a':
4430 abort_rebase = 1;
4431 break;
4432 case 'c':
4433 continue_rebase = 1;
4434 break;
4435 default:
4436 usage_rebase();
4437 /* NOTREACHED */
4441 argc -= optind;
4442 argv += optind;
4444 #ifndef PROFILE
4445 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4446 "unveil", NULL) == -1)
4447 err(1, "pledge");
4448 #endif
4449 if (abort_rebase && continue_rebase)
4450 usage_rebase();
4451 else if (abort_rebase || continue_rebase) {
4452 if (argc != 0)
4453 usage_rebase();
4454 } else if (argc != 1)
4455 usage_rebase();
4457 cwd = getcwd(NULL, 0);
4458 if (cwd == NULL) {
4459 error = got_error_from_errno("getcwd");
4460 goto done;
4462 error = got_worktree_open(&worktree, cwd);
4463 if (error)
4464 goto done;
4466 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4467 if (error != NULL)
4468 goto done;
4470 error = apply_unveil(got_repo_get_path(repo), 0,
4471 got_worktree_get_root_path(worktree));
4472 if (error)
4473 goto done;
4475 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4476 if (error)
4477 goto done;
4479 if (abort_rebase) {
4480 int did_something;
4481 if (!rebase_in_progress) {
4482 error = got_error(GOT_ERR_NOT_REBASING);
4483 goto done;
4485 error = got_worktree_rebase_continue(&resume_commit_id,
4486 &new_base_branch, &tmp_branch, &branch, &fileindex,
4487 worktree, repo);
4488 if (error)
4489 goto done;
4490 printf("Switching work tree to %s\n",
4491 got_ref_get_symref_target(new_base_branch));
4492 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4493 new_base_branch, update_progress, &did_something);
4494 if (error)
4495 goto done;
4496 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4497 goto done; /* nothing else to do */
4500 if (continue_rebase) {
4501 if (!rebase_in_progress) {
4502 error = got_error(GOT_ERR_NOT_REBASING);
4503 goto done;
4505 error = got_worktree_rebase_continue(&resume_commit_id,
4506 &new_base_branch, &tmp_branch, &branch, &fileindex,
4507 worktree, repo);
4508 if (error)
4509 goto done;
4511 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4512 resume_commit_id, repo);
4513 if (error)
4514 goto done;
4516 yca_id = got_object_id_dup(resume_commit_id);
4517 if (yca_id == NULL) {
4518 error = got_error_from_errno("got_object_id_dup");
4519 goto done;
4521 } else {
4522 error = got_ref_open(&branch, repo, argv[0], 0);
4523 if (error != NULL)
4524 goto done;
4527 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4528 if (error)
4529 goto done;
4531 if (!continue_rebase) {
4532 struct got_object_id *base_commit_id;
4534 base_commit_id = got_worktree_get_base_commit_id(worktree);
4535 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4536 base_commit_id, branch_head_commit_id, repo,
4537 check_cancelled, NULL);
4538 if (error)
4539 goto done;
4540 if (yca_id == NULL) {
4541 error = got_error_msg(GOT_ERR_ANCESTRY,
4542 "specified branch shares no common ancestry "
4543 "with work tree's branch");
4544 goto done;
4547 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4548 if (error) {
4549 if (error->code != GOT_ERR_ANCESTRY)
4550 goto done;
4551 error = NULL;
4552 } else {
4553 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4554 "specified branch resolves to a commit which "
4555 "is already contained in work tree's branch");
4556 goto done;
4558 error = got_worktree_rebase_prepare(&new_base_branch,
4559 &tmp_branch, &fileindex, worktree, branch, repo);
4560 if (error)
4561 goto done;
4564 commit_id = branch_head_commit_id;
4565 error = got_object_open_as_commit(&commit, repo, commit_id);
4566 if (error)
4567 goto done;
4569 parent_ids = got_object_commit_get_parent_ids(commit);
4570 pid = SIMPLEQ_FIRST(parent_ids);
4571 if (pid == NULL) {
4572 if (!continue_rebase) {
4573 int did_something;
4574 error = got_worktree_rebase_abort(worktree, fileindex,
4575 repo, new_base_branch, update_progress,
4576 &did_something);
4577 if (error)
4578 goto done;
4579 printf("Rebase of %s aborted\n",
4580 got_ref_get_name(branch));
4582 error = got_error(GOT_ERR_EMPTY_REBASE);
4583 goto done;
4585 error = collect_commits(&commits, commit_id, pid->id,
4586 yca_id, got_worktree_get_path_prefix(worktree),
4587 GOT_ERR_REBASE_PATH, repo);
4588 got_object_commit_close(commit);
4589 commit = NULL;
4590 if (error)
4591 goto done;
4593 if (SIMPLEQ_EMPTY(&commits)) {
4594 if (continue_rebase)
4595 error = rebase_complete(worktree, fileindex,
4596 branch, new_base_branch, tmp_branch, repo);
4597 else
4598 error = got_error(GOT_ERR_EMPTY_REBASE);
4599 goto done;
4602 pid = NULL;
4603 SIMPLEQ_FOREACH(qid, &commits, entry) {
4604 commit_id = qid->id;
4605 parent_id = pid ? pid->id : yca_id;
4606 pid = qid;
4608 error = got_worktree_rebase_merge_files(&merged_paths,
4609 worktree, fileindex, parent_id, commit_id, repo,
4610 rebase_progress, &rebase_status, check_cancelled, NULL);
4611 if (error)
4612 goto done;
4614 if (rebase_status == GOT_STATUS_CONFLICT) {
4615 got_worktree_rebase_pathlist_free(&merged_paths);
4616 break;
4619 error = rebase_commit(&merged_paths, worktree, fileindex,
4620 tmp_branch, commit_id, repo);
4621 got_worktree_rebase_pathlist_free(&merged_paths);
4622 if (error)
4623 goto done;
4626 if (rebase_status == GOT_STATUS_CONFLICT) {
4627 error = got_worktree_rebase_postpone(worktree, fileindex);
4628 if (error)
4629 goto done;
4630 error = got_error_msg(GOT_ERR_CONFLICTS,
4631 "conflicts must be resolved before rebasing can continue");
4632 } else
4633 error = rebase_complete(worktree, fileindex, branch,
4634 new_base_branch, tmp_branch, repo);
4635 done:
4636 got_object_id_queue_free(&commits);
4637 free(branch_head_commit_id);
4638 free(resume_commit_id);
4639 free(yca_id);
4640 if (commit)
4641 got_object_commit_close(commit);
4642 if (branch)
4643 got_ref_close(branch);
4644 if (new_base_branch)
4645 got_ref_close(new_base_branch);
4646 if (tmp_branch)
4647 got_ref_close(tmp_branch);
4648 if (worktree)
4649 got_worktree_close(worktree);
4650 if (repo)
4651 got_repo_close(repo);
4652 return error;
4655 __dead static void
4656 usage_histedit(void)
4658 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4659 getprogname());
4660 exit(1);
4663 #define GOT_HISTEDIT_PICK 'p'
4664 #define GOT_HISTEDIT_EDIT 'e'
4665 #define GOT_HISTEDIT_FOLD 'f'
4666 #define GOT_HISTEDIT_DROP 'd'
4667 #define GOT_HISTEDIT_MESG 'm'
4669 static struct got_histedit_cmd {
4670 unsigned char code;
4671 const char *name;
4672 const char *desc;
4673 } got_histedit_cmds[] = {
4674 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4675 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4676 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4677 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4678 { GOT_HISTEDIT_MESG, "mesg",
4679 "single-line log message for commit above (open editor if empty)" },
4682 struct got_histedit_list_entry {
4683 TAILQ_ENTRY(got_histedit_list_entry) entry;
4684 struct got_object_id *commit_id;
4685 const struct got_histedit_cmd *cmd;
4686 char *logmsg;
4688 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4690 static const struct got_error *
4691 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4692 FILE *f, struct got_repository *repo)
4694 const struct got_error *err = NULL;
4695 char *logmsg = NULL, *id_str = NULL;
4696 struct got_commit_object *commit = NULL;
4697 int n;
4699 err = got_object_open_as_commit(&commit, repo, commit_id);
4700 if (err)
4701 goto done;
4703 err = get_short_logmsg(&logmsg, 34, commit);
4704 if (err)
4705 goto done;
4707 err = got_object_id_str(&id_str, commit_id);
4708 if (err)
4709 goto done;
4711 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4712 if (n < 0)
4713 err = got_ferror(f, GOT_ERR_IO);
4714 done:
4715 if (commit)
4716 got_object_commit_close(commit);
4717 free(id_str);
4718 free(logmsg);
4719 return err;
4722 static const struct got_error *
4723 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4724 struct got_repository *repo)
4726 const struct got_error *err = NULL;
4727 struct got_object_qid *qid;
4729 if (SIMPLEQ_EMPTY(commits))
4730 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4732 SIMPLEQ_FOREACH(qid, commits, entry) {
4733 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4734 f, repo);
4735 if (err)
4736 break;
4739 return err;
4742 static const struct got_error *
4743 write_cmd_list(FILE *f)
4745 const struct got_error *err = NULL;
4746 int n, i;
4748 n = fprintf(f, "# Available histedit commands:\n");
4749 if (n < 0)
4750 return got_ferror(f, GOT_ERR_IO);
4752 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4753 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4754 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4755 cmd->desc);
4756 if (n < 0) {
4757 err = got_ferror(f, GOT_ERR_IO);
4758 break;
4761 n = fprintf(f, "# Commits will be processed in order from top to "
4762 "bottom of this file.\n");
4763 if (n < 0)
4764 return got_ferror(f, GOT_ERR_IO);
4765 return err;
4768 static const struct got_error *
4769 histedit_syntax_error(int lineno)
4771 static char msg[42];
4772 int ret;
4774 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4775 lineno);
4776 if (ret == -1 || ret >= sizeof(msg))
4777 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4779 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4782 static const struct got_error *
4783 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4784 char *logmsg, struct got_repository *repo)
4786 const struct got_error *err;
4787 struct got_commit_object *folded_commit = NULL;
4788 char *id_str, *folded_logmsg = NULL;
4790 err = got_object_id_str(&id_str, hle->commit_id);
4791 if (err)
4792 return err;
4794 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4795 if (err)
4796 goto done;
4798 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4799 if (err)
4800 goto done;
4801 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4802 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4803 folded_logmsg) == -1) {
4804 err = got_error_from_errno("asprintf");
4805 goto done;
4807 done:
4808 if (folded_commit)
4809 got_object_commit_close(folded_commit);
4810 free(id_str);
4811 free(folded_logmsg);
4812 return err;
4815 static struct got_histedit_list_entry *
4816 get_folded_commits(struct got_histedit_list_entry *hle)
4818 struct got_histedit_list_entry *prev, *folded = NULL;
4820 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4821 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4822 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4823 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4824 folded = prev;
4825 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4828 return folded;
4831 static const struct got_error *
4832 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4833 struct got_repository *repo)
4835 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4836 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4837 const struct got_error *err = NULL;
4838 struct got_commit_object *commit = NULL;
4839 int fd;
4840 struct got_histedit_list_entry *folded = NULL;
4842 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4843 if (err)
4844 return err;
4846 folded = get_folded_commits(hle);
4847 if (folded) {
4848 while (folded != hle) {
4849 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4850 folded = TAILQ_NEXT(folded, entry);
4851 continue;
4853 err = append_folded_commit_msg(&new_msg, folded,
4854 logmsg, repo);
4855 if (err)
4856 goto done;
4857 free(logmsg);
4858 logmsg = new_msg;
4859 folded = TAILQ_NEXT(folded, entry);
4863 err = got_object_id_str(&id_str, hle->commit_id);
4864 if (err)
4865 goto done;
4866 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4867 if (err)
4868 goto done;
4869 if (asprintf(&new_msg,
4870 "%s\n# original log message of commit %s: %s",
4871 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4872 err = got_error_from_errno("asprintf");
4873 goto done;
4875 free(logmsg);
4876 logmsg = new_msg;
4878 err = got_object_id_str(&id_str, hle->commit_id);
4879 if (err)
4880 goto done;
4882 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4883 if (err)
4884 goto done;
4886 dprintf(fd, logmsg);
4887 close(fd);
4889 err = get_editor(&editor);
4890 if (err)
4891 goto done;
4893 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4894 if (err) {
4895 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4896 goto done;
4897 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4899 done:
4900 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4901 err = got_error_from_errno2("unlink", logmsg_path);
4902 free(logmsg_path);
4903 free(logmsg);
4904 free(orig_logmsg);
4905 free(editor);
4906 if (commit)
4907 got_object_commit_close(commit);
4908 return err;
4911 static const struct got_error *
4912 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4913 FILE *f, struct got_repository *repo)
4915 const struct got_error *err = NULL;
4916 char *line = NULL, *p, *end;
4917 size_t size;
4918 ssize_t len;
4919 int lineno = 0, i;
4920 const struct got_histedit_cmd *cmd;
4921 struct got_object_id *commit_id = NULL;
4922 struct got_histedit_list_entry *hle = NULL;
4924 for (;;) {
4925 len = getline(&line, &size, f);
4926 if (len == -1) {
4927 const struct got_error *getline_err;
4928 if (feof(f))
4929 break;
4930 getline_err = got_error_from_errno("getline");
4931 err = got_ferror(f, getline_err->code);
4932 break;
4934 lineno++;
4935 p = line;
4936 while (isspace((unsigned char)p[0]))
4937 p++;
4938 if (p[0] == '#' || p[0] == '\0') {
4939 free(line);
4940 line = NULL;
4941 continue;
4943 cmd = NULL;
4944 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4945 cmd = &got_histedit_cmds[i];
4946 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4947 isspace((unsigned char)p[strlen(cmd->name)])) {
4948 p += strlen(cmd->name);
4949 break;
4951 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4952 p++;
4953 break;
4956 if (i == nitems(got_histedit_cmds)) {
4957 err = histedit_syntax_error(lineno);
4958 break;
4960 while (isspace((unsigned char)p[0]))
4961 p++;
4962 if (cmd->code == GOT_HISTEDIT_MESG) {
4963 if (hle == NULL || hle->logmsg != NULL) {
4964 err = got_error(GOT_ERR_HISTEDIT_CMD);
4965 break;
4967 if (p[0] == '\0') {
4968 err = histedit_edit_logmsg(hle, repo);
4969 if (err)
4970 break;
4971 } else {
4972 hle->logmsg = strdup(p);
4973 if (hle->logmsg == NULL) {
4974 err = got_error_from_errno("strdup");
4975 break;
4978 free(line);
4979 line = NULL;
4980 continue;
4981 } else {
4982 end = p;
4983 while (end[0] && !isspace((unsigned char)end[0]))
4984 end++;
4985 *end = '\0';
4987 err = got_object_resolve_id_str(&commit_id, repo, p);
4988 if (err) {
4989 /* override error code */
4990 err = histedit_syntax_error(lineno);
4991 break;
4994 hle = malloc(sizeof(*hle));
4995 if (hle == NULL) {
4996 err = got_error_from_errno("malloc");
4997 break;
4999 hle->cmd = cmd;
5000 hle->commit_id = commit_id;
5001 hle->logmsg = NULL;
5002 commit_id = NULL;
5003 free(line);
5004 line = NULL;
5005 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5008 free(line);
5009 free(commit_id);
5010 return err;
5013 static const struct got_error *
5014 histedit_check_script(struct got_histedit_list *histedit_cmds,
5015 struct got_object_id_queue *commits, struct got_repository *repo)
5017 const struct got_error *err = NULL;
5018 struct got_object_qid *qid;
5019 struct got_histedit_list_entry *hle;
5020 static char msg[80];
5021 char *id_str;
5023 if (TAILQ_EMPTY(histedit_cmds))
5024 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5025 "histedit script contains no commands");
5026 if (SIMPLEQ_EMPTY(commits))
5027 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5029 SIMPLEQ_FOREACH(qid, commits, entry) {
5030 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5031 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5032 break;
5034 if (hle == NULL) {
5035 err = got_object_id_str(&id_str, qid->id);
5036 if (err)
5037 return err;
5038 snprintf(msg, sizeof(msg),
5039 "commit %s missing from histedit script", id_str);
5040 free(id_str);
5041 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5045 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5046 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5047 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5048 "last commit in histedit script cannot be folded");
5050 return NULL;
5053 static const struct got_error *
5054 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5055 const char *path, struct got_object_id_queue *commits,
5056 struct got_repository *repo)
5058 const struct got_error *err = NULL;
5059 char *editor;
5060 FILE *f = NULL;
5062 err = get_editor(&editor);
5063 if (err)
5064 return err;
5066 if (spawn_editor(editor, path) == -1) {
5067 err = got_error_from_errno("failed spawning editor");
5068 goto done;
5071 f = fopen(path, "r");
5072 if (f == NULL) {
5073 err = got_error_from_errno("fopen");
5074 goto done;
5076 err = histedit_parse_list(histedit_cmds, f, repo);
5077 if (err)
5078 goto done;
5080 err = histedit_check_script(histedit_cmds, commits, repo);
5081 done:
5082 if (f && fclose(f) != 0 && err == NULL)
5083 err = got_error_from_errno("fclose");
5084 free(editor);
5085 return err;
5088 static const struct got_error *
5089 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5090 struct got_object_id_queue *, const char *, struct got_repository *);
5092 static const struct got_error *
5093 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5094 struct got_object_id_queue *commits, struct got_repository *repo)
5096 const struct got_error *err;
5097 FILE *f = NULL;
5098 char *path = NULL;
5100 err = got_opentemp_named(&path, &f, "got-histedit");
5101 if (err)
5102 return err;
5104 err = write_cmd_list(f);
5105 if (err)
5106 goto done;
5108 err = histedit_write_commit_list(commits, f, repo);
5109 if (err)
5110 goto done;
5112 if (fclose(f) != 0) {
5113 err = got_error_from_errno("fclose");
5114 goto done;
5116 f = NULL;
5118 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5119 if (err) {
5120 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5121 err->code != GOT_ERR_HISTEDIT_CMD)
5122 goto done;
5123 err = histedit_edit_list_retry(histedit_cmds, err,
5124 commits, path, repo);
5126 done:
5127 if (f && fclose(f) != 0 && err == NULL)
5128 err = got_error_from_errno("fclose");
5129 if (path && unlink(path) != 0 && err == NULL)
5130 err = got_error_from_errno2("unlink", path);
5131 free(path);
5132 return err;
5135 static const struct got_error *
5136 histedit_save_list(struct got_histedit_list *histedit_cmds,
5137 struct got_worktree *worktree, struct got_repository *repo)
5139 const struct got_error *err = NULL;
5140 char *path = NULL;
5141 FILE *f = NULL;
5142 struct got_histedit_list_entry *hle;
5143 struct got_commit_object *commit = NULL;
5145 err = got_worktree_get_histedit_script_path(&path, worktree);
5146 if (err)
5147 return err;
5149 f = fopen(path, "w");
5150 if (f == NULL) {
5151 err = got_error_from_errno2("fopen", path);
5152 goto done;
5154 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5155 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5156 repo);
5157 if (err)
5158 break;
5160 if (hle->logmsg) {
5161 int n = fprintf(f, "%c %s\n",
5162 GOT_HISTEDIT_MESG, hle->logmsg);
5163 if (n < 0) {
5164 err = got_ferror(f, GOT_ERR_IO);
5165 break;
5169 done:
5170 if (f && fclose(f) != 0 && err == NULL)
5171 err = got_error_from_errno("fclose");
5172 free(path);
5173 if (commit)
5174 got_object_commit_close(commit);
5175 return err;
5178 void
5179 histedit_free_list(struct got_histedit_list *histedit_cmds)
5181 struct got_histedit_list_entry *hle;
5183 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5184 TAILQ_REMOVE(histedit_cmds, hle, entry);
5185 free(hle);
5189 static const struct got_error *
5190 histedit_load_list(struct got_histedit_list *histedit_cmds,
5191 const char *path, struct got_repository *repo)
5193 const struct got_error *err = NULL;
5194 FILE *f = NULL;
5196 f = fopen(path, "r");
5197 if (f == NULL) {
5198 err = got_error_from_errno2("fopen", path);
5199 goto done;
5202 err = histedit_parse_list(histedit_cmds, f, repo);
5203 done:
5204 if (f && fclose(f) != 0 && err == NULL)
5205 err = got_error_from_errno("fclose");
5206 return err;
5209 static const struct got_error *
5210 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5211 const struct got_error *edit_err, struct got_object_id_queue *commits,
5212 const char *path, struct got_repository *repo)
5214 const struct got_error *err = NULL, *prev_err = edit_err;
5215 int resp = ' ';
5217 while (resp != 'c' && resp != 'r' && resp != 'a') {
5218 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5219 "or (a)bort: ", getprogname(), prev_err->msg);
5220 resp = getchar();
5221 if (resp == '\n')
5222 resp = getchar();
5223 if (resp == 'c') {
5224 histedit_free_list(histedit_cmds);
5225 err = histedit_run_editor(histedit_cmds, path, commits,
5226 repo);
5227 if (err) {
5228 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5229 err->code != GOT_ERR_HISTEDIT_CMD)
5230 break;
5231 prev_err = err;
5232 resp = ' ';
5233 continue;
5235 break;
5236 } else if (resp == 'r') {
5237 histedit_free_list(histedit_cmds);
5238 err = histedit_edit_script(histedit_cmds,
5239 commits, repo);
5240 if (err) {
5241 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5242 err->code != GOT_ERR_HISTEDIT_CMD)
5243 break;
5244 prev_err = err;
5245 resp = ' ';
5246 continue;
5248 break;
5249 } else if (resp == 'a') {
5250 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5251 break;
5252 } else
5253 printf("invalid response '%c'\n", resp);
5256 return err;
5259 static const struct got_error *
5260 histedit_complete(struct got_worktree *worktree,
5261 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5262 struct got_reference *branch, struct got_repository *repo)
5264 printf("Switching work tree to %s\n",
5265 got_ref_get_symref_target(branch));
5266 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5267 branch, repo);
5270 static const struct got_error *
5271 show_histedit_progress(struct got_commit_object *commit,
5272 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5274 const struct got_error *err;
5275 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5277 err = got_object_id_str(&old_id_str, hle->commit_id);
5278 if (err)
5279 goto done;
5281 if (new_id) {
5282 err = got_object_id_str(&new_id_str, new_id);
5283 if (err)
5284 goto done;
5287 old_id_str[12] = '\0';
5288 if (new_id_str)
5289 new_id_str[12] = '\0';
5291 if (hle->logmsg) {
5292 logmsg = strdup(hle->logmsg);
5293 if (logmsg == NULL) {
5294 err = got_error_from_errno("strdup");
5295 goto done;
5297 trim_logmsg(logmsg, 42);
5298 } else {
5299 err = get_short_logmsg(&logmsg, 42, commit);
5300 if (err)
5301 goto done;
5304 switch (hle->cmd->code) {
5305 case GOT_HISTEDIT_PICK:
5306 case GOT_HISTEDIT_EDIT:
5307 printf("%s -> %s: %s\n", old_id_str,
5308 new_id_str ? new_id_str : "no-op change", logmsg);
5309 break;
5310 case GOT_HISTEDIT_DROP:
5311 case GOT_HISTEDIT_FOLD:
5312 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5313 logmsg);
5314 break;
5315 default:
5316 break;
5319 done:
5320 free(old_id_str);
5321 free(new_id_str);
5322 return err;
5325 static const struct got_error *
5326 histedit_commit(struct got_pathlist_head *merged_paths,
5327 struct got_worktree *worktree, struct got_fileindex *fileindex,
5328 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5329 struct got_repository *repo)
5331 const struct got_error *err;
5332 struct got_commit_object *commit;
5333 struct got_object_id *new_commit_id;
5335 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5336 && hle->logmsg == NULL) {
5337 err = histedit_edit_logmsg(hle, repo);
5338 if (err)
5339 return err;
5342 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5343 if (err)
5344 return err;
5346 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5347 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5348 hle->logmsg, repo);
5349 if (err) {
5350 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5351 goto done;
5352 err = show_histedit_progress(commit, hle, NULL);
5353 } else {
5354 err = show_histedit_progress(commit, hle, new_commit_id);
5355 free(new_commit_id);
5357 done:
5358 got_object_commit_close(commit);
5359 return err;
5362 static const struct got_error *
5363 histedit_skip_commit(struct got_histedit_list_entry *hle,
5364 struct got_worktree *worktree, struct got_repository *repo)
5366 const struct got_error *error;
5367 struct got_commit_object *commit;
5369 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5370 repo);
5371 if (error)
5372 return error;
5374 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5375 if (error)
5376 return error;
5378 error = show_histedit_progress(commit, hle, NULL);
5379 got_object_commit_close(commit);
5380 return error;
5383 static const struct got_error *
5384 cmd_histedit(int argc, char *argv[])
5386 const struct got_error *error = NULL;
5387 struct got_worktree *worktree = NULL;
5388 struct got_fileindex *fileindex = NULL;
5389 struct got_repository *repo = NULL;
5390 char *cwd = NULL;
5391 struct got_reference *branch = NULL;
5392 struct got_reference *tmp_branch = NULL;
5393 struct got_object_id *resume_commit_id = NULL;
5394 struct got_object_id *base_commit_id = NULL;
5395 struct got_object_id *head_commit_id = NULL;
5396 struct got_commit_object *commit = NULL;
5397 int ch, rebase_in_progress = 0, did_something;
5398 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5399 const char *edit_script_path = NULL;
5400 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5401 struct got_object_id_queue commits;
5402 struct got_pathlist_head merged_paths;
5403 const struct got_object_id_queue *parent_ids;
5404 struct got_object_qid *pid;
5405 struct got_histedit_list histedit_cmds;
5406 struct got_histedit_list_entry *hle;
5408 SIMPLEQ_INIT(&commits);
5409 TAILQ_INIT(&histedit_cmds);
5410 TAILQ_INIT(&merged_paths);
5412 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5413 switch (ch) {
5414 case 'a':
5415 abort_edit = 1;
5416 break;
5417 case 'c':
5418 continue_edit = 1;
5419 break;
5420 case 'F':
5421 edit_script_path = optarg;
5422 break;
5423 default:
5424 usage_histedit();
5425 /* NOTREACHED */
5429 argc -= optind;
5430 argv += optind;
5432 #ifndef PROFILE
5433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5434 "unveil", NULL) == -1)
5435 err(1, "pledge");
5436 #endif
5437 if (abort_edit && continue_edit)
5438 usage_histedit();
5439 if (argc != 0)
5440 usage_histedit();
5443 * This command cannot apply unveil(2) in all cases because the
5444 * user may choose to run an editor to edit the histedit script
5445 * and to edit individual commit log messages.
5446 * unveil(2) traverses exec(2); if an editor is used we have to
5447 * apply unveil after edit script and log messages have been written.
5448 * XXX TODO: Make use of unveil(2) where possible.
5451 cwd = getcwd(NULL, 0);
5452 if (cwd == NULL) {
5453 error = got_error_from_errno("getcwd");
5454 goto done;
5456 error = got_worktree_open(&worktree, cwd);
5457 if (error)
5458 goto done;
5460 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5461 if (error != NULL)
5462 goto done;
5464 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5465 if (error)
5466 goto done;
5467 if (rebase_in_progress) {
5468 error = got_error(GOT_ERR_REBASING);
5469 goto done;
5472 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5473 if (error)
5474 goto done;
5476 if (edit_in_progress && abort_edit) {
5477 error = got_worktree_histedit_continue(&resume_commit_id,
5478 &tmp_branch, &branch, &base_commit_id, &fileindex,
5479 worktree, repo);
5480 if (error)
5481 goto done;
5482 printf("Switching work tree to %s\n",
5483 got_ref_get_symref_target(branch));
5484 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5485 branch, base_commit_id, update_progress, &did_something);
5486 if (error)
5487 goto done;
5488 printf("Histedit of %s aborted\n",
5489 got_ref_get_symref_target(branch));
5490 goto done; /* nothing else to do */
5491 } else if (abort_edit) {
5492 error = got_error(GOT_ERR_NOT_HISTEDIT);
5493 goto done;
5496 if (continue_edit) {
5497 char *path;
5499 if (!edit_in_progress) {
5500 error = got_error(GOT_ERR_NOT_HISTEDIT);
5501 goto done;
5504 error = got_worktree_get_histedit_script_path(&path, worktree);
5505 if (error)
5506 goto done;
5508 error = histedit_load_list(&histedit_cmds, path, repo);
5509 free(path);
5510 if (error)
5511 goto done;
5513 error = got_worktree_histedit_continue(&resume_commit_id,
5514 &tmp_branch, &branch, &base_commit_id, &fileindex,
5515 worktree, repo);
5516 if (error)
5517 goto done;
5519 error = got_ref_resolve(&head_commit_id, repo, branch);
5520 if (error)
5521 goto done;
5523 error = got_object_open_as_commit(&commit, repo,
5524 head_commit_id);
5525 if (error)
5526 goto done;
5527 parent_ids = got_object_commit_get_parent_ids(commit);
5528 pid = SIMPLEQ_FIRST(parent_ids);
5529 if (pid == NULL) {
5530 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5531 goto done;
5533 error = collect_commits(&commits, head_commit_id, pid->id,
5534 base_commit_id, got_worktree_get_path_prefix(worktree),
5535 GOT_ERR_HISTEDIT_PATH, repo);
5536 got_object_commit_close(commit);
5537 commit = NULL;
5538 if (error)
5539 goto done;
5540 } else {
5541 if (edit_in_progress) {
5542 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5543 goto done;
5546 error = got_ref_open(&branch, repo,
5547 got_worktree_get_head_ref_name(worktree), 0);
5548 if (error != NULL)
5549 goto done;
5551 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5552 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5553 "will not edit commit history of a branch outside "
5554 "the \"refs/heads/\" reference namespace");
5555 goto done;
5558 error = got_ref_resolve(&head_commit_id, repo, branch);
5559 got_ref_close(branch);
5560 branch = NULL;
5561 if (error)
5562 goto done;
5564 error = got_object_open_as_commit(&commit, repo,
5565 head_commit_id);
5566 if (error)
5567 goto done;
5568 parent_ids = got_object_commit_get_parent_ids(commit);
5569 pid = SIMPLEQ_FIRST(parent_ids);
5570 if (pid == NULL) {
5571 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5572 goto done;
5574 error = collect_commits(&commits, head_commit_id, pid->id,
5575 got_worktree_get_base_commit_id(worktree),
5576 got_worktree_get_path_prefix(worktree),
5577 GOT_ERR_HISTEDIT_PATH, repo);
5578 got_object_commit_close(commit);
5579 commit = NULL;
5580 if (error)
5581 goto done;
5583 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5584 &base_commit_id, &fileindex, worktree, repo);
5585 if (error)
5586 goto done;
5588 if (edit_script_path) {
5589 error = histedit_load_list(&histedit_cmds,
5590 edit_script_path, repo);
5591 if (error) {
5592 got_worktree_histedit_abort(worktree, fileindex,
5593 repo, branch, base_commit_id,
5594 update_progress, &did_something);
5595 goto done;
5597 } else {
5598 error = histedit_edit_script(&histedit_cmds, &commits,
5599 repo);
5600 if (error) {
5601 got_worktree_histedit_abort(worktree, fileindex,
5602 repo, branch, base_commit_id,
5603 update_progress, &did_something);
5604 goto done;
5609 error = histedit_save_list(&histedit_cmds, worktree,
5610 repo);
5611 if (error) {
5612 got_worktree_histedit_abort(worktree, fileindex,
5613 repo, branch, base_commit_id,
5614 update_progress, &did_something);
5615 goto done;
5620 error = histedit_check_script(&histedit_cmds, &commits, repo);
5621 if (error)
5622 goto done;
5624 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5625 if (resume_commit_id) {
5626 if (got_object_id_cmp(hle->commit_id,
5627 resume_commit_id) != 0)
5628 continue;
5630 resume_commit_id = NULL;
5631 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5632 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5633 error = histedit_skip_commit(hle, worktree,
5634 repo);
5635 } else {
5636 error = histedit_commit(NULL, worktree,
5637 fileindex, tmp_branch, hle, repo);
5639 if (error)
5640 goto done;
5641 continue;
5644 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5645 error = histedit_skip_commit(hle, worktree, repo);
5646 if (error)
5647 goto done;
5648 continue;
5651 error = got_object_open_as_commit(&commit, repo,
5652 hle->commit_id);
5653 if (error)
5654 goto done;
5655 parent_ids = got_object_commit_get_parent_ids(commit);
5656 pid = SIMPLEQ_FIRST(parent_ids);
5658 error = got_worktree_histedit_merge_files(&merged_paths,
5659 worktree, fileindex, pid->id, hle->commit_id, repo,
5660 rebase_progress, &rebase_status, check_cancelled, NULL);
5661 if (error)
5662 goto done;
5663 got_object_commit_close(commit);
5664 commit = NULL;
5666 if (rebase_status == GOT_STATUS_CONFLICT) {
5667 got_worktree_rebase_pathlist_free(&merged_paths);
5668 break;
5671 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5672 char *id_str;
5673 error = got_object_id_str(&id_str, hle->commit_id);
5674 if (error)
5675 goto done;
5676 printf("Stopping histedit for amending commit %s\n",
5677 id_str);
5678 free(id_str);
5679 got_worktree_rebase_pathlist_free(&merged_paths);
5680 error = got_worktree_histedit_postpone(worktree,
5681 fileindex);
5682 goto done;
5685 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5686 error = histedit_skip_commit(hle, worktree, repo);
5687 if (error)
5688 goto done;
5689 continue;
5692 error = histedit_commit(&merged_paths, worktree, fileindex,
5693 tmp_branch, hle, repo);
5694 got_worktree_rebase_pathlist_free(&merged_paths);
5695 if (error)
5696 goto done;
5699 if (rebase_status == GOT_STATUS_CONFLICT) {
5700 error = got_worktree_histedit_postpone(worktree, fileindex);
5701 if (error)
5702 goto done;
5703 error = got_error_msg(GOT_ERR_CONFLICTS,
5704 "conflicts must be resolved before rebasing can continue");
5705 } else
5706 error = histedit_complete(worktree, fileindex, tmp_branch,
5707 branch, repo);
5708 done:
5709 got_object_id_queue_free(&commits);
5710 histedit_free_list(&histedit_cmds);
5711 free(head_commit_id);
5712 free(base_commit_id);
5713 free(resume_commit_id);
5714 if (commit)
5715 got_object_commit_close(commit);
5716 if (branch)
5717 got_ref_close(branch);
5718 if (tmp_branch)
5719 got_ref_close(tmp_branch);
5720 if (worktree)
5721 got_worktree_close(worktree);
5722 if (repo)
5723 got_repo_close(repo);
5724 return error;
5727 __dead static void
5728 usage_stage(void)
5730 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5731 "[file-path ...]\n",
5732 getprogname());
5733 exit(1);
5736 static const struct got_error *
5737 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5738 const char *path, struct got_object_id *blob_id,
5739 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5741 const struct got_error *err = NULL;
5742 char *id_str = NULL;
5744 if (staged_status != GOT_STATUS_ADD &&
5745 staged_status != GOT_STATUS_MODIFY &&
5746 staged_status != GOT_STATUS_DELETE)
5747 return NULL;
5749 if (staged_status == GOT_STATUS_ADD ||
5750 staged_status == GOT_STATUS_MODIFY)
5751 err = got_object_id_str(&id_str, staged_blob_id);
5752 else
5753 err = got_object_id_str(&id_str, blob_id);
5754 if (err)
5755 return err;
5757 printf("%s %c %s\n", id_str, staged_status, path);
5758 free(id_str);
5759 return NULL;
5762 static const struct got_error *
5763 cmd_stage(int argc, char *argv[])
5765 const struct got_error *error = NULL;
5766 struct got_repository *repo = NULL;
5767 struct got_worktree *worktree = NULL;
5768 char *cwd = NULL;
5769 struct got_pathlist_head paths;
5770 struct got_pathlist_entry *pe;
5771 int ch, list_stage = 0, pflag = 0;
5772 FILE *patch_script_file = NULL;
5773 const char *patch_script_path = NULL;
5774 struct choose_patch_arg cpa;
5776 TAILQ_INIT(&paths);
5778 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5779 switch (ch) {
5780 case 'l':
5781 list_stage = 1;
5782 break;
5783 case 'p':
5784 pflag = 1;
5785 break;
5786 case 'F':
5787 patch_script_path = optarg;
5788 break;
5789 default:
5790 usage_stage();
5791 /* NOTREACHED */
5795 argc -= optind;
5796 argv += optind;
5798 #ifndef PROFILE
5799 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5800 "unveil", NULL) == -1)
5801 err(1, "pledge");
5802 #endif
5803 if (list_stage && (pflag || patch_script_path))
5804 errx(1, "-l option cannot be used with other options");
5805 if (patch_script_path && !pflag)
5806 errx(1, "-F option can only be used together with -p option");
5808 cwd = getcwd(NULL, 0);
5809 if (cwd == NULL) {
5810 error = got_error_from_errno("getcwd");
5811 goto done;
5814 error = got_worktree_open(&worktree, cwd);
5815 if (error)
5816 goto done;
5818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5819 if (error != NULL)
5820 goto done;
5822 if (patch_script_path) {
5823 patch_script_file = fopen(patch_script_path, "r");
5824 if (patch_script_file == NULL) {
5825 error = got_error_from_errno2("fopen",
5826 patch_script_path);
5827 goto done;
5830 error = apply_unveil(got_repo_get_path(repo), 1,
5831 got_worktree_get_root_path(worktree));
5832 if (error)
5833 goto done;
5835 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5836 if (error)
5837 goto done;
5839 if (list_stage)
5840 error = got_worktree_status(worktree, &paths, repo,
5841 print_stage, NULL, check_cancelled, NULL);
5842 else {
5843 cpa.patch_script_file = patch_script_file;
5844 cpa.action = "stage";
5845 error = got_worktree_stage(worktree, &paths,
5846 pflag ? NULL : print_status, NULL,
5847 pflag ? choose_patch : NULL, &cpa, repo);
5849 done:
5850 if (patch_script_file && fclose(patch_script_file) == EOF &&
5851 error == NULL)
5852 error = got_error_from_errno2("fclose", patch_script_path);
5853 if (repo)
5854 got_repo_close(repo);
5855 if (worktree)
5856 got_worktree_close(worktree);
5857 TAILQ_FOREACH(pe, &paths, entry)
5858 free((char *)pe->path);
5859 got_pathlist_free(&paths);
5860 free(cwd);
5861 return error;
5864 __dead static void
5865 usage_unstage(void)
5867 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5868 "[file-path ...]\n",
5869 getprogname());
5870 exit(1);
5874 static const struct got_error *
5875 cmd_unstage(int argc, char *argv[])
5877 const struct got_error *error = NULL;
5878 struct got_repository *repo = NULL;
5879 struct got_worktree *worktree = NULL;
5880 char *cwd = NULL;
5881 struct got_pathlist_head paths;
5882 struct got_pathlist_entry *pe;
5883 int ch, did_something = 0, pflag = 0;
5884 FILE *patch_script_file = NULL;
5885 const char *patch_script_path = NULL;
5886 struct choose_patch_arg cpa;
5888 TAILQ_INIT(&paths);
5890 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5891 switch (ch) {
5892 case 'p':
5893 pflag = 1;
5894 break;
5895 case 'F':
5896 patch_script_path = optarg;
5897 break;
5898 default:
5899 usage_unstage();
5900 /* NOTREACHED */
5904 argc -= optind;
5905 argv += optind;
5907 #ifndef PROFILE
5908 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5909 "unveil", NULL) == -1)
5910 err(1, "pledge");
5911 #endif
5912 if (patch_script_path && !pflag)
5913 errx(1, "-F option can only be used together with -p option");
5915 cwd = getcwd(NULL, 0);
5916 if (cwd == NULL) {
5917 error = got_error_from_errno("getcwd");
5918 goto done;
5921 error = got_worktree_open(&worktree, cwd);
5922 if (error)
5923 goto done;
5925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5926 if (error != NULL)
5927 goto done;
5929 if (patch_script_path) {
5930 patch_script_file = fopen(patch_script_path, "r");
5931 if (patch_script_file == NULL) {
5932 error = got_error_from_errno2("fopen",
5933 patch_script_path);
5934 goto done;
5938 error = apply_unveil(got_repo_get_path(repo), 1,
5939 got_worktree_get_root_path(worktree));
5940 if (error)
5941 goto done;
5943 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5944 if (error)
5945 goto done;
5947 cpa.patch_script_file = patch_script_file;
5948 cpa.action = "unstage";
5949 error = got_worktree_unstage(worktree, &paths, update_progress,
5950 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5951 done:
5952 if (patch_script_file && fclose(patch_script_file) == EOF &&
5953 error == NULL)
5954 error = got_error_from_errno2("fclose", patch_script_path);
5955 if (repo)
5956 got_repo_close(repo);
5957 if (worktree)
5958 got_worktree_close(worktree);
5959 TAILQ_FOREACH(pe, &paths, entry)
5960 free((char *)pe->path);
5961 got_pathlist_free(&paths);
5962 free(cwd);
5963 return error;
5966 __dead static void
5967 usage_cat(void)
5969 fprintf(stderr, "usage: %s cat [-r repository ] object1 "
5970 "[object2 ...]\n", getprogname());
5971 exit(1);
5974 static const struct got_error *
5975 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5977 const struct got_error *err;
5978 struct got_blob_object *blob;
5980 err = got_object_open_as_blob(&blob, repo, id, 8192);
5981 if (err)
5982 return err;
5984 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
5985 got_object_blob_close(blob);
5986 return err;
5989 static const struct got_error *
5990 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
5992 const struct got_error *err;
5993 struct got_tree_object *tree;
5994 const struct got_tree_entries *entries;
5995 struct got_tree_entry *te;
5997 err = got_object_open_as_tree(&tree, repo, id);
5998 if (err)
5999 return err;
6001 entries = got_object_tree_get_entries(tree);
6002 te = SIMPLEQ_FIRST(&entries->head);
6003 while (te) {
6004 char *id_str;
6005 if (sigint_received || sigpipe_received)
6006 break;
6007 err = got_object_id_str(&id_str, te->id);
6008 if (err)
6009 break;
6010 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6011 free(id_str);
6012 te = SIMPLEQ_NEXT(te, entry);
6015 got_object_tree_close(tree);
6016 return err;
6019 static const struct got_error *
6020 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6022 const struct got_error *err;
6023 struct got_commit_object *commit;
6024 const struct got_object_id_queue *parent_ids;
6025 struct got_object_qid *pid;
6026 char *id_str = NULL;
6027 const char *logmsg = NULL;
6028 int i;
6030 err = got_object_open_as_commit(&commit, repo, id);
6031 if (err)
6032 return err;
6034 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6035 if (err)
6036 goto done;
6038 fprintf(outfile, "tree: %s\n", id_str);
6039 parent_ids = got_object_commit_get_parent_ids(commit);
6040 fprintf(outfile, "parents: %d\n",
6041 got_object_commit_get_nparents(commit));
6042 i = 1;
6043 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6044 char *pid_str;
6045 err = got_object_id_str(&pid_str, pid->id);
6046 if (err)
6047 goto done;
6048 fprintf(outfile, "parent %d: %s\n", i++, pid_str);
6049 free(pid_str);
6051 fprintf(outfile, "author: %s\n",
6052 got_object_commit_get_author(commit));
6053 fprintf(outfile, "author-time: %lld\n",
6054 got_object_commit_get_author_time(commit));
6056 fprintf(outfile, "committer: %s\n",
6057 got_object_commit_get_author(commit));
6058 fprintf(outfile, "committer-time: %lld\n",
6059 got_object_commit_get_committer_time(commit));
6061 logmsg = got_object_commit_get_logmsg_raw(commit);
6062 fprintf(outfile, "log-message: %zd bytes\n", strlen(logmsg));
6063 fprintf(outfile, "%s", logmsg);
6064 done:
6065 free(id_str);
6066 got_object_commit_close(commit);
6067 return err;
6070 static const struct got_error *
6071 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6073 const struct got_error *err;
6074 struct got_tag_object *tag;
6075 char *id_str = NULL;
6076 const char *tagmsg = NULL;
6078 err = got_object_open_as_tag(&tag, repo, id);
6079 if (err)
6080 return err;
6082 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6083 if (err)
6084 goto done;
6086 fprintf(outfile, "tag-name: %s\n", got_object_tag_get_name(tag));
6087 switch (got_object_tag_get_object_type(tag)) {
6088 case GOT_OBJ_TYPE_BLOB:
6089 fprintf(outfile, "tagged-object-type: blob\n");
6090 break;
6091 case GOT_OBJ_TYPE_TREE:
6092 fprintf(outfile, "tagged-object-type: tree\n");
6093 break;
6094 case GOT_OBJ_TYPE_COMMIT:
6095 fprintf(outfile, "tagged-object-type: commit\n");
6096 break;
6097 case GOT_OBJ_TYPE_TAG:
6098 fprintf(outfile, "tagged-object-type: tag\n");
6099 break;
6100 default:
6101 break;
6103 fprintf(outfile, "tagged-object: %s\n", id_str);
6105 fprintf(outfile, "tagger: %s\n",
6106 got_object_tag_get_tagger(tag));
6107 fprintf(outfile, "tagger-time: %lld %lld\n",
6108 got_object_tag_get_tagger_time(tag),
6109 got_object_tag_get_tagger_gmtoff(tag));
6111 tagmsg = got_object_tag_get_message(tag);
6112 fprintf(outfile, "tag-message: %zd bytes\n", strlen(tagmsg));
6113 fprintf(outfile, "%s", tagmsg);
6114 done:
6115 free(id_str);
6116 got_object_tag_close(tag);
6117 return err;
6120 static const struct got_error *
6121 cmd_cat(int argc, char *argv[])
6123 const struct got_error *error;
6124 struct got_repository *repo = NULL;
6125 struct got_worktree *worktree = NULL;
6126 char *cwd = NULL, *repo_path = NULL, *label = NULL;
6127 struct got_object_id *id = NULL;
6128 int ch, obj_type, i;
6130 #ifndef PROFILE
6131 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6132 NULL) == -1)
6133 err(1, "pledge");
6134 #endif
6136 while ((ch = getopt(argc, argv, "r:")) != -1) {
6137 switch (ch) {
6138 case 'r':
6139 repo_path = realpath(optarg, NULL);
6140 if (repo_path == NULL)
6141 err(1, "-r option");
6142 got_path_strip_trailing_slashes(repo_path);
6143 break;
6144 default:
6145 usage_cat();
6146 /* NOTREACHED */
6150 argc -= optind;
6151 argv += optind;
6153 cwd = getcwd(NULL, 0);
6154 if (cwd == NULL) {
6155 error = got_error_from_errno("getcwd");
6156 goto done;
6158 error = got_worktree_open(&worktree, cwd);
6159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6160 goto done;
6161 if (worktree) {
6162 if (repo_path == NULL) {
6163 repo_path = strdup(
6164 got_worktree_get_repo_path(worktree));
6165 if (repo_path == NULL) {
6166 error = got_error_from_errno("strdup");
6167 goto done;
6172 if (repo_path == NULL) {
6173 repo_path = getcwd(NULL, 0);
6174 if (repo_path == NULL)
6175 return got_error_from_errno("getcwd");
6178 error = got_repo_open(&repo, repo_path);
6179 free(repo_path);
6180 if (error != NULL)
6181 goto done;
6183 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6184 if (error)
6185 goto done;
6187 for (i = 0; i < argc; i++) {
6188 error = match_object_id(&id, &label, argv[i],
6189 GOT_OBJ_TYPE_ANY, 0, repo);
6190 if (error)
6191 break;
6193 error = got_object_get_type(&obj_type, repo, id);
6194 if (error)
6195 break;
6197 switch (obj_type) {
6198 case GOT_OBJ_TYPE_BLOB:
6199 error = cat_blob(id, repo, stdout);
6200 break;
6201 case GOT_OBJ_TYPE_TREE:
6202 error = cat_tree(id, repo, stdout);
6203 break;
6204 case GOT_OBJ_TYPE_COMMIT:
6205 error = cat_commit(id, repo, stdout);
6206 break;
6207 case GOT_OBJ_TYPE_TAG:
6208 error = cat_tag(id, repo, stdout);
6209 break;
6210 default:
6211 error = got_error(GOT_ERR_OBJ_TYPE);
6212 break;
6214 if (error)
6215 break;
6216 free(label);
6217 label = NULL;
6218 free(id);
6219 id = NULL;
6222 done:
6223 free(label);
6224 free(id);
6225 if (worktree)
6226 got_worktree_close(worktree);
6227 if (repo) {
6228 const struct got_error *repo_error;
6229 repo_error = got_repo_close(repo);
6230 if (error == NULL)
6231 error = repo_error;
6233 return error;