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_repo_match_object_id_prefix(&commit_id,
564 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
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_repo_match_object_id_prefix(&commit_id,
745 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
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_repo_match_object_id_prefix(&id,
1194 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
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_repo_match_object_id_prefix(&id1, id_str1,
1433 GOT_OBJ_TYPE_ANY, repo);
1434 if (error) {
1435 struct got_reference *ref;
1436 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1437 goto done;
1438 error = got_ref_open(&ref, repo, id_str1, 0);
1439 if (error != NULL)
1440 goto done;
1441 label1 = strdup(got_ref_get_name(ref));
1442 if (label1 == NULL) {
1443 error = got_error_from_errno("strdup");
1444 goto done;
1446 error = got_ref_resolve(&id1, repo, ref);
1447 got_ref_close(ref);
1448 if (error != NULL)
1449 goto done;
1450 } else {
1451 label1 = strdup(id_str1);
1452 if (label1 == NULL) {
1453 error = got_error_from_errno("strdup");
1454 goto done;
1458 error = got_repo_match_object_id_prefix(&id2, id_str2,
1459 GOT_OBJ_TYPE_ANY, repo);
1460 if (error) {
1461 struct got_reference *ref;
1462 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1463 goto done;
1464 error = got_ref_open(&ref, repo, id_str2, 0);
1465 if (error != NULL)
1466 goto done;
1467 label2 = strdup(got_ref_get_name(ref));
1468 if (label2 == NULL) {
1469 error = got_error_from_errno("strdup");
1470 goto done;
1472 error = got_ref_resolve(&id2, repo, ref);
1473 got_ref_close(ref);
1474 if (error != NULL)
1475 goto done;
1476 } else {
1477 label2 = strdup(id_str2);
1478 if (label2 == NULL) {
1479 error = got_error_from_errno("strdup");
1480 goto done;
1484 error = got_object_get_type(&type1, repo, id1);
1485 if (error)
1486 goto done;
1488 error = got_object_get_type(&type2, repo, id2);
1489 if (error)
1490 goto done;
1492 if (type1 != type2) {
1493 error = got_error(GOT_ERR_OBJ_TYPE);
1494 goto done;
1497 switch (type1) {
1498 case GOT_OBJ_TYPE_BLOB:
1499 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1500 diff_context, repo, stdout);
1501 break;
1502 case GOT_OBJ_TYPE_TREE:
1503 error = got_diff_objects_as_trees(id1, id2, "", "",
1504 diff_context, repo, stdout);
1505 break;
1506 case GOT_OBJ_TYPE_COMMIT:
1507 printf("diff %s %s\n", label1, label2);
1508 error = got_diff_objects_as_commits(id1, id2, diff_context,
1509 repo, stdout);
1510 break;
1511 default:
1512 error = got_error(GOT_ERR_OBJ_TYPE);
1515 done:
1516 free(label1);
1517 free(label2);
1518 free(id1);
1519 free(id2);
1520 free(path);
1521 if (worktree)
1522 got_worktree_close(worktree);
1523 if (repo) {
1524 const struct got_error *repo_error;
1525 repo_error = got_repo_close(repo);
1526 if (error == NULL)
1527 error = repo_error;
1529 return error;
1532 __dead static void
1533 usage_blame(void)
1535 fprintf(stderr,
1536 "usage: %s blame [-c commit] [-r repository-path] path\n",
1537 getprogname());
1538 exit(1);
1541 static const struct got_error *
1542 cmd_blame(int argc, char *argv[])
1544 const struct got_error *error;
1545 struct got_repository *repo = NULL;
1546 struct got_worktree *worktree = NULL;
1547 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1548 struct got_object_id *commit_id = NULL;
1549 char *commit_id_str = NULL;
1550 int ch;
1552 #ifndef PROFILE
1553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1554 NULL) == -1)
1555 err(1, "pledge");
1556 #endif
1558 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1559 switch (ch) {
1560 case 'c':
1561 commit_id_str = optarg;
1562 break;
1563 case 'r':
1564 repo_path = realpath(optarg, NULL);
1565 if (repo_path == NULL)
1566 err(1, "-r option");
1567 got_path_strip_trailing_slashes(repo_path);
1568 break;
1569 default:
1570 usage_blame();
1571 /* NOTREACHED */
1575 argc -= optind;
1576 argv += optind;
1578 if (argc == 1)
1579 path = argv[0];
1580 else
1581 usage_blame();
1583 cwd = getcwd(NULL, 0);
1584 if (cwd == NULL) {
1585 error = got_error_from_errno("getcwd");
1586 goto done;
1588 if (repo_path == NULL) {
1589 error = got_worktree_open(&worktree, cwd);
1590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1591 goto done;
1592 else
1593 error = NULL;
1594 if (worktree) {
1595 repo_path =
1596 strdup(got_worktree_get_repo_path(worktree));
1597 if (repo_path == NULL)
1598 error = got_error_from_errno("strdup");
1599 if (error)
1600 goto done;
1601 } else {
1602 repo_path = strdup(cwd);
1603 if (repo_path == NULL) {
1604 error = got_error_from_errno("strdup");
1605 goto done;
1610 error = got_repo_open(&repo, repo_path);
1611 if (error != NULL)
1612 goto done;
1614 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1615 if (error)
1616 goto done;
1618 if (worktree) {
1619 const char *prefix = got_worktree_get_path_prefix(worktree);
1620 char *p, *worktree_subdir = cwd +
1621 strlen(got_worktree_get_root_path(worktree));
1622 if (asprintf(&p, "%s%s%s%s%s",
1623 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1624 worktree_subdir, worktree_subdir[0] ? "/" : "",
1625 path) == -1) {
1626 error = got_error_from_errno("asprintf");
1627 goto done;
1629 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1630 free(p);
1631 } else {
1632 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1634 if (error)
1635 goto done;
1637 if (commit_id_str == NULL) {
1638 struct got_reference *head_ref;
1639 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1640 if (error != NULL)
1641 goto done;
1642 error = got_ref_resolve(&commit_id, repo, head_ref);
1643 got_ref_close(head_ref);
1644 if (error != NULL)
1645 goto done;
1646 } else {
1647 error = got_repo_match_object_id_prefix(&commit_id,
1648 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1649 if (error != NULL)
1650 goto done;
1653 error = got_blame(in_repo_path, commit_id, repo, stdout);
1654 done:
1655 free(in_repo_path);
1656 free(repo_path);
1657 free(cwd);
1658 free(commit_id);
1659 if (worktree)
1660 got_worktree_close(worktree);
1661 if (repo) {
1662 const struct got_error *repo_error;
1663 repo_error = got_repo_close(repo);
1664 if (error == NULL)
1665 error = repo_error;
1667 return error;
1670 __dead static void
1671 usage_tree(void)
1673 fprintf(stderr,
1674 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1675 getprogname());
1676 exit(1);
1679 static void
1680 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1681 const char *root_path)
1683 int is_root_path = (strcmp(path, root_path) == 0);
1685 path += strlen(root_path);
1686 while (path[0] == '/')
1687 path++;
1689 printf("%s%s%s%s%s\n", id ? id : "", path,
1690 is_root_path ? "" : "/", te->name,
1691 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1694 static const struct got_error *
1695 print_tree(const char *path, struct got_object_id *commit_id,
1696 int show_ids, int recurse, const char *root_path,
1697 struct got_repository *repo)
1699 const struct got_error *err = NULL;
1700 struct got_object_id *tree_id = NULL;
1701 struct got_tree_object *tree = NULL;
1702 const struct got_tree_entries *entries;
1703 struct got_tree_entry *te;
1705 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1706 if (err)
1707 goto done;
1709 err = got_object_open_as_tree(&tree, repo, tree_id);
1710 if (err)
1711 goto done;
1712 entries = got_object_tree_get_entries(tree);
1713 te = SIMPLEQ_FIRST(&entries->head);
1714 while (te) {
1715 char *id = NULL;
1717 if (sigint_received || sigpipe_received)
1718 break;
1720 if (show_ids) {
1721 char *id_str;
1722 err = got_object_id_str(&id_str, te->id);
1723 if (err)
1724 goto done;
1725 if (asprintf(&id, "%s ", id_str) == -1) {
1726 err = got_error_from_errno("asprintf");
1727 free(id_str);
1728 goto done;
1730 free(id_str);
1732 print_entry(te, id, path, root_path);
1733 free(id);
1735 if (recurse && S_ISDIR(te->mode)) {
1736 char *child_path;
1737 if (asprintf(&child_path, "%s%s%s", path,
1738 path[0] == '/' && path[1] == '\0' ? "" : "/",
1739 te->name) == -1) {
1740 err = got_error_from_errno("asprintf");
1741 goto done;
1743 err = print_tree(child_path, commit_id, show_ids, 1,
1744 root_path, repo);
1745 free(child_path);
1746 if (err)
1747 goto done;
1750 te = SIMPLEQ_NEXT(te, entry);
1752 done:
1753 if (tree)
1754 got_object_tree_close(tree);
1755 free(tree_id);
1756 return err;
1759 static const struct got_error *
1760 cmd_tree(int argc, char *argv[])
1762 const struct got_error *error;
1763 struct got_repository *repo = NULL;
1764 struct got_worktree *worktree = NULL;
1765 const char *path;
1766 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1767 struct got_object_id *commit_id = NULL;
1768 char *commit_id_str = NULL;
1769 int show_ids = 0, recurse = 0;
1770 int ch;
1772 #ifndef PROFILE
1773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1774 NULL) == -1)
1775 err(1, "pledge");
1776 #endif
1778 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1779 switch (ch) {
1780 case 'c':
1781 commit_id_str = optarg;
1782 break;
1783 case 'r':
1784 repo_path = realpath(optarg, NULL);
1785 if (repo_path == NULL)
1786 err(1, "-r option");
1787 got_path_strip_trailing_slashes(repo_path);
1788 break;
1789 case 'i':
1790 show_ids = 1;
1791 break;
1792 case 'R':
1793 recurse = 1;
1794 break;
1795 default:
1796 usage_tree();
1797 /* NOTREACHED */
1801 argc -= optind;
1802 argv += optind;
1804 if (argc == 1)
1805 path = argv[0];
1806 else if (argc > 1)
1807 usage_tree();
1808 else
1809 path = NULL;
1811 cwd = getcwd(NULL, 0);
1812 if (cwd == NULL) {
1813 error = got_error_from_errno("getcwd");
1814 goto done;
1816 if (repo_path == NULL) {
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 goto done;
1820 else
1821 error = NULL;
1822 if (worktree) {
1823 repo_path =
1824 strdup(got_worktree_get_repo_path(worktree));
1825 if (repo_path == NULL)
1826 error = got_error_from_errno("strdup");
1827 if (error)
1828 goto done;
1829 } else {
1830 repo_path = strdup(cwd);
1831 if (repo_path == NULL) {
1832 error = got_error_from_errno("strdup");
1833 goto done;
1838 error = got_repo_open(&repo, repo_path);
1839 if (error != NULL)
1840 goto done;
1842 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1843 if (error)
1844 goto done;
1846 if (path == NULL) {
1847 if (worktree) {
1848 char *p, *worktree_subdir = cwd +
1849 strlen(got_worktree_get_root_path(worktree));
1850 if (asprintf(&p, "%s/%s",
1851 got_worktree_get_path_prefix(worktree),
1852 worktree_subdir) == -1) {
1853 error = got_error_from_errno("asprintf");
1854 goto done;
1856 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1857 free(p);
1858 if (error)
1859 goto done;
1860 } else
1861 path = "/";
1863 if (in_repo_path == NULL) {
1864 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1865 if (error != NULL)
1866 goto done;
1869 if (commit_id_str == NULL) {
1870 struct got_reference *head_ref;
1871 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1872 if (error != NULL)
1873 goto done;
1874 error = got_ref_resolve(&commit_id, repo, head_ref);
1875 got_ref_close(head_ref);
1876 if (error != NULL)
1877 goto done;
1878 } else {
1879 error = got_repo_match_object_id_prefix(&commit_id,
1880 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1881 if (error != NULL)
1882 goto done;
1885 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1886 in_repo_path, repo);
1887 done:
1888 free(in_repo_path);
1889 free(repo_path);
1890 free(cwd);
1891 free(commit_id);
1892 if (worktree)
1893 got_worktree_close(worktree);
1894 if (repo) {
1895 const struct got_error *repo_error;
1896 repo_error = got_repo_close(repo);
1897 if (error == NULL)
1898 error = repo_error;
1900 return error;
1903 __dead static void
1904 usage_status(void)
1906 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1907 exit(1);
1910 static const struct got_error *
1911 print_status(void *arg, unsigned char status, const char *path,
1912 struct got_object_id *blob_id, struct got_object_id *commit_id)
1914 printf("%c %s\n", status, path);
1915 return NULL;
1918 static const struct got_error *
1919 cmd_status(int argc, char *argv[])
1921 const struct got_error *error = NULL;
1922 struct got_repository *repo = NULL;
1923 struct got_worktree *worktree = NULL;
1924 char *cwd = NULL, *path = NULL;
1925 int ch;
1927 while ((ch = getopt(argc, argv, "")) != -1) {
1928 switch (ch) {
1929 default:
1930 usage_status();
1931 /* NOTREACHED */
1935 argc -= optind;
1936 argv += optind;
1938 #ifndef PROFILE
1939 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1940 NULL) == -1)
1941 err(1, "pledge");
1942 #endif
1943 cwd = getcwd(NULL, 0);
1944 if (cwd == NULL) {
1945 error = got_error_from_errno("getcwd");
1946 goto done;
1949 error = got_worktree_open(&worktree, cwd);
1950 if (error != NULL)
1951 goto done;
1953 if (argc == 0) {
1954 path = strdup("");
1955 if (path == NULL) {
1956 error = got_error_from_errno("strdup");
1957 goto done;
1959 } else if (argc == 1) {
1960 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1961 if (error)
1962 goto done;
1963 } else
1964 usage_status();
1966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1967 if (error != NULL)
1968 goto done;
1970 error = apply_unveil(got_repo_get_path(repo), 1,
1971 got_worktree_get_root_path(worktree), 0);
1972 if (error)
1973 goto done;
1975 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1976 check_cancelled, NULL);
1977 done:
1978 free(cwd);
1979 free(path);
1980 return error;
1983 __dead static void
1984 usage_ref(void)
1986 fprintf(stderr,
1987 "usage: %s ref [-r repository] -l | -d name | name target\n",
1988 getprogname());
1989 exit(1);
1992 static const struct got_error *
1993 list_refs(struct got_repository *repo)
1995 static const struct got_error *err = NULL;
1996 struct got_reflist_head refs;
1997 struct got_reflist_entry *re;
1999 SIMPLEQ_INIT(&refs);
2000 err = got_ref_list(&refs, repo);
2001 if (err)
2002 return err;
2004 SIMPLEQ_FOREACH(re, &refs, entry) {
2005 char *refstr;
2006 refstr = got_ref_to_str(re->ref);
2007 if (refstr == NULL)
2008 return got_error_from_errno("got_ref_to_str");
2009 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2010 free(refstr);
2013 got_ref_list_free(&refs);
2014 return NULL;
2017 static const struct got_error *
2018 delete_ref(struct got_repository *repo, const char *refname)
2020 const struct got_error *err = NULL;
2021 struct got_reference *ref;
2023 err = got_ref_open(&ref, repo, refname, 0);
2024 if (err)
2025 return err;
2027 err = got_ref_delete(ref, repo);
2028 got_ref_close(ref);
2029 return err;
2032 static const struct got_error *
2033 add_ref(struct got_repository *repo, const char *refname, const char *target)
2035 const struct got_error *err = NULL;
2036 struct got_object_id *id;
2037 struct got_reference *ref = NULL;
2039 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2040 repo);
2041 if (err) {
2042 struct got_reference *target_ref;
2044 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2045 return err;
2046 err = got_ref_open(&target_ref, repo, target, 0);
2047 if (err)
2048 return err;
2049 err = got_ref_resolve(&id, repo, target_ref);
2050 got_ref_close(target_ref);
2051 if (err)
2052 return err;
2055 err = got_ref_alloc(&ref, refname, id);
2056 if (err)
2057 goto done;
2059 err = got_ref_write(ref, repo);
2060 done:
2061 if (ref)
2062 got_ref_close(ref);
2063 free(id);
2064 return err;
2067 static const struct got_error *
2068 cmd_ref(int argc, char *argv[])
2070 const struct got_error *error = NULL;
2071 struct got_repository *repo = NULL;
2072 struct got_worktree *worktree = NULL;
2073 char *cwd = NULL, *repo_path = NULL;
2074 int ch, do_list = 0;
2075 const char *delref = NULL;
2077 /* TODO: Add -s option for adding symbolic references. */
2078 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2079 switch (ch) {
2080 case 'd':
2081 delref = optarg;
2082 break;
2083 case 'r':
2084 repo_path = realpath(optarg, NULL);
2085 if (repo_path == NULL)
2086 err(1, "-r option");
2087 got_path_strip_trailing_slashes(repo_path);
2088 break;
2089 case 'l':
2090 do_list = 1;
2091 break;
2092 default:
2093 usage_ref();
2094 /* NOTREACHED */
2098 if (do_list && delref)
2099 errx(1, "-l and -d options are mutually exclusive\n");
2101 argc -= optind;
2102 argv += optind;
2104 if (do_list || delref) {
2105 if (argc > 0)
2106 usage_ref();
2107 } else if (argc != 2)
2108 usage_ref();
2110 #ifndef PROFILE
2111 if (do_list) {
2112 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2113 NULL) == -1)
2114 err(1, "pledge");
2115 } else {
2116 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2117 "sendfd unveil", NULL) == -1)
2118 err(1, "pledge");
2120 #endif
2121 cwd = getcwd(NULL, 0);
2122 if (cwd == NULL) {
2123 error = got_error_from_errno("getcwd");
2124 goto done;
2127 if (repo_path == NULL) {
2128 error = got_worktree_open(&worktree, cwd);
2129 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2130 goto done;
2131 else
2132 error = NULL;
2133 if (worktree) {
2134 repo_path =
2135 strdup(got_worktree_get_repo_path(worktree));
2136 if (repo_path == NULL)
2137 error = got_error_from_errno("strdup");
2138 if (error)
2139 goto done;
2140 } else {
2141 repo_path = strdup(cwd);
2142 if (repo_path == NULL) {
2143 error = got_error_from_errno("strdup");
2144 goto done;
2149 error = got_repo_open(&repo, repo_path);
2150 if (error != NULL)
2151 goto done;
2153 error = apply_unveil(got_repo_get_path(repo), do_list,
2154 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2155 if (error)
2156 goto done;
2158 if (do_list)
2159 error = list_refs(repo);
2160 else if (delref)
2161 error = delete_ref(repo, delref);
2162 else
2163 error = add_ref(repo, argv[0], argv[1]);
2164 done:
2165 if (repo)
2166 got_repo_close(repo);
2167 if (worktree)
2168 got_worktree_close(worktree);
2169 free(cwd);
2170 free(repo_path);
2171 return error;
2174 __dead static void
2175 usage_branch(void)
2177 fprintf(stderr,
2178 "usage: %s branch [-r repository] -l | -d name | "
2179 "name [base-branch]\n", getprogname());
2180 exit(1);
2183 static const struct got_error *
2184 list_branches(struct got_repository *repo)
2186 static const struct got_error *err = NULL;
2187 struct got_reflist_head refs;
2188 struct got_reflist_entry *re;
2190 SIMPLEQ_INIT(&refs);
2191 err = got_ref_list(&refs, repo);
2192 if (err)
2193 return err;
2195 SIMPLEQ_FOREACH(re, &refs, entry) {
2196 const char *refname;
2197 char *refstr;
2198 refname = got_ref_get_name(re->ref);
2199 if (strncmp(refname, "refs/heads/", 11) != 0)
2200 continue;
2201 refname += 11;
2202 refstr = got_ref_to_str(re->ref);
2203 if (refstr == NULL)
2204 return got_error_from_errno("got_ref_to_str");
2205 printf("%s: %s\n", refname, refstr);
2206 free(refstr);
2209 got_ref_list_free(&refs);
2210 return NULL;
2213 static const struct got_error *
2214 delete_branch(struct got_repository *repo, const char *branch_name)
2216 const struct got_error *err = NULL;
2217 struct got_reference *ref;
2218 char *refname;
2220 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2221 return got_error_from_errno("asprintf");
2223 err = got_ref_open(&ref, repo, refname, 0);
2224 if (err)
2225 goto done;
2227 err = got_ref_delete(ref, repo);
2228 got_ref_close(ref);
2229 done:
2230 free(refname);
2231 return err;
2234 static const struct got_error *
2235 add_branch(struct got_repository *repo, const char *branch_name,
2236 const char *base_branch)
2238 const struct got_error *err = NULL;
2239 struct got_object_id *id = NULL;
2240 struct got_reference *ref = NULL;
2241 char *base_refname = NULL, *refname = NULL;
2242 struct got_reference *base_ref;
2244 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2245 base_refname = strdup(GOT_REF_HEAD);
2246 if (base_refname == NULL)
2247 return got_error_from_errno("strdup");
2248 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2249 return got_error_from_errno("asprintf");
2251 err = got_ref_open(&base_ref, repo, base_refname, 0);
2252 if (err)
2253 goto done;
2254 err = got_ref_resolve(&id, repo, base_ref);
2255 got_ref_close(base_ref);
2256 if (err)
2257 goto done;
2259 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2260 err = got_error_from_errno("asprintf");
2261 goto done;
2264 err = got_ref_open(&ref, repo, refname, 0);
2265 if (err == NULL) {
2266 err = got_error(GOT_ERR_BRANCH_EXISTS);
2267 goto done;
2268 } else if (err->code != GOT_ERR_NOT_REF)
2269 goto done;
2271 err = got_ref_alloc(&ref, refname, id);
2272 if (err)
2273 goto done;
2275 err = got_ref_write(ref, repo);
2276 done:
2277 if (ref)
2278 got_ref_close(ref);
2279 free(id);
2280 free(base_refname);
2281 free(refname);
2282 return err;
2285 static const struct got_error *
2286 cmd_branch(int argc, char *argv[])
2288 const struct got_error *error = NULL;
2289 struct got_repository *repo = NULL;
2290 struct got_worktree *worktree = NULL;
2291 char *cwd = NULL, *repo_path = NULL;
2292 int ch, do_list = 0;
2293 const char *delref = NULL;
2295 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2296 switch (ch) {
2297 case 'd':
2298 delref = optarg;
2299 break;
2300 case 'r':
2301 repo_path = realpath(optarg, NULL);
2302 if (repo_path == NULL)
2303 err(1, "-r option");
2304 got_path_strip_trailing_slashes(repo_path);
2305 break;
2306 case 'l':
2307 do_list = 1;
2308 break;
2309 default:
2310 usage_branch();
2311 /* NOTREACHED */
2315 if (do_list && delref)
2316 errx(1, "-l and -d options are mutually exclusive\n");
2318 argc -= optind;
2319 argv += optind;
2321 if (do_list || delref) {
2322 if (argc > 0)
2323 usage_branch();
2324 } else if (argc < 1 || argc > 2)
2325 usage_branch();
2327 #ifndef PROFILE
2328 if (do_list) {
2329 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2330 NULL) == -1)
2331 err(1, "pledge");
2332 } else {
2333 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2334 "sendfd unveil", NULL) == -1)
2335 err(1, "pledge");
2337 #endif
2338 cwd = getcwd(NULL, 0);
2339 if (cwd == NULL) {
2340 error = got_error_from_errno("getcwd");
2341 goto done;
2344 if (repo_path == NULL) {
2345 error = got_worktree_open(&worktree, cwd);
2346 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2347 goto done;
2348 else
2349 error = NULL;
2350 if (worktree) {
2351 repo_path =
2352 strdup(got_worktree_get_repo_path(worktree));
2353 if (repo_path == NULL)
2354 error = got_error_from_errno("strdup");
2355 if (error)
2356 goto done;
2357 } else {
2358 repo_path = strdup(cwd);
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2361 goto done;
2366 error = got_repo_open(&repo, repo_path);
2367 if (error != NULL)
2368 goto done;
2370 error = apply_unveil(got_repo_get_path(repo), do_list,
2371 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2372 if (error)
2373 goto done;
2375 if (do_list)
2376 error = list_branches(repo);
2377 else if (delref)
2378 error = delete_branch(repo, delref);
2379 else {
2380 const char *base_branch;
2381 if (argc == 1) {
2382 base_branch = worktree ?
2383 got_worktree_get_head_ref_name(worktree) :
2384 GOT_REF_HEAD;
2385 } else
2386 base_branch = argv[1];
2387 error = add_branch(repo, argv[0], base_branch);
2389 done:
2390 if (repo)
2391 got_repo_close(repo);
2392 if (worktree)
2393 got_worktree_close(worktree);
2394 free(cwd);
2395 free(repo_path);
2396 return error;
2399 __dead static void
2400 usage_add(void)
2402 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2403 exit(1);
2406 static const struct got_error *
2407 cmd_add(int argc, char *argv[])
2409 const struct got_error *error = NULL;
2410 struct got_repository *repo = NULL;
2411 struct got_worktree *worktree = NULL;
2412 char *cwd = NULL;
2413 struct got_pathlist_head paths;
2414 struct got_pathlist_entry *pe;
2415 int ch, x;
2417 TAILQ_INIT(&paths);
2419 while ((ch = getopt(argc, argv, "")) != -1) {
2420 switch (ch) {
2421 default:
2422 usage_add();
2423 /* NOTREACHED */
2427 argc -= optind;
2428 argv += optind;
2430 if (argc < 1)
2431 usage_add();
2433 /* make sure each file exists before doing anything halfway */
2434 for (x = 0; x < argc; x++) {
2435 char *path = realpath(argv[x], NULL);
2436 if (path == NULL) {
2437 error = got_error_from_errno2("realpath", argv[x]);
2438 goto done;
2440 free(path);
2443 cwd = getcwd(NULL, 0);
2444 if (cwd == NULL) {
2445 error = got_error_from_errno("getcwd");
2446 goto done;
2449 error = got_worktree_open(&worktree, cwd);
2450 if (error)
2451 goto done;
2453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2454 if (error != NULL)
2455 goto done;
2457 error = apply_unveil(got_repo_get_path(repo), 1,
2458 got_worktree_get_root_path(worktree), 0);
2459 if (error)
2460 goto done;
2462 for (x = 0; x < argc; x++) {
2463 char *path = realpath(argv[x], NULL);
2464 if (path == NULL) {
2465 error = got_error_from_errno2("realpath", argv[x]);
2466 goto done;
2469 got_path_strip_trailing_slashes(path);
2470 error = got_pathlist_insert(&pe, &paths, path, NULL);
2471 if (error) {
2472 free(path);
2473 goto done;
2476 error = got_worktree_schedule_add(worktree, &paths, print_status,
2477 NULL, repo);
2478 done:
2479 if (repo)
2480 got_repo_close(repo);
2481 if (worktree)
2482 got_worktree_close(worktree);
2483 TAILQ_FOREACH(pe, &paths, entry)
2484 free((char *)pe->path);
2485 got_pathlist_free(&paths);
2486 free(cwd);
2487 return error;
2490 __dead static void
2491 usage_rm(void)
2493 fprintf(stderr, "usage: %s rm [-f] file-path ...\n", getprogname());
2494 exit(1);
2497 static const struct got_error *
2498 cmd_rm(int argc, char *argv[])
2500 const struct got_error *error = NULL;
2501 struct got_worktree *worktree = NULL;
2502 struct got_repository *repo = NULL;
2503 char *cwd = NULL;
2504 struct got_pathlist_head paths;
2505 struct got_pathlist_entry *pe;
2506 int ch, i, delete_local_mods = 0;
2508 TAILQ_INIT(&paths);
2510 while ((ch = getopt(argc, argv, "f")) != -1) {
2511 switch (ch) {
2512 case 'f':
2513 delete_local_mods = 1;
2514 break;
2515 default:
2516 usage_add();
2517 /* NOTREACHED */
2521 argc -= optind;
2522 argv += optind;
2524 if (argc < 1)
2525 usage_rm();
2527 /* make sure each file exists before doing anything halfway */
2528 for (i = 0; i < argc; i++) {
2529 char *path = realpath(argv[i], NULL);
2530 if (path == NULL) {
2531 error = got_error_from_errno2("realpath", argv[i]);
2532 goto done;
2534 free(path);
2537 cwd = getcwd(NULL, 0);
2538 if (cwd == NULL) {
2539 error = got_error_from_errno("getcwd");
2540 goto done;
2542 error = got_worktree_open(&worktree, cwd);
2543 if (error)
2544 goto done;
2546 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2547 if (error)
2548 goto done;
2550 error = apply_unveil(got_repo_get_path(repo), 1,
2551 got_worktree_get_root_path(worktree), 0);
2552 if (error)
2553 goto done;
2555 for (i = 0; i < argc; i++) {
2556 char *path = realpath(argv[i], NULL);
2557 if (path == NULL) {
2558 error = got_error_from_errno2("realpath", argv[i]);
2559 goto done;
2562 got_path_strip_trailing_slashes(path);
2563 error = got_pathlist_insert(&pe, &paths, path, NULL);
2564 if (error) {
2565 free(path);
2566 goto done;
2569 error = got_worktree_schedule_delete(worktree, &paths,
2570 delete_local_mods, print_status, NULL, repo);
2571 if (error)
2572 goto done;
2573 done:
2574 if (repo)
2575 got_repo_close(repo);
2576 if (worktree)
2577 got_worktree_close(worktree);
2578 TAILQ_FOREACH(pe, &paths, entry)
2579 free((char *)pe->path);
2580 got_pathlist_free(&paths);
2581 free(cwd);
2582 return error;
2585 __dead static void
2586 usage_revert(void)
2588 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2589 exit(1);
2592 static void
2593 revert_progress(void *arg, unsigned char status, const char *path)
2595 while (path[0] == '/')
2596 path++;
2597 printf("%c %s\n", status, path);
2600 static const struct got_error *
2601 cmd_revert(int argc, char *argv[])
2603 const struct got_error *error = NULL;
2604 struct got_worktree *worktree = NULL;
2605 struct got_repository *repo = NULL;
2606 char *cwd = NULL, *path = NULL;
2607 struct got_pathlist_head paths;
2608 struct got_pathlist_entry *pe;
2609 int ch, i;
2611 TAILQ_INIT(&paths);
2613 while ((ch = getopt(argc, argv, "")) != -1) {
2614 switch (ch) {
2615 default:
2616 usage_revert();
2617 /* NOTREACHED */
2621 argc -= optind;
2622 argv += optind;
2624 if (argc < 1)
2625 usage_revert();
2627 for (i = 0; i < argc; i++) {
2628 char *path = realpath(argv[i], NULL);
2629 if (path == NULL) {
2630 error = got_error_from_errno2("realpath", argv[i]);
2631 goto done;
2634 got_path_strip_trailing_slashes(path);
2635 error = got_pathlist_insert(&pe, &paths, path, NULL);
2636 if (error) {
2637 free(path);
2638 goto done;
2642 cwd = getcwd(NULL, 0);
2643 if (cwd == NULL) {
2644 error = got_error_from_errno("getcwd");
2645 goto done;
2647 error = got_worktree_open(&worktree, cwd);
2648 if (error)
2649 goto done;
2651 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2652 if (error != NULL)
2653 goto done;
2655 error = apply_unveil(got_repo_get_path(repo), 1,
2656 got_worktree_get_root_path(worktree), 0);
2657 if (error)
2658 goto done;
2660 error = got_worktree_revert(worktree, &paths,
2661 revert_progress, NULL, repo);
2662 if (error)
2663 goto done;
2664 done:
2665 if (repo)
2666 got_repo_close(repo);
2667 if (worktree)
2668 got_worktree_close(worktree);
2669 free(path);
2670 free(cwd);
2671 return error;
2674 __dead static void
2675 usage_commit(void)
2677 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2678 exit(1);
2681 int
2682 spawn_editor(const char *editor, const char *file)
2684 pid_t pid;
2685 sig_t sighup, sigint, sigquit;
2686 int st = -1;
2688 sighup = signal(SIGHUP, SIG_IGN);
2689 sigint = signal(SIGINT, SIG_IGN);
2690 sigquit = signal(SIGQUIT, SIG_IGN);
2692 switch (pid = fork()) {
2693 case -1:
2694 goto doneediting;
2695 case 0:
2696 execl(editor, editor, file, (char *)NULL);
2697 _exit(127);
2700 while (waitpid(pid, &st, 0) == -1)
2701 if (errno != EINTR)
2702 break;
2704 doneediting:
2705 (void)signal(SIGHUP, sighup);
2706 (void)signal(SIGINT, sigint);
2707 (void)signal(SIGQUIT, sigquit);
2709 if (!WIFEXITED(st)) {
2710 errno = EINTR;
2711 return -1;
2714 return WEXITSTATUS(st);
2717 struct collect_commit_logmsg_arg {
2718 const char *cmdline_log;
2719 const char *editor;
2720 const char *worktree_path;
2721 const char *branch_name;
2722 const char *repo_path;
2723 char *logmsg_path;
2727 static const struct got_error *
2728 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2729 void *arg)
2731 char *initial_content = NULL;
2732 struct got_pathlist_entry *pe;
2733 const struct got_error *err = NULL;
2734 char *template = NULL;
2735 struct collect_commit_logmsg_arg *a = arg;
2736 char buf[1024];
2737 struct stat st, st2;
2738 FILE *fp;
2739 size_t len;
2740 int fd, content_changed = 0;
2742 /* if a message was specified on the command line, just use it */
2743 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2744 len = strlen(a->cmdline_log) + 1;
2745 *logmsg = malloc(len + 1);
2746 if (*logmsg == NULL)
2747 return got_error_from_errno("malloc");
2748 strlcpy(*logmsg, a->cmdline_log, len);
2749 return NULL;
2752 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2753 return got_error_from_errno("asprintf");
2755 if (asprintf(&initial_content,
2756 "\n# changes to be committed on branch %s:\n",
2757 a->branch_name) == -1)
2758 return got_error_from_errno("asprintf");
2760 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2761 if (err)
2762 goto done;
2764 dprintf(fd, initial_content);
2766 TAILQ_FOREACH(pe, commitable_paths, entry) {
2767 struct got_commitable *ct = pe->data;
2768 dprintf(fd, "# %c %s\n",
2769 got_commitable_get_status(ct),
2770 got_commitable_get_path(ct));
2772 close(fd);
2774 if (stat(a->logmsg_path, &st) == -1) {
2775 err = got_error_from_errno2("stat", a->logmsg_path);
2776 goto done;
2779 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2780 err = got_error_from_errno("failed spawning editor");
2781 goto done;
2784 if (stat(a->logmsg_path, &st2) == -1) {
2785 err = got_error_from_errno("stat");
2786 goto done;
2789 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2790 unlink(a->logmsg_path);
2791 free(a->logmsg_path);
2792 a->logmsg_path = NULL;
2793 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2794 "no changes made to commit message, aborting");
2795 goto done;
2798 *logmsg = malloc(st2.st_size + 1);
2799 if (*logmsg == NULL) {
2800 err = got_error_from_errno("malloc");
2801 goto done;
2803 (*logmsg)[0] = '\0';
2804 len = 0;
2806 fp = fopen(a->logmsg_path, "r");
2807 if (fp == NULL) {
2808 err = got_error_from_errno("fopen");
2809 goto done;
2811 while (fgets(buf, sizeof(buf), fp) != NULL) {
2812 if (!content_changed && strcmp(buf, initial_content) != 0)
2813 content_changed = 1;
2814 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2815 continue; /* remove comments and leading empty lines */
2816 len = strlcat(*logmsg, buf, st2.st_size);
2818 fclose(fp);
2820 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2821 (*logmsg)[len - 1] = '\0';
2822 len--;
2825 if (len == 0 || !content_changed) {
2826 unlink(a->logmsg_path);
2827 free(a->logmsg_path);
2828 a->logmsg_path = NULL;
2829 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2830 "commit message cannot be empty, aborting");
2831 goto done;
2833 done:
2834 free(initial_content);
2835 free(template);
2837 /* Editor is done; we can now apply unveil(2) */
2838 if (err == NULL)
2839 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2840 return err;
2843 static const struct got_error *
2844 cmd_commit(int argc, char *argv[])
2846 const struct got_error *error = NULL;
2847 struct got_worktree *worktree = NULL;
2848 struct got_repository *repo = NULL;
2849 char *cwd = NULL, *path = NULL, *id_str = NULL;
2850 struct got_object_id *id = NULL;
2851 const char *logmsg = NULL;
2852 const char *got_author = getenv("GOT_AUTHOR");
2853 struct collect_commit_logmsg_arg cl_arg;
2854 char *editor = NULL;
2855 int ch;
2857 while ((ch = getopt(argc, argv, "m:")) != -1) {
2858 switch (ch) {
2859 case 'm':
2860 logmsg = optarg;
2861 break;
2862 default:
2863 usage_commit();
2864 /* NOTREACHED */
2868 argc -= optind;
2869 argv += optind;
2871 if (argc == 1) {
2872 path = realpath(argv[0], NULL);
2873 if (path == NULL) {
2874 error = got_error_from_errno2("realpath", argv[0]);
2875 goto done;
2877 got_path_strip_trailing_slashes(path);
2878 } else if (argc != 0)
2879 usage_commit();
2881 if (got_author == NULL) {
2882 /* TODO: Look current user up in password database */
2883 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2884 goto done;
2887 cwd = getcwd(NULL, 0);
2888 if (cwd == NULL) {
2889 error = got_error_from_errno("getcwd");
2890 goto done;
2892 error = got_worktree_open(&worktree, cwd);
2893 if (error)
2894 goto done;
2896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2897 if (error != NULL)
2898 goto done;
2901 * unveil(2) traverses exec(2); if an editor is used we have
2902 * to apply unveil after the log message has been written.
2904 if (logmsg == NULL || strlen(logmsg) == 0)
2905 error = get_editor(&editor);
2906 else
2907 error = apply_unveil(got_repo_get_path(repo), 0,
2908 got_worktree_get_root_path(worktree), 0);
2909 if (error)
2910 goto done;
2912 cl_arg.editor = editor;
2913 cl_arg.cmdline_log = logmsg;
2914 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2915 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2916 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2917 cl_arg.branch_name += 5;
2918 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2919 cl_arg.branch_name += 6;
2920 cl_arg.repo_path = got_repo_get_path(repo);
2921 cl_arg.logmsg_path = NULL;
2922 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2923 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2924 if (error) {
2925 if (cl_arg.logmsg_path)
2926 fprintf(stderr, "%s: log message preserved in %s\n",
2927 getprogname(), cl_arg.logmsg_path);
2928 goto done;
2931 if (cl_arg.logmsg_path)
2932 unlink(cl_arg.logmsg_path);
2934 error = got_object_id_str(&id_str, id);
2935 if (error)
2936 goto done;
2937 printf("Created commit %s\n", id_str);
2938 done:
2939 if (repo)
2940 got_repo_close(repo);
2941 if (worktree)
2942 got_worktree_close(worktree);
2943 free(path);
2944 free(cwd);
2945 free(id_str);
2946 free(editor);
2947 return error;
2950 __dead static void
2951 usage_cherrypick(void)
2953 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2954 exit(1);
2957 static const struct got_error *
2958 cmd_cherrypick(int argc, char *argv[])
2960 const struct got_error *error = NULL;
2961 struct got_worktree *worktree = NULL;
2962 struct got_repository *repo = NULL;
2963 char *cwd = NULL, *commit_id_str = NULL;
2964 struct got_object_id *commit_id = NULL;
2965 struct got_commit_object *commit = NULL;
2966 struct got_object_qid *pid;
2967 struct got_reference *head_ref = NULL;
2968 int ch, did_something = 0;
2970 while ((ch = getopt(argc, argv, "")) != -1) {
2971 switch (ch) {
2972 default:
2973 usage_cherrypick();
2974 /* NOTREACHED */
2978 argc -= optind;
2979 argv += optind;
2981 if (argc != 1)
2982 usage_cherrypick();
2984 cwd = getcwd(NULL, 0);
2985 if (cwd == NULL) {
2986 error = got_error_from_errno("getcwd");
2987 goto done;
2989 error = got_worktree_open(&worktree, cwd);
2990 if (error)
2991 goto done;
2993 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2994 if (error != NULL)
2995 goto done;
2997 error = apply_unveil(got_repo_get_path(repo), 0,
2998 got_worktree_get_root_path(worktree), 0);
2999 if (error)
3000 goto done;
3002 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3003 GOT_OBJ_TYPE_COMMIT, repo);
3004 if (error != NULL) {
3005 struct got_reference *ref;
3006 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3007 goto done;
3008 error = got_ref_open(&ref, repo, argv[0], 0);
3009 if (error != NULL)
3010 goto done;
3011 error = got_ref_resolve(&commit_id, repo, ref);
3012 got_ref_close(ref);
3013 if (error != NULL)
3014 goto done;
3016 error = got_object_id_str(&commit_id_str, commit_id);
3017 if (error)
3018 goto done;
3020 error = got_ref_open(&head_ref, repo,
3021 got_worktree_get_head_ref_name(worktree), 0);
3022 if (error != NULL)
3023 goto done;
3025 error = check_same_branch(commit_id, head_ref, repo);
3026 if (error) {
3027 if (error->code != GOT_ERR_ANCESTRY)
3028 goto done;
3029 error = NULL;
3030 } else {
3031 error = got_error(GOT_ERR_SAME_BRANCH);
3032 goto done;
3035 error = got_object_open_as_commit(&commit, repo, commit_id);
3036 if (error)
3037 goto done;
3038 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3039 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3040 commit_id, repo, update_progress, &did_something, check_cancelled,
3041 NULL);
3042 if (error != NULL)
3043 goto done;
3045 if (did_something)
3046 printf("Merged commit %s\n", commit_id_str);
3047 done:
3048 if (commit)
3049 got_object_commit_close(commit);
3050 free(commit_id_str);
3051 if (head_ref)
3052 got_ref_close(head_ref);
3053 if (worktree)
3054 got_worktree_close(worktree);
3055 if (repo)
3056 got_repo_close(repo);
3057 return error;
3060 __dead static void
3061 usage_backout(void)
3063 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3064 exit(1);
3067 static const struct got_error *
3068 cmd_backout(int argc, char *argv[])
3070 const struct got_error *error = NULL;
3071 struct got_worktree *worktree = NULL;
3072 struct got_repository *repo = NULL;
3073 char *cwd = NULL, *commit_id_str = NULL;
3074 struct got_object_id *commit_id = NULL;
3075 struct got_commit_object *commit = NULL;
3076 struct got_object_qid *pid;
3077 struct got_reference *head_ref = NULL;
3078 int ch, did_something = 0;
3080 while ((ch = getopt(argc, argv, "")) != -1) {
3081 switch (ch) {
3082 default:
3083 usage_backout();
3084 /* NOTREACHED */
3088 argc -= optind;
3089 argv += optind;
3091 if (argc != 1)
3092 usage_backout();
3094 cwd = getcwd(NULL, 0);
3095 if (cwd == NULL) {
3096 error = got_error_from_errno("getcwd");
3097 goto done;
3099 error = got_worktree_open(&worktree, cwd);
3100 if (error)
3101 goto done;
3103 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3104 if (error != NULL)
3105 goto done;
3107 error = apply_unveil(got_repo_get_path(repo), 0,
3108 got_worktree_get_root_path(worktree), 0);
3109 if (error)
3110 goto done;
3112 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3113 GOT_OBJ_TYPE_COMMIT, repo);
3114 if (error != NULL) {
3115 struct got_reference *ref;
3116 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3117 goto done;
3118 error = got_ref_open(&ref, repo, argv[0], 0);
3119 if (error != NULL)
3120 goto done;
3121 error = got_ref_resolve(&commit_id, repo, ref);
3122 got_ref_close(ref);
3123 if (error != NULL)
3124 goto done;
3126 error = got_object_id_str(&commit_id_str, commit_id);
3127 if (error)
3128 goto done;
3130 error = got_ref_open(&head_ref, repo,
3131 got_worktree_get_head_ref_name(worktree), 0);
3132 if (error != NULL)
3133 goto done;
3135 error = check_same_branch(commit_id, head_ref, repo);
3136 if (error)
3137 goto done;
3139 error = got_object_open_as_commit(&commit, repo, commit_id);
3140 if (error)
3141 goto done;
3142 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3143 if (pid == NULL) {
3144 error = got_error(GOT_ERR_ROOT_COMMIT);
3145 goto done;
3148 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3149 update_progress, &did_something, check_cancelled, NULL);
3150 if (error != NULL)
3151 goto done;
3153 if (did_something)
3154 printf("Backed out commit %s\n", commit_id_str);
3155 done:
3156 if (commit)
3157 got_object_commit_close(commit);
3158 free(commit_id_str);
3159 if (head_ref)
3160 got_ref_close(head_ref);
3161 if (worktree)
3162 got_worktree_close(worktree);
3163 if (repo)
3164 got_repo_close(repo);
3165 return error;