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_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_worktree.h"
44 #include "got_diff.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
70 struct got_cmd {
71 const char *cmd_name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
75 };
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
97 static const struct got_error* cmd_init(int, char *[]);
98 static const struct got_error* cmd_import(int, char *[]);
99 static const struct got_error* cmd_checkout(int, char *[]);
100 static const struct got_error* cmd_update(int, char *[]);
101 static const struct got_error* cmd_log(int, char *[]);
102 static const struct got_error* cmd_diff(int, char *[]);
103 static const struct got_error* cmd_blame(int, char *[]);
104 static const struct got_error* cmd_tree(int, char *[]);
105 static const struct got_error* cmd_status(int, char *[]);
106 static const struct got_error* cmd_ref(int, char *[]);
107 static const struct got_error* cmd_branch(int, char *[]);
108 static const struct got_error* cmd_add(int, char *[]);
109 static const struct got_error* cmd_remove(int, char *[]);
110 static const struct got_error* cmd_revert(int, char *[]);
111 static const struct got_error* cmd_commit(int, char *[]);
112 static const struct got_error* cmd_cherrypick(int, char *[]);
113 static const struct got_error* cmd_backout(int, char *[]);
114 static const struct got_error* cmd_rebase(int, char *[]);
116 static struct got_cmd got_commands[] = {
117 { "init", cmd_init, usage_init, "" },
118 { "import", cmd_import, usage_import, "" },
119 { "checkout", cmd_checkout, usage_checkout, "co" },
120 { "update", cmd_update, usage_update, "up" },
121 { "log", cmd_log, usage_log, "" },
122 { "diff", cmd_diff, usage_diff, "" },
123 { "blame", cmd_blame, usage_blame, "" },
124 { "tree", cmd_tree, usage_tree, "" },
125 { "status", cmd_status, usage_status, "st" },
126 { "ref", cmd_ref, usage_ref, "" },
127 { "branch", cmd_branch, usage_branch, "br" },
128 { "add", cmd_add, usage_add, "" },
129 { "remove", cmd_remove, usage_remove, "rm" },
130 { "revert", cmd_revert, usage_revert, "rv" },
131 { "commit", cmd_commit, usage_commit, "ci" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 { "backout", cmd_backout, usage_backout, "bo" },
134 { "rebase", cmd_rebase, usage_rebase, "rb" },
135 };
137 static void
138 list_commands(void)
140 int i;
142 fprintf(stderr, "commands:");
143 for (i = 0; i < nitems(got_commands); i++) {
144 struct got_cmd *cmd = &got_commands[i];
145 fprintf(stderr, " %s", cmd->cmd_name);
147 fputc('\n', stderr);
150 int
151 main(int argc, char *argv[])
153 struct got_cmd *cmd;
154 unsigned int i;
155 int ch;
156 int hflag = 0;
158 setlocale(LC_CTYPE, "");
160 while ((ch = getopt(argc, argv, "h")) != -1) {
161 switch (ch) {
162 case 'h':
163 hflag = 1;
164 break;
165 default:
166 usage(hflag);
167 /* NOTREACHED */
171 argc -= optind;
172 argv += optind;
173 optind = 0;
175 if (argc <= 0)
176 usage(hflag);
178 signal(SIGINT, catch_sigint);
179 signal(SIGPIPE, catch_sigpipe);
181 for (i = 0; i < nitems(got_commands); i++) {
182 const struct got_error *error;
184 cmd = &got_commands[i];
186 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 strcmp(cmd->cmd_alias, argv[0]) != 0)
188 continue;
190 if (hflag)
191 got_commands[i].cmd_usage();
193 error = got_commands[i].cmd_main(argc, argv);
194 if (error && !(sigint_received || sigpipe_received)) {
195 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
196 return 1;
199 return 0;
202 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
203 list_commands();
204 return 1;
207 __dead static void
208 usage(int hflag)
210 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
211 if (hflag)
212 list_commands();
213 exit(1);
216 static const struct got_error *
217 get_editor(char **abspath)
219 const struct got_error *err = NULL;
220 const char *editor;
222 editor = getenv("VISUAL");
223 if (editor == NULL)
224 editor = getenv("EDITOR");
226 if (editor) {
227 err = got_path_find_prog(abspath, editor);
228 if (err)
229 return err;
232 if (*abspath == NULL) {
233 *abspath = strdup("/bin/ed");
234 if (*abspath == NULL)
235 return got_error_from_errno("strdup");
238 return NULL;
241 static const struct got_error *
242 apply_unveil(const char *repo_path, int repo_read_only,
243 const char *worktree_path, int create_worktree)
245 const struct got_error *err;
247 if (create_worktree) {
248 /* Pre-create work tree path to avoid unveiling its parents. */
249 err = got_path_mkdir(worktree_path);
251 if (errno == EEXIST) {
252 if (got_path_dir_is_empty(worktree_path)) {
253 errno = 0;
254 err = NULL;
255 } else {
256 err = got_error_path(worktree_path,
257 GOT_ERR_DIR_NOT_EMPTY);
261 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
262 return err;
265 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
266 return got_error_from_errno2("unveil", repo_path);
268 if (worktree_path && unveil(worktree_path, "rwc") != 0)
269 return got_error_from_errno2("unveil", worktree_path);
271 if (unveil("/tmp", "rwc") != 0)
272 return got_error_from_errno2("unveil", "/tmp");
274 err = got_privsep_unveil_exec_helpers();
275 if (err != NULL)
276 return err;
278 if (unveil(NULL, NULL) != 0)
279 return got_error_from_errno("unveil");
281 return NULL;
284 __dead static void
285 usage_init(void)
287 fprintf(stderr, "usage: %s init path\n", getprogname());
288 exit(1);
291 static const struct got_error *
292 cmd_init(int argc, char *argv[])
294 const struct got_error *error = NULL;
295 char *repo_path = NULL;
296 int ch;
298 while ((ch = getopt(argc, argv, "")) != -1) {
299 switch (ch) {
300 default:
301 usage_init();
302 /* NOTREACHED */
306 argc -= optind;
307 argv += optind;
309 #ifndef PROFILE
310 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
311 err(1, "pledge");
312 #endif
313 if (argc != 1)
314 usage_init();
316 repo_path = strdup(argv[0]);
317 if (repo_path == NULL)
318 return got_error_from_errno("strdup");
320 got_path_strip_trailing_slashes(repo_path);
322 error = got_path_mkdir(repo_path);
323 if (error &&
324 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
325 goto done;
327 error = apply_unveil(repo_path, 0, NULL, 0);
328 if (error)
329 goto done;
331 error = got_repo_init(repo_path);
332 if (error != NULL)
333 goto done;
335 done:
336 free(repo_path);
337 return error;
340 __dead static void
341 usage_import(void)
343 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
344 "[-r repository-path] [-I pattern] path\n", getprogname());
345 exit(1);
348 int
349 spawn_editor(const char *editor, const char *file)
351 pid_t pid;
352 sig_t sighup, sigint, sigquit;
353 int st = -1;
355 sighup = signal(SIGHUP, SIG_IGN);
356 sigint = signal(SIGINT, SIG_IGN);
357 sigquit = signal(SIGQUIT, SIG_IGN);
359 switch (pid = fork()) {
360 case -1:
361 goto doneediting;
362 case 0:
363 execl(editor, editor, file, (char *)NULL);
364 _exit(127);
367 while (waitpid(pid, &st, 0) == -1)
368 if (errno != EINTR)
369 break;
371 doneediting:
372 (void)signal(SIGHUP, sighup);
373 (void)signal(SIGINT, sigint);
374 (void)signal(SIGQUIT, sigquit);
376 if (!WIFEXITED(st)) {
377 errno = EINTR;
378 return -1;
381 return WEXITSTATUS(st);
384 static const struct got_error *
385 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
386 const char *initial_content)
388 const struct got_error *err = NULL;
389 char buf[1024];
390 struct stat st, st2;
391 FILE *fp;
392 int content_changed = 0;
393 size_t len;
395 *logmsg = NULL;
397 if (stat(logmsg_path, &st) == -1)
398 return got_error_from_errno2("stat", logmsg_path);
400 if (spawn_editor(editor, logmsg_path) == -1)
401 return got_error_from_errno("failed spawning editor");
403 if (stat(logmsg_path, &st2) == -1)
404 return got_error_from_errno("stat");
406 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
407 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
408 "no changes made to commit message, aborting");
410 *logmsg = malloc(st2.st_size + 1);
411 if (*logmsg == NULL)
412 return got_error_from_errno("malloc");
413 (*logmsg)[0] = '\0';
414 len = 0;
416 fp = fopen(logmsg_path, "r");
417 if (fp == NULL) {
418 err = got_error_from_errno("fopen");
419 goto done;
421 while (fgets(buf, sizeof(buf), fp) != NULL) {
422 if (!content_changed && strcmp(buf, initial_content) != 0)
423 content_changed = 1;
424 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
425 continue; /* remove comments and leading empty lines */
426 len = strlcat(*logmsg, buf, st2.st_size);
428 fclose(fp);
430 while (len > 0 && (*logmsg)[len - 1] == '\n') {
431 (*logmsg)[len - 1] = '\0';
432 len--;
435 if (len == 0 || !content_changed)
436 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "commit message cannot be empty, aborting");
438 done:
439 if (err) {
440 free(*logmsg);
441 *logmsg = NULL;
443 return err;
446 static const struct got_error *
447 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
448 const char *branch_name)
450 char *initial_content = NULL, *logmsg_path = NULL;
451 const struct got_error *err = NULL;
452 int fd;
454 if (asprintf(&initial_content,
455 "\n# %s to be imported to branch %s\n", path_dir,
456 branch_name) == -1)
457 return got_error_from_errno("asprintf");
459 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
460 if (err)
461 goto done;
463 dprintf(fd, initial_content);
464 close(fd);
466 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
467 done:
468 free(initial_content);
469 free(logmsg_path);
470 return err;
473 static const struct got_error *
474 import_progress(void *arg, const char *path)
476 printf("A %s\n", path);
477 return NULL;
480 static const struct got_error *
481 cmd_import(int argc, char *argv[])
483 const struct got_error *error = NULL;
484 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
485 char *editor = NULL;
486 const char *got_author = getenv("GOT_AUTHOR");
487 const char *branch_name = "master";
488 char *refname = NULL, *id_str = NULL;
489 struct got_repository *repo = NULL;
490 struct got_reference *branch_ref = NULL, *head_ref = NULL;
491 struct got_object_id *new_commit_id = NULL;
492 int ch;
493 struct got_pathlist_head ignores;
494 struct got_pathlist_entry *pe;
496 TAILQ_INIT(&ignores);
498 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
499 switch (ch) {
500 case 'b':
501 branch_name = optarg;
502 break;
503 case 'm':
504 logmsg = strdup(optarg);
505 if (logmsg == NULL) {
506 error = got_error_from_errno("strdup");
507 goto done;
509 break;
510 case 'r':
511 repo_path = realpath(optarg, NULL);
512 if (repo_path == NULL) {
513 error = got_error_from_errno("realpath");
514 goto done;
516 break;
517 case 'I':
518 if (optarg[0] == '\0')
519 break;
520 error = got_pathlist_insert(&pe, &ignores, optarg,
521 NULL);
522 if (error)
523 goto done;
524 break;
525 default:
526 usage_init();
527 /* NOTREACHED */
531 argc -= optind;
532 argv += optind;
534 #ifndef PROFILE
535 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
536 NULL) == -1)
537 err(1, "pledge");
538 #endif
539 if (argc != 1)
540 usage_import();
542 if (got_author == NULL) {
543 /* TODO: Look current user up in password database */
544 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
545 goto done;
548 if (repo_path == NULL) {
549 repo_path = getcwd(NULL, 0);
550 if (repo_path == NULL)
551 return got_error_from_errno("getcwd");
553 got_path_strip_trailing_slashes(repo_path);
554 error = got_repo_open(&repo, repo_path);
555 if (error)
556 goto done;
558 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
559 error = got_error_from_errno("asprintf");
560 goto done;
563 error = got_ref_open(&branch_ref, repo, refname, 0);
564 if (error) {
565 if (error->code != GOT_ERR_NOT_REF)
566 goto done;
567 } else {
568 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
569 "import target branch already exists");
570 goto done;
573 path_dir = realpath(argv[0], NULL);
574 if (path_dir == NULL) {
575 error = got_error_from_errno("realpath");
576 goto done;
578 got_path_strip_trailing_slashes(path_dir);
580 /*
581 * unveil(2) traverses exec(2); if an editor is used we have
582 * to apply unveil after the log message has been written.
583 */
584 if (logmsg == NULL || strlen(logmsg) == 0) {
585 error = get_editor(&editor);
586 if (error)
587 goto done;
588 error = collect_import_msg(&logmsg, editor, path_dir, refname);
589 if (error)
590 goto done;
593 if (unveil(path_dir, "r") != 0)
594 return got_error_from_errno2("unveil", path_dir);
596 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
597 if (error)
598 goto done;
600 error = got_repo_import(&new_commit_id, path_dir, logmsg,
601 got_author, &ignores, repo, import_progress, NULL);
602 if (error)
603 goto done;
605 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
606 if (error)
607 goto done;
609 error = got_ref_write(branch_ref, repo);
610 if (error)
611 goto done;
613 error = got_object_id_str(&id_str, new_commit_id);
614 if (error)
615 goto done;
617 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
618 if (error) {
619 if (error->code != GOT_ERR_NOT_REF)
620 goto done;
622 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
623 branch_ref);
624 if (error)
625 goto done;
627 error = got_ref_write(head_ref, repo);
628 if (error)
629 goto done;
632 printf("Created branch %s with commit %s\n",
633 got_ref_get_name(branch_ref), id_str);
634 done:
635 free(repo_path);
636 free(editor);
637 free(refname);
638 free(new_commit_id);
639 free(id_str);
640 if (branch_ref)
641 got_ref_close(branch_ref);
642 if (head_ref)
643 got_ref_close(head_ref);
644 return error;
647 __dead static void
648 usage_checkout(void)
650 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
651 "[-p prefix] repository-path [worktree-path]\n", getprogname());
652 exit(1);
655 static const struct got_error *
656 checkout_progress(void *arg, unsigned char status, const char *path)
658 char *worktree_path = arg;
660 /* Base commit bump happens silently. */
661 if (status == GOT_STATUS_BUMP_BASE)
662 return NULL;
664 while (path[0] == '/')
665 path++;
667 printf("%c %s/%s\n", status, worktree_path, path);
668 return NULL;
671 static const struct got_error *
672 check_cancelled(void *arg)
674 if (sigint_received || sigpipe_received)
675 return got_error(GOT_ERR_CANCELLED);
676 return NULL;
679 static const struct got_error *
680 check_linear_ancestry(struct got_object_id *commit_id,
681 struct got_object_id *base_commit_id, struct got_repository *repo)
683 const struct got_error *err = NULL;
684 struct got_object_id *yca_id;
686 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
687 commit_id, base_commit_id, repo);
688 if (err)
689 return err;
691 if (yca_id == NULL)
692 return got_error(GOT_ERR_ANCESTRY);
694 /*
695 * Require a straight line of history between the target commit
696 * and the work tree's base commit.
698 * Non-linear situations such as this require a rebase:
700 * (commit) D F (base_commit)
701 * \ /
702 * C E
703 * \ /
704 * B (yca)
705 * |
706 * A
708 * 'got update' only handles linear cases:
709 * Update forwards in time: A (base/yca) - B - C - D (commit)
710 * Update backwards in time: D (base) - C - B - A (commit/yca)
711 */
712 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
713 got_object_id_cmp(base_commit_id, yca_id) != 0)
714 return got_error(GOT_ERR_ANCESTRY);
716 free(yca_id);
717 return NULL;
720 static const struct got_error *
721 check_same_branch(struct got_object_id *commit_id,
722 struct got_reference *head_ref, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_commit_graph *graph = NULL;
726 struct got_object_id *head_commit_id = NULL;
727 int is_same_branch = 0;
729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
730 if (err)
731 goto done;
733 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
734 if (err)
735 goto done;
737 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
738 if (err)
739 goto done;
741 for (;;) {
742 struct got_object_id *id;
743 err = got_commit_graph_iter_next(&id, graph);
744 if (err) {
745 if (err->code == GOT_ERR_ITER_COMPLETED) {
746 err = NULL;
747 break;
749 else if (err->code != GOT_ERR_ITER_NEED_MORE)
750 break;
751 err = got_commit_graph_fetch_commits(graph, 1,
752 repo);
753 if (err)
754 break;
757 if (id) {
758 if (got_object_id_cmp(id, commit_id) == 0) {
759 is_same_branch = 1;
760 break;
764 done:
765 if (graph)
766 got_commit_graph_close(graph);
767 free(head_commit_id);
768 if (!err && !is_same_branch)
769 err = got_error(GOT_ERR_ANCESTRY);
770 return err;
773 static const struct got_error *
774 cmd_checkout(int argc, char *argv[])
776 const struct got_error *error = NULL;
777 struct got_repository *repo = NULL;
778 struct got_reference *head_ref = NULL;
779 struct got_worktree *worktree = NULL;
780 char *repo_path = NULL;
781 char *worktree_path = NULL;
782 const char *path_prefix = "";
783 const char *branch_name = GOT_REF_HEAD;
784 char *commit_id_str = NULL;
785 int ch, same_path_prefix;
787 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
788 switch (ch) {
789 case 'b':
790 branch_name = optarg;
791 break;
792 case 'c':
793 commit_id_str = strdup(optarg);
794 if (commit_id_str == NULL)
795 return got_error_from_errno("strdup");
796 break;
797 case 'p':
798 path_prefix = optarg;
799 break;
800 default:
801 usage_checkout();
802 /* NOTREACHED */
806 argc -= optind;
807 argv += optind;
809 #ifndef PROFILE
810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
811 "unveil", NULL) == -1)
812 err(1, "pledge");
813 #endif
814 if (argc == 1) {
815 char *cwd, *base, *dotgit;
816 repo_path = realpath(argv[0], NULL);
817 if (repo_path == NULL)
818 return got_error_from_errno2("realpath", argv[0]);
819 cwd = getcwd(NULL, 0);
820 if (cwd == NULL) {
821 error = got_error_from_errno("getcwd");
822 goto done;
824 if (path_prefix[0]) {
825 base = basename(path_prefix);
826 if (base == NULL) {
827 error = got_error_from_errno2("basename",
828 path_prefix);
829 goto done;
831 } else {
832 base = basename(repo_path);
833 if (base == NULL) {
834 error = got_error_from_errno2("basename",
835 repo_path);
836 goto done;
839 dotgit = strstr(base, ".git");
840 if (dotgit)
841 *dotgit = '\0';
842 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
843 error = got_error_from_errno("asprintf");
844 free(cwd);
845 goto done;
847 free(cwd);
848 } else if (argc == 2) {
849 repo_path = realpath(argv[0], NULL);
850 if (repo_path == NULL) {
851 error = got_error_from_errno2("realpath", argv[0]);
852 goto done;
854 worktree_path = realpath(argv[1], NULL);
855 if (worktree_path == NULL) {
856 error = got_error_from_errno2("realpath", argv[1]);
857 goto done;
859 } else
860 usage_checkout();
862 got_path_strip_trailing_slashes(repo_path);
863 got_path_strip_trailing_slashes(worktree_path);
865 error = got_repo_open(&repo, repo_path);
866 if (error != NULL)
867 goto done;
869 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
870 if (error)
871 goto done;
873 error = got_ref_open(&head_ref, repo, branch_name, 0);
874 if (error != NULL)
875 goto done;
877 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
878 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
879 goto done;
881 error = got_worktree_open(&worktree, worktree_path);
882 if (error != NULL)
883 goto done;
885 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
886 path_prefix);
887 if (error != NULL)
888 goto done;
889 if (!same_path_prefix) {
890 error = got_error(GOT_ERR_PATH_PREFIX);
891 goto done;
894 if (commit_id_str) {
895 struct got_object_id *commit_id;
896 error = got_repo_match_object_id_prefix(&commit_id,
897 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
898 if (error != NULL)
899 goto done;
900 error = check_linear_ancestry(commit_id,
901 got_worktree_get_base_commit_id(worktree), repo);
902 if (error != NULL) {
903 free(commit_id);
904 goto done;
906 error = check_same_branch(commit_id, head_ref, repo);
907 if (error)
908 goto done;
909 error = got_worktree_set_base_commit_id(worktree, repo,
910 commit_id);
911 free(commit_id);
912 if (error)
913 goto done;
916 error = got_worktree_checkout_files(worktree, "", repo,
917 checkout_progress, worktree_path, check_cancelled, NULL);
918 if (error != NULL)
919 goto done;
921 printf("Now shut up and hack\n");
923 done:
924 free(commit_id_str);
925 free(repo_path);
926 free(worktree_path);
927 return error;
930 __dead static void
931 usage_update(void)
933 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
934 getprogname());
935 exit(1);
938 static const struct got_error *
939 update_progress(void *arg, unsigned char status, const char *path)
941 int *did_something = arg;
943 if (status == GOT_STATUS_EXISTS)
944 return NULL;
946 *did_something = 1;
948 /* Base commit bump happens silently. */
949 if (status == GOT_STATUS_BUMP_BASE)
950 return NULL;
952 while (path[0] == '/')
953 path++;
954 printf("%c %s\n", status, path);
955 return NULL;
958 static const struct got_error *
959 switch_head_ref(struct got_reference *head_ref,
960 struct got_object_id *commit_id, struct got_worktree *worktree,
961 struct got_repository *repo)
963 const struct got_error *err = NULL;
964 char *base_id_str;
965 int ref_has_moved = 0;
967 /* Trivial case: switching between two different references. */
968 if (strcmp(got_ref_get_name(head_ref),
969 got_worktree_get_head_ref_name(worktree)) != 0) {
970 printf("Switching work tree from %s to %s\n",
971 got_worktree_get_head_ref_name(worktree),
972 got_ref_get_name(head_ref));
973 return got_worktree_set_head_ref(worktree, head_ref);
976 err = check_linear_ancestry(commit_id,
977 got_worktree_get_base_commit_id(worktree), repo);
978 if (err) {
979 if (err->code != GOT_ERR_ANCESTRY)
980 return err;
981 ref_has_moved = 1;
983 if (!ref_has_moved)
984 return NULL;
986 /* Switching to a rebased branch with the same reference name. */
987 err = got_object_id_str(&base_id_str,
988 got_worktree_get_base_commit_id(worktree));
989 if (err)
990 return err;
991 printf("Reference %s now points at a different branch\n",
992 got_worktree_get_head_ref_name(worktree));
993 printf("Switching work tree from %s to %s\n", base_id_str,
994 got_worktree_get_head_ref_name(worktree));
995 return NULL;
998 static const struct got_error *
999 cmd_update(int argc, char *argv[])
1001 const struct got_error *error = NULL;
1002 struct got_repository *repo = NULL;
1003 struct got_worktree *worktree = NULL;
1004 char *worktree_path = NULL, *path = NULL;
1005 struct got_object_id *commit_id = NULL;
1006 char *commit_id_str = NULL;
1007 const char *branch_name = NULL;
1008 struct got_reference *head_ref = NULL;
1009 int ch, did_something = 0, rebase_in_progress;
1011 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1012 switch (ch) {
1013 case 'b':
1014 branch_name = optarg;
1015 break;
1016 case 'c':
1017 commit_id_str = strdup(optarg);
1018 if (commit_id_str == NULL)
1019 return got_error_from_errno("strdup");
1020 break;
1021 default:
1022 usage_update();
1023 /* NOTREACHED */
1027 argc -= optind;
1028 argv += optind;
1030 #ifndef PROFILE
1031 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1032 "unveil", NULL) == -1)
1033 err(1, "pledge");
1034 #endif
1035 worktree_path = getcwd(NULL, 0);
1036 if (worktree_path == NULL) {
1037 error = got_error_from_errno("getcwd");
1038 goto done;
1040 error = got_worktree_open(&worktree, worktree_path);
1041 if (error)
1042 goto done;
1044 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1045 if (error)
1046 goto done;
1047 if (rebase_in_progress) {
1048 error = got_error(GOT_ERR_REBASING);
1049 goto done;
1052 if (argc == 0) {
1053 path = strdup("");
1054 if (path == NULL) {
1055 error = got_error_from_errno("strdup");
1056 goto done;
1058 } else if (argc == 1) {
1059 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1060 if (error)
1061 goto done;
1062 } else
1063 usage_update();
1065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1066 if (error != NULL)
1067 goto done;
1069 error = apply_unveil(got_repo_get_path(repo), 0,
1070 got_worktree_get_root_path(worktree), 0);
1071 if (error)
1072 goto done;
1074 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1075 got_worktree_get_head_ref_name(worktree), 0);
1076 if (error != NULL)
1077 goto done;
1078 if (commit_id_str == NULL) {
1079 error = got_ref_resolve(&commit_id, repo, head_ref);
1080 if (error != NULL)
1081 goto done;
1082 error = got_object_id_str(&commit_id_str, commit_id);
1083 if (error != NULL)
1084 goto done;
1085 } else {
1086 error = got_repo_match_object_id_prefix(&commit_id,
1087 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1088 if (error != NULL)
1089 goto done;
1090 free(commit_id_str);
1091 error = got_object_id_str(&commit_id_str, commit_id);
1092 if (error)
1093 goto done;
1096 if (branch_name) {
1097 struct got_object_id *head_commit_id;
1098 if (strlen(path) != 0) {
1099 fprintf(stderr, "%s: switching between branches "
1100 "requires that the entire work tree "
1101 "gets updated, not just '%s'\n",
1102 getprogname(), path);
1103 error = got_error(GOT_ERR_BAD_PATH);
1104 goto done;
1106 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1107 if (error)
1108 goto done;
1109 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1110 free(head_commit_id);
1111 if (error != NULL)
1112 goto done;
1113 error = check_same_branch(commit_id, head_ref, repo);
1114 if (error)
1115 goto done;
1116 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1117 if (error)
1118 goto done;
1119 } else {
1120 error = check_linear_ancestry(commit_id,
1121 got_worktree_get_base_commit_id(worktree), repo);
1122 if (error != NULL) {
1123 if (error->code == GOT_ERR_ANCESTRY)
1124 error = got_error(GOT_ERR_BRANCH_MOVED);
1125 goto done;
1127 error = check_same_branch(commit_id, head_ref, repo);
1128 if (error)
1129 goto done;
1132 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1133 commit_id) != 0) {
1134 error = got_worktree_set_base_commit_id(worktree, repo,
1135 commit_id);
1136 if (error)
1137 goto done;
1140 error = got_worktree_checkout_files(worktree, path, repo,
1141 update_progress, &did_something, check_cancelled, NULL);
1142 if (error != NULL)
1143 goto done;
1145 if (did_something)
1146 printf("Updated to commit %s\n", commit_id_str);
1147 else
1148 printf("Already up-to-date\n");
1149 done:
1150 free(worktree_path);
1151 free(path);
1152 free(commit_id);
1153 free(commit_id_str);
1154 return error;
1157 static const struct got_error *
1158 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1159 int diff_context, struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_tree_object *tree1 = NULL, *tree2;
1163 struct got_object_qid *qid;
1164 char *id_str1 = NULL, *id_str2;
1165 struct got_diff_blob_output_unidiff_arg arg;
1167 err = got_object_open_as_tree(&tree2, repo,
1168 got_object_commit_get_tree_id(commit));
1169 if (err)
1170 return err;
1172 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1173 if (qid != NULL) {
1174 struct got_commit_object *pcommit;
1176 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1177 if (err)
1178 return err;
1180 err = got_object_open_as_tree(&tree1, repo,
1181 got_object_commit_get_tree_id(pcommit));
1182 got_object_commit_close(pcommit);
1183 if (err)
1184 return err;
1186 err = got_object_id_str(&id_str1, qid->id);
1187 if (err)
1188 return err;
1191 err = got_object_id_str(&id_str2, id);
1192 if (err)
1193 goto done;
1195 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1196 arg.diff_context = diff_context;
1197 arg.outfile = stdout;
1198 err = got_diff_tree(tree1, tree2, "", "", repo,
1199 got_diff_blob_output_unidiff, &arg);
1200 done:
1201 if (tree1)
1202 got_object_tree_close(tree1);
1203 got_object_tree_close(tree2);
1204 free(id_str1);
1205 free(id_str2);
1206 return err;
1209 static char *
1210 get_datestr(time_t *time, char *datebuf)
1212 char *p, *s = ctime_r(time, datebuf);
1213 p = strchr(s, '\n');
1214 if (p)
1215 *p = '\0';
1216 return s;
1219 static const struct got_error *
1220 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1221 struct got_repository *repo, int show_patch, int diff_context,
1222 struct got_reflist_head *refs)
1224 const struct got_error *err = NULL;
1225 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1226 char datebuf[26];
1227 time_t committer_time;
1228 const char *author, *committer;
1229 char *refs_str = NULL;
1230 struct got_reflist_entry *re;
1232 SIMPLEQ_FOREACH(re, refs, entry) {
1233 char *s;
1234 const char *name;
1235 if (got_object_id_cmp(re->id, id) != 0)
1236 continue;
1237 name = got_ref_get_name(re->ref);
1238 if (strcmp(name, GOT_REF_HEAD) == 0)
1239 continue;
1240 if (strncmp(name, "refs/", 5) == 0)
1241 name += 5;
1242 if (strncmp(name, "got/", 4) == 0)
1243 continue;
1244 if (strncmp(name, "heads/", 6) == 0)
1245 name += 6;
1246 if (strncmp(name, "remotes/", 8) == 0)
1247 name += 8;
1248 s = refs_str;
1249 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1250 name) == -1) {
1251 err = got_error_from_errno("asprintf");
1252 free(s);
1253 break;
1255 free(s);
1257 err = got_object_id_str(&id_str, id);
1258 if (err)
1259 return err;
1261 printf("-----------------------------------------------\n");
1262 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1263 refs_str ? refs_str : "", refs_str ? ")" : "");
1264 free(id_str);
1265 id_str = NULL;
1266 free(refs_str);
1267 refs_str = NULL;
1268 printf("from: %s\n", got_object_commit_get_author(commit));
1269 committer_time = got_object_commit_get_committer_time(commit);
1270 datestr = get_datestr(&committer_time, datebuf);
1271 printf("date: %s UTC\n", datestr);
1272 author = got_object_commit_get_author(commit);
1273 committer = got_object_commit_get_committer(commit);
1274 if (strcmp(author, committer) != 0)
1275 printf("via: %s\n", committer);
1276 if (got_object_commit_get_nparents(commit) > 1) {
1277 const struct got_object_id_queue *parent_ids;
1278 struct got_object_qid *qid;
1279 int n = 1;
1280 parent_ids = got_object_commit_get_parent_ids(commit);
1281 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1282 err = got_object_id_str(&id_str, qid->id);
1283 if (err)
1284 return err;
1285 printf("parent %d: %s\n", n++, id_str);
1286 free(id_str);
1290 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1291 if (logmsg0 == NULL)
1292 return got_error_from_errno("strdup");
1294 logmsg = logmsg0;
1295 do {
1296 line = strsep(&logmsg, "\n");
1297 if (line)
1298 printf(" %s\n", line);
1299 } while (line);
1300 free(logmsg0);
1302 if (show_patch) {
1303 err = print_patch(commit, id, diff_context, repo);
1304 if (err == 0)
1305 printf("\n");
1308 if (fflush(stdout) != 0 && err == NULL)
1309 err = got_error_from_errno("fflush");
1310 return err;
1313 static const struct got_error *
1314 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1315 char *path, int show_patch, int diff_context, int limit,
1316 int first_parent_traversal, struct got_reflist_head *refs)
1318 const struct got_error *err;
1319 struct got_commit_graph *graph;
1321 err = got_commit_graph_open(&graph, root_id, path,
1322 first_parent_traversal, repo);
1323 if (err)
1324 return err;
1325 err = got_commit_graph_iter_start(graph, root_id, repo);
1326 if (err)
1327 goto done;
1328 for (;;) {
1329 struct got_commit_object *commit;
1330 struct got_object_id *id;
1332 if (sigint_received || sigpipe_received)
1333 break;
1335 err = got_commit_graph_iter_next(&id, graph);
1336 if (err) {
1337 if (err->code == GOT_ERR_ITER_COMPLETED) {
1338 err = NULL;
1339 break;
1341 if (err->code != GOT_ERR_ITER_NEED_MORE)
1342 break;
1343 err = got_commit_graph_fetch_commits(graph, 1, repo);
1344 if (err)
1345 break;
1346 else
1347 continue;
1349 if (id == NULL)
1350 break;
1352 err = got_object_open_as_commit(&commit, repo, id);
1353 if (err)
1354 break;
1355 err = print_commit(commit, id, repo, show_patch, diff_context,
1356 refs);
1357 got_object_commit_close(commit);
1358 if (err || (limit && --limit == 0))
1359 break;
1361 done:
1362 got_commit_graph_close(graph);
1363 return err;
1366 __dead static void
1367 usage_log(void)
1369 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1370 "[-r repository-path] [path]\n", getprogname());
1371 exit(1);
1374 static const struct got_error *
1375 cmd_log(int argc, char *argv[])
1377 const struct got_error *error;
1378 struct got_repository *repo = NULL;
1379 struct got_worktree *worktree = NULL;
1380 struct got_commit_object *commit = NULL;
1381 struct got_object_id *id = NULL;
1382 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1383 char *start_commit = NULL;
1384 int diff_context = 3, ch;
1385 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1386 const char *errstr;
1387 struct got_reflist_head refs;
1389 SIMPLEQ_INIT(&refs);
1391 #ifndef PROFILE
1392 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1393 NULL)
1394 == -1)
1395 err(1, "pledge");
1396 #endif
1398 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1399 switch (ch) {
1400 case 'p':
1401 show_patch = 1;
1402 break;
1403 case 'c':
1404 start_commit = optarg;
1405 break;
1406 case 'C':
1407 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1408 &errstr);
1409 if (errstr != NULL)
1410 err(1, "-C option %s", errstr);
1411 break;
1412 case 'l':
1413 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1414 if (errstr != NULL)
1415 err(1, "-l option %s", errstr);
1416 break;
1417 case 'f':
1418 first_parent_traversal = 1;
1419 break;
1420 case 'r':
1421 repo_path = realpath(optarg, NULL);
1422 if (repo_path == NULL)
1423 err(1, "-r option");
1424 got_path_strip_trailing_slashes(repo_path);
1425 break;
1426 default:
1427 usage_log();
1428 /* NOTREACHED */
1432 argc -= optind;
1433 argv += optind;
1435 cwd = getcwd(NULL, 0);
1436 if (cwd == NULL) {
1437 error = got_error_from_errno("getcwd");
1438 goto done;
1441 error = got_worktree_open(&worktree, cwd);
1442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1443 goto done;
1444 error = NULL;
1446 if (argc == 0) {
1447 path = strdup("");
1448 if (path == NULL) {
1449 error = got_error_from_errno("strdup");
1450 goto done;
1452 } else if (argc == 1) {
1453 if (worktree) {
1454 error = got_worktree_resolve_path(&path, worktree,
1455 argv[0]);
1456 if (error)
1457 goto done;
1458 } else {
1459 path = strdup(argv[0]);
1460 if (path == NULL) {
1461 error = got_error_from_errno("strdup");
1462 goto done;
1465 } else
1466 usage_log();
1468 if (repo_path == NULL) {
1469 repo_path = worktree ?
1470 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1472 if (repo_path == NULL) {
1473 error = got_error_from_errno("strdup");
1474 goto done;
1477 error = got_repo_open(&repo, repo_path);
1478 if (error != NULL)
1479 goto done;
1481 error = apply_unveil(got_repo_get_path(repo), 1,
1482 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1483 if (error)
1484 goto done;
1486 if (start_commit == NULL) {
1487 struct got_reference *head_ref;
1488 error = got_ref_open(&head_ref, repo,
1489 worktree ? got_worktree_get_head_ref_name(worktree)
1490 : GOT_REF_HEAD, 0);
1491 if (error != NULL)
1492 return error;
1493 error = got_ref_resolve(&id, repo, head_ref);
1494 got_ref_close(head_ref);
1495 if (error != NULL)
1496 return error;
1497 error = got_object_open_as_commit(&commit, repo, id);
1498 } else {
1499 struct got_reference *ref;
1500 error = got_ref_open(&ref, repo, start_commit, 0);
1501 if (error == NULL) {
1502 int obj_type;
1503 error = got_ref_resolve(&id, repo, ref);
1504 got_ref_close(ref);
1505 if (error != NULL)
1506 goto done;
1507 error = got_object_get_type(&obj_type, repo, id);
1508 if (error != NULL)
1509 goto done;
1510 if (obj_type == GOT_OBJ_TYPE_TAG) {
1511 struct got_tag_object *tag;
1512 error = got_object_open_as_tag(&tag, repo, id);
1513 if (error != NULL)
1514 goto done;
1515 if (got_object_tag_get_object_type(tag) !=
1516 GOT_OBJ_TYPE_COMMIT) {
1517 got_object_tag_close(tag);
1518 error = got_error(GOT_ERR_OBJ_TYPE);
1519 goto done;
1521 free(id);
1522 id = got_object_id_dup(
1523 got_object_tag_get_object_id(tag));
1524 if (id == NULL)
1525 error = got_error_from_errno(
1526 "got_object_id_dup");
1527 got_object_tag_close(tag);
1528 if (error)
1529 goto done;
1530 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1531 error = got_error(GOT_ERR_OBJ_TYPE);
1532 goto done;
1534 error = got_object_open_as_commit(&commit, repo, id);
1535 if (error != NULL)
1536 goto done;
1538 if (commit == NULL) {
1539 error = got_repo_match_object_id_prefix(&id,
1540 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1541 if (error != NULL)
1542 return error;
1545 if (error != NULL)
1546 goto done;
1548 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1549 if (error != NULL)
1550 goto done;
1551 if (in_repo_path) {
1552 free(path);
1553 path = in_repo_path;
1556 error = got_ref_list(&refs, repo);
1557 if (error)
1558 goto done;
1560 error = print_commits(id, repo, path, show_patch,
1561 diff_context, limit, first_parent_traversal, &refs);
1562 done:
1563 free(path);
1564 free(repo_path);
1565 free(cwd);
1566 free(id);
1567 if (worktree)
1568 got_worktree_close(worktree);
1569 if (repo) {
1570 const struct got_error *repo_error;
1571 repo_error = got_repo_close(repo);
1572 if (error == NULL)
1573 error = repo_error;
1575 got_ref_list_free(&refs);
1576 return error;
1579 __dead static void
1580 usage_diff(void)
1582 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1583 "[object1 object2 | path]\n", getprogname());
1584 exit(1);
1587 struct print_diff_arg {
1588 struct got_repository *repo;
1589 struct got_worktree *worktree;
1590 int diff_context;
1591 const char *id_str;
1592 int header_shown;
1595 static const struct got_error *
1596 print_diff(void *arg, unsigned char status, const char *path,
1597 struct got_object_id *blob_id, struct got_object_id *commit_id)
1599 struct print_diff_arg *a = arg;
1600 const struct got_error *err = NULL;
1601 struct got_blob_object *blob1 = NULL;
1602 FILE *f2 = NULL;
1603 char *abspath = NULL;
1604 struct stat sb;
1606 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1607 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1608 return NULL;
1610 if (!a->header_shown) {
1611 printf("diff %s %s\n", a->id_str,
1612 got_worktree_get_root_path(a->worktree));
1613 a->header_shown = 1;
1616 if (status != GOT_STATUS_ADD) {
1617 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1618 if (err)
1619 goto done;
1623 if (status != GOT_STATUS_DELETE) {
1624 if (asprintf(&abspath, "%s/%s",
1625 got_worktree_get_root_path(a->worktree), path) == -1) {
1626 err = got_error_from_errno("asprintf");
1627 goto done;
1630 f2 = fopen(abspath, "r");
1631 if (f2 == NULL) {
1632 err = got_error_from_errno2("fopen", abspath);
1633 goto done;
1635 if (lstat(abspath, &sb) == -1) {
1636 err = got_error_from_errno2("lstat", abspath);
1637 goto done;
1639 } else
1640 sb.st_size = 0;
1642 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1643 stdout);
1644 done:
1645 if (blob1)
1646 got_object_blob_close(blob1);
1647 if (f2 && fclose(f2) != 0 && err == NULL)
1648 err = got_error_from_errno("fclose");
1649 free(abspath);
1650 return err;
1653 static const struct got_error *
1654 cmd_diff(int argc, char *argv[])
1656 const struct got_error *error;
1657 struct got_repository *repo = NULL;
1658 struct got_worktree *worktree = NULL;
1659 char *cwd = NULL, *repo_path = NULL;
1660 struct got_object_id *id1 = NULL, *id2 = NULL;
1661 const char *id_str1 = NULL, *id_str2 = NULL;
1662 char *label1 = NULL, *label2 = NULL;
1663 int type1, type2;
1664 int diff_context = 3, ch;
1665 const char *errstr;
1666 char *path = NULL;
1668 #ifndef PROFILE
1669 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1670 NULL) == -1)
1671 err(1, "pledge");
1672 #endif
1674 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1675 switch (ch) {
1676 case 'C':
1677 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1678 if (errstr != NULL)
1679 err(1, "-C option %s", errstr);
1680 break;
1681 case 'r':
1682 repo_path = realpath(optarg, NULL);
1683 if (repo_path == NULL)
1684 err(1, "-r option");
1685 got_path_strip_trailing_slashes(repo_path);
1686 break;
1687 default:
1688 usage_diff();
1689 /* NOTREACHED */
1693 argc -= optind;
1694 argv += optind;
1696 cwd = getcwd(NULL, 0);
1697 if (cwd == NULL) {
1698 error = got_error_from_errno("getcwd");
1699 goto done;
1701 error = got_worktree_open(&worktree, cwd);
1702 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1703 goto done;
1704 if (argc <= 1) {
1705 if (worktree == NULL) {
1706 error = got_error(GOT_ERR_NOT_WORKTREE);
1707 goto done;
1709 if (repo_path)
1710 errx(1,
1711 "-r option can't be used when diffing a work tree");
1712 repo_path = strdup(got_worktree_get_repo_path(worktree));
1713 if (repo_path == NULL) {
1714 error = got_error_from_errno("strdup");
1715 goto done;
1717 if (argc == 1) {
1718 error = got_worktree_resolve_path(&path, worktree,
1719 argv[0]);
1720 if (error)
1721 goto done;
1722 } else {
1723 path = strdup("");
1724 if (path == NULL) {
1725 error = got_error_from_errno("strdup");
1726 goto done;
1729 } else if (argc == 2) {
1730 id_str1 = argv[0];
1731 id_str2 = argv[1];
1732 if (worktree && repo_path == NULL) {
1733 repo_path =
1734 strdup(got_worktree_get_repo_path(worktree));
1735 if (repo_path == NULL) {
1736 error = got_error_from_errno("strdup");
1737 goto done;
1740 } else
1741 usage_diff();
1743 if (repo_path == NULL) {
1744 repo_path = getcwd(NULL, 0);
1745 if (repo_path == NULL)
1746 return got_error_from_errno("getcwd");
1749 error = got_repo_open(&repo, repo_path);
1750 free(repo_path);
1751 if (error != NULL)
1752 goto done;
1754 error = apply_unveil(got_repo_get_path(repo), 1,
1755 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1756 if (error)
1757 goto done;
1759 if (argc <= 1) {
1760 struct print_diff_arg arg;
1761 char *id_str;
1762 error = got_object_id_str(&id_str,
1763 got_worktree_get_base_commit_id(worktree));
1764 if (error)
1765 goto done;
1766 arg.repo = repo;
1767 arg.worktree = worktree;
1768 arg.diff_context = diff_context;
1769 arg.id_str = id_str;
1770 arg.header_shown = 0;
1772 error = got_worktree_status(worktree, path, repo, print_diff,
1773 &arg, check_cancelled, NULL);
1774 free(id_str);
1775 goto done;
1778 error = got_repo_match_object_id_prefix(&id1, id_str1,
1779 GOT_OBJ_TYPE_ANY, repo);
1780 if (error) {
1781 struct got_reference *ref;
1782 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1783 goto done;
1784 error = got_ref_open(&ref, repo, id_str1, 0);
1785 if (error != NULL)
1786 goto done;
1787 label1 = strdup(got_ref_get_name(ref));
1788 if (label1 == NULL) {
1789 error = got_error_from_errno("strdup");
1790 goto done;
1792 error = got_ref_resolve(&id1, repo, ref);
1793 got_ref_close(ref);
1794 if (error != NULL)
1795 goto done;
1796 } else {
1797 error = got_object_id_str(&label1, id1);
1798 if (label1 == NULL) {
1799 error = got_error_from_errno("strdup");
1800 goto done;
1804 error = got_repo_match_object_id_prefix(&id2, id_str2,
1805 GOT_OBJ_TYPE_ANY, repo);
1806 if (error) {
1807 struct got_reference *ref;
1808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1809 goto done;
1810 error = got_ref_open(&ref, repo, id_str2, 0);
1811 if (error != NULL)
1812 goto done;
1813 label2 = strdup(got_ref_get_name(ref));
1814 if (label2 == NULL) {
1815 error = got_error_from_errno("strdup");
1816 goto done;
1818 error = got_ref_resolve(&id2, repo, ref);
1819 got_ref_close(ref);
1820 if (error != NULL)
1821 goto done;
1822 } else {
1823 error = got_object_id_str(&label2, id2);
1824 if (label2 == NULL) {
1825 error = got_error_from_errno("strdup");
1826 goto done;
1830 error = got_object_get_type(&type1, repo, id1);
1831 if (error)
1832 goto done;
1834 error = got_object_get_type(&type2, repo, id2);
1835 if (error)
1836 goto done;
1838 if (type1 != type2) {
1839 error = got_error(GOT_ERR_OBJ_TYPE);
1840 goto done;
1843 switch (type1) {
1844 case GOT_OBJ_TYPE_BLOB:
1845 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1846 diff_context, repo, stdout);
1847 break;
1848 case GOT_OBJ_TYPE_TREE:
1849 error = got_diff_objects_as_trees(id1, id2, "", "",
1850 diff_context, repo, stdout);
1851 break;
1852 case GOT_OBJ_TYPE_COMMIT:
1853 printf("diff %s %s\n", label1, label2);
1854 error = got_diff_objects_as_commits(id1, id2, diff_context,
1855 repo, stdout);
1856 break;
1857 default:
1858 error = got_error(GOT_ERR_OBJ_TYPE);
1861 done:
1862 free(label1);
1863 free(label2);
1864 free(id1);
1865 free(id2);
1866 free(path);
1867 if (worktree)
1868 got_worktree_close(worktree);
1869 if (repo) {
1870 const struct got_error *repo_error;
1871 repo_error = got_repo_close(repo);
1872 if (error == NULL)
1873 error = repo_error;
1875 return error;
1878 __dead static void
1879 usage_blame(void)
1881 fprintf(stderr,
1882 "usage: %s blame [-c commit] [-r repository-path] path\n",
1883 getprogname());
1884 exit(1);
1887 static const struct got_error *
1888 cmd_blame(int argc, char *argv[])
1890 const struct got_error *error;
1891 struct got_repository *repo = NULL;
1892 struct got_worktree *worktree = NULL;
1893 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1894 struct got_object_id *commit_id = NULL;
1895 char *commit_id_str = NULL;
1896 int ch;
1898 #ifndef PROFILE
1899 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1900 NULL) == -1)
1901 err(1, "pledge");
1902 #endif
1904 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1905 switch (ch) {
1906 case 'c':
1907 commit_id_str = optarg;
1908 break;
1909 case 'r':
1910 repo_path = realpath(optarg, NULL);
1911 if (repo_path == NULL)
1912 err(1, "-r option");
1913 got_path_strip_trailing_slashes(repo_path);
1914 break;
1915 default:
1916 usage_blame();
1917 /* NOTREACHED */
1921 argc -= optind;
1922 argv += optind;
1924 if (argc == 1)
1925 path = argv[0];
1926 else
1927 usage_blame();
1929 cwd = getcwd(NULL, 0);
1930 if (cwd == NULL) {
1931 error = got_error_from_errno("getcwd");
1932 goto done;
1934 if (repo_path == NULL) {
1935 error = got_worktree_open(&worktree, cwd);
1936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1937 goto done;
1938 else
1939 error = NULL;
1940 if (worktree) {
1941 repo_path =
1942 strdup(got_worktree_get_repo_path(worktree));
1943 if (repo_path == NULL)
1944 error = got_error_from_errno("strdup");
1945 if (error)
1946 goto done;
1947 } else {
1948 repo_path = strdup(cwd);
1949 if (repo_path == NULL) {
1950 error = got_error_from_errno("strdup");
1951 goto done;
1956 error = got_repo_open(&repo, repo_path);
1957 if (error != NULL)
1958 goto done;
1960 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1961 if (error)
1962 goto done;
1964 if (worktree) {
1965 const char *prefix = got_worktree_get_path_prefix(worktree);
1966 char *p, *worktree_subdir = cwd +
1967 strlen(got_worktree_get_root_path(worktree));
1968 if (asprintf(&p, "%s%s%s%s%s",
1969 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1970 worktree_subdir, worktree_subdir[0] ? "/" : "",
1971 path) == -1) {
1972 error = got_error_from_errno("asprintf");
1973 goto done;
1975 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1976 free(p);
1977 } else {
1978 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1980 if (error)
1981 goto done;
1983 if (commit_id_str == NULL) {
1984 struct got_reference *head_ref;
1985 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1986 if (error != NULL)
1987 goto done;
1988 error = got_ref_resolve(&commit_id, repo, head_ref);
1989 got_ref_close(head_ref);
1990 if (error != NULL)
1991 goto done;
1992 } else {
1993 error = got_repo_match_object_id_prefix(&commit_id,
1994 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1995 if (error != NULL)
1996 goto done;
1999 error = got_blame(in_repo_path, commit_id, repo, stdout);
2000 done:
2001 free(in_repo_path);
2002 free(repo_path);
2003 free(cwd);
2004 free(commit_id);
2005 if (worktree)
2006 got_worktree_close(worktree);
2007 if (repo) {
2008 const struct got_error *repo_error;
2009 repo_error = got_repo_close(repo);
2010 if (error == NULL)
2011 error = repo_error;
2013 return error;
2016 __dead static void
2017 usage_tree(void)
2019 fprintf(stderr,
2020 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2021 getprogname());
2022 exit(1);
2025 static void
2026 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2027 const char *root_path)
2029 int is_root_path = (strcmp(path, root_path) == 0);
2031 path += strlen(root_path);
2032 while (path[0] == '/')
2033 path++;
2035 printf("%s%s%s%s%s\n", id ? id : "", path,
2036 is_root_path ? "" : "/", te->name,
2037 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2040 static const struct got_error *
2041 print_tree(const char *path, struct got_object_id *commit_id,
2042 int show_ids, int recurse, const char *root_path,
2043 struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_object_id *tree_id = NULL;
2047 struct got_tree_object *tree = NULL;
2048 const struct got_tree_entries *entries;
2049 struct got_tree_entry *te;
2051 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2052 if (err)
2053 goto done;
2055 err = got_object_open_as_tree(&tree, repo, tree_id);
2056 if (err)
2057 goto done;
2058 entries = got_object_tree_get_entries(tree);
2059 te = SIMPLEQ_FIRST(&entries->head);
2060 while (te) {
2061 char *id = NULL;
2063 if (sigint_received || sigpipe_received)
2064 break;
2066 if (show_ids) {
2067 char *id_str;
2068 err = got_object_id_str(&id_str, te->id);
2069 if (err)
2070 goto done;
2071 if (asprintf(&id, "%s ", id_str) == -1) {
2072 err = got_error_from_errno("asprintf");
2073 free(id_str);
2074 goto done;
2076 free(id_str);
2078 print_entry(te, id, path, root_path);
2079 free(id);
2081 if (recurse && S_ISDIR(te->mode)) {
2082 char *child_path;
2083 if (asprintf(&child_path, "%s%s%s", path,
2084 path[0] == '/' && path[1] == '\0' ? "" : "/",
2085 te->name) == -1) {
2086 err = got_error_from_errno("asprintf");
2087 goto done;
2089 err = print_tree(child_path, commit_id, show_ids, 1,
2090 root_path, repo);
2091 free(child_path);
2092 if (err)
2093 goto done;
2096 te = SIMPLEQ_NEXT(te, entry);
2098 done:
2099 if (tree)
2100 got_object_tree_close(tree);
2101 free(tree_id);
2102 return err;
2105 static const struct got_error *
2106 cmd_tree(int argc, char *argv[])
2108 const struct got_error *error;
2109 struct got_repository *repo = NULL;
2110 struct got_worktree *worktree = NULL;
2111 const char *path;
2112 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2113 struct got_object_id *commit_id = NULL;
2114 char *commit_id_str = NULL;
2115 int show_ids = 0, recurse = 0;
2116 int ch;
2118 #ifndef PROFILE
2119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2120 NULL) == -1)
2121 err(1, "pledge");
2122 #endif
2124 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2125 switch (ch) {
2126 case 'c':
2127 commit_id_str = optarg;
2128 break;
2129 case 'r':
2130 repo_path = realpath(optarg, NULL);
2131 if (repo_path == NULL)
2132 err(1, "-r option");
2133 got_path_strip_trailing_slashes(repo_path);
2134 break;
2135 case 'i':
2136 show_ids = 1;
2137 break;
2138 case 'R':
2139 recurse = 1;
2140 break;
2141 default:
2142 usage_tree();
2143 /* NOTREACHED */
2147 argc -= optind;
2148 argv += optind;
2150 if (argc == 1)
2151 path = argv[0];
2152 else if (argc > 1)
2153 usage_tree();
2154 else
2155 path = NULL;
2157 cwd = getcwd(NULL, 0);
2158 if (cwd == NULL) {
2159 error = got_error_from_errno("getcwd");
2160 goto done;
2162 if (repo_path == NULL) {
2163 error = got_worktree_open(&worktree, cwd);
2164 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2165 goto done;
2166 else
2167 error = NULL;
2168 if (worktree) {
2169 repo_path =
2170 strdup(got_worktree_get_repo_path(worktree));
2171 if (repo_path == NULL)
2172 error = got_error_from_errno("strdup");
2173 if (error)
2174 goto done;
2175 } else {
2176 repo_path = strdup(cwd);
2177 if (repo_path == NULL) {
2178 error = got_error_from_errno("strdup");
2179 goto done;
2184 error = got_repo_open(&repo, repo_path);
2185 if (error != NULL)
2186 goto done;
2188 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2189 if (error)
2190 goto done;
2192 if (path == NULL) {
2193 if (worktree) {
2194 char *p, *worktree_subdir = cwd +
2195 strlen(got_worktree_get_root_path(worktree));
2196 if (asprintf(&p, "%s/%s",
2197 got_worktree_get_path_prefix(worktree),
2198 worktree_subdir) == -1) {
2199 error = got_error_from_errno("asprintf");
2200 goto done;
2202 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2203 free(p);
2204 if (error)
2205 goto done;
2206 } else
2207 path = "/";
2209 if (in_repo_path == NULL) {
2210 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2211 if (error != NULL)
2212 goto done;
2215 if (commit_id_str == NULL) {
2216 struct got_reference *head_ref;
2217 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2218 if (error != NULL)
2219 goto done;
2220 error = got_ref_resolve(&commit_id, repo, head_ref);
2221 got_ref_close(head_ref);
2222 if (error != NULL)
2223 goto done;
2224 } else {
2225 error = got_repo_match_object_id_prefix(&commit_id,
2226 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2227 if (error != NULL)
2228 goto done;
2231 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2232 in_repo_path, repo);
2233 done:
2234 free(in_repo_path);
2235 free(repo_path);
2236 free(cwd);
2237 free(commit_id);
2238 if (worktree)
2239 got_worktree_close(worktree);
2240 if (repo) {
2241 const struct got_error *repo_error;
2242 repo_error = got_repo_close(repo);
2243 if (error == NULL)
2244 error = repo_error;
2246 return error;
2249 __dead static void
2250 usage_status(void)
2252 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2253 exit(1);
2256 static const struct got_error *
2257 print_status(void *arg, unsigned char status, const char *path,
2258 struct got_object_id *blob_id, struct got_object_id *commit_id)
2260 printf("%c %s\n", status, path);
2261 return NULL;
2264 static const struct got_error *
2265 cmd_status(int argc, char *argv[])
2267 const struct got_error *error = NULL;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2270 char *cwd = NULL, *path = NULL;
2271 int ch;
2273 while ((ch = getopt(argc, argv, "")) != -1) {
2274 switch (ch) {
2275 default:
2276 usage_status();
2277 /* NOTREACHED */
2281 argc -= optind;
2282 argv += optind;
2284 #ifndef PROFILE
2285 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2286 NULL) == -1)
2287 err(1, "pledge");
2288 #endif
2289 cwd = getcwd(NULL, 0);
2290 if (cwd == NULL) {
2291 error = got_error_from_errno("getcwd");
2292 goto done;
2295 error = got_worktree_open(&worktree, cwd);
2296 if (error != NULL)
2297 goto done;
2299 if (argc == 0) {
2300 path = strdup("");
2301 if (path == NULL) {
2302 error = got_error_from_errno("strdup");
2303 goto done;
2305 } else if (argc == 1) {
2306 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2307 if (error)
2308 goto done;
2309 } else
2310 usage_status();
2312 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2313 if (error != NULL)
2314 goto done;
2316 error = apply_unveil(got_repo_get_path(repo), 1,
2317 got_worktree_get_root_path(worktree), 0);
2318 if (error)
2319 goto done;
2321 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2322 check_cancelled, NULL);
2323 done:
2324 free(cwd);
2325 free(path);
2326 return error;
2329 __dead static void
2330 usage_ref(void)
2332 fprintf(stderr,
2333 "usage: %s ref [-r repository] -l | -d name | name target\n",
2334 getprogname());
2335 exit(1);
2338 static const struct got_error *
2339 list_refs(struct got_repository *repo)
2341 static const struct got_error *err = NULL;
2342 struct got_reflist_head refs;
2343 struct got_reflist_entry *re;
2345 SIMPLEQ_INIT(&refs);
2346 err = got_ref_list(&refs, repo);
2347 if (err)
2348 return err;
2350 SIMPLEQ_FOREACH(re, &refs, entry) {
2351 char *refstr;
2352 refstr = got_ref_to_str(re->ref);
2353 if (refstr == NULL)
2354 return got_error_from_errno("got_ref_to_str");
2355 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2356 free(refstr);
2359 got_ref_list_free(&refs);
2360 return NULL;
2363 static const struct got_error *
2364 delete_ref(struct got_repository *repo, const char *refname)
2366 const struct got_error *err = NULL;
2367 struct got_reference *ref;
2369 err = got_ref_open(&ref, repo, refname, 0);
2370 if (err)
2371 return err;
2373 err = got_ref_delete(ref, repo);
2374 got_ref_close(ref);
2375 return err;
2378 static const struct got_error *
2379 add_ref(struct got_repository *repo, const char *refname, const char *target)
2381 const struct got_error *err = NULL;
2382 struct got_object_id *id;
2383 struct got_reference *ref = NULL;
2386 * Don't let the user create a reference named '-'.
2387 * While technically a valid reference name, this case is usually
2388 * an unintended typo.
2390 if (refname[0] == '-' && refname[1] == '\0')
2391 return got_error(GOT_ERR_BAD_REF_NAME);
2393 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2394 repo);
2395 if (err) {
2396 struct got_reference *target_ref;
2398 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2399 return err;
2400 err = got_ref_open(&target_ref, repo, target, 0);
2401 if (err)
2402 return err;
2403 err = got_ref_resolve(&id, repo, target_ref);
2404 got_ref_close(target_ref);
2405 if (err)
2406 return err;
2409 err = got_ref_alloc(&ref, refname, id);
2410 if (err)
2411 goto done;
2413 err = got_ref_write(ref, repo);
2414 done:
2415 if (ref)
2416 got_ref_close(ref);
2417 free(id);
2418 return err;
2421 static const struct got_error *
2422 cmd_ref(int argc, char *argv[])
2424 const struct got_error *error = NULL;
2425 struct got_repository *repo = NULL;
2426 struct got_worktree *worktree = NULL;
2427 char *cwd = NULL, *repo_path = NULL;
2428 int ch, do_list = 0;
2429 const char *delref = NULL;
2431 /* TODO: Add -s option for adding symbolic references. */
2432 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2433 switch (ch) {
2434 case 'd':
2435 delref = optarg;
2436 break;
2437 case 'r':
2438 repo_path = realpath(optarg, NULL);
2439 if (repo_path == NULL)
2440 err(1, "-r option");
2441 got_path_strip_trailing_slashes(repo_path);
2442 break;
2443 case 'l':
2444 do_list = 1;
2445 break;
2446 default:
2447 usage_ref();
2448 /* NOTREACHED */
2452 if (do_list && delref)
2453 errx(1, "-l and -d options are mutually exclusive\n");
2455 argc -= optind;
2456 argv += optind;
2458 if (do_list || delref) {
2459 if (argc > 0)
2460 usage_ref();
2461 } else if (argc != 2)
2462 usage_ref();
2464 #ifndef PROFILE
2465 if (do_list) {
2466 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2467 NULL) == -1)
2468 err(1, "pledge");
2469 } else {
2470 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2471 "sendfd unveil", NULL) == -1)
2472 err(1, "pledge");
2474 #endif
2475 cwd = getcwd(NULL, 0);
2476 if (cwd == NULL) {
2477 error = got_error_from_errno("getcwd");
2478 goto done;
2481 if (repo_path == NULL) {
2482 error = got_worktree_open(&worktree, cwd);
2483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2484 goto done;
2485 else
2486 error = NULL;
2487 if (worktree) {
2488 repo_path =
2489 strdup(got_worktree_get_repo_path(worktree));
2490 if (repo_path == NULL)
2491 error = got_error_from_errno("strdup");
2492 if (error)
2493 goto done;
2494 } else {
2495 repo_path = strdup(cwd);
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2498 goto done;
2503 error = got_repo_open(&repo, repo_path);
2504 if (error != NULL)
2505 goto done;
2507 error = apply_unveil(got_repo_get_path(repo), do_list,
2508 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2509 if (error)
2510 goto done;
2512 if (do_list)
2513 error = list_refs(repo);
2514 else if (delref)
2515 error = delete_ref(repo, delref);
2516 else
2517 error = add_ref(repo, argv[0], argv[1]);
2518 done:
2519 if (repo)
2520 got_repo_close(repo);
2521 if (worktree)
2522 got_worktree_close(worktree);
2523 free(cwd);
2524 free(repo_path);
2525 return error;
2528 __dead static void
2529 usage_branch(void)
2531 fprintf(stderr,
2532 "usage: %s branch [-r repository] -l | -d name | "
2533 "name [base-branch]\n", getprogname());
2534 exit(1);
2537 static const struct got_error *
2538 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2540 static const struct got_error *err = NULL;
2541 struct got_reflist_head refs;
2542 struct got_reflist_entry *re;
2544 SIMPLEQ_INIT(&refs);
2546 err = got_ref_list(&refs, repo);
2547 if (err)
2548 return err;
2550 SIMPLEQ_FOREACH(re, &refs, entry) {
2551 const char *refname, *marker = " ";
2552 char *refstr;
2553 refname = got_ref_get_name(re->ref);
2554 if (strncmp(refname, "refs/heads/", 11) != 0)
2555 continue;
2556 if (worktree && strcmp(refname,
2557 got_worktree_get_head_ref_name(worktree)) == 0) {
2558 struct got_object_id *id = NULL;
2559 err = got_ref_resolve(&id, repo, re->ref);
2560 if (err)
2561 return err;
2562 if (got_object_id_cmp(id,
2563 got_worktree_get_base_commit_id(worktree)) == 0)
2564 marker = "* ";
2565 else
2566 marker = "~ ";
2567 free(id);
2569 refname += 11;
2570 refstr = got_ref_to_str(re->ref);
2571 if (refstr == NULL)
2572 return got_error_from_errno("got_ref_to_str");
2573 printf("%s%s: %s\n", marker, refname, refstr);
2574 free(refstr);
2577 got_ref_list_free(&refs);
2578 return NULL;
2581 static const struct got_error *
2582 delete_branch(struct got_repository *repo, const char *branch_name)
2584 const struct got_error *err = NULL;
2585 struct got_reference *ref;
2586 char *refname;
2588 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2589 return got_error_from_errno("asprintf");
2591 err = got_ref_open(&ref, repo, refname, 0);
2592 if (err)
2593 goto done;
2595 err = got_ref_delete(ref, repo);
2596 got_ref_close(ref);
2597 done:
2598 free(refname);
2599 return err;
2602 static const struct got_error *
2603 add_branch(struct got_repository *repo, const char *branch_name,
2604 const char *base_branch)
2606 const struct got_error *err = NULL;
2607 struct got_object_id *id = NULL;
2608 struct got_reference *ref = NULL;
2609 char *base_refname = NULL, *refname = NULL;
2610 struct got_reference *base_ref;
2613 * Don't let the user create a branch named '-'.
2614 * While technically a valid reference name, this case is usually
2615 * an unintended typo.
2617 if (branch_name[0] == '-' && branch_name[1] == '\0')
2618 return got_error(GOT_ERR_BAD_REF_NAME);
2620 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2621 base_refname = strdup(GOT_REF_HEAD);
2622 if (base_refname == NULL)
2623 return got_error_from_errno("strdup");
2624 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2625 return got_error_from_errno("asprintf");
2627 err = got_ref_open(&base_ref, repo, base_refname, 0);
2628 if (err)
2629 goto done;
2630 err = got_ref_resolve(&id, repo, base_ref);
2631 got_ref_close(base_ref);
2632 if (err)
2633 goto done;
2635 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2636 err = got_error_from_errno("asprintf");
2637 goto done;
2640 err = got_ref_open(&ref, repo, refname, 0);
2641 if (err == NULL) {
2642 err = got_error(GOT_ERR_BRANCH_EXISTS);
2643 goto done;
2644 } else if (err->code != GOT_ERR_NOT_REF)
2645 goto done;
2647 err = got_ref_alloc(&ref, refname, id);
2648 if (err)
2649 goto done;
2651 err = got_ref_write(ref, repo);
2652 done:
2653 if (ref)
2654 got_ref_close(ref);
2655 free(id);
2656 free(base_refname);
2657 free(refname);
2658 return err;
2661 static const struct got_error *
2662 cmd_branch(int argc, char *argv[])
2664 const struct got_error *error = NULL;
2665 struct got_repository *repo = NULL;
2666 struct got_worktree *worktree = NULL;
2667 char *cwd = NULL, *repo_path = NULL;
2668 int ch, do_list = 0;
2669 const char *delref = NULL;
2671 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2672 switch (ch) {
2673 case 'd':
2674 delref = optarg;
2675 break;
2676 case 'r':
2677 repo_path = realpath(optarg, NULL);
2678 if (repo_path == NULL)
2679 err(1, "-r option");
2680 got_path_strip_trailing_slashes(repo_path);
2681 break;
2682 case 'l':
2683 do_list = 1;
2684 break;
2685 default:
2686 usage_branch();
2687 /* NOTREACHED */
2691 if (do_list && delref)
2692 errx(1, "-l and -d options are mutually exclusive\n");
2694 argc -= optind;
2695 argv += optind;
2697 if (do_list || delref) {
2698 if (argc > 0)
2699 usage_branch();
2700 } else if (argc < 1 || argc > 2)
2701 usage_branch();
2703 #ifndef PROFILE
2704 if (do_list) {
2705 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2706 NULL) == -1)
2707 err(1, "pledge");
2708 } else {
2709 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2710 "sendfd unveil", NULL) == -1)
2711 err(1, "pledge");
2713 #endif
2714 cwd = getcwd(NULL, 0);
2715 if (cwd == NULL) {
2716 error = got_error_from_errno("getcwd");
2717 goto done;
2720 if (repo_path == NULL) {
2721 error = got_worktree_open(&worktree, cwd);
2722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2723 goto done;
2724 else
2725 error = NULL;
2726 if (worktree) {
2727 repo_path =
2728 strdup(got_worktree_get_repo_path(worktree));
2729 if (repo_path == NULL)
2730 error = got_error_from_errno("strdup");
2731 if (error)
2732 goto done;
2733 } else {
2734 repo_path = strdup(cwd);
2735 if (repo_path == NULL) {
2736 error = got_error_from_errno("strdup");
2737 goto done;
2742 error = got_repo_open(&repo, repo_path);
2743 if (error != NULL)
2744 goto done;
2746 error = apply_unveil(got_repo_get_path(repo), do_list,
2747 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2748 if (error)
2749 goto done;
2751 if (do_list)
2752 error = list_branches(repo, worktree);
2753 else if (delref)
2754 error = delete_branch(repo, delref);
2755 else {
2756 const char *base_branch;
2757 if (argc == 1) {
2758 base_branch = worktree ?
2759 got_worktree_get_head_ref_name(worktree) :
2760 GOT_REF_HEAD;
2761 } else
2762 base_branch = argv[1];
2763 error = add_branch(repo, argv[0], base_branch);
2765 done:
2766 if (repo)
2767 got_repo_close(repo);
2768 if (worktree)
2769 got_worktree_close(worktree);
2770 free(cwd);
2771 free(repo_path);
2772 return error;
2775 __dead static void
2776 usage_add(void)
2778 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2779 exit(1);
2782 static const struct got_error *
2783 cmd_add(int argc, char *argv[])
2785 const struct got_error *error = NULL;
2786 struct got_repository *repo = NULL;
2787 struct got_worktree *worktree = NULL;
2788 char *cwd = NULL;
2789 struct got_pathlist_head paths;
2790 struct got_pathlist_entry *pe;
2791 int ch, x;
2793 TAILQ_INIT(&paths);
2795 while ((ch = getopt(argc, argv, "")) != -1) {
2796 switch (ch) {
2797 default:
2798 usage_add();
2799 /* NOTREACHED */
2803 argc -= optind;
2804 argv += optind;
2806 #ifndef PROFILE
2807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2808 NULL) == -1)
2809 err(1, "pledge");
2810 #endif
2811 if (argc < 1)
2812 usage_add();
2814 /* make sure each file exists before doing anything halfway */
2815 for (x = 0; x < argc; x++) {
2816 char *path = realpath(argv[x], NULL);
2817 if (path == NULL) {
2818 error = got_error_from_errno2("realpath", argv[x]);
2819 goto done;
2821 free(path);
2824 cwd = getcwd(NULL, 0);
2825 if (cwd == NULL) {
2826 error = got_error_from_errno("getcwd");
2827 goto done;
2830 error = got_worktree_open(&worktree, cwd);
2831 if (error)
2832 goto done;
2834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2835 if (error != NULL)
2836 goto done;
2838 error = apply_unveil(got_repo_get_path(repo), 1,
2839 got_worktree_get_root_path(worktree), 0);
2840 if (error)
2841 goto done;
2843 for (x = 0; x < argc; x++) {
2844 char *path = realpath(argv[x], NULL);
2845 if (path == NULL) {
2846 error = got_error_from_errno2("realpath", argv[x]);
2847 goto done;
2850 got_path_strip_trailing_slashes(path);
2851 error = got_pathlist_insert(&pe, &paths, path, NULL);
2852 if (error) {
2853 free(path);
2854 goto done;
2857 error = got_worktree_schedule_add(worktree, &paths, print_status,
2858 NULL, repo);
2859 done:
2860 if (repo)
2861 got_repo_close(repo);
2862 if (worktree)
2863 got_worktree_close(worktree);
2864 TAILQ_FOREACH(pe, &paths, entry)
2865 free((char *)pe->path);
2866 got_pathlist_free(&paths);
2867 free(cwd);
2868 return error;
2871 __dead static void
2872 usage_remove(void)
2874 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2875 exit(1);
2878 static const struct got_error *
2879 cmd_remove(int argc, char *argv[])
2881 const struct got_error *error = NULL;
2882 struct got_worktree *worktree = NULL;
2883 struct got_repository *repo = NULL;
2884 char *cwd = NULL;
2885 struct got_pathlist_head paths;
2886 struct got_pathlist_entry *pe;
2887 int ch, i, delete_local_mods = 0;
2889 TAILQ_INIT(&paths);
2891 while ((ch = getopt(argc, argv, "f")) != -1) {
2892 switch (ch) {
2893 case 'f':
2894 delete_local_mods = 1;
2895 break;
2896 default:
2897 usage_add();
2898 /* NOTREACHED */
2902 argc -= optind;
2903 argv += optind;
2905 #ifndef PROFILE
2906 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2907 NULL) == -1)
2908 err(1, "pledge");
2909 #endif
2910 if (argc < 1)
2911 usage_remove();
2913 /* make sure each file exists before doing anything halfway */
2914 for (i = 0; i < argc; i++) {
2915 char *path = realpath(argv[i], NULL);
2916 if (path == NULL) {
2917 error = got_error_from_errno2("realpath", argv[i]);
2918 goto done;
2920 free(path);
2923 cwd = getcwd(NULL, 0);
2924 if (cwd == NULL) {
2925 error = got_error_from_errno("getcwd");
2926 goto done;
2928 error = got_worktree_open(&worktree, cwd);
2929 if (error)
2930 goto done;
2932 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2933 if (error)
2934 goto done;
2936 error = apply_unveil(got_repo_get_path(repo), 1,
2937 got_worktree_get_root_path(worktree), 0);
2938 if (error)
2939 goto done;
2941 for (i = 0; i < argc; i++) {
2942 char *path = realpath(argv[i], NULL);
2943 if (path == NULL) {
2944 error = got_error_from_errno2("realpath", argv[i]);
2945 goto done;
2948 got_path_strip_trailing_slashes(path);
2949 error = got_pathlist_insert(&pe, &paths, path, NULL);
2950 if (error) {
2951 free(path);
2952 goto done;
2955 error = got_worktree_schedule_delete(worktree, &paths,
2956 delete_local_mods, print_status, NULL, repo);
2957 if (error)
2958 goto done;
2959 done:
2960 if (repo)
2961 got_repo_close(repo);
2962 if (worktree)
2963 got_worktree_close(worktree);
2964 TAILQ_FOREACH(pe, &paths, entry)
2965 free((char *)pe->path);
2966 got_pathlist_free(&paths);
2967 free(cwd);
2968 return error;
2971 __dead static void
2972 usage_revert(void)
2974 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2975 exit(1);
2978 static const struct got_error *
2979 revert_progress(void *arg, unsigned char status, const char *path)
2981 while (path[0] == '/')
2982 path++;
2983 printf("%c %s\n", status, path);
2984 return NULL;
2987 static const struct got_error *
2988 cmd_revert(int argc, char *argv[])
2990 const struct got_error *error = NULL;
2991 struct got_worktree *worktree = NULL;
2992 struct got_repository *repo = NULL;
2993 char *cwd = NULL, *path = NULL;
2994 struct got_pathlist_head paths;
2995 struct got_pathlist_entry *pe;
2996 int ch, i;
2998 TAILQ_INIT(&paths);
3000 while ((ch = getopt(argc, argv, "")) != -1) {
3001 switch (ch) {
3002 default:
3003 usage_revert();
3004 /* NOTREACHED */
3008 argc -= optind;
3009 argv += optind;
3011 #ifndef PROFILE
3012 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3013 "unveil", NULL) == -1)
3014 err(1, "pledge");
3015 #endif
3016 if (argc < 1)
3017 usage_revert();
3019 for (i = 0; i < argc; i++) {
3020 char *path = realpath(argv[i], NULL);
3021 if (path == NULL) {
3022 error = got_error_from_errno2("realpath", argv[i]);
3023 goto done;
3026 got_path_strip_trailing_slashes(path);
3027 error = got_pathlist_insert(&pe, &paths, path, NULL);
3028 if (error) {
3029 free(path);
3030 goto done;
3034 cwd = getcwd(NULL, 0);
3035 if (cwd == NULL) {
3036 error = got_error_from_errno("getcwd");
3037 goto done;
3039 error = got_worktree_open(&worktree, cwd);
3040 if (error)
3041 goto done;
3043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3044 if (error != NULL)
3045 goto done;
3047 error = apply_unveil(got_repo_get_path(repo), 1,
3048 got_worktree_get_root_path(worktree), 0);
3049 if (error)
3050 goto done;
3052 error = got_worktree_revert(worktree, &paths,
3053 revert_progress, NULL, repo);
3054 if (error)
3055 goto done;
3056 done:
3057 if (repo)
3058 got_repo_close(repo);
3059 if (worktree)
3060 got_worktree_close(worktree);
3061 free(path);
3062 free(cwd);
3063 return error;
3066 __dead static void
3067 usage_commit(void)
3069 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3070 exit(1);
3073 struct collect_commit_logmsg_arg {
3074 const char *cmdline_log;
3075 const char *editor;
3076 const char *worktree_path;
3077 const char *branch_name;
3078 const char *repo_path;
3079 char *logmsg_path;
3083 static const struct got_error *
3084 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3085 void *arg)
3087 char *initial_content = NULL;
3088 struct got_pathlist_entry *pe;
3089 const struct got_error *err = NULL;
3090 char *template = NULL;
3091 struct collect_commit_logmsg_arg *a = arg;
3092 int fd;
3093 size_t len;
3095 /* if a message was specified on the command line, just use it */
3096 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3097 len = strlen(a->cmdline_log) + 1;
3098 *logmsg = malloc(len + 1);
3099 if (*logmsg == NULL)
3100 return got_error_from_errno("malloc");
3101 strlcpy(*logmsg, a->cmdline_log, len);
3102 return NULL;
3105 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3106 return got_error_from_errno("asprintf");
3108 if (asprintf(&initial_content,
3109 "\n# changes to be committed on branch %s:\n",
3110 a->branch_name) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3114 if (err)
3115 goto done;
3117 dprintf(fd, initial_content);
3119 TAILQ_FOREACH(pe, commitable_paths, entry) {
3120 struct got_commitable *ct = pe->data;
3121 dprintf(fd, "# %c %s\n",
3122 got_commitable_get_status(ct),
3123 got_commitable_get_path(ct));
3125 close(fd);
3127 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3128 done:
3129 unlink(a->logmsg_path);
3130 free(a->logmsg_path);
3131 free(initial_content);
3132 free(template);
3134 /* Editor is done; we can now apply unveil(2) */
3135 if (err == NULL) {
3136 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3137 if (err) {
3138 free(*logmsg);
3139 *logmsg = NULL;
3142 return err;
3145 static const struct got_error *
3146 cmd_commit(int argc, char *argv[])
3148 const struct got_error *error = NULL;
3149 struct got_worktree *worktree = NULL;
3150 struct got_repository *repo = NULL;
3151 char *cwd = NULL, *path = NULL, *id_str = NULL;
3152 struct got_object_id *id = NULL;
3153 const char *logmsg = NULL;
3154 const char *got_author = getenv("GOT_AUTHOR");
3155 struct collect_commit_logmsg_arg cl_arg;
3156 char *editor = NULL;
3157 int ch, rebase_in_progress;
3159 while ((ch = getopt(argc, argv, "m:")) != -1) {
3160 switch (ch) {
3161 case 'm':
3162 logmsg = optarg;
3163 break;
3164 default:
3165 usage_commit();
3166 /* NOTREACHED */
3170 argc -= optind;
3171 argv += optind;
3173 #ifndef PROFILE
3174 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3175 "unveil", NULL) == -1)
3176 err(1, "pledge");
3177 #endif
3178 if (argc == 1) {
3179 path = realpath(argv[0], NULL);
3180 if (path == NULL) {
3181 error = got_error_from_errno2("realpath", argv[0]);
3182 goto done;
3184 got_path_strip_trailing_slashes(path);
3185 } else if (argc != 0)
3186 usage_commit();
3188 if (got_author == NULL) {
3189 /* TODO: Look current user up in password database */
3190 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3191 goto done;
3194 cwd = getcwd(NULL, 0);
3195 if (cwd == NULL) {
3196 error = got_error_from_errno("getcwd");
3197 goto done;
3199 error = got_worktree_open(&worktree, cwd);
3200 if (error)
3201 goto done;
3203 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3204 if (error)
3205 goto done;
3206 if (rebase_in_progress) {
3207 error = got_error(GOT_ERR_REBASING);
3208 goto done;
3211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3212 if (error != NULL)
3213 goto done;
3216 * unveil(2) traverses exec(2); if an editor is used we have
3217 * to apply unveil after the log message has been written.
3219 if (logmsg == NULL || strlen(logmsg) == 0)
3220 error = get_editor(&editor);
3221 else
3222 error = apply_unveil(got_repo_get_path(repo), 0,
3223 got_worktree_get_root_path(worktree), 0);
3224 if (error)
3225 goto done;
3227 cl_arg.editor = editor;
3228 cl_arg.cmdline_log = logmsg;
3229 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3230 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3231 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3232 cl_arg.branch_name += 5;
3233 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3234 cl_arg.branch_name += 6;
3235 cl_arg.repo_path = got_repo_get_path(repo);
3236 cl_arg.logmsg_path = NULL;
3237 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3238 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3239 if (error) {
3240 if (cl_arg.logmsg_path)
3241 fprintf(stderr, "%s: log message preserved in %s\n",
3242 getprogname(), cl_arg.logmsg_path);
3243 goto done;
3246 if (cl_arg.logmsg_path)
3247 unlink(cl_arg.logmsg_path);
3249 error = got_object_id_str(&id_str, id);
3250 if (error)
3251 goto done;
3252 printf("Created commit %s\n", id_str);
3253 done:
3254 if (repo)
3255 got_repo_close(repo);
3256 if (worktree)
3257 got_worktree_close(worktree);
3258 free(path);
3259 free(cwd);
3260 free(id_str);
3261 free(editor);
3262 return error;
3265 __dead static void
3266 usage_cherrypick(void)
3268 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3269 exit(1);
3272 static const struct got_error *
3273 cmd_cherrypick(int argc, char *argv[])
3275 const struct got_error *error = NULL;
3276 struct got_worktree *worktree = NULL;
3277 struct got_repository *repo = NULL;
3278 char *cwd = NULL, *commit_id_str = NULL;
3279 struct got_object_id *commit_id = NULL;
3280 struct got_commit_object *commit = NULL;
3281 struct got_object_qid *pid;
3282 struct got_reference *head_ref = NULL;
3283 int ch, did_something = 0;
3285 while ((ch = getopt(argc, argv, "")) != -1) {
3286 switch (ch) {
3287 default:
3288 usage_cherrypick();
3289 /* NOTREACHED */
3293 argc -= optind;
3294 argv += optind;
3296 #ifndef PROFILE
3297 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3298 "unveil", NULL) == -1)
3299 err(1, "pledge");
3300 #endif
3301 if (argc != 1)
3302 usage_cherrypick();
3304 cwd = getcwd(NULL, 0);
3305 if (cwd == NULL) {
3306 error = got_error_from_errno("getcwd");
3307 goto done;
3309 error = got_worktree_open(&worktree, cwd);
3310 if (error)
3311 goto done;
3313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3314 if (error != NULL)
3315 goto done;
3317 error = apply_unveil(got_repo_get_path(repo), 0,
3318 got_worktree_get_root_path(worktree), 0);
3319 if (error)
3320 goto done;
3322 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3323 GOT_OBJ_TYPE_COMMIT, repo);
3324 if (error != NULL) {
3325 struct got_reference *ref;
3326 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3327 goto done;
3328 error = got_ref_open(&ref, repo, argv[0], 0);
3329 if (error != NULL)
3330 goto done;
3331 error = got_ref_resolve(&commit_id, repo, ref);
3332 got_ref_close(ref);
3333 if (error != NULL)
3334 goto done;
3336 error = got_object_id_str(&commit_id_str, commit_id);
3337 if (error)
3338 goto done;
3340 error = got_ref_open(&head_ref, repo,
3341 got_worktree_get_head_ref_name(worktree), 0);
3342 if (error != NULL)
3343 goto done;
3345 error = check_same_branch(commit_id, head_ref, repo);
3346 if (error) {
3347 if (error->code != GOT_ERR_ANCESTRY)
3348 goto done;
3349 error = NULL;
3350 } else {
3351 error = got_error(GOT_ERR_SAME_BRANCH);
3352 goto done;
3355 error = got_object_open_as_commit(&commit, repo, commit_id);
3356 if (error)
3357 goto done;
3358 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3359 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3360 commit_id, repo, update_progress, &did_something, check_cancelled,
3361 NULL);
3362 if (error != NULL)
3363 goto done;
3365 if (did_something)
3366 printf("Merged commit %s\n", commit_id_str);
3367 done:
3368 if (commit)
3369 got_object_commit_close(commit);
3370 free(commit_id_str);
3371 if (head_ref)
3372 got_ref_close(head_ref);
3373 if (worktree)
3374 got_worktree_close(worktree);
3375 if (repo)
3376 got_repo_close(repo);
3377 return error;
3380 __dead static void
3381 usage_backout(void)
3383 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3384 exit(1);
3387 static const struct got_error *
3388 cmd_backout(int argc, char *argv[])
3390 const struct got_error *error = NULL;
3391 struct got_worktree *worktree = NULL;
3392 struct got_repository *repo = NULL;
3393 char *cwd = NULL, *commit_id_str = NULL;
3394 struct got_object_id *commit_id = NULL;
3395 struct got_commit_object *commit = NULL;
3396 struct got_object_qid *pid;
3397 struct got_reference *head_ref = NULL;
3398 int ch, did_something = 0;
3400 while ((ch = getopt(argc, argv, "")) != -1) {
3401 switch (ch) {
3402 default:
3403 usage_backout();
3404 /* NOTREACHED */
3408 argc -= optind;
3409 argv += optind;
3411 #ifndef PROFILE
3412 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3413 "unveil", NULL) == -1)
3414 err(1, "pledge");
3415 #endif
3416 if (argc != 1)
3417 usage_backout();
3419 cwd = getcwd(NULL, 0);
3420 if (cwd == NULL) {
3421 error = got_error_from_errno("getcwd");
3422 goto done;
3424 error = got_worktree_open(&worktree, cwd);
3425 if (error)
3426 goto done;
3428 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3429 if (error != NULL)
3430 goto done;
3432 error = apply_unveil(got_repo_get_path(repo), 0,
3433 got_worktree_get_root_path(worktree), 0);
3434 if (error)
3435 goto done;
3437 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3438 GOT_OBJ_TYPE_COMMIT, repo);
3439 if (error != NULL) {
3440 struct got_reference *ref;
3441 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3442 goto done;
3443 error = got_ref_open(&ref, repo, argv[0], 0);
3444 if (error != NULL)
3445 goto done;
3446 error = got_ref_resolve(&commit_id, repo, ref);
3447 got_ref_close(ref);
3448 if (error != NULL)
3449 goto done;
3451 error = got_object_id_str(&commit_id_str, commit_id);
3452 if (error)
3453 goto done;
3455 error = got_ref_open(&head_ref, repo,
3456 got_worktree_get_head_ref_name(worktree), 0);
3457 if (error != NULL)
3458 goto done;
3460 error = check_same_branch(commit_id, head_ref, repo);
3461 if (error)
3462 goto done;
3464 error = got_object_open_as_commit(&commit, repo, commit_id);
3465 if (error)
3466 goto done;
3467 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3468 if (pid == NULL) {
3469 error = got_error(GOT_ERR_ROOT_COMMIT);
3470 goto done;
3473 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3474 update_progress, &did_something, check_cancelled, NULL);
3475 if (error != NULL)
3476 goto done;
3478 if (did_something)
3479 printf("Backed out commit %s\n", commit_id_str);
3480 done:
3481 if (commit)
3482 got_object_commit_close(commit);
3483 free(commit_id_str);
3484 if (head_ref)
3485 got_ref_close(head_ref);
3486 if (worktree)
3487 got_worktree_close(worktree);
3488 if (repo)
3489 got_repo_close(repo);
3490 return error;
3493 __dead static void
3494 usage_rebase(void)
3496 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3497 getprogname());
3498 exit(1);
3501 static const struct got_error *
3502 show_rebase_progress(struct got_commit_object *commit,
3503 struct got_object_id *old_id, struct got_object_id *new_id)
3505 const struct got_error *err;
3506 char *old_id_str = NULL, *new_id_str = NULL;
3507 char *logmsg0 = NULL, *logmsg, *nl;
3508 size_t len;
3510 err = got_object_id_str(&old_id_str, old_id);
3511 if (err)
3512 goto done;
3514 if (new_id) {
3515 err = got_object_id_str(&new_id_str, new_id);
3516 if (err)
3517 goto done;
3520 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3521 if (logmsg0 == NULL) {
3522 err = got_error_from_errno("strdup");
3523 goto done;
3525 logmsg = logmsg0;
3527 while (isspace((unsigned char)logmsg[0]))
3528 logmsg++;
3530 old_id_str[12] = '\0';
3531 if (new_id_str)
3532 new_id_str[12] = '\0';
3533 len = strlen(logmsg);
3534 if (len > 42)
3535 len = 42;
3536 logmsg[len] = '\0';
3537 nl = strchr(logmsg, '\n');
3538 if (nl)
3539 *nl = '\0';
3540 printf("%s -> %s: %s\n", old_id_str,
3541 new_id_str ? new_id_str : "no-op change", logmsg);
3542 done:
3543 free(old_id_str);
3544 free(new_id_str);
3545 free(logmsg0);
3546 return err;
3549 static const struct got_error *
3550 rebase_progress(void *arg, unsigned char status, const char *path)
3552 unsigned char *rebase_status = arg;
3554 while (path[0] == '/')
3555 path++;
3556 printf("%c %s\n", status, path);
3558 if (*rebase_status == GOT_STATUS_CONFLICT)
3559 return NULL;
3560 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3561 *rebase_status = status;
3562 return NULL;
3565 static const struct got_error *
3566 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3567 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3568 struct got_repository *repo)
3570 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3571 return got_worktree_rebase_complete(worktree,
3572 new_base_branch, tmp_branch, branch, repo);
3575 static const struct got_error *
3576 rebase_commit(struct got_pathlist_head *merged_paths,
3577 struct got_worktree *worktree, struct got_reference *tmp_branch,
3578 struct got_object_id *commit_id, struct got_repository *repo)
3580 const struct got_error *error;
3581 struct got_commit_object *commit;
3582 struct got_object_id *new_commit_id;
3584 error = got_object_open_as_commit(&commit, repo, commit_id);
3585 if (error)
3586 return error;
3588 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3589 worktree, tmp_branch, commit, commit_id, repo);
3590 if (error) {
3591 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3592 goto done;
3593 error = show_rebase_progress(commit, commit_id, NULL);
3594 } else {
3595 error = show_rebase_progress(commit, commit_id, new_commit_id);
3596 free(new_commit_id);
3598 done:
3599 got_object_commit_close(commit);
3600 return error;
3603 struct check_path_prefix_arg {
3604 const char *path_prefix;
3605 size_t len;
3608 static const struct got_error *
3609 check_path_prefix(void *arg, struct got_blob_object *blob1,
3610 struct got_blob_object *blob2, struct got_object_id *id1,
3611 struct got_object_id *id2, const char *path1, const char *path2,
3612 struct got_repository *repo)
3614 struct check_path_prefix_arg *a = arg;
3616 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3617 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3618 return got_error(GOT_ERR_REBASE_PATH);
3620 return NULL;
3623 static const struct got_error *
3624 rebase_check_path_prefix(struct got_object_id *parent_id,
3625 struct got_object_id *commit_id, const char *path_prefix,
3626 struct got_repository *repo)
3628 const struct got_error *err;
3629 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3630 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3631 struct check_path_prefix_arg cpp_arg;
3633 if (got_path_is_root_dir(path_prefix))
3634 return NULL;
3636 err = got_object_open_as_commit(&commit, repo, commit_id);
3637 if (err)
3638 goto done;
3640 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3641 if (err)
3642 goto done;
3644 err = got_object_open_as_tree(&tree1, repo,
3645 got_object_commit_get_tree_id(parent_commit));
3646 if (err)
3647 goto done;
3649 err = got_object_open_as_tree(&tree2, repo,
3650 got_object_commit_get_tree_id(commit));
3651 if (err)
3652 goto done;
3654 cpp_arg.path_prefix = path_prefix;
3655 cpp_arg.len = strlen(path_prefix);
3656 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3657 &cpp_arg);
3658 done:
3659 if (tree1)
3660 got_object_tree_close(tree1);
3661 if (tree2)
3662 got_object_tree_close(tree2);
3663 if (commit)
3664 got_object_commit_close(commit);
3665 if (parent_commit)
3666 got_object_commit_close(parent_commit);
3667 return err;
3670 static const struct got_error *
3671 cmd_rebase(int argc, char *argv[])
3673 const struct got_error *error = NULL;
3674 struct got_worktree *worktree = NULL;
3675 struct got_repository *repo = NULL;
3676 char *cwd = NULL;
3677 struct got_reference *branch = NULL;
3678 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3679 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3680 struct got_object_id *resume_commit_id = NULL;
3681 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3682 struct got_commit_graph *graph = NULL;
3683 struct got_commit_object *commit = NULL;
3684 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3685 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3686 struct got_object_id_queue commits;
3687 struct got_pathlist_head merged_paths;
3688 const struct got_object_id_queue *parent_ids;
3689 struct got_object_qid *qid, *pid;
3691 SIMPLEQ_INIT(&commits);
3692 TAILQ_INIT(&merged_paths);
3694 while ((ch = getopt(argc, argv, "ac")) != -1) {
3695 switch (ch) {
3696 case 'a':
3697 abort_rebase = 1;
3698 break;
3699 case 'c':
3700 continue_rebase = 1;
3701 break;
3702 default:
3703 usage_rebase();
3704 /* NOTREACHED */
3708 argc -= optind;
3709 argv += optind;
3711 #ifndef PROFILE
3712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3713 "unveil", NULL) == -1)
3714 err(1, "pledge");
3715 #endif
3716 if (abort_rebase && continue_rebase)
3717 usage_rebase();
3718 else if (abort_rebase || continue_rebase) {
3719 if (argc != 0)
3720 usage_rebase();
3721 } else if (argc != 1)
3722 usage_rebase();
3724 cwd = getcwd(NULL, 0);
3725 if (cwd == NULL) {
3726 error = got_error_from_errno("getcwd");
3727 goto done;
3729 error = got_worktree_open(&worktree, cwd);
3730 if (error)
3731 goto done;
3733 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3734 if (error != NULL)
3735 goto done;
3737 error = apply_unveil(got_repo_get_path(repo), 0,
3738 got_worktree_get_root_path(worktree), 0);
3739 if (error)
3740 goto done;
3742 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3743 if (error)
3744 goto done;
3746 if (rebase_in_progress && abort_rebase) {
3747 int did_something;
3748 error = got_worktree_rebase_continue(&resume_commit_id,
3749 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3750 if (error)
3751 goto done;
3752 printf("Switching work tree to %s\n",
3753 got_ref_get_symref_target(new_base_branch));
3754 error = got_worktree_rebase_abort(worktree, repo,
3755 new_base_branch, update_progress, &did_something);
3756 if (error)
3757 goto done;
3758 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3759 goto done; /* nothing else to do */
3760 } else if (abort_rebase) {
3761 error = got_error(GOT_ERR_NOT_REBASING);
3762 goto done;
3765 if (continue_rebase) {
3766 error = got_worktree_rebase_continue(&resume_commit_id,
3767 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3768 if (error)
3769 goto done;
3771 error = rebase_commit(NULL, worktree, tmp_branch,
3772 resume_commit_id, repo);
3773 if (error)
3774 goto done;
3776 yca_id = got_object_id_dup(resume_commit_id);
3777 if (yca_id == NULL) {
3778 error = got_error_from_errno("got_object_id_dup");
3779 goto done;
3781 } else {
3782 error = got_ref_open(&branch, repo, argv[0], 0);
3783 if (error != NULL)
3784 goto done;
3786 error = check_same_branch(
3787 got_worktree_get_base_commit_id(worktree), branch, repo);
3788 if (error) {
3789 if (error->code != GOT_ERR_ANCESTRY)
3790 goto done;
3791 error = NULL;
3792 } else {
3793 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3794 "specified branch resolves to a commit which "
3795 "is already contained in work tree's branch");
3796 goto done;
3800 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3801 if (error)
3802 goto done;
3804 if (!continue_rebase) {
3805 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3806 got_worktree_get_base_commit_id(worktree),
3807 branch_head_commit_id, repo);
3808 if (error)
3809 goto done;
3810 if (yca_id == NULL) {
3811 error = got_error_msg(GOT_ERR_ANCESTRY,
3812 "specified branch shares no common ancestry "
3813 "with work tree's branch");
3814 goto done;
3817 error = got_worktree_rebase_prepare(&new_base_branch,
3818 &tmp_branch, worktree, branch, repo);
3819 if (error)
3820 goto done;
3823 commit_id = branch_head_commit_id;
3824 error = got_object_open_as_commit(&commit, repo, commit_id);
3825 if (error)
3826 goto done;
3828 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3829 if (error)
3830 goto done;
3831 parent_ids = got_object_commit_get_parent_ids(commit);
3832 pid = SIMPLEQ_FIRST(parent_ids);
3833 error = got_commit_graph_iter_start(graph, pid->id, repo);
3834 got_object_commit_close(commit);
3835 commit = NULL;
3836 if (error)
3837 goto done;
3838 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3839 error = got_commit_graph_iter_next(&parent_id, graph);
3840 if (error) {
3841 if (error->code == GOT_ERR_ITER_COMPLETED) {
3842 error = got_error_msg(GOT_ERR_ANCESTRY,
3843 "ran out of commits to rebase before "
3844 "youngest common ancestor commit has "
3845 "been reached?!?");
3846 goto done;
3847 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3848 goto done;
3849 error = got_commit_graph_fetch_commits(graph, 1, repo);
3850 if (error)
3851 goto done;
3852 } else {
3853 error = rebase_check_path_prefix(parent_id, commit_id,
3854 got_worktree_get_path_prefix(worktree), repo);
3855 if (error)
3856 goto done;
3858 error = got_object_qid_alloc(&qid, commit_id);
3859 if (error)
3860 goto done;
3861 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3862 commit_id = parent_id;
3866 if (SIMPLEQ_EMPTY(&commits)) {
3867 if (continue_rebase)
3868 error = rebase_complete(worktree, branch,
3869 new_base_branch, tmp_branch, repo);
3870 else
3871 error = got_error(GOT_ERR_EMPTY_REBASE);
3872 goto done;
3875 pid = NULL;
3876 SIMPLEQ_FOREACH(qid, &commits, entry) {
3877 commit_id = qid->id;
3878 parent_id = pid ? pid->id : yca_id;
3879 pid = qid;
3881 error = got_worktree_rebase_merge_files(&merged_paths,
3882 worktree, parent_id, commit_id, repo, rebase_progress,
3883 &rebase_status, check_cancelled, NULL);
3884 if (error)
3885 goto done;
3887 if (rebase_status == GOT_STATUS_CONFLICT) {
3888 got_worktree_rebase_pathlist_free(&merged_paths);
3889 break;
3892 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3893 commit_id, repo);
3894 got_worktree_rebase_pathlist_free(&merged_paths);
3895 if (error)
3896 goto done;
3899 if (rebase_status == GOT_STATUS_CONFLICT) {
3900 error = got_worktree_rebase_postpone(worktree);
3901 if (error)
3902 goto done;
3903 error = got_error_msg(GOT_ERR_CONFLICTS,
3904 "conflicts must be resolved before rebasing can continue");
3905 } else
3906 error = rebase_complete(worktree, branch, new_base_branch,
3907 tmp_branch, repo);
3908 done:
3909 got_object_id_queue_free(&commits);
3910 free(branch_head_commit_id);
3911 free(resume_commit_id);
3912 free(yca_id);
3913 if (graph)
3914 got_commit_graph_close(graph);
3915 if (commit)
3916 got_object_commit_close(commit);
3917 if (branch)
3918 got_ref_close(branch);
3919 if (new_base_branch)
3920 got_ref_close(new_base_branch);
3921 if (tmp_branch)
3922 got_ref_close(tmp_branch);
3923 if (worktree)
3924 got_worktree_close(worktree);
3925 if (repo)
3926 got_repo_close(repo);
3927 return error;