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 <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <time.h>
35 #include <paths.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
69 struct got_cmd {
70 const char *cmd_name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_alias;
74 };
76 __dead static void usage(int);
77 __dead static void usage_init(void);
78 __dead static void usage_checkout(void);
79 __dead static void usage_update(void);
80 __dead static void usage_log(void);
81 __dead static void usage_diff(void);
82 __dead static void usage_blame(void);
83 __dead static void usage_tree(void);
84 __dead static void usage_status(void);
85 __dead static void usage_ref(void);
86 __dead static void usage_branch(void);
87 __dead static void usage_add(void);
88 __dead static void usage_rm(void);
89 __dead static void usage_revert(void);
90 __dead static void usage_commit(void);
91 __dead static void usage_cherrypick(void);
92 __dead static void usage_backout(void);
94 static const struct got_error* cmd_init(int, char *[]);
95 static const struct got_error* cmd_checkout(int, char *[]);
96 static const struct got_error* cmd_update(int, char *[]);
97 static const struct got_error* cmd_log(int, char *[]);
98 static const struct got_error* cmd_diff(int, char *[]);
99 static const struct got_error* cmd_blame(int, char *[]);
100 static const struct got_error* cmd_tree(int, char *[]);
101 static const struct got_error* cmd_status(int, char *[]);
102 static const struct got_error* cmd_ref(int, char *[]);
103 static const struct got_error* cmd_branch(int, char *[]);
104 static const struct got_error* cmd_add(int, char *[]);
105 static const struct got_error* cmd_rm(int, char *[]);
106 static const struct got_error* cmd_revert(int, char *[]);
107 static const struct got_error* cmd_commit(int, char *[]);
108 static const struct got_error* cmd_cherrypick(int, char *[]);
109 static const struct got_error* cmd_backout(int, char *[]);
111 static struct got_cmd got_commands[] = {
112 { "init", cmd_init, usage_init, "" },
113 { "checkout", cmd_checkout, usage_checkout, "co" },
114 { "update", cmd_update, usage_update, "up" },
115 { "log", cmd_log, usage_log, "" },
116 { "diff", cmd_diff, usage_diff, "" },
117 { "blame", cmd_blame, usage_blame, "" },
118 { "tree", cmd_tree, usage_tree, "" },
119 { "status", cmd_status, usage_status, "st" },
120 { "ref", cmd_ref, usage_ref, "" },
121 { "branch", cmd_branch, usage_branch, "br" },
122 { "add", cmd_add, usage_add, "" },
123 { "rm", cmd_rm, usage_rm, "" },
124 { "revert", cmd_revert, usage_revert, "rv" },
125 { "commit", cmd_commit, usage_commit, "ci" },
126 { "cherrypick", cmd_cherrypick, usage_cherrypick, "ch" },
127 { "backout", cmd_backout, usage_backout, "bo" },
128 };
130 static void
131 list_commands(void)
133 int i;
135 fprintf(stderr, "commands:");
136 for (i = 0; i < nitems(got_commands); i++) {
137 struct got_cmd *cmd = &got_commands[i];
138 fprintf(stderr, " %s", cmd->cmd_name);
140 fputc('\n', stderr);
143 int
144 main(int argc, char *argv[])
146 struct got_cmd *cmd;
147 unsigned int i;
148 int ch;
149 int hflag = 0;
151 setlocale(LC_CTYPE, "");
153 while ((ch = getopt(argc, argv, "h")) != -1) {
154 switch (ch) {
155 case 'h':
156 hflag = 1;
157 break;
158 default:
159 usage(hflag);
160 /* NOTREACHED */
164 argc -= optind;
165 argv += optind;
166 optind = 0;
168 if (argc <= 0)
169 usage(hflag);
171 signal(SIGINT, catch_sigint);
172 signal(SIGPIPE, catch_sigpipe);
174 for (i = 0; i < nitems(got_commands); i++) {
175 const struct got_error *error;
177 cmd = &got_commands[i];
179 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
180 strcmp(cmd->cmd_alias, argv[0]) != 0)
181 continue;
183 if (hflag)
184 got_commands[i].cmd_usage();
186 error = got_commands[i].cmd_main(argc, argv);
187 if (error && !(sigint_received || sigpipe_received)) {
188 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
189 return 1;
192 return 0;
195 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
196 list_commands();
197 return 1;
200 __dead static void
201 usage(int hflag)
203 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
204 if (hflag)
205 list_commands();
206 exit(1);
209 static const struct got_error *
210 get_editor(char **abspath)
212 const struct got_error *err = NULL;
213 const char *editor;
215 editor = getenv("VISUAL");
216 if (editor == NULL)
217 editor = getenv("EDITOR");
219 if (editor) {
220 err = got_path_find_prog(abspath, editor);
221 if (err)
222 return err;
225 if (*abspath == NULL) {
226 *abspath = strdup("/bin/ed");
227 if (*abspath == NULL)
228 return got_error_from_errno("strdup");
231 return NULL;
234 static const struct got_error *
235 apply_unveil(const char *repo_path, int repo_read_only,
236 const char *worktree_path, int create_worktree)
238 const struct got_error *err;
240 if (create_worktree) {
241 /* Pre-create work tree path to avoid unveiling its parents. */
242 err = got_path_mkdir(worktree_path);
244 if (errno == EEXIST) {
245 if (got_path_dir_is_empty(worktree_path)) {
246 errno = 0;
247 err = NULL;
248 } else {
249 err = got_error_path(worktree_path,
250 GOT_ERR_DIR_NOT_EMPTY);
254 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
255 return err;
258 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
259 return got_error_from_errno2("unveil", repo_path);
261 if (worktree_path && unveil(worktree_path, "rwc") != 0)
262 return got_error_from_errno2("unveil", worktree_path);
264 if (unveil("/tmp", "rwc") != 0)
265 return got_error_from_errno2("unveil", "/tmp");
267 err = got_privsep_unveil_exec_helpers();
268 if (err != NULL)
269 return err;
271 if (unveil(NULL, NULL) != 0)
272 return got_error_from_errno("unveil");
274 return NULL;
277 __dead static void
278 usage_init(void)
280 fprintf(stderr, "usage: %s init path\n", getprogname());
281 exit(1);
284 static const struct got_error *
285 cmd_init(int argc, char *argv[])
287 const struct got_error *error = NULL;
288 char *repo_path = NULL;
289 int ch;
291 while ((ch = getopt(argc, argv, "")) != -1) {
292 switch (ch) {
293 default:
294 usage_init();
295 /* NOTREACHED */
299 argc -= optind;
300 argv += optind;
302 #ifndef PROFILE
303 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
304 err(1, "pledge");
305 #endif
306 if (argc != 1)
307 usage_init();
309 repo_path = strdup(argv[0]);
310 if (repo_path == NULL)
311 return got_error_from_errno("strdup");
313 got_path_strip_trailing_slashes(repo_path);
315 error = got_path_mkdir(repo_path);
316 if (error &&
317 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
318 goto done;
320 error = apply_unveil(repo_path, 0, NULL, 0);
321 if (error)
322 goto done;
324 error = got_repo_init(repo_path);
325 if (error != NULL)
326 goto done;
328 done:
329 free(repo_path);
330 return error;
333 __dead static void
334 usage_checkout(void)
336 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
337 "[-p prefix] repository-path [worktree-path]\n", getprogname());
338 exit(1);
341 static void
342 checkout_progress(void *arg, unsigned char status, const char *path)
344 char *worktree_path = arg;
346 /* Base commit bump happens silently. */
347 if (status == GOT_STATUS_BUMP_BASE)
348 return;
350 while (path[0] == '/')
351 path++;
353 printf("%c %s/%s\n", status, worktree_path, path);
356 static const struct got_error *
357 check_cancelled(void *arg)
359 if (sigint_received || sigpipe_received)
360 return got_error(GOT_ERR_CANCELLED);
361 return NULL;
364 static const struct got_error *
365 check_linear_ancestry(struct got_object_id *commit_id,
366 struct got_object_id *base_commit_id, struct got_repository *repo)
368 const struct got_error *err = NULL;
369 struct got_object_id *yca_id;
371 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
372 commit_id, base_commit_id, repo);
373 if (err)
374 return err;
376 if (yca_id == NULL)
377 return got_error(GOT_ERR_ANCESTRY);
379 /*
380 * Require a straight line of history between the target commit
381 * and the work tree's base commit.
383 * Non-linear situations such as this require a rebase:
385 * (commit) D F (base_commit)
386 * \ /
387 * C E
388 * \ /
389 * B (yca)
390 * |
391 * A
393 * 'got update' only handles linear cases:
394 * Update forwards in time: A (base/yca) - B - C - D (commit)
395 * Update backwards in time: D (base) - C - B - A (commit/yca)
396 */
397 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
398 got_object_id_cmp(base_commit_id, yca_id) != 0)
399 return got_error(GOT_ERR_ANCESTRY);
401 free(yca_id);
402 return NULL;
405 static const struct got_error *
406 check_same_branch(struct got_object_id *commit_id,
407 struct got_reference *head_ref, struct got_repository *repo)
409 const struct got_error *err = NULL;
410 struct got_commit_graph *graph = NULL;
411 struct got_object_id *head_commit_id = NULL;
412 int is_same_branch = 0;
414 err = got_ref_resolve(&head_commit_id, repo, head_ref);
415 if (err)
416 goto done;
418 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
419 if (err)
420 goto done;
422 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
423 if (err)
424 goto done;
426 for (;;) {
427 struct got_object_id *id;
428 err = got_commit_graph_iter_next(&id, graph);
429 if (err) {
430 if (err->code == GOT_ERR_ITER_COMPLETED) {
431 err = NULL;
432 break;
434 else if (err->code != GOT_ERR_ITER_NEED_MORE)
435 break;
436 err = got_commit_graph_fetch_commits(graph, 1,
437 repo);
438 if (err)
439 break;
442 if (id) {
443 if (got_object_id_cmp(id, commit_id) == 0) {
444 is_same_branch = 1;
445 break;
449 done:
450 if (graph)
451 got_commit_graph_close(graph);
452 free(head_commit_id);
453 if (!err && !is_same_branch)
454 err = got_error(GOT_ERR_ANCESTRY);
455 return err;
458 static const struct got_error *
459 cmd_checkout(int argc, char *argv[])
461 const struct got_error *error = NULL;
462 struct got_repository *repo = NULL;
463 struct got_reference *head_ref = NULL;
464 struct got_worktree *worktree = NULL;
465 char *repo_path = NULL;
466 char *worktree_path = NULL;
467 const char *path_prefix = "";
468 const char *branch_name = GOT_REF_HEAD;
469 char *commit_id_str = NULL;
470 int ch, same_path_prefix;
472 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
473 switch (ch) {
474 case 'b':
475 branch_name = optarg;
476 break;
477 case 'c':
478 commit_id_str = strdup(optarg);
479 if (commit_id_str == NULL)
480 return got_error_from_errno("strdup");
481 break;
482 case 'p':
483 path_prefix = optarg;
484 break;
485 default:
486 usage_checkout();
487 /* NOTREACHED */
491 argc -= optind;
492 argv += optind;
494 #ifndef PROFILE
495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
496 "unveil", NULL) == -1)
497 err(1, "pledge");
498 #endif
499 if (argc == 1) {
500 char *cwd, *base, *dotgit;
501 repo_path = realpath(argv[0], NULL);
502 if (repo_path == NULL)
503 return got_error_from_errno2("realpath", argv[0]);
504 cwd = getcwd(NULL, 0);
505 if (cwd == NULL) {
506 error = got_error_from_errno("getcwd");
507 goto done;
509 if (path_prefix[0]) {
510 base = basename(path_prefix);
511 if (base == NULL) {
512 error = got_error_from_errno2("basename",
513 path_prefix);
514 goto done;
516 } else {
517 base = basename(repo_path);
518 if (base == NULL) {
519 error = got_error_from_errno2("basename",
520 repo_path);
521 goto done;
524 dotgit = strstr(base, ".git");
525 if (dotgit)
526 *dotgit = '\0';
527 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
528 error = got_error_from_errno("asprintf");
529 free(cwd);
530 goto done;
532 free(cwd);
533 } else if (argc == 2) {
534 repo_path = realpath(argv[0], NULL);
535 if (repo_path == NULL) {
536 error = got_error_from_errno2("realpath", argv[0]);
537 goto done;
539 worktree_path = realpath(argv[1], NULL);
540 if (worktree_path == NULL) {
541 error = got_error_from_errno2("realpath", argv[1]);
542 goto done;
544 } else
545 usage_checkout();
547 got_path_strip_trailing_slashes(repo_path);
548 got_path_strip_trailing_slashes(worktree_path);
550 error = got_repo_open(&repo, repo_path);
551 if (error != NULL)
552 goto done;
554 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
555 if (error)
556 goto done;
558 error = got_ref_open(&head_ref, repo, branch_name, 0);
559 if (error != NULL)
560 goto done;
562 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
563 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
564 goto done;
566 error = got_worktree_open(&worktree, worktree_path);
567 if (error != NULL)
568 goto done;
570 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
571 path_prefix);
572 if (error != NULL)
573 goto done;
574 if (!same_path_prefix) {
575 error = got_error(GOT_ERR_PATH_PREFIX);
576 goto done;
579 if (commit_id_str) {
580 struct got_object_id *commit_id;
581 error = got_repo_match_object_id_prefix(&commit_id,
582 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
583 if (error != NULL)
584 goto done;
585 error = check_linear_ancestry(commit_id,
586 got_worktree_get_base_commit_id(worktree), repo);
587 if (error != NULL) {
588 free(commit_id);
589 goto done;
591 error = check_same_branch(commit_id, head_ref, repo);
592 if (error)
593 goto done;
594 error = got_worktree_set_base_commit_id(worktree, repo,
595 commit_id);
596 free(commit_id);
597 if (error)
598 goto done;
601 error = got_worktree_checkout_files(worktree, "", repo,
602 checkout_progress, worktree_path, check_cancelled, NULL);
603 if (error != NULL)
604 goto done;
606 printf("Now shut up and hack\n");
608 done:
609 free(commit_id_str);
610 free(repo_path);
611 free(worktree_path);
612 return error;
615 __dead static void
616 usage_update(void)
618 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
619 getprogname());
620 exit(1);
623 static void
624 update_progress(void *arg, unsigned char status, const char *path)
626 int *did_something = arg;
628 if (status == GOT_STATUS_EXISTS)
629 return;
631 *did_something = 1;
633 /* Base commit bump happens silently. */
634 if (status == GOT_STATUS_BUMP_BASE)
635 return;
637 while (path[0] == '/')
638 path++;
639 printf("%c %s\n", status, path);
642 static const struct got_error *
643 switch_head_ref(struct got_reference *head_ref,
644 struct got_object_id *commit_id, struct got_worktree *worktree,
645 struct got_repository *repo)
647 const struct got_error *err = NULL;
648 char *base_id_str;
649 int ref_has_moved = 0;
651 /* Trivial case: switching between two different references. */
652 if (strcmp(got_ref_get_name(head_ref),
653 got_worktree_get_head_ref_name(worktree)) != 0) {
654 printf("Switching work tree from %s to %s\n",
655 got_worktree_get_head_ref_name(worktree),
656 got_ref_get_name(head_ref));
657 return got_worktree_set_head_ref(worktree, head_ref);
660 err = check_linear_ancestry(commit_id,
661 got_worktree_get_base_commit_id(worktree), repo);
662 if (err) {
663 if (err->code != GOT_ERR_ANCESTRY)
664 return err;
665 ref_has_moved = 1;
667 if (!ref_has_moved)
668 return NULL;
670 /* Switching to a rebased branch with the same reference name. */
671 err = got_object_id_str(&base_id_str,
672 got_worktree_get_base_commit_id(worktree));
673 if (err)
674 return err;
675 printf("Reference %s now points at a different branch\n",
676 got_worktree_get_head_ref_name(worktree));
677 printf("Switching work tree from %s to %s\n", base_id_str,
678 got_worktree_get_head_ref_name(worktree));
679 return NULL;
682 static const struct got_error *
683 cmd_update(int argc, char *argv[])
685 const struct got_error *error = NULL;
686 struct got_repository *repo = NULL;
687 struct got_worktree *worktree = NULL;
688 char *worktree_path = NULL, *path = NULL;
689 struct got_object_id *commit_id = NULL;
690 char *commit_id_str = NULL;
691 const char *branch_name = NULL;
692 struct got_reference *head_ref = NULL;
693 int ch, did_something = 0;
695 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
696 switch (ch) {
697 case 'b':
698 branch_name = optarg;
699 break;
700 case 'c':
701 commit_id_str = strdup(optarg);
702 if (commit_id_str == NULL)
703 return got_error_from_errno("strdup");
704 break;
705 default:
706 usage_update();
707 /* NOTREACHED */
711 argc -= optind;
712 argv += optind;
714 #ifndef PROFILE
715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
716 "unveil", NULL) == -1)
717 err(1, "pledge");
718 #endif
719 worktree_path = getcwd(NULL, 0);
720 if (worktree_path == NULL) {
721 error = got_error_from_errno("getcwd");
722 goto done;
724 error = got_worktree_open(&worktree, worktree_path);
725 if (error)
726 goto done;
728 if (argc == 0) {
729 path = strdup("");
730 if (path == NULL) {
731 error = got_error_from_errno("strdup");
732 goto done;
734 } else if (argc == 1) {
735 error = got_worktree_resolve_path(&path, worktree, argv[0]);
736 if (error)
737 goto done;
738 } else
739 usage_update();
741 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
742 if (error != NULL)
743 goto done;
745 error = apply_unveil(got_repo_get_path(repo), 0,
746 got_worktree_get_root_path(worktree), 0);
747 if (error)
748 goto done;
750 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
751 got_worktree_get_head_ref_name(worktree), 0);
752 if (error != NULL)
753 goto done;
754 if (commit_id_str == NULL) {
755 error = got_ref_resolve(&commit_id, repo, head_ref);
756 if (error != NULL)
757 goto done;
758 error = got_object_id_str(&commit_id_str, commit_id);
759 if (error != NULL)
760 goto done;
761 } else {
762 error = got_repo_match_object_id_prefix(&commit_id,
763 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
764 if (error != NULL)
765 goto done;
768 if (branch_name) {
769 struct got_object_id *head_commit_id;
770 if (strlen(path) != 0) {
771 fprintf(stderr, "%s: switching between branches "
772 "requires that the entire work tree "
773 "gets updated, not just '%s'\n",
774 getprogname(), path);
775 error = got_error(GOT_ERR_BAD_PATH);
776 goto done;
778 error = got_ref_resolve(&head_commit_id, repo, head_ref);
779 if (error)
780 goto done;
781 error = check_linear_ancestry(commit_id, head_commit_id, repo);
782 free(head_commit_id);
783 if (error != NULL)
784 goto done;
785 error = check_same_branch(commit_id, head_ref, repo);
786 if (error)
787 goto done;
788 error = switch_head_ref(head_ref, commit_id, worktree, repo);
789 if (error)
790 goto done;
791 } else {
792 error = check_linear_ancestry(commit_id,
793 got_worktree_get_base_commit_id(worktree), repo);
794 if (error != NULL) {
795 if (error->code == GOT_ERR_ANCESTRY)
796 error = got_error(GOT_ERR_BRANCH_MOVED);
797 goto done;
799 error = check_same_branch(commit_id, head_ref, repo);
800 if (error)
801 goto done;
804 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
805 commit_id) != 0) {
806 error = got_worktree_set_base_commit_id(worktree, repo,
807 commit_id);
808 if (error)
809 goto done;
812 error = got_worktree_checkout_files(worktree, path, repo,
813 update_progress, &did_something, check_cancelled, NULL);
814 if (error != NULL)
815 goto done;
817 if (did_something)
818 printf("Updated to commit %s\n", commit_id_str);
819 else
820 printf("Already up-to-date\n");
821 done:
822 free(worktree_path);
823 free(path);
824 free(commit_id);
825 free(commit_id_str);
826 return error;
829 static const struct got_error *
830 print_patch(struct got_commit_object *commit, struct got_object_id *id,
831 int diff_context, struct got_repository *repo)
833 const struct got_error *err = NULL;
834 struct got_tree_object *tree1 = NULL, *tree2;
835 struct got_object_qid *qid;
836 char *id_str1 = NULL, *id_str2;
837 struct got_diff_blob_output_unidiff_arg arg;
839 err = got_object_open_as_tree(&tree2, repo,
840 got_object_commit_get_tree_id(commit));
841 if (err)
842 return err;
844 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
845 if (qid != NULL) {
846 struct got_commit_object *pcommit;
848 err = got_object_open_as_commit(&pcommit, repo, qid->id);
849 if (err)
850 return err;
852 err = got_object_open_as_tree(&tree1, repo,
853 got_object_commit_get_tree_id(pcommit));
854 got_object_commit_close(pcommit);
855 if (err)
856 return err;
858 err = got_object_id_str(&id_str1, qid->id);
859 if (err)
860 return err;
863 err = got_object_id_str(&id_str2, id);
864 if (err)
865 goto done;
867 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
868 arg.diff_context = diff_context;
869 arg.outfile = stdout;
870 err = got_diff_tree(tree1, tree2, "", "", repo,
871 got_diff_blob_output_unidiff, &arg);
872 done:
873 if (tree1)
874 got_object_tree_close(tree1);
875 got_object_tree_close(tree2);
876 free(id_str1);
877 free(id_str2);
878 return err;
881 static char *
882 get_datestr(time_t *time, char *datebuf)
884 char *p, *s = ctime_r(time, datebuf);
885 p = strchr(s, '\n');
886 if (p)
887 *p = '\0';
888 return s;
891 static const struct got_error *
892 print_commit(struct got_commit_object *commit, struct got_object_id *id,
893 struct got_repository *repo, int show_patch, int diff_context,
894 struct got_reflist_head *refs)
896 const struct got_error *err = NULL;
897 char *id_str, *datestr, *logmsg0, *logmsg, *line;
898 char datebuf[26];
899 time_t committer_time;
900 const char *author, *committer;
901 char *refs_str = NULL;
902 struct got_reflist_entry *re;
904 SIMPLEQ_FOREACH(re, refs, entry) {
905 char *s;
906 const char *name;
907 if (got_object_id_cmp(re->id, id) != 0)
908 continue;
909 name = got_ref_get_name(re->ref);
910 if (strcmp(name, GOT_REF_HEAD) == 0)
911 continue;
912 if (strncmp(name, "refs/", 5) == 0)
913 name += 5;
914 if (strncmp(name, "got/", 4) == 0)
915 continue;
916 if (strncmp(name, "heads/", 6) == 0)
917 name += 6;
918 if (strncmp(name, "remotes/", 8) == 0)
919 name += 8;
920 s = refs_str;
921 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
922 name) == -1) {
923 err = got_error_from_errno("asprintf");
924 free(s);
925 break;
927 free(s);
929 err = got_object_id_str(&id_str, id);
930 if (err)
931 return err;
933 printf("-----------------------------------------------\n");
934 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
935 refs_str ? refs_str : "", refs_str ? ")" : "");
936 free(id_str);
937 id_str = NULL;
938 free(refs_str);
939 refs_str = NULL;
940 printf("from: %s\n", got_object_commit_get_author(commit));
941 committer_time = got_object_commit_get_committer_time(commit);
942 datestr = get_datestr(&committer_time, datebuf);
943 printf("date: %s UTC\n", datestr);
944 author = got_object_commit_get_author(commit);
945 committer = got_object_commit_get_committer(commit);
946 if (strcmp(author, committer) != 0)
947 printf("via: %s\n", committer);
948 if (got_object_commit_get_nparents(commit) > 1) {
949 const struct got_object_id_queue *parent_ids;
950 struct got_object_qid *qid;
951 int n = 1;
952 parent_ids = got_object_commit_get_parent_ids(commit);
953 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
954 err = got_object_id_str(&id_str, qid->id);
955 if (err)
956 return err;
957 printf("parent %d: %s\n", n++, id_str);
958 free(id_str);
962 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
963 if (logmsg0 == NULL)
964 return got_error_from_errno("strdup");
966 logmsg = logmsg0;
967 do {
968 line = strsep(&logmsg, "\n");
969 if (line)
970 printf(" %s\n", line);
971 } while (line);
972 free(logmsg0);
974 if (show_patch) {
975 err = print_patch(commit, id, diff_context, repo);
976 if (err == 0)
977 printf("\n");
980 if (fflush(stdout) != 0 && err == NULL)
981 err = got_error_from_errno("fflush");
982 return err;
985 static const struct got_error *
986 print_commits(struct got_object_id *root_id, struct got_repository *repo,
987 char *path, int show_patch, int diff_context, int limit,
988 int first_parent_traversal, struct got_reflist_head *refs)
990 const struct got_error *err;
991 struct got_commit_graph *graph;
993 err = got_commit_graph_open(&graph, root_id, path,
994 first_parent_traversal, repo);
995 if (err)
996 return err;
997 err = got_commit_graph_iter_start(graph, root_id, repo);
998 if (err)
999 goto done;
1000 for (;;) {
1001 struct got_commit_object *commit;
1002 struct got_object_id *id;
1004 if (sigint_received || sigpipe_received)
1005 break;
1007 err = got_commit_graph_iter_next(&id, graph);
1008 if (err) {
1009 if (err->code == GOT_ERR_ITER_COMPLETED) {
1010 err = NULL;
1011 break;
1013 if (err->code != GOT_ERR_ITER_NEED_MORE)
1014 break;
1015 err = got_commit_graph_fetch_commits(graph, 1, repo);
1016 if (err)
1017 break;
1018 else
1019 continue;
1021 if (id == NULL)
1022 break;
1024 err = got_object_open_as_commit(&commit, repo, id);
1025 if (err)
1026 break;
1027 err = print_commit(commit, id, repo, show_patch, diff_context,
1028 refs);
1029 got_object_commit_close(commit);
1030 if (err || (limit && --limit == 0))
1031 break;
1033 done:
1034 got_commit_graph_close(graph);
1035 return err;
1038 __dead static void
1039 usage_log(void)
1041 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1042 "[-r repository-path] [path]\n", getprogname());
1043 exit(1);
1046 static const struct got_error *
1047 cmd_log(int argc, char *argv[])
1049 const struct got_error *error;
1050 struct got_repository *repo = NULL;
1051 struct got_worktree *worktree = NULL;
1052 struct got_commit_object *commit = NULL;
1053 struct got_object_id *id = NULL;
1054 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1055 char *start_commit = NULL;
1056 int diff_context = 3, ch;
1057 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1058 const char *errstr;
1059 struct got_reflist_head refs;
1061 SIMPLEQ_INIT(&refs);
1063 #ifndef PROFILE
1064 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1065 NULL)
1066 == -1)
1067 err(1, "pledge");
1068 #endif
1070 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1071 switch (ch) {
1072 case 'b':
1073 first_parent_traversal = 1;
1074 break;
1075 case 'p':
1076 show_patch = 1;
1077 break;
1078 case 'c':
1079 start_commit = optarg;
1080 break;
1081 case 'C':
1082 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1083 &errstr);
1084 if (errstr != NULL)
1085 err(1, "-C option %s", errstr);
1086 break;
1087 case 'l':
1088 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1089 if (errstr != NULL)
1090 err(1, "-l option %s", errstr);
1091 break;
1092 case 'r':
1093 repo_path = realpath(optarg, NULL);
1094 if (repo_path == NULL)
1095 err(1, "-r option");
1096 got_path_strip_trailing_slashes(repo_path);
1097 break;
1098 default:
1099 usage_log();
1100 /* NOTREACHED */
1104 argc -= optind;
1105 argv += optind;
1107 cwd = getcwd(NULL, 0);
1108 if (cwd == NULL) {
1109 error = got_error_from_errno("getcwd");
1110 goto done;
1113 error = got_worktree_open(&worktree, cwd);
1114 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1115 goto done;
1116 error = NULL;
1118 if (argc == 0) {
1119 path = strdup("");
1120 if (path == NULL) {
1121 error = got_error_from_errno("strdup");
1122 goto done;
1124 } else if (argc == 1) {
1125 if (worktree) {
1126 error = got_worktree_resolve_path(&path, worktree,
1127 argv[0]);
1128 if (error)
1129 goto done;
1130 } else {
1131 path = strdup(argv[0]);
1132 if (path == NULL) {
1133 error = got_error_from_errno("strdup");
1134 goto done;
1137 } else
1138 usage_log();
1140 if (repo_path == NULL) {
1141 repo_path = worktree ?
1142 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1144 if (repo_path == NULL) {
1145 error = got_error_from_errno("strdup");
1146 goto done;
1149 error = got_repo_open(&repo, repo_path);
1150 if (error != NULL)
1151 goto done;
1153 error = apply_unveil(got_repo_get_path(repo), 1,
1154 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1155 if (error)
1156 goto done;
1158 if (start_commit == NULL) {
1159 struct got_reference *head_ref;
1160 error = got_ref_open(&head_ref, repo,
1161 worktree ? got_worktree_get_head_ref_name(worktree)
1162 : GOT_REF_HEAD, 0);
1163 if (error != NULL)
1164 return error;
1165 error = got_ref_resolve(&id, repo, head_ref);
1166 got_ref_close(head_ref);
1167 if (error != NULL)
1168 return error;
1169 error = got_object_open_as_commit(&commit, repo, id);
1170 } else {
1171 struct got_reference *ref;
1172 error = got_ref_open(&ref, repo, start_commit, 0);
1173 if (error == NULL) {
1174 int obj_type;
1175 error = got_ref_resolve(&id, repo, ref);
1176 got_ref_close(ref);
1177 if (error != NULL)
1178 goto done;
1179 error = got_object_get_type(&obj_type, repo, id);
1180 if (error != NULL)
1181 goto done;
1182 if (obj_type == GOT_OBJ_TYPE_TAG) {
1183 struct got_tag_object *tag;
1184 error = got_object_open_as_tag(&tag, repo, id);
1185 if (error != NULL)
1186 goto done;
1187 if (got_object_tag_get_object_type(tag) !=
1188 GOT_OBJ_TYPE_COMMIT) {
1189 got_object_tag_close(tag);
1190 error = got_error(GOT_ERR_OBJ_TYPE);
1191 goto done;
1193 free(id);
1194 id = got_object_id_dup(
1195 got_object_tag_get_object_id(tag));
1196 if (id == NULL)
1197 error = got_error_from_errno(
1198 "got_object_id_dup");
1199 got_object_tag_close(tag);
1200 if (error)
1201 goto done;
1202 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1203 error = got_error(GOT_ERR_OBJ_TYPE);
1204 goto done;
1206 error = got_object_open_as_commit(&commit, repo, id);
1207 if (error != NULL)
1208 goto done;
1210 if (commit == NULL) {
1211 error = got_repo_match_object_id_prefix(&id,
1212 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1213 if (error != NULL)
1214 return error;
1217 if (error != NULL)
1218 goto done;
1220 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1221 if (error != NULL)
1222 goto done;
1223 if (in_repo_path) {
1224 free(path);
1225 path = in_repo_path;
1228 error = got_ref_list(&refs, repo);
1229 if (error)
1230 goto done;
1232 error = print_commits(id, repo, path, show_patch,
1233 diff_context, limit, first_parent_traversal, &refs);
1234 done:
1235 free(path);
1236 free(repo_path);
1237 free(cwd);
1238 free(id);
1239 if (worktree)
1240 got_worktree_close(worktree);
1241 if (repo) {
1242 const struct got_error *repo_error;
1243 repo_error = got_repo_close(repo);
1244 if (error == NULL)
1245 error = repo_error;
1247 got_ref_list_free(&refs);
1248 return error;
1251 __dead static void
1252 usage_diff(void)
1254 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1255 "[object1 object2 | path]\n", getprogname());
1256 exit(1);
1259 struct print_diff_arg {
1260 struct got_repository *repo;
1261 struct got_worktree *worktree;
1262 int diff_context;
1263 const char *id_str;
1264 int header_shown;
1267 static const struct got_error *
1268 print_diff(void *arg, unsigned char status, const char *path,
1269 struct got_object_id *blob_id, struct got_object_id *commit_id)
1271 struct print_diff_arg *a = arg;
1272 const struct got_error *err = NULL;
1273 struct got_blob_object *blob1 = NULL;
1274 FILE *f2 = NULL;
1275 char *abspath = NULL;
1276 struct stat sb;
1278 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1279 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1280 return NULL;
1282 if (!a->header_shown) {
1283 printf("diff %s %s\n", a->id_str,
1284 got_worktree_get_root_path(a->worktree));
1285 a->header_shown = 1;
1288 if (status != GOT_STATUS_ADD) {
1289 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1290 if (err)
1291 goto done;
1295 if (status != GOT_STATUS_DELETE) {
1296 if (asprintf(&abspath, "%s/%s",
1297 got_worktree_get_root_path(a->worktree), path) == -1) {
1298 err = got_error_from_errno("asprintf");
1299 goto done;
1302 f2 = fopen(abspath, "r");
1303 if (f2 == NULL) {
1304 err = got_error_from_errno2("fopen", abspath);
1305 goto done;
1307 if (lstat(abspath, &sb) == -1) {
1308 err = got_error_from_errno2("lstat", abspath);
1309 goto done;
1311 } else
1312 sb.st_size = 0;
1314 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1315 stdout);
1316 done:
1317 if (blob1)
1318 got_object_blob_close(blob1);
1319 if (f2 && fclose(f2) != 0 && err == NULL)
1320 err = got_error_from_errno("fclose");
1321 free(abspath);
1322 return err;
1325 static const struct got_error *
1326 cmd_diff(int argc, char *argv[])
1328 const struct got_error *error;
1329 struct got_repository *repo = NULL;
1330 struct got_worktree *worktree = NULL;
1331 char *cwd = NULL, *repo_path = NULL;
1332 struct got_object_id *id1 = NULL, *id2 = NULL;
1333 const char *id_str1 = NULL, *id_str2 = NULL;
1334 char *label1 = NULL, *label2 = NULL;
1335 int type1, type2;
1336 int diff_context = 3, ch;
1337 const char *errstr;
1338 char *path = NULL;
1340 #ifndef PROFILE
1341 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1342 NULL) == -1)
1343 err(1, "pledge");
1344 #endif
1346 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1347 switch (ch) {
1348 case 'C':
1349 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1350 if (errstr != NULL)
1351 err(1, "-C option %s", errstr);
1352 break;
1353 case 'r':
1354 repo_path = realpath(optarg, NULL);
1355 if (repo_path == NULL)
1356 err(1, "-r option");
1357 got_path_strip_trailing_slashes(repo_path);
1358 break;
1359 default:
1360 usage_diff();
1361 /* NOTREACHED */
1365 argc -= optind;
1366 argv += optind;
1368 cwd = getcwd(NULL, 0);
1369 if (cwd == NULL) {
1370 error = got_error_from_errno("getcwd");
1371 goto done;
1373 error = got_worktree_open(&worktree, cwd);
1374 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1375 goto done;
1376 if (argc <= 1) {
1377 if (worktree == NULL) {
1378 error = got_error(GOT_ERR_NOT_WORKTREE);
1379 goto done;
1381 if (repo_path)
1382 errx(1,
1383 "-r option can't be used when diffing a work tree");
1384 repo_path = strdup(got_worktree_get_repo_path(worktree));
1385 if (repo_path == NULL) {
1386 error = got_error_from_errno("strdup");
1387 goto done;
1389 if (argc == 1) {
1390 error = got_worktree_resolve_path(&path, worktree,
1391 argv[0]);
1392 if (error)
1393 goto done;
1394 } else {
1395 path = strdup("");
1396 if (path == NULL) {
1397 error = got_error_from_errno("strdup");
1398 goto done;
1401 } else if (argc == 2) {
1402 id_str1 = argv[0];
1403 id_str2 = argv[1];
1404 if (worktree && repo_path == NULL) {
1405 repo_path =
1406 strdup(got_worktree_get_repo_path(worktree));
1407 if (repo_path == NULL) {
1408 error = got_error_from_errno("strdup");
1409 goto done;
1412 } else
1413 usage_diff();
1415 if (repo_path == NULL) {
1416 repo_path = getcwd(NULL, 0);
1417 if (repo_path == NULL)
1418 return got_error_from_errno("getcwd");
1421 error = got_repo_open(&repo, repo_path);
1422 free(repo_path);
1423 if (error != NULL)
1424 goto done;
1426 error = apply_unveil(got_repo_get_path(repo), 1,
1427 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1428 if (error)
1429 goto done;
1431 if (argc <= 1) {
1432 struct print_diff_arg arg;
1433 char *id_str;
1434 error = got_object_id_str(&id_str,
1435 got_worktree_get_base_commit_id(worktree));
1436 if (error)
1437 goto done;
1438 arg.repo = repo;
1439 arg.worktree = worktree;
1440 arg.diff_context = diff_context;
1441 arg.id_str = id_str;
1442 arg.header_shown = 0;
1444 error = got_worktree_status(worktree, path, repo, print_diff,
1445 &arg, check_cancelled, NULL);
1446 free(id_str);
1447 goto done;
1450 error = got_repo_match_object_id_prefix(&id1, id_str1,
1451 GOT_OBJ_TYPE_ANY, repo);
1452 if (error) {
1453 struct got_reference *ref;
1454 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1455 goto done;
1456 error = got_ref_open(&ref, repo, id_str1, 0);
1457 if (error != NULL)
1458 goto done;
1459 label1 = strdup(got_ref_get_name(ref));
1460 if (label1 == NULL) {
1461 error = got_error_from_errno("strdup");
1462 goto done;
1464 error = got_ref_resolve(&id1, repo, ref);
1465 got_ref_close(ref);
1466 if (error != NULL)
1467 goto done;
1468 } else {
1469 label1 = strdup(id_str1);
1470 if (label1 == NULL) {
1471 error = got_error_from_errno("strdup");
1472 goto done;
1476 error = got_repo_match_object_id_prefix(&id2, id_str2,
1477 GOT_OBJ_TYPE_ANY, repo);
1478 if (error) {
1479 struct got_reference *ref;
1480 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1481 goto done;
1482 error = got_ref_open(&ref, repo, id_str2, 0);
1483 if (error != NULL)
1484 goto done;
1485 label2 = strdup(got_ref_get_name(ref));
1486 if (label2 == NULL) {
1487 error = got_error_from_errno("strdup");
1488 goto done;
1490 error = got_ref_resolve(&id2, repo, ref);
1491 got_ref_close(ref);
1492 if (error != NULL)
1493 goto done;
1494 } else {
1495 label2 = strdup(id_str2);
1496 if (label2 == NULL) {
1497 error = got_error_from_errno("strdup");
1498 goto done;
1502 error = got_object_get_type(&type1, repo, id1);
1503 if (error)
1504 goto done;
1506 error = got_object_get_type(&type2, repo, id2);
1507 if (error)
1508 goto done;
1510 if (type1 != type2) {
1511 error = got_error(GOT_ERR_OBJ_TYPE);
1512 goto done;
1515 switch (type1) {
1516 case GOT_OBJ_TYPE_BLOB:
1517 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1518 diff_context, repo, stdout);
1519 break;
1520 case GOT_OBJ_TYPE_TREE:
1521 error = got_diff_objects_as_trees(id1, id2, "", "",
1522 diff_context, repo, stdout);
1523 break;
1524 case GOT_OBJ_TYPE_COMMIT:
1525 printf("diff %s %s\n", label1, label2);
1526 error = got_diff_objects_as_commits(id1, id2, diff_context,
1527 repo, stdout);
1528 break;
1529 default:
1530 error = got_error(GOT_ERR_OBJ_TYPE);
1533 done:
1534 free(label1);
1535 free(label2);
1536 free(id1);
1537 free(id2);
1538 free(path);
1539 if (worktree)
1540 got_worktree_close(worktree);
1541 if (repo) {
1542 const struct got_error *repo_error;
1543 repo_error = got_repo_close(repo);
1544 if (error == NULL)
1545 error = repo_error;
1547 return error;
1550 __dead static void
1551 usage_blame(void)
1553 fprintf(stderr,
1554 "usage: %s blame [-c commit] [-r repository-path] path\n",
1555 getprogname());
1556 exit(1);
1559 static const struct got_error *
1560 cmd_blame(int argc, char *argv[])
1562 const struct got_error *error;
1563 struct got_repository *repo = NULL;
1564 struct got_worktree *worktree = NULL;
1565 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1566 struct got_object_id *commit_id = NULL;
1567 char *commit_id_str = NULL;
1568 int ch;
1570 #ifndef PROFILE
1571 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1572 NULL) == -1)
1573 err(1, "pledge");
1574 #endif
1576 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1577 switch (ch) {
1578 case 'c':
1579 commit_id_str = optarg;
1580 break;
1581 case 'r':
1582 repo_path = realpath(optarg, NULL);
1583 if (repo_path == NULL)
1584 err(1, "-r option");
1585 got_path_strip_trailing_slashes(repo_path);
1586 break;
1587 default:
1588 usage_blame();
1589 /* NOTREACHED */
1593 argc -= optind;
1594 argv += optind;
1596 if (argc == 1)
1597 path = argv[0];
1598 else
1599 usage_blame();
1601 cwd = getcwd(NULL, 0);
1602 if (cwd == NULL) {
1603 error = got_error_from_errno("getcwd");
1604 goto done;
1606 if (repo_path == NULL) {
1607 error = got_worktree_open(&worktree, cwd);
1608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1609 goto done;
1610 else
1611 error = NULL;
1612 if (worktree) {
1613 repo_path =
1614 strdup(got_worktree_get_repo_path(worktree));
1615 if (repo_path == NULL)
1616 error = got_error_from_errno("strdup");
1617 if (error)
1618 goto done;
1619 } else {
1620 repo_path = strdup(cwd);
1621 if (repo_path == NULL) {
1622 error = got_error_from_errno("strdup");
1623 goto done;
1628 error = got_repo_open(&repo, repo_path);
1629 if (error != NULL)
1630 goto done;
1632 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1633 if (error)
1634 goto done;
1636 if (worktree) {
1637 const char *prefix = got_worktree_get_path_prefix(worktree);
1638 char *p, *worktree_subdir = cwd +
1639 strlen(got_worktree_get_root_path(worktree));
1640 if (asprintf(&p, "%s%s%s%s%s",
1641 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1642 worktree_subdir, worktree_subdir[0] ? "/" : "",
1643 path) == -1) {
1644 error = got_error_from_errno("asprintf");
1645 goto done;
1647 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1648 free(p);
1649 } else {
1650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 if (error)
1653 goto done;
1655 if (commit_id_str == NULL) {
1656 struct got_reference *head_ref;
1657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1658 if (error != NULL)
1659 goto done;
1660 error = got_ref_resolve(&commit_id, repo, head_ref);
1661 got_ref_close(head_ref);
1662 if (error != NULL)
1663 goto done;
1664 } else {
1665 error = got_repo_match_object_id_prefix(&commit_id,
1666 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1667 if (error != NULL)
1668 goto done;
1671 error = got_blame(in_repo_path, commit_id, repo, stdout);
1672 done:
1673 free(in_repo_path);
1674 free(repo_path);
1675 free(cwd);
1676 free(commit_id);
1677 if (worktree)
1678 got_worktree_close(worktree);
1679 if (repo) {
1680 const struct got_error *repo_error;
1681 repo_error = got_repo_close(repo);
1682 if (error == NULL)
1683 error = repo_error;
1685 return error;
1688 __dead static void
1689 usage_tree(void)
1691 fprintf(stderr,
1692 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1693 getprogname());
1694 exit(1);
1697 static void
1698 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1699 const char *root_path)
1701 int is_root_path = (strcmp(path, root_path) == 0);
1703 path += strlen(root_path);
1704 while (path[0] == '/')
1705 path++;
1707 printf("%s%s%s%s%s\n", id ? id : "", path,
1708 is_root_path ? "" : "/", te->name,
1709 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1712 static const struct got_error *
1713 print_tree(const char *path, struct got_object_id *commit_id,
1714 int show_ids, int recurse, const char *root_path,
1715 struct got_repository *repo)
1717 const struct got_error *err = NULL;
1718 struct got_object_id *tree_id = NULL;
1719 struct got_tree_object *tree = NULL;
1720 const struct got_tree_entries *entries;
1721 struct got_tree_entry *te;
1723 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1724 if (err)
1725 goto done;
1727 err = got_object_open_as_tree(&tree, repo, tree_id);
1728 if (err)
1729 goto done;
1730 entries = got_object_tree_get_entries(tree);
1731 te = SIMPLEQ_FIRST(&entries->head);
1732 while (te) {
1733 char *id = NULL;
1735 if (sigint_received || sigpipe_received)
1736 break;
1738 if (show_ids) {
1739 char *id_str;
1740 err = got_object_id_str(&id_str, te->id);
1741 if (err)
1742 goto done;
1743 if (asprintf(&id, "%s ", id_str) == -1) {
1744 err = got_error_from_errno("asprintf");
1745 free(id_str);
1746 goto done;
1748 free(id_str);
1750 print_entry(te, id, path, root_path);
1751 free(id);
1753 if (recurse && S_ISDIR(te->mode)) {
1754 char *child_path;
1755 if (asprintf(&child_path, "%s%s%s", path,
1756 path[0] == '/' && path[1] == '\0' ? "" : "/",
1757 te->name) == -1) {
1758 err = got_error_from_errno("asprintf");
1759 goto done;
1761 err = print_tree(child_path, commit_id, show_ids, 1,
1762 root_path, repo);
1763 free(child_path);
1764 if (err)
1765 goto done;
1768 te = SIMPLEQ_NEXT(te, entry);
1770 done:
1771 if (tree)
1772 got_object_tree_close(tree);
1773 free(tree_id);
1774 return err;
1777 static const struct got_error *
1778 cmd_tree(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1783 const char *path;
1784 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1785 struct got_object_id *commit_id = NULL;
1786 char *commit_id_str = NULL;
1787 int show_ids = 0, recurse = 0;
1788 int ch;
1790 #ifndef PROFILE
1791 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1792 NULL) == -1)
1793 err(1, "pledge");
1794 #endif
1796 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1797 switch (ch) {
1798 case 'c':
1799 commit_id_str = optarg;
1800 break;
1801 case 'r':
1802 repo_path = realpath(optarg, NULL);
1803 if (repo_path == NULL)
1804 err(1, "-r option");
1805 got_path_strip_trailing_slashes(repo_path);
1806 break;
1807 case 'i':
1808 show_ids = 1;
1809 break;
1810 case 'R':
1811 recurse = 1;
1812 break;
1813 default:
1814 usage_tree();
1815 /* NOTREACHED */
1819 argc -= optind;
1820 argv += optind;
1822 if (argc == 1)
1823 path = argv[0];
1824 else if (argc > 1)
1825 usage_tree();
1826 else
1827 path = NULL;
1829 cwd = getcwd(NULL, 0);
1830 if (cwd == NULL) {
1831 error = got_error_from_errno("getcwd");
1832 goto done;
1834 if (repo_path == NULL) {
1835 error = got_worktree_open(&worktree, cwd);
1836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1837 goto done;
1838 else
1839 error = NULL;
1840 if (worktree) {
1841 repo_path =
1842 strdup(got_worktree_get_repo_path(worktree));
1843 if (repo_path == NULL)
1844 error = got_error_from_errno("strdup");
1845 if (error)
1846 goto done;
1847 } else {
1848 repo_path = strdup(cwd);
1849 if (repo_path == NULL) {
1850 error = got_error_from_errno("strdup");
1851 goto done;
1856 error = got_repo_open(&repo, repo_path);
1857 if (error != NULL)
1858 goto done;
1860 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1861 if (error)
1862 goto done;
1864 if (path == NULL) {
1865 if (worktree) {
1866 char *p, *worktree_subdir = cwd +
1867 strlen(got_worktree_get_root_path(worktree));
1868 if (asprintf(&p, "%s/%s",
1869 got_worktree_get_path_prefix(worktree),
1870 worktree_subdir) == -1) {
1871 error = got_error_from_errno("asprintf");
1872 goto done;
1874 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1875 free(p);
1876 if (error)
1877 goto done;
1878 } else
1879 path = "/";
1881 if (in_repo_path == NULL) {
1882 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1883 if (error != NULL)
1884 goto done;
1887 if (commit_id_str == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1890 if (error != NULL)
1891 goto done;
1892 error = got_ref_resolve(&commit_id, repo, head_ref);
1893 got_ref_close(head_ref);
1894 if (error != NULL)
1895 goto done;
1896 } else {
1897 error = got_repo_match_object_id_prefix(&commit_id,
1898 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1899 if (error != NULL)
1900 goto done;
1903 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1904 in_repo_path, repo);
1905 done:
1906 free(in_repo_path);
1907 free(repo_path);
1908 free(cwd);
1909 free(commit_id);
1910 if (worktree)
1911 got_worktree_close(worktree);
1912 if (repo) {
1913 const struct got_error *repo_error;
1914 repo_error = got_repo_close(repo);
1915 if (error == NULL)
1916 error = repo_error;
1918 return error;
1921 __dead static void
1922 usage_status(void)
1924 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1925 exit(1);
1928 static const struct got_error *
1929 print_status(void *arg, unsigned char status, const char *path,
1930 struct got_object_id *blob_id, struct got_object_id *commit_id)
1932 printf("%c %s\n", status, path);
1933 return NULL;
1936 static const struct got_error *
1937 cmd_status(int argc, char *argv[])
1939 const struct got_error *error = NULL;
1940 struct got_repository *repo = NULL;
1941 struct got_worktree *worktree = NULL;
1942 char *cwd = NULL, *path = NULL;
1943 int ch;
1945 while ((ch = getopt(argc, argv, "")) != -1) {
1946 switch (ch) {
1947 default:
1948 usage_status();
1949 /* NOTREACHED */
1953 argc -= optind;
1954 argv += optind;
1956 #ifndef PROFILE
1957 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1958 NULL) == -1)
1959 err(1, "pledge");
1960 #endif
1961 cwd = getcwd(NULL, 0);
1962 if (cwd == NULL) {
1963 error = got_error_from_errno("getcwd");
1964 goto done;
1967 error = got_worktree_open(&worktree, cwd);
1968 if (error != NULL)
1969 goto done;
1971 if (argc == 0) {
1972 path = strdup("");
1973 if (path == NULL) {
1974 error = got_error_from_errno("strdup");
1975 goto done;
1977 } else if (argc == 1) {
1978 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1979 if (error)
1980 goto done;
1981 } else
1982 usage_status();
1984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1985 if (error != NULL)
1986 goto done;
1988 error = apply_unveil(got_repo_get_path(repo), 1,
1989 got_worktree_get_root_path(worktree), 0);
1990 if (error)
1991 goto done;
1993 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1994 check_cancelled, NULL);
1995 done:
1996 free(cwd);
1997 free(path);
1998 return error;
2001 __dead static void
2002 usage_ref(void)
2004 fprintf(stderr,
2005 "usage: %s ref [-r repository] -l | -d name | name target\n",
2006 getprogname());
2007 exit(1);
2010 static const struct got_error *
2011 list_refs(struct got_repository *repo)
2013 static const struct got_error *err = NULL;
2014 struct got_reflist_head refs;
2015 struct got_reflist_entry *re;
2017 SIMPLEQ_INIT(&refs);
2018 err = got_ref_list(&refs, repo);
2019 if (err)
2020 return err;
2022 SIMPLEQ_FOREACH(re, &refs, entry) {
2023 char *refstr;
2024 refstr = got_ref_to_str(re->ref);
2025 if (refstr == NULL)
2026 return got_error_from_errno("got_ref_to_str");
2027 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2028 free(refstr);
2031 got_ref_list_free(&refs);
2032 return NULL;
2035 static const struct got_error *
2036 delete_ref(struct got_repository *repo, const char *refname)
2038 const struct got_error *err = NULL;
2039 struct got_reference *ref;
2041 err = got_ref_open(&ref, repo, refname, 0);
2042 if (err)
2043 return err;
2045 err = got_ref_delete(ref, repo);
2046 got_ref_close(ref);
2047 return err;
2050 static const struct got_error *
2051 add_ref(struct got_repository *repo, const char *refname, const char *target)
2053 const struct got_error *err = NULL;
2054 struct got_object_id *id;
2055 struct got_reference *ref = NULL;
2057 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2058 repo);
2059 if (err) {
2060 struct got_reference *target_ref;
2062 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2063 return err;
2064 err = got_ref_open(&target_ref, repo, target, 0);
2065 if (err)
2066 return err;
2067 err = got_ref_resolve(&id, repo, target_ref);
2068 got_ref_close(target_ref);
2069 if (err)
2070 return err;
2073 err = got_ref_alloc(&ref, refname, id);
2074 if (err)
2075 goto done;
2077 err = got_ref_write(ref, repo);
2078 done:
2079 if (ref)
2080 got_ref_close(ref);
2081 free(id);
2082 return err;
2085 static const struct got_error *
2086 cmd_ref(int argc, char *argv[])
2088 const struct got_error *error = NULL;
2089 struct got_repository *repo = NULL;
2090 struct got_worktree *worktree = NULL;
2091 char *cwd = NULL, *repo_path = NULL;
2092 int ch, do_list = 0;
2093 const char *delref = NULL;
2095 /* TODO: Add -s option for adding symbolic references. */
2096 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2097 switch (ch) {
2098 case 'd':
2099 delref = optarg;
2100 break;
2101 case 'r':
2102 repo_path = realpath(optarg, NULL);
2103 if (repo_path == NULL)
2104 err(1, "-r option");
2105 got_path_strip_trailing_slashes(repo_path);
2106 break;
2107 case 'l':
2108 do_list = 1;
2109 break;
2110 default:
2111 usage_ref();
2112 /* NOTREACHED */
2116 if (do_list && delref)
2117 errx(1, "-l and -d options are mutually exclusive\n");
2119 argc -= optind;
2120 argv += optind;
2122 if (do_list || delref) {
2123 if (argc > 0)
2124 usage_ref();
2125 } else if (argc != 2)
2126 usage_ref();
2128 #ifndef PROFILE
2129 if (do_list) {
2130 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2131 NULL) == -1)
2132 err(1, "pledge");
2133 } else {
2134 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2135 "sendfd unveil", NULL) == -1)
2136 err(1, "pledge");
2138 #endif
2139 cwd = getcwd(NULL, 0);
2140 if (cwd == NULL) {
2141 error = got_error_from_errno("getcwd");
2142 goto done;
2145 if (repo_path == NULL) {
2146 error = got_worktree_open(&worktree, cwd);
2147 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2148 goto done;
2149 else
2150 error = NULL;
2151 if (worktree) {
2152 repo_path =
2153 strdup(got_worktree_get_repo_path(worktree));
2154 if (repo_path == NULL)
2155 error = got_error_from_errno("strdup");
2156 if (error)
2157 goto done;
2158 } else {
2159 repo_path = strdup(cwd);
2160 if (repo_path == NULL) {
2161 error = got_error_from_errno("strdup");
2162 goto done;
2167 error = got_repo_open(&repo, repo_path);
2168 if (error != NULL)
2169 goto done;
2171 error = apply_unveil(got_repo_get_path(repo), do_list,
2172 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2173 if (error)
2174 goto done;
2176 if (do_list)
2177 error = list_refs(repo);
2178 else if (delref)
2179 error = delete_ref(repo, delref);
2180 else
2181 error = add_ref(repo, argv[0], argv[1]);
2182 done:
2183 if (repo)
2184 got_repo_close(repo);
2185 if (worktree)
2186 got_worktree_close(worktree);
2187 free(cwd);
2188 free(repo_path);
2189 return error;
2192 __dead static void
2193 usage_branch(void)
2195 fprintf(stderr,
2196 "usage: %s branch [-r repository] -l | -d name | "
2197 "name [base-branch]\n", getprogname());
2198 exit(1);
2201 static const struct got_error *
2202 list_branches(struct got_repository *repo)
2204 static const struct got_error *err = NULL;
2205 struct got_reflist_head refs;
2206 struct got_reflist_entry *re;
2208 SIMPLEQ_INIT(&refs);
2209 err = got_ref_list(&refs, repo);
2210 if (err)
2211 return err;
2213 SIMPLEQ_FOREACH(re, &refs, entry) {
2214 const char *refname;
2215 char *refstr;
2216 refname = got_ref_get_name(re->ref);
2217 if (strncmp(refname, "refs/heads/", 11) != 0)
2218 continue;
2219 refname += 11;
2220 refstr = got_ref_to_str(re->ref);
2221 if (refstr == NULL)
2222 return got_error_from_errno("got_ref_to_str");
2223 printf("%s: %s\n", refname, refstr);
2224 free(refstr);
2227 got_ref_list_free(&refs);
2228 return NULL;
2231 static const struct got_error *
2232 delete_branch(struct got_repository *repo, const char *branch_name)
2234 const struct got_error *err = NULL;
2235 struct got_reference *ref;
2236 char *refname;
2238 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2239 return got_error_from_errno("asprintf");
2241 err = got_ref_open(&ref, repo, refname, 0);
2242 if (err)
2243 goto done;
2245 err = got_ref_delete(ref, repo);
2246 got_ref_close(ref);
2247 done:
2248 free(refname);
2249 return err;
2252 static const struct got_error *
2253 add_branch(struct got_repository *repo, const char *branch_name,
2254 const char *base_branch)
2256 const struct got_error *err = NULL;
2257 struct got_object_id *id = NULL;
2258 struct got_reference *ref = NULL;
2259 char *base_refname = NULL, *refname = NULL;
2260 struct got_reference *base_ref;
2262 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2263 base_refname = strdup(GOT_REF_HEAD);
2264 if (base_refname == NULL)
2265 return got_error_from_errno("strdup");
2266 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2267 return got_error_from_errno("asprintf");
2269 err = got_ref_open(&base_ref, repo, base_refname, 0);
2270 if (err)
2271 goto done;
2272 err = got_ref_resolve(&id, repo, base_ref);
2273 got_ref_close(base_ref);
2274 if (err)
2275 goto done;
2277 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2278 err = got_error_from_errno("asprintf");
2279 goto done;
2282 err = got_ref_open(&ref, repo, refname, 0);
2283 if (err == NULL) {
2284 err = got_error(GOT_ERR_BRANCH_EXISTS);
2285 goto done;
2286 } else if (err->code != GOT_ERR_NOT_REF)
2287 goto done;
2289 err = got_ref_alloc(&ref, refname, id);
2290 if (err)
2291 goto done;
2293 err = got_ref_write(ref, repo);
2294 done:
2295 if (ref)
2296 got_ref_close(ref);
2297 free(id);
2298 free(base_refname);
2299 free(refname);
2300 return err;
2303 static const struct got_error *
2304 cmd_branch(int argc, char *argv[])
2306 const struct got_error *error = NULL;
2307 struct got_repository *repo = NULL;
2308 struct got_worktree *worktree = NULL;
2309 char *cwd = NULL, *repo_path = NULL;
2310 int ch, do_list = 0;
2311 const char *delref = NULL;
2313 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2314 switch (ch) {
2315 case 'd':
2316 delref = optarg;
2317 break;
2318 case 'r':
2319 repo_path = realpath(optarg, NULL);
2320 if (repo_path == NULL)
2321 err(1, "-r option");
2322 got_path_strip_trailing_slashes(repo_path);
2323 break;
2324 case 'l':
2325 do_list = 1;
2326 break;
2327 default:
2328 usage_branch();
2329 /* NOTREACHED */
2333 if (do_list && delref)
2334 errx(1, "-l and -d options are mutually exclusive\n");
2336 argc -= optind;
2337 argv += optind;
2339 if (do_list || delref) {
2340 if (argc > 0)
2341 usage_branch();
2342 } else if (argc < 1 || argc > 2)
2343 usage_branch();
2345 #ifndef PROFILE
2346 if (do_list) {
2347 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2348 NULL) == -1)
2349 err(1, "pledge");
2350 } else {
2351 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2352 "sendfd unveil", NULL) == -1)
2353 err(1, "pledge");
2355 #endif
2356 cwd = getcwd(NULL, 0);
2357 if (cwd == NULL) {
2358 error = got_error_from_errno("getcwd");
2359 goto done;
2362 if (repo_path == NULL) {
2363 error = got_worktree_open(&worktree, cwd);
2364 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2365 goto done;
2366 else
2367 error = NULL;
2368 if (worktree) {
2369 repo_path =
2370 strdup(got_worktree_get_repo_path(worktree));
2371 if (repo_path == NULL)
2372 error = got_error_from_errno("strdup");
2373 if (error)
2374 goto done;
2375 } else {
2376 repo_path = strdup(cwd);
2377 if (repo_path == NULL) {
2378 error = got_error_from_errno("strdup");
2379 goto done;
2384 error = got_repo_open(&repo, repo_path);
2385 if (error != NULL)
2386 goto done;
2388 error = apply_unveil(got_repo_get_path(repo), do_list,
2389 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2390 if (error)
2391 goto done;
2393 if (do_list)
2394 error = list_branches(repo);
2395 else if (delref)
2396 error = delete_branch(repo, delref);
2397 else {
2398 const char *base_branch;
2399 if (argc == 1) {
2400 base_branch = worktree ?
2401 got_worktree_get_head_ref_name(worktree) :
2402 GOT_REF_HEAD;
2403 } else
2404 base_branch = argv[1];
2405 error = add_branch(repo, argv[0], base_branch);
2407 done:
2408 if (repo)
2409 got_repo_close(repo);
2410 if (worktree)
2411 got_worktree_close(worktree);
2412 free(cwd);
2413 free(repo_path);
2414 return error;
2417 __dead static void
2418 usage_add(void)
2420 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2421 exit(1);
2424 static const struct got_error *
2425 cmd_add(int argc, char *argv[])
2427 const struct got_error *error = NULL;
2428 struct got_repository *repo = NULL;
2429 struct got_worktree *worktree = NULL;
2430 char *cwd = NULL;
2431 struct got_pathlist_head paths;
2432 struct got_pathlist_entry *pe;
2433 int ch, x;
2435 TAILQ_INIT(&paths);
2437 while ((ch = getopt(argc, argv, "")) != -1) {
2438 switch (ch) {
2439 default:
2440 usage_add();
2441 /* NOTREACHED */
2445 argc -= optind;
2446 argv += optind;
2448 if (argc < 1)
2449 usage_add();
2451 /* make sure each file exists before doing anything halfway */
2452 for (x = 0; x < argc; x++) {
2453 char *path = realpath(argv[x], NULL);
2454 if (path == NULL) {
2455 error = got_error_from_errno2("realpath", argv[x]);
2456 goto done;
2458 free(path);
2461 cwd = getcwd(NULL, 0);
2462 if (cwd == NULL) {
2463 error = got_error_from_errno("getcwd");
2464 goto done;
2467 error = got_worktree_open(&worktree, cwd);
2468 if (error)
2469 goto done;
2471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2472 if (error != NULL)
2473 goto done;
2475 error = apply_unveil(got_repo_get_path(repo), 1,
2476 got_worktree_get_root_path(worktree), 0);
2477 if (error)
2478 goto done;
2480 for (x = 0; x < argc; x++) {
2481 char *path = realpath(argv[x], NULL);
2482 if (path == NULL) {
2483 error = got_error_from_errno2("realpath", argv[x]);
2484 goto done;
2487 got_path_strip_trailing_slashes(path);
2488 error = got_pathlist_insert(&pe, &paths, path, NULL);
2489 if (error) {
2490 free(path);
2491 goto done;
2494 error = got_worktree_schedule_add(worktree, &paths, print_status,
2495 NULL, repo);
2496 done:
2497 if (repo)
2498 got_repo_close(repo);
2499 if (worktree)
2500 got_worktree_close(worktree);
2501 TAILQ_FOREACH(pe, &paths, entry)
2502 free((char *)pe->path);
2503 got_pathlist_free(&paths);
2504 free(cwd);
2505 return error;
2508 __dead static void
2509 usage_rm(void)
2511 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2512 exit(1);
2515 static const struct got_error *
2516 cmd_rm(int argc, char *argv[])
2518 const struct got_error *error = NULL;
2519 struct got_worktree *worktree = NULL;
2520 struct got_repository *repo = NULL;
2521 char *cwd = NULL;
2522 struct got_pathlist_head paths;
2523 struct got_pathlist_entry *pe;
2524 int ch, i, delete_local_mods = 0;
2526 TAILQ_INIT(&paths);
2528 while ((ch = getopt(argc, argv, "f")) != -1) {
2529 switch (ch) {
2530 case 'f':
2531 delete_local_mods = 1;
2532 break;
2533 default:
2534 usage_add();
2535 /* NOTREACHED */
2539 argc -= optind;
2540 argv += optind;
2542 if (argc < 1)
2543 usage_rm();
2545 /* make sure each file exists before doing anything halfway */
2546 for (i = 0; i < argc; i++) {
2547 char *path = realpath(argv[i], NULL);
2548 if (path == NULL) {
2549 error = got_error_from_errno2("realpath", argv[i]);
2550 goto done;
2552 free(path);
2555 cwd = getcwd(NULL, 0);
2556 if (cwd == NULL) {
2557 error = got_error_from_errno("getcwd");
2558 goto done;
2560 error = got_worktree_open(&worktree, cwd);
2561 if (error)
2562 goto done;
2564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2565 if (error)
2566 goto done;
2568 error = apply_unveil(got_repo_get_path(repo), 1,
2569 got_worktree_get_root_path(worktree), 0);
2570 if (error)
2571 goto done;
2573 for (i = 0; i < argc; i++) {
2574 char *path = realpath(argv[i], NULL);
2575 if (path == NULL) {
2576 error = got_error_from_errno2("realpath", argv[i]);
2577 goto done;
2580 got_path_strip_trailing_slashes(path);
2581 error = got_pathlist_insert(&pe, &paths, path, NULL);
2582 if (error) {
2583 free(path);
2584 goto done;
2587 error = got_worktree_schedule_delete(worktree, &paths,
2588 delete_local_mods, print_status, NULL, repo);
2589 if (error)
2590 goto done;
2591 done:
2592 if (repo)
2593 got_repo_close(repo);
2594 if (worktree)
2595 got_worktree_close(worktree);
2596 TAILQ_FOREACH(pe, &paths, entry)
2597 free((char *)pe->path);
2598 got_pathlist_free(&paths);
2599 free(cwd);
2600 return error;
2603 __dead static void
2604 usage_revert(void)
2606 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2607 exit(1);
2610 static void
2611 revert_progress(void *arg, unsigned char status, const char *path)
2613 while (path[0] == '/')
2614 path++;
2615 printf("%c %s\n", status, path);
2618 static const struct got_error *
2619 cmd_revert(int argc, char *argv[])
2621 const struct got_error *error = NULL;
2622 struct got_worktree *worktree = NULL;
2623 struct got_repository *repo = NULL;
2624 char *cwd = NULL, *path = NULL;
2625 struct got_pathlist_head paths;
2626 struct got_pathlist_entry *pe;
2627 int ch, i;
2629 TAILQ_INIT(&paths);
2631 while ((ch = getopt(argc, argv, "")) != -1) {
2632 switch (ch) {
2633 default:
2634 usage_revert();
2635 /* NOTREACHED */
2639 argc -= optind;
2640 argv += optind;
2642 if (argc < 1)
2643 usage_revert();
2645 for (i = 0; i < argc; i++) {
2646 char *path = realpath(argv[i], NULL);
2647 if (path == NULL) {
2648 error = got_error_from_errno2("realpath", argv[i]);
2649 goto done;
2652 got_path_strip_trailing_slashes(path);
2653 error = got_pathlist_insert(&pe, &paths, path, NULL);
2654 if (error) {
2655 free(path);
2656 goto done;
2660 cwd = getcwd(NULL, 0);
2661 if (cwd == NULL) {
2662 error = got_error_from_errno("getcwd");
2663 goto done;
2665 error = got_worktree_open(&worktree, cwd);
2666 if (error)
2667 goto done;
2669 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2670 if (error != NULL)
2671 goto done;
2673 error = apply_unveil(got_repo_get_path(repo), 1,
2674 got_worktree_get_root_path(worktree), 0);
2675 if (error)
2676 goto done;
2678 error = got_worktree_revert(worktree, &paths,
2679 revert_progress, NULL, repo);
2680 if (error)
2681 goto done;
2682 done:
2683 if (repo)
2684 got_repo_close(repo);
2685 if (worktree)
2686 got_worktree_close(worktree);
2687 free(path);
2688 free(cwd);
2689 return error;
2692 __dead static void
2693 usage_commit(void)
2695 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2696 exit(1);
2699 int
2700 spawn_editor(const char *editor, const char *file)
2702 pid_t pid;
2703 sig_t sighup, sigint, sigquit;
2704 int st = -1;
2706 sighup = signal(SIGHUP, SIG_IGN);
2707 sigint = signal(SIGINT, SIG_IGN);
2708 sigquit = signal(SIGQUIT, SIG_IGN);
2710 switch (pid = fork()) {
2711 case -1:
2712 goto doneediting;
2713 case 0:
2714 execl(editor, editor, file, (char *)NULL);
2715 _exit(127);
2718 while (waitpid(pid, &st, 0) == -1)
2719 if (errno != EINTR)
2720 break;
2722 doneediting:
2723 (void)signal(SIGHUP, sighup);
2724 (void)signal(SIGINT, sigint);
2725 (void)signal(SIGQUIT, sigquit);
2727 if (!WIFEXITED(st)) {
2728 errno = EINTR;
2729 return -1;
2732 return WEXITSTATUS(st);
2735 struct collect_commit_logmsg_arg {
2736 const char *cmdline_log;
2737 const char *editor;
2738 const char *worktree_path;
2739 const char *branch_name;
2740 const char *repo_path;
2741 char *logmsg_path;
2745 static const struct got_error *
2746 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2747 void *arg)
2749 char *initial_content = NULL;
2750 struct got_pathlist_entry *pe;
2751 const struct got_error *err = NULL;
2752 char *template = NULL;
2753 struct collect_commit_logmsg_arg *a = arg;
2754 char buf[1024];
2755 struct stat st, st2;
2756 FILE *fp;
2757 size_t len;
2758 int fd, content_changed = 0;
2760 /* if a message was specified on the command line, just use it */
2761 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2762 len = strlen(a->cmdline_log) + 1;
2763 *logmsg = malloc(len + 1);
2764 if (*logmsg == NULL)
2765 return got_error_from_errno("malloc");
2766 strlcpy(*logmsg, a->cmdline_log, len);
2767 return NULL;
2770 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2771 return got_error_from_errno("asprintf");
2773 if (asprintf(&initial_content,
2774 "\n# changes to be committed on branch %s:\n",
2775 a->branch_name) == -1)
2776 return got_error_from_errno("asprintf");
2778 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2779 if (err)
2780 goto done;
2782 dprintf(fd, initial_content);
2784 TAILQ_FOREACH(pe, commitable_paths, entry) {
2785 struct got_commitable *ct = pe->data;
2786 dprintf(fd, "# %c %s\n",
2787 got_commitable_get_status(ct),
2788 got_commitable_get_path(ct));
2790 close(fd);
2792 if (stat(a->logmsg_path, &st) == -1) {
2793 err = got_error_from_errno2("stat", a->logmsg_path);
2794 goto done;
2797 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2798 err = got_error_from_errno("failed spawning editor");
2799 goto done;
2802 if (stat(a->logmsg_path, &st2) == -1) {
2803 err = got_error_from_errno("stat");
2804 goto done;
2807 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2808 unlink(a->logmsg_path);
2809 free(a->logmsg_path);
2810 a->logmsg_path = NULL;
2811 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2812 "no changes made to commit message, aborting");
2813 goto done;
2816 *logmsg = malloc(st2.st_size + 1);
2817 if (*logmsg == NULL) {
2818 err = got_error_from_errno("malloc");
2819 goto done;
2821 (*logmsg)[0] = '\0';
2822 len = 0;
2824 fp = fopen(a->logmsg_path, "r");
2825 if (fp == NULL) {
2826 err = got_error_from_errno("fopen");
2827 goto done;
2829 while (fgets(buf, sizeof(buf), fp) != NULL) {
2830 if (!content_changed && strcmp(buf, initial_content) != 0)
2831 content_changed = 1;
2832 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2833 continue; /* remove comments and leading empty lines */
2834 len = strlcat(*logmsg, buf, st2.st_size);
2836 fclose(fp);
2838 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2839 (*logmsg)[len - 1] = '\0';
2840 len--;
2843 if (len == 0 || !content_changed) {
2844 unlink(a->logmsg_path);
2845 free(a->logmsg_path);
2846 a->logmsg_path = NULL;
2847 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2848 "commit message cannot be empty, aborting");
2849 goto done;
2851 done:
2852 free(initial_content);
2853 free(template);
2855 /* Editor is done; we can now apply unveil(2) */
2856 if (err == NULL)
2857 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2858 return err;
2861 static const struct got_error *
2862 cmd_commit(int argc, char *argv[])
2864 const struct got_error *error = NULL;
2865 struct got_worktree *worktree = NULL;
2866 struct got_repository *repo = NULL;
2867 char *cwd = NULL, *path = NULL, *id_str = NULL;
2868 struct got_object_id *id = NULL;
2869 const char *logmsg = NULL;
2870 const char *got_author = getenv("GOT_AUTHOR");
2871 struct collect_commit_logmsg_arg cl_arg;
2872 char *editor = NULL;
2873 int ch;
2875 while ((ch = getopt(argc, argv, "m:")) != -1) {
2876 switch (ch) {
2877 case 'm':
2878 logmsg = optarg;
2879 break;
2880 default:
2881 usage_commit();
2882 /* NOTREACHED */
2886 argc -= optind;
2887 argv += optind;
2889 if (argc == 1) {
2890 path = realpath(argv[0], NULL);
2891 if (path == NULL) {
2892 error = got_error_from_errno2("realpath", argv[0]);
2893 goto done;
2895 got_path_strip_trailing_slashes(path);
2896 } else if (argc != 0)
2897 usage_commit();
2899 if (got_author == NULL) {
2900 /* TODO: Look current user up in password database */
2901 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2902 goto done;
2905 cwd = getcwd(NULL, 0);
2906 if (cwd == NULL) {
2907 error = got_error_from_errno("getcwd");
2908 goto done;
2910 error = got_worktree_open(&worktree, cwd);
2911 if (error)
2912 goto done;
2914 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2915 if (error != NULL)
2916 goto done;
2919 * unveil(2) traverses exec(2); if an editor is used we have
2920 * to apply unveil after the log message has been written.
2922 if (logmsg == NULL || strlen(logmsg) == 0)
2923 error = get_editor(&editor);
2924 else
2925 error = apply_unveil(got_repo_get_path(repo), 0,
2926 got_worktree_get_root_path(worktree), 0);
2927 if (error)
2928 goto done;
2930 cl_arg.editor = editor;
2931 cl_arg.cmdline_log = logmsg;
2932 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2933 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2934 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2935 cl_arg.branch_name += 5;
2936 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2937 cl_arg.branch_name += 6;
2938 cl_arg.repo_path = got_repo_get_path(repo);
2939 cl_arg.logmsg_path = NULL;
2940 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2941 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2942 if (error) {
2943 if (cl_arg.logmsg_path)
2944 fprintf(stderr, "%s: log message preserved in %s\n",
2945 getprogname(), cl_arg.logmsg_path);
2946 goto done;
2949 if (cl_arg.logmsg_path)
2950 unlink(cl_arg.logmsg_path);
2952 error = got_object_id_str(&id_str, id);
2953 if (error)
2954 goto done;
2955 printf("Created commit %s\n", id_str);
2956 done:
2957 if (repo)
2958 got_repo_close(repo);
2959 if (worktree)
2960 got_worktree_close(worktree);
2961 free(path);
2962 free(cwd);
2963 free(id_str);
2964 free(editor);
2965 return error;
2968 __dead static void
2969 usage_cherrypick(void)
2971 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2972 exit(1);
2975 static const struct got_error *
2976 cmd_cherrypick(int argc, char *argv[])
2978 const struct got_error *error = NULL;
2979 struct got_worktree *worktree = NULL;
2980 struct got_repository *repo = NULL;
2981 char *cwd = NULL, *commit_id_str = NULL;
2982 struct got_object_id *commit_id = NULL;
2983 struct got_commit_object *commit = NULL;
2984 struct got_object_qid *pid;
2985 struct got_reference *head_ref = NULL;
2986 int ch, did_something = 0;
2988 while ((ch = getopt(argc, argv, "")) != -1) {
2989 switch (ch) {
2990 default:
2991 usage_cherrypick();
2992 /* NOTREACHED */
2996 argc -= optind;
2997 argv += optind;
2999 if (argc != 1)
3000 usage_cherrypick();
3002 cwd = getcwd(NULL, 0);
3003 if (cwd == NULL) {
3004 error = got_error_from_errno("getcwd");
3005 goto done;
3007 error = got_worktree_open(&worktree, cwd);
3008 if (error)
3009 goto done;
3011 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3012 if (error != NULL)
3013 goto done;
3015 error = apply_unveil(got_repo_get_path(repo), 0,
3016 got_worktree_get_root_path(worktree), 0);
3017 if (error)
3018 goto done;
3020 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3021 GOT_OBJ_TYPE_COMMIT, repo);
3022 if (error != NULL) {
3023 struct got_reference *ref;
3024 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3025 goto done;
3026 error = got_ref_open(&ref, repo, argv[0], 0);
3027 if (error != NULL)
3028 goto done;
3029 error = got_ref_resolve(&commit_id, repo, ref);
3030 got_ref_close(ref);
3031 if (error != NULL)
3032 goto done;
3034 error = got_object_id_str(&commit_id_str, commit_id);
3035 if (error)
3036 goto done;
3038 error = got_ref_open(&head_ref, repo,
3039 got_worktree_get_head_ref_name(worktree), 0);
3040 if (error != NULL)
3041 goto done;
3043 error = check_same_branch(commit_id, head_ref, repo);
3044 if (error) {
3045 if (error->code != GOT_ERR_ANCESTRY)
3046 goto done;
3047 error = NULL;
3048 } else {
3049 error = got_error(GOT_ERR_SAME_BRANCH);
3050 goto done;
3053 error = got_object_open_as_commit(&commit, repo, commit_id);
3054 if (error)
3055 goto done;
3056 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3057 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3058 commit_id, repo, update_progress, &did_something, check_cancelled,
3059 NULL);
3060 if (error != NULL)
3061 goto done;
3063 if (did_something)
3064 printf("Merged commit %s\n", commit_id_str);
3065 done:
3066 if (commit)
3067 got_object_commit_close(commit);
3068 free(commit_id_str);
3069 if (head_ref)
3070 got_ref_close(head_ref);
3071 if (worktree)
3072 got_worktree_close(worktree);
3073 if (repo)
3074 got_repo_close(repo);
3075 return error;
3078 __dead static void
3079 usage_backout(void)
3081 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3082 exit(1);
3085 static const struct got_error *
3086 cmd_backout(int argc, char *argv[])
3088 const struct got_error *error = NULL;
3089 struct got_worktree *worktree = NULL;
3090 struct got_repository *repo = NULL;
3091 char *cwd = NULL, *commit_id_str = NULL;
3092 struct got_object_id *commit_id = NULL;
3093 struct got_commit_object *commit = NULL;
3094 struct got_object_qid *pid;
3095 struct got_reference *head_ref = NULL;
3096 int ch, did_something = 0;
3098 while ((ch = getopt(argc, argv, "")) != -1) {
3099 switch (ch) {
3100 default:
3101 usage_backout();
3102 /* NOTREACHED */
3106 argc -= optind;
3107 argv += optind;
3109 if (argc != 1)
3110 usage_backout();
3112 cwd = getcwd(NULL, 0);
3113 if (cwd == NULL) {
3114 error = got_error_from_errno("getcwd");
3115 goto done;
3117 error = got_worktree_open(&worktree, cwd);
3118 if (error)
3119 goto done;
3121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3122 if (error != NULL)
3123 goto done;
3125 error = apply_unveil(got_repo_get_path(repo), 0,
3126 got_worktree_get_root_path(worktree), 0);
3127 if (error)
3128 goto done;
3130 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3131 GOT_OBJ_TYPE_COMMIT, repo);
3132 if (error != NULL) {
3133 struct got_reference *ref;
3134 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3135 goto done;
3136 error = got_ref_open(&ref, repo, argv[0], 0);
3137 if (error != NULL)
3138 goto done;
3139 error = got_ref_resolve(&commit_id, repo, ref);
3140 got_ref_close(ref);
3141 if (error != NULL)
3142 goto done;
3144 error = got_object_id_str(&commit_id_str, commit_id);
3145 if (error)
3146 goto done;
3148 error = got_ref_open(&head_ref, repo,
3149 got_worktree_get_head_ref_name(worktree), 0);
3150 if (error != NULL)
3151 goto done;
3153 error = check_same_branch(commit_id, head_ref, repo);
3154 if (error)
3155 goto done;
3157 error = got_object_open_as_commit(&commit, repo, commit_id);
3158 if (error)
3159 goto done;
3160 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3161 if (pid == NULL) {
3162 error = got_error(GOT_ERR_ROOT_COMMIT);
3163 goto done;
3166 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3167 update_progress, &did_something, check_cancelled, NULL);
3168 if (error != NULL)
3169 goto done;
3171 if (did_something)
3172 printf("Backed out commit %s\n", commit_id_str);
3173 done:
3174 if (commit)
3175 got_object_commit_close(commit);
3176 free(commit_id_str);
3177 if (head_ref)
3178 got_ref_close(head_ref);
3179 if (worktree)
3180 got_worktree_close(worktree);
3181 if (repo)
3182 got_repo_close(repo);
3183 return error;