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 struct got_tag_object *tag = NULL;
1390 int cmp;
1392 name = got_ref_get_name(re->ref);
1393 if (strcmp(name, GOT_REF_HEAD) == 0)
1394 continue;
1395 if (strncmp(name, "refs/", 5) == 0)
1396 name += 5;
1397 if (strncmp(name, "got/", 4) == 0)
1398 continue;
1399 if (strncmp(name, "heads/", 6) == 0)
1400 name += 6;
1401 if (strncmp(name, "remotes/", 8) == 0)
1402 name += 8;
1403 if (strncmp(name, "tags/", 5) == 0) {
1404 err = got_object_open_as_tag(&tag, repo, re->id);
1405 if (err)
1406 break;
1408 cmp = got_object_id_cmp(tag ?
1409 got_object_tag_get_object_id(tag) : re->id, id);
1410 if (tag)
1411 got_object_tag_close(tag);
1412 if (cmp != 0)
1413 continue;
1414 s = refs_str;
1415 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1416 name) == -1) {
1417 err = got_error_from_errno("asprintf");
1418 free(s);
1419 break;
1421 free(s);
1423 err = got_object_id_str(&id_str, id);
1424 if (err)
1425 return err;
1427 printf(GOT_COMMIT_SEP_STR);
1428 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1429 refs_str ? refs_str : "", refs_str ? ")" : "");
1430 free(id_str);
1431 id_str = NULL;
1432 free(refs_str);
1433 refs_str = NULL;
1434 printf("from: %s\n", got_object_commit_get_author(commit));
1435 committer_time = got_object_commit_get_committer_time(commit);
1436 datestr = get_datestr(&committer_time, datebuf);
1437 printf("date: %s UTC\n", datestr);
1438 author = got_object_commit_get_author(commit);
1439 committer = got_object_commit_get_committer(commit);
1440 if (strcmp(author, committer) != 0)
1441 printf("via: %s\n", committer);
1442 if (got_object_commit_get_nparents(commit) > 1) {
1443 const struct got_object_id_queue *parent_ids;
1444 struct got_object_qid *qid;
1445 int n = 1;
1446 parent_ids = got_object_commit_get_parent_ids(commit);
1447 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1448 err = got_object_id_str(&id_str, qid->id);
1449 if (err)
1450 return err;
1451 printf("parent %d: %s\n", n++, id_str);
1452 free(id_str);
1456 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1457 if (logmsg0 == NULL)
1458 return got_error_from_errno("strdup");
1460 logmsg = logmsg0;
1461 do {
1462 line = strsep(&logmsg, "\n");
1463 if (line)
1464 printf(" %s\n", line);
1465 } while (line);
1466 free(logmsg0);
1468 if (show_patch) {
1469 err = print_patch(commit, id, diff_context, repo);
1470 if (err == 0)
1471 printf("\n");
1474 if (fflush(stdout) != 0 && err == NULL)
1475 err = got_error_from_errno("fflush");
1476 return err;
1479 static const struct got_error *
1480 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1481 char *path, int show_patch, int diff_context, int limit,
1482 int first_parent_traversal, struct got_reflist_head *refs)
1484 const struct got_error *err;
1485 struct got_commit_graph *graph;
1487 err = got_commit_graph_open(&graph, root_id, path,
1488 first_parent_traversal, repo);
1489 if (err)
1490 return err;
1491 err = got_commit_graph_iter_start(graph, root_id, repo);
1492 if (err)
1493 goto done;
1494 for (;;) {
1495 struct got_commit_object *commit;
1496 struct got_object_id *id;
1498 if (sigint_received || sigpipe_received)
1499 break;
1501 err = got_commit_graph_iter_next(&id, graph);
1502 if (err) {
1503 if (err->code == GOT_ERR_ITER_COMPLETED) {
1504 err = NULL;
1505 break;
1507 if (err->code != GOT_ERR_ITER_NEED_MORE)
1508 break;
1509 err = got_commit_graph_fetch_commits(graph, 1, repo);
1510 if (err)
1511 break;
1512 else
1513 continue;
1515 if (id == NULL)
1516 break;
1518 err = got_object_open_as_commit(&commit, repo, id);
1519 if (err)
1520 break;
1521 err = print_commit(commit, id, repo, show_patch, diff_context,
1522 refs);
1523 got_object_commit_close(commit);
1524 if (err || (limit && --limit == 0))
1525 break;
1527 done:
1528 got_commit_graph_close(graph);
1529 return err;
1532 __dead static void
1533 usage_log(void)
1535 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1536 "[-r repository-path] [path]\n", getprogname());
1537 exit(1);
1540 static const struct got_error *
1541 cmd_log(int argc, char *argv[])
1543 const struct got_error *error;
1544 struct got_repository *repo = NULL;
1545 struct got_worktree *worktree = NULL;
1546 struct got_commit_object *commit = NULL;
1547 struct got_object_id *id = NULL;
1548 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1549 char *start_commit = NULL;
1550 int diff_context = 3, ch;
1551 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1552 const char *errstr;
1553 struct got_reflist_head refs;
1555 SIMPLEQ_INIT(&refs);
1557 #ifndef PROFILE
1558 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1559 NULL)
1560 == -1)
1561 err(1, "pledge");
1562 #endif
1564 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1565 switch (ch) {
1566 case 'p':
1567 show_patch = 1;
1568 break;
1569 case 'c':
1570 start_commit = optarg;
1571 break;
1572 case 'C':
1573 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1574 &errstr);
1575 if (errstr != NULL)
1576 err(1, "-C option %s", errstr);
1577 break;
1578 case 'l':
1579 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1580 if (errstr != NULL)
1581 err(1, "-l option %s", errstr);
1582 break;
1583 case 'f':
1584 first_parent_traversal = 1;
1585 break;
1586 case 'r':
1587 repo_path = realpath(optarg, NULL);
1588 if (repo_path == NULL)
1589 err(1, "-r option");
1590 got_path_strip_trailing_slashes(repo_path);
1591 break;
1592 default:
1593 usage_log();
1594 /* NOTREACHED */
1598 argc -= optind;
1599 argv += optind;
1601 cwd = getcwd(NULL, 0);
1602 if (cwd == NULL) {
1603 error = got_error_from_errno("getcwd");
1604 goto done;
1607 error = got_worktree_open(&worktree, cwd);
1608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 goto done;
1610 error = NULL;
1612 if (argc == 0) {
1613 path = strdup("");
1614 if (path == NULL) {
1615 error = got_error_from_errno("strdup");
1616 goto done;
1618 } else if (argc == 1) {
1619 if (worktree) {
1620 error = got_worktree_resolve_path(&path, worktree,
1621 argv[0]);
1622 if (error)
1623 goto done;
1624 } else {
1625 path = strdup(argv[0]);
1626 if (path == NULL) {
1627 error = got_error_from_errno("strdup");
1628 goto done;
1631 } else
1632 usage_log();
1634 if (repo_path == NULL) {
1635 repo_path = worktree ?
1636 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1638 if (repo_path == NULL) {
1639 error = got_error_from_errno("strdup");
1640 goto done;
1643 error = got_repo_open(&repo, repo_path);
1644 if (error != NULL)
1645 goto done;
1647 error = apply_unveil(got_repo_get_path(repo), 1,
1648 worktree ? got_worktree_get_root_path(worktree) : NULL);
1649 if (error)
1650 goto done;
1652 if (start_commit == NULL) {
1653 struct got_reference *head_ref;
1654 error = got_ref_open(&head_ref, repo,
1655 worktree ? got_worktree_get_head_ref_name(worktree)
1656 : GOT_REF_HEAD, 0);
1657 if (error != NULL)
1658 return error;
1659 error = got_ref_resolve(&id, repo, head_ref);
1660 got_ref_close(head_ref);
1661 if (error != NULL)
1662 return error;
1663 error = got_object_open_as_commit(&commit, repo, id);
1664 } else {
1665 struct got_reference *ref;
1666 error = got_ref_open(&ref, repo, start_commit, 0);
1667 if (error == NULL) {
1668 int obj_type;
1669 error = got_ref_resolve(&id, repo, ref);
1670 got_ref_close(ref);
1671 if (error != NULL)
1672 goto done;
1673 error = got_object_get_type(&obj_type, repo, id);
1674 if (error != NULL)
1675 goto done;
1676 if (obj_type == GOT_OBJ_TYPE_TAG) {
1677 struct got_tag_object *tag;
1678 error = got_object_open_as_tag(&tag, repo, id);
1679 if (error != NULL)
1680 goto done;
1681 if (got_object_tag_get_object_type(tag) !=
1682 GOT_OBJ_TYPE_COMMIT) {
1683 got_object_tag_close(tag);
1684 error = got_error(GOT_ERR_OBJ_TYPE);
1685 goto done;
1687 free(id);
1688 id = got_object_id_dup(
1689 got_object_tag_get_object_id(tag));
1690 if (id == NULL)
1691 error = got_error_from_errno(
1692 "got_object_id_dup");
1693 got_object_tag_close(tag);
1694 if (error)
1695 goto done;
1696 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1697 error = got_error(GOT_ERR_OBJ_TYPE);
1698 goto done;
1700 error = got_object_open_as_commit(&commit, repo, id);
1701 if (error != NULL)
1702 goto done;
1704 if (commit == NULL) {
1705 error = got_repo_match_object_id_prefix(&id,
1706 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1707 if (error != NULL)
1708 return error;
1711 if (error != NULL)
1712 goto done;
1714 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1715 if (error != NULL)
1716 goto done;
1717 if (in_repo_path) {
1718 free(path);
1719 path = in_repo_path;
1722 error = got_ref_list(&refs, repo);
1723 if (error)
1724 goto done;
1726 error = print_commits(id, repo, path, show_patch,
1727 diff_context, limit, first_parent_traversal, &refs);
1728 done:
1729 free(path);
1730 free(repo_path);
1731 free(cwd);
1732 free(id);
1733 if (worktree)
1734 got_worktree_close(worktree);
1735 if (repo) {
1736 const struct got_error *repo_error;
1737 repo_error = got_repo_close(repo);
1738 if (error == NULL)
1739 error = repo_error;
1741 got_ref_list_free(&refs);
1742 return error;
1745 __dead static void
1746 usage_diff(void)
1748 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1749 "[object1 object2 | path]\n", getprogname());
1750 exit(1);
1753 struct print_diff_arg {
1754 struct got_repository *repo;
1755 struct got_worktree *worktree;
1756 int diff_context;
1757 const char *id_str;
1758 int header_shown;
1759 int diff_staged;
1762 static const struct got_error *
1763 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1764 const char *path, struct got_object_id *blob_id,
1765 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1767 struct print_diff_arg *a = arg;
1768 const struct got_error *err = NULL;
1769 struct got_blob_object *blob1 = NULL;
1770 FILE *f2 = NULL;
1771 char *abspath = NULL, *label1 = NULL;
1772 struct stat sb;
1774 if (a->diff_staged) {
1775 if (staged_status != GOT_STATUS_MODIFY &&
1776 staged_status != GOT_STATUS_ADD &&
1777 staged_status != GOT_STATUS_DELETE)
1778 return NULL;
1779 } else {
1780 if (staged_status == GOT_STATUS_DELETE)
1781 return NULL;
1782 if (status != GOT_STATUS_MODIFY &&
1783 status != GOT_STATUS_ADD &&
1784 status != GOT_STATUS_DELETE &&
1785 status != GOT_STATUS_CONFLICT)
1786 return NULL;
1789 if (!a->header_shown) {
1790 printf("diff %s %s%s\n", a->id_str,
1791 got_worktree_get_root_path(a->worktree),
1792 a->diff_staged ? " (staged changes)" : "");
1793 a->header_shown = 1;
1796 if (a->diff_staged) {
1797 const char *label1 = NULL, *label2 = NULL;
1798 switch (staged_status) {
1799 case GOT_STATUS_MODIFY:
1800 label1 = path;
1801 label2 = path;
1802 break;
1803 case GOT_STATUS_ADD:
1804 label2 = path;
1805 break;
1806 case GOT_STATUS_DELETE:
1807 label1 = path;
1808 break;
1809 default:
1810 return got_error(GOT_ERR_FILE_STATUS);
1812 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1813 label1, label2, a->diff_context, a->repo, stdout);
1816 if (staged_status == GOT_STATUS_ADD ||
1817 staged_status == GOT_STATUS_MODIFY) {
1818 char *id_str;
1819 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1820 8192);
1821 if (err)
1822 goto done;
1823 err = got_object_id_str(&id_str, staged_blob_id);
1824 if (err)
1825 goto done;
1826 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1827 err = got_error_from_errno("asprintf");
1828 free(id_str);
1829 goto done;
1831 free(id_str);
1832 } else if (status != GOT_STATUS_ADD) {
1833 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1834 if (err)
1835 goto done;
1838 if (status != GOT_STATUS_DELETE) {
1839 if (asprintf(&abspath, "%s/%s",
1840 got_worktree_get_root_path(a->worktree), path) == -1) {
1841 err = got_error_from_errno("asprintf");
1842 goto done;
1845 f2 = fopen(abspath, "r");
1846 if (f2 == NULL) {
1847 err = got_error_from_errno2("fopen", abspath);
1848 goto done;
1850 if (lstat(abspath, &sb) == -1) {
1851 err = got_error_from_errno2("lstat", abspath);
1852 goto done;
1854 } else
1855 sb.st_size = 0;
1857 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1858 a->diff_context, stdout);
1859 done:
1860 if (blob1)
1861 got_object_blob_close(blob1);
1862 if (f2 && fclose(f2) != 0 && err == NULL)
1863 err = got_error_from_errno("fclose");
1864 free(abspath);
1865 return err;
1868 static const struct got_error *
1869 match_object_id(struct got_object_id **id, char **label,
1870 const char *id_str, int obj_type, struct got_repository *repo)
1872 const struct got_error *err;
1873 struct got_tag_object *tag;
1874 struct got_reference *ref = NULL;
1876 *id = NULL;
1877 *label = NULL;
1879 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1880 if (err == NULL) {
1881 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1882 if (*id == NULL)
1883 err = got_error_from_errno("got_object_id_dup");
1884 if (asprintf(label, "refs/tags/%s",
1885 got_object_tag_get_name(tag)) == -1)
1886 err = got_error_from_errno("asprintf");
1887 got_object_tag_close(tag);
1888 return err;
1889 } else if (err->code != GOT_ERR_NO_OBJ)
1890 return err;
1892 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1893 if (err) {
1894 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 return err;
1896 err = got_ref_open(&ref, repo, id_str, 0);
1897 if (err != NULL)
1898 goto done;
1899 *label = strdup(got_ref_get_name(ref));
1900 if (*label == NULL) {
1901 err = got_error_from_errno("strdup");
1902 goto done;
1904 err = got_ref_resolve(id, repo, ref);
1905 } else {
1906 err = got_object_id_str(label, *id);
1907 if (*label == NULL) {
1908 err = got_error_from_errno("strdup");
1909 goto done;
1912 done:
1913 if (ref)
1914 got_ref_close(ref);
1915 return err;
1919 static const struct got_error *
1920 cmd_diff(int argc, char *argv[])
1922 const struct got_error *error;
1923 struct got_repository *repo = NULL;
1924 struct got_worktree *worktree = NULL;
1925 char *cwd = NULL, *repo_path = NULL;
1926 struct got_object_id *id1 = NULL, *id2 = NULL;
1927 const char *id_str1 = NULL, *id_str2 = NULL;
1928 char *label1 = NULL, *label2 = NULL;
1929 int type1, type2;
1930 int diff_context = 3, diff_staged = 0, ch;
1931 const char *errstr;
1932 char *path = NULL;
1934 #ifndef PROFILE
1935 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1936 NULL) == -1)
1937 err(1, "pledge");
1938 #endif
1940 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1941 switch (ch) {
1942 case 'C':
1943 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1944 if (errstr != NULL)
1945 err(1, "-C option %s", errstr);
1946 break;
1947 case 'r':
1948 repo_path = realpath(optarg, NULL);
1949 if (repo_path == NULL)
1950 err(1, "-r option");
1951 got_path_strip_trailing_slashes(repo_path);
1952 break;
1953 case 's':
1954 diff_staged = 1;
1955 break;
1956 default:
1957 usage_diff();
1958 /* NOTREACHED */
1962 argc -= optind;
1963 argv += optind;
1965 cwd = getcwd(NULL, 0);
1966 if (cwd == NULL) {
1967 error = got_error_from_errno("getcwd");
1968 goto done;
1970 error = got_worktree_open(&worktree, cwd);
1971 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1972 goto done;
1973 if (argc <= 1) {
1974 if (worktree == NULL) {
1975 error = got_error(GOT_ERR_NOT_WORKTREE);
1976 goto done;
1978 if (repo_path)
1979 errx(1,
1980 "-r option can't be used when diffing a work tree");
1981 repo_path = strdup(got_worktree_get_repo_path(worktree));
1982 if (repo_path == NULL) {
1983 error = got_error_from_errno("strdup");
1984 goto done;
1986 if (argc == 1) {
1987 error = got_worktree_resolve_path(&path, worktree,
1988 argv[0]);
1989 if (error)
1990 goto done;
1991 } else {
1992 path = strdup("");
1993 if (path == NULL) {
1994 error = got_error_from_errno("strdup");
1995 goto done;
1998 } else if (argc == 2) {
1999 if (diff_staged)
2000 errx(1, "-s option can't be used when diffing "
2001 "objects in repository");
2002 id_str1 = argv[0];
2003 id_str2 = argv[1];
2004 if (worktree && repo_path == NULL) {
2005 repo_path =
2006 strdup(got_worktree_get_repo_path(worktree));
2007 if (repo_path == NULL) {
2008 error = got_error_from_errno("strdup");
2009 goto done;
2012 } else
2013 usage_diff();
2015 if (repo_path == NULL) {
2016 repo_path = getcwd(NULL, 0);
2017 if (repo_path == NULL)
2018 return got_error_from_errno("getcwd");
2021 error = got_repo_open(&repo, repo_path);
2022 free(repo_path);
2023 if (error != NULL)
2024 goto done;
2026 error = apply_unveil(got_repo_get_path(repo), 1,
2027 worktree ? got_worktree_get_root_path(worktree) : NULL);
2028 if (error)
2029 goto done;
2031 if (argc <= 1) {
2032 struct print_diff_arg arg;
2033 struct got_pathlist_head paths;
2034 char *id_str;
2036 TAILQ_INIT(&paths);
2038 error = got_object_id_str(&id_str,
2039 got_worktree_get_base_commit_id(worktree));
2040 if (error)
2041 goto done;
2042 arg.repo = repo;
2043 arg.worktree = worktree;
2044 arg.diff_context = diff_context;
2045 arg.id_str = id_str;
2046 arg.header_shown = 0;
2047 arg.diff_staged = diff_staged;
2049 error = got_pathlist_append(&paths, path, NULL);
2050 if (error)
2051 goto done;
2053 error = got_worktree_status(worktree, &paths, repo, print_diff,
2054 &arg, check_cancelled, NULL);
2055 free(id_str);
2056 got_pathlist_free(&paths);
2057 goto done;
2060 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2061 if (error)
2062 goto done;
2064 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2065 if (error)
2066 goto done;
2068 error = got_object_get_type(&type1, repo, id1);
2069 if (error)
2070 goto done;
2072 error = got_object_get_type(&type2, repo, id2);
2073 if (error)
2074 goto done;
2076 if (type1 != type2) {
2077 error = got_error(GOT_ERR_OBJ_TYPE);
2078 goto done;
2081 switch (type1) {
2082 case GOT_OBJ_TYPE_BLOB:
2083 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2084 diff_context, repo, stdout);
2085 break;
2086 case GOT_OBJ_TYPE_TREE:
2087 error = got_diff_objects_as_trees(id1, id2, "", "",
2088 diff_context, repo, stdout);
2089 break;
2090 case GOT_OBJ_TYPE_COMMIT:
2091 printf("diff %s %s\n", label1, label2);
2092 error = got_diff_objects_as_commits(id1, id2, diff_context,
2093 repo, stdout);
2094 break;
2095 default:
2096 error = got_error(GOT_ERR_OBJ_TYPE);
2099 done:
2100 free(label1);
2101 free(label2);
2102 free(id1);
2103 free(id2);
2104 free(path);
2105 if (worktree)
2106 got_worktree_close(worktree);
2107 if (repo) {
2108 const struct got_error *repo_error;
2109 repo_error = got_repo_close(repo);
2110 if (error == NULL)
2111 error = repo_error;
2113 return error;
2116 __dead static void
2117 usage_blame(void)
2119 fprintf(stderr,
2120 "usage: %s blame [-c commit] [-r repository-path] path\n",
2121 getprogname());
2122 exit(1);
2125 static const struct got_error *
2126 cmd_blame(int argc, char *argv[])
2128 const struct got_error *error;
2129 struct got_repository *repo = NULL;
2130 struct got_worktree *worktree = NULL;
2131 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2132 struct got_object_id *commit_id = NULL;
2133 char *commit_id_str = NULL;
2134 int ch;
2136 #ifndef PROFILE
2137 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2138 NULL) == -1)
2139 err(1, "pledge");
2140 #endif
2142 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2143 switch (ch) {
2144 case 'c':
2145 commit_id_str = optarg;
2146 break;
2147 case 'r':
2148 repo_path = realpath(optarg, NULL);
2149 if (repo_path == NULL)
2150 err(1, "-r option");
2151 got_path_strip_trailing_slashes(repo_path);
2152 break;
2153 default:
2154 usage_blame();
2155 /* NOTREACHED */
2159 argc -= optind;
2160 argv += optind;
2162 if (argc == 1)
2163 path = argv[0];
2164 else
2165 usage_blame();
2167 cwd = getcwd(NULL, 0);
2168 if (cwd == NULL) {
2169 error = got_error_from_errno("getcwd");
2170 goto done;
2172 if (repo_path == NULL) {
2173 error = got_worktree_open(&worktree, cwd);
2174 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2175 goto done;
2176 else
2177 error = NULL;
2178 if (worktree) {
2179 repo_path =
2180 strdup(got_worktree_get_repo_path(worktree));
2181 if (repo_path == NULL)
2182 error = got_error_from_errno("strdup");
2183 if (error)
2184 goto done;
2185 } else {
2186 repo_path = strdup(cwd);
2187 if (repo_path == NULL) {
2188 error = got_error_from_errno("strdup");
2189 goto done;
2194 error = got_repo_open(&repo, repo_path);
2195 if (error != NULL)
2196 goto done;
2198 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2199 if (error)
2200 goto done;
2202 if (worktree) {
2203 const char *prefix = got_worktree_get_path_prefix(worktree);
2204 char *p, *worktree_subdir = cwd +
2205 strlen(got_worktree_get_root_path(worktree));
2206 if (asprintf(&p, "%s%s%s%s%s",
2207 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2208 worktree_subdir, worktree_subdir[0] ? "/" : "",
2209 path) == -1) {
2210 error = got_error_from_errno("asprintf");
2211 goto done;
2213 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2214 free(p);
2215 } else {
2216 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2218 if (error)
2219 goto done;
2221 if (commit_id_str == NULL) {
2222 struct got_reference *head_ref;
2223 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2224 if (error != NULL)
2225 goto done;
2226 error = got_ref_resolve(&commit_id, repo, head_ref);
2227 got_ref_close(head_ref);
2228 if (error != NULL)
2229 goto done;
2230 } else {
2231 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2232 if (error)
2233 goto done;
2236 error = got_blame(in_repo_path, commit_id, repo, stdout);
2237 done:
2238 free(in_repo_path);
2239 free(repo_path);
2240 free(cwd);
2241 free(commit_id);
2242 if (worktree)
2243 got_worktree_close(worktree);
2244 if (repo) {
2245 const struct got_error *repo_error;
2246 repo_error = got_repo_close(repo);
2247 if (error == NULL)
2248 error = repo_error;
2250 return error;
2253 __dead static void
2254 usage_tree(void)
2256 fprintf(stderr,
2257 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2258 getprogname());
2259 exit(1);
2262 static void
2263 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2264 const char *root_path)
2266 int is_root_path = (strcmp(path, root_path) == 0);
2267 const char *modestr = "";
2269 path += strlen(root_path);
2270 while (path[0] == '/')
2271 path++;
2273 if (S_ISLNK(te->mode))
2274 modestr = "@";
2275 else if (S_ISDIR(te->mode))
2276 modestr = "/";
2277 else if (te->mode & S_IXUSR)
2278 modestr = "*";
2280 printf("%s%s%s%s%s\n", id ? id : "", path,
2281 is_root_path ? "" : "/", te->name, modestr);
2284 static const struct got_error *
2285 print_tree(const char *path, struct got_object_id *commit_id,
2286 int show_ids, int recurse, const char *root_path,
2287 struct got_repository *repo)
2289 const struct got_error *err = NULL;
2290 struct got_object_id *tree_id = NULL;
2291 struct got_tree_object *tree = NULL;
2292 const struct got_tree_entries *entries;
2293 struct got_tree_entry *te;
2295 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2296 if (err)
2297 goto done;
2299 err = got_object_open_as_tree(&tree, repo, tree_id);
2300 if (err)
2301 goto done;
2302 entries = got_object_tree_get_entries(tree);
2303 te = SIMPLEQ_FIRST(&entries->head);
2304 while (te) {
2305 char *id = NULL;
2307 if (sigint_received || sigpipe_received)
2308 break;
2310 if (show_ids) {
2311 char *id_str;
2312 err = got_object_id_str(&id_str, te->id);
2313 if (err)
2314 goto done;
2315 if (asprintf(&id, "%s ", id_str) == -1) {
2316 err = got_error_from_errno("asprintf");
2317 free(id_str);
2318 goto done;
2320 free(id_str);
2322 print_entry(te, id, path, root_path);
2323 free(id);
2325 if (recurse && S_ISDIR(te->mode)) {
2326 char *child_path;
2327 if (asprintf(&child_path, "%s%s%s", path,
2328 path[0] == '/' && path[1] == '\0' ? "" : "/",
2329 te->name) == -1) {
2330 err = got_error_from_errno("asprintf");
2331 goto done;
2333 err = print_tree(child_path, commit_id, show_ids, 1,
2334 root_path, repo);
2335 free(child_path);
2336 if (err)
2337 goto done;
2340 te = SIMPLEQ_NEXT(te, entry);
2342 done:
2343 if (tree)
2344 got_object_tree_close(tree);
2345 free(tree_id);
2346 return err;
2349 static const struct got_error *
2350 cmd_tree(int argc, char *argv[])
2352 const struct got_error *error;
2353 struct got_repository *repo = NULL;
2354 struct got_worktree *worktree = NULL;
2355 const char *path;
2356 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2357 struct got_object_id *commit_id = NULL;
2358 char *commit_id_str = NULL;
2359 int show_ids = 0, recurse = 0;
2360 int ch;
2362 #ifndef PROFILE
2363 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2364 NULL) == -1)
2365 err(1, "pledge");
2366 #endif
2368 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2369 switch (ch) {
2370 case 'c':
2371 commit_id_str = optarg;
2372 break;
2373 case 'r':
2374 repo_path = realpath(optarg, NULL);
2375 if (repo_path == NULL)
2376 err(1, "-r option");
2377 got_path_strip_trailing_slashes(repo_path);
2378 break;
2379 case 'i':
2380 show_ids = 1;
2381 break;
2382 case 'R':
2383 recurse = 1;
2384 break;
2385 default:
2386 usage_tree();
2387 /* NOTREACHED */
2391 argc -= optind;
2392 argv += optind;
2394 if (argc == 1)
2395 path = argv[0];
2396 else if (argc > 1)
2397 usage_tree();
2398 else
2399 path = NULL;
2401 cwd = getcwd(NULL, 0);
2402 if (cwd == NULL) {
2403 error = got_error_from_errno("getcwd");
2404 goto done;
2406 if (repo_path == NULL) {
2407 error = got_worktree_open(&worktree, cwd);
2408 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2409 goto done;
2410 else
2411 error = NULL;
2412 if (worktree) {
2413 repo_path =
2414 strdup(got_worktree_get_repo_path(worktree));
2415 if (repo_path == NULL)
2416 error = got_error_from_errno("strdup");
2417 if (error)
2418 goto done;
2419 } else {
2420 repo_path = strdup(cwd);
2421 if (repo_path == NULL) {
2422 error = got_error_from_errno("strdup");
2423 goto done;
2428 error = got_repo_open(&repo, repo_path);
2429 if (error != NULL)
2430 goto done;
2432 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2433 if (error)
2434 goto done;
2436 if (path == NULL) {
2437 if (worktree) {
2438 char *p, *worktree_subdir = cwd +
2439 strlen(got_worktree_get_root_path(worktree));
2440 if (asprintf(&p, "%s/%s",
2441 got_worktree_get_path_prefix(worktree),
2442 worktree_subdir) == -1) {
2443 error = got_error_from_errno("asprintf");
2444 goto done;
2446 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2447 free(p);
2448 if (error)
2449 goto done;
2450 } else
2451 path = "/";
2453 if (in_repo_path == NULL) {
2454 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2455 if (error != NULL)
2456 goto done;
2459 if (commit_id_str == NULL) {
2460 struct got_reference *head_ref;
2461 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2462 if (error != NULL)
2463 goto done;
2464 error = got_ref_resolve(&commit_id, repo, head_ref);
2465 got_ref_close(head_ref);
2466 if (error != NULL)
2467 goto done;
2468 } else {
2469 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2470 if (error)
2471 goto done;
2474 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2475 in_repo_path, repo);
2476 done:
2477 free(in_repo_path);
2478 free(repo_path);
2479 free(cwd);
2480 free(commit_id);
2481 if (worktree)
2482 got_worktree_close(worktree);
2483 if (repo) {
2484 const struct got_error *repo_error;
2485 repo_error = got_repo_close(repo);
2486 if (error == NULL)
2487 error = repo_error;
2489 return error;
2492 __dead static void
2493 usage_status(void)
2495 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2496 exit(1);
2499 static const struct got_error *
2500 print_status(void *arg, unsigned char status, unsigned char staged_status,
2501 const char *path, struct got_object_id *blob_id,
2502 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2504 if (status == staged_status && (status == GOT_STATUS_DELETE))
2505 status = GOT_STATUS_NO_CHANGE;
2506 printf("%c%c %s\n", status, staged_status, path);
2507 return NULL;
2510 static const struct got_error *
2511 cmd_status(int argc, char *argv[])
2513 const struct got_error *error = NULL;
2514 struct got_repository *repo = NULL;
2515 struct got_worktree *worktree = NULL;
2516 char *cwd = NULL;
2517 struct got_pathlist_head paths;
2518 struct got_pathlist_entry *pe;
2519 int ch;
2521 TAILQ_INIT(&paths);
2523 while ((ch = getopt(argc, argv, "")) != -1) {
2524 switch (ch) {
2525 default:
2526 usage_status();
2527 /* NOTREACHED */
2531 argc -= optind;
2532 argv += optind;
2534 #ifndef PROFILE
2535 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2536 NULL) == -1)
2537 err(1, "pledge");
2538 #endif
2539 cwd = getcwd(NULL, 0);
2540 if (cwd == NULL) {
2541 error = got_error_from_errno("getcwd");
2542 goto done;
2545 error = got_worktree_open(&worktree, cwd);
2546 if (error != NULL)
2547 goto done;
2549 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2550 if (error != NULL)
2551 goto done;
2553 error = apply_unveil(got_repo_get_path(repo), 1,
2554 got_worktree_get_root_path(worktree));
2555 if (error)
2556 goto done;
2558 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2559 if (error)
2560 goto done;
2562 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2563 check_cancelled, NULL);
2564 done:
2565 TAILQ_FOREACH(pe, &paths, entry)
2566 free((char *)pe->path);
2567 got_pathlist_free(&paths);
2568 free(cwd);
2569 return error;
2572 __dead static void
2573 usage_ref(void)
2575 fprintf(stderr,
2576 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2577 getprogname());
2578 exit(1);
2581 static const struct got_error *
2582 list_refs(struct got_repository *repo)
2584 static const struct got_error *err = NULL;
2585 struct got_reflist_head refs;
2586 struct got_reflist_entry *re;
2588 SIMPLEQ_INIT(&refs);
2589 err = got_ref_list(&refs, repo);
2590 if (err)
2591 return err;
2593 SIMPLEQ_FOREACH(re, &refs, entry) {
2594 char *refstr;
2595 refstr = got_ref_to_str(re->ref);
2596 if (refstr == NULL)
2597 return got_error_from_errno("got_ref_to_str");
2598 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2599 free(refstr);
2602 got_ref_list_free(&refs);
2603 return NULL;
2606 static const struct got_error *
2607 delete_ref(struct got_repository *repo, const char *refname)
2609 const struct got_error *err = NULL;
2610 struct got_reference *ref;
2612 err = got_ref_open(&ref, repo, refname, 0);
2613 if (err)
2614 return err;
2616 err = got_ref_delete(ref, repo);
2617 got_ref_close(ref);
2618 return err;
2621 static const struct got_error *
2622 add_ref(struct got_repository *repo, const char *refname, const char *target)
2624 const struct got_error *err = NULL;
2625 struct got_object_id *id;
2626 struct got_reference *ref = NULL;
2629 * Don't let the user create a reference named '-'.
2630 * While technically a valid reference name, this case is usually
2631 * an unintended typo.
2633 if (refname[0] == '-' && refname[1] == '\0')
2634 return got_error(GOT_ERR_BAD_REF_NAME);
2636 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2637 repo);
2638 if (err) {
2639 struct got_reference *target_ref;
2641 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2642 return err;
2643 err = got_ref_open(&target_ref, repo, target, 0);
2644 if (err)
2645 return err;
2646 err = got_ref_resolve(&id, repo, target_ref);
2647 got_ref_close(target_ref);
2648 if (err)
2649 return err;
2652 err = got_ref_alloc(&ref, refname, id);
2653 if (err)
2654 goto done;
2656 err = got_ref_write(ref, repo);
2657 done:
2658 if (ref)
2659 got_ref_close(ref);
2660 free(id);
2661 return err;
2664 static const struct got_error *
2665 add_symref(struct got_repository *repo, const char *refname, const char *target)
2667 const struct got_error *err = NULL;
2668 struct got_reference *ref = NULL;
2669 struct got_reference *target_ref = NULL;
2672 * Don't let the user create a reference named '-'.
2673 * While technically a valid reference name, this case is usually
2674 * an unintended typo.
2676 if (refname[0] == '-' && refname[1] == '\0')
2677 return got_error(GOT_ERR_BAD_REF_NAME);
2679 err = got_ref_open(&target_ref, repo, target, 0);
2680 if (err)
2681 return err;
2683 err = got_ref_alloc_symref(&ref, refname, target_ref);
2684 if (err)
2685 goto done;
2687 err = got_ref_write(ref, repo);
2688 done:
2689 if (target_ref)
2690 got_ref_close(target_ref);
2691 if (ref)
2692 got_ref_close(ref);
2693 return err;
2696 static const struct got_error *
2697 cmd_ref(int argc, char *argv[])
2699 const struct got_error *error = NULL;
2700 struct got_repository *repo = NULL;
2701 struct got_worktree *worktree = NULL;
2702 char *cwd = NULL, *repo_path = NULL;
2703 int ch, do_list = 0, create_symref = 0;
2704 const char *delref = NULL;
2706 /* TODO: Add -s option for adding symbolic references. */
2707 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2708 switch (ch) {
2709 case 'd':
2710 delref = optarg;
2711 break;
2712 case 'r':
2713 repo_path = realpath(optarg, NULL);
2714 if (repo_path == NULL)
2715 err(1, "-r option");
2716 got_path_strip_trailing_slashes(repo_path);
2717 break;
2718 case 'l':
2719 do_list = 1;
2720 break;
2721 case 's':
2722 create_symref = 1;
2723 break;
2724 default:
2725 usage_ref();
2726 /* NOTREACHED */
2730 if (do_list && delref)
2731 errx(1, "-l and -d options are mutually exclusive\n");
2733 argc -= optind;
2734 argv += optind;
2736 if (do_list || delref) {
2737 if (create_symref)
2738 errx(1, "-s option cannot be used together with the "
2739 "-l or -d options");
2740 if (argc > 0)
2741 usage_ref();
2742 } else if (argc != 2)
2743 usage_ref();
2745 #ifndef PROFILE
2746 if (do_list) {
2747 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2748 NULL) == -1)
2749 err(1, "pledge");
2750 } else {
2751 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2752 "sendfd unveil", NULL) == -1)
2753 err(1, "pledge");
2755 #endif
2756 cwd = getcwd(NULL, 0);
2757 if (cwd == NULL) {
2758 error = got_error_from_errno("getcwd");
2759 goto done;
2762 if (repo_path == NULL) {
2763 error = got_worktree_open(&worktree, cwd);
2764 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2765 goto done;
2766 else
2767 error = NULL;
2768 if (worktree) {
2769 repo_path =
2770 strdup(got_worktree_get_repo_path(worktree));
2771 if (repo_path == NULL)
2772 error = got_error_from_errno("strdup");
2773 if (error)
2774 goto done;
2775 } else {
2776 repo_path = strdup(cwd);
2777 if (repo_path == NULL) {
2778 error = got_error_from_errno("strdup");
2779 goto done;
2784 error = got_repo_open(&repo, repo_path);
2785 if (error != NULL)
2786 goto done;
2788 error = apply_unveil(got_repo_get_path(repo), do_list,
2789 worktree ? got_worktree_get_root_path(worktree) : NULL);
2790 if (error)
2791 goto done;
2793 if (do_list)
2794 error = list_refs(repo);
2795 else if (delref)
2796 error = delete_ref(repo, delref);
2797 else if (create_symref)
2798 error = add_symref(repo, argv[0], argv[1]);
2799 else
2800 error = add_ref(repo, argv[0], argv[1]);
2801 done:
2802 if (repo)
2803 got_repo_close(repo);
2804 if (worktree)
2805 got_worktree_close(worktree);
2806 free(cwd);
2807 free(repo_path);
2808 return error;
2811 __dead static void
2812 usage_branch(void)
2814 fprintf(stderr,
2815 "usage: %s branch [-r repository] -l | -d name | "
2816 "name [base-branch]\n", getprogname());
2817 exit(1);
2820 static const struct got_error *
2821 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2823 static const struct got_error *err = NULL;
2824 struct got_reflist_head refs;
2825 struct got_reflist_entry *re;
2827 SIMPLEQ_INIT(&refs);
2829 err = got_ref_list(&refs, repo);
2830 if (err)
2831 return err;
2833 SIMPLEQ_FOREACH(re, &refs, entry) {
2834 const char *refname, *marker = " ";
2835 char *refstr;
2836 refname = got_ref_get_name(re->ref);
2837 if (strncmp(refname, "refs/heads/", 11) != 0)
2838 continue;
2839 if (worktree && strcmp(refname,
2840 got_worktree_get_head_ref_name(worktree)) == 0) {
2841 struct got_object_id *id = NULL;
2842 err = got_ref_resolve(&id, repo, re->ref);
2843 if (err)
2844 return err;
2845 if (got_object_id_cmp(id,
2846 got_worktree_get_base_commit_id(worktree)) == 0)
2847 marker = "* ";
2848 else
2849 marker = "~ ";
2850 free(id);
2852 refname += 11;
2853 refstr = got_ref_to_str(re->ref);
2854 if (refstr == NULL)
2855 return got_error_from_errno("got_ref_to_str");
2856 printf("%s%s: %s\n", marker, refname, refstr);
2857 free(refstr);
2860 got_ref_list_free(&refs);
2861 return NULL;
2864 static const struct got_error *
2865 delete_branch(struct got_repository *repo, const char *branch_name)
2867 const struct got_error *err = NULL;
2868 struct got_reference *ref;
2869 char *refname;
2871 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2872 return got_error_from_errno("asprintf");
2874 err = got_ref_open(&ref, repo, refname, 0);
2875 if (err)
2876 goto done;
2878 err = got_ref_delete(ref, repo);
2879 got_ref_close(ref);
2880 done:
2881 free(refname);
2882 return err;
2885 static const struct got_error *
2886 add_branch(struct got_repository *repo, const char *branch_name,
2887 const char *base_branch)
2889 const struct got_error *err = NULL;
2890 struct got_object_id *id = NULL;
2891 struct got_reference *ref = NULL;
2892 char *base_refname = NULL, *refname = NULL;
2893 struct got_reference *base_ref;
2896 * Don't let the user create a branch named '-'.
2897 * While technically a valid reference name, this case is usually
2898 * an unintended typo.
2900 if (branch_name[0] == '-' && branch_name[1] == '\0')
2901 return got_error(GOT_ERR_BAD_REF_NAME);
2903 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2904 base_refname = strdup(GOT_REF_HEAD);
2905 if (base_refname == NULL)
2906 return got_error_from_errno("strdup");
2907 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2908 return got_error_from_errno("asprintf");
2910 err = got_ref_open(&base_ref, repo, base_refname, 0);
2911 if (err)
2912 goto done;
2913 err = got_ref_resolve(&id, repo, base_ref);
2914 got_ref_close(base_ref);
2915 if (err)
2916 goto done;
2918 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2919 err = got_error_from_errno("asprintf");
2920 goto done;
2923 err = got_ref_open(&ref, repo, refname, 0);
2924 if (err == NULL) {
2925 err = got_error(GOT_ERR_BRANCH_EXISTS);
2926 goto done;
2927 } else if (err->code != GOT_ERR_NOT_REF)
2928 goto done;
2930 err = got_ref_alloc(&ref, refname, id);
2931 if (err)
2932 goto done;
2934 err = got_ref_write(ref, repo);
2935 done:
2936 if (ref)
2937 got_ref_close(ref);
2938 free(id);
2939 free(base_refname);
2940 free(refname);
2941 return err;
2944 static const struct got_error *
2945 cmd_branch(int argc, char *argv[])
2947 const struct got_error *error = NULL;
2948 struct got_repository *repo = NULL;
2949 struct got_worktree *worktree = NULL;
2950 char *cwd = NULL, *repo_path = NULL;
2951 int ch, do_list = 0;
2952 const char *delref = NULL;
2954 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2955 switch (ch) {
2956 case 'd':
2957 delref = optarg;
2958 break;
2959 case 'r':
2960 repo_path = realpath(optarg, NULL);
2961 if (repo_path == NULL)
2962 err(1, "-r option");
2963 got_path_strip_trailing_slashes(repo_path);
2964 break;
2965 case 'l':
2966 do_list = 1;
2967 break;
2968 default:
2969 usage_branch();
2970 /* NOTREACHED */
2974 if (do_list && delref)
2975 errx(1, "-l and -d options are mutually exclusive\n");
2977 argc -= optind;
2978 argv += optind;
2980 if (do_list || delref) {
2981 if (argc > 0)
2982 usage_branch();
2983 } else if (argc < 1 || argc > 2)
2984 usage_branch();
2986 #ifndef PROFILE
2987 if (do_list) {
2988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2989 NULL) == -1)
2990 err(1, "pledge");
2991 } else {
2992 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2993 "sendfd unveil", NULL) == -1)
2994 err(1, "pledge");
2996 #endif
2997 cwd = getcwd(NULL, 0);
2998 if (cwd == NULL) {
2999 error = got_error_from_errno("getcwd");
3000 goto done;
3003 if (repo_path == NULL) {
3004 error = got_worktree_open(&worktree, cwd);
3005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3006 goto done;
3007 else
3008 error = NULL;
3009 if (worktree) {
3010 repo_path =
3011 strdup(got_worktree_get_repo_path(worktree));
3012 if (repo_path == NULL)
3013 error = got_error_from_errno("strdup");
3014 if (error)
3015 goto done;
3016 } else {
3017 repo_path = strdup(cwd);
3018 if (repo_path == NULL) {
3019 error = got_error_from_errno("strdup");
3020 goto done;
3025 error = got_repo_open(&repo, repo_path);
3026 if (error != NULL)
3027 goto done;
3029 error = apply_unveil(got_repo_get_path(repo), do_list,
3030 worktree ? got_worktree_get_root_path(worktree) : NULL);
3031 if (error)
3032 goto done;
3034 if (do_list)
3035 error = list_branches(repo, worktree);
3036 else if (delref)
3037 error = delete_branch(repo, delref);
3038 else {
3039 const char *base_branch;
3040 if (argc == 1) {
3041 base_branch = worktree ?
3042 got_worktree_get_head_ref_name(worktree) :
3043 GOT_REF_HEAD;
3044 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3045 base_branch += 11;
3046 } else
3047 base_branch = argv[1];
3048 error = add_branch(repo, argv[0], base_branch);
3050 done:
3051 if (repo)
3052 got_repo_close(repo);
3053 if (worktree)
3054 got_worktree_close(worktree);
3055 free(cwd);
3056 free(repo_path);
3057 return error;
3060 __dead static void
3061 usage_add(void)
3063 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3064 exit(1);
3067 static const struct got_error *
3068 cmd_add(int argc, char *argv[])
3070 const struct got_error *error = NULL;
3071 struct got_repository *repo = NULL;
3072 struct got_worktree *worktree = NULL;
3073 char *cwd = NULL;
3074 struct got_pathlist_head paths;
3075 struct got_pathlist_entry *pe;
3076 int ch;
3078 TAILQ_INIT(&paths);
3080 while ((ch = getopt(argc, argv, "")) != -1) {
3081 switch (ch) {
3082 default:
3083 usage_add();
3084 /* NOTREACHED */
3088 argc -= optind;
3089 argv += optind;
3091 #ifndef PROFILE
3092 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3093 NULL) == -1)
3094 err(1, "pledge");
3095 #endif
3096 if (argc < 1)
3097 usage_add();
3099 cwd = getcwd(NULL, 0);
3100 if (cwd == NULL) {
3101 error = got_error_from_errno("getcwd");
3102 goto done;
3105 error = got_worktree_open(&worktree, cwd);
3106 if (error)
3107 goto done;
3109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3110 if (error != NULL)
3111 goto done;
3113 error = apply_unveil(got_repo_get_path(repo), 1,
3114 got_worktree_get_root_path(worktree));
3115 if (error)
3116 goto done;
3118 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3119 if (error)
3120 goto done;
3122 error = got_worktree_schedule_add(worktree, &paths, print_status,
3123 NULL, repo);
3124 done:
3125 if (repo)
3126 got_repo_close(repo);
3127 if (worktree)
3128 got_worktree_close(worktree);
3129 TAILQ_FOREACH(pe, &paths, entry)
3130 free((char *)pe->path);
3131 got_pathlist_free(&paths);
3132 free(cwd);
3133 return error;
3136 __dead static void
3137 usage_remove(void)
3139 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3140 exit(1);
3143 static const struct got_error *
3144 cmd_remove(int argc, char *argv[])
3146 const struct got_error *error = NULL;
3147 struct got_worktree *worktree = NULL;
3148 struct got_repository *repo = NULL;
3149 char *cwd = NULL;
3150 struct got_pathlist_head paths;
3151 struct got_pathlist_entry *pe;
3152 int ch, delete_local_mods = 0;
3154 TAILQ_INIT(&paths);
3156 while ((ch = getopt(argc, argv, "f")) != -1) {
3157 switch (ch) {
3158 case 'f':
3159 delete_local_mods = 1;
3160 break;
3161 default:
3162 usage_add();
3163 /* NOTREACHED */
3167 argc -= optind;
3168 argv += optind;
3170 #ifndef PROFILE
3171 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3172 NULL) == -1)
3173 err(1, "pledge");
3174 #endif
3175 if (argc < 1)
3176 usage_remove();
3178 cwd = getcwd(NULL, 0);
3179 if (cwd == NULL) {
3180 error = got_error_from_errno("getcwd");
3181 goto done;
3183 error = got_worktree_open(&worktree, cwd);
3184 if (error)
3185 goto done;
3187 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3188 if (error)
3189 goto done;
3191 error = apply_unveil(got_repo_get_path(repo), 1,
3192 got_worktree_get_root_path(worktree));
3193 if (error)
3194 goto done;
3196 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3197 if (error)
3198 goto done;
3200 error = got_worktree_schedule_delete(worktree, &paths,
3201 delete_local_mods, print_status, NULL, repo);
3202 if (error)
3203 goto done;
3204 done:
3205 if (repo)
3206 got_repo_close(repo);
3207 if (worktree)
3208 got_worktree_close(worktree);
3209 TAILQ_FOREACH(pe, &paths, entry)
3210 free((char *)pe->path);
3211 got_pathlist_free(&paths);
3212 free(cwd);
3213 return error;
3216 __dead static void
3217 usage_revert(void)
3219 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3220 "path ...\n", getprogname());
3221 exit(1);
3224 static const struct got_error *
3225 revert_progress(void *arg, unsigned char status, const char *path)
3227 while (path[0] == '/')
3228 path++;
3229 printf("%c %s\n", status, path);
3230 return NULL;
3233 struct choose_patch_arg {
3234 FILE *patch_script_file;
3235 const char *action;
3238 static const struct got_error *
3239 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3240 int nchanges, const char *action)
3242 char *line = NULL;
3243 size_t linesize = 0;
3244 ssize_t linelen;
3246 switch (status) {
3247 case GOT_STATUS_ADD:
3248 printf("A %s\n%s this addition? [y/n] ", path, action);
3249 break;
3250 case GOT_STATUS_DELETE:
3251 printf("D %s\n%s this deletion? [y/n] ", path, action);
3252 break;
3253 case GOT_STATUS_MODIFY:
3254 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3255 return got_error_from_errno("fseek");
3256 printf(GOT_COMMIT_SEP_STR);
3257 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3258 printf("%s", line);
3259 if (ferror(patch_file))
3260 return got_error_from_errno("getline");
3261 printf(GOT_COMMIT_SEP_STR);
3262 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3263 path, n, nchanges, action);
3264 break;
3265 default:
3266 return got_error_path(path, GOT_ERR_FILE_STATUS);
3269 return NULL;
3272 static const struct got_error *
3273 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3274 FILE *patch_file, int n, int nchanges)
3276 const struct got_error *err = NULL;
3277 char *line = NULL;
3278 size_t linesize = 0;
3279 ssize_t linelen;
3280 int resp = ' ';
3281 struct choose_patch_arg *a = arg;
3283 *choice = GOT_PATCH_CHOICE_NONE;
3285 if (a->patch_script_file) {
3286 char *nl;
3287 err = show_change(status, path, patch_file, n, nchanges,
3288 a->action);
3289 if (err)
3290 return err;
3291 linelen = getline(&line, &linesize, a->patch_script_file);
3292 if (linelen == -1) {
3293 if (ferror(a->patch_script_file))
3294 return got_error_from_errno("getline");
3295 return NULL;
3297 nl = strchr(line, '\n');
3298 if (nl)
3299 *nl = '\0';
3300 if (strcmp(line, "y") == 0) {
3301 *choice = GOT_PATCH_CHOICE_YES;
3302 printf("y\n");
3303 } else if (strcmp(line, "n") == 0) {
3304 *choice = GOT_PATCH_CHOICE_NO;
3305 printf("n\n");
3306 } else if (strcmp(line, "q") == 0 &&
3307 status == GOT_STATUS_MODIFY) {
3308 *choice = GOT_PATCH_CHOICE_QUIT;
3309 printf("q\n");
3310 } else
3311 printf("invalid response '%s'\n", line);
3312 free(line);
3313 return NULL;
3316 while (resp != 'y' && resp != 'n' && resp != 'q') {
3317 err = show_change(status, path, patch_file, n, nchanges,
3318 a->action);
3319 if (err)
3320 return err;
3321 resp = getchar();
3322 if (resp == '\n')
3323 resp = getchar();
3324 if (status == GOT_STATUS_MODIFY) {
3325 if (resp != 'y' && resp != 'n' && resp != 'q') {
3326 printf("invalid response '%c'\n", resp);
3327 resp = ' ';
3329 } else if (resp != 'y' && resp != 'n') {
3330 printf("invalid response '%c'\n", resp);
3331 resp = ' ';
3335 if (resp == 'y')
3336 *choice = GOT_PATCH_CHOICE_YES;
3337 else if (resp == 'n')
3338 *choice = GOT_PATCH_CHOICE_NO;
3339 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3340 *choice = GOT_PATCH_CHOICE_QUIT;
3342 return NULL;
3346 static const struct got_error *
3347 cmd_revert(int argc, char *argv[])
3349 const struct got_error *error = NULL;
3350 struct got_worktree *worktree = NULL;
3351 struct got_repository *repo = NULL;
3352 char *cwd = NULL, *path = NULL;
3353 struct got_pathlist_head paths;
3354 struct got_pathlist_entry *pe;
3355 int ch, can_recurse = 0, pflag = 0;
3356 FILE *patch_script_file = NULL;
3357 const char *patch_script_path = NULL;
3358 struct choose_patch_arg cpa;
3360 TAILQ_INIT(&paths);
3362 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3363 switch (ch) {
3364 case 'p':
3365 pflag = 1;
3366 break;
3367 case 'F':
3368 patch_script_path = optarg;
3369 break;
3370 case 'R':
3371 can_recurse = 1;
3372 break;
3373 default:
3374 usage_revert();
3375 /* NOTREACHED */
3379 argc -= optind;
3380 argv += optind;
3382 #ifndef PROFILE
3383 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3384 "unveil", NULL) == -1)
3385 err(1, "pledge");
3386 #endif
3387 if (argc < 1)
3388 usage_revert();
3389 if (patch_script_path && !pflag)
3390 errx(1, "-F option can only be used together with -p option");
3392 cwd = getcwd(NULL, 0);
3393 if (cwd == NULL) {
3394 error = got_error_from_errno("getcwd");
3395 goto done;
3397 error = got_worktree_open(&worktree, cwd);
3398 if (error)
3399 goto done;
3401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3402 if (error != NULL)
3403 goto done;
3405 if (patch_script_path) {
3406 patch_script_file = fopen(patch_script_path, "r");
3407 if (patch_script_file == NULL) {
3408 error = got_error_from_errno2("fopen",
3409 patch_script_path);
3410 goto done;
3413 error = apply_unveil(got_repo_get_path(repo), 1,
3414 got_worktree_get_root_path(worktree));
3415 if (error)
3416 goto done;
3418 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3419 if (error)
3420 goto done;
3422 if (!can_recurse) {
3423 char *ondisk_path;
3424 struct stat sb;
3425 TAILQ_FOREACH(pe, &paths, entry) {
3426 if (asprintf(&ondisk_path, "%s/%s",
3427 got_worktree_get_root_path(worktree),
3428 pe->path) == -1) {
3429 error = got_error_from_errno("asprintf");
3430 goto done;
3432 if (lstat(ondisk_path, &sb) == -1) {
3433 if (errno == ENOENT) {
3434 free(ondisk_path);
3435 continue;
3437 error = got_error_from_errno2("lstat",
3438 ondisk_path);
3439 free(ondisk_path);
3440 goto done;
3442 free(ondisk_path);
3443 if (S_ISDIR(sb.st_mode)) {
3444 error = got_error_msg(GOT_ERR_BAD_PATH,
3445 "reverting directories requires -R option");
3446 goto done;
3451 cpa.patch_script_file = patch_script_file;
3452 cpa.action = "revert";
3453 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3454 pflag ? choose_patch : NULL, &cpa, repo);
3455 if (error)
3456 goto done;
3457 done:
3458 if (patch_script_file && fclose(patch_script_file) == EOF &&
3459 error == NULL)
3460 error = got_error_from_errno2("fclose", patch_script_path);
3461 if (repo)
3462 got_repo_close(repo);
3463 if (worktree)
3464 got_worktree_close(worktree);
3465 free(path);
3466 free(cwd);
3467 return error;
3470 __dead static void
3471 usage_commit(void)
3473 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3474 getprogname());
3475 exit(1);
3478 struct collect_commit_logmsg_arg {
3479 const char *cmdline_log;
3480 const char *editor;
3481 const char *worktree_path;
3482 const char *branch_name;
3483 const char *repo_path;
3484 char *logmsg_path;
3488 static const struct got_error *
3489 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3490 void *arg)
3492 char *initial_content = NULL;
3493 struct got_pathlist_entry *pe;
3494 const struct got_error *err = NULL;
3495 char *template = NULL;
3496 struct collect_commit_logmsg_arg *a = arg;
3497 int fd;
3498 size_t len;
3500 /* if a message was specified on the command line, just use it */
3501 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3502 len = strlen(a->cmdline_log) + 1;
3503 *logmsg = malloc(len + 1);
3504 if (*logmsg == NULL)
3505 return got_error_from_errno("malloc");
3506 strlcpy(*logmsg, a->cmdline_log, len);
3507 return NULL;
3510 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3511 return got_error_from_errno("asprintf");
3513 if (asprintf(&initial_content,
3514 "\n# changes to be committed on branch %s:\n",
3515 a->branch_name) == -1)
3516 return got_error_from_errno("asprintf");
3518 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3519 if (err)
3520 goto done;
3522 dprintf(fd, initial_content);
3524 TAILQ_FOREACH(pe, commitable_paths, entry) {
3525 struct got_commitable *ct = pe->data;
3526 dprintf(fd, "# %c %s\n",
3527 got_commitable_get_status(ct),
3528 got_commitable_get_path(ct));
3530 close(fd);
3532 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3533 done:
3534 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3535 unlink(a->logmsg_path);
3536 free(a->logmsg_path);
3537 a->logmsg_path = NULL;
3539 free(initial_content);
3540 free(template);
3542 /* Editor is done; we can now apply unveil(2) */
3543 if (err == NULL) {
3544 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3545 if (err) {
3546 free(*logmsg);
3547 *logmsg = NULL;
3550 return err;
3553 static const struct got_error *
3554 cmd_commit(int argc, char *argv[])
3556 const struct got_error *error = NULL;
3557 struct got_worktree *worktree = NULL;
3558 struct got_repository *repo = NULL;
3559 char *cwd = NULL, *id_str = NULL;
3560 struct got_object_id *id = NULL;
3561 const char *logmsg = NULL;
3562 const char *author;
3563 struct collect_commit_logmsg_arg cl_arg;
3564 char *editor = NULL;
3565 int ch, rebase_in_progress, histedit_in_progress;
3566 struct got_pathlist_head paths;
3568 TAILQ_INIT(&paths);
3569 cl_arg.logmsg_path = NULL;
3571 while ((ch = getopt(argc, argv, "m:")) != -1) {
3572 switch (ch) {
3573 case 'm':
3574 logmsg = optarg;
3575 break;
3576 default:
3577 usage_commit();
3578 /* NOTREACHED */
3582 argc -= optind;
3583 argv += optind;
3585 #ifndef PROFILE
3586 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3587 "unveil", NULL) == -1)
3588 err(1, "pledge");
3589 #endif
3590 error = get_author(&author);
3591 if (error)
3592 return error;
3594 cwd = getcwd(NULL, 0);
3595 if (cwd == NULL) {
3596 error = got_error_from_errno("getcwd");
3597 goto done;
3599 error = got_worktree_open(&worktree, cwd);
3600 if (error)
3601 goto done;
3603 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3604 if (error)
3605 goto done;
3606 if (rebase_in_progress) {
3607 error = got_error(GOT_ERR_REBASING);
3608 goto done;
3611 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3612 worktree);
3613 if (error)
3614 goto done;
3616 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3617 if (error != NULL)
3618 goto done;
3621 * unveil(2) traverses exec(2); if an editor is used we have
3622 * to apply unveil after the log message has been written.
3624 if (logmsg == NULL || strlen(logmsg) == 0)
3625 error = get_editor(&editor);
3626 else
3627 error = apply_unveil(got_repo_get_path(repo), 0,
3628 got_worktree_get_root_path(worktree));
3629 if (error)
3630 goto done;
3632 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3633 if (error)
3634 goto done;
3636 cl_arg.editor = editor;
3637 cl_arg.cmdline_log = logmsg;
3638 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3639 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3640 if (!histedit_in_progress) {
3641 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3642 error = got_error(GOT_ERR_COMMIT_BRANCH);
3643 goto done;
3645 cl_arg.branch_name += 11;
3647 cl_arg.repo_path = got_repo_get_path(repo);
3648 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3649 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3650 if (error) {
3651 if (cl_arg.logmsg_path)
3652 fprintf(stderr, "%s: log message preserved in %s\n",
3653 getprogname(), cl_arg.logmsg_path);
3654 goto done;
3657 if (cl_arg.logmsg_path)
3658 unlink(cl_arg.logmsg_path);
3660 error = got_object_id_str(&id_str, id);
3661 if (error)
3662 goto done;
3663 printf("Created commit %s\n", id_str);
3664 done:
3665 free(cl_arg.logmsg_path);
3666 if (repo)
3667 got_repo_close(repo);
3668 if (worktree)
3669 got_worktree_close(worktree);
3670 free(cwd);
3671 free(id_str);
3672 free(editor);
3673 return error;
3676 __dead static void
3677 usage_cherrypick(void)
3679 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3680 exit(1);
3683 static const struct got_error *
3684 cmd_cherrypick(int argc, char *argv[])
3686 const struct got_error *error = NULL;
3687 struct got_worktree *worktree = NULL;
3688 struct got_repository *repo = NULL;
3689 char *cwd = NULL, *commit_id_str = NULL;
3690 struct got_object_id *commit_id = NULL;
3691 struct got_commit_object *commit = NULL;
3692 struct got_object_qid *pid;
3693 struct got_reference *head_ref = NULL;
3694 int ch, did_something = 0;
3696 while ((ch = getopt(argc, argv, "")) != -1) {
3697 switch (ch) {
3698 default:
3699 usage_cherrypick();
3700 /* NOTREACHED */
3704 argc -= optind;
3705 argv += optind;
3707 #ifndef PROFILE
3708 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3709 "unveil", NULL) == -1)
3710 err(1, "pledge");
3711 #endif
3712 if (argc != 1)
3713 usage_cherrypick();
3715 cwd = getcwd(NULL, 0);
3716 if (cwd == NULL) {
3717 error = got_error_from_errno("getcwd");
3718 goto done;
3720 error = got_worktree_open(&worktree, cwd);
3721 if (error)
3722 goto done;
3724 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3725 if (error != NULL)
3726 goto done;
3728 error = apply_unveil(got_repo_get_path(repo), 0,
3729 got_worktree_get_root_path(worktree));
3730 if (error)
3731 goto done;
3733 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3734 GOT_OBJ_TYPE_COMMIT, repo);
3735 if (error != NULL) {
3736 struct got_reference *ref;
3737 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3738 goto done;
3739 error = got_ref_open(&ref, repo, argv[0], 0);
3740 if (error != NULL)
3741 goto done;
3742 error = got_ref_resolve(&commit_id, repo, ref);
3743 got_ref_close(ref);
3744 if (error != NULL)
3745 goto done;
3747 error = got_object_id_str(&commit_id_str, commit_id);
3748 if (error)
3749 goto done;
3751 error = got_ref_open(&head_ref, repo,
3752 got_worktree_get_head_ref_name(worktree), 0);
3753 if (error != NULL)
3754 goto done;
3756 error = check_same_branch(commit_id, head_ref, NULL, repo);
3757 if (error) {
3758 if (error->code != GOT_ERR_ANCESTRY)
3759 goto done;
3760 error = NULL;
3761 } else {
3762 error = got_error(GOT_ERR_SAME_BRANCH);
3763 goto done;
3766 error = got_object_open_as_commit(&commit, repo, commit_id);
3767 if (error)
3768 goto done;
3769 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3770 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3771 commit_id, repo, update_progress, &did_something, check_cancelled,
3772 NULL);
3773 if (error != NULL)
3774 goto done;
3776 if (did_something)
3777 printf("Merged commit %s\n", commit_id_str);
3778 done:
3779 if (commit)
3780 got_object_commit_close(commit);
3781 free(commit_id_str);
3782 if (head_ref)
3783 got_ref_close(head_ref);
3784 if (worktree)
3785 got_worktree_close(worktree);
3786 if (repo)
3787 got_repo_close(repo);
3788 return error;
3791 __dead static void
3792 usage_backout(void)
3794 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3795 exit(1);
3798 static const struct got_error *
3799 cmd_backout(int argc, char *argv[])
3801 const struct got_error *error = NULL;
3802 struct got_worktree *worktree = NULL;
3803 struct got_repository *repo = NULL;
3804 char *cwd = NULL, *commit_id_str = NULL;
3805 struct got_object_id *commit_id = NULL;
3806 struct got_commit_object *commit = NULL;
3807 struct got_object_qid *pid;
3808 struct got_reference *head_ref = NULL;
3809 int ch, did_something = 0;
3811 while ((ch = getopt(argc, argv, "")) != -1) {
3812 switch (ch) {
3813 default:
3814 usage_backout();
3815 /* NOTREACHED */
3819 argc -= optind;
3820 argv += optind;
3822 #ifndef PROFILE
3823 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3824 "unveil", NULL) == -1)
3825 err(1, "pledge");
3826 #endif
3827 if (argc != 1)
3828 usage_backout();
3830 cwd = getcwd(NULL, 0);
3831 if (cwd == NULL) {
3832 error = got_error_from_errno("getcwd");
3833 goto done;
3835 error = got_worktree_open(&worktree, cwd);
3836 if (error)
3837 goto done;
3839 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3840 if (error != NULL)
3841 goto done;
3843 error = apply_unveil(got_repo_get_path(repo), 0,
3844 got_worktree_get_root_path(worktree));
3845 if (error)
3846 goto done;
3848 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3849 GOT_OBJ_TYPE_COMMIT, repo);
3850 if (error != NULL) {
3851 struct got_reference *ref;
3852 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3853 goto done;
3854 error = got_ref_open(&ref, repo, argv[0], 0);
3855 if (error != NULL)
3856 goto done;
3857 error = got_ref_resolve(&commit_id, repo, ref);
3858 got_ref_close(ref);
3859 if (error != NULL)
3860 goto done;
3862 error = got_object_id_str(&commit_id_str, commit_id);
3863 if (error)
3864 goto done;
3866 error = got_ref_open(&head_ref, repo,
3867 got_worktree_get_head_ref_name(worktree), 0);
3868 if (error != NULL)
3869 goto done;
3871 error = check_same_branch(commit_id, head_ref, NULL, repo);
3872 if (error)
3873 goto done;
3875 error = got_object_open_as_commit(&commit, repo, commit_id);
3876 if (error)
3877 goto done;
3878 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3879 if (pid == NULL) {
3880 error = got_error(GOT_ERR_ROOT_COMMIT);
3881 goto done;
3884 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3885 update_progress, &did_something, check_cancelled, NULL);
3886 if (error != NULL)
3887 goto done;
3889 if (did_something)
3890 printf("Backed out commit %s\n", commit_id_str);
3891 done:
3892 if (commit)
3893 got_object_commit_close(commit);
3894 free(commit_id_str);
3895 if (head_ref)
3896 got_ref_close(head_ref);
3897 if (worktree)
3898 got_worktree_close(worktree);
3899 if (repo)
3900 got_repo_close(repo);
3901 return error;
3904 __dead static void
3905 usage_rebase(void)
3907 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3908 getprogname());
3909 exit(1);
3912 void
3913 trim_logmsg(char *logmsg, int limit)
3915 char *nl;
3916 size_t len;
3918 len = strlen(logmsg);
3919 if (len > limit)
3920 len = limit;
3921 logmsg[len] = '\0';
3922 nl = strchr(logmsg, '\n');
3923 if (nl)
3924 *nl = '\0';
3927 static const struct got_error *
3928 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3930 const char *logmsg0 = NULL;
3932 logmsg0 = got_object_commit_get_logmsg(commit);
3934 while (isspace((unsigned char)logmsg0[0]))
3935 logmsg0++;
3937 *logmsg = strdup(logmsg0);
3938 if (*logmsg == NULL)
3939 return got_error_from_errno("strdup");
3941 trim_logmsg(*logmsg, limit);
3942 return NULL;
3945 static const struct got_error *
3946 show_rebase_progress(struct got_commit_object *commit,
3947 struct got_object_id *old_id, struct got_object_id *new_id)
3949 const struct got_error *err;
3950 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3952 err = got_object_id_str(&old_id_str, old_id);
3953 if (err)
3954 goto done;
3956 if (new_id) {
3957 err = got_object_id_str(&new_id_str, new_id);
3958 if (err)
3959 goto done;
3962 old_id_str[12] = '\0';
3963 if (new_id_str)
3964 new_id_str[12] = '\0';
3966 err = get_short_logmsg(&logmsg, 42, commit);
3967 if (err)
3968 goto done;
3970 printf("%s -> %s: %s\n", old_id_str,
3971 new_id_str ? new_id_str : "no-op change", logmsg);
3972 done:
3973 free(old_id_str);
3974 free(new_id_str);
3975 return err;
3978 static const struct got_error *
3979 rebase_progress(void *arg, unsigned char status, const char *path)
3981 unsigned char *rebase_status = arg;
3983 while (path[0] == '/')
3984 path++;
3985 printf("%c %s\n", status, path);
3987 if (*rebase_status == GOT_STATUS_CONFLICT)
3988 return NULL;
3989 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3990 *rebase_status = status;
3991 return NULL;
3994 static const struct got_error *
3995 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3996 struct got_reference *branch, struct got_reference *new_base_branch,
3997 struct got_reference *tmp_branch, struct got_repository *repo)
3999 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4000 return got_worktree_rebase_complete(worktree, fileindex,
4001 new_base_branch, tmp_branch, branch, repo);
4004 static const struct got_error *
4005 rebase_commit(struct got_pathlist_head *merged_paths,
4006 struct got_worktree *worktree, struct got_fileindex *fileindex,
4007 struct got_reference *tmp_branch,
4008 struct got_object_id *commit_id, struct got_repository *repo)
4010 const struct got_error *error;
4011 struct got_commit_object *commit;
4012 struct got_object_id *new_commit_id;
4014 error = got_object_open_as_commit(&commit, repo, commit_id);
4015 if (error)
4016 return error;
4018 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4019 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4020 if (error) {
4021 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4022 goto done;
4023 error = show_rebase_progress(commit, commit_id, NULL);
4024 } else {
4025 error = show_rebase_progress(commit, commit_id, new_commit_id);
4026 free(new_commit_id);
4028 done:
4029 got_object_commit_close(commit);
4030 return error;
4033 struct check_path_prefix_arg {
4034 const char *path_prefix;
4035 size_t len;
4036 int errcode;
4039 static const struct got_error *
4040 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4041 struct got_blob_object *blob2, struct got_object_id *id1,
4042 struct got_object_id *id2, const char *path1, const char *path2,
4043 struct got_repository *repo)
4045 struct check_path_prefix_arg *a = arg;
4047 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4048 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4049 return got_error(a->errcode);
4051 return NULL;
4054 static const struct got_error *
4055 check_path_prefix(struct got_object_id *parent_id,
4056 struct got_object_id *commit_id, const char *path_prefix,
4057 int errcode, struct got_repository *repo)
4059 const struct got_error *err;
4060 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4061 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4062 struct check_path_prefix_arg cpp_arg;
4064 if (got_path_is_root_dir(path_prefix))
4065 return NULL;
4067 err = got_object_open_as_commit(&commit, repo, commit_id);
4068 if (err)
4069 goto done;
4071 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4072 if (err)
4073 goto done;
4075 err = got_object_open_as_tree(&tree1, repo,
4076 got_object_commit_get_tree_id(parent_commit));
4077 if (err)
4078 goto done;
4080 err = got_object_open_as_tree(&tree2, repo,
4081 got_object_commit_get_tree_id(commit));
4082 if (err)
4083 goto done;
4085 cpp_arg.path_prefix = path_prefix;
4086 while (cpp_arg.path_prefix[0] == '/')
4087 cpp_arg.path_prefix++;
4088 cpp_arg.len = strlen(cpp_arg.path_prefix);
4089 cpp_arg.errcode = errcode;
4090 err = got_diff_tree(tree1, tree2, "", "", repo,
4091 check_path_prefix_in_diff, &cpp_arg, 0);
4092 done:
4093 if (tree1)
4094 got_object_tree_close(tree1);
4095 if (tree2)
4096 got_object_tree_close(tree2);
4097 if (commit)
4098 got_object_commit_close(commit);
4099 if (parent_commit)
4100 got_object_commit_close(parent_commit);
4101 return err;
4104 static const struct got_error *
4105 collect_commits(struct got_object_id_queue *commits,
4106 struct got_object_id *initial_commit_id,
4107 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4108 const char *path_prefix, int path_prefix_errcode,
4109 struct got_repository *repo)
4111 const struct got_error *err = NULL;
4112 struct got_commit_graph *graph = NULL;
4113 struct got_object_id *parent_id = NULL;
4114 struct got_object_qid *qid;
4115 struct got_object_id *commit_id = initial_commit_id;
4117 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4118 if (err)
4119 return err;
4121 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4122 if (err)
4123 goto done;
4124 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4125 err = got_commit_graph_iter_next(&parent_id, graph);
4126 if (err) {
4127 if (err->code == GOT_ERR_ITER_COMPLETED) {
4128 err = got_error_msg(GOT_ERR_ANCESTRY,
4129 "ran out of commits to rebase before "
4130 "youngest common ancestor commit has "
4131 "been reached?!?");
4132 goto done;
4133 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4134 goto done;
4135 err = got_commit_graph_fetch_commits(graph, 1, repo);
4136 if (err)
4137 goto done;
4138 } else {
4139 err = check_path_prefix(parent_id, commit_id,
4140 path_prefix, path_prefix_errcode, repo);
4141 if (err)
4142 goto done;
4144 err = got_object_qid_alloc(&qid, commit_id);
4145 if (err)
4146 goto done;
4147 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4148 commit_id = parent_id;
4151 done:
4152 got_commit_graph_close(graph);
4153 return err;
4156 static const struct got_error *
4157 cmd_rebase(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_worktree *worktree = NULL;
4161 struct got_repository *repo = NULL;
4162 struct got_fileindex *fileindex = NULL;
4163 char *cwd = NULL;
4164 struct got_reference *branch = NULL;
4165 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4166 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4167 struct got_object_id *resume_commit_id = NULL;
4168 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4169 struct got_commit_object *commit = NULL;
4170 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4171 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4172 struct got_object_id_queue commits;
4173 struct got_pathlist_head merged_paths;
4174 const struct got_object_id_queue *parent_ids;
4175 struct got_object_qid *qid, *pid;
4177 SIMPLEQ_INIT(&commits);
4178 TAILQ_INIT(&merged_paths);
4180 while ((ch = getopt(argc, argv, "ac")) != -1) {
4181 switch (ch) {
4182 case 'a':
4183 abort_rebase = 1;
4184 break;
4185 case 'c':
4186 continue_rebase = 1;
4187 break;
4188 default:
4189 usage_rebase();
4190 /* NOTREACHED */
4194 argc -= optind;
4195 argv += optind;
4197 #ifndef PROFILE
4198 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4199 "unveil", NULL) == -1)
4200 err(1, "pledge");
4201 #endif
4202 if (abort_rebase && continue_rebase)
4203 usage_rebase();
4204 else if (abort_rebase || continue_rebase) {
4205 if (argc != 0)
4206 usage_rebase();
4207 } else if (argc != 1)
4208 usage_rebase();
4210 cwd = getcwd(NULL, 0);
4211 if (cwd == NULL) {
4212 error = got_error_from_errno("getcwd");
4213 goto done;
4215 error = got_worktree_open(&worktree, cwd);
4216 if (error)
4217 goto done;
4219 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4220 if (error != NULL)
4221 goto done;
4223 error = apply_unveil(got_repo_get_path(repo), 0,
4224 got_worktree_get_root_path(worktree));
4225 if (error)
4226 goto done;
4228 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4229 if (error)
4230 goto done;
4232 if (abort_rebase) {
4233 int did_something;
4234 if (!rebase_in_progress) {
4235 error = got_error(GOT_ERR_NOT_REBASING);
4236 goto done;
4238 error = got_worktree_rebase_continue(&resume_commit_id,
4239 &new_base_branch, &tmp_branch, &branch, &fileindex,
4240 worktree, repo);
4241 if (error)
4242 goto done;
4243 printf("Switching work tree to %s\n",
4244 got_ref_get_symref_target(new_base_branch));
4245 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4246 new_base_branch, update_progress, &did_something);
4247 if (error)
4248 goto done;
4249 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4250 goto done; /* nothing else to do */
4253 if (continue_rebase) {
4254 if (!rebase_in_progress) {
4255 error = got_error(GOT_ERR_NOT_REBASING);
4256 goto done;
4258 error = got_worktree_rebase_continue(&resume_commit_id,
4259 &new_base_branch, &tmp_branch, &branch, &fileindex,
4260 worktree, repo);
4261 if (error)
4262 goto done;
4264 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4265 resume_commit_id, repo);
4266 if (error)
4267 goto done;
4269 yca_id = got_object_id_dup(resume_commit_id);
4270 if (yca_id == NULL) {
4271 error = got_error_from_errno("got_object_id_dup");
4272 goto done;
4274 } else {
4275 error = got_ref_open(&branch, repo, argv[0], 0);
4276 if (error != NULL)
4277 goto done;
4280 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4281 if (error)
4282 goto done;
4284 if (!continue_rebase) {
4285 struct got_object_id *base_commit_id;
4287 base_commit_id = got_worktree_get_base_commit_id(worktree);
4288 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4289 base_commit_id, branch_head_commit_id, repo);
4290 if (error)
4291 goto done;
4292 if (yca_id == NULL) {
4293 error = got_error_msg(GOT_ERR_ANCESTRY,
4294 "specified branch shares no common ancestry "
4295 "with work tree's branch");
4296 goto done;
4299 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4300 if (error) {
4301 if (error->code != GOT_ERR_ANCESTRY)
4302 goto done;
4303 error = NULL;
4304 } else {
4305 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4306 "specified branch resolves to a commit which "
4307 "is already contained in work tree's branch");
4308 goto done;
4310 error = got_worktree_rebase_prepare(&new_base_branch,
4311 &tmp_branch, &fileindex, worktree, branch, repo);
4312 if (error)
4313 goto done;
4316 commit_id = branch_head_commit_id;
4317 error = got_object_open_as_commit(&commit, repo, commit_id);
4318 if (error)
4319 goto done;
4321 parent_ids = got_object_commit_get_parent_ids(commit);
4322 pid = SIMPLEQ_FIRST(parent_ids);
4323 if (pid == NULL) {
4324 if (!continue_rebase) {
4325 int did_something;
4326 error = got_worktree_rebase_abort(worktree, fileindex,
4327 repo, new_base_branch, update_progress,
4328 &did_something);
4329 if (error)
4330 goto done;
4331 printf("Rebase of %s aborted\n",
4332 got_ref_get_name(branch));
4334 error = got_error(GOT_ERR_EMPTY_REBASE);
4335 goto done;
4337 error = collect_commits(&commits, commit_id, pid->id,
4338 yca_id, got_worktree_get_path_prefix(worktree),
4339 GOT_ERR_REBASE_PATH, repo);
4340 got_object_commit_close(commit);
4341 commit = NULL;
4342 if (error)
4343 goto done;
4345 if (SIMPLEQ_EMPTY(&commits)) {
4346 if (continue_rebase)
4347 error = rebase_complete(worktree, fileindex,
4348 branch, new_base_branch, tmp_branch, repo);
4349 else
4350 error = got_error(GOT_ERR_EMPTY_REBASE);
4351 goto done;
4354 pid = NULL;
4355 SIMPLEQ_FOREACH(qid, &commits, entry) {
4356 commit_id = qid->id;
4357 parent_id = pid ? pid->id : yca_id;
4358 pid = qid;
4360 error = got_worktree_rebase_merge_files(&merged_paths,
4361 worktree, fileindex, parent_id, commit_id, repo,
4362 rebase_progress, &rebase_status, check_cancelled, NULL);
4363 if (error)
4364 goto done;
4366 if (rebase_status == GOT_STATUS_CONFLICT) {
4367 got_worktree_rebase_pathlist_free(&merged_paths);
4368 break;
4371 error = rebase_commit(&merged_paths, worktree, fileindex,
4372 tmp_branch, commit_id, repo);
4373 got_worktree_rebase_pathlist_free(&merged_paths);
4374 if (error)
4375 goto done;
4378 if (rebase_status == GOT_STATUS_CONFLICT) {
4379 error = got_worktree_rebase_postpone(worktree, fileindex);
4380 if (error)
4381 goto done;
4382 error = got_error_msg(GOT_ERR_CONFLICTS,
4383 "conflicts must be resolved before rebasing can continue");
4384 } else
4385 error = rebase_complete(worktree, fileindex, branch,
4386 new_base_branch, tmp_branch, repo);
4387 done:
4388 got_object_id_queue_free(&commits);
4389 free(branch_head_commit_id);
4390 free(resume_commit_id);
4391 free(yca_id);
4392 if (commit)
4393 got_object_commit_close(commit);
4394 if (branch)
4395 got_ref_close(branch);
4396 if (new_base_branch)
4397 got_ref_close(new_base_branch);
4398 if (tmp_branch)
4399 got_ref_close(tmp_branch);
4400 if (worktree)
4401 got_worktree_close(worktree);
4402 if (repo)
4403 got_repo_close(repo);
4404 return error;
4407 __dead static void
4408 usage_histedit(void)
4410 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4411 getprogname());
4412 exit(1);
4415 #define GOT_HISTEDIT_PICK 'p'
4416 #define GOT_HISTEDIT_EDIT 'e'
4417 #define GOT_HISTEDIT_FOLD 'f'
4418 #define GOT_HISTEDIT_DROP 'd'
4419 #define GOT_HISTEDIT_MESG 'm'
4421 static struct got_histedit_cmd {
4422 unsigned char code;
4423 const char *name;
4424 const char *desc;
4425 } got_histedit_cmds[] = {
4426 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4427 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4428 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4429 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4430 { GOT_HISTEDIT_MESG, "mesg",
4431 "single-line log message for commit above (open editor if empty)" },
4434 struct got_histedit_list_entry {
4435 TAILQ_ENTRY(got_histedit_list_entry) entry;
4436 struct got_object_id *commit_id;
4437 const struct got_histedit_cmd *cmd;
4438 char *logmsg;
4440 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4442 static const struct got_error *
4443 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4444 FILE *f, struct got_repository *repo)
4446 const struct got_error *err = NULL;
4447 char *logmsg = NULL, *id_str = NULL;
4448 struct got_commit_object *commit = NULL;
4449 int n;
4451 err = got_object_open_as_commit(&commit, repo, commit_id);
4452 if (err)
4453 goto done;
4455 err = get_short_logmsg(&logmsg, 34, commit);
4456 if (err)
4457 goto done;
4459 err = got_object_id_str(&id_str, commit_id);
4460 if (err)
4461 goto done;
4463 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4464 if (n < 0)
4465 err = got_ferror(f, GOT_ERR_IO);
4466 done:
4467 if (commit)
4468 got_object_commit_close(commit);
4469 free(id_str);
4470 free(logmsg);
4471 return err;
4474 static const struct got_error *
4475 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4476 struct got_repository *repo)
4478 const struct got_error *err = NULL;
4479 struct got_object_qid *qid;
4481 if (SIMPLEQ_EMPTY(commits))
4482 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4484 SIMPLEQ_FOREACH(qid, commits, entry) {
4485 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4486 f, repo);
4487 if (err)
4488 break;
4491 return err;
4494 static const struct got_error *
4495 write_cmd_list(FILE *f)
4497 const struct got_error *err = NULL;
4498 int n, i;
4500 n = fprintf(f, "# Available histedit commands:\n");
4501 if (n < 0)
4502 return got_ferror(f, GOT_ERR_IO);
4504 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4505 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4506 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4507 cmd->desc);
4508 if (n < 0) {
4509 err = got_ferror(f, GOT_ERR_IO);
4510 break;
4513 n = fprintf(f, "# Commits will be processed in order from top to "
4514 "bottom of this file.\n");
4515 if (n < 0)
4516 return got_ferror(f, GOT_ERR_IO);
4517 return err;
4520 static const struct got_error *
4521 histedit_syntax_error(int lineno)
4523 static char msg[42];
4524 int ret;
4526 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4527 lineno);
4528 if (ret == -1 || ret >= sizeof(msg))
4529 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4531 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4534 static const struct got_error *
4535 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4536 char *logmsg, struct got_repository *repo)
4538 const struct got_error *err;
4539 struct got_commit_object *folded_commit = NULL;
4540 char *id_str;
4542 err = got_object_id_str(&id_str, hle->commit_id);
4543 if (err)
4544 return err;
4546 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4547 if (err)
4548 goto done;
4550 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4551 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4552 got_object_commit_get_logmsg(folded_commit)) == -1) {
4553 err = got_error_from_errno("asprintf");
4554 goto done;
4556 done:
4557 if (folded_commit)
4558 got_object_commit_close(folded_commit);
4559 free(id_str);
4560 return err;
4563 static struct got_histedit_list_entry *
4564 get_folded_commits(struct got_histedit_list_entry *hle)
4566 struct got_histedit_list_entry *prev, *folded = NULL;
4568 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4569 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4570 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4571 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4572 folded = prev;
4573 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4576 return folded;
4579 static const struct got_error *
4580 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4581 struct got_repository *repo)
4583 char *logmsg_path = NULL, *id_str = NULL;
4584 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4585 const struct got_error *err = NULL;
4586 struct got_commit_object *commit = NULL;
4587 int fd;
4588 struct got_histedit_list_entry *folded = NULL;
4590 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4591 if (err)
4592 return err;
4594 folded = get_folded_commits(hle);
4595 if (folded) {
4596 while (folded != hle) {
4597 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4598 folded = TAILQ_NEXT(folded, entry);
4599 continue;
4601 err = append_folded_commit_msg(&new_msg, folded,
4602 logmsg, repo);
4603 if (err)
4604 goto done;
4605 free(logmsg);
4606 logmsg = new_msg;
4607 folded = TAILQ_NEXT(folded, entry);
4611 err = got_object_id_str(&id_str, hle->commit_id);
4612 if (err)
4613 goto done;
4614 if (asprintf(&new_msg,
4615 "%s\n# original log message of commit %s: %s",
4616 logmsg ? logmsg : "", id_str,
4617 got_object_commit_get_logmsg(commit)) == -1) {
4618 err = got_error_from_errno("asprintf");
4619 goto done;
4621 free(logmsg);
4622 logmsg = new_msg;
4624 err = got_object_id_str(&id_str, hle->commit_id);
4625 if (err)
4626 goto done;
4628 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4629 if (err)
4630 goto done;
4632 dprintf(fd, logmsg);
4633 close(fd);
4635 err = get_editor(&editor);
4636 if (err)
4637 goto done;
4639 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4640 if (err) {
4641 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4642 goto done;
4643 err = NULL;
4644 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4645 if (hle->logmsg == NULL)
4646 err = got_error_from_errno("strdup");
4648 done:
4649 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4650 err = got_error_from_errno2("unlink", logmsg_path);
4651 free(logmsg_path);
4652 free(logmsg);
4653 free(editor);
4654 if (commit)
4655 got_object_commit_close(commit);
4656 return err;
4659 static const struct got_error *
4660 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4661 FILE *f, struct got_repository *repo)
4663 const struct got_error *err = NULL;
4664 char *line = NULL, *p, *end;
4665 size_t size;
4666 ssize_t len;
4667 int lineno = 0, i;
4668 const struct got_histedit_cmd *cmd;
4669 struct got_object_id *commit_id = NULL;
4670 struct got_histedit_list_entry *hle = NULL;
4672 for (;;) {
4673 len = getline(&line, &size, f);
4674 if (len == -1) {
4675 const struct got_error *getline_err;
4676 if (feof(f))
4677 break;
4678 getline_err = got_error_from_errno("getline");
4679 err = got_ferror(f, getline_err->code);
4680 break;
4682 lineno++;
4683 p = line;
4684 while (isspace((unsigned char)p[0]))
4685 p++;
4686 if (p[0] == '#' || p[0] == '\0') {
4687 free(line);
4688 line = NULL;
4689 continue;
4691 cmd = NULL;
4692 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4693 cmd = &got_histedit_cmds[i];
4694 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4695 isspace((unsigned char)p[strlen(cmd->name)])) {
4696 p += strlen(cmd->name);
4697 break;
4699 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4700 p++;
4701 break;
4704 if (i == nitems(got_histedit_cmds)) {
4705 err = histedit_syntax_error(lineno);
4706 break;
4708 while (isspace((unsigned char)p[0]))
4709 p++;
4710 if (cmd->code == GOT_HISTEDIT_MESG) {
4711 if (hle == NULL || hle->logmsg != NULL) {
4712 err = got_error(GOT_ERR_HISTEDIT_CMD);
4713 break;
4715 if (p[0] == '\0') {
4716 err = histedit_edit_logmsg(hle, repo);
4717 if (err)
4718 break;
4719 } else {
4720 hle->logmsg = strdup(p);
4721 if (hle->logmsg == NULL) {
4722 err = got_error_from_errno("strdup");
4723 break;
4726 free(line);
4727 line = NULL;
4728 continue;
4729 } else {
4730 end = p;
4731 while (end[0] && !isspace((unsigned char)end[0]))
4732 end++;
4733 *end = '\0';
4735 err = got_object_resolve_id_str(&commit_id, repo, p);
4736 if (err) {
4737 /* override error code */
4738 err = histedit_syntax_error(lineno);
4739 break;
4742 hle = malloc(sizeof(*hle));
4743 if (hle == NULL) {
4744 err = got_error_from_errno("malloc");
4745 break;
4747 hle->cmd = cmd;
4748 hle->commit_id = commit_id;
4749 hle->logmsg = NULL;
4750 commit_id = NULL;
4751 free(line);
4752 line = NULL;
4753 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4756 free(line);
4757 free(commit_id);
4758 return err;
4761 static const struct got_error *
4762 histedit_check_script(struct got_histedit_list *histedit_cmds,
4763 struct got_object_id_queue *commits, struct got_repository *repo)
4765 const struct got_error *err = NULL;
4766 struct got_object_qid *qid;
4767 struct got_histedit_list_entry *hle;
4768 static char msg[80];
4769 char *id_str;
4771 if (TAILQ_EMPTY(histedit_cmds))
4772 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4773 "histedit script contains no commands");
4774 if (SIMPLEQ_EMPTY(commits))
4775 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4777 SIMPLEQ_FOREACH(qid, commits, entry) {
4778 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4779 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4780 break;
4782 if (hle == NULL) {
4783 err = got_object_id_str(&id_str, qid->id);
4784 if (err)
4785 return err;
4786 snprintf(msg, sizeof(msg),
4787 "commit %s missing from histedit script", id_str);
4788 free(id_str);
4789 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4793 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4794 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4795 "last commit in histedit script cannot be folded");
4797 return NULL;
4800 static const struct got_error *
4801 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4802 const char *path, struct got_object_id_queue *commits,
4803 struct got_repository *repo)
4805 const struct got_error *err = NULL;
4806 char *editor;
4807 FILE *f = NULL;
4809 err = get_editor(&editor);
4810 if (err)
4811 return err;
4813 if (spawn_editor(editor, path) == -1) {
4814 err = got_error_from_errno("failed spawning editor");
4815 goto done;
4818 f = fopen(path, "r");
4819 if (f == NULL) {
4820 err = got_error_from_errno("fopen");
4821 goto done;
4823 err = histedit_parse_list(histedit_cmds, f, repo);
4824 if (err)
4825 goto done;
4827 err = histedit_check_script(histedit_cmds, commits, repo);
4828 done:
4829 if (f && fclose(f) != 0 && err == NULL)
4830 err = got_error_from_errno("fclose");
4831 free(editor);
4832 return err;
4835 static const struct got_error *
4836 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4837 struct got_object_id_queue *, const char *, struct got_repository *);
4839 static const struct got_error *
4840 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4841 struct got_object_id_queue *commits, struct got_repository *repo)
4843 const struct got_error *err;
4844 FILE *f = NULL;
4845 char *path = NULL;
4847 err = got_opentemp_named(&path, &f, "got-histedit");
4848 if (err)
4849 return err;
4851 err = write_cmd_list(f);
4852 if (err)
4853 goto done;
4855 err = histedit_write_commit_list(commits, f, repo);
4856 if (err)
4857 goto done;
4859 if (fclose(f) != 0) {
4860 err = got_error_from_errno("fclose");
4861 goto done;
4863 f = NULL;
4865 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4866 if (err) {
4867 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4868 err->code != GOT_ERR_HISTEDIT_CMD)
4869 goto done;
4870 err = histedit_edit_list_retry(histedit_cmds, err,
4871 commits, path, repo);
4873 done:
4874 if (f && fclose(f) != 0 && err == NULL)
4875 err = got_error_from_errno("fclose");
4876 if (path && unlink(path) != 0 && err == NULL)
4877 err = got_error_from_errno2("unlink", path);
4878 free(path);
4879 return err;
4882 static const struct got_error *
4883 histedit_save_list(struct got_histedit_list *histedit_cmds,
4884 struct got_worktree *worktree, struct got_repository *repo)
4886 const struct got_error *err = NULL;
4887 char *path = NULL;
4888 FILE *f = NULL;
4889 struct got_histedit_list_entry *hle;
4890 struct got_commit_object *commit = NULL;
4892 err = got_worktree_get_histedit_script_path(&path, worktree);
4893 if (err)
4894 return err;
4896 f = fopen(path, "w");
4897 if (f == NULL) {
4898 err = got_error_from_errno2("fopen", path);
4899 goto done;
4901 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4902 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4903 repo);
4904 if (err)
4905 break;
4907 if (hle->logmsg) {
4908 int n = fprintf(f, "%c %s\n",
4909 GOT_HISTEDIT_MESG, hle->logmsg);
4910 if (n < 0) {
4911 err = got_ferror(f, GOT_ERR_IO);
4912 break;
4916 done:
4917 if (f && fclose(f) != 0 && err == NULL)
4918 err = got_error_from_errno("fclose");
4919 free(path);
4920 if (commit)
4921 got_object_commit_close(commit);
4922 return err;
4925 void
4926 histedit_free_list(struct got_histedit_list *histedit_cmds)
4928 struct got_histedit_list_entry *hle;
4930 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4931 TAILQ_REMOVE(histedit_cmds, hle, entry);
4932 free(hle);
4936 static const struct got_error *
4937 histedit_load_list(struct got_histedit_list *histedit_cmds,
4938 const char *path, struct got_repository *repo)
4940 const struct got_error *err = NULL;
4941 FILE *f = NULL;
4943 f = fopen(path, "r");
4944 if (f == NULL) {
4945 err = got_error_from_errno2("fopen", path);
4946 goto done;
4949 err = histedit_parse_list(histedit_cmds, f, repo);
4950 done:
4951 if (f && fclose(f) != 0 && err == NULL)
4952 err = got_error_from_errno("fclose");
4953 return err;
4956 static const struct got_error *
4957 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4958 const struct got_error *edit_err, struct got_object_id_queue *commits,
4959 const char *path, struct got_repository *repo)
4961 const struct got_error *err = NULL, *prev_err = edit_err;
4962 int resp = ' ';
4964 while (resp != 'c' && resp != 'r' && resp != 'a') {
4965 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4966 "or (a)bort: ", getprogname(), prev_err->msg);
4967 resp = getchar();
4968 if (resp == '\n')
4969 resp = getchar();
4970 if (resp == 'c') {
4971 histedit_free_list(histedit_cmds);
4972 err = histedit_run_editor(histedit_cmds, path, commits,
4973 repo);
4974 if (err) {
4975 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4976 err->code != GOT_ERR_HISTEDIT_CMD)
4977 break;
4978 prev_err = err;
4979 resp = ' ';
4980 continue;
4982 break;
4983 } else if (resp == 'r') {
4984 histedit_free_list(histedit_cmds);
4985 err = histedit_edit_script(histedit_cmds,
4986 commits, repo);
4987 if (err) {
4988 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4989 err->code != GOT_ERR_HISTEDIT_CMD)
4990 break;
4991 prev_err = err;
4992 resp = ' ';
4993 continue;
4995 break;
4996 } else if (resp == 'a') {
4997 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4998 break;
4999 } else
5000 printf("invalid response '%c'\n", resp);
5003 return err;
5006 static const struct got_error *
5007 histedit_complete(struct got_worktree *worktree,
5008 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5009 struct got_reference *branch, struct got_repository *repo)
5011 printf("Switching work tree to %s\n",
5012 got_ref_get_symref_target(branch));
5013 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5014 branch, repo);
5017 static const struct got_error *
5018 show_histedit_progress(struct got_commit_object *commit,
5019 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5021 const struct got_error *err;
5022 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5024 err = got_object_id_str(&old_id_str, hle->commit_id);
5025 if (err)
5026 goto done;
5028 if (new_id) {
5029 err = got_object_id_str(&new_id_str, new_id);
5030 if (err)
5031 goto done;
5034 old_id_str[12] = '\0';
5035 if (new_id_str)
5036 new_id_str[12] = '\0';
5038 if (hle->logmsg) {
5039 logmsg = strdup(hle->logmsg);
5040 if (logmsg == NULL) {
5041 err = got_error_from_errno("strdup");
5042 goto done;
5044 trim_logmsg(logmsg, 42);
5045 } else {
5046 err = get_short_logmsg(&logmsg, 42, commit);
5047 if (err)
5048 goto done;
5051 switch (hle->cmd->code) {
5052 case GOT_HISTEDIT_PICK:
5053 case GOT_HISTEDIT_EDIT:
5054 printf("%s -> %s: %s\n", old_id_str,
5055 new_id_str ? new_id_str : "no-op change", logmsg);
5056 break;
5057 case GOT_HISTEDIT_DROP:
5058 case GOT_HISTEDIT_FOLD:
5059 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5060 logmsg);
5061 break;
5062 default:
5063 break;
5066 done:
5067 free(old_id_str);
5068 free(new_id_str);
5069 return err;
5072 static const struct got_error *
5073 histedit_commit(struct got_pathlist_head *merged_paths,
5074 struct got_worktree *worktree, struct got_fileindex *fileindex,
5075 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5076 struct got_repository *repo)
5078 const struct got_error *err;
5079 struct got_commit_object *commit;
5080 struct got_object_id *new_commit_id;
5082 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5083 && hle->logmsg == NULL) {
5084 err = histedit_edit_logmsg(hle, repo);
5085 if (err)
5086 return err;
5089 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5090 if (err)
5091 return err;
5093 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5094 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5095 hle->logmsg, repo);
5096 if (err) {
5097 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5098 goto done;
5099 err = show_histedit_progress(commit, hle, NULL);
5100 } else {
5101 err = show_histedit_progress(commit, hle, new_commit_id);
5102 free(new_commit_id);
5104 done:
5105 got_object_commit_close(commit);
5106 return err;
5109 static const struct got_error *
5110 histedit_skip_commit(struct got_histedit_list_entry *hle,
5111 struct got_worktree *worktree, struct got_repository *repo)
5113 const struct got_error *error;
5114 struct got_commit_object *commit;
5116 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5117 repo);
5118 if (error)
5119 return error;
5121 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5122 if (error)
5123 return error;
5125 error = show_histedit_progress(commit, hle, NULL);
5126 got_object_commit_close(commit);
5127 return error;
5130 static const struct got_error *
5131 cmd_histedit(int argc, char *argv[])
5133 const struct got_error *error = NULL;
5134 struct got_worktree *worktree = NULL;
5135 struct got_fileindex *fileindex = NULL;
5136 struct got_repository *repo = NULL;
5137 char *cwd = NULL;
5138 struct got_reference *branch = NULL;
5139 struct got_reference *tmp_branch = NULL;
5140 struct got_object_id *resume_commit_id = NULL;
5141 struct got_object_id *base_commit_id = NULL;
5142 struct got_object_id *head_commit_id = NULL;
5143 struct got_commit_object *commit = NULL;
5144 int ch, rebase_in_progress = 0, did_something;
5145 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5146 const char *edit_script_path = NULL;
5147 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5148 struct got_object_id_queue commits;
5149 struct got_pathlist_head merged_paths;
5150 const struct got_object_id_queue *parent_ids;
5151 struct got_object_qid *pid;
5152 struct got_histedit_list histedit_cmds;
5153 struct got_histedit_list_entry *hle;
5155 SIMPLEQ_INIT(&commits);
5156 TAILQ_INIT(&histedit_cmds);
5157 TAILQ_INIT(&merged_paths);
5159 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5160 switch (ch) {
5161 case 'a':
5162 abort_edit = 1;
5163 break;
5164 case 'c':
5165 continue_edit = 1;
5166 break;
5167 case 'F':
5168 edit_script_path = optarg;
5169 break;
5170 default:
5171 usage_histedit();
5172 /* NOTREACHED */
5176 argc -= optind;
5177 argv += optind;
5179 #ifndef PROFILE
5180 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5181 "unveil", NULL) == -1)
5182 err(1, "pledge");
5183 #endif
5184 if (abort_edit && continue_edit)
5185 usage_histedit();
5186 if (argc != 0)
5187 usage_histedit();
5190 * This command cannot apply unveil(2) in all cases because the
5191 * user may choose to run an editor to edit the histedit script
5192 * and to edit individual commit log messages.
5193 * unveil(2) traverses exec(2); if an editor is used we have to
5194 * apply unveil after edit script and log messages have been written.
5195 * XXX TODO: Make use of unveil(2) where possible.
5198 cwd = getcwd(NULL, 0);
5199 if (cwd == NULL) {
5200 error = got_error_from_errno("getcwd");
5201 goto done;
5203 error = got_worktree_open(&worktree, cwd);
5204 if (error)
5205 goto done;
5207 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5208 if (error != NULL)
5209 goto done;
5211 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5212 if (error)
5213 goto done;
5214 if (rebase_in_progress) {
5215 error = got_error(GOT_ERR_REBASING);
5216 goto done;
5219 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5220 if (error)
5221 goto done;
5223 if (edit_in_progress && abort_edit) {
5224 error = got_worktree_histedit_continue(&resume_commit_id,
5225 &tmp_branch, &branch, &base_commit_id, &fileindex,
5226 worktree, repo);
5227 if (error)
5228 goto done;
5229 printf("Switching work tree to %s\n",
5230 got_ref_get_symref_target(branch));
5231 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5232 branch, base_commit_id, update_progress, &did_something);
5233 if (error)
5234 goto done;
5235 printf("Histedit of %s aborted\n",
5236 got_ref_get_symref_target(branch));
5237 goto done; /* nothing else to do */
5238 } else if (abort_edit) {
5239 error = got_error(GOT_ERR_NOT_HISTEDIT);
5240 goto done;
5243 if (continue_edit) {
5244 char *path;
5246 if (!edit_in_progress) {
5247 error = got_error(GOT_ERR_NOT_HISTEDIT);
5248 goto done;
5251 error = got_worktree_get_histedit_script_path(&path, worktree);
5252 if (error)
5253 goto done;
5255 error = histedit_load_list(&histedit_cmds, path, repo);
5256 free(path);
5257 if (error)
5258 goto done;
5260 error = got_worktree_histedit_continue(&resume_commit_id,
5261 &tmp_branch, &branch, &base_commit_id, &fileindex,
5262 worktree, repo);
5263 if (error)
5264 goto done;
5266 error = got_ref_resolve(&head_commit_id, repo, branch);
5267 if (error)
5268 goto done;
5270 error = got_object_open_as_commit(&commit, repo,
5271 head_commit_id);
5272 if (error)
5273 goto done;
5274 parent_ids = got_object_commit_get_parent_ids(commit);
5275 pid = SIMPLEQ_FIRST(parent_ids);
5276 if (pid == NULL) {
5277 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5278 goto done;
5280 error = collect_commits(&commits, head_commit_id, pid->id,
5281 base_commit_id, got_worktree_get_path_prefix(worktree),
5282 GOT_ERR_HISTEDIT_PATH, repo);
5283 got_object_commit_close(commit);
5284 commit = NULL;
5285 if (error)
5286 goto done;
5287 } else {
5288 if (edit_in_progress) {
5289 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5290 goto done;
5293 error = got_ref_open(&branch, repo,
5294 got_worktree_get_head_ref_name(worktree), 0);
5295 if (error != NULL)
5296 goto done;
5298 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5299 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5300 "will not edit commit history of a branch outside "
5301 "the \"refs/heads/\" reference namespace");
5302 goto done;
5305 error = got_ref_resolve(&head_commit_id, repo, branch);
5306 got_ref_close(branch);
5307 branch = NULL;
5308 if (error)
5309 goto done;
5311 error = got_object_open_as_commit(&commit, repo,
5312 head_commit_id);
5313 if (error)
5314 goto done;
5315 parent_ids = got_object_commit_get_parent_ids(commit);
5316 pid = SIMPLEQ_FIRST(parent_ids);
5317 if (pid == NULL) {
5318 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5319 goto done;
5321 error = collect_commits(&commits, head_commit_id, pid->id,
5322 got_worktree_get_base_commit_id(worktree),
5323 got_worktree_get_path_prefix(worktree),
5324 GOT_ERR_HISTEDIT_PATH, repo);
5325 got_object_commit_close(commit);
5326 commit = NULL;
5327 if (error)
5328 goto done;
5330 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5331 &base_commit_id, &fileindex, worktree, repo);
5332 if (error)
5333 goto done;
5335 if (edit_script_path) {
5336 error = histedit_load_list(&histedit_cmds,
5337 edit_script_path, repo);
5338 if (error) {
5339 got_worktree_histedit_abort(worktree, fileindex,
5340 repo, branch, base_commit_id,
5341 update_progress, &did_something);
5342 goto done;
5344 } else {
5345 error = histedit_edit_script(&histedit_cmds, &commits,
5346 repo);
5347 if (error) {
5348 got_worktree_histedit_abort(worktree, fileindex,
5349 repo, branch, base_commit_id,
5350 update_progress, &did_something);
5351 goto done;
5356 error = histedit_save_list(&histedit_cmds, worktree,
5357 repo);
5358 if (error) {
5359 got_worktree_histedit_abort(worktree, fileindex,
5360 repo, branch, base_commit_id,
5361 update_progress, &did_something);
5362 goto done;
5367 error = histedit_check_script(&histedit_cmds, &commits, repo);
5368 if (error)
5369 goto done;
5371 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5372 if (resume_commit_id) {
5373 if (got_object_id_cmp(hle->commit_id,
5374 resume_commit_id) != 0)
5375 continue;
5377 resume_commit_id = NULL;
5378 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5379 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5380 error = histedit_skip_commit(hle, worktree,
5381 repo);
5382 } else {
5383 error = histedit_commit(NULL, worktree,
5384 fileindex, tmp_branch, hle, repo);
5386 if (error)
5387 goto done;
5388 continue;
5391 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5392 error = histedit_skip_commit(hle, worktree, repo);
5393 if (error)
5394 goto done;
5395 continue;
5398 error = got_object_open_as_commit(&commit, repo,
5399 hle->commit_id);
5400 if (error)
5401 goto done;
5402 parent_ids = got_object_commit_get_parent_ids(commit);
5403 pid = SIMPLEQ_FIRST(parent_ids);
5405 error = got_worktree_histedit_merge_files(&merged_paths,
5406 worktree, fileindex, pid->id, hle->commit_id, repo,
5407 rebase_progress, &rebase_status, check_cancelled, NULL);
5408 if (error)
5409 goto done;
5410 got_object_commit_close(commit);
5411 commit = NULL;
5413 if (rebase_status == GOT_STATUS_CONFLICT) {
5414 got_worktree_rebase_pathlist_free(&merged_paths);
5415 break;
5418 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5419 char *id_str;
5420 error = got_object_id_str(&id_str, hle->commit_id);
5421 if (error)
5422 goto done;
5423 printf("Stopping histedit for amending commit %s\n",
5424 id_str);
5425 free(id_str);
5426 got_worktree_rebase_pathlist_free(&merged_paths);
5427 error = got_worktree_histedit_postpone(worktree,
5428 fileindex);
5429 goto done;
5432 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5433 error = histedit_skip_commit(hle, worktree, repo);
5434 if (error)
5435 goto done;
5436 continue;
5439 error = histedit_commit(&merged_paths, worktree, fileindex,
5440 tmp_branch, hle, repo);
5441 got_worktree_rebase_pathlist_free(&merged_paths);
5442 if (error)
5443 goto done;
5446 if (rebase_status == GOT_STATUS_CONFLICT) {
5447 error = got_worktree_histedit_postpone(worktree, fileindex);
5448 if (error)
5449 goto done;
5450 error = got_error_msg(GOT_ERR_CONFLICTS,
5451 "conflicts must be resolved before rebasing can continue");
5452 } else
5453 error = histedit_complete(worktree, fileindex, tmp_branch,
5454 branch, repo);
5455 done:
5456 got_object_id_queue_free(&commits);
5457 histedit_free_list(&histedit_cmds);
5458 free(head_commit_id);
5459 free(base_commit_id);
5460 free(resume_commit_id);
5461 if (commit)
5462 got_object_commit_close(commit);
5463 if (branch)
5464 got_ref_close(branch);
5465 if (tmp_branch)
5466 got_ref_close(tmp_branch);
5467 if (worktree)
5468 got_worktree_close(worktree);
5469 if (repo)
5470 got_repo_close(repo);
5471 return error;
5474 __dead static void
5475 usage_stage(void)
5477 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5478 "[file-path ...]\n",
5479 getprogname());
5480 exit(1);
5483 static const struct got_error *
5484 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5485 const char *path, struct got_object_id *blob_id,
5486 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5488 const struct got_error *err = NULL;
5489 char *id_str = NULL;
5491 if (staged_status != GOT_STATUS_ADD &&
5492 staged_status != GOT_STATUS_MODIFY &&
5493 staged_status != GOT_STATUS_DELETE)
5494 return NULL;
5496 if (staged_status == GOT_STATUS_ADD ||
5497 staged_status == GOT_STATUS_MODIFY)
5498 err = got_object_id_str(&id_str, staged_blob_id);
5499 else
5500 err = got_object_id_str(&id_str, blob_id);
5501 if (err)
5502 return err;
5504 printf("%s %c %s\n", id_str, staged_status, path);
5505 free(id_str);
5506 return NULL;
5509 static const struct got_error *
5510 cmd_stage(int argc, char *argv[])
5512 const struct got_error *error = NULL;
5513 struct got_repository *repo = NULL;
5514 struct got_worktree *worktree = NULL;
5515 char *cwd = NULL;
5516 struct got_pathlist_head paths;
5517 struct got_pathlist_entry *pe;
5518 int ch, list_stage = 0, pflag = 0;
5519 FILE *patch_script_file = NULL;
5520 const char *patch_script_path = NULL;
5521 struct choose_patch_arg cpa;
5523 TAILQ_INIT(&paths);
5525 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5526 switch (ch) {
5527 case 'l':
5528 list_stage = 1;
5529 break;
5530 case 'p':
5531 pflag = 1;
5532 break;
5533 case 'F':
5534 patch_script_path = optarg;
5535 break;
5536 default:
5537 usage_stage();
5538 /* NOTREACHED */
5542 argc -= optind;
5543 argv += optind;
5545 #ifndef PROFILE
5546 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5547 "unveil", NULL) == -1)
5548 err(1, "pledge");
5549 #endif
5550 if (list_stage && (pflag || patch_script_path))
5551 errx(1, "-l option cannot be used with other options");
5552 if (patch_script_path && !pflag)
5553 errx(1, "-F option can only be used together with -p option");
5555 cwd = getcwd(NULL, 0);
5556 if (cwd == NULL) {
5557 error = got_error_from_errno("getcwd");
5558 goto done;
5561 error = got_worktree_open(&worktree, cwd);
5562 if (error)
5563 goto done;
5565 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5566 if (error != NULL)
5567 goto done;
5569 if (patch_script_path) {
5570 patch_script_file = fopen(patch_script_path, "r");
5571 if (patch_script_file == NULL) {
5572 error = got_error_from_errno2("fopen",
5573 patch_script_path);
5574 goto done;
5577 error = apply_unveil(got_repo_get_path(repo), 1,
5578 got_worktree_get_root_path(worktree));
5579 if (error)
5580 goto done;
5582 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5583 if (error)
5584 goto done;
5586 if (list_stage)
5587 error = got_worktree_status(worktree, &paths, repo,
5588 print_stage, NULL, check_cancelled, NULL);
5589 else {
5590 cpa.patch_script_file = patch_script_file;
5591 cpa.action = "stage";
5592 error = got_worktree_stage(worktree, &paths,
5593 pflag ? NULL : print_status, NULL,
5594 pflag ? choose_patch : NULL, &cpa, repo);
5596 done:
5597 if (patch_script_file && fclose(patch_script_file) == EOF &&
5598 error == NULL)
5599 error = got_error_from_errno2("fclose", patch_script_path);
5600 if (repo)
5601 got_repo_close(repo);
5602 if (worktree)
5603 got_worktree_close(worktree);
5604 TAILQ_FOREACH(pe, &paths, entry)
5605 free((char *)pe->path);
5606 got_pathlist_free(&paths);
5607 free(cwd);
5608 return error;
5611 __dead static void
5612 usage_unstage(void)
5614 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5615 "[file-path ...]\n",
5616 getprogname());
5617 exit(1);
5621 static const struct got_error *
5622 cmd_unstage(int argc, char *argv[])
5624 const struct got_error *error = NULL;
5625 struct got_repository *repo = NULL;
5626 struct got_worktree *worktree = NULL;
5627 char *cwd = NULL;
5628 struct got_pathlist_head paths;
5629 struct got_pathlist_entry *pe;
5630 int ch, did_something = 0, pflag = 0;
5631 FILE *patch_script_file = NULL;
5632 const char *patch_script_path = NULL;
5633 struct choose_patch_arg cpa;
5635 TAILQ_INIT(&paths);
5637 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5638 switch (ch) {
5639 case 'p':
5640 pflag = 1;
5641 break;
5642 case 'F':
5643 patch_script_path = optarg;
5644 break;
5645 default:
5646 usage_unstage();
5647 /* NOTREACHED */
5651 argc -= optind;
5652 argv += optind;
5654 #ifndef PROFILE
5655 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5656 "unveil", NULL) == -1)
5657 err(1, "pledge");
5658 #endif
5659 if (patch_script_path && !pflag)
5660 errx(1, "-F option can only be used together with -p option");
5662 cwd = getcwd(NULL, 0);
5663 if (cwd == NULL) {
5664 error = got_error_from_errno("getcwd");
5665 goto done;
5668 error = got_worktree_open(&worktree, cwd);
5669 if (error)
5670 goto done;
5672 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5673 if (error != NULL)
5674 goto done;
5676 if (patch_script_path) {
5677 patch_script_file = fopen(patch_script_path, "r");
5678 if (patch_script_file == NULL) {
5679 error = got_error_from_errno2("fopen",
5680 patch_script_path);
5681 goto done;
5685 error = apply_unveil(got_repo_get_path(repo), 1,
5686 got_worktree_get_root_path(worktree));
5687 if (error)
5688 goto done;
5690 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5691 if (error)
5692 goto done;
5694 cpa.patch_script_file = patch_script_file;
5695 cpa.action = "unstage";
5696 error = got_worktree_unstage(worktree, &paths, update_progress,
5697 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5698 done:
5699 if (patch_script_file && fclose(patch_script_file) == EOF &&
5700 error == NULL)
5701 error = got_error_from_errno2("fclose", patch_script_path);
5702 if (repo)
5703 got_repo_close(repo);
5704 if (worktree)
5705 got_worktree_close(worktree);
5706 TAILQ_FOREACH(pe, &paths, entry)
5707 free((char *)pe->path);
5708 got_pathlist_free(&paths);
5709 free(cwd);
5710 return error;