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 if (err->code != GOT_ERR_OBJ_TYPE)
1407 return err;
1408 /* Ref points at something other than a tag. */
1409 err = NULL;
1410 tag = NULL;
1413 cmp = got_object_id_cmp(tag ?
1414 got_object_tag_get_object_id(tag) : re->id, id);
1415 if (tag)
1416 got_object_tag_close(tag);
1417 if (cmp != 0)
1418 continue;
1419 s = refs_str;
1420 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1421 name) == -1) {
1422 err = got_error_from_errno("asprintf");
1423 free(s);
1424 return err;
1426 free(s);
1428 err = got_object_id_str(&id_str, id);
1429 if (err)
1430 return err;
1432 printf(GOT_COMMIT_SEP_STR);
1433 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1434 refs_str ? refs_str : "", refs_str ? ")" : "");
1435 free(id_str);
1436 id_str = NULL;
1437 free(refs_str);
1438 refs_str = NULL;
1439 printf("from: %s\n", got_object_commit_get_author(commit));
1440 committer_time = got_object_commit_get_committer_time(commit);
1441 datestr = get_datestr(&committer_time, datebuf);
1442 printf("date: %s UTC\n", datestr);
1443 author = got_object_commit_get_author(commit);
1444 committer = got_object_commit_get_committer(commit);
1445 if (strcmp(author, committer) != 0)
1446 printf("via: %s\n", committer);
1447 if (got_object_commit_get_nparents(commit) > 1) {
1448 const struct got_object_id_queue *parent_ids;
1449 struct got_object_qid *qid;
1450 int n = 1;
1451 parent_ids = got_object_commit_get_parent_ids(commit);
1452 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1453 err = got_object_id_str(&id_str, qid->id);
1454 if (err)
1455 return err;
1456 printf("parent %d: %s\n", n++, id_str);
1457 free(id_str);
1461 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1462 if (logmsg0 == NULL)
1463 return got_error_from_errno("strdup");
1465 logmsg = logmsg0;
1466 do {
1467 line = strsep(&logmsg, "\n");
1468 if (line)
1469 printf(" %s\n", line);
1470 } while (line);
1471 free(logmsg0);
1473 if (show_patch) {
1474 err = print_patch(commit, id, diff_context, repo);
1475 if (err == 0)
1476 printf("\n");
1479 if (fflush(stdout) != 0 && err == NULL)
1480 err = got_error_from_errno("fflush");
1481 return err;
1484 static const struct got_error *
1485 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1486 char *path, int show_patch, int diff_context, int limit,
1487 int first_parent_traversal, struct got_reflist_head *refs)
1489 const struct got_error *err;
1490 struct got_commit_graph *graph;
1492 err = got_commit_graph_open(&graph, root_id, path,
1493 first_parent_traversal, repo);
1494 if (err)
1495 return err;
1496 err = got_commit_graph_iter_start(graph, root_id, repo);
1497 if (err)
1498 goto done;
1499 for (;;) {
1500 struct got_commit_object *commit;
1501 struct got_object_id *id;
1503 if (sigint_received || sigpipe_received)
1504 break;
1506 err = got_commit_graph_iter_next(&id, graph);
1507 if (err) {
1508 if (err->code == GOT_ERR_ITER_COMPLETED) {
1509 err = NULL;
1510 break;
1512 if (err->code != GOT_ERR_ITER_NEED_MORE)
1513 break;
1514 err = got_commit_graph_fetch_commits(graph, 1, repo);
1515 if (err)
1516 break;
1517 else
1518 continue;
1520 if (id == NULL)
1521 break;
1523 err = got_object_open_as_commit(&commit, repo, id);
1524 if (err)
1525 break;
1526 err = print_commit(commit, id, repo, show_patch, diff_context,
1527 refs);
1528 got_object_commit_close(commit);
1529 if (err || (limit && --limit == 0))
1530 break;
1532 done:
1533 got_commit_graph_close(graph);
1534 return err;
1537 __dead static void
1538 usage_log(void)
1540 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1541 "[-r repository-path] [path]\n", getprogname());
1542 exit(1);
1545 static const struct got_error *
1546 cmd_log(int argc, char *argv[])
1548 const struct got_error *error;
1549 struct got_repository *repo = NULL;
1550 struct got_worktree *worktree = NULL;
1551 struct got_commit_object *commit = NULL;
1552 struct got_object_id *id = NULL;
1553 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1554 char *start_commit = NULL;
1555 int diff_context = 3, ch;
1556 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1557 const char *errstr;
1558 struct got_reflist_head refs;
1560 SIMPLEQ_INIT(&refs);
1562 #ifndef PROFILE
1563 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1564 NULL)
1565 == -1)
1566 err(1, "pledge");
1567 #endif
1569 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1570 switch (ch) {
1571 case 'p':
1572 show_patch = 1;
1573 break;
1574 case 'c':
1575 start_commit = optarg;
1576 break;
1577 case 'C':
1578 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1579 &errstr);
1580 if (errstr != NULL)
1581 err(1, "-C option %s", errstr);
1582 break;
1583 case 'l':
1584 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1585 if (errstr != NULL)
1586 err(1, "-l option %s", errstr);
1587 break;
1588 case 'f':
1589 first_parent_traversal = 1;
1590 break;
1591 case 'r':
1592 repo_path = realpath(optarg, NULL);
1593 if (repo_path == NULL)
1594 err(1, "-r option");
1595 got_path_strip_trailing_slashes(repo_path);
1596 break;
1597 default:
1598 usage_log();
1599 /* NOTREACHED */
1603 argc -= optind;
1604 argv += optind;
1606 cwd = getcwd(NULL, 0);
1607 if (cwd == NULL) {
1608 error = got_error_from_errno("getcwd");
1609 goto done;
1612 error = got_worktree_open(&worktree, cwd);
1613 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1614 goto done;
1615 error = NULL;
1617 if (argc == 0) {
1618 path = strdup("");
1619 if (path == NULL) {
1620 error = got_error_from_errno("strdup");
1621 goto done;
1623 } else if (argc == 1) {
1624 if (worktree) {
1625 error = got_worktree_resolve_path(&path, worktree,
1626 argv[0]);
1627 if (error)
1628 goto done;
1629 } else {
1630 path = strdup(argv[0]);
1631 if (path == NULL) {
1632 error = got_error_from_errno("strdup");
1633 goto done;
1636 } else
1637 usage_log();
1639 if (repo_path == NULL) {
1640 repo_path = worktree ?
1641 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1643 if (repo_path == NULL) {
1644 error = got_error_from_errno("strdup");
1645 goto done;
1648 error = got_repo_open(&repo, repo_path);
1649 if (error != NULL)
1650 goto done;
1652 error = apply_unveil(got_repo_get_path(repo), 1,
1653 worktree ? got_worktree_get_root_path(worktree) : NULL);
1654 if (error)
1655 goto done;
1657 if (start_commit == NULL) {
1658 struct got_reference *head_ref;
1659 error = got_ref_open(&head_ref, repo,
1660 worktree ? got_worktree_get_head_ref_name(worktree)
1661 : GOT_REF_HEAD, 0);
1662 if (error != NULL)
1663 return error;
1664 error = got_ref_resolve(&id, repo, head_ref);
1665 got_ref_close(head_ref);
1666 if (error != NULL)
1667 return error;
1668 error = got_object_open_as_commit(&commit, repo, id);
1669 } else {
1670 struct got_reference *ref;
1671 error = got_ref_open(&ref, repo, start_commit, 0);
1672 if (error == NULL) {
1673 int obj_type;
1674 error = got_ref_resolve(&id, repo, ref);
1675 got_ref_close(ref);
1676 if (error != NULL)
1677 goto done;
1678 error = got_object_get_type(&obj_type, repo, id);
1679 if (error != NULL)
1680 goto done;
1681 if (obj_type == GOT_OBJ_TYPE_TAG) {
1682 struct got_tag_object *tag;
1683 error = got_object_open_as_tag(&tag, repo, id);
1684 if (error != NULL)
1685 goto done;
1686 if (got_object_tag_get_object_type(tag) !=
1687 GOT_OBJ_TYPE_COMMIT) {
1688 got_object_tag_close(tag);
1689 error = got_error(GOT_ERR_OBJ_TYPE);
1690 goto done;
1692 free(id);
1693 id = got_object_id_dup(
1694 got_object_tag_get_object_id(tag));
1695 if (id == NULL)
1696 error = got_error_from_errno(
1697 "got_object_id_dup");
1698 got_object_tag_close(tag);
1699 if (error)
1700 goto done;
1701 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1702 error = got_error(GOT_ERR_OBJ_TYPE);
1703 goto done;
1705 error = got_object_open_as_commit(&commit, repo, id);
1706 if (error != NULL)
1707 goto done;
1709 if (commit == NULL) {
1710 error = got_repo_match_object_id_prefix(&id,
1711 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1712 if (error != NULL)
1713 return error;
1716 if (error != NULL)
1717 goto done;
1719 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1720 if (error != NULL)
1721 goto done;
1722 if (in_repo_path) {
1723 free(path);
1724 path = in_repo_path;
1727 error = got_ref_list(&refs, repo);
1728 if (error)
1729 goto done;
1731 error = print_commits(id, repo, path, show_patch,
1732 diff_context, limit, first_parent_traversal, &refs);
1733 done:
1734 free(path);
1735 free(repo_path);
1736 free(cwd);
1737 free(id);
1738 if (worktree)
1739 got_worktree_close(worktree);
1740 if (repo) {
1741 const struct got_error *repo_error;
1742 repo_error = got_repo_close(repo);
1743 if (error == NULL)
1744 error = repo_error;
1746 got_ref_list_free(&refs);
1747 return error;
1750 __dead static void
1751 usage_diff(void)
1753 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1754 "[object1 object2 | path]\n", getprogname());
1755 exit(1);
1758 struct print_diff_arg {
1759 struct got_repository *repo;
1760 struct got_worktree *worktree;
1761 int diff_context;
1762 const char *id_str;
1763 int header_shown;
1764 int diff_staged;
1767 static const struct got_error *
1768 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1769 const char *path, struct got_object_id *blob_id,
1770 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1772 struct print_diff_arg *a = arg;
1773 const struct got_error *err = NULL;
1774 struct got_blob_object *blob1 = NULL;
1775 FILE *f2 = NULL;
1776 char *abspath = NULL, *label1 = NULL;
1777 struct stat sb;
1779 if (a->diff_staged) {
1780 if (staged_status != GOT_STATUS_MODIFY &&
1781 staged_status != GOT_STATUS_ADD &&
1782 staged_status != GOT_STATUS_DELETE)
1783 return NULL;
1784 } else {
1785 if (staged_status == GOT_STATUS_DELETE)
1786 return NULL;
1787 if (status != GOT_STATUS_MODIFY &&
1788 status != GOT_STATUS_ADD &&
1789 status != GOT_STATUS_DELETE &&
1790 status != GOT_STATUS_CONFLICT)
1791 return NULL;
1794 if (!a->header_shown) {
1795 printf("diff %s %s%s\n", a->id_str,
1796 got_worktree_get_root_path(a->worktree),
1797 a->diff_staged ? " (staged changes)" : "");
1798 a->header_shown = 1;
1801 if (a->diff_staged) {
1802 const char *label1 = NULL, *label2 = NULL;
1803 switch (staged_status) {
1804 case GOT_STATUS_MODIFY:
1805 label1 = path;
1806 label2 = path;
1807 break;
1808 case GOT_STATUS_ADD:
1809 label2 = path;
1810 break;
1811 case GOT_STATUS_DELETE:
1812 label1 = path;
1813 break;
1814 default:
1815 return got_error(GOT_ERR_FILE_STATUS);
1817 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1818 label1, label2, a->diff_context, a->repo, stdout);
1821 if (staged_status == GOT_STATUS_ADD ||
1822 staged_status == GOT_STATUS_MODIFY) {
1823 char *id_str;
1824 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1825 8192);
1826 if (err)
1827 goto done;
1828 err = got_object_id_str(&id_str, staged_blob_id);
1829 if (err)
1830 goto done;
1831 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1832 err = got_error_from_errno("asprintf");
1833 free(id_str);
1834 goto done;
1836 free(id_str);
1837 } else if (status != GOT_STATUS_ADD) {
1838 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1839 if (err)
1840 goto done;
1843 if (status != GOT_STATUS_DELETE) {
1844 if (asprintf(&abspath, "%s/%s",
1845 got_worktree_get_root_path(a->worktree), path) == -1) {
1846 err = got_error_from_errno("asprintf");
1847 goto done;
1850 f2 = fopen(abspath, "r");
1851 if (f2 == NULL) {
1852 err = got_error_from_errno2("fopen", abspath);
1853 goto done;
1855 if (lstat(abspath, &sb) == -1) {
1856 err = got_error_from_errno2("lstat", abspath);
1857 goto done;
1859 } else
1860 sb.st_size = 0;
1862 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1863 a->diff_context, stdout);
1864 done:
1865 if (blob1)
1866 got_object_blob_close(blob1);
1867 if (f2 && fclose(f2) != 0 && err == NULL)
1868 err = got_error_from_errno("fclose");
1869 free(abspath);
1870 return err;
1873 static const struct got_error *
1874 match_object_id(struct got_object_id **id, char **label,
1875 const char *id_str, int obj_type, struct got_repository *repo)
1877 const struct got_error *err;
1878 struct got_tag_object *tag;
1879 struct got_reference *ref = NULL;
1881 *id = NULL;
1882 *label = NULL;
1884 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1885 if (err == NULL) {
1886 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1887 if (*id == NULL)
1888 err = got_error_from_errno("got_object_id_dup");
1889 if (asprintf(label, "refs/tags/%s",
1890 got_object_tag_get_name(tag)) == -1)
1891 err = got_error_from_errno("asprintf");
1892 got_object_tag_close(tag);
1893 return err;
1894 } else if (err->code != GOT_ERR_NO_OBJ)
1895 return err;
1897 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1898 if (err) {
1899 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1900 return err;
1901 err = got_ref_open(&ref, repo, id_str, 0);
1902 if (err != NULL)
1903 goto done;
1904 *label = strdup(got_ref_get_name(ref));
1905 if (*label == NULL) {
1906 err = got_error_from_errno("strdup");
1907 goto done;
1909 err = got_ref_resolve(id, repo, ref);
1910 } else {
1911 err = got_object_id_str(label, *id);
1912 if (*label == NULL) {
1913 err = got_error_from_errno("strdup");
1914 goto done;
1917 done:
1918 if (ref)
1919 got_ref_close(ref);
1920 return err;
1924 static const struct got_error *
1925 cmd_diff(int argc, char *argv[])
1927 const struct got_error *error;
1928 struct got_repository *repo = NULL;
1929 struct got_worktree *worktree = NULL;
1930 char *cwd = NULL, *repo_path = NULL;
1931 struct got_object_id *id1 = NULL, *id2 = NULL;
1932 const char *id_str1 = NULL, *id_str2 = NULL;
1933 char *label1 = NULL, *label2 = NULL;
1934 int type1, type2;
1935 int diff_context = 3, diff_staged = 0, ch;
1936 const char *errstr;
1937 char *path = NULL;
1939 #ifndef PROFILE
1940 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1941 NULL) == -1)
1942 err(1, "pledge");
1943 #endif
1945 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1946 switch (ch) {
1947 case 'C':
1948 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1949 if (errstr != NULL)
1950 err(1, "-C option %s", errstr);
1951 break;
1952 case 'r':
1953 repo_path = realpath(optarg, NULL);
1954 if (repo_path == NULL)
1955 err(1, "-r option");
1956 got_path_strip_trailing_slashes(repo_path);
1957 break;
1958 case 's':
1959 diff_staged = 1;
1960 break;
1961 default:
1962 usage_diff();
1963 /* NOTREACHED */
1967 argc -= optind;
1968 argv += optind;
1970 cwd = getcwd(NULL, 0);
1971 if (cwd == NULL) {
1972 error = got_error_from_errno("getcwd");
1973 goto done;
1975 error = got_worktree_open(&worktree, cwd);
1976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1977 goto done;
1978 if (argc <= 1) {
1979 if (worktree == NULL) {
1980 error = got_error(GOT_ERR_NOT_WORKTREE);
1981 goto done;
1983 if (repo_path)
1984 errx(1,
1985 "-r option can't be used when diffing a work tree");
1986 repo_path = strdup(got_worktree_get_repo_path(worktree));
1987 if (repo_path == NULL) {
1988 error = got_error_from_errno("strdup");
1989 goto done;
1991 if (argc == 1) {
1992 error = got_worktree_resolve_path(&path, worktree,
1993 argv[0]);
1994 if (error)
1995 goto done;
1996 } else {
1997 path = strdup("");
1998 if (path == NULL) {
1999 error = got_error_from_errno("strdup");
2000 goto done;
2003 } else if (argc == 2) {
2004 if (diff_staged)
2005 errx(1, "-s option can't be used when diffing "
2006 "objects in repository");
2007 id_str1 = argv[0];
2008 id_str2 = argv[1];
2009 if (worktree && repo_path == NULL) {
2010 repo_path =
2011 strdup(got_worktree_get_repo_path(worktree));
2012 if (repo_path == NULL) {
2013 error = got_error_from_errno("strdup");
2014 goto done;
2017 } else
2018 usage_diff();
2020 if (repo_path == NULL) {
2021 repo_path = getcwd(NULL, 0);
2022 if (repo_path == NULL)
2023 return got_error_from_errno("getcwd");
2026 error = got_repo_open(&repo, repo_path);
2027 free(repo_path);
2028 if (error != NULL)
2029 goto done;
2031 error = apply_unveil(got_repo_get_path(repo), 1,
2032 worktree ? got_worktree_get_root_path(worktree) : NULL);
2033 if (error)
2034 goto done;
2036 if (argc <= 1) {
2037 struct print_diff_arg arg;
2038 struct got_pathlist_head paths;
2039 char *id_str;
2041 TAILQ_INIT(&paths);
2043 error = got_object_id_str(&id_str,
2044 got_worktree_get_base_commit_id(worktree));
2045 if (error)
2046 goto done;
2047 arg.repo = repo;
2048 arg.worktree = worktree;
2049 arg.diff_context = diff_context;
2050 arg.id_str = id_str;
2051 arg.header_shown = 0;
2052 arg.diff_staged = diff_staged;
2054 error = got_pathlist_append(&paths, path, NULL);
2055 if (error)
2056 goto done;
2058 error = got_worktree_status(worktree, &paths, repo, print_diff,
2059 &arg, check_cancelled, NULL);
2060 free(id_str);
2061 got_pathlist_free(&paths);
2062 goto done;
2065 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2066 if (error)
2067 goto done;
2069 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2070 if (error)
2071 goto done;
2073 error = got_object_get_type(&type1, repo, id1);
2074 if (error)
2075 goto done;
2077 error = got_object_get_type(&type2, repo, id2);
2078 if (error)
2079 goto done;
2081 if (type1 != type2) {
2082 error = got_error(GOT_ERR_OBJ_TYPE);
2083 goto done;
2086 switch (type1) {
2087 case GOT_OBJ_TYPE_BLOB:
2088 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2089 diff_context, repo, stdout);
2090 break;
2091 case GOT_OBJ_TYPE_TREE:
2092 error = got_diff_objects_as_trees(id1, id2, "", "",
2093 diff_context, repo, stdout);
2094 break;
2095 case GOT_OBJ_TYPE_COMMIT:
2096 printf("diff %s %s\n", label1, label2);
2097 error = got_diff_objects_as_commits(id1, id2, diff_context,
2098 repo, stdout);
2099 break;
2100 default:
2101 error = got_error(GOT_ERR_OBJ_TYPE);
2104 done:
2105 free(label1);
2106 free(label2);
2107 free(id1);
2108 free(id2);
2109 free(path);
2110 if (worktree)
2111 got_worktree_close(worktree);
2112 if (repo) {
2113 const struct got_error *repo_error;
2114 repo_error = got_repo_close(repo);
2115 if (error == NULL)
2116 error = repo_error;
2118 return error;
2121 __dead static void
2122 usage_blame(void)
2124 fprintf(stderr,
2125 "usage: %s blame [-c commit] [-r repository-path] path\n",
2126 getprogname());
2127 exit(1);
2130 static const struct got_error *
2131 cmd_blame(int argc, char *argv[])
2133 const struct got_error *error;
2134 struct got_repository *repo = NULL;
2135 struct got_worktree *worktree = NULL;
2136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2137 struct got_object_id *commit_id = NULL;
2138 char *commit_id_str = NULL;
2139 int ch;
2141 #ifndef PROFILE
2142 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2143 NULL) == -1)
2144 err(1, "pledge");
2145 #endif
2147 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2148 switch (ch) {
2149 case 'c':
2150 commit_id_str = optarg;
2151 break;
2152 case 'r':
2153 repo_path = realpath(optarg, NULL);
2154 if (repo_path == NULL)
2155 err(1, "-r option");
2156 got_path_strip_trailing_slashes(repo_path);
2157 break;
2158 default:
2159 usage_blame();
2160 /* NOTREACHED */
2164 argc -= optind;
2165 argv += optind;
2167 if (argc == 1)
2168 path = argv[0];
2169 else
2170 usage_blame();
2172 cwd = getcwd(NULL, 0);
2173 if (cwd == NULL) {
2174 error = got_error_from_errno("getcwd");
2175 goto done;
2177 if (repo_path == NULL) {
2178 error = got_worktree_open(&worktree, cwd);
2179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2180 goto done;
2181 else
2182 error = NULL;
2183 if (worktree) {
2184 repo_path =
2185 strdup(got_worktree_get_repo_path(worktree));
2186 if (repo_path == NULL)
2187 error = got_error_from_errno("strdup");
2188 if (error)
2189 goto done;
2190 } else {
2191 repo_path = strdup(cwd);
2192 if (repo_path == NULL) {
2193 error = got_error_from_errno("strdup");
2194 goto done;
2199 error = got_repo_open(&repo, repo_path);
2200 if (error != NULL)
2201 goto done;
2203 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2204 if (error)
2205 goto done;
2207 if (worktree) {
2208 const char *prefix = got_worktree_get_path_prefix(worktree);
2209 char *p, *worktree_subdir = cwd +
2210 strlen(got_worktree_get_root_path(worktree));
2211 if (asprintf(&p, "%s%s%s%s%s",
2212 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2213 worktree_subdir, worktree_subdir[0] ? "/" : "",
2214 path) == -1) {
2215 error = got_error_from_errno("asprintf");
2216 goto done;
2218 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2219 free(p);
2220 } else {
2221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2223 if (error)
2224 goto done;
2226 if (commit_id_str == NULL) {
2227 struct got_reference *head_ref;
2228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2229 if (error != NULL)
2230 goto done;
2231 error = got_ref_resolve(&commit_id, repo, head_ref);
2232 got_ref_close(head_ref);
2233 if (error != NULL)
2234 goto done;
2235 } else {
2236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2237 if (error)
2238 goto done;
2241 error = got_blame(in_repo_path, commit_id, repo, stdout);
2242 done:
2243 free(in_repo_path);
2244 free(repo_path);
2245 free(cwd);
2246 free(commit_id);
2247 if (worktree)
2248 got_worktree_close(worktree);
2249 if (repo) {
2250 const struct got_error *repo_error;
2251 repo_error = got_repo_close(repo);
2252 if (error == NULL)
2253 error = repo_error;
2255 return error;
2258 __dead static void
2259 usage_tree(void)
2261 fprintf(stderr,
2262 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2263 getprogname());
2264 exit(1);
2267 static void
2268 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2269 const char *root_path)
2271 int is_root_path = (strcmp(path, root_path) == 0);
2272 const char *modestr = "";
2274 path += strlen(root_path);
2275 while (path[0] == '/')
2276 path++;
2278 if (S_ISLNK(te->mode))
2279 modestr = "@";
2280 else if (S_ISDIR(te->mode))
2281 modestr = "/";
2282 else if (te->mode & S_IXUSR)
2283 modestr = "*";
2285 printf("%s%s%s%s%s\n", id ? id : "", path,
2286 is_root_path ? "" : "/", te->name, modestr);
2289 static const struct got_error *
2290 print_tree(const char *path, struct got_object_id *commit_id,
2291 int show_ids, int recurse, const char *root_path,
2292 struct got_repository *repo)
2294 const struct got_error *err = NULL;
2295 struct got_object_id *tree_id = NULL;
2296 struct got_tree_object *tree = NULL;
2297 const struct got_tree_entries *entries;
2298 struct got_tree_entry *te;
2300 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2301 if (err)
2302 goto done;
2304 err = got_object_open_as_tree(&tree, repo, tree_id);
2305 if (err)
2306 goto done;
2307 entries = got_object_tree_get_entries(tree);
2308 te = SIMPLEQ_FIRST(&entries->head);
2309 while (te) {
2310 char *id = NULL;
2312 if (sigint_received || sigpipe_received)
2313 break;
2315 if (show_ids) {
2316 char *id_str;
2317 err = got_object_id_str(&id_str, te->id);
2318 if (err)
2319 goto done;
2320 if (asprintf(&id, "%s ", id_str) == -1) {
2321 err = got_error_from_errno("asprintf");
2322 free(id_str);
2323 goto done;
2325 free(id_str);
2327 print_entry(te, id, path, root_path);
2328 free(id);
2330 if (recurse && S_ISDIR(te->mode)) {
2331 char *child_path;
2332 if (asprintf(&child_path, "%s%s%s", path,
2333 path[0] == '/' && path[1] == '\0' ? "" : "/",
2334 te->name) == -1) {
2335 err = got_error_from_errno("asprintf");
2336 goto done;
2338 err = print_tree(child_path, commit_id, show_ids, 1,
2339 root_path, repo);
2340 free(child_path);
2341 if (err)
2342 goto done;
2345 te = SIMPLEQ_NEXT(te, entry);
2347 done:
2348 if (tree)
2349 got_object_tree_close(tree);
2350 free(tree_id);
2351 return err;
2354 static const struct got_error *
2355 cmd_tree(int argc, char *argv[])
2357 const struct got_error *error;
2358 struct got_repository *repo = NULL;
2359 struct got_worktree *worktree = NULL;
2360 const char *path;
2361 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2362 struct got_object_id *commit_id = NULL;
2363 char *commit_id_str = NULL;
2364 int show_ids = 0, recurse = 0;
2365 int ch;
2367 #ifndef PROFILE
2368 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2369 NULL) == -1)
2370 err(1, "pledge");
2371 #endif
2373 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2374 switch (ch) {
2375 case 'c':
2376 commit_id_str = optarg;
2377 break;
2378 case 'r':
2379 repo_path = realpath(optarg, NULL);
2380 if (repo_path == NULL)
2381 err(1, "-r option");
2382 got_path_strip_trailing_slashes(repo_path);
2383 break;
2384 case 'i':
2385 show_ids = 1;
2386 break;
2387 case 'R':
2388 recurse = 1;
2389 break;
2390 default:
2391 usage_tree();
2392 /* NOTREACHED */
2396 argc -= optind;
2397 argv += optind;
2399 if (argc == 1)
2400 path = argv[0];
2401 else if (argc > 1)
2402 usage_tree();
2403 else
2404 path = NULL;
2406 cwd = getcwd(NULL, 0);
2407 if (cwd == NULL) {
2408 error = got_error_from_errno("getcwd");
2409 goto done;
2411 if (repo_path == NULL) {
2412 error = got_worktree_open(&worktree, cwd);
2413 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2414 goto done;
2415 else
2416 error = NULL;
2417 if (worktree) {
2418 repo_path =
2419 strdup(got_worktree_get_repo_path(worktree));
2420 if (repo_path == NULL)
2421 error = got_error_from_errno("strdup");
2422 if (error)
2423 goto done;
2424 } else {
2425 repo_path = strdup(cwd);
2426 if (repo_path == NULL) {
2427 error = got_error_from_errno("strdup");
2428 goto done;
2433 error = got_repo_open(&repo, repo_path);
2434 if (error != NULL)
2435 goto done;
2437 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2438 if (error)
2439 goto done;
2441 if (path == NULL) {
2442 if (worktree) {
2443 char *p, *worktree_subdir = cwd +
2444 strlen(got_worktree_get_root_path(worktree));
2445 if (asprintf(&p, "%s/%s",
2446 got_worktree_get_path_prefix(worktree),
2447 worktree_subdir) == -1) {
2448 error = got_error_from_errno("asprintf");
2449 goto done;
2451 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2452 free(p);
2453 if (error)
2454 goto done;
2455 } else
2456 path = "/";
2458 if (in_repo_path == NULL) {
2459 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2460 if (error != NULL)
2461 goto done;
2464 if (commit_id_str == NULL) {
2465 struct got_reference *head_ref;
2466 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2467 if (error != NULL)
2468 goto done;
2469 error = got_ref_resolve(&commit_id, repo, head_ref);
2470 got_ref_close(head_ref);
2471 if (error != NULL)
2472 goto done;
2473 } else {
2474 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2475 if (error)
2476 goto done;
2479 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2480 in_repo_path, repo);
2481 done:
2482 free(in_repo_path);
2483 free(repo_path);
2484 free(cwd);
2485 free(commit_id);
2486 if (worktree)
2487 got_worktree_close(worktree);
2488 if (repo) {
2489 const struct got_error *repo_error;
2490 repo_error = got_repo_close(repo);
2491 if (error == NULL)
2492 error = repo_error;
2494 return error;
2497 __dead static void
2498 usage_status(void)
2500 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2501 exit(1);
2504 static const struct got_error *
2505 print_status(void *arg, unsigned char status, unsigned char staged_status,
2506 const char *path, struct got_object_id *blob_id,
2507 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2509 if (status == staged_status && (status == GOT_STATUS_DELETE))
2510 status = GOT_STATUS_NO_CHANGE;
2511 printf("%c%c %s\n", status, staged_status, path);
2512 return NULL;
2515 static const struct got_error *
2516 cmd_status(int argc, char *argv[])
2518 const struct got_error *error = NULL;
2519 struct got_repository *repo = NULL;
2520 struct got_worktree *worktree = NULL;
2521 char *cwd = NULL;
2522 struct got_pathlist_head paths;
2523 struct got_pathlist_entry *pe;
2524 int ch;
2526 TAILQ_INIT(&paths);
2528 while ((ch = getopt(argc, argv, "")) != -1) {
2529 switch (ch) {
2530 default:
2531 usage_status();
2532 /* NOTREACHED */
2536 argc -= optind;
2537 argv += optind;
2539 #ifndef PROFILE
2540 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2541 NULL) == -1)
2542 err(1, "pledge");
2543 #endif
2544 cwd = getcwd(NULL, 0);
2545 if (cwd == NULL) {
2546 error = got_error_from_errno("getcwd");
2547 goto done;
2550 error = got_worktree_open(&worktree, cwd);
2551 if (error != NULL)
2552 goto done;
2554 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2555 if (error != NULL)
2556 goto done;
2558 error = apply_unveil(got_repo_get_path(repo), 1,
2559 got_worktree_get_root_path(worktree));
2560 if (error)
2561 goto done;
2563 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2564 if (error)
2565 goto done;
2567 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2568 check_cancelled, NULL);
2569 done:
2570 TAILQ_FOREACH(pe, &paths, entry)
2571 free((char *)pe->path);
2572 got_pathlist_free(&paths);
2573 free(cwd);
2574 return error;
2577 __dead static void
2578 usage_ref(void)
2580 fprintf(stderr,
2581 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2582 getprogname());
2583 exit(1);
2586 static const struct got_error *
2587 list_refs(struct got_repository *repo)
2589 static const struct got_error *err = NULL;
2590 struct got_reflist_head refs;
2591 struct got_reflist_entry *re;
2593 SIMPLEQ_INIT(&refs);
2594 err = got_ref_list(&refs, repo);
2595 if (err)
2596 return err;
2598 SIMPLEQ_FOREACH(re, &refs, entry) {
2599 char *refstr;
2600 refstr = got_ref_to_str(re->ref);
2601 if (refstr == NULL)
2602 return got_error_from_errno("got_ref_to_str");
2603 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2604 free(refstr);
2607 got_ref_list_free(&refs);
2608 return NULL;
2611 static const struct got_error *
2612 delete_ref(struct got_repository *repo, const char *refname)
2614 const struct got_error *err = NULL;
2615 struct got_reference *ref;
2617 err = got_ref_open(&ref, repo, refname, 0);
2618 if (err)
2619 return err;
2621 err = got_ref_delete(ref, repo);
2622 got_ref_close(ref);
2623 return err;
2626 static const struct got_error *
2627 add_ref(struct got_repository *repo, const char *refname, const char *target)
2629 const struct got_error *err = NULL;
2630 struct got_object_id *id;
2631 struct got_reference *ref = NULL;
2634 * Don't let the user create a reference named '-'.
2635 * While technically a valid reference name, this case is usually
2636 * an unintended typo.
2638 if (refname[0] == '-' && refname[1] == '\0')
2639 return got_error(GOT_ERR_BAD_REF_NAME);
2641 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2642 repo);
2643 if (err) {
2644 struct got_reference *target_ref;
2646 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2647 return err;
2648 err = got_ref_open(&target_ref, repo, target, 0);
2649 if (err)
2650 return err;
2651 err = got_ref_resolve(&id, repo, target_ref);
2652 got_ref_close(target_ref);
2653 if (err)
2654 return err;
2657 err = got_ref_alloc(&ref, refname, id);
2658 if (err)
2659 goto done;
2661 err = got_ref_write(ref, repo);
2662 done:
2663 if (ref)
2664 got_ref_close(ref);
2665 free(id);
2666 return err;
2669 static const struct got_error *
2670 add_symref(struct got_repository *repo, const char *refname, const char *target)
2672 const struct got_error *err = NULL;
2673 struct got_reference *ref = NULL;
2674 struct got_reference *target_ref = NULL;
2677 * Don't let the user create a reference named '-'.
2678 * While technically a valid reference name, this case is usually
2679 * an unintended typo.
2681 if (refname[0] == '-' && refname[1] == '\0')
2682 return got_error(GOT_ERR_BAD_REF_NAME);
2684 err = got_ref_open(&target_ref, repo, target, 0);
2685 if (err)
2686 return err;
2688 err = got_ref_alloc_symref(&ref, refname, target_ref);
2689 if (err)
2690 goto done;
2692 err = got_ref_write(ref, repo);
2693 done:
2694 if (target_ref)
2695 got_ref_close(target_ref);
2696 if (ref)
2697 got_ref_close(ref);
2698 return err;
2701 static const struct got_error *
2702 cmd_ref(int argc, char *argv[])
2704 const struct got_error *error = NULL;
2705 struct got_repository *repo = NULL;
2706 struct got_worktree *worktree = NULL;
2707 char *cwd = NULL, *repo_path = NULL;
2708 int ch, do_list = 0, create_symref = 0;
2709 const char *delref = NULL;
2711 /* TODO: Add -s option for adding symbolic references. */
2712 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2713 switch (ch) {
2714 case 'd':
2715 delref = optarg;
2716 break;
2717 case 'r':
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2722 break;
2723 case 'l':
2724 do_list = 1;
2725 break;
2726 case 's':
2727 create_symref = 1;
2728 break;
2729 default:
2730 usage_ref();
2731 /* NOTREACHED */
2735 if (do_list && delref)
2736 errx(1, "-l and -d options are mutually exclusive\n");
2738 argc -= optind;
2739 argv += optind;
2741 if (do_list || delref) {
2742 if (create_symref)
2743 errx(1, "-s option cannot be used together with the "
2744 "-l or -d options");
2745 if (argc > 0)
2746 usage_ref();
2747 } else if (argc != 2)
2748 usage_ref();
2750 #ifndef PROFILE
2751 if (do_list) {
2752 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2753 NULL) == -1)
2754 err(1, "pledge");
2755 } else {
2756 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2757 "sendfd unveil", NULL) == -1)
2758 err(1, "pledge");
2760 #endif
2761 cwd = getcwd(NULL, 0);
2762 if (cwd == NULL) {
2763 error = got_error_from_errno("getcwd");
2764 goto done;
2767 if (repo_path == NULL) {
2768 error = got_worktree_open(&worktree, cwd);
2769 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2770 goto done;
2771 else
2772 error = NULL;
2773 if (worktree) {
2774 repo_path =
2775 strdup(got_worktree_get_repo_path(worktree));
2776 if (repo_path == NULL)
2777 error = got_error_from_errno("strdup");
2778 if (error)
2779 goto done;
2780 } else {
2781 repo_path = strdup(cwd);
2782 if (repo_path == NULL) {
2783 error = got_error_from_errno("strdup");
2784 goto done;
2789 error = got_repo_open(&repo, repo_path);
2790 if (error != NULL)
2791 goto done;
2793 error = apply_unveil(got_repo_get_path(repo), do_list,
2794 worktree ? got_worktree_get_root_path(worktree) : NULL);
2795 if (error)
2796 goto done;
2798 if (do_list)
2799 error = list_refs(repo);
2800 else if (delref)
2801 error = delete_ref(repo, delref);
2802 else if (create_symref)
2803 error = add_symref(repo, argv[0], argv[1]);
2804 else
2805 error = add_ref(repo, argv[0], argv[1]);
2806 done:
2807 if (repo)
2808 got_repo_close(repo);
2809 if (worktree)
2810 got_worktree_close(worktree);
2811 free(cwd);
2812 free(repo_path);
2813 return error;
2816 __dead static void
2817 usage_branch(void)
2819 fprintf(stderr,
2820 "usage: %s branch [-r repository] -l | -d name | "
2821 "name [base-branch]\n", getprogname());
2822 exit(1);
2825 static const struct got_error *
2826 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2828 static const struct got_error *err = NULL;
2829 struct got_reflist_head refs;
2830 struct got_reflist_entry *re;
2832 SIMPLEQ_INIT(&refs);
2834 err = got_ref_list(&refs, repo);
2835 if (err)
2836 return err;
2838 SIMPLEQ_FOREACH(re, &refs, entry) {
2839 const char *refname, *marker = " ";
2840 char *refstr;
2841 refname = got_ref_get_name(re->ref);
2842 if (strncmp(refname, "refs/heads/", 11) != 0)
2843 continue;
2844 if (worktree && strcmp(refname,
2845 got_worktree_get_head_ref_name(worktree)) == 0) {
2846 struct got_object_id *id = NULL;
2847 err = got_ref_resolve(&id, repo, re->ref);
2848 if (err)
2849 return err;
2850 if (got_object_id_cmp(id,
2851 got_worktree_get_base_commit_id(worktree)) == 0)
2852 marker = "* ";
2853 else
2854 marker = "~ ";
2855 free(id);
2857 refname += 11;
2858 refstr = got_ref_to_str(re->ref);
2859 if (refstr == NULL)
2860 return got_error_from_errno("got_ref_to_str");
2861 printf("%s%s: %s\n", marker, refname, refstr);
2862 free(refstr);
2865 got_ref_list_free(&refs);
2866 return NULL;
2869 static const struct got_error *
2870 delete_branch(struct got_repository *repo, const char *branch_name)
2872 const struct got_error *err = NULL;
2873 struct got_reference *ref;
2874 char *refname;
2876 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2877 return got_error_from_errno("asprintf");
2879 err = got_ref_open(&ref, repo, refname, 0);
2880 if (err)
2881 goto done;
2883 err = got_ref_delete(ref, repo);
2884 got_ref_close(ref);
2885 done:
2886 free(refname);
2887 return err;
2890 static const struct got_error *
2891 add_branch(struct got_repository *repo, const char *branch_name,
2892 const char *base_branch)
2894 const struct got_error *err = NULL;
2895 struct got_object_id *id = NULL;
2896 struct got_reference *ref = NULL;
2897 char *base_refname = NULL, *refname = NULL;
2898 struct got_reference *base_ref;
2901 * Don't let the user create a branch named '-'.
2902 * While technically a valid reference name, this case is usually
2903 * an unintended typo.
2905 if (branch_name[0] == '-' && branch_name[1] == '\0')
2906 return got_error(GOT_ERR_BAD_REF_NAME);
2908 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2909 base_refname = strdup(GOT_REF_HEAD);
2910 if (base_refname == NULL)
2911 return got_error_from_errno("strdup");
2912 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2913 return got_error_from_errno("asprintf");
2915 err = got_ref_open(&base_ref, repo, base_refname, 0);
2916 if (err)
2917 goto done;
2918 err = got_ref_resolve(&id, repo, base_ref);
2919 got_ref_close(base_ref);
2920 if (err)
2921 goto done;
2923 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2924 err = got_error_from_errno("asprintf");
2925 goto done;
2928 err = got_ref_open(&ref, repo, refname, 0);
2929 if (err == NULL) {
2930 err = got_error(GOT_ERR_BRANCH_EXISTS);
2931 goto done;
2932 } else if (err->code != GOT_ERR_NOT_REF)
2933 goto done;
2935 err = got_ref_alloc(&ref, refname, id);
2936 if (err)
2937 goto done;
2939 err = got_ref_write(ref, repo);
2940 done:
2941 if (ref)
2942 got_ref_close(ref);
2943 free(id);
2944 free(base_refname);
2945 free(refname);
2946 return err;
2949 static const struct got_error *
2950 cmd_branch(int argc, char *argv[])
2952 const struct got_error *error = NULL;
2953 struct got_repository *repo = NULL;
2954 struct got_worktree *worktree = NULL;
2955 char *cwd = NULL, *repo_path = NULL;
2956 int ch, do_list = 0;
2957 const char *delref = NULL;
2959 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2960 switch (ch) {
2961 case 'd':
2962 delref = optarg;
2963 break;
2964 case 'r':
2965 repo_path = realpath(optarg, NULL);
2966 if (repo_path == NULL)
2967 err(1, "-r option");
2968 got_path_strip_trailing_slashes(repo_path);
2969 break;
2970 case 'l':
2971 do_list = 1;
2972 break;
2973 default:
2974 usage_branch();
2975 /* NOTREACHED */
2979 if (do_list && delref)
2980 errx(1, "-l and -d options are mutually exclusive\n");
2982 argc -= optind;
2983 argv += optind;
2985 if (do_list || delref) {
2986 if (argc > 0)
2987 usage_branch();
2988 } else if (argc < 1 || argc > 2)
2989 usage_branch();
2991 #ifndef PROFILE
2992 if (do_list) {
2993 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2994 NULL) == -1)
2995 err(1, "pledge");
2996 } else {
2997 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2998 "sendfd unveil", NULL) == -1)
2999 err(1, "pledge");
3001 #endif
3002 cwd = getcwd(NULL, 0);
3003 if (cwd == NULL) {
3004 error = got_error_from_errno("getcwd");
3005 goto done;
3008 if (repo_path == NULL) {
3009 error = got_worktree_open(&worktree, cwd);
3010 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3011 goto done;
3012 else
3013 error = NULL;
3014 if (worktree) {
3015 repo_path =
3016 strdup(got_worktree_get_repo_path(worktree));
3017 if (repo_path == NULL)
3018 error = got_error_from_errno("strdup");
3019 if (error)
3020 goto done;
3021 } else {
3022 repo_path = strdup(cwd);
3023 if (repo_path == NULL) {
3024 error = got_error_from_errno("strdup");
3025 goto done;
3030 error = got_repo_open(&repo, repo_path);
3031 if (error != NULL)
3032 goto done;
3034 error = apply_unveil(got_repo_get_path(repo), do_list,
3035 worktree ? got_worktree_get_root_path(worktree) : NULL);
3036 if (error)
3037 goto done;
3039 if (do_list)
3040 error = list_branches(repo, worktree);
3041 else if (delref)
3042 error = delete_branch(repo, delref);
3043 else {
3044 const char *base_branch;
3045 if (argc == 1) {
3046 base_branch = worktree ?
3047 got_worktree_get_head_ref_name(worktree) :
3048 GOT_REF_HEAD;
3049 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3050 base_branch += 11;
3051 } else
3052 base_branch = argv[1];
3053 error = add_branch(repo, argv[0], base_branch);
3055 done:
3056 if (repo)
3057 got_repo_close(repo);
3058 if (worktree)
3059 got_worktree_close(worktree);
3060 free(cwd);
3061 free(repo_path);
3062 return error;
3065 __dead static void
3066 usage_add(void)
3068 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3069 exit(1);
3072 static const struct got_error *
3073 cmd_add(int argc, char *argv[])
3075 const struct got_error *error = NULL;
3076 struct got_repository *repo = NULL;
3077 struct got_worktree *worktree = NULL;
3078 char *cwd = NULL;
3079 struct got_pathlist_head paths;
3080 struct got_pathlist_entry *pe;
3081 int ch;
3083 TAILQ_INIT(&paths);
3085 while ((ch = getopt(argc, argv, "")) != -1) {
3086 switch (ch) {
3087 default:
3088 usage_add();
3089 /* NOTREACHED */
3093 argc -= optind;
3094 argv += optind;
3096 #ifndef PROFILE
3097 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3098 NULL) == -1)
3099 err(1, "pledge");
3100 #endif
3101 if (argc < 1)
3102 usage_add();
3104 cwd = getcwd(NULL, 0);
3105 if (cwd == NULL) {
3106 error = got_error_from_errno("getcwd");
3107 goto done;
3110 error = got_worktree_open(&worktree, cwd);
3111 if (error)
3112 goto done;
3114 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3115 if (error != NULL)
3116 goto done;
3118 error = apply_unveil(got_repo_get_path(repo), 1,
3119 got_worktree_get_root_path(worktree));
3120 if (error)
3121 goto done;
3123 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3124 if (error)
3125 goto done;
3127 error = got_worktree_schedule_add(worktree, &paths, print_status,
3128 NULL, repo);
3129 done:
3130 if (repo)
3131 got_repo_close(repo);
3132 if (worktree)
3133 got_worktree_close(worktree);
3134 TAILQ_FOREACH(pe, &paths, entry)
3135 free((char *)pe->path);
3136 got_pathlist_free(&paths);
3137 free(cwd);
3138 return error;
3141 __dead static void
3142 usage_remove(void)
3144 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3145 exit(1);
3148 static const struct got_error *
3149 cmd_remove(int argc, char *argv[])
3151 const struct got_error *error = NULL;
3152 struct got_worktree *worktree = NULL;
3153 struct got_repository *repo = NULL;
3154 char *cwd = NULL;
3155 struct got_pathlist_head paths;
3156 struct got_pathlist_entry *pe;
3157 int ch, delete_local_mods = 0;
3159 TAILQ_INIT(&paths);
3161 while ((ch = getopt(argc, argv, "f")) != -1) {
3162 switch (ch) {
3163 case 'f':
3164 delete_local_mods = 1;
3165 break;
3166 default:
3167 usage_add();
3168 /* NOTREACHED */
3172 argc -= optind;
3173 argv += optind;
3175 #ifndef PROFILE
3176 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3177 NULL) == -1)
3178 err(1, "pledge");
3179 #endif
3180 if (argc < 1)
3181 usage_remove();
3183 cwd = getcwd(NULL, 0);
3184 if (cwd == NULL) {
3185 error = got_error_from_errno("getcwd");
3186 goto done;
3188 error = got_worktree_open(&worktree, cwd);
3189 if (error)
3190 goto done;
3192 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3193 if (error)
3194 goto done;
3196 error = apply_unveil(got_repo_get_path(repo), 1,
3197 got_worktree_get_root_path(worktree));
3198 if (error)
3199 goto done;
3201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3202 if (error)
3203 goto done;
3205 error = got_worktree_schedule_delete(worktree, &paths,
3206 delete_local_mods, print_status, NULL, repo);
3207 if (error)
3208 goto done;
3209 done:
3210 if (repo)
3211 got_repo_close(repo);
3212 if (worktree)
3213 got_worktree_close(worktree);
3214 TAILQ_FOREACH(pe, &paths, entry)
3215 free((char *)pe->path);
3216 got_pathlist_free(&paths);
3217 free(cwd);
3218 return error;
3221 __dead static void
3222 usage_revert(void)
3224 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3225 "path ...\n", getprogname());
3226 exit(1);
3229 static const struct got_error *
3230 revert_progress(void *arg, unsigned char status, const char *path)
3232 while (path[0] == '/')
3233 path++;
3234 printf("%c %s\n", status, path);
3235 return NULL;
3238 struct choose_patch_arg {
3239 FILE *patch_script_file;
3240 const char *action;
3243 static const struct got_error *
3244 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3245 int nchanges, const char *action)
3247 char *line = NULL;
3248 size_t linesize = 0;
3249 ssize_t linelen;
3251 switch (status) {
3252 case GOT_STATUS_ADD:
3253 printf("A %s\n%s this addition? [y/n] ", path, action);
3254 break;
3255 case GOT_STATUS_DELETE:
3256 printf("D %s\n%s this deletion? [y/n] ", path, action);
3257 break;
3258 case GOT_STATUS_MODIFY:
3259 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3260 return got_error_from_errno("fseek");
3261 printf(GOT_COMMIT_SEP_STR);
3262 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3263 printf("%s", line);
3264 if (ferror(patch_file))
3265 return got_error_from_errno("getline");
3266 printf(GOT_COMMIT_SEP_STR);
3267 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3268 path, n, nchanges, action);
3269 break;
3270 default:
3271 return got_error_path(path, GOT_ERR_FILE_STATUS);
3274 return NULL;
3277 static const struct got_error *
3278 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3279 FILE *patch_file, int n, int nchanges)
3281 const struct got_error *err = NULL;
3282 char *line = NULL;
3283 size_t linesize = 0;
3284 ssize_t linelen;
3285 int resp = ' ';
3286 struct choose_patch_arg *a = arg;
3288 *choice = GOT_PATCH_CHOICE_NONE;
3290 if (a->patch_script_file) {
3291 char *nl;
3292 err = show_change(status, path, patch_file, n, nchanges,
3293 a->action);
3294 if (err)
3295 return err;
3296 linelen = getline(&line, &linesize, a->patch_script_file);
3297 if (linelen == -1) {
3298 if (ferror(a->patch_script_file))
3299 return got_error_from_errno("getline");
3300 return NULL;
3302 nl = strchr(line, '\n');
3303 if (nl)
3304 *nl = '\0';
3305 if (strcmp(line, "y") == 0) {
3306 *choice = GOT_PATCH_CHOICE_YES;
3307 printf("y\n");
3308 } else if (strcmp(line, "n") == 0) {
3309 *choice = GOT_PATCH_CHOICE_NO;
3310 printf("n\n");
3311 } else if (strcmp(line, "q") == 0 &&
3312 status == GOT_STATUS_MODIFY) {
3313 *choice = GOT_PATCH_CHOICE_QUIT;
3314 printf("q\n");
3315 } else
3316 printf("invalid response '%s'\n", line);
3317 free(line);
3318 return NULL;
3321 while (resp != 'y' && resp != 'n' && resp != 'q') {
3322 err = show_change(status, path, patch_file, n, nchanges,
3323 a->action);
3324 if (err)
3325 return err;
3326 resp = getchar();
3327 if (resp == '\n')
3328 resp = getchar();
3329 if (status == GOT_STATUS_MODIFY) {
3330 if (resp != 'y' && resp != 'n' && resp != 'q') {
3331 printf("invalid response '%c'\n", resp);
3332 resp = ' ';
3334 } else if (resp != 'y' && resp != 'n') {
3335 printf("invalid response '%c'\n", resp);
3336 resp = ' ';
3340 if (resp == 'y')
3341 *choice = GOT_PATCH_CHOICE_YES;
3342 else if (resp == 'n')
3343 *choice = GOT_PATCH_CHOICE_NO;
3344 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3345 *choice = GOT_PATCH_CHOICE_QUIT;
3347 return NULL;
3351 static const struct got_error *
3352 cmd_revert(int argc, char *argv[])
3354 const struct got_error *error = NULL;
3355 struct got_worktree *worktree = NULL;
3356 struct got_repository *repo = NULL;
3357 char *cwd = NULL, *path = NULL;
3358 struct got_pathlist_head paths;
3359 struct got_pathlist_entry *pe;
3360 int ch, can_recurse = 0, pflag = 0;
3361 FILE *patch_script_file = NULL;
3362 const char *patch_script_path = NULL;
3363 struct choose_patch_arg cpa;
3365 TAILQ_INIT(&paths);
3367 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3368 switch (ch) {
3369 case 'p':
3370 pflag = 1;
3371 break;
3372 case 'F':
3373 patch_script_path = optarg;
3374 break;
3375 case 'R':
3376 can_recurse = 1;
3377 break;
3378 default:
3379 usage_revert();
3380 /* NOTREACHED */
3384 argc -= optind;
3385 argv += optind;
3387 #ifndef PROFILE
3388 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3389 "unveil", NULL) == -1)
3390 err(1, "pledge");
3391 #endif
3392 if (argc < 1)
3393 usage_revert();
3394 if (patch_script_path && !pflag)
3395 errx(1, "-F option can only be used together with -p option");
3397 cwd = getcwd(NULL, 0);
3398 if (cwd == NULL) {
3399 error = got_error_from_errno("getcwd");
3400 goto done;
3402 error = got_worktree_open(&worktree, cwd);
3403 if (error)
3404 goto done;
3406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3407 if (error != NULL)
3408 goto done;
3410 if (patch_script_path) {
3411 patch_script_file = fopen(patch_script_path, "r");
3412 if (patch_script_file == NULL) {
3413 error = got_error_from_errno2("fopen",
3414 patch_script_path);
3415 goto done;
3418 error = apply_unveil(got_repo_get_path(repo), 1,
3419 got_worktree_get_root_path(worktree));
3420 if (error)
3421 goto done;
3423 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3424 if (error)
3425 goto done;
3427 if (!can_recurse) {
3428 char *ondisk_path;
3429 struct stat sb;
3430 TAILQ_FOREACH(pe, &paths, entry) {
3431 if (asprintf(&ondisk_path, "%s/%s",
3432 got_worktree_get_root_path(worktree),
3433 pe->path) == -1) {
3434 error = got_error_from_errno("asprintf");
3435 goto done;
3437 if (lstat(ondisk_path, &sb) == -1) {
3438 if (errno == ENOENT) {
3439 free(ondisk_path);
3440 continue;
3442 error = got_error_from_errno2("lstat",
3443 ondisk_path);
3444 free(ondisk_path);
3445 goto done;
3447 free(ondisk_path);
3448 if (S_ISDIR(sb.st_mode)) {
3449 error = got_error_msg(GOT_ERR_BAD_PATH,
3450 "reverting directories requires -R option");
3451 goto done;
3456 cpa.patch_script_file = patch_script_file;
3457 cpa.action = "revert";
3458 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3459 pflag ? choose_patch : NULL, &cpa, repo);
3460 if (error)
3461 goto done;
3462 done:
3463 if (patch_script_file && fclose(patch_script_file) == EOF &&
3464 error == NULL)
3465 error = got_error_from_errno2("fclose", patch_script_path);
3466 if (repo)
3467 got_repo_close(repo);
3468 if (worktree)
3469 got_worktree_close(worktree);
3470 free(path);
3471 free(cwd);
3472 return error;
3475 __dead static void
3476 usage_commit(void)
3478 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3479 getprogname());
3480 exit(1);
3483 struct collect_commit_logmsg_arg {
3484 const char *cmdline_log;
3485 const char *editor;
3486 const char *worktree_path;
3487 const char *branch_name;
3488 const char *repo_path;
3489 char *logmsg_path;
3493 static const struct got_error *
3494 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3495 void *arg)
3497 char *initial_content = NULL;
3498 struct got_pathlist_entry *pe;
3499 const struct got_error *err = NULL;
3500 char *template = NULL;
3501 struct collect_commit_logmsg_arg *a = arg;
3502 int fd;
3503 size_t len;
3505 /* if a message was specified on the command line, just use it */
3506 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3507 len = strlen(a->cmdline_log) + 1;
3508 *logmsg = malloc(len + 1);
3509 if (*logmsg == NULL)
3510 return got_error_from_errno("malloc");
3511 strlcpy(*logmsg, a->cmdline_log, len);
3512 return NULL;
3515 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3516 return got_error_from_errno("asprintf");
3518 if (asprintf(&initial_content,
3519 "\n# changes to be committed on branch %s:\n",
3520 a->branch_name) == -1)
3521 return got_error_from_errno("asprintf");
3523 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3524 if (err)
3525 goto done;
3527 dprintf(fd, initial_content);
3529 TAILQ_FOREACH(pe, commitable_paths, entry) {
3530 struct got_commitable *ct = pe->data;
3531 dprintf(fd, "# %c %s\n",
3532 got_commitable_get_status(ct),
3533 got_commitable_get_path(ct));
3535 close(fd);
3537 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3538 done:
3539 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3540 unlink(a->logmsg_path);
3541 free(a->logmsg_path);
3542 a->logmsg_path = NULL;
3544 free(initial_content);
3545 free(template);
3547 /* Editor is done; we can now apply unveil(2) */
3548 if (err == NULL) {
3549 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3550 if (err) {
3551 free(*logmsg);
3552 *logmsg = NULL;
3555 return err;
3558 static const struct got_error *
3559 cmd_commit(int argc, char *argv[])
3561 const struct got_error *error = NULL;
3562 struct got_worktree *worktree = NULL;
3563 struct got_repository *repo = NULL;
3564 char *cwd = NULL, *id_str = NULL;
3565 struct got_object_id *id = NULL;
3566 const char *logmsg = NULL;
3567 const char *author;
3568 struct collect_commit_logmsg_arg cl_arg;
3569 char *editor = NULL;
3570 int ch, rebase_in_progress, histedit_in_progress;
3571 struct got_pathlist_head paths;
3573 TAILQ_INIT(&paths);
3574 cl_arg.logmsg_path = NULL;
3576 while ((ch = getopt(argc, argv, "m:")) != -1) {
3577 switch (ch) {
3578 case 'm':
3579 logmsg = optarg;
3580 break;
3581 default:
3582 usage_commit();
3583 /* NOTREACHED */
3587 argc -= optind;
3588 argv += optind;
3590 #ifndef PROFILE
3591 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3592 "unveil", NULL) == -1)
3593 err(1, "pledge");
3594 #endif
3595 error = get_author(&author);
3596 if (error)
3597 return error;
3599 cwd = getcwd(NULL, 0);
3600 if (cwd == NULL) {
3601 error = got_error_from_errno("getcwd");
3602 goto done;
3604 error = got_worktree_open(&worktree, cwd);
3605 if (error)
3606 goto done;
3608 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3609 if (error)
3610 goto done;
3611 if (rebase_in_progress) {
3612 error = got_error(GOT_ERR_REBASING);
3613 goto done;
3616 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3617 worktree);
3618 if (error)
3619 goto done;
3621 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3622 if (error != NULL)
3623 goto done;
3626 * unveil(2) traverses exec(2); if an editor is used we have
3627 * to apply unveil after the log message has been written.
3629 if (logmsg == NULL || strlen(logmsg) == 0)
3630 error = get_editor(&editor);
3631 else
3632 error = apply_unveil(got_repo_get_path(repo), 0,
3633 got_worktree_get_root_path(worktree));
3634 if (error)
3635 goto done;
3637 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3638 if (error)
3639 goto done;
3641 cl_arg.editor = editor;
3642 cl_arg.cmdline_log = logmsg;
3643 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3644 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3645 if (!histedit_in_progress) {
3646 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3647 error = got_error(GOT_ERR_COMMIT_BRANCH);
3648 goto done;
3650 cl_arg.branch_name += 11;
3652 cl_arg.repo_path = got_repo_get_path(repo);
3653 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3654 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3655 if (error) {
3656 if (cl_arg.logmsg_path)
3657 fprintf(stderr, "%s: log message preserved in %s\n",
3658 getprogname(), cl_arg.logmsg_path);
3659 goto done;
3662 if (cl_arg.logmsg_path)
3663 unlink(cl_arg.logmsg_path);
3665 error = got_object_id_str(&id_str, id);
3666 if (error)
3667 goto done;
3668 printf("Created commit %s\n", id_str);
3669 done:
3670 free(cl_arg.logmsg_path);
3671 if (repo)
3672 got_repo_close(repo);
3673 if (worktree)
3674 got_worktree_close(worktree);
3675 free(cwd);
3676 free(id_str);
3677 free(editor);
3678 return error;
3681 __dead static void
3682 usage_cherrypick(void)
3684 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3685 exit(1);
3688 static const struct got_error *
3689 cmd_cherrypick(int argc, char *argv[])
3691 const struct got_error *error = NULL;
3692 struct got_worktree *worktree = NULL;
3693 struct got_repository *repo = NULL;
3694 char *cwd = NULL, *commit_id_str = NULL;
3695 struct got_object_id *commit_id = NULL;
3696 struct got_commit_object *commit = NULL;
3697 struct got_object_qid *pid;
3698 struct got_reference *head_ref = NULL;
3699 int ch, did_something = 0;
3701 while ((ch = getopt(argc, argv, "")) != -1) {
3702 switch (ch) {
3703 default:
3704 usage_cherrypick();
3705 /* NOTREACHED */
3709 argc -= optind;
3710 argv += optind;
3712 #ifndef PROFILE
3713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3714 "unveil", NULL) == -1)
3715 err(1, "pledge");
3716 #endif
3717 if (argc != 1)
3718 usage_cherrypick();
3720 cwd = getcwd(NULL, 0);
3721 if (cwd == NULL) {
3722 error = got_error_from_errno("getcwd");
3723 goto done;
3725 error = got_worktree_open(&worktree, cwd);
3726 if (error)
3727 goto done;
3729 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3730 if (error != NULL)
3731 goto done;
3733 error = apply_unveil(got_repo_get_path(repo), 0,
3734 got_worktree_get_root_path(worktree));
3735 if (error)
3736 goto done;
3738 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3739 GOT_OBJ_TYPE_COMMIT, repo);
3740 if (error != NULL) {
3741 struct got_reference *ref;
3742 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3743 goto done;
3744 error = got_ref_open(&ref, repo, argv[0], 0);
3745 if (error != NULL)
3746 goto done;
3747 error = got_ref_resolve(&commit_id, repo, ref);
3748 got_ref_close(ref);
3749 if (error != NULL)
3750 goto done;
3752 error = got_object_id_str(&commit_id_str, commit_id);
3753 if (error)
3754 goto done;
3756 error = got_ref_open(&head_ref, repo,
3757 got_worktree_get_head_ref_name(worktree), 0);
3758 if (error != NULL)
3759 goto done;
3761 error = check_same_branch(commit_id, head_ref, NULL, repo);
3762 if (error) {
3763 if (error->code != GOT_ERR_ANCESTRY)
3764 goto done;
3765 error = NULL;
3766 } else {
3767 error = got_error(GOT_ERR_SAME_BRANCH);
3768 goto done;
3771 error = got_object_open_as_commit(&commit, repo, commit_id);
3772 if (error)
3773 goto done;
3774 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3775 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3776 commit_id, repo, update_progress, &did_something, check_cancelled,
3777 NULL);
3778 if (error != NULL)
3779 goto done;
3781 if (did_something)
3782 printf("Merged commit %s\n", commit_id_str);
3783 done:
3784 if (commit)
3785 got_object_commit_close(commit);
3786 free(commit_id_str);
3787 if (head_ref)
3788 got_ref_close(head_ref);
3789 if (worktree)
3790 got_worktree_close(worktree);
3791 if (repo)
3792 got_repo_close(repo);
3793 return error;
3796 __dead static void
3797 usage_backout(void)
3799 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3800 exit(1);
3803 static const struct got_error *
3804 cmd_backout(int argc, char *argv[])
3806 const struct got_error *error = NULL;
3807 struct got_worktree *worktree = NULL;
3808 struct got_repository *repo = NULL;
3809 char *cwd = NULL, *commit_id_str = NULL;
3810 struct got_object_id *commit_id = NULL;
3811 struct got_commit_object *commit = NULL;
3812 struct got_object_qid *pid;
3813 struct got_reference *head_ref = NULL;
3814 int ch, did_something = 0;
3816 while ((ch = getopt(argc, argv, "")) != -1) {
3817 switch (ch) {
3818 default:
3819 usage_backout();
3820 /* NOTREACHED */
3824 argc -= optind;
3825 argv += optind;
3827 #ifndef PROFILE
3828 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3829 "unveil", NULL) == -1)
3830 err(1, "pledge");
3831 #endif
3832 if (argc != 1)
3833 usage_backout();
3835 cwd = getcwd(NULL, 0);
3836 if (cwd == NULL) {
3837 error = got_error_from_errno("getcwd");
3838 goto done;
3840 error = got_worktree_open(&worktree, cwd);
3841 if (error)
3842 goto done;
3844 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3845 if (error != NULL)
3846 goto done;
3848 error = apply_unveil(got_repo_get_path(repo), 0,
3849 got_worktree_get_root_path(worktree));
3850 if (error)
3851 goto done;
3853 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3854 GOT_OBJ_TYPE_COMMIT, repo);
3855 if (error != NULL) {
3856 struct got_reference *ref;
3857 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3858 goto done;
3859 error = got_ref_open(&ref, repo, argv[0], 0);
3860 if (error != NULL)
3861 goto done;
3862 error = got_ref_resolve(&commit_id, repo, ref);
3863 got_ref_close(ref);
3864 if (error != NULL)
3865 goto done;
3867 error = got_object_id_str(&commit_id_str, commit_id);
3868 if (error)
3869 goto done;
3871 error = got_ref_open(&head_ref, repo,
3872 got_worktree_get_head_ref_name(worktree), 0);
3873 if (error != NULL)
3874 goto done;
3876 error = check_same_branch(commit_id, head_ref, NULL, repo);
3877 if (error)
3878 goto done;
3880 error = got_object_open_as_commit(&commit, repo, commit_id);
3881 if (error)
3882 goto done;
3883 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3884 if (pid == NULL) {
3885 error = got_error(GOT_ERR_ROOT_COMMIT);
3886 goto done;
3889 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3890 update_progress, &did_something, check_cancelled, NULL);
3891 if (error != NULL)
3892 goto done;
3894 if (did_something)
3895 printf("Backed out commit %s\n", commit_id_str);
3896 done:
3897 if (commit)
3898 got_object_commit_close(commit);
3899 free(commit_id_str);
3900 if (head_ref)
3901 got_ref_close(head_ref);
3902 if (worktree)
3903 got_worktree_close(worktree);
3904 if (repo)
3905 got_repo_close(repo);
3906 return error;
3909 __dead static void
3910 usage_rebase(void)
3912 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3913 getprogname());
3914 exit(1);
3917 void
3918 trim_logmsg(char *logmsg, int limit)
3920 char *nl;
3921 size_t len;
3923 len = strlen(logmsg);
3924 if (len > limit)
3925 len = limit;
3926 logmsg[len] = '\0';
3927 nl = strchr(logmsg, '\n');
3928 if (nl)
3929 *nl = '\0';
3932 static const struct got_error *
3933 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3935 const char *logmsg0 = NULL;
3937 logmsg0 = got_object_commit_get_logmsg(commit);
3939 while (isspace((unsigned char)logmsg0[0]))
3940 logmsg0++;
3942 *logmsg = strdup(logmsg0);
3943 if (*logmsg == NULL)
3944 return got_error_from_errno("strdup");
3946 trim_logmsg(*logmsg, limit);
3947 return NULL;
3950 static const struct got_error *
3951 show_rebase_progress(struct got_commit_object *commit,
3952 struct got_object_id *old_id, struct got_object_id *new_id)
3954 const struct got_error *err;
3955 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3957 err = got_object_id_str(&old_id_str, old_id);
3958 if (err)
3959 goto done;
3961 if (new_id) {
3962 err = got_object_id_str(&new_id_str, new_id);
3963 if (err)
3964 goto done;
3967 old_id_str[12] = '\0';
3968 if (new_id_str)
3969 new_id_str[12] = '\0';
3971 err = get_short_logmsg(&logmsg, 42, commit);
3972 if (err)
3973 goto done;
3975 printf("%s -> %s: %s\n", old_id_str,
3976 new_id_str ? new_id_str : "no-op change", logmsg);
3977 done:
3978 free(old_id_str);
3979 free(new_id_str);
3980 return err;
3983 static const struct got_error *
3984 rebase_progress(void *arg, unsigned char status, const char *path)
3986 unsigned char *rebase_status = arg;
3988 while (path[0] == '/')
3989 path++;
3990 printf("%c %s\n", status, path);
3992 if (*rebase_status == GOT_STATUS_CONFLICT)
3993 return NULL;
3994 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3995 *rebase_status = status;
3996 return NULL;
3999 static const struct got_error *
4000 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4001 struct got_reference *branch, struct got_reference *new_base_branch,
4002 struct got_reference *tmp_branch, struct got_repository *repo)
4004 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4005 return got_worktree_rebase_complete(worktree, fileindex,
4006 new_base_branch, tmp_branch, branch, repo);
4009 static const struct got_error *
4010 rebase_commit(struct got_pathlist_head *merged_paths,
4011 struct got_worktree *worktree, struct got_fileindex *fileindex,
4012 struct got_reference *tmp_branch,
4013 struct got_object_id *commit_id, struct got_repository *repo)
4015 const struct got_error *error;
4016 struct got_commit_object *commit;
4017 struct got_object_id *new_commit_id;
4019 error = got_object_open_as_commit(&commit, repo, commit_id);
4020 if (error)
4021 return error;
4023 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4024 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4025 if (error) {
4026 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4027 goto done;
4028 error = show_rebase_progress(commit, commit_id, NULL);
4029 } else {
4030 error = show_rebase_progress(commit, commit_id, new_commit_id);
4031 free(new_commit_id);
4033 done:
4034 got_object_commit_close(commit);
4035 return error;
4038 struct check_path_prefix_arg {
4039 const char *path_prefix;
4040 size_t len;
4041 int errcode;
4044 static const struct got_error *
4045 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4046 struct got_blob_object *blob2, struct got_object_id *id1,
4047 struct got_object_id *id2, const char *path1, const char *path2,
4048 struct got_repository *repo)
4050 struct check_path_prefix_arg *a = arg;
4052 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4053 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4054 return got_error(a->errcode);
4056 return NULL;
4059 static const struct got_error *
4060 check_path_prefix(struct got_object_id *parent_id,
4061 struct got_object_id *commit_id, const char *path_prefix,
4062 int errcode, struct got_repository *repo)
4064 const struct got_error *err;
4065 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4066 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4067 struct check_path_prefix_arg cpp_arg;
4069 if (got_path_is_root_dir(path_prefix))
4070 return NULL;
4072 err = got_object_open_as_commit(&commit, repo, commit_id);
4073 if (err)
4074 goto done;
4076 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4077 if (err)
4078 goto done;
4080 err = got_object_open_as_tree(&tree1, repo,
4081 got_object_commit_get_tree_id(parent_commit));
4082 if (err)
4083 goto done;
4085 err = got_object_open_as_tree(&tree2, repo,
4086 got_object_commit_get_tree_id(commit));
4087 if (err)
4088 goto done;
4090 cpp_arg.path_prefix = path_prefix;
4091 while (cpp_arg.path_prefix[0] == '/')
4092 cpp_arg.path_prefix++;
4093 cpp_arg.len = strlen(cpp_arg.path_prefix);
4094 cpp_arg.errcode = errcode;
4095 err = got_diff_tree(tree1, tree2, "", "", repo,
4096 check_path_prefix_in_diff, &cpp_arg, 0);
4097 done:
4098 if (tree1)
4099 got_object_tree_close(tree1);
4100 if (tree2)
4101 got_object_tree_close(tree2);
4102 if (commit)
4103 got_object_commit_close(commit);
4104 if (parent_commit)
4105 got_object_commit_close(parent_commit);
4106 return err;
4109 static const struct got_error *
4110 collect_commits(struct got_object_id_queue *commits,
4111 struct got_object_id *initial_commit_id,
4112 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4113 const char *path_prefix, int path_prefix_errcode,
4114 struct got_repository *repo)
4116 const struct got_error *err = NULL;
4117 struct got_commit_graph *graph = NULL;
4118 struct got_object_id *parent_id = NULL;
4119 struct got_object_qid *qid;
4120 struct got_object_id *commit_id = initial_commit_id;
4122 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4123 if (err)
4124 return err;
4126 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4127 if (err)
4128 goto done;
4129 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4130 err = got_commit_graph_iter_next(&parent_id, graph);
4131 if (err) {
4132 if (err->code == GOT_ERR_ITER_COMPLETED) {
4133 err = got_error_msg(GOT_ERR_ANCESTRY,
4134 "ran out of commits to rebase before "
4135 "youngest common ancestor commit has "
4136 "been reached?!?");
4137 goto done;
4138 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4139 goto done;
4140 err = got_commit_graph_fetch_commits(graph, 1, repo);
4141 if (err)
4142 goto done;
4143 } else {
4144 err = check_path_prefix(parent_id, commit_id,
4145 path_prefix, path_prefix_errcode, repo);
4146 if (err)
4147 goto done;
4149 err = got_object_qid_alloc(&qid, commit_id);
4150 if (err)
4151 goto done;
4152 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4153 commit_id = parent_id;
4156 done:
4157 got_commit_graph_close(graph);
4158 return err;
4161 static const struct got_error *
4162 cmd_rebase(int argc, char *argv[])
4164 const struct got_error *error = NULL;
4165 struct got_worktree *worktree = NULL;
4166 struct got_repository *repo = NULL;
4167 struct got_fileindex *fileindex = NULL;
4168 char *cwd = NULL;
4169 struct got_reference *branch = NULL;
4170 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4171 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4172 struct got_object_id *resume_commit_id = NULL;
4173 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4174 struct got_commit_object *commit = NULL;
4175 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4176 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4177 struct got_object_id_queue commits;
4178 struct got_pathlist_head merged_paths;
4179 const struct got_object_id_queue *parent_ids;
4180 struct got_object_qid *qid, *pid;
4182 SIMPLEQ_INIT(&commits);
4183 TAILQ_INIT(&merged_paths);
4185 while ((ch = getopt(argc, argv, "ac")) != -1) {
4186 switch (ch) {
4187 case 'a':
4188 abort_rebase = 1;
4189 break;
4190 case 'c':
4191 continue_rebase = 1;
4192 break;
4193 default:
4194 usage_rebase();
4195 /* NOTREACHED */
4199 argc -= optind;
4200 argv += optind;
4202 #ifndef PROFILE
4203 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4204 "unveil", NULL) == -1)
4205 err(1, "pledge");
4206 #endif
4207 if (abort_rebase && continue_rebase)
4208 usage_rebase();
4209 else if (abort_rebase || continue_rebase) {
4210 if (argc != 0)
4211 usage_rebase();
4212 } else if (argc != 1)
4213 usage_rebase();
4215 cwd = getcwd(NULL, 0);
4216 if (cwd == NULL) {
4217 error = got_error_from_errno("getcwd");
4218 goto done;
4220 error = got_worktree_open(&worktree, cwd);
4221 if (error)
4222 goto done;
4224 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4225 if (error != NULL)
4226 goto done;
4228 error = apply_unveil(got_repo_get_path(repo), 0,
4229 got_worktree_get_root_path(worktree));
4230 if (error)
4231 goto done;
4233 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4234 if (error)
4235 goto done;
4237 if (abort_rebase) {
4238 int did_something;
4239 if (!rebase_in_progress) {
4240 error = got_error(GOT_ERR_NOT_REBASING);
4241 goto done;
4243 error = got_worktree_rebase_continue(&resume_commit_id,
4244 &new_base_branch, &tmp_branch, &branch, &fileindex,
4245 worktree, repo);
4246 if (error)
4247 goto done;
4248 printf("Switching work tree to %s\n",
4249 got_ref_get_symref_target(new_base_branch));
4250 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4251 new_base_branch, update_progress, &did_something);
4252 if (error)
4253 goto done;
4254 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4255 goto done; /* nothing else to do */
4258 if (continue_rebase) {
4259 if (!rebase_in_progress) {
4260 error = got_error(GOT_ERR_NOT_REBASING);
4261 goto done;
4263 error = got_worktree_rebase_continue(&resume_commit_id,
4264 &new_base_branch, &tmp_branch, &branch, &fileindex,
4265 worktree, repo);
4266 if (error)
4267 goto done;
4269 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4270 resume_commit_id, repo);
4271 if (error)
4272 goto done;
4274 yca_id = got_object_id_dup(resume_commit_id);
4275 if (yca_id == NULL) {
4276 error = got_error_from_errno("got_object_id_dup");
4277 goto done;
4279 } else {
4280 error = got_ref_open(&branch, repo, argv[0], 0);
4281 if (error != NULL)
4282 goto done;
4285 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4286 if (error)
4287 goto done;
4289 if (!continue_rebase) {
4290 struct got_object_id *base_commit_id;
4292 base_commit_id = got_worktree_get_base_commit_id(worktree);
4293 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4294 base_commit_id, branch_head_commit_id, repo);
4295 if (error)
4296 goto done;
4297 if (yca_id == NULL) {
4298 error = got_error_msg(GOT_ERR_ANCESTRY,
4299 "specified branch shares no common ancestry "
4300 "with work tree's branch");
4301 goto done;
4304 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4305 if (error) {
4306 if (error->code != GOT_ERR_ANCESTRY)
4307 goto done;
4308 error = NULL;
4309 } else {
4310 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4311 "specified branch resolves to a commit which "
4312 "is already contained in work tree's branch");
4313 goto done;
4315 error = got_worktree_rebase_prepare(&new_base_branch,
4316 &tmp_branch, &fileindex, worktree, branch, repo);
4317 if (error)
4318 goto done;
4321 commit_id = branch_head_commit_id;
4322 error = got_object_open_as_commit(&commit, repo, commit_id);
4323 if (error)
4324 goto done;
4326 parent_ids = got_object_commit_get_parent_ids(commit);
4327 pid = SIMPLEQ_FIRST(parent_ids);
4328 if (pid == NULL) {
4329 if (!continue_rebase) {
4330 int did_something;
4331 error = got_worktree_rebase_abort(worktree, fileindex,
4332 repo, new_base_branch, update_progress,
4333 &did_something);
4334 if (error)
4335 goto done;
4336 printf("Rebase of %s aborted\n",
4337 got_ref_get_name(branch));
4339 error = got_error(GOT_ERR_EMPTY_REBASE);
4340 goto done;
4342 error = collect_commits(&commits, commit_id, pid->id,
4343 yca_id, got_worktree_get_path_prefix(worktree),
4344 GOT_ERR_REBASE_PATH, repo);
4345 got_object_commit_close(commit);
4346 commit = NULL;
4347 if (error)
4348 goto done;
4350 if (SIMPLEQ_EMPTY(&commits)) {
4351 if (continue_rebase)
4352 error = rebase_complete(worktree, fileindex,
4353 branch, new_base_branch, tmp_branch, repo);
4354 else
4355 error = got_error(GOT_ERR_EMPTY_REBASE);
4356 goto done;
4359 pid = NULL;
4360 SIMPLEQ_FOREACH(qid, &commits, entry) {
4361 commit_id = qid->id;
4362 parent_id = pid ? pid->id : yca_id;
4363 pid = qid;
4365 error = got_worktree_rebase_merge_files(&merged_paths,
4366 worktree, fileindex, parent_id, commit_id, repo,
4367 rebase_progress, &rebase_status, check_cancelled, NULL);
4368 if (error)
4369 goto done;
4371 if (rebase_status == GOT_STATUS_CONFLICT) {
4372 got_worktree_rebase_pathlist_free(&merged_paths);
4373 break;
4376 error = rebase_commit(&merged_paths, worktree, fileindex,
4377 tmp_branch, commit_id, repo);
4378 got_worktree_rebase_pathlist_free(&merged_paths);
4379 if (error)
4380 goto done;
4383 if (rebase_status == GOT_STATUS_CONFLICT) {
4384 error = got_worktree_rebase_postpone(worktree, fileindex);
4385 if (error)
4386 goto done;
4387 error = got_error_msg(GOT_ERR_CONFLICTS,
4388 "conflicts must be resolved before rebasing can continue");
4389 } else
4390 error = rebase_complete(worktree, fileindex, branch,
4391 new_base_branch, tmp_branch, repo);
4392 done:
4393 got_object_id_queue_free(&commits);
4394 free(branch_head_commit_id);
4395 free(resume_commit_id);
4396 free(yca_id);
4397 if (commit)
4398 got_object_commit_close(commit);
4399 if (branch)
4400 got_ref_close(branch);
4401 if (new_base_branch)
4402 got_ref_close(new_base_branch);
4403 if (tmp_branch)
4404 got_ref_close(tmp_branch);
4405 if (worktree)
4406 got_worktree_close(worktree);
4407 if (repo)
4408 got_repo_close(repo);
4409 return error;
4412 __dead static void
4413 usage_histedit(void)
4415 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4416 getprogname());
4417 exit(1);
4420 #define GOT_HISTEDIT_PICK 'p'
4421 #define GOT_HISTEDIT_EDIT 'e'
4422 #define GOT_HISTEDIT_FOLD 'f'
4423 #define GOT_HISTEDIT_DROP 'd'
4424 #define GOT_HISTEDIT_MESG 'm'
4426 static struct got_histedit_cmd {
4427 unsigned char code;
4428 const char *name;
4429 const char *desc;
4430 } got_histedit_cmds[] = {
4431 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4432 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4433 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4434 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4435 { GOT_HISTEDIT_MESG, "mesg",
4436 "single-line log message for commit above (open editor if empty)" },
4439 struct got_histedit_list_entry {
4440 TAILQ_ENTRY(got_histedit_list_entry) entry;
4441 struct got_object_id *commit_id;
4442 const struct got_histedit_cmd *cmd;
4443 char *logmsg;
4445 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4447 static const struct got_error *
4448 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4449 FILE *f, struct got_repository *repo)
4451 const struct got_error *err = NULL;
4452 char *logmsg = NULL, *id_str = NULL;
4453 struct got_commit_object *commit = NULL;
4454 int n;
4456 err = got_object_open_as_commit(&commit, repo, commit_id);
4457 if (err)
4458 goto done;
4460 err = get_short_logmsg(&logmsg, 34, commit);
4461 if (err)
4462 goto done;
4464 err = got_object_id_str(&id_str, commit_id);
4465 if (err)
4466 goto done;
4468 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4469 if (n < 0)
4470 err = got_ferror(f, GOT_ERR_IO);
4471 done:
4472 if (commit)
4473 got_object_commit_close(commit);
4474 free(id_str);
4475 free(logmsg);
4476 return err;
4479 static const struct got_error *
4480 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4481 struct got_repository *repo)
4483 const struct got_error *err = NULL;
4484 struct got_object_qid *qid;
4486 if (SIMPLEQ_EMPTY(commits))
4487 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4489 SIMPLEQ_FOREACH(qid, commits, entry) {
4490 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4491 f, repo);
4492 if (err)
4493 break;
4496 return err;
4499 static const struct got_error *
4500 write_cmd_list(FILE *f)
4502 const struct got_error *err = NULL;
4503 int n, i;
4505 n = fprintf(f, "# Available histedit commands:\n");
4506 if (n < 0)
4507 return got_ferror(f, GOT_ERR_IO);
4509 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4510 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4511 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4512 cmd->desc);
4513 if (n < 0) {
4514 err = got_ferror(f, GOT_ERR_IO);
4515 break;
4518 n = fprintf(f, "# Commits will be processed in order from top to "
4519 "bottom of this file.\n");
4520 if (n < 0)
4521 return got_ferror(f, GOT_ERR_IO);
4522 return err;
4525 static const struct got_error *
4526 histedit_syntax_error(int lineno)
4528 static char msg[42];
4529 int ret;
4531 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4532 lineno);
4533 if (ret == -1 || ret >= sizeof(msg))
4534 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4536 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4539 static const struct got_error *
4540 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4541 char *logmsg, struct got_repository *repo)
4543 const struct got_error *err;
4544 struct got_commit_object *folded_commit = NULL;
4545 char *id_str;
4547 err = got_object_id_str(&id_str, hle->commit_id);
4548 if (err)
4549 return err;
4551 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4552 if (err)
4553 goto done;
4555 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4556 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4557 got_object_commit_get_logmsg(folded_commit)) == -1) {
4558 err = got_error_from_errno("asprintf");
4559 goto done;
4561 done:
4562 if (folded_commit)
4563 got_object_commit_close(folded_commit);
4564 free(id_str);
4565 return err;
4568 static struct got_histedit_list_entry *
4569 get_folded_commits(struct got_histedit_list_entry *hle)
4571 struct got_histedit_list_entry *prev, *folded = NULL;
4573 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4574 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4575 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4576 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4577 folded = prev;
4578 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4581 return folded;
4584 static const struct got_error *
4585 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4586 struct got_repository *repo)
4588 char *logmsg_path = NULL, *id_str = NULL;
4589 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4590 const struct got_error *err = NULL;
4591 struct got_commit_object *commit = NULL;
4592 int fd;
4593 struct got_histedit_list_entry *folded = NULL;
4595 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4596 if (err)
4597 return err;
4599 folded = get_folded_commits(hle);
4600 if (folded) {
4601 while (folded != hle) {
4602 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4603 folded = TAILQ_NEXT(folded, entry);
4604 continue;
4606 err = append_folded_commit_msg(&new_msg, folded,
4607 logmsg, repo);
4608 if (err)
4609 goto done;
4610 free(logmsg);
4611 logmsg = new_msg;
4612 folded = TAILQ_NEXT(folded, entry);
4616 err = got_object_id_str(&id_str, hle->commit_id);
4617 if (err)
4618 goto done;
4619 if (asprintf(&new_msg,
4620 "%s\n# original log message of commit %s: %s",
4621 logmsg ? logmsg : "", id_str,
4622 got_object_commit_get_logmsg(commit)) == -1) {
4623 err = got_error_from_errno("asprintf");
4624 goto done;
4626 free(logmsg);
4627 logmsg = new_msg;
4629 err = got_object_id_str(&id_str, hle->commit_id);
4630 if (err)
4631 goto done;
4633 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4634 if (err)
4635 goto done;
4637 dprintf(fd, logmsg);
4638 close(fd);
4640 err = get_editor(&editor);
4641 if (err)
4642 goto done;
4644 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4645 if (err) {
4646 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4647 goto done;
4648 err = NULL;
4649 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4650 if (hle->logmsg == NULL)
4651 err = got_error_from_errno("strdup");
4653 done:
4654 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4655 err = got_error_from_errno2("unlink", logmsg_path);
4656 free(logmsg_path);
4657 free(logmsg);
4658 free(editor);
4659 if (commit)
4660 got_object_commit_close(commit);
4661 return err;
4664 static const struct got_error *
4665 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4666 FILE *f, struct got_repository *repo)
4668 const struct got_error *err = NULL;
4669 char *line = NULL, *p, *end;
4670 size_t size;
4671 ssize_t len;
4672 int lineno = 0, i;
4673 const struct got_histedit_cmd *cmd;
4674 struct got_object_id *commit_id = NULL;
4675 struct got_histedit_list_entry *hle = NULL;
4677 for (;;) {
4678 len = getline(&line, &size, f);
4679 if (len == -1) {
4680 const struct got_error *getline_err;
4681 if (feof(f))
4682 break;
4683 getline_err = got_error_from_errno("getline");
4684 err = got_ferror(f, getline_err->code);
4685 break;
4687 lineno++;
4688 p = line;
4689 while (isspace((unsigned char)p[0]))
4690 p++;
4691 if (p[0] == '#' || p[0] == '\0') {
4692 free(line);
4693 line = NULL;
4694 continue;
4696 cmd = NULL;
4697 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4698 cmd = &got_histedit_cmds[i];
4699 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4700 isspace((unsigned char)p[strlen(cmd->name)])) {
4701 p += strlen(cmd->name);
4702 break;
4704 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4705 p++;
4706 break;
4709 if (i == nitems(got_histedit_cmds)) {
4710 err = histedit_syntax_error(lineno);
4711 break;
4713 while (isspace((unsigned char)p[0]))
4714 p++;
4715 if (cmd->code == GOT_HISTEDIT_MESG) {
4716 if (hle == NULL || hle->logmsg != NULL) {
4717 err = got_error(GOT_ERR_HISTEDIT_CMD);
4718 break;
4720 if (p[0] == '\0') {
4721 err = histedit_edit_logmsg(hle, repo);
4722 if (err)
4723 break;
4724 } else {
4725 hle->logmsg = strdup(p);
4726 if (hle->logmsg == NULL) {
4727 err = got_error_from_errno("strdup");
4728 break;
4731 free(line);
4732 line = NULL;
4733 continue;
4734 } else {
4735 end = p;
4736 while (end[0] && !isspace((unsigned char)end[0]))
4737 end++;
4738 *end = '\0';
4740 err = got_object_resolve_id_str(&commit_id, repo, p);
4741 if (err) {
4742 /* override error code */
4743 err = histedit_syntax_error(lineno);
4744 break;
4747 hle = malloc(sizeof(*hle));
4748 if (hle == NULL) {
4749 err = got_error_from_errno("malloc");
4750 break;
4752 hle->cmd = cmd;
4753 hle->commit_id = commit_id;
4754 hle->logmsg = NULL;
4755 commit_id = NULL;
4756 free(line);
4757 line = NULL;
4758 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4761 free(line);
4762 free(commit_id);
4763 return err;
4766 static const struct got_error *
4767 histedit_check_script(struct got_histedit_list *histedit_cmds,
4768 struct got_object_id_queue *commits, struct got_repository *repo)
4770 const struct got_error *err = NULL;
4771 struct got_object_qid *qid;
4772 struct got_histedit_list_entry *hle;
4773 static char msg[80];
4774 char *id_str;
4776 if (TAILQ_EMPTY(histedit_cmds))
4777 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4778 "histedit script contains no commands");
4779 if (SIMPLEQ_EMPTY(commits))
4780 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4782 SIMPLEQ_FOREACH(qid, commits, entry) {
4783 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4784 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4785 break;
4787 if (hle == NULL) {
4788 err = got_object_id_str(&id_str, qid->id);
4789 if (err)
4790 return err;
4791 snprintf(msg, sizeof(msg),
4792 "commit %s missing from histedit script", id_str);
4793 free(id_str);
4794 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4798 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4799 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4800 "last commit in histedit script cannot be folded");
4802 return NULL;
4805 static const struct got_error *
4806 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4807 const char *path, struct got_object_id_queue *commits,
4808 struct got_repository *repo)
4810 const struct got_error *err = NULL;
4811 char *editor;
4812 FILE *f = NULL;
4814 err = get_editor(&editor);
4815 if (err)
4816 return err;
4818 if (spawn_editor(editor, path) == -1) {
4819 err = got_error_from_errno("failed spawning editor");
4820 goto done;
4823 f = fopen(path, "r");
4824 if (f == NULL) {
4825 err = got_error_from_errno("fopen");
4826 goto done;
4828 err = histedit_parse_list(histedit_cmds, f, repo);
4829 if (err)
4830 goto done;
4832 err = histedit_check_script(histedit_cmds, commits, repo);
4833 done:
4834 if (f && fclose(f) != 0 && err == NULL)
4835 err = got_error_from_errno("fclose");
4836 free(editor);
4837 return err;
4840 static const struct got_error *
4841 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4842 struct got_object_id_queue *, const char *, struct got_repository *);
4844 static const struct got_error *
4845 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4846 struct got_object_id_queue *commits, struct got_repository *repo)
4848 const struct got_error *err;
4849 FILE *f = NULL;
4850 char *path = NULL;
4852 err = got_opentemp_named(&path, &f, "got-histedit");
4853 if (err)
4854 return err;
4856 err = write_cmd_list(f);
4857 if (err)
4858 goto done;
4860 err = histedit_write_commit_list(commits, f, repo);
4861 if (err)
4862 goto done;
4864 if (fclose(f) != 0) {
4865 err = got_error_from_errno("fclose");
4866 goto done;
4868 f = NULL;
4870 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4871 if (err) {
4872 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4873 err->code != GOT_ERR_HISTEDIT_CMD)
4874 goto done;
4875 err = histedit_edit_list_retry(histedit_cmds, err,
4876 commits, path, repo);
4878 done:
4879 if (f && fclose(f) != 0 && err == NULL)
4880 err = got_error_from_errno("fclose");
4881 if (path && unlink(path) != 0 && err == NULL)
4882 err = got_error_from_errno2("unlink", path);
4883 free(path);
4884 return err;
4887 static const struct got_error *
4888 histedit_save_list(struct got_histedit_list *histedit_cmds,
4889 struct got_worktree *worktree, struct got_repository *repo)
4891 const struct got_error *err = NULL;
4892 char *path = NULL;
4893 FILE *f = NULL;
4894 struct got_histedit_list_entry *hle;
4895 struct got_commit_object *commit = NULL;
4897 err = got_worktree_get_histedit_script_path(&path, worktree);
4898 if (err)
4899 return err;
4901 f = fopen(path, "w");
4902 if (f == NULL) {
4903 err = got_error_from_errno2("fopen", path);
4904 goto done;
4906 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4907 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4908 repo);
4909 if (err)
4910 break;
4912 if (hle->logmsg) {
4913 int n = fprintf(f, "%c %s\n",
4914 GOT_HISTEDIT_MESG, hle->logmsg);
4915 if (n < 0) {
4916 err = got_ferror(f, GOT_ERR_IO);
4917 break;
4921 done:
4922 if (f && fclose(f) != 0 && err == NULL)
4923 err = got_error_from_errno("fclose");
4924 free(path);
4925 if (commit)
4926 got_object_commit_close(commit);
4927 return err;
4930 void
4931 histedit_free_list(struct got_histedit_list *histedit_cmds)
4933 struct got_histedit_list_entry *hle;
4935 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4936 TAILQ_REMOVE(histedit_cmds, hle, entry);
4937 free(hle);
4941 static const struct got_error *
4942 histedit_load_list(struct got_histedit_list *histedit_cmds,
4943 const char *path, struct got_repository *repo)
4945 const struct got_error *err = NULL;
4946 FILE *f = NULL;
4948 f = fopen(path, "r");
4949 if (f == NULL) {
4950 err = got_error_from_errno2("fopen", path);
4951 goto done;
4954 err = histedit_parse_list(histedit_cmds, f, repo);
4955 done:
4956 if (f && fclose(f) != 0 && err == NULL)
4957 err = got_error_from_errno("fclose");
4958 return err;
4961 static const struct got_error *
4962 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4963 const struct got_error *edit_err, struct got_object_id_queue *commits,
4964 const char *path, struct got_repository *repo)
4966 const struct got_error *err = NULL, *prev_err = edit_err;
4967 int resp = ' ';
4969 while (resp != 'c' && resp != 'r' && resp != 'a') {
4970 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4971 "or (a)bort: ", getprogname(), prev_err->msg);
4972 resp = getchar();
4973 if (resp == '\n')
4974 resp = getchar();
4975 if (resp == 'c') {
4976 histedit_free_list(histedit_cmds);
4977 err = histedit_run_editor(histedit_cmds, path, commits,
4978 repo);
4979 if (err) {
4980 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4981 err->code != GOT_ERR_HISTEDIT_CMD)
4982 break;
4983 prev_err = err;
4984 resp = ' ';
4985 continue;
4987 break;
4988 } else if (resp == 'r') {
4989 histedit_free_list(histedit_cmds);
4990 err = histedit_edit_script(histedit_cmds,
4991 commits, repo);
4992 if (err) {
4993 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4994 err->code != GOT_ERR_HISTEDIT_CMD)
4995 break;
4996 prev_err = err;
4997 resp = ' ';
4998 continue;
5000 break;
5001 } else if (resp == 'a') {
5002 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5003 break;
5004 } else
5005 printf("invalid response '%c'\n", resp);
5008 return err;
5011 static const struct got_error *
5012 histedit_complete(struct got_worktree *worktree,
5013 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5014 struct got_reference *branch, struct got_repository *repo)
5016 printf("Switching work tree to %s\n",
5017 got_ref_get_symref_target(branch));
5018 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5019 branch, repo);
5022 static const struct got_error *
5023 show_histedit_progress(struct got_commit_object *commit,
5024 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5026 const struct got_error *err;
5027 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5029 err = got_object_id_str(&old_id_str, hle->commit_id);
5030 if (err)
5031 goto done;
5033 if (new_id) {
5034 err = got_object_id_str(&new_id_str, new_id);
5035 if (err)
5036 goto done;
5039 old_id_str[12] = '\0';
5040 if (new_id_str)
5041 new_id_str[12] = '\0';
5043 if (hle->logmsg) {
5044 logmsg = strdup(hle->logmsg);
5045 if (logmsg == NULL) {
5046 err = got_error_from_errno("strdup");
5047 goto done;
5049 trim_logmsg(logmsg, 42);
5050 } else {
5051 err = get_short_logmsg(&logmsg, 42, commit);
5052 if (err)
5053 goto done;
5056 switch (hle->cmd->code) {
5057 case GOT_HISTEDIT_PICK:
5058 case GOT_HISTEDIT_EDIT:
5059 printf("%s -> %s: %s\n", old_id_str,
5060 new_id_str ? new_id_str : "no-op change", logmsg);
5061 break;
5062 case GOT_HISTEDIT_DROP:
5063 case GOT_HISTEDIT_FOLD:
5064 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5065 logmsg);
5066 break;
5067 default:
5068 break;
5071 done:
5072 free(old_id_str);
5073 free(new_id_str);
5074 return err;
5077 static const struct got_error *
5078 histedit_commit(struct got_pathlist_head *merged_paths,
5079 struct got_worktree *worktree, struct got_fileindex *fileindex,
5080 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5081 struct got_repository *repo)
5083 const struct got_error *err;
5084 struct got_commit_object *commit;
5085 struct got_object_id *new_commit_id;
5087 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5088 && hle->logmsg == NULL) {
5089 err = histedit_edit_logmsg(hle, repo);
5090 if (err)
5091 return err;
5094 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5095 if (err)
5096 return err;
5098 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5099 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5100 hle->logmsg, repo);
5101 if (err) {
5102 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5103 goto done;
5104 err = show_histedit_progress(commit, hle, NULL);
5105 } else {
5106 err = show_histedit_progress(commit, hle, new_commit_id);
5107 free(new_commit_id);
5109 done:
5110 got_object_commit_close(commit);
5111 return err;
5114 static const struct got_error *
5115 histedit_skip_commit(struct got_histedit_list_entry *hle,
5116 struct got_worktree *worktree, struct got_repository *repo)
5118 const struct got_error *error;
5119 struct got_commit_object *commit;
5121 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5122 repo);
5123 if (error)
5124 return error;
5126 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5127 if (error)
5128 return error;
5130 error = show_histedit_progress(commit, hle, NULL);
5131 got_object_commit_close(commit);
5132 return error;
5135 static const struct got_error *
5136 cmd_histedit(int argc, char *argv[])
5138 const struct got_error *error = NULL;
5139 struct got_worktree *worktree = NULL;
5140 struct got_fileindex *fileindex = NULL;
5141 struct got_repository *repo = NULL;
5142 char *cwd = NULL;
5143 struct got_reference *branch = NULL;
5144 struct got_reference *tmp_branch = NULL;
5145 struct got_object_id *resume_commit_id = NULL;
5146 struct got_object_id *base_commit_id = NULL;
5147 struct got_object_id *head_commit_id = NULL;
5148 struct got_commit_object *commit = NULL;
5149 int ch, rebase_in_progress = 0, did_something;
5150 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5151 const char *edit_script_path = NULL;
5152 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5153 struct got_object_id_queue commits;
5154 struct got_pathlist_head merged_paths;
5155 const struct got_object_id_queue *parent_ids;
5156 struct got_object_qid *pid;
5157 struct got_histedit_list histedit_cmds;
5158 struct got_histedit_list_entry *hle;
5160 SIMPLEQ_INIT(&commits);
5161 TAILQ_INIT(&histedit_cmds);
5162 TAILQ_INIT(&merged_paths);
5164 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5165 switch (ch) {
5166 case 'a':
5167 abort_edit = 1;
5168 break;
5169 case 'c':
5170 continue_edit = 1;
5171 break;
5172 case 'F':
5173 edit_script_path = optarg;
5174 break;
5175 default:
5176 usage_histedit();
5177 /* NOTREACHED */
5181 argc -= optind;
5182 argv += optind;
5184 #ifndef PROFILE
5185 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5186 "unveil", NULL) == -1)
5187 err(1, "pledge");
5188 #endif
5189 if (abort_edit && continue_edit)
5190 usage_histedit();
5191 if (argc != 0)
5192 usage_histedit();
5195 * This command cannot apply unveil(2) in all cases because the
5196 * user may choose to run an editor to edit the histedit script
5197 * and to edit individual commit log messages.
5198 * unveil(2) traverses exec(2); if an editor is used we have to
5199 * apply unveil after edit script and log messages have been written.
5200 * XXX TODO: Make use of unveil(2) where possible.
5203 cwd = getcwd(NULL, 0);
5204 if (cwd == NULL) {
5205 error = got_error_from_errno("getcwd");
5206 goto done;
5208 error = got_worktree_open(&worktree, cwd);
5209 if (error)
5210 goto done;
5212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5213 if (error != NULL)
5214 goto done;
5216 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5217 if (error)
5218 goto done;
5219 if (rebase_in_progress) {
5220 error = got_error(GOT_ERR_REBASING);
5221 goto done;
5224 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5225 if (error)
5226 goto done;
5228 if (edit_in_progress && abort_edit) {
5229 error = got_worktree_histedit_continue(&resume_commit_id,
5230 &tmp_branch, &branch, &base_commit_id, &fileindex,
5231 worktree, repo);
5232 if (error)
5233 goto done;
5234 printf("Switching work tree to %s\n",
5235 got_ref_get_symref_target(branch));
5236 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5237 branch, base_commit_id, update_progress, &did_something);
5238 if (error)
5239 goto done;
5240 printf("Histedit of %s aborted\n",
5241 got_ref_get_symref_target(branch));
5242 goto done; /* nothing else to do */
5243 } else if (abort_edit) {
5244 error = got_error(GOT_ERR_NOT_HISTEDIT);
5245 goto done;
5248 if (continue_edit) {
5249 char *path;
5251 if (!edit_in_progress) {
5252 error = got_error(GOT_ERR_NOT_HISTEDIT);
5253 goto done;
5256 error = got_worktree_get_histedit_script_path(&path, worktree);
5257 if (error)
5258 goto done;
5260 error = histedit_load_list(&histedit_cmds, path, repo);
5261 free(path);
5262 if (error)
5263 goto done;
5265 error = got_worktree_histedit_continue(&resume_commit_id,
5266 &tmp_branch, &branch, &base_commit_id, &fileindex,
5267 worktree, repo);
5268 if (error)
5269 goto done;
5271 error = got_ref_resolve(&head_commit_id, repo, branch);
5272 if (error)
5273 goto done;
5275 error = got_object_open_as_commit(&commit, repo,
5276 head_commit_id);
5277 if (error)
5278 goto done;
5279 parent_ids = got_object_commit_get_parent_ids(commit);
5280 pid = SIMPLEQ_FIRST(parent_ids);
5281 if (pid == NULL) {
5282 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5283 goto done;
5285 error = collect_commits(&commits, head_commit_id, pid->id,
5286 base_commit_id, got_worktree_get_path_prefix(worktree),
5287 GOT_ERR_HISTEDIT_PATH, repo);
5288 got_object_commit_close(commit);
5289 commit = NULL;
5290 if (error)
5291 goto done;
5292 } else {
5293 if (edit_in_progress) {
5294 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5295 goto done;
5298 error = got_ref_open(&branch, repo,
5299 got_worktree_get_head_ref_name(worktree), 0);
5300 if (error != NULL)
5301 goto done;
5303 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5304 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5305 "will not edit commit history of a branch outside "
5306 "the \"refs/heads/\" reference namespace");
5307 goto done;
5310 error = got_ref_resolve(&head_commit_id, repo, branch);
5311 got_ref_close(branch);
5312 branch = NULL;
5313 if (error)
5314 goto done;
5316 error = got_object_open_as_commit(&commit, repo,
5317 head_commit_id);
5318 if (error)
5319 goto done;
5320 parent_ids = got_object_commit_get_parent_ids(commit);
5321 pid = SIMPLEQ_FIRST(parent_ids);
5322 if (pid == NULL) {
5323 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5324 goto done;
5326 error = collect_commits(&commits, head_commit_id, pid->id,
5327 got_worktree_get_base_commit_id(worktree),
5328 got_worktree_get_path_prefix(worktree),
5329 GOT_ERR_HISTEDIT_PATH, repo);
5330 got_object_commit_close(commit);
5331 commit = NULL;
5332 if (error)
5333 goto done;
5335 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5336 &base_commit_id, &fileindex, worktree, repo);
5337 if (error)
5338 goto done;
5340 if (edit_script_path) {
5341 error = histedit_load_list(&histedit_cmds,
5342 edit_script_path, repo);
5343 if (error) {
5344 got_worktree_histedit_abort(worktree, fileindex,
5345 repo, branch, base_commit_id,
5346 update_progress, &did_something);
5347 goto done;
5349 } else {
5350 error = histedit_edit_script(&histedit_cmds, &commits,
5351 repo);
5352 if (error) {
5353 got_worktree_histedit_abort(worktree, fileindex,
5354 repo, branch, base_commit_id,
5355 update_progress, &did_something);
5356 goto done;
5361 error = histedit_save_list(&histedit_cmds, worktree,
5362 repo);
5363 if (error) {
5364 got_worktree_histedit_abort(worktree, fileindex,
5365 repo, branch, base_commit_id,
5366 update_progress, &did_something);
5367 goto done;
5372 error = histedit_check_script(&histedit_cmds, &commits, repo);
5373 if (error)
5374 goto done;
5376 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5377 if (resume_commit_id) {
5378 if (got_object_id_cmp(hle->commit_id,
5379 resume_commit_id) != 0)
5380 continue;
5382 resume_commit_id = NULL;
5383 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5384 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5385 error = histedit_skip_commit(hle, worktree,
5386 repo);
5387 } else {
5388 error = histedit_commit(NULL, worktree,
5389 fileindex, tmp_branch, hle, repo);
5391 if (error)
5392 goto done;
5393 continue;
5396 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5397 error = histedit_skip_commit(hle, worktree, repo);
5398 if (error)
5399 goto done;
5400 continue;
5403 error = got_object_open_as_commit(&commit, repo,
5404 hle->commit_id);
5405 if (error)
5406 goto done;
5407 parent_ids = got_object_commit_get_parent_ids(commit);
5408 pid = SIMPLEQ_FIRST(parent_ids);
5410 error = got_worktree_histedit_merge_files(&merged_paths,
5411 worktree, fileindex, pid->id, hle->commit_id, repo,
5412 rebase_progress, &rebase_status, check_cancelled, NULL);
5413 if (error)
5414 goto done;
5415 got_object_commit_close(commit);
5416 commit = NULL;
5418 if (rebase_status == GOT_STATUS_CONFLICT) {
5419 got_worktree_rebase_pathlist_free(&merged_paths);
5420 break;
5423 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5424 char *id_str;
5425 error = got_object_id_str(&id_str, hle->commit_id);
5426 if (error)
5427 goto done;
5428 printf("Stopping histedit for amending commit %s\n",
5429 id_str);
5430 free(id_str);
5431 got_worktree_rebase_pathlist_free(&merged_paths);
5432 error = got_worktree_histedit_postpone(worktree,
5433 fileindex);
5434 goto done;
5437 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5438 error = histedit_skip_commit(hle, worktree, repo);
5439 if (error)
5440 goto done;
5441 continue;
5444 error = histedit_commit(&merged_paths, worktree, fileindex,
5445 tmp_branch, hle, repo);
5446 got_worktree_rebase_pathlist_free(&merged_paths);
5447 if (error)
5448 goto done;
5451 if (rebase_status == GOT_STATUS_CONFLICT) {
5452 error = got_worktree_histedit_postpone(worktree, fileindex);
5453 if (error)
5454 goto done;
5455 error = got_error_msg(GOT_ERR_CONFLICTS,
5456 "conflicts must be resolved before rebasing can continue");
5457 } else
5458 error = histedit_complete(worktree, fileindex, tmp_branch,
5459 branch, repo);
5460 done:
5461 got_object_id_queue_free(&commits);
5462 histedit_free_list(&histedit_cmds);
5463 free(head_commit_id);
5464 free(base_commit_id);
5465 free(resume_commit_id);
5466 if (commit)
5467 got_object_commit_close(commit);
5468 if (branch)
5469 got_ref_close(branch);
5470 if (tmp_branch)
5471 got_ref_close(tmp_branch);
5472 if (worktree)
5473 got_worktree_close(worktree);
5474 if (repo)
5475 got_repo_close(repo);
5476 return error;
5479 __dead static void
5480 usage_stage(void)
5482 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5483 "[file-path ...]\n",
5484 getprogname());
5485 exit(1);
5488 static const struct got_error *
5489 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5490 const char *path, struct got_object_id *blob_id,
5491 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5493 const struct got_error *err = NULL;
5494 char *id_str = NULL;
5496 if (staged_status != GOT_STATUS_ADD &&
5497 staged_status != GOT_STATUS_MODIFY &&
5498 staged_status != GOT_STATUS_DELETE)
5499 return NULL;
5501 if (staged_status == GOT_STATUS_ADD ||
5502 staged_status == GOT_STATUS_MODIFY)
5503 err = got_object_id_str(&id_str, staged_blob_id);
5504 else
5505 err = got_object_id_str(&id_str, blob_id);
5506 if (err)
5507 return err;
5509 printf("%s %c %s\n", id_str, staged_status, path);
5510 free(id_str);
5511 return NULL;
5514 static const struct got_error *
5515 cmd_stage(int argc, char *argv[])
5517 const struct got_error *error = NULL;
5518 struct got_repository *repo = NULL;
5519 struct got_worktree *worktree = NULL;
5520 char *cwd = NULL;
5521 struct got_pathlist_head paths;
5522 struct got_pathlist_entry *pe;
5523 int ch, list_stage = 0, pflag = 0;
5524 FILE *patch_script_file = NULL;
5525 const char *patch_script_path = NULL;
5526 struct choose_patch_arg cpa;
5528 TAILQ_INIT(&paths);
5530 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5531 switch (ch) {
5532 case 'l':
5533 list_stage = 1;
5534 break;
5535 case 'p':
5536 pflag = 1;
5537 break;
5538 case 'F':
5539 patch_script_path = optarg;
5540 break;
5541 default:
5542 usage_stage();
5543 /* NOTREACHED */
5547 argc -= optind;
5548 argv += optind;
5550 #ifndef PROFILE
5551 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5552 "unveil", NULL) == -1)
5553 err(1, "pledge");
5554 #endif
5555 if (list_stage && (pflag || patch_script_path))
5556 errx(1, "-l option cannot be used with other options");
5557 if (patch_script_path && !pflag)
5558 errx(1, "-F option can only be used together with -p option");
5560 cwd = getcwd(NULL, 0);
5561 if (cwd == NULL) {
5562 error = got_error_from_errno("getcwd");
5563 goto done;
5566 error = got_worktree_open(&worktree, cwd);
5567 if (error)
5568 goto done;
5570 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5571 if (error != NULL)
5572 goto done;
5574 if (patch_script_path) {
5575 patch_script_file = fopen(patch_script_path, "r");
5576 if (patch_script_file == NULL) {
5577 error = got_error_from_errno2("fopen",
5578 patch_script_path);
5579 goto done;
5582 error = apply_unveil(got_repo_get_path(repo), 1,
5583 got_worktree_get_root_path(worktree));
5584 if (error)
5585 goto done;
5587 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5588 if (error)
5589 goto done;
5591 if (list_stage)
5592 error = got_worktree_status(worktree, &paths, repo,
5593 print_stage, NULL, check_cancelled, NULL);
5594 else {
5595 cpa.patch_script_file = patch_script_file;
5596 cpa.action = "stage";
5597 error = got_worktree_stage(worktree, &paths,
5598 pflag ? NULL : print_status, NULL,
5599 pflag ? choose_patch : NULL, &cpa, repo);
5601 done:
5602 if (patch_script_file && fclose(patch_script_file) == EOF &&
5603 error == NULL)
5604 error = got_error_from_errno2("fclose", patch_script_path);
5605 if (repo)
5606 got_repo_close(repo);
5607 if (worktree)
5608 got_worktree_close(worktree);
5609 TAILQ_FOREACH(pe, &paths, entry)
5610 free((char *)pe->path);
5611 got_pathlist_free(&paths);
5612 free(cwd);
5613 return error;
5616 __dead static void
5617 usage_unstage(void)
5619 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5620 "[file-path ...]\n",
5621 getprogname());
5622 exit(1);
5626 static const struct got_error *
5627 cmd_unstage(int argc, char *argv[])
5629 const struct got_error *error = NULL;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5632 char *cwd = NULL;
5633 struct got_pathlist_head paths;
5634 struct got_pathlist_entry *pe;
5635 int ch, did_something = 0, pflag = 0;
5636 FILE *patch_script_file = NULL;
5637 const char *patch_script_path = NULL;
5638 struct choose_patch_arg cpa;
5640 TAILQ_INIT(&paths);
5642 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5643 switch (ch) {
5644 case 'p':
5645 pflag = 1;
5646 break;
5647 case 'F':
5648 patch_script_path = optarg;
5649 break;
5650 default:
5651 usage_unstage();
5652 /* NOTREACHED */
5656 argc -= optind;
5657 argv += optind;
5659 #ifndef PROFILE
5660 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5661 "unveil", NULL) == -1)
5662 err(1, "pledge");
5663 #endif
5664 if (patch_script_path && !pflag)
5665 errx(1, "-F option can only be used together with -p option");
5667 cwd = getcwd(NULL, 0);
5668 if (cwd == NULL) {
5669 error = got_error_from_errno("getcwd");
5670 goto done;
5673 error = got_worktree_open(&worktree, cwd);
5674 if (error)
5675 goto done;
5677 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5678 if (error != NULL)
5679 goto done;
5681 if (patch_script_path) {
5682 patch_script_file = fopen(patch_script_path, "r");
5683 if (patch_script_file == NULL) {
5684 error = got_error_from_errno2("fopen",
5685 patch_script_path);
5686 goto done;
5690 error = apply_unveil(got_repo_get_path(repo), 1,
5691 got_worktree_get_root_path(worktree));
5692 if (error)
5693 goto done;
5695 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5696 if (error)
5697 goto done;
5699 cpa.patch_script_file = patch_script_file;
5700 cpa.action = "unstage";
5701 error = got_worktree_unstage(worktree, &paths, update_progress,
5702 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5703 done:
5704 if (patch_script_file && fclose(patch_script_file) == EOF &&
5705 error == NULL)
5706 error = got_error_from_errno2("fclose", patch_script_path);
5707 if (repo)
5708 got_repo_close(repo);
5709 if (worktree)
5710 got_worktree_close(worktree);
5711 TAILQ_FOREACH(pe, &paths, entry)
5712 free((char *)pe->path);
5713 got_pathlist_free(&paths);
5714 free(cwd);
5715 return error;