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 goto done;
921 if (!got_path_dir_is_empty(worktree_path)) {
922 error = got_error_path(worktree_path,
923 GOT_ERR_DIR_NOT_EMPTY);
924 goto done;
928 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
929 if (error)
930 goto done;
932 error = got_ref_open(&head_ref, repo, branch_name, 0);
933 if (error != NULL)
934 goto done;
936 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
937 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
938 goto done;
940 error = got_worktree_open(&worktree, worktree_path);
941 if (error != NULL)
942 goto done;
944 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
945 path_prefix);
946 if (error != NULL)
947 goto done;
948 if (!same_path_prefix) {
949 error = got_error(GOT_ERR_PATH_PREFIX);
950 goto done;
953 if (commit_id_str) {
954 struct got_object_id *commit_id;
955 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
956 if (error)
957 goto done;
958 error = check_linear_ancestry(commit_id,
959 got_worktree_get_base_commit_id(worktree), repo);
960 if (error != NULL) {
961 free(commit_id);
962 goto done;
964 error = check_same_branch(commit_id, head_ref, NULL, repo);
965 if (error)
966 goto done;
967 error = got_worktree_set_base_commit_id(worktree, repo,
968 commit_id);
969 free(commit_id);
970 if (error)
971 goto done;
974 error = got_pathlist_append(&paths, "", NULL);
975 if (error)
976 goto done;
977 error = got_worktree_checkout_files(worktree, &paths, repo,
978 checkout_progress, worktree_path, check_cancelled, NULL);
979 if (error != NULL)
980 goto done;
982 printf("Now shut up and hack\n");
984 done:
985 got_pathlist_free(&paths);
986 free(commit_id_str);
987 free(repo_path);
988 free(worktree_path);
989 return error;
992 __dead static void
993 usage_update(void)
995 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
996 getprogname());
997 exit(1);
1000 static const struct got_error *
1001 update_progress(void *arg, unsigned char status, const char *path)
1003 int *did_something = arg;
1005 if (status == GOT_STATUS_EXISTS)
1006 return NULL;
1008 *did_something = 1;
1010 /* Base commit bump happens silently. */
1011 if (status == GOT_STATUS_BUMP_BASE)
1012 return NULL;
1014 while (path[0] == '/')
1015 path++;
1016 printf("%c %s\n", status, path);
1017 return NULL;
1020 static const struct got_error *
1021 switch_head_ref(struct got_reference *head_ref,
1022 struct got_object_id *commit_id, struct got_worktree *worktree,
1023 struct got_repository *repo)
1025 const struct got_error *err = NULL;
1026 char *base_id_str;
1027 int ref_has_moved = 0;
1029 /* Trivial case: switching between two different references. */
1030 if (strcmp(got_ref_get_name(head_ref),
1031 got_worktree_get_head_ref_name(worktree)) != 0) {
1032 printf("Switching work tree from %s to %s\n",
1033 got_worktree_get_head_ref_name(worktree),
1034 got_ref_get_name(head_ref));
1035 return got_worktree_set_head_ref(worktree, head_ref);
1038 err = check_linear_ancestry(commit_id,
1039 got_worktree_get_base_commit_id(worktree), repo);
1040 if (err) {
1041 if (err->code != GOT_ERR_ANCESTRY)
1042 return err;
1043 ref_has_moved = 1;
1045 if (!ref_has_moved)
1046 return NULL;
1048 /* Switching to a rebased branch with the same reference name. */
1049 err = got_object_id_str(&base_id_str,
1050 got_worktree_get_base_commit_id(worktree));
1051 if (err)
1052 return err;
1053 printf("Reference %s now points at a different branch\n",
1054 got_worktree_get_head_ref_name(worktree));
1055 printf("Switching work tree from %s to %s\n", base_id_str,
1056 got_worktree_get_head_ref_name(worktree));
1057 return NULL;
1060 static const struct got_error *
1061 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1063 const struct got_error *err;
1064 int in_progress;
1066 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1067 if (err)
1068 return err;
1069 if (in_progress)
1070 return got_error(GOT_ERR_REBASING);
1072 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1073 if (err)
1074 return err;
1075 if (in_progress)
1076 return got_error(GOT_ERR_HISTEDIT_BUSY);
1078 return NULL;
1081 static const struct got_error *
1082 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1083 char *argv[], struct got_worktree *worktree)
1085 const struct got_error *err;
1086 char *path;
1087 int i;
1089 if (argc == 0) {
1090 path = strdup("");
1091 if (path == NULL)
1092 return got_error_from_errno("strdup");
1093 return got_pathlist_append(paths, path, NULL);
1096 for (i = 0; i < argc; i++) {
1097 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1098 if (err)
1099 break;
1100 err = got_pathlist_append(paths, path, NULL);
1101 if (err) {
1102 free(path);
1103 break;
1107 return err;
1110 static const struct got_error *
1111 cmd_update(int argc, char *argv[])
1113 const struct got_error *error = NULL;
1114 struct got_repository *repo = NULL;
1115 struct got_worktree *worktree = NULL;
1116 char *worktree_path = NULL;
1117 struct got_object_id *commit_id = NULL;
1118 char *commit_id_str = NULL;
1119 const char *branch_name = NULL;
1120 struct got_reference *head_ref = NULL;
1121 struct got_pathlist_head paths;
1122 struct got_pathlist_entry *pe;
1123 int ch, did_something = 0;
1125 TAILQ_INIT(&paths);
1127 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1128 switch (ch) {
1129 case 'b':
1130 branch_name = optarg;
1131 break;
1132 case 'c':
1133 commit_id_str = strdup(optarg);
1134 if (commit_id_str == NULL)
1135 return got_error_from_errno("strdup");
1136 break;
1137 default:
1138 usage_update();
1139 /* NOTREACHED */
1143 argc -= optind;
1144 argv += optind;
1146 #ifndef PROFILE
1147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1148 "unveil", NULL) == -1)
1149 err(1, "pledge");
1150 #endif
1151 worktree_path = getcwd(NULL, 0);
1152 if (worktree_path == NULL) {
1153 error = got_error_from_errno("getcwd");
1154 goto done;
1156 error = got_worktree_open(&worktree, worktree_path);
1157 if (error)
1158 goto done;
1160 error = check_rebase_or_histedit_in_progress(worktree);
1161 if (error)
1162 goto done;
1164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1165 if (error != NULL)
1166 goto done;
1168 error = apply_unveil(got_repo_get_path(repo), 0,
1169 got_worktree_get_root_path(worktree));
1170 if (error)
1171 goto done;
1173 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1174 if (error)
1175 goto done;
1177 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1178 got_worktree_get_head_ref_name(worktree), 0);
1179 if (error != NULL)
1180 goto done;
1181 if (commit_id_str == NULL) {
1182 error = got_ref_resolve(&commit_id, repo, head_ref);
1183 if (error != NULL)
1184 goto done;
1185 error = got_object_id_str(&commit_id_str, commit_id);
1186 if (error != NULL)
1187 goto done;
1188 } else {
1189 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1190 free(commit_id_str);
1191 commit_id_str = NULL;
1192 if (error)
1193 goto done;
1194 error = got_object_id_str(&commit_id_str, commit_id);
1195 if (error)
1196 goto done;
1199 if (branch_name) {
1200 struct got_object_id *head_commit_id;
1201 TAILQ_FOREACH(pe, &paths, entry) {
1202 if (pe->path_len == 0)
1203 continue;
1204 error = got_error_msg(GOT_ERR_BAD_PATH,
1205 "switching between branches requires that "
1206 "the entire work tree gets updated");
1207 goto done;
1209 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1210 if (error)
1211 goto done;
1212 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1213 free(head_commit_id);
1214 if (error != NULL)
1215 goto done;
1216 error = check_same_branch(commit_id, head_ref, NULL, repo);
1217 if (error)
1218 goto done;
1219 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1220 if (error)
1221 goto done;
1222 } else {
1223 error = check_linear_ancestry(commit_id,
1224 got_worktree_get_base_commit_id(worktree), repo);
1225 if (error != NULL) {
1226 if (error->code == GOT_ERR_ANCESTRY)
1227 error = got_error(GOT_ERR_BRANCH_MOVED);
1228 goto done;
1230 error = check_same_branch(commit_id, head_ref, NULL, repo);
1231 if (error)
1232 goto done;
1235 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1236 commit_id) != 0) {
1237 error = got_worktree_set_base_commit_id(worktree, repo,
1238 commit_id);
1239 if (error)
1240 goto done;
1243 error = got_worktree_checkout_files(worktree, &paths, repo,
1244 update_progress, &did_something, check_cancelled, NULL);
1245 if (error != NULL)
1246 goto done;
1248 if (did_something)
1249 printf("Updated to commit %s\n", commit_id_str);
1250 else
1251 printf("Already up-to-date\n");
1252 done:
1253 free(worktree_path);
1254 TAILQ_FOREACH(pe, &paths, entry)
1255 free((char *)pe->path);
1256 got_pathlist_free(&paths);
1257 free(commit_id);
1258 free(commit_id_str);
1259 return error;
1262 static const struct got_error *
1263 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1264 int diff_context, struct got_repository *repo)
1266 const struct got_error *err = NULL;
1267 struct got_tree_object *tree1 = NULL, *tree2;
1268 struct got_object_qid *qid;
1269 char *id_str1 = NULL, *id_str2;
1270 struct got_diff_blob_output_unidiff_arg arg;
1272 err = got_object_open_as_tree(&tree2, repo,
1273 got_object_commit_get_tree_id(commit));
1274 if (err)
1275 return err;
1277 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1278 if (qid != NULL) {
1279 struct got_commit_object *pcommit;
1281 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1282 if (err)
1283 return err;
1285 err = got_object_open_as_tree(&tree1, repo,
1286 got_object_commit_get_tree_id(pcommit));
1287 got_object_commit_close(pcommit);
1288 if (err)
1289 return err;
1291 err = got_object_id_str(&id_str1, qid->id);
1292 if (err)
1293 return err;
1296 err = got_object_id_str(&id_str2, id);
1297 if (err)
1298 goto done;
1300 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1301 arg.diff_context = diff_context;
1302 arg.outfile = stdout;
1303 err = got_diff_tree(tree1, tree2, "", "", repo,
1304 got_diff_blob_output_unidiff, &arg, 1);
1305 done:
1306 if (tree1)
1307 got_object_tree_close(tree1);
1308 got_object_tree_close(tree2);
1309 free(id_str1);
1310 free(id_str2);
1311 return err;
1314 static char *
1315 get_datestr(time_t *time, char *datebuf)
1317 char *p, *s = ctime_r(time, datebuf);
1318 p = strchr(s, '\n');
1319 if (p)
1320 *p = '\0';
1321 return s;
1324 static const struct got_error *
1325 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1326 struct got_repository *repo, int show_patch, int diff_context,
1327 struct got_reflist_head *refs)
1329 const struct got_error *err = NULL;
1330 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1331 char datebuf[26];
1332 time_t committer_time;
1333 const char *author, *committer;
1334 char *refs_str = NULL;
1335 struct got_reflist_entry *re;
1337 SIMPLEQ_FOREACH(re, refs, entry) {
1338 char *s;
1339 const char *name;
1340 if (got_object_id_cmp(re->id, id) != 0)
1341 continue;
1342 name = got_ref_get_name(re->ref);
1343 if (strcmp(name, GOT_REF_HEAD) == 0)
1344 continue;
1345 if (strncmp(name, "refs/", 5) == 0)
1346 name += 5;
1347 if (strncmp(name, "got/", 4) == 0)
1348 continue;
1349 if (strncmp(name, "heads/", 6) == 0)
1350 name += 6;
1351 if (strncmp(name, "remotes/", 8) == 0)
1352 name += 8;
1353 s = refs_str;
1354 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1355 name) == -1) {
1356 err = got_error_from_errno("asprintf");
1357 free(s);
1358 break;
1360 free(s);
1362 err = got_object_id_str(&id_str, id);
1363 if (err)
1364 return err;
1366 printf("-----------------------------------------------\n");
1367 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1368 refs_str ? refs_str : "", refs_str ? ")" : "");
1369 free(id_str);
1370 id_str = NULL;
1371 free(refs_str);
1372 refs_str = NULL;
1373 printf("from: %s\n", got_object_commit_get_author(commit));
1374 committer_time = got_object_commit_get_committer_time(commit);
1375 datestr = get_datestr(&committer_time, datebuf);
1376 printf("date: %s UTC\n", datestr);
1377 author = got_object_commit_get_author(commit);
1378 committer = got_object_commit_get_committer(commit);
1379 if (strcmp(author, committer) != 0)
1380 printf("via: %s\n", committer);
1381 if (got_object_commit_get_nparents(commit) > 1) {
1382 const struct got_object_id_queue *parent_ids;
1383 struct got_object_qid *qid;
1384 int n = 1;
1385 parent_ids = got_object_commit_get_parent_ids(commit);
1386 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1387 err = got_object_id_str(&id_str, qid->id);
1388 if (err)
1389 return err;
1390 printf("parent %d: %s\n", n++, id_str);
1391 free(id_str);
1395 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1396 if (logmsg0 == NULL)
1397 return got_error_from_errno("strdup");
1399 logmsg = logmsg0;
1400 do {
1401 line = strsep(&logmsg, "\n");
1402 if (line)
1403 printf(" %s\n", line);
1404 } while (line);
1405 free(logmsg0);
1407 if (show_patch) {
1408 err = print_patch(commit, id, diff_context, repo);
1409 if (err == 0)
1410 printf("\n");
1413 if (fflush(stdout) != 0 && err == NULL)
1414 err = got_error_from_errno("fflush");
1415 return err;
1418 static const struct got_error *
1419 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1420 char *path, int show_patch, int diff_context, int limit,
1421 int first_parent_traversal, struct got_reflist_head *refs)
1423 const struct got_error *err;
1424 struct got_commit_graph *graph;
1426 err = got_commit_graph_open(&graph, root_id, path,
1427 first_parent_traversal, repo);
1428 if (err)
1429 return err;
1430 err = got_commit_graph_iter_start(graph, root_id, repo);
1431 if (err)
1432 goto done;
1433 for (;;) {
1434 struct got_commit_object *commit;
1435 struct got_object_id *id;
1437 if (sigint_received || sigpipe_received)
1438 break;
1440 err = got_commit_graph_iter_next(&id, graph);
1441 if (err) {
1442 if (err->code == GOT_ERR_ITER_COMPLETED) {
1443 err = NULL;
1444 break;
1446 if (err->code != GOT_ERR_ITER_NEED_MORE)
1447 break;
1448 err = got_commit_graph_fetch_commits(graph, 1, repo);
1449 if (err)
1450 break;
1451 else
1452 continue;
1454 if (id == NULL)
1455 break;
1457 err = got_object_open_as_commit(&commit, repo, id);
1458 if (err)
1459 break;
1460 err = print_commit(commit, id, repo, show_patch, diff_context,
1461 refs);
1462 got_object_commit_close(commit);
1463 if (err || (limit && --limit == 0))
1464 break;
1466 done:
1467 got_commit_graph_close(graph);
1468 return err;
1471 __dead static void
1472 usage_log(void)
1474 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1475 "[-r repository-path] [path]\n", getprogname());
1476 exit(1);
1479 static const struct got_error *
1480 cmd_log(int argc, char *argv[])
1482 const struct got_error *error;
1483 struct got_repository *repo = NULL;
1484 struct got_worktree *worktree = NULL;
1485 struct got_commit_object *commit = NULL;
1486 struct got_object_id *id = NULL;
1487 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1488 char *start_commit = NULL;
1489 int diff_context = 3, ch;
1490 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1491 const char *errstr;
1492 struct got_reflist_head refs;
1494 SIMPLEQ_INIT(&refs);
1496 #ifndef PROFILE
1497 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1498 NULL)
1499 == -1)
1500 err(1, "pledge");
1501 #endif
1503 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1504 switch (ch) {
1505 case 'p':
1506 show_patch = 1;
1507 break;
1508 case 'c':
1509 start_commit = optarg;
1510 break;
1511 case 'C':
1512 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1513 &errstr);
1514 if (errstr != NULL)
1515 err(1, "-C option %s", errstr);
1516 break;
1517 case 'l':
1518 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1519 if (errstr != NULL)
1520 err(1, "-l option %s", errstr);
1521 break;
1522 case 'f':
1523 first_parent_traversal = 1;
1524 break;
1525 case 'r':
1526 repo_path = realpath(optarg, NULL);
1527 if (repo_path == NULL)
1528 err(1, "-r option");
1529 got_path_strip_trailing_slashes(repo_path);
1530 break;
1531 default:
1532 usage_log();
1533 /* NOTREACHED */
1537 argc -= optind;
1538 argv += optind;
1540 cwd = getcwd(NULL, 0);
1541 if (cwd == NULL) {
1542 error = got_error_from_errno("getcwd");
1543 goto done;
1546 error = got_worktree_open(&worktree, cwd);
1547 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1548 goto done;
1549 error = NULL;
1551 if (argc == 0) {
1552 path = strdup("");
1553 if (path == NULL) {
1554 error = got_error_from_errno("strdup");
1555 goto done;
1557 } else if (argc == 1) {
1558 if (worktree) {
1559 error = got_worktree_resolve_path(&path, worktree,
1560 argv[0]);
1561 if (error)
1562 goto done;
1563 } else {
1564 path = strdup(argv[0]);
1565 if (path == NULL) {
1566 error = got_error_from_errno("strdup");
1567 goto done;
1570 } else
1571 usage_log();
1573 if (repo_path == NULL) {
1574 repo_path = worktree ?
1575 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1577 if (repo_path == NULL) {
1578 error = got_error_from_errno("strdup");
1579 goto done;
1582 error = got_repo_open(&repo, repo_path);
1583 if (error != NULL)
1584 goto done;
1586 error = apply_unveil(got_repo_get_path(repo), 1,
1587 worktree ? got_worktree_get_root_path(worktree) : NULL);
1588 if (error)
1589 goto done;
1591 if (start_commit == NULL) {
1592 struct got_reference *head_ref;
1593 error = got_ref_open(&head_ref, repo,
1594 worktree ? got_worktree_get_head_ref_name(worktree)
1595 : GOT_REF_HEAD, 0);
1596 if (error != NULL)
1597 return error;
1598 error = got_ref_resolve(&id, repo, head_ref);
1599 got_ref_close(head_ref);
1600 if (error != NULL)
1601 return error;
1602 error = got_object_open_as_commit(&commit, repo, id);
1603 } else {
1604 struct got_reference *ref;
1605 error = got_ref_open(&ref, repo, start_commit, 0);
1606 if (error == NULL) {
1607 int obj_type;
1608 error = got_ref_resolve(&id, repo, ref);
1609 got_ref_close(ref);
1610 if (error != NULL)
1611 goto done;
1612 error = got_object_get_type(&obj_type, repo, id);
1613 if (error != NULL)
1614 goto done;
1615 if (obj_type == GOT_OBJ_TYPE_TAG) {
1616 struct got_tag_object *tag;
1617 error = got_object_open_as_tag(&tag, repo, id);
1618 if (error != NULL)
1619 goto done;
1620 if (got_object_tag_get_object_type(tag) !=
1621 GOT_OBJ_TYPE_COMMIT) {
1622 got_object_tag_close(tag);
1623 error = got_error(GOT_ERR_OBJ_TYPE);
1624 goto done;
1626 free(id);
1627 id = got_object_id_dup(
1628 got_object_tag_get_object_id(tag));
1629 if (id == NULL)
1630 error = got_error_from_errno(
1631 "got_object_id_dup");
1632 got_object_tag_close(tag);
1633 if (error)
1634 goto done;
1635 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1636 error = got_error(GOT_ERR_OBJ_TYPE);
1637 goto done;
1639 error = got_object_open_as_commit(&commit, repo, id);
1640 if (error != NULL)
1641 goto done;
1643 if (commit == NULL) {
1644 error = got_repo_match_object_id_prefix(&id,
1645 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1646 if (error != NULL)
1647 return error;
1650 if (error != NULL)
1651 goto done;
1653 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1654 if (error != NULL)
1655 goto done;
1656 if (in_repo_path) {
1657 free(path);
1658 path = in_repo_path;
1661 error = got_ref_list(&refs, repo);
1662 if (error)
1663 goto done;
1665 error = print_commits(id, repo, path, show_patch,
1666 diff_context, limit, first_parent_traversal, &refs);
1667 done:
1668 free(path);
1669 free(repo_path);
1670 free(cwd);
1671 free(id);
1672 if (worktree)
1673 got_worktree_close(worktree);
1674 if (repo) {
1675 const struct got_error *repo_error;
1676 repo_error = got_repo_close(repo);
1677 if (error == NULL)
1678 error = repo_error;
1680 got_ref_list_free(&refs);
1681 return error;
1684 __dead static void
1685 usage_diff(void)
1687 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1688 "[object1 object2 | path]\n", getprogname());
1689 exit(1);
1692 struct print_diff_arg {
1693 struct got_repository *repo;
1694 struct got_worktree *worktree;
1695 int diff_context;
1696 const char *id_str;
1697 int header_shown;
1698 int diff_staged;
1701 static const struct got_error *
1702 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1703 const char *path, struct got_object_id *blob_id,
1704 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1706 struct print_diff_arg *a = arg;
1707 const struct got_error *err = NULL;
1708 struct got_blob_object *blob1 = NULL;
1709 FILE *f2 = NULL;
1710 char *abspath = NULL;
1711 struct stat sb;
1713 if (a->diff_staged) {
1714 if (staged_status != GOT_STATUS_MODIFY &&
1715 staged_status != GOT_STATUS_ADD &&
1716 staged_status != GOT_STATUS_DELETE)
1717 return NULL;
1718 } else {
1719 if (staged_status == GOT_STATUS_DELETE)
1720 return NULL;
1721 if (status != GOT_STATUS_MODIFY &&
1722 status != GOT_STATUS_ADD &&
1723 status != GOT_STATUS_DELETE &&
1724 status != GOT_STATUS_CONFLICT)
1725 return NULL;
1728 if (!a->header_shown) {
1729 printf("diff %s %s%s\n", a->id_str,
1730 got_worktree_get_root_path(a->worktree),
1731 a->diff_staged ? " (staged changes)" : "");
1732 a->header_shown = 1;
1735 if (a->diff_staged) {
1736 const char *label1 = NULL, *label2 = NULL;
1737 switch (staged_status) {
1738 case GOT_STATUS_MODIFY:
1739 label1 = path;
1740 label2 = path;
1741 break;
1742 case GOT_STATUS_ADD:
1743 label2 = path;
1744 break;
1745 case GOT_STATUS_DELETE:
1746 label1 = path;
1747 break;
1748 default:
1749 return got_error(GOT_ERR_FILE_STATUS);
1751 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1752 label1, label2, a->diff_context, a->repo, stdout);
1755 if (staged_status == GOT_STATUS_ADD ||
1756 staged_status == GOT_STATUS_MODIFY)
1757 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1758 8192);
1759 else if (status != GOT_STATUS_ADD)
1760 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1761 if (err)
1762 goto done;
1764 if (status != GOT_STATUS_DELETE) {
1765 if (asprintf(&abspath, "%s/%s",
1766 got_worktree_get_root_path(a->worktree), path) == -1) {
1767 err = got_error_from_errno("asprintf");
1768 goto done;
1771 f2 = fopen(abspath, "r");
1772 if (f2 == NULL) {
1773 err = got_error_from_errno2("fopen", abspath);
1774 goto done;
1776 if (lstat(abspath, &sb) == -1) {
1777 err = got_error_from_errno2("lstat", abspath);
1778 goto done;
1780 } else
1781 sb.st_size = 0;
1783 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1784 stdout);
1785 done:
1786 if (blob1)
1787 got_object_blob_close(blob1);
1788 if (f2 && fclose(f2) != 0 && err == NULL)
1789 err = got_error_from_errno("fclose");
1790 free(abspath);
1791 return err;
1794 static const struct got_error *
1795 cmd_diff(int argc, char *argv[])
1797 const struct got_error *error;
1798 struct got_repository *repo = NULL;
1799 struct got_worktree *worktree = NULL;
1800 char *cwd = NULL, *repo_path = NULL;
1801 struct got_object_id *id1 = NULL, *id2 = NULL;
1802 const char *id_str1 = NULL, *id_str2 = NULL;
1803 char *label1 = NULL, *label2 = NULL;
1804 int type1, type2;
1805 int diff_context = 3, diff_staged = 0, ch;
1806 const char *errstr;
1807 char *path = NULL;
1809 #ifndef PROFILE
1810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1811 NULL) == -1)
1812 err(1, "pledge");
1813 #endif
1815 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1816 switch (ch) {
1817 case 'C':
1818 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1819 if (errstr != NULL)
1820 err(1, "-C option %s", errstr);
1821 break;
1822 case 'r':
1823 repo_path = realpath(optarg, NULL);
1824 if (repo_path == NULL)
1825 err(1, "-r option");
1826 got_path_strip_trailing_slashes(repo_path);
1827 break;
1828 case 's':
1829 diff_staged = 1;
1830 break;
1831 default:
1832 usage_diff();
1833 /* NOTREACHED */
1837 argc -= optind;
1838 argv += optind;
1840 cwd = getcwd(NULL, 0);
1841 if (cwd == NULL) {
1842 error = got_error_from_errno("getcwd");
1843 goto done;
1845 error = got_worktree_open(&worktree, cwd);
1846 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1847 goto done;
1848 if (argc <= 1) {
1849 if (worktree == NULL) {
1850 error = got_error(GOT_ERR_NOT_WORKTREE);
1851 goto done;
1853 if (repo_path)
1854 errx(1,
1855 "-r option can't be used when diffing a work tree");
1856 repo_path = strdup(got_worktree_get_repo_path(worktree));
1857 if (repo_path == NULL) {
1858 error = got_error_from_errno("strdup");
1859 goto done;
1861 if (argc == 1) {
1862 error = got_worktree_resolve_path(&path, worktree,
1863 argv[0]);
1864 if (error)
1865 goto done;
1866 } else {
1867 path = strdup("");
1868 if (path == NULL) {
1869 error = got_error_from_errno("strdup");
1870 goto done;
1873 } else if (argc == 2) {
1874 if (diff_staged)
1875 errx(1, "-s option can't be used when diffing "
1876 "objects in repository");
1877 id_str1 = argv[0];
1878 id_str2 = argv[1];
1879 if (worktree && repo_path == NULL) {
1880 repo_path =
1881 strdup(got_worktree_get_repo_path(worktree));
1882 if (repo_path == NULL) {
1883 error = got_error_from_errno("strdup");
1884 goto done;
1887 } else
1888 usage_diff();
1890 if (repo_path == NULL) {
1891 repo_path = getcwd(NULL, 0);
1892 if (repo_path == NULL)
1893 return got_error_from_errno("getcwd");
1896 error = got_repo_open(&repo, repo_path);
1897 free(repo_path);
1898 if (error != NULL)
1899 goto done;
1901 error = apply_unveil(got_repo_get_path(repo), 1,
1902 worktree ? got_worktree_get_root_path(worktree) : NULL);
1903 if (error)
1904 goto done;
1906 if (argc <= 1) {
1907 struct print_diff_arg arg;
1908 struct got_pathlist_head paths;
1909 char *id_str;
1911 TAILQ_INIT(&paths);
1913 error = got_object_id_str(&id_str,
1914 got_worktree_get_base_commit_id(worktree));
1915 if (error)
1916 goto done;
1917 arg.repo = repo;
1918 arg.worktree = worktree;
1919 arg.diff_context = diff_context;
1920 arg.id_str = id_str;
1921 arg.header_shown = 0;
1922 arg.diff_staged = diff_staged;
1924 error = got_pathlist_append(&paths, path, NULL);
1925 if (error)
1926 goto done;
1928 error = got_worktree_status(worktree, &paths, repo, print_diff,
1929 &arg, check_cancelled, NULL);
1930 free(id_str);
1931 got_pathlist_free(&paths);
1932 goto done;
1935 error = got_repo_match_object_id_prefix(&id1, id_str1,
1936 GOT_OBJ_TYPE_ANY, repo);
1937 if (error) {
1938 struct got_reference *ref;
1939 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1940 goto done;
1941 error = got_ref_open(&ref, repo, id_str1, 0);
1942 if (error != NULL)
1943 goto done;
1944 label1 = strdup(got_ref_get_name(ref));
1945 if (label1 == NULL) {
1946 error = got_error_from_errno("strdup");
1947 goto done;
1949 error = got_ref_resolve(&id1, repo, ref);
1950 got_ref_close(ref);
1951 if (error != NULL)
1952 goto done;
1953 } else {
1954 error = got_object_id_str(&label1, id1);
1955 if (label1 == NULL) {
1956 error = got_error_from_errno("strdup");
1957 goto done;
1961 error = got_repo_match_object_id_prefix(&id2, id_str2,
1962 GOT_OBJ_TYPE_ANY, repo);
1963 if (error) {
1964 struct got_reference *ref;
1965 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1966 goto done;
1967 error = got_ref_open(&ref, repo, id_str2, 0);
1968 if (error != NULL)
1969 goto done;
1970 label2 = strdup(got_ref_get_name(ref));
1971 if (label2 == NULL) {
1972 error = got_error_from_errno("strdup");
1973 goto done;
1975 error = got_ref_resolve(&id2, repo, ref);
1976 got_ref_close(ref);
1977 if (error != NULL)
1978 goto done;
1979 } else {
1980 error = got_object_id_str(&label2, id2);
1981 if (label2 == NULL) {
1982 error = got_error_from_errno("strdup");
1983 goto done;
1987 error = got_object_get_type(&type1, repo, id1);
1988 if (error)
1989 goto done;
1991 error = got_object_get_type(&type2, repo, id2);
1992 if (error)
1993 goto done;
1995 if (type1 != type2) {
1996 error = got_error(GOT_ERR_OBJ_TYPE);
1997 goto done;
2000 switch (type1) {
2001 case GOT_OBJ_TYPE_BLOB:
2002 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2003 diff_context, repo, stdout);
2004 break;
2005 case GOT_OBJ_TYPE_TREE:
2006 error = got_diff_objects_as_trees(id1, id2, "", "",
2007 diff_context, repo, stdout);
2008 break;
2009 case GOT_OBJ_TYPE_COMMIT:
2010 printf("diff %s %s\n", label1, label2);
2011 error = got_diff_objects_as_commits(id1, id2, diff_context,
2012 repo, stdout);
2013 break;
2014 default:
2015 error = got_error(GOT_ERR_OBJ_TYPE);
2018 done:
2019 free(label1);
2020 free(label2);
2021 free(id1);
2022 free(id2);
2023 free(path);
2024 if (worktree)
2025 got_worktree_close(worktree);
2026 if (repo) {
2027 const struct got_error *repo_error;
2028 repo_error = got_repo_close(repo);
2029 if (error == NULL)
2030 error = repo_error;
2032 return error;
2035 __dead static void
2036 usage_blame(void)
2038 fprintf(stderr,
2039 "usage: %s blame [-c commit] [-r repository-path] path\n",
2040 getprogname());
2041 exit(1);
2044 static const struct got_error *
2045 cmd_blame(int argc, char *argv[])
2047 const struct got_error *error;
2048 struct got_repository *repo = NULL;
2049 struct got_worktree *worktree = NULL;
2050 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2051 struct got_object_id *commit_id = NULL;
2052 char *commit_id_str = NULL;
2053 int ch;
2055 #ifndef PROFILE
2056 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2057 NULL) == -1)
2058 err(1, "pledge");
2059 #endif
2061 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2062 switch (ch) {
2063 case 'c':
2064 commit_id_str = optarg;
2065 break;
2066 case 'r':
2067 repo_path = realpath(optarg, NULL);
2068 if (repo_path == NULL)
2069 err(1, "-r option");
2070 got_path_strip_trailing_slashes(repo_path);
2071 break;
2072 default:
2073 usage_blame();
2074 /* NOTREACHED */
2078 argc -= optind;
2079 argv += optind;
2081 if (argc == 1)
2082 path = argv[0];
2083 else
2084 usage_blame();
2086 cwd = getcwd(NULL, 0);
2087 if (cwd == NULL) {
2088 error = got_error_from_errno("getcwd");
2089 goto done;
2091 if (repo_path == NULL) {
2092 error = got_worktree_open(&worktree, cwd);
2093 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2094 goto done;
2095 else
2096 error = NULL;
2097 if (worktree) {
2098 repo_path =
2099 strdup(got_worktree_get_repo_path(worktree));
2100 if (repo_path == NULL)
2101 error = got_error_from_errno("strdup");
2102 if (error)
2103 goto done;
2104 } else {
2105 repo_path = strdup(cwd);
2106 if (repo_path == NULL) {
2107 error = got_error_from_errno("strdup");
2108 goto done;
2113 error = got_repo_open(&repo, repo_path);
2114 if (error != NULL)
2115 goto done;
2117 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2118 if (error)
2119 goto done;
2121 if (worktree) {
2122 const char *prefix = got_worktree_get_path_prefix(worktree);
2123 char *p, *worktree_subdir = cwd +
2124 strlen(got_worktree_get_root_path(worktree));
2125 if (asprintf(&p, "%s%s%s%s%s",
2126 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2127 worktree_subdir, worktree_subdir[0] ? "/" : "",
2128 path) == -1) {
2129 error = got_error_from_errno("asprintf");
2130 goto done;
2132 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2133 free(p);
2134 } else {
2135 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2137 if (error)
2138 goto done;
2140 if (commit_id_str == NULL) {
2141 struct got_reference *head_ref;
2142 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2143 if (error != NULL)
2144 goto done;
2145 error = got_ref_resolve(&commit_id, repo, head_ref);
2146 got_ref_close(head_ref);
2147 if (error != NULL)
2148 goto done;
2149 } else {
2150 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2151 if (error)
2152 goto done;
2155 error = got_blame(in_repo_path, commit_id, repo, stdout);
2156 done:
2157 free(in_repo_path);
2158 free(repo_path);
2159 free(cwd);
2160 free(commit_id);
2161 if (worktree)
2162 got_worktree_close(worktree);
2163 if (repo) {
2164 const struct got_error *repo_error;
2165 repo_error = got_repo_close(repo);
2166 if (error == NULL)
2167 error = repo_error;
2169 return error;
2172 __dead static void
2173 usage_tree(void)
2175 fprintf(stderr,
2176 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2177 getprogname());
2178 exit(1);
2181 static void
2182 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2183 const char *root_path)
2185 int is_root_path = (strcmp(path, root_path) == 0);
2187 path += strlen(root_path);
2188 while (path[0] == '/')
2189 path++;
2191 printf("%s%s%s%s%s\n", id ? id : "", path,
2192 is_root_path ? "" : "/", te->name,
2193 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2196 static const struct got_error *
2197 print_tree(const char *path, struct got_object_id *commit_id,
2198 int show_ids, int recurse, const char *root_path,
2199 struct got_repository *repo)
2201 const struct got_error *err = NULL;
2202 struct got_object_id *tree_id = NULL;
2203 struct got_tree_object *tree = NULL;
2204 const struct got_tree_entries *entries;
2205 struct got_tree_entry *te;
2207 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2208 if (err)
2209 goto done;
2211 err = got_object_open_as_tree(&tree, repo, tree_id);
2212 if (err)
2213 goto done;
2214 entries = got_object_tree_get_entries(tree);
2215 te = SIMPLEQ_FIRST(&entries->head);
2216 while (te) {
2217 char *id = NULL;
2219 if (sigint_received || sigpipe_received)
2220 break;
2222 if (show_ids) {
2223 char *id_str;
2224 err = got_object_id_str(&id_str, te->id);
2225 if (err)
2226 goto done;
2227 if (asprintf(&id, "%s ", id_str) == -1) {
2228 err = got_error_from_errno("asprintf");
2229 free(id_str);
2230 goto done;
2232 free(id_str);
2234 print_entry(te, id, path, root_path);
2235 free(id);
2237 if (recurse && S_ISDIR(te->mode)) {
2238 char *child_path;
2239 if (asprintf(&child_path, "%s%s%s", path,
2240 path[0] == '/' && path[1] == '\0' ? "" : "/",
2241 te->name) == -1) {
2242 err = got_error_from_errno("asprintf");
2243 goto done;
2245 err = print_tree(child_path, commit_id, show_ids, 1,
2246 root_path, repo);
2247 free(child_path);
2248 if (err)
2249 goto done;
2252 te = SIMPLEQ_NEXT(te, entry);
2254 done:
2255 if (tree)
2256 got_object_tree_close(tree);
2257 free(tree_id);
2258 return err;
2261 static const struct got_error *
2262 cmd_tree(int argc, char *argv[])
2264 const struct got_error *error;
2265 struct got_repository *repo = NULL;
2266 struct got_worktree *worktree = NULL;
2267 const char *path;
2268 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2269 struct got_object_id *commit_id = NULL;
2270 char *commit_id_str = NULL;
2271 int show_ids = 0, recurse = 0;
2272 int ch;
2274 #ifndef PROFILE
2275 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2276 NULL) == -1)
2277 err(1, "pledge");
2278 #endif
2280 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2281 switch (ch) {
2282 case 'c':
2283 commit_id_str = optarg;
2284 break;
2285 case 'r':
2286 repo_path = realpath(optarg, NULL);
2287 if (repo_path == NULL)
2288 err(1, "-r option");
2289 got_path_strip_trailing_slashes(repo_path);
2290 break;
2291 case 'i':
2292 show_ids = 1;
2293 break;
2294 case 'R':
2295 recurse = 1;
2296 break;
2297 default:
2298 usage_tree();
2299 /* NOTREACHED */
2303 argc -= optind;
2304 argv += optind;
2306 if (argc == 1)
2307 path = argv[0];
2308 else if (argc > 1)
2309 usage_tree();
2310 else
2311 path = NULL;
2313 cwd = getcwd(NULL, 0);
2314 if (cwd == NULL) {
2315 error = got_error_from_errno("getcwd");
2316 goto done;
2318 if (repo_path == NULL) {
2319 error = got_worktree_open(&worktree, cwd);
2320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2321 goto done;
2322 else
2323 error = NULL;
2324 if (worktree) {
2325 repo_path =
2326 strdup(got_worktree_get_repo_path(worktree));
2327 if (repo_path == NULL)
2328 error = got_error_from_errno("strdup");
2329 if (error)
2330 goto done;
2331 } else {
2332 repo_path = strdup(cwd);
2333 if (repo_path == NULL) {
2334 error = got_error_from_errno("strdup");
2335 goto done;
2340 error = got_repo_open(&repo, repo_path);
2341 if (error != NULL)
2342 goto done;
2344 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2345 if (error)
2346 goto done;
2348 if (path == NULL) {
2349 if (worktree) {
2350 char *p, *worktree_subdir = cwd +
2351 strlen(got_worktree_get_root_path(worktree));
2352 if (asprintf(&p, "%s/%s",
2353 got_worktree_get_path_prefix(worktree),
2354 worktree_subdir) == -1) {
2355 error = got_error_from_errno("asprintf");
2356 goto done;
2358 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2359 free(p);
2360 if (error)
2361 goto done;
2362 } else
2363 path = "/";
2365 if (in_repo_path == NULL) {
2366 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2367 if (error != NULL)
2368 goto done;
2371 if (commit_id_str == NULL) {
2372 struct got_reference *head_ref;
2373 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2374 if (error != NULL)
2375 goto done;
2376 error = got_ref_resolve(&commit_id, repo, head_ref);
2377 got_ref_close(head_ref);
2378 if (error != NULL)
2379 goto done;
2380 } else {
2381 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2382 if (error)
2383 goto done;
2386 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2387 in_repo_path, repo);
2388 done:
2389 free(in_repo_path);
2390 free(repo_path);
2391 free(cwd);
2392 free(commit_id);
2393 if (worktree)
2394 got_worktree_close(worktree);
2395 if (repo) {
2396 const struct got_error *repo_error;
2397 repo_error = got_repo_close(repo);
2398 if (error == NULL)
2399 error = repo_error;
2401 return error;
2404 __dead static void
2405 usage_status(void)
2407 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2408 exit(1);
2411 static const struct got_error *
2412 print_status(void *arg, unsigned char status, unsigned char staged_status,
2413 const char *path, struct got_object_id *blob_id,
2414 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2416 if (status == staged_status && (status == GOT_STATUS_DELETE))
2417 status = GOT_STATUS_NO_CHANGE;
2418 printf("%c%c %s\n", status, staged_status, path);
2419 return NULL;
2422 static const struct got_error *
2423 cmd_status(int argc, char *argv[])
2425 const struct got_error *error = NULL;
2426 struct got_repository *repo = NULL;
2427 struct got_worktree *worktree = NULL;
2428 char *cwd = NULL;
2429 struct got_pathlist_head paths;
2430 struct got_pathlist_entry *pe;
2431 int ch;
2433 TAILQ_INIT(&paths);
2435 while ((ch = getopt(argc, argv, "")) != -1) {
2436 switch (ch) {
2437 default:
2438 usage_status();
2439 /* NOTREACHED */
2443 argc -= optind;
2444 argv += optind;
2446 #ifndef PROFILE
2447 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2448 NULL) == -1)
2449 err(1, "pledge");
2450 #endif
2451 cwd = getcwd(NULL, 0);
2452 if (cwd == NULL) {
2453 error = got_error_from_errno("getcwd");
2454 goto done;
2457 error = got_worktree_open(&worktree, cwd);
2458 if (error != NULL)
2459 goto done;
2461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2462 if (error != NULL)
2463 goto done;
2465 error = apply_unveil(got_repo_get_path(repo), 1,
2466 got_worktree_get_root_path(worktree));
2467 if (error)
2468 goto done;
2470 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2471 if (error)
2472 goto done;
2474 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2475 check_cancelled, NULL);
2476 done:
2477 TAILQ_FOREACH(pe, &paths, entry)
2478 free((char *)pe->path);
2479 got_pathlist_free(&paths);
2480 free(cwd);
2481 return error;
2484 __dead static void
2485 usage_ref(void)
2487 fprintf(stderr,
2488 "usage: %s ref [-r repository] -l | -d name | name target\n",
2489 getprogname());
2490 exit(1);
2493 static const struct got_error *
2494 list_refs(struct got_repository *repo)
2496 static const struct got_error *err = NULL;
2497 struct got_reflist_head refs;
2498 struct got_reflist_entry *re;
2500 SIMPLEQ_INIT(&refs);
2501 err = got_ref_list(&refs, repo);
2502 if (err)
2503 return err;
2505 SIMPLEQ_FOREACH(re, &refs, entry) {
2506 char *refstr;
2507 refstr = got_ref_to_str(re->ref);
2508 if (refstr == NULL)
2509 return got_error_from_errno("got_ref_to_str");
2510 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2511 free(refstr);
2514 got_ref_list_free(&refs);
2515 return NULL;
2518 static const struct got_error *
2519 delete_ref(struct got_repository *repo, const char *refname)
2521 const struct got_error *err = NULL;
2522 struct got_reference *ref;
2524 err = got_ref_open(&ref, repo, refname, 0);
2525 if (err)
2526 return err;
2528 err = got_ref_delete(ref, repo);
2529 got_ref_close(ref);
2530 return err;
2533 static const struct got_error *
2534 add_ref(struct got_repository *repo, const char *refname, const char *target)
2536 const struct got_error *err = NULL;
2537 struct got_object_id *id;
2538 struct got_reference *ref = NULL;
2541 * Don't let the user create a reference named '-'.
2542 * While technically a valid reference name, this case is usually
2543 * an unintended typo.
2545 if (refname[0] == '-' && refname[1] == '\0')
2546 return got_error(GOT_ERR_BAD_REF_NAME);
2548 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2549 repo);
2550 if (err) {
2551 struct got_reference *target_ref;
2553 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2554 return err;
2555 err = got_ref_open(&target_ref, repo, target, 0);
2556 if (err)
2557 return err;
2558 err = got_ref_resolve(&id, repo, target_ref);
2559 got_ref_close(target_ref);
2560 if (err)
2561 return err;
2564 err = got_ref_alloc(&ref, refname, id);
2565 if (err)
2566 goto done;
2568 err = got_ref_write(ref, repo);
2569 done:
2570 if (ref)
2571 got_ref_close(ref);
2572 free(id);
2573 return err;
2576 static const struct got_error *
2577 cmd_ref(int argc, char *argv[])
2579 const struct got_error *error = NULL;
2580 struct got_repository *repo = NULL;
2581 struct got_worktree *worktree = NULL;
2582 char *cwd = NULL, *repo_path = NULL;
2583 int ch, do_list = 0;
2584 const char *delref = NULL;
2586 /* TODO: Add -s option for adding symbolic references. */
2587 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2588 switch (ch) {
2589 case 'd':
2590 delref = optarg;
2591 break;
2592 case 'r':
2593 repo_path = realpath(optarg, NULL);
2594 if (repo_path == NULL)
2595 err(1, "-r option");
2596 got_path_strip_trailing_slashes(repo_path);
2597 break;
2598 case 'l':
2599 do_list = 1;
2600 break;
2601 default:
2602 usage_ref();
2603 /* NOTREACHED */
2607 if (do_list && delref)
2608 errx(1, "-l and -d options are mutually exclusive\n");
2610 argc -= optind;
2611 argv += optind;
2613 if (do_list || delref) {
2614 if (argc > 0)
2615 usage_ref();
2616 } else if (argc != 2)
2617 usage_ref();
2619 #ifndef PROFILE
2620 if (do_list) {
2621 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2622 NULL) == -1)
2623 err(1, "pledge");
2624 } else {
2625 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2626 "sendfd unveil", NULL) == -1)
2627 err(1, "pledge");
2629 #endif
2630 cwd = getcwd(NULL, 0);
2631 if (cwd == NULL) {
2632 error = got_error_from_errno("getcwd");
2633 goto done;
2636 if (repo_path == NULL) {
2637 error = got_worktree_open(&worktree, cwd);
2638 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2639 goto done;
2640 else
2641 error = NULL;
2642 if (worktree) {
2643 repo_path =
2644 strdup(got_worktree_get_repo_path(worktree));
2645 if (repo_path == NULL)
2646 error = got_error_from_errno("strdup");
2647 if (error)
2648 goto done;
2649 } else {
2650 repo_path = strdup(cwd);
2651 if (repo_path == NULL) {
2652 error = got_error_from_errno("strdup");
2653 goto done;
2658 error = got_repo_open(&repo, repo_path);
2659 if (error != NULL)
2660 goto done;
2662 error = apply_unveil(got_repo_get_path(repo), do_list,
2663 worktree ? got_worktree_get_root_path(worktree) : NULL);
2664 if (error)
2665 goto done;
2667 if (do_list)
2668 error = list_refs(repo);
2669 else if (delref)
2670 error = delete_ref(repo, delref);
2671 else
2672 error = add_ref(repo, argv[0], argv[1]);
2673 done:
2674 if (repo)
2675 got_repo_close(repo);
2676 if (worktree)
2677 got_worktree_close(worktree);
2678 free(cwd);
2679 free(repo_path);
2680 return error;
2683 __dead static void
2684 usage_branch(void)
2686 fprintf(stderr,
2687 "usage: %s branch [-r repository] -l | -d name | "
2688 "name [base-branch]\n", getprogname());
2689 exit(1);
2692 static const struct got_error *
2693 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2695 static const struct got_error *err = NULL;
2696 struct got_reflist_head refs;
2697 struct got_reflist_entry *re;
2699 SIMPLEQ_INIT(&refs);
2701 err = got_ref_list(&refs, repo);
2702 if (err)
2703 return err;
2705 SIMPLEQ_FOREACH(re, &refs, entry) {
2706 const char *refname, *marker = " ";
2707 char *refstr;
2708 refname = got_ref_get_name(re->ref);
2709 if (strncmp(refname, "refs/heads/", 11) != 0)
2710 continue;
2711 if (worktree && strcmp(refname,
2712 got_worktree_get_head_ref_name(worktree)) == 0) {
2713 struct got_object_id *id = NULL;
2714 err = got_ref_resolve(&id, repo, re->ref);
2715 if (err)
2716 return err;
2717 if (got_object_id_cmp(id,
2718 got_worktree_get_base_commit_id(worktree)) == 0)
2719 marker = "* ";
2720 else
2721 marker = "~ ";
2722 free(id);
2724 refname += 11;
2725 refstr = got_ref_to_str(re->ref);
2726 if (refstr == NULL)
2727 return got_error_from_errno("got_ref_to_str");
2728 printf("%s%s: %s\n", marker, refname, refstr);
2729 free(refstr);
2732 got_ref_list_free(&refs);
2733 return NULL;
2736 static const struct got_error *
2737 delete_branch(struct got_repository *repo, const char *branch_name)
2739 const struct got_error *err = NULL;
2740 struct got_reference *ref;
2741 char *refname;
2743 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2744 return got_error_from_errno("asprintf");
2746 err = got_ref_open(&ref, repo, refname, 0);
2747 if (err)
2748 goto done;
2750 err = got_ref_delete(ref, repo);
2751 got_ref_close(ref);
2752 done:
2753 free(refname);
2754 return err;
2757 static const struct got_error *
2758 add_branch(struct got_repository *repo, const char *branch_name,
2759 const char *base_branch)
2761 const struct got_error *err = NULL;
2762 struct got_object_id *id = NULL;
2763 struct got_reference *ref = NULL;
2764 char *base_refname = NULL, *refname = NULL;
2765 struct got_reference *base_ref;
2768 * Don't let the user create a branch named '-'.
2769 * While technically a valid reference name, this case is usually
2770 * an unintended typo.
2772 if (branch_name[0] == '-' && branch_name[1] == '\0')
2773 return got_error(GOT_ERR_BAD_REF_NAME);
2775 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2776 base_refname = strdup(GOT_REF_HEAD);
2777 if (base_refname == NULL)
2778 return got_error_from_errno("strdup");
2779 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2780 return got_error_from_errno("asprintf");
2782 err = got_ref_open(&base_ref, repo, base_refname, 0);
2783 if (err)
2784 goto done;
2785 err = got_ref_resolve(&id, repo, base_ref);
2786 got_ref_close(base_ref);
2787 if (err)
2788 goto done;
2790 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2791 err = got_error_from_errno("asprintf");
2792 goto done;
2795 err = got_ref_open(&ref, repo, refname, 0);
2796 if (err == NULL) {
2797 err = got_error(GOT_ERR_BRANCH_EXISTS);
2798 goto done;
2799 } else if (err->code != GOT_ERR_NOT_REF)
2800 goto done;
2802 err = got_ref_alloc(&ref, refname, id);
2803 if (err)
2804 goto done;
2806 err = got_ref_write(ref, repo);
2807 done:
2808 if (ref)
2809 got_ref_close(ref);
2810 free(id);
2811 free(base_refname);
2812 free(refname);
2813 return err;
2816 static const struct got_error *
2817 cmd_branch(int argc, char *argv[])
2819 const struct got_error *error = NULL;
2820 struct got_repository *repo = NULL;
2821 struct got_worktree *worktree = NULL;
2822 char *cwd = NULL, *repo_path = NULL;
2823 int ch, do_list = 0;
2824 const char *delref = NULL;
2826 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2827 switch (ch) {
2828 case 'd':
2829 delref = optarg;
2830 break;
2831 case 'r':
2832 repo_path = realpath(optarg, NULL);
2833 if (repo_path == NULL)
2834 err(1, "-r option");
2835 got_path_strip_trailing_slashes(repo_path);
2836 break;
2837 case 'l':
2838 do_list = 1;
2839 break;
2840 default:
2841 usage_branch();
2842 /* NOTREACHED */
2846 if (do_list && delref)
2847 errx(1, "-l and -d options are mutually exclusive\n");
2849 argc -= optind;
2850 argv += optind;
2852 if (do_list || delref) {
2853 if (argc > 0)
2854 usage_branch();
2855 } else if (argc < 1 || argc > 2)
2856 usage_branch();
2858 #ifndef PROFILE
2859 if (do_list) {
2860 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2861 NULL) == -1)
2862 err(1, "pledge");
2863 } else {
2864 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2865 "sendfd unveil", NULL) == -1)
2866 err(1, "pledge");
2868 #endif
2869 cwd = getcwd(NULL, 0);
2870 if (cwd == NULL) {
2871 error = got_error_from_errno("getcwd");
2872 goto done;
2875 if (repo_path == NULL) {
2876 error = got_worktree_open(&worktree, cwd);
2877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2878 goto done;
2879 else
2880 error = NULL;
2881 if (worktree) {
2882 repo_path =
2883 strdup(got_worktree_get_repo_path(worktree));
2884 if (repo_path == NULL)
2885 error = got_error_from_errno("strdup");
2886 if (error)
2887 goto done;
2888 } else {
2889 repo_path = strdup(cwd);
2890 if (repo_path == NULL) {
2891 error = got_error_from_errno("strdup");
2892 goto done;
2897 error = got_repo_open(&repo, repo_path);
2898 if (error != NULL)
2899 goto done;
2901 error = apply_unveil(got_repo_get_path(repo), do_list,
2902 worktree ? got_worktree_get_root_path(worktree) : NULL);
2903 if (error)
2904 goto done;
2906 if (do_list)
2907 error = list_branches(repo, worktree);
2908 else if (delref)
2909 error = delete_branch(repo, delref);
2910 else {
2911 const char *base_branch;
2912 if (argc == 1) {
2913 base_branch = worktree ?
2914 got_worktree_get_head_ref_name(worktree) :
2915 GOT_REF_HEAD;
2916 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2917 base_branch += 11;
2918 } else
2919 base_branch = argv[1];
2920 error = add_branch(repo, argv[0], base_branch);
2922 done:
2923 if (repo)
2924 got_repo_close(repo);
2925 if (worktree)
2926 got_worktree_close(worktree);
2927 free(cwd);
2928 free(repo_path);
2929 return error;
2932 __dead static void
2933 usage_add(void)
2935 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2936 exit(1);
2939 static const struct got_error *
2940 cmd_add(int argc, char *argv[])
2942 const struct got_error *error = NULL;
2943 struct got_repository *repo = NULL;
2944 struct got_worktree *worktree = NULL;
2945 char *cwd = NULL;
2946 struct got_pathlist_head paths;
2947 struct got_pathlist_entry *pe;
2948 int ch;
2950 TAILQ_INIT(&paths);
2952 while ((ch = getopt(argc, argv, "")) != -1) {
2953 switch (ch) {
2954 default:
2955 usage_add();
2956 /* NOTREACHED */
2960 argc -= optind;
2961 argv += optind;
2963 #ifndef PROFILE
2964 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2965 NULL) == -1)
2966 err(1, "pledge");
2967 #endif
2968 if (argc < 1)
2969 usage_add();
2971 cwd = getcwd(NULL, 0);
2972 if (cwd == NULL) {
2973 error = got_error_from_errno("getcwd");
2974 goto done;
2977 error = got_worktree_open(&worktree, cwd);
2978 if (error)
2979 goto done;
2981 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2982 if (error != NULL)
2983 goto done;
2985 error = apply_unveil(got_repo_get_path(repo), 1,
2986 got_worktree_get_root_path(worktree));
2987 if (error)
2988 goto done;
2990 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2991 if (error)
2992 goto done;
2994 error = got_worktree_schedule_add(worktree, &paths, print_status,
2995 NULL, repo);
2996 done:
2997 if (repo)
2998 got_repo_close(repo);
2999 if (worktree)
3000 got_worktree_close(worktree);
3001 TAILQ_FOREACH(pe, &paths, entry)
3002 free((char *)pe->path);
3003 got_pathlist_free(&paths);
3004 free(cwd);
3005 return error;
3008 __dead static void
3009 usage_remove(void)
3011 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3012 exit(1);
3015 static const struct got_error *
3016 cmd_remove(int argc, char *argv[])
3018 const struct got_error *error = NULL;
3019 struct got_worktree *worktree = NULL;
3020 struct got_repository *repo = NULL;
3021 char *cwd = NULL;
3022 struct got_pathlist_head paths;
3023 struct got_pathlist_entry *pe;
3024 int ch, delete_local_mods = 0;
3026 TAILQ_INIT(&paths);
3028 while ((ch = getopt(argc, argv, "f")) != -1) {
3029 switch (ch) {
3030 case 'f':
3031 delete_local_mods = 1;
3032 break;
3033 default:
3034 usage_add();
3035 /* NOTREACHED */
3039 argc -= optind;
3040 argv += optind;
3042 #ifndef PROFILE
3043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3044 NULL) == -1)
3045 err(1, "pledge");
3046 #endif
3047 if (argc < 1)
3048 usage_remove();
3050 cwd = getcwd(NULL, 0);
3051 if (cwd == NULL) {
3052 error = got_error_from_errno("getcwd");
3053 goto done;
3055 error = got_worktree_open(&worktree, cwd);
3056 if (error)
3057 goto done;
3059 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3060 if (error)
3061 goto done;
3063 error = apply_unveil(got_repo_get_path(repo), 1,
3064 got_worktree_get_root_path(worktree));
3065 if (error)
3066 goto done;
3068 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3069 if (error)
3070 goto done;
3072 error = got_worktree_schedule_delete(worktree, &paths,
3073 delete_local_mods, print_status, NULL, repo);
3074 if (error)
3075 goto done;
3076 done:
3077 if (repo)
3078 got_repo_close(repo);
3079 if (worktree)
3080 got_worktree_close(worktree);
3081 TAILQ_FOREACH(pe, &paths, entry)
3082 free((char *)pe->path);
3083 got_pathlist_free(&paths);
3084 free(cwd);
3085 return error;
3088 __dead static void
3089 usage_revert(void)
3091 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3092 exit(1);
3095 static const struct got_error *
3096 revert_progress(void *arg, unsigned char status, const char *path)
3098 while (path[0] == '/')
3099 path++;
3100 printf("%c %s\n", status, path);
3101 return NULL;
3104 static const struct got_error *
3105 cmd_revert(int argc, char *argv[])
3107 const struct got_error *error = NULL;
3108 struct got_worktree *worktree = NULL;
3109 struct got_repository *repo = NULL;
3110 char *cwd = NULL, *path = NULL;
3111 struct got_pathlist_head paths;
3112 int ch;
3114 TAILQ_INIT(&paths);
3116 while ((ch = getopt(argc, argv, "")) != -1) {
3117 switch (ch) {
3118 default:
3119 usage_revert();
3120 /* NOTREACHED */
3124 argc -= optind;
3125 argv += optind;
3127 #ifndef PROFILE
3128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3129 "unveil", NULL) == -1)
3130 err(1, "pledge");
3131 #endif
3132 if (argc < 1)
3133 usage_revert();
3135 cwd = getcwd(NULL, 0);
3136 if (cwd == NULL) {
3137 error = got_error_from_errno("getcwd");
3138 goto done;
3140 error = got_worktree_open(&worktree, cwd);
3141 if (error)
3142 goto done;
3144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3145 if (error != NULL)
3146 goto done;
3148 error = apply_unveil(got_repo_get_path(repo), 1,
3149 got_worktree_get_root_path(worktree));
3150 if (error)
3151 goto done;
3153 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3154 if (error)
3155 goto done;
3157 error = got_worktree_revert(worktree, &paths,
3158 revert_progress, NULL, repo);
3159 if (error)
3160 goto done;
3161 done:
3162 if (repo)
3163 got_repo_close(repo);
3164 if (worktree)
3165 got_worktree_close(worktree);
3166 free(path);
3167 free(cwd);
3168 return error;
3171 __dead static void
3172 usage_commit(void)
3174 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3175 getprogname());
3176 exit(1);
3179 struct collect_commit_logmsg_arg {
3180 const char *cmdline_log;
3181 const char *editor;
3182 const char *worktree_path;
3183 const char *branch_name;
3184 const char *repo_path;
3185 char *logmsg_path;
3189 static const struct got_error *
3190 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3191 void *arg)
3193 char *initial_content = NULL;
3194 struct got_pathlist_entry *pe;
3195 const struct got_error *err = NULL;
3196 char *template = NULL;
3197 struct collect_commit_logmsg_arg *a = arg;
3198 int fd;
3199 size_t len;
3201 /* if a message was specified on the command line, just use it */
3202 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3203 len = strlen(a->cmdline_log) + 1;
3204 *logmsg = malloc(len + 1);
3205 if (*logmsg == NULL)
3206 return got_error_from_errno("malloc");
3207 strlcpy(*logmsg, a->cmdline_log, len);
3208 return NULL;
3211 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3212 return got_error_from_errno("asprintf");
3214 if (asprintf(&initial_content,
3215 "\n# changes to be committed on branch %s:\n",
3216 a->branch_name) == -1)
3217 return got_error_from_errno("asprintf");
3219 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3220 if (err)
3221 goto done;
3223 dprintf(fd, initial_content);
3225 TAILQ_FOREACH(pe, commitable_paths, entry) {
3226 struct got_commitable *ct = pe->data;
3227 dprintf(fd, "# %c %s\n",
3228 got_commitable_get_status(ct),
3229 got_commitable_get_path(ct));
3231 close(fd);
3233 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3234 done:
3235 unlink(a->logmsg_path);
3236 free(a->logmsg_path);
3237 free(initial_content);
3238 free(template);
3240 /* Editor is done; we can now apply unveil(2) */
3241 if (err == NULL) {
3242 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3243 if (err) {
3244 free(*logmsg);
3245 *logmsg = NULL;
3248 return err;
3251 static const struct got_error *
3252 cmd_commit(int argc, char *argv[])
3254 const struct got_error *error = NULL;
3255 struct got_worktree *worktree = NULL;
3256 struct got_repository *repo = NULL;
3257 char *cwd = NULL, *id_str = NULL;
3258 struct got_object_id *id = NULL;
3259 const char *logmsg = NULL;
3260 const char *got_author = getenv("GOT_AUTHOR");
3261 struct collect_commit_logmsg_arg cl_arg;
3262 char *editor = NULL;
3263 int ch, rebase_in_progress, histedit_in_progress;
3264 struct got_pathlist_head paths;
3266 TAILQ_INIT(&paths);
3268 while ((ch = getopt(argc, argv, "m:")) != -1) {
3269 switch (ch) {
3270 case 'm':
3271 logmsg = optarg;
3272 break;
3273 default:
3274 usage_commit();
3275 /* NOTREACHED */
3279 argc -= optind;
3280 argv += optind;
3282 #ifndef PROFILE
3283 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3284 "unveil", NULL) == -1)
3285 err(1, "pledge");
3286 #endif
3287 if (got_author == NULL) {
3288 /* TODO: Look current user up in password database */
3289 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3290 goto done;
3293 cwd = getcwd(NULL, 0);
3294 if (cwd == NULL) {
3295 error = got_error_from_errno("getcwd");
3296 goto done;
3298 error = got_worktree_open(&worktree, cwd);
3299 if (error)
3300 goto done;
3302 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3303 if (error)
3304 goto done;
3305 if (rebase_in_progress) {
3306 error = got_error(GOT_ERR_REBASING);
3307 goto done;
3310 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3311 worktree);
3312 if (error)
3313 goto done;
3315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3316 if (error != NULL)
3317 goto done;
3320 * unveil(2) traverses exec(2); if an editor is used we have
3321 * to apply unveil after the log message has been written.
3323 if (logmsg == NULL || strlen(logmsg) == 0)
3324 error = get_editor(&editor);
3325 else
3326 error = apply_unveil(got_repo_get_path(repo), 0,
3327 got_worktree_get_root_path(worktree));
3328 if (error)
3329 goto done;
3331 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3332 if (error)
3333 goto done;
3335 cl_arg.editor = editor;
3336 cl_arg.cmdline_log = logmsg;
3337 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3338 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3339 if (!histedit_in_progress) {
3340 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3341 error = got_error(GOT_ERR_COMMIT_BRANCH);
3342 goto done;
3344 cl_arg.branch_name += 11;
3346 cl_arg.repo_path = got_repo_get_path(repo);
3347 cl_arg.logmsg_path = NULL;
3348 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3349 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3350 if (error) {
3351 if (cl_arg.logmsg_path)
3352 fprintf(stderr, "%s: log message preserved in %s\n",
3353 getprogname(), cl_arg.logmsg_path);
3354 goto done;
3357 if (cl_arg.logmsg_path)
3358 unlink(cl_arg.logmsg_path);
3360 error = got_object_id_str(&id_str, id);
3361 if (error)
3362 goto done;
3363 printf("Created commit %s\n", id_str);
3364 done:
3365 if (repo)
3366 got_repo_close(repo);
3367 if (worktree)
3368 got_worktree_close(worktree);
3369 free(cwd);
3370 free(id_str);
3371 free(editor);
3372 return error;
3375 __dead static void
3376 usage_cherrypick(void)
3378 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3379 exit(1);
3382 static const struct got_error *
3383 cmd_cherrypick(int argc, char *argv[])
3385 const struct got_error *error = NULL;
3386 struct got_worktree *worktree = NULL;
3387 struct got_repository *repo = NULL;
3388 char *cwd = NULL, *commit_id_str = NULL;
3389 struct got_object_id *commit_id = NULL;
3390 struct got_commit_object *commit = NULL;
3391 struct got_object_qid *pid;
3392 struct got_reference *head_ref = NULL;
3393 int ch, did_something = 0;
3395 while ((ch = getopt(argc, argv, "")) != -1) {
3396 switch (ch) {
3397 default:
3398 usage_cherrypick();
3399 /* NOTREACHED */
3403 argc -= optind;
3404 argv += optind;
3406 #ifndef PROFILE
3407 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3408 "unveil", NULL) == -1)
3409 err(1, "pledge");
3410 #endif
3411 if (argc != 1)
3412 usage_cherrypick();
3414 cwd = getcwd(NULL, 0);
3415 if (cwd == NULL) {
3416 error = got_error_from_errno("getcwd");
3417 goto done;
3419 error = got_worktree_open(&worktree, cwd);
3420 if (error)
3421 goto done;
3423 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3424 if (error != NULL)
3425 goto done;
3427 error = apply_unveil(got_repo_get_path(repo), 0,
3428 got_worktree_get_root_path(worktree));
3429 if (error)
3430 goto done;
3432 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3433 GOT_OBJ_TYPE_COMMIT, repo);
3434 if (error != NULL) {
3435 struct got_reference *ref;
3436 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3437 goto done;
3438 error = got_ref_open(&ref, repo, argv[0], 0);
3439 if (error != NULL)
3440 goto done;
3441 error = got_ref_resolve(&commit_id, repo, ref);
3442 got_ref_close(ref);
3443 if (error != NULL)
3444 goto done;
3446 error = got_object_id_str(&commit_id_str, commit_id);
3447 if (error)
3448 goto done;
3450 error = got_ref_open(&head_ref, repo,
3451 got_worktree_get_head_ref_name(worktree), 0);
3452 if (error != NULL)
3453 goto done;
3455 error = check_same_branch(commit_id, head_ref, NULL, repo);
3456 if (error) {
3457 if (error->code != GOT_ERR_ANCESTRY)
3458 goto done;
3459 error = NULL;
3460 } else {
3461 error = got_error(GOT_ERR_SAME_BRANCH);
3462 goto done;
3465 error = got_object_open_as_commit(&commit, repo, commit_id);
3466 if (error)
3467 goto done;
3468 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3469 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3470 commit_id, repo, update_progress, &did_something, check_cancelled,
3471 NULL);
3472 if (error != NULL)
3473 goto done;
3475 if (did_something)
3476 printf("Merged commit %s\n", commit_id_str);
3477 done:
3478 if (commit)
3479 got_object_commit_close(commit);
3480 free(commit_id_str);
3481 if (head_ref)
3482 got_ref_close(head_ref);
3483 if (worktree)
3484 got_worktree_close(worktree);
3485 if (repo)
3486 got_repo_close(repo);
3487 return error;
3490 __dead static void
3491 usage_backout(void)
3493 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3494 exit(1);
3497 static const struct got_error *
3498 cmd_backout(int argc, char *argv[])
3500 const struct got_error *error = NULL;
3501 struct got_worktree *worktree = NULL;
3502 struct got_repository *repo = NULL;
3503 char *cwd = NULL, *commit_id_str = NULL;
3504 struct got_object_id *commit_id = NULL;
3505 struct got_commit_object *commit = NULL;
3506 struct got_object_qid *pid;
3507 struct got_reference *head_ref = NULL;
3508 int ch, did_something = 0;
3510 while ((ch = getopt(argc, argv, "")) != -1) {
3511 switch (ch) {
3512 default:
3513 usage_backout();
3514 /* NOTREACHED */
3518 argc -= optind;
3519 argv += optind;
3521 #ifndef PROFILE
3522 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3523 "unveil", NULL) == -1)
3524 err(1, "pledge");
3525 #endif
3526 if (argc != 1)
3527 usage_backout();
3529 cwd = getcwd(NULL, 0);
3530 if (cwd == NULL) {
3531 error = got_error_from_errno("getcwd");
3532 goto done;
3534 error = got_worktree_open(&worktree, cwd);
3535 if (error)
3536 goto done;
3538 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3539 if (error != NULL)
3540 goto done;
3542 error = apply_unveil(got_repo_get_path(repo), 0,
3543 got_worktree_get_root_path(worktree));
3544 if (error)
3545 goto done;
3547 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3548 GOT_OBJ_TYPE_COMMIT, repo);
3549 if (error != NULL) {
3550 struct got_reference *ref;
3551 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3552 goto done;
3553 error = got_ref_open(&ref, repo, argv[0], 0);
3554 if (error != NULL)
3555 goto done;
3556 error = got_ref_resolve(&commit_id, repo, ref);
3557 got_ref_close(ref);
3558 if (error != NULL)
3559 goto done;
3561 error = got_object_id_str(&commit_id_str, commit_id);
3562 if (error)
3563 goto done;
3565 error = got_ref_open(&head_ref, repo,
3566 got_worktree_get_head_ref_name(worktree), 0);
3567 if (error != NULL)
3568 goto done;
3570 error = check_same_branch(commit_id, head_ref, NULL, repo);
3571 if (error)
3572 goto done;
3574 error = got_object_open_as_commit(&commit, repo, commit_id);
3575 if (error)
3576 goto done;
3577 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3578 if (pid == NULL) {
3579 error = got_error(GOT_ERR_ROOT_COMMIT);
3580 goto done;
3583 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3584 update_progress, &did_something, check_cancelled, NULL);
3585 if (error != NULL)
3586 goto done;
3588 if (did_something)
3589 printf("Backed out commit %s\n", commit_id_str);
3590 done:
3591 if (commit)
3592 got_object_commit_close(commit);
3593 free(commit_id_str);
3594 if (head_ref)
3595 got_ref_close(head_ref);
3596 if (worktree)
3597 got_worktree_close(worktree);
3598 if (repo)
3599 got_repo_close(repo);
3600 return error;
3603 __dead static void
3604 usage_rebase(void)
3606 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3607 getprogname());
3608 exit(1);
3611 void
3612 trim_logmsg(char *logmsg, int limit)
3614 char *nl;
3615 size_t len;
3617 len = strlen(logmsg);
3618 if (len > limit)
3619 len = limit;
3620 logmsg[len] = '\0';
3621 nl = strchr(logmsg, '\n');
3622 if (nl)
3623 *nl = '\0';
3626 static const struct got_error *
3627 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3629 const char *logmsg0 = NULL;
3631 logmsg0 = got_object_commit_get_logmsg(commit);
3633 while (isspace((unsigned char)logmsg0[0]))
3634 logmsg0++;
3636 *logmsg = strdup(logmsg0);
3637 if (*logmsg == NULL)
3638 return got_error_from_errno("strdup");
3640 trim_logmsg(*logmsg, limit);
3641 return NULL;
3644 static const struct got_error *
3645 show_rebase_progress(struct got_commit_object *commit,
3646 struct got_object_id *old_id, struct got_object_id *new_id)
3648 const struct got_error *err;
3649 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3651 err = got_object_id_str(&old_id_str, old_id);
3652 if (err)
3653 goto done;
3655 if (new_id) {
3656 err = got_object_id_str(&new_id_str, new_id);
3657 if (err)
3658 goto done;
3661 old_id_str[12] = '\0';
3662 if (new_id_str)
3663 new_id_str[12] = '\0';
3665 err = get_short_logmsg(&logmsg, 42, commit);
3666 if (err)
3667 goto done;
3669 printf("%s -> %s: %s\n", old_id_str,
3670 new_id_str ? new_id_str : "no-op change", logmsg);
3671 done:
3672 free(old_id_str);
3673 free(new_id_str);
3674 return err;
3677 static const struct got_error *
3678 rebase_progress(void *arg, unsigned char status, const char *path)
3680 unsigned char *rebase_status = arg;
3682 while (path[0] == '/')
3683 path++;
3684 printf("%c %s\n", status, path);
3686 if (*rebase_status == GOT_STATUS_CONFLICT)
3687 return NULL;
3688 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3689 *rebase_status = status;
3690 return NULL;
3693 static const struct got_error *
3694 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3695 struct got_reference *branch, struct got_reference *new_base_branch,
3696 struct got_reference *tmp_branch, struct got_repository *repo)
3698 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3699 return got_worktree_rebase_complete(worktree, fileindex,
3700 new_base_branch, tmp_branch, branch, repo);
3703 static const struct got_error *
3704 rebase_commit(struct got_pathlist_head *merged_paths,
3705 struct got_worktree *worktree, struct got_fileindex *fileindex,
3706 struct got_reference *tmp_branch,
3707 struct got_object_id *commit_id, struct got_repository *repo)
3709 const struct got_error *error;
3710 struct got_commit_object *commit;
3711 struct got_object_id *new_commit_id;
3713 error = got_object_open_as_commit(&commit, repo, commit_id);
3714 if (error)
3715 return error;
3717 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3718 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3719 if (error) {
3720 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3721 goto done;
3722 error = show_rebase_progress(commit, commit_id, NULL);
3723 } else {
3724 error = show_rebase_progress(commit, commit_id, new_commit_id);
3725 free(new_commit_id);
3727 done:
3728 got_object_commit_close(commit);
3729 return error;
3732 struct check_path_prefix_arg {
3733 const char *path_prefix;
3734 size_t len;
3735 int errcode;
3738 static const struct got_error *
3739 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3740 struct got_blob_object *blob2, struct got_object_id *id1,
3741 struct got_object_id *id2, const char *path1, const char *path2,
3742 struct got_repository *repo)
3744 struct check_path_prefix_arg *a = arg;
3746 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3747 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3748 return got_error(a->errcode);
3750 return NULL;
3753 static const struct got_error *
3754 check_path_prefix(struct got_object_id *parent_id,
3755 struct got_object_id *commit_id, const char *path_prefix,
3756 int errcode, struct got_repository *repo)
3758 const struct got_error *err;
3759 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3760 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3761 struct check_path_prefix_arg cpp_arg;
3763 if (got_path_is_root_dir(path_prefix))
3764 return NULL;
3766 err = got_object_open_as_commit(&commit, repo, commit_id);
3767 if (err)
3768 goto done;
3770 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3771 if (err)
3772 goto done;
3774 err = got_object_open_as_tree(&tree1, repo,
3775 got_object_commit_get_tree_id(parent_commit));
3776 if (err)
3777 goto done;
3779 err = got_object_open_as_tree(&tree2, repo,
3780 got_object_commit_get_tree_id(commit));
3781 if (err)
3782 goto done;
3784 cpp_arg.path_prefix = path_prefix;
3785 while (cpp_arg.path_prefix[0] == '/')
3786 cpp_arg.path_prefix++;
3787 cpp_arg.len = strlen(cpp_arg.path_prefix);
3788 cpp_arg.errcode = errcode;
3789 err = got_diff_tree(tree1, tree2, "", "", repo,
3790 check_path_prefix_in_diff, &cpp_arg, 0);
3791 done:
3792 if (tree1)
3793 got_object_tree_close(tree1);
3794 if (tree2)
3795 got_object_tree_close(tree2);
3796 if (commit)
3797 got_object_commit_close(commit);
3798 if (parent_commit)
3799 got_object_commit_close(parent_commit);
3800 return err;
3803 static const struct got_error *
3804 collect_commits(struct got_object_id_queue *commits,
3805 struct got_object_id *initial_commit_id,
3806 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3807 const char *path_prefix, int path_prefix_errcode,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3811 struct got_commit_graph *graph = NULL;
3812 struct got_object_id *parent_id = NULL;
3813 struct got_object_qid *qid;
3814 struct got_object_id *commit_id = initial_commit_id;
3816 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3817 if (err)
3818 return err;
3820 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3821 if (err)
3822 goto done;
3823 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3824 err = got_commit_graph_iter_next(&parent_id, graph);
3825 if (err) {
3826 if (err->code == GOT_ERR_ITER_COMPLETED) {
3827 err = got_error_msg(GOT_ERR_ANCESTRY,
3828 "ran out of commits to rebase before "
3829 "youngest common ancestor commit has "
3830 "been reached?!?");
3831 goto done;
3832 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3833 goto done;
3834 err = got_commit_graph_fetch_commits(graph, 1, repo);
3835 if (err)
3836 goto done;
3837 } else {
3838 err = check_path_prefix(parent_id, commit_id,
3839 path_prefix, path_prefix_errcode, repo);
3840 if (err)
3841 goto done;
3843 err = got_object_qid_alloc(&qid, commit_id);
3844 if (err)
3845 goto done;
3846 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3847 commit_id = parent_id;
3850 done:
3851 got_commit_graph_close(graph);
3852 return err;
3855 static const struct got_error *
3856 cmd_rebase(int argc, char *argv[])
3858 const struct got_error *error = NULL;
3859 struct got_worktree *worktree = NULL;
3860 struct got_repository *repo = NULL;
3861 struct got_fileindex *fileindex = NULL;
3862 char *cwd = NULL;
3863 struct got_reference *branch = NULL;
3864 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3865 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3866 struct got_object_id *resume_commit_id = NULL;
3867 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3868 struct got_commit_object *commit = NULL;
3869 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3870 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3871 struct got_object_id_queue commits;
3872 struct got_pathlist_head merged_paths;
3873 const struct got_object_id_queue *parent_ids;
3874 struct got_object_qid *qid, *pid;
3876 SIMPLEQ_INIT(&commits);
3877 TAILQ_INIT(&merged_paths);
3879 while ((ch = getopt(argc, argv, "ac")) != -1) {
3880 switch (ch) {
3881 case 'a':
3882 abort_rebase = 1;
3883 break;
3884 case 'c':
3885 continue_rebase = 1;
3886 break;
3887 default:
3888 usage_rebase();
3889 /* NOTREACHED */
3893 argc -= optind;
3894 argv += optind;
3896 #ifndef PROFILE
3897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3898 "unveil", NULL) == -1)
3899 err(1, "pledge");
3900 #endif
3901 if (abort_rebase && continue_rebase)
3902 usage_rebase();
3903 else if (abort_rebase || continue_rebase) {
3904 if (argc != 0)
3905 usage_rebase();
3906 } else if (argc != 1)
3907 usage_rebase();
3909 cwd = getcwd(NULL, 0);
3910 if (cwd == NULL) {
3911 error = got_error_from_errno("getcwd");
3912 goto done;
3914 error = got_worktree_open(&worktree, cwd);
3915 if (error)
3916 goto done;
3918 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3919 if (error != NULL)
3920 goto done;
3922 error = apply_unveil(got_repo_get_path(repo), 0,
3923 got_worktree_get_root_path(worktree));
3924 if (error)
3925 goto done;
3927 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3928 if (error)
3929 goto done;
3931 if (abort_rebase) {
3932 int did_something;
3933 if (!rebase_in_progress) {
3934 error = got_error(GOT_ERR_NOT_REBASING);
3935 goto done;
3937 error = got_worktree_rebase_continue(&resume_commit_id,
3938 &new_base_branch, &tmp_branch, &branch, &fileindex,
3939 worktree, repo);
3940 if (error)
3941 goto done;
3942 printf("Switching work tree to %s\n",
3943 got_ref_get_symref_target(new_base_branch));
3944 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3945 new_base_branch, update_progress, &did_something);
3946 if (error)
3947 goto done;
3948 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3949 goto done; /* nothing else to do */
3952 if (continue_rebase) {
3953 if (!rebase_in_progress) {
3954 error = got_error(GOT_ERR_NOT_REBASING);
3955 goto done;
3957 error = got_worktree_rebase_continue(&resume_commit_id,
3958 &new_base_branch, &tmp_branch, &branch, &fileindex,
3959 worktree, repo);
3960 if (error)
3961 goto done;
3963 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3964 resume_commit_id, repo);
3965 if (error)
3966 goto done;
3968 yca_id = got_object_id_dup(resume_commit_id);
3969 if (yca_id == NULL) {
3970 error = got_error_from_errno("got_object_id_dup");
3971 goto done;
3973 } else {
3974 error = got_ref_open(&branch, repo, argv[0], 0);
3975 if (error != NULL)
3976 goto done;
3979 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3980 if (error)
3981 goto done;
3983 if (!continue_rebase) {
3984 struct got_object_id *base_commit_id;
3986 base_commit_id = got_worktree_get_base_commit_id(worktree);
3987 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3988 base_commit_id, branch_head_commit_id, repo);
3989 if (error)
3990 goto done;
3991 if (yca_id == NULL) {
3992 error = got_error_msg(GOT_ERR_ANCESTRY,
3993 "specified branch shares no common ancestry "
3994 "with work tree's branch");
3995 goto done;
3998 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3999 if (error) {
4000 if (error->code != GOT_ERR_ANCESTRY)
4001 goto done;
4002 error = NULL;
4003 } else {
4004 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4005 "specified branch resolves to a commit which "
4006 "is already contained in work tree's branch");
4007 goto done;
4009 error = got_worktree_rebase_prepare(&new_base_branch,
4010 &tmp_branch, &fileindex, worktree, branch, repo);
4011 if (error)
4012 goto done;
4015 commit_id = branch_head_commit_id;
4016 error = got_object_open_as_commit(&commit, repo, commit_id);
4017 if (error)
4018 goto done;
4020 parent_ids = got_object_commit_get_parent_ids(commit);
4021 pid = SIMPLEQ_FIRST(parent_ids);
4022 error = collect_commits(&commits, commit_id, pid->id,
4023 yca_id, got_worktree_get_path_prefix(worktree),
4024 GOT_ERR_REBASE_PATH, repo);
4025 got_object_commit_close(commit);
4026 commit = NULL;
4027 if (error)
4028 goto done;
4030 if (SIMPLEQ_EMPTY(&commits)) {
4031 if (continue_rebase)
4032 error = rebase_complete(worktree, fileindex,
4033 branch, new_base_branch, tmp_branch, repo);
4034 else
4035 error = got_error(GOT_ERR_EMPTY_REBASE);
4036 goto done;
4039 pid = NULL;
4040 SIMPLEQ_FOREACH(qid, &commits, entry) {
4041 commit_id = qid->id;
4042 parent_id = pid ? pid->id : yca_id;
4043 pid = qid;
4045 error = got_worktree_rebase_merge_files(&merged_paths,
4046 worktree, fileindex, parent_id, commit_id, repo,
4047 rebase_progress, &rebase_status, check_cancelled, NULL);
4048 if (error)
4049 goto done;
4051 if (rebase_status == GOT_STATUS_CONFLICT) {
4052 got_worktree_rebase_pathlist_free(&merged_paths);
4053 break;
4056 error = rebase_commit(&merged_paths, worktree, fileindex,
4057 tmp_branch, commit_id, repo);
4058 got_worktree_rebase_pathlist_free(&merged_paths);
4059 if (error)
4060 goto done;
4063 if (rebase_status == GOT_STATUS_CONFLICT) {
4064 error = got_worktree_rebase_postpone(worktree, fileindex);
4065 if (error)
4066 goto done;
4067 error = got_error_msg(GOT_ERR_CONFLICTS,
4068 "conflicts must be resolved before rebasing can continue");
4069 } else
4070 error = rebase_complete(worktree, fileindex, branch,
4071 new_base_branch, tmp_branch, repo);
4072 done:
4073 got_object_id_queue_free(&commits);
4074 free(branch_head_commit_id);
4075 free(resume_commit_id);
4076 free(yca_id);
4077 if (commit)
4078 got_object_commit_close(commit);
4079 if (branch)
4080 got_ref_close(branch);
4081 if (new_base_branch)
4082 got_ref_close(new_base_branch);
4083 if (tmp_branch)
4084 got_ref_close(tmp_branch);
4085 if (worktree)
4086 got_worktree_close(worktree);
4087 if (repo)
4088 got_repo_close(repo);
4089 return error;
4092 __dead static void
4093 usage_histedit(void)
4095 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4096 getprogname());
4097 exit(1);
4100 #define GOT_HISTEDIT_PICK 'p'
4101 #define GOT_HISTEDIT_EDIT 'e'
4102 #define GOT_HISTEDIT_FOLD 'f'
4103 #define GOT_HISTEDIT_DROP 'd'
4104 #define GOT_HISTEDIT_MESG 'm'
4106 static struct got_histedit_cmd {
4107 unsigned char code;
4108 const char *name;
4109 const char *desc;
4110 } got_histedit_cmds[] = {
4111 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4112 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4113 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4114 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4115 { GOT_HISTEDIT_MESG, "mesg",
4116 "single-line log message for commit above (open editor if empty)" },
4119 struct got_histedit_list_entry {
4120 TAILQ_ENTRY(got_histedit_list_entry) entry;
4121 struct got_object_id *commit_id;
4122 const struct got_histedit_cmd *cmd;
4123 char *logmsg;
4125 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4127 static const struct got_error *
4128 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4129 FILE *f, struct got_repository *repo)
4131 const struct got_error *err = NULL;
4132 char *logmsg = NULL, *id_str = NULL;
4133 struct got_commit_object *commit = NULL;
4134 size_t n;
4136 err = got_object_open_as_commit(&commit, repo, commit_id);
4137 if (err)
4138 goto done;
4140 err = get_short_logmsg(&logmsg, 34, commit);
4141 if (err)
4142 goto done;
4144 err = got_object_id_str(&id_str, commit_id);
4145 if (err)
4146 goto done;
4148 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4149 if (n < 0)
4150 err = got_ferror(f, GOT_ERR_IO);
4151 done:
4152 if (commit)
4153 got_object_commit_close(commit);
4154 free(id_str);
4155 free(logmsg);
4156 return err;
4159 static const struct got_error *
4160 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4161 struct got_repository *repo)
4163 const struct got_error *err = NULL;
4164 struct got_object_qid *qid;
4166 if (SIMPLEQ_EMPTY(commits))
4167 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4169 SIMPLEQ_FOREACH(qid, commits, entry) {
4170 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4171 f, repo);
4172 if (err)
4173 break;
4176 return err;
4179 static const struct got_error *
4180 write_cmd_list(FILE *f)
4182 const struct got_error *err = NULL;
4183 int n, i;
4185 n = fprintf(f, "# Available histedit commands:\n");
4186 if (n < 0)
4187 return got_ferror(f, GOT_ERR_IO);
4189 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4190 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4191 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4192 cmd->desc);
4193 if (n < 0) {
4194 err = got_ferror(f, GOT_ERR_IO);
4195 break;
4198 n = fprintf(f, "# Commits will be processed in order from top to "
4199 "bottom of this file.\n");
4200 if (n < 0)
4201 return got_ferror(f, GOT_ERR_IO);
4202 return err;
4205 static const struct got_error *
4206 histedit_syntax_error(int lineno)
4208 static char msg[42];
4209 int ret;
4211 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4212 lineno);
4213 if (ret == -1 || ret >= sizeof(msg))
4214 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4216 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4219 static const struct got_error *
4220 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4221 char *logmsg, struct got_repository *repo)
4223 const struct got_error *err;
4224 struct got_commit_object *folded_commit = NULL;
4225 char *id_str;
4227 err = got_object_id_str(&id_str, hle->commit_id);
4228 if (err)
4229 return err;
4231 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4232 if (err)
4233 goto done;
4235 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4236 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4237 got_object_commit_get_logmsg(folded_commit)) == -1) {
4238 err = got_error_from_errno("asprintf");
4239 goto done;
4241 done:
4242 if (folded_commit)
4243 got_object_commit_close(folded_commit);
4244 free(id_str);
4245 return err;
4248 static struct got_histedit_list_entry *
4249 get_folded_commits(struct got_histedit_list_entry *hle)
4251 struct got_histedit_list_entry *prev, *folded = NULL;
4253 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4254 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4255 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4256 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4257 folded = prev;
4258 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4261 return folded;
4264 static const struct got_error *
4265 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4266 struct got_repository *repo)
4268 char *logmsg_path = NULL, *id_str = NULL;
4269 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4270 const struct got_error *err = NULL;
4271 struct got_commit_object *commit = NULL;
4272 int fd;
4273 struct got_histedit_list_entry *folded = NULL;
4275 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4276 if (err)
4277 return err;
4279 folded = get_folded_commits(hle);
4280 if (folded) {
4281 while (folded != hle) {
4282 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4283 folded = TAILQ_NEXT(folded, entry);
4284 continue;
4286 err = append_folded_commit_msg(&new_msg, folded,
4287 logmsg, repo);
4288 if (err)
4289 goto done;
4290 free(logmsg);
4291 logmsg = new_msg;
4292 folded = TAILQ_NEXT(folded, entry);
4296 err = got_object_id_str(&id_str, hle->commit_id);
4297 if (err)
4298 goto done;
4299 if (asprintf(&new_msg,
4300 "%s\n# original log message of commit %s: %s",
4301 logmsg ? logmsg : "", id_str,
4302 got_object_commit_get_logmsg(commit)) == -1) {
4303 err = got_error_from_errno("asprintf");
4304 goto done;
4306 free(logmsg);
4307 logmsg = new_msg;
4309 err = got_object_id_str(&id_str, hle->commit_id);
4310 if (err)
4311 goto done;
4313 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4314 if (err)
4315 goto done;
4317 dprintf(fd, logmsg);
4318 close(fd);
4320 err = get_editor(&editor);
4321 if (err)
4322 goto done;
4324 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4325 if (err) {
4326 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4327 goto done;
4328 err = NULL;
4329 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4330 if (hle->logmsg == NULL)
4331 err = got_error_from_errno("strdup");
4333 done:
4334 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4335 err = got_error_from_errno2("unlink", logmsg_path);
4336 free(logmsg_path);
4337 free(logmsg);
4338 free(editor);
4339 if (commit)
4340 got_object_commit_close(commit);
4341 return err;
4344 static const struct got_error *
4345 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4346 FILE *f, struct got_repository *repo)
4348 const struct got_error *err = NULL;
4349 char *line = NULL, *p, *end;
4350 size_t size;
4351 ssize_t len;
4352 int lineno = 0, i;
4353 const struct got_histedit_cmd *cmd;
4354 struct got_object_id *commit_id = NULL;
4355 struct got_histedit_list_entry *hle = NULL;
4357 for (;;) {
4358 len = getline(&line, &size, f);
4359 if (len == -1) {
4360 const struct got_error *getline_err;
4361 if (feof(f))
4362 break;
4363 getline_err = got_error_from_errno("getline");
4364 err = got_ferror(f, getline_err->code);
4365 break;
4367 lineno++;
4368 p = line;
4369 while (isspace((unsigned char)p[0]))
4370 p++;
4371 if (p[0] == '#' || p[0] == '\0') {
4372 free(line);
4373 line = NULL;
4374 continue;
4376 cmd = NULL;
4377 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4378 cmd = &got_histedit_cmds[i];
4379 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4380 isspace((unsigned char)p[strlen(cmd->name)])) {
4381 p += strlen(cmd->name);
4382 break;
4384 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4385 p++;
4386 break;
4389 if (i == nitems(got_histedit_cmds)) {
4390 err = histedit_syntax_error(lineno);
4391 break;
4393 while (isspace((unsigned char)p[0]))
4394 p++;
4395 if (cmd->code == GOT_HISTEDIT_MESG) {
4396 if (hle == NULL || hle->logmsg != NULL) {
4397 err = got_error(GOT_ERR_HISTEDIT_CMD);
4398 break;
4400 if (p[0] == '\0') {
4401 err = histedit_edit_logmsg(hle, repo);
4402 if (err)
4403 break;
4404 } else {
4405 hle->logmsg = strdup(p);
4406 if (hle->logmsg == NULL) {
4407 err = got_error_from_errno("strdup");
4408 break;
4411 free(line);
4412 line = NULL;
4413 continue;
4414 } else {
4415 end = p;
4416 while (end[0] && !isspace((unsigned char)end[0]))
4417 end++;
4418 *end = '\0';
4420 err = got_object_resolve_id_str(&commit_id, repo, p);
4421 if (err) {
4422 /* override error code */
4423 err = histedit_syntax_error(lineno);
4424 break;
4427 hle = malloc(sizeof(*hle));
4428 if (hle == NULL) {
4429 err = got_error_from_errno("malloc");
4430 break;
4432 hle->cmd = cmd;
4433 hle->commit_id = commit_id;
4434 hle->logmsg = NULL;
4435 commit_id = NULL;
4436 free(line);
4437 line = NULL;
4438 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4441 free(line);
4442 free(commit_id);
4443 return err;
4446 static const struct got_error *
4447 histedit_check_script(struct got_histedit_list *histedit_cmds,
4448 struct got_object_id_queue *commits, struct got_repository *repo)
4450 const struct got_error *err = NULL;
4451 struct got_object_qid *qid;
4452 struct got_histedit_list_entry *hle;
4453 static char msg[80];
4454 char *id_str;
4456 if (TAILQ_EMPTY(histedit_cmds))
4457 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4458 "histedit script contains no commands");
4460 SIMPLEQ_FOREACH(qid, commits, entry) {
4461 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4462 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4463 break;
4465 if (hle == NULL) {
4466 err = got_object_id_str(&id_str, qid->id);
4467 if (err)
4468 return err;
4469 snprintf(msg, sizeof(msg),
4470 "commit %s missing from histedit script", id_str);
4471 free(id_str);
4472 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4476 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4477 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4478 "last commit in histedit script cannot be folded");
4480 return NULL;
4483 static const struct got_error *
4484 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4485 const char *path, struct got_object_id_queue *commits,
4486 struct got_repository *repo)
4488 const struct got_error *err = NULL;
4489 char *editor;
4490 FILE *f = NULL;
4492 err = get_editor(&editor);
4493 if (err)
4494 return err;
4496 if (spawn_editor(editor, path) == -1) {
4497 err = got_error_from_errno("failed spawning editor");
4498 goto done;
4501 f = fopen(path, "r");
4502 if (f == NULL) {
4503 err = got_error_from_errno("fopen");
4504 goto done;
4506 err = histedit_parse_list(histedit_cmds, f, repo);
4507 if (err)
4508 goto done;
4510 err = histedit_check_script(histedit_cmds, commits, repo);
4511 done:
4512 if (f && fclose(f) != 0 && err == NULL)
4513 err = got_error_from_errno("fclose");
4514 free(editor);
4515 return err;
4518 static const struct got_error *
4519 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4520 struct got_object_id_queue *, const char *, struct got_repository *);
4522 static const struct got_error *
4523 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4524 struct got_object_id_queue *commits, struct got_repository *repo)
4526 const struct got_error *err;
4527 FILE *f = NULL;
4528 char *path = NULL;
4530 err = got_opentemp_named(&path, &f, "got-histedit");
4531 if (err)
4532 return err;
4534 err = write_cmd_list(f);
4535 if (err)
4536 goto done;
4538 err = histedit_write_commit_list(commits, f, repo);
4539 if (err)
4540 goto done;
4542 if (fclose(f) != 0) {
4543 err = got_error_from_errno("fclose");
4544 goto done;
4546 f = NULL;
4548 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4549 if (err) {
4550 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4551 err->code != GOT_ERR_HISTEDIT_CMD)
4552 goto done;
4553 err = histedit_edit_list_retry(histedit_cmds, err,
4554 commits, path, repo);
4556 done:
4557 if (f && fclose(f) != 0 && err == NULL)
4558 err = got_error_from_errno("fclose");
4559 if (path && unlink(path) != 0 && err == NULL)
4560 err = got_error_from_errno2("unlink", path);
4561 free(path);
4562 return err;
4565 static const struct got_error *
4566 histedit_save_list(struct got_histedit_list *histedit_cmds,
4567 struct got_worktree *worktree, struct got_repository *repo)
4569 const struct got_error *err = NULL;
4570 char *path = NULL;
4571 FILE *f = NULL;
4572 struct got_histedit_list_entry *hle;
4573 struct got_commit_object *commit = NULL;
4575 err = got_worktree_get_histedit_script_path(&path, worktree);
4576 if (err)
4577 return err;
4579 f = fopen(path, "w");
4580 if (f == NULL) {
4581 err = got_error_from_errno2("fopen", path);
4582 goto done;
4584 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4585 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4586 repo);
4587 if (err)
4588 break;
4590 if (hle->logmsg) {
4591 int n = fprintf(f, "%c %s\n",
4592 GOT_HISTEDIT_MESG, hle->logmsg);
4593 if (n < 0) {
4594 err = got_ferror(f, GOT_ERR_IO);
4595 break;
4599 done:
4600 if (f && fclose(f) != 0 && err == NULL)
4601 err = got_error_from_errno("fclose");
4602 free(path);
4603 if (commit)
4604 got_object_commit_close(commit);
4605 return err;
4608 void
4609 histedit_free_list(struct got_histedit_list *histedit_cmds)
4611 struct got_histedit_list_entry *hle;
4613 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4614 TAILQ_REMOVE(histedit_cmds, hle, entry);
4615 free(hle);
4619 static const struct got_error *
4620 histedit_load_list(struct got_histedit_list *histedit_cmds,
4621 const char *path, struct got_repository *repo)
4623 const struct got_error *err = NULL;
4624 FILE *f = NULL;
4626 f = fopen(path, "r");
4627 if (f == NULL) {
4628 err = got_error_from_errno2("fopen", path);
4629 goto done;
4632 err = histedit_parse_list(histedit_cmds, f, repo);
4633 done:
4634 if (f && fclose(f) != 0 && err == NULL)
4635 err = got_error_from_errno("fclose");
4636 return err;
4639 static const struct got_error *
4640 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4641 const struct got_error *edit_err, struct got_object_id_queue *commits,
4642 const char *path, struct got_repository *repo)
4644 const struct got_error *err = NULL, *prev_err = edit_err;
4645 int resp = ' ';
4647 while (resp != 'c' && resp != 'r' && resp != 'a') {
4648 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4649 "or (a)bort: ", getprogname(), prev_err->msg);
4650 resp = getchar();
4651 if (resp == 'c') {
4652 histedit_free_list(histedit_cmds);
4653 err = histedit_run_editor(histedit_cmds, path, commits,
4654 repo);
4655 if (err) {
4656 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4657 err->code != GOT_ERR_HISTEDIT_CMD)
4658 break;
4659 prev_err = err;
4660 resp = ' ';
4661 continue;
4663 break;
4664 } else if (resp == 'r') {
4665 histedit_free_list(histedit_cmds);
4666 err = histedit_edit_script(histedit_cmds,
4667 commits, repo);
4668 if (err) {
4669 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4670 err->code != GOT_ERR_HISTEDIT_CMD)
4671 break;
4672 prev_err = err;
4673 resp = ' ';
4674 continue;
4676 break;
4677 } else if (resp == 'a') {
4678 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4679 break;
4680 } else
4681 printf("invalid response '%c'\n", resp);
4684 return err;
4687 static const struct got_error *
4688 histedit_complete(struct got_worktree *worktree,
4689 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4690 struct got_reference *branch, struct got_repository *repo)
4692 printf("Switching work tree to %s\n",
4693 got_ref_get_symref_target(branch));
4694 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4695 branch, repo);
4698 static const struct got_error *
4699 show_histedit_progress(struct got_commit_object *commit,
4700 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4702 const struct got_error *err;
4703 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4705 err = got_object_id_str(&old_id_str, hle->commit_id);
4706 if (err)
4707 goto done;
4709 if (new_id) {
4710 err = got_object_id_str(&new_id_str, new_id);
4711 if (err)
4712 goto done;
4715 old_id_str[12] = '\0';
4716 if (new_id_str)
4717 new_id_str[12] = '\0';
4719 if (hle->logmsg) {
4720 logmsg = strdup(hle->logmsg);
4721 if (logmsg == NULL) {
4722 err = got_error_from_errno("strdup");
4723 goto done;
4725 trim_logmsg(logmsg, 42);
4726 } else {
4727 err = get_short_logmsg(&logmsg, 42, commit);
4728 if (err)
4729 goto done;
4732 switch (hle->cmd->code) {
4733 case GOT_HISTEDIT_PICK:
4734 case GOT_HISTEDIT_EDIT:
4735 printf("%s -> %s: %s\n", old_id_str,
4736 new_id_str ? new_id_str : "no-op change", logmsg);
4737 break;
4738 case GOT_HISTEDIT_DROP:
4739 case GOT_HISTEDIT_FOLD:
4740 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4741 logmsg);
4742 break;
4743 default:
4744 break;
4747 done:
4748 free(old_id_str);
4749 free(new_id_str);
4750 return err;
4753 static const struct got_error *
4754 histedit_commit(struct got_pathlist_head *merged_paths,
4755 struct got_worktree *worktree, struct got_fileindex *fileindex,
4756 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4757 struct got_repository *repo)
4759 const struct got_error *err;
4760 struct got_commit_object *commit;
4761 struct got_object_id *new_commit_id;
4763 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4764 && hle->logmsg == NULL) {
4765 err = histedit_edit_logmsg(hle, repo);
4766 if (err)
4767 return err;
4770 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4771 if (err)
4772 return err;
4774 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4775 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4776 hle->logmsg, repo);
4777 if (err) {
4778 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4779 goto done;
4780 err = show_histedit_progress(commit, hle, NULL);
4781 } else {
4782 err = show_histedit_progress(commit, hle, new_commit_id);
4783 free(new_commit_id);
4785 done:
4786 got_object_commit_close(commit);
4787 return err;
4790 static const struct got_error *
4791 histedit_skip_commit(struct got_histedit_list_entry *hle,
4792 struct got_worktree *worktree, struct got_repository *repo)
4794 const struct got_error *error;
4795 struct got_commit_object *commit;
4797 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4798 repo);
4799 if (error)
4800 return error;
4802 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4803 if (error)
4804 return error;
4806 error = show_histedit_progress(commit, hle, NULL);
4807 got_object_commit_close(commit);
4808 return error;
4811 static const struct got_error *
4812 cmd_histedit(int argc, char *argv[])
4814 const struct got_error *error = NULL;
4815 struct got_worktree *worktree = NULL;
4816 struct got_fileindex *fileindex = NULL;
4817 struct got_repository *repo = NULL;
4818 char *cwd = NULL;
4819 struct got_reference *branch = NULL;
4820 struct got_reference *tmp_branch = NULL;
4821 struct got_object_id *resume_commit_id = NULL;
4822 struct got_object_id *base_commit_id = NULL;
4823 struct got_object_id *head_commit_id = NULL;
4824 struct got_commit_object *commit = NULL;
4825 int ch, rebase_in_progress = 0, did_something;
4826 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4827 const char *edit_script_path = NULL;
4828 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4829 struct got_object_id_queue commits;
4830 struct got_pathlist_head merged_paths;
4831 const struct got_object_id_queue *parent_ids;
4832 struct got_object_qid *pid;
4833 struct got_histedit_list histedit_cmds;
4834 struct got_histedit_list_entry *hle;
4836 SIMPLEQ_INIT(&commits);
4837 TAILQ_INIT(&histedit_cmds);
4838 TAILQ_INIT(&merged_paths);
4840 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4841 switch (ch) {
4842 case 'a':
4843 abort_edit = 1;
4844 break;
4845 case 'c':
4846 continue_edit = 1;
4847 break;
4848 case 'F':
4849 edit_script_path = optarg;
4850 break;
4851 default:
4852 usage_histedit();
4853 /* NOTREACHED */
4857 argc -= optind;
4858 argv += optind;
4860 #ifndef PROFILE
4861 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4862 "unveil", NULL) == -1)
4863 err(1, "pledge");
4864 #endif
4865 if (abort_edit && continue_edit)
4866 usage_histedit();
4867 if (argc != 0)
4868 usage_histedit();
4871 * This command cannot apply unveil(2) in all cases because the
4872 * user may choose to run an editor to edit the histedit script
4873 * and to edit individual commit log messages.
4874 * unveil(2) traverses exec(2); if an editor is used we have to
4875 * apply unveil after edit script and log messages have been written.
4876 * XXX TODO: Make use of unveil(2) where possible.
4879 cwd = getcwd(NULL, 0);
4880 if (cwd == NULL) {
4881 error = got_error_from_errno("getcwd");
4882 goto done;
4884 error = got_worktree_open(&worktree, cwd);
4885 if (error)
4886 goto done;
4888 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4889 if (error != NULL)
4890 goto done;
4892 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4893 if (error)
4894 goto done;
4895 if (rebase_in_progress) {
4896 error = got_error(GOT_ERR_REBASING);
4897 goto done;
4900 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4901 if (error)
4902 goto done;
4904 if (edit_in_progress && abort_edit) {
4905 error = got_worktree_histedit_continue(&resume_commit_id,
4906 &tmp_branch, &branch, &base_commit_id, &fileindex,
4907 worktree, repo);
4908 if (error)
4909 goto done;
4910 printf("Switching work tree to %s\n",
4911 got_ref_get_symref_target(branch));
4912 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4913 branch, base_commit_id, update_progress, &did_something);
4914 if (error)
4915 goto done;
4916 printf("Histedit of %s aborted\n",
4917 got_ref_get_symref_target(branch));
4918 goto done; /* nothing else to do */
4919 } else if (abort_edit) {
4920 error = got_error(GOT_ERR_NOT_HISTEDIT);
4921 goto done;
4924 if (continue_edit) {
4925 char *path;
4927 if (!edit_in_progress) {
4928 error = got_error(GOT_ERR_NOT_HISTEDIT);
4929 goto done;
4932 error = got_worktree_get_histedit_script_path(&path, worktree);
4933 if (error)
4934 goto done;
4936 error = histedit_load_list(&histedit_cmds, path, repo);
4937 free(path);
4938 if (error)
4939 goto done;
4941 error = got_worktree_histedit_continue(&resume_commit_id,
4942 &tmp_branch, &branch, &base_commit_id, &fileindex,
4943 worktree, repo);
4944 if (error)
4945 goto done;
4947 error = got_ref_resolve(&head_commit_id, repo, branch);
4948 if (error)
4949 goto done;
4951 error = got_object_open_as_commit(&commit, repo,
4952 head_commit_id);
4953 if (error)
4954 goto done;
4955 parent_ids = got_object_commit_get_parent_ids(commit);
4956 pid = SIMPLEQ_FIRST(parent_ids);
4957 if (pid == NULL) {
4958 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4959 goto done;
4961 error = collect_commits(&commits, head_commit_id, pid->id,
4962 base_commit_id, got_worktree_get_path_prefix(worktree),
4963 GOT_ERR_HISTEDIT_PATH, repo);
4964 got_object_commit_close(commit);
4965 commit = NULL;
4966 if (error)
4967 goto done;
4968 } else {
4969 if (edit_in_progress) {
4970 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4971 goto done;
4974 error = got_ref_open(&branch, repo,
4975 got_worktree_get_head_ref_name(worktree), 0);
4976 if (error != NULL)
4977 goto done;
4979 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4980 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4981 "will not edit commit history of a branch outside "
4982 "the \"refs/heads/\" reference namespace");
4983 goto done;
4986 error = got_ref_resolve(&head_commit_id, repo, branch);
4987 got_ref_close(branch);
4988 branch = NULL;
4989 if (error)
4990 goto done;
4992 error = got_object_open_as_commit(&commit, repo,
4993 head_commit_id);
4994 if (error)
4995 goto done;
4996 parent_ids = got_object_commit_get_parent_ids(commit);
4997 pid = SIMPLEQ_FIRST(parent_ids);
4998 if (pid == NULL) {
4999 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5000 goto done;
5002 error = collect_commits(&commits, head_commit_id, pid->id,
5003 got_worktree_get_base_commit_id(worktree),
5004 got_worktree_get_path_prefix(worktree),
5005 GOT_ERR_HISTEDIT_PATH, repo);
5006 got_object_commit_close(commit);
5007 commit = NULL;
5008 if (error)
5009 goto done;
5011 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5012 &base_commit_id, &fileindex, worktree, repo);
5013 if (error)
5014 goto done;
5016 if (edit_script_path) {
5017 error = histedit_load_list(&histedit_cmds,
5018 edit_script_path, repo);
5019 if (error) {
5020 got_worktree_histedit_abort(worktree, fileindex,
5021 repo, branch, base_commit_id,
5022 update_progress, &did_something);
5023 goto done;
5025 } else {
5026 error = histedit_edit_script(&histedit_cmds, &commits,
5027 repo);
5028 if (error) {
5029 got_worktree_histedit_abort(worktree, fileindex,
5030 repo, branch, base_commit_id,
5031 update_progress, &did_something);
5032 goto done;
5037 error = histedit_save_list(&histedit_cmds, worktree,
5038 repo);
5039 if (error) {
5040 got_worktree_histedit_abort(worktree, fileindex,
5041 repo, branch, base_commit_id,
5042 update_progress, &did_something);
5043 goto done;
5048 error = histedit_check_script(&histedit_cmds, &commits, repo);
5049 if (error)
5050 goto done;
5052 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5053 if (resume_commit_id) {
5054 if (got_object_id_cmp(hle->commit_id,
5055 resume_commit_id) != 0)
5056 continue;
5058 resume_commit_id = NULL;
5059 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5060 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5061 error = histedit_skip_commit(hle, worktree,
5062 repo);
5063 } else {
5064 error = histedit_commit(NULL, worktree,
5065 fileindex, tmp_branch, hle, repo);
5067 if (error)
5068 goto done;
5069 continue;
5072 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5073 error = histedit_skip_commit(hle, worktree, repo);
5074 if (error)
5075 goto done;
5076 continue;
5079 error = got_object_open_as_commit(&commit, repo,
5080 hle->commit_id);
5081 if (error)
5082 goto done;
5083 parent_ids = got_object_commit_get_parent_ids(commit);
5084 pid = SIMPLEQ_FIRST(parent_ids);
5086 error = got_worktree_histedit_merge_files(&merged_paths,
5087 worktree, fileindex, pid->id, hle->commit_id, repo,
5088 rebase_progress, &rebase_status, check_cancelled, NULL);
5089 if (error)
5090 goto done;
5091 got_object_commit_close(commit);
5092 commit = NULL;
5094 if (rebase_status == GOT_STATUS_CONFLICT) {
5095 got_worktree_rebase_pathlist_free(&merged_paths);
5096 break;
5099 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5100 char *id_str;
5101 error = got_object_id_str(&id_str, hle->commit_id);
5102 if (error)
5103 goto done;
5104 printf("Stopping histedit for amending commit %s\n",
5105 id_str);
5106 free(id_str);
5107 got_worktree_rebase_pathlist_free(&merged_paths);
5108 error = got_worktree_histedit_postpone(worktree,
5109 fileindex);
5110 goto done;
5113 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5114 error = histedit_skip_commit(hle, worktree, repo);
5115 if (error)
5116 goto done;
5117 continue;
5120 error = histedit_commit(&merged_paths, worktree, fileindex,
5121 tmp_branch, hle, repo);
5122 got_worktree_rebase_pathlist_free(&merged_paths);
5123 if (error)
5124 goto done;
5127 if (rebase_status == GOT_STATUS_CONFLICT) {
5128 error = got_worktree_histedit_postpone(worktree, fileindex);
5129 if (error)
5130 goto done;
5131 error = got_error_msg(GOT_ERR_CONFLICTS,
5132 "conflicts must be resolved before rebasing can continue");
5133 } else
5134 error = histedit_complete(worktree, fileindex, tmp_branch,
5135 branch, repo);
5136 done:
5137 got_object_id_queue_free(&commits);
5138 histedit_free_list(&histedit_cmds);
5139 free(head_commit_id);
5140 free(base_commit_id);
5141 free(resume_commit_id);
5142 if (commit)
5143 got_object_commit_close(commit);
5144 if (branch)
5145 got_ref_close(branch);
5146 if (tmp_branch)
5147 got_ref_close(tmp_branch);
5148 if (worktree)
5149 got_worktree_close(worktree);
5150 if (repo)
5151 got_repo_close(repo);
5152 return error;
5155 __dead static void
5156 usage_stage(void)
5158 fprintf(stderr, "usage: %s stage [-l] | file-path ...\n",
5159 getprogname());
5160 exit(1);
5163 static const struct got_error *
5164 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5165 const char *path, struct got_object_id *blob_id,
5166 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5168 const struct got_error *err = NULL;
5169 char *id_str = NULL;
5171 if (staged_status != GOT_STATUS_ADD &&
5172 staged_status != GOT_STATUS_MODIFY &&
5173 staged_status != GOT_STATUS_DELETE)
5174 return NULL;
5176 if (staged_status == GOT_STATUS_ADD ||
5177 staged_status == GOT_STATUS_MODIFY)
5178 err = got_object_id_str(&id_str, staged_blob_id);
5179 else
5180 err = got_object_id_str(&id_str, blob_id);
5181 if (err)
5182 return err;
5184 printf("%s %c %s\n", id_str, staged_status, path);
5185 free(id_str);
5186 return NULL;
5189 static const struct got_error *
5190 cmd_stage(int argc, char *argv[])
5192 const struct got_error *error = NULL;
5193 struct got_repository *repo = NULL;
5194 struct got_worktree *worktree = NULL;
5195 char *cwd = NULL;
5196 struct got_pathlist_head paths;
5197 struct got_pathlist_entry *pe;
5198 int ch, list_stage = 0;
5200 TAILQ_INIT(&paths);
5202 while ((ch = getopt(argc, argv, "l")) != -1) {
5203 switch (ch) {
5204 case 'l':
5205 list_stage = 1;
5206 break;
5207 default:
5208 usage_stage();
5209 /* NOTREACHED */
5213 argc -= optind;
5214 argv += optind;
5216 #ifndef PROFILE
5217 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5218 "unveil", NULL) == -1)
5219 err(1, "pledge");
5220 #endif
5221 if (!list_stage && argc < 1)
5222 usage_stage();
5224 cwd = getcwd(NULL, 0);
5225 if (cwd == NULL) {
5226 error = got_error_from_errno("getcwd");
5227 goto done;
5230 error = got_worktree_open(&worktree, cwd);
5231 if (error)
5232 goto done;
5234 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5235 if (error != NULL)
5236 goto done;
5238 error = apply_unveil(got_repo_get_path(repo), 1,
5239 got_worktree_get_root_path(worktree));
5240 if (error)
5241 goto done;
5243 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5244 if (error)
5245 goto done;
5247 if (list_stage)
5248 error = got_worktree_status(worktree, &paths, repo,
5249 print_stage, NULL, check_cancelled, NULL);
5250 else
5251 error = got_worktree_stage(worktree, &paths, print_status,
5252 NULL, repo);
5253 done:
5254 if (repo)
5255 got_repo_close(repo);
5256 if (worktree)
5257 got_worktree_close(worktree);
5258 TAILQ_FOREACH(pe, &paths, entry)
5259 free((char *)pe->path);
5260 got_pathlist_free(&paths);
5261 free(cwd);
5262 return error;
5265 __dead static void
5266 usage_unstage(void)
5268 fprintf(stderr, "usage: %s unstage [file-path ...]\n",
5269 getprogname());
5270 exit(1);
5274 static const struct got_error *
5275 cmd_unstage(int argc, char *argv[])
5277 const struct got_error *error = NULL;
5278 struct got_repository *repo = NULL;
5279 struct got_worktree *worktree = NULL;
5280 char *cwd = NULL;
5281 struct got_pathlist_head paths;
5282 struct got_pathlist_entry *pe;
5283 int ch, did_something = 0;
5285 TAILQ_INIT(&paths);
5287 while ((ch = getopt(argc, argv, "")) != -1) {
5288 switch (ch) {
5289 default:
5290 usage_unstage();
5291 /* NOTREACHED */
5295 argc -= optind;
5296 argv += optind;
5298 #ifndef PROFILE
5299 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5300 "unveil", NULL) == -1)
5301 err(1, "pledge");
5302 #endif
5303 cwd = getcwd(NULL, 0);
5304 if (cwd == NULL) {
5305 error = got_error_from_errno("getcwd");
5306 goto done;
5309 error = got_worktree_open(&worktree, cwd);
5310 if (error)
5311 goto done;
5313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5314 if (error != NULL)
5315 goto done;
5317 error = apply_unveil(got_repo_get_path(repo), 1,
5318 got_worktree_get_root_path(worktree));
5319 if (error)
5320 goto done;
5322 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5323 if (error)
5324 goto done;
5326 error = got_worktree_unstage(worktree, &paths, update_progress,
5327 &did_something, repo);
5328 done:
5329 if (repo)
5330 got_repo_close(repo);
5331 if (worktree)
5332 got_worktree_close(worktree);
5333 TAILQ_FOREACH(pe, &paths, entry)
5334 free((char *)pe->path);
5335 got_pathlist_free(&paths);
5336 free(cwd);
5337 return error;