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_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
58 static void
59 catch_sigint(int signo)
60 {
61 sigint_received = 1;
62 }
64 static void
65 catch_sigpipe(int signo)
66 {
67 sigpipe_received = 1;
68 }
71 struct got_cmd {
72 const char *cmd_name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
76 };
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
145 };
147 static void
148 list_commands(void)
150 int i;
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
157 fputc('\n', stderr);
160 int
161 main(int argc, char *argv[])
163 struct got_cmd *cmd;
164 unsigned int i;
165 int ch;
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
171 switch (ch) {
172 case 'h':
173 hflag = 1;
174 break;
175 case 'V':
176 Vflag = 1;
177 break;
178 default:
179 usage(hflag);
180 /* NOTREACHED */
184 argc -= optind;
185 argv += optind;
186 optind = 0;
188 if (Vflag) {
189 got_version_print_str();
190 return 1;
193 if (argc <= 0)
194 usage(hflag);
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
206 continue;
208 if (hflag)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
214 return 1;
217 return 0;
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
221 list_commands();
222 return 1;
225 __dead static void
226 usage(int hflag)
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 getprogname());
230 if (hflag)
231 list_commands();
232 exit(1);
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
239 const char *editor;
241 editor = getenv("VISUAL");
242 if (editor == NULL)
243 editor = getenv("EDITOR");
245 if (editor) {
246 err = got_path_find_prog(abspath, editor);
247 if (err)
248 return err;
251 if (*abspath == NULL) {
252 *abspath = strdup("/bin/ed");
253 if (*abspath == NULL)
254 return got_error_from_errno("strdup");
257 return NULL;
260 static const struct got_error *
261 apply_unveil(const char *repo_path, int repo_read_only,
262 const char *worktree_path)
264 const struct got_error *err;
266 #ifdef PROFILE
267 if (unveil("gmon.out", "rwc") != 0)
268 return got_error_from_errno2("unveil", "gmon.out");
269 #endif
270 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 return got_error_from_errno2("unveil", repo_path);
273 if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 return got_error_from_errno2("unveil", worktree_path);
276 if (unveil("/tmp", "rwc") != 0)
277 return got_error_from_errno2("unveil", "/tmp");
279 err = got_privsep_unveil_exec_helpers();
280 if (err != NULL)
281 return err;
283 if (unveil(NULL, NULL) != 0)
284 return got_error_from_errno("unveil");
286 return NULL;
289 __dead static void
290 usage_init(void)
292 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
293 exit(1);
296 static const struct got_error *
297 cmd_init(int argc, char *argv[])
299 const struct got_error *error = NULL;
300 char *repo_path = NULL;
301 int ch;
303 while ((ch = getopt(argc, argv, "")) != -1) {
304 switch (ch) {
305 default:
306 usage_init();
307 /* NOTREACHED */
311 argc -= optind;
312 argv += optind;
314 #ifndef PROFILE
315 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
316 err(1, "pledge");
317 #endif
318 if (argc != 1)
319 usage_init();
321 repo_path = strdup(argv[0]);
322 if (repo_path == NULL)
323 return got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(repo_path);
327 error = got_path_mkdir(repo_path);
328 if (error &&
329 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
330 goto done;
332 error = apply_unveil(repo_path, 0, NULL);
333 if (error)
334 goto done;
336 error = got_repo_init(repo_path);
337 if (error != NULL)
338 goto done;
340 done:
341 free(repo_path);
342 return error;
345 __dead static void
346 usage_import(void)
348 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 "[-r repository-path] [-I pattern] path\n", getprogname());
350 exit(1);
353 int
354 spawn_editor(const char *editor, const char *file)
356 pid_t pid;
357 sig_t sighup, sigint, sigquit;
358 int st = -1;
360 sighup = signal(SIGHUP, SIG_IGN);
361 sigint = signal(SIGINT, SIG_IGN);
362 sigquit = signal(SIGQUIT, SIG_IGN);
364 switch (pid = fork()) {
365 case -1:
366 goto doneediting;
367 case 0:
368 execl(editor, editor, file, (char *)NULL);
369 _exit(127);
372 while (waitpid(pid, &st, 0) == -1)
373 if (errno != EINTR)
374 break;
376 doneediting:
377 (void)signal(SIGHUP, sighup);
378 (void)signal(SIGINT, sigint);
379 (void)signal(SIGQUIT, sigquit);
381 if (!WIFEXITED(st)) {
382 errno = EINTR;
383 return -1;
386 return WEXITSTATUS(st);
389 static const struct got_error *
390 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 const char *initial_content)
393 const struct got_error *err = NULL;
394 char buf[1024];
395 struct stat st, st2;
396 FILE *fp;
397 int content_changed = 0;
398 size_t len;
400 *logmsg = NULL;
402 if (stat(logmsg_path, &st) == -1)
403 return got_error_from_errno2("stat", logmsg_path);
405 if (spawn_editor(editor, logmsg_path) == -1)
406 return got_error_from_errno("failed spawning editor");
408 if (stat(logmsg_path, &st2) == -1)
409 return got_error_from_errno("stat");
411 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 "no changes made to commit message, aborting");
415 *logmsg = malloc(st2.st_size + 1);
416 if (*logmsg == NULL)
417 return got_error_from_errno("malloc");
418 (*logmsg)[0] = '\0';
419 len = 0;
421 fp = fopen(logmsg_path, "r");
422 if (fp == NULL) {
423 err = got_error_from_errno("fopen");
424 goto done;
426 while (fgets(buf, sizeof(buf), fp) != NULL) {
427 if (!content_changed && strcmp(buf, initial_content) != 0)
428 content_changed = 1;
429 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 continue; /* remove comments and leading empty lines */
431 len = strlcat(*logmsg, buf, st2.st_size);
433 fclose(fp);
435 while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 (*logmsg)[len - 1] = '\0';
437 len--;
440 if (len == 0 || !content_changed)
441 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 "commit message cannot be empty, aborting");
443 done:
444 if (err) {
445 free(*logmsg);
446 *logmsg = NULL;
448 return err;
451 static const struct got_error *
452 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 const char *branch_name)
455 char *initial_content = NULL, *logmsg_path = NULL;
456 const struct got_error *err = NULL;
457 int fd;
459 if (asprintf(&initial_content,
460 "\n# %s to be imported to branch %s\n", path_dir,
461 branch_name) == -1)
462 return got_error_from_errno("asprintf");
464 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
465 if (err)
466 goto done;
468 dprintf(fd, initial_content);
469 close(fd);
471 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
472 done:
473 free(initial_content);
474 free(logmsg_path);
475 return err;
478 static const struct got_error *
479 import_progress(void *arg, const char *path)
481 printf("A %s\n", path);
482 return NULL;
485 static const struct got_error *
486 get_author(const char **author)
488 const char *got_author;
490 *author = NULL;
492 got_author = getenv("GOT_AUTHOR");
493 if (got_author == NULL) {
494 /* TODO: Look up user in password database? */
495 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 *author = got_author;
500 /*
501 * Really dumb email address check; we're only doing this to
502 * avoid git's object parser breaking on commits we create.
503 */
504 while (*got_author && *got_author != '<')
505 got_author++;
506 if (*got_author != '<')
507 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 while (*got_author && *got_author != '@')
509 got_author++;
510 if (*got_author != '@')
511 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 while (*got_author && *got_author != '>')
513 got_author++;
514 if (*got_author != '>')
515 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
517 return NULL;
520 static const struct got_error *
521 cmd_import(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
525 char *editor = NULL;
526 const char *author;
527 const char *branch_name = "master";
528 char *refname = NULL, *id_str = NULL;
529 struct got_repository *repo = NULL;
530 struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 struct got_object_id *new_commit_id = NULL;
532 int ch;
533 struct got_pathlist_head ignores;
534 struct got_pathlist_entry *pe;
536 TAILQ_INIT(&ignores);
538 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
539 switch (ch) {
540 case 'b':
541 branch_name = optarg;
542 break;
543 case 'm':
544 logmsg = strdup(optarg);
545 if (logmsg == NULL) {
546 error = got_error_from_errno("strdup");
547 goto done;
549 break;
550 case 'r':
551 repo_path = realpath(optarg, NULL);
552 if (repo_path == NULL) {
553 error = got_error_from_errno("realpath");
554 goto done;
556 break;
557 case 'I':
558 if (optarg[0] == '\0')
559 break;
560 error = got_pathlist_insert(&pe, &ignores, optarg,
561 NULL);
562 if (error)
563 goto done;
564 break;
565 default:
566 usage_init();
567 /* NOTREACHED */
571 argc -= optind;
572 argv += optind;
574 #ifndef PROFILE
575 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
576 NULL) == -1)
577 err(1, "pledge");
578 #endif
579 if (argc != 1)
580 usage_import();
582 error = get_author(&author);
583 if (error)
584 return error;
586 if (repo_path == NULL) {
587 repo_path = getcwd(NULL, 0);
588 if (repo_path == NULL)
589 return got_error_from_errno("getcwd");
591 got_path_strip_trailing_slashes(repo_path);
592 error = got_repo_open(&repo, repo_path);
593 if (error)
594 goto done;
596 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 error = got_error_from_errno("asprintf");
598 goto done;
601 error = got_ref_open(&branch_ref, repo, refname, 0);
602 if (error) {
603 if (error->code != GOT_ERR_NOT_REF)
604 goto done;
605 } else {
606 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 "import target branch already exists");
608 goto done;
611 path_dir = realpath(argv[0], NULL);
612 if (path_dir == NULL) {
613 error = got_error_from_errno("realpath");
614 goto done;
616 got_path_strip_trailing_slashes(path_dir);
618 /*
619 * unveil(2) traverses exec(2); if an editor is used we have
620 * to apply unveil after the log message has been written.
621 */
622 if (logmsg == NULL || strlen(logmsg) == 0) {
623 error = get_editor(&editor);
624 if (error)
625 goto done;
626 error = collect_import_msg(&logmsg, editor, path_dir, refname);
627 if (error)
628 goto done;
631 if (unveil(path_dir, "r") != 0)
632 return got_error_from_errno2("unveil", path_dir);
634 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
635 if (error)
636 goto done;
638 error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 author, &ignores, repo, import_progress, NULL);
640 if (error)
641 goto done;
643 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
644 if (error)
645 goto done;
647 error = got_ref_write(branch_ref, repo);
648 if (error)
649 goto done;
651 error = got_object_id_str(&id_str, new_commit_id);
652 if (error)
653 goto done;
655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
656 if (error) {
657 if (error->code != GOT_ERR_NOT_REF)
658 goto done;
660 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
661 branch_ref);
662 if (error)
663 goto done;
665 error = got_ref_write(head_ref, repo);
666 if (error)
667 goto done;
670 printf("Created branch %s with commit %s\n",
671 got_ref_get_name(branch_ref), id_str);
672 done:
673 free(repo_path);
674 free(editor);
675 free(refname);
676 free(new_commit_id);
677 free(id_str);
678 if (branch_ref)
679 got_ref_close(branch_ref);
680 if (head_ref)
681 got_ref_close(head_ref);
682 return error;
685 __dead static void
686 usage_checkout(void)
688 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 "[-p prefix] repository-path [worktree-path]\n", getprogname());
690 exit(1);
693 static const struct got_error *
694 checkout_progress(void *arg, unsigned char status, const char *path)
696 char *worktree_path = arg;
698 /* Base commit bump happens silently. */
699 if (status == GOT_STATUS_BUMP_BASE)
700 return NULL;
702 while (path[0] == '/')
703 path++;
705 printf("%c %s/%s\n", status, worktree_path, path);
706 return NULL;
709 static const struct got_error *
710 check_cancelled(void *arg)
712 if (sigint_received || sigpipe_received)
713 return got_error(GOT_ERR_CANCELLED);
714 return NULL;
717 static const struct got_error *
718 check_linear_ancestry(struct got_object_id *commit_id,
719 struct got_object_id *base_commit_id, struct got_repository *repo)
721 const struct got_error *err = NULL;
722 struct got_object_id *yca_id;
724 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 commit_id, base_commit_id, repo);
726 if (err)
727 return err;
729 if (yca_id == NULL)
730 return got_error(GOT_ERR_ANCESTRY);
732 /*
733 * Require a straight line of history between the target commit
734 * and the work tree's base commit.
736 * Non-linear situations such as this require a rebase:
738 * (commit) D F (base_commit)
739 * \ /
740 * C E
741 * \ /
742 * B (yca)
743 * |
744 * A
746 * 'got update' only handles linear cases:
747 * Update forwards in time: A (base/yca) - B - C - D (commit)
748 * Update backwards in time: D (base) - C - B - A (commit/yca)
749 */
750 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 got_object_id_cmp(base_commit_id, yca_id) != 0)
752 return got_error(GOT_ERR_ANCESTRY);
754 free(yca_id);
755 return NULL;
758 static const struct got_error *
759 check_same_branch(struct got_object_id *commit_id,
760 struct got_reference *head_ref, struct got_object_id *yca_id,
761 struct got_repository *repo)
763 const struct got_error *err = NULL;
764 struct got_commit_graph *graph = NULL;
765 struct got_object_id *head_commit_id = NULL;
766 int is_same_branch = 0;
768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
769 if (err)
770 goto done;
772 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
773 is_same_branch = 1;
774 goto done;
776 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
777 is_same_branch = 1;
778 goto done;
781 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
782 if (err)
783 goto done;
785 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
786 if (err)
787 goto done;
789 for (;;) {
790 struct got_object_id *id;
791 err = got_commit_graph_iter_next(&id, graph);
792 if (err) {
793 if (err->code == GOT_ERR_ITER_COMPLETED) {
794 err = NULL;
795 break;
796 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
797 break;
798 err = got_commit_graph_fetch_commits(graph, 1,
799 repo);
800 if (err)
801 break;
804 if (id) {
805 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
806 break;
807 if (got_object_id_cmp(id, commit_id) == 0) {
808 is_same_branch = 1;
809 break;
813 done:
814 if (graph)
815 got_commit_graph_close(graph);
816 free(head_commit_id);
817 if (!err && !is_same_branch)
818 err = got_error(GOT_ERR_ANCESTRY);
819 return err;
822 static const struct got_error *
823 resolve_commit_arg(struct got_object_id **commit_id,
824 const char *commit_id_arg, struct got_repository *repo)
826 const struct got_error *err;
827 struct got_reference *ref;
828 struct got_tag_object *tag;
830 err = got_repo_object_match_tag(&tag, commit_id_arg,
831 GOT_OBJ_TYPE_COMMIT, repo);
832 if (err == NULL) {
833 *commit_id = got_object_id_dup(
834 got_object_tag_get_object_id(tag));
835 if (*commit_id == NULL)
836 err = got_error_from_errno("got_object_id_dup");
837 got_object_tag_close(tag);
838 return err;
839 } else if (err->code != GOT_ERR_NO_OBJ)
840 return err;
842 err = got_ref_open(&ref, repo, commit_id_arg, 0);
843 if (err == NULL) {
844 err = got_ref_resolve(commit_id, repo, ref);
845 got_ref_close(ref);
846 } else {
847 if (err->code != GOT_ERR_NOT_REF)
848 return err;
849 err = got_repo_match_object_id_prefix(commit_id,
850 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
852 return err;
855 static const struct got_error *
856 cmd_checkout(int argc, char *argv[])
858 const struct got_error *error = NULL;
859 struct got_repository *repo = NULL;
860 struct got_reference *head_ref = NULL;
861 struct got_worktree *worktree = NULL;
862 char *repo_path = NULL;
863 char *worktree_path = NULL;
864 const char *path_prefix = "";
865 const char *branch_name = GOT_REF_HEAD;
866 char *commit_id_str = NULL;
867 int ch, same_path_prefix;
868 struct got_pathlist_head paths;
870 TAILQ_INIT(&paths);
872 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
873 switch (ch) {
874 case 'b':
875 branch_name = optarg;
876 break;
877 case 'c':
878 commit_id_str = strdup(optarg);
879 if (commit_id_str == NULL)
880 return got_error_from_errno("strdup");
881 break;
882 case 'p':
883 path_prefix = optarg;
884 break;
885 default:
886 usage_checkout();
887 /* NOTREACHED */
891 argc -= optind;
892 argv += optind;
894 #ifndef PROFILE
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 "unveil", NULL) == -1)
897 err(1, "pledge");
898 #endif
899 if (argc == 1) {
900 char *cwd, *base, *dotgit;
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno2("realpath", argv[0]);
904 cwd = getcwd(NULL, 0);
905 if (cwd == NULL) {
906 error = got_error_from_errno("getcwd");
907 goto done;
909 if (path_prefix[0]) {
910 base = basename(path_prefix);
911 if (base == NULL) {
912 error = got_error_from_errno2("basename",
913 path_prefix);
914 goto done;
916 } else {
917 base = basename(repo_path);
918 if (base == NULL) {
919 error = got_error_from_errno2("basename",
920 repo_path);
921 goto done;
924 dotgit = strstr(base, ".git");
925 if (dotgit)
926 *dotgit = '\0';
927 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 error = got_error_from_errno("asprintf");
929 free(cwd);
930 goto done;
932 free(cwd);
933 } else if (argc == 2) {
934 repo_path = realpath(argv[0], NULL);
935 if (repo_path == NULL) {
936 error = got_error_from_errno2("realpath", argv[0]);
937 goto done;
939 worktree_path = realpath(argv[1], NULL);
940 if (worktree_path == NULL) {
941 if (errno != ENOENT) {
942 error = got_error_from_errno2("realpath",
943 argv[1]);
944 goto done;
946 worktree_path = strdup(argv[1]);
947 if (worktree_path == NULL) {
948 error = got_error_from_errno("strdup");
949 goto done;
952 } else
953 usage_checkout();
955 got_path_strip_trailing_slashes(repo_path);
956 got_path_strip_trailing_slashes(worktree_path);
958 error = got_repo_open(&repo, repo_path);
959 if (error != NULL)
960 goto done;
962 /* Pre-create work tree path for unveil(2) */
963 error = got_path_mkdir(worktree_path);
964 if (error) {
965 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
967 goto done;
968 if (!got_path_dir_is_empty(worktree_path)) {
969 error = got_error_path(worktree_path,
970 GOT_ERR_DIR_NOT_EMPTY);
971 goto done;
975 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
976 if (error)
977 goto done;
979 error = got_ref_open(&head_ref, repo, branch_name, 0);
980 if (error != NULL)
981 goto done;
983 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
985 goto done;
987 error = got_worktree_open(&worktree, worktree_path);
988 if (error != NULL)
989 goto done;
991 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
992 path_prefix);
993 if (error != NULL)
994 goto done;
995 if (!same_path_prefix) {
996 error = got_error(GOT_ERR_PATH_PREFIX);
997 goto done;
1000 if (commit_id_str) {
1001 struct got_object_id *commit_id;
1002 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1003 if (error)
1004 goto done;
1005 error = check_linear_ancestry(commit_id,
1006 got_worktree_get_base_commit_id(worktree), repo);
1007 if (error != NULL) {
1008 free(commit_id);
1009 goto done;
1011 error = check_same_branch(commit_id, head_ref, NULL, repo);
1012 if (error)
1013 goto done;
1014 error = got_worktree_set_base_commit_id(worktree, repo,
1015 commit_id);
1016 free(commit_id);
1017 if (error)
1018 goto done;
1021 error = got_pathlist_append(&paths, "", NULL);
1022 if (error)
1023 goto done;
1024 error = got_worktree_checkout_files(worktree, &paths, repo,
1025 checkout_progress, worktree_path, check_cancelled, NULL);
1026 if (error != NULL)
1027 goto done;
1029 printf("Now shut up and hack\n");
1031 done:
1032 got_pathlist_free(&paths);
1033 free(commit_id_str);
1034 free(repo_path);
1035 free(worktree_path);
1036 return error;
1039 __dead static void
1040 usage_update(void)
1042 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1043 getprogname());
1044 exit(1);
1047 static const struct got_error *
1048 update_progress(void *arg, unsigned char status, const char *path)
1050 int *did_something = arg;
1052 if (status == GOT_STATUS_EXISTS)
1053 return NULL;
1055 *did_something = 1;
1057 /* Base commit bump happens silently. */
1058 if (status == GOT_STATUS_BUMP_BASE)
1059 return NULL;
1061 while (path[0] == '/')
1062 path++;
1063 printf("%c %s\n", status, path);
1064 return NULL;
1067 static const struct got_error *
1068 switch_head_ref(struct got_reference *head_ref,
1069 struct got_object_id *commit_id, struct got_worktree *worktree,
1070 struct got_repository *repo)
1072 const struct got_error *err = NULL;
1073 char *base_id_str;
1074 int ref_has_moved = 0;
1076 /* Trivial case: switching between two different references. */
1077 if (strcmp(got_ref_get_name(head_ref),
1078 got_worktree_get_head_ref_name(worktree)) != 0) {
1079 printf("Switching work tree from %s to %s\n",
1080 got_worktree_get_head_ref_name(worktree),
1081 got_ref_get_name(head_ref));
1082 return got_worktree_set_head_ref(worktree, head_ref);
1085 err = check_linear_ancestry(commit_id,
1086 got_worktree_get_base_commit_id(worktree), repo);
1087 if (err) {
1088 if (err->code != GOT_ERR_ANCESTRY)
1089 return err;
1090 ref_has_moved = 1;
1092 if (!ref_has_moved)
1093 return NULL;
1095 /* Switching to a rebased branch with the same reference name. */
1096 err = got_object_id_str(&base_id_str,
1097 got_worktree_get_base_commit_id(worktree));
1098 if (err)
1099 return err;
1100 printf("Reference %s now points at a different branch\n",
1101 got_worktree_get_head_ref_name(worktree));
1102 printf("Switching work tree from %s to %s\n", base_id_str,
1103 got_worktree_get_head_ref_name(worktree));
1104 return NULL;
1107 static const struct got_error *
1108 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 const struct got_error *err;
1111 int in_progress;
1113 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1114 if (err)
1115 return err;
1116 if (in_progress)
1117 return got_error(GOT_ERR_REBASING);
1119 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1120 if (err)
1121 return err;
1122 if (in_progress)
1123 return got_error(GOT_ERR_HISTEDIT_BUSY);
1125 return NULL;
1128 static const struct got_error *
1129 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 char *argv[], struct got_worktree *worktree)
1132 const struct got_error *err = NULL;
1133 char *path;
1134 int i;
1136 if (argc == 0) {
1137 path = strdup("");
1138 if (path == NULL)
1139 return got_error_from_errno("strdup");
1140 return got_pathlist_append(paths, path, NULL);
1143 for (i = 0; i < argc; i++) {
1144 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1145 if (err)
1146 break;
1147 err = got_pathlist_append(paths, path, NULL);
1148 if (err) {
1149 free(path);
1150 break;
1154 return err;
1157 static const struct got_error *
1158 cmd_update(int argc, char *argv[])
1160 const struct got_error *error = NULL;
1161 struct got_repository *repo = NULL;
1162 struct got_worktree *worktree = NULL;
1163 char *worktree_path = NULL;
1164 struct got_object_id *commit_id = NULL;
1165 char *commit_id_str = NULL;
1166 const char *branch_name = NULL;
1167 struct got_reference *head_ref = NULL;
1168 struct got_pathlist_head paths;
1169 struct got_pathlist_entry *pe;
1170 int ch, did_something = 0;
1172 TAILQ_INIT(&paths);
1174 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1175 switch (ch) {
1176 case 'b':
1177 branch_name = optarg;
1178 break;
1179 case 'c':
1180 commit_id_str = strdup(optarg);
1181 if (commit_id_str == NULL)
1182 return got_error_from_errno("strdup");
1183 break;
1184 default:
1185 usage_update();
1186 /* NOTREACHED */
1190 argc -= optind;
1191 argv += optind;
1193 #ifndef PROFILE
1194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 "unveil", NULL) == -1)
1196 err(1, "pledge");
1197 #endif
1198 worktree_path = getcwd(NULL, 0);
1199 if (worktree_path == NULL) {
1200 error = got_error_from_errno("getcwd");
1201 goto done;
1203 error = got_worktree_open(&worktree, worktree_path);
1204 if (error)
1205 goto done;
1207 error = check_rebase_or_histedit_in_progress(worktree);
1208 if (error)
1209 goto done;
1211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1212 if (error != NULL)
1213 goto done;
1215 error = apply_unveil(got_repo_get_path(repo), 0,
1216 got_worktree_get_root_path(worktree));
1217 if (error)
1218 goto done;
1220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1221 if (error)
1222 goto done;
1224 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 got_worktree_get_head_ref_name(worktree), 0);
1226 if (error != NULL)
1227 goto done;
1228 if (commit_id_str == NULL) {
1229 error = got_ref_resolve(&commit_id, repo, head_ref);
1230 if (error != NULL)
1231 goto done;
1232 error = got_object_id_str(&commit_id_str, commit_id);
1233 if (error != NULL)
1234 goto done;
1235 } else {
1236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 free(commit_id_str);
1238 commit_id_str = NULL;
1239 if (error)
1240 goto done;
1241 error = got_object_id_str(&commit_id_str, commit_id);
1242 if (error)
1243 goto done;
1246 if (branch_name) {
1247 struct got_object_id *head_commit_id;
1248 TAILQ_FOREACH(pe, &paths, entry) {
1249 if (pe->path_len == 0)
1250 continue;
1251 error = got_error_msg(GOT_ERR_BAD_PATH,
1252 "switching between branches requires that "
1253 "the entire work tree gets updated");
1254 goto done;
1256 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1257 if (error)
1258 goto done;
1259 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 free(head_commit_id);
1261 if (error != NULL)
1262 goto done;
1263 error = check_same_branch(commit_id, head_ref, NULL, repo);
1264 if (error)
1265 goto done;
1266 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1267 if (error)
1268 goto done;
1269 } else {
1270 error = check_linear_ancestry(commit_id,
1271 got_worktree_get_base_commit_id(worktree), repo);
1272 if (error != NULL) {
1273 if (error->code == GOT_ERR_ANCESTRY)
1274 error = got_error(GOT_ERR_BRANCH_MOVED);
1275 goto done;
1277 error = check_same_branch(commit_id, head_ref, NULL, repo);
1278 if (error)
1279 goto done;
1282 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1283 commit_id) != 0) {
1284 error = got_worktree_set_base_commit_id(worktree, repo,
1285 commit_id);
1286 if (error)
1287 goto done;
1290 error = got_worktree_checkout_files(worktree, &paths, repo,
1291 update_progress, &did_something, check_cancelled, NULL);
1292 if (error != NULL)
1293 goto done;
1295 if (did_something)
1296 printf("Updated to commit %s\n", commit_id_str);
1297 else
1298 printf("Already up-to-date\n");
1299 done:
1300 free(worktree_path);
1301 TAILQ_FOREACH(pe, &paths, entry)
1302 free((char *)pe->path);
1303 got_pathlist_free(&paths);
1304 free(commit_id);
1305 free(commit_id_str);
1306 return error;
1309 static const struct got_error *
1310 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 int diff_context, struct got_repository *repo)
1313 const struct got_error *err = NULL;
1314 struct got_tree_object *tree1 = NULL, *tree2;
1315 struct got_object_qid *qid;
1316 char *id_str1 = NULL, *id_str2;
1317 struct got_diff_blob_output_unidiff_arg arg;
1319 err = got_object_open_as_tree(&tree2, repo,
1320 got_object_commit_get_tree_id(commit));
1321 if (err)
1322 return err;
1324 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1325 if (qid != NULL) {
1326 struct got_commit_object *pcommit;
1328 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1329 if (err)
1330 return err;
1332 err = got_object_open_as_tree(&tree1, repo,
1333 got_object_commit_get_tree_id(pcommit));
1334 got_object_commit_close(pcommit);
1335 if (err)
1336 return err;
1338 err = got_object_id_str(&id_str1, qid->id);
1339 if (err)
1340 return err;
1343 err = got_object_id_str(&id_str2, id);
1344 if (err)
1345 goto done;
1347 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 arg.diff_context = diff_context;
1349 arg.outfile = stdout;
1350 err = got_diff_tree(tree1, tree2, "", "", repo,
1351 got_diff_blob_output_unidiff, &arg, 1);
1352 done:
1353 if (tree1)
1354 got_object_tree_close(tree1);
1355 got_object_tree_close(tree2);
1356 free(id_str1);
1357 free(id_str2);
1358 return err;
1361 static char *
1362 get_datestr(time_t *time, char *datebuf)
1364 char *p, *s = ctime_r(time, datebuf);
1365 p = strchr(s, '\n');
1366 if (p)
1367 *p = '\0';
1368 return s;
1371 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1373 static const struct got_error *
1374 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1375 struct got_repository *repo, int show_patch, int diff_context,
1376 struct got_reflist_head *refs)
1378 const struct got_error *err = NULL;
1379 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1380 char datebuf[26];
1381 time_t committer_time;
1382 const char *author, *committer;
1383 char *refs_str = NULL;
1384 struct got_reflist_entry *re;
1386 SIMPLEQ_FOREACH(re, refs, entry) {
1387 char *s;
1388 const char *name;
1389 if (got_object_id_cmp(re->id, id) != 0)
1390 continue;
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1393 continue;
1394 if (strncmp(name, "refs/", 5) == 0)
1395 name += 5;
1396 if (strncmp(name, "got/", 4) == 0)
1397 continue;
1398 if (strncmp(name, "heads/", 6) == 0)
1399 name += 6;
1400 if (strncmp(name, "remotes/", 8) == 0)
1401 name += 8;
1402 s = refs_str;
1403 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1404 name) == -1) {
1405 err = got_error_from_errno("asprintf");
1406 free(s);
1407 break;
1409 free(s);
1411 err = got_object_id_str(&id_str, id);
1412 if (err)
1413 return err;
1415 printf(GOT_COMMIT_SEP_STR);
1416 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1417 refs_str ? refs_str : "", refs_str ? ")" : "");
1418 free(id_str);
1419 id_str = NULL;
1420 free(refs_str);
1421 refs_str = NULL;
1422 printf("from: %s\n", got_object_commit_get_author(commit));
1423 committer_time = got_object_commit_get_committer_time(commit);
1424 datestr = get_datestr(&committer_time, datebuf);
1425 printf("date: %s UTC\n", datestr);
1426 author = got_object_commit_get_author(commit);
1427 committer = got_object_commit_get_committer(commit);
1428 if (strcmp(author, committer) != 0)
1429 printf("via: %s\n", committer);
1430 if (got_object_commit_get_nparents(commit) > 1) {
1431 const struct got_object_id_queue *parent_ids;
1432 struct got_object_qid *qid;
1433 int n = 1;
1434 parent_ids = got_object_commit_get_parent_ids(commit);
1435 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1436 err = got_object_id_str(&id_str, qid->id);
1437 if (err)
1438 return err;
1439 printf("parent %d: %s\n", n++, id_str);
1440 free(id_str);
1444 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1445 if (logmsg0 == NULL)
1446 return got_error_from_errno("strdup");
1448 logmsg = logmsg0;
1449 do {
1450 line = strsep(&logmsg, "\n");
1451 if (line)
1452 printf(" %s\n", line);
1453 } while (line);
1454 free(logmsg0);
1456 if (show_patch) {
1457 err = print_patch(commit, id, diff_context, repo);
1458 if (err == 0)
1459 printf("\n");
1462 if (fflush(stdout) != 0 && err == NULL)
1463 err = got_error_from_errno("fflush");
1464 return err;
1467 static const struct got_error *
1468 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1469 char *path, int show_patch, int diff_context, int limit,
1470 int first_parent_traversal, struct got_reflist_head *refs)
1472 const struct got_error *err;
1473 struct got_commit_graph *graph;
1475 err = got_commit_graph_open(&graph, root_id, path,
1476 first_parent_traversal, repo);
1477 if (err)
1478 return err;
1479 err = got_commit_graph_iter_start(graph, root_id, repo);
1480 if (err)
1481 goto done;
1482 for (;;) {
1483 struct got_commit_object *commit;
1484 struct got_object_id *id;
1486 if (sigint_received || sigpipe_received)
1487 break;
1489 err = got_commit_graph_iter_next(&id, graph);
1490 if (err) {
1491 if (err->code == GOT_ERR_ITER_COMPLETED) {
1492 err = NULL;
1493 break;
1495 if (err->code != GOT_ERR_ITER_NEED_MORE)
1496 break;
1497 err = got_commit_graph_fetch_commits(graph, 1, repo);
1498 if (err)
1499 break;
1500 else
1501 continue;
1503 if (id == NULL)
1504 break;
1506 err = got_object_open_as_commit(&commit, repo, id);
1507 if (err)
1508 break;
1509 err = print_commit(commit, id, repo, show_patch, diff_context,
1510 refs);
1511 got_object_commit_close(commit);
1512 if (err || (limit && --limit == 0))
1513 break;
1515 done:
1516 got_commit_graph_close(graph);
1517 return err;
1520 __dead static void
1521 usage_log(void)
1523 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1524 "[-r repository-path] [path]\n", getprogname());
1525 exit(1);
1528 static const struct got_error *
1529 cmd_log(int argc, char *argv[])
1531 const struct got_error *error;
1532 struct got_repository *repo = NULL;
1533 struct got_worktree *worktree = NULL;
1534 struct got_commit_object *commit = NULL;
1535 struct got_object_id *id = NULL;
1536 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1537 char *start_commit = NULL;
1538 int diff_context = 3, ch;
1539 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1540 const char *errstr;
1541 struct got_reflist_head refs;
1543 SIMPLEQ_INIT(&refs);
1545 #ifndef PROFILE
1546 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1547 NULL)
1548 == -1)
1549 err(1, "pledge");
1550 #endif
1552 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1553 switch (ch) {
1554 case 'p':
1555 show_patch = 1;
1556 break;
1557 case 'c':
1558 start_commit = optarg;
1559 break;
1560 case 'C':
1561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1562 &errstr);
1563 if (errstr != NULL)
1564 err(1, "-C option %s", errstr);
1565 break;
1566 case 'l':
1567 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1568 if (errstr != NULL)
1569 err(1, "-l option %s", errstr);
1570 break;
1571 case 'f':
1572 first_parent_traversal = 1;
1573 break;
1574 case 'r':
1575 repo_path = realpath(optarg, NULL);
1576 if (repo_path == NULL)
1577 err(1, "-r option");
1578 got_path_strip_trailing_slashes(repo_path);
1579 break;
1580 default:
1581 usage_log();
1582 /* NOTREACHED */
1586 argc -= optind;
1587 argv += optind;
1589 cwd = getcwd(NULL, 0);
1590 if (cwd == NULL) {
1591 error = got_error_from_errno("getcwd");
1592 goto done;
1595 error = got_worktree_open(&worktree, cwd);
1596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1597 goto done;
1598 error = NULL;
1600 if (argc == 0) {
1601 path = strdup("");
1602 if (path == NULL) {
1603 error = got_error_from_errno("strdup");
1604 goto done;
1606 } else if (argc == 1) {
1607 if (worktree) {
1608 error = got_worktree_resolve_path(&path, worktree,
1609 argv[0]);
1610 if (error)
1611 goto done;
1612 } else {
1613 path = strdup(argv[0]);
1614 if (path == NULL) {
1615 error = got_error_from_errno("strdup");
1616 goto done;
1619 } else
1620 usage_log();
1622 if (repo_path == NULL) {
1623 repo_path = worktree ?
1624 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1626 if (repo_path == NULL) {
1627 error = got_error_from_errno("strdup");
1628 goto done;
1631 error = got_repo_open(&repo, repo_path);
1632 if (error != NULL)
1633 goto done;
1635 error = apply_unveil(got_repo_get_path(repo), 1,
1636 worktree ? got_worktree_get_root_path(worktree) : NULL);
1637 if (error)
1638 goto done;
1640 if (start_commit == NULL) {
1641 struct got_reference *head_ref;
1642 error = got_ref_open(&head_ref, repo,
1643 worktree ? got_worktree_get_head_ref_name(worktree)
1644 : GOT_REF_HEAD, 0);
1645 if (error != NULL)
1646 return error;
1647 error = got_ref_resolve(&id, repo, head_ref);
1648 got_ref_close(head_ref);
1649 if (error != NULL)
1650 return error;
1651 error = got_object_open_as_commit(&commit, repo, id);
1652 } else {
1653 struct got_reference *ref;
1654 error = got_ref_open(&ref, repo, start_commit, 0);
1655 if (error == NULL) {
1656 int obj_type;
1657 error = got_ref_resolve(&id, repo, ref);
1658 got_ref_close(ref);
1659 if (error != NULL)
1660 goto done;
1661 error = got_object_get_type(&obj_type, repo, id);
1662 if (error != NULL)
1663 goto done;
1664 if (obj_type == GOT_OBJ_TYPE_TAG) {
1665 struct got_tag_object *tag;
1666 error = got_object_open_as_tag(&tag, repo, id);
1667 if (error != NULL)
1668 goto done;
1669 if (got_object_tag_get_object_type(tag) !=
1670 GOT_OBJ_TYPE_COMMIT) {
1671 got_object_tag_close(tag);
1672 error = got_error(GOT_ERR_OBJ_TYPE);
1673 goto done;
1675 free(id);
1676 id = got_object_id_dup(
1677 got_object_tag_get_object_id(tag));
1678 if (id == NULL)
1679 error = got_error_from_errno(
1680 "got_object_id_dup");
1681 got_object_tag_close(tag);
1682 if (error)
1683 goto done;
1684 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1685 error = got_error(GOT_ERR_OBJ_TYPE);
1686 goto done;
1688 error = got_object_open_as_commit(&commit, repo, id);
1689 if (error != NULL)
1690 goto done;
1692 if (commit == NULL) {
1693 error = got_repo_match_object_id_prefix(&id,
1694 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1695 if (error != NULL)
1696 return error;
1699 if (error != NULL)
1700 goto done;
1702 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1703 if (error != NULL)
1704 goto done;
1705 if (in_repo_path) {
1706 free(path);
1707 path = in_repo_path;
1710 error = got_ref_list(&refs, repo);
1711 if (error)
1712 goto done;
1714 error = print_commits(id, repo, path, show_patch,
1715 diff_context, limit, first_parent_traversal, &refs);
1716 done:
1717 free(path);
1718 free(repo_path);
1719 free(cwd);
1720 free(id);
1721 if (worktree)
1722 got_worktree_close(worktree);
1723 if (repo) {
1724 const struct got_error *repo_error;
1725 repo_error = got_repo_close(repo);
1726 if (error == NULL)
1727 error = repo_error;
1729 got_ref_list_free(&refs);
1730 return error;
1733 __dead static void
1734 usage_diff(void)
1736 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1737 "[object1 object2 | path]\n", getprogname());
1738 exit(1);
1741 struct print_diff_arg {
1742 struct got_repository *repo;
1743 struct got_worktree *worktree;
1744 int diff_context;
1745 const char *id_str;
1746 int header_shown;
1747 int diff_staged;
1750 static const struct got_error *
1751 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1752 const char *path, struct got_object_id *blob_id,
1753 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1755 struct print_diff_arg *a = arg;
1756 const struct got_error *err = NULL;
1757 struct got_blob_object *blob1 = NULL;
1758 FILE *f2 = NULL;
1759 char *abspath = NULL, *label1 = NULL;
1760 struct stat sb;
1762 if (a->diff_staged) {
1763 if (staged_status != GOT_STATUS_MODIFY &&
1764 staged_status != GOT_STATUS_ADD &&
1765 staged_status != GOT_STATUS_DELETE)
1766 return NULL;
1767 } else {
1768 if (staged_status == GOT_STATUS_DELETE)
1769 return NULL;
1770 if (status != GOT_STATUS_MODIFY &&
1771 status != GOT_STATUS_ADD &&
1772 status != GOT_STATUS_DELETE &&
1773 status != GOT_STATUS_CONFLICT)
1774 return NULL;
1777 if (!a->header_shown) {
1778 printf("diff %s %s%s\n", a->id_str,
1779 got_worktree_get_root_path(a->worktree),
1780 a->diff_staged ? " (staged changes)" : "");
1781 a->header_shown = 1;
1784 if (a->diff_staged) {
1785 const char *label1 = NULL, *label2 = NULL;
1786 switch (staged_status) {
1787 case GOT_STATUS_MODIFY:
1788 label1 = path;
1789 label2 = path;
1790 break;
1791 case GOT_STATUS_ADD:
1792 label2 = path;
1793 break;
1794 case GOT_STATUS_DELETE:
1795 label1 = path;
1796 break;
1797 default:
1798 return got_error(GOT_ERR_FILE_STATUS);
1800 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1801 label1, label2, a->diff_context, a->repo, stdout);
1804 if (staged_status == GOT_STATUS_ADD ||
1805 staged_status == GOT_STATUS_MODIFY) {
1806 char *id_str;
1807 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1808 8192);
1809 if (err)
1810 goto done;
1811 err = got_object_id_str(&id_str, staged_blob_id);
1812 if (err)
1813 goto done;
1814 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1816 free(id_str);
1817 goto done;
1819 free(id_str);
1820 } else if (status != GOT_STATUS_ADD) {
1821 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1822 if (err)
1823 goto done;
1826 if (status != GOT_STATUS_DELETE) {
1827 if (asprintf(&abspath, "%s/%s",
1828 got_worktree_get_root_path(a->worktree), path) == -1) {
1829 err = got_error_from_errno("asprintf");
1830 goto done;
1833 f2 = fopen(abspath, "r");
1834 if (f2 == NULL) {
1835 err = got_error_from_errno2("fopen", abspath);
1836 goto done;
1838 if (lstat(abspath, &sb) == -1) {
1839 err = got_error_from_errno2("lstat", abspath);
1840 goto done;
1842 } else
1843 sb.st_size = 0;
1845 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1846 a->diff_context, stdout);
1847 done:
1848 if (blob1)
1849 got_object_blob_close(blob1);
1850 if (f2 && fclose(f2) != 0 && err == NULL)
1851 err = got_error_from_errno("fclose");
1852 free(abspath);
1853 return err;
1856 static const struct got_error *
1857 match_object_id(struct got_object_id **id, char **label,
1858 const char *id_str, int obj_type, struct got_repository *repo)
1860 const struct got_error *err;
1861 struct got_tag_object *tag;
1862 struct got_reference *ref = NULL;
1864 *id = NULL;
1865 *label = NULL;
1867 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1868 if (err == NULL) {
1869 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1870 if (*id == NULL)
1871 err = got_error_from_errno("got_object_id_dup");
1872 if (asprintf(label, "refs/tags/%s",
1873 got_object_tag_get_name(tag)) == -1)
1874 err = got_error_from_errno("asprintf");
1875 got_object_tag_close(tag);
1876 return err;
1877 } else if (err->code != GOT_ERR_NO_OBJ)
1878 return err;
1880 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1881 if (err) {
1882 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1883 return err;
1884 err = got_ref_open(&ref, repo, id_str, 0);
1885 if (err != NULL)
1886 goto done;
1887 *label = strdup(got_ref_get_name(ref));
1888 if (*label == NULL) {
1889 err = got_error_from_errno("strdup");
1890 goto done;
1892 err = got_ref_resolve(id, repo, ref);
1893 } else {
1894 err = got_object_id_str(label, *id);
1895 if (*label == NULL) {
1896 err = got_error_from_errno("strdup");
1897 goto done;
1900 done:
1901 if (ref)
1902 got_ref_close(ref);
1903 return err;
1907 static const struct got_error *
1908 cmd_diff(int argc, char *argv[])
1910 const struct got_error *error;
1911 struct got_repository *repo = NULL;
1912 struct got_worktree *worktree = NULL;
1913 char *cwd = NULL, *repo_path = NULL;
1914 struct got_object_id *id1 = NULL, *id2 = NULL;
1915 const char *id_str1 = NULL, *id_str2 = NULL;
1916 char *label1 = NULL, *label2 = NULL;
1917 int type1, type2;
1918 int diff_context = 3, diff_staged = 0, ch;
1919 const char *errstr;
1920 char *path = NULL;
1922 #ifndef PROFILE
1923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1924 NULL) == -1)
1925 err(1, "pledge");
1926 #endif
1928 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1929 switch (ch) {
1930 case 'C':
1931 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1932 if (errstr != NULL)
1933 err(1, "-C option %s", errstr);
1934 break;
1935 case 'r':
1936 repo_path = realpath(optarg, NULL);
1937 if (repo_path == NULL)
1938 err(1, "-r option");
1939 got_path_strip_trailing_slashes(repo_path);
1940 break;
1941 case 's':
1942 diff_staged = 1;
1943 break;
1944 default:
1945 usage_diff();
1946 /* NOTREACHED */
1950 argc -= optind;
1951 argv += optind;
1953 cwd = getcwd(NULL, 0);
1954 if (cwd == NULL) {
1955 error = got_error_from_errno("getcwd");
1956 goto done;
1958 error = got_worktree_open(&worktree, cwd);
1959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1960 goto done;
1961 if (argc <= 1) {
1962 if (worktree == NULL) {
1963 error = got_error(GOT_ERR_NOT_WORKTREE);
1964 goto done;
1966 if (repo_path)
1967 errx(1,
1968 "-r option can't be used when diffing a work tree");
1969 repo_path = strdup(got_worktree_get_repo_path(worktree));
1970 if (repo_path == NULL) {
1971 error = got_error_from_errno("strdup");
1972 goto done;
1974 if (argc == 1) {
1975 error = got_worktree_resolve_path(&path, worktree,
1976 argv[0]);
1977 if (error)
1978 goto done;
1979 } else {
1980 path = strdup("");
1981 if (path == NULL) {
1982 error = got_error_from_errno("strdup");
1983 goto done;
1986 } else if (argc == 2) {
1987 if (diff_staged)
1988 errx(1, "-s option can't be used when diffing "
1989 "objects in repository");
1990 id_str1 = argv[0];
1991 id_str2 = argv[1];
1992 if (worktree && repo_path == NULL) {
1993 repo_path =
1994 strdup(got_worktree_get_repo_path(worktree));
1995 if (repo_path == NULL) {
1996 error = got_error_from_errno("strdup");
1997 goto done;
2000 } else
2001 usage_diff();
2003 if (repo_path == NULL) {
2004 repo_path = getcwd(NULL, 0);
2005 if (repo_path == NULL)
2006 return got_error_from_errno("getcwd");
2009 error = got_repo_open(&repo, repo_path);
2010 free(repo_path);
2011 if (error != NULL)
2012 goto done;
2014 error = apply_unveil(got_repo_get_path(repo), 1,
2015 worktree ? got_worktree_get_root_path(worktree) : NULL);
2016 if (error)
2017 goto done;
2019 if (argc <= 1) {
2020 struct print_diff_arg arg;
2021 struct got_pathlist_head paths;
2022 char *id_str;
2024 TAILQ_INIT(&paths);
2026 error = got_object_id_str(&id_str,
2027 got_worktree_get_base_commit_id(worktree));
2028 if (error)
2029 goto done;
2030 arg.repo = repo;
2031 arg.worktree = worktree;
2032 arg.diff_context = diff_context;
2033 arg.id_str = id_str;
2034 arg.header_shown = 0;
2035 arg.diff_staged = diff_staged;
2037 error = got_pathlist_append(&paths, path, NULL);
2038 if (error)
2039 goto done;
2041 error = got_worktree_status(worktree, &paths, repo, print_diff,
2042 &arg, check_cancelled, NULL);
2043 free(id_str);
2044 got_pathlist_free(&paths);
2045 goto done;
2048 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2049 if (error)
2050 goto done;
2052 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2053 if (error)
2054 goto done;
2056 error = got_object_get_type(&type1, repo, id1);
2057 if (error)
2058 goto done;
2060 error = got_object_get_type(&type2, repo, id2);
2061 if (error)
2062 goto done;
2064 if (type1 != type2) {
2065 error = got_error(GOT_ERR_OBJ_TYPE);
2066 goto done;
2069 switch (type1) {
2070 case GOT_OBJ_TYPE_BLOB:
2071 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2072 diff_context, repo, stdout);
2073 break;
2074 case GOT_OBJ_TYPE_TREE:
2075 error = got_diff_objects_as_trees(id1, id2, "", "",
2076 diff_context, repo, stdout);
2077 break;
2078 case GOT_OBJ_TYPE_COMMIT:
2079 printf("diff %s %s\n", label1, label2);
2080 error = got_diff_objects_as_commits(id1, id2, diff_context,
2081 repo, stdout);
2082 break;
2083 default:
2084 error = got_error(GOT_ERR_OBJ_TYPE);
2087 done:
2088 free(label1);
2089 free(label2);
2090 free(id1);
2091 free(id2);
2092 free(path);
2093 if (worktree)
2094 got_worktree_close(worktree);
2095 if (repo) {
2096 const struct got_error *repo_error;
2097 repo_error = got_repo_close(repo);
2098 if (error == NULL)
2099 error = repo_error;
2101 return error;
2104 __dead static void
2105 usage_blame(void)
2107 fprintf(stderr,
2108 "usage: %s blame [-c commit] [-r repository-path] path\n",
2109 getprogname());
2110 exit(1);
2113 static const struct got_error *
2114 cmd_blame(int argc, char *argv[])
2116 const struct got_error *error;
2117 struct got_repository *repo = NULL;
2118 struct got_worktree *worktree = NULL;
2119 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2120 struct got_object_id *commit_id = NULL;
2121 char *commit_id_str = NULL;
2122 int ch;
2124 #ifndef PROFILE
2125 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2126 NULL) == -1)
2127 err(1, "pledge");
2128 #endif
2130 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2131 switch (ch) {
2132 case 'c':
2133 commit_id_str = optarg;
2134 break;
2135 case 'r':
2136 repo_path = realpath(optarg, NULL);
2137 if (repo_path == NULL)
2138 err(1, "-r option");
2139 got_path_strip_trailing_slashes(repo_path);
2140 break;
2141 default:
2142 usage_blame();
2143 /* NOTREACHED */
2147 argc -= optind;
2148 argv += optind;
2150 if (argc == 1)
2151 path = argv[0];
2152 else
2153 usage_blame();
2155 cwd = getcwd(NULL, 0);
2156 if (cwd == NULL) {
2157 error = got_error_from_errno("getcwd");
2158 goto done;
2160 if (repo_path == NULL) {
2161 error = got_worktree_open(&worktree, cwd);
2162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2163 goto done;
2164 else
2165 error = NULL;
2166 if (worktree) {
2167 repo_path =
2168 strdup(got_worktree_get_repo_path(worktree));
2169 if (repo_path == NULL)
2170 error = got_error_from_errno("strdup");
2171 if (error)
2172 goto done;
2173 } else {
2174 repo_path = strdup(cwd);
2175 if (repo_path == NULL) {
2176 error = got_error_from_errno("strdup");
2177 goto done;
2182 error = got_repo_open(&repo, repo_path);
2183 if (error != NULL)
2184 goto done;
2186 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2187 if (error)
2188 goto done;
2190 if (worktree) {
2191 const char *prefix = got_worktree_get_path_prefix(worktree);
2192 char *p, *worktree_subdir = cwd +
2193 strlen(got_worktree_get_root_path(worktree));
2194 if (asprintf(&p, "%s%s%s%s%s",
2195 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2196 worktree_subdir, worktree_subdir[0] ? "/" : "",
2197 path) == -1) {
2198 error = got_error_from_errno("asprintf");
2199 goto done;
2201 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2202 free(p);
2203 } else {
2204 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2206 if (error)
2207 goto done;
2209 if (commit_id_str == NULL) {
2210 struct got_reference *head_ref;
2211 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2212 if (error != NULL)
2213 goto done;
2214 error = got_ref_resolve(&commit_id, repo, head_ref);
2215 got_ref_close(head_ref);
2216 if (error != NULL)
2217 goto done;
2218 } else {
2219 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2220 if (error)
2221 goto done;
2224 error = got_blame(in_repo_path, commit_id, repo, stdout);
2225 done:
2226 free(in_repo_path);
2227 free(repo_path);
2228 free(cwd);
2229 free(commit_id);
2230 if (worktree)
2231 got_worktree_close(worktree);
2232 if (repo) {
2233 const struct got_error *repo_error;
2234 repo_error = got_repo_close(repo);
2235 if (error == NULL)
2236 error = repo_error;
2238 return error;
2241 __dead static void
2242 usage_tree(void)
2244 fprintf(stderr,
2245 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2246 getprogname());
2247 exit(1);
2250 static void
2251 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2252 const char *root_path)
2254 int is_root_path = (strcmp(path, root_path) == 0);
2255 const char *modestr = "";
2257 path += strlen(root_path);
2258 while (path[0] == '/')
2259 path++;
2261 if (S_ISLNK(te->mode))
2262 modestr = "@";
2263 else if (S_ISDIR(te->mode))
2264 modestr = "/";
2265 else if (te->mode & S_IXUSR)
2266 modestr = "*";
2268 printf("%s%s%s%s%s\n", id ? id : "", path,
2269 is_root_path ? "" : "/", te->name, modestr);
2272 static const struct got_error *
2273 print_tree(const char *path, struct got_object_id *commit_id,
2274 int show_ids, int recurse, const char *root_path,
2275 struct got_repository *repo)
2277 const struct got_error *err = NULL;
2278 struct got_object_id *tree_id = NULL;
2279 struct got_tree_object *tree = NULL;
2280 const struct got_tree_entries *entries;
2281 struct got_tree_entry *te;
2283 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2284 if (err)
2285 goto done;
2287 err = got_object_open_as_tree(&tree, repo, tree_id);
2288 if (err)
2289 goto done;
2290 entries = got_object_tree_get_entries(tree);
2291 te = SIMPLEQ_FIRST(&entries->head);
2292 while (te) {
2293 char *id = NULL;
2295 if (sigint_received || sigpipe_received)
2296 break;
2298 if (show_ids) {
2299 char *id_str;
2300 err = got_object_id_str(&id_str, te->id);
2301 if (err)
2302 goto done;
2303 if (asprintf(&id, "%s ", id_str) == -1) {
2304 err = got_error_from_errno("asprintf");
2305 free(id_str);
2306 goto done;
2308 free(id_str);
2310 print_entry(te, id, path, root_path);
2311 free(id);
2313 if (recurse && S_ISDIR(te->mode)) {
2314 char *child_path;
2315 if (asprintf(&child_path, "%s%s%s", path,
2316 path[0] == '/' && path[1] == '\0' ? "" : "/",
2317 te->name) == -1) {
2318 err = got_error_from_errno("asprintf");
2319 goto done;
2321 err = print_tree(child_path, commit_id, show_ids, 1,
2322 root_path, repo);
2323 free(child_path);
2324 if (err)
2325 goto done;
2328 te = SIMPLEQ_NEXT(te, entry);
2330 done:
2331 if (tree)
2332 got_object_tree_close(tree);
2333 free(tree_id);
2334 return err;
2337 static const struct got_error *
2338 cmd_tree(int argc, char *argv[])
2340 const struct got_error *error;
2341 struct got_repository *repo = NULL;
2342 struct got_worktree *worktree = NULL;
2343 const char *path;
2344 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2345 struct got_object_id *commit_id = NULL;
2346 char *commit_id_str = NULL;
2347 int show_ids = 0, recurse = 0;
2348 int ch;
2350 #ifndef PROFILE
2351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2352 NULL) == -1)
2353 err(1, "pledge");
2354 #endif
2356 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2357 switch (ch) {
2358 case 'c':
2359 commit_id_str = optarg;
2360 break;
2361 case 'r':
2362 repo_path = realpath(optarg, NULL);
2363 if (repo_path == NULL)
2364 err(1, "-r option");
2365 got_path_strip_trailing_slashes(repo_path);
2366 break;
2367 case 'i':
2368 show_ids = 1;
2369 break;
2370 case 'R':
2371 recurse = 1;
2372 break;
2373 default:
2374 usage_tree();
2375 /* NOTREACHED */
2379 argc -= optind;
2380 argv += optind;
2382 if (argc == 1)
2383 path = argv[0];
2384 else if (argc > 1)
2385 usage_tree();
2386 else
2387 path = NULL;
2389 cwd = getcwd(NULL, 0);
2390 if (cwd == NULL) {
2391 error = got_error_from_errno("getcwd");
2392 goto done;
2394 if (repo_path == NULL) {
2395 error = got_worktree_open(&worktree, cwd);
2396 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2397 goto done;
2398 else
2399 error = NULL;
2400 if (worktree) {
2401 repo_path =
2402 strdup(got_worktree_get_repo_path(worktree));
2403 if (repo_path == NULL)
2404 error = got_error_from_errno("strdup");
2405 if (error)
2406 goto done;
2407 } else {
2408 repo_path = strdup(cwd);
2409 if (repo_path == NULL) {
2410 error = got_error_from_errno("strdup");
2411 goto done;
2416 error = got_repo_open(&repo, repo_path);
2417 if (error != NULL)
2418 goto done;
2420 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2421 if (error)
2422 goto done;
2424 if (path == NULL) {
2425 if (worktree) {
2426 char *p, *worktree_subdir = cwd +
2427 strlen(got_worktree_get_root_path(worktree));
2428 if (asprintf(&p, "%s/%s",
2429 got_worktree_get_path_prefix(worktree),
2430 worktree_subdir) == -1) {
2431 error = got_error_from_errno("asprintf");
2432 goto done;
2434 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2435 free(p);
2436 if (error)
2437 goto done;
2438 } else
2439 path = "/";
2441 if (in_repo_path == NULL) {
2442 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2443 if (error != NULL)
2444 goto done;
2447 if (commit_id_str == NULL) {
2448 struct got_reference *head_ref;
2449 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2450 if (error != NULL)
2451 goto done;
2452 error = got_ref_resolve(&commit_id, repo, head_ref);
2453 got_ref_close(head_ref);
2454 if (error != NULL)
2455 goto done;
2456 } else {
2457 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2458 if (error)
2459 goto done;
2462 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2463 in_repo_path, repo);
2464 done:
2465 free(in_repo_path);
2466 free(repo_path);
2467 free(cwd);
2468 free(commit_id);
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 return error;
2480 __dead static void
2481 usage_status(void)
2483 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2484 exit(1);
2487 static const struct got_error *
2488 print_status(void *arg, unsigned char status, unsigned char staged_status,
2489 const char *path, struct got_object_id *blob_id,
2490 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2492 if (status == staged_status && (status == GOT_STATUS_DELETE))
2493 status = GOT_STATUS_NO_CHANGE;
2494 printf("%c%c %s\n", status, staged_status, path);
2495 return NULL;
2498 static const struct got_error *
2499 cmd_status(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 char *cwd = NULL;
2505 struct got_pathlist_head paths;
2506 struct got_pathlist_entry *pe;
2507 int ch;
2509 TAILQ_INIT(&paths);
2511 while ((ch = getopt(argc, argv, "")) != -1) {
2512 switch (ch) {
2513 default:
2514 usage_status();
2515 /* NOTREACHED */
2519 argc -= optind;
2520 argv += optind;
2522 #ifndef PROFILE
2523 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2524 NULL) == -1)
2525 err(1, "pledge");
2526 #endif
2527 cwd = getcwd(NULL, 0);
2528 if (cwd == NULL) {
2529 error = got_error_from_errno("getcwd");
2530 goto done;
2533 error = got_worktree_open(&worktree, cwd);
2534 if (error != NULL)
2535 goto done;
2537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2538 if (error != NULL)
2539 goto done;
2541 error = apply_unveil(got_repo_get_path(repo), 1,
2542 got_worktree_get_root_path(worktree));
2543 if (error)
2544 goto done;
2546 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2547 if (error)
2548 goto done;
2550 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2551 check_cancelled, NULL);
2552 done:
2553 TAILQ_FOREACH(pe, &paths, entry)
2554 free((char *)pe->path);
2555 got_pathlist_free(&paths);
2556 free(cwd);
2557 return error;
2560 __dead static void
2561 usage_ref(void)
2563 fprintf(stderr,
2564 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2565 getprogname());
2566 exit(1);
2569 static const struct got_error *
2570 list_refs(struct got_repository *repo)
2572 static const struct got_error *err = NULL;
2573 struct got_reflist_head refs;
2574 struct got_reflist_entry *re;
2576 SIMPLEQ_INIT(&refs);
2577 err = got_ref_list(&refs, repo);
2578 if (err)
2579 return err;
2581 SIMPLEQ_FOREACH(re, &refs, entry) {
2582 char *refstr;
2583 refstr = got_ref_to_str(re->ref);
2584 if (refstr == NULL)
2585 return got_error_from_errno("got_ref_to_str");
2586 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2587 free(refstr);
2590 got_ref_list_free(&refs);
2591 return NULL;
2594 static const struct got_error *
2595 delete_ref(struct got_repository *repo, const char *refname)
2597 const struct got_error *err = NULL;
2598 struct got_reference *ref;
2600 err = got_ref_open(&ref, repo, refname, 0);
2601 if (err)
2602 return err;
2604 err = got_ref_delete(ref, repo);
2605 got_ref_close(ref);
2606 return err;
2609 static const struct got_error *
2610 add_ref(struct got_repository *repo, const char *refname, const char *target)
2612 const struct got_error *err = NULL;
2613 struct got_object_id *id;
2614 struct got_reference *ref = NULL;
2617 * Don't let the user create a reference named '-'.
2618 * While technically a valid reference name, this case is usually
2619 * an unintended typo.
2621 if (refname[0] == '-' && refname[1] == '\0')
2622 return got_error(GOT_ERR_BAD_REF_NAME);
2624 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2625 repo);
2626 if (err) {
2627 struct got_reference *target_ref;
2629 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2630 return err;
2631 err = got_ref_open(&target_ref, repo, target, 0);
2632 if (err)
2633 return err;
2634 err = got_ref_resolve(&id, repo, target_ref);
2635 got_ref_close(target_ref);
2636 if (err)
2637 return err;
2640 err = got_ref_alloc(&ref, refname, id);
2641 if (err)
2642 goto done;
2644 err = got_ref_write(ref, repo);
2645 done:
2646 if (ref)
2647 got_ref_close(ref);
2648 free(id);
2649 return err;
2652 static const struct got_error *
2653 add_symref(struct got_repository *repo, const char *refname, const char *target)
2655 const struct got_error *err = NULL;
2656 struct got_reference *ref = NULL;
2657 struct got_reference *target_ref = NULL;
2660 * Don't let the user create a reference named '-'.
2661 * While technically a valid reference name, this case is usually
2662 * an unintended typo.
2664 if (refname[0] == '-' && refname[1] == '\0')
2665 return got_error(GOT_ERR_BAD_REF_NAME);
2667 err = got_ref_open(&target_ref, repo, target, 0);
2668 if (err)
2669 return err;
2671 err = got_ref_alloc_symref(&ref, refname, target_ref);
2672 if (err)
2673 goto done;
2675 err = got_ref_write(ref, repo);
2676 done:
2677 if (target_ref)
2678 got_ref_close(target_ref);
2679 if (ref)
2680 got_ref_close(ref);
2681 return err;
2684 static const struct got_error *
2685 cmd_ref(int argc, char *argv[])
2687 const struct got_error *error = NULL;
2688 struct got_repository *repo = NULL;
2689 struct got_worktree *worktree = NULL;
2690 char *cwd = NULL, *repo_path = NULL;
2691 int ch, do_list = 0, create_symref = 0;
2692 const char *delref = NULL;
2694 /* TODO: Add -s option for adding symbolic references. */
2695 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2696 switch (ch) {
2697 case 'd':
2698 delref = optarg;
2699 break;
2700 case 'r':
2701 repo_path = realpath(optarg, NULL);
2702 if (repo_path == NULL)
2703 err(1, "-r option");
2704 got_path_strip_trailing_slashes(repo_path);
2705 break;
2706 case 'l':
2707 do_list = 1;
2708 break;
2709 case 's':
2710 create_symref = 1;
2711 break;
2712 default:
2713 usage_ref();
2714 /* NOTREACHED */
2718 if (do_list && delref)
2719 errx(1, "-l and -d options are mutually exclusive\n");
2721 argc -= optind;
2722 argv += optind;
2724 if (do_list || delref) {
2725 if (create_symref)
2726 errx(1, "-s option cannot be used together with the "
2727 "-l or -d options");
2728 if (argc > 0)
2729 usage_ref();
2730 } else if (argc != 2)
2731 usage_ref();
2733 #ifndef PROFILE
2734 if (do_list) {
2735 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2736 NULL) == -1)
2737 err(1, "pledge");
2738 } else {
2739 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2740 "sendfd unveil", NULL) == -1)
2741 err(1, "pledge");
2743 #endif
2744 cwd = getcwd(NULL, 0);
2745 if (cwd == NULL) {
2746 error = got_error_from_errno("getcwd");
2747 goto done;
2750 if (repo_path == NULL) {
2751 error = got_worktree_open(&worktree, cwd);
2752 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2753 goto done;
2754 else
2755 error = NULL;
2756 if (worktree) {
2757 repo_path =
2758 strdup(got_worktree_get_repo_path(worktree));
2759 if (repo_path == NULL)
2760 error = got_error_from_errno("strdup");
2761 if (error)
2762 goto done;
2763 } else {
2764 repo_path = strdup(cwd);
2765 if (repo_path == NULL) {
2766 error = got_error_from_errno("strdup");
2767 goto done;
2772 error = got_repo_open(&repo, repo_path);
2773 if (error != NULL)
2774 goto done;
2776 error = apply_unveil(got_repo_get_path(repo), do_list,
2777 worktree ? got_worktree_get_root_path(worktree) : NULL);
2778 if (error)
2779 goto done;
2781 if (do_list)
2782 error = list_refs(repo);
2783 else if (delref)
2784 error = delete_ref(repo, delref);
2785 else if (create_symref)
2786 error = add_symref(repo, argv[0], argv[1]);
2787 else
2788 error = add_ref(repo, argv[0], argv[1]);
2789 done:
2790 if (repo)
2791 got_repo_close(repo);
2792 if (worktree)
2793 got_worktree_close(worktree);
2794 free(cwd);
2795 free(repo_path);
2796 return error;
2799 __dead static void
2800 usage_branch(void)
2802 fprintf(stderr,
2803 "usage: %s branch [-r repository] -l | -d name | "
2804 "name [base-branch]\n", getprogname());
2805 exit(1);
2808 static const struct got_error *
2809 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2811 static const struct got_error *err = NULL;
2812 struct got_reflist_head refs;
2813 struct got_reflist_entry *re;
2815 SIMPLEQ_INIT(&refs);
2817 err = got_ref_list(&refs, repo);
2818 if (err)
2819 return err;
2821 SIMPLEQ_FOREACH(re, &refs, entry) {
2822 const char *refname, *marker = " ";
2823 char *refstr;
2824 refname = got_ref_get_name(re->ref);
2825 if (strncmp(refname, "refs/heads/", 11) != 0)
2826 continue;
2827 if (worktree && strcmp(refname,
2828 got_worktree_get_head_ref_name(worktree)) == 0) {
2829 struct got_object_id *id = NULL;
2830 err = got_ref_resolve(&id, repo, re->ref);
2831 if (err)
2832 return err;
2833 if (got_object_id_cmp(id,
2834 got_worktree_get_base_commit_id(worktree)) == 0)
2835 marker = "* ";
2836 else
2837 marker = "~ ";
2838 free(id);
2840 refname += 11;
2841 refstr = got_ref_to_str(re->ref);
2842 if (refstr == NULL)
2843 return got_error_from_errno("got_ref_to_str");
2844 printf("%s%s: %s\n", marker, refname, refstr);
2845 free(refstr);
2848 got_ref_list_free(&refs);
2849 return NULL;
2852 static const struct got_error *
2853 delete_branch(struct got_repository *repo, const char *branch_name)
2855 const struct got_error *err = NULL;
2856 struct got_reference *ref;
2857 char *refname;
2859 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2860 return got_error_from_errno("asprintf");
2862 err = got_ref_open(&ref, repo, refname, 0);
2863 if (err)
2864 goto done;
2866 err = got_ref_delete(ref, repo);
2867 got_ref_close(ref);
2868 done:
2869 free(refname);
2870 return err;
2873 static const struct got_error *
2874 add_branch(struct got_repository *repo, const char *branch_name,
2875 const char *base_branch)
2877 const struct got_error *err = NULL;
2878 struct got_object_id *id = NULL;
2879 struct got_reference *ref = NULL;
2880 char *base_refname = NULL, *refname = NULL;
2881 struct got_reference *base_ref;
2884 * Don't let the user create a branch named '-'.
2885 * While technically a valid reference name, this case is usually
2886 * an unintended typo.
2888 if (branch_name[0] == '-' && branch_name[1] == '\0')
2889 return got_error(GOT_ERR_BAD_REF_NAME);
2891 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2892 base_refname = strdup(GOT_REF_HEAD);
2893 if (base_refname == NULL)
2894 return got_error_from_errno("strdup");
2895 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2896 return got_error_from_errno("asprintf");
2898 err = got_ref_open(&base_ref, repo, base_refname, 0);
2899 if (err)
2900 goto done;
2901 err = got_ref_resolve(&id, repo, base_ref);
2902 got_ref_close(base_ref);
2903 if (err)
2904 goto done;
2906 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2907 err = got_error_from_errno("asprintf");
2908 goto done;
2911 err = got_ref_open(&ref, repo, refname, 0);
2912 if (err == NULL) {
2913 err = got_error(GOT_ERR_BRANCH_EXISTS);
2914 goto done;
2915 } else if (err->code != GOT_ERR_NOT_REF)
2916 goto done;
2918 err = got_ref_alloc(&ref, refname, id);
2919 if (err)
2920 goto done;
2922 err = got_ref_write(ref, repo);
2923 done:
2924 if (ref)
2925 got_ref_close(ref);
2926 free(id);
2927 free(base_refname);
2928 free(refname);
2929 return err;
2932 static const struct got_error *
2933 cmd_branch(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;
2940 const char *delref = NULL;
2942 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2943 switch (ch) {
2944 case 'd':
2945 delref = optarg;
2946 break;
2947 case 'r':
2948 repo_path = realpath(optarg, NULL);
2949 if (repo_path == NULL)
2950 err(1, "-r option");
2951 got_path_strip_trailing_slashes(repo_path);
2952 break;
2953 case 'l':
2954 do_list = 1;
2955 break;
2956 default:
2957 usage_branch();
2958 /* NOTREACHED */
2962 if (do_list && delref)
2963 errx(1, "-l and -d options are mutually exclusive\n");
2965 argc -= optind;
2966 argv += optind;
2968 if (do_list || delref) {
2969 if (argc > 0)
2970 usage_branch();
2971 } else if (argc < 1 || argc > 2)
2972 usage_branch();
2974 #ifndef PROFILE
2975 if (do_list) {
2976 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2977 NULL) == -1)
2978 err(1, "pledge");
2979 } else {
2980 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2981 "sendfd unveil", NULL) == -1)
2982 err(1, "pledge");
2984 #endif
2985 cwd = getcwd(NULL, 0);
2986 if (cwd == NULL) {
2987 error = got_error_from_errno("getcwd");
2988 goto done;
2991 if (repo_path == NULL) {
2992 error = got_worktree_open(&worktree, cwd);
2993 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2994 goto done;
2995 else
2996 error = NULL;
2997 if (worktree) {
2998 repo_path =
2999 strdup(got_worktree_get_repo_path(worktree));
3000 if (repo_path == NULL)
3001 error = got_error_from_errno("strdup");
3002 if (error)
3003 goto done;
3004 } else {
3005 repo_path = strdup(cwd);
3006 if (repo_path == NULL) {
3007 error = got_error_from_errno("strdup");
3008 goto done;
3013 error = got_repo_open(&repo, repo_path);
3014 if (error != NULL)
3015 goto done;
3017 error = apply_unveil(got_repo_get_path(repo), do_list,
3018 worktree ? got_worktree_get_root_path(worktree) : NULL);
3019 if (error)
3020 goto done;
3022 if (do_list)
3023 error = list_branches(repo, worktree);
3024 else if (delref)
3025 error = delete_branch(repo, delref);
3026 else {
3027 const char *base_branch;
3028 if (argc == 1) {
3029 base_branch = worktree ?
3030 got_worktree_get_head_ref_name(worktree) :
3031 GOT_REF_HEAD;
3032 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3033 base_branch += 11;
3034 } else
3035 base_branch = argv[1];
3036 error = add_branch(repo, argv[0], base_branch);
3038 done:
3039 if (repo)
3040 got_repo_close(repo);
3041 if (worktree)
3042 got_worktree_close(worktree);
3043 free(cwd);
3044 free(repo_path);
3045 return error;
3048 __dead static void
3049 usage_add(void)
3051 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3052 exit(1);
3055 static const struct got_error *
3056 cmd_add(int argc, char *argv[])
3058 const struct got_error *error = NULL;
3059 struct got_repository *repo = NULL;
3060 struct got_worktree *worktree = NULL;
3061 char *cwd = NULL;
3062 struct got_pathlist_head paths;
3063 struct got_pathlist_entry *pe;
3064 int ch;
3066 TAILQ_INIT(&paths);
3068 while ((ch = getopt(argc, argv, "")) != -1) {
3069 switch (ch) {
3070 default:
3071 usage_add();
3072 /* NOTREACHED */
3076 argc -= optind;
3077 argv += optind;
3079 #ifndef PROFILE
3080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3081 NULL) == -1)
3082 err(1, "pledge");
3083 #endif
3084 if (argc < 1)
3085 usage_add();
3087 cwd = getcwd(NULL, 0);
3088 if (cwd == NULL) {
3089 error = got_error_from_errno("getcwd");
3090 goto done;
3093 error = got_worktree_open(&worktree, cwd);
3094 if (error)
3095 goto done;
3097 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3098 if (error != NULL)
3099 goto done;
3101 error = apply_unveil(got_repo_get_path(repo), 1,
3102 got_worktree_get_root_path(worktree));
3103 if (error)
3104 goto done;
3106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3107 if (error)
3108 goto done;
3110 error = got_worktree_schedule_add(worktree, &paths, print_status,
3111 NULL, repo);
3112 done:
3113 if (repo)
3114 got_repo_close(repo);
3115 if (worktree)
3116 got_worktree_close(worktree);
3117 TAILQ_FOREACH(pe, &paths, entry)
3118 free((char *)pe->path);
3119 got_pathlist_free(&paths);
3120 free(cwd);
3121 return error;
3124 __dead static void
3125 usage_remove(void)
3127 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3128 exit(1);
3131 static const struct got_error *
3132 cmd_remove(int argc, char *argv[])
3134 const struct got_error *error = NULL;
3135 struct got_worktree *worktree = NULL;
3136 struct got_repository *repo = NULL;
3137 char *cwd = NULL;
3138 struct got_pathlist_head paths;
3139 struct got_pathlist_entry *pe;
3140 int ch, delete_local_mods = 0;
3142 TAILQ_INIT(&paths);
3144 while ((ch = getopt(argc, argv, "f")) != -1) {
3145 switch (ch) {
3146 case 'f':
3147 delete_local_mods = 1;
3148 break;
3149 default:
3150 usage_add();
3151 /* NOTREACHED */
3155 argc -= optind;
3156 argv += optind;
3158 #ifndef PROFILE
3159 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3160 NULL) == -1)
3161 err(1, "pledge");
3162 #endif
3163 if (argc < 1)
3164 usage_remove();
3166 cwd = getcwd(NULL, 0);
3167 if (cwd == NULL) {
3168 error = got_error_from_errno("getcwd");
3169 goto done;
3171 error = got_worktree_open(&worktree, cwd);
3172 if (error)
3173 goto done;
3175 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3176 if (error)
3177 goto done;
3179 error = apply_unveil(got_repo_get_path(repo), 1,
3180 got_worktree_get_root_path(worktree));
3181 if (error)
3182 goto done;
3184 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3185 if (error)
3186 goto done;
3188 error = got_worktree_schedule_delete(worktree, &paths,
3189 delete_local_mods, print_status, NULL, repo);
3190 if (error)
3191 goto done;
3192 done:
3193 if (repo)
3194 got_repo_close(repo);
3195 if (worktree)
3196 got_worktree_close(worktree);
3197 TAILQ_FOREACH(pe, &paths, entry)
3198 free((char *)pe->path);
3199 got_pathlist_free(&paths);
3200 free(cwd);
3201 return error;
3204 __dead static void
3205 usage_revert(void)
3207 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3208 "path ...\n", getprogname());
3209 exit(1);
3212 static const struct got_error *
3213 revert_progress(void *arg, unsigned char status, const char *path)
3215 while (path[0] == '/')
3216 path++;
3217 printf("%c %s\n", status, path);
3218 return NULL;
3221 struct choose_patch_arg {
3222 FILE *patch_script_file;
3223 const char *action;
3226 static const struct got_error *
3227 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3228 int nchanges, const char *action)
3230 char *line = NULL;
3231 size_t linesize = 0;
3232 ssize_t linelen;
3234 switch (status) {
3235 case GOT_STATUS_ADD:
3236 printf("A %s\n%s this addition? [y/n] ", path, action);
3237 break;
3238 case GOT_STATUS_DELETE:
3239 printf("D %s\n%s this deletion? [y/n] ", path, action);
3240 break;
3241 case GOT_STATUS_MODIFY:
3242 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3243 return got_error_from_errno("fseek");
3244 printf(GOT_COMMIT_SEP_STR);
3245 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3246 printf("%s", line);
3247 if (ferror(patch_file))
3248 return got_error_from_errno("getline");
3249 printf(GOT_COMMIT_SEP_STR);
3250 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3251 path, n, nchanges, action);
3252 break;
3253 default:
3254 return got_error_path(path, GOT_ERR_FILE_STATUS);
3257 return NULL;
3260 static const struct got_error *
3261 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3262 FILE *patch_file, int n, int nchanges)
3264 const struct got_error *err = NULL;
3265 char *line = NULL;
3266 size_t linesize = 0;
3267 ssize_t linelen;
3268 int resp = ' ';
3269 struct choose_patch_arg *a = arg;
3271 *choice = GOT_PATCH_CHOICE_NONE;
3273 if (a->patch_script_file) {
3274 char *nl;
3275 err = show_change(status, path, patch_file, n, nchanges,
3276 a->action);
3277 if (err)
3278 return err;
3279 linelen = getline(&line, &linesize, a->patch_script_file);
3280 if (linelen == -1) {
3281 if (ferror(a->patch_script_file))
3282 return got_error_from_errno("getline");
3283 return NULL;
3285 nl = strchr(line, '\n');
3286 if (nl)
3287 *nl = '\0';
3288 if (strcmp(line, "y") == 0) {
3289 *choice = GOT_PATCH_CHOICE_YES;
3290 printf("y\n");
3291 } else if (strcmp(line, "n") == 0) {
3292 *choice = GOT_PATCH_CHOICE_NO;
3293 printf("n\n");
3294 } else if (strcmp(line, "q") == 0 &&
3295 status == GOT_STATUS_MODIFY) {
3296 *choice = GOT_PATCH_CHOICE_QUIT;
3297 printf("q\n");
3298 } else
3299 printf("invalid response '%s'\n", line);
3300 free(line);
3301 return NULL;
3304 while (resp != 'y' && resp != 'n' && resp != 'q') {
3305 err = show_change(status, path, patch_file, n, nchanges,
3306 a->action);
3307 if (err)
3308 return err;
3309 resp = getchar();
3310 if (resp == '\n')
3311 resp = getchar();
3312 if (status == GOT_STATUS_MODIFY) {
3313 if (resp != 'y' && resp != 'n' && resp != 'q') {
3314 printf("invalid response '%c'\n", resp);
3315 resp = ' ';
3317 } else if (resp != 'y' && resp != 'n') {
3318 printf("invalid response '%c'\n", resp);
3319 resp = ' ';
3323 if (resp == 'y')
3324 *choice = GOT_PATCH_CHOICE_YES;
3325 else if (resp == 'n')
3326 *choice = GOT_PATCH_CHOICE_NO;
3327 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3328 *choice = GOT_PATCH_CHOICE_QUIT;
3330 return NULL;
3334 static const struct got_error *
3335 cmd_revert(int argc, char *argv[])
3337 const struct got_error *error = NULL;
3338 struct got_worktree *worktree = NULL;
3339 struct got_repository *repo = NULL;
3340 char *cwd = NULL, *path = NULL;
3341 struct got_pathlist_head paths;
3342 struct got_pathlist_entry *pe;
3343 int ch, can_recurse = 0, pflag = 0;
3344 FILE *patch_script_file = NULL;
3345 const char *patch_script_path = NULL;
3346 struct choose_patch_arg cpa;
3348 TAILQ_INIT(&paths);
3350 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3351 switch (ch) {
3352 case 'p':
3353 pflag = 1;
3354 break;
3355 case 'F':
3356 patch_script_path = optarg;
3357 break;
3358 case 'R':
3359 can_recurse = 1;
3360 break;
3361 default:
3362 usage_revert();
3363 /* NOTREACHED */
3367 argc -= optind;
3368 argv += optind;
3370 #ifndef PROFILE
3371 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3372 "unveil", NULL) == -1)
3373 err(1, "pledge");
3374 #endif
3375 if (argc < 1)
3376 usage_revert();
3377 if (patch_script_path && !pflag)
3378 errx(1, "-F option can only be used together with -p option");
3380 cwd = getcwd(NULL, 0);
3381 if (cwd == NULL) {
3382 error = got_error_from_errno("getcwd");
3383 goto done;
3385 error = got_worktree_open(&worktree, cwd);
3386 if (error)
3387 goto done;
3389 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3390 if (error != NULL)
3391 goto done;
3393 if (patch_script_path) {
3394 patch_script_file = fopen(patch_script_path, "r");
3395 if (patch_script_file == NULL) {
3396 error = got_error_from_errno2("fopen",
3397 patch_script_path);
3398 goto done;
3401 error = apply_unveil(got_repo_get_path(repo), 1,
3402 got_worktree_get_root_path(worktree));
3403 if (error)
3404 goto done;
3406 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3407 if (error)
3408 goto done;
3410 if (!can_recurse) {
3411 char *ondisk_path;
3412 struct stat sb;
3413 TAILQ_FOREACH(pe, &paths, entry) {
3414 if (asprintf(&ondisk_path, "%s/%s",
3415 got_worktree_get_root_path(worktree),
3416 pe->path) == -1) {
3417 error = got_error_from_errno("asprintf");
3418 goto done;
3420 if (lstat(ondisk_path, &sb) == -1) {
3421 if (errno == ENOENT) {
3422 free(ondisk_path);
3423 continue;
3425 error = got_error_from_errno2("lstat",
3426 ondisk_path);
3427 free(ondisk_path);
3428 goto done;
3430 free(ondisk_path);
3431 if (S_ISDIR(sb.st_mode)) {
3432 error = got_error_msg(GOT_ERR_BAD_PATH,
3433 "reverting directories requires -R option");
3434 goto done;
3439 cpa.patch_script_file = patch_script_file;
3440 cpa.action = "revert";
3441 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3442 pflag ? choose_patch : NULL, &cpa, repo);
3443 if (error)
3444 goto done;
3445 done:
3446 if (patch_script_file && fclose(patch_script_file) == EOF &&
3447 error == NULL)
3448 error = got_error_from_errno2("fclose", patch_script_path);
3449 if (repo)
3450 got_repo_close(repo);
3451 if (worktree)
3452 got_worktree_close(worktree);
3453 free(path);
3454 free(cwd);
3455 return error;
3458 __dead static void
3459 usage_commit(void)
3461 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3462 getprogname());
3463 exit(1);
3466 struct collect_commit_logmsg_arg {
3467 const char *cmdline_log;
3468 const char *editor;
3469 const char *worktree_path;
3470 const char *branch_name;
3471 const char *repo_path;
3472 char *logmsg_path;
3476 static const struct got_error *
3477 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3478 void *arg)
3480 char *initial_content = NULL;
3481 struct got_pathlist_entry *pe;
3482 const struct got_error *err = NULL;
3483 char *template = NULL;
3484 struct collect_commit_logmsg_arg *a = arg;
3485 int fd;
3486 size_t len;
3488 /* if a message was specified on the command line, just use it */
3489 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3490 len = strlen(a->cmdline_log) + 1;
3491 *logmsg = malloc(len + 1);
3492 if (*logmsg == NULL)
3493 return got_error_from_errno("malloc");
3494 strlcpy(*logmsg, a->cmdline_log, len);
3495 return NULL;
3498 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3499 return got_error_from_errno("asprintf");
3501 if (asprintf(&initial_content,
3502 "\n# changes to be committed on branch %s:\n",
3503 a->branch_name) == -1)
3504 return got_error_from_errno("asprintf");
3506 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3507 if (err)
3508 goto done;
3510 dprintf(fd, initial_content);
3512 TAILQ_FOREACH(pe, commitable_paths, entry) {
3513 struct got_commitable *ct = pe->data;
3514 dprintf(fd, "# %c %s\n",
3515 got_commitable_get_status(ct),
3516 got_commitable_get_path(ct));
3518 close(fd);
3520 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3521 done:
3522 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3523 unlink(a->logmsg_path);
3524 free(a->logmsg_path);
3525 a->logmsg_path = NULL;
3527 free(initial_content);
3528 free(template);
3530 /* Editor is done; we can now apply unveil(2) */
3531 if (err == NULL) {
3532 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3533 if (err) {
3534 free(*logmsg);
3535 *logmsg = NULL;
3538 return err;
3541 static const struct got_error *
3542 cmd_commit(int argc, char *argv[])
3544 const struct got_error *error = NULL;
3545 struct got_worktree *worktree = NULL;
3546 struct got_repository *repo = NULL;
3547 char *cwd = NULL, *id_str = NULL;
3548 struct got_object_id *id = NULL;
3549 const char *logmsg = NULL;
3550 const char *author;
3551 struct collect_commit_logmsg_arg cl_arg;
3552 char *editor = NULL;
3553 int ch, rebase_in_progress, histedit_in_progress;
3554 struct got_pathlist_head paths;
3556 TAILQ_INIT(&paths);
3557 cl_arg.logmsg_path = NULL;
3559 while ((ch = getopt(argc, argv, "m:")) != -1) {
3560 switch (ch) {
3561 case 'm':
3562 logmsg = optarg;
3563 break;
3564 default:
3565 usage_commit();
3566 /* NOTREACHED */
3570 argc -= optind;
3571 argv += optind;
3573 #ifndef PROFILE
3574 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3575 "unveil", NULL) == -1)
3576 err(1, "pledge");
3577 #endif
3578 error = get_author(&author);
3579 if (error)
3580 return error;
3582 cwd = getcwd(NULL, 0);
3583 if (cwd == NULL) {
3584 error = got_error_from_errno("getcwd");
3585 goto done;
3587 error = got_worktree_open(&worktree, cwd);
3588 if (error)
3589 goto done;
3591 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3592 if (error)
3593 goto done;
3594 if (rebase_in_progress) {
3595 error = got_error(GOT_ERR_REBASING);
3596 goto done;
3599 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3600 worktree);
3601 if (error)
3602 goto done;
3604 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3605 if (error != NULL)
3606 goto done;
3609 * unveil(2) traverses exec(2); if an editor is used we have
3610 * to apply unveil after the log message has been written.
3612 if (logmsg == NULL || strlen(logmsg) == 0)
3613 error = get_editor(&editor);
3614 else
3615 error = apply_unveil(got_repo_get_path(repo), 0,
3616 got_worktree_get_root_path(worktree));
3617 if (error)
3618 goto done;
3620 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3621 if (error)
3622 goto done;
3624 cl_arg.editor = editor;
3625 cl_arg.cmdline_log = logmsg;
3626 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3627 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3628 if (!histedit_in_progress) {
3629 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3630 error = got_error(GOT_ERR_COMMIT_BRANCH);
3631 goto done;
3633 cl_arg.branch_name += 11;
3635 cl_arg.repo_path = got_repo_get_path(repo);
3636 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3637 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3638 if (error) {
3639 if (cl_arg.logmsg_path)
3640 fprintf(stderr, "%s: log message preserved in %s\n",
3641 getprogname(), cl_arg.logmsg_path);
3642 goto done;
3645 if (cl_arg.logmsg_path)
3646 unlink(cl_arg.logmsg_path);
3648 error = got_object_id_str(&id_str, id);
3649 if (error)
3650 goto done;
3651 printf("Created commit %s\n", id_str);
3652 done:
3653 free(cl_arg.logmsg_path);
3654 if (repo)
3655 got_repo_close(repo);
3656 if (worktree)
3657 got_worktree_close(worktree);
3658 free(cwd);
3659 free(id_str);
3660 free(editor);
3661 return error;
3664 __dead static void
3665 usage_cherrypick(void)
3667 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3668 exit(1);
3671 static const struct got_error *
3672 cmd_cherrypick(int argc, char *argv[])
3674 const struct got_error *error = NULL;
3675 struct got_worktree *worktree = NULL;
3676 struct got_repository *repo = NULL;
3677 char *cwd = NULL, *commit_id_str = NULL;
3678 struct got_object_id *commit_id = NULL;
3679 struct got_commit_object *commit = NULL;
3680 struct got_object_qid *pid;
3681 struct got_reference *head_ref = NULL;
3682 int ch, did_something = 0;
3684 while ((ch = getopt(argc, argv, "")) != -1) {
3685 switch (ch) {
3686 default:
3687 usage_cherrypick();
3688 /* NOTREACHED */
3692 argc -= optind;
3693 argv += optind;
3695 #ifndef PROFILE
3696 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3697 "unveil", NULL) == -1)
3698 err(1, "pledge");
3699 #endif
3700 if (argc != 1)
3701 usage_cherrypick();
3703 cwd = getcwd(NULL, 0);
3704 if (cwd == NULL) {
3705 error = got_error_from_errno("getcwd");
3706 goto done;
3708 error = got_worktree_open(&worktree, cwd);
3709 if (error)
3710 goto done;
3712 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3713 if (error != NULL)
3714 goto done;
3716 error = apply_unveil(got_repo_get_path(repo), 0,
3717 got_worktree_get_root_path(worktree));
3718 if (error)
3719 goto done;
3721 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3722 GOT_OBJ_TYPE_COMMIT, repo);
3723 if (error != NULL) {
3724 struct got_reference *ref;
3725 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3726 goto done;
3727 error = got_ref_open(&ref, repo, argv[0], 0);
3728 if (error != NULL)
3729 goto done;
3730 error = got_ref_resolve(&commit_id, repo, ref);
3731 got_ref_close(ref);
3732 if (error != NULL)
3733 goto done;
3735 error = got_object_id_str(&commit_id_str, commit_id);
3736 if (error)
3737 goto done;
3739 error = got_ref_open(&head_ref, repo,
3740 got_worktree_get_head_ref_name(worktree), 0);
3741 if (error != NULL)
3742 goto done;
3744 error = check_same_branch(commit_id, head_ref, NULL, repo);
3745 if (error) {
3746 if (error->code != GOT_ERR_ANCESTRY)
3747 goto done;
3748 error = NULL;
3749 } else {
3750 error = got_error(GOT_ERR_SAME_BRANCH);
3751 goto done;
3754 error = got_object_open_as_commit(&commit, repo, commit_id);
3755 if (error)
3756 goto done;
3757 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3758 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3759 commit_id, repo, update_progress, &did_something, check_cancelled,
3760 NULL);
3761 if (error != NULL)
3762 goto done;
3764 if (did_something)
3765 printf("Merged commit %s\n", commit_id_str);
3766 done:
3767 if (commit)
3768 got_object_commit_close(commit);
3769 free(commit_id_str);
3770 if (head_ref)
3771 got_ref_close(head_ref);
3772 if (worktree)
3773 got_worktree_close(worktree);
3774 if (repo)
3775 got_repo_close(repo);
3776 return error;
3779 __dead static void
3780 usage_backout(void)
3782 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3783 exit(1);
3786 static const struct got_error *
3787 cmd_backout(int argc, char *argv[])
3789 const struct got_error *error = NULL;
3790 struct got_worktree *worktree = NULL;
3791 struct got_repository *repo = NULL;
3792 char *cwd = NULL, *commit_id_str = NULL;
3793 struct got_object_id *commit_id = NULL;
3794 struct got_commit_object *commit = NULL;
3795 struct got_object_qid *pid;
3796 struct got_reference *head_ref = NULL;
3797 int ch, did_something = 0;
3799 while ((ch = getopt(argc, argv, "")) != -1) {
3800 switch (ch) {
3801 default:
3802 usage_backout();
3803 /* NOTREACHED */
3807 argc -= optind;
3808 argv += optind;
3810 #ifndef PROFILE
3811 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3812 "unveil", NULL) == -1)
3813 err(1, "pledge");
3814 #endif
3815 if (argc != 1)
3816 usage_backout();
3818 cwd = getcwd(NULL, 0);
3819 if (cwd == NULL) {
3820 error = got_error_from_errno("getcwd");
3821 goto done;
3823 error = got_worktree_open(&worktree, cwd);
3824 if (error)
3825 goto done;
3827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3828 if (error != NULL)
3829 goto done;
3831 error = apply_unveil(got_repo_get_path(repo), 0,
3832 got_worktree_get_root_path(worktree));
3833 if (error)
3834 goto done;
3836 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3837 GOT_OBJ_TYPE_COMMIT, repo);
3838 if (error != NULL) {
3839 struct got_reference *ref;
3840 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3841 goto done;
3842 error = got_ref_open(&ref, repo, argv[0], 0);
3843 if (error != NULL)
3844 goto done;
3845 error = got_ref_resolve(&commit_id, repo, ref);
3846 got_ref_close(ref);
3847 if (error != NULL)
3848 goto done;
3850 error = got_object_id_str(&commit_id_str, commit_id);
3851 if (error)
3852 goto done;
3854 error = got_ref_open(&head_ref, repo,
3855 got_worktree_get_head_ref_name(worktree), 0);
3856 if (error != NULL)
3857 goto done;
3859 error = check_same_branch(commit_id, head_ref, NULL, repo);
3860 if (error)
3861 goto done;
3863 error = got_object_open_as_commit(&commit, repo, commit_id);
3864 if (error)
3865 goto done;
3866 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3867 if (pid == NULL) {
3868 error = got_error(GOT_ERR_ROOT_COMMIT);
3869 goto done;
3872 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3873 update_progress, &did_something, check_cancelled, NULL);
3874 if (error != NULL)
3875 goto done;
3877 if (did_something)
3878 printf("Backed out commit %s\n", commit_id_str);
3879 done:
3880 if (commit)
3881 got_object_commit_close(commit);
3882 free(commit_id_str);
3883 if (head_ref)
3884 got_ref_close(head_ref);
3885 if (worktree)
3886 got_worktree_close(worktree);
3887 if (repo)
3888 got_repo_close(repo);
3889 return error;
3892 __dead static void
3893 usage_rebase(void)
3895 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3896 getprogname());
3897 exit(1);
3900 void
3901 trim_logmsg(char *logmsg, int limit)
3903 char *nl;
3904 size_t len;
3906 len = strlen(logmsg);
3907 if (len > limit)
3908 len = limit;
3909 logmsg[len] = '\0';
3910 nl = strchr(logmsg, '\n');
3911 if (nl)
3912 *nl = '\0';
3915 static const struct got_error *
3916 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3918 const char *logmsg0 = NULL;
3920 logmsg0 = got_object_commit_get_logmsg(commit);
3922 while (isspace((unsigned char)logmsg0[0]))
3923 logmsg0++;
3925 *logmsg = strdup(logmsg0);
3926 if (*logmsg == NULL)
3927 return got_error_from_errno("strdup");
3929 trim_logmsg(*logmsg, limit);
3930 return NULL;
3933 static const struct got_error *
3934 show_rebase_progress(struct got_commit_object *commit,
3935 struct got_object_id *old_id, struct got_object_id *new_id)
3937 const struct got_error *err;
3938 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3940 err = got_object_id_str(&old_id_str, old_id);
3941 if (err)
3942 goto done;
3944 if (new_id) {
3945 err = got_object_id_str(&new_id_str, new_id);
3946 if (err)
3947 goto done;
3950 old_id_str[12] = '\0';
3951 if (new_id_str)
3952 new_id_str[12] = '\0';
3954 err = get_short_logmsg(&logmsg, 42, commit);
3955 if (err)
3956 goto done;
3958 printf("%s -> %s: %s\n", old_id_str,
3959 new_id_str ? new_id_str : "no-op change", logmsg);
3960 done:
3961 free(old_id_str);
3962 free(new_id_str);
3963 return err;
3966 static const struct got_error *
3967 rebase_progress(void *arg, unsigned char status, const char *path)
3969 unsigned char *rebase_status = arg;
3971 while (path[0] == '/')
3972 path++;
3973 printf("%c %s\n", status, path);
3975 if (*rebase_status == GOT_STATUS_CONFLICT)
3976 return NULL;
3977 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3978 *rebase_status = status;
3979 return NULL;
3982 static const struct got_error *
3983 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3984 struct got_reference *branch, struct got_reference *new_base_branch,
3985 struct got_reference *tmp_branch, struct got_repository *repo)
3987 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3988 return got_worktree_rebase_complete(worktree, fileindex,
3989 new_base_branch, tmp_branch, branch, repo);
3992 static const struct got_error *
3993 rebase_commit(struct got_pathlist_head *merged_paths,
3994 struct got_worktree *worktree, struct got_fileindex *fileindex,
3995 struct got_reference *tmp_branch,
3996 struct got_object_id *commit_id, struct got_repository *repo)
3998 const struct got_error *error;
3999 struct got_commit_object *commit;
4000 struct got_object_id *new_commit_id;
4002 error = got_object_open_as_commit(&commit, repo, commit_id);
4003 if (error)
4004 return error;
4006 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4007 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4008 if (error) {
4009 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4010 goto done;
4011 error = show_rebase_progress(commit, commit_id, NULL);
4012 } else {
4013 error = show_rebase_progress(commit, commit_id, new_commit_id);
4014 free(new_commit_id);
4016 done:
4017 got_object_commit_close(commit);
4018 return error;
4021 struct check_path_prefix_arg {
4022 const char *path_prefix;
4023 size_t len;
4024 int errcode;
4027 static const struct got_error *
4028 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4029 struct got_blob_object *blob2, struct got_object_id *id1,
4030 struct got_object_id *id2, const char *path1, const char *path2,
4031 struct got_repository *repo)
4033 struct check_path_prefix_arg *a = arg;
4035 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4036 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4037 return got_error(a->errcode);
4039 return NULL;
4042 static const struct got_error *
4043 check_path_prefix(struct got_object_id *parent_id,
4044 struct got_object_id *commit_id, const char *path_prefix,
4045 int errcode, struct got_repository *repo)
4047 const struct got_error *err;
4048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4049 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4050 struct check_path_prefix_arg cpp_arg;
4052 if (got_path_is_root_dir(path_prefix))
4053 return NULL;
4055 err = got_object_open_as_commit(&commit, repo, commit_id);
4056 if (err)
4057 goto done;
4059 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4060 if (err)
4061 goto done;
4063 err = got_object_open_as_tree(&tree1, repo,
4064 got_object_commit_get_tree_id(parent_commit));
4065 if (err)
4066 goto done;
4068 err = got_object_open_as_tree(&tree2, repo,
4069 got_object_commit_get_tree_id(commit));
4070 if (err)
4071 goto done;
4073 cpp_arg.path_prefix = path_prefix;
4074 while (cpp_arg.path_prefix[0] == '/')
4075 cpp_arg.path_prefix++;
4076 cpp_arg.len = strlen(cpp_arg.path_prefix);
4077 cpp_arg.errcode = errcode;
4078 err = got_diff_tree(tree1, tree2, "", "", repo,
4079 check_path_prefix_in_diff, &cpp_arg, 0);
4080 done:
4081 if (tree1)
4082 got_object_tree_close(tree1);
4083 if (tree2)
4084 got_object_tree_close(tree2);
4085 if (commit)
4086 got_object_commit_close(commit);
4087 if (parent_commit)
4088 got_object_commit_close(parent_commit);
4089 return err;
4092 static const struct got_error *
4093 collect_commits(struct got_object_id_queue *commits,
4094 struct got_object_id *initial_commit_id,
4095 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4096 const char *path_prefix, int path_prefix_errcode,
4097 struct got_repository *repo)
4099 const struct got_error *err = NULL;
4100 struct got_commit_graph *graph = NULL;
4101 struct got_object_id *parent_id = NULL;
4102 struct got_object_qid *qid;
4103 struct got_object_id *commit_id = initial_commit_id;
4105 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4106 if (err)
4107 return err;
4109 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4110 if (err)
4111 goto done;
4112 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4113 err = got_commit_graph_iter_next(&parent_id, graph);
4114 if (err) {
4115 if (err->code == GOT_ERR_ITER_COMPLETED) {
4116 err = got_error_msg(GOT_ERR_ANCESTRY,
4117 "ran out of commits to rebase before "
4118 "youngest common ancestor commit has "
4119 "been reached?!?");
4120 goto done;
4121 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4122 goto done;
4123 err = got_commit_graph_fetch_commits(graph, 1, repo);
4124 if (err)
4125 goto done;
4126 } else {
4127 err = check_path_prefix(parent_id, commit_id,
4128 path_prefix, path_prefix_errcode, repo);
4129 if (err)
4130 goto done;
4132 err = got_object_qid_alloc(&qid, commit_id);
4133 if (err)
4134 goto done;
4135 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4136 commit_id = parent_id;
4139 done:
4140 got_commit_graph_close(graph);
4141 return err;
4144 static const struct got_error *
4145 cmd_rebase(int argc, char *argv[])
4147 const struct got_error *error = NULL;
4148 struct got_worktree *worktree = NULL;
4149 struct got_repository *repo = NULL;
4150 struct got_fileindex *fileindex = NULL;
4151 char *cwd = NULL;
4152 struct got_reference *branch = NULL;
4153 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4154 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4155 struct got_object_id *resume_commit_id = NULL;
4156 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4157 struct got_commit_object *commit = NULL;
4158 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4159 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4160 struct got_object_id_queue commits;
4161 struct got_pathlist_head merged_paths;
4162 const struct got_object_id_queue *parent_ids;
4163 struct got_object_qid *qid, *pid;
4165 SIMPLEQ_INIT(&commits);
4166 TAILQ_INIT(&merged_paths);
4168 while ((ch = getopt(argc, argv, "ac")) != -1) {
4169 switch (ch) {
4170 case 'a':
4171 abort_rebase = 1;
4172 break;
4173 case 'c':
4174 continue_rebase = 1;
4175 break;
4176 default:
4177 usage_rebase();
4178 /* NOTREACHED */
4182 argc -= optind;
4183 argv += optind;
4185 #ifndef PROFILE
4186 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4187 "unveil", NULL) == -1)
4188 err(1, "pledge");
4189 #endif
4190 if (abort_rebase && continue_rebase)
4191 usage_rebase();
4192 else if (abort_rebase || continue_rebase) {
4193 if (argc != 0)
4194 usage_rebase();
4195 } else if (argc != 1)
4196 usage_rebase();
4198 cwd = getcwd(NULL, 0);
4199 if (cwd == NULL) {
4200 error = got_error_from_errno("getcwd");
4201 goto done;
4203 error = got_worktree_open(&worktree, cwd);
4204 if (error)
4205 goto done;
4207 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4208 if (error != NULL)
4209 goto done;
4211 error = apply_unveil(got_repo_get_path(repo), 0,
4212 got_worktree_get_root_path(worktree));
4213 if (error)
4214 goto done;
4216 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4217 if (error)
4218 goto done;
4220 if (abort_rebase) {
4221 int did_something;
4222 if (!rebase_in_progress) {
4223 error = got_error(GOT_ERR_NOT_REBASING);
4224 goto done;
4226 error = got_worktree_rebase_continue(&resume_commit_id,
4227 &new_base_branch, &tmp_branch, &branch, &fileindex,
4228 worktree, repo);
4229 if (error)
4230 goto done;
4231 printf("Switching work tree to %s\n",
4232 got_ref_get_symref_target(new_base_branch));
4233 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4234 new_base_branch, update_progress, &did_something);
4235 if (error)
4236 goto done;
4237 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4238 goto done; /* nothing else to do */
4241 if (continue_rebase) {
4242 if (!rebase_in_progress) {
4243 error = got_error(GOT_ERR_NOT_REBASING);
4244 goto done;
4246 error = got_worktree_rebase_continue(&resume_commit_id,
4247 &new_base_branch, &tmp_branch, &branch, &fileindex,
4248 worktree, repo);
4249 if (error)
4250 goto done;
4252 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4253 resume_commit_id, repo);
4254 if (error)
4255 goto done;
4257 yca_id = got_object_id_dup(resume_commit_id);
4258 if (yca_id == NULL) {
4259 error = got_error_from_errno("got_object_id_dup");
4260 goto done;
4262 } else {
4263 error = got_ref_open(&branch, repo, argv[0], 0);
4264 if (error != NULL)
4265 goto done;
4268 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4269 if (error)
4270 goto done;
4272 if (!continue_rebase) {
4273 struct got_object_id *base_commit_id;
4275 base_commit_id = got_worktree_get_base_commit_id(worktree);
4276 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4277 base_commit_id, branch_head_commit_id, repo);
4278 if (error)
4279 goto done;
4280 if (yca_id == NULL) {
4281 error = got_error_msg(GOT_ERR_ANCESTRY,
4282 "specified branch shares no common ancestry "
4283 "with work tree's branch");
4284 goto done;
4287 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4288 if (error) {
4289 if (error->code != GOT_ERR_ANCESTRY)
4290 goto done;
4291 error = NULL;
4292 } else {
4293 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4294 "specified branch resolves to a commit which "
4295 "is already contained in work tree's branch");
4296 goto done;
4298 error = got_worktree_rebase_prepare(&new_base_branch,
4299 &tmp_branch, &fileindex, worktree, branch, repo);
4300 if (error)
4301 goto done;
4304 commit_id = branch_head_commit_id;
4305 error = got_object_open_as_commit(&commit, repo, commit_id);
4306 if (error)
4307 goto done;
4309 parent_ids = got_object_commit_get_parent_ids(commit);
4310 pid = SIMPLEQ_FIRST(parent_ids);
4311 if (pid == NULL) {
4312 if (!continue_rebase) {
4313 int did_something;
4314 error = got_worktree_rebase_abort(worktree, fileindex,
4315 repo, new_base_branch, update_progress,
4316 &did_something);
4317 if (error)
4318 goto done;
4319 printf("Rebase of %s aborted\n",
4320 got_ref_get_name(branch));
4322 error = got_error(GOT_ERR_EMPTY_REBASE);
4323 goto done;
4325 error = collect_commits(&commits, commit_id, pid->id,
4326 yca_id, got_worktree_get_path_prefix(worktree),
4327 GOT_ERR_REBASE_PATH, repo);
4328 got_object_commit_close(commit);
4329 commit = NULL;
4330 if (error)
4331 goto done;
4333 if (SIMPLEQ_EMPTY(&commits)) {
4334 if (continue_rebase)
4335 error = rebase_complete(worktree, fileindex,
4336 branch, new_base_branch, tmp_branch, repo);
4337 else
4338 error = got_error(GOT_ERR_EMPTY_REBASE);
4339 goto done;
4342 pid = NULL;
4343 SIMPLEQ_FOREACH(qid, &commits, entry) {
4344 commit_id = qid->id;
4345 parent_id = pid ? pid->id : yca_id;
4346 pid = qid;
4348 error = got_worktree_rebase_merge_files(&merged_paths,
4349 worktree, fileindex, parent_id, commit_id, repo,
4350 rebase_progress, &rebase_status, check_cancelled, NULL);
4351 if (error)
4352 goto done;
4354 if (rebase_status == GOT_STATUS_CONFLICT) {
4355 got_worktree_rebase_pathlist_free(&merged_paths);
4356 break;
4359 error = rebase_commit(&merged_paths, worktree, fileindex,
4360 tmp_branch, commit_id, repo);
4361 got_worktree_rebase_pathlist_free(&merged_paths);
4362 if (error)
4363 goto done;
4366 if (rebase_status == GOT_STATUS_CONFLICT) {
4367 error = got_worktree_rebase_postpone(worktree, fileindex);
4368 if (error)
4369 goto done;
4370 error = got_error_msg(GOT_ERR_CONFLICTS,
4371 "conflicts must be resolved before rebasing can continue");
4372 } else
4373 error = rebase_complete(worktree, fileindex, branch,
4374 new_base_branch, tmp_branch, repo);
4375 done:
4376 got_object_id_queue_free(&commits);
4377 free(branch_head_commit_id);
4378 free(resume_commit_id);
4379 free(yca_id);
4380 if (commit)
4381 got_object_commit_close(commit);
4382 if (branch)
4383 got_ref_close(branch);
4384 if (new_base_branch)
4385 got_ref_close(new_base_branch);
4386 if (tmp_branch)
4387 got_ref_close(tmp_branch);
4388 if (worktree)
4389 got_worktree_close(worktree);
4390 if (repo)
4391 got_repo_close(repo);
4392 return error;
4395 __dead static void
4396 usage_histedit(void)
4398 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4399 getprogname());
4400 exit(1);
4403 #define GOT_HISTEDIT_PICK 'p'
4404 #define GOT_HISTEDIT_EDIT 'e'
4405 #define GOT_HISTEDIT_FOLD 'f'
4406 #define GOT_HISTEDIT_DROP 'd'
4407 #define GOT_HISTEDIT_MESG 'm'
4409 static struct got_histedit_cmd {
4410 unsigned char code;
4411 const char *name;
4412 const char *desc;
4413 } got_histedit_cmds[] = {
4414 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4415 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4416 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4417 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4418 { GOT_HISTEDIT_MESG, "mesg",
4419 "single-line log message for commit above (open editor if empty)" },
4422 struct got_histedit_list_entry {
4423 TAILQ_ENTRY(got_histedit_list_entry) entry;
4424 struct got_object_id *commit_id;
4425 const struct got_histedit_cmd *cmd;
4426 char *logmsg;
4428 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4430 static const struct got_error *
4431 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4432 FILE *f, struct got_repository *repo)
4434 const struct got_error *err = NULL;
4435 char *logmsg = NULL, *id_str = NULL;
4436 struct got_commit_object *commit = NULL;
4437 int n;
4439 err = got_object_open_as_commit(&commit, repo, commit_id);
4440 if (err)
4441 goto done;
4443 err = get_short_logmsg(&logmsg, 34, commit);
4444 if (err)
4445 goto done;
4447 err = got_object_id_str(&id_str, commit_id);
4448 if (err)
4449 goto done;
4451 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4452 if (n < 0)
4453 err = got_ferror(f, GOT_ERR_IO);
4454 done:
4455 if (commit)
4456 got_object_commit_close(commit);
4457 free(id_str);
4458 free(logmsg);
4459 return err;
4462 static const struct got_error *
4463 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4464 struct got_repository *repo)
4466 const struct got_error *err = NULL;
4467 struct got_object_qid *qid;
4469 if (SIMPLEQ_EMPTY(commits))
4470 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4472 SIMPLEQ_FOREACH(qid, commits, entry) {
4473 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4474 f, repo);
4475 if (err)
4476 break;
4479 return err;
4482 static const struct got_error *
4483 write_cmd_list(FILE *f)
4485 const struct got_error *err = NULL;
4486 int n, i;
4488 n = fprintf(f, "# Available histedit commands:\n");
4489 if (n < 0)
4490 return got_ferror(f, GOT_ERR_IO);
4492 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4493 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4494 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4495 cmd->desc);
4496 if (n < 0) {
4497 err = got_ferror(f, GOT_ERR_IO);
4498 break;
4501 n = fprintf(f, "# Commits will be processed in order from top to "
4502 "bottom of this file.\n");
4503 if (n < 0)
4504 return got_ferror(f, GOT_ERR_IO);
4505 return err;
4508 static const struct got_error *
4509 histedit_syntax_error(int lineno)
4511 static char msg[42];
4512 int ret;
4514 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4515 lineno);
4516 if (ret == -1 || ret >= sizeof(msg))
4517 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4519 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4522 static const struct got_error *
4523 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4524 char *logmsg, struct got_repository *repo)
4526 const struct got_error *err;
4527 struct got_commit_object *folded_commit = NULL;
4528 char *id_str;
4530 err = got_object_id_str(&id_str, hle->commit_id);
4531 if (err)
4532 return err;
4534 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4535 if (err)
4536 goto done;
4538 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4539 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4540 got_object_commit_get_logmsg(folded_commit)) == -1) {
4541 err = got_error_from_errno("asprintf");
4542 goto done;
4544 done:
4545 if (folded_commit)
4546 got_object_commit_close(folded_commit);
4547 free(id_str);
4548 return err;
4551 static struct got_histedit_list_entry *
4552 get_folded_commits(struct got_histedit_list_entry *hle)
4554 struct got_histedit_list_entry *prev, *folded = NULL;
4556 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4557 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4558 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4559 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4560 folded = prev;
4561 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4564 return folded;
4567 static const struct got_error *
4568 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4569 struct got_repository *repo)
4571 char *logmsg_path = NULL, *id_str = NULL;
4572 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4573 const struct got_error *err = NULL;
4574 struct got_commit_object *commit = NULL;
4575 int fd;
4576 struct got_histedit_list_entry *folded = NULL;
4578 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4579 if (err)
4580 return err;
4582 folded = get_folded_commits(hle);
4583 if (folded) {
4584 while (folded != hle) {
4585 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4586 folded = TAILQ_NEXT(folded, entry);
4587 continue;
4589 err = append_folded_commit_msg(&new_msg, folded,
4590 logmsg, repo);
4591 if (err)
4592 goto done;
4593 free(logmsg);
4594 logmsg = new_msg;
4595 folded = TAILQ_NEXT(folded, entry);
4599 err = got_object_id_str(&id_str, hle->commit_id);
4600 if (err)
4601 goto done;
4602 if (asprintf(&new_msg,
4603 "%s\n# original log message of commit %s: %s",
4604 logmsg ? logmsg : "", id_str,
4605 got_object_commit_get_logmsg(commit)) == -1) {
4606 err = got_error_from_errno("asprintf");
4607 goto done;
4609 free(logmsg);
4610 logmsg = new_msg;
4612 err = got_object_id_str(&id_str, hle->commit_id);
4613 if (err)
4614 goto done;
4616 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4617 if (err)
4618 goto done;
4620 dprintf(fd, logmsg);
4621 close(fd);
4623 err = get_editor(&editor);
4624 if (err)
4625 goto done;
4627 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4628 if (err) {
4629 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4630 goto done;
4631 err = NULL;
4632 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4633 if (hle->logmsg == NULL)
4634 err = got_error_from_errno("strdup");
4636 done:
4637 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4638 err = got_error_from_errno2("unlink", logmsg_path);
4639 free(logmsg_path);
4640 free(logmsg);
4641 free(editor);
4642 if (commit)
4643 got_object_commit_close(commit);
4644 return err;
4647 static const struct got_error *
4648 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4649 FILE *f, struct got_repository *repo)
4651 const struct got_error *err = NULL;
4652 char *line = NULL, *p, *end;
4653 size_t size;
4654 ssize_t len;
4655 int lineno = 0, i;
4656 const struct got_histedit_cmd *cmd;
4657 struct got_object_id *commit_id = NULL;
4658 struct got_histedit_list_entry *hle = NULL;
4660 for (;;) {
4661 len = getline(&line, &size, f);
4662 if (len == -1) {
4663 const struct got_error *getline_err;
4664 if (feof(f))
4665 break;
4666 getline_err = got_error_from_errno("getline");
4667 err = got_ferror(f, getline_err->code);
4668 break;
4670 lineno++;
4671 p = line;
4672 while (isspace((unsigned char)p[0]))
4673 p++;
4674 if (p[0] == '#' || p[0] == '\0') {
4675 free(line);
4676 line = NULL;
4677 continue;
4679 cmd = NULL;
4680 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4681 cmd = &got_histedit_cmds[i];
4682 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4683 isspace((unsigned char)p[strlen(cmd->name)])) {
4684 p += strlen(cmd->name);
4685 break;
4687 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4688 p++;
4689 break;
4692 if (i == nitems(got_histedit_cmds)) {
4693 err = histedit_syntax_error(lineno);
4694 break;
4696 while (isspace((unsigned char)p[0]))
4697 p++;
4698 if (cmd->code == GOT_HISTEDIT_MESG) {
4699 if (hle == NULL || hle->logmsg != NULL) {
4700 err = got_error(GOT_ERR_HISTEDIT_CMD);
4701 break;
4703 if (p[0] == '\0') {
4704 err = histedit_edit_logmsg(hle, repo);
4705 if (err)
4706 break;
4707 } else {
4708 hle->logmsg = strdup(p);
4709 if (hle->logmsg == NULL) {
4710 err = got_error_from_errno("strdup");
4711 break;
4714 free(line);
4715 line = NULL;
4716 continue;
4717 } else {
4718 end = p;
4719 while (end[0] && !isspace((unsigned char)end[0]))
4720 end++;
4721 *end = '\0';
4723 err = got_object_resolve_id_str(&commit_id, repo, p);
4724 if (err) {
4725 /* override error code */
4726 err = histedit_syntax_error(lineno);
4727 break;
4730 hle = malloc(sizeof(*hle));
4731 if (hle == NULL) {
4732 err = got_error_from_errno("malloc");
4733 break;
4735 hle->cmd = cmd;
4736 hle->commit_id = commit_id;
4737 hle->logmsg = NULL;
4738 commit_id = NULL;
4739 free(line);
4740 line = NULL;
4741 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4744 free(line);
4745 free(commit_id);
4746 return err;
4749 static const struct got_error *
4750 histedit_check_script(struct got_histedit_list *histedit_cmds,
4751 struct got_object_id_queue *commits, struct got_repository *repo)
4753 const struct got_error *err = NULL;
4754 struct got_object_qid *qid;
4755 struct got_histedit_list_entry *hle;
4756 static char msg[80];
4757 char *id_str;
4759 if (TAILQ_EMPTY(histedit_cmds))
4760 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4761 "histedit script contains no commands");
4762 if (SIMPLEQ_EMPTY(commits))
4763 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4765 SIMPLEQ_FOREACH(qid, commits, entry) {
4766 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4767 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4768 break;
4770 if (hle == NULL) {
4771 err = got_object_id_str(&id_str, qid->id);
4772 if (err)
4773 return err;
4774 snprintf(msg, sizeof(msg),
4775 "commit %s missing from histedit script", id_str);
4776 free(id_str);
4777 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4781 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4782 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4783 "last commit in histedit script cannot be folded");
4785 return NULL;
4788 static const struct got_error *
4789 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4790 const char *path, struct got_object_id_queue *commits,
4791 struct got_repository *repo)
4793 const struct got_error *err = NULL;
4794 char *editor;
4795 FILE *f = NULL;
4797 err = get_editor(&editor);
4798 if (err)
4799 return err;
4801 if (spawn_editor(editor, path) == -1) {
4802 err = got_error_from_errno("failed spawning editor");
4803 goto done;
4806 f = fopen(path, "r");
4807 if (f == NULL) {
4808 err = got_error_from_errno("fopen");
4809 goto done;
4811 err = histedit_parse_list(histedit_cmds, f, repo);
4812 if (err)
4813 goto done;
4815 err = histedit_check_script(histedit_cmds, commits, repo);
4816 done:
4817 if (f && fclose(f) != 0 && err == NULL)
4818 err = got_error_from_errno("fclose");
4819 free(editor);
4820 return err;
4823 static const struct got_error *
4824 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4825 struct got_object_id_queue *, const char *, struct got_repository *);
4827 static const struct got_error *
4828 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4829 struct got_object_id_queue *commits, struct got_repository *repo)
4831 const struct got_error *err;
4832 FILE *f = NULL;
4833 char *path = NULL;
4835 err = got_opentemp_named(&path, &f, "got-histedit");
4836 if (err)
4837 return err;
4839 err = write_cmd_list(f);
4840 if (err)
4841 goto done;
4843 err = histedit_write_commit_list(commits, f, repo);
4844 if (err)
4845 goto done;
4847 if (fclose(f) != 0) {
4848 err = got_error_from_errno("fclose");
4849 goto done;
4851 f = NULL;
4853 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4854 if (err) {
4855 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4856 err->code != GOT_ERR_HISTEDIT_CMD)
4857 goto done;
4858 err = histedit_edit_list_retry(histedit_cmds, err,
4859 commits, path, repo);
4861 done:
4862 if (f && fclose(f) != 0 && err == NULL)
4863 err = got_error_from_errno("fclose");
4864 if (path && unlink(path) != 0 && err == NULL)
4865 err = got_error_from_errno2("unlink", path);
4866 free(path);
4867 return err;
4870 static const struct got_error *
4871 histedit_save_list(struct got_histedit_list *histedit_cmds,
4872 struct got_worktree *worktree, struct got_repository *repo)
4874 const struct got_error *err = NULL;
4875 char *path = NULL;
4876 FILE *f = NULL;
4877 struct got_histedit_list_entry *hle;
4878 struct got_commit_object *commit = NULL;
4880 err = got_worktree_get_histedit_script_path(&path, worktree);
4881 if (err)
4882 return err;
4884 f = fopen(path, "w");
4885 if (f == NULL) {
4886 err = got_error_from_errno2("fopen", path);
4887 goto done;
4889 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4890 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4891 repo);
4892 if (err)
4893 break;
4895 if (hle->logmsg) {
4896 int n = fprintf(f, "%c %s\n",
4897 GOT_HISTEDIT_MESG, hle->logmsg);
4898 if (n < 0) {
4899 err = got_ferror(f, GOT_ERR_IO);
4900 break;
4904 done:
4905 if (f && fclose(f) != 0 && err == NULL)
4906 err = got_error_from_errno("fclose");
4907 free(path);
4908 if (commit)
4909 got_object_commit_close(commit);
4910 return err;
4913 void
4914 histedit_free_list(struct got_histedit_list *histedit_cmds)
4916 struct got_histedit_list_entry *hle;
4918 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4919 TAILQ_REMOVE(histedit_cmds, hle, entry);
4920 free(hle);
4924 static const struct got_error *
4925 histedit_load_list(struct got_histedit_list *histedit_cmds,
4926 const char *path, struct got_repository *repo)
4928 const struct got_error *err = NULL;
4929 FILE *f = NULL;
4931 f = fopen(path, "r");
4932 if (f == NULL) {
4933 err = got_error_from_errno2("fopen", path);
4934 goto done;
4937 err = histedit_parse_list(histedit_cmds, f, repo);
4938 done:
4939 if (f && fclose(f) != 0 && err == NULL)
4940 err = got_error_from_errno("fclose");
4941 return err;
4944 static const struct got_error *
4945 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4946 const struct got_error *edit_err, struct got_object_id_queue *commits,
4947 const char *path, struct got_repository *repo)
4949 const struct got_error *err = NULL, *prev_err = edit_err;
4950 int resp = ' ';
4952 while (resp != 'c' && resp != 'r' && resp != 'a') {
4953 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4954 "or (a)bort: ", getprogname(), prev_err->msg);
4955 resp = getchar();
4956 if (resp == '\n')
4957 resp = getchar();
4958 if (resp == 'c') {
4959 histedit_free_list(histedit_cmds);
4960 err = histedit_run_editor(histedit_cmds, path, commits,
4961 repo);
4962 if (err) {
4963 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4964 err->code != GOT_ERR_HISTEDIT_CMD)
4965 break;
4966 prev_err = err;
4967 resp = ' ';
4968 continue;
4970 break;
4971 } else if (resp == 'r') {
4972 histedit_free_list(histedit_cmds);
4973 err = histedit_edit_script(histedit_cmds,
4974 commits, repo);
4975 if (err) {
4976 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4977 err->code != GOT_ERR_HISTEDIT_CMD)
4978 break;
4979 prev_err = err;
4980 resp = ' ';
4981 continue;
4983 break;
4984 } else if (resp == 'a') {
4985 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4986 break;
4987 } else
4988 printf("invalid response '%c'\n", resp);
4991 return err;
4994 static const struct got_error *
4995 histedit_complete(struct got_worktree *worktree,
4996 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4997 struct got_reference *branch, struct got_repository *repo)
4999 printf("Switching work tree to %s\n",
5000 got_ref_get_symref_target(branch));
5001 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5002 branch, repo);
5005 static const struct got_error *
5006 show_histedit_progress(struct got_commit_object *commit,
5007 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5009 const struct got_error *err;
5010 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5012 err = got_object_id_str(&old_id_str, hle->commit_id);
5013 if (err)
5014 goto done;
5016 if (new_id) {
5017 err = got_object_id_str(&new_id_str, new_id);
5018 if (err)
5019 goto done;
5022 old_id_str[12] = '\0';
5023 if (new_id_str)
5024 new_id_str[12] = '\0';
5026 if (hle->logmsg) {
5027 logmsg = strdup(hle->logmsg);
5028 if (logmsg == NULL) {
5029 err = got_error_from_errno("strdup");
5030 goto done;
5032 trim_logmsg(logmsg, 42);
5033 } else {
5034 err = get_short_logmsg(&logmsg, 42, commit);
5035 if (err)
5036 goto done;
5039 switch (hle->cmd->code) {
5040 case GOT_HISTEDIT_PICK:
5041 case GOT_HISTEDIT_EDIT:
5042 printf("%s -> %s: %s\n", old_id_str,
5043 new_id_str ? new_id_str : "no-op change", logmsg);
5044 break;
5045 case GOT_HISTEDIT_DROP:
5046 case GOT_HISTEDIT_FOLD:
5047 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5048 logmsg);
5049 break;
5050 default:
5051 break;
5054 done:
5055 free(old_id_str);
5056 free(new_id_str);
5057 return err;
5060 static const struct got_error *
5061 histedit_commit(struct got_pathlist_head *merged_paths,
5062 struct got_worktree *worktree, struct got_fileindex *fileindex,
5063 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5064 struct got_repository *repo)
5066 const struct got_error *err;
5067 struct got_commit_object *commit;
5068 struct got_object_id *new_commit_id;
5070 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5071 && hle->logmsg == NULL) {
5072 err = histedit_edit_logmsg(hle, repo);
5073 if (err)
5074 return err;
5077 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5078 if (err)
5079 return err;
5081 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5082 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5083 hle->logmsg, repo);
5084 if (err) {
5085 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5086 goto done;
5087 err = show_histedit_progress(commit, hle, NULL);
5088 } else {
5089 err = show_histedit_progress(commit, hle, new_commit_id);
5090 free(new_commit_id);
5092 done:
5093 got_object_commit_close(commit);
5094 return err;
5097 static const struct got_error *
5098 histedit_skip_commit(struct got_histedit_list_entry *hle,
5099 struct got_worktree *worktree, struct got_repository *repo)
5101 const struct got_error *error;
5102 struct got_commit_object *commit;
5104 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5105 repo);
5106 if (error)
5107 return error;
5109 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5110 if (error)
5111 return error;
5113 error = show_histedit_progress(commit, hle, NULL);
5114 got_object_commit_close(commit);
5115 return error;
5118 static const struct got_error *
5119 cmd_histedit(int argc, char *argv[])
5121 const struct got_error *error = NULL;
5122 struct got_worktree *worktree = NULL;
5123 struct got_fileindex *fileindex = NULL;
5124 struct got_repository *repo = NULL;
5125 char *cwd = NULL;
5126 struct got_reference *branch = NULL;
5127 struct got_reference *tmp_branch = NULL;
5128 struct got_object_id *resume_commit_id = NULL;
5129 struct got_object_id *base_commit_id = NULL;
5130 struct got_object_id *head_commit_id = NULL;
5131 struct got_commit_object *commit = NULL;
5132 int ch, rebase_in_progress = 0, did_something;
5133 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5134 const char *edit_script_path = NULL;
5135 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5136 struct got_object_id_queue commits;
5137 struct got_pathlist_head merged_paths;
5138 const struct got_object_id_queue *parent_ids;
5139 struct got_object_qid *pid;
5140 struct got_histedit_list histedit_cmds;
5141 struct got_histedit_list_entry *hle;
5143 SIMPLEQ_INIT(&commits);
5144 TAILQ_INIT(&histedit_cmds);
5145 TAILQ_INIT(&merged_paths);
5147 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5148 switch (ch) {
5149 case 'a':
5150 abort_edit = 1;
5151 break;
5152 case 'c':
5153 continue_edit = 1;
5154 break;
5155 case 'F':
5156 edit_script_path = optarg;
5157 break;
5158 default:
5159 usage_histedit();
5160 /* NOTREACHED */
5164 argc -= optind;
5165 argv += optind;
5167 #ifndef PROFILE
5168 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5169 "unveil", NULL) == -1)
5170 err(1, "pledge");
5171 #endif
5172 if (abort_edit && continue_edit)
5173 usage_histedit();
5174 if (argc != 0)
5175 usage_histedit();
5178 * This command cannot apply unveil(2) in all cases because the
5179 * user may choose to run an editor to edit the histedit script
5180 * and to edit individual commit log messages.
5181 * unveil(2) traverses exec(2); if an editor is used we have to
5182 * apply unveil after edit script and log messages have been written.
5183 * XXX TODO: Make use of unveil(2) where possible.
5186 cwd = getcwd(NULL, 0);
5187 if (cwd == NULL) {
5188 error = got_error_from_errno("getcwd");
5189 goto done;
5191 error = got_worktree_open(&worktree, cwd);
5192 if (error)
5193 goto done;
5195 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5196 if (error != NULL)
5197 goto done;
5199 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5200 if (error)
5201 goto done;
5202 if (rebase_in_progress) {
5203 error = got_error(GOT_ERR_REBASING);
5204 goto done;
5207 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5208 if (error)
5209 goto done;
5211 if (edit_in_progress && abort_edit) {
5212 error = got_worktree_histedit_continue(&resume_commit_id,
5213 &tmp_branch, &branch, &base_commit_id, &fileindex,
5214 worktree, repo);
5215 if (error)
5216 goto done;
5217 printf("Switching work tree to %s\n",
5218 got_ref_get_symref_target(branch));
5219 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5220 branch, base_commit_id, update_progress, &did_something);
5221 if (error)
5222 goto done;
5223 printf("Histedit of %s aborted\n",
5224 got_ref_get_symref_target(branch));
5225 goto done; /* nothing else to do */
5226 } else if (abort_edit) {
5227 error = got_error(GOT_ERR_NOT_HISTEDIT);
5228 goto done;
5231 if (continue_edit) {
5232 char *path;
5234 if (!edit_in_progress) {
5235 error = got_error(GOT_ERR_NOT_HISTEDIT);
5236 goto done;
5239 error = got_worktree_get_histedit_script_path(&path, worktree);
5240 if (error)
5241 goto done;
5243 error = histedit_load_list(&histedit_cmds, path, repo);
5244 free(path);
5245 if (error)
5246 goto done;
5248 error = got_worktree_histedit_continue(&resume_commit_id,
5249 &tmp_branch, &branch, &base_commit_id, &fileindex,
5250 worktree, repo);
5251 if (error)
5252 goto done;
5254 error = got_ref_resolve(&head_commit_id, repo, branch);
5255 if (error)
5256 goto done;
5258 error = got_object_open_as_commit(&commit, repo,
5259 head_commit_id);
5260 if (error)
5261 goto done;
5262 parent_ids = got_object_commit_get_parent_ids(commit);
5263 pid = SIMPLEQ_FIRST(parent_ids);
5264 if (pid == NULL) {
5265 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5266 goto done;
5268 error = collect_commits(&commits, head_commit_id, pid->id,
5269 base_commit_id, got_worktree_get_path_prefix(worktree),
5270 GOT_ERR_HISTEDIT_PATH, repo);
5271 got_object_commit_close(commit);
5272 commit = NULL;
5273 if (error)
5274 goto done;
5275 } else {
5276 if (edit_in_progress) {
5277 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5278 goto done;
5281 error = got_ref_open(&branch, repo,
5282 got_worktree_get_head_ref_name(worktree), 0);
5283 if (error != NULL)
5284 goto done;
5286 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5287 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5288 "will not edit commit history of a branch outside "
5289 "the \"refs/heads/\" reference namespace");
5290 goto done;
5293 error = got_ref_resolve(&head_commit_id, repo, branch);
5294 got_ref_close(branch);
5295 branch = NULL;
5296 if (error)
5297 goto done;
5299 error = got_object_open_as_commit(&commit, repo,
5300 head_commit_id);
5301 if (error)
5302 goto done;
5303 parent_ids = got_object_commit_get_parent_ids(commit);
5304 pid = SIMPLEQ_FIRST(parent_ids);
5305 if (pid == NULL) {
5306 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5307 goto done;
5309 error = collect_commits(&commits, head_commit_id, pid->id,
5310 got_worktree_get_base_commit_id(worktree),
5311 got_worktree_get_path_prefix(worktree),
5312 GOT_ERR_HISTEDIT_PATH, repo);
5313 got_object_commit_close(commit);
5314 commit = NULL;
5315 if (error)
5316 goto done;
5318 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5319 &base_commit_id, &fileindex, worktree, repo);
5320 if (error)
5321 goto done;
5323 if (edit_script_path) {
5324 error = histedit_load_list(&histedit_cmds,
5325 edit_script_path, repo);
5326 if (error) {
5327 got_worktree_histedit_abort(worktree, fileindex,
5328 repo, branch, base_commit_id,
5329 update_progress, &did_something);
5330 goto done;
5332 } else {
5333 error = histedit_edit_script(&histedit_cmds, &commits,
5334 repo);
5335 if (error) {
5336 got_worktree_histedit_abort(worktree, fileindex,
5337 repo, branch, base_commit_id,
5338 update_progress, &did_something);
5339 goto done;
5344 error = histedit_save_list(&histedit_cmds, worktree,
5345 repo);
5346 if (error) {
5347 got_worktree_histedit_abort(worktree, fileindex,
5348 repo, branch, base_commit_id,
5349 update_progress, &did_something);
5350 goto done;
5355 error = histedit_check_script(&histedit_cmds, &commits, repo);
5356 if (error)
5357 goto done;
5359 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5360 if (resume_commit_id) {
5361 if (got_object_id_cmp(hle->commit_id,
5362 resume_commit_id) != 0)
5363 continue;
5365 resume_commit_id = NULL;
5366 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5367 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5368 error = histedit_skip_commit(hle, worktree,
5369 repo);
5370 } else {
5371 error = histedit_commit(NULL, worktree,
5372 fileindex, tmp_branch, hle, repo);
5374 if (error)
5375 goto done;
5376 continue;
5379 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5380 error = histedit_skip_commit(hle, worktree, repo);
5381 if (error)
5382 goto done;
5383 continue;
5386 error = got_object_open_as_commit(&commit, repo,
5387 hle->commit_id);
5388 if (error)
5389 goto done;
5390 parent_ids = got_object_commit_get_parent_ids(commit);
5391 pid = SIMPLEQ_FIRST(parent_ids);
5393 error = got_worktree_histedit_merge_files(&merged_paths,
5394 worktree, fileindex, pid->id, hle->commit_id, repo,
5395 rebase_progress, &rebase_status, check_cancelled, NULL);
5396 if (error)
5397 goto done;
5398 got_object_commit_close(commit);
5399 commit = NULL;
5401 if (rebase_status == GOT_STATUS_CONFLICT) {
5402 got_worktree_rebase_pathlist_free(&merged_paths);
5403 break;
5406 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5407 char *id_str;
5408 error = got_object_id_str(&id_str, hle->commit_id);
5409 if (error)
5410 goto done;
5411 printf("Stopping histedit for amending commit %s\n",
5412 id_str);
5413 free(id_str);
5414 got_worktree_rebase_pathlist_free(&merged_paths);
5415 error = got_worktree_histedit_postpone(worktree,
5416 fileindex);
5417 goto done;
5420 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5421 error = histedit_skip_commit(hle, worktree, repo);
5422 if (error)
5423 goto done;
5424 continue;
5427 error = histedit_commit(&merged_paths, worktree, fileindex,
5428 tmp_branch, hle, repo);
5429 got_worktree_rebase_pathlist_free(&merged_paths);
5430 if (error)
5431 goto done;
5434 if (rebase_status == GOT_STATUS_CONFLICT) {
5435 error = got_worktree_histedit_postpone(worktree, fileindex);
5436 if (error)
5437 goto done;
5438 error = got_error_msg(GOT_ERR_CONFLICTS,
5439 "conflicts must be resolved before rebasing can continue");
5440 } else
5441 error = histedit_complete(worktree, fileindex, tmp_branch,
5442 branch, repo);
5443 done:
5444 got_object_id_queue_free(&commits);
5445 histedit_free_list(&histedit_cmds);
5446 free(head_commit_id);
5447 free(base_commit_id);
5448 free(resume_commit_id);
5449 if (commit)
5450 got_object_commit_close(commit);
5451 if (branch)
5452 got_ref_close(branch);
5453 if (tmp_branch)
5454 got_ref_close(tmp_branch);
5455 if (worktree)
5456 got_worktree_close(worktree);
5457 if (repo)
5458 got_repo_close(repo);
5459 return error;
5462 __dead static void
5463 usage_stage(void)
5465 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5466 "[file-path ...]\n",
5467 getprogname());
5468 exit(1);
5471 static const struct got_error *
5472 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5473 const char *path, struct got_object_id *blob_id,
5474 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5476 const struct got_error *err = NULL;
5477 char *id_str = NULL;
5479 if (staged_status != GOT_STATUS_ADD &&
5480 staged_status != GOT_STATUS_MODIFY &&
5481 staged_status != GOT_STATUS_DELETE)
5482 return NULL;
5484 if (staged_status == GOT_STATUS_ADD ||
5485 staged_status == GOT_STATUS_MODIFY)
5486 err = got_object_id_str(&id_str, staged_blob_id);
5487 else
5488 err = got_object_id_str(&id_str, blob_id);
5489 if (err)
5490 return err;
5492 printf("%s %c %s\n", id_str, staged_status, path);
5493 free(id_str);
5494 return NULL;
5497 static const struct got_error *
5498 cmd_stage(int argc, char *argv[])
5500 const struct got_error *error = NULL;
5501 struct got_repository *repo = NULL;
5502 struct got_worktree *worktree = NULL;
5503 char *cwd = NULL;
5504 struct got_pathlist_head paths;
5505 struct got_pathlist_entry *pe;
5506 int ch, list_stage = 0, pflag = 0;
5507 FILE *patch_script_file = NULL;
5508 const char *patch_script_path = NULL;
5509 struct choose_patch_arg cpa;
5511 TAILQ_INIT(&paths);
5513 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5514 switch (ch) {
5515 case 'l':
5516 list_stage = 1;
5517 break;
5518 case 'p':
5519 pflag = 1;
5520 break;
5521 case 'F':
5522 patch_script_path = optarg;
5523 break;
5524 default:
5525 usage_stage();
5526 /* NOTREACHED */
5530 argc -= optind;
5531 argv += optind;
5533 #ifndef PROFILE
5534 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5535 "unveil", NULL) == -1)
5536 err(1, "pledge");
5537 #endif
5538 if (list_stage && (pflag || patch_script_path))
5539 errx(1, "-l option cannot be used with other options");
5540 if (patch_script_path && !pflag)
5541 errx(1, "-F option can only be used together with -p option");
5543 cwd = getcwd(NULL, 0);
5544 if (cwd == NULL) {
5545 error = got_error_from_errno("getcwd");
5546 goto done;
5549 error = got_worktree_open(&worktree, cwd);
5550 if (error)
5551 goto done;
5553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5554 if (error != NULL)
5555 goto done;
5557 if (patch_script_path) {
5558 patch_script_file = fopen(patch_script_path, "r");
5559 if (patch_script_file == NULL) {
5560 error = got_error_from_errno2("fopen",
5561 patch_script_path);
5562 goto done;
5565 error = apply_unveil(got_repo_get_path(repo), 1,
5566 got_worktree_get_root_path(worktree));
5567 if (error)
5568 goto done;
5570 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5571 if (error)
5572 goto done;
5574 if (list_stage)
5575 error = got_worktree_status(worktree, &paths, repo,
5576 print_stage, NULL, check_cancelled, NULL);
5577 else {
5578 cpa.patch_script_file = patch_script_file;
5579 cpa.action = "stage";
5580 error = got_worktree_stage(worktree, &paths,
5581 pflag ? NULL : print_status, NULL,
5582 pflag ? choose_patch : NULL, &cpa, repo);
5584 done:
5585 if (patch_script_file && fclose(patch_script_file) == EOF &&
5586 error == NULL)
5587 error = got_error_from_errno2("fclose", patch_script_path);
5588 if (repo)
5589 got_repo_close(repo);
5590 if (worktree)
5591 got_worktree_close(worktree);
5592 TAILQ_FOREACH(pe, &paths, entry)
5593 free((char *)pe->path);
5594 got_pathlist_free(&paths);
5595 free(cwd);
5596 return error;
5599 __dead static void
5600 usage_unstage(void)
5602 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5603 "[file-path ...]\n",
5604 getprogname());
5605 exit(1);
5609 static const struct got_error *
5610 cmd_unstage(int argc, char *argv[])
5612 const struct got_error *error = NULL;
5613 struct got_repository *repo = NULL;
5614 struct got_worktree *worktree = NULL;
5615 char *cwd = NULL;
5616 struct got_pathlist_head paths;
5617 struct got_pathlist_entry *pe;
5618 int ch, did_something = 0, pflag = 0;
5619 FILE *patch_script_file = NULL;
5620 const char *patch_script_path = NULL;
5621 struct choose_patch_arg cpa;
5623 TAILQ_INIT(&paths);
5625 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5626 switch (ch) {
5627 case 'p':
5628 pflag = 1;
5629 break;
5630 case 'F':
5631 patch_script_path = optarg;
5632 break;
5633 default:
5634 usage_unstage();
5635 /* NOTREACHED */
5639 argc -= optind;
5640 argv += optind;
5642 #ifndef PROFILE
5643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5644 "unveil", NULL) == -1)
5645 err(1, "pledge");
5646 #endif
5647 if (patch_script_path && !pflag)
5648 errx(1, "-F option can only be used together with -p option");
5650 cwd = getcwd(NULL, 0);
5651 if (cwd == NULL) {
5652 error = got_error_from_errno("getcwd");
5653 goto done;
5656 error = got_worktree_open(&worktree, cwd);
5657 if (error)
5658 goto done;
5660 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5661 if (error != NULL)
5662 goto done;
5664 if (patch_script_path) {
5665 patch_script_file = fopen(patch_script_path, "r");
5666 if (patch_script_file == NULL) {
5667 error = got_error_from_errno2("fopen",
5668 patch_script_path);
5669 goto done;
5673 error = apply_unveil(got_repo_get_path(repo), 1,
5674 got_worktree_get_root_path(worktree));
5675 if (error)
5676 goto done;
5678 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5679 if (error)
5680 goto done;
5682 cpa.patch_script_file = patch_script_file;
5683 cpa.action = "unstage";
5684 error = got_worktree_unstage(worktree, &paths, update_progress,
5685 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5686 done:
5687 if (patch_script_file && fclose(patch_script_file) == EOF &&
5688 error == NULL)
5689 error = got_error_from_errno2("fclose", patch_script_path);
5690 if (repo)
5691 got_repo_close(repo);
5692 if (worktree)
5693 got_worktree_close(worktree);
5694 TAILQ_FOREACH(pe, &paths, entry)
5695 free((char *)pe->path);
5696 got_pathlist_free(&paths);
5697 free(cwd);
5698 return error;