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 #ifdef PROFILE
248 if (unveil("gmon.out", "rwc") != 0)
249 return got_error_from_errno2("unveil", "gmon.out");
250 #endif
251 if (create_worktree) {
252 /* Pre-create work tree path to avoid unveiling its parents. */
253 err = got_path_mkdir(worktree_path);
255 if (errno == EEXIST) {
256 if (got_path_dir_is_empty(worktree_path)) {
257 errno = 0;
258 err = NULL;
259 } else {
260 err = got_error_path(worktree_path,
261 GOT_ERR_DIR_NOT_EMPTY);
265 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
266 return err;
269 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
270 return got_error_from_errno2("unveil", repo_path);
272 if (worktree_path && unveil(worktree_path, "rwc") != 0)
273 return got_error_from_errno2("unveil", worktree_path);
275 if (unveil("/tmp", "rwc") != 0)
276 return got_error_from_errno2("unveil", "/tmp");
278 err = got_privsep_unveil_exec_helpers();
279 if (err != NULL)
280 return err;
282 if (unveil(NULL, NULL) != 0)
283 return got_error_from_errno("unveil");
285 return NULL;
288 __dead static void
289 usage_init(void)
291 fprintf(stderr, "usage: %s init path\n", getprogname());
292 exit(1);
295 static const struct got_error *
296 cmd_init(int argc, char *argv[])
298 const struct got_error *error = NULL;
299 char *repo_path = NULL;
300 int ch;
302 while ((ch = getopt(argc, argv, "")) != -1) {
303 switch (ch) {
304 default:
305 usage_init();
306 /* NOTREACHED */
310 argc -= optind;
311 argv += optind;
313 #ifndef PROFILE
314 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
315 err(1, "pledge");
316 #endif
317 if (argc != 1)
318 usage_init();
320 repo_path = strdup(argv[0]);
321 if (repo_path == NULL)
322 return got_error_from_errno("strdup");
324 got_path_strip_trailing_slashes(repo_path);
326 error = got_path_mkdir(repo_path);
327 if (error &&
328 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 goto done;
331 error = apply_unveil(repo_path, 0, NULL, 0);
332 if (error)
333 goto done;
335 error = got_repo_init(repo_path);
336 if (error != NULL)
337 goto done;
339 done:
340 free(repo_path);
341 return error;
344 __dead static void
345 usage_import(void)
347 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
348 "[-r repository-path] [-I pattern] path\n", getprogname());
349 exit(1);
352 int
353 spawn_editor(const char *editor, const char *file)
355 pid_t pid;
356 sig_t sighup, sigint, sigquit;
357 int st = -1;
359 sighup = signal(SIGHUP, SIG_IGN);
360 sigint = signal(SIGINT, SIG_IGN);
361 sigquit = signal(SIGQUIT, SIG_IGN);
363 switch (pid = fork()) {
364 case -1:
365 goto doneediting;
366 case 0:
367 execl(editor, editor, file, (char *)NULL);
368 _exit(127);
371 while (waitpid(pid, &st, 0) == -1)
372 if (errno != EINTR)
373 break;
375 doneediting:
376 (void)signal(SIGHUP, sighup);
377 (void)signal(SIGINT, sigint);
378 (void)signal(SIGQUIT, sigquit);
380 if (!WIFEXITED(st)) {
381 errno = EINTR;
382 return -1;
385 return WEXITSTATUS(st);
388 static const struct got_error *
389 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
390 const char *initial_content)
392 const struct got_error *err = NULL;
393 char buf[1024];
394 struct stat st, st2;
395 FILE *fp;
396 int content_changed = 0;
397 size_t len;
399 *logmsg = NULL;
401 if (stat(logmsg_path, &st) == -1)
402 return got_error_from_errno2("stat", logmsg_path);
404 if (spawn_editor(editor, logmsg_path) == -1)
405 return got_error_from_errno("failed spawning editor");
407 if (stat(logmsg_path, &st2) == -1)
408 return got_error_from_errno("stat");
410 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
411 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
412 "no changes made to commit message, aborting");
414 *logmsg = malloc(st2.st_size + 1);
415 if (*logmsg == NULL)
416 return got_error_from_errno("malloc");
417 (*logmsg)[0] = '\0';
418 len = 0;
420 fp = fopen(logmsg_path, "r");
421 if (fp == NULL) {
422 err = got_error_from_errno("fopen");
423 goto done;
425 while (fgets(buf, sizeof(buf), fp) != NULL) {
426 if (!content_changed && strcmp(buf, initial_content) != 0)
427 content_changed = 1;
428 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
429 continue; /* remove comments and leading empty lines */
430 len = strlcat(*logmsg, buf, st2.st_size);
432 fclose(fp);
434 while (len > 0 && (*logmsg)[len - 1] == '\n') {
435 (*logmsg)[len - 1] = '\0';
436 len--;
439 if (len == 0 || !content_changed)
440 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 "commit message cannot be empty, aborting");
442 done:
443 if (err) {
444 free(*logmsg);
445 *logmsg = NULL;
447 return err;
450 static const struct got_error *
451 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
452 const char *branch_name)
454 char *initial_content = NULL, *logmsg_path = NULL;
455 const struct got_error *err = NULL;
456 int fd;
458 if (asprintf(&initial_content,
459 "\n# %s to be imported to branch %s\n", path_dir,
460 branch_name) == -1)
461 return got_error_from_errno("asprintf");
463 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
464 if (err)
465 goto done;
467 dprintf(fd, initial_content);
468 close(fd);
470 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
471 done:
472 free(initial_content);
473 free(logmsg_path);
474 return err;
477 static const struct got_error *
478 import_progress(void *arg, const char *path)
480 printf("A %s\n", path);
481 return NULL;
484 static const struct got_error *
485 cmd_import(int argc, char *argv[])
487 const struct got_error *error = NULL;
488 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
489 char *editor = NULL;
490 const char *got_author = getenv("GOT_AUTHOR");
491 const char *branch_name = "master";
492 char *refname = NULL, *id_str = NULL;
493 struct got_repository *repo = NULL;
494 struct got_reference *branch_ref = NULL, *head_ref = NULL;
495 struct got_object_id *new_commit_id = NULL;
496 int ch;
497 struct got_pathlist_head ignores;
498 struct got_pathlist_entry *pe;
500 TAILQ_INIT(&ignores);
502 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
503 switch (ch) {
504 case 'b':
505 branch_name = optarg;
506 break;
507 case 'm':
508 logmsg = strdup(optarg);
509 if (logmsg == NULL) {
510 error = got_error_from_errno("strdup");
511 goto done;
513 break;
514 case 'r':
515 repo_path = realpath(optarg, NULL);
516 if (repo_path == NULL) {
517 error = got_error_from_errno("realpath");
518 goto done;
520 break;
521 case 'I':
522 if (optarg[0] == '\0')
523 break;
524 error = got_pathlist_insert(&pe, &ignores, optarg,
525 NULL);
526 if (error)
527 goto done;
528 break;
529 default:
530 usage_init();
531 /* NOTREACHED */
535 argc -= optind;
536 argv += optind;
538 #ifndef PROFILE
539 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
540 NULL) == -1)
541 err(1, "pledge");
542 #endif
543 if (argc != 1)
544 usage_import();
546 if (got_author == NULL) {
547 /* TODO: Look current user up in password database */
548 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
549 goto done;
552 if (repo_path == NULL) {
553 repo_path = getcwd(NULL, 0);
554 if (repo_path == NULL)
555 return got_error_from_errno("getcwd");
557 got_path_strip_trailing_slashes(repo_path);
558 error = got_repo_open(&repo, repo_path);
559 if (error)
560 goto done;
562 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
563 error = got_error_from_errno("asprintf");
564 goto done;
567 error = got_ref_open(&branch_ref, repo, refname, 0);
568 if (error) {
569 if (error->code != GOT_ERR_NOT_REF)
570 goto done;
571 } else {
572 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
573 "import target branch already exists");
574 goto done;
577 path_dir = realpath(argv[0], NULL);
578 if (path_dir == NULL) {
579 error = got_error_from_errno("realpath");
580 goto done;
582 got_path_strip_trailing_slashes(path_dir);
584 /*
585 * unveil(2) traverses exec(2); if an editor is used we have
586 * to apply unveil after the log message has been written.
587 */
588 if (logmsg == NULL || strlen(logmsg) == 0) {
589 error = get_editor(&editor);
590 if (error)
591 goto done;
592 error = collect_import_msg(&logmsg, editor, path_dir, refname);
593 if (error)
594 goto done;
597 if (unveil(path_dir, "r") != 0)
598 return got_error_from_errno2("unveil", path_dir);
600 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
601 if (error)
602 goto done;
604 error = got_repo_import(&new_commit_id, path_dir, logmsg,
605 got_author, &ignores, repo, import_progress, NULL);
606 if (error)
607 goto done;
609 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
610 if (error)
611 goto done;
613 error = got_ref_write(branch_ref, repo);
614 if (error)
615 goto done;
617 error = got_object_id_str(&id_str, new_commit_id);
618 if (error)
619 goto done;
621 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
622 if (error) {
623 if (error->code != GOT_ERR_NOT_REF)
624 goto done;
626 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
627 branch_ref);
628 if (error)
629 goto done;
631 error = got_ref_write(head_ref, repo);
632 if (error)
633 goto done;
636 printf("Created branch %s with commit %s\n",
637 got_ref_get_name(branch_ref), id_str);
638 done:
639 free(repo_path);
640 free(editor);
641 free(refname);
642 free(new_commit_id);
643 free(id_str);
644 if (branch_ref)
645 got_ref_close(branch_ref);
646 if (head_ref)
647 got_ref_close(head_ref);
648 return error;
651 __dead static void
652 usage_checkout(void)
654 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
655 "[-p prefix] repository-path [worktree-path]\n", getprogname());
656 exit(1);
659 static const struct got_error *
660 checkout_progress(void *arg, unsigned char status, const char *path)
662 char *worktree_path = arg;
664 /* Base commit bump happens silently. */
665 if (status == GOT_STATUS_BUMP_BASE)
666 return NULL;
668 while (path[0] == '/')
669 path++;
671 printf("%c %s/%s\n", status, worktree_path, path);
672 return NULL;
675 static const struct got_error *
676 check_cancelled(void *arg)
678 if (sigint_received || sigpipe_received)
679 return got_error(GOT_ERR_CANCELLED);
680 return NULL;
683 static const struct got_error *
684 check_linear_ancestry(struct got_object_id *commit_id,
685 struct got_object_id *base_commit_id, struct got_repository *repo)
687 const struct got_error *err = NULL;
688 struct got_object_id *yca_id;
690 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
691 commit_id, base_commit_id, repo);
692 if (err)
693 return err;
695 if (yca_id == NULL)
696 return got_error(GOT_ERR_ANCESTRY);
698 /*
699 * Require a straight line of history between the target commit
700 * and the work tree's base commit.
702 * Non-linear situations such as this require a rebase:
704 * (commit) D F (base_commit)
705 * \ /
706 * C E
707 * \ /
708 * B (yca)
709 * |
710 * A
712 * 'got update' only handles linear cases:
713 * Update forwards in time: A (base/yca) - B - C - D (commit)
714 * Update backwards in time: D (base) - C - B - A (commit/yca)
715 */
716 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
717 got_object_id_cmp(base_commit_id, yca_id) != 0)
718 return got_error(GOT_ERR_ANCESTRY);
720 free(yca_id);
721 return NULL;
724 static const struct got_error *
725 check_same_branch(struct got_object_id *commit_id,
726 struct got_reference *head_ref, struct got_repository *repo)
728 const struct got_error *err = NULL;
729 struct got_commit_graph *graph = NULL;
730 struct got_object_id *head_commit_id = NULL;
731 int is_same_branch = 0;
733 err = got_ref_resolve(&head_commit_id, repo, head_ref);
734 if (err)
735 goto done;
737 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
738 if (err)
739 goto done;
741 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
742 if (err)
743 goto done;
745 for (;;) {
746 struct got_object_id *id;
747 err = got_commit_graph_iter_next(&id, graph);
748 if (err) {
749 if (err->code == GOT_ERR_ITER_COMPLETED) {
750 err = NULL;
751 break;
752 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
753 break;
754 err = got_commit_graph_fetch_commits(graph, 1,
755 repo);
756 if (err)
757 break;
760 if (id) {
761 if (got_object_id_cmp(id, commit_id) == 0) {
762 is_same_branch = 1;
763 break;
767 done:
768 if (graph)
769 got_commit_graph_close(graph);
770 free(head_commit_id);
771 if (!err && !is_same_branch)
772 err = got_error(GOT_ERR_ANCESTRY);
773 return err;
776 static const struct got_error *
777 cmd_checkout(int argc, char *argv[])
779 const struct got_error *error = NULL;
780 struct got_repository *repo = NULL;
781 struct got_reference *head_ref = NULL;
782 struct got_worktree *worktree = NULL;
783 char *repo_path = NULL;
784 char *worktree_path = NULL;
785 const char *path_prefix = "";
786 const char *branch_name = GOT_REF_HEAD;
787 char *commit_id_str = NULL;
788 int ch, same_path_prefix;
790 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
791 switch (ch) {
792 case 'b':
793 branch_name = optarg;
794 break;
795 case 'c':
796 commit_id_str = strdup(optarg);
797 if (commit_id_str == NULL)
798 return got_error_from_errno("strdup");
799 break;
800 case 'p':
801 path_prefix = optarg;
802 break;
803 default:
804 usage_checkout();
805 /* NOTREACHED */
809 argc -= optind;
810 argv += optind;
812 #ifndef PROFILE
813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
814 "unveil", NULL) == -1)
815 err(1, "pledge");
816 #endif
817 if (argc == 1) {
818 char *cwd, *base, *dotgit;
819 repo_path = realpath(argv[0], NULL);
820 if (repo_path == NULL)
821 return got_error_from_errno2("realpath", argv[0]);
822 cwd = getcwd(NULL, 0);
823 if (cwd == NULL) {
824 error = got_error_from_errno("getcwd");
825 goto done;
827 if (path_prefix[0]) {
828 base = basename(path_prefix);
829 if (base == NULL) {
830 error = got_error_from_errno2("basename",
831 path_prefix);
832 goto done;
834 } else {
835 base = basename(repo_path);
836 if (base == NULL) {
837 error = got_error_from_errno2("basename",
838 repo_path);
839 goto done;
842 dotgit = strstr(base, ".git");
843 if (dotgit)
844 *dotgit = '\0';
845 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
846 error = got_error_from_errno("asprintf");
847 free(cwd);
848 goto done;
850 free(cwd);
851 } else if (argc == 2) {
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL) {
854 error = got_error_from_errno2("realpath", argv[0]);
855 goto done;
857 worktree_path = realpath(argv[1], NULL);
858 if (worktree_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[1]);
860 goto done;
862 } else
863 usage_checkout();
865 got_path_strip_trailing_slashes(repo_path);
866 got_path_strip_trailing_slashes(worktree_path);
868 error = got_repo_open(&repo, repo_path);
869 if (error != NULL)
870 goto done;
872 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
873 if (error)
874 goto done;
876 error = got_ref_open(&head_ref, repo, branch_name, 0);
877 if (error != NULL)
878 goto done;
880 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
881 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
882 goto done;
884 error = got_worktree_open(&worktree, worktree_path);
885 if (error != NULL)
886 goto done;
888 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
889 path_prefix);
890 if (error != NULL)
891 goto done;
892 if (!same_path_prefix) {
893 error = got_error(GOT_ERR_PATH_PREFIX);
894 goto done;
897 if (commit_id_str) {
898 struct got_object_id *commit_id;
899 error = got_repo_match_object_id_prefix(&commit_id,
900 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
901 if (error != NULL)
902 goto done;
903 error = check_linear_ancestry(commit_id,
904 got_worktree_get_base_commit_id(worktree), repo);
905 if (error != NULL) {
906 free(commit_id);
907 goto done;
909 error = check_same_branch(commit_id, head_ref, repo);
910 if (error)
911 goto done;
912 error = got_worktree_set_base_commit_id(worktree, repo,
913 commit_id);
914 free(commit_id);
915 if (error)
916 goto done;
919 error = got_worktree_checkout_files(worktree, "", repo,
920 checkout_progress, worktree_path, check_cancelled, NULL);
921 if (error != NULL)
922 goto done;
924 printf("Now shut up and hack\n");
926 done:
927 free(commit_id_str);
928 free(repo_path);
929 free(worktree_path);
930 return error;
933 __dead static void
934 usage_update(void)
936 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
937 getprogname());
938 exit(1);
941 static const struct got_error *
942 update_progress(void *arg, unsigned char status, const char *path)
944 int *did_something = arg;
946 if (status == GOT_STATUS_EXISTS)
947 return NULL;
949 *did_something = 1;
951 /* Base commit bump happens silently. */
952 if (status == GOT_STATUS_BUMP_BASE)
953 return NULL;
955 while (path[0] == '/')
956 path++;
957 printf("%c %s\n", status, path);
958 return NULL;
961 static const struct got_error *
962 switch_head_ref(struct got_reference *head_ref,
963 struct got_object_id *commit_id, struct got_worktree *worktree,
964 struct got_repository *repo)
966 const struct got_error *err = NULL;
967 char *base_id_str;
968 int ref_has_moved = 0;
970 /* Trivial case: switching between two different references. */
971 if (strcmp(got_ref_get_name(head_ref),
972 got_worktree_get_head_ref_name(worktree)) != 0) {
973 printf("Switching work tree from %s to %s\n",
974 got_worktree_get_head_ref_name(worktree),
975 got_ref_get_name(head_ref));
976 return got_worktree_set_head_ref(worktree, head_ref);
979 err = check_linear_ancestry(commit_id,
980 got_worktree_get_base_commit_id(worktree), repo);
981 if (err) {
982 if (err->code != GOT_ERR_ANCESTRY)
983 return err;
984 ref_has_moved = 1;
986 if (!ref_has_moved)
987 return NULL;
989 /* Switching to a rebased branch with the same reference name. */
990 err = got_object_id_str(&base_id_str,
991 got_worktree_get_base_commit_id(worktree));
992 if (err)
993 return err;
994 printf("Reference %s now points at a different branch\n",
995 got_worktree_get_head_ref_name(worktree));
996 printf("Switching work tree from %s to %s\n", base_id_str,
997 got_worktree_get_head_ref_name(worktree));
998 return NULL;
1001 static const struct got_error *
1002 cmd_update(int argc, char *argv[])
1004 const struct got_error *error = NULL;
1005 struct got_repository *repo = NULL;
1006 struct got_worktree *worktree = NULL;
1007 char *worktree_path = NULL, *path = NULL;
1008 struct got_object_id *commit_id = NULL;
1009 char *commit_id_str = NULL;
1010 const char *branch_name = NULL;
1011 struct got_reference *head_ref = NULL;
1012 int ch, did_something = 0, rebase_in_progress;
1014 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1015 switch (ch) {
1016 case 'b':
1017 branch_name = optarg;
1018 break;
1019 case 'c':
1020 commit_id_str = strdup(optarg);
1021 if (commit_id_str == NULL)
1022 return got_error_from_errno("strdup");
1023 break;
1024 default:
1025 usage_update();
1026 /* NOTREACHED */
1030 argc -= optind;
1031 argv += optind;
1033 #ifndef PROFILE
1034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1035 "unveil", NULL) == -1)
1036 err(1, "pledge");
1037 #endif
1038 worktree_path = getcwd(NULL, 0);
1039 if (worktree_path == NULL) {
1040 error = got_error_from_errno("getcwd");
1041 goto done;
1043 error = got_worktree_open(&worktree, worktree_path);
1044 if (error)
1045 goto done;
1047 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1048 if (error)
1049 goto done;
1050 if (rebase_in_progress) {
1051 error = got_error(GOT_ERR_REBASING);
1052 goto done;
1055 if (argc == 0) {
1056 path = strdup("");
1057 if (path == NULL) {
1058 error = got_error_from_errno("strdup");
1059 goto done;
1061 } else if (argc == 1) {
1062 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1063 if (error)
1064 goto done;
1065 } else
1066 usage_update();
1068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1069 if (error != NULL)
1070 goto done;
1072 error = apply_unveil(got_repo_get_path(repo), 0,
1073 got_worktree_get_root_path(worktree), 0);
1074 if (error)
1075 goto done;
1077 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1078 got_worktree_get_head_ref_name(worktree), 0);
1079 if (error != NULL)
1080 goto done;
1081 if (commit_id_str == NULL) {
1082 error = got_ref_resolve(&commit_id, repo, head_ref);
1083 if (error != NULL)
1084 goto done;
1085 error = got_object_id_str(&commit_id_str, commit_id);
1086 if (error != NULL)
1087 goto done;
1088 } else {
1089 error = got_repo_match_object_id_prefix(&commit_id,
1090 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1091 if (error != NULL)
1092 goto done;
1093 free(commit_id_str);
1094 error = got_object_id_str(&commit_id_str, commit_id);
1095 if (error)
1096 goto done;
1099 if (branch_name) {
1100 struct got_object_id *head_commit_id;
1101 if (strlen(path) != 0) {
1102 fprintf(stderr, "%s: switching between branches "
1103 "requires that the entire work tree "
1104 "gets updated, not just '%s'\n",
1105 getprogname(), path);
1106 error = got_error(GOT_ERR_BAD_PATH);
1107 goto done;
1109 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1110 if (error)
1111 goto done;
1112 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1113 free(head_commit_id);
1114 if (error != NULL)
1115 goto done;
1116 error = check_same_branch(commit_id, head_ref, repo);
1117 if (error)
1118 goto done;
1119 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1120 if (error)
1121 goto done;
1122 } else {
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), repo);
1125 if (error != NULL) {
1126 if (error->code == GOT_ERR_ANCESTRY)
1127 error = got_error(GOT_ERR_BRANCH_MOVED);
1128 goto done;
1130 error = check_same_branch(commit_id, head_ref, repo);
1131 if (error)
1132 goto done;
1135 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1136 commit_id) != 0) {
1137 error = got_worktree_set_base_commit_id(worktree, repo,
1138 commit_id);
1139 if (error)
1140 goto done;
1143 error = got_worktree_checkout_files(worktree, path, repo,
1144 update_progress, &did_something, check_cancelled, NULL);
1145 if (error != NULL)
1146 goto done;
1148 if (did_something)
1149 printf("Updated to commit %s\n", commit_id_str);
1150 else
1151 printf("Already up-to-date\n");
1152 done:
1153 free(worktree_path);
1154 free(path);
1155 free(commit_id);
1156 free(commit_id_str);
1157 return error;
1160 static const struct got_error *
1161 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1162 int diff_context, struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 struct got_tree_object *tree1 = NULL, *tree2;
1166 struct got_object_qid *qid;
1167 char *id_str1 = NULL, *id_str2;
1168 struct got_diff_blob_output_unidiff_arg arg;
1170 err = got_object_open_as_tree(&tree2, repo,
1171 got_object_commit_get_tree_id(commit));
1172 if (err)
1173 return err;
1175 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1176 if (qid != NULL) {
1177 struct got_commit_object *pcommit;
1179 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1180 if (err)
1181 return err;
1183 err = got_object_open_as_tree(&tree1, repo,
1184 got_object_commit_get_tree_id(pcommit));
1185 got_object_commit_close(pcommit);
1186 if (err)
1187 return err;
1189 err = got_object_id_str(&id_str1, qid->id);
1190 if (err)
1191 return err;
1194 err = got_object_id_str(&id_str2, id);
1195 if (err)
1196 goto done;
1198 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1199 arg.diff_context = diff_context;
1200 arg.outfile = stdout;
1201 err = got_diff_tree(tree1, tree2, "", "", repo,
1202 got_diff_blob_output_unidiff, &arg);
1203 done:
1204 if (tree1)
1205 got_object_tree_close(tree1);
1206 got_object_tree_close(tree2);
1207 free(id_str1);
1208 free(id_str2);
1209 return err;
1212 static char *
1213 get_datestr(time_t *time, char *datebuf)
1215 char *p, *s = ctime_r(time, datebuf);
1216 p = strchr(s, '\n');
1217 if (p)
1218 *p = '\0';
1219 return s;
1222 static const struct got_error *
1223 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1224 struct got_repository *repo, int show_patch, int diff_context,
1225 struct got_reflist_head *refs)
1227 const struct got_error *err = NULL;
1228 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1229 char datebuf[26];
1230 time_t committer_time;
1231 const char *author, *committer;
1232 char *refs_str = NULL;
1233 struct got_reflist_entry *re;
1235 SIMPLEQ_FOREACH(re, refs, entry) {
1236 char *s;
1237 const char *name;
1238 if (got_object_id_cmp(re->id, id) != 0)
1239 continue;
1240 name = got_ref_get_name(re->ref);
1241 if (strcmp(name, GOT_REF_HEAD) == 0)
1242 continue;
1243 if (strncmp(name, "refs/", 5) == 0)
1244 name += 5;
1245 if (strncmp(name, "got/", 4) == 0)
1246 continue;
1247 if (strncmp(name, "heads/", 6) == 0)
1248 name += 6;
1249 if (strncmp(name, "remotes/", 8) == 0)
1250 name += 8;
1251 s = refs_str;
1252 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1253 name) == -1) {
1254 err = got_error_from_errno("asprintf");
1255 free(s);
1256 break;
1258 free(s);
1260 err = got_object_id_str(&id_str, id);
1261 if (err)
1262 return err;
1264 printf("-----------------------------------------------\n");
1265 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1266 refs_str ? refs_str : "", refs_str ? ")" : "");
1267 free(id_str);
1268 id_str = NULL;
1269 free(refs_str);
1270 refs_str = NULL;
1271 printf("from: %s\n", got_object_commit_get_author(commit));
1272 committer_time = got_object_commit_get_committer_time(commit);
1273 datestr = get_datestr(&committer_time, datebuf);
1274 printf("date: %s UTC\n", datestr);
1275 author = got_object_commit_get_author(commit);
1276 committer = got_object_commit_get_committer(commit);
1277 if (strcmp(author, committer) != 0)
1278 printf("via: %s\n", committer);
1279 if (got_object_commit_get_nparents(commit) > 1) {
1280 const struct got_object_id_queue *parent_ids;
1281 struct got_object_qid *qid;
1282 int n = 1;
1283 parent_ids = got_object_commit_get_parent_ids(commit);
1284 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1285 err = got_object_id_str(&id_str, qid->id);
1286 if (err)
1287 return err;
1288 printf("parent %d: %s\n", n++, id_str);
1289 free(id_str);
1293 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1294 if (logmsg0 == NULL)
1295 return got_error_from_errno("strdup");
1297 logmsg = logmsg0;
1298 do {
1299 line = strsep(&logmsg, "\n");
1300 if (line)
1301 printf(" %s\n", line);
1302 } while (line);
1303 free(logmsg0);
1305 if (show_patch) {
1306 err = print_patch(commit, id, diff_context, repo);
1307 if (err == 0)
1308 printf("\n");
1311 if (fflush(stdout) != 0 && err == NULL)
1312 err = got_error_from_errno("fflush");
1313 return err;
1316 static const struct got_error *
1317 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1318 char *path, int show_patch, int diff_context, int limit,
1319 int first_parent_traversal, struct got_reflist_head *refs)
1321 const struct got_error *err;
1322 struct got_commit_graph *graph;
1324 err = got_commit_graph_open(&graph, root_id, path,
1325 first_parent_traversal, repo);
1326 if (err)
1327 return err;
1328 err = got_commit_graph_iter_start(graph, root_id, repo);
1329 if (err)
1330 goto done;
1331 for (;;) {
1332 struct got_commit_object *commit;
1333 struct got_object_id *id;
1335 if (sigint_received || sigpipe_received)
1336 break;
1338 err = got_commit_graph_iter_next(&id, graph);
1339 if (err) {
1340 if (err->code == GOT_ERR_ITER_COMPLETED) {
1341 err = NULL;
1342 break;
1344 if (err->code != GOT_ERR_ITER_NEED_MORE)
1345 break;
1346 err = got_commit_graph_fetch_commits(graph, 1, repo);
1347 if (err)
1348 break;
1349 else
1350 continue;
1352 if (id == NULL)
1353 break;
1355 err = got_object_open_as_commit(&commit, repo, id);
1356 if (err)
1357 break;
1358 err = print_commit(commit, id, repo, show_patch, diff_context,
1359 refs);
1360 got_object_commit_close(commit);
1361 if (err || (limit && --limit == 0))
1362 break;
1364 done:
1365 got_commit_graph_close(graph);
1366 return err;
1369 __dead static void
1370 usage_log(void)
1372 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1373 "[-r repository-path] [path]\n", getprogname());
1374 exit(1);
1377 static const struct got_error *
1378 cmd_log(int argc, char *argv[])
1380 const struct got_error *error;
1381 struct got_repository *repo = NULL;
1382 struct got_worktree *worktree = NULL;
1383 struct got_commit_object *commit = NULL;
1384 struct got_object_id *id = NULL;
1385 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1386 char *start_commit = NULL;
1387 int diff_context = 3, ch;
1388 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1389 const char *errstr;
1390 struct got_reflist_head refs;
1392 SIMPLEQ_INIT(&refs);
1394 #ifndef PROFILE
1395 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1396 NULL)
1397 == -1)
1398 err(1, "pledge");
1399 #endif
1401 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1402 switch (ch) {
1403 case 'p':
1404 show_patch = 1;
1405 break;
1406 case 'c':
1407 start_commit = optarg;
1408 break;
1409 case 'C':
1410 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1411 &errstr);
1412 if (errstr != NULL)
1413 err(1, "-C option %s", errstr);
1414 break;
1415 case 'l':
1416 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1417 if (errstr != NULL)
1418 err(1, "-l option %s", errstr);
1419 break;
1420 case 'f':
1421 first_parent_traversal = 1;
1422 break;
1423 case 'r':
1424 repo_path = realpath(optarg, NULL);
1425 if (repo_path == NULL)
1426 err(1, "-r option");
1427 got_path_strip_trailing_slashes(repo_path);
1428 break;
1429 default:
1430 usage_log();
1431 /* NOTREACHED */
1435 argc -= optind;
1436 argv += optind;
1438 cwd = getcwd(NULL, 0);
1439 if (cwd == NULL) {
1440 error = got_error_from_errno("getcwd");
1441 goto done;
1444 error = got_worktree_open(&worktree, cwd);
1445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1446 goto done;
1447 error = NULL;
1449 if (argc == 0) {
1450 path = strdup("");
1451 if (path == NULL) {
1452 error = got_error_from_errno("strdup");
1453 goto done;
1455 } else if (argc == 1) {
1456 if (worktree) {
1457 error = got_worktree_resolve_path(&path, worktree,
1458 argv[0]);
1459 if (error)
1460 goto done;
1461 } else {
1462 path = strdup(argv[0]);
1463 if (path == NULL) {
1464 error = got_error_from_errno("strdup");
1465 goto done;
1468 } else
1469 usage_log();
1471 if (repo_path == NULL) {
1472 repo_path = worktree ?
1473 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1475 if (repo_path == NULL) {
1476 error = got_error_from_errno("strdup");
1477 goto done;
1480 error = got_repo_open(&repo, repo_path);
1481 if (error != NULL)
1482 goto done;
1484 error = apply_unveil(got_repo_get_path(repo), 1,
1485 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1486 if (error)
1487 goto done;
1489 if (start_commit == NULL) {
1490 struct got_reference *head_ref;
1491 error = got_ref_open(&head_ref, repo,
1492 worktree ? got_worktree_get_head_ref_name(worktree)
1493 : GOT_REF_HEAD, 0);
1494 if (error != NULL)
1495 return error;
1496 error = got_ref_resolve(&id, repo, head_ref);
1497 got_ref_close(head_ref);
1498 if (error != NULL)
1499 return error;
1500 error = got_object_open_as_commit(&commit, repo, id);
1501 } else {
1502 struct got_reference *ref;
1503 error = got_ref_open(&ref, repo, start_commit, 0);
1504 if (error == NULL) {
1505 int obj_type;
1506 error = got_ref_resolve(&id, repo, ref);
1507 got_ref_close(ref);
1508 if (error != NULL)
1509 goto done;
1510 error = got_object_get_type(&obj_type, repo, id);
1511 if (error != NULL)
1512 goto done;
1513 if (obj_type == GOT_OBJ_TYPE_TAG) {
1514 struct got_tag_object *tag;
1515 error = got_object_open_as_tag(&tag, repo, id);
1516 if (error != NULL)
1517 goto done;
1518 if (got_object_tag_get_object_type(tag) !=
1519 GOT_OBJ_TYPE_COMMIT) {
1520 got_object_tag_close(tag);
1521 error = got_error(GOT_ERR_OBJ_TYPE);
1522 goto done;
1524 free(id);
1525 id = got_object_id_dup(
1526 got_object_tag_get_object_id(tag));
1527 if (id == NULL)
1528 error = got_error_from_errno(
1529 "got_object_id_dup");
1530 got_object_tag_close(tag);
1531 if (error)
1532 goto done;
1533 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1534 error = got_error(GOT_ERR_OBJ_TYPE);
1535 goto done;
1537 error = got_object_open_as_commit(&commit, repo, id);
1538 if (error != NULL)
1539 goto done;
1541 if (commit == NULL) {
1542 error = got_repo_match_object_id_prefix(&id,
1543 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1544 if (error != NULL)
1545 return error;
1548 if (error != NULL)
1549 goto done;
1551 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1552 if (error != NULL)
1553 goto done;
1554 if (in_repo_path) {
1555 free(path);
1556 path = in_repo_path;
1559 error = got_ref_list(&refs, repo);
1560 if (error)
1561 goto done;
1563 error = print_commits(id, repo, path, show_patch,
1564 diff_context, limit, first_parent_traversal, &refs);
1565 done:
1566 free(path);
1567 free(repo_path);
1568 free(cwd);
1569 free(id);
1570 if (worktree)
1571 got_worktree_close(worktree);
1572 if (repo) {
1573 const struct got_error *repo_error;
1574 repo_error = got_repo_close(repo);
1575 if (error == NULL)
1576 error = repo_error;
1578 got_ref_list_free(&refs);
1579 return error;
1582 __dead static void
1583 usage_diff(void)
1585 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1586 "[object1 object2 | path]\n", getprogname());
1587 exit(1);
1590 struct print_diff_arg {
1591 struct got_repository *repo;
1592 struct got_worktree *worktree;
1593 int diff_context;
1594 const char *id_str;
1595 int header_shown;
1598 static const struct got_error *
1599 print_diff(void *arg, unsigned char status, const char *path,
1600 struct got_object_id *blob_id, struct got_object_id *commit_id)
1602 struct print_diff_arg *a = arg;
1603 const struct got_error *err = NULL;
1604 struct got_blob_object *blob1 = NULL;
1605 FILE *f2 = NULL;
1606 char *abspath = NULL;
1607 struct stat sb;
1609 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1610 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1611 return NULL;
1613 if (!a->header_shown) {
1614 printf("diff %s %s\n", a->id_str,
1615 got_worktree_get_root_path(a->worktree));
1616 a->header_shown = 1;
1619 if (status != GOT_STATUS_ADD) {
1620 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1621 if (err)
1622 goto done;
1626 if (status != GOT_STATUS_DELETE) {
1627 if (asprintf(&abspath, "%s/%s",
1628 got_worktree_get_root_path(a->worktree), path) == -1) {
1629 err = got_error_from_errno("asprintf");
1630 goto done;
1633 f2 = fopen(abspath, "r");
1634 if (f2 == NULL) {
1635 err = got_error_from_errno2("fopen", abspath);
1636 goto done;
1638 if (lstat(abspath, &sb) == -1) {
1639 err = got_error_from_errno2("lstat", abspath);
1640 goto done;
1642 } else
1643 sb.st_size = 0;
1645 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1646 stdout);
1647 done:
1648 if (blob1)
1649 got_object_blob_close(blob1);
1650 if (f2 && fclose(f2) != 0 && err == NULL)
1651 err = got_error_from_errno("fclose");
1652 free(abspath);
1653 return err;
1656 static const struct got_error *
1657 cmd_diff(int argc, char *argv[])
1659 const struct got_error *error;
1660 struct got_repository *repo = NULL;
1661 struct got_worktree *worktree = NULL;
1662 char *cwd = NULL, *repo_path = NULL;
1663 struct got_object_id *id1 = NULL, *id2 = NULL;
1664 const char *id_str1 = NULL, *id_str2 = NULL;
1665 char *label1 = NULL, *label2 = NULL;
1666 int type1, type2;
1667 int diff_context = 3, ch;
1668 const char *errstr;
1669 char *path = NULL;
1671 #ifndef PROFILE
1672 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1673 NULL) == -1)
1674 err(1, "pledge");
1675 #endif
1677 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1678 switch (ch) {
1679 case 'C':
1680 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1681 if (errstr != NULL)
1682 err(1, "-C option %s", errstr);
1683 break;
1684 case 'r':
1685 repo_path = realpath(optarg, NULL);
1686 if (repo_path == NULL)
1687 err(1, "-r option");
1688 got_path_strip_trailing_slashes(repo_path);
1689 break;
1690 default:
1691 usage_diff();
1692 /* NOTREACHED */
1696 argc -= optind;
1697 argv += optind;
1699 cwd = getcwd(NULL, 0);
1700 if (cwd == NULL) {
1701 error = got_error_from_errno("getcwd");
1702 goto done;
1704 error = got_worktree_open(&worktree, cwd);
1705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1706 goto done;
1707 if (argc <= 1) {
1708 if (worktree == NULL) {
1709 error = got_error(GOT_ERR_NOT_WORKTREE);
1710 goto done;
1712 if (repo_path)
1713 errx(1,
1714 "-r option can't be used when diffing a work tree");
1715 repo_path = strdup(got_worktree_get_repo_path(worktree));
1716 if (repo_path == NULL) {
1717 error = got_error_from_errno("strdup");
1718 goto done;
1720 if (argc == 1) {
1721 error = got_worktree_resolve_path(&path, worktree,
1722 argv[0]);
1723 if (error)
1724 goto done;
1725 } else {
1726 path = strdup("");
1727 if (path == NULL) {
1728 error = got_error_from_errno("strdup");
1729 goto done;
1732 } else if (argc == 2) {
1733 id_str1 = argv[0];
1734 id_str2 = argv[1];
1735 if (worktree && repo_path == NULL) {
1736 repo_path =
1737 strdup(got_worktree_get_repo_path(worktree));
1738 if (repo_path == NULL) {
1739 error = got_error_from_errno("strdup");
1740 goto done;
1743 } else
1744 usage_diff();
1746 if (repo_path == NULL) {
1747 repo_path = getcwd(NULL, 0);
1748 if (repo_path == NULL)
1749 return got_error_from_errno("getcwd");
1752 error = got_repo_open(&repo, repo_path);
1753 free(repo_path);
1754 if (error != NULL)
1755 goto done;
1757 error = apply_unveil(got_repo_get_path(repo), 1,
1758 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1759 if (error)
1760 goto done;
1762 if (argc <= 1) {
1763 struct print_diff_arg arg;
1764 char *id_str;
1765 error = got_object_id_str(&id_str,
1766 got_worktree_get_base_commit_id(worktree));
1767 if (error)
1768 goto done;
1769 arg.repo = repo;
1770 arg.worktree = worktree;
1771 arg.diff_context = diff_context;
1772 arg.id_str = id_str;
1773 arg.header_shown = 0;
1775 error = got_worktree_status(worktree, path, repo, print_diff,
1776 &arg, check_cancelled, NULL);
1777 free(id_str);
1778 goto done;
1781 error = got_repo_match_object_id_prefix(&id1, id_str1,
1782 GOT_OBJ_TYPE_ANY, repo);
1783 if (error) {
1784 struct got_reference *ref;
1785 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1786 goto done;
1787 error = got_ref_open(&ref, repo, id_str1, 0);
1788 if (error != NULL)
1789 goto done;
1790 label1 = strdup(got_ref_get_name(ref));
1791 if (label1 == NULL) {
1792 error = got_error_from_errno("strdup");
1793 goto done;
1795 error = got_ref_resolve(&id1, repo, ref);
1796 got_ref_close(ref);
1797 if (error != NULL)
1798 goto done;
1799 } else {
1800 error = got_object_id_str(&label1, id1);
1801 if (label1 == NULL) {
1802 error = got_error_from_errno("strdup");
1803 goto done;
1807 error = got_repo_match_object_id_prefix(&id2, id_str2,
1808 GOT_OBJ_TYPE_ANY, repo);
1809 if (error) {
1810 struct got_reference *ref;
1811 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1812 goto done;
1813 error = got_ref_open(&ref, repo, id_str2, 0);
1814 if (error != NULL)
1815 goto done;
1816 label2 = strdup(got_ref_get_name(ref));
1817 if (label2 == NULL) {
1818 error = got_error_from_errno("strdup");
1819 goto done;
1821 error = got_ref_resolve(&id2, repo, ref);
1822 got_ref_close(ref);
1823 if (error != NULL)
1824 goto done;
1825 } else {
1826 error = got_object_id_str(&label2, id2);
1827 if (label2 == NULL) {
1828 error = got_error_from_errno("strdup");
1829 goto done;
1833 error = got_object_get_type(&type1, repo, id1);
1834 if (error)
1835 goto done;
1837 error = got_object_get_type(&type2, repo, id2);
1838 if (error)
1839 goto done;
1841 if (type1 != type2) {
1842 error = got_error(GOT_ERR_OBJ_TYPE);
1843 goto done;
1846 switch (type1) {
1847 case GOT_OBJ_TYPE_BLOB:
1848 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1849 diff_context, repo, stdout);
1850 break;
1851 case GOT_OBJ_TYPE_TREE:
1852 error = got_diff_objects_as_trees(id1, id2, "", "",
1853 diff_context, repo, stdout);
1854 break;
1855 case GOT_OBJ_TYPE_COMMIT:
1856 printf("diff %s %s\n", label1, label2);
1857 error = got_diff_objects_as_commits(id1, id2, diff_context,
1858 repo, stdout);
1859 break;
1860 default:
1861 error = got_error(GOT_ERR_OBJ_TYPE);
1864 done:
1865 free(label1);
1866 free(label2);
1867 free(id1);
1868 free(id2);
1869 free(path);
1870 if (worktree)
1871 got_worktree_close(worktree);
1872 if (repo) {
1873 const struct got_error *repo_error;
1874 repo_error = got_repo_close(repo);
1875 if (error == NULL)
1876 error = repo_error;
1878 return error;
1881 __dead static void
1882 usage_blame(void)
1884 fprintf(stderr,
1885 "usage: %s blame [-c commit] [-r repository-path] path\n",
1886 getprogname());
1887 exit(1);
1890 static const struct got_error *
1891 cmd_blame(int argc, char *argv[])
1893 const struct got_error *error;
1894 struct got_repository *repo = NULL;
1895 struct got_worktree *worktree = NULL;
1896 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1897 struct got_object_id *commit_id = NULL;
1898 char *commit_id_str = NULL;
1899 int ch;
1901 #ifndef PROFILE
1902 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1903 NULL) == -1)
1904 err(1, "pledge");
1905 #endif
1907 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1908 switch (ch) {
1909 case 'c':
1910 commit_id_str = optarg;
1911 break;
1912 case 'r':
1913 repo_path = realpath(optarg, NULL);
1914 if (repo_path == NULL)
1915 err(1, "-r option");
1916 got_path_strip_trailing_slashes(repo_path);
1917 break;
1918 default:
1919 usage_blame();
1920 /* NOTREACHED */
1924 argc -= optind;
1925 argv += optind;
1927 if (argc == 1)
1928 path = argv[0];
1929 else
1930 usage_blame();
1932 cwd = getcwd(NULL, 0);
1933 if (cwd == NULL) {
1934 error = got_error_from_errno("getcwd");
1935 goto done;
1937 if (repo_path == NULL) {
1938 error = got_worktree_open(&worktree, cwd);
1939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1940 goto done;
1941 else
1942 error = NULL;
1943 if (worktree) {
1944 repo_path =
1945 strdup(got_worktree_get_repo_path(worktree));
1946 if (repo_path == NULL)
1947 error = got_error_from_errno("strdup");
1948 if (error)
1949 goto done;
1950 } else {
1951 repo_path = strdup(cwd);
1952 if (repo_path == NULL) {
1953 error = got_error_from_errno("strdup");
1954 goto done;
1959 error = got_repo_open(&repo, repo_path);
1960 if (error != NULL)
1961 goto done;
1963 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1964 if (error)
1965 goto done;
1967 if (worktree) {
1968 const char *prefix = got_worktree_get_path_prefix(worktree);
1969 char *p, *worktree_subdir = cwd +
1970 strlen(got_worktree_get_root_path(worktree));
1971 if (asprintf(&p, "%s%s%s%s%s",
1972 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1973 worktree_subdir, worktree_subdir[0] ? "/" : "",
1974 path) == -1) {
1975 error = got_error_from_errno("asprintf");
1976 goto done;
1978 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1979 free(p);
1980 } else {
1981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1983 if (error)
1984 goto done;
1986 if (commit_id_str == NULL) {
1987 struct got_reference *head_ref;
1988 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1989 if (error != NULL)
1990 goto done;
1991 error = got_ref_resolve(&commit_id, repo, head_ref);
1992 got_ref_close(head_ref);
1993 if (error != NULL)
1994 goto done;
1995 } else {
1996 error = got_repo_match_object_id_prefix(&commit_id,
1997 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1998 if (error != NULL)
1999 goto done;
2002 error = got_blame(in_repo_path, commit_id, repo, stdout);
2003 done:
2004 free(in_repo_path);
2005 free(repo_path);
2006 free(cwd);
2007 free(commit_id);
2008 if (worktree)
2009 got_worktree_close(worktree);
2010 if (repo) {
2011 const struct got_error *repo_error;
2012 repo_error = got_repo_close(repo);
2013 if (error == NULL)
2014 error = repo_error;
2016 return error;
2019 __dead static void
2020 usage_tree(void)
2022 fprintf(stderr,
2023 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2024 getprogname());
2025 exit(1);
2028 static void
2029 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2030 const char *root_path)
2032 int is_root_path = (strcmp(path, root_path) == 0);
2034 path += strlen(root_path);
2035 while (path[0] == '/')
2036 path++;
2038 printf("%s%s%s%s%s\n", id ? id : "", path,
2039 is_root_path ? "" : "/", te->name,
2040 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2043 static const struct got_error *
2044 print_tree(const char *path, struct got_object_id *commit_id,
2045 int show_ids, int recurse, const char *root_path,
2046 struct got_repository *repo)
2048 const struct got_error *err = NULL;
2049 struct got_object_id *tree_id = NULL;
2050 struct got_tree_object *tree = NULL;
2051 const struct got_tree_entries *entries;
2052 struct got_tree_entry *te;
2054 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2055 if (err)
2056 goto done;
2058 err = got_object_open_as_tree(&tree, repo, tree_id);
2059 if (err)
2060 goto done;
2061 entries = got_object_tree_get_entries(tree);
2062 te = SIMPLEQ_FIRST(&entries->head);
2063 while (te) {
2064 char *id = NULL;
2066 if (sigint_received || sigpipe_received)
2067 break;
2069 if (show_ids) {
2070 char *id_str;
2071 err = got_object_id_str(&id_str, te->id);
2072 if (err)
2073 goto done;
2074 if (asprintf(&id, "%s ", id_str) == -1) {
2075 err = got_error_from_errno("asprintf");
2076 free(id_str);
2077 goto done;
2079 free(id_str);
2081 print_entry(te, id, path, root_path);
2082 free(id);
2084 if (recurse && S_ISDIR(te->mode)) {
2085 char *child_path;
2086 if (asprintf(&child_path, "%s%s%s", path,
2087 path[0] == '/' && path[1] == '\0' ? "" : "/",
2088 te->name) == -1) {
2089 err = got_error_from_errno("asprintf");
2090 goto done;
2092 err = print_tree(child_path, commit_id, show_ids, 1,
2093 root_path, repo);
2094 free(child_path);
2095 if (err)
2096 goto done;
2099 te = SIMPLEQ_NEXT(te, entry);
2101 done:
2102 if (tree)
2103 got_object_tree_close(tree);
2104 free(tree_id);
2105 return err;
2108 static const struct got_error *
2109 cmd_tree(int argc, char *argv[])
2111 const struct got_error *error;
2112 struct got_repository *repo = NULL;
2113 struct got_worktree *worktree = NULL;
2114 const char *path;
2115 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2116 struct got_object_id *commit_id = NULL;
2117 char *commit_id_str = NULL;
2118 int show_ids = 0, recurse = 0;
2119 int ch;
2121 #ifndef PROFILE
2122 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2123 NULL) == -1)
2124 err(1, "pledge");
2125 #endif
2127 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2128 switch (ch) {
2129 case 'c':
2130 commit_id_str = optarg;
2131 break;
2132 case 'r':
2133 repo_path = realpath(optarg, NULL);
2134 if (repo_path == NULL)
2135 err(1, "-r option");
2136 got_path_strip_trailing_slashes(repo_path);
2137 break;
2138 case 'i':
2139 show_ids = 1;
2140 break;
2141 case 'R':
2142 recurse = 1;
2143 break;
2144 default:
2145 usage_tree();
2146 /* NOTREACHED */
2150 argc -= optind;
2151 argv += optind;
2153 if (argc == 1)
2154 path = argv[0];
2155 else if (argc > 1)
2156 usage_tree();
2157 else
2158 path = NULL;
2160 cwd = getcwd(NULL, 0);
2161 if (cwd == NULL) {
2162 error = got_error_from_errno("getcwd");
2163 goto done;
2165 if (repo_path == NULL) {
2166 error = got_worktree_open(&worktree, cwd);
2167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2168 goto done;
2169 else
2170 error = NULL;
2171 if (worktree) {
2172 repo_path =
2173 strdup(got_worktree_get_repo_path(worktree));
2174 if (repo_path == NULL)
2175 error = got_error_from_errno("strdup");
2176 if (error)
2177 goto done;
2178 } else {
2179 repo_path = strdup(cwd);
2180 if (repo_path == NULL) {
2181 error = got_error_from_errno("strdup");
2182 goto done;
2187 error = got_repo_open(&repo, repo_path);
2188 if (error != NULL)
2189 goto done;
2191 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2192 if (error)
2193 goto done;
2195 if (path == NULL) {
2196 if (worktree) {
2197 char *p, *worktree_subdir = cwd +
2198 strlen(got_worktree_get_root_path(worktree));
2199 if (asprintf(&p, "%s/%s",
2200 got_worktree_get_path_prefix(worktree),
2201 worktree_subdir) == -1) {
2202 error = got_error_from_errno("asprintf");
2203 goto done;
2205 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2206 free(p);
2207 if (error)
2208 goto done;
2209 } else
2210 path = "/";
2212 if (in_repo_path == NULL) {
2213 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2214 if (error != NULL)
2215 goto done;
2218 if (commit_id_str == NULL) {
2219 struct got_reference *head_ref;
2220 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2221 if (error != NULL)
2222 goto done;
2223 error = got_ref_resolve(&commit_id, repo, head_ref);
2224 got_ref_close(head_ref);
2225 if (error != NULL)
2226 goto done;
2227 } else {
2228 error = got_repo_match_object_id_prefix(&commit_id,
2229 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2230 if (error != NULL)
2231 goto done;
2234 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2235 in_repo_path, repo);
2236 done:
2237 free(in_repo_path);
2238 free(repo_path);
2239 free(cwd);
2240 free(commit_id);
2241 if (worktree)
2242 got_worktree_close(worktree);
2243 if (repo) {
2244 const struct got_error *repo_error;
2245 repo_error = got_repo_close(repo);
2246 if (error == NULL)
2247 error = repo_error;
2249 return error;
2252 __dead static void
2253 usage_status(void)
2255 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2256 exit(1);
2259 static const struct got_error *
2260 print_status(void *arg, unsigned char status, const char *path,
2261 struct got_object_id *blob_id, struct got_object_id *commit_id)
2263 printf("%c %s\n", status, path);
2264 return NULL;
2267 static const struct got_error *
2268 cmd_status(int argc, char *argv[])
2270 const struct got_error *error = NULL;
2271 struct got_repository *repo = NULL;
2272 struct got_worktree *worktree = NULL;
2273 char *cwd = NULL, *path = NULL;
2274 int ch;
2276 while ((ch = getopt(argc, argv, "")) != -1) {
2277 switch (ch) {
2278 default:
2279 usage_status();
2280 /* NOTREACHED */
2284 argc -= optind;
2285 argv += optind;
2287 #ifndef PROFILE
2288 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2289 NULL) == -1)
2290 err(1, "pledge");
2291 #endif
2292 cwd = getcwd(NULL, 0);
2293 if (cwd == NULL) {
2294 error = got_error_from_errno("getcwd");
2295 goto done;
2298 error = got_worktree_open(&worktree, cwd);
2299 if (error != NULL)
2300 goto done;
2302 if (argc == 0) {
2303 path = strdup("");
2304 if (path == NULL) {
2305 error = got_error_from_errno("strdup");
2306 goto done;
2308 } else if (argc == 1) {
2309 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2310 if (error)
2311 goto done;
2312 } else
2313 usage_status();
2315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2316 if (error != NULL)
2317 goto done;
2319 error = apply_unveil(got_repo_get_path(repo), 1,
2320 got_worktree_get_root_path(worktree), 0);
2321 if (error)
2322 goto done;
2324 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2325 check_cancelled, NULL);
2326 done:
2327 free(cwd);
2328 free(path);
2329 return error;
2332 __dead static void
2333 usage_ref(void)
2335 fprintf(stderr,
2336 "usage: %s ref [-r repository] -l | -d name | name target\n",
2337 getprogname());
2338 exit(1);
2341 static const struct got_error *
2342 list_refs(struct got_repository *repo)
2344 static const struct got_error *err = NULL;
2345 struct got_reflist_head refs;
2346 struct got_reflist_entry *re;
2348 SIMPLEQ_INIT(&refs);
2349 err = got_ref_list(&refs, repo);
2350 if (err)
2351 return err;
2353 SIMPLEQ_FOREACH(re, &refs, entry) {
2354 char *refstr;
2355 refstr = got_ref_to_str(re->ref);
2356 if (refstr == NULL)
2357 return got_error_from_errno("got_ref_to_str");
2358 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2359 free(refstr);
2362 got_ref_list_free(&refs);
2363 return NULL;
2366 static const struct got_error *
2367 delete_ref(struct got_repository *repo, const char *refname)
2369 const struct got_error *err = NULL;
2370 struct got_reference *ref;
2372 err = got_ref_open(&ref, repo, refname, 0);
2373 if (err)
2374 return err;
2376 err = got_ref_delete(ref, repo);
2377 got_ref_close(ref);
2378 return err;
2381 static const struct got_error *
2382 add_ref(struct got_repository *repo, const char *refname, const char *target)
2384 const struct got_error *err = NULL;
2385 struct got_object_id *id;
2386 struct got_reference *ref = NULL;
2389 * Don't let the user create a reference named '-'.
2390 * While technically a valid reference name, this case is usually
2391 * an unintended typo.
2393 if (refname[0] == '-' && refname[1] == '\0')
2394 return got_error(GOT_ERR_BAD_REF_NAME);
2396 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2397 repo);
2398 if (err) {
2399 struct got_reference *target_ref;
2401 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2402 return err;
2403 err = got_ref_open(&target_ref, repo, target, 0);
2404 if (err)
2405 return err;
2406 err = got_ref_resolve(&id, repo, target_ref);
2407 got_ref_close(target_ref);
2408 if (err)
2409 return err;
2412 err = got_ref_alloc(&ref, refname, id);
2413 if (err)
2414 goto done;
2416 err = got_ref_write(ref, repo);
2417 done:
2418 if (ref)
2419 got_ref_close(ref);
2420 free(id);
2421 return err;
2424 static const struct got_error *
2425 cmd_ref(int argc, char *argv[])
2427 const struct got_error *error = NULL;
2428 struct got_repository *repo = NULL;
2429 struct got_worktree *worktree = NULL;
2430 char *cwd = NULL, *repo_path = NULL;
2431 int ch, do_list = 0;
2432 const char *delref = NULL;
2434 /* TODO: Add -s option for adding symbolic references. */
2435 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2436 switch (ch) {
2437 case 'd':
2438 delref = optarg;
2439 break;
2440 case 'r':
2441 repo_path = realpath(optarg, NULL);
2442 if (repo_path == NULL)
2443 err(1, "-r option");
2444 got_path_strip_trailing_slashes(repo_path);
2445 break;
2446 case 'l':
2447 do_list = 1;
2448 break;
2449 default:
2450 usage_ref();
2451 /* NOTREACHED */
2455 if (do_list && delref)
2456 errx(1, "-l and -d options are mutually exclusive\n");
2458 argc -= optind;
2459 argv += optind;
2461 if (do_list || delref) {
2462 if (argc > 0)
2463 usage_ref();
2464 } else if (argc != 2)
2465 usage_ref();
2467 #ifndef PROFILE
2468 if (do_list) {
2469 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2470 NULL) == -1)
2471 err(1, "pledge");
2472 } else {
2473 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2474 "sendfd unveil", NULL) == -1)
2475 err(1, "pledge");
2477 #endif
2478 cwd = getcwd(NULL, 0);
2479 if (cwd == NULL) {
2480 error = got_error_from_errno("getcwd");
2481 goto done;
2484 if (repo_path == NULL) {
2485 error = got_worktree_open(&worktree, cwd);
2486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2487 goto done;
2488 else
2489 error = NULL;
2490 if (worktree) {
2491 repo_path =
2492 strdup(got_worktree_get_repo_path(worktree));
2493 if (repo_path == NULL)
2494 error = got_error_from_errno("strdup");
2495 if (error)
2496 goto done;
2497 } else {
2498 repo_path = strdup(cwd);
2499 if (repo_path == NULL) {
2500 error = got_error_from_errno("strdup");
2501 goto done;
2506 error = got_repo_open(&repo, repo_path);
2507 if (error != NULL)
2508 goto done;
2510 error = apply_unveil(got_repo_get_path(repo), do_list,
2511 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2512 if (error)
2513 goto done;
2515 if (do_list)
2516 error = list_refs(repo);
2517 else if (delref)
2518 error = delete_ref(repo, delref);
2519 else
2520 error = add_ref(repo, argv[0], argv[1]);
2521 done:
2522 if (repo)
2523 got_repo_close(repo);
2524 if (worktree)
2525 got_worktree_close(worktree);
2526 free(cwd);
2527 free(repo_path);
2528 return error;
2531 __dead static void
2532 usage_branch(void)
2534 fprintf(stderr,
2535 "usage: %s branch [-r repository] -l | -d name | "
2536 "name [base-branch]\n", getprogname());
2537 exit(1);
2540 static const struct got_error *
2541 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2543 static const struct got_error *err = NULL;
2544 struct got_reflist_head refs;
2545 struct got_reflist_entry *re;
2547 SIMPLEQ_INIT(&refs);
2549 err = got_ref_list(&refs, repo);
2550 if (err)
2551 return err;
2553 SIMPLEQ_FOREACH(re, &refs, entry) {
2554 const char *refname, *marker = " ";
2555 char *refstr;
2556 refname = got_ref_get_name(re->ref);
2557 if (strncmp(refname, "refs/heads/", 11) != 0)
2558 continue;
2559 if (worktree && strcmp(refname,
2560 got_worktree_get_head_ref_name(worktree)) == 0) {
2561 struct got_object_id *id = NULL;
2562 err = got_ref_resolve(&id, repo, re->ref);
2563 if (err)
2564 return err;
2565 if (got_object_id_cmp(id,
2566 got_worktree_get_base_commit_id(worktree)) == 0)
2567 marker = "* ";
2568 else
2569 marker = "~ ";
2570 free(id);
2572 refname += 11;
2573 refstr = got_ref_to_str(re->ref);
2574 if (refstr == NULL)
2575 return got_error_from_errno("got_ref_to_str");
2576 printf("%s%s: %s\n", marker, refname, refstr);
2577 free(refstr);
2580 got_ref_list_free(&refs);
2581 return NULL;
2584 static const struct got_error *
2585 delete_branch(struct got_repository *repo, const char *branch_name)
2587 const struct got_error *err = NULL;
2588 struct got_reference *ref;
2589 char *refname;
2591 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2592 return got_error_from_errno("asprintf");
2594 err = got_ref_open(&ref, repo, refname, 0);
2595 if (err)
2596 goto done;
2598 err = got_ref_delete(ref, repo);
2599 got_ref_close(ref);
2600 done:
2601 free(refname);
2602 return err;
2605 static const struct got_error *
2606 add_branch(struct got_repository *repo, const char *branch_name,
2607 const char *base_branch)
2609 const struct got_error *err = NULL;
2610 struct got_object_id *id = NULL;
2611 struct got_reference *ref = NULL;
2612 char *base_refname = NULL, *refname = NULL;
2613 struct got_reference *base_ref;
2616 * Don't let the user create a branch named '-'.
2617 * While technically a valid reference name, this case is usually
2618 * an unintended typo.
2620 if (branch_name[0] == '-' && branch_name[1] == '\0')
2621 return got_error(GOT_ERR_BAD_REF_NAME);
2623 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2624 base_refname = strdup(GOT_REF_HEAD);
2625 if (base_refname == NULL)
2626 return got_error_from_errno("strdup");
2627 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2628 return got_error_from_errno("asprintf");
2630 err = got_ref_open(&base_ref, repo, base_refname, 0);
2631 if (err)
2632 goto done;
2633 err = got_ref_resolve(&id, repo, base_ref);
2634 got_ref_close(base_ref);
2635 if (err)
2636 goto done;
2638 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2639 err = got_error_from_errno("asprintf");
2640 goto done;
2643 err = got_ref_open(&ref, repo, refname, 0);
2644 if (err == NULL) {
2645 err = got_error(GOT_ERR_BRANCH_EXISTS);
2646 goto done;
2647 } else if (err->code != GOT_ERR_NOT_REF)
2648 goto done;
2650 err = got_ref_alloc(&ref, refname, id);
2651 if (err)
2652 goto done;
2654 err = got_ref_write(ref, repo);
2655 done:
2656 if (ref)
2657 got_ref_close(ref);
2658 free(id);
2659 free(base_refname);
2660 free(refname);
2661 return err;
2664 static const struct got_error *
2665 cmd_branch(int argc, char *argv[])
2667 const struct got_error *error = NULL;
2668 struct got_repository *repo = NULL;
2669 struct got_worktree *worktree = NULL;
2670 char *cwd = NULL, *repo_path = NULL;
2671 int ch, do_list = 0;
2672 const char *delref = NULL;
2674 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2675 switch (ch) {
2676 case 'd':
2677 delref = optarg;
2678 break;
2679 case 'r':
2680 repo_path = realpath(optarg, NULL);
2681 if (repo_path == NULL)
2682 err(1, "-r option");
2683 got_path_strip_trailing_slashes(repo_path);
2684 break;
2685 case 'l':
2686 do_list = 1;
2687 break;
2688 default:
2689 usage_branch();
2690 /* NOTREACHED */
2694 if (do_list && delref)
2695 errx(1, "-l and -d options are mutually exclusive\n");
2697 argc -= optind;
2698 argv += optind;
2700 if (do_list || delref) {
2701 if (argc > 0)
2702 usage_branch();
2703 } else if (argc < 1 || argc > 2)
2704 usage_branch();
2706 #ifndef PROFILE
2707 if (do_list) {
2708 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2709 NULL) == -1)
2710 err(1, "pledge");
2711 } else {
2712 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2713 "sendfd unveil", NULL) == -1)
2714 err(1, "pledge");
2716 #endif
2717 cwd = getcwd(NULL, 0);
2718 if (cwd == NULL) {
2719 error = got_error_from_errno("getcwd");
2720 goto done;
2723 if (repo_path == NULL) {
2724 error = got_worktree_open(&worktree, cwd);
2725 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2726 goto done;
2727 else
2728 error = NULL;
2729 if (worktree) {
2730 repo_path =
2731 strdup(got_worktree_get_repo_path(worktree));
2732 if (repo_path == NULL)
2733 error = got_error_from_errno("strdup");
2734 if (error)
2735 goto done;
2736 } else {
2737 repo_path = strdup(cwd);
2738 if (repo_path == NULL) {
2739 error = got_error_from_errno("strdup");
2740 goto done;
2745 error = got_repo_open(&repo, repo_path);
2746 if (error != NULL)
2747 goto done;
2749 error = apply_unveil(got_repo_get_path(repo), do_list,
2750 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2751 if (error)
2752 goto done;
2754 if (do_list)
2755 error = list_branches(repo, worktree);
2756 else if (delref)
2757 error = delete_branch(repo, delref);
2758 else {
2759 const char *base_branch;
2760 if (argc == 1) {
2761 base_branch = worktree ?
2762 got_worktree_get_head_ref_name(worktree) :
2763 GOT_REF_HEAD;
2764 } else
2765 base_branch = argv[1];
2766 error = add_branch(repo, argv[0], base_branch);
2768 done:
2769 if (repo)
2770 got_repo_close(repo);
2771 if (worktree)
2772 got_worktree_close(worktree);
2773 free(cwd);
2774 free(repo_path);
2775 return error;
2778 __dead static void
2779 usage_add(void)
2781 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2782 exit(1);
2785 static const struct got_error *
2786 cmd_add(int argc, char *argv[])
2788 const struct got_error *error = NULL;
2789 struct got_repository *repo = NULL;
2790 struct got_worktree *worktree = NULL;
2791 char *cwd = NULL;
2792 struct got_pathlist_head paths;
2793 struct got_pathlist_entry *pe;
2794 int ch, x;
2796 TAILQ_INIT(&paths);
2798 while ((ch = getopt(argc, argv, "")) != -1) {
2799 switch (ch) {
2800 default:
2801 usage_add();
2802 /* NOTREACHED */
2806 argc -= optind;
2807 argv += optind;
2809 #ifndef PROFILE
2810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2811 NULL) == -1)
2812 err(1, "pledge");
2813 #endif
2814 if (argc < 1)
2815 usage_add();
2817 /* make sure each file exists before doing anything halfway */
2818 for (x = 0; x < argc; x++) {
2819 char *path = realpath(argv[x], NULL);
2820 if (path == NULL) {
2821 error = got_error_from_errno2("realpath", argv[x]);
2822 goto done;
2824 free(path);
2827 cwd = getcwd(NULL, 0);
2828 if (cwd == NULL) {
2829 error = got_error_from_errno("getcwd");
2830 goto done;
2833 error = got_worktree_open(&worktree, cwd);
2834 if (error)
2835 goto done;
2837 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2838 if (error != NULL)
2839 goto done;
2841 error = apply_unveil(got_repo_get_path(repo), 1,
2842 got_worktree_get_root_path(worktree), 0);
2843 if (error)
2844 goto done;
2846 for (x = 0; x < argc; x++) {
2847 char *path = realpath(argv[x], NULL);
2848 if (path == NULL) {
2849 error = got_error_from_errno2("realpath", argv[x]);
2850 goto done;
2853 got_path_strip_trailing_slashes(path);
2854 error = got_pathlist_insert(&pe, &paths, path, NULL);
2855 if (error) {
2856 free(path);
2857 goto done;
2860 error = got_worktree_schedule_add(worktree, &paths, print_status,
2861 NULL, repo);
2862 done:
2863 if (repo)
2864 got_repo_close(repo);
2865 if (worktree)
2866 got_worktree_close(worktree);
2867 TAILQ_FOREACH(pe, &paths, entry)
2868 free((char *)pe->path);
2869 got_pathlist_free(&paths);
2870 free(cwd);
2871 return error;
2874 __dead static void
2875 usage_remove(void)
2877 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2878 exit(1);
2881 static const struct got_error *
2882 cmd_remove(int argc, char *argv[])
2884 const struct got_error *error = NULL;
2885 struct got_worktree *worktree = NULL;
2886 struct got_repository *repo = NULL;
2887 char *cwd = NULL;
2888 struct got_pathlist_head paths;
2889 struct got_pathlist_entry *pe;
2890 int ch, i, delete_local_mods = 0;
2892 TAILQ_INIT(&paths);
2894 while ((ch = getopt(argc, argv, "f")) != -1) {
2895 switch (ch) {
2896 case 'f':
2897 delete_local_mods = 1;
2898 break;
2899 default:
2900 usage_add();
2901 /* NOTREACHED */
2905 argc -= optind;
2906 argv += optind;
2908 #ifndef PROFILE
2909 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2910 NULL) == -1)
2911 err(1, "pledge");
2912 #endif
2913 if (argc < 1)
2914 usage_remove();
2916 /* make sure each file exists before doing anything halfway */
2917 for (i = 0; i < argc; i++) {
2918 char *path = realpath(argv[i], NULL);
2919 if (path == NULL) {
2920 error = got_error_from_errno2("realpath", argv[i]);
2921 goto done;
2923 free(path);
2926 cwd = getcwd(NULL, 0);
2927 if (cwd == NULL) {
2928 error = got_error_from_errno("getcwd");
2929 goto done;
2931 error = got_worktree_open(&worktree, cwd);
2932 if (error)
2933 goto done;
2935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2936 if (error)
2937 goto done;
2939 error = apply_unveil(got_repo_get_path(repo), 1,
2940 got_worktree_get_root_path(worktree), 0);
2941 if (error)
2942 goto done;
2944 for (i = 0; i < argc; i++) {
2945 char *path = realpath(argv[i], NULL);
2946 if (path == NULL) {
2947 error = got_error_from_errno2("realpath", argv[i]);
2948 goto done;
2951 got_path_strip_trailing_slashes(path);
2952 error = got_pathlist_insert(&pe, &paths, path, NULL);
2953 if (error) {
2954 free(path);
2955 goto done;
2958 error = got_worktree_schedule_delete(worktree, &paths,
2959 delete_local_mods, print_status, NULL, repo);
2960 if (error)
2961 goto done;
2962 done:
2963 if (repo)
2964 got_repo_close(repo);
2965 if (worktree)
2966 got_worktree_close(worktree);
2967 TAILQ_FOREACH(pe, &paths, entry)
2968 free((char *)pe->path);
2969 got_pathlist_free(&paths);
2970 free(cwd);
2971 return error;
2974 __dead static void
2975 usage_revert(void)
2977 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2978 exit(1);
2981 static const struct got_error *
2982 revert_progress(void *arg, unsigned char status, const char *path)
2984 while (path[0] == '/')
2985 path++;
2986 printf("%c %s\n", status, path);
2987 return NULL;
2990 static const struct got_error *
2991 cmd_revert(int argc, char *argv[])
2993 const struct got_error *error = NULL;
2994 struct got_worktree *worktree = NULL;
2995 struct got_repository *repo = NULL;
2996 char *cwd = NULL, *path = NULL;
2997 struct got_pathlist_head paths;
2998 struct got_pathlist_entry *pe;
2999 int ch, i;
3001 TAILQ_INIT(&paths);
3003 while ((ch = getopt(argc, argv, "")) != -1) {
3004 switch (ch) {
3005 default:
3006 usage_revert();
3007 /* NOTREACHED */
3011 argc -= optind;
3012 argv += optind;
3014 #ifndef PROFILE
3015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3016 "unveil", NULL) == -1)
3017 err(1, "pledge");
3018 #endif
3019 if (argc < 1)
3020 usage_revert();
3022 for (i = 0; i < argc; i++) {
3023 char *path = realpath(argv[i], NULL);
3024 if (path == NULL) {
3025 error = got_error_from_errno2("realpath", argv[i]);
3026 goto done;
3029 got_path_strip_trailing_slashes(path);
3030 error = got_pathlist_insert(&pe, &paths, path, NULL);
3031 if (error) {
3032 free(path);
3033 goto done;
3037 cwd = getcwd(NULL, 0);
3038 if (cwd == NULL) {
3039 error = got_error_from_errno("getcwd");
3040 goto done;
3042 error = got_worktree_open(&worktree, cwd);
3043 if (error)
3044 goto done;
3046 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3047 if (error != NULL)
3048 goto done;
3050 error = apply_unveil(got_repo_get_path(repo), 1,
3051 got_worktree_get_root_path(worktree), 0);
3052 if (error)
3053 goto done;
3055 error = got_worktree_revert(worktree, &paths,
3056 revert_progress, NULL, repo);
3057 if (error)
3058 goto done;
3059 done:
3060 if (repo)
3061 got_repo_close(repo);
3062 if (worktree)
3063 got_worktree_close(worktree);
3064 free(path);
3065 free(cwd);
3066 return error;
3069 __dead static void
3070 usage_commit(void)
3072 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3073 exit(1);
3076 struct collect_commit_logmsg_arg {
3077 const char *cmdline_log;
3078 const char *editor;
3079 const char *worktree_path;
3080 const char *branch_name;
3081 const char *repo_path;
3082 char *logmsg_path;
3086 static const struct got_error *
3087 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3088 void *arg)
3090 char *initial_content = NULL;
3091 struct got_pathlist_entry *pe;
3092 const struct got_error *err = NULL;
3093 char *template = NULL;
3094 struct collect_commit_logmsg_arg *a = arg;
3095 int fd;
3096 size_t len;
3098 /* if a message was specified on the command line, just use it */
3099 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3100 len = strlen(a->cmdline_log) + 1;
3101 *logmsg = malloc(len + 1);
3102 if (*logmsg == NULL)
3103 return got_error_from_errno("malloc");
3104 strlcpy(*logmsg, a->cmdline_log, len);
3105 return NULL;
3108 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3109 return got_error_from_errno("asprintf");
3111 if (asprintf(&initial_content,
3112 "\n# changes to be committed on branch %s:\n",
3113 a->branch_name) == -1)
3114 return got_error_from_errno("asprintf");
3116 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3117 if (err)
3118 goto done;
3120 dprintf(fd, initial_content);
3122 TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 struct got_commitable *ct = pe->data;
3124 dprintf(fd, "# %c %s\n",
3125 got_commitable_get_status(ct),
3126 got_commitable_get_path(ct));
3128 close(fd);
3130 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3131 done:
3132 unlink(a->logmsg_path);
3133 free(a->logmsg_path);
3134 free(initial_content);
3135 free(template);
3137 /* Editor is done; we can now apply unveil(2) */
3138 if (err == NULL) {
3139 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3140 if (err) {
3141 free(*logmsg);
3142 *logmsg = NULL;
3145 return err;
3148 static const struct got_error *
3149 cmd_commit(int argc, char *argv[])
3151 const struct got_error *error = NULL;
3152 struct got_worktree *worktree = NULL;
3153 struct got_repository *repo = NULL;
3154 char *cwd = NULL, *path = NULL, *id_str = NULL;
3155 struct got_object_id *id = NULL;
3156 const char *logmsg = NULL;
3157 const char *got_author = getenv("GOT_AUTHOR");
3158 struct collect_commit_logmsg_arg cl_arg;
3159 char *editor = NULL;
3160 int ch, rebase_in_progress;
3162 while ((ch = getopt(argc, argv, "m:")) != -1) {
3163 switch (ch) {
3164 case 'm':
3165 logmsg = optarg;
3166 break;
3167 default:
3168 usage_commit();
3169 /* NOTREACHED */
3173 argc -= optind;
3174 argv += optind;
3176 #ifndef PROFILE
3177 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3178 "unveil", NULL) == -1)
3179 err(1, "pledge");
3180 #endif
3181 if (argc == 1) {
3182 path = realpath(argv[0], NULL);
3183 if (path == NULL) {
3184 error = got_error_from_errno2("realpath", argv[0]);
3185 goto done;
3187 got_path_strip_trailing_slashes(path);
3188 } else if (argc != 0)
3189 usage_commit();
3191 if (got_author == NULL) {
3192 /* TODO: Look current user up in password database */
3193 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3194 goto done;
3197 cwd = getcwd(NULL, 0);
3198 if (cwd == NULL) {
3199 error = got_error_from_errno("getcwd");
3200 goto done;
3202 error = got_worktree_open(&worktree, cwd);
3203 if (error)
3204 goto done;
3206 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3207 if (error)
3208 goto done;
3209 if (rebase_in_progress) {
3210 error = got_error(GOT_ERR_REBASING);
3211 goto done;
3214 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3215 if (error != NULL)
3216 goto done;
3219 * unveil(2) traverses exec(2); if an editor is used we have
3220 * to apply unveil after the log message has been written.
3222 if (logmsg == NULL || strlen(logmsg) == 0)
3223 error = get_editor(&editor);
3224 else
3225 error = apply_unveil(got_repo_get_path(repo), 0,
3226 got_worktree_get_root_path(worktree), 0);
3227 if (error)
3228 goto done;
3230 cl_arg.editor = editor;
3231 cl_arg.cmdline_log = logmsg;
3232 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3233 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3234 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3235 cl_arg.branch_name += 5;
3236 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3237 cl_arg.branch_name += 6;
3238 cl_arg.repo_path = got_repo_get_path(repo);
3239 cl_arg.logmsg_path = NULL;
3240 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3241 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3242 if (error) {
3243 if (cl_arg.logmsg_path)
3244 fprintf(stderr, "%s: log message preserved in %s\n",
3245 getprogname(), cl_arg.logmsg_path);
3246 goto done;
3249 if (cl_arg.logmsg_path)
3250 unlink(cl_arg.logmsg_path);
3252 error = got_object_id_str(&id_str, id);
3253 if (error)
3254 goto done;
3255 printf("Created commit %s\n", id_str);
3256 done:
3257 if (repo)
3258 got_repo_close(repo);
3259 if (worktree)
3260 got_worktree_close(worktree);
3261 free(path);
3262 free(cwd);
3263 free(id_str);
3264 free(editor);
3265 return error;
3268 __dead static void
3269 usage_cherrypick(void)
3271 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3272 exit(1);
3275 static const struct got_error *
3276 cmd_cherrypick(int argc, char *argv[])
3278 const struct got_error *error = NULL;
3279 struct got_worktree *worktree = NULL;
3280 struct got_repository *repo = NULL;
3281 char *cwd = NULL, *commit_id_str = NULL;
3282 struct got_object_id *commit_id = NULL;
3283 struct got_commit_object *commit = NULL;
3284 struct got_object_qid *pid;
3285 struct got_reference *head_ref = NULL;
3286 int ch, did_something = 0;
3288 while ((ch = getopt(argc, argv, "")) != -1) {
3289 switch (ch) {
3290 default:
3291 usage_cherrypick();
3292 /* NOTREACHED */
3296 argc -= optind;
3297 argv += optind;
3299 #ifndef PROFILE
3300 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3301 "unveil", NULL) == -1)
3302 err(1, "pledge");
3303 #endif
3304 if (argc != 1)
3305 usage_cherrypick();
3307 cwd = getcwd(NULL, 0);
3308 if (cwd == NULL) {
3309 error = got_error_from_errno("getcwd");
3310 goto done;
3312 error = got_worktree_open(&worktree, cwd);
3313 if (error)
3314 goto done;
3316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3317 if (error != NULL)
3318 goto done;
3320 error = apply_unveil(got_repo_get_path(repo), 0,
3321 got_worktree_get_root_path(worktree), 0);
3322 if (error)
3323 goto done;
3325 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3326 GOT_OBJ_TYPE_COMMIT, repo);
3327 if (error != NULL) {
3328 struct got_reference *ref;
3329 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3330 goto done;
3331 error = got_ref_open(&ref, repo, argv[0], 0);
3332 if (error != NULL)
3333 goto done;
3334 error = got_ref_resolve(&commit_id, repo, ref);
3335 got_ref_close(ref);
3336 if (error != NULL)
3337 goto done;
3339 error = got_object_id_str(&commit_id_str, commit_id);
3340 if (error)
3341 goto done;
3343 error = got_ref_open(&head_ref, repo,
3344 got_worktree_get_head_ref_name(worktree), 0);
3345 if (error != NULL)
3346 goto done;
3348 error = check_same_branch(commit_id, head_ref, repo);
3349 if (error) {
3350 if (error->code != GOT_ERR_ANCESTRY)
3351 goto done;
3352 error = NULL;
3353 } else {
3354 error = got_error(GOT_ERR_SAME_BRANCH);
3355 goto done;
3358 error = got_object_open_as_commit(&commit, repo, commit_id);
3359 if (error)
3360 goto done;
3361 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3362 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3363 commit_id, repo, update_progress, &did_something, check_cancelled,
3364 NULL);
3365 if (error != NULL)
3366 goto done;
3368 if (did_something)
3369 printf("Merged commit %s\n", commit_id_str);
3370 done:
3371 if (commit)
3372 got_object_commit_close(commit);
3373 free(commit_id_str);
3374 if (head_ref)
3375 got_ref_close(head_ref);
3376 if (worktree)
3377 got_worktree_close(worktree);
3378 if (repo)
3379 got_repo_close(repo);
3380 return error;
3383 __dead static void
3384 usage_backout(void)
3386 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3387 exit(1);
3390 static const struct got_error *
3391 cmd_backout(int argc, char *argv[])
3393 const struct got_error *error = NULL;
3394 struct got_worktree *worktree = NULL;
3395 struct got_repository *repo = NULL;
3396 char *cwd = NULL, *commit_id_str = NULL;
3397 struct got_object_id *commit_id = NULL;
3398 struct got_commit_object *commit = NULL;
3399 struct got_object_qid *pid;
3400 struct got_reference *head_ref = NULL;
3401 int ch, did_something = 0;
3403 while ((ch = getopt(argc, argv, "")) != -1) {
3404 switch (ch) {
3405 default:
3406 usage_backout();
3407 /* NOTREACHED */
3411 argc -= optind;
3412 argv += optind;
3414 #ifndef PROFILE
3415 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3416 "unveil", NULL) == -1)
3417 err(1, "pledge");
3418 #endif
3419 if (argc != 1)
3420 usage_backout();
3422 cwd = getcwd(NULL, 0);
3423 if (cwd == NULL) {
3424 error = got_error_from_errno("getcwd");
3425 goto done;
3427 error = got_worktree_open(&worktree, cwd);
3428 if (error)
3429 goto done;
3431 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3432 if (error != NULL)
3433 goto done;
3435 error = apply_unveil(got_repo_get_path(repo), 0,
3436 got_worktree_get_root_path(worktree), 0);
3437 if (error)
3438 goto done;
3440 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3441 GOT_OBJ_TYPE_COMMIT, repo);
3442 if (error != NULL) {
3443 struct got_reference *ref;
3444 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3445 goto done;
3446 error = got_ref_open(&ref, repo, argv[0], 0);
3447 if (error != NULL)
3448 goto done;
3449 error = got_ref_resolve(&commit_id, repo, ref);
3450 got_ref_close(ref);
3451 if (error != NULL)
3452 goto done;
3454 error = got_object_id_str(&commit_id_str, commit_id);
3455 if (error)
3456 goto done;
3458 error = got_ref_open(&head_ref, repo,
3459 got_worktree_get_head_ref_name(worktree), 0);
3460 if (error != NULL)
3461 goto done;
3463 error = check_same_branch(commit_id, head_ref, repo);
3464 if (error)
3465 goto done;
3467 error = got_object_open_as_commit(&commit, repo, commit_id);
3468 if (error)
3469 goto done;
3470 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3471 if (pid == NULL) {
3472 error = got_error(GOT_ERR_ROOT_COMMIT);
3473 goto done;
3476 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3477 update_progress, &did_something, check_cancelled, NULL);
3478 if (error != NULL)
3479 goto done;
3481 if (did_something)
3482 printf("Backed out commit %s\n", commit_id_str);
3483 done:
3484 if (commit)
3485 got_object_commit_close(commit);
3486 free(commit_id_str);
3487 if (head_ref)
3488 got_ref_close(head_ref);
3489 if (worktree)
3490 got_worktree_close(worktree);
3491 if (repo)
3492 got_repo_close(repo);
3493 return error;
3496 __dead static void
3497 usage_rebase(void)
3499 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3500 getprogname());
3501 exit(1);
3504 static const struct got_error *
3505 show_rebase_progress(struct got_commit_object *commit,
3506 struct got_object_id *old_id, struct got_object_id *new_id)
3508 const struct got_error *err;
3509 char *old_id_str = NULL, *new_id_str = NULL;
3510 char *logmsg0 = NULL, *logmsg, *nl;
3511 size_t len;
3513 err = got_object_id_str(&old_id_str, old_id);
3514 if (err)
3515 goto done;
3517 if (new_id) {
3518 err = got_object_id_str(&new_id_str, new_id);
3519 if (err)
3520 goto done;
3523 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3524 if (logmsg0 == NULL) {
3525 err = got_error_from_errno("strdup");
3526 goto done;
3528 logmsg = logmsg0;
3530 while (isspace((unsigned char)logmsg[0]))
3531 logmsg++;
3533 old_id_str[12] = '\0';
3534 if (new_id_str)
3535 new_id_str[12] = '\0';
3536 len = strlen(logmsg);
3537 if (len > 42)
3538 len = 42;
3539 logmsg[len] = '\0';
3540 nl = strchr(logmsg, '\n');
3541 if (nl)
3542 *nl = '\0';
3543 printf("%s -> %s: %s\n", old_id_str,
3544 new_id_str ? new_id_str : "no-op change", logmsg);
3545 done:
3546 free(old_id_str);
3547 free(new_id_str);
3548 free(logmsg0);
3549 return err;
3552 static const struct got_error *
3553 rebase_progress(void *arg, unsigned char status, const char *path)
3555 unsigned char *rebase_status = arg;
3557 while (path[0] == '/')
3558 path++;
3559 printf("%c %s\n", status, path);
3561 if (*rebase_status == GOT_STATUS_CONFLICT)
3562 return NULL;
3563 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3564 *rebase_status = status;
3565 return NULL;
3568 static const struct got_error *
3569 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3570 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3571 struct got_repository *repo)
3573 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3574 return got_worktree_rebase_complete(worktree,
3575 new_base_branch, tmp_branch, branch, repo);
3578 static const struct got_error *
3579 rebase_commit(struct got_pathlist_head *merged_paths,
3580 struct got_worktree *worktree, struct got_reference *tmp_branch,
3581 struct got_object_id *commit_id, struct got_repository *repo)
3583 const struct got_error *error;
3584 struct got_commit_object *commit;
3585 struct got_object_id *new_commit_id;
3587 error = got_object_open_as_commit(&commit, repo, commit_id);
3588 if (error)
3589 return error;
3591 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3592 worktree, tmp_branch, commit, commit_id, repo);
3593 if (error) {
3594 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3595 goto done;
3596 error = show_rebase_progress(commit, commit_id, NULL);
3597 } else {
3598 error = show_rebase_progress(commit, commit_id, new_commit_id);
3599 free(new_commit_id);
3601 done:
3602 got_object_commit_close(commit);
3603 return error;
3606 struct check_path_prefix_arg {
3607 const char *path_prefix;
3608 size_t len;
3611 static const struct got_error *
3612 check_path_prefix(void *arg, struct got_blob_object *blob1,
3613 struct got_blob_object *blob2, struct got_object_id *id1,
3614 struct got_object_id *id2, const char *path1, const char *path2,
3615 struct got_repository *repo)
3617 struct check_path_prefix_arg *a = arg;
3619 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3620 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3621 return got_error(GOT_ERR_REBASE_PATH);
3623 return NULL;
3626 static const struct got_error *
3627 rebase_check_path_prefix(struct got_object_id *parent_id,
3628 struct got_object_id *commit_id, const char *path_prefix,
3629 struct got_repository *repo)
3631 const struct got_error *err;
3632 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3633 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3634 struct check_path_prefix_arg cpp_arg;
3636 if (got_path_is_root_dir(path_prefix))
3637 return NULL;
3639 err = got_object_open_as_commit(&commit, repo, commit_id);
3640 if (err)
3641 goto done;
3643 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3644 if (err)
3645 goto done;
3647 err = got_object_open_as_tree(&tree1, repo,
3648 got_object_commit_get_tree_id(parent_commit));
3649 if (err)
3650 goto done;
3652 err = got_object_open_as_tree(&tree2, repo,
3653 got_object_commit_get_tree_id(commit));
3654 if (err)
3655 goto done;
3657 cpp_arg.path_prefix = path_prefix;
3658 cpp_arg.len = strlen(path_prefix);
3659 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3660 &cpp_arg);
3661 done:
3662 if (tree1)
3663 got_object_tree_close(tree1);
3664 if (tree2)
3665 got_object_tree_close(tree2);
3666 if (commit)
3667 got_object_commit_close(commit);
3668 if (parent_commit)
3669 got_object_commit_close(parent_commit);
3670 return err;
3673 static const struct got_error *
3674 cmd_rebase(int argc, char *argv[])
3676 const struct got_error *error = NULL;
3677 struct got_worktree *worktree = NULL;
3678 struct got_repository *repo = NULL;
3679 char *cwd = NULL;
3680 struct got_reference *branch = NULL;
3681 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3682 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3683 struct got_object_id *resume_commit_id = NULL;
3684 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3685 struct got_commit_graph *graph = NULL;
3686 struct got_commit_object *commit = NULL;
3687 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3688 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3689 struct got_object_id_queue commits;
3690 struct got_pathlist_head merged_paths;
3691 const struct got_object_id_queue *parent_ids;
3692 struct got_object_qid *qid, *pid;
3694 SIMPLEQ_INIT(&commits);
3695 TAILQ_INIT(&merged_paths);
3697 while ((ch = getopt(argc, argv, "ac")) != -1) {
3698 switch (ch) {
3699 case 'a':
3700 abort_rebase = 1;
3701 break;
3702 case 'c':
3703 continue_rebase = 1;
3704 break;
3705 default:
3706 usage_rebase();
3707 /* NOTREACHED */
3711 argc -= optind;
3712 argv += optind;
3714 #ifndef PROFILE
3715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3716 "unveil", NULL) == -1)
3717 err(1, "pledge");
3718 #endif
3719 if (abort_rebase && continue_rebase)
3720 usage_rebase();
3721 else if (abort_rebase || continue_rebase) {
3722 if (argc != 0)
3723 usage_rebase();
3724 } else if (argc != 1)
3725 usage_rebase();
3727 cwd = getcwd(NULL, 0);
3728 if (cwd == NULL) {
3729 error = got_error_from_errno("getcwd");
3730 goto done;
3732 error = got_worktree_open(&worktree, cwd);
3733 if (error)
3734 goto done;
3736 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3737 if (error != NULL)
3738 goto done;
3740 error = apply_unveil(got_repo_get_path(repo), 0,
3741 got_worktree_get_root_path(worktree), 0);
3742 if (error)
3743 goto done;
3745 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3746 if (error)
3747 goto done;
3749 if (rebase_in_progress && abort_rebase) {
3750 int did_something;
3751 error = got_worktree_rebase_continue(&resume_commit_id,
3752 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3753 if (error)
3754 goto done;
3755 printf("Switching work tree to %s\n",
3756 got_ref_get_symref_target(new_base_branch));
3757 error = got_worktree_rebase_abort(worktree, repo,
3758 new_base_branch, update_progress, &did_something);
3759 if (error)
3760 goto done;
3761 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3762 goto done; /* nothing else to do */
3763 } else if (abort_rebase) {
3764 error = got_error(GOT_ERR_NOT_REBASING);
3765 goto done;
3768 if (continue_rebase) {
3769 error = got_worktree_rebase_continue(&resume_commit_id,
3770 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3771 if (error)
3772 goto done;
3774 error = rebase_commit(NULL, worktree, tmp_branch,
3775 resume_commit_id, repo);
3776 if (error)
3777 goto done;
3779 yca_id = got_object_id_dup(resume_commit_id);
3780 if (yca_id == NULL) {
3781 error = got_error_from_errno("got_object_id_dup");
3782 goto done;
3784 } else {
3785 error = got_ref_open(&branch, repo, argv[0], 0);
3786 if (error != NULL)
3787 goto done;
3789 error = check_same_branch(
3790 got_worktree_get_base_commit_id(worktree), branch, repo);
3791 if (error) {
3792 if (error->code != GOT_ERR_ANCESTRY)
3793 goto done;
3794 error = NULL;
3795 } else {
3796 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3797 "specified branch resolves to a commit which "
3798 "is already contained in work tree's branch");
3799 goto done;
3803 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3804 if (error)
3805 goto done;
3807 if (!continue_rebase) {
3808 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3809 got_worktree_get_base_commit_id(worktree),
3810 branch_head_commit_id, repo);
3811 if (error)
3812 goto done;
3813 if (yca_id == NULL) {
3814 error = got_error_msg(GOT_ERR_ANCESTRY,
3815 "specified branch shares no common ancestry "
3816 "with work tree's branch");
3817 goto done;
3820 error = got_worktree_rebase_prepare(&new_base_branch,
3821 &tmp_branch, worktree, branch, repo);
3822 if (error)
3823 goto done;
3826 commit_id = branch_head_commit_id;
3827 error = got_object_open_as_commit(&commit, repo, commit_id);
3828 if (error)
3829 goto done;
3831 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3832 if (error)
3833 goto done;
3834 parent_ids = got_object_commit_get_parent_ids(commit);
3835 pid = SIMPLEQ_FIRST(parent_ids);
3836 error = got_commit_graph_iter_start(graph, pid->id, repo);
3837 got_object_commit_close(commit);
3838 commit = NULL;
3839 if (error)
3840 goto done;
3841 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3842 error = got_commit_graph_iter_next(&parent_id, graph);
3843 if (error) {
3844 if (error->code == GOT_ERR_ITER_COMPLETED) {
3845 error = got_error_msg(GOT_ERR_ANCESTRY,
3846 "ran out of commits to rebase before "
3847 "youngest common ancestor commit has "
3848 "been reached?!?");
3849 goto done;
3850 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3851 goto done;
3852 error = got_commit_graph_fetch_commits(graph, 1, repo);
3853 if (error)
3854 goto done;
3855 } else {
3856 error = rebase_check_path_prefix(parent_id, commit_id,
3857 got_worktree_get_path_prefix(worktree), repo);
3858 if (error)
3859 goto done;
3861 error = got_object_qid_alloc(&qid, commit_id);
3862 if (error)
3863 goto done;
3864 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3865 commit_id = parent_id;
3868 got_commit_graph_close(graph);
3869 graph = NULL;
3871 if (SIMPLEQ_EMPTY(&commits)) {
3872 if (continue_rebase)
3873 error = rebase_complete(worktree, branch,
3874 new_base_branch, tmp_branch, repo);
3875 else
3876 error = got_error(GOT_ERR_EMPTY_REBASE);
3877 goto done;
3880 pid = NULL;
3881 SIMPLEQ_FOREACH(qid, &commits, entry) {
3882 commit_id = qid->id;
3883 parent_id = pid ? pid->id : yca_id;
3884 pid = qid;
3886 error = got_worktree_rebase_merge_files(&merged_paths,
3887 worktree, parent_id, commit_id, repo, rebase_progress,
3888 &rebase_status, check_cancelled, NULL);
3889 if (error)
3890 goto done;
3892 if (rebase_status == GOT_STATUS_CONFLICT) {
3893 got_worktree_rebase_pathlist_free(&merged_paths);
3894 break;
3897 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3898 commit_id, repo);
3899 got_worktree_rebase_pathlist_free(&merged_paths);
3900 if (error)
3901 goto done;
3904 if (rebase_status == GOT_STATUS_CONFLICT) {
3905 error = got_worktree_rebase_postpone(worktree);
3906 if (error)
3907 goto done;
3908 error = got_error_msg(GOT_ERR_CONFLICTS,
3909 "conflicts must be resolved before rebasing can continue");
3910 } else
3911 error = rebase_complete(worktree, branch, new_base_branch,
3912 tmp_branch, repo);
3913 done:
3914 got_object_id_queue_free(&commits);
3915 free(branch_head_commit_id);
3916 free(resume_commit_id);
3917 free(yca_id);
3918 if (graph)
3919 got_commit_graph_close(graph);
3920 if (commit)
3921 got_object_commit_close(commit);
3922 if (branch)
3923 got_ref_close(branch);
3924 if (new_base_branch)
3925 got_ref_close(new_base_branch);
3926 if (tmp_branch)
3927 got_ref_close(tmp_branch);
3928 if (worktree)
3929 got_worktree_close(worktree);
3930 if (repo)
3931 got_repo_close(repo);
3932 return error;