Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_path.h"
43 #include "got_worktree.h"
44 #include "got_diff.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
50 #ifndef nitems
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 #endif
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
57 static void
58 catch_sigint(int signo)
59 {
60 sigint_received = 1;
61 }
63 static void
64 catch_sigpipe(int signo)
65 {
66 sigpipe_received = 1;
67 }
70 struct got_cmd {
71 const char *cmd_name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
75 };
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
97 static const struct got_error* cmd_init(int, char *[]);
98 static const struct got_error* cmd_import(int, char *[]);
99 static const struct got_error* cmd_checkout(int, char *[]);
100 static const struct got_error* cmd_update(int, char *[]);
101 static const struct got_error* cmd_log(int, char *[]);
102 static const struct got_error* cmd_diff(int, char *[]);
103 static const struct got_error* cmd_blame(int, char *[]);
104 static const struct got_error* cmd_tree(int, char *[]);
105 static const struct got_error* cmd_status(int, char *[]);
106 static const struct got_error* cmd_ref(int, char *[]);
107 static const struct got_error* cmd_branch(int, char *[]);
108 static const struct got_error* cmd_add(int, char *[]);
109 static const struct got_error* cmd_remove(int, char *[]);
110 static const struct got_error* cmd_revert(int, char *[]);
111 static const struct got_error* cmd_commit(int, char *[]);
112 static const struct got_error* cmd_cherrypick(int, char *[]);
113 static const struct got_error* cmd_backout(int, char *[]);
114 static const struct got_error* cmd_rebase(int, char *[]);
116 static struct got_cmd got_commands[] = {
117 { "init", cmd_init, usage_init, "" },
118 { "import", cmd_import, usage_import, "" },
119 { "checkout", cmd_checkout, usage_checkout, "co" },
120 { "update", cmd_update, usage_update, "up" },
121 { "log", cmd_log, usage_log, "" },
122 { "diff", cmd_diff, usage_diff, "" },
123 { "blame", cmd_blame, usage_blame, "" },
124 { "tree", cmd_tree, usage_tree, "" },
125 { "status", cmd_status, usage_status, "st" },
126 { "ref", cmd_ref, usage_ref, "" },
127 { "branch", cmd_branch, usage_branch, "br" },
128 { "add", cmd_add, usage_add, "" },
129 { "remove", cmd_remove, usage_remove, "rm" },
130 { "revert", cmd_revert, usage_revert, "rv" },
131 { "commit", cmd_commit, usage_commit, "ci" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 { "backout", cmd_backout, usage_backout, "bo" },
134 { "rebase", cmd_rebase, usage_rebase, "rb" },
135 };
137 static void
138 list_commands(void)
140 int i;
142 fprintf(stderr, "commands:");
143 for (i = 0; i < nitems(got_commands); i++) {
144 struct got_cmd *cmd = &got_commands[i];
145 fprintf(stderr, " %s", cmd->cmd_name);
147 fputc('\n', stderr);
150 int
151 main(int argc, char *argv[])
153 struct got_cmd *cmd;
154 unsigned int i;
155 int ch;
156 int hflag = 0;
158 setlocale(LC_CTYPE, "");
160 while ((ch = getopt(argc, argv, "h")) != -1) {
161 switch (ch) {
162 case 'h':
163 hflag = 1;
164 break;
165 default:
166 usage(hflag);
167 /* NOTREACHED */
171 argc -= optind;
172 argv += optind;
173 optind = 0;
175 if (argc <= 0)
176 usage(hflag);
178 signal(SIGINT, catch_sigint);
179 signal(SIGPIPE, catch_sigpipe);
181 for (i = 0; i < nitems(got_commands); i++) {
182 const struct got_error *error;
184 cmd = &got_commands[i];
186 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 strcmp(cmd->cmd_alias, argv[0]) != 0)
188 continue;
190 if (hflag)
191 got_commands[i].cmd_usage();
193 error = got_commands[i].cmd_main(argc, argv);
194 if (error && !(sigint_received || sigpipe_received)) {
195 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
196 return 1;
199 return 0;
202 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
203 list_commands();
204 return 1;
207 __dead static void
208 usage(int hflag)
210 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
211 if (hflag)
212 list_commands();
213 exit(1);
216 static const struct got_error *
217 get_editor(char **abspath)
219 const struct got_error *err = NULL;
220 const char *editor;
222 editor = getenv("VISUAL");
223 if (editor == NULL)
224 editor = getenv("EDITOR");
226 if (editor) {
227 err = got_path_find_prog(abspath, editor);
228 if (err)
229 return err;
232 if (*abspath == NULL) {
233 *abspath = strdup("/bin/ed");
234 if (*abspath == NULL)
235 return got_error_from_errno("strdup");
238 return NULL;
241 static const struct got_error *
242 apply_unveil(const char *repo_path, int repo_read_only,
243 const char *worktree_path, int create_worktree)
245 const struct got_error *err;
247 #ifdef PROFILE
248 if (unveil("gmon.out", "rwc") != 0)
249 return got_error_from_errno2("unveil", "gmon.out");
250 #endif
251 if (create_worktree) {
252 /* Pre-create work tree path to avoid unveiling its parents. */
253 err = got_path_mkdir(worktree_path);
255 if (errno == EEXIST) {
256 if (got_path_dir_is_empty(worktree_path)) {
257 errno = 0;
258 err = NULL;
259 } else {
260 err = got_error_path(worktree_path,
261 GOT_ERR_DIR_NOT_EMPTY);
265 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
266 return err;
269 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
270 return got_error_from_errno2("unveil", repo_path);
272 if (worktree_path && unveil(worktree_path, "rwc") != 0)
273 return got_error_from_errno2("unveil", worktree_path);
275 if (unveil("/tmp", "rwc") != 0)
276 return got_error_from_errno2("unveil", "/tmp");
278 err = got_privsep_unveil_exec_helpers();
279 if (err != NULL)
280 return err;
282 if (unveil(NULL, NULL) != 0)
283 return got_error_from_errno("unveil");
285 return NULL;
288 __dead static void
289 usage_init(void)
291 fprintf(stderr, "usage: %s init path\n", getprogname());
292 exit(1);
295 static const struct got_error *
296 cmd_init(int argc, char *argv[])
298 const struct got_error *error = NULL;
299 char *repo_path = NULL;
300 int ch;
302 while ((ch = getopt(argc, argv, "")) != -1) {
303 switch (ch) {
304 default:
305 usage_init();
306 /* NOTREACHED */
310 argc -= optind;
311 argv += optind;
313 #ifndef PROFILE
314 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
315 err(1, "pledge");
316 #endif
317 if (argc != 1)
318 usage_init();
320 repo_path = strdup(argv[0]);
321 if (repo_path == NULL)
322 return got_error_from_errno("strdup");
324 got_path_strip_trailing_slashes(repo_path);
326 error = got_path_mkdir(repo_path);
327 if (error &&
328 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 goto done;
331 error = apply_unveil(repo_path, 0, NULL, 0);
332 if (error)
333 goto done;
335 error = got_repo_init(repo_path);
336 if (error != NULL)
337 goto done;
339 done:
340 free(repo_path);
341 return error;
344 __dead static void
345 usage_import(void)
347 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
348 "[-r repository-path] [-I pattern] path\n", getprogname());
349 exit(1);
352 int
353 spawn_editor(const char *editor, const char *file)
355 pid_t pid;
356 sig_t sighup, sigint, sigquit;
357 int st = -1;
359 sighup = signal(SIGHUP, SIG_IGN);
360 sigint = signal(SIGINT, SIG_IGN);
361 sigquit = signal(SIGQUIT, SIG_IGN);
363 switch (pid = fork()) {
364 case -1:
365 goto doneediting;
366 case 0:
367 execl(editor, editor, file, (char *)NULL);
368 _exit(127);
371 while (waitpid(pid, &st, 0) == -1)
372 if (errno != EINTR)
373 break;
375 doneediting:
376 (void)signal(SIGHUP, sighup);
377 (void)signal(SIGINT, sigint);
378 (void)signal(SIGQUIT, sigquit);
380 if (!WIFEXITED(st)) {
381 errno = EINTR;
382 return -1;
385 return WEXITSTATUS(st);
388 static const struct got_error *
389 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
390 const char *initial_content)
392 const struct got_error *err = NULL;
393 char buf[1024];
394 struct stat st, st2;
395 FILE *fp;
396 int content_changed = 0;
397 size_t len;
399 *logmsg = NULL;
401 if (stat(logmsg_path, &st) == -1)
402 return got_error_from_errno2("stat", logmsg_path);
404 if (spawn_editor(editor, logmsg_path) == -1)
405 return got_error_from_errno("failed spawning editor");
407 if (stat(logmsg_path, &st2) == -1)
408 return got_error_from_errno("stat");
410 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
411 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
412 "no changes made to commit message, aborting");
414 *logmsg = malloc(st2.st_size + 1);
415 if (*logmsg == NULL)
416 return got_error_from_errno("malloc");
417 (*logmsg)[0] = '\0';
418 len = 0;
420 fp = fopen(logmsg_path, "r");
421 if (fp == NULL) {
422 err = got_error_from_errno("fopen");
423 goto done;
425 while (fgets(buf, sizeof(buf), fp) != NULL) {
426 if (!content_changed && strcmp(buf, initial_content) != 0)
427 content_changed = 1;
428 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
429 continue; /* remove comments and leading empty lines */
430 len = strlcat(*logmsg, buf, st2.st_size);
432 fclose(fp);
434 while (len > 0 && (*logmsg)[len - 1] == '\n') {
435 (*logmsg)[len - 1] = '\0';
436 len--;
439 if (len == 0 || !content_changed)
440 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 "commit message cannot be empty, aborting");
442 done:
443 if (err) {
444 free(*logmsg);
445 *logmsg = NULL;
447 return err;
450 static const struct got_error *
451 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
452 const char *branch_name)
454 char *initial_content = NULL, *logmsg_path = NULL;
455 const struct got_error *err = NULL;
456 int fd;
458 if (asprintf(&initial_content,
459 "\n# %s to be imported to branch %s\n", path_dir,
460 branch_name) == -1)
461 return got_error_from_errno("asprintf");
463 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
464 if (err)
465 goto done;
467 dprintf(fd, initial_content);
468 close(fd);
470 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
471 done:
472 free(initial_content);
473 free(logmsg_path);
474 return err;
477 static const struct got_error *
478 import_progress(void *arg, const char *path)
480 printf("A %s\n", path);
481 return NULL;
484 static const struct got_error *
485 cmd_import(int argc, char *argv[])
487 const struct got_error *error = NULL;
488 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
489 char *editor = NULL;
490 const char *got_author = getenv("GOT_AUTHOR");
491 const char *branch_name = "master";
492 char *refname = NULL, *id_str = NULL;
493 struct got_repository *repo = NULL;
494 struct got_reference *branch_ref = NULL, *head_ref = NULL;
495 struct got_object_id *new_commit_id = NULL;
496 int ch;
497 struct got_pathlist_head ignores;
498 struct got_pathlist_entry *pe;
500 TAILQ_INIT(&ignores);
502 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
503 switch (ch) {
504 case 'b':
505 branch_name = optarg;
506 break;
507 case 'm':
508 logmsg = strdup(optarg);
509 if (logmsg == NULL) {
510 error = got_error_from_errno("strdup");
511 goto done;
513 break;
514 case 'r':
515 repo_path = realpath(optarg, NULL);
516 if (repo_path == NULL) {
517 error = got_error_from_errno("realpath");
518 goto done;
520 break;
521 case 'I':
522 if (optarg[0] == '\0')
523 break;
524 error = got_pathlist_insert(&pe, &ignores, optarg,
525 NULL);
526 if (error)
527 goto done;
528 break;
529 default:
530 usage_init();
531 /* NOTREACHED */
535 argc -= optind;
536 argv += optind;
538 #ifndef PROFILE
539 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
540 NULL) == -1)
541 err(1, "pledge");
542 #endif
543 if (argc != 1)
544 usage_import();
546 if (got_author == NULL) {
547 /* TODO: Look current user up in password database */
548 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
549 goto done;
552 if (repo_path == NULL) {
553 repo_path = getcwd(NULL, 0);
554 if (repo_path == NULL)
555 return got_error_from_errno("getcwd");
557 got_path_strip_trailing_slashes(repo_path);
558 error = got_repo_open(&repo, repo_path);
559 if (error)
560 goto done;
562 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
563 error = got_error_from_errno("asprintf");
564 goto done;
567 error = got_ref_open(&branch_ref, repo, refname, 0);
568 if (error) {
569 if (error->code != GOT_ERR_NOT_REF)
570 goto done;
571 } else {
572 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
573 "import target branch already exists");
574 goto done;
577 path_dir = realpath(argv[0], NULL);
578 if (path_dir == NULL) {
579 error = got_error_from_errno("realpath");
580 goto done;
582 got_path_strip_trailing_slashes(path_dir);
584 /*
585 * unveil(2) traverses exec(2); if an editor is used we have
586 * to apply unveil after the log message has been written.
587 */
588 if (logmsg == NULL || strlen(logmsg) == 0) {
589 error = get_editor(&editor);
590 if (error)
591 goto done;
592 error = collect_import_msg(&logmsg, editor, path_dir, refname);
593 if (error)
594 goto done;
597 if (unveil(path_dir, "r") != 0)
598 return got_error_from_errno2("unveil", path_dir);
600 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
601 if (error)
602 goto done;
604 error = got_repo_import(&new_commit_id, path_dir, logmsg,
605 got_author, &ignores, repo, import_progress, NULL);
606 if (error)
607 goto done;
609 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
610 if (error)
611 goto done;
613 error = got_ref_write(branch_ref, repo);
614 if (error)
615 goto done;
617 error = got_object_id_str(&id_str, new_commit_id);
618 if (error)
619 goto done;
621 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
622 if (error) {
623 if (error->code != GOT_ERR_NOT_REF)
624 goto done;
626 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
627 branch_ref);
628 if (error)
629 goto done;
631 error = got_ref_write(head_ref, repo);
632 if (error)
633 goto done;
636 printf("Created branch %s with commit %s\n",
637 got_ref_get_name(branch_ref), id_str);
638 done:
639 free(repo_path);
640 free(editor);
641 free(refname);
642 free(new_commit_id);
643 free(id_str);
644 if (branch_ref)
645 got_ref_close(branch_ref);
646 if (head_ref)
647 got_ref_close(head_ref);
648 return error;
651 __dead static void
652 usage_checkout(void)
654 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
655 "[-p prefix] repository-path [worktree-path]\n", getprogname());
656 exit(1);
659 static const struct got_error *
660 checkout_progress(void *arg, unsigned char status, const char *path)
662 char *worktree_path = arg;
664 /* Base commit bump happens silently. */
665 if (status == GOT_STATUS_BUMP_BASE)
666 return NULL;
668 while (path[0] == '/')
669 path++;
671 printf("%c %s/%s\n", status, worktree_path, path);
672 return NULL;
675 static const struct got_error *
676 check_cancelled(void *arg)
678 if (sigint_received || sigpipe_received)
679 return got_error(GOT_ERR_CANCELLED);
680 return NULL;
683 static const struct got_error *
684 check_linear_ancestry(struct got_object_id *commit_id,
685 struct got_object_id *base_commit_id, struct got_repository *repo)
687 const struct got_error *err = NULL;
688 struct got_object_id *yca_id;
690 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
691 commit_id, base_commit_id, repo);
692 if (err)
693 return err;
695 if (yca_id == NULL)
696 return got_error(GOT_ERR_ANCESTRY);
698 /*
699 * Require a straight line of history between the target commit
700 * and the work tree's base commit.
702 * Non-linear situations such as this require a rebase:
704 * (commit) D F (base_commit)
705 * \ /
706 * C E
707 * \ /
708 * B (yca)
709 * |
710 * A
712 * 'got update' only handles linear cases:
713 * Update forwards in time: A (base/yca) - B - C - D (commit)
714 * Update backwards in time: D (base) - C - B - A (commit/yca)
715 */
716 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
717 got_object_id_cmp(base_commit_id, yca_id) != 0)
718 return got_error(GOT_ERR_ANCESTRY);
720 free(yca_id);
721 return NULL;
724 static const struct got_error *
725 check_same_branch(struct got_object_id *commit_id,
726 struct got_reference *head_ref, struct got_repository *repo)
728 const struct got_error *err = NULL;
729 struct got_commit_graph *graph = NULL;
730 struct got_object_id *head_commit_id = NULL;
731 int is_same_branch = 0;
733 err = got_ref_resolve(&head_commit_id, repo, head_ref);
734 if (err)
735 goto done;
737 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
738 if (err)
739 goto done;
741 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
742 if (err)
743 goto done;
745 for (;;) {
746 struct got_object_id *id;
747 err = got_commit_graph_iter_next(&id, graph);
748 if (err) {
749 if (err->code == GOT_ERR_ITER_COMPLETED) {
750 err = NULL;
751 break;
752 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
753 break;
754 err = got_commit_graph_fetch_commits(graph, 1,
755 repo);
756 if (err)
757 break;
760 if (id) {
761 if (got_object_id_cmp(id, commit_id) == 0) {
762 is_same_branch = 1;
763 break;
767 done:
768 if (graph)
769 got_commit_graph_close(graph);
770 free(head_commit_id);
771 if (!err && !is_same_branch)
772 err = got_error(GOT_ERR_ANCESTRY);
773 return err;
776 static const struct got_error *
777 cmd_checkout(int argc, char *argv[])
779 const struct got_error *error = NULL;
780 struct got_repository *repo = NULL;
781 struct got_reference *head_ref = NULL;
782 struct got_worktree *worktree = NULL;
783 char *repo_path = NULL;
784 char *worktree_path = NULL;
785 const char *path_prefix = "";
786 const char *branch_name = GOT_REF_HEAD;
787 char *commit_id_str = NULL;
788 int ch, same_path_prefix;
790 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
791 switch (ch) {
792 case 'b':
793 branch_name = optarg;
794 break;
795 case 'c':
796 commit_id_str = strdup(optarg);
797 if (commit_id_str == NULL)
798 return got_error_from_errno("strdup");
799 break;
800 case 'p':
801 path_prefix = optarg;
802 break;
803 default:
804 usage_checkout();
805 /* NOTREACHED */
809 argc -= optind;
810 argv += optind;
812 #ifndef PROFILE
813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
814 "unveil", NULL) == -1)
815 err(1, "pledge");
816 #endif
817 if (argc == 1) {
818 char *cwd, *base, *dotgit;
819 repo_path = realpath(argv[0], NULL);
820 if (repo_path == NULL)
821 return got_error_from_errno2("realpath", argv[0]);
822 cwd = getcwd(NULL, 0);
823 if (cwd == NULL) {
824 error = got_error_from_errno("getcwd");
825 goto done;
827 if (path_prefix[0]) {
828 base = basename(path_prefix);
829 if (base == NULL) {
830 error = got_error_from_errno2("basename",
831 path_prefix);
832 goto done;
834 } else {
835 base = basename(repo_path);
836 if (base == NULL) {
837 error = got_error_from_errno2("basename",
838 repo_path);
839 goto done;
842 dotgit = strstr(base, ".git");
843 if (dotgit)
844 *dotgit = '\0';
845 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
846 error = got_error_from_errno("asprintf");
847 free(cwd);
848 goto done;
850 free(cwd);
851 } else if (argc == 2) {
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL) {
854 error = got_error_from_errno2("realpath", argv[0]);
855 goto done;
857 worktree_path = realpath(argv[1], NULL);
858 if (worktree_path == NULL) {
859 if (errno != ENOENT) {
860 error = got_error_from_errno2("realpath",
861 argv[1]);
862 goto done;
864 worktree_path = strdup(argv[1]);
865 if (worktree_path == NULL) {
866 error = got_error_from_errno("strdup");
867 goto done;
870 } else
871 usage_checkout();
873 got_path_strip_trailing_slashes(repo_path);
874 got_path_strip_trailing_slashes(worktree_path);
876 error = got_repo_open(&repo, repo_path);
877 if (error != NULL)
878 goto done;
880 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
881 if (error)
882 goto done;
884 error = got_ref_open(&head_ref, repo, branch_name, 0);
885 if (error != NULL)
886 goto done;
888 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
889 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
890 goto done;
892 error = got_worktree_open(&worktree, worktree_path);
893 if (error != NULL)
894 goto done;
896 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
897 path_prefix);
898 if (error != NULL)
899 goto done;
900 if (!same_path_prefix) {
901 error = got_error(GOT_ERR_PATH_PREFIX);
902 goto done;
905 if (commit_id_str) {
906 struct got_object_id *commit_id;
907 error = got_repo_match_object_id_prefix(&commit_id,
908 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
909 if (error != NULL)
910 goto done;
911 error = check_linear_ancestry(commit_id,
912 got_worktree_get_base_commit_id(worktree), repo);
913 if (error != NULL) {
914 free(commit_id);
915 goto done;
917 error = check_same_branch(commit_id, head_ref, repo);
918 if (error)
919 goto done;
920 error = got_worktree_set_base_commit_id(worktree, repo,
921 commit_id);
922 free(commit_id);
923 if (error)
924 goto done;
927 error = got_worktree_checkout_files(worktree, "", repo,
928 checkout_progress, worktree_path, check_cancelled, NULL);
929 if (error != NULL)
930 goto done;
932 printf("Now shut up and hack\n");
934 done:
935 free(commit_id_str);
936 free(repo_path);
937 free(worktree_path);
938 return error;
941 __dead static void
942 usage_update(void)
944 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
945 getprogname());
946 exit(1);
949 static const struct got_error *
950 update_progress(void *arg, unsigned char status, const char *path)
952 int *did_something = arg;
954 if (status == GOT_STATUS_EXISTS)
955 return NULL;
957 *did_something = 1;
959 /* Base commit bump happens silently. */
960 if (status == GOT_STATUS_BUMP_BASE)
961 return NULL;
963 while (path[0] == '/')
964 path++;
965 printf("%c %s\n", status, path);
966 return NULL;
969 static const struct got_error *
970 switch_head_ref(struct got_reference *head_ref,
971 struct got_object_id *commit_id, struct got_worktree *worktree,
972 struct got_repository *repo)
974 const struct got_error *err = NULL;
975 char *base_id_str;
976 int ref_has_moved = 0;
978 /* Trivial case: switching between two different references. */
979 if (strcmp(got_ref_get_name(head_ref),
980 got_worktree_get_head_ref_name(worktree)) != 0) {
981 printf("Switching work tree from %s to %s\n",
982 got_worktree_get_head_ref_name(worktree),
983 got_ref_get_name(head_ref));
984 return got_worktree_set_head_ref(worktree, head_ref);
987 err = check_linear_ancestry(commit_id,
988 got_worktree_get_base_commit_id(worktree), repo);
989 if (err) {
990 if (err->code != GOT_ERR_ANCESTRY)
991 return err;
992 ref_has_moved = 1;
994 if (!ref_has_moved)
995 return NULL;
997 /* Switching to a rebased branch with the same reference name. */
998 err = got_object_id_str(&base_id_str,
999 got_worktree_get_base_commit_id(worktree));
1000 if (err)
1001 return err;
1002 printf("Reference %s now points at a different branch\n",
1003 got_worktree_get_head_ref_name(worktree));
1004 printf("Switching work tree from %s to %s\n", base_id_str,
1005 got_worktree_get_head_ref_name(worktree));
1006 return NULL;
1009 static const struct got_error *
1010 cmd_update(int argc, char *argv[])
1012 const struct got_error *error = NULL;
1013 struct got_repository *repo = NULL;
1014 struct got_worktree *worktree = NULL;
1015 char *worktree_path = NULL, *path = NULL;
1016 struct got_object_id *commit_id = NULL;
1017 char *commit_id_str = NULL;
1018 const char *branch_name = NULL;
1019 struct got_reference *head_ref = NULL;
1020 int ch, did_something = 0, rebase_in_progress;
1022 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1023 switch (ch) {
1024 case 'b':
1025 branch_name = optarg;
1026 break;
1027 case 'c':
1028 commit_id_str = strdup(optarg);
1029 if (commit_id_str == NULL)
1030 return got_error_from_errno("strdup");
1031 break;
1032 default:
1033 usage_update();
1034 /* NOTREACHED */
1038 argc -= optind;
1039 argv += optind;
1041 #ifndef PROFILE
1042 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1043 "unveil", NULL) == -1)
1044 err(1, "pledge");
1045 #endif
1046 worktree_path = getcwd(NULL, 0);
1047 if (worktree_path == NULL) {
1048 error = got_error_from_errno("getcwd");
1049 goto done;
1051 error = got_worktree_open(&worktree, worktree_path);
1052 if (error)
1053 goto done;
1055 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1056 if (error)
1057 goto done;
1058 if (rebase_in_progress) {
1059 error = got_error(GOT_ERR_REBASING);
1060 goto done;
1063 if (argc == 0) {
1064 path = strdup("");
1065 if (path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1069 } else if (argc == 1) {
1070 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1071 if (error)
1072 goto done;
1073 } else
1074 usage_update();
1076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1077 if (error != NULL)
1078 goto done;
1080 error = apply_unveil(got_repo_get_path(repo), 0,
1081 got_worktree_get_root_path(worktree), 0);
1082 if (error)
1083 goto done;
1085 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1086 got_worktree_get_head_ref_name(worktree), 0);
1087 if (error != NULL)
1088 goto done;
1089 if (commit_id_str == NULL) {
1090 error = got_ref_resolve(&commit_id, repo, head_ref);
1091 if (error != NULL)
1092 goto done;
1093 error = got_object_id_str(&commit_id_str, commit_id);
1094 if (error != NULL)
1095 goto done;
1096 } else {
1097 error = got_repo_match_object_id_prefix(&commit_id,
1098 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1099 if (error != NULL)
1100 goto done;
1101 free(commit_id_str);
1102 error = got_object_id_str(&commit_id_str, commit_id);
1103 if (error)
1104 goto done;
1107 if (branch_name) {
1108 struct got_object_id *head_commit_id;
1109 if (strlen(path) != 0) {
1110 fprintf(stderr, "%s: switching between branches "
1111 "requires that the entire work tree "
1112 "gets updated, not just '%s'\n",
1113 getprogname(), path);
1114 error = got_error(GOT_ERR_BAD_PATH);
1115 goto done;
1117 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1118 if (error)
1119 goto done;
1120 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1121 free(head_commit_id);
1122 if (error != NULL)
1123 goto done;
1124 error = check_same_branch(commit_id, head_ref, repo);
1125 if (error)
1126 goto done;
1127 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1128 if (error)
1129 goto done;
1130 } else {
1131 error = check_linear_ancestry(commit_id,
1132 got_worktree_get_base_commit_id(worktree), repo);
1133 if (error != NULL) {
1134 if (error->code == GOT_ERR_ANCESTRY)
1135 error = got_error(GOT_ERR_BRANCH_MOVED);
1136 goto done;
1138 error = check_same_branch(commit_id, head_ref, repo);
1139 if (error)
1140 goto done;
1143 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1144 commit_id) != 0) {
1145 error = got_worktree_set_base_commit_id(worktree, repo,
1146 commit_id);
1147 if (error)
1148 goto done;
1151 error = got_worktree_checkout_files(worktree, path, repo,
1152 update_progress, &did_something, check_cancelled, NULL);
1153 if (error != NULL)
1154 goto done;
1156 if (did_something)
1157 printf("Updated to commit %s\n", commit_id_str);
1158 else
1159 printf("Already up-to-date\n");
1160 done:
1161 free(worktree_path);
1162 free(path);
1163 free(commit_id);
1164 free(commit_id_str);
1165 return error;
1168 static const struct got_error *
1169 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1170 int diff_context, struct got_repository *repo)
1172 const struct got_error *err = NULL;
1173 struct got_tree_object *tree1 = NULL, *tree2;
1174 struct got_object_qid *qid;
1175 char *id_str1 = NULL, *id_str2;
1176 struct got_diff_blob_output_unidiff_arg arg;
1178 err = got_object_open_as_tree(&tree2, repo,
1179 got_object_commit_get_tree_id(commit));
1180 if (err)
1181 return err;
1183 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1184 if (qid != NULL) {
1185 struct got_commit_object *pcommit;
1187 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1188 if (err)
1189 return err;
1191 err = got_object_open_as_tree(&tree1, repo,
1192 got_object_commit_get_tree_id(pcommit));
1193 got_object_commit_close(pcommit);
1194 if (err)
1195 return err;
1197 err = got_object_id_str(&id_str1, qid->id);
1198 if (err)
1199 return err;
1202 err = got_object_id_str(&id_str2, id);
1203 if (err)
1204 goto done;
1206 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1207 arg.diff_context = diff_context;
1208 arg.outfile = stdout;
1209 err = got_diff_tree(tree1, tree2, "", "", repo,
1210 got_diff_blob_output_unidiff, &arg);
1211 done:
1212 if (tree1)
1213 got_object_tree_close(tree1);
1214 got_object_tree_close(tree2);
1215 free(id_str1);
1216 free(id_str2);
1217 return err;
1220 static char *
1221 get_datestr(time_t *time, char *datebuf)
1223 char *p, *s = ctime_r(time, datebuf);
1224 p = strchr(s, '\n');
1225 if (p)
1226 *p = '\0';
1227 return s;
1230 static const struct got_error *
1231 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1232 struct got_repository *repo, int show_patch, int diff_context,
1233 struct got_reflist_head *refs)
1235 const struct got_error *err = NULL;
1236 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1237 char datebuf[26];
1238 time_t committer_time;
1239 const char *author, *committer;
1240 char *refs_str = NULL;
1241 struct got_reflist_entry *re;
1243 SIMPLEQ_FOREACH(re, refs, entry) {
1244 char *s;
1245 const char *name;
1246 if (got_object_id_cmp(re->id, id) != 0)
1247 continue;
1248 name = got_ref_get_name(re->ref);
1249 if (strcmp(name, GOT_REF_HEAD) == 0)
1250 continue;
1251 if (strncmp(name, "refs/", 5) == 0)
1252 name += 5;
1253 if (strncmp(name, "got/", 4) == 0)
1254 continue;
1255 if (strncmp(name, "heads/", 6) == 0)
1256 name += 6;
1257 if (strncmp(name, "remotes/", 8) == 0)
1258 name += 8;
1259 s = refs_str;
1260 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1261 name) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 free(s);
1264 break;
1266 free(s);
1268 err = got_object_id_str(&id_str, id);
1269 if (err)
1270 return err;
1272 printf("-----------------------------------------------\n");
1273 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1274 refs_str ? refs_str : "", refs_str ? ")" : "");
1275 free(id_str);
1276 id_str = NULL;
1277 free(refs_str);
1278 refs_str = NULL;
1279 printf("from: %s\n", got_object_commit_get_author(commit));
1280 committer_time = got_object_commit_get_committer_time(commit);
1281 datestr = get_datestr(&committer_time, datebuf);
1282 printf("date: %s UTC\n", datestr);
1283 author = got_object_commit_get_author(commit);
1284 committer = got_object_commit_get_committer(commit);
1285 if (strcmp(author, committer) != 0)
1286 printf("via: %s\n", committer);
1287 if (got_object_commit_get_nparents(commit) > 1) {
1288 const struct got_object_id_queue *parent_ids;
1289 struct got_object_qid *qid;
1290 int n = 1;
1291 parent_ids = got_object_commit_get_parent_ids(commit);
1292 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1293 err = got_object_id_str(&id_str, qid->id);
1294 if (err)
1295 return err;
1296 printf("parent %d: %s\n", n++, id_str);
1297 free(id_str);
1301 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1302 if (logmsg0 == NULL)
1303 return got_error_from_errno("strdup");
1305 logmsg = logmsg0;
1306 do {
1307 line = strsep(&logmsg, "\n");
1308 if (line)
1309 printf(" %s\n", line);
1310 } while (line);
1311 free(logmsg0);
1313 if (show_patch) {
1314 err = print_patch(commit, id, diff_context, repo);
1315 if (err == 0)
1316 printf("\n");
1319 if (fflush(stdout) != 0 && err == NULL)
1320 err = got_error_from_errno("fflush");
1321 return err;
1324 static const struct got_error *
1325 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1326 char *path, int show_patch, int diff_context, int limit,
1327 int first_parent_traversal, struct got_reflist_head *refs)
1329 const struct got_error *err;
1330 struct got_commit_graph *graph;
1332 err = got_commit_graph_open(&graph, root_id, path,
1333 first_parent_traversal, repo);
1334 if (err)
1335 return err;
1336 err = got_commit_graph_iter_start(graph, root_id, repo);
1337 if (err)
1338 goto done;
1339 for (;;) {
1340 struct got_commit_object *commit;
1341 struct got_object_id *id;
1343 if (sigint_received || sigpipe_received)
1344 break;
1346 err = got_commit_graph_iter_next(&id, graph);
1347 if (err) {
1348 if (err->code == GOT_ERR_ITER_COMPLETED) {
1349 err = NULL;
1350 break;
1352 if (err->code != GOT_ERR_ITER_NEED_MORE)
1353 break;
1354 err = got_commit_graph_fetch_commits(graph, 1, repo);
1355 if (err)
1356 break;
1357 else
1358 continue;
1360 if (id == NULL)
1361 break;
1363 err = got_object_open_as_commit(&commit, repo, id);
1364 if (err)
1365 break;
1366 err = print_commit(commit, id, repo, show_patch, diff_context,
1367 refs);
1368 got_object_commit_close(commit);
1369 if (err || (limit && --limit == 0))
1370 break;
1372 done:
1373 got_commit_graph_close(graph);
1374 return err;
1377 __dead static void
1378 usage_log(void)
1380 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1381 "[-r repository-path] [path]\n", getprogname());
1382 exit(1);
1385 static const struct got_error *
1386 cmd_log(int argc, char *argv[])
1388 const struct got_error *error;
1389 struct got_repository *repo = NULL;
1390 struct got_worktree *worktree = NULL;
1391 struct got_commit_object *commit = NULL;
1392 struct got_object_id *id = NULL;
1393 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1394 char *start_commit = NULL;
1395 int diff_context = 3, ch;
1396 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1397 const char *errstr;
1398 struct got_reflist_head refs;
1400 SIMPLEQ_INIT(&refs);
1402 #ifndef PROFILE
1403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1404 NULL)
1405 == -1)
1406 err(1, "pledge");
1407 #endif
1409 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1410 switch (ch) {
1411 case 'p':
1412 show_patch = 1;
1413 break;
1414 case 'c':
1415 start_commit = optarg;
1416 break;
1417 case 'C':
1418 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1419 &errstr);
1420 if (errstr != NULL)
1421 err(1, "-C option %s", errstr);
1422 break;
1423 case 'l':
1424 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1425 if (errstr != NULL)
1426 err(1, "-l option %s", errstr);
1427 break;
1428 case 'f':
1429 first_parent_traversal = 1;
1430 break;
1431 case 'r':
1432 repo_path = realpath(optarg, NULL);
1433 if (repo_path == NULL)
1434 err(1, "-r option");
1435 got_path_strip_trailing_slashes(repo_path);
1436 break;
1437 default:
1438 usage_log();
1439 /* NOTREACHED */
1443 argc -= optind;
1444 argv += optind;
1446 cwd = getcwd(NULL, 0);
1447 if (cwd == NULL) {
1448 error = got_error_from_errno("getcwd");
1449 goto done;
1452 error = got_worktree_open(&worktree, cwd);
1453 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1454 goto done;
1455 error = NULL;
1457 if (argc == 0) {
1458 path = strdup("");
1459 if (path == NULL) {
1460 error = got_error_from_errno("strdup");
1461 goto done;
1463 } else if (argc == 1) {
1464 if (worktree) {
1465 error = got_worktree_resolve_path(&path, worktree,
1466 argv[0]);
1467 if (error)
1468 goto done;
1469 } else {
1470 path = strdup(argv[0]);
1471 if (path == NULL) {
1472 error = got_error_from_errno("strdup");
1473 goto done;
1476 } else
1477 usage_log();
1479 if (repo_path == NULL) {
1480 repo_path = worktree ?
1481 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1483 if (repo_path == NULL) {
1484 error = got_error_from_errno("strdup");
1485 goto done;
1488 error = got_repo_open(&repo, repo_path);
1489 if (error != NULL)
1490 goto done;
1492 error = apply_unveil(got_repo_get_path(repo), 1,
1493 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1494 if (error)
1495 goto done;
1497 if (start_commit == NULL) {
1498 struct got_reference *head_ref;
1499 error = got_ref_open(&head_ref, repo,
1500 worktree ? got_worktree_get_head_ref_name(worktree)
1501 : GOT_REF_HEAD, 0);
1502 if (error != NULL)
1503 return error;
1504 error = got_ref_resolve(&id, repo, head_ref);
1505 got_ref_close(head_ref);
1506 if (error != NULL)
1507 return error;
1508 error = got_object_open_as_commit(&commit, repo, id);
1509 } else {
1510 struct got_reference *ref;
1511 error = got_ref_open(&ref, repo, start_commit, 0);
1512 if (error == NULL) {
1513 int obj_type;
1514 error = got_ref_resolve(&id, repo, ref);
1515 got_ref_close(ref);
1516 if (error != NULL)
1517 goto done;
1518 error = got_object_get_type(&obj_type, repo, id);
1519 if (error != NULL)
1520 goto done;
1521 if (obj_type == GOT_OBJ_TYPE_TAG) {
1522 struct got_tag_object *tag;
1523 error = got_object_open_as_tag(&tag, repo, id);
1524 if (error != NULL)
1525 goto done;
1526 if (got_object_tag_get_object_type(tag) !=
1527 GOT_OBJ_TYPE_COMMIT) {
1528 got_object_tag_close(tag);
1529 error = got_error(GOT_ERR_OBJ_TYPE);
1530 goto done;
1532 free(id);
1533 id = got_object_id_dup(
1534 got_object_tag_get_object_id(tag));
1535 if (id == NULL)
1536 error = got_error_from_errno(
1537 "got_object_id_dup");
1538 got_object_tag_close(tag);
1539 if (error)
1540 goto done;
1541 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1542 error = got_error(GOT_ERR_OBJ_TYPE);
1543 goto done;
1545 error = got_object_open_as_commit(&commit, repo, id);
1546 if (error != NULL)
1547 goto done;
1549 if (commit == NULL) {
1550 error = got_repo_match_object_id_prefix(&id,
1551 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1552 if (error != NULL)
1553 return error;
1556 if (error != NULL)
1557 goto done;
1559 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1560 if (error != NULL)
1561 goto done;
1562 if (in_repo_path) {
1563 free(path);
1564 path = in_repo_path;
1567 error = got_ref_list(&refs, repo);
1568 if (error)
1569 goto done;
1571 error = print_commits(id, repo, path, show_patch,
1572 diff_context, limit, first_parent_traversal, &refs);
1573 done:
1574 free(path);
1575 free(repo_path);
1576 free(cwd);
1577 free(id);
1578 if (worktree)
1579 got_worktree_close(worktree);
1580 if (repo) {
1581 const struct got_error *repo_error;
1582 repo_error = got_repo_close(repo);
1583 if (error == NULL)
1584 error = repo_error;
1586 got_ref_list_free(&refs);
1587 return error;
1590 __dead static void
1591 usage_diff(void)
1593 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1594 "[object1 object2 | path]\n", getprogname());
1595 exit(1);
1598 struct print_diff_arg {
1599 struct got_repository *repo;
1600 struct got_worktree *worktree;
1601 int diff_context;
1602 const char *id_str;
1603 int header_shown;
1606 static const struct got_error *
1607 print_diff(void *arg, unsigned char status, const char *path,
1608 struct got_object_id *blob_id, struct got_object_id *commit_id)
1610 struct print_diff_arg *a = arg;
1611 const struct got_error *err = NULL;
1612 struct got_blob_object *blob1 = NULL;
1613 FILE *f2 = NULL;
1614 char *abspath = NULL;
1615 struct stat sb;
1617 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1618 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1619 return NULL;
1621 if (!a->header_shown) {
1622 printf("diff %s %s\n", a->id_str,
1623 got_worktree_get_root_path(a->worktree));
1624 a->header_shown = 1;
1627 if (status != GOT_STATUS_ADD) {
1628 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1629 if (err)
1630 goto done;
1634 if (status != GOT_STATUS_DELETE) {
1635 if (asprintf(&abspath, "%s/%s",
1636 got_worktree_get_root_path(a->worktree), path) == -1) {
1637 err = got_error_from_errno("asprintf");
1638 goto done;
1641 f2 = fopen(abspath, "r");
1642 if (f2 == NULL) {
1643 err = got_error_from_errno2("fopen", abspath);
1644 goto done;
1646 if (lstat(abspath, &sb) == -1) {
1647 err = got_error_from_errno2("lstat", abspath);
1648 goto done;
1650 } else
1651 sb.st_size = 0;
1653 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1654 stdout);
1655 done:
1656 if (blob1)
1657 got_object_blob_close(blob1);
1658 if (f2 && fclose(f2) != 0 && err == NULL)
1659 err = got_error_from_errno("fclose");
1660 free(abspath);
1661 return err;
1664 static const struct got_error *
1665 cmd_diff(int argc, char *argv[])
1667 const struct got_error *error;
1668 struct got_repository *repo = NULL;
1669 struct got_worktree *worktree = NULL;
1670 char *cwd = NULL, *repo_path = NULL;
1671 struct got_object_id *id1 = NULL, *id2 = NULL;
1672 const char *id_str1 = NULL, *id_str2 = NULL;
1673 char *label1 = NULL, *label2 = NULL;
1674 int type1, type2;
1675 int diff_context = 3, ch;
1676 const char *errstr;
1677 char *path = NULL;
1679 #ifndef PROFILE
1680 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1681 NULL) == -1)
1682 err(1, "pledge");
1683 #endif
1685 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1686 switch (ch) {
1687 case 'C':
1688 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1689 if (errstr != NULL)
1690 err(1, "-C option %s", errstr);
1691 break;
1692 case 'r':
1693 repo_path = realpath(optarg, NULL);
1694 if (repo_path == NULL)
1695 err(1, "-r option");
1696 got_path_strip_trailing_slashes(repo_path);
1697 break;
1698 default:
1699 usage_diff();
1700 /* NOTREACHED */
1704 argc -= optind;
1705 argv += optind;
1707 cwd = getcwd(NULL, 0);
1708 if (cwd == NULL) {
1709 error = got_error_from_errno("getcwd");
1710 goto done;
1712 error = got_worktree_open(&worktree, cwd);
1713 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1714 goto done;
1715 if (argc <= 1) {
1716 if (worktree == NULL) {
1717 error = got_error(GOT_ERR_NOT_WORKTREE);
1718 goto done;
1720 if (repo_path)
1721 errx(1,
1722 "-r option can't be used when diffing a work tree");
1723 repo_path = strdup(got_worktree_get_repo_path(worktree));
1724 if (repo_path == NULL) {
1725 error = got_error_from_errno("strdup");
1726 goto done;
1728 if (argc == 1) {
1729 error = got_worktree_resolve_path(&path, worktree,
1730 argv[0]);
1731 if (error)
1732 goto done;
1733 } else {
1734 path = strdup("");
1735 if (path == NULL) {
1736 error = got_error_from_errno("strdup");
1737 goto done;
1740 } else if (argc == 2) {
1741 id_str1 = argv[0];
1742 id_str2 = argv[1];
1743 if (worktree && repo_path == NULL) {
1744 repo_path =
1745 strdup(got_worktree_get_repo_path(worktree));
1746 if (repo_path == NULL) {
1747 error = got_error_from_errno("strdup");
1748 goto done;
1751 } else
1752 usage_diff();
1754 if (repo_path == NULL) {
1755 repo_path = getcwd(NULL, 0);
1756 if (repo_path == NULL)
1757 return got_error_from_errno("getcwd");
1760 error = got_repo_open(&repo, repo_path);
1761 free(repo_path);
1762 if (error != NULL)
1763 goto done;
1765 error = apply_unveil(got_repo_get_path(repo), 1,
1766 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1767 if (error)
1768 goto done;
1770 if (argc <= 1) {
1771 struct print_diff_arg arg;
1772 char *id_str;
1773 error = got_object_id_str(&id_str,
1774 got_worktree_get_base_commit_id(worktree));
1775 if (error)
1776 goto done;
1777 arg.repo = repo;
1778 arg.worktree = worktree;
1779 arg.diff_context = diff_context;
1780 arg.id_str = id_str;
1781 arg.header_shown = 0;
1783 error = got_worktree_status(worktree, path, repo, print_diff,
1784 &arg, check_cancelled, NULL);
1785 free(id_str);
1786 goto done;
1789 error = got_repo_match_object_id_prefix(&id1, id_str1,
1790 GOT_OBJ_TYPE_ANY, repo);
1791 if (error) {
1792 struct got_reference *ref;
1793 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1794 goto done;
1795 error = got_ref_open(&ref, repo, id_str1, 0);
1796 if (error != NULL)
1797 goto done;
1798 label1 = strdup(got_ref_get_name(ref));
1799 if (label1 == NULL) {
1800 error = got_error_from_errno("strdup");
1801 goto done;
1803 error = got_ref_resolve(&id1, repo, ref);
1804 got_ref_close(ref);
1805 if (error != NULL)
1806 goto done;
1807 } else {
1808 error = got_object_id_str(&label1, id1);
1809 if (label1 == NULL) {
1810 error = got_error_from_errno("strdup");
1811 goto done;
1815 error = got_repo_match_object_id_prefix(&id2, id_str2,
1816 GOT_OBJ_TYPE_ANY, repo);
1817 if (error) {
1818 struct got_reference *ref;
1819 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1820 goto done;
1821 error = got_ref_open(&ref, repo, id_str2, 0);
1822 if (error != NULL)
1823 goto done;
1824 label2 = strdup(got_ref_get_name(ref));
1825 if (label2 == NULL) {
1826 error = got_error_from_errno("strdup");
1827 goto done;
1829 error = got_ref_resolve(&id2, repo, ref);
1830 got_ref_close(ref);
1831 if (error != NULL)
1832 goto done;
1833 } else {
1834 error = got_object_id_str(&label2, id2);
1835 if (label2 == NULL) {
1836 error = got_error_from_errno("strdup");
1837 goto done;
1841 error = got_object_get_type(&type1, repo, id1);
1842 if (error)
1843 goto done;
1845 error = got_object_get_type(&type2, repo, id2);
1846 if (error)
1847 goto done;
1849 if (type1 != type2) {
1850 error = got_error(GOT_ERR_OBJ_TYPE);
1851 goto done;
1854 switch (type1) {
1855 case GOT_OBJ_TYPE_BLOB:
1856 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1857 diff_context, repo, stdout);
1858 break;
1859 case GOT_OBJ_TYPE_TREE:
1860 error = got_diff_objects_as_trees(id1, id2, "", "",
1861 diff_context, repo, stdout);
1862 break;
1863 case GOT_OBJ_TYPE_COMMIT:
1864 printf("diff %s %s\n", label1, label2);
1865 error = got_diff_objects_as_commits(id1, id2, diff_context,
1866 repo, stdout);
1867 break;
1868 default:
1869 error = got_error(GOT_ERR_OBJ_TYPE);
1872 done:
1873 free(label1);
1874 free(label2);
1875 free(id1);
1876 free(id2);
1877 free(path);
1878 if (worktree)
1879 got_worktree_close(worktree);
1880 if (repo) {
1881 const struct got_error *repo_error;
1882 repo_error = got_repo_close(repo);
1883 if (error == NULL)
1884 error = repo_error;
1886 return error;
1889 __dead static void
1890 usage_blame(void)
1892 fprintf(stderr,
1893 "usage: %s blame [-c commit] [-r repository-path] path\n",
1894 getprogname());
1895 exit(1);
1898 static const struct got_error *
1899 cmd_blame(int argc, char *argv[])
1901 const struct got_error *error;
1902 struct got_repository *repo = NULL;
1903 struct got_worktree *worktree = NULL;
1904 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1905 struct got_object_id *commit_id = NULL;
1906 char *commit_id_str = NULL;
1907 int ch;
1909 #ifndef PROFILE
1910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1911 NULL) == -1)
1912 err(1, "pledge");
1913 #endif
1915 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1916 switch (ch) {
1917 case 'c':
1918 commit_id_str = optarg;
1919 break;
1920 case 'r':
1921 repo_path = realpath(optarg, NULL);
1922 if (repo_path == NULL)
1923 err(1, "-r option");
1924 got_path_strip_trailing_slashes(repo_path);
1925 break;
1926 default:
1927 usage_blame();
1928 /* NOTREACHED */
1932 argc -= optind;
1933 argv += optind;
1935 if (argc == 1)
1936 path = argv[0];
1937 else
1938 usage_blame();
1940 cwd = getcwd(NULL, 0);
1941 if (cwd == NULL) {
1942 error = got_error_from_errno("getcwd");
1943 goto done;
1945 if (repo_path == NULL) {
1946 error = got_worktree_open(&worktree, cwd);
1947 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1948 goto done;
1949 else
1950 error = NULL;
1951 if (worktree) {
1952 repo_path =
1953 strdup(got_worktree_get_repo_path(worktree));
1954 if (repo_path == NULL)
1955 error = got_error_from_errno("strdup");
1956 if (error)
1957 goto done;
1958 } else {
1959 repo_path = strdup(cwd);
1960 if (repo_path == NULL) {
1961 error = got_error_from_errno("strdup");
1962 goto done;
1967 error = got_repo_open(&repo, repo_path);
1968 if (error != NULL)
1969 goto done;
1971 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1972 if (error)
1973 goto done;
1975 if (worktree) {
1976 const char *prefix = got_worktree_get_path_prefix(worktree);
1977 char *p, *worktree_subdir = cwd +
1978 strlen(got_worktree_get_root_path(worktree));
1979 if (asprintf(&p, "%s%s%s%s%s",
1980 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1981 worktree_subdir, worktree_subdir[0] ? "/" : "",
1982 path) == -1) {
1983 error = got_error_from_errno("asprintf");
1984 goto done;
1986 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1987 free(p);
1988 } else {
1989 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1991 if (error)
1992 goto done;
1994 if (commit_id_str == NULL) {
1995 struct got_reference *head_ref;
1996 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1997 if (error != NULL)
1998 goto done;
1999 error = got_ref_resolve(&commit_id, repo, head_ref);
2000 got_ref_close(head_ref);
2001 if (error != NULL)
2002 goto done;
2003 } else {
2004 error = got_repo_match_object_id_prefix(&commit_id,
2005 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2006 if (error != NULL)
2007 goto done;
2010 error = got_blame(in_repo_path, commit_id, repo, stdout);
2011 done:
2012 free(in_repo_path);
2013 free(repo_path);
2014 free(cwd);
2015 free(commit_id);
2016 if (worktree)
2017 got_worktree_close(worktree);
2018 if (repo) {
2019 const struct got_error *repo_error;
2020 repo_error = got_repo_close(repo);
2021 if (error == NULL)
2022 error = repo_error;
2024 return error;
2027 __dead static void
2028 usage_tree(void)
2030 fprintf(stderr,
2031 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2032 getprogname());
2033 exit(1);
2036 static void
2037 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2038 const char *root_path)
2040 int is_root_path = (strcmp(path, root_path) == 0);
2042 path += strlen(root_path);
2043 while (path[0] == '/')
2044 path++;
2046 printf("%s%s%s%s%s\n", id ? id : "", path,
2047 is_root_path ? "" : "/", te->name,
2048 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2051 static const struct got_error *
2052 print_tree(const char *path, struct got_object_id *commit_id,
2053 int show_ids, int recurse, const char *root_path,
2054 struct got_repository *repo)
2056 const struct got_error *err = NULL;
2057 struct got_object_id *tree_id = NULL;
2058 struct got_tree_object *tree = NULL;
2059 const struct got_tree_entries *entries;
2060 struct got_tree_entry *te;
2062 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2063 if (err)
2064 goto done;
2066 err = got_object_open_as_tree(&tree, repo, tree_id);
2067 if (err)
2068 goto done;
2069 entries = got_object_tree_get_entries(tree);
2070 te = SIMPLEQ_FIRST(&entries->head);
2071 while (te) {
2072 char *id = NULL;
2074 if (sigint_received || sigpipe_received)
2075 break;
2077 if (show_ids) {
2078 char *id_str;
2079 err = got_object_id_str(&id_str, te->id);
2080 if (err)
2081 goto done;
2082 if (asprintf(&id, "%s ", id_str) == -1) {
2083 err = got_error_from_errno("asprintf");
2084 free(id_str);
2085 goto done;
2087 free(id_str);
2089 print_entry(te, id, path, root_path);
2090 free(id);
2092 if (recurse && S_ISDIR(te->mode)) {
2093 char *child_path;
2094 if (asprintf(&child_path, "%s%s%s", path,
2095 path[0] == '/' && path[1] == '\0' ? "" : "/",
2096 te->name) == -1) {
2097 err = got_error_from_errno("asprintf");
2098 goto done;
2100 err = print_tree(child_path, commit_id, show_ids, 1,
2101 root_path, repo);
2102 free(child_path);
2103 if (err)
2104 goto done;
2107 te = SIMPLEQ_NEXT(te, entry);
2109 done:
2110 if (tree)
2111 got_object_tree_close(tree);
2112 free(tree_id);
2113 return err;
2116 static const struct got_error *
2117 cmd_tree(int argc, char *argv[])
2119 const struct got_error *error;
2120 struct got_repository *repo = NULL;
2121 struct got_worktree *worktree = NULL;
2122 const char *path;
2123 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2124 struct got_object_id *commit_id = NULL;
2125 char *commit_id_str = NULL;
2126 int show_ids = 0, recurse = 0;
2127 int ch;
2129 #ifndef PROFILE
2130 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2131 NULL) == -1)
2132 err(1, "pledge");
2133 #endif
2135 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2136 switch (ch) {
2137 case 'c':
2138 commit_id_str = optarg;
2139 break;
2140 case 'r':
2141 repo_path = realpath(optarg, NULL);
2142 if (repo_path == NULL)
2143 err(1, "-r option");
2144 got_path_strip_trailing_slashes(repo_path);
2145 break;
2146 case 'i':
2147 show_ids = 1;
2148 break;
2149 case 'R':
2150 recurse = 1;
2151 break;
2152 default:
2153 usage_tree();
2154 /* NOTREACHED */
2158 argc -= optind;
2159 argv += optind;
2161 if (argc == 1)
2162 path = argv[0];
2163 else if (argc > 1)
2164 usage_tree();
2165 else
2166 path = NULL;
2168 cwd = getcwd(NULL, 0);
2169 if (cwd == NULL) {
2170 error = got_error_from_errno("getcwd");
2171 goto done;
2173 if (repo_path == NULL) {
2174 error = got_worktree_open(&worktree, cwd);
2175 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2176 goto done;
2177 else
2178 error = NULL;
2179 if (worktree) {
2180 repo_path =
2181 strdup(got_worktree_get_repo_path(worktree));
2182 if (repo_path == NULL)
2183 error = got_error_from_errno("strdup");
2184 if (error)
2185 goto done;
2186 } else {
2187 repo_path = strdup(cwd);
2188 if (repo_path == NULL) {
2189 error = got_error_from_errno("strdup");
2190 goto done;
2195 error = got_repo_open(&repo, repo_path);
2196 if (error != NULL)
2197 goto done;
2199 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2200 if (error)
2201 goto done;
2203 if (path == NULL) {
2204 if (worktree) {
2205 char *p, *worktree_subdir = cwd +
2206 strlen(got_worktree_get_root_path(worktree));
2207 if (asprintf(&p, "%s/%s",
2208 got_worktree_get_path_prefix(worktree),
2209 worktree_subdir) == -1) {
2210 error = got_error_from_errno("asprintf");
2211 goto done;
2213 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2214 free(p);
2215 if (error)
2216 goto done;
2217 } else
2218 path = "/";
2220 if (in_repo_path == NULL) {
2221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2222 if (error != NULL)
2223 goto done;
2226 if (commit_id_str == NULL) {
2227 struct got_reference *head_ref;
2228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2229 if (error != NULL)
2230 goto done;
2231 error = got_ref_resolve(&commit_id, repo, head_ref);
2232 got_ref_close(head_ref);
2233 if (error != NULL)
2234 goto done;
2235 } else {
2236 error = got_repo_match_object_id_prefix(&commit_id,
2237 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2238 if (error != NULL)
2239 goto done;
2242 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2243 in_repo_path, repo);
2244 done:
2245 free(in_repo_path);
2246 free(repo_path);
2247 free(cwd);
2248 free(commit_id);
2249 if (worktree)
2250 got_worktree_close(worktree);
2251 if (repo) {
2252 const struct got_error *repo_error;
2253 repo_error = got_repo_close(repo);
2254 if (error == NULL)
2255 error = repo_error;
2257 return error;
2260 __dead static void
2261 usage_status(void)
2263 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2264 exit(1);
2267 static const struct got_error *
2268 print_status(void *arg, unsigned char status, const char *path,
2269 struct got_object_id *blob_id, struct got_object_id *commit_id)
2271 printf("%c %s\n", status, path);
2272 return NULL;
2275 static const struct got_error *
2276 cmd_status(int argc, char *argv[])
2278 const struct got_error *error = NULL;
2279 struct got_repository *repo = NULL;
2280 struct got_worktree *worktree = NULL;
2281 char *cwd = NULL, *path = NULL;
2282 int ch;
2284 while ((ch = getopt(argc, argv, "")) != -1) {
2285 switch (ch) {
2286 default:
2287 usage_status();
2288 /* NOTREACHED */
2292 argc -= optind;
2293 argv += optind;
2295 #ifndef PROFILE
2296 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2297 NULL) == -1)
2298 err(1, "pledge");
2299 #endif
2300 cwd = getcwd(NULL, 0);
2301 if (cwd == NULL) {
2302 error = got_error_from_errno("getcwd");
2303 goto done;
2306 error = got_worktree_open(&worktree, cwd);
2307 if (error != NULL)
2308 goto done;
2310 if (argc == 0) {
2311 path = strdup("");
2312 if (path == NULL) {
2313 error = got_error_from_errno("strdup");
2314 goto done;
2316 } else if (argc == 1) {
2317 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2318 if (error)
2319 goto done;
2320 } else
2321 usage_status();
2323 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2324 if (error != NULL)
2325 goto done;
2327 error = apply_unveil(got_repo_get_path(repo), 1,
2328 got_worktree_get_root_path(worktree), 0);
2329 if (error)
2330 goto done;
2332 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2333 check_cancelled, NULL);
2334 done:
2335 free(cwd);
2336 free(path);
2337 return error;
2340 __dead static void
2341 usage_ref(void)
2343 fprintf(stderr,
2344 "usage: %s ref [-r repository] -l | -d name | name target\n",
2345 getprogname());
2346 exit(1);
2349 static const struct got_error *
2350 list_refs(struct got_repository *repo)
2352 static const struct got_error *err = NULL;
2353 struct got_reflist_head refs;
2354 struct got_reflist_entry *re;
2356 SIMPLEQ_INIT(&refs);
2357 err = got_ref_list(&refs, repo);
2358 if (err)
2359 return err;
2361 SIMPLEQ_FOREACH(re, &refs, entry) {
2362 char *refstr;
2363 refstr = got_ref_to_str(re->ref);
2364 if (refstr == NULL)
2365 return got_error_from_errno("got_ref_to_str");
2366 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2367 free(refstr);
2370 got_ref_list_free(&refs);
2371 return NULL;
2374 static const struct got_error *
2375 delete_ref(struct got_repository *repo, const char *refname)
2377 const struct got_error *err = NULL;
2378 struct got_reference *ref;
2380 err = got_ref_open(&ref, repo, refname, 0);
2381 if (err)
2382 return err;
2384 err = got_ref_delete(ref, repo);
2385 got_ref_close(ref);
2386 return err;
2389 static const struct got_error *
2390 add_ref(struct got_repository *repo, const char *refname, const char *target)
2392 const struct got_error *err = NULL;
2393 struct got_object_id *id;
2394 struct got_reference *ref = NULL;
2397 * Don't let the user create a reference named '-'.
2398 * While technically a valid reference name, this case is usually
2399 * an unintended typo.
2401 if (refname[0] == '-' && refname[1] == '\0')
2402 return got_error(GOT_ERR_BAD_REF_NAME);
2404 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2405 repo);
2406 if (err) {
2407 struct got_reference *target_ref;
2409 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2410 return err;
2411 err = got_ref_open(&target_ref, repo, target, 0);
2412 if (err)
2413 return err;
2414 err = got_ref_resolve(&id, repo, target_ref);
2415 got_ref_close(target_ref);
2416 if (err)
2417 return err;
2420 err = got_ref_alloc(&ref, refname, id);
2421 if (err)
2422 goto done;
2424 err = got_ref_write(ref, repo);
2425 done:
2426 if (ref)
2427 got_ref_close(ref);
2428 free(id);
2429 return err;
2432 static const struct got_error *
2433 cmd_ref(int argc, char *argv[])
2435 const struct got_error *error = NULL;
2436 struct got_repository *repo = NULL;
2437 struct got_worktree *worktree = NULL;
2438 char *cwd = NULL, *repo_path = NULL;
2439 int ch, do_list = 0;
2440 const char *delref = NULL;
2442 /* TODO: Add -s option for adding symbolic references. */
2443 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2444 switch (ch) {
2445 case 'd':
2446 delref = optarg;
2447 break;
2448 case 'r':
2449 repo_path = realpath(optarg, NULL);
2450 if (repo_path == NULL)
2451 err(1, "-r option");
2452 got_path_strip_trailing_slashes(repo_path);
2453 break;
2454 case 'l':
2455 do_list = 1;
2456 break;
2457 default:
2458 usage_ref();
2459 /* NOTREACHED */
2463 if (do_list && delref)
2464 errx(1, "-l and -d options are mutually exclusive\n");
2466 argc -= optind;
2467 argv += optind;
2469 if (do_list || delref) {
2470 if (argc > 0)
2471 usage_ref();
2472 } else if (argc != 2)
2473 usage_ref();
2475 #ifndef PROFILE
2476 if (do_list) {
2477 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2478 NULL) == -1)
2479 err(1, "pledge");
2480 } else {
2481 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2482 "sendfd unveil", NULL) == -1)
2483 err(1, "pledge");
2485 #endif
2486 cwd = getcwd(NULL, 0);
2487 if (cwd == NULL) {
2488 error = got_error_from_errno("getcwd");
2489 goto done;
2492 if (repo_path == NULL) {
2493 error = got_worktree_open(&worktree, cwd);
2494 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2495 goto done;
2496 else
2497 error = NULL;
2498 if (worktree) {
2499 repo_path =
2500 strdup(got_worktree_get_repo_path(worktree));
2501 if (repo_path == NULL)
2502 error = got_error_from_errno("strdup");
2503 if (error)
2504 goto done;
2505 } else {
2506 repo_path = strdup(cwd);
2507 if (repo_path == NULL) {
2508 error = got_error_from_errno("strdup");
2509 goto done;
2514 error = got_repo_open(&repo, repo_path);
2515 if (error != NULL)
2516 goto done;
2518 error = apply_unveil(got_repo_get_path(repo), do_list,
2519 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2520 if (error)
2521 goto done;
2523 if (do_list)
2524 error = list_refs(repo);
2525 else if (delref)
2526 error = delete_ref(repo, delref);
2527 else
2528 error = add_ref(repo, argv[0], argv[1]);
2529 done:
2530 if (repo)
2531 got_repo_close(repo);
2532 if (worktree)
2533 got_worktree_close(worktree);
2534 free(cwd);
2535 free(repo_path);
2536 return error;
2539 __dead static void
2540 usage_branch(void)
2542 fprintf(stderr,
2543 "usage: %s branch [-r repository] -l | -d name | "
2544 "name [base-branch]\n", getprogname());
2545 exit(1);
2548 static const struct got_error *
2549 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2551 static const struct got_error *err = NULL;
2552 struct got_reflist_head refs;
2553 struct got_reflist_entry *re;
2555 SIMPLEQ_INIT(&refs);
2557 err = got_ref_list(&refs, repo);
2558 if (err)
2559 return err;
2561 SIMPLEQ_FOREACH(re, &refs, entry) {
2562 const char *refname, *marker = " ";
2563 char *refstr;
2564 refname = got_ref_get_name(re->ref);
2565 if (strncmp(refname, "refs/heads/", 11) != 0)
2566 continue;
2567 if (worktree && strcmp(refname,
2568 got_worktree_get_head_ref_name(worktree)) == 0) {
2569 struct got_object_id *id = NULL;
2570 err = got_ref_resolve(&id, repo, re->ref);
2571 if (err)
2572 return err;
2573 if (got_object_id_cmp(id,
2574 got_worktree_get_base_commit_id(worktree)) == 0)
2575 marker = "* ";
2576 else
2577 marker = "~ ";
2578 free(id);
2580 refname += 11;
2581 refstr = got_ref_to_str(re->ref);
2582 if (refstr == NULL)
2583 return got_error_from_errno("got_ref_to_str");
2584 printf("%s%s: %s\n", marker, refname, refstr);
2585 free(refstr);
2588 got_ref_list_free(&refs);
2589 return NULL;
2592 static const struct got_error *
2593 delete_branch(struct got_repository *repo, const char *branch_name)
2595 const struct got_error *err = NULL;
2596 struct got_reference *ref;
2597 char *refname;
2599 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2600 return got_error_from_errno("asprintf");
2602 err = got_ref_open(&ref, repo, refname, 0);
2603 if (err)
2604 goto done;
2606 err = got_ref_delete(ref, repo);
2607 got_ref_close(ref);
2608 done:
2609 free(refname);
2610 return err;
2613 static const struct got_error *
2614 add_branch(struct got_repository *repo, const char *branch_name,
2615 const char *base_branch)
2617 const struct got_error *err = NULL;
2618 struct got_object_id *id = NULL;
2619 struct got_reference *ref = NULL;
2620 char *base_refname = NULL, *refname = NULL;
2621 struct got_reference *base_ref;
2624 * Don't let the user create a branch named '-'.
2625 * While technically a valid reference name, this case is usually
2626 * an unintended typo.
2628 if (branch_name[0] == '-' && branch_name[1] == '\0')
2629 return got_error(GOT_ERR_BAD_REF_NAME);
2631 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2632 base_refname = strdup(GOT_REF_HEAD);
2633 if (base_refname == NULL)
2634 return got_error_from_errno("strdup");
2635 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2636 return got_error_from_errno("asprintf");
2638 err = got_ref_open(&base_ref, repo, base_refname, 0);
2639 if (err)
2640 goto done;
2641 err = got_ref_resolve(&id, repo, base_ref);
2642 got_ref_close(base_ref);
2643 if (err)
2644 goto done;
2646 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2647 err = got_error_from_errno("asprintf");
2648 goto done;
2651 err = got_ref_open(&ref, repo, refname, 0);
2652 if (err == NULL) {
2653 err = got_error(GOT_ERR_BRANCH_EXISTS);
2654 goto done;
2655 } else if (err->code != GOT_ERR_NOT_REF)
2656 goto done;
2658 err = got_ref_alloc(&ref, refname, id);
2659 if (err)
2660 goto done;
2662 err = got_ref_write(ref, repo);
2663 done:
2664 if (ref)
2665 got_ref_close(ref);
2666 free(id);
2667 free(base_refname);
2668 free(refname);
2669 return err;
2672 static const struct got_error *
2673 cmd_branch(int argc, char *argv[])
2675 const struct got_error *error = NULL;
2676 struct got_repository *repo = NULL;
2677 struct got_worktree *worktree = NULL;
2678 char *cwd = NULL, *repo_path = NULL;
2679 int ch, do_list = 0;
2680 const char *delref = NULL;
2682 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2683 switch (ch) {
2684 case 'd':
2685 delref = optarg;
2686 break;
2687 case 'r':
2688 repo_path = realpath(optarg, NULL);
2689 if (repo_path == NULL)
2690 err(1, "-r option");
2691 got_path_strip_trailing_slashes(repo_path);
2692 break;
2693 case 'l':
2694 do_list = 1;
2695 break;
2696 default:
2697 usage_branch();
2698 /* NOTREACHED */
2702 if (do_list && delref)
2703 errx(1, "-l and -d options are mutually exclusive\n");
2705 argc -= optind;
2706 argv += optind;
2708 if (do_list || delref) {
2709 if (argc > 0)
2710 usage_branch();
2711 } else if (argc < 1 || argc > 2)
2712 usage_branch();
2714 #ifndef PROFILE
2715 if (do_list) {
2716 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2717 NULL) == -1)
2718 err(1, "pledge");
2719 } else {
2720 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2721 "sendfd unveil", NULL) == -1)
2722 err(1, "pledge");
2724 #endif
2725 cwd = getcwd(NULL, 0);
2726 if (cwd == NULL) {
2727 error = got_error_from_errno("getcwd");
2728 goto done;
2731 if (repo_path == NULL) {
2732 error = got_worktree_open(&worktree, cwd);
2733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2734 goto done;
2735 else
2736 error = NULL;
2737 if (worktree) {
2738 repo_path =
2739 strdup(got_worktree_get_repo_path(worktree));
2740 if (repo_path == NULL)
2741 error = got_error_from_errno("strdup");
2742 if (error)
2743 goto done;
2744 } else {
2745 repo_path = strdup(cwd);
2746 if (repo_path == NULL) {
2747 error = got_error_from_errno("strdup");
2748 goto done;
2753 error = got_repo_open(&repo, repo_path);
2754 if (error != NULL)
2755 goto done;
2757 error = apply_unveil(got_repo_get_path(repo), do_list,
2758 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2759 if (error)
2760 goto done;
2762 if (do_list)
2763 error = list_branches(repo, worktree);
2764 else if (delref)
2765 error = delete_branch(repo, delref);
2766 else {
2767 const char *base_branch;
2768 if (argc == 1) {
2769 base_branch = worktree ?
2770 got_worktree_get_head_ref_name(worktree) :
2771 GOT_REF_HEAD;
2772 } else
2773 base_branch = argv[1];
2774 error = add_branch(repo, argv[0], base_branch);
2776 done:
2777 if (repo)
2778 got_repo_close(repo);
2779 if (worktree)
2780 got_worktree_close(worktree);
2781 free(cwd);
2782 free(repo_path);
2783 return error;
2786 __dead static void
2787 usage_add(void)
2789 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2790 exit(1);
2793 static const struct got_error *
2794 cmd_add(int argc, char *argv[])
2796 const struct got_error *error = NULL;
2797 struct got_repository *repo = NULL;
2798 struct got_worktree *worktree = NULL;
2799 char *cwd = NULL;
2800 struct got_pathlist_head paths;
2801 struct got_pathlist_entry *pe;
2802 int ch, x;
2804 TAILQ_INIT(&paths);
2806 while ((ch = getopt(argc, argv, "")) != -1) {
2807 switch (ch) {
2808 default:
2809 usage_add();
2810 /* NOTREACHED */
2814 argc -= optind;
2815 argv += optind;
2817 #ifndef PROFILE
2818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2819 NULL) == -1)
2820 err(1, "pledge");
2821 #endif
2822 if (argc < 1)
2823 usage_add();
2825 cwd = getcwd(NULL, 0);
2826 if (cwd == NULL) {
2827 error = got_error_from_errno("getcwd");
2828 goto done;
2831 error = got_worktree_open(&worktree, cwd);
2832 if (error)
2833 goto done;
2835 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2836 if (error != NULL)
2837 goto done;
2839 error = apply_unveil(got_repo_get_path(repo), 1,
2840 got_worktree_get_root_path(worktree), 0);
2841 if (error)
2842 goto done;
2844 for (x = 0; x < argc; x++) {
2845 char *path = realpath(argv[x], NULL);
2846 if (path == NULL) {
2847 error = got_error_from_errno2("realpath", argv[x]);
2848 goto done;
2851 got_path_strip_trailing_slashes(path);
2852 error = got_pathlist_insert(&pe, &paths, path, NULL);
2853 if (error) {
2854 free(path);
2855 goto done;
2858 error = got_worktree_schedule_add(worktree, &paths, print_status,
2859 NULL, repo);
2860 done:
2861 if (repo)
2862 got_repo_close(repo);
2863 if (worktree)
2864 got_worktree_close(worktree);
2865 TAILQ_FOREACH(pe, &paths, entry)
2866 free((char *)pe->path);
2867 got_pathlist_free(&paths);
2868 free(cwd);
2869 return error;
2872 __dead static void
2873 usage_remove(void)
2875 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2876 exit(1);
2879 static const struct got_error *
2880 cmd_remove(int argc, char *argv[])
2882 const struct got_error *error = NULL;
2883 struct got_worktree *worktree = NULL;
2884 struct got_repository *repo = NULL;
2885 char *cwd = NULL;
2886 struct got_pathlist_head paths;
2887 struct got_pathlist_entry *pe;
2888 int ch, i, delete_local_mods = 0;
2890 TAILQ_INIT(&paths);
2892 while ((ch = getopt(argc, argv, "f")) != -1) {
2893 switch (ch) {
2894 case 'f':
2895 delete_local_mods = 1;
2896 break;
2897 default:
2898 usage_add();
2899 /* NOTREACHED */
2903 argc -= optind;
2904 argv += optind;
2906 #ifndef PROFILE
2907 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2908 NULL) == -1)
2909 err(1, "pledge");
2910 #endif
2911 if (argc < 1)
2912 usage_remove();
2914 cwd = getcwd(NULL, 0);
2915 if (cwd == NULL) {
2916 error = got_error_from_errno("getcwd");
2917 goto done;
2919 error = got_worktree_open(&worktree, cwd);
2920 if (error)
2921 goto done;
2923 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2924 if (error)
2925 goto done;
2927 error = apply_unveil(got_repo_get_path(repo), 1,
2928 got_worktree_get_root_path(worktree), 0);
2929 if (error)
2930 goto done;
2932 for (i = 0; i < argc; i++) {
2933 char *path = realpath(argv[i], NULL);
2934 if (path == NULL) {
2935 error = got_error_from_errno2("realpath", argv[i]);
2936 goto done;
2939 got_path_strip_trailing_slashes(path);
2940 error = got_pathlist_insert(&pe, &paths, path, NULL);
2941 if (error) {
2942 free(path);
2943 goto done;
2946 error = got_worktree_schedule_delete(worktree, &paths,
2947 delete_local_mods, print_status, NULL, repo);
2948 if (error)
2949 goto done;
2950 done:
2951 if (repo)
2952 got_repo_close(repo);
2953 if (worktree)
2954 got_worktree_close(worktree);
2955 TAILQ_FOREACH(pe, &paths, entry)
2956 free((char *)pe->path);
2957 got_pathlist_free(&paths);
2958 free(cwd);
2959 return error;
2962 __dead static void
2963 usage_revert(void)
2965 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2966 exit(1);
2969 static const struct got_error *
2970 revert_progress(void *arg, unsigned char status, const char *path)
2972 while (path[0] == '/')
2973 path++;
2974 printf("%c %s\n", status, path);
2975 return NULL;
2978 static const struct got_error *
2979 cmd_revert(int argc, char *argv[])
2981 const struct got_error *error = NULL;
2982 struct got_worktree *worktree = NULL;
2983 struct got_repository *repo = NULL;
2984 char *cwd = NULL, *path = NULL;
2985 struct got_pathlist_head paths;
2986 struct got_pathlist_entry *pe;
2987 int ch, i;
2989 TAILQ_INIT(&paths);
2991 while ((ch = getopt(argc, argv, "")) != -1) {
2992 switch (ch) {
2993 default:
2994 usage_revert();
2995 /* NOTREACHED */
2999 argc -= optind;
3000 argv += optind;
3002 #ifndef PROFILE
3003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3004 "unveil", NULL) == -1)
3005 err(1, "pledge");
3006 #endif
3007 if (argc < 1)
3008 usage_revert();
3010 for (i = 0; i < argc; i++) {
3011 char *path = realpath(argv[i], NULL);
3012 if (path == NULL) {
3013 error = got_error_from_errno2("realpath", argv[i]);
3014 goto done;
3017 got_path_strip_trailing_slashes(path);
3018 error = got_pathlist_insert(&pe, &paths, path, NULL);
3019 if (error) {
3020 free(path);
3021 goto done;
3025 cwd = getcwd(NULL, 0);
3026 if (cwd == NULL) {
3027 error = got_error_from_errno("getcwd");
3028 goto done;
3030 error = got_worktree_open(&worktree, cwd);
3031 if (error)
3032 goto done;
3034 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3035 if (error != NULL)
3036 goto done;
3038 error = apply_unveil(got_repo_get_path(repo), 1,
3039 got_worktree_get_root_path(worktree), 0);
3040 if (error)
3041 goto done;
3043 error = got_worktree_revert(worktree, &paths,
3044 revert_progress, NULL, repo);
3045 if (error)
3046 goto done;
3047 done:
3048 if (repo)
3049 got_repo_close(repo);
3050 if (worktree)
3051 got_worktree_close(worktree);
3052 free(path);
3053 free(cwd);
3054 return error;
3057 __dead static void
3058 usage_commit(void)
3060 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3061 exit(1);
3064 struct collect_commit_logmsg_arg {
3065 const char *cmdline_log;
3066 const char *editor;
3067 const char *worktree_path;
3068 const char *branch_name;
3069 const char *repo_path;
3070 char *logmsg_path;
3074 static const struct got_error *
3075 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3076 void *arg)
3078 char *initial_content = NULL;
3079 struct got_pathlist_entry *pe;
3080 const struct got_error *err = NULL;
3081 char *template = NULL;
3082 struct collect_commit_logmsg_arg *a = arg;
3083 int fd;
3084 size_t len;
3086 /* if a message was specified on the command line, just use it */
3087 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3088 len = strlen(a->cmdline_log) + 1;
3089 *logmsg = malloc(len + 1);
3090 if (*logmsg == NULL)
3091 return got_error_from_errno("malloc");
3092 strlcpy(*logmsg, a->cmdline_log, len);
3093 return NULL;
3096 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3097 return got_error_from_errno("asprintf");
3099 if (asprintf(&initial_content,
3100 "\n# changes to be committed on branch %s:\n",
3101 a->branch_name) == -1)
3102 return got_error_from_errno("asprintf");
3104 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3105 if (err)
3106 goto done;
3108 dprintf(fd, initial_content);
3110 TAILQ_FOREACH(pe, commitable_paths, entry) {
3111 struct got_commitable *ct = pe->data;
3112 dprintf(fd, "# %c %s\n",
3113 got_commitable_get_status(ct),
3114 got_commitable_get_path(ct));
3116 close(fd);
3118 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3119 done:
3120 unlink(a->logmsg_path);
3121 free(a->logmsg_path);
3122 free(initial_content);
3123 free(template);
3125 /* Editor is done; we can now apply unveil(2) */
3126 if (err == NULL) {
3127 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3128 if (err) {
3129 free(*logmsg);
3130 *logmsg = NULL;
3133 return err;
3136 static const struct got_error *
3137 cmd_commit(int argc, char *argv[])
3139 const struct got_error *error = NULL;
3140 struct got_worktree *worktree = NULL;
3141 struct got_repository *repo = NULL;
3142 char *cwd = NULL, *path = NULL, *id_str = NULL;
3143 struct got_object_id *id = NULL;
3144 const char *logmsg = NULL;
3145 const char *got_author = getenv("GOT_AUTHOR");
3146 struct collect_commit_logmsg_arg cl_arg;
3147 char *editor = NULL;
3148 int ch, rebase_in_progress;
3150 while ((ch = getopt(argc, argv, "m:")) != -1) {
3151 switch (ch) {
3152 case 'm':
3153 logmsg = optarg;
3154 break;
3155 default:
3156 usage_commit();
3157 /* NOTREACHED */
3161 argc -= optind;
3162 argv += optind;
3164 #ifndef PROFILE
3165 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3166 "unveil", NULL) == -1)
3167 err(1, "pledge");
3168 #endif
3169 if (argc == 1) {
3170 path = realpath(argv[0], NULL);
3171 if (path == NULL) {
3172 error = got_error_from_errno2("realpath", argv[0]);
3173 goto done;
3175 got_path_strip_trailing_slashes(path);
3176 } else if (argc != 0)
3177 usage_commit();
3179 if (got_author == NULL) {
3180 /* TODO: Look current user up in password database */
3181 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3182 goto done;
3185 cwd = getcwd(NULL, 0);
3186 if (cwd == NULL) {
3187 error = got_error_from_errno("getcwd");
3188 goto done;
3190 error = got_worktree_open(&worktree, cwd);
3191 if (error)
3192 goto done;
3194 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3195 if (error)
3196 goto done;
3197 if (rebase_in_progress) {
3198 error = got_error(GOT_ERR_REBASING);
3199 goto done;
3202 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3203 if (error != NULL)
3204 goto done;
3207 * unveil(2) traverses exec(2); if an editor is used we have
3208 * to apply unveil after the log message has been written.
3210 if (logmsg == NULL || strlen(logmsg) == 0)
3211 error = get_editor(&editor);
3212 else
3213 error = apply_unveil(got_repo_get_path(repo), 0,
3214 got_worktree_get_root_path(worktree), 0);
3215 if (error)
3216 goto done;
3218 cl_arg.editor = editor;
3219 cl_arg.cmdline_log = logmsg;
3220 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3221 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3222 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3223 cl_arg.branch_name += 5;
3224 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3225 cl_arg.branch_name += 6;
3226 cl_arg.repo_path = got_repo_get_path(repo);
3227 cl_arg.logmsg_path = NULL;
3228 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3229 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3230 if (error) {
3231 if (cl_arg.logmsg_path)
3232 fprintf(stderr, "%s: log message preserved in %s\n",
3233 getprogname(), cl_arg.logmsg_path);
3234 goto done;
3237 if (cl_arg.logmsg_path)
3238 unlink(cl_arg.logmsg_path);
3240 error = got_object_id_str(&id_str, id);
3241 if (error)
3242 goto done;
3243 printf("Created commit %s\n", id_str);
3244 done:
3245 if (repo)
3246 got_repo_close(repo);
3247 if (worktree)
3248 got_worktree_close(worktree);
3249 free(path);
3250 free(cwd);
3251 free(id_str);
3252 free(editor);
3253 return error;
3256 __dead static void
3257 usage_cherrypick(void)
3259 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3260 exit(1);
3263 static const struct got_error *
3264 cmd_cherrypick(int argc, char *argv[])
3266 const struct got_error *error = NULL;
3267 struct got_worktree *worktree = NULL;
3268 struct got_repository *repo = NULL;
3269 char *cwd = NULL, *commit_id_str = NULL;
3270 struct got_object_id *commit_id = NULL;
3271 struct got_commit_object *commit = NULL;
3272 struct got_object_qid *pid;
3273 struct got_reference *head_ref = NULL;
3274 int ch, did_something = 0;
3276 while ((ch = getopt(argc, argv, "")) != -1) {
3277 switch (ch) {
3278 default:
3279 usage_cherrypick();
3280 /* NOTREACHED */
3284 argc -= optind;
3285 argv += optind;
3287 #ifndef PROFILE
3288 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3289 "unveil", NULL) == -1)
3290 err(1, "pledge");
3291 #endif
3292 if (argc != 1)
3293 usage_cherrypick();
3295 cwd = getcwd(NULL, 0);
3296 if (cwd == NULL) {
3297 error = got_error_from_errno("getcwd");
3298 goto done;
3300 error = got_worktree_open(&worktree, cwd);
3301 if (error)
3302 goto done;
3304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3305 if (error != NULL)
3306 goto done;
3308 error = apply_unveil(got_repo_get_path(repo), 0,
3309 got_worktree_get_root_path(worktree), 0);
3310 if (error)
3311 goto done;
3313 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3314 GOT_OBJ_TYPE_COMMIT, repo);
3315 if (error != NULL) {
3316 struct got_reference *ref;
3317 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3318 goto done;
3319 error = got_ref_open(&ref, repo, argv[0], 0);
3320 if (error != NULL)
3321 goto done;
3322 error = got_ref_resolve(&commit_id, repo, ref);
3323 got_ref_close(ref);
3324 if (error != NULL)
3325 goto done;
3327 error = got_object_id_str(&commit_id_str, commit_id);
3328 if (error)
3329 goto done;
3331 error = got_ref_open(&head_ref, repo,
3332 got_worktree_get_head_ref_name(worktree), 0);
3333 if (error != NULL)
3334 goto done;
3336 error = check_same_branch(commit_id, head_ref, repo);
3337 if (error) {
3338 if (error->code != GOT_ERR_ANCESTRY)
3339 goto done;
3340 error = NULL;
3341 } else {
3342 error = got_error(GOT_ERR_SAME_BRANCH);
3343 goto done;
3346 error = got_object_open_as_commit(&commit, repo, commit_id);
3347 if (error)
3348 goto done;
3349 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3350 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3351 commit_id, repo, update_progress, &did_something, check_cancelled,
3352 NULL);
3353 if (error != NULL)
3354 goto done;
3356 if (did_something)
3357 printf("Merged commit %s\n", commit_id_str);
3358 done:
3359 if (commit)
3360 got_object_commit_close(commit);
3361 free(commit_id_str);
3362 if (head_ref)
3363 got_ref_close(head_ref);
3364 if (worktree)
3365 got_worktree_close(worktree);
3366 if (repo)
3367 got_repo_close(repo);
3368 return error;
3371 __dead static void
3372 usage_backout(void)
3374 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3375 exit(1);
3378 static const struct got_error *
3379 cmd_backout(int argc, char *argv[])
3381 const struct got_error *error = NULL;
3382 struct got_worktree *worktree = NULL;
3383 struct got_repository *repo = NULL;
3384 char *cwd = NULL, *commit_id_str = NULL;
3385 struct got_object_id *commit_id = NULL;
3386 struct got_commit_object *commit = NULL;
3387 struct got_object_qid *pid;
3388 struct got_reference *head_ref = NULL;
3389 int ch, did_something = 0;
3391 while ((ch = getopt(argc, argv, "")) != -1) {
3392 switch (ch) {
3393 default:
3394 usage_backout();
3395 /* NOTREACHED */
3399 argc -= optind;
3400 argv += optind;
3402 #ifndef PROFILE
3403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3404 "unveil", NULL) == -1)
3405 err(1, "pledge");
3406 #endif
3407 if (argc != 1)
3408 usage_backout();
3410 cwd = getcwd(NULL, 0);
3411 if (cwd == NULL) {
3412 error = got_error_from_errno("getcwd");
3413 goto done;
3415 error = got_worktree_open(&worktree, cwd);
3416 if (error)
3417 goto done;
3419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3420 if (error != NULL)
3421 goto done;
3423 error = apply_unveil(got_repo_get_path(repo), 0,
3424 got_worktree_get_root_path(worktree), 0);
3425 if (error)
3426 goto done;
3428 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3429 GOT_OBJ_TYPE_COMMIT, repo);
3430 if (error != NULL) {
3431 struct got_reference *ref;
3432 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3433 goto done;
3434 error = got_ref_open(&ref, repo, argv[0], 0);
3435 if (error != NULL)
3436 goto done;
3437 error = got_ref_resolve(&commit_id, repo, ref);
3438 got_ref_close(ref);
3439 if (error != NULL)
3440 goto done;
3442 error = got_object_id_str(&commit_id_str, commit_id);
3443 if (error)
3444 goto done;
3446 error = got_ref_open(&head_ref, repo,
3447 got_worktree_get_head_ref_name(worktree), 0);
3448 if (error != NULL)
3449 goto done;
3451 error = check_same_branch(commit_id, head_ref, repo);
3452 if (error)
3453 goto done;
3455 error = got_object_open_as_commit(&commit, repo, commit_id);
3456 if (error)
3457 goto done;
3458 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3459 if (pid == NULL) {
3460 error = got_error(GOT_ERR_ROOT_COMMIT);
3461 goto done;
3464 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3465 update_progress, &did_something, check_cancelled, NULL);
3466 if (error != NULL)
3467 goto done;
3469 if (did_something)
3470 printf("Backed out commit %s\n", commit_id_str);
3471 done:
3472 if (commit)
3473 got_object_commit_close(commit);
3474 free(commit_id_str);
3475 if (head_ref)
3476 got_ref_close(head_ref);
3477 if (worktree)
3478 got_worktree_close(worktree);
3479 if (repo)
3480 got_repo_close(repo);
3481 return error;
3484 __dead static void
3485 usage_rebase(void)
3487 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3488 getprogname());
3489 exit(1);
3492 static const struct got_error *
3493 show_rebase_progress(struct got_commit_object *commit,
3494 struct got_object_id *old_id, struct got_object_id *new_id)
3496 const struct got_error *err;
3497 char *old_id_str = NULL, *new_id_str = NULL;
3498 char *logmsg0 = NULL, *logmsg, *nl;
3499 size_t len;
3501 err = got_object_id_str(&old_id_str, old_id);
3502 if (err)
3503 goto done;
3505 if (new_id) {
3506 err = got_object_id_str(&new_id_str, new_id);
3507 if (err)
3508 goto done;
3511 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3512 if (logmsg0 == NULL) {
3513 err = got_error_from_errno("strdup");
3514 goto done;
3516 logmsg = logmsg0;
3518 while (isspace((unsigned char)logmsg[0]))
3519 logmsg++;
3521 old_id_str[12] = '\0';
3522 if (new_id_str)
3523 new_id_str[12] = '\0';
3524 len = strlen(logmsg);
3525 if (len > 42)
3526 len = 42;
3527 logmsg[len] = '\0';
3528 nl = strchr(logmsg, '\n');
3529 if (nl)
3530 *nl = '\0';
3531 printf("%s -> %s: %s\n", old_id_str,
3532 new_id_str ? new_id_str : "no-op change", logmsg);
3533 done:
3534 free(old_id_str);
3535 free(new_id_str);
3536 free(logmsg0);
3537 return err;
3540 static const struct got_error *
3541 rebase_progress(void *arg, unsigned char status, const char *path)
3543 unsigned char *rebase_status = arg;
3545 while (path[0] == '/')
3546 path++;
3547 printf("%c %s\n", status, path);
3549 if (*rebase_status == GOT_STATUS_CONFLICT)
3550 return NULL;
3551 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3552 *rebase_status = status;
3553 return NULL;
3556 static const struct got_error *
3557 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3558 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3559 struct got_repository *repo)
3561 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3562 return got_worktree_rebase_complete(worktree,
3563 new_base_branch, tmp_branch, branch, repo);
3566 static const struct got_error *
3567 rebase_commit(struct got_pathlist_head *merged_paths,
3568 struct got_worktree *worktree, struct got_reference *tmp_branch,
3569 struct got_object_id *commit_id, struct got_repository *repo)
3571 const struct got_error *error;
3572 struct got_commit_object *commit;
3573 struct got_object_id *new_commit_id;
3575 error = got_object_open_as_commit(&commit, repo, commit_id);
3576 if (error)
3577 return error;
3579 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3580 worktree, tmp_branch, commit, commit_id, repo);
3581 if (error) {
3582 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3583 goto done;
3584 error = show_rebase_progress(commit, commit_id, NULL);
3585 } else {
3586 error = show_rebase_progress(commit, commit_id, new_commit_id);
3587 free(new_commit_id);
3589 done:
3590 got_object_commit_close(commit);
3591 return error;
3594 struct check_path_prefix_arg {
3595 const char *path_prefix;
3596 size_t len;
3599 static const struct got_error *
3600 check_path_prefix(void *arg, struct got_blob_object *blob1,
3601 struct got_blob_object *blob2, struct got_object_id *id1,
3602 struct got_object_id *id2, const char *path1, const char *path2,
3603 struct got_repository *repo)
3605 struct check_path_prefix_arg *a = arg;
3607 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3608 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3609 return got_error(GOT_ERR_REBASE_PATH);
3611 return NULL;
3614 static const struct got_error *
3615 rebase_check_path_prefix(struct got_object_id *parent_id,
3616 struct got_object_id *commit_id, const char *path_prefix,
3617 struct got_repository *repo)
3619 const struct got_error *err;
3620 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3621 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3622 struct check_path_prefix_arg cpp_arg;
3624 if (got_path_is_root_dir(path_prefix))
3625 return NULL;
3627 err = got_object_open_as_commit(&commit, repo, commit_id);
3628 if (err)
3629 goto done;
3631 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3632 if (err)
3633 goto done;
3635 err = got_object_open_as_tree(&tree1, repo,
3636 got_object_commit_get_tree_id(parent_commit));
3637 if (err)
3638 goto done;
3640 err = got_object_open_as_tree(&tree2, repo,
3641 got_object_commit_get_tree_id(commit));
3642 if (err)
3643 goto done;
3645 cpp_arg.path_prefix = path_prefix;
3646 cpp_arg.len = strlen(path_prefix);
3647 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3648 &cpp_arg);
3649 done:
3650 if (tree1)
3651 got_object_tree_close(tree1);
3652 if (tree2)
3653 got_object_tree_close(tree2);
3654 if (commit)
3655 got_object_commit_close(commit);
3656 if (parent_commit)
3657 got_object_commit_close(parent_commit);
3658 return err;
3661 static const struct got_error *
3662 collect_commits_to_rebase(struct got_object_id_queue *commits,
3663 struct got_object_id *initial_commit_id,
3664 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3665 const char *path_prefix, struct got_repository *repo)
3667 const struct got_error *err = NULL;
3668 struct got_commit_graph *graph = NULL;
3669 struct got_object_id *parent_id = NULL;
3670 struct got_object_qid *qid;
3671 struct got_object_id *commit_id = initial_commit_id;
3673 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3674 if (err)
3675 return err;
3677 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3678 if (err)
3679 goto done;
3680 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3681 err = got_commit_graph_iter_next(&parent_id, graph);
3682 if (err) {
3683 if (err->code == GOT_ERR_ITER_COMPLETED) {
3684 err = got_error_msg(GOT_ERR_ANCESTRY,
3685 "ran out of commits to rebase before "
3686 "youngest common ancestor commit has "
3687 "been reached?!?");
3688 goto done;
3689 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3690 goto done;
3691 err = got_commit_graph_fetch_commits(graph, 1, repo);
3692 if (err)
3693 goto done;
3694 } else {
3695 err = rebase_check_path_prefix(parent_id, commit_id,
3696 path_prefix, repo);
3697 if (err)
3698 goto done;
3700 err = got_object_qid_alloc(&qid, commit_id);
3701 if (err)
3702 goto done;
3703 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3704 commit_id = parent_id;
3707 done:
3708 got_commit_graph_close(graph);
3709 return err;
3712 static const struct got_error *
3713 cmd_rebase(int argc, char *argv[])
3715 const struct got_error *error = NULL;
3716 struct got_worktree *worktree = NULL;
3717 struct got_repository *repo = NULL;
3718 char *cwd = NULL;
3719 struct got_reference *branch = NULL;
3720 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3721 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3722 struct got_object_id *resume_commit_id = NULL;
3723 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3724 struct got_commit_object *commit = NULL;
3725 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3726 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3727 struct got_object_id_queue commits;
3728 struct got_pathlist_head merged_paths;
3729 const struct got_object_id_queue *parent_ids;
3730 struct got_object_qid *qid, *pid;
3732 SIMPLEQ_INIT(&commits);
3733 TAILQ_INIT(&merged_paths);
3735 while ((ch = getopt(argc, argv, "ac")) != -1) {
3736 switch (ch) {
3737 case 'a':
3738 abort_rebase = 1;
3739 break;
3740 case 'c':
3741 continue_rebase = 1;
3742 break;
3743 default:
3744 usage_rebase();
3745 /* NOTREACHED */
3749 argc -= optind;
3750 argv += optind;
3752 #ifndef PROFILE
3753 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3754 "unveil", NULL) == -1)
3755 err(1, "pledge");
3756 #endif
3757 if (abort_rebase && continue_rebase)
3758 usage_rebase();
3759 else if (abort_rebase || continue_rebase) {
3760 if (argc != 0)
3761 usage_rebase();
3762 } else if (argc != 1)
3763 usage_rebase();
3765 cwd = getcwd(NULL, 0);
3766 if (cwd == NULL) {
3767 error = got_error_from_errno("getcwd");
3768 goto done;
3770 error = got_worktree_open(&worktree, cwd);
3771 if (error)
3772 goto done;
3774 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3775 if (error != NULL)
3776 goto done;
3778 error = apply_unveil(got_repo_get_path(repo), 0,
3779 got_worktree_get_root_path(worktree), 0);
3780 if (error)
3781 goto done;
3783 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3784 if (error)
3785 goto done;
3787 if (rebase_in_progress && abort_rebase) {
3788 int did_something;
3789 error = got_worktree_rebase_continue(&resume_commit_id,
3790 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3791 if (error)
3792 goto done;
3793 printf("Switching work tree to %s\n",
3794 got_ref_get_symref_target(new_base_branch));
3795 error = got_worktree_rebase_abort(worktree, repo,
3796 new_base_branch, update_progress, &did_something);
3797 if (error)
3798 goto done;
3799 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3800 goto done; /* nothing else to do */
3801 } else if (abort_rebase) {
3802 error = got_error(GOT_ERR_NOT_REBASING);
3803 goto done;
3806 if (continue_rebase) {
3807 error = got_worktree_rebase_continue(&resume_commit_id,
3808 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3809 if (error)
3810 goto done;
3812 error = rebase_commit(NULL, worktree, tmp_branch,
3813 resume_commit_id, repo);
3814 if (error)
3815 goto done;
3817 yca_id = got_object_id_dup(resume_commit_id);
3818 if (yca_id == NULL) {
3819 error = got_error_from_errno("got_object_id_dup");
3820 goto done;
3822 } else {
3823 error = got_ref_open(&branch, repo, argv[0], 0);
3824 if (error != NULL)
3825 goto done;
3827 error = check_same_branch(
3828 got_worktree_get_base_commit_id(worktree), branch, repo);
3829 if (error) {
3830 if (error->code != GOT_ERR_ANCESTRY)
3831 goto done;
3832 error = NULL;
3833 } else {
3834 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3835 "specified branch resolves to a commit which "
3836 "is already contained in work tree's branch");
3837 goto done;
3841 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3842 if (error)
3843 goto done;
3845 if (!continue_rebase) {
3846 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3847 got_worktree_get_base_commit_id(worktree),
3848 branch_head_commit_id, repo);
3849 if (error)
3850 goto done;
3851 if (yca_id == NULL) {
3852 error = got_error_msg(GOT_ERR_ANCESTRY,
3853 "specified branch shares no common ancestry "
3854 "with work tree's branch");
3855 goto done;
3858 error = got_worktree_rebase_prepare(&new_base_branch,
3859 &tmp_branch, worktree, branch, repo);
3860 if (error)
3861 goto done;
3864 commit_id = branch_head_commit_id;
3865 error = got_object_open_as_commit(&commit, repo, commit_id);
3866 if (error)
3867 goto done;
3869 parent_ids = got_object_commit_get_parent_ids(commit);
3870 pid = SIMPLEQ_FIRST(parent_ids);
3871 error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3872 yca_id, got_worktree_get_path_prefix(worktree), repo);
3873 got_object_commit_close(commit);
3874 commit = NULL;
3875 if (error)
3876 goto done;
3878 if (SIMPLEQ_EMPTY(&commits)) {
3879 if (continue_rebase)
3880 error = rebase_complete(worktree, branch,
3881 new_base_branch, tmp_branch, repo);
3882 else
3883 error = got_error(GOT_ERR_EMPTY_REBASE);
3884 goto done;
3887 pid = NULL;
3888 SIMPLEQ_FOREACH(qid, &commits, entry) {
3889 commit_id = qid->id;
3890 parent_id = pid ? pid->id : yca_id;
3891 pid = qid;
3893 error = got_worktree_rebase_merge_files(&merged_paths,
3894 worktree, parent_id, commit_id, repo, rebase_progress,
3895 &rebase_status, check_cancelled, NULL);
3896 if (error)
3897 goto done;
3899 if (rebase_status == GOT_STATUS_CONFLICT) {
3900 got_worktree_rebase_pathlist_free(&merged_paths);
3901 break;
3904 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3905 commit_id, repo);
3906 got_worktree_rebase_pathlist_free(&merged_paths);
3907 if (error)
3908 goto done;
3911 if (rebase_status == GOT_STATUS_CONFLICT) {
3912 error = got_worktree_rebase_postpone(worktree);
3913 if (error)
3914 goto done;
3915 error = got_error_msg(GOT_ERR_CONFLICTS,
3916 "conflicts must be resolved before rebasing can continue");
3917 } else
3918 error = rebase_complete(worktree, branch, new_base_branch,
3919 tmp_branch, repo);
3920 done:
3921 got_object_id_queue_free(&commits);
3922 free(branch_head_commit_id);
3923 free(resume_commit_id);
3924 free(yca_id);
3925 if (commit)
3926 got_object_commit_close(commit);
3927 if (branch)
3928 got_ref_close(branch);
3929 if (new_base_branch)
3930 got_ref_close(new_base_branch);
3931 if (tmp_branch)
3932 got_ref_close(tmp_branch);
3933 if (worktree)
3934 got_worktree_close(worktree);
3935 if (repo)
3936 got_repo_close(repo);
3937 return error;