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;
753 else if (err->code != GOT_ERR_ITER_NEED_MORE)
754 break;
755 err = got_commit_graph_fetch_commits(graph, 1,
756 repo);
757 if (err)
758 break;
761 if (id) {
762 if (got_object_id_cmp(id, commit_id) == 0) {
763 is_same_branch = 1;
764 break;
768 done:
769 if (graph)
770 got_commit_graph_close(graph);
771 free(head_commit_id);
772 if (!err && !is_same_branch)
773 err = got_error(GOT_ERR_ANCESTRY);
774 return err;
777 static const struct got_error *
778 cmd_checkout(int argc, char *argv[])
780 const struct got_error *error = NULL;
781 struct got_repository *repo = NULL;
782 struct got_reference *head_ref = NULL;
783 struct got_worktree *worktree = NULL;
784 char *repo_path = NULL;
785 char *worktree_path = NULL;
786 const char *path_prefix = "";
787 const char *branch_name = GOT_REF_HEAD;
788 char *commit_id_str = NULL;
789 int ch, same_path_prefix;
791 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
792 switch (ch) {
793 case 'b':
794 branch_name = optarg;
795 break;
796 case 'c':
797 commit_id_str = strdup(optarg);
798 if (commit_id_str == NULL)
799 return got_error_from_errno("strdup");
800 break;
801 case 'p':
802 path_prefix = optarg;
803 break;
804 default:
805 usage_checkout();
806 /* NOTREACHED */
810 argc -= optind;
811 argv += optind;
813 #ifndef PROFILE
814 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
815 "unveil", NULL) == -1)
816 err(1, "pledge");
817 #endif
818 if (argc == 1) {
819 char *cwd, *base, *dotgit;
820 repo_path = realpath(argv[0], NULL);
821 if (repo_path == NULL)
822 return got_error_from_errno2("realpath", argv[0]);
823 cwd = getcwd(NULL, 0);
824 if (cwd == NULL) {
825 error = got_error_from_errno("getcwd");
826 goto done;
828 if (path_prefix[0]) {
829 base = basename(path_prefix);
830 if (base == NULL) {
831 error = got_error_from_errno2("basename",
832 path_prefix);
833 goto done;
835 } else {
836 base = basename(repo_path);
837 if (base == NULL) {
838 error = got_error_from_errno2("basename",
839 repo_path);
840 goto done;
843 dotgit = strstr(base, ".git");
844 if (dotgit)
845 *dotgit = '\0';
846 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
847 error = got_error_from_errno("asprintf");
848 free(cwd);
849 goto done;
851 free(cwd);
852 } else if (argc == 2) {
853 repo_path = realpath(argv[0], NULL);
854 if (repo_path == NULL) {
855 error = got_error_from_errno2("realpath", argv[0]);
856 goto done;
858 worktree_path = realpath(argv[1], NULL);
859 if (worktree_path == NULL) {
860 error = got_error_from_errno2("realpath", argv[1]);
861 goto done;
863 } else
864 usage_checkout();
866 got_path_strip_trailing_slashes(repo_path);
867 got_path_strip_trailing_slashes(worktree_path);
869 error = got_repo_open(&repo, repo_path);
870 if (error != NULL)
871 goto done;
873 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
874 if (error)
875 goto done;
877 error = got_ref_open(&head_ref, repo, branch_name, 0);
878 if (error != NULL)
879 goto done;
881 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
882 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
883 goto done;
885 error = got_worktree_open(&worktree, worktree_path);
886 if (error != NULL)
887 goto done;
889 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
890 path_prefix);
891 if (error != NULL)
892 goto done;
893 if (!same_path_prefix) {
894 error = got_error(GOT_ERR_PATH_PREFIX);
895 goto done;
898 if (commit_id_str) {
899 struct got_object_id *commit_id;
900 error = got_repo_match_object_id_prefix(&commit_id,
901 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
902 if (error != NULL)
903 goto done;
904 error = check_linear_ancestry(commit_id,
905 got_worktree_get_base_commit_id(worktree), repo);
906 if (error != NULL) {
907 free(commit_id);
908 goto done;
910 error = check_same_branch(commit_id, head_ref, repo);
911 if (error)
912 goto done;
913 error = got_worktree_set_base_commit_id(worktree, repo,
914 commit_id);
915 free(commit_id);
916 if (error)
917 goto done;
920 error = got_worktree_checkout_files(worktree, "", repo,
921 checkout_progress, worktree_path, check_cancelled, NULL);
922 if (error != NULL)
923 goto done;
925 printf("Now shut up and hack\n");
927 done:
928 free(commit_id_str);
929 free(repo_path);
930 free(worktree_path);
931 return error;
934 __dead static void
935 usage_update(void)
937 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
938 getprogname());
939 exit(1);
942 static const struct got_error *
943 update_progress(void *arg, unsigned char status, const char *path)
945 int *did_something = arg;
947 if (status == GOT_STATUS_EXISTS)
948 return NULL;
950 *did_something = 1;
952 /* Base commit bump happens silently. */
953 if (status == GOT_STATUS_BUMP_BASE)
954 return NULL;
956 while (path[0] == '/')
957 path++;
958 printf("%c %s\n", status, path);
959 return NULL;
962 static const struct got_error *
963 switch_head_ref(struct got_reference *head_ref,
964 struct got_object_id *commit_id, struct got_worktree *worktree,
965 struct got_repository *repo)
967 const struct got_error *err = NULL;
968 char *base_id_str;
969 int ref_has_moved = 0;
971 /* Trivial case: switching between two different references. */
972 if (strcmp(got_ref_get_name(head_ref),
973 got_worktree_get_head_ref_name(worktree)) != 0) {
974 printf("Switching work tree from %s to %s\n",
975 got_worktree_get_head_ref_name(worktree),
976 got_ref_get_name(head_ref));
977 return got_worktree_set_head_ref(worktree, head_ref);
980 err = check_linear_ancestry(commit_id,
981 got_worktree_get_base_commit_id(worktree), repo);
982 if (err) {
983 if (err->code != GOT_ERR_ANCESTRY)
984 return err;
985 ref_has_moved = 1;
987 if (!ref_has_moved)
988 return NULL;
990 /* Switching to a rebased branch with the same reference name. */
991 err = got_object_id_str(&base_id_str,
992 got_worktree_get_base_commit_id(worktree));
993 if (err)
994 return err;
995 printf("Reference %s now points at a different branch\n",
996 got_worktree_get_head_ref_name(worktree));
997 printf("Switching work tree from %s to %s\n", base_id_str,
998 got_worktree_get_head_ref_name(worktree));
999 return NULL;
1002 static const struct got_error *
1003 cmd_update(int argc, char *argv[])
1005 const struct got_error *error = NULL;
1006 struct got_repository *repo = NULL;
1007 struct got_worktree *worktree = NULL;
1008 char *worktree_path = NULL, *path = NULL;
1009 struct got_object_id *commit_id = NULL;
1010 char *commit_id_str = NULL;
1011 const char *branch_name = NULL;
1012 struct got_reference *head_ref = NULL;
1013 int ch, did_something = 0, rebase_in_progress;
1015 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1016 switch (ch) {
1017 case 'b':
1018 branch_name = optarg;
1019 break;
1020 case 'c':
1021 commit_id_str = strdup(optarg);
1022 if (commit_id_str == NULL)
1023 return got_error_from_errno("strdup");
1024 break;
1025 default:
1026 usage_update();
1027 /* NOTREACHED */
1031 argc -= optind;
1032 argv += optind;
1034 #ifndef PROFILE
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1037 err(1, "pledge");
1038 #endif
1039 worktree_path = getcwd(NULL, 0);
1040 if (worktree_path == NULL) {
1041 error = got_error_from_errno("getcwd");
1042 goto done;
1044 error = got_worktree_open(&worktree, worktree_path);
1045 if (error)
1046 goto done;
1048 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1049 if (error)
1050 goto done;
1051 if (rebase_in_progress) {
1052 error = got_error(GOT_ERR_REBASING);
1053 goto done;
1056 if (argc == 0) {
1057 path = strdup("");
1058 if (path == NULL) {
1059 error = got_error_from_errno("strdup");
1060 goto done;
1062 } else if (argc == 1) {
1063 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1064 if (error)
1065 goto done;
1066 } else
1067 usage_update();
1069 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1070 if (error != NULL)
1071 goto done;
1073 error = apply_unveil(got_repo_get_path(repo), 0,
1074 got_worktree_get_root_path(worktree), 0);
1075 if (error)
1076 goto done;
1078 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1079 got_worktree_get_head_ref_name(worktree), 0);
1080 if (error != NULL)
1081 goto done;
1082 if (commit_id_str == NULL) {
1083 error = got_ref_resolve(&commit_id, repo, head_ref);
1084 if (error != NULL)
1085 goto done;
1086 error = got_object_id_str(&commit_id_str, commit_id);
1087 if (error != NULL)
1088 goto done;
1089 } else {
1090 error = got_repo_match_object_id_prefix(&commit_id,
1091 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1092 if (error != NULL)
1093 goto done;
1094 free(commit_id_str);
1095 error = got_object_id_str(&commit_id_str, commit_id);
1096 if (error)
1097 goto done;
1100 if (branch_name) {
1101 struct got_object_id *head_commit_id;
1102 if (strlen(path) != 0) {
1103 fprintf(stderr, "%s: switching between branches "
1104 "requires that the entire work tree "
1105 "gets updated, not just '%s'\n",
1106 getprogname(), path);
1107 error = got_error(GOT_ERR_BAD_PATH);
1108 goto done;
1110 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1111 if (error)
1112 goto done;
1113 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1114 free(head_commit_id);
1115 if (error != NULL)
1116 goto done;
1117 error = check_same_branch(commit_id, head_ref, repo);
1118 if (error)
1119 goto done;
1120 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1121 if (error)
1122 goto done;
1123 } else {
1124 error = check_linear_ancestry(commit_id,
1125 got_worktree_get_base_commit_id(worktree), repo);
1126 if (error != NULL) {
1127 if (error->code == GOT_ERR_ANCESTRY)
1128 error = got_error(GOT_ERR_BRANCH_MOVED);
1129 goto done;
1131 error = check_same_branch(commit_id, head_ref, repo);
1132 if (error)
1133 goto done;
1136 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1137 commit_id) != 0) {
1138 error = got_worktree_set_base_commit_id(worktree, repo,
1139 commit_id);
1140 if (error)
1141 goto done;
1144 error = got_worktree_checkout_files(worktree, path, repo,
1145 update_progress, &did_something, check_cancelled, NULL);
1146 if (error != NULL)
1147 goto done;
1149 if (did_something)
1150 printf("Updated to commit %s\n", commit_id_str);
1151 else
1152 printf("Already up-to-date\n");
1153 done:
1154 free(worktree_path);
1155 free(path);
1156 free(commit_id);
1157 free(commit_id_str);
1158 return error;
1161 static const struct got_error *
1162 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1163 int diff_context, struct got_repository *repo)
1165 const struct got_error *err = NULL;
1166 struct got_tree_object *tree1 = NULL, *tree2;
1167 struct got_object_qid *qid;
1168 char *id_str1 = NULL, *id_str2;
1169 struct got_diff_blob_output_unidiff_arg arg;
1171 err = got_object_open_as_tree(&tree2, repo,
1172 got_object_commit_get_tree_id(commit));
1173 if (err)
1174 return err;
1176 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1177 if (qid != NULL) {
1178 struct got_commit_object *pcommit;
1180 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1181 if (err)
1182 return err;
1184 err = got_object_open_as_tree(&tree1, repo,
1185 got_object_commit_get_tree_id(pcommit));
1186 got_object_commit_close(pcommit);
1187 if (err)
1188 return err;
1190 err = got_object_id_str(&id_str1, qid->id);
1191 if (err)
1192 return err;
1195 err = got_object_id_str(&id_str2, id);
1196 if (err)
1197 goto done;
1199 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1200 arg.diff_context = diff_context;
1201 arg.outfile = stdout;
1202 err = got_diff_tree(tree1, tree2, "", "", repo,
1203 got_diff_blob_output_unidiff, &arg);
1204 done:
1205 if (tree1)
1206 got_object_tree_close(tree1);
1207 got_object_tree_close(tree2);
1208 free(id_str1);
1209 free(id_str2);
1210 return err;
1213 static char *
1214 get_datestr(time_t *time, char *datebuf)
1216 char *p, *s = ctime_r(time, datebuf);
1217 p = strchr(s, '\n');
1218 if (p)
1219 *p = '\0';
1220 return s;
1223 static const struct got_error *
1224 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1225 struct got_repository *repo, int show_patch, int diff_context,
1226 struct got_reflist_head *refs)
1228 const struct got_error *err = NULL;
1229 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1230 char datebuf[26];
1231 time_t committer_time;
1232 const char *author, *committer;
1233 char *refs_str = NULL;
1234 struct got_reflist_entry *re;
1236 SIMPLEQ_FOREACH(re, refs, entry) {
1237 char *s;
1238 const char *name;
1239 if (got_object_id_cmp(re->id, id) != 0)
1240 continue;
1241 name = got_ref_get_name(re->ref);
1242 if (strcmp(name, GOT_REF_HEAD) == 0)
1243 continue;
1244 if (strncmp(name, "refs/", 5) == 0)
1245 name += 5;
1246 if (strncmp(name, "got/", 4) == 0)
1247 continue;
1248 if (strncmp(name, "heads/", 6) == 0)
1249 name += 6;
1250 if (strncmp(name, "remotes/", 8) == 0)
1251 name += 8;
1252 s = refs_str;
1253 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1254 name) == -1) {
1255 err = got_error_from_errno("asprintf");
1256 free(s);
1257 break;
1259 free(s);
1261 err = got_object_id_str(&id_str, id);
1262 if (err)
1263 return err;
1265 printf("-----------------------------------------------\n");
1266 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1267 refs_str ? refs_str : "", refs_str ? ")" : "");
1268 free(id_str);
1269 id_str = NULL;
1270 free(refs_str);
1271 refs_str = NULL;
1272 printf("from: %s\n", got_object_commit_get_author(commit));
1273 committer_time = got_object_commit_get_committer_time(commit);
1274 datestr = get_datestr(&committer_time, datebuf);
1275 printf("date: %s UTC\n", datestr);
1276 author = got_object_commit_get_author(commit);
1277 committer = got_object_commit_get_committer(commit);
1278 if (strcmp(author, committer) != 0)
1279 printf("via: %s\n", committer);
1280 if (got_object_commit_get_nparents(commit) > 1) {
1281 const struct got_object_id_queue *parent_ids;
1282 struct got_object_qid *qid;
1283 int n = 1;
1284 parent_ids = got_object_commit_get_parent_ids(commit);
1285 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1286 err = got_object_id_str(&id_str, qid->id);
1287 if (err)
1288 return err;
1289 printf("parent %d: %s\n", n++, id_str);
1290 free(id_str);
1294 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1295 if (logmsg0 == NULL)
1296 return got_error_from_errno("strdup");
1298 logmsg = logmsg0;
1299 do {
1300 line = strsep(&logmsg, "\n");
1301 if (line)
1302 printf(" %s\n", line);
1303 } while (line);
1304 free(logmsg0);
1306 if (show_patch) {
1307 err = print_patch(commit, id, diff_context, repo);
1308 if (err == 0)
1309 printf("\n");
1312 if (fflush(stdout) != 0 && err == NULL)
1313 err = got_error_from_errno("fflush");
1314 return err;
1317 static const struct got_error *
1318 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1319 char *path, int show_patch, int diff_context, int limit,
1320 int first_parent_traversal, struct got_reflist_head *refs)
1322 const struct got_error *err;
1323 struct got_commit_graph *graph;
1325 err = got_commit_graph_open(&graph, root_id, path,
1326 first_parent_traversal, repo);
1327 if (err)
1328 return err;
1329 err = got_commit_graph_iter_start(graph, root_id, repo);
1330 if (err)
1331 goto done;
1332 for (;;) {
1333 struct got_commit_object *commit;
1334 struct got_object_id *id;
1336 if (sigint_received || sigpipe_received)
1337 break;
1339 err = got_commit_graph_iter_next(&id, graph);
1340 if (err) {
1341 if (err->code == GOT_ERR_ITER_COMPLETED) {
1342 err = NULL;
1343 break;
1345 if (err->code != GOT_ERR_ITER_NEED_MORE)
1346 break;
1347 err = got_commit_graph_fetch_commits(graph, 1, repo);
1348 if (err)
1349 break;
1350 else
1351 continue;
1353 if (id == NULL)
1354 break;
1356 err = got_object_open_as_commit(&commit, repo, id);
1357 if (err)
1358 break;
1359 err = print_commit(commit, id, repo, show_patch, diff_context,
1360 refs);
1361 got_object_commit_close(commit);
1362 if (err || (limit && --limit == 0))
1363 break;
1365 done:
1366 got_commit_graph_close(graph);
1367 return err;
1370 __dead static void
1371 usage_log(void)
1373 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1374 "[-r repository-path] [path]\n", getprogname());
1375 exit(1);
1378 static const struct got_error *
1379 cmd_log(int argc, char *argv[])
1381 const struct got_error *error;
1382 struct got_repository *repo = NULL;
1383 struct got_worktree *worktree = NULL;
1384 struct got_commit_object *commit = NULL;
1385 struct got_object_id *id = NULL;
1386 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1387 char *start_commit = NULL;
1388 int diff_context = 3, ch;
1389 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1390 const char *errstr;
1391 struct got_reflist_head refs;
1393 SIMPLEQ_INIT(&refs);
1395 #ifndef PROFILE
1396 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1397 NULL)
1398 == -1)
1399 err(1, "pledge");
1400 #endif
1402 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1403 switch (ch) {
1404 case 'p':
1405 show_patch = 1;
1406 break;
1407 case 'c':
1408 start_commit = optarg;
1409 break;
1410 case 'C':
1411 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1412 &errstr);
1413 if (errstr != NULL)
1414 err(1, "-C option %s", errstr);
1415 break;
1416 case 'l':
1417 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1418 if (errstr != NULL)
1419 err(1, "-l option %s", errstr);
1420 break;
1421 case 'f':
1422 first_parent_traversal = 1;
1423 break;
1424 case 'r':
1425 repo_path = realpath(optarg, NULL);
1426 if (repo_path == NULL)
1427 err(1, "-r option");
1428 got_path_strip_trailing_slashes(repo_path);
1429 break;
1430 default:
1431 usage_log();
1432 /* NOTREACHED */
1436 argc -= optind;
1437 argv += optind;
1439 cwd = getcwd(NULL, 0);
1440 if (cwd == NULL) {
1441 error = got_error_from_errno("getcwd");
1442 goto done;
1445 error = got_worktree_open(&worktree, cwd);
1446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1447 goto done;
1448 error = NULL;
1450 if (argc == 0) {
1451 path = strdup("");
1452 if (path == NULL) {
1453 error = got_error_from_errno("strdup");
1454 goto done;
1456 } else if (argc == 1) {
1457 if (worktree) {
1458 error = got_worktree_resolve_path(&path, worktree,
1459 argv[0]);
1460 if (error)
1461 goto done;
1462 } else {
1463 path = strdup(argv[0]);
1464 if (path == NULL) {
1465 error = got_error_from_errno("strdup");
1466 goto done;
1469 } else
1470 usage_log();
1472 if (repo_path == NULL) {
1473 repo_path = worktree ?
1474 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1476 if (repo_path == NULL) {
1477 error = got_error_from_errno("strdup");
1478 goto done;
1481 error = got_repo_open(&repo, repo_path);
1482 if (error != NULL)
1483 goto done;
1485 error = apply_unveil(got_repo_get_path(repo), 1,
1486 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1487 if (error)
1488 goto done;
1490 if (start_commit == NULL) {
1491 struct got_reference *head_ref;
1492 error = got_ref_open(&head_ref, repo,
1493 worktree ? got_worktree_get_head_ref_name(worktree)
1494 : GOT_REF_HEAD, 0);
1495 if (error != NULL)
1496 return error;
1497 error = got_ref_resolve(&id, repo, head_ref);
1498 got_ref_close(head_ref);
1499 if (error != NULL)
1500 return error;
1501 error = got_object_open_as_commit(&commit, repo, id);
1502 } else {
1503 struct got_reference *ref;
1504 error = got_ref_open(&ref, repo, start_commit, 0);
1505 if (error == NULL) {
1506 int obj_type;
1507 error = got_ref_resolve(&id, repo, ref);
1508 got_ref_close(ref);
1509 if (error != NULL)
1510 goto done;
1511 error = got_object_get_type(&obj_type, repo, id);
1512 if (error != NULL)
1513 goto done;
1514 if (obj_type == GOT_OBJ_TYPE_TAG) {
1515 struct got_tag_object *tag;
1516 error = got_object_open_as_tag(&tag, repo, id);
1517 if (error != NULL)
1518 goto done;
1519 if (got_object_tag_get_object_type(tag) !=
1520 GOT_OBJ_TYPE_COMMIT) {
1521 got_object_tag_close(tag);
1522 error = got_error(GOT_ERR_OBJ_TYPE);
1523 goto done;
1525 free(id);
1526 id = got_object_id_dup(
1527 got_object_tag_get_object_id(tag));
1528 if (id == NULL)
1529 error = got_error_from_errno(
1530 "got_object_id_dup");
1531 got_object_tag_close(tag);
1532 if (error)
1533 goto done;
1534 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1535 error = got_error(GOT_ERR_OBJ_TYPE);
1536 goto done;
1538 error = got_object_open_as_commit(&commit, repo, id);
1539 if (error != NULL)
1540 goto done;
1542 if (commit == NULL) {
1543 error = got_repo_match_object_id_prefix(&id,
1544 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1545 if (error != NULL)
1546 return error;
1549 if (error != NULL)
1550 goto done;
1552 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1553 if (error != NULL)
1554 goto done;
1555 if (in_repo_path) {
1556 free(path);
1557 path = in_repo_path;
1560 error = got_ref_list(&refs, repo);
1561 if (error)
1562 goto done;
1564 error = print_commits(id, repo, path, show_patch,
1565 diff_context, limit, first_parent_traversal, &refs);
1566 done:
1567 free(path);
1568 free(repo_path);
1569 free(cwd);
1570 free(id);
1571 if (worktree)
1572 got_worktree_close(worktree);
1573 if (repo) {
1574 const struct got_error *repo_error;
1575 repo_error = got_repo_close(repo);
1576 if (error == NULL)
1577 error = repo_error;
1579 got_ref_list_free(&refs);
1580 return error;
1583 __dead static void
1584 usage_diff(void)
1586 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1587 "[object1 object2 | path]\n", getprogname());
1588 exit(1);
1591 struct print_diff_arg {
1592 struct got_repository *repo;
1593 struct got_worktree *worktree;
1594 int diff_context;
1595 const char *id_str;
1596 int header_shown;
1599 static const struct got_error *
1600 print_diff(void *arg, unsigned char status, const char *path,
1601 struct got_object_id *blob_id, struct got_object_id *commit_id)
1603 struct print_diff_arg *a = arg;
1604 const struct got_error *err = NULL;
1605 struct got_blob_object *blob1 = NULL;
1606 FILE *f2 = NULL;
1607 char *abspath = NULL;
1608 struct stat sb;
1610 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1611 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1612 return NULL;
1614 if (!a->header_shown) {
1615 printf("diff %s %s\n", a->id_str,
1616 got_worktree_get_root_path(a->worktree));
1617 a->header_shown = 1;
1620 if (status != GOT_STATUS_ADD) {
1621 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1622 if (err)
1623 goto done;
1627 if (status != GOT_STATUS_DELETE) {
1628 if (asprintf(&abspath, "%s/%s",
1629 got_worktree_get_root_path(a->worktree), path) == -1) {
1630 err = got_error_from_errno("asprintf");
1631 goto done;
1634 f2 = fopen(abspath, "r");
1635 if (f2 == NULL) {
1636 err = got_error_from_errno2("fopen", abspath);
1637 goto done;
1639 if (lstat(abspath, &sb) == -1) {
1640 err = got_error_from_errno2("lstat", abspath);
1641 goto done;
1643 } else
1644 sb.st_size = 0;
1646 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1647 stdout);
1648 done:
1649 if (blob1)
1650 got_object_blob_close(blob1);
1651 if (f2 && fclose(f2) != 0 && err == NULL)
1652 err = got_error_from_errno("fclose");
1653 free(abspath);
1654 return err;
1657 static const struct got_error *
1658 cmd_diff(int argc, char *argv[])
1660 const struct got_error *error;
1661 struct got_repository *repo = NULL;
1662 struct got_worktree *worktree = NULL;
1663 char *cwd = NULL, *repo_path = NULL;
1664 struct got_object_id *id1 = NULL, *id2 = NULL;
1665 const char *id_str1 = NULL, *id_str2 = NULL;
1666 char *label1 = NULL, *label2 = NULL;
1667 int type1, type2;
1668 int diff_context = 3, ch;
1669 const char *errstr;
1670 char *path = NULL;
1672 #ifndef PROFILE
1673 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1674 NULL) == -1)
1675 err(1, "pledge");
1676 #endif
1678 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1679 switch (ch) {
1680 case 'C':
1681 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1682 if (errstr != NULL)
1683 err(1, "-C option %s", errstr);
1684 break;
1685 case 'r':
1686 repo_path = realpath(optarg, NULL);
1687 if (repo_path == NULL)
1688 err(1, "-r option");
1689 got_path_strip_trailing_slashes(repo_path);
1690 break;
1691 default:
1692 usage_diff();
1693 /* NOTREACHED */
1697 argc -= optind;
1698 argv += optind;
1700 cwd = getcwd(NULL, 0);
1701 if (cwd == NULL) {
1702 error = got_error_from_errno("getcwd");
1703 goto done;
1705 error = got_worktree_open(&worktree, cwd);
1706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1707 goto done;
1708 if (argc <= 1) {
1709 if (worktree == NULL) {
1710 error = got_error(GOT_ERR_NOT_WORKTREE);
1711 goto done;
1713 if (repo_path)
1714 errx(1,
1715 "-r option can't be used when diffing a work tree");
1716 repo_path = strdup(got_worktree_get_repo_path(worktree));
1717 if (repo_path == NULL) {
1718 error = got_error_from_errno("strdup");
1719 goto done;
1721 if (argc == 1) {
1722 error = got_worktree_resolve_path(&path, worktree,
1723 argv[0]);
1724 if (error)
1725 goto done;
1726 } else {
1727 path = strdup("");
1728 if (path == NULL) {
1729 error = got_error_from_errno("strdup");
1730 goto done;
1733 } else if (argc == 2) {
1734 id_str1 = argv[0];
1735 id_str2 = argv[1];
1736 if (worktree && repo_path == NULL) {
1737 repo_path =
1738 strdup(got_worktree_get_repo_path(worktree));
1739 if (repo_path == NULL) {
1740 error = got_error_from_errno("strdup");
1741 goto done;
1744 } else
1745 usage_diff();
1747 if (repo_path == NULL) {
1748 repo_path = getcwd(NULL, 0);
1749 if (repo_path == NULL)
1750 return got_error_from_errno("getcwd");
1753 error = got_repo_open(&repo, repo_path);
1754 free(repo_path);
1755 if (error != NULL)
1756 goto done;
1758 error = apply_unveil(got_repo_get_path(repo), 1,
1759 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1760 if (error)
1761 goto done;
1763 if (argc <= 1) {
1764 struct print_diff_arg arg;
1765 char *id_str;
1766 error = got_object_id_str(&id_str,
1767 got_worktree_get_base_commit_id(worktree));
1768 if (error)
1769 goto done;
1770 arg.repo = repo;
1771 arg.worktree = worktree;
1772 arg.diff_context = diff_context;
1773 arg.id_str = id_str;
1774 arg.header_shown = 0;
1776 error = got_worktree_status(worktree, path, repo, print_diff,
1777 &arg, check_cancelled, NULL);
1778 free(id_str);
1779 goto done;
1782 error = got_repo_match_object_id_prefix(&id1, id_str1,
1783 GOT_OBJ_TYPE_ANY, repo);
1784 if (error) {
1785 struct got_reference *ref;
1786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1787 goto done;
1788 error = got_ref_open(&ref, repo, id_str1, 0);
1789 if (error != NULL)
1790 goto done;
1791 label1 = strdup(got_ref_get_name(ref));
1792 if (label1 == NULL) {
1793 error = got_error_from_errno("strdup");
1794 goto done;
1796 error = got_ref_resolve(&id1, repo, ref);
1797 got_ref_close(ref);
1798 if (error != NULL)
1799 goto done;
1800 } else {
1801 error = got_object_id_str(&label1, id1);
1802 if (label1 == NULL) {
1803 error = got_error_from_errno("strdup");
1804 goto done;
1808 error = got_repo_match_object_id_prefix(&id2, id_str2,
1809 GOT_OBJ_TYPE_ANY, repo);
1810 if (error) {
1811 struct got_reference *ref;
1812 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1813 goto done;
1814 error = got_ref_open(&ref, repo, id_str2, 0);
1815 if (error != NULL)
1816 goto done;
1817 label2 = strdup(got_ref_get_name(ref));
1818 if (label2 == NULL) {
1819 error = got_error_from_errno("strdup");
1820 goto done;
1822 error = got_ref_resolve(&id2, repo, ref);
1823 got_ref_close(ref);
1824 if (error != NULL)
1825 goto done;
1826 } else {
1827 error = got_object_id_str(&label2, id2);
1828 if (label2 == NULL) {
1829 error = got_error_from_errno("strdup");
1830 goto done;
1834 error = got_object_get_type(&type1, repo, id1);
1835 if (error)
1836 goto done;
1838 error = got_object_get_type(&type2, repo, id2);
1839 if (error)
1840 goto done;
1842 if (type1 != type2) {
1843 error = got_error(GOT_ERR_OBJ_TYPE);
1844 goto done;
1847 switch (type1) {
1848 case GOT_OBJ_TYPE_BLOB:
1849 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1850 diff_context, repo, stdout);
1851 break;
1852 case GOT_OBJ_TYPE_TREE:
1853 error = got_diff_objects_as_trees(id1, id2, "", "",
1854 diff_context, repo, stdout);
1855 break;
1856 case GOT_OBJ_TYPE_COMMIT:
1857 printf("diff %s %s\n", label1, label2);
1858 error = got_diff_objects_as_commits(id1, id2, diff_context,
1859 repo, stdout);
1860 break;
1861 default:
1862 error = got_error(GOT_ERR_OBJ_TYPE);
1865 done:
1866 free(label1);
1867 free(label2);
1868 free(id1);
1869 free(id2);
1870 free(path);
1871 if (worktree)
1872 got_worktree_close(worktree);
1873 if (repo) {
1874 const struct got_error *repo_error;
1875 repo_error = got_repo_close(repo);
1876 if (error == NULL)
1877 error = repo_error;
1879 return error;
1882 __dead static void
1883 usage_blame(void)
1885 fprintf(stderr,
1886 "usage: %s blame [-c commit] [-r repository-path] path\n",
1887 getprogname());
1888 exit(1);
1891 static const struct got_error *
1892 cmd_blame(int argc, char *argv[])
1894 const struct got_error *error;
1895 struct got_repository *repo = NULL;
1896 struct got_worktree *worktree = NULL;
1897 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1898 struct got_object_id *commit_id = NULL;
1899 char *commit_id_str = NULL;
1900 int ch;
1902 #ifndef PROFILE
1903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1904 NULL) == -1)
1905 err(1, "pledge");
1906 #endif
1908 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1909 switch (ch) {
1910 case 'c':
1911 commit_id_str = optarg;
1912 break;
1913 case 'r':
1914 repo_path = realpath(optarg, NULL);
1915 if (repo_path == NULL)
1916 err(1, "-r option");
1917 got_path_strip_trailing_slashes(repo_path);
1918 break;
1919 default:
1920 usage_blame();
1921 /* NOTREACHED */
1925 argc -= optind;
1926 argv += optind;
1928 if (argc == 1)
1929 path = argv[0];
1930 else
1931 usage_blame();
1933 cwd = getcwd(NULL, 0);
1934 if (cwd == NULL) {
1935 error = got_error_from_errno("getcwd");
1936 goto done;
1938 if (repo_path == NULL) {
1939 error = got_worktree_open(&worktree, cwd);
1940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1941 goto done;
1942 else
1943 error = NULL;
1944 if (worktree) {
1945 repo_path =
1946 strdup(got_worktree_get_repo_path(worktree));
1947 if (repo_path == NULL)
1948 error = got_error_from_errno("strdup");
1949 if (error)
1950 goto done;
1951 } else {
1952 repo_path = strdup(cwd);
1953 if (repo_path == NULL) {
1954 error = got_error_from_errno("strdup");
1955 goto done;
1960 error = got_repo_open(&repo, repo_path);
1961 if (error != NULL)
1962 goto done;
1964 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1965 if (error)
1966 goto done;
1968 if (worktree) {
1969 const char *prefix = got_worktree_get_path_prefix(worktree);
1970 char *p, *worktree_subdir = cwd +
1971 strlen(got_worktree_get_root_path(worktree));
1972 if (asprintf(&p, "%s%s%s%s%s",
1973 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1974 worktree_subdir, worktree_subdir[0] ? "/" : "",
1975 path) == -1) {
1976 error = got_error_from_errno("asprintf");
1977 goto done;
1979 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1980 free(p);
1981 } else {
1982 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1984 if (error)
1985 goto done;
1987 if (commit_id_str == NULL) {
1988 struct got_reference *head_ref;
1989 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1990 if (error != NULL)
1991 goto done;
1992 error = got_ref_resolve(&commit_id, repo, head_ref);
1993 got_ref_close(head_ref);
1994 if (error != NULL)
1995 goto done;
1996 } else {
1997 error = got_repo_match_object_id_prefix(&commit_id,
1998 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1999 if (error != NULL)
2000 goto done;
2003 error = got_blame(in_repo_path, commit_id, repo, stdout);
2004 done:
2005 free(in_repo_path);
2006 free(repo_path);
2007 free(cwd);
2008 free(commit_id);
2009 if (worktree)
2010 got_worktree_close(worktree);
2011 if (repo) {
2012 const struct got_error *repo_error;
2013 repo_error = got_repo_close(repo);
2014 if (error == NULL)
2015 error = repo_error;
2017 return error;
2020 __dead static void
2021 usage_tree(void)
2023 fprintf(stderr,
2024 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2025 getprogname());
2026 exit(1);
2029 static void
2030 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2031 const char *root_path)
2033 int is_root_path = (strcmp(path, root_path) == 0);
2035 path += strlen(root_path);
2036 while (path[0] == '/')
2037 path++;
2039 printf("%s%s%s%s%s\n", id ? id : "", path,
2040 is_root_path ? "" : "/", te->name,
2041 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2044 static const struct got_error *
2045 print_tree(const char *path, struct got_object_id *commit_id,
2046 int show_ids, int recurse, const char *root_path,
2047 struct got_repository *repo)
2049 const struct got_error *err = NULL;
2050 struct got_object_id *tree_id = NULL;
2051 struct got_tree_object *tree = NULL;
2052 const struct got_tree_entries *entries;
2053 struct got_tree_entry *te;
2055 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2056 if (err)
2057 goto done;
2059 err = got_object_open_as_tree(&tree, repo, tree_id);
2060 if (err)
2061 goto done;
2062 entries = got_object_tree_get_entries(tree);
2063 te = SIMPLEQ_FIRST(&entries->head);
2064 while (te) {
2065 char *id = NULL;
2067 if (sigint_received || sigpipe_received)
2068 break;
2070 if (show_ids) {
2071 char *id_str;
2072 err = got_object_id_str(&id_str, te->id);
2073 if (err)
2074 goto done;
2075 if (asprintf(&id, "%s ", id_str) == -1) {
2076 err = got_error_from_errno("asprintf");
2077 free(id_str);
2078 goto done;
2080 free(id_str);
2082 print_entry(te, id, path, root_path);
2083 free(id);
2085 if (recurse && S_ISDIR(te->mode)) {
2086 char *child_path;
2087 if (asprintf(&child_path, "%s%s%s", path,
2088 path[0] == '/' && path[1] == '\0' ? "" : "/",
2089 te->name) == -1) {
2090 err = got_error_from_errno("asprintf");
2091 goto done;
2093 err = print_tree(child_path, commit_id, show_ids, 1,
2094 root_path, repo);
2095 free(child_path);
2096 if (err)
2097 goto done;
2100 te = SIMPLEQ_NEXT(te, entry);
2102 done:
2103 if (tree)
2104 got_object_tree_close(tree);
2105 free(tree_id);
2106 return err;
2109 static const struct got_error *
2110 cmd_tree(int argc, char *argv[])
2112 const struct got_error *error;
2113 struct got_repository *repo = NULL;
2114 struct got_worktree *worktree = NULL;
2115 const char *path;
2116 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2117 struct got_object_id *commit_id = NULL;
2118 char *commit_id_str = NULL;
2119 int show_ids = 0, recurse = 0;
2120 int ch;
2122 #ifndef PROFILE
2123 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2124 NULL) == -1)
2125 err(1, "pledge");
2126 #endif
2128 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2129 switch (ch) {
2130 case 'c':
2131 commit_id_str = optarg;
2132 break;
2133 case 'r':
2134 repo_path = realpath(optarg, NULL);
2135 if (repo_path == NULL)
2136 err(1, "-r option");
2137 got_path_strip_trailing_slashes(repo_path);
2138 break;
2139 case 'i':
2140 show_ids = 1;
2141 break;
2142 case 'R':
2143 recurse = 1;
2144 break;
2145 default:
2146 usage_tree();
2147 /* NOTREACHED */
2151 argc -= optind;
2152 argv += optind;
2154 if (argc == 1)
2155 path = argv[0];
2156 else if (argc > 1)
2157 usage_tree();
2158 else
2159 path = NULL;
2161 cwd = getcwd(NULL, 0);
2162 if (cwd == NULL) {
2163 error = got_error_from_errno("getcwd");
2164 goto done;
2166 if (repo_path == NULL) {
2167 error = got_worktree_open(&worktree, cwd);
2168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2169 goto done;
2170 else
2171 error = NULL;
2172 if (worktree) {
2173 repo_path =
2174 strdup(got_worktree_get_repo_path(worktree));
2175 if (repo_path == NULL)
2176 error = got_error_from_errno("strdup");
2177 if (error)
2178 goto done;
2179 } else {
2180 repo_path = strdup(cwd);
2181 if (repo_path == NULL) {
2182 error = got_error_from_errno("strdup");
2183 goto done;
2188 error = got_repo_open(&repo, repo_path);
2189 if (error != NULL)
2190 goto done;
2192 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2193 if (error)
2194 goto done;
2196 if (path == NULL) {
2197 if (worktree) {
2198 char *p, *worktree_subdir = cwd +
2199 strlen(got_worktree_get_root_path(worktree));
2200 if (asprintf(&p, "%s/%s",
2201 got_worktree_get_path_prefix(worktree),
2202 worktree_subdir) == -1) {
2203 error = got_error_from_errno("asprintf");
2204 goto done;
2206 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2207 free(p);
2208 if (error)
2209 goto done;
2210 } else
2211 path = "/";
2213 if (in_repo_path == NULL) {
2214 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2215 if (error != NULL)
2216 goto done;
2219 if (commit_id_str == NULL) {
2220 struct got_reference *head_ref;
2221 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2222 if (error != NULL)
2223 goto done;
2224 error = got_ref_resolve(&commit_id, repo, head_ref);
2225 got_ref_close(head_ref);
2226 if (error != NULL)
2227 goto done;
2228 } else {
2229 error = got_repo_match_object_id_prefix(&commit_id,
2230 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2231 if (error != NULL)
2232 goto done;
2235 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2236 in_repo_path, repo);
2237 done:
2238 free(in_repo_path);
2239 free(repo_path);
2240 free(cwd);
2241 free(commit_id);
2242 if (worktree)
2243 got_worktree_close(worktree);
2244 if (repo) {
2245 const struct got_error *repo_error;
2246 repo_error = got_repo_close(repo);
2247 if (error == NULL)
2248 error = repo_error;
2250 return error;
2253 __dead static void
2254 usage_status(void)
2256 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2257 exit(1);
2260 static const struct got_error *
2261 print_status(void *arg, unsigned char status, const char *path,
2262 struct got_object_id *blob_id, struct got_object_id *commit_id)
2264 printf("%c %s\n", status, path);
2265 return NULL;
2268 static const struct got_error *
2269 cmd_status(int argc, char *argv[])
2271 const struct got_error *error = NULL;
2272 struct got_repository *repo = NULL;
2273 struct got_worktree *worktree = NULL;
2274 char *cwd = NULL, *path = NULL;
2275 int ch;
2277 while ((ch = getopt(argc, argv, "")) != -1) {
2278 switch (ch) {
2279 default:
2280 usage_status();
2281 /* NOTREACHED */
2285 argc -= optind;
2286 argv += optind;
2288 #ifndef PROFILE
2289 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2290 NULL) == -1)
2291 err(1, "pledge");
2292 #endif
2293 cwd = getcwd(NULL, 0);
2294 if (cwd == NULL) {
2295 error = got_error_from_errno("getcwd");
2296 goto done;
2299 error = got_worktree_open(&worktree, cwd);
2300 if (error != NULL)
2301 goto done;
2303 if (argc == 0) {
2304 path = strdup("");
2305 if (path == NULL) {
2306 error = got_error_from_errno("strdup");
2307 goto done;
2309 } else if (argc == 1) {
2310 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2311 if (error)
2312 goto done;
2313 } else
2314 usage_status();
2316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2317 if (error != NULL)
2318 goto done;
2320 error = apply_unveil(got_repo_get_path(repo), 1,
2321 got_worktree_get_root_path(worktree), 0);
2322 if (error)
2323 goto done;
2325 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2326 check_cancelled, NULL);
2327 done:
2328 free(cwd);
2329 free(path);
2330 return error;
2333 __dead static void
2334 usage_ref(void)
2336 fprintf(stderr,
2337 "usage: %s ref [-r repository] -l | -d name | name target\n",
2338 getprogname());
2339 exit(1);
2342 static const struct got_error *
2343 list_refs(struct got_repository *repo)
2345 static const struct got_error *err = NULL;
2346 struct got_reflist_head refs;
2347 struct got_reflist_entry *re;
2349 SIMPLEQ_INIT(&refs);
2350 err = got_ref_list(&refs, repo);
2351 if (err)
2352 return err;
2354 SIMPLEQ_FOREACH(re, &refs, entry) {
2355 char *refstr;
2356 refstr = got_ref_to_str(re->ref);
2357 if (refstr == NULL)
2358 return got_error_from_errno("got_ref_to_str");
2359 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2360 free(refstr);
2363 got_ref_list_free(&refs);
2364 return NULL;
2367 static const struct got_error *
2368 delete_ref(struct got_repository *repo, const char *refname)
2370 const struct got_error *err = NULL;
2371 struct got_reference *ref;
2373 err = got_ref_open(&ref, repo, refname, 0);
2374 if (err)
2375 return err;
2377 err = got_ref_delete(ref, repo);
2378 got_ref_close(ref);
2379 return err;
2382 static const struct got_error *
2383 add_ref(struct got_repository *repo, const char *refname, const char *target)
2385 const struct got_error *err = NULL;
2386 struct got_object_id *id;
2387 struct got_reference *ref = NULL;
2390 * Don't let the user create a reference named '-'.
2391 * While technically a valid reference name, this case is usually
2392 * an unintended typo.
2394 if (refname[0] == '-' && refname[1] == '\0')
2395 return got_error(GOT_ERR_BAD_REF_NAME);
2397 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2398 repo);
2399 if (err) {
2400 struct got_reference *target_ref;
2402 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2403 return err;
2404 err = got_ref_open(&target_ref, repo, target, 0);
2405 if (err)
2406 return err;
2407 err = got_ref_resolve(&id, repo, target_ref);
2408 got_ref_close(target_ref);
2409 if (err)
2410 return err;
2413 err = got_ref_alloc(&ref, refname, id);
2414 if (err)
2415 goto done;
2417 err = got_ref_write(ref, repo);
2418 done:
2419 if (ref)
2420 got_ref_close(ref);
2421 free(id);
2422 return err;
2425 static const struct got_error *
2426 cmd_ref(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_repository *repo = NULL;
2430 struct got_worktree *worktree = NULL;
2431 char *cwd = NULL, *repo_path = NULL;
2432 int ch, do_list = 0;
2433 const char *delref = NULL;
2435 /* TODO: Add -s option for adding symbolic references. */
2436 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2437 switch (ch) {
2438 case 'd':
2439 delref = optarg;
2440 break;
2441 case 'r':
2442 repo_path = realpath(optarg, NULL);
2443 if (repo_path == NULL)
2444 err(1, "-r option");
2445 got_path_strip_trailing_slashes(repo_path);
2446 break;
2447 case 'l':
2448 do_list = 1;
2449 break;
2450 default:
2451 usage_ref();
2452 /* NOTREACHED */
2456 if (do_list && delref)
2457 errx(1, "-l and -d options are mutually exclusive\n");
2459 argc -= optind;
2460 argv += optind;
2462 if (do_list || delref) {
2463 if (argc > 0)
2464 usage_ref();
2465 } else if (argc != 2)
2466 usage_ref();
2468 #ifndef PROFILE
2469 if (do_list) {
2470 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2471 NULL) == -1)
2472 err(1, "pledge");
2473 } else {
2474 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2475 "sendfd unveil", NULL) == -1)
2476 err(1, "pledge");
2478 #endif
2479 cwd = getcwd(NULL, 0);
2480 if (cwd == NULL) {
2481 error = got_error_from_errno("getcwd");
2482 goto done;
2485 if (repo_path == NULL) {
2486 error = got_worktree_open(&worktree, cwd);
2487 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2488 goto done;
2489 else
2490 error = NULL;
2491 if (worktree) {
2492 repo_path =
2493 strdup(got_worktree_get_repo_path(worktree));
2494 if (repo_path == NULL)
2495 error = got_error_from_errno("strdup");
2496 if (error)
2497 goto done;
2498 } else {
2499 repo_path = strdup(cwd);
2500 if (repo_path == NULL) {
2501 error = got_error_from_errno("strdup");
2502 goto done;
2507 error = got_repo_open(&repo, repo_path);
2508 if (error != NULL)
2509 goto done;
2511 error = apply_unveil(got_repo_get_path(repo), do_list,
2512 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2513 if (error)
2514 goto done;
2516 if (do_list)
2517 error = list_refs(repo);
2518 else if (delref)
2519 error = delete_ref(repo, delref);
2520 else
2521 error = add_ref(repo, argv[0], argv[1]);
2522 done:
2523 if (repo)
2524 got_repo_close(repo);
2525 if (worktree)
2526 got_worktree_close(worktree);
2527 free(cwd);
2528 free(repo_path);
2529 return error;
2532 __dead static void
2533 usage_branch(void)
2535 fprintf(stderr,
2536 "usage: %s branch [-r repository] -l | -d name | "
2537 "name [base-branch]\n", getprogname());
2538 exit(1);
2541 static const struct got_error *
2542 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2544 static const struct got_error *err = NULL;
2545 struct got_reflist_head refs;
2546 struct got_reflist_entry *re;
2548 SIMPLEQ_INIT(&refs);
2550 err = got_ref_list(&refs, repo);
2551 if (err)
2552 return err;
2554 SIMPLEQ_FOREACH(re, &refs, entry) {
2555 const char *refname, *marker = " ";
2556 char *refstr;
2557 refname = got_ref_get_name(re->ref);
2558 if (strncmp(refname, "refs/heads/", 11) != 0)
2559 continue;
2560 if (worktree && strcmp(refname,
2561 got_worktree_get_head_ref_name(worktree)) == 0) {
2562 struct got_object_id *id = NULL;
2563 err = got_ref_resolve(&id, repo, re->ref);
2564 if (err)
2565 return err;
2566 if (got_object_id_cmp(id,
2567 got_worktree_get_base_commit_id(worktree)) == 0)
2568 marker = "* ";
2569 else
2570 marker = "~ ";
2571 free(id);
2573 refname += 11;
2574 refstr = got_ref_to_str(re->ref);
2575 if (refstr == NULL)
2576 return got_error_from_errno("got_ref_to_str");
2577 printf("%s%s: %s\n", marker, refname, refstr);
2578 free(refstr);
2581 got_ref_list_free(&refs);
2582 return NULL;
2585 static const struct got_error *
2586 delete_branch(struct got_repository *repo, const char *branch_name)
2588 const struct got_error *err = NULL;
2589 struct got_reference *ref;
2590 char *refname;
2592 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2593 return got_error_from_errno("asprintf");
2595 err = got_ref_open(&ref, repo, refname, 0);
2596 if (err)
2597 goto done;
2599 err = got_ref_delete(ref, repo);
2600 got_ref_close(ref);
2601 done:
2602 free(refname);
2603 return err;
2606 static const struct got_error *
2607 add_branch(struct got_repository *repo, const char *branch_name,
2608 const char *base_branch)
2610 const struct got_error *err = NULL;
2611 struct got_object_id *id = NULL;
2612 struct got_reference *ref = NULL;
2613 char *base_refname = NULL, *refname = NULL;
2614 struct got_reference *base_ref;
2617 * Don't let the user create a branch named '-'.
2618 * While technically a valid reference name, this case is usually
2619 * an unintended typo.
2621 if (branch_name[0] == '-' && branch_name[1] == '\0')
2622 return got_error(GOT_ERR_BAD_REF_NAME);
2624 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2625 base_refname = strdup(GOT_REF_HEAD);
2626 if (base_refname == NULL)
2627 return got_error_from_errno("strdup");
2628 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2629 return got_error_from_errno("asprintf");
2631 err = got_ref_open(&base_ref, repo, base_refname, 0);
2632 if (err)
2633 goto done;
2634 err = got_ref_resolve(&id, repo, base_ref);
2635 got_ref_close(base_ref);
2636 if (err)
2637 goto done;
2639 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2640 err = got_error_from_errno("asprintf");
2641 goto done;
2644 err = got_ref_open(&ref, repo, refname, 0);
2645 if (err == NULL) {
2646 err = got_error(GOT_ERR_BRANCH_EXISTS);
2647 goto done;
2648 } else if (err->code != GOT_ERR_NOT_REF)
2649 goto done;
2651 err = got_ref_alloc(&ref, refname, id);
2652 if (err)
2653 goto done;
2655 err = got_ref_write(ref, repo);
2656 done:
2657 if (ref)
2658 got_ref_close(ref);
2659 free(id);
2660 free(base_refname);
2661 free(refname);
2662 return err;
2665 static const struct got_error *
2666 cmd_branch(int argc, char *argv[])
2668 const struct got_error *error = NULL;
2669 struct got_repository *repo = NULL;
2670 struct got_worktree *worktree = NULL;
2671 char *cwd = NULL, *repo_path = NULL;
2672 int ch, do_list = 0;
2673 const char *delref = NULL;
2675 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2676 switch (ch) {
2677 case 'd':
2678 delref = optarg;
2679 break;
2680 case 'r':
2681 repo_path = realpath(optarg, NULL);
2682 if (repo_path == NULL)
2683 err(1, "-r option");
2684 got_path_strip_trailing_slashes(repo_path);
2685 break;
2686 case 'l':
2687 do_list = 1;
2688 break;
2689 default:
2690 usage_branch();
2691 /* NOTREACHED */
2695 if (do_list && delref)
2696 errx(1, "-l and -d options are mutually exclusive\n");
2698 argc -= optind;
2699 argv += optind;
2701 if (do_list || delref) {
2702 if (argc > 0)
2703 usage_branch();
2704 } else if (argc < 1 || argc > 2)
2705 usage_branch();
2707 #ifndef PROFILE
2708 if (do_list) {
2709 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2710 NULL) == -1)
2711 err(1, "pledge");
2712 } else {
2713 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2714 "sendfd unveil", NULL) == -1)
2715 err(1, "pledge");
2717 #endif
2718 cwd = getcwd(NULL, 0);
2719 if (cwd == NULL) {
2720 error = got_error_from_errno("getcwd");
2721 goto done;
2724 if (repo_path == NULL) {
2725 error = got_worktree_open(&worktree, cwd);
2726 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2727 goto done;
2728 else
2729 error = NULL;
2730 if (worktree) {
2731 repo_path =
2732 strdup(got_worktree_get_repo_path(worktree));
2733 if (repo_path == NULL)
2734 error = got_error_from_errno("strdup");
2735 if (error)
2736 goto done;
2737 } else {
2738 repo_path = strdup(cwd);
2739 if (repo_path == NULL) {
2740 error = got_error_from_errno("strdup");
2741 goto done;
2746 error = got_repo_open(&repo, repo_path);
2747 if (error != NULL)
2748 goto done;
2750 error = apply_unveil(got_repo_get_path(repo), do_list,
2751 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2752 if (error)
2753 goto done;
2755 if (do_list)
2756 error = list_branches(repo, worktree);
2757 else if (delref)
2758 error = delete_branch(repo, delref);
2759 else {
2760 const char *base_branch;
2761 if (argc == 1) {
2762 base_branch = worktree ?
2763 got_worktree_get_head_ref_name(worktree) :
2764 GOT_REF_HEAD;
2765 } else
2766 base_branch = argv[1];
2767 error = add_branch(repo, argv[0], base_branch);
2769 done:
2770 if (repo)
2771 got_repo_close(repo);
2772 if (worktree)
2773 got_worktree_close(worktree);
2774 free(cwd);
2775 free(repo_path);
2776 return error;
2779 __dead static void
2780 usage_add(void)
2782 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2783 exit(1);
2786 static const struct got_error *
2787 cmd_add(int argc, char *argv[])
2789 const struct got_error *error = NULL;
2790 struct got_repository *repo = NULL;
2791 struct got_worktree *worktree = NULL;
2792 char *cwd = NULL;
2793 struct got_pathlist_head paths;
2794 struct got_pathlist_entry *pe;
2795 int ch, x;
2797 TAILQ_INIT(&paths);
2799 while ((ch = getopt(argc, argv, "")) != -1) {
2800 switch (ch) {
2801 default:
2802 usage_add();
2803 /* NOTREACHED */
2807 argc -= optind;
2808 argv += optind;
2810 #ifndef PROFILE
2811 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2812 NULL) == -1)
2813 err(1, "pledge");
2814 #endif
2815 if (argc < 1)
2816 usage_add();
2818 /* make sure each file exists before doing anything halfway */
2819 for (x = 0; x < argc; x++) {
2820 char *path = realpath(argv[x], NULL);
2821 if (path == NULL) {
2822 error = got_error_from_errno2("realpath", argv[x]);
2823 goto done;
2825 free(path);
2828 cwd = getcwd(NULL, 0);
2829 if (cwd == NULL) {
2830 error = got_error_from_errno("getcwd");
2831 goto done;
2834 error = got_worktree_open(&worktree, cwd);
2835 if (error)
2836 goto done;
2838 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2839 if (error != NULL)
2840 goto done;
2842 error = apply_unveil(got_repo_get_path(repo), 1,
2843 got_worktree_get_root_path(worktree), 0);
2844 if (error)
2845 goto done;
2847 for (x = 0; x < argc; x++) {
2848 char *path = realpath(argv[x], NULL);
2849 if (path == NULL) {
2850 error = got_error_from_errno2("realpath", argv[x]);
2851 goto done;
2854 got_path_strip_trailing_slashes(path);
2855 error = got_pathlist_insert(&pe, &paths, path, NULL);
2856 if (error) {
2857 free(path);
2858 goto done;
2861 error = got_worktree_schedule_add(worktree, &paths, print_status,
2862 NULL, repo);
2863 done:
2864 if (repo)
2865 got_repo_close(repo);
2866 if (worktree)
2867 got_worktree_close(worktree);
2868 TAILQ_FOREACH(pe, &paths, entry)
2869 free((char *)pe->path);
2870 got_pathlist_free(&paths);
2871 free(cwd);
2872 return error;
2875 __dead static void
2876 usage_remove(void)
2878 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2879 exit(1);
2882 static const struct got_error *
2883 cmd_remove(int argc, char *argv[])
2885 const struct got_error *error = NULL;
2886 struct got_worktree *worktree = NULL;
2887 struct got_repository *repo = NULL;
2888 char *cwd = NULL;
2889 struct got_pathlist_head paths;
2890 struct got_pathlist_entry *pe;
2891 int ch, i, delete_local_mods = 0;
2893 TAILQ_INIT(&paths);
2895 while ((ch = getopt(argc, argv, "f")) != -1) {
2896 switch (ch) {
2897 case 'f':
2898 delete_local_mods = 1;
2899 break;
2900 default:
2901 usage_add();
2902 /* NOTREACHED */
2906 argc -= optind;
2907 argv += optind;
2909 #ifndef PROFILE
2910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2911 NULL) == -1)
2912 err(1, "pledge");
2913 #endif
2914 if (argc < 1)
2915 usage_remove();
2917 /* make sure each file exists before doing anything halfway */
2918 for (i = 0; i < argc; i++) {
2919 char *path = realpath(argv[i], NULL);
2920 if (path == NULL) {
2921 error = got_error_from_errno2("realpath", argv[i]);
2922 goto done;
2924 free(path);
2927 cwd = getcwd(NULL, 0);
2928 if (cwd == NULL) {
2929 error = got_error_from_errno("getcwd");
2930 goto done;
2932 error = got_worktree_open(&worktree, cwd);
2933 if (error)
2934 goto done;
2936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2937 if (error)
2938 goto done;
2940 error = apply_unveil(got_repo_get_path(repo), 1,
2941 got_worktree_get_root_path(worktree), 0);
2942 if (error)
2943 goto done;
2945 for (i = 0; i < argc; i++) {
2946 char *path = realpath(argv[i], NULL);
2947 if (path == NULL) {
2948 error = got_error_from_errno2("realpath", argv[i]);
2949 goto done;
2952 got_path_strip_trailing_slashes(path);
2953 error = got_pathlist_insert(&pe, &paths, path, NULL);
2954 if (error) {
2955 free(path);
2956 goto done;
2959 error = got_worktree_schedule_delete(worktree, &paths,
2960 delete_local_mods, print_status, NULL, repo);
2961 if (error)
2962 goto done;
2963 done:
2964 if (repo)
2965 got_repo_close(repo);
2966 if (worktree)
2967 got_worktree_close(worktree);
2968 TAILQ_FOREACH(pe, &paths, entry)
2969 free((char *)pe->path);
2970 got_pathlist_free(&paths);
2971 free(cwd);
2972 return error;
2975 __dead static void
2976 usage_revert(void)
2978 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2979 exit(1);
2982 static const struct got_error *
2983 revert_progress(void *arg, unsigned char status, const char *path)
2985 while (path[0] == '/')
2986 path++;
2987 printf("%c %s\n", status, path);
2988 return NULL;
2991 static const struct got_error *
2992 cmd_revert(int argc, char *argv[])
2994 const struct got_error *error = NULL;
2995 struct got_worktree *worktree = NULL;
2996 struct got_repository *repo = NULL;
2997 char *cwd = NULL, *path = NULL;
2998 struct got_pathlist_head paths;
2999 struct got_pathlist_entry *pe;
3000 int ch, i;
3002 TAILQ_INIT(&paths);
3004 while ((ch = getopt(argc, argv, "")) != -1) {
3005 switch (ch) {
3006 default:
3007 usage_revert();
3008 /* NOTREACHED */
3012 argc -= optind;
3013 argv += optind;
3015 #ifndef PROFILE
3016 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3017 "unveil", NULL) == -1)
3018 err(1, "pledge");
3019 #endif
3020 if (argc < 1)
3021 usage_revert();
3023 for (i = 0; i < argc; i++) {
3024 char *path = realpath(argv[i], NULL);
3025 if (path == NULL) {
3026 error = got_error_from_errno2("realpath", argv[i]);
3027 goto done;
3030 got_path_strip_trailing_slashes(path);
3031 error = got_pathlist_insert(&pe, &paths, path, NULL);
3032 if (error) {
3033 free(path);
3034 goto done;
3038 cwd = getcwd(NULL, 0);
3039 if (cwd == NULL) {
3040 error = got_error_from_errno("getcwd");
3041 goto done;
3043 error = got_worktree_open(&worktree, cwd);
3044 if (error)
3045 goto done;
3047 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3048 if (error != NULL)
3049 goto done;
3051 error = apply_unveil(got_repo_get_path(repo), 1,
3052 got_worktree_get_root_path(worktree), 0);
3053 if (error)
3054 goto done;
3056 error = got_worktree_revert(worktree, &paths,
3057 revert_progress, NULL, repo);
3058 if (error)
3059 goto done;
3060 done:
3061 if (repo)
3062 got_repo_close(repo);
3063 if (worktree)
3064 got_worktree_close(worktree);
3065 free(path);
3066 free(cwd);
3067 return error;
3070 __dead static void
3071 usage_commit(void)
3073 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3074 exit(1);
3077 struct collect_commit_logmsg_arg {
3078 const char *cmdline_log;
3079 const char *editor;
3080 const char *worktree_path;
3081 const char *branch_name;
3082 const char *repo_path;
3083 char *logmsg_path;
3087 static const struct got_error *
3088 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3089 void *arg)
3091 char *initial_content = NULL;
3092 struct got_pathlist_entry *pe;
3093 const struct got_error *err = NULL;
3094 char *template = NULL;
3095 struct collect_commit_logmsg_arg *a = arg;
3096 int fd;
3097 size_t len;
3099 /* if a message was specified on the command line, just use it */
3100 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3101 len = strlen(a->cmdline_log) + 1;
3102 *logmsg = malloc(len + 1);
3103 if (*logmsg == NULL)
3104 return got_error_from_errno("malloc");
3105 strlcpy(*logmsg, a->cmdline_log, len);
3106 return NULL;
3109 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3110 return got_error_from_errno("asprintf");
3112 if (asprintf(&initial_content,
3113 "\n# changes to be committed on branch %s:\n",
3114 a->branch_name) == -1)
3115 return got_error_from_errno("asprintf");
3117 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3118 if (err)
3119 goto done;
3121 dprintf(fd, initial_content);
3123 TAILQ_FOREACH(pe, commitable_paths, entry) {
3124 struct got_commitable *ct = pe->data;
3125 dprintf(fd, "# %c %s\n",
3126 got_commitable_get_status(ct),
3127 got_commitable_get_path(ct));
3129 close(fd);
3131 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3132 done:
3133 unlink(a->logmsg_path);
3134 free(a->logmsg_path);
3135 free(initial_content);
3136 free(template);
3138 /* Editor is done; we can now apply unveil(2) */
3139 if (err == NULL) {
3140 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3141 if (err) {
3142 free(*logmsg);
3143 *logmsg = NULL;
3146 return err;
3149 static const struct got_error *
3150 cmd_commit(int argc, char *argv[])
3152 const struct got_error *error = NULL;
3153 struct got_worktree *worktree = NULL;
3154 struct got_repository *repo = NULL;
3155 char *cwd = NULL, *path = NULL, *id_str = NULL;
3156 struct got_object_id *id = NULL;
3157 const char *logmsg = NULL;
3158 const char *got_author = getenv("GOT_AUTHOR");
3159 struct collect_commit_logmsg_arg cl_arg;
3160 char *editor = NULL;
3161 int ch, rebase_in_progress;
3163 while ((ch = getopt(argc, argv, "m:")) != -1) {
3164 switch (ch) {
3165 case 'm':
3166 logmsg = optarg;
3167 break;
3168 default:
3169 usage_commit();
3170 /* NOTREACHED */
3174 argc -= optind;
3175 argv += optind;
3177 #ifndef PROFILE
3178 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3179 "unveil", NULL) == -1)
3180 err(1, "pledge");
3181 #endif
3182 if (argc == 1) {
3183 path = realpath(argv[0], NULL);
3184 if (path == NULL) {
3185 error = got_error_from_errno2("realpath", argv[0]);
3186 goto done;
3188 got_path_strip_trailing_slashes(path);
3189 } else if (argc != 0)
3190 usage_commit();
3192 if (got_author == NULL) {
3193 /* TODO: Look current user up in password database */
3194 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3195 goto done;
3198 cwd = getcwd(NULL, 0);
3199 if (cwd == NULL) {
3200 error = got_error_from_errno("getcwd");
3201 goto done;
3203 error = got_worktree_open(&worktree, cwd);
3204 if (error)
3205 goto done;
3207 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3208 if (error)
3209 goto done;
3210 if (rebase_in_progress) {
3211 error = got_error(GOT_ERR_REBASING);
3212 goto done;
3215 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3216 if (error != NULL)
3217 goto done;
3220 * unveil(2) traverses exec(2); if an editor is used we have
3221 * to apply unveil after the log message has been written.
3223 if (logmsg == NULL || strlen(logmsg) == 0)
3224 error = get_editor(&editor);
3225 else
3226 error = apply_unveil(got_repo_get_path(repo), 0,
3227 got_worktree_get_root_path(worktree), 0);
3228 if (error)
3229 goto done;
3231 cl_arg.editor = editor;
3232 cl_arg.cmdline_log = logmsg;
3233 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3234 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3235 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3236 cl_arg.branch_name += 5;
3237 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3238 cl_arg.branch_name += 6;
3239 cl_arg.repo_path = got_repo_get_path(repo);
3240 cl_arg.logmsg_path = NULL;
3241 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3242 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3243 if (error) {
3244 if (cl_arg.logmsg_path)
3245 fprintf(stderr, "%s: log message preserved in %s\n",
3246 getprogname(), cl_arg.logmsg_path);
3247 goto done;
3250 if (cl_arg.logmsg_path)
3251 unlink(cl_arg.logmsg_path);
3253 error = got_object_id_str(&id_str, id);
3254 if (error)
3255 goto done;
3256 printf("Created commit %s\n", id_str);
3257 done:
3258 if (repo)
3259 got_repo_close(repo);
3260 if (worktree)
3261 got_worktree_close(worktree);
3262 free(path);
3263 free(cwd);
3264 free(id_str);
3265 free(editor);
3266 return error;
3269 __dead static void
3270 usage_cherrypick(void)
3272 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3273 exit(1);
3276 static const struct got_error *
3277 cmd_cherrypick(int argc, char *argv[])
3279 const struct got_error *error = NULL;
3280 struct got_worktree *worktree = NULL;
3281 struct got_repository *repo = NULL;
3282 char *cwd = NULL, *commit_id_str = NULL;
3283 struct got_object_id *commit_id = NULL;
3284 struct got_commit_object *commit = NULL;
3285 struct got_object_qid *pid;
3286 struct got_reference *head_ref = NULL;
3287 int ch, did_something = 0;
3289 while ((ch = getopt(argc, argv, "")) != -1) {
3290 switch (ch) {
3291 default:
3292 usage_cherrypick();
3293 /* NOTREACHED */
3297 argc -= optind;
3298 argv += optind;
3300 #ifndef PROFILE
3301 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3302 "unveil", NULL) == -1)
3303 err(1, "pledge");
3304 #endif
3305 if (argc != 1)
3306 usage_cherrypick();
3308 cwd = getcwd(NULL, 0);
3309 if (cwd == NULL) {
3310 error = got_error_from_errno("getcwd");
3311 goto done;
3313 error = got_worktree_open(&worktree, cwd);
3314 if (error)
3315 goto done;
3317 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3318 if (error != NULL)
3319 goto done;
3321 error = apply_unveil(got_repo_get_path(repo), 0,
3322 got_worktree_get_root_path(worktree), 0);
3323 if (error)
3324 goto done;
3326 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3327 GOT_OBJ_TYPE_COMMIT, repo);
3328 if (error != NULL) {
3329 struct got_reference *ref;
3330 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3331 goto done;
3332 error = got_ref_open(&ref, repo, argv[0], 0);
3333 if (error != NULL)
3334 goto done;
3335 error = got_ref_resolve(&commit_id, repo, ref);
3336 got_ref_close(ref);
3337 if (error != NULL)
3338 goto done;
3340 error = got_object_id_str(&commit_id_str, commit_id);
3341 if (error)
3342 goto done;
3344 error = got_ref_open(&head_ref, repo,
3345 got_worktree_get_head_ref_name(worktree), 0);
3346 if (error != NULL)
3347 goto done;
3349 error = check_same_branch(commit_id, head_ref, repo);
3350 if (error) {
3351 if (error->code != GOT_ERR_ANCESTRY)
3352 goto done;
3353 error = NULL;
3354 } else {
3355 error = got_error(GOT_ERR_SAME_BRANCH);
3356 goto done;
3359 error = got_object_open_as_commit(&commit, repo, commit_id);
3360 if (error)
3361 goto done;
3362 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3363 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3364 commit_id, repo, update_progress, &did_something, check_cancelled,
3365 NULL);
3366 if (error != NULL)
3367 goto done;
3369 if (did_something)
3370 printf("Merged commit %s\n", commit_id_str);
3371 done:
3372 if (commit)
3373 got_object_commit_close(commit);
3374 free(commit_id_str);
3375 if (head_ref)
3376 got_ref_close(head_ref);
3377 if (worktree)
3378 got_worktree_close(worktree);
3379 if (repo)
3380 got_repo_close(repo);
3381 return error;
3384 __dead static void
3385 usage_backout(void)
3387 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3388 exit(1);
3391 static const struct got_error *
3392 cmd_backout(int argc, char *argv[])
3394 const struct got_error *error = NULL;
3395 struct got_worktree *worktree = NULL;
3396 struct got_repository *repo = NULL;
3397 char *cwd = NULL, *commit_id_str = NULL;
3398 struct got_object_id *commit_id = NULL;
3399 struct got_commit_object *commit = NULL;
3400 struct got_object_qid *pid;
3401 struct got_reference *head_ref = NULL;
3402 int ch, did_something = 0;
3404 while ((ch = getopt(argc, argv, "")) != -1) {
3405 switch (ch) {
3406 default:
3407 usage_backout();
3408 /* NOTREACHED */
3412 argc -= optind;
3413 argv += optind;
3415 #ifndef PROFILE
3416 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3417 "unveil", NULL) == -1)
3418 err(1, "pledge");
3419 #endif
3420 if (argc != 1)
3421 usage_backout();
3423 cwd = getcwd(NULL, 0);
3424 if (cwd == NULL) {
3425 error = got_error_from_errno("getcwd");
3426 goto done;
3428 error = got_worktree_open(&worktree, cwd);
3429 if (error)
3430 goto done;
3432 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3433 if (error != NULL)
3434 goto done;
3436 error = apply_unveil(got_repo_get_path(repo), 0,
3437 got_worktree_get_root_path(worktree), 0);
3438 if (error)
3439 goto done;
3441 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3442 GOT_OBJ_TYPE_COMMIT, repo);
3443 if (error != NULL) {
3444 struct got_reference *ref;
3445 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3446 goto done;
3447 error = got_ref_open(&ref, repo, argv[0], 0);
3448 if (error != NULL)
3449 goto done;
3450 error = got_ref_resolve(&commit_id, repo, ref);
3451 got_ref_close(ref);
3452 if (error != NULL)
3453 goto done;
3455 error = got_object_id_str(&commit_id_str, commit_id);
3456 if (error)
3457 goto done;
3459 error = got_ref_open(&head_ref, repo,
3460 got_worktree_get_head_ref_name(worktree), 0);
3461 if (error != NULL)
3462 goto done;
3464 error = check_same_branch(commit_id, head_ref, repo);
3465 if (error)
3466 goto done;
3468 error = got_object_open_as_commit(&commit, repo, commit_id);
3469 if (error)
3470 goto done;
3471 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3472 if (pid == NULL) {
3473 error = got_error(GOT_ERR_ROOT_COMMIT);
3474 goto done;
3477 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3478 update_progress, &did_something, check_cancelled, NULL);
3479 if (error != NULL)
3480 goto done;
3482 if (did_something)
3483 printf("Backed out commit %s\n", commit_id_str);
3484 done:
3485 if (commit)
3486 got_object_commit_close(commit);
3487 free(commit_id_str);
3488 if (head_ref)
3489 got_ref_close(head_ref);
3490 if (worktree)
3491 got_worktree_close(worktree);
3492 if (repo)
3493 got_repo_close(repo);
3494 return error;
3497 __dead static void
3498 usage_rebase(void)
3500 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3501 getprogname());
3502 exit(1);
3505 static const struct got_error *
3506 show_rebase_progress(struct got_commit_object *commit,
3507 struct got_object_id *old_id, struct got_object_id *new_id)
3509 const struct got_error *err;
3510 char *old_id_str = NULL, *new_id_str = NULL;
3511 char *logmsg0 = NULL, *logmsg, *nl;
3512 size_t len;
3514 err = got_object_id_str(&old_id_str, old_id);
3515 if (err)
3516 goto done;
3518 if (new_id) {
3519 err = got_object_id_str(&new_id_str, new_id);
3520 if (err)
3521 goto done;
3524 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3525 if (logmsg0 == NULL) {
3526 err = got_error_from_errno("strdup");
3527 goto done;
3529 logmsg = logmsg0;
3531 while (isspace((unsigned char)logmsg[0]))
3532 logmsg++;
3534 old_id_str[12] = '\0';
3535 if (new_id_str)
3536 new_id_str[12] = '\0';
3537 len = strlen(logmsg);
3538 if (len > 42)
3539 len = 42;
3540 logmsg[len] = '\0';
3541 nl = strchr(logmsg, '\n');
3542 if (nl)
3543 *nl = '\0';
3544 printf("%s -> %s: %s\n", old_id_str,
3545 new_id_str ? new_id_str : "no-op change", logmsg);
3546 done:
3547 free(old_id_str);
3548 free(new_id_str);
3549 free(logmsg0);
3550 return err;
3553 static const struct got_error *
3554 rebase_progress(void *arg, unsigned char status, const char *path)
3556 unsigned char *rebase_status = arg;
3558 while (path[0] == '/')
3559 path++;
3560 printf("%c %s\n", status, path);
3562 if (*rebase_status == GOT_STATUS_CONFLICT)
3563 return NULL;
3564 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3565 *rebase_status = status;
3566 return NULL;
3569 static const struct got_error *
3570 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3571 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3572 struct got_repository *repo)
3574 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3575 return got_worktree_rebase_complete(worktree,
3576 new_base_branch, tmp_branch, branch, repo);
3579 static const struct got_error *
3580 rebase_commit(struct got_pathlist_head *merged_paths,
3581 struct got_worktree *worktree, struct got_reference *tmp_branch,
3582 struct got_object_id *commit_id, struct got_repository *repo)
3584 const struct got_error *error;
3585 struct got_commit_object *commit;
3586 struct got_object_id *new_commit_id;
3588 error = got_object_open_as_commit(&commit, repo, commit_id);
3589 if (error)
3590 return error;
3592 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3593 worktree, tmp_branch, commit, commit_id, repo);
3594 if (error) {
3595 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3596 goto done;
3597 error = show_rebase_progress(commit, commit_id, NULL);
3598 } else {
3599 error = show_rebase_progress(commit, commit_id, new_commit_id);
3600 free(new_commit_id);
3602 done:
3603 got_object_commit_close(commit);
3604 return error;
3607 struct check_path_prefix_arg {
3608 const char *path_prefix;
3609 size_t len;
3612 static const struct got_error *
3613 check_path_prefix(void *arg, struct got_blob_object *blob1,
3614 struct got_blob_object *blob2, struct got_object_id *id1,
3615 struct got_object_id *id2, const char *path1, const char *path2,
3616 struct got_repository *repo)
3618 struct check_path_prefix_arg *a = arg;
3620 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3621 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3622 return got_error(GOT_ERR_REBASE_PATH);
3624 return NULL;
3627 static const struct got_error *
3628 rebase_check_path_prefix(struct got_object_id *parent_id,
3629 struct got_object_id *commit_id, const char *path_prefix,
3630 struct got_repository *repo)
3632 const struct got_error *err;
3633 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3634 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3635 struct check_path_prefix_arg cpp_arg;
3637 if (got_path_is_root_dir(path_prefix))
3638 return NULL;
3640 err = got_object_open_as_commit(&commit, repo, commit_id);
3641 if (err)
3642 goto done;
3644 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3645 if (err)
3646 goto done;
3648 err = got_object_open_as_tree(&tree1, repo,
3649 got_object_commit_get_tree_id(parent_commit));
3650 if (err)
3651 goto done;
3653 err = got_object_open_as_tree(&tree2, repo,
3654 got_object_commit_get_tree_id(commit));
3655 if (err)
3656 goto done;
3658 cpp_arg.path_prefix = path_prefix;
3659 cpp_arg.len = strlen(path_prefix);
3660 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3661 &cpp_arg);
3662 done:
3663 if (tree1)
3664 got_object_tree_close(tree1);
3665 if (tree2)
3666 got_object_tree_close(tree2);
3667 if (commit)
3668 got_object_commit_close(commit);
3669 if (parent_commit)
3670 got_object_commit_close(parent_commit);
3671 return err;
3674 static const struct got_error *
3675 cmd_rebase(int argc, char *argv[])
3677 const struct got_error *error = NULL;
3678 struct got_worktree *worktree = NULL;
3679 struct got_repository *repo = NULL;
3680 char *cwd = NULL;
3681 struct got_reference *branch = NULL;
3682 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3683 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3684 struct got_object_id *resume_commit_id = NULL;
3685 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3686 struct got_commit_graph *graph = NULL;
3687 struct got_commit_object *commit = NULL;
3688 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3690 struct got_object_id_queue commits;
3691 struct got_pathlist_head merged_paths;
3692 const struct got_object_id_queue *parent_ids;
3693 struct got_object_qid *qid, *pid;
3695 SIMPLEQ_INIT(&commits);
3696 TAILQ_INIT(&merged_paths);
3698 while ((ch = getopt(argc, argv, "ac")) != -1) {
3699 switch (ch) {
3700 case 'a':
3701 abort_rebase = 1;
3702 break;
3703 case 'c':
3704 continue_rebase = 1;
3705 break;
3706 default:
3707 usage_rebase();
3708 /* NOTREACHED */
3712 argc -= optind;
3713 argv += optind;
3715 #ifndef PROFILE
3716 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3717 "unveil", NULL) == -1)
3718 err(1, "pledge");
3719 #endif
3720 if (abort_rebase && continue_rebase)
3721 usage_rebase();
3722 else if (abort_rebase || continue_rebase) {
3723 if (argc != 0)
3724 usage_rebase();
3725 } else if (argc != 1)
3726 usage_rebase();
3728 cwd = getcwd(NULL, 0);
3729 if (cwd == NULL) {
3730 error = got_error_from_errno("getcwd");
3731 goto done;
3733 error = got_worktree_open(&worktree, cwd);
3734 if (error)
3735 goto done;
3737 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3738 if (error != NULL)
3739 goto done;
3741 error = apply_unveil(got_repo_get_path(repo), 0,
3742 got_worktree_get_root_path(worktree), 0);
3743 if (error)
3744 goto done;
3746 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3747 if (error)
3748 goto done;
3750 if (rebase_in_progress && abort_rebase) {
3751 int did_something;
3752 error = got_worktree_rebase_continue(&resume_commit_id,
3753 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3754 if (error)
3755 goto done;
3756 printf("Switching work tree to %s\n",
3757 got_ref_get_symref_target(new_base_branch));
3758 error = got_worktree_rebase_abort(worktree, repo,
3759 new_base_branch, update_progress, &did_something);
3760 if (error)
3761 goto done;
3762 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3763 goto done; /* nothing else to do */
3764 } else if (abort_rebase) {
3765 error = got_error(GOT_ERR_NOT_REBASING);
3766 goto done;
3769 if (continue_rebase) {
3770 error = got_worktree_rebase_continue(&resume_commit_id,
3771 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3772 if (error)
3773 goto done;
3775 error = rebase_commit(NULL, worktree, tmp_branch,
3776 resume_commit_id, repo);
3777 if (error)
3778 goto done;
3780 yca_id = got_object_id_dup(resume_commit_id);
3781 if (yca_id == NULL) {
3782 error = got_error_from_errno("got_object_id_dup");
3783 goto done;
3785 } else {
3786 error = got_ref_open(&branch, repo, argv[0], 0);
3787 if (error != NULL)
3788 goto done;
3790 error = check_same_branch(
3791 got_worktree_get_base_commit_id(worktree), branch, repo);
3792 if (error) {
3793 if (error->code != GOT_ERR_ANCESTRY)
3794 goto done;
3795 error = NULL;
3796 } else {
3797 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3798 "specified branch resolves to a commit which "
3799 "is already contained in work tree's branch");
3800 goto done;
3804 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3805 if (error)
3806 goto done;
3808 if (!continue_rebase) {
3809 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3810 got_worktree_get_base_commit_id(worktree),
3811 branch_head_commit_id, repo);
3812 if (error)
3813 goto done;
3814 if (yca_id == NULL) {
3815 error = got_error_msg(GOT_ERR_ANCESTRY,
3816 "specified branch shares no common ancestry "
3817 "with work tree's branch");
3818 goto done;
3821 error = got_worktree_rebase_prepare(&new_base_branch,
3822 &tmp_branch, worktree, branch, repo);
3823 if (error)
3824 goto done;
3827 commit_id = branch_head_commit_id;
3828 error = got_object_open_as_commit(&commit, repo, commit_id);
3829 if (error)
3830 goto done;
3832 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3833 if (error)
3834 goto done;
3835 parent_ids = got_object_commit_get_parent_ids(commit);
3836 pid = SIMPLEQ_FIRST(parent_ids);
3837 error = got_commit_graph_iter_start(graph, pid->id, repo);
3838 got_object_commit_close(commit);
3839 commit = NULL;
3840 if (error)
3841 goto done;
3842 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3843 error = got_commit_graph_iter_next(&parent_id, graph);
3844 if (error) {
3845 if (error->code == GOT_ERR_ITER_COMPLETED) {
3846 error = got_error_msg(GOT_ERR_ANCESTRY,
3847 "ran out of commits to rebase before "
3848 "youngest common ancestor commit has "
3849 "been reached?!?");
3850 goto done;
3851 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3852 goto done;
3853 error = got_commit_graph_fetch_commits(graph, 1, repo);
3854 if (error)
3855 goto done;
3856 } else {
3857 error = rebase_check_path_prefix(parent_id, commit_id,
3858 got_worktree_get_path_prefix(worktree), repo);
3859 if (error)
3860 goto done;
3862 error = got_object_qid_alloc(&qid, commit_id);
3863 if (error)
3864 goto done;
3865 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3866 commit_id = parent_id;
3869 got_commit_graph_close(graph);
3870 graph = NULL;
3872 if (SIMPLEQ_EMPTY(&commits)) {
3873 if (continue_rebase)
3874 error = rebase_complete(worktree, branch,
3875 new_base_branch, tmp_branch, repo);
3876 else
3877 error = got_error(GOT_ERR_EMPTY_REBASE);
3878 goto done;
3881 pid = NULL;
3882 SIMPLEQ_FOREACH(qid, &commits, entry) {
3883 commit_id = qid->id;
3884 parent_id = pid ? pid->id : yca_id;
3885 pid = qid;
3887 error = got_worktree_rebase_merge_files(&merged_paths,
3888 worktree, parent_id, commit_id, repo, rebase_progress,
3889 &rebase_status, check_cancelled, NULL);
3890 if (error)
3891 goto done;
3893 if (rebase_status == GOT_STATUS_CONFLICT) {
3894 got_worktree_rebase_pathlist_free(&merged_paths);
3895 break;
3898 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3899 commit_id, repo);
3900 got_worktree_rebase_pathlist_free(&merged_paths);
3901 if (error)
3902 goto done;
3905 if (rebase_status == GOT_STATUS_CONFLICT) {
3906 error = got_worktree_rebase_postpone(worktree);
3907 if (error)
3908 goto done;
3909 error = got_error_msg(GOT_ERR_CONFLICTS,
3910 "conflicts must be resolved before rebasing can continue");
3911 } else
3912 error = rebase_complete(worktree, branch, new_base_branch,
3913 tmp_branch, repo);
3914 done:
3915 got_object_id_queue_free(&commits);
3916 free(branch_head_commit_id);
3917 free(resume_commit_id);
3918 free(yca_id);
3919 if (graph)
3920 got_commit_graph_close(graph);
3921 if (commit)
3922 got_object_commit_close(commit);
3923 if (branch)
3924 got_ref_close(branch);
3925 if (new_base_branch)
3926 got_ref_close(new_base_branch);
3927 if (tmp_branch)
3928 got_ref_close(tmp_branch);
3929 if (worktree)
3930 got_worktree_close(worktree);
3931 if (repo)
3932 got_repo_close(repo);
3933 return error;