Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_path.h"
44 #include "got_worktree.h"
45 #include "got_diff.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
51 #ifndef nitems
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 #endif
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
58 static void
59 catch_sigint(int signo)
60 {
61 sigint_received = 1;
62 }
64 static void
65 catch_sigpipe(int signo)
66 {
67 sigpipe_received = 1;
68 }
71 struct got_cmd {
72 const char *cmd_name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
76 };
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
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 cmd_import(int argc, char *argv[])
488 const struct got_error *error = NULL;
489 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
490 char *editor = NULL;
491 const char *got_author = getenv("GOT_AUTHOR");
492 const char *branch_name = "master";
493 char *refname = NULL, *id_str = NULL;
494 struct got_repository *repo = NULL;
495 struct got_reference *branch_ref = NULL, *head_ref = NULL;
496 struct got_object_id *new_commit_id = NULL;
497 int ch;
498 struct got_pathlist_head ignores;
499 struct got_pathlist_entry *pe;
501 TAILQ_INIT(&ignores);
503 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
504 switch (ch) {
505 case 'b':
506 branch_name = optarg;
507 break;
508 case 'm':
509 logmsg = strdup(optarg);
510 if (logmsg == NULL) {
511 error = got_error_from_errno("strdup");
512 goto done;
514 break;
515 case 'r':
516 repo_path = realpath(optarg, NULL);
517 if (repo_path == NULL) {
518 error = got_error_from_errno("realpath");
519 goto done;
521 break;
522 case 'I':
523 if (optarg[0] == '\0')
524 break;
525 error = got_pathlist_insert(&pe, &ignores, optarg,
526 NULL);
527 if (error)
528 goto done;
529 break;
530 default:
531 usage_init();
532 /* NOTREACHED */
536 argc -= optind;
537 argv += optind;
539 #ifndef PROFILE
540 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
541 NULL) == -1)
542 err(1, "pledge");
543 #endif
544 if (argc != 1)
545 usage_import();
547 if (got_author == NULL) {
548 /* TODO: Look current user up in password database */
549 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
550 goto done;
553 if (repo_path == NULL) {
554 repo_path = getcwd(NULL, 0);
555 if (repo_path == NULL)
556 return got_error_from_errno("getcwd");
558 got_path_strip_trailing_slashes(repo_path);
559 error = got_repo_open(&repo, repo_path);
560 if (error)
561 goto done;
563 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
564 error = got_error_from_errno("asprintf");
565 goto done;
568 error = got_ref_open(&branch_ref, repo, refname, 0);
569 if (error) {
570 if (error->code != GOT_ERR_NOT_REF)
571 goto done;
572 } else {
573 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
574 "import target branch already exists");
575 goto done;
578 path_dir = realpath(argv[0], NULL);
579 if (path_dir == NULL) {
580 error = got_error_from_errno("realpath");
581 goto done;
583 got_path_strip_trailing_slashes(path_dir);
585 /*
586 * unveil(2) traverses exec(2); if an editor is used we have
587 * to apply unveil after the log message has been written.
588 */
589 if (logmsg == NULL || strlen(logmsg) == 0) {
590 error = get_editor(&editor);
591 if (error)
592 goto done;
593 error = collect_import_msg(&logmsg, editor, path_dir, refname);
594 if (error)
595 goto done;
598 if (unveil(path_dir, "r") != 0)
599 return got_error_from_errno2("unveil", path_dir);
601 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
602 if (error)
603 goto done;
605 error = got_repo_import(&new_commit_id, path_dir, logmsg,
606 got_author, &ignores, repo, import_progress, NULL);
607 if (error)
608 goto done;
610 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
611 if (error)
612 goto done;
614 error = got_ref_write(branch_ref, repo);
615 if (error)
616 goto done;
618 error = got_object_id_str(&id_str, new_commit_id);
619 if (error)
620 goto done;
622 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
623 if (error) {
624 if (error->code != GOT_ERR_NOT_REF)
625 goto done;
627 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
628 branch_ref);
629 if (error)
630 goto done;
632 error = got_ref_write(head_ref, repo);
633 if (error)
634 goto done;
637 printf("Created branch %s with commit %s\n",
638 got_ref_get_name(branch_ref), id_str);
639 done:
640 free(repo_path);
641 free(editor);
642 free(refname);
643 free(new_commit_id);
644 free(id_str);
645 if (branch_ref)
646 got_ref_close(branch_ref);
647 if (head_ref)
648 got_ref_close(head_ref);
649 return error;
652 __dead static void
653 usage_checkout(void)
655 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
656 "[-p prefix] repository-path [worktree-path]\n", getprogname());
657 exit(1);
660 static const struct got_error *
661 checkout_progress(void *arg, unsigned char status, const char *path)
663 char *worktree_path = arg;
665 /* Base commit bump happens silently. */
666 if (status == GOT_STATUS_BUMP_BASE)
667 return NULL;
669 while (path[0] == '/')
670 path++;
672 printf("%c %s/%s\n", status, worktree_path, path);
673 return NULL;
676 static const struct got_error *
677 check_cancelled(void *arg)
679 if (sigint_received || sigpipe_received)
680 return got_error(GOT_ERR_CANCELLED);
681 return NULL;
684 static const struct got_error *
685 check_linear_ancestry(struct got_object_id *commit_id,
686 struct got_object_id *base_commit_id, struct got_repository *repo)
688 const struct got_error *err = NULL;
689 struct got_object_id *yca_id;
691 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
692 commit_id, base_commit_id, repo);
693 if (err)
694 return err;
696 if (yca_id == NULL)
697 return got_error(GOT_ERR_ANCESTRY);
699 /*
700 * Require a straight line of history between the target commit
701 * and the work tree's base commit.
703 * Non-linear situations such as this require a rebase:
705 * (commit) D F (base_commit)
706 * \ /
707 * C E
708 * \ /
709 * B (yca)
710 * |
711 * A
713 * 'got update' only handles linear cases:
714 * Update forwards in time: A (base/yca) - B - C - D (commit)
715 * Update backwards in time: D (base) - C - B - A (commit/yca)
716 */
717 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
718 got_object_id_cmp(base_commit_id, yca_id) != 0)
719 return got_error(GOT_ERR_ANCESTRY);
721 free(yca_id);
722 return NULL;
725 static const struct got_error *
726 check_same_branch(struct got_object_id *commit_id,
727 struct got_reference *head_ref, struct got_object_id *yca_id,
728 struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_commit_graph *graph = NULL;
732 struct got_object_id *head_commit_id = NULL;
733 int is_same_branch = 0;
735 err = got_ref_resolve(&head_commit_id, repo, head_ref);
736 if (err)
737 goto done;
739 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
740 is_same_branch = 1;
741 goto done;
743 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
744 is_same_branch = 1;
745 goto done;
748 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
749 if (err)
750 goto done;
752 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
753 if (err)
754 goto done;
756 for (;;) {
757 struct got_object_id *id;
758 err = got_commit_graph_iter_next(&id, graph);
759 if (err) {
760 if (err->code == GOT_ERR_ITER_COMPLETED) {
761 err = NULL;
762 break;
763 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
764 break;
765 err = got_commit_graph_fetch_commits(graph, 1,
766 repo);
767 if (err)
768 break;
771 if (id) {
772 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
773 break;
774 if (got_object_id_cmp(id, commit_id) == 0) {
775 is_same_branch = 1;
776 break;
780 done:
781 if (graph)
782 got_commit_graph_close(graph);
783 free(head_commit_id);
784 if (!err && !is_same_branch)
785 err = got_error(GOT_ERR_ANCESTRY);
786 return err;
789 static const struct got_error *
790 resolve_commit_arg(struct got_object_id **commit_id,
791 const char *commit_id_arg, struct got_repository *repo)
793 const struct got_error *err;
794 struct got_reference *ref;
796 err = got_ref_open(&ref, repo, commit_id_arg, 0);
797 if (err == NULL) {
798 err = got_ref_resolve(commit_id, repo, ref);
799 got_ref_close(ref);
800 } else {
801 if (err->code != GOT_ERR_NOT_REF)
802 return err;
803 err = got_repo_match_object_id_prefix(commit_id,
804 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
806 return err;
809 static const struct got_error *
810 cmd_checkout(int argc, char *argv[])
812 const struct got_error *error = NULL;
813 struct got_repository *repo = NULL;
814 struct got_reference *head_ref = NULL;
815 struct got_worktree *worktree = NULL;
816 char *repo_path = NULL;
817 char *worktree_path = NULL;
818 const char *path_prefix = "";
819 const char *branch_name = GOT_REF_HEAD;
820 char *commit_id_str = NULL;
821 int ch, same_path_prefix;
822 struct got_pathlist_head paths;
824 TAILQ_INIT(&paths);
826 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
827 switch (ch) {
828 case 'b':
829 branch_name = optarg;
830 break;
831 case 'c':
832 commit_id_str = strdup(optarg);
833 if (commit_id_str == NULL)
834 return got_error_from_errno("strdup");
835 break;
836 case 'p':
837 path_prefix = optarg;
838 break;
839 default:
840 usage_checkout();
841 /* NOTREACHED */
845 argc -= optind;
846 argv += optind;
848 #ifndef PROFILE
849 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
850 "unveil", NULL) == -1)
851 err(1, "pledge");
852 #endif
853 if (argc == 1) {
854 char *cwd, *base, *dotgit;
855 repo_path = realpath(argv[0], NULL);
856 if (repo_path == NULL)
857 return got_error_from_errno2("realpath", argv[0]);
858 cwd = getcwd(NULL, 0);
859 if (cwd == NULL) {
860 error = got_error_from_errno("getcwd");
861 goto done;
863 if (path_prefix[0]) {
864 base = basename(path_prefix);
865 if (base == NULL) {
866 error = got_error_from_errno2("basename",
867 path_prefix);
868 goto done;
870 } else {
871 base = basename(repo_path);
872 if (base == NULL) {
873 error = got_error_from_errno2("basename",
874 repo_path);
875 goto done;
878 dotgit = strstr(base, ".git");
879 if (dotgit)
880 *dotgit = '\0';
881 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
882 error = got_error_from_errno("asprintf");
883 free(cwd);
884 goto done;
886 free(cwd);
887 } else if (argc == 2) {
888 repo_path = realpath(argv[0], NULL);
889 if (repo_path == NULL) {
890 error = got_error_from_errno2("realpath", argv[0]);
891 goto done;
893 worktree_path = realpath(argv[1], NULL);
894 if (worktree_path == NULL) {
895 if (errno != ENOENT) {
896 error = got_error_from_errno2("realpath",
897 argv[1]);
898 goto done;
900 worktree_path = strdup(argv[1]);
901 if (worktree_path == NULL) {
902 error = got_error_from_errno("strdup");
903 goto done;
906 } else
907 usage_checkout();
909 got_path_strip_trailing_slashes(repo_path);
910 got_path_strip_trailing_slashes(worktree_path);
912 error = got_repo_open(&repo, repo_path);
913 if (error != NULL)
914 goto done;
916 /* Pre-create work tree path for unveil(2) */
917 error = got_path_mkdir(worktree_path);
918 if (error) {
919 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
920 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
921 goto done;
922 if (!got_path_dir_is_empty(worktree_path)) {
923 error = got_error_path(worktree_path,
924 GOT_ERR_DIR_NOT_EMPTY);
925 goto done;
929 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
930 if (error)
931 goto done;
933 error = got_ref_open(&head_ref, repo, branch_name, 0);
934 if (error != NULL)
935 goto done;
937 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
938 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
939 goto done;
941 error = got_worktree_open(&worktree, worktree_path);
942 if (error != NULL)
943 goto done;
945 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
946 path_prefix);
947 if (error != NULL)
948 goto done;
949 if (!same_path_prefix) {
950 error = got_error(GOT_ERR_PATH_PREFIX);
951 goto done;
954 if (commit_id_str) {
955 struct got_object_id *commit_id;
956 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
957 if (error)
958 goto done;
959 error = check_linear_ancestry(commit_id,
960 got_worktree_get_base_commit_id(worktree), repo);
961 if (error != NULL) {
962 free(commit_id);
963 goto done;
965 error = check_same_branch(commit_id, head_ref, NULL, repo);
966 if (error)
967 goto done;
968 error = got_worktree_set_base_commit_id(worktree, repo,
969 commit_id);
970 free(commit_id);
971 if (error)
972 goto done;
975 error = got_pathlist_append(&paths, "", NULL);
976 if (error)
977 goto done;
978 error = got_worktree_checkout_files(worktree, &paths, repo,
979 checkout_progress, worktree_path, check_cancelled, NULL);
980 if (error != NULL)
981 goto done;
983 printf("Now shut up and hack\n");
985 done:
986 got_pathlist_free(&paths);
987 free(commit_id_str);
988 free(repo_path);
989 free(worktree_path);
990 return error;
993 __dead static void
994 usage_update(void)
996 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
997 getprogname());
998 exit(1);
1001 static const struct got_error *
1002 update_progress(void *arg, unsigned char status, const char *path)
1004 int *did_something = arg;
1006 if (status == GOT_STATUS_EXISTS)
1007 return NULL;
1009 *did_something = 1;
1011 /* Base commit bump happens silently. */
1012 if (status == GOT_STATUS_BUMP_BASE)
1013 return NULL;
1015 while (path[0] == '/')
1016 path++;
1017 printf("%c %s\n", status, path);
1018 return NULL;
1021 static const struct got_error *
1022 switch_head_ref(struct got_reference *head_ref,
1023 struct got_object_id *commit_id, struct got_worktree *worktree,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1027 char *base_id_str;
1028 int ref_has_moved = 0;
1030 /* Trivial case: switching between two different references. */
1031 if (strcmp(got_ref_get_name(head_ref),
1032 got_worktree_get_head_ref_name(worktree)) != 0) {
1033 printf("Switching work tree from %s to %s\n",
1034 got_worktree_get_head_ref_name(worktree),
1035 got_ref_get_name(head_ref));
1036 return got_worktree_set_head_ref(worktree, head_ref);
1039 err = check_linear_ancestry(commit_id,
1040 got_worktree_get_base_commit_id(worktree), repo);
1041 if (err) {
1042 if (err->code != GOT_ERR_ANCESTRY)
1043 return err;
1044 ref_has_moved = 1;
1046 if (!ref_has_moved)
1047 return NULL;
1049 /* Switching to a rebased branch with the same reference name. */
1050 err = got_object_id_str(&base_id_str,
1051 got_worktree_get_base_commit_id(worktree));
1052 if (err)
1053 return err;
1054 printf("Reference %s now points at a different branch\n",
1055 got_worktree_get_head_ref_name(worktree));
1056 printf("Switching work tree from %s to %s\n", base_id_str,
1057 got_worktree_get_head_ref_name(worktree));
1058 return NULL;
1061 static const struct got_error *
1062 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1064 const struct got_error *err;
1065 int in_progress;
1067 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1068 if (err)
1069 return err;
1070 if (in_progress)
1071 return got_error(GOT_ERR_REBASING);
1073 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1074 if (err)
1075 return err;
1076 if (in_progress)
1077 return got_error(GOT_ERR_HISTEDIT_BUSY);
1079 return NULL;
1082 static const struct got_error *
1083 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1084 char *argv[], struct got_worktree *worktree)
1086 const struct got_error *err;
1087 char *path;
1088 int i;
1090 if (argc == 0) {
1091 path = strdup("");
1092 if (path == NULL)
1093 return got_error_from_errno("strdup");
1094 return got_pathlist_append(paths, path, NULL);
1097 for (i = 0; i < argc; i++) {
1098 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1099 if (err)
1100 break;
1101 err = got_pathlist_append(paths, path, NULL);
1102 if (err) {
1103 free(path);
1104 break;
1108 return err;
1111 static const struct got_error *
1112 cmd_update(int argc, char *argv[])
1114 const struct got_error *error = NULL;
1115 struct got_repository *repo = NULL;
1116 struct got_worktree *worktree = NULL;
1117 char *worktree_path = NULL;
1118 struct got_object_id *commit_id = NULL;
1119 char *commit_id_str = NULL;
1120 const char *branch_name = NULL;
1121 struct got_reference *head_ref = NULL;
1122 struct got_pathlist_head paths;
1123 struct got_pathlist_entry *pe;
1124 int ch, did_something = 0;
1126 TAILQ_INIT(&paths);
1128 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1129 switch (ch) {
1130 case 'b':
1131 branch_name = optarg;
1132 break;
1133 case 'c':
1134 commit_id_str = strdup(optarg);
1135 if (commit_id_str == NULL)
1136 return got_error_from_errno("strdup");
1137 break;
1138 default:
1139 usage_update();
1140 /* NOTREACHED */
1144 argc -= optind;
1145 argv += optind;
1147 #ifndef PROFILE
1148 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1149 "unveil", NULL) == -1)
1150 err(1, "pledge");
1151 #endif
1152 worktree_path = getcwd(NULL, 0);
1153 if (worktree_path == NULL) {
1154 error = got_error_from_errno("getcwd");
1155 goto done;
1157 error = got_worktree_open(&worktree, worktree_path);
1158 if (error)
1159 goto done;
1161 error = check_rebase_or_histedit_in_progress(worktree);
1162 if (error)
1163 goto done;
1165 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 if (error != NULL)
1167 goto done;
1169 error = apply_unveil(got_repo_get_path(repo), 0,
1170 got_worktree_get_root_path(worktree));
1171 if (error)
1172 goto done;
1174 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1175 if (error)
1176 goto done;
1178 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1179 got_worktree_get_head_ref_name(worktree), 0);
1180 if (error != NULL)
1181 goto done;
1182 if (commit_id_str == NULL) {
1183 error = got_ref_resolve(&commit_id, repo, head_ref);
1184 if (error != NULL)
1185 goto done;
1186 error = got_object_id_str(&commit_id_str, commit_id);
1187 if (error != NULL)
1188 goto done;
1189 } else {
1190 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1191 free(commit_id_str);
1192 commit_id_str = NULL;
1193 if (error)
1194 goto done;
1195 error = got_object_id_str(&commit_id_str, commit_id);
1196 if (error)
1197 goto done;
1200 if (branch_name) {
1201 struct got_object_id *head_commit_id;
1202 TAILQ_FOREACH(pe, &paths, entry) {
1203 if (pe->path_len == 0)
1204 continue;
1205 error = got_error_msg(GOT_ERR_BAD_PATH,
1206 "switching between branches requires that "
1207 "the entire work tree gets updated");
1208 goto done;
1210 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1211 if (error)
1212 goto done;
1213 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1214 free(head_commit_id);
1215 if (error != NULL)
1216 goto done;
1217 error = check_same_branch(commit_id, head_ref, NULL, repo);
1218 if (error)
1219 goto done;
1220 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1221 if (error)
1222 goto done;
1223 } else {
1224 error = check_linear_ancestry(commit_id,
1225 got_worktree_get_base_commit_id(worktree), repo);
1226 if (error != NULL) {
1227 if (error->code == GOT_ERR_ANCESTRY)
1228 error = got_error(GOT_ERR_BRANCH_MOVED);
1229 goto done;
1231 error = check_same_branch(commit_id, head_ref, NULL, repo);
1232 if (error)
1233 goto done;
1236 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1237 commit_id) != 0) {
1238 error = got_worktree_set_base_commit_id(worktree, repo,
1239 commit_id);
1240 if (error)
1241 goto done;
1244 error = got_worktree_checkout_files(worktree, &paths, repo,
1245 update_progress, &did_something, check_cancelled, NULL);
1246 if (error != NULL)
1247 goto done;
1249 if (did_something)
1250 printf("Updated to commit %s\n", commit_id_str);
1251 else
1252 printf("Already up-to-date\n");
1253 done:
1254 free(worktree_path);
1255 TAILQ_FOREACH(pe, &paths, entry)
1256 free((char *)pe->path);
1257 got_pathlist_free(&paths);
1258 free(commit_id);
1259 free(commit_id_str);
1260 return error;
1263 static const struct got_error *
1264 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1265 int diff_context, struct got_repository *repo)
1267 const struct got_error *err = NULL;
1268 struct got_tree_object *tree1 = NULL, *tree2;
1269 struct got_object_qid *qid;
1270 char *id_str1 = NULL, *id_str2;
1271 struct got_diff_blob_output_unidiff_arg arg;
1273 err = got_object_open_as_tree(&tree2, repo,
1274 got_object_commit_get_tree_id(commit));
1275 if (err)
1276 return err;
1278 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1279 if (qid != NULL) {
1280 struct got_commit_object *pcommit;
1282 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1283 if (err)
1284 return err;
1286 err = got_object_open_as_tree(&tree1, repo,
1287 got_object_commit_get_tree_id(pcommit));
1288 got_object_commit_close(pcommit);
1289 if (err)
1290 return err;
1292 err = got_object_id_str(&id_str1, qid->id);
1293 if (err)
1294 return err;
1297 err = got_object_id_str(&id_str2, id);
1298 if (err)
1299 goto done;
1301 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1302 arg.diff_context = diff_context;
1303 arg.outfile = stdout;
1304 err = got_diff_tree(tree1, tree2, "", "", repo,
1305 got_diff_blob_output_unidiff, &arg, 1);
1306 done:
1307 if (tree1)
1308 got_object_tree_close(tree1);
1309 got_object_tree_close(tree2);
1310 free(id_str1);
1311 free(id_str2);
1312 return err;
1315 static char *
1316 get_datestr(time_t *time, char *datebuf)
1318 char *p, *s = ctime_r(time, datebuf);
1319 p = strchr(s, '\n');
1320 if (p)
1321 *p = '\0';
1322 return s;
1325 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1327 static const struct got_error *
1328 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1329 struct got_repository *repo, int show_patch, int diff_context,
1330 struct got_reflist_head *refs)
1332 const struct got_error *err = NULL;
1333 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1334 char datebuf[26];
1335 time_t committer_time;
1336 const char *author, *committer;
1337 char *refs_str = NULL;
1338 struct got_reflist_entry *re;
1340 SIMPLEQ_FOREACH(re, refs, entry) {
1341 char *s;
1342 const char *name;
1343 if (got_object_id_cmp(re->id, id) != 0)
1344 continue;
1345 name = got_ref_get_name(re->ref);
1346 if (strcmp(name, GOT_REF_HEAD) == 0)
1347 continue;
1348 if (strncmp(name, "refs/", 5) == 0)
1349 name += 5;
1350 if (strncmp(name, "got/", 4) == 0)
1351 continue;
1352 if (strncmp(name, "heads/", 6) == 0)
1353 name += 6;
1354 if (strncmp(name, "remotes/", 8) == 0)
1355 name += 8;
1356 s = refs_str;
1357 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1358 name) == -1) {
1359 err = got_error_from_errno("asprintf");
1360 free(s);
1361 break;
1363 free(s);
1365 err = got_object_id_str(&id_str, id);
1366 if (err)
1367 return err;
1369 printf(GOT_COMMIT_SEP_STR);
1370 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1371 refs_str ? refs_str : "", refs_str ? ")" : "");
1372 free(id_str);
1373 id_str = NULL;
1374 free(refs_str);
1375 refs_str = NULL;
1376 printf("from: %s\n", got_object_commit_get_author(commit));
1377 committer_time = got_object_commit_get_committer_time(commit);
1378 datestr = get_datestr(&committer_time, datebuf);
1379 printf("date: %s UTC\n", datestr);
1380 author = got_object_commit_get_author(commit);
1381 committer = got_object_commit_get_committer(commit);
1382 if (strcmp(author, committer) != 0)
1383 printf("via: %s\n", committer);
1384 if (got_object_commit_get_nparents(commit) > 1) {
1385 const struct got_object_id_queue *parent_ids;
1386 struct got_object_qid *qid;
1387 int n = 1;
1388 parent_ids = got_object_commit_get_parent_ids(commit);
1389 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1390 err = got_object_id_str(&id_str, qid->id);
1391 if (err)
1392 return err;
1393 printf("parent %d: %s\n", n++, id_str);
1394 free(id_str);
1398 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1399 if (logmsg0 == NULL)
1400 return got_error_from_errno("strdup");
1402 logmsg = logmsg0;
1403 do {
1404 line = strsep(&logmsg, "\n");
1405 if (line)
1406 printf(" %s\n", line);
1407 } while (line);
1408 free(logmsg0);
1410 if (show_patch) {
1411 err = print_patch(commit, id, diff_context, repo);
1412 if (err == 0)
1413 printf("\n");
1416 if (fflush(stdout) != 0 && err == NULL)
1417 err = got_error_from_errno("fflush");
1418 return err;
1421 static const struct got_error *
1422 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1423 char *path, int show_patch, int diff_context, int limit,
1424 int first_parent_traversal, struct got_reflist_head *refs)
1426 const struct got_error *err;
1427 struct got_commit_graph *graph;
1429 err = got_commit_graph_open(&graph, root_id, path,
1430 first_parent_traversal, repo);
1431 if (err)
1432 return err;
1433 err = got_commit_graph_iter_start(graph, root_id, repo);
1434 if (err)
1435 goto done;
1436 for (;;) {
1437 struct got_commit_object *commit;
1438 struct got_object_id *id;
1440 if (sigint_received || sigpipe_received)
1441 break;
1443 err = got_commit_graph_iter_next(&id, graph);
1444 if (err) {
1445 if (err->code == GOT_ERR_ITER_COMPLETED) {
1446 err = NULL;
1447 break;
1449 if (err->code != GOT_ERR_ITER_NEED_MORE)
1450 break;
1451 err = got_commit_graph_fetch_commits(graph, 1, repo);
1452 if (err)
1453 break;
1454 else
1455 continue;
1457 if (id == NULL)
1458 break;
1460 err = got_object_open_as_commit(&commit, repo, id);
1461 if (err)
1462 break;
1463 err = print_commit(commit, id, repo, show_patch, diff_context,
1464 refs);
1465 got_object_commit_close(commit);
1466 if (err || (limit && --limit == 0))
1467 break;
1469 done:
1470 got_commit_graph_close(graph);
1471 return err;
1474 __dead static void
1475 usage_log(void)
1477 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1478 "[-r repository-path] [path]\n", getprogname());
1479 exit(1);
1482 static const struct got_error *
1483 cmd_log(int argc, char *argv[])
1485 const struct got_error *error;
1486 struct got_repository *repo = NULL;
1487 struct got_worktree *worktree = NULL;
1488 struct got_commit_object *commit = NULL;
1489 struct got_object_id *id = NULL;
1490 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1491 char *start_commit = NULL;
1492 int diff_context = 3, ch;
1493 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1494 const char *errstr;
1495 struct got_reflist_head refs;
1497 SIMPLEQ_INIT(&refs);
1499 #ifndef PROFILE
1500 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1501 NULL)
1502 == -1)
1503 err(1, "pledge");
1504 #endif
1506 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1507 switch (ch) {
1508 case 'p':
1509 show_patch = 1;
1510 break;
1511 case 'c':
1512 start_commit = optarg;
1513 break;
1514 case 'C':
1515 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1516 &errstr);
1517 if (errstr != NULL)
1518 err(1, "-C option %s", errstr);
1519 break;
1520 case 'l':
1521 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1522 if (errstr != NULL)
1523 err(1, "-l option %s", errstr);
1524 break;
1525 case 'f':
1526 first_parent_traversal = 1;
1527 break;
1528 case 'r':
1529 repo_path = realpath(optarg, NULL);
1530 if (repo_path == NULL)
1531 err(1, "-r option");
1532 got_path_strip_trailing_slashes(repo_path);
1533 break;
1534 default:
1535 usage_log();
1536 /* NOTREACHED */
1540 argc -= optind;
1541 argv += optind;
1543 cwd = getcwd(NULL, 0);
1544 if (cwd == NULL) {
1545 error = got_error_from_errno("getcwd");
1546 goto done;
1549 error = got_worktree_open(&worktree, cwd);
1550 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1551 goto done;
1552 error = NULL;
1554 if (argc == 0) {
1555 path = strdup("");
1556 if (path == NULL) {
1557 error = got_error_from_errno("strdup");
1558 goto done;
1560 } else if (argc == 1) {
1561 if (worktree) {
1562 error = got_worktree_resolve_path(&path, worktree,
1563 argv[0]);
1564 if (error)
1565 goto done;
1566 } else {
1567 path = strdup(argv[0]);
1568 if (path == NULL) {
1569 error = got_error_from_errno("strdup");
1570 goto done;
1573 } else
1574 usage_log();
1576 if (repo_path == NULL) {
1577 repo_path = worktree ?
1578 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1580 if (repo_path == NULL) {
1581 error = got_error_from_errno("strdup");
1582 goto done;
1585 error = got_repo_open(&repo, repo_path);
1586 if (error != NULL)
1587 goto done;
1589 error = apply_unveil(got_repo_get_path(repo), 1,
1590 worktree ? got_worktree_get_root_path(worktree) : NULL);
1591 if (error)
1592 goto done;
1594 if (start_commit == NULL) {
1595 struct got_reference *head_ref;
1596 error = got_ref_open(&head_ref, repo,
1597 worktree ? got_worktree_get_head_ref_name(worktree)
1598 : GOT_REF_HEAD, 0);
1599 if (error != NULL)
1600 return error;
1601 error = got_ref_resolve(&id, repo, head_ref);
1602 got_ref_close(head_ref);
1603 if (error != NULL)
1604 return error;
1605 error = got_object_open_as_commit(&commit, repo, id);
1606 } else {
1607 struct got_reference *ref;
1608 error = got_ref_open(&ref, repo, start_commit, 0);
1609 if (error == NULL) {
1610 int obj_type;
1611 error = got_ref_resolve(&id, repo, ref);
1612 got_ref_close(ref);
1613 if (error != NULL)
1614 goto done;
1615 error = got_object_get_type(&obj_type, repo, id);
1616 if (error != NULL)
1617 goto done;
1618 if (obj_type == GOT_OBJ_TYPE_TAG) {
1619 struct got_tag_object *tag;
1620 error = got_object_open_as_tag(&tag, repo, id);
1621 if (error != NULL)
1622 goto done;
1623 if (got_object_tag_get_object_type(tag) !=
1624 GOT_OBJ_TYPE_COMMIT) {
1625 got_object_tag_close(tag);
1626 error = got_error(GOT_ERR_OBJ_TYPE);
1627 goto done;
1629 free(id);
1630 id = got_object_id_dup(
1631 got_object_tag_get_object_id(tag));
1632 if (id == NULL)
1633 error = got_error_from_errno(
1634 "got_object_id_dup");
1635 got_object_tag_close(tag);
1636 if (error)
1637 goto done;
1638 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1639 error = got_error(GOT_ERR_OBJ_TYPE);
1640 goto done;
1642 error = got_object_open_as_commit(&commit, repo, id);
1643 if (error != NULL)
1644 goto done;
1646 if (commit == NULL) {
1647 error = got_repo_match_object_id_prefix(&id,
1648 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1649 if (error != NULL)
1650 return error;
1653 if (error != NULL)
1654 goto done;
1656 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1657 if (error != NULL)
1658 goto done;
1659 if (in_repo_path) {
1660 free(path);
1661 path = in_repo_path;
1664 error = got_ref_list(&refs, repo);
1665 if (error)
1666 goto done;
1668 error = print_commits(id, repo, path, show_patch,
1669 diff_context, limit, first_parent_traversal, &refs);
1670 done:
1671 free(path);
1672 free(repo_path);
1673 free(cwd);
1674 free(id);
1675 if (worktree)
1676 got_worktree_close(worktree);
1677 if (repo) {
1678 const struct got_error *repo_error;
1679 repo_error = got_repo_close(repo);
1680 if (error == NULL)
1681 error = repo_error;
1683 got_ref_list_free(&refs);
1684 return error;
1687 __dead static void
1688 usage_diff(void)
1690 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1691 "[object1 object2 | path]\n", getprogname());
1692 exit(1);
1695 struct print_diff_arg {
1696 struct got_repository *repo;
1697 struct got_worktree *worktree;
1698 int diff_context;
1699 const char *id_str;
1700 int header_shown;
1701 int diff_staged;
1704 static const struct got_error *
1705 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1706 const char *path, struct got_object_id *blob_id,
1707 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1709 struct print_diff_arg *a = arg;
1710 const struct got_error *err = NULL;
1711 struct got_blob_object *blob1 = NULL;
1712 FILE *f2 = NULL;
1713 char *abspath = NULL;
1714 struct stat sb;
1716 if (a->diff_staged) {
1717 if (staged_status != GOT_STATUS_MODIFY &&
1718 staged_status != GOT_STATUS_ADD &&
1719 staged_status != GOT_STATUS_DELETE)
1720 return NULL;
1721 } else {
1722 if (staged_status == GOT_STATUS_DELETE)
1723 return NULL;
1724 if (status != GOT_STATUS_MODIFY &&
1725 status != GOT_STATUS_ADD &&
1726 status != GOT_STATUS_DELETE &&
1727 status != GOT_STATUS_CONFLICT)
1728 return NULL;
1731 if (!a->header_shown) {
1732 printf("diff %s %s%s\n", a->id_str,
1733 got_worktree_get_root_path(a->worktree),
1734 a->diff_staged ? " (staged changes)" : "");
1735 a->header_shown = 1;
1738 if (a->diff_staged) {
1739 const char *label1 = NULL, *label2 = NULL;
1740 switch (staged_status) {
1741 case GOT_STATUS_MODIFY:
1742 label1 = path;
1743 label2 = path;
1744 break;
1745 case GOT_STATUS_ADD:
1746 label2 = path;
1747 break;
1748 case GOT_STATUS_DELETE:
1749 label1 = path;
1750 break;
1751 default:
1752 return got_error(GOT_ERR_FILE_STATUS);
1754 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1755 label1, label2, a->diff_context, a->repo, stdout);
1758 if (staged_status == GOT_STATUS_ADD ||
1759 staged_status == GOT_STATUS_MODIFY)
1760 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1761 8192);
1762 else if (status != GOT_STATUS_ADD)
1763 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1764 if (err)
1765 goto done;
1767 if (status != GOT_STATUS_DELETE) {
1768 if (asprintf(&abspath, "%s/%s",
1769 got_worktree_get_root_path(a->worktree), path) == -1) {
1770 err = got_error_from_errno("asprintf");
1771 goto done;
1774 f2 = fopen(abspath, "r");
1775 if (f2 == NULL) {
1776 err = got_error_from_errno2("fopen", abspath);
1777 goto done;
1779 if (lstat(abspath, &sb) == -1) {
1780 err = got_error_from_errno2("lstat", abspath);
1781 goto done;
1783 } else
1784 sb.st_size = 0;
1786 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1787 stdout);
1788 done:
1789 if (blob1)
1790 got_object_blob_close(blob1);
1791 if (f2 && fclose(f2) != 0 && err == NULL)
1792 err = got_error_from_errno("fclose");
1793 free(abspath);
1794 return err;
1797 static const struct got_error *
1798 cmd_diff(int argc, char *argv[])
1800 const struct got_error *error;
1801 struct got_repository *repo = NULL;
1802 struct got_worktree *worktree = NULL;
1803 char *cwd = NULL, *repo_path = NULL;
1804 struct got_object_id *id1 = NULL, *id2 = NULL;
1805 const char *id_str1 = NULL, *id_str2 = NULL;
1806 char *label1 = NULL, *label2 = NULL;
1807 int type1, type2;
1808 int diff_context = 3, diff_staged = 0, ch;
1809 const char *errstr;
1810 char *path = NULL;
1812 #ifndef PROFILE
1813 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1814 NULL) == -1)
1815 err(1, "pledge");
1816 #endif
1818 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1819 switch (ch) {
1820 case 'C':
1821 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1822 if (errstr != NULL)
1823 err(1, "-C option %s", errstr);
1824 break;
1825 case 'r':
1826 repo_path = realpath(optarg, NULL);
1827 if (repo_path == NULL)
1828 err(1, "-r option");
1829 got_path_strip_trailing_slashes(repo_path);
1830 break;
1831 case 's':
1832 diff_staged = 1;
1833 break;
1834 default:
1835 usage_diff();
1836 /* NOTREACHED */
1840 argc -= optind;
1841 argv += optind;
1843 cwd = getcwd(NULL, 0);
1844 if (cwd == NULL) {
1845 error = got_error_from_errno("getcwd");
1846 goto done;
1848 error = got_worktree_open(&worktree, cwd);
1849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1850 goto done;
1851 if (argc <= 1) {
1852 if (worktree == NULL) {
1853 error = got_error(GOT_ERR_NOT_WORKTREE);
1854 goto done;
1856 if (repo_path)
1857 errx(1,
1858 "-r option can't be used when diffing a work tree");
1859 repo_path = strdup(got_worktree_get_repo_path(worktree));
1860 if (repo_path == NULL) {
1861 error = got_error_from_errno("strdup");
1862 goto done;
1864 if (argc == 1) {
1865 error = got_worktree_resolve_path(&path, worktree,
1866 argv[0]);
1867 if (error)
1868 goto done;
1869 } else {
1870 path = strdup("");
1871 if (path == NULL) {
1872 error = got_error_from_errno("strdup");
1873 goto done;
1876 } else if (argc == 2) {
1877 if (diff_staged)
1878 errx(1, "-s option can't be used when diffing "
1879 "objects in repository");
1880 id_str1 = argv[0];
1881 id_str2 = argv[1];
1882 if (worktree && repo_path == NULL) {
1883 repo_path =
1884 strdup(got_worktree_get_repo_path(worktree));
1885 if (repo_path == NULL) {
1886 error = got_error_from_errno("strdup");
1887 goto done;
1890 } else
1891 usage_diff();
1893 if (repo_path == NULL) {
1894 repo_path = getcwd(NULL, 0);
1895 if (repo_path == NULL)
1896 return got_error_from_errno("getcwd");
1899 error = got_repo_open(&repo, repo_path);
1900 free(repo_path);
1901 if (error != NULL)
1902 goto done;
1904 error = apply_unveil(got_repo_get_path(repo), 1,
1905 worktree ? got_worktree_get_root_path(worktree) : NULL);
1906 if (error)
1907 goto done;
1909 if (argc <= 1) {
1910 struct print_diff_arg arg;
1911 struct got_pathlist_head paths;
1912 char *id_str;
1914 TAILQ_INIT(&paths);
1916 error = got_object_id_str(&id_str,
1917 got_worktree_get_base_commit_id(worktree));
1918 if (error)
1919 goto done;
1920 arg.repo = repo;
1921 arg.worktree = worktree;
1922 arg.diff_context = diff_context;
1923 arg.id_str = id_str;
1924 arg.header_shown = 0;
1925 arg.diff_staged = diff_staged;
1927 error = got_pathlist_append(&paths, path, NULL);
1928 if (error)
1929 goto done;
1931 error = got_worktree_status(worktree, &paths, repo, print_diff,
1932 &arg, check_cancelled, NULL);
1933 free(id_str);
1934 got_pathlist_free(&paths);
1935 goto done;
1938 error = got_repo_match_object_id_prefix(&id1, id_str1,
1939 GOT_OBJ_TYPE_ANY, repo);
1940 if (error) {
1941 struct got_reference *ref;
1942 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1943 goto done;
1944 error = got_ref_open(&ref, repo, id_str1, 0);
1945 if (error != NULL)
1946 goto done;
1947 label1 = strdup(got_ref_get_name(ref));
1948 if (label1 == NULL) {
1949 error = got_error_from_errno("strdup");
1950 goto done;
1952 error = got_ref_resolve(&id1, repo, ref);
1953 got_ref_close(ref);
1954 if (error != NULL)
1955 goto done;
1956 } else {
1957 error = got_object_id_str(&label1, id1);
1958 if (label1 == NULL) {
1959 error = got_error_from_errno("strdup");
1960 goto done;
1964 error = got_repo_match_object_id_prefix(&id2, id_str2,
1965 GOT_OBJ_TYPE_ANY, repo);
1966 if (error) {
1967 struct got_reference *ref;
1968 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1969 goto done;
1970 error = got_ref_open(&ref, repo, id_str2, 0);
1971 if (error != NULL)
1972 goto done;
1973 label2 = strdup(got_ref_get_name(ref));
1974 if (label2 == NULL) {
1975 error = got_error_from_errno("strdup");
1976 goto done;
1978 error = got_ref_resolve(&id2, repo, ref);
1979 got_ref_close(ref);
1980 if (error != NULL)
1981 goto done;
1982 } else {
1983 error = got_object_id_str(&label2, id2);
1984 if (label2 == NULL) {
1985 error = got_error_from_errno("strdup");
1986 goto done;
1990 error = got_object_get_type(&type1, repo, id1);
1991 if (error)
1992 goto done;
1994 error = got_object_get_type(&type2, repo, id2);
1995 if (error)
1996 goto done;
1998 if (type1 != type2) {
1999 error = got_error(GOT_ERR_OBJ_TYPE);
2000 goto done;
2003 switch (type1) {
2004 case GOT_OBJ_TYPE_BLOB:
2005 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2006 diff_context, repo, stdout);
2007 break;
2008 case GOT_OBJ_TYPE_TREE:
2009 error = got_diff_objects_as_trees(id1, id2, "", "",
2010 diff_context, repo, stdout);
2011 break;
2012 case GOT_OBJ_TYPE_COMMIT:
2013 printf("diff %s %s\n", label1, label2);
2014 error = got_diff_objects_as_commits(id1, id2, diff_context,
2015 repo, stdout);
2016 break;
2017 default:
2018 error = got_error(GOT_ERR_OBJ_TYPE);
2021 done:
2022 free(label1);
2023 free(label2);
2024 free(id1);
2025 free(id2);
2026 free(path);
2027 if (worktree)
2028 got_worktree_close(worktree);
2029 if (repo) {
2030 const struct got_error *repo_error;
2031 repo_error = got_repo_close(repo);
2032 if (error == NULL)
2033 error = repo_error;
2035 return error;
2038 __dead static void
2039 usage_blame(void)
2041 fprintf(stderr,
2042 "usage: %s blame [-c commit] [-r repository-path] path\n",
2043 getprogname());
2044 exit(1);
2047 static const struct got_error *
2048 cmd_blame(int argc, char *argv[])
2050 const struct got_error *error;
2051 struct got_repository *repo = NULL;
2052 struct got_worktree *worktree = NULL;
2053 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2054 struct got_object_id *commit_id = NULL;
2055 char *commit_id_str = NULL;
2056 int ch;
2058 #ifndef PROFILE
2059 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2060 NULL) == -1)
2061 err(1, "pledge");
2062 #endif
2064 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2065 switch (ch) {
2066 case 'c':
2067 commit_id_str = optarg;
2068 break;
2069 case 'r':
2070 repo_path = realpath(optarg, NULL);
2071 if (repo_path == NULL)
2072 err(1, "-r option");
2073 got_path_strip_trailing_slashes(repo_path);
2074 break;
2075 default:
2076 usage_blame();
2077 /* NOTREACHED */
2081 argc -= optind;
2082 argv += optind;
2084 if (argc == 1)
2085 path = argv[0];
2086 else
2087 usage_blame();
2089 cwd = getcwd(NULL, 0);
2090 if (cwd == NULL) {
2091 error = got_error_from_errno("getcwd");
2092 goto done;
2094 if (repo_path == NULL) {
2095 error = got_worktree_open(&worktree, cwd);
2096 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2097 goto done;
2098 else
2099 error = NULL;
2100 if (worktree) {
2101 repo_path =
2102 strdup(got_worktree_get_repo_path(worktree));
2103 if (repo_path == NULL)
2104 error = got_error_from_errno("strdup");
2105 if (error)
2106 goto done;
2107 } else {
2108 repo_path = strdup(cwd);
2109 if (repo_path == NULL) {
2110 error = got_error_from_errno("strdup");
2111 goto done;
2116 error = got_repo_open(&repo, repo_path);
2117 if (error != NULL)
2118 goto done;
2120 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2121 if (error)
2122 goto done;
2124 if (worktree) {
2125 const char *prefix = got_worktree_get_path_prefix(worktree);
2126 char *p, *worktree_subdir = cwd +
2127 strlen(got_worktree_get_root_path(worktree));
2128 if (asprintf(&p, "%s%s%s%s%s",
2129 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2130 worktree_subdir, worktree_subdir[0] ? "/" : "",
2131 path) == -1) {
2132 error = got_error_from_errno("asprintf");
2133 goto done;
2135 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2136 free(p);
2137 } else {
2138 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2140 if (error)
2141 goto done;
2143 if (commit_id_str == NULL) {
2144 struct got_reference *head_ref;
2145 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2146 if (error != NULL)
2147 goto done;
2148 error = got_ref_resolve(&commit_id, repo, head_ref);
2149 got_ref_close(head_ref);
2150 if (error != NULL)
2151 goto done;
2152 } else {
2153 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2154 if (error)
2155 goto done;
2158 error = got_blame(in_repo_path, commit_id, repo, stdout);
2159 done:
2160 free(in_repo_path);
2161 free(repo_path);
2162 free(cwd);
2163 free(commit_id);
2164 if (worktree)
2165 got_worktree_close(worktree);
2166 if (repo) {
2167 const struct got_error *repo_error;
2168 repo_error = got_repo_close(repo);
2169 if (error == NULL)
2170 error = repo_error;
2172 return error;
2175 __dead static void
2176 usage_tree(void)
2178 fprintf(stderr,
2179 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2180 getprogname());
2181 exit(1);
2184 static void
2185 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2186 const char *root_path)
2188 int is_root_path = (strcmp(path, root_path) == 0);
2190 path += strlen(root_path);
2191 while (path[0] == '/')
2192 path++;
2194 printf("%s%s%s%s%s\n", id ? id : "", path,
2195 is_root_path ? "" : "/", te->name,
2196 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2199 static const struct got_error *
2200 print_tree(const char *path, struct got_object_id *commit_id,
2201 int show_ids, int recurse, const char *root_path,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 struct got_object_id *tree_id = NULL;
2206 struct got_tree_object *tree = NULL;
2207 const struct got_tree_entries *entries;
2208 struct got_tree_entry *te;
2210 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2211 if (err)
2212 goto done;
2214 err = got_object_open_as_tree(&tree, repo, tree_id);
2215 if (err)
2216 goto done;
2217 entries = got_object_tree_get_entries(tree);
2218 te = SIMPLEQ_FIRST(&entries->head);
2219 while (te) {
2220 char *id = NULL;
2222 if (sigint_received || sigpipe_received)
2223 break;
2225 if (show_ids) {
2226 char *id_str;
2227 err = got_object_id_str(&id_str, te->id);
2228 if (err)
2229 goto done;
2230 if (asprintf(&id, "%s ", id_str) == -1) {
2231 err = got_error_from_errno("asprintf");
2232 free(id_str);
2233 goto done;
2235 free(id_str);
2237 print_entry(te, id, path, root_path);
2238 free(id);
2240 if (recurse && S_ISDIR(te->mode)) {
2241 char *child_path;
2242 if (asprintf(&child_path, "%s%s%s", path,
2243 path[0] == '/' && path[1] == '\0' ? "" : "/",
2244 te->name) == -1) {
2245 err = got_error_from_errno("asprintf");
2246 goto done;
2248 err = print_tree(child_path, commit_id, show_ids, 1,
2249 root_path, repo);
2250 free(child_path);
2251 if (err)
2252 goto done;
2255 te = SIMPLEQ_NEXT(te, entry);
2257 done:
2258 if (tree)
2259 got_object_tree_close(tree);
2260 free(tree_id);
2261 return err;
2264 static const struct got_error *
2265 cmd_tree(int argc, char *argv[])
2267 const struct got_error *error;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2270 const char *path;
2271 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2272 struct got_object_id *commit_id = NULL;
2273 char *commit_id_str = NULL;
2274 int show_ids = 0, recurse = 0;
2275 int ch;
2277 #ifndef PROFILE
2278 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2279 NULL) == -1)
2280 err(1, "pledge");
2281 #endif
2283 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2284 switch (ch) {
2285 case 'c':
2286 commit_id_str = optarg;
2287 break;
2288 case 'r':
2289 repo_path = realpath(optarg, NULL);
2290 if (repo_path == NULL)
2291 err(1, "-r option");
2292 got_path_strip_trailing_slashes(repo_path);
2293 break;
2294 case 'i':
2295 show_ids = 1;
2296 break;
2297 case 'R':
2298 recurse = 1;
2299 break;
2300 default:
2301 usage_tree();
2302 /* NOTREACHED */
2306 argc -= optind;
2307 argv += optind;
2309 if (argc == 1)
2310 path = argv[0];
2311 else if (argc > 1)
2312 usage_tree();
2313 else
2314 path = NULL;
2316 cwd = getcwd(NULL, 0);
2317 if (cwd == NULL) {
2318 error = got_error_from_errno("getcwd");
2319 goto done;
2321 if (repo_path == NULL) {
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2324 goto done;
2325 else
2326 error = NULL;
2327 if (worktree) {
2328 repo_path =
2329 strdup(got_worktree_get_repo_path(worktree));
2330 if (repo_path == NULL)
2331 error = got_error_from_errno("strdup");
2332 if (error)
2333 goto done;
2334 } else {
2335 repo_path = strdup(cwd);
2336 if (repo_path == NULL) {
2337 error = got_error_from_errno("strdup");
2338 goto done;
2343 error = got_repo_open(&repo, repo_path);
2344 if (error != NULL)
2345 goto done;
2347 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2348 if (error)
2349 goto done;
2351 if (path == NULL) {
2352 if (worktree) {
2353 char *p, *worktree_subdir = cwd +
2354 strlen(got_worktree_get_root_path(worktree));
2355 if (asprintf(&p, "%s/%s",
2356 got_worktree_get_path_prefix(worktree),
2357 worktree_subdir) == -1) {
2358 error = got_error_from_errno("asprintf");
2359 goto done;
2361 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2362 free(p);
2363 if (error)
2364 goto done;
2365 } else
2366 path = "/";
2368 if (in_repo_path == NULL) {
2369 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2370 if (error != NULL)
2371 goto done;
2374 if (commit_id_str == NULL) {
2375 struct got_reference *head_ref;
2376 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2377 if (error != NULL)
2378 goto done;
2379 error = got_ref_resolve(&commit_id, repo, head_ref);
2380 got_ref_close(head_ref);
2381 if (error != NULL)
2382 goto done;
2383 } else {
2384 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2385 if (error)
2386 goto done;
2389 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2390 in_repo_path, repo);
2391 done:
2392 free(in_repo_path);
2393 free(repo_path);
2394 free(cwd);
2395 free(commit_id);
2396 if (worktree)
2397 got_worktree_close(worktree);
2398 if (repo) {
2399 const struct got_error *repo_error;
2400 repo_error = got_repo_close(repo);
2401 if (error == NULL)
2402 error = repo_error;
2404 return error;
2407 __dead static void
2408 usage_status(void)
2410 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2411 exit(1);
2414 static const struct got_error *
2415 print_status(void *arg, unsigned char status, unsigned char staged_status,
2416 const char *path, struct got_object_id *blob_id,
2417 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2419 if (status == staged_status && (status == GOT_STATUS_DELETE))
2420 status = GOT_STATUS_NO_CHANGE;
2421 printf("%c%c %s\n", status, staged_status, path);
2422 return NULL;
2425 static const struct got_error *
2426 cmd_status(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_repository *repo = NULL;
2430 struct got_worktree *worktree = NULL;
2431 char *cwd = NULL;
2432 struct got_pathlist_head paths;
2433 struct got_pathlist_entry *pe;
2434 int ch;
2436 TAILQ_INIT(&paths);
2438 while ((ch = getopt(argc, argv, "")) != -1) {
2439 switch (ch) {
2440 default:
2441 usage_status();
2442 /* NOTREACHED */
2446 argc -= optind;
2447 argv += optind;
2449 #ifndef PROFILE
2450 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2451 NULL) == -1)
2452 err(1, "pledge");
2453 #endif
2454 cwd = getcwd(NULL, 0);
2455 if (cwd == NULL) {
2456 error = got_error_from_errno("getcwd");
2457 goto done;
2460 error = got_worktree_open(&worktree, cwd);
2461 if (error != NULL)
2462 goto done;
2464 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2465 if (error != NULL)
2466 goto done;
2468 error = apply_unveil(got_repo_get_path(repo), 1,
2469 got_worktree_get_root_path(worktree));
2470 if (error)
2471 goto done;
2473 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2474 if (error)
2475 goto done;
2477 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2478 check_cancelled, NULL);
2479 done:
2480 TAILQ_FOREACH(pe, &paths, entry)
2481 free((char *)pe->path);
2482 got_pathlist_free(&paths);
2483 free(cwd);
2484 return error;
2487 __dead static void
2488 usage_ref(void)
2490 fprintf(stderr,
2491 "usage: %s ref [-r repository] -l | -d name | name target\n",
2492 getprogname());
2493 exit(1);
2496 static const struct got_error *
2497 list_refs(struct got_repository *repo)
2499 static const struct got_error *err = NULL;
2500 struct got_reflist_head refs;
2501 struct got_reflist_entry *re;
2503 SIMPLEQ_INIT(&refs);
2504 err = got_ref_list(&refs, repo);
2505 if (err)
2506 return err;
2508 SIMPLEQ_FOREACH(re, &refs, entry) {
2509 char *refstr;
2510 refstr = got_ref_to_str(re->ref);
2511 if (refstr == NULL)
2512 return got_error_from_errno("got_ref_to_str");
2513 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2514 free(refstr);
2517 got_ref_list_free(&refs);
2518 return NULL;
2521 static const struct got_error *
2522 delete_ref(struct got_repository *repo, const char *refname)
2524 const struct got_error *err = NULL;
2525 struct got_reference *ref;
2527 err = got_ref_open(&ref, repo, refname, 0);
2528 if (err)
2529 return err;
2531 err = got_ref_delete(ref, repo);
2532 got_ref_close(ref);
2533 return err;
2536 static const struct got_error *
2537 add_ref(struct got_repository *repo, const char *refname, const char *target)
2539 const struct got_error *err = NULL;
2540 struct got_object_id *id;
2541 struct got_reference *ref = NULL;
2544 * Don't let the user create a reference named '-'.
2545 * While technically a valid reference name, this case is usually
2546 * an unintended typo.
2548 if (refname[0] == '-' && refname[1] == '\0')
2549 return got_error(GOT_ERR_BAD_REF_NAME);
2551 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2552 repo);
2553 if (err) {
2554 struct got_reference *target_ref;
2556 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2557 return err;
2558 err = got_ref_open(&target_ref, repo, target, 0);
2559 if (err)
2560 return err;
2561 err = got_ref_resolve(&id, repo, target_ref);
2562 got_ref_close(target_ref);
2563 if (err)
2564 return err;
2567 err = got_ref_alloc(&ref, refname, id);
2568 if (err)
2569 goto done;
2571 err = got_ref_write(ref, repo);
2572 done:
2573 if (ref)
2574 got_ref_close(ref);
2575 free(id);
2576 return err;
2579 static const struct got_error *
2580 cmd_ref(int argc, char *argv[])
2582 const struct got_error *error = NULL;
2583 struct got_repository *repo = NULL;
2584 struct got_worktree *worktree = NULL;
2585 char *cwd = NULL, *repo_path = NULL;
2586 int ch, do_list = 0;
2587 const char *delref = NULL;
2589 /* TODO: Add -s option for adding symbolic references. */
2590 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2591 switch (ch) {
2592 case 'd':
2593 delref = optarg;
2594 break;
2595 case 'r':
2596 repo_path = realpath(optarg, NULL);
2597 if (repo_path == NULL)
2598 err(1, "-r option");
2599 got_path_strip_trailing_slashes(repo_path);
2600 break;
2601 case 'l':
2602 do_list = 1;
2603 break;
2604 default:
2605 usage_ref();
2606 /* NOTREACHED */
2610 if (do_list && delref)
2611 errx(1, "-l and -d options are mutually exclusive\n");
2613 argc -= optind;
2614 argv += optind;
2616 if (do_list || delref) {
2617 if (argc > 0)
2618 usage_ref();
2619 } else if (argc != 2)
2620 usage_ref();
2622 #ifndef PROFILE
2623 if (do_list) {
2624 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2625 NULL) == -1)
2626 err(1, "pledge");
2627 } else {
2628 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2629 "sendfd unveil", NULL) == -1)
2630 err(1, "pledge");
2632 #endif
2633 cwd = getcwd(NULL, 0);
2634 if (cwd == NULL) {
2635 error = got_error_from_errno("getcwd");
2636 goto done;
2639 if (repo_path == NULL) {
2640 error = got_worktree_open(&worktree, cwd);
2641 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2642 goto done;
2643 else
2644 error = NULL;
2645 if (worktree) {
2646 repo_path =
2647 strdup(got_worktree_get_repo_path(worktree));
2648 if (repo_path == NULL)
2649 error = got_error_from_errno("strdup");
2650 if (error)
2651 goto done;
2652 } else {
2653 repo_path = strdup(cwd);
2654 if (repo_path == NULL) {
2655 error = got_error_from_errno("strdup");
2656 goto done;
2661 error = got_repo_open(&repo, repo_path);
2662 if (error != NULL)
2663 goto done;
2665 error = apply_unveil(got_repo_get_path(repo), do_list,
2666 worktree ? got_worktree_get_root_path(worktree) : NULL);
2667 if (error)
2668 goto done;
2670 if (do_list)
2671 error = list_refs(repo);
2672 else if (delref)
2673 error = delete_ref(repo, delref);
2674 else
2675 error = add_ref(repo, argv[0], argv[1]);
2676 done:
2677 if (repo)
2678 got_repo_close(repo);
2679 if (worktree)
2680 got_worktree_close(worktree);
2681 free(cwd);
2682 free(repo_path);
2683 return error;
2686 __dead static void
2687 usage_branch(void)
2689 fprintf(stderr,
2690 "usage: %s branch [-r repository] -l | -d name | "
2691 "name [base-branch]\n", getprogname());
2692 exit(1);
2695 static const struct got_error *
2696 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2698 static const struct got_error *err = NULL;
2699 struct got_reflist_head refs;
2700 struct got_reflist_entry *re;
2702 SIMPLEQ_INIT(&refs);
2704 err = got_ref_list(&refs, repo);
2705 if (err)
2706 return err;
2708 SIMPLEQ_FOREACH(re, &refs, entry) {
2709 const char *refname, *marker = " ";
2710 char *refstr;
2711 refname = got_ref_get_name(re->ref);
2712 if (strncmp(refname, "refs/heads/", 11) != 0)
2713 continue;
2714 if (worktree && strcmp(refname,
2715 got_worktree_get_head_ref_name(worktree)) == 0) {
2716 struct got_object_id *id = NULL;
2717 err = got_ref_resolve(&id, repo, re->ref);
2718 if (err)
2719 return err;
2720 if (got_object_id_cmp(id,
2721 got_worktree_get_base_commit_id(worktree)) == 0)
2722 marker = "* ";
2723 else
2724 marker = "~ ";
2725 free(id);
2727 refname += 11;
2728 refstr = got_ref_to_str(re->ref);
2729 if (refstr == NULL)
2730 return got_error_from_errno("got_ref_to_str");
2731 printf("%s%s: %s\n", marker, refname, refstr);
2732 free(refstr);
2735 got_ref_list_free(&refs);
2736 return NULL;
2739 static const struct got_error *
2740 delete_branch(struct got_repository *repo, const char *branch_name)
2742 const struct got_error *err = NULL;
2743 struct got_reference *ref;
2744 char *refname;
2746 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2747 return got_error_from_errno("asprintf");
2749 err = got_ref_open(&ref, repo, refname, 0);
2750 if (err)
2751 goto done;
2753 err = got_ref_delete(ref, repo);
2754 got_ref_close(ref);
2755 done:
2756 free(refname);
2757 return err;
2760 static const struct got_error *
2761 add_branch(struct got_repository *repo, const char *branch_name,
2762 const char *base_branch)
2764 const struct got_error *err = NULL;
2765 struct got_object_id *id = NULL;
2766 struct got_reference *ref = NULL;
2767 char *base_refname = NULL, *refname = NULL;
2768 struct got_reference *base_ref;
2771 * Don't let the user create a branch named '-'.
2772 * While technically a valid reference name, this case is usually
2773 * an unintended typo.
2775 if (branch_name[0] == '-' && branch_name[1] == '\0')
2776 return got_error(GOT_ERR_BAD_REF_NAME);
2778 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2779 base_refname = strdup(GOT_REF_HEAD);
2780 if (base_refname == NULL)
2781 return got_error_from_errno("strdup");
2782 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2783 return got_error_from_errno("asprintf");
2785 err = got_ref_open(&base_ref, repo, base_refname, 0);
2786 if (err)
2787 goto done;
2788 err = got_ref_resolve(&id, repo, base_ref);
2789 got_ref_close(base_ref);
2790 if (err)
2791 goto done;
2793 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2794 err = got_error_from_errno("asprintf");
2795 goto done;
2798 err = got_ref_open(&ref, repo, refname, 0);
2799 if (err == NULL) {
2800 err = got_error(GOT_ERR_BRANCH_EXISTS);
2801 goto done;
2802 } else if (err->code != GOT_ERR_NOT_REF)
2803 goto done;
2805 err = got_ref_alloc(&ref, refname, id);
2806 if (err)
2807 goto done;
2809 err = got_ref_write(ref, repo);
2810 done:
2811 if (ref)
2812 got_ref_close(ref);
2813 free(id);
2814 free(base_refname);
2815 free(refname);
2816 return err;
2819 static const struct got_error *
2820 cmd_branch(int argc, char *argv[])
2822 const struct got_error *error = NULL;
2823 struct got_repository *repo = NULL;
2824 struct got_worktree *worktree = NULL;
2825 char *cwd = NULL, *repo_path = NULL;
2826 int ch, do_list = 0;
2827 const char *delref = NULL;
2829 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2830 switch (ch) {
2831 case 'd':
2832 delref = optarg;
2833 break;
2834 case 'r':
2835 repo_path = realpath(optarg, NULL);
2836 if (repo_path == NULL)
2837 err(1, "-r option");
2838 got_path_strip_trailing_slashes(repo_path);
2839 break;
2840 case 'l':
2841 do_list = 1;
2842 break;
2843 default:
2844 usage_branch();
2845 /* NOTREACHED */
2849 if (do_list && delref)
2850 errx(1, "-l and -d options are mutually exclusive\n");
2852 argc -= optind;
2853 argv += optind;
2855 if (do_list || delref) {
2856 if (argc > 0)
2857 usage_branch();
2858 } else if (argc < 1 || argc > 2)
2859 usage_branch();
2861 #ifndef PROFILE
2862 if (do_list) {
2863 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2864 NULL) == -1)
2865 err(1, "pledge");
2866 } else {
2867 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2868 "sendfd unveil", NULL) == -1)
2869 err(1, "pledge");
2871 #endif
2872 cwd = getcwd(NULL, 0);
2873 if (cwd == NULL) {
2874 error = got_error_from_errno("getcwd");
2875 goto done;
2878 if (repo_path == NULL) {
2879 error = got_worktree_open(&worktree, cwd);
2880 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2881 goto done;
2882 else
2883 error = NULL;
2884 if (worktree) {
2885 repo_path =
2886 strdup(got_worktree_get_repo_path(worktree));
2887 if (repo_path == NULL)
2888 error = got_error_from_errno("strdup");
2889 if (error)
2890 goto done;
2891 } else {
2892 repo_path = strdup(cwd);
2893 if (repo_path == NULL) {
2894 error = got_error_from_errno("strdup");
2895 goto done;
2900 error = got_repo_open(&repo, repo_path);
2901 if (error != NULL)
2902 goto done;
2904 error = apply_unveil(got_repo_get_path(repo), do_list,
2905 worktree ? got_worktree_get_root_path(worktree) : NULL);
2906 if (error)
2907 goto done;
2909 if (do_list)
2910 error = list_branches(repo, worktree);
2911 else if (delref)
2912 error = delete_branch(repo, delref);
2913 else {
2914 const char *base_branch;
2915 if (argc == 1) {
2916 base_branch = worktree ?
2917 got_worktree_get_head_ref_name(worktree) :
2918 GOT_REF_HEAD;
2919 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2920 base_branch += 11;
2921 } else
2922 base_branch = argv[1];
2923 error = add_branch(repo, argv[0], base_branch);
2925 done:
2926 if (repo)
2927 got_repo_close(repo);
2928 if (worktree)
2929 got_worktree_close(worktree);
2930 free(cwd);
2931 free(repo_path);
2932 return error;
2935 __dead static void
2936 usage_add(void)
2938 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2939 exit(1);
2942 static const struct got_error *
2943 cmd_add(int argc, char *argv[])
2945 const struct got_error *error = NULL;
2946 struct got_repository *repo = NULL;
2947 struct got_worktree *worktree = NULL;
2948 char *cwd = NULL;
2949 struct got_pathlist_head paths;
2950 struct got_pathlist_entry *pe;
2951 int ch;
2953 TAILQ_INIT(&paths);
2955 while ((ch = getopt(argc, argv, "")) != -1) {
2956 switch (ch) {
2957 default:
2958 usage_add();
2959 /* NOTREACHED */
2963 argc -= optind;
2964 argv += optind;
2966 #ifndef PROFILE
2967 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2968 NULL) == -1)
2969 err(1, "pledge");
2970 #endif
2971 if (argc < 1)
2972 usage_add();
2974 cwd = getcwd(NULL, 0);
2975 if (cwd == NULL) {
2976 error = got_error_from_errno("getcwd");
2977 goto done;
2980 error = got_worktree_open(&worktree, cwd);
2981 if (error)
2982 goto done;
2984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2985 if (error != NULL)
2986 goto done;
2988 error = apply_unveil(got_repo_get_path(repo), 1,
2989 got_worktree_get_root_path(worktree));
2990 if (error)
2991 goto done;
2993 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2994 if (error)
2995 goto done;
2997 error = got_worktree_schedule_add(worktree, &paths, print_status,
2998 NULL, repo);
2999 done:
3000 if (repo)
3001 got_repo_close(repo);
3002 if (worktree)
3003 got_worktree_close(worktree);
3004 TAILQ_FOREACH(pe, &paths, entry)
3005 free((char *)pe->path);
3006 got_pathlist_free(&paths);
3007 free(cwd);
3008 return error;
3011 __dead static void
3012 usage_remove(void)
3014 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3015 exit(1);
3018 static const struct got_error *
3019 cmd_remove(int argc, char *argv[])
3021 const struct got_error *error = NULL;
3022 struct got_worktree *worktree = NULL;
3023 struct got_repository *repo = NULL;
3024 char *cwd = NULL;
3025 struct got_pathlist_head paths;
3026 struct got_pathlist_entry *pe;
3027 int ch, delete_local_mods = 0;
3029 TAILQ_INIT(&paths);
3031 while ((ch = getopt(argc, argv, "f")) != -1) {
3032 switch (ch) {
3033 case 'f':
3034 delete_local_mods = 1;
3035 break;
3036 default:
3037 usage_add();
3038 /* NOTREACHED */
3042 argc -= optind;
3043 argv += optind;
3045 #ifndef PROFILE
3046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3047 NULL) == -1)
3048 err(1, "pledge");
3049 #endif
3050 if (argc < 1)
3051 usage_remove();
3053 cwd = getcwd(NULL, 0);
3054 if (cwd == NULL) {
3055 error = got_error_from_errno("getcwd");
3056 goto done;
3058 error = got_worktree_open(&worktree, cwd);
3059 if (error)
3060 goto done;
3062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3063 if (error)
3064 goto done;
3066 error = apply_unveil(got_repo_get_path(repo), 1,
3067 got_worktree_get_root_path(worktree));
3068 if (error)
3069 goto done;
3071 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3072 if (error)
3073 goto done;
3075 error = got_worktree_schedule_delete(worktree, &paths,
3076 delete_local_mods, print_status, NULL, repo);
3077 if (error)
3078 goto done;
3079 done:
3080 if (repo)
3081 got_repo_close(repo);
3082 if (worktree)
3083 got_worktree_close(worktree);
3084 TAILQ_FOREACH(pe, &paths, entry)
3085 free((char *)pe->path);
3086 got_pathlist_free(&paths);
3087 free(cwd);
3088 return error;
3091 __dead static void
3092 usage_revert(void)
3094 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3095 exit(1);
3098 static const struct got_error *
3099 revert_progress(void *arg, unsigned char status, const char *path)
3101 while (path[0] == '/')
3102 path++;
3103 printf("%c %s\n", status, path);
3104 return NULL;
3107 static const struct got_error *
3108 cmd_revert(int argc, char *argv[])
3110 const struct got_error *error = NULL;
3111 struct got_worktree *worktree = NULL;
3112 struct got_repository *repo = NULL;
3113 char *cwd = NULL, *path = NULL;
3114 struct got_pathlist_head paths;
3115 int ch;
3117 TAILQ_INIT(&paths);
3119 while ((ch = getopt(argc, argv, "")) != -1) {
3120 switch (ch) {
3121 default:
3122 usage_revert();
3123 /* NOTREACHED */
3127 argc -= optind;
3128 argv += optind;
3130 #ifndef PROFILE
3131 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3132 "unveil", NULL) == -1)
3133 err(1, "pledge");
3134 #endif
3135 if (argc < 1)
3136 usage_revert();
3138 cwd = getcwd(NULL, 0);
3139 if (cwd == NULL) {
3140 error = got_error_from_errno("getcwd");
3141 goto done;
3143 error = got_worktree_open(&worktree, cwd);
3144 if (error)
3145 goto done;
3147 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3148 if (error != NULL)
3149 goto done;
3151 error = apply_unveil(got_repo_get_path(repo), 1,
3152 got_worktree_get_root_path(worktree));
3153 if (error)
3154 goto done;
3156 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3157 if (error)
3158 goto done;
3160 error = got_worktree_revert(worktree, &paths,
3161 revert_progress, NULL, repo);
3162 if (error)
3163 goto done;
3164 done:
3165 if (repo)
3166 got_repo_close(repo);
3167 if (worktree)
3168 got_worktree_close(worktree);
3169 free(path);
3170 free(cwd);
3171 return error;
3174 __dead static void
3175 usage_commit(void)
3177 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3178 getprogname());
3179 exit(1);
3182 struct collect_commit_logmsg_arg {
3183 const char *cmdline_log;
3184 const char *editor;
3185 const char *worktree_path;
3186 const char *branch_name;
3187 const char *repo_path;
3188 char *logmsg_path;
3192 static const struct got_error *
3193 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3194 void *arg)
3196 char *initial_content = NULL;
3197 struct got_pathlist_entry *pe;
3198 const struct got_error *err = NULL;
3199 char *template = NULL;
3200 struct collect_commit_logmsg_arg *a = arg;
3201 int fd;
3202 size_t len;
3204 /* if a message was specified on the command line, just use it */
3205 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3206 len = strlen(a->cmdline_log) + 1;
3207 *logmsg = malloc(len + 1);
3208 if (*logmsg == NULL)
3209 return got_error_from_errno("malloc");
3210 strlcpy(*logmsg, a->cmdline_log, len);
3211 return NULL;
3214 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3215 return got_error_from_errno("asprintf");
3217 if (asprintf(&initial_content,
3218 "\n# changes to be committed on branch %s:\n",
3219 a->branch_name) == -1)
3220 return got_error_from_errno("asprintf");
3222 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3223 if (err)
3224 goto done;
3226 dprintf(fd, initial_content);
3228 TAILQ_FOREACH(pe, commitable_paths, entry) {
3229 struct got_commitable *ct = pe->data;
3230 dprintf(fd, "# %c %s\n",
3231 got_commitable_get_status(ct),
3232 got_commitable_get_path(ct));
3234 close(fd);
3236 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3237 done:
3238 unlink(a->logmsg_path);
3239 free(a->logmsg_path);
3240 free(initial_content);
3241 free(template);
3243 /* Editor is done; we can now apply unveil(2) */
3244 if (err == NULL) {
3245 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3246 if (err) {
3247 free(*logmsg);
3248 *logmsg = NULL;
3251 return err;
3254 static const struct got_error *
3255 cmd_commit(int argc, char *argv[])
3257 const struct got_error *error = NULL;
3258 struct got_worktree *worktree = NULL;
3259 struct got_repository *repo = NULL;
3260 char *cwd = NULL, *id_str = NULL;
3261 struct got_object_id *id = NULL;
3262 const char *logmsg = NULL;
3263 const char *got_author = getenv("GOT_AUTHOR");
3264 struct collect_commit_logmsg_arg cl_arg;
3265 char *editor = NULL;
3266 int ch, rebase_in_progress, histedit_in_progress;
3267 struct got_pathlist_head paths;
3269 TAILQ_INIT(&paths);
3271 while ((ch = getopt(argc, argv, "m:")) != -1) {
3272 switch (ch) {
3273 case 'm':
3274 logmsg = optarg;
3275 break;
3276 default:
3277 usage_commit();
3278 /* NOTREACHED */
3282 argc -= optind;
3283 argv += optind;
3285 #ifndef PROFILE
3286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3287 "unveil", NULL) == -1)
3288 err(1, "pledge");
3289 #endif
3290 if (got_author == NULL) {
3291 /* TODO: Look current user up in password database */
3292 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3293 goto done;
3296 cwd = getcwd(NULL, 0);
3297 if (cwd == NULL) {
3298 error = got_error_from_errno("getcwd");
3299 goto done;
3301 error = got_worktree_open(&worktree, cwd);
3302 if (error)
3303 goto done;
3305 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3306 if (error)
3307 goto done;
3308 if (rebase_in_progress) {
3309 error = got_error(GOT_ERR_REBASING);
3310 goto done;
3313 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3314 worktree);
3315 if (error)
3316 goto done;
3318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3319 if (error != NULL)
3320 goto done;
3323 * unveil(2) traverses exec(2); if an editor is used we have
3324 * to apply unveil after the log message has been written.
3326 if (logmsg == NULL || strlen(logmsg) == 0)
3327 error = get_editor(&editor);
3328 else
3329 error = apply_unveil(got_repo_get_path(repo), 0,
3330 got_worktree_get_root_path(worktree));
3331 if (error)
3332 goto done;
3334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3335 if (error)
3336 goto done;
3338 cl_arg.editor = editor;
3339 cl_arg.cmdline_log = logmsg;
3340 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3341 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3342 if (!histedit_in_progress) {
3343 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3344 error = got_error(GOT_ERR_COMMIT_BRANCH);
3345 goto done;
3347 cl_arg.branch_name += 11;
3349 cl_arg.repo_path = got_repo_get_path(repo);
3350 cl_arg.logmsg_path = NULL;
3351 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3352 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3353 if (error) {
3354 if (cl_arg.logmsg_path)
3355 fprintf(stderr, "%s: log message preserved in %s\n",
3356 getprogname(), cl_arg.logmsg_path);
3357 goto done;
3360 if (cl_arg.logmsg_path)
3361 unlink(cl_arg.logmsg_path);
3363 error = got_object_id_str(&id_str, id);
3364 if (error)
3365 goto done;
3366 printf("Created commit %s\n", id_str);
3367 done:
3368 if (repo)
3369 got_repo_close(repo);
3370 if (worktree)
3371 got_worktree_close(worktree);
3372 free(cwd);
3373 free(id_str);
3374 free(editor);
3375 return error;
3378 __dead static void
3379 usage_cherrypick(void)
3381 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3382 exit(1);
3385 static const struct got_error *
3386 cmd_cherrypick(int argc, char *argv[])
3388 const struct got_error *error = NULL;
3389 struct got_worktree *worktree = NULL;
3390 struct got_repository *repo = NULL;
3391 char *cwd = NULL, *commit_id_str = NULL;
3392 struct got_object_id *commit_id = NULL;
3393 struct got_commit_object *commit = NULL;
3394 struct got_object_qid *pid;
3395 struct got_reference *head_ref = NULL;
3396 int ch, did_something = 0;
3398 while ((ch = getopt(argc, argv, "")) != -1) {
3399 switch (ch) {
3400 default:
3401 usage_cherrypick();
3402 /* NOTREACHED */
3406 argc -= optind;
3407 argv += optind;
3409 #ifndef PROFILE
3410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3411 "unveil", NULL) == -1)
3412 err(1, "pledge");
3413 #endif
3414 if (argc != 1)
3415 usage_cherrypick();
3417 cwd = getcwd(NULL, 0);
3418 if (cwd == NULL) {
3419 error = got_error_from_errno("getcwd");
3420 goto done;
3422 error = got_worktree_open(&worktree, cwd);
3423 if (error)
3424 goto done;
3426 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3427 if (error != NULL)
3428 goto done;
3430 error = apply_unveil(got_repo_get_path(repo), 0,
3431 got_worktree_get_root_path(worktree));
3432 if (error)
3433 goto done;
3435 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3436 GOT_OBJ_TYPE_COMMIT, repo);
3437 if (error != NULL) {
3438 struct got_reference *ref;
3439 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3440 goto done;
3441 error = got_ref_open(&ref, repo, argv[0], 0);
3442 if (error != NULL)
3443 goto done;
3444 error = got_ref_resolve(&commit_id, repo, ref);
3445 got_ref_close(ref);
3446 if (error != NULL)
3447 goto done;
3449 error = got_object_id_str(&commit_id_str, commit_id);
3450 if (error)
3451 goto done;
3453 error = got_ref_open(&head_ref, repo,
3454 got_worktree_get_head_ref_name(worktree), 0);
3455 if (error != NULL)
3456 goto done;
3458 error = check_same_branch(commit_id, head_ref, NULL, repo);
3459 if (error) {
3460 if (error->code != GOT_ERR_ANCESTRY)
3461 goto done;
3462 error = NULL;
3463 } else {
3464 error = got_error(GOT_ERR_SAME_BRANCH);
3465 goto done;
3468 error = got_object_open_as_commit(&commit, repo, commit_id);
3469 if (error)
3470 goto done;
3471 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3472 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3473 commit_id, repo, update_progress, &did_something, check_cancelled,
3474 NULL);
3475 if (error != NULL)
3476 goto done;
3478 if (did_something)
3479 printf("Merged commit %s\n", commit_id_str);
3480 done:
3481 if (commit)
3482 got_object_commit_close(commit);
3483 free(commit_id_str);
3484 if (head_ref)
3485 got_ref_close(head_ref);
3486 if (worktree)
3487 got_worktree_close(worktree);
3488 if (repo)
3489 got_repo_close(repo);
3490 return error;
3493 __dead static void
3494 usage_backout(void)
3496 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3497 exit(1);
3500 static const struct got_error *
3501 cmd_backout(int argc, char *argv[])
3503 const struct got_error *error = NULL;
3504 struct got_worktree *worktree = NULL;
3505 struct got_repository *repo = NULL;
3506 char *cwd = NULL, *commit_id_str = NULL;
3507 struct got_object_id *commit_id = NULL;
3508 struct got_commit_object *commit = NULL;
3509 struct got_object_qid *pid;
3510 struct got_reference *head_ref = NULL;
3511 int ch, did_something = 0;
3513 while ((ch = getopt(argc, argv, "")) != -1) {
3514 switch (ch) {
3515 default:
3516 usage_backout();
3517 /* NOTREACHED */
3521 argc -= optind;
3522 argv += optind;
3524 #ifndef PROFILE
3525 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3526 "unveil", NULL) == -1)
3527 err(1, "pledge");
3528 #endif
3529 if (argc != 1)
3530 usage_backout();
3532 cwd = getcwd(NULL, 0);
3533 if (cwd == NULL) {
3534 error = got_error_from_errno("getcwd");
3535 goto done;
3537 error = got_worktree_open(&worktree, cwd);
3538 if (error)
3539 goto done;
3541 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3542 if (error != NULL)
3543 goto done;
3545 error = apply_unveil(got_repo_get_path(repo), 0,
3546 got_worktree_get_root_path(worktree));
3547 if (error)
3548 goto done;
3550 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3551 GOT_OBJ_TYPE_COMMIT, repo);
3552 if (error != NULL) {
3553 struct got_reference *ref;
3554 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3555 goto done;
3556 error = got_ref_open(&ref, repo, argv[0], 0);
3557 if (error != NULL)
3558 goto done;
3559 error = got_ref_resolve(&commit_id, repo, ref);
3560 got_ref_close(ref);
3561 if (error != NULL)
3562 goto done;
3564 error = got_object_id_str(&commit_id_str, commit_id);
3565 if (error)
3566 goto done;
3568 error = got_ref_open(&head_ref, repo,
3569 got_worktree_get_head_ref_name(worktree), 0);
3570 if (error != NULL)
3571 goto done;
3573 error = check_same_branch(commit_id, head_ref, NULL, repo);
3574 if (error)
3575 goto done;
3577 error = got_object_open_as_commit(&commit, repo, commit_id);
3578 if (error)
3579 goto done;
3580 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3581 if (pid == NULL) {
3582 error = got_error(GOT_ERR_ROOT_COMMIT);
3583 goto done;
3586 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3587 update_progress, &did_something, check_cancelled, NULL);
3588 if (error != NULL)
3589 goto done;
3591 if (did_something)
3592 printf("Backed out commit %s\n", commit_id_str);
3593 done:
3594 if (commit)
3595 got_object_commit_close(commit);
3596 free(commit_id_str);
3597 if (head_ref)
3598 got_ref_close(head_ref);
3599 if (worktree)
3600 got_worktree_close(worktree);
3601 if (repo)
3602 got_repo_close(repo);
3603 return error;
3606 __dead static void
3607 usage_rebase(void)
3609 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3610 getprogname());
3611 exit(1);
3614 void
3615 trim_logmsg(char *logmsg, int limit)
3617 char *nl;
3618 size_t len;
3620 len = strlen(logmsg);
3621 if (len > limit)
3622 len = limit;
3623 logmsg[len] = '\0';
3624 nl = strchr(logmsg, '\n');
3625 if (nl)
3626 *nl = '\0';
3629 static const struct got_error *
3630 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3632 const char *logmsg0 = NULL;
3634 logmsg0 = got_object_commit_get_logmsg(commit);
3636 while (isspace((unsigned char)logmsg0[0]))
3637 logmsg0++;
3639 *logmsg = strdup(logmsg0);
3640 if (*logmsg == NULL)
3641 return got_error_from_errno("strdup");
3643 trim_logmsg(*logmsg, limit);
3644 return NULL;
3647 static const struct got_error *
3648 show_rebase_progress(struct got_commit_object *commit,
3649 struct got_object_id *old_id, struct got_object_id *new_id)
3651 const struct got_error *err;
3652 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3654 err = got_object_id_str(&old_id_str, old_id);
3655 if (err)
3656 goto done;
3658 if (new_id) {
3659 err = got_object_id_str(&new_id_str, new_id);
3660 if (err)
3661 goto done;
3664 old_id_str[12] = '\0';
3665 if (new_id_str)
3666 new_id_str[12] = '\0';
3668 err = get_short_logmsg(&logmsg, 42, commit);
3669 if (err)
3670 goto done;
3672 printf("%s -> %s: %s\n", old_id_str,
3673 new_id_str ? new_id_str : "no-op change", logmsg);
3674 done:
3675 free(old_id_str);
3676 free(new_id_str);
3677 return err;
3680 static const struct got_error *
3681 rebase_progress(void *arg, unsigned char status, const char *path)
3683 unsigned char *rebase_status = arg;
3685 while (path[0] == '/')
3686 path++;
3687 printf("%c %s\n", status, path);
3689 if (*rebase_status == GOT_STATUS_CONFLICT)
3690 return NULL;
3691 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3692 *rebase_status = status;
3693 return NULL;
3696 static const struct got_error *
3697 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3698 struct got_reference *branch, struct got_reference *new_base_branch,
3699 struct got_reference *tmp_branch, struct got_repository *repo)
3701 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3702 return got_worktree_rebase_complete(worktree, fileindex,
3703 new_base_branch, tmp_branch, branch, repo);
3706 static const struct got_error *
3707 rebase_commit(struct got_pathlist_head *merged_paths,
3708 struct got_worktree *worktree, struct got_fileindex *fileindex,
3709 struct got_reference *tmp_branch,
3710 struct got_object_id *commit_id, struct got_repository *repo)
3712 const struct got_error *error;
3713 struct got_commit_object *commit;
3714 struct got_object_id *new_commit_id;
3716 error = got_object_open_as_commit(&commit, repo, commit_id);
3717 if (error)
3718 return error;
3720 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3721 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3722 if (error) {
3723 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3724 goto done;
3725 error = show_rebase_progress(commit, commit_id, NULL);
3726 } else {
3727 error = show_rebase_progress(commit, commit_id, new_commit_id);
3728 free(new_commit_id);
3730 done:
3731 got_object_commit_close(commit);
3732 return error;
3735 struct check_path_prefix_arg {
3736 const char *path_prefix;
3737 size_t len;
3738 int errcode;
3741 static const struct got_error *
3742 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3743 struct got_blob_object *blob2, struct got_object_id *id1,
3744 struct got_object_id *id2, const char *path1, const char *path2,
3745 struct got_repository *repo)
3747 struct check_path_prefix_arg *a = arg;
3749 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3750 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3751 return got_error(a->errcode);
3753 return NULL;
3756 static const struct got_error *
3757 check_path_prefix(struct got_object_id *parent_id,
3758 struct got_object_id *commit_id, const char *path_prefix,
3759 int errcode, struct got_repository *repo)
3761 const struct got_error *err;
3762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3763 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3764 struct check_path_prefix_arg cpp_arg;
3766 if (got_path_is_root_dir(path_prefix))
3767 return NULL;
3769 err = got_object_open_as_commit(&commit, repo, commit_id);
3770 if (err)
3771 goto done;
3773 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3774 if (err)
3775 goto done;
3777 err = got_object_open_as_tree(&tree1, repo,
3778 got_object_commit_get_tree_id(parent_commit));
3779 if (err)
3780 goto done;
3782 err = got_object_open_as_tree(&tree2, repo,
3783 got_object_commit_get_tree_id(commit));
3784 if (err)
3785 goto done;
3787 cpp_arg.path_prefix = path_prefix;
3788 while (cpp_arg.path_prefix[0] == '/')
3789 cpp_arg.path_prefix++;
3790 cpp_arg.len = strlen(cpp_arg.path_prefix);
3791 cpp_arg.errcode = errcode;
3792 err = got_diff_tree(tree1, tree2, "", "", repo,
3793 check_path_prefix_in_diff, &cpp_arg, 0);
3794 done:
3795 if (tree1)
3796 got_object_tree_close(tree1);
3797 if (tree2)
3798 got_object_tree_close(tree2);
3799 if (commit)
3800 got_object_commit_close(commit);
3801 if (parent_commit)
3802 got_object_commit_close(parent_commit);
3803 return err;
3806 static const struct got_error *
3807 collect_commits(struct got_object_id_queue *commits,
3808 struct got_object_id *initial_commit_id,
3809 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3810 const char *path_prefix, int path_prefix_errcode,
3811 struct got_repository *repo)
3813 const struct got_error *err = NULL;
3814 struct got_commit_graph *graph = NULL;
3815 struct got_object_id *parent_id = NULL;
3816 struct got_object_qid *qid;
3817 struct got_object_id *commit_id = initial_commit_id;
3819 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3820 if (err)
3821 return err;
3823 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3824 if (err)
3825 goto done;
3826 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3827 err = got_commit_graph_iter_next(&parent_id, graph);
3828 if (err) {
3829 if (err->code == GOT_ERR_ITER_COMPLETED) {
3830 err = got_error_msg(GOT_ERR_ANCESTRY,
3831 "ran out of commits to rebase before "
3832 "youngest common ancestor commit has "
3833 "been reached?!?");
3834 goto done;
3835 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3836 goto done;
3837 err = got_commit_graph_fetch_commits(graph, 1, repo);
3838 if (err)
3839 goto done;
3840 } else {
3841 err = check_path_prefix(parent_id, commit_id,
3842 path_prefix, path_prefix_errcode, repo);
3843 if (err)
3844 goto done;
3846 err = got_object_qid_alloc(&qid, commit_id);
3847 if (err)
3848 goto done;
3849 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3850 commit_id = parent_id;
3853 done:
3854 got_commit_graph_close(graph);
3855 return err;
3858 static const struct got_error *
3859 cmd_rebase(int argc, char *argv[])
3861 const struct got_error *error = NULL;
3862 struct got_worktree *worktree = NULL;
3863 struct got_repository *repo = NULL;
3864 struct got_fileindex *fileindex = NULL;
3865 char *cwd = NULL;
3866 struct got_reference *branch = NULL;
3867 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3868 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3869 struct got_object_id *resume_commit_id = NULL;
3870 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3871 struct got_commit_object *commit = NULL;
3872 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3873 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3874 struct got_object_id_queue commits;
3875 struct got_pathlist_head merged_paths;
3876 const struct got_object_id_queue *parent_ids;
3877 struct got_object_qid *qid, *pid;
3879 SIMPLEQ_INIT(&commits);
3880 TAILQ_INIT(&merged_paths);
3882 while ((ch = getopt(argc, argv, "ac")) != -1) {
3883 switch (ch) {
3884 case 'a':
3885 abort_rebase = 1;
3886 break;
3887 case 'c':
3888 continue_rebase = 1;
3889 break;
3890 default:
3891 usage_rebase();
3892 /* NOTREACHED */
3896 argc -= optind;
3897 argv += optind;
3899 #ifndef PROFILE
3900 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3901 "unveil", NULL) == -1)
3902 err(1, "pledge");
3903 #endif
3904 if (abort_rebase && continue_rebase)
3905 usage_rebase();
3906 else if (abort_rebase || continue_rebase) {
3907 if (argc != 0)
3908 usage_rebase();
3909 } else if (argc != 1)
3910 usage_rebase();
3912 cwd = getcwd(NULL, 0);
3913 if (cwd == NULL) {
3914 error = got_error_from_errno("getcwd");
3915 goto done;
3917 error = got_worktree_open(&worktree, cwd);
3918 if (error)
3919 goto done;
3921 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3922 if (error != NULL)
3923 goto done;
3925 error = apply_unveil(got_repo_get_path(repo), 0,
3926 got_worktree_get_root_path(worktree));
3927 if (error)
3928 goto done;
3930 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3931 if (error)
3932 goto done;
3934 if (abort_rebase) {
3935 int did_something;
3936 if (!rebase_in_progress) {
3937 error = got_error(GOT_ERR_NOT_REBASING);
3938 goto done;
3940 error = got_worktree_rebase_continue(&resume_commit_id,
3941 &new_base_branch, &tmp_branch, &branch, &fileindex,
3942 worktree, repo);
3943 if (error)
3944 goto done;
3945 printf("Switching work tree to %s\n",
3946 got_ref_get_symref_target(new_base_branch));
3947 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3948 new_base_branch, update_progress, &did_something);
3949 if (error)
3950 goto done;
3951 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3952 goto done; /* nothing else to do */
3955 if (continue_rebase) {
3956 if (!rebase_in_progress) {
3957 error = got_error(GOT_ERR_NOT_REBASING);
3958 goto done;
3960 error = got_worktree_rebase_continue(&resume_commit_id,
3961 &new_base_branch, &tmp_branch, &branch, &fileindex,
3962 worktree, repo);
3963 if (error)
3964 goto done;
3966 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3967 resume_commit_id, repo);
3968 if (error)
3969 goto done;
3971 yca_id = got_object_id_dup(resume_commit_id);
3972 if (yca_id == NULL) {
3973 error = got_error_from_errno("got_object_id_dup");
3974 goto done;
3976 } else {
3977 error = got_ref_open(&branch, repo, argv[0], 0);
3978 if (error != NULL)
3979 goto done;
3982 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3983 if (error)
3984 goto done;
3986 if (!continue_rebase) {
3987 struct got_object_id *base_commit_id;
3989 base_commit_id = got_worktree_get_base_commit_id(worktree);
3990 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3991 base_commit_id, branch_head_commit_id, repo);
3992 if (error)
3993 goto done;
3994 if (yca_id == NULL) {
3995 error = got_error_msg(GOT_ERR_ANCESTRY,
3996 "specified branch shares no common ancestry "
3997 "with work tree's branch");
3998 goto done;
4001 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4002 if (error) {
4003 if (error->code != GOT_ERR_ANCESTRY)
4004 goto done;
4005 error = NULL;
4006 } else {
4007 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4008 "specified branch resolves to a commit which "
4009 "is already contained in work tree's branch");
4010 goto done;
4012 error = got_worktree_rebase_prepare(&new_base_branch,
4013 &tmp_branch, &fileindex, worktree, branch, repo);
4014 if (error)
4015 goto done;
4018 commit_id = branch_head_commit_id;
4019 error = got_object_open_as_commit(&commit, repo, commit_id);
4020 if (error)
4021 goto done;
4023 parent_ids = got_object_commit_get_parent_ids(commit);
4024 pid = SIMPLEQ_FIRST(parent_ids);
4025 error = collect_commits(&commits, commit_id, pid->id,
4026 yca_id, got_worktree_get_path_prefix(worktree),
4027 GOT_ERR_REBASE_PATH, repo);
4028 got_object_commit_close(commit);
4029 commit = NULL;
4030 if (error)
4031 goto done;
4033 if (SIMPLEQ_EMPTY(&commits)) {
4034 if (continue_rebase)
4035 error = rebase_complete(worktree, fileindex,
4036 branch, new_base_branch, tmp_branch, repo);
4037 else
4038 error = got_error(GOT_ERR_EMPTY_REBASE);
4039 goto done;
4042 pid = NULL;
4043 SIMPLEQ_FOREACH(qid, &commits, entry) {
4044 commit_id = qid->id;
4045 parent_id = pid ? pid->id : yca_id;
4046 pid = qid;
4048 error = got_worktree_rebase_merge_files(&merged_paths,
4049 worktree, fileindex, parent_id, commit_id, repo,
4050 rebase_progress, &rebase_status, check_cancelled, NULL);
4051 if (error)
4052 goto done;
4054 if (rebase_status == GOT_STATUS_CONFLICT) {
4055 got_worktree_rebase_pathlist_free(&merged_paths);
4056 break;
4059 error = rebase_commit(&merged_paths, worktree, fileindex,
4060 tmp_branch, commit_id, repo);
4061 got_worktree_rebase_pathlist_free(&merged_paths);
4062 if (error)
4063 goto done;
4066 if (rebase_status == GOT_STATUS_CONFLICT) {
4067 error = got_worktree_rebase_postpone(worktree, fileindex);
4068 if (error)
4069 goto done;
4070 error = got_error_msg(GOT_ERR_CONFLICTS,
4071 "conflicts must be resolved before rebasing can continue");
4072 } else
4073 error = rebase_complete(worktree, fileindex, branch,
4074 new_base_branch, tmp_branch, repo);
4075 done:
4076 got_object_id_queue_free(&commits);
4077 free(branch_head_commit_id);
4078 free(resume_commit_id);
4079 free(yca_id);
4080 if (commit)
4081 got_object_commit_close(commit);
4082 if (branch)
4083 got_ref_close(branch);
4084 if (new_base_branch)
4085 got_ref_close(new_base_branch);
4086 if (tmp_branch)
4087 got_ref_close(tmp_branch);
4088 if (worktree)
4089 got_worktree_close(worktree);
4090 if (repo)
4091 got_repo_close(repo);
4092 return error;
4095 __dead static void
4096 usage_histedit(void)
4098 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4099 getprogname());
4100 exit(1);
4103 #define GOT_HISTEDIT_PICK 'p'
4104 #define GOT_HISTEDIT_EDIT 'e'
4105 #define GOT_HISTEDIT_FOLD 'f'
4106 #define GOT_HISTEDIT_DROP 'd'
4107 #define GOT_HISTEDIT_MESG 'm'
4109 static struct got_histedit_cmd {
4110 unsigned char code;
4111 const char *name;
4112 const char *desc;
4113 } got_histedit_cmds[] = {
4114 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4115 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4116 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4117 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4118 { GOT_HISTEDIT_MESG, "mesg",
4119 "single-line log message for commit above (open editor if empty)" },
4122 struct got_histedit_list_entry {
4123 TAILQ_ENTRY(got_histedit_list_entry) entry;
4124 struct got_object_id *commit_id;
4125 const struct got_histedit_cmd *cmd;
4126 char *logmsg;
4128 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4130 static const struct got_error *
4131 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4132 FILE *f, struct got_repository *repo)
4134 const struct got_error *err = NULL;
4135 char *logmsg = NULL, *id_str = NULL;
4136 struct got_commit_object *commit = NULL;
4137 size_t n;
4139 err = got_object_open_as_commit(&commit, repo, commit_id);
4140 if (err)
4141 goto done;
4143 err = get_short_logmsg(&logmsg, 34, commit);
4144 if (err)
4145 goto done;
4147 err = got_object_id_str(&id_str, commit_id);
4148 if (err)
4149 goto done;
4151 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4152 if (n < 0)
4153 err = got_ferror(f, GOT_ERR_IO);
4154 done:
4155 if (commit)
4156 got_object_commit_close(commit);
4157 free(id_str);
4158 free(logmsg);
4159 return err;
4162 static const struct got_error *
4163 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4164 struct got_repository *repo)
4166 const struct got_error *err = NULL;
4167 struct got_object_qid *qid;
4169 if (SIMPLEQ_EMPTY(commits))
4170 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4172 SIMPLEQ_FOREACH(qid, commits, entry) {
4173 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4174 f, repo);
4175 if (err)
4176 break;
4179 return err;
4182 static const struct got_error *
4183 write_cmd_list(FILE *f)
4185 const struct got_error *err = NULL;
4186 int n, i;
4188 n = fprintf(f, "# Available histedit commands:\n");
4189 if (n < 0)
4190 return got_ferror(f, GOT_ERR_IO);
4192 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4193 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4194 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4195 cmd->desc);
4196 if (n < 0) {
4197 err = got_ferror(f, GOT_ERR_IO);
4198 break;
4201 n = fprintf(f, "# Commits will be processed in order from top to "
4202 "bottom of this file.\n");
4203 if (n < 0)
4204 return got_ferror(f, GOT_ERR_IO);
4205 return err;
4208 static const struct got_error *
4209 histedit_syntax_error(int lineno)
4211 static char msg[42];
4212 int ret;
4214 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4215 lineno);
4216 if (ret == -1 || ret >= sizeof(msg))
4217 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4219 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4222 static const struct got_error *
4223 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4224 char *logmsg, struct got_repository *repo)
4226 const struct got_error *err;
4227 struct got_commit_object *folded_commit = NULL;
4228 char *id_str;
4230 err = got_object_id_str(&id_str, hle->commit_id);
4231 if (err)
4232 return err;
4234 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4235 if (err)
4236 goto done;
4238 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4239 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4240 got_object_commit_get_logmsg(folded_commit)) == -1) {
4241 err = got_error_from_errno("asprintf");
4242 goto done;
4244 done:
4245 if (folded_commit)
4246 got_object_commit_close(folded_commit);
4247 free(id_str);
4248 return err;
4251 static struct got_histedit_list_entry *
4252 get_folded_commits(struct got_histedit_list_entry *hle)
4254 struct got_histedit_list_entry *prev, *folded = NULL;
4256 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4257 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4258 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4259 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4260 folded = prev;
4261 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4264 return folded;
4267 static const struct got_error *
4268 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4269 struct got_repository *repo)
4271 char *logmsg_path = NULL, *id_str = NULL;
4272 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4273 const struct got_error *err = NULL;
4274 struct got_commit_object *commit = NULL;
4275 int fd;
4276 struct got_histedit_list_entry *folded = NULL;
4278 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4279 if (err)
4280 return err;
4282 folded = get_folded_commits(hle);
4283 if (folded) {
4284 while (folded != hle) {
4285 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4286 folded = TAILQ_NEXT(folded, entry);
4287 continue;
4289 err = append_folded_commit_msg(&new_msg, folded,
4290 logmsg, repo);
4291 if (err)
4292 goto done;
4293 free(logmsg);
4294 logmsg = new_msg;
4295 folded = TAILQ_NEXT(folded, entry);
4299 err = got_object_id_str(&id_str, hle->commit_id);
4300 if (err)
4301 goto done;
4302 if (asprintf(&new_msg,
4303 "%s\n# original log message of commit %s: %s",
4304 logmsg ? logmsg : "", id_str,
4305 got_object_commit_get_logmsg(commit)) == -1) {
4306 err = got_error_from_errno("asprintf");
4307 goto done;
4309 free(logmsg);
4310 logmsg = new_msg;
4312 err = got_object_id_str(&id_str, hle->commit_id);
4313 if (err)
4314 goto done;
4316 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4317 if (err)
4318 goto done;
4320 dprintf(fd, logmsg);
4321 close(fd);
4323 err = get_editor(&editor);
4324 if (err)
4325 goto done;
4327 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4328 if (err) {
4329 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4330 goto done;
4331 err = NULL;
4332 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4333 if (hle->logmsg == NULL)
4334 err = got_error_from_errno("strdup");
4336 done:
4337 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4338 err = got_error_from_errno2("unlink", logmsg_path);
4339 free(logmsg_path);
4340 free(logmsg);
4341 free(editor);
4342 if (commit)
4343 got_object_commit_close(commit);
4344 return err;
4347 static const struct got_error *
4348 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4349 FILE *f, struct got_repository *repo)
4351 const struct got_error *err = NULL;
4352 char *line = NULL, *p, *end;
4353 size_t size;
4354 ssize_t len;
4355 int lineno = 0, i;
4356 const struct got_histedit_cmd *cmd;
4357 struct got_object_id *commit_id = NULL;
4358 struct got_histedit_list_entry *hle = NULL;
4360 for (;;) {
4361 len = getline(&line, &size, f);
4362 if (len == -1) {
4363 const struct got_error *getline_err;
4364 if (feof(f))
4365 break;
4366 getline_err = got_error_from_errno("getline");
4367 err = got_ferror(f, getline_err->code);
4368 break;
4370 lineno++;
4371 p = line;
4372 while (isspace((unsigned char)p[0]))
4373 p++;
4374 if (p[0] == '#' || p[0] == '\0') {
4375 free(line);
4376 line = NULL;
4377 continue;
4379 cmd = NULL;
4380 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4381 cmd = &got_histedit_cmds[i];
4382 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4383 isspace((unsigned char)p[strlen(cmd->name)])) {
4384 p += strlen(cmd->name);
4385 break;
4387 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4388 p++;
4389 break;
4392 if (i == nitems(got_histedit_cmds)) {
4393 err = histedit_syntax_error(lineno);
4394 break;
4396 while (isspace((unsigned char)p[0]))
4397 p++;
4398 if (cmd->code == GOT_HISTEDIT_MESG) {
4399 if (hle == NULL || hle->logmsg != NULL) {
4400 err = got_error(GOT_ERR_HISTEDIT_CMD);
4401 break;
4403 if (p[0] == '\0') {
4404 err = histedit_edit_logmsg(hle, repo);
4405 if (err)
4406 break;
4407 } else {
4408 hle->logmsg = strdup(p);
4409 if (hle->logmsg == NULL) {
4410 err = got_error_from_errno("strdup");
4411 break;
4414 free(line);
4415 line = NULL;
4416 continue;
4417 } else {
4418 end = p;
4419 while (end[0] && !isspace((unsigned char)end[0]))
4420 end++;
4421 *end = '\0';
4423 err = got_object_resolve_id_str(&commit_id, repo, p);
4424 if (err) {
4425 /* override error code */
4426 err = histedit_syntax_error(lineno);
4427 break;
4430 hle = malloc(sizeof(*hle));
4431 if (hle == NULL) {
4432 err = got_error_from_errno("malloc");
4433 break;
4435 hle->cmd = cmd;
4436 hle->commit_id = commit_id;
4437 hle->logmsg = NULL;
4438 commit_id = NULL;
4439 free(line);
4440 line = NULL;
4441 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4444 free(line);
4445 free(commit_id);
4446 return err;
4449 static const struct got_error *
4450 histedit_check_script(struct got_histedit_list *histedit_cmds,
4451 struct got_object_id_queue *commits, struct got_repository *repo)
4453 const struct got_error *err = NULL;
4454 struct got_object_qid *qid;
4455 struct got_histedit_list_entry *hle;
4456 static char msg[80];
4457 char *id_str;
4459 if (TAILQ_EMPTY(histedit_cmds))
4460 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4461 "histedit script contains no commands");
4463 SIMPLEQ_FOREACH(qid, commits, entry) {
4464 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4465 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4466 break;
4468 if (hle == NULL) {
4469 err = got_object_id_str(&id_str, qid->id);
4470 if (err)
4471 return err;
4472 snprintf(msg, sizeof(msg),
4473 "commit %s missing from histedit script", id_str);
4474 free(id_str);
4475 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4479 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4480 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4481 "last commit in histedit script cannot be folded");
4483 return NULL;
4486 static const struct got_error *
4487 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4488 const char *path, struct got_object_id_queue *commits,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4492 char *editor;
4493 FILE *f = NULL;
4495 err = get_editor(&editor);
4496 if (err)
4497 return err;
4499 if (spawn_editor(editor, path) == -1) {
4500 err = got_error_from_errno("failed spawning editor");
4501 goto done;
4504 f = fopen(path, "r");
4505 if (f == NULL) {
4506 err = got_error_from_errno("fopen");
4507 goto done;
4509 err = histedit_parse_list(histedit_cmds, f, repo);
4510 if (err)
4511 goto done;
4513 err = histedit_check_script(histedit_cmds, commits, repo);
4514 done:
4515 if (f && fclose(f) != 0 && err == NULL)
4516 err = got_error_from_errno("fclose");
4517 free(editor);
4518 return err;
4521 static const struct got_error *
4522 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4523 struct got_object_id_queue *, const char *, struct got_repository *);
4525 static const struct got_error *
4526 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4527 struct got_object_id_queue *commits, struct got_repository *repo)
4529 const struct got_error *err;
4530 FILE *f = NULL;
4531 char *path = NULL;
4533 err = got_opentemp_named(&path, &f, "got-histedit");
4534 if (err)
4535 return err;
4537 err = write_cmd_list(f);
4538 if (err)
4539 goto done;
4541 err = histedit_write_commit_list(commits, f, repo);
4542 if (err)
4543 goto done;
4545 if (fclose(f) != 0) {
4546 err = got_error_from_errno("fclose");
4547 goto done;
4549 f = NULL;
4551 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4552 if (err) {
4553 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4554 err->code != GOT_ERR_HISTEDIT_CMD)
4555 goto done;
4556 err = histedit_edit_list_retry(histedit_cmds, err,
4557 commits, path, repo);
4559 done:
4560 if (f && fclose(f) != 0 && err == NULL)
4561 err = got_error_from_errno("fclose");
4562 if (path && unlink(path) != 0 && err == NULL)
4563 err = got_error_from_errno2("unlink", path);
4564 free(path);
4565 return err;
4568 static const struct got_error *
4569 histedit_save_list(struct got_histedit_list *histedit_cmds,
4570 struct got_worktree *worktree, struct got_repository *repo)
4572 const struct got_error *err = NULL;
4573 char *path = NULL;
4574 FILE *f = NULL;
4575 struct got_histedit_list_entry *hle;
4576 struct got_commit_object *commit = NULL;
4578 err = got_worktree_get_histedit_script_path(&path, worktree);
4579 if (err)
4580 return err;
4582 f = fopen(path, "w");
4583 if (f == NULL) {
4584 err = got_error_from_errno2("fopen", path);
4585 goto done;
4587 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4588 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4589 repo);
4590 if (err)
4591 break;
4593 if (hle->logmsg) {
4594 int n = fprintf(f, "%c %s\n",
4595 GOT_HISTEDIT_MESG, hle->logmsg);
4596 if (n < 0) {
4597 err = got_ferror(f, GOT_ERR_IO);
4598 break;
4602 done:
4603 if (f && fclose(f) != 0 && err == NULL)
4604 err = got_error_from_errno("fclose");
4605 free(path);
4606 if (commit)
4607 got_object_commit_close(commit);
4608 return err;
4611 void
4612 histedit_free_list(struct got_histedit_list *histedit_cmds)
4614 struct got_histedit_list_entry *hle;
4616 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4617 TAILQ_REMOVE(histedit_cmds, hle, entry);
4618 free(hle);
4622 static const struct got_error *
4623 histedit_load_list(struct got_histedit_list *histedit_cmds,
4624 const char *path, struct got_repository *repo)
4626 const struct got_error *err = NULL;
4627 FILE *f = NULL;
4629 f = fopen(path, "r");
4630 if (f == NULL) {
4631 err = got_error_from_errno2("fopen", path);
4632 goto done;
4635 err = histedit_parse_list(histedit_cmds, f, repo);
4636 done:
4637 if (f && fclose(f) != 0 && err == NULL)
4638 err = got_error_from_errno("fclose");
4639 return err;
4642 static const struct got_error *
4643 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4644 const struct got_error *edit_err, struct got_object_id_queue *commits,
4645 const char *path, struct got_repository *repo)
4647 const struct got_error *err = NULL, *prev_err = edit_err;
4648 int resp = ' ';
4650 while (resp != 'c' && resp != 'r' && resp != 'a') {
4651 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4652 "or (a)bort: ", getprogname(), prev_err->msg);
4653 resp = getchar();
4654 if (resp == '\n')
4655 resp = getchar();
4656 if (resp == 'c') {
4657 histedit_free_list(histedit_cmds);
4658 err = histedit_run_editor(histedit_cmds, path, commits,
4659 repo);
4660 if (err) {
4661 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4662 err->code != GOT_ERR_HISTEDIT_CMD)
4663 break;
4664 prev_err = err;
4665 resp = ' ';
4666 continue;
4668 break;
4669 } else if (resp == 'r') {
4670 histedit_free_list(histedit_cmds);
4671 err = histedit_edit_script(histedit_cmds,
4672 commits, repo);
4673 if (err) {
4674 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4675 err->code != GOT_ERR_HISTEDIT_CMD)
4676 break;
4677 prev_err = err;
4678 resp = ' ';
4679 continue;
4681 break;
4682 } else if (resp == 'a') {
4683 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4684 break;
4685 } else
4686 printf("invalid response '%c'\n", resp);
4689 return err;
4692 static const struct got_error *
4693 histedit_complete(struct got_worktree *worktree,
4694 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4695 struct got_reference *branch, struct got_repository *repo)
4697 printf("Switching work tree to %s\n",
4698 got_ref_get_symref_target(branch));
4699 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4700 branch, repo);
4703 static const struct got_error *
4704 show_histedit_progress(struct got_commit_object *commit,
4705 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4707 const struct got_error *err;
4708 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4710 err = got_object_id_str(&old_id_str, hle->commit_id);
4711 if (err)
4712 goto done;
4714 if (new_id) {
4715 err = got_object_id_str(&new_id_str, new_id);
4716 if (err)
4717 goto done;
4720 old_id_str[12] = '\0';
4721 if (new_id_str)
4722 new_id_str[12] = '\0';
4724 if (hle->logmsg) {
4725 logmsg = strdup(hle->logmsg);
4726 if (logmsg == NULL) {
4727 err = got_error_from_errno("strdup");
4728 goto done;
4730 trim_logmsg(logmsg, 42);
4731 } else {
4732 err = get_short_logmsg(&logmsg, 42, commit);
4733 if (err)
4734 goto done;
4737 switch (hle->cmd->code) {
4738 case GOT_HISTEDIT_PICK:
4739 case GOT_HISTEDIT_EDIT:
4740 printf("%s -> %s: %s\n", old_id_str,
4741 new_id_str ? new_id_str : "no-op change", logmsg);
4742 break;
4743 case GOT_HISTEDIT_DROP:
4744 case GOT_HISTEDIT_FOLD:
4745 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4746 logmsg);
4747 break;
4748 default:
4749 break;
4752 done:
4753 free(old_id_str);
4754 free(new_id_str);
4755 return err;
4758 static const struct got_error *
4759 histedit_commit(struct got_pathlist_head *merged_paths,
4760 struct got_worktree *worktree, struct got_fileindex *fileindex,
4761 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4762 struct got_repository *repo)
4764 const struct got_error *err;
4765 struct got_commit_object *commit;
4766 struct got_object_id *new_commit_id;
4768 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4769 && hle->logmsg == NULL) {
4770 err = histedit_edit_logmsg(hle, repo);
4771 if (err)
4772 return err;
4775 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4776 if (err)
4777 return err;
4779 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4780 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4781 hle->logmsg, repo);
4782 if (err) {
4783 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4784 goto done;
4785 err = show_histedit_progress(commit, hle, NULL);
4786 } else {
4787 err = show_histedit_progress(commit, hle, new_commit_id);
4788 free(new_commit_id);
4790 done:
4791 got_object_commit_close(commit);
4792 return err;
4795 static const struct got_error *
4796 histedit_skip_commit(struct got_histedit_list_entry *hle,
4797 struct got_worktree *worktree, struct got_repository *repo)
4799 const struct got_error *error;
4800 struct got_commit_object *commit;
4802 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4803 repo);
4804 if (error)
4805 return error;
4807 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4808 if (error)
4809 return error;
4811 error = show_histedit_progress(commit, hle, NULL);
4812 got_object_commit_close(commit);
4813 return error;
4816 static const struct got_error *
4817 cmd_histedit(int argc, char *argv[])
4819 const struct got_error *error = NULL;
4820 struct got_worktree *worktree = NULL;
4821 struct got_fileindex *fileindex = NULL;
4822 struct got_repository *repo = NULL;
4823 char *cwd = NULL;
4824 struct got_reference *branch = NULL;
4825 struct got_reference *tmp_branch = NULL;
4826 struct got_object_id *resume_commit_id = NULL;
4827 struct got_object_id *base_commit_id = NULL;
4828 struct got_object_id *head_commit_id = NULL;
4829 struct got_commit_object *commit = NULL;
4830 int ch, rebase_in_progress = 0, did_something;
4831 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4832 const char *edit_script_path = NULL;
4833 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4834 struct got_object_id_queue commits;
4835 struct got_pathlist_head merged_paths;
4836 const struct got_object_id_queue *parent_ids;
4837 struct got_object_qid *pid;
4838 struct got_histedit_list histedit_cmds;
4839 struct got_histedit_list_entry *hle;
4841 SIMPLEQ_INIT(&commits);
4842 TAILQ_INIT(&histedit_cmds);
4843 TAILQ_INIT(&merged_paths);
4845 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4846 switch (ch) {
4847 case 'a':
4848 abort_edit = 1;
4849 break;
4850 case 'c':
4851 continue_edit = 1;
4852 break;
4853 case 'F':
4854 edit_script_path = optarg;
4855 break;
4856 default:
4857 usage_histedit();
4858 /* NOTREACHED */
4862 argc -= optind;
4863 argv += optind;
4865 #ifndef PROFILE
4866 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4867 "unveil", NULL) == -1)
4868 err(1, "pledge");
4869 #endif
4870 if (abort_edit && continue_edit)
4871 usage_histedit();
4872 if (argc != 0)
4873 usage_histedit();
4876 * This command cannot apply unveil(2) in all cases because the
4877 * user may choose to run an editor to edit the histedit script
4878 * and to edit individual commit log messages.
4879 * unveil(2) traverses exec(2); if an editor is used we have to
4880 * apply unveil after edit script and log messages have been written.
4881 * XXX TODO: Make use of unveil(2) where possible.
4884 cwd = getcwd(NULL, 0);
4885 if (cwd == NULL) {
4886 error = got_error_from_errno("getcwd");
4887 goto done;
4889 error = got_worktree_open(&worktree, cwd);
4890 if (error)
4891 goto done;
4893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4894 if (error != NULL)
4895 goto done;
4897 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4898 if (error)
4899 goto done;
4900 if (rebase_in_progress) {
4901 error = got_error(GOT_ERR_REBASING);
4902 goto done;
4905 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4906 if (error)
4907 goto done;
4909 if (edit_in_progress && abort_edit) {
4910 error = got_worktree_histedit_continue(&resume_commit_id,
4911 &tmp_branch, &branch, &base_commit_id, &fileindex,
4912 worktree, repo);
4913 if (error)
4914 goto done;
4915 printf("Switching work tree to %s\n",
4916 got_ref_get_symref_target(branch));
4917 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4918 branch, base_commit_id, update_progress, &did_something);
4919 if (error)
4920 goto done;
4921 printf("Histedit of %s aborted\n",
4922 got_ref_get_symref_target(branch));
4923 goto done; /* nothing else to do */
4924 } else if (abort_edit) {
4925 error = got_error(GOT_ERR_NOT_HISTEDIT);
4926 goto done;
4929 if (continue_edit) {
4930 char *path;
4932 if (!edit_in_progress) {
4933 error = got_error(GOT_ERR_NOT_HISTEDIT);
4934 goto done;
4937 error = got_worktree_get_histedit_script_path(&path, worktree);
4938 if (error)
4939 goto done;
4941 error = histedit_load_list(&histedit_cmds, path, repo);
4942 free(path);
4943 if (error)
4944 goto done;
4946 error = got_worktree_histedit_continue(&resume_commit_id,
4947 &tmp_branch, &branch, &base_commit_id, &fileindex,
4948 worktree, repo);
4949 if (error)
4950 goto done;
4952 error = got_ref_resolve(&head_commit_id, repo, branch);
4953 if (error)
4954 goto done;
4956 error = got_object_open_as_commit(&commit, repo,
4957 head_commit_id);
4958 if (error)
4959 goto done;
4960 parent_ids = got_object_commit_get_parent_ids(commit);
4961 pid = SIMPLEQ_FIRST(parent_ids);
4962 if (pid == NULL) {
4963 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4964 goto done;
4966 error = collect_commits(&commits, head_commit_id, pid->id,
4967 base_commit_id, got_worktree_get_path_prefix(worktree),
4968 GOT_ERR_HISTEDIT_PATH, repo);
4969 got_object_commit_close(commit);
4970 commit = NULL;
4971 if (error)
4972 goto done;
4973 } else {
4974 if (edit_in_progress) {
4975 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4976 goto done;
4979 error = got_ref_open(&branch, repo,
4980 got_worktree_get_head_ref_name(worktree), 0);
4981 if (error != NULL)
4982 goto done;
4984 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4985 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4986 "will not edit commit history of a branch outside "
4987 "the \"refs/heads/\" reference namespace");
4988 goto done;
4991 error = got_ref_resolve(&head_commit_id, repo, branch);
4992 got_ref_close(branch);
4993 branch = NULL;
4994 if (error)
4995 goto done;
4997 error = got_object_open_as_commit(&commit, repo,
4998 head_commit_id);
4999 if (error)
5000 goto done;
5001 parent_ids = got_object_commit_get_parent_ids(commit);
5002 pid = SIMPLEQ_FIRST(parent_ids);
5003 if (pid == NULL) {
5004 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5005 goto done;
5007 error = collect_commits(&commits, head_commit_id, pid->id,
5008 got_worktree_get_base_commit_id(worktree),
5009 got_worktree_get_path_prefix(worktree),
5010 GOT_ERR_HISTEDIT_PATH, repo);
5011 got_object_commit_close(commit);
5012 commit = NULL;
5013 if (error)
5014 goto done;
5016 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5017 &base_commit_id, &fileindex, worktree, repo);
5018 if (error)
5019 goto done;
5021 if (edit_script_path) {
5022 error = histedit_load_list(&histedit_cmds,
5023 edit_script_path, repo);
5024 if (error) {
5025 got_worktree_histedit_abort(worktree, fileindex,
5026 repo, branch, base_commit_id,
5027 update_progress, &did_something);
5028 goto done;
5030 } else {
5031 error = histedit_edit_script(&histedit_cmds, &commits,
5032 repo);
5033 if (error) {
5034 got_worktree_histedit_abort(worktree, fileindex,
5035 repo, branch, base_commit_id,
5036 update_progress, &did_something);
5037 goto done;
5042 error = histedit_save_list(&histedit_cmds, worktree,
5043 repo);
5044 if (error) {
5045 got_worktree_histedit_abort(worktree, fileindex,
5046 repo, branch, base_commit_id,
5047 update_progress, &did_something);
5048 goto done;
5053 error = histedit_check_script(&histedit_cmds, &commits, repo);
5054 if (error)
5055 goto done;
5057 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5058 if (resume_commit_id) {
5059 if (got_object_id_cmp(hle->commit_id,
5060 resume_commit_id) != 0)
5061 continue;
5063 resume_commit_id = NULL;
5064 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5065 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5066 error = histedit_skip_commit(hle, worktree,
5067 repo);
5068 } else {
5069 error = histedit_commit(NULL, worktree,
5070 fileindex, tmp_branch, hle, repo);
5072 if (error)
5073 goto done;
5074 continue;
5077 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5078 error = histedit_skip_commit(hle, worktree, repo);
5079 if (error)
5080 goto done;
5081 continue;
5084 error = got_object_open_as_commit(&commit, repo,
5085 hle->commit_id);
5086 if (error)
5087 goto done;
5088 parent_ids = got_object_commit_get_parent_ids(commit);
5089 pid = SIMPLEQ_FIRST(parent_ids);
5091 error = got_worktree_histedit_merge_files(&merged_paths,
5092 worktree, fileindex, pid->id, hle->commit_id, repo,
5093 rebase_progress, &rebase_status, check_cancelled, NULL);
5094 if (error)
5095 goto done;
5096 got_object_commit_close(commit);
5097 commit = NULL;
5099 if (rebase_status == GOT_STATUS_CONFLICT) {
5100 got_worktree_rebase_pathlist_free(&merged_paths);
5101 break;
5104 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5105 char *id_str;
5106 error = got_object_id_str(&id_str, hle->commit_id);
5107 if (error)
5108 goto done;
5109 printf("Stopping histedit for amending commit %s\n",
5110 id_str);
5111 free(id_str);
5112 got_worktree_rebase_pathlist_free(&merged_paths);
5113 error = got_worktree_histedit_postpone(worktree,
5114 fileindex);
5115 goto done;
5118 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5119 error = histedit_skip_commit(hle, worktree, repo);
5120 if (error)
5121 goto done;
5122 continue;
5125 error = histedit_commit(&merged_paths, worktree, fileindex,
5126 tmp_branch, hle, repo);
5127 got_worktree_rebase_pathlist_free(&merged_paths);
5128 if (error)
5129 goto done;
5132 if (rebase_status == GOT_STATUS_CONFLICT) {
5133 error = got_worktree_histedit_postpone(worktree, fileindex);
5134 if (error)
5135 goto done;
5136 error = got_error_msg(GOT_ERR_CONFLICTS,
5137 "conflicts must be resolved before rebasing can continue");
5138 } else
5139 error = histedit_complete(worktree, fileindex, tmp_branch,
5140 branch, repo);
5141 done:
5142 got_object_id_queue_free(&commits);
5143 histedit_free_list(&histedit_cmds);
5144 free(head_commit_id);
5145 free(base_commit_id);
5146 free(resume_commit_id);
5147 if (commit)
5148 got_object_commit_close(commit);
5149 if (branch)
5150 got_ref_close(branch);
5151 if (tmp_branch)
5152 got_ref_close(tmp_branch);
5153 if (worktree)
5154 got_worktree_close(worktree);
5155 if (repo)
5156 got_repo_close(repo);
5157 return error;
5160 __dead static void
5161 usage_stage(void)
5163 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5164 "[file-path ...]\n",
5165 getprogname());
5166 exit(1);
5169 static const struct got_error *
5170 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5171 const char *path, struct got_object_id *blob_id,
5172 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5174 const struct got_error *err = NULL;
5175 char *id_str = NULL;
5177 if (staged_status != GOT_STATUS_ADD &&
5178 staged_status != GOT_STATUS_MODIFY &&
5179 staged_status != GOT_STATUS_DELETE)
5180 return NULL;
5182 if (staged_status == GOT_STATUS_ADD ||
5183 staged_status == GOT_STATUS_MODIFY)
5184 err = got_object_id_str(&id_str, staged_blob_id);
5185 else
5186 err = got_object_id_str(&id_str, blob_id);
5187 if (err)
5188 return err;
5190 printf("%s %c %s\n", id_str, staged_status, path);
5191 free(id_str);
5192 return NULL;
5195 static const struct got_error *
5196 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5197 int nchanges, const char *action)
5199 char *line = NULL;
5200 size_t linesize = 0;
5201 ssize_t linelen;
5203 switch (status) {
5204 case GOT_STATUS_ADD:
5205 printf("A %s\n%s this addition? [y/n] ", path, action);
5206 break;
5207 case GOT_STATUS_DELETE:
5208 printf("D %s\n%s this deletion? [y/n] ", path, action);
5209 break;
5210 case GOT_STATUS_MODIFY:
5211 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5212 return got_error_from_errno("fseek");
5213 printf(GOT_COMMIT_SEP_STR);
5214 while ((linelen = getline(&line, &linesize, patch_file) != -1))
5215 printf("%s", line);
5216 if (ferror(patch_file))
5217 return got_error_from_errno("getline");
5218 printf(GOT_COMMIT_SEP_STR);
5219 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5220 path, n, nchanges, action);
5221 break;
5222 default:
5223 return got_error_path(path, GOT_ERR_FILE_STATUS);
5226 return NULL;
5229 struct choose_patch_arg {
5230 FILE *patch_script_file;
5231 const char *action;
5234 static const struct got_error *
5235 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5236 FILE *patch_file, int n, int nchanges)
5238 const struct got_error *err = NULL;
5239 char *line = NULL;
5240 size_t linesize = 0;
5241 ssize_t linelen;
5242 int resp = ' ';
5243 struct choose_patch_arg *a = arg;
5245 *choice = GOT_PATCH_CHOICE_NONE;
5247 if (a->patch_script_file) {
5248 char *nl;
5249 err = show_change(status, path, patch_file, n, nchanges,
5250 a->action);
5251 if (err)
5252 return err;
5253 linelen = getline(&line, &linesize, a->patch_script_file);
5254 if (linelen == -1) {
5255 if (ferror(a->patch_script_file))
5256 return got_error_from_errno("getline");
5257 return NULL;
5259 nl = strchr(line, '\n');
5260 if (nl)
5261 *nl = '\0';
5262 if (strcmp(line, "y") == 0) {
5263 *choice = GOT_PATCH_CHOICE_YES;
5264 printf("y\n");
5265 } else if (strcmp(line, "n") == 0) {
5266 *choice = GOT_PATCH_CHOICE_NO;
5267 printf("n\n");
5268 } else if (strcmp(line, "q") == 0 &&
5269 status == GOT_STATUS_MODIFY) {
5270 *choice = GOT_PATCH_CHOICE_QUIT;
5271 printf("q\n");
5272 } else
5273 printf("invalid response '%s'\n", line);
5274 free(line);
5275 return NULL;
5278 while (resp != 'y' && resp != 'n' && resp != 'q') {
5279 err = show_change(status, path, patch_file, n, nchanges,
5280 a->action);
5281 if (err)
5282 return err;
5283 resp = getchar();
5284 if (resp == '\n')
5285 resp = getchar();
5286 if (status == GOT_STATUS_MODIFY) {
5287 if (resp != 'y' && resp != 'n' && resp != 'q') {
5288 printf("invalid response '%c'\n", resp);
5289 resp = ' ';
5291 } else if (resp != 'y' && resp != 'n') {
5292 printf("invalid response '%c'\n", resp);
5293 resp = ' ';
5297 if (resp == 'y')
5298 *choice = GOT_PATCH_CHOICE_YES;
5299 else if (resp == 'n')
5300 *choice = GOT_PATCH_CHOICE_NO;
5301 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5302 *choice = GOT_PATCH_CHOICE_QUIT;
5304 return NULL;
5307 static const struct got_error *
5308 cmd_stage(int argc, char *argv[])
5310 const struct got_error *error = NULL;
5311 struct got_repository *repo = NULL;
5312 struct got_worktree *worktree = NULL;
5313 char *cwd = NULL;
5314 struct got_pathlist_head paths;
5315 struct got_pathlist_entry *pe;
5316 int ch, list_stage = 0, pflag = 0;
5317 FILE *patch_script_file = NULL;
5318 const char *patch_script_path = NULL;
5319 struct choose_patch_arg cpa;
5321 TAILQ_INIT(&paths);
5323 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5324 switch (ch) {
5325 case 'l':
5326 list_stage = 1;
5327 break;
5328 case 'p':
5329 pflag = 1;
5330 break;
5331 case 'F':
5332 patch_script_path = optarg;
5333 break;
5334 default:
5335 usage_stage();
5336 /* NOTREACHED */
5340 argc -= optind;
5341 argv += optind;
5343 #ifndef PROFILE
5344 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5345 "unveil", NULL) == -1)
5346 err(1, "pledge");
5347 #endif
5348 if (list_stage && (pflag || patch_script_path))
5349 errx(1, "-l option cannot be used with other options");
5350 if (patch_script_path && !pflag)
5351 errx(1, "-F option can only be used together with -p option");
5353 cwd = getcwd(NULL, 0);
5354 if (cwd == NULL) {
5355 error = got_error_from_errno("getcwd");
5356 goto done;
5359 error = got_worktree_open(&worktree, cwd);
5360 if (error)
5361 goto done;
5363 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5364 if (error != NULL)
5365 goto done;
5367 if (patch_script_path) {
5368 patch_script_file = fopen(patch_script_path, "r");
5369 if (patch_script_file == NULL) {
5370 error = got_error_from_errno2("fopen",
5371 patch_script_path);
5372 goto done;
5375 error = apply_unveil(got_repo_get_path(repo), 1,
5376 got_worktree_get_root_path(worktree));
5377 if (error)
5378 goto done;
5380 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5381 if (error)
5382 goto done;
5384 if (list_stage)
5385 error = got_worktree_status(worktree, &paths, repo,
5386 print_stage, NULL, check_cancelled, NULL);
5387 else {
5388 cpa.patch_script_file = patch_script_file;
5389 cpa.action = "stage";
5390 error = got_worktree_stage(worktree, &paths,
5391 pflag ? NULL : print_status, NULL,
5392 pflag ? choose_patch : NULL, &cpa, repo);
5394 done:
5395 if (patch_script_file && fclose(patch_script_file) == EOF &&
5396 error == NULL)
5397 error = got_error_from_errno2("fclose", patch_script_path);
5398 if (repo)
5399 got_repo_close(repo);
5400 if (worktree)
5401 got_worktree_close(worktree);
5402 TAILQ_FOREACH(pe, &paths, entry)
5403 free((char *)pe->path);
5404 got_pathlist_free(&paths);
5405 free(cwd);
5406 return error;
5409 __dead static void
5410 usage_unstage(void)
5412 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5413 "[file-path ...]\n",
5414 getprogname());
5415 exit(1);
5419 static const struct got_error *
5420 cmd_unstage(int argc, char *argv[])
5422 const struct got_error *error = NULL;
5423 struct got_repository *repo = NULL;
5424 struct got_worktree *worktree = NULL;
5425 char *cwd = NULL;
5426 struct got_pathlist_head paths;
5427 struct got_pathlist_entry *pe;
5428 int ch, did_something = 0, pflag = 0;
5429 FILE *patch_script_file = NULL;
5430 const char *patch_script_path = NULL;
5431 struct choose_patch_arg cpa;
5433 TAILQ_INIT(&paths);
5435 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5436 switch (ch) {
5437 case 'p':
5438 pflag = 1;
5439 break;
5440 case 'F':
5441 patch_script_path = optarg;
5442 break;
5443 default:
5444 usage_unstage();
5445 /* NOTREACHED */
5449 argc -= optind;
5450 argv += optind;
5452 #ifndef PROFILE
5453 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5454 "unveil", NULL) == -1)
5455 err(1, "pledge");
5456 #endif
5457 if (patch_script_path && !pflag)
5458 errx(1, "-F option can only be used together with -p option");
5460 cwd = getcwd(NULL, 0);
5461 if (cwd == NULL) {
5462 error = got_error_from_errno("getcwd");
5463 goto done;
5466 error = got_worktree_open(&worktree, cwd);
5467 if (error)
5468 goto done;
5470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5471 if (error != NULL)
5472 goto done;
5474 if (patch_script_path) {
5475 patch_script_file = fopen(patch_script_path, "r");
5476 if (patch_script_file == NULL) {
5477 error = got_error_from_errno2("fopen",
5478 patch_script_path);
5479 goto done;
5483 error = apply_unveil(got_repo_get_path(repo), 1,
5484 got_worktree_get_root_path(worktree));
5485 if (error)
5486 goto done;
5488 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5489 if (error)
5490 goto done;
5492 cpa.patch_script_file = patch_script_file;
5493 cpa.action = "unstage";
5494 error = got_worktree_unstage(worktree, &paths, update_progress,
5495 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5496 done:
5497 if (patch_script_file && fclose(patch_script_file) == EOF &&
5498 error == NULL)
5499 error = got_error_from_errno2("fclose", patch_script_path);
5500 if (repo)
5501 got_repo_close(repo);
5502 if (worktree)
5503 got_worktree_close(worktree);
5504 TAILQ_FOREACH(pe, &paths, entry)
5505 free((char *)pe->path);
5506 got_pathlist_free(&paths);
5507 free(cwd);
5508 return error;