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 cmd {
70 const char *cmd_name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 };
75 __dead static void usage(void);
76 __dead static void usage_init(void);
77 __dead static void usage_checkout(void);
78 __dead static void usage_update(void);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_status(void);
84 __dead static void usage_ref(void);
85 __dead static void usage_branch(void);
86 __dead static void usage_add(void);
87 __dead static void usage_rm(void);
88 __dead static void usage_revert(void);
89 __dead static void usage_commit(void);
90 __dead static void usage_cherrypick(void);
91 __dead static void usage_backout(void);
93 static const struct got_error* cmd_init(int, char *[]);
94 static const struct got_error* cmd_checkout(int, char *[]);
95 static const struct got_error* cmd_update(int, char *[]);
96 static const struct got_error* cmd_log(int, char *[]);
97 static const struct got_error* cmd_diff(int, char *[]);
98 static const struct got_error* cmd_blame(int, char *[]);
99 static const struct got_error* cmd_tree(int, char *[]);
100 static const struct got_error* cmd_status(int, char *[]);
101 static const struct got_error* cmd_ref(int, char *[]);
102 static const struct got_error* cmd_branch(int, char *[]);
103 static const struct got_error* cmd_add(int, char *[]);
104 static const struct got_error* cmd_rm(int, char *[]);
105 static const struct got_error* cmd_revert(int, char *[]);
106 static const struct got_error* cmd_commit(int, char *[]);
107 static const struct got_error* cmd_cherrypick(int, char *[]);
108 static const struct got_error* cmd_backout(int, char *[]);
110 static struct cmd got_commands[] = {
111 { "init", cmd_init, usage_init },
112 { "checkout", cmd_checkout, usage_checkout },
113 { "update", cmd_update, usage_update },
114 { "log", cmd_log, usage_log },
115 { "diff", cmd_diff, usage_diff },
116 { "blame", cmd_blame, usage_blame },
117 { "tree", cmd_tree, usage_tree },
118 { "status", cmd_status, usage_status },
119 { "ref", cmd_ref, usage_ref },
120 { "branch", cmd_branch, usage_branch },
121 { "add", cmd_add, usage_add },
122 { "rm", cmd_rm, usage_rm },
123 { "revert", cmd_revert, usage_revert },
124 { "commit", cmd_commit, usage_commit },
125 { "cherrypick", cmd_cherrypick, usage_cherrypick },
126 { "backout", cmd_backout, usage_backout },
127 };
129 int
130 main(int argc, char *argv[])
132 struct cmd *cmd;
133 unsigned int i;
134 int ch;
135 int hflag = 0;
137 setlocale(LC_CTYPE, "");
139 while ((ch = getopt(argc, argv, "h")) != -1) {
140 switch (ch) {
141 case 'h':
142 hflag = 1;
143 break;
144 default:
145 usage();
146 /* NOTREACHED */
150 argc -= optind;
151 argv += optind;
152 optind = 0;
154 if (argc <= 0)
155 usage();
157 signal(SIGINT, catch_sigint);
158 signal(SIGPIPE, catch_sigpipe);
160 for (i = 0; i < nitems(got_commands); i++) {
161 const struct got_error *error;
163 cmd = &got_commands[i];
165 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 continue;
168 if (hflag)
169 got_commands[i].cmd_usage();
171 error = got_commands[i].cmd_main(argc, argv);
172 if (error && !(sigint_received || sigpipe_received)) {
173 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
174 return 1;
177 return 0;
180 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
181 return 1;
184 __dead static void
185 usage(void)
187 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
188 exit(1);
191 static const struct got_error *
192 get_editor(char **abspath)
194 const struct got_error *err = NULL;
195 const char *editor;
197 editor = getenv("VISUAL");
198 if (editor == NULL)
199 editor = getenv("EDITOR");
201 if (editor) {
202 err = got_path_find_prog(abspath, editor);
203 if (err)
204 return err;
207 if (*abspath == NULL) {
208 *abspath = strdup("/bin/ed");
209 if (*abspath == NULL)
210 return got_error_from_errno("strdup");
213 return NULL;
216 static const struct got_error *
217 apply_unveil(const char *repo_path, int repo_read_only,
218 const char *worktree_path, int create_worktree)
220 const struct got_error *err;
222 if (create_worktree) {
223 /* Pre-create work tree path to avoid unveiling its parents. */
224 err = got_path_mkdir(worktree_path);
226 if (errno == EEXIST) {
227 if (got_path_dir_is_empty(worktree_path)) {
228 errno = 0;
229 err = NULL;
230 } else {
231 err = got_error_path(worktree_path,
232 GOT_ERR_DIR_NOT_EMPTY);
236 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
237 return err;
240 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
241 return got_error_from_errno2("unveil", repo_path);
243 if (worktree_path && unveil(worktree_path, "rwc") != 0)
244 return got_error_from_errno2("unveil", worktree_path);
246 if (unveil("/tmp", "rwc") != 0)
247 return got_error_from_errno2("unveil", "/tmp");
249 err = got_privsep_unveil_exec_helpers();
250 if (err != NULL)
251 return err;
253 if (unveil(NULL, NULL) != 0)
254 return got_error_from_errno("unveil");
256 return NULL;
259 __dead static void
260 usage_init(void)
262 fprintf(stderr, "usage: %s init path\n", getprogname());
263 exit(1);
266 static const struct got_error *
267 cmd_init(int argc, char *argv[])
269 const struct got_error *error = NULL;
270 char *repo_path = NULL;
271 int ch;
273 while ((ch = getopt(argc, argv, "")) != -1) {
274 switch (ch) {
275 default:
276 usage_init();
277 /* NOTREACHED */
281 argc -= optind;
282 argv += optind;
284 #ifndef PROFILE
285 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
286 err(1, "pledge");
287 #endif
288 if (argc != 1)
289 usage_init();
291 repo_path = strdup(argv[0]);
292 if (repo_path == NULL)
293 return got_error_from_errno("strdup");
295 got_path_strip_trailing_slashes(repo_path);
297 error = got_path_mkdir(repo_path);
298 if (error &&
299 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
300 goto done;
302 error = apply_unveil(repo_path, 0, NULL, 0);
303 if (error)
304 goto done;
306 error = got_repo_init(repo_path);
307 if (error != NULL)
308 goto done;
310 done:
311 free(repo_path);
312 return error;
315 __dead static void
316 usage_checkout(void)
318 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
319 "[-p prefix] repository-path [worktree-path]\n", getprogname());
320 exit(1);
323 static void
324 checkout_progress(void *arg, unsigned char status, const char *path)
326 char *worktree_path = arg;
328 /* Base commit bump happens silently. */
329 if (status == GOT_STATUS_BUMP_BASE)
330 return;
332 while (path[0] == '/')
333 path++;
335 printf("%c %s/%s\n", status, worktree_path, path);
338 static const struct got_error *
339 check_cancelled(void *arg)
341 if (sigint_received || sigpipe_received)
342 return got_error(GOT_ERR_CANCELLED);
343 return NULL;
346 static const struct got_error *
347 check_linear_ancestry(struct got_object_id *commit_id,
348 struct got_object_id *base_commit_id, struct got_repository *repo)
350 const struct got_error *err = NULL;
351 struct got_object_id *yca_id;
353 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
354 commit_id, base_commit_id, repo);
355 if (err)
356 return err;
358 if (yca_id == NULL)
359 return got_error(GOT_ERR_ANCESTRY);
361 /*
362 * Require a straight line of history between the target commit
363 * and the work tree's base commit.
365 * Non-linear situations such as this require a rebase:
367 * (commit) D F (base_commit)
368 * \ /
369 * C E
370 * \ /
371 * B (yca)
372 * |
373 * A
375 * 'got update' only handles linear cases:
376 * Update forwards in time: A (base/yca) - B - C - D (commit)
377 * Update backwards in time: D (base) - C - B - A (commit/yca)
378 */
379 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
380 got_object_id_cmp(base_commit_id, yca_id) != 0)
381 return got_error(GOT_ERR_ANCESTRY);
383 free(yca_id);
384 return NULL;
387 static const struct got_error *
388 check_same_branch(struct got_object_id *commit_id,
389 struct got_reference *head_ref, struct got_repository *repo)
391 const struct got_error *err = NULL;
392 struct got_commit_graph *graph = NULL;
393 struct got_object_id *head_commit_id = NULL;
394 int is_same_branch = 0;
396 err = got_ref_resolve(&head_commit_id, repo, head_ref);
397 if (err)
398 goto done;
400 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
401 if (err)
402 goto done;
404 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
405 if (err)
406 goto done;
408 for (;;) {
409 struct got_object_id *id;
410 err = got_commit_graph_iter_next(&id, graph);
411 if (err) {
412 if (err->code == GOT_ERR_ITER_COMPLETED) {
413 err = NULL;
414 break;
416 else if (err->code != GOT_ERR_ITER_NEED_MORE)
417 break;
418 err = got_commit_graph_fetch_commits(graph, 1,
419 repo);
420 if (err)
421 break;
424 if (id) {
425 if (got_object_id_cmp(id, commit_id) == 0) {
426 is_same_branch = 1;
427 break;
431 done:
432 if (graph)
433 got_commit_graph_close(graph);
434 free(head_commit_id);
435 if (!err && !is_same_branch)
436 err = got_error(GOT_ERR_ANCESTRY);
437 return err;
440 static const struct got_error *
441 cmd_checkout(int argc, char *argv[])
443 const struct got_error *error = NULL;
444 struct got_repository *repo = NULL;
445 struct got_reference *head_ref = NULL;
446 struct got_worktree *worktree = NULL;
447 char *repo_path = NULL;
448 char *worktree_path = NULL;
449 const char *path_prefix = "";
450 const char *branch_name = GOT_REF_HEAD;
451 char *commit_id_str = NULL;
452 int ch, same_path_prefix;
454 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
455 switch (ch) {
456 case 'b':
457 branch_name = optarg;
458 break;
459 case 'c':
460 commit_id_str = strdup(optarg);
461 if (commit_id_str == NULL)
462 return got_error_from_errno("strdup");
463 break;
464 case 'p':
465 path_prefix = optarg;
466 break;
467 default:
468 usage_checkout();
469 /* NOTREACHED */
473 argc -= optind;
474 argv += optind;
476 #ifndef PROFILE
477 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
478 "unveil", NULL) == -1)
479 err(1, "pledge");
480 #endif
481 if (argc == 1) {
482 char *cwd, *base, *dotgit;
483 repo_path = realpath(argv[0], NULL);
484 if (repo_path == NULL)
485 return got_error_from_errno2("realpath", argv[0]);
486 cwd = getcwd(NULL, 0);
487 if (cwd == NULL) {
488 error = got_error_from_errno("getcwd");
489 goto done;
491 if (path_prefix[0]) {
492 base = basename(path_prefix);
493 if (base == NULL) {
494 error = got_error_from_errno2("basename",
495 path_prefix);
496 goto done;
498 } else {
499 base = basename(repo_path);
500 if (base == NULL) {
501 error = got_error_from_errno2("basename",
502 repo_path);
503 goto done;
506 dotgit = strstr(base, ".git");
507 if (dotgit)
508 *dotgit = '\0';
509 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
510 error = got_error_from_errno("asprintf");
511 free(cwd);
512 goto done;
514 free(cwd);
515 } else if (argc == 2) {
516 repo_path = realpath(argv[0], NULL);
517 if (repo_path == NULL) {
518 error = got_error_from_errno2("realpath", argv[0]);
519 goto done;
521 worktree_path = realpath(argv[1], NULL);
522 if (worktree_path == NULL) {
523 error = got_error_from_errno2("realpath", argv[1]);
524 goto done;
526 } else
527 usage_checkout();
529 got_path_strip_trailing_slashes(repo_path);
530 got_path_strip_trailing_slashes(worktree_path);
532 error = got_repo_open(&repo, repo_path);
533 if (error != NULL)
534 goto done;
536 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
537 if (error)
538 goto done;
540 error = got_ref_open(&head_ref, repo, branch_name, 0);
541 if (error != NULL)
542 goto done;
544 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
545 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
546 goto done;
548 error = got_worktree_open(&worktree, worktree_path);
549 if (error != NULL)
550 goto done;
552 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
553 path_prefix);
554 if (error != NULL)
555 goto done;
556 if (!same_path_prefix) {
557 error = got_error(GOT_ERR_PATH_PREFIX);
558 goto done;
561 if (commit_id_str) {
562 struct got_object_id *commit_id;
563 error = got_object_resolve_id_str(&commit_id, repo,
564 commit_id_str);
565 if (error != NULL)
566 goto done;
567 error = check_linear_ancestry(commit_id,
568 got_worktree_get_base_commit_id(worktree), repo);
569 if (error != NULL) {
570 free(commit_id);
571 goto done;
573 error = check_same_branch(commit_id, head_ref, repo);
574 if (error)
575 goto done;
576 error = got_worktree_set_base_commit_id(worktree, repo,
577 commit_id);
578 free(commit_id);
579 if (error)
580 goto done;
583 error = got_worktree_checkout_files(worktree, "", repo,
584 checkout_progress, worktree_path, check_cancelled, NULL);
585 if (error != NULL)
586 goto done;
588 printf("Now shut up and hack\n");
590 done:
591 free(commit_id_str);
592 free(repo_path);
593 free(worktree_path);
594 return error;
597 __dead static void
598 usage_update(void)
600 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
601 getprogname());
602 exit(1);
605 static void
606 update_progress(void *arg, unsigned char status, const char *path)
608 int *did_something = arg;
610 if (status == GOT_STATUS_EXISTS)
611 return;
613 *did_something = 1;
615 /* Base commit bump happens silently. */
616 if (status == GOT_STATUS_BUMP_BASE)
617 return;
619 while (path[0] == '/')
620 path++;
621 printf("%c %s\n", status, path);
624 static const struct got_error *
625 switch_head_ref(struct got_reference *head_ref,
626 struct got_object_id *commit_id, struct got_worktree *worktree,
627 struct got_repository *repo)
629 const struct got_error *err = NULL;
630 char *base_id_str;
631 int ref_has_moved = 0;
633 /* Trivial case: switching between two different references. */
634 if (strcmp(got_ref_get_name(head_ref),
635 got_worktree_get_head_ref_name(worktree)) != 0) {
636 printf("Switching work tree from %s to %s\n",
637 got_worktree_get_head_ref_name(worktree),
638 got_ref_get_name(head_ref));
639 return got_worktree_set_head_ref(worktree, head_ref);
642 err = check_linear_ancestry(commit_id,
643 got_worktree_get_base_commit_id(worktree), repo);
644 if (err) {
645 if (err->code != GOT_ERR_ANCESTRY)
646 return err;
647 ref_has_moved = 1;
649 if (!ref_has_moved)
650 return NULL;
652 /* Switching to a rebased branch with the same reference name. */
653 err = got_object_id_str(&base_id_str,
654 got_worktree_get_base_commit_id(worktree));
655 if (err)
656 return err;
657 printf("Reference %s now points at a different branch\n",
658 got_worktree_get_head_ref_name(worktree));
659 printf("Switching work tree from %s to %s\n", base_id_str,
660 got_worktree_get_head_ref_name(worktree));
661 return NULL;
664 static const struct got_error *
665 cmd_update(int argc, char *argv[])
667 const struct got_error *error = NULL;
668 struct got_repository *repo = NULL;
669 struct got_worktree *worktree = NULL;
670 char *worktree_path = NULL, *path = NULL;
671 struct got_object_id *commit_id = NULL;
672 char *commit_id_str = NULL;
673 const char *branch_name = NULL;
674 struct got_reference *head_ref = NULL;
675 int ch, did_something = 0;
677 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
678 switch (ch) {
679 case 'b':
680 branch_name = optarg;
681 break;
682 case 'c':
683 commit_id_str = strdup(optarg);
684 if (commit_id_str == NULL)
685 return got_error_from_errno("strdup");
686 break;
687 default:
688 usage_update();
689 /* NOTREACHED */
693 argc -= optind;
694 argv += optind;
696 #ifndef PROFILE
697 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
698 "unveil", NULL) == -1)
699 err(1, "pledge");
700 #endif
701 worktree_path = getcwd(NULL, 0);
702 if (worktree_path == NULL) {
703 error = got_error_from_errno("getcwd");
704 goto done;
706 error = got_worktree_open(&worktree, worktree_path);
707 if (error)
708 goto done;
710 if (argc == 0) {
711 path = strdup("");
712 if (path == NULL) {
713 error = got_error_from_errno("strdup");
714 goto done;
716 } else if (argc == 1) {
717 error = got_worktree_resolve_path(&path, worktree, argv[0]);
718 if (error)
719 goto done;
720 } else
721 usage_update();
723 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
724 if (error != NULL)
725 goto done;
727 error = apply_unveil(got_repo_get_path(repo), 0,
728 got_worktree_get_root_path(worktree), 0);
729 if (error)
730 goto done;
732 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
733 got_worktree_get_head_ref_name(worktree), 0);
734 if (error != NULL)
735 goto done;
736 if (commit_id_str == NULL) {
737 error = got_ref_resolve(&commit_id, repo, head_ref);
738 if (error != NULL)
739 goto done;
740 error = got_object_id_str(&commit_id_str, commit_id);
741 if (error != NULL)
742 goto done;
743 } else {
744 error = got_object_resolve_id_str(&commit_id, repo,
745 commit_id_str);
746 if (error != NULL)
747 goto done;
750 if (branch_name) {
751 struct got_object_id *head_commit_id;
752 if (strlen(path) != 0) {
753 fprintf(stderr, "%s: switching between branches "
754 "requires that the entire work tree "
755 "gets updated, not just '%s'\n",
756 getprogname(), path);
757 error = got_error(GOT_ERR_BAD_PATH);
758 goto done;
760 error = got_ref_resolve(&head_commit_id, repo, head_ref);
761 if (error)
762 goto done;
763 error = check_linear_ancestry(commit_id, head_commit_id, repo);
764 free(head_commit_id);
765 if (error != NULL)
766 goto done;
767 error = check_same_branch(commit_id, head_ref, repo);
768 if (error)
769 goto done;
770 error = switch_head_ref(head_ref, commit_id, worktree, repo);
771 if (error)
772 goto done;
773 } else {
774 error = check_linear_ancestry(commit_id,
775 got_worktree_get_base_commit_id(worktree), repo);
776 if (error != NULL) {
777 if (error->code == GOT_ERR_ANCESTRY)
778 error = got_error(GOT_ERR_BRANCH_MOVED);
779 goto done;
781 error = check_same_branch(commit_id, head_ref, repo);
782 if (error)
783 goto done;
786 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
787 commit_id) != 0) {
788 error = got_worktree_set_base_commit_id(worktree, repo,
789 commit_id);
790 if (error)
791 goto done;
794 error = got_worktree_checkout_files(worktree, path, repo,
795 update_progress, &did_something, check_cancelled, NULL);
796 if (error != NULL)
797 goto done;
799 if (did_something)
800 printf("Updated to commit %s\n", commit_id_str);
801 else
802 printf("Already up-to-date\n");
803 done:
804 free(worktree_path);
805 free(path);
806 free(commit_id);
807 free(commit_id_str);
808 return error;
811 static const struct got_error *
812 print_patch(struct got_commit_object *commit, struct got_object_id *id,
813 int diff_context, struct got_repository *repo)
815 const struct got_error *err = NULL;
816 struct got_tree_object *tree1 = NULL, *tree2;
817 struct got_object_qid *qid;
818 char *id_str1 = NULL, *id_str2;
819 struct got_diff_blob_output_unidiff_arg arg;
821 err = got_object_open_as_tree(&tree2, repo,
822 got_object_commit_get_tree_id(commit));
823 if (err)
824 return err;
826 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
827 if (qid != NULL) {
828 struct got_commit_object *pcommit;
830 err = got_object_open_as_commit(&pcommit, repo, qid->id);
831 if (err)
832 return err;
834 err = got_object_open_as_tree(&tree1, repo,
835 got_object_commit_get_tree_id(pcommit));
836 got_object_commit_close(pcommit);
837 if (err)
838 return err;
840 err = got_object_id_str(&id_str1, qid->id);
841 if (err)
842 return err;
845 err = got_object_id_str(&id_str2, id);
846 if (err)
847 goto done;
849 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
850 arg.diff_context = diff_context;
851 arg.outfile = stdout;
852 err = got_diff_tree(tree1, tree2, "", "", repo,
853 got_diff_blob_output_unidiff, &arg);
854 done:
855 if (tree1)
856 got_object_tree_close(tree1);
857 got_object_tree_close(tree2);
858 free(id_str1);
859 free(id_str2);
860 return err;
863 static char *
864 get_datestr(time_t *time, char *datebuf)
866 char *p, *s = ctime_r(time, datebuf);
867 p = strchr(s, '\n');
868 if (p)
869 *p = '\0';
870 return s;
873 static const struct got_error *
874 print_commit(struct got_commit_object *commit, struct got_object_id *id,
875 struct got_repository *repo, int show_patch, int diff_context,
876 struct got_reflist_head *refs)
878 const struct got_error *err = NULL;
879 char *id_str, *datestr, *logmsg0, *logmsg, *line;
880 char datebuf[26];
881 time_t committer_time;
882 const char *author, *committer;
883 char *refs_str = NULL;
884 struct got_reflist_entry *re;
886 SIMPLEQ_FOREACH(re, refs, entry) {
887 char *s;
888 const char *name;
889 if (got_object_id_cmp(re->id, id) != 0)
890 continue;
891 name = got_ref_get_name(re->ref);
892 if (strcmp(name, GOT_REF_HEAD) == 0)
893 continue;
894 if (strncmp(name, "refs/", 5) == 0)
895 name += 5;
896 if (strncmp(name, "got/", 4) == 0)
897 continue;
898 if (strncmp(name, "heads/", 6) == 0)
899 name += 6;
900 if (strncmp(name, "remotes/", 8) == 0)
901 name += 8;
902 s = refs_str;
903 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
904 name) == -1) {
905 err = got_error_from_errno("asprintf");
906 free(s);
907 break;
909 free(s);
911 err = got_object_id_str(&id_str, id);
912 if (err)
913 return err;
915 printf("-----------------------------------------------\n");
916 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
917 refs_str ? refs_str : "", refs_str ? ")" : "");
918 free(id_str);
919 id_str = NULL;
920 free(refs_str);
921 refs_str = NULL;
922 printf("from: %s\n", got_object_commit_get_author(commit));
923 committer_time = got_object_commit_get_committer_time(commit);
924 datestr = get_datestr(&committer_time, datebuf);
925 printf("date: %s UTC\n", datestr);
926 author = got_object_commit_get_author(commit);
927 committer = got_object_commit_get_committer(commit);
928 if (strcmp(author, committer) != 0)
929 printf("via: %s\n", committer);
930 if (got_object_commit_get_nparents(commit) > 1) {
931 const struct got_object_id_queue *parent_ids;
932 struct got_object_qid *qid;
933 int n = 1;
934 parent_ids = got_object_commit_get_parent_ids(commit);
935 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
936 err = got_object_id_str(&id_str, qid->id);
937 if (err)
938 return err;
939 printf("parent %d: %s\n", n++, id_str);
940 free(id_str);
944 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
945 if (logmsg0 == NULL)
946 return got_error_from_errno("strdup");
948 logmsg = logmsg0;
949 do {
950 line = strsep(&logmsg, "\n");
951 if (line)
952 printf(" %s\n", line);
953 } while (line);
954 free(logmsg0);
956 if (show_patch) {
957 err = print_patch(commit, id, diff_context, repo);
958 if (err == 0)
959 printf("\n");
962 if (fflush(stdout) != 0 && err == NULL)
963 err = got_error_from_errno("fflush");
964 return err;
967 static const struct got_error *
968 print_commits(struct got_object_id *root_id, struct got_repository *repo,
969 char *path, int show_patch, int diff_context, int limit,
970 int first_parent_traversal, struct got_reflist_head *refs)
972 const struct got_error *err;
973 struct got_commit_graph *graph;
975 err = got_commit_graph_open(&graph, root_id, path,
976 first_parent_traversal, repo);
977 if (err)
978 return err;
979 err = got_commit_graph_iter_start(graph, root_id, repo);
980 if (err)
981 goto done;
982 for (;;) {
983 struct got_commit_object *commit;
984 struct got_object_id *id;
986 if (sigint_received || sigpipe_received)
987 break;
989 err = got_commit_graph_iter_next(&id, graph);
990 if (err) {
991 if (err->code == GOT_ERR_ITER_COMPLETED) {
992 err = NULL;
993 break;
995 if (err->code != GOT_ERR_ITER_NEED_MORE)
996 break;
997 err = got_commit_graph_fetch_commits(graph, 1, repo);
998 if (err)
999 break;
1000 else
1001 continue;
1003 if (id == NULL)
1004 break;
1006 err = got_object_open_as_commit(&commit, repo, id);
1007 if (err)
1008 break;
1009 err = print_commit(commit, id, repo, show_patch, diff_context,
1010 refs);
1011 got_object_commit_close(commit);
1012 if (err || (limit && --limit == 0))
1013 break;
1015 done:
1016 got_commit_graph_close(graph);
1017 return err;
1020 __dead static void
1021 usage_log(void)
1023 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1024 "[-r repository-path] [path]\n", getprogname());
1025 exit(1);
1028 static const struct got_error *
1029 cmd_log(int argc, char *argv[])
1031 const struct got_error *error;
1032 struct got_repository *repo = NULL;
1033 struct got_worktree *worktree = NULL;
1034 struct got_commit_object *commit = NULL;
1035 struct got_object_id *id = NULL;
1036 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1037 char *start_commit = NULL;
1038 int diff_context = 3, ch;
1039 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1040 const char *errstr;
1041 struct got_reflist_head refs;
1043 SIMPLEQ_INIT(&refs);
1045 #ifndef PROFILE
1046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1047 NULL)
1048 == -1)
1049 err(1, "pledge");
1050 #endif
1052 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1053 switch (ch) {
1054 case 'b':
1055 first_parent_traversal = 1;
1056 break;
1057 case 'p':
1058 show_patch = 1;
1059 break;
1060 case 'c':
1061 start_commit = optarg;
1062 break;
1063 case 'C':
1064 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1065 &errstr);
1066 if (errstr != NULL)
1067 err(1, "-C option %s", errstr);
1068 break;
1069 case 'l':
1070 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1071 if (errstr != NULL)
1072 err(1, "-l option %s", errstr);
1073 break;
1074 case 'r':
1075 repo_path = realpath(optarg, NULL);
1076 if (repo_path == NULL)
1077 err(1, "-r option");
1078 got_path_strip_trailing_slashes(repo_path);
1079 break;
1080 default:
1081 usage_log();
1082 /* NOTREACHED */
1086 argc -= optind;
1087 argv += optind;
1089 cwd = getcwd(NULL, 0);
1090 if (cwd == NULL) {
1091 error = got_error_from_errno("getcwd");
1092 goto done;
1095 error = got_worktree_open(&worktree, cwd);
1096 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1097 goto done;
1098 error = NULL;
1100 if (argc == 0) {
1101 path = strdup("");
1102 if (path == NULL) {
1103 error = got_error_from_errno("strdup");
1104 goto done;
1106 } else if (argc == 1) {
1107 if (worktree) {
1108 error = got_worktree_resolve_path(&path, worktree,
1109 argv[0]);
1110 if (error)
1111 goto done;
1112 } else {
1113 path = strdup(argv[0]);
1114 if (path == NULL) {
1115 error = got_error_from_errno("strdup");
1116 goto done;
1119 } else
1120 usage_log();
1122 if (repo_path == NULL) {
1123 repo_path = worktree ?
1124 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1126 if (repo_path == NULL) {
1127 error = got_error_from_errno("strdup");
1128 goto done;
1131 error = got_repo_open(&repo, repo_path);
1132 if (error != NULL)
1133 goto done;
1135 error = apply_unveil(got_repo_get_path(repo), 1,
1136 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1137 if (error)
1138 goto done;
1140 if (start_commit == NULL) {
1141 struct got_reference *head_ref;
1142 error = got_ref_open(&head_ref, repo,
1143 worktree ? got_worktree_get_head_ref_name(worktree)
1144 : GOT_REF_HEAD, 0);
1145 if (error != NULL)
1146 return error;
1147 error = got_ref_resolve(&id, repo, head_ref);
1148 got_ref_close(head_ref);
1149 if (error != NULL)
1150 return error;
1151 error = got_object_open_as_commit(&commit, repo, id);
1152 } else {
1153 struct got_reference *ref;
1154 error = got_ref_open(&ref, repo, start_commit, 0);
1155 if (error == NULL) {
1156 int obj_type;
1157 error = got_ref_resolve(&id, repo, ref);
1158 got_ref_close(ref);
1159 if (error != NULL)
1160 goto done;
1161 error = got_object_get_type(&obj_type, repo, id);
1162 if (error != NULL)
1163 goto done;
1164 if (obj_type == GOT_OBJ_TYPE_TAG) {
1165 struct got_tag_object *tag;
1166 error = got_object_open_as_tag(&tag, repo, id);
1167 if (error != NULL)
1168 goto done;
1169 if (got_object_tag_get_object_type(tag) !=
1170 GOT_OBJ_TYPE_COMMIT) {
1171 got_object_tag_close(tag);
1172 error = got_error(GOT_ERR_OBJ_TYPE);
1173 goto done;
1175 free(id);
1176 id = got_object_id_dup(
1177 got_object_tag_get_object_id(tag));
1178 if (id == NULL)
1179 error = got_error_from_errno(
1180 "got_object_id_dup");
1181 got_object_tag_close(tag);
1182 if (error)
1183 goto done;
1184 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1185 error = got_error(GOT_ERR_OBJ_TYPE);
1186 goto done;
1188 error = got_object_open_as_commit(&commit, repo, id);
1189 if (error != NULL)
1190 goto done;
1192 if (commit == NULL) {
1193 error = got_object_resolve_id_str(&id, repo,
1194 start_commit);
1195 if (error != NULL)
1196 return error;
1199 if (error != NULL)
1200 goto done;
1202 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1203 if (error != NULL)
1204 goto done;
1205 if (in_repo_path) {
1206 free(path);
1207 path = in_repo_path;
1210 error = got_ref_list(&refs, repo);
1211 if (error)
1212 goto done;
1214 error = print_commits(id, repo, path, show_patch,
1215 diff_context, limit, first_parent_traversal, &refs);
1216 done:
1217 free(path);
1218 free(repo_path);
1219 free(cwd);
1220 free(id);
1221 if (worktree)
1222 got_worktree_close(worktree);
1223 if (repo) {
1224 const struct got_error *repo_error;
1225 repo_error = got_repo_close(repo);
1226 if (error == NULL)
1227 error = repo_error;
1229 got_ref_list_free(&refs);
1230 return error;
1233 __dead static void
1234 usage_diff(void)
1236 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1237 "[object1 object2 | path]\n", getprogname());
1238 exit(1);
1241 struct print_diff_arg {
1242 struct got_repository *repo;
1243 struct got_worktree *worktree;
1244 int diff_context;
1245 const char *id_str;
1246 int header_shown;
1249 static const struct got_error *
1250 print_diff(void *arg, unsigned char status, const char *path,
1251 struct got_object_id *blob_id, struct got_object_id *commit_id)
1253 struct print_diff_arg *a = arg;
1254 const struct got_error *err = NULL;
1255 struct got_blob_object *blob1 = NULL;
1256 FILE *f2 = NULL;
1257 char *abspath = NULL;
1258 struct stat sb;
1260 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1261 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1262 return NULL;
1264 if (!a->header_shown) {
1265 printf("diff %s %s\n", a->id_str,
1266 got_worktree_get_root_path(a->worktree));
1267 a->header_shown = 1;
1270 if (status != GOT_STATUS_ADD) {
1271 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1272 if (err)
1273 goto done;
1277 if (status != GOT_STATUS_DELETE) {
1278 if (asprintf(&abspath, "%s/%s",
1279 got_worktree_get_root_path(a->worktree), path) == -1) {
1280 err = got_error_from_errno("asprintf");
1281 goto done;
1284 f2 = fopen(abspath, "r");
1285 if (f2 == NULL) {
1286 err = got_error_from_errno2("fopen", abspath);
1287 goto done;
1289 if (lstat(abspath, &sb) == -1) {
1290 err = got_error_from_errno2("lstat", abspath);
1291 goto done;
1293 } else
1294 sb.st_size = 0;
1296 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1297 stdout);
1298 done:
1299 if (blob1)
1300 got_object_blob_close(blob1);
1301 if (f2 && fclose(f2) != 0 && err == NULL)
1302 err = got_error_from_errno("fclose");
1303 free(abspath);
1304 return err;
1307 static const struct got_error *
1308 cmd_diff(int argc, char *argv[])
1310 const struct got_error *error;
1311 struct got_repository *repo = NULL;
1312 struct got_worktree *worktree = NULL;
1313 char *cwd = NULL, *repo_path = NULL;
1314 struct got_object_id *id1 = NULL, *id2 = NULL;
1315 const char *id_str1 = NULL, *id_str2 = NULL;
1316 char *label1 = NULL, *label2 = NULL;
1317 int type1, type2;
1318 int diff_context = 3, ch;
1319 const char *errstr;
1320 char *path = NULL;
1322 #ifndef PROFILE
1323 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1324 NULL) == -1)
1325 err(1, "pledge");
1326 #endif
1328 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1329 switch (ch) {
1330 case 'C':
1331 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1332 if (errstr != NULL)
1333 err(1, "-C option %s", errstr);
1334 break;
1335 case 'r':
1336 repo_path = realpath(optarg, NULL);
1337 if (repo_path == NULL)
1338 err(1, "-r option");
1339 got_path_strip_trailing_slashes(repo_path);
1340 break;
1341 default:
1342 usage_diff();
1343 /* NOTREACHED */
1347 argc -= optind;
1348 argv += optind;
1350 cwd = getcwd(NULL, 0);
1351 if (cwd == NULL) {
1352 error = got_error_from_errno("getcwd");
1353 goto done;
1355 error = got_worktree_open(&worktree, cwd);
1356 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1357 goto done;
1358 if (argc <= 1) {
1359 if (worktree == NULL) {
1360 error = got_error(GOT_ERR_NOT_WORKTREE);
1361 goto done;
1363 if (repo_path)
1364 errx(1,
1365 "-r option can't be used when diffing a work tree");
1366 repo_path = strdup(got_worktree_get_repo_path(worktree));
1367 if (repo_path == NULL) {
1368 error = got_error_from_errno("strdup");
1369 goto done;
1371 if (argc == 1) {
1372 error = got_worktree_resolve_path(&path, worktree,
1373 argv[0]);
1374 if (error)
1375 goto done;
1376 } else {
1377 path = strdup("");
1378 if (path == NULL) {
1379 error = got_error_from_errno("strdup");
1380 goto done;
1383 } else if (argc == 2) {
1384 id_str1 = argv[0];
1385 id_str2 = argv[1];
1386 if (worktree && repo_path == NULL) {
1387 repo_path =
1388 strdup(got_worktree_get_repo_path(worktree));
1389 if (repo_path == NULL) {
1390 error = got_error_from_errno("strdup");
1391 goto done;
1394 } else
1395 usage_diff();
1397 if (repo_path == NULL) {
1398 repo_path = getcwd(NULL, 0);
1399 if (repo_path == NULL)
1400 return got_error_from_errno("getcwd");
1403 error = got_repo_open(&repo, repo_path);
1404 free(repo_path);
1405 if (error != NULL)
1406 goto done;
1408 error = apply_unveil(got_repo_get_path(repo), 1,
1409 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1410 if (error)
1411 goto done;
1413 if (argc <= 1) {
1414 struct print_diff_arg arg;
1415 char *id_str;
1416 error = got_object_id_str(&id_str,
1417 got_worktree_get_base_commit_id(worktree));
1418 if (error)
1419 goto done;
1420 arg.repo = repo;
1421 arg.worktree = worktree;
1422 arg.diff_context = diff_context;
1423 arg.id_str = id_str;
1424 arg.header_shown = 0;
1426 error = got_worktree_status(worktree, path, repo, print_diff,
1427 &arg, check_cancelled, NULL);
1428 free(id_str);
1429 goto done;
1432 error = got_object_resolve_id_str(&id1, repo, id_str1);
1433 if (error) {
1434 struct got_reference *ref;
1435 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1436 goto done;
1437 error = got_ref_open(&ref, repo, id_str1, 0);
1438 if (error != NULL)
1439 goto done;
1440 label1 = strdup(got_ref_get_name(ref));
1441 if (label1 == NULL) {
1442 error = got_error_from_errno("strdup");
1443 goto done;
1445 error = got_ref_resolve(&id1, repo, ref);
1446 got_ref_close(ref);
1447 if (error != NULL)
1448 goto done;
1449 } else {
1450 label1 = strdup(id_str1);
1451 if (label1 == NULL) {
1452 error = got_error_from_errno("strdup");
1453 goto done;
1457 error = got_object_resolve_id_str(&id2, repo, id_str2);
1458 if (error) {
1459 struct got_reference *ref;
1460 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1461 goto done;
1462 error = got_ref_open(&ref, repo, id_str2, 0);
1463 if (error != NULL)
1464 goto done;
1465 label2 = strdup(got_ref_get_name(ref));
1466 if (label2 == NULL) {
1467 error = got_error_from_errno("strdup");
1468 goto done;
1470 error = got_ref_resolve(&id2, repo, ref);
1471 got_ref_close(ref);
1472 if (error != NULL)
1473 goto done;
1474 } else {
1475 label2 = strdup(id_str2);
1476 if (label2 == NULL) {
1477 error = got_error_from_errno("strdup");
1478 goto done;
1482 error = got_object_get_type(&type1, repo, id1);
1483 if (error)
1484 goto done;
1486 error = got_object_get_type(&type2, repo, id2);
1487 if (error)
1488 goto done;
1490 if (type1 != type2) {
1491 error = got_error(GOT_ERR_OBJ_TYPE);
1492 goto done;
1495 switch (type1) {
1496 case GOT_OBJ_TYPE_BLOB:
1497 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1498 diff_context, repo, stdout);
1499 break;
1500 case GOT_OBJ_TYPE_TREE:
1501 error = got_diff_objects_as_trees(id1, id2, "", "",
1502 diff_context, repo, stdout);
1503 break;
1504 case GOT_OBJ_TYPE_COMMIT:
1505 printf("diff %s %s\n", label1, label2);
1506 error = got_diff_objects_as_commits(id1, id2, diff_context,
1507 repo, stdout);
1508 break;
1509 default:
1510 error = got_error(GOT_ERR_OBJ_TYPE);
1513 done:
1514 free(label1);
1515 free(label2);
1516 free(id1);
1517 free(id2);
1518 free(path);
1519 if (worktree)
1520 got_worktree_close(worktree);
1521 if (repo) {
1522 const struct got_error *repo_error;
1523 repo_error = got_repo_close(repo);
1524 if (error == NULL)
1525 error = repo_error;
1527 return error;
1530 __dead static void
1531 usage_blame(void)
1533 fprintf(stderr,
1534 "usage: %s blame [-c commit] [-r repository-path] path\n",
1535 getprogname());
1536 exit(1);
1539 static const struct got_error *
1540 cmd_blame(int argc, char *argv[])
1542 const struct got_error *error;
1543 struct got_repository *repo = NULL;
1544 struct got_worktree *worktree = NULL;
1545 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1546 struct got_object_id *commit_id = NULL;
1547 char *commit_id_str = NULL;
1548 int ch;
1550 #ifndef PROFILE
1551 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1552 NULL) == -1)
1553 err(1, "pledge");
1554 #endif
1556 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1557 switch (ch) {
1558 case 'c':
1559 commit_id_str = optarg;
1560 break;
1561 case 'r':
1562 repo_path = realpath(optarg, NULL);
1563 if (repo_path == NULL)
1564 err(1, "-r option");
1565 got_path_strip_trailing_slashes(repo_path);
1566 break;
1567 default:
1568 usage_blame();
1569 /* NOTREACHED */
1573 argc -= optind;
1574 argv += optind;
1576 if (argc == 1)
1577 path = argv[0];
1578 else
1579 usage_blame();
1581 cwd = getcwd(NULL, 0);
1582 if (cwd == NULL) {
1583 error = got_error_from_errno("getcwd");
1584 goto done;
1586 if (repo_path == NULL) {
1587 error = got_worktree_open(&worktree, cwd);
1588 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1589 goto done;
1590 else
1591 error = NULL;
1592 if (worktree) {
1593 repo_path =
1594 strdup(got_worktree_get_repo_path(worktree));
1595 if (repo_path == NULL)
1596 error = got_error_from_errno("strdup");
1597 if (error)
1598 goto done;
1599 } else {
1600 repo_path = strdup(cwd);
1601 if (repo_path == NULL) {
1602 error = got_error_from_errno("strdup");
1603 goto done;
1608 error = got_repo_open(&repo, repo_path);
1609 if (error != NULL)
1610 goto done;
1612 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1613 if (error)
1614 goto done;
1616 if (worktree) {
1617 const char *prefix = got_worktree_get_path_prefix(worktree);
1618 char *p, *worktree_subdir = cwd +
1619 strlen(got_worktree_get_root_path(worktree));
1620 if (asprintf(&p, "%s%s%s%s%s",
1621 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1622 worktree_subdir, worktree_subdir[0] ? "/" : "",
1623 path) == -1) {
1624 error = got_error_from_errno("asprintf");
1625 goto done;
1627 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1628 free(p);
1629 } else {
1630 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1632 if (error)
1633 goto done;
1635 if (commit_id_str == NULL) {
1636 struct got_reference *head_ref;
1637 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1638 if (error != NULL)
1639 goto done;
1640 error = got_ref_resolve(&commit_id, repo, head_ref);
1641 got_ref_close(head_ref);
1642 if (error != NULL)
1643 goto done;
1644 } else {
1645 error = got_object_resolve_id_str(&commit_id, repo,
1646 commit_id_str);
1647 if (error != NULL)
1648 goto done;
1651 error = got_blame(in_repo_path, commit_id, repo, stdout);
1652 done:
1653 free(in_repo_path);
1654 free(repo_path);
1655 free(cwd);
1656 free(commit_id);
1657 if (worktree)
1658 got_worktree_close(worktree);
1659 if (repo) {
1660 const struct got_error *repo_error;
1661 repo_error = got_repo_close(repo);
1662 if (error == NULL)
1663 error = repo_error;
1665 return error;
1668 __dead static void
1669 usage_tree(void)
1671 fprintf(stderr,
1672 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1673 getprogname());
1674 exit(1);
1677 static void
1678 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1679 const char *root_path)
1681 int is_root_path = (strcmp(path, root_path) == 0);
1683 path += strlen(root_path);
1684 while (path[0] == '/')
1685 path++;
1687 printf("%s%s%s%s%s\n", id ? id : "", path,
1688 is_root_path ? "" : "/", te->name,
1689 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1692 static const struct got_error *
1693 print_tree(const char *path, struct got_object_id *commit_id,
1694 int show_ids, int recurse, const char *root_path,
1695 struct got_repository *repo)
1697 const struct got_error *err = NULL;
1698 struct got_object_id *tree_id = NULL;
1699 struct got_tree_object *tree = NULL;
1700 const struct got_tree_entries *entries;
1701 struct got_tree_entry *te;
1703 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1704 if (err)
1705 goto done;
1707 err = got_object_open_as_tree(&tree, repo, tree_id);
1708 if (err)
1709 goto done;
1710 entries = got_object_tree_get_entries(tree);
1711 te = SIMPLEQ_FIRST(&entries->head);
1712 while (te) {
1713 char *id = NULL;
1715 if (sigint_received || sigpipe_received)
1716 break;
1718 if (show_ids) {
1719 char *id_str;
1720 err = got_object_id_str(&id_str, te->id);
1721 if (err)
1722 goto done;
1723 if (asprintf(&id, "%s ", id_str) == -1) {
1724 err = got_error_from_errno("asprintf");
1725 free(id_str);
1726 goto done;
1728 free(id_str);
1730 print_entry(te, id, path, root_path);
1731 free(id);
1733 if (recurse && S_ISDIR(te->mode)) {
1734 char *child_path;
1735 if (asprintf(&child_path, "%s%s%s", path,
1736 path[0] == '/' && path[1] == '\0' ? "" : "/",
1737 te->name) == -1) {
1738 err = got_error_from_errno("asprintf");
1739 goto done;
1741 err = print_tree(child_path, commit_id, show_ids, 1,
1742 root_path, repo);
1743 free(child_path);
1744 if (err)
1745 goto done;
1748 te = SIMPLEQ_NEXT(te, entry);
1750 done:
1751 if (tree)
1752 got_object_tree_close(tree);
1753 free(tree_id);
1754 return err;
1757 static const struct got_error *
1758 cmd_tree(int argc, char *argv[])
1760 const struct got_error *error;
1761 struct got_repository *repo = NULL;
1762 struct got_worktree *worktree = NULL;
1763 const char *path;
1764 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1765 struct got_object_id *commit_id = NULL;
1766 char *commit_id_str = NULL;
1767 int show_ids = 0, recurse = 0;
1768 int ch;
1770 #ifndef PROFILE
1771 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1772 NULL) == -1)
1773 err(1, "pledge");
1774 #endif
1776 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1777 switch (ch) {
1778 case 'c':
1779 commit_id_str = optarg;
1780 break;
1781 case 'r':
1782 repo_path = realpath(optarg, NULL);
1783 if (repo_path == NULL)
1784 err(1, "-r option");
1785 got_path_strip_trailing_slashes(repo_path);
1786 break;
1787 case 'i':
1788 show_ids = 1;
1789 break;
1790 case 'R':
1791 recurse = 1;
1792 break;
1793 default:
1794 usage_tree();
1795 /* NOTREACHED */
1799 argc -= optind;
1800 argv += optind;
1802 if (argc == 1)
1803 path = argv[0];
1804 else if (argc > 1)
1805 usage_tree();
1806 else
1807 path = NULL;
1809 cwd = getcwd(NULL, 0);
1810 if (cwd == NULL) {
1811 error = got_error_from_errno("getcwd");
1812 goto done;
1814 if (repo_path == NULL) {
1815 error = got_worktree_open(&worktree, cwd);
1816 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1817 goto done;
1818 else
1819 error = NULL;
1820 if (worktree) {
1821 repo_path =
1822 strdup(got_worktree_get_repo_path(worktree));
1823 if (repo_path == NULL)
1824 error = got_error_from_errno("strdup");
1825 if (error)
1826 goto done;
1827 } else {
1828 repo_path = strdup(cwd);
1829 if (repo_path == NULL) {
1830 error = got_error_from_errno("strdup");
1831 goto done;
1836 error = got_repo_open(&repo, repo_path);
1837 if (error != NULL)
1838 goto done;
1840 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1841 if (error)
1842 goto done;
1844 if (path == NULL) {
1845 if (worktree) {
1846 char *p, *worktree_subdir = cwd +
1847 strlen(got_worktree_get_root_path(worktree));
1848 if (asprintf(&p, "%s/%s",
1849 got_worktree_get_path_prefix(worktree),
1850 worktree_subdir) == -1) {
1851 error = got_error_from_errno("asprintf");
1852 goto done;
1854 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1855 free(p);
1856 if (error)
1857 goto done;
1858 } else
1859 path = "/";
1861 if (in_repo_path == NULL) {
1862 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1863 if (error != NULL)
1864 goto done;
1867 if (commit_id_str == NULL) {
1868 struct got_reference *head_ref;
1869 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1870 if (error != NULL)
1871 goto done;
1872 error = got_ref_resolve(&commit_id, repo, head_ref);
1873 got_ref_close(head_ref);
1874 if (error != NULL)
1875 goto done;
1876 } else {
1877 error = got_object_resolve_id_str(&commit_id, repo,
1878 commit_id_str);
1879 if (error != NULL)
1880 goto done;
1883 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1884 in_repo_path, repo);
1885 done:
1886 free(in_repo_path);
1887 free(repo_path);
1888 free(cwd);
1889 free(commit_id);
1890 if (worktree)
1891 got_worktree_close(worktree);
1892 if (repo) {
1893 const struct got_error *repo_error;
1894 repo_error = got_repo_close(repo);
1895 if (error == NULL)
1896 error = repo_error;
1898 return error;
1901 __dead static void
1902 usage_status(void)
1904 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1905 exit(1);
1908 static const struct got_error *
1909 print_status(void *arg, unsigned char status, const char *path,
1910 struct got_object_id *blob_id, struct got_object_id *commit_id)
1912 printf("%c %s\n", status, path);
1913 return NULL;
1916 static const struct got_error *
1917 cmd_status(int argc, char *argv[])
1919 const struct got_error *error = NULL;
1920 struct got_repository *repo = NULL;
1921 struct got_worktree *worktree = NULL;
1922 char *cwd = NULL, *path = NULL;
1923 int ch;
1925 while ((ch = getopt(argc, argv, "")) != -1) {
1926 switch (ch) {
1927 default:
1928 usage_status();
1929 /* NOTREACHED */
1933 argc -= optind;
1934 argv += optind;
1936 #ifndef PROFILE
1937 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1938 NULL) == -1)
1939 err(1, "pledge");
1940 #endif
1941 cwd = getcwd(NULL, 0);
1942 if (cwd == NULL) {
1943 error = got_error_from_errno("getcwd");
1944 goto done;
1947 error = got_worktree_open(&worktree, cwd);
1948 if (error != NULL)
1949 goto done;
1951 if (argc == 0) {
1952 path = strdup("");
1953 if (path == NULL) {
1954 error = got_error_from_errno("strdup");
1955 goto done;
1957 } else if (argc == 1) {
1958 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1959 if (error)
1960 goto done;
1961 } else
1962 usage_status();
1964 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1965 if (error != NULL)
1966 goto done;
1968 error = apply_unveil(got_repo_get_path(repo), 1,
1969 got_worktree_get_root_path(worktree), 0);
1970 if (error)
1971 goto done;
1973 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1974 check_cancelled, NULL);
1975 done:
1976 free(cwd);
1977 free(path);
1978 return error;
1981 __dead static void
1982 usage_ref(void)
1984 fprintf(stderr,
1985 "usage: %s ref [-r repository] -l | -d name | name target\n",
1986 getprogname());
1987 exit(1);
1990 static const struct got_error *
1991 list_refs(struct got_repository *repo)
1993 static const struct got_error *err = NULL;
1994 struct got_reflist_head refs;
1995 struct got_reflist_entry *re;
1997 SIMPLEQ_INIT(&refs);
1998 err = got_ref_list(&refs, repo);
1999 if (err)
2000 return err;
2002 SIMPLEQ_FOREACH(re, &refs, entry) {
2003 char *refstr;
2004 refstr = got_ref_to_str(re->ref);
2005 if (refstr == NULL)
2006 return got_error_from_errno("got_ref_to_str");
2007 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2008 free(refstr);
2011 got_ref_list_free(&refs);
2012 return NULL;
2015 static const struct got_error *
2016 delete_ref(struct got_repository *repo, const char *refname)
2018 const struct got_error *err = NULL;
2019 struct got_reference *ref;
2021 err = got_ref_open(&ref, repo, refname, 0);
2022 if (err)
2023 return err;
2025 err = got_ref_delete(ref, repo);
2026 got_ref_close(ref);
2027 return err;
2030 static const struct got_error *
2031 add_ref(struct got_repository *repo, const char *refname, const char *target)
2033 const struct got_error *err = NULL;
2034 struct got_object_id *id;
2035 struct got_reference *ref = NULL;
2037 err = got_object_resolve_id_str(&id, repo, target);
2038 if (err) {
2039 struct got_reference *target_ref;
2041 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2042 return err;
2043 err = got_ref_open(&target_ref, repo, target, 0);
2044 if (err)
2045 return err;
2046 err = got_ref_resolve(&id, repo, target_ref);
2047 got_ref_close(target_ref);
2048 if (err)
2049 return err;
2052 err = got_ref_alloc(&ref, refname, id);
2053 if (err)
2054 goto done;
2056 err = got_ref_write(ref, repo);
2057 done:
2058 if (ref)
2059 got_ref_close(ref);
2060 free(id);
2061 return err;
2064 static const struct got_error *
2065 cmd_ref(int argc, char *argv[])
2067 const struct got_error *error = NULL;
2068 struct got_repository *repo = NULL;
2069 struct got_worktree *worktree = NULL;
2070 char *cwd = NULL, *repo_path = NULL;
2071 int ch, do_list = 0;
2072 const char *delref = NULL;
2074 /* TODO: Add -s option for adding symbolic references. */
2075 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2076 switch (ch) {
2077 case 'd':
2078 delref = optarg;
2079 break;
2080 case 'r':
2081 repo_path = realpath(optarg, NULL);
2082 if (repo_path == NULL)
2083 err(1, "-r option");
2084 got_path_strip_trailing_slashes(repo_path);
2085 break;
2086 case 'l':
2087 do_list = 1;
2088 break;
2089 default:
2090 usage_ref();
2091 /* NOTREACHED */
2095 if (do_list && delref)
2096 errx(1, "-l and -d options are mutually exclusive\n");
2098 argc -= optind;
2099 argv += optind;
2101 if (do_list || delref) {
2102 if (argc > 0)
2103 usage_ref();
2104 } else if (argc != 2)
2105 usage_ref();
2107 #ifndef PROFILE
2108 if (do_list) {
2109 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2110 NULL) == -1)
2111 err(1, "pledge");
2112 } else {
2113 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2114 "sendfd unveil", NULL) == -1)
2115 err(1, "pledge");
2117 #endif
2118 cwd = getcwd(NULL, 0);
2119 if (cwd == NULL) {
2120 error = got_error_from_errno("getcwd");
2121 goto done;
2124 if (repo_path == NULL) {
2125 error = got_worktree_open(&worktree, cwd);
2126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2127 goto done;
2128 else
2129 error = NULL;
2130 if (worktree) {
2131 repo_path =
2132 strdup(got_worktree_get_repo_path(worktree));
2133 if (repo_path == NULL)
2134 error = got_error_from_errno("strdup");
2135 if (error)
2136 goto done;
2137 } else {
2138 repo_path = strdup(cwd);
2139 if (repo_path == NULL) {
2140 error = got_error_from_errno("strdup");
2141 goto done;
2146 error = got_repo_open(&repo, repo_path);
2147 if (error != NULL)
2148 goto done;
2150 error = apply_unveil(got_repo_get_path(repo), do_list,
2151 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2152 if (error)
2153 goto done;
2155 if (do_list)
2156 error = list_refs(repo);
2157 else if (delref)
2158 error = delete_ref(repo, delref);
2159 else
2160 error = add_ref(repo, argv[0], argv[1]);
2161 done:
2162 if (repo)
2163 got_repo_close(repo);
2164 if (worktree)
2165 got_worktree_close(worktree);
2166 free(cwd);
2167 free(repo_path);
2168 return error;
2171 __dead static void
2172 usage_branch(void)
2174 fprintf(stderr,
2175 "usage: %s branch [-r repository] -l | -d name | "
2176 "name [base-branch]\n", getprogname());
2177 exit(1);
2180 static const struct got_error *
2181 list_branches(struct got_repository *repo)
2183 static const struct got_error *err = NULL;
2184 struct got_reflist_head refs;
2185 struct got_reflist_entry *re;
2187 SIMPLEQ_INIT(&refs);
2188 err = got_ref_list(&refs, repo);
2189 if (err)
2190 return err;
2192 SIMPLEQ_FOREACH(re, &refs, entry) {
2193 const char *refname;
2194 char *refstr;
2195 refname = got_ref_get_name(re->ref);
2196 if (strncmp(refname, "refs/heads/", 11) != 0)
2197 continue;
2198 refname += 11;
2199 refstr = got_ref_to_str(re->ref);
2200 if (refstr == NULL)
2201 return got_error_from_errno("got_ref_to_str");
2202 printf("%s: %s\n", refname, refstr);
2203 free(refstr);
2206 got_ref_list_free(&refs);
2207 return NULL;
2210 static const struct got_error *
2211 delete_branch(struct got_repository *repo, const char *branch_name)
2213 const struct got_error *err = NULL;
2214 struct got_reference *ref;
2215 char *refname;
2217 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2218 return got_error_from_errno("asprintf");
2220 err = got_ref_open(&ref, repo, refname, 0);
2221 if (err)
2222 goto done;
2224 err = got_ref_delete(ref, repo);
2225 got_ref_close(ref);
2226 done:
2227 free(refname);
2228 return err;
2231 static const struct got_error *
2232 add_branch(struct got_repository *repo, const char *branch_name,
2233 const char *base_branch)
2235 const struct got_error *err = NULL;
2236 struct got_object_id *id = NULL;
2237 struct got_reference *ref = NULL;
2238 char *base_refname = NULL, *refname = NULL;
2239 struct got_reference *base_ref;
2241 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2242 base_refname = strdup(GOT_REF_HEAD);
2243 if (base_refname == NULL)
2244 return got_error_from_errno("strdup");
2245 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2246 return got_error_from_errno("asprintf");
2248 err = got_ref_open(&base_ref, repo, base_refname, 0);
2249 if (err)
2250 goto done;
2251 err = got_ref_resolve(&id, repo, base_ref);
2252 got_ref_close(base_ref);
2253 if (err)
2254 goto done;
2256 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2257 err = got_error_from_errno("asprintf");
2258 goto done;
2261 err = got_ref_open(&ref, repo, refname, 0);
2262 if (err == NULL) {
2263 err = got_error(GOT_ERR_BRANCH_EXISTS);
2264 goto done;
2265 } else if (err->code != GOT_ERR_NOT_REF)
2266 goto done;
2268 err = got_ref_alloc(&ref, refname, id);
2269 if (err)
2270 goto done;
2272 err = got_ref_write(ref, repo);
2273 done:
2274 if (ref)
2275 got_ref_close(ref);
2276 free(id);
2277 free(base_refname);
2278 free(refname);
2279 return err;
2282 static const struct got_error *
2283 cmd_branch(int argc, char *argv[])
2285 const struct got_error *error = NULL;
2286 struct got_repository *repo = NULL;
2287 struct got_worktree *worktree = NULL;
2288 char *cwd = NULL, *repo_path = NULL;
2289 int ch, do_list = 0;
2290 const char *delref = NULL;
2292 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2293 switch (ch) {
2294 case 'd':
2295 delref = optarg;
2296 break;
2297 case 'r':
2298 repo_path = realpath(optarg, NULL);
2299 if (repo_path == NULL)
2300 err(1, "-r option");
2301 got_path_strip_trailing_slashes(repo_path);
2302 break;
2303 case 'l':
2304 do_list = 1;
2305 break;
2306 default:
2307 usage_branch();
2308 /* NOTREACHED */
2312 if (do_list && delref)
2313 errx(1, "-l and -d options are mutually exclusive\n");
2315 argc -= optind;
2316 argv += optind;
2318 if (do_list || delref) {
2319 if (argc > 0)
2320 usage_branch();
2321 } else if (argc < 1 || argc > 2)
2322 usage_branch();
2324 #ifndef PROFILE
2325 if (do_list) {
2326 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2327 NULL) == -1)
2328 err(1, "pledge");
2329 } else {
2330 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2331 "sendfd unveil", NULL) == -1)
2332 err(1, "pledge");
2334 #endif
2335 cwd = getcwd(NULL, 0);
2336 if (cwd == NULL) {
2337 error = got_error_from_errno("getcwd");
2338 goto done;
2341 if (repo_path == NULL) {
2342 error = got_worktree_open(&worktree, cwd);
2343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2344 goto done;
2345 else
2346 error = NULL;
2347 if (worktree) {
2348 repo_path =
2349 strdup(got_worktree_get_repo_path(worktree));
2350 if (repo_path == NULL)
2351 error = got_error_from_errno("strdup");
2352 if (error)
2353 goto done;
2354 } else {
2355 repo_path = strdup(cwd);
2356 if (repo_path == NULL) {
2357 error = got_error_from_errno("strdup");
2358 goto done;
2363 error = got_repo_open(&repo, repo_path);
2364 if (error != NULL)
2365 goto done;
2367 error = apply_unveil(got_repo_get_path(repo), do_list,
2368 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2369 if (error)
2370 goto done;
2372 if (do_list)
2373 error = list_branches(repo);
2374 else if (delref)
2375 error = delete_branch(repo, delref);
2376 else {
2377 const char *base_branch;
2378 if (argc == 1) {
2379 base_branch = worktree ?
2380 got_worktree_get_head_ref_name(worktree) :
2381 GOT_REF_HEAD;
2382 } else
2383 base_branch = argv[1];
2384 error = add_branch(repo, argv[0], base_branch);
2386 done:
2387 if (repo)
2388 got_repo_close(repo);
2389 if (worktree)
2390 got_worktree_close(worktree);
2391 free(cwd);
2392 free(repo_path);
2393 return error;
2396 __dead static void
2397 usage_add(void)
2399 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2400 exit(1);
2403 static const struct got_error *
2404 cmd_add(int argc, char *argv[])
2406 const struct got_error *error = NULL;
2407 struct got_repository *repo = NULL;
2408 struct got_worktree *worktree = NULL;
2409 char *cwd = NULL;
2410 struct got_pathlist_head paths;
2411 struct got_pathlist_entry *pe;
2412 int ch, x;
2414 TAILQ_INIT(&paths);
2416 while ((ch = getopt(argc, argv, "")) != -1) {
2417 switch (ch) {
2418 default:
2419 usage_add();
2420 /* NOTREACHED */
2424 argc -= optind;
2425 argv += optind;
2427 if (argc < 1)
2428 usage_add();
2430 /* make sure each file exists before doing anything halfway */
2431 for (x = 0; x < argc; x++) {
2432 char *path = realpath(argv[x], NULL);
2433 if (path == NULL) {
2434 error = got_error_from_errno2("realpath", argv[x]);
2435 goto done;
2437 free(path);
2440 cwd = getcwd(NULL, 0);
2441 if (cwd == NULL) {
2442 error = got_error_from_errno("getcwd");
2443 goto done;
2446 error = got_worktree_open(&worktree, cwd);
2447 if (error)
2448 goto done;
2450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2451 if (error != NULL)
2452 goto done;
2454 error = apply_unveil(got_repo_get_path(repo), 1,
2455 got_worktree_get_root_path(worktree), 0);
2456 if (error)
2457 goto done;
2459 for (x = 0; x < argc; x++) {
2460 char *path = realpath(argv[x], NULL);
2461 if (path == NULL) {
2462 error = got_error_from_errno2("realpath", argv[x]);
2463 goto done;
2466 got_path_strip_trailing_slashes(path);
2467 error = got_pathlist_insert(&pe, &paths, path, NULL);
2468 if (error) {
2469 free(path);
2470 goto done;
2473 error = got_worktree_schedule_add(worktree, &paths, print_status,
2474 NULL, repo);
2475 done:
2476 if (repo)
2477 got_repo_close(repo);
2478 if (worktree)
2479 got_worktree_close(worktree);
2480 TAILQ_FOREACH(pe, &paths, entry)
2481 free((char *)pe->path);
2482 got_pathlist_free(&paths);
2483 free(cwd);
2484 return error;
2487 __dead static void
2488 usage_rm(void)
2490 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2491 exit(1);
2494 static const struct got_error *
2495 cmd_rm(int argc, char *argv[])
2497 const struct got_error *error = NULL;
2498 struct got_worktree *worktree = NULL;
2499 struct got_repository *repo = NULL;
2500 char *cwd = NULL;
2501 struct got_pathlist_head paths;
2502 struct got_pathlist_entry *pe;
2503 int ch, i, delete_local_mods = 0;
2505 TAILQ_INIT(&paths);
2507 while ((ch = getopt(argc, argv, "f")) != -1) {
2508 switch (ch) {
2509 case 'f':
2510 delete_local_mods = 1;
2511 break;
2512 default:
2513 usage_add();
2514 /* NOTREACHED */
2518 argc -= optind;
2519 argv += optind;
2521 if (argc < 1)
2522 usage_rm();
2524 /* make sure each file exists before doing anything halfway */
2525 for (i = 0; i < argc; i++) {
2526 char *path = realpath(argv[i], NULL);
2527 if (path == NULL) {
2528 error = got_error_from_errno2("realpath", argv[i]);
2529 goto done;
2531 free(path);
2534 cwd = getcwd(NULL, 0);
2535 if (cwd == NULL) {
2536 error = got_error_from_errno("getcwd");
2537 goto done;
2539 error = got_worktree_open(&worktree, cwd);
2540 if (error)
2541 goto done;
2543 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2544 if (error)
2545 goto done;
2547 error = apply_unveil(got_repo_get_path(repo), 1,
2548 got_worktree_get_root_path(worktree), 0);
2549 if (error)
2550 goto done;
2552 for (i = 0; i < argc; i++) {
2553 char *path = realpath(argv[i], NULL);
2554 if (path == NULL) {
2555 error = got_error_from_errno2("realpath", argv[i]);
2556 goto done;
2559 got_path_strip_trailing_slashes(path);
2560 error = got_pathlist_insert(&pe, &paths, path, NULL);
2561 if (error) {
2562 free(path);
2563 goto done;
2566 error = got_worktree_schedule_delete(worktree, &paths,
2567 delete_local_mods, print_status, NULL, repo);
2568 if (error)
2569 goto done;
2570 done:
2571 if (repo)
2572 got_repo_close(repo);
2573 if (worktree)
2574 got_worktree_close(worktree);
2575 TAILQ_FOREACH(pe, &paths, entry)
2576 free((char *)pe->path);
2577 got_pathlist_free(&paths);
2578 free(cwd);
2579 return error;
2582 __dead static void
2583 usage_revert(void)
2585 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2586 exit(1);
2589 static void
2590 revert_progress(void *arg, unsigned char status, const char *path)
2592 while (path[0] == '/')
2593 path++;
2594 printf("%c %s\n", status, path);
2597 static const struct got_error *
2598 cmd_revert(int argc, char *argv[])
2600 const struct got_error *error = NULL;
2601 struct got_worktree *worktree = NULL;
2602 struct got_repository *repo = NULL;
2603 char *cwd = NULL, *path = NULL;
2604 struct got_pathlist_head paths;
2605 struct got_pathlist_entry *pe;
2606 int ch, i;
2608 TAILQ_INIT(&paths);
2610 while ((ch = getopt(argc, argv, "")) != -1) {
2611 switch (ch) {
2612 default:
2613 usage_revert();
2614 /* NOTREACHED */
2618 argc -= optind;
2619 argv += optind;
2621 if (argc < 1)
2622 usage_revert();
2624 for (i = 0; i < argc; i++) {
2625 char *path = realpath(argv[i], NULL);
2626 if (path == NULL) {
2627 error = got_error_from_errno2("realpath", argv[i]);
2628 goto done;
2631 got_path_strip_trailing_slashes(path);
2632 error = got_pathlist_insert(&pe, &paths, path, NULL);
2633 if (error) {
2634 free(path);
2635 goto done;
2639 cwd = getcwd(NULL, 0);
2640 if (cwd == NULL) {
2641 error = got_error_from_errno("getcwd");
2642 goto done;
2644 error = got_worktree_open(&worktree, cwd);
2645 if (error)
2646 goto done;
2648 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2649 if (error != NULL)
2650 goto done;
2652 error = apply_unveil(got_repo_get_path(repo), 1,
2653 got_worktree_get_root_path(worktree), 0);
2654 if (error)
2655 goto done;
2657 error = got_worktree_revert(worktree, &paths,
2658 revert_progress, NULL, repo);
2659 if (error)
2660 goto done;
2661 done:
2662 if (repo)
2663 got_repo_close(repo);
2664 if (worktree)
2665 got_worktree_close(worktree);
2666 free(path);
2667 free(cwd);
2668 return error;
2671 __dead static void
2672 usage_commit(void)
2674 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2675 exit(1);
2678 int
2679 spawn_editor(const char *editor, const char *file)
2681 pid_t pid;
2682 sig_t sighup, sigint, sigquit;
2683 int st = -1;
2685 sighup = signal(SIGHUP, SIG_IGN);
2686 sigint = signal(SIGINT, SIG_IGN);
2687 sigquit = signal(SIGQUIT, SIG_IGN);
2689 switch (pid = fork()) {
2690 case -1:
2691 goto doneediting;
2692 case 0:
2693 execl(editor, editor, file, (char *)NULL);
2694 _exit(127);
2697 while (waitpid(pid, &st, 0) == -1)
2698 if (errno != EINTR)
2699 break;
2701 doneediting:
2702 (void)signal(SIGHUP, sighup);
2703 (void)signal(SIGINT, sigint);
2704 (void)signal(SIGQUIT, sigquit);
2706 if (!WIFEXITED(st)) {
2707 errno = EINTR;
2708 return -1;
2711 return WEXITSTATUS(st);
2714 struct collect_commit_logmsg_arg {
2715 const char *cmdline_log;
2716 const char *editor;
2717 const char *worktree_path;
2718 const char *branch_name;
2719 const char *repo_path;
2720 char *logmsg_path;
2724 static const struct got_error *
2725 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2726 void *arg)
2728 char *initial_content = NULL;
2729 struct got_pathlist_entry *pe;
2730 const struct got_error *err = NULL;
2731 char *template = NULL;
2732 struct collect_commit_logmsg_arg *a = arg;
2733 char buf[1024];
2734 struct stat st, st2;
2735 FILE *fp;
2736 size_t len;
2737 int fd, content_changed = 0;
2739 /* if a message was specified on the command line, just use it */
2740 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2741 len = strlen(a->cmdline_log) + 1;
2742 *logmsg = malloc(len + 1);
2743 if (*logmsg == NULL)
2744 return got_error_from_errno("malloc");
2745 strlcpy(*logmsg, a->cmdline_log, len);
2746 return NULL;
2749 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2750 return got_error_from_errno("asprintf");
2752 if (asprintf(&initial_content,
2753 "\n# changes to be committed on branch %s:\n",
2754 a->branch_name) == -1)
2755 return got_error_from_errno("asprintf");
2757 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2758 if (err)
2759 goto done;
2761 dprintf(fd, initial_content);
2763 TAILQ_FOREACH(pe, commitable_paths, entry) {
2764 struct got_commitable *ct = pe->data;
2765 dprintf(fd, "# %c %s\n",
2766 got_commitable_get_status(ct),
2767 got_commitable_get_path(ct));
2769 close(fd);
2771 if (stat(a->logmsg_path, &st) == -1) {
2772 err = got_error_from_errno2("stat", a->logmsg_path);
2773 goto done;
2776 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2777 err = got_error_from_errno("failed spawning editor");
2778 goto done;
2781 if (stat(a->logmsg_path, &st2) == -1) {
2782 err = got_error_from_errno("stat");
2783 goto done;
2786 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2787 unlink(a->logmsg_path);
2788 free(a->logmsg_path);
2789 a->logmsg_path = NULL;
2790 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2791 "no changes made to commit message, aborting");
2792 goto done;
2795 *logmsg = malloc(st2.st_size + 1);
2796 if (*logmsg == NULL) {
2797 err = got_error_from_errno("malloc");
2798 goto done;
2800 (*logmsg)[0] = '\0';
2801 len = 0;
2803 fp = fopen(a->logmsg_path, "r");
2804 if (fp == NULL) {
2805 err = got_error_from_errno("fopen");
2806 goto done;
2808 while (fgets(buf, sizeof(buf), fp) != NULL) {
2809 if (!content_changed && strcmp(buf, initial_content) != 0)
2810 content_changed = 1;
2811 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2812 continue; /* remove comments and leading empty lines */
2813 len = strlcat(*logmsg, buf, st2.st_size);
2815 fclose(fp);
2817 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2818 (*logmsg)[len - 1] = '\0';
2819 len--;
2822 if (len == 0 || !content_changed) {
2823 unlink(a->logmsg_path);
2824 free(a->logmsg_path);
2825 a->logmsg_path = NULL;
2826 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2827 "commit message cannot be empty, aborting");
2828 goto done;
2830 done:
2831 free(initial_content);
2832 free(template);
2834 /* Editor is done; we can now apply unveil(2) */
2835 if (err == NULL)
2836 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2837 return err;
2840 static const struct got_error *
2841 cmd_commit(int argc, char *argv[])
2843 const struct got_error *error = NULL;
2844 struct got_worktree *worktree = NULL;
2845 struct got_repository *repo = NULL;
2846 char *cwd = NULL, *path = NULL, *id_str = NULL;
2847 struct got_object_id *id = NULL;
2848 const char *logmsg = NULL;
2849 const char *got_author = getenv("GOT_AUTHOR");
2850 struct collect_commit_logmsg_arg cl_arg;
2851 char *editor = NULL;
2852 int ch;
2854 while ((ch = getopt(argc, argv, "m:")) != -1) {
2855 switch (ch) {
2856 case 'm':
2857 logmsg = optarg;
2858 break;
2859 default:
2860 usage_commit();
2861 /* NOTREACHED */
2865 argc -= optind;
2866 argv += optind;
2868 if (argc == 1) {
2869 path = realpath(argv[0], NULL);
2870 if (path == NULL) {
2871 error = got_error_from_errno2("realpath", argv[0]);
2872 goto done;
2874 got_path_strip_trailing_slashes(path);
2875 } else if (argc != 0)
2876 usage_commit();
2878 if (got_author == NULL) {
2879 /* TODO: Look current user up in password database */
2880 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2881 goto done;
2884 cwd = getcwd(NULL, 0);
2885 if (cwd == NULL) {
2886 error = got_error_from_errno("getcwd");
2887 goto done;
2889 error = got_worktree_open(&worktree, cwd);
2890 if (error)
2891 goto done;
2893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2894 if (error != NULL)
2895 goto done;
2898 * unveil(2) traverses exec(2); if an editor is used we have
2899 * to apply unveil after the log message has been written.
2901 if (logmsg == NULL || strlen(logmsg) == 0)
2902 error = get_editor(&editor);
2903 else
2904 error = apply_unveil(got_repo_get_path(repo), 0,
2905 got_worktree_get_root_path(worktree), 0);
2906 if (error)
2907 goto done;
2909 cl_arg.editor = editor;
2910 cl_arg.cmdline_log = logmsg;
2911 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2912 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2913 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2914 cl_arg.branch_name += 5;
2915 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2916 cl_arg.branch_name += 6;
2917 cl_arg.repo_path = got_repo_get_path(repo);
2918 cl_arg.logmsg_path = NULL;
2919 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2920 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2921 if (error) {
2922 if (cl_arg.logmsg_path)
2923 fprintf(stderr, "%s: log message preserved in %s\n",
2924 getprogname(), cl_arg.logmsg_path);
2925 goto done;
2928 if (cl_arg.logmsg_path)
2929 unlink(cl_arg.logmsg_path);
2931 error = got_object_id_str(&id_str, id);
2932 if (error)
2933 goto done;
2934 printf("Created commit %s\n", id_str);
2935 done:
2936 if (repo)
2937 got_repo_close(repo);
2938 if (worktree)
2939 got_worktree_close(worktree);
2940 free(path);
2941 free(cwd);
2942 free(id_str);
2943 free(editor);
2944 return error;
2947 __dead static void
2948 usage_cherrypick(void)
2950 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2951 exit(1);
2954 static const struct got_error *
2955 cmd_cherrypick(int argc, char *argv[])
2957 const struct got_error *error = NULL;
2958 struct got_worktree *worktree = NULL;
2959 struct got_repository *repo = NULL;
2960 char *cwd = NULL, *commit_id_str = NULL;
2961 struct got_object_id *commit_id = NULL;
2962 struct got_commit_object *commit = NULL;
2963 struct got_object_qid *pid;
2964 struct got_reference *head_ref = NULL;
2965 int ch, did_something = 0;
2967 while ((ch = getopt(argc, argv, "")) != -1) {
2968 switch (ch) {
2969 default:
2970 usage_cherrypick();
2971 /* NOTREACHED */
2975 argc -= optind;
2976 argv += optind;
2978 if (argc != 1)
2979 usage_cherrypick();
2981 cwd = getcwd(NULL, 0);
2982 if (cwd == NULL) {
2983 error = got_error_from_errno("getcwd");
2984 goto done;
2986 error = got_worktree_open(&worktree, cwd);
2987 if (error)
2988 goto done;
2990 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2991 if (error != NULL)
2992 goto done;
2994 error = apply_unveil(got_repo_get_path(repo), 0,
2995 got_worktree_get_root_path(worktree), 0);
2996 if (error)
2997 goto done;
2999 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
3000 if (error != NULL) {
3001 struct got_reference *ref;
3002 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3003 goto done;
3004 error = got_ref_open(&ref, repo, argv[0], 0);
3005 if (error != NULL)
3006 goto done;
3007 error = got_ref_resolve(&commit_id, repo, ref);
3008 got_ref_close(ref);
3009 if (error != NULL)
3010 goto done;
3012 error = got_object_id_str(&commit_id_str, commit_id);
3013 if (error)
3014 goto done;
3016 error = got_ref_open(&head_ref, repo,
3017 got_worktree_get_head_ref_name(worktree), 0);
3018 if (error != NULL)
3019 goto done;
3021 error = check_same_branch(commit_id, head_ref, repo);
3022 if (error) {
3023 if (error->code != GOT_ERR_ANCESTRY)
3024 goto done;
3025 error = NULL;
3026 } else {
3027 error = got_error(GOT_ERR_SAME_BRANCH);
3028 goto done;
3031 error = got_object_open_as_commit(&commit, repo, commit_id);
3032 if (error)
3033 goto done;
3034 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3035 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3036 commit_id, repo, update_progress, &did_something, check_cancelled,
3037 NULL);
3038 if (error != NULL)
3039 goto done;
3041 if (did_something)
3042 printf("Merged commit %s\n", commit_id_str);
3043 done:
3044 if (commit)
3045 got_object_commit_close(commit);
3046 free(commit_id_str);
3047 if (head_ref)
3048 got_ref_close(head_ref);
3049 if (worktree)
3050 got_worktree_close(worktree);
3051 if (repo)
3052 got_repo_close(repo);
3053 return error;
3056 __dead static void
3057 usage_backout(void)
3059 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3060 exit(1);
3063 static const struct got_error *
3064 cmd_backout(int argc, char *argv[])
3066 const struct got_error *error = NULL;
3067 struct got_worktree *worktree = NULL;
3068 struct got_repository *repo = NULL;
3069 char *cwd = NULL, *commit_id_str = NULL;
3070 struct got_object_id *commit_id = NULL;
3071 struct got_commit_object *commit = NULL;
3072 struct got_object_qid *pid;
3073 struct got_reference *head_ref = NULL;
3074 int ch, did_something = 0;
3076 while ((ch = getopt(argc, argv, "")) != -1) {
3077 switch (ch) {
3078 default:
3079 usage_backout();
3080 /* NOTREACHED */
3084 argc -= optind;
3085 argv += optind;
3087 if (argc != 1)
3088 usage_backout();
3090 cwd = getcwd(NULL, 0);
3091 if (cwd == NULL) {
3092 error = got_error_from_errno("getcwd");
3093 goto done;
3095 error = got_worktree_open(&worktree, cwd);
3096 if (error)
3097 goto done;
3099 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3100 if (error != NULL)
3101 goto done;
3103 error = apply_unveil(got_repo_get_path(repo), 0,
3104 got_worktree_get_root_path(worktree), 0);
3105 if (error)
3106 goto done;
3108 error = got_object_resolve_id_str(&commit_id, repo, argv[0]);
3109 if (error != NULL) {
3110 struct got_reference *ref;
3111 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3112 goto done;
3113 error = got_ref_open(&ref, repo, argv[0], 0);
3114 if (error != NULL)
3115 goto done;
3116 error = got_ref_resolve(&commit_id, repo, ref);
3117 got_ref_close(ref);
3118 if (error != NULL)
3119 goto done;
3121 error = got_object_id_str(&commit_id_str, commit_id);
3122 if (error)
3123 goto done;
3125 error = got_ref_open(&head_ref, repo,
3126 got_worktree_get_head_ref_name(worktree), 0);
3127 if (error != NULL)
3128 goto done;
3130 error = check_same_branch(commit_id, head_ref, repo);
3131 if (error)
3132 goto done;
3134 error = got_object_open_as_commit(&commit, repo, commit_id);
3135 if (error)
3136 goto done;
3137 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3138 if (pid == NULL) {
3139 error = got_error(GOT_ERR_ROOT_COMMIT);
3140 goto done;
3143 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3144 update_progress, &did_something, check_cancelled, NULL);
3145 if (error != NULL)
3146 goto done;
3148 if (did_something)
3149 printf("Backed out commit %s\n", commit_id_str);
3150 done:
3151 if (commit)
3152 got_object_commit_close(commit);
3153 free(commit_id_str);
3154 if (head_ref)
3155 got_ref_close(head_ref);
3156 if (worktree)
3157 got_worktree_close(worktree);
3158 if (repo)
3159 got_repo_close(repo);
3160 return error;