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 const char *cmd_descr;
74 };
76 __dead static void usage(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_add(void);
86 __dead static void usage_rm(void);
87 __dead static void usage_revert(void);
88 __dead static void usage_commit(void);
90 static const struct got_error* cmd_checkout(int, char *[]);
91 static const struct got_error* cmd_update(int, char *[]);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_status(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct got_error* cmd_add(int, char *[]);
99 static const struct got_error* cmd_rm(int, char *[]);
100 static const struct got_error* cmd_revert(int, char *[]);
101 static const struct got_error* cmd_commit(int, char *[]);
103 static struct cmd got_commands[] = {
104 { "checkout", cmd_checkout, usage_checkout,
105 "check out a new work tree from a repository" },
106 { "update", cmd_update, usage_update,
107 "update a work tree to a different commit" },
108 { "log", cmd_log, usage_log,
109 "show repository history" },
110 { "diff", cmd_diff, usage_diff,
111 "compare files and directories" },
112 { "blame", cmd_blame, usage_blame,
113 "show when lines in a file were changed" },
114 { "tree", cmd_tree, usage_tree,
115 "list files and directories in repository" },
116 { "status", cmd_status, usage_status,
117 "show modification status of files" },
118 { "ref", cmd_ref, usage_ref,
119 "manage references in repository" },
120 { "add", cmd_add, usage_add,
121 "add new files to version control" },
122 { "rm", cmd_rm, usage_rm,
123 "remove a versioned file" },
124 { "revert", cmd_revert, usage_revert,
125 "revert uncommitted changes" },
126 { "commit", cmd_commit, usage_commit,
127 "write changes from work tree to repository" },
128 };
130 int
131 main(int argc, char *argv[])
133 struct cmd *cmd;
134 unsigned int i;
135 int ch;
136 int hflag = 0;
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
141 switch (ch) {
142 case 'h':
143 hflag = 1;
144 break;
145 default:
146 usage();
147 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
153 optind = 0;
155 if (argc <= 0)
156 usage();
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(got_commands); i++) {
162 const struct got_error *error;
164 cmd = &got_commands[i];
166 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
167 continue;
169 if (hflag)
170 got_commands[i].cmd_usage();
172 error = got_commands[i].cmd_main(argc, argv);
173 if (error && !(sigint_received || sigpipe_received)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
175 return 1;
178 return 0;
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 return 1;
185 __dead static void
186 usage(void)
188 int i;
190 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 "Available commands:\n", getprogname());
192 for (i = 0; i < nitems(got_commands); i++) {
193 struct cmd *cmd = &got_commands[i];
194 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
196 exit(1);
199 static const struct got_error *
200 get_editor(char **abspath)
202 const struct got_error *err = NULL;
203 const char *editor;
205 editor = getenv("VISUAL");
206 if (editor == NULL)
207 editor = getenv("EDITOR");
209 if (editor) {
210 err = got_path_find_prog(abspath, editor);
211 if (err)
212 return err;
215 if (*abspath == NULL) {
216 *abspath = strdup("/bin/ed");
217 if (*abspath == NULL)
218 return got_error_from_errno("strdup");
221 return NULL;
224 static const struct got_error *
225 apply_unveil(const char *repo_path, int repo_read_only,
226 const char *worktree_path, int create_worktree)
228 const struct got_error *err;
230 if (create_worktree) {
231 /* Pre-create work tree path to avoid unveiling its parents. */
232 err = got_path_mkdir(worktree_path);
234 if (errno == EEXIST) {
235 if (got_path_dir_is_empty(worktree_path)) {
236 errno = 0;
237 err = NULL;
238 } else {
239 err = got_error_path(worktree_path,
240 GOT_ERR_DIR_NOT_EMPTY);
244 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
245 return err;
248 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
249 return got_error_from_errno2("unveil", repo_path);
251 if (worktree_path && unveil(worktree_path, "rwc") != 0)
252 return got_error_from_errno2("unveil", worktree_path);
254 if (unveil("/tmp", "rwc") != 0)
255 return got_error_from_errno2("unveil", "/tmp");
257 err = got_privsep_unveil_exec_helpers();
258 if (err != NULL)
259 return err;
261 if (unveil(NULL, NULL) != 0)
262 return got_error_from_errno("unveil");
264 return NULL;
267 __dead static void
268 usage_checkout(void)
270 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
271 "[-p prefix] repository-path [worktree-path]\n", getprogname());
272 exit(1);
275 static void
276 checkout_progress(void *arg, unsigned char status, const char *path)
278 char *worktree_path = arg;
280 while (path[0] == '/')
281 path++;
283 printf("%c %s/%s\n", status, worktree_path, path);
286 static const struct got_error *
287 check_cancelled(void *arg)
289 if (sigint_received || sigpipe_received)
290 return got_error(GOT_ERR_CANCELLED);
291 return NULL;
294 static const struct got_error *
295 check_linear_ancestry(struct got_object_id *commit_id,
296 struct got_object_id *base_commit_id, struct got_repository *repo)
298 const struct got_error *err = NULL;
299 struct got_object_id *yca_id;
301 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
302 commit_id, base_commit_id, repo);
303 if (err)
304 return err;
306 if (yca_id == NULL)
307 return got_error(GOT_ERR_ANCESTRY);
309 /*
310 * Require a straight line of history between the target commit
311 * and the work tree's base commit.
313 * Non-linear situations such as this require a rebase:
315 * (commit) D F (base_commit)
316 * \ /
317 * C E
318 * \ /
319 * B (yca)
320 * |
321 * A
323 * 'got update' only handles linear cases:
324 * Update forwards in time: A (base/yca) - B - C - D (commit)
325 * Update backwards in time: D (base) - C - B - A (commit/yca)
326 */
327 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
328 got_object_id_cmp(base_commit_id, yca_id) != 0)
329 return got_error(GOT_ERR_ANCESTRY);
331 free(yca_id);
332 return NULL;
335 static const struct got_error *
336 check_same_branch(struct got_object_id *commit_id,
337 struct got_reference *head_ref, struct got_repository *repo)
339 const struct got_error *err = NULL;
340 struct got_commit_graph *graph = NULL;
341 struct got_object_id *head_commit_id = NULL;
342 int is_same_branch = 0;
344 err = got_ref_resolve(&head_commit_id, repo, head_ref);
345 if (err)
346 goto done;
348 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
349 if (err)
350 goto done;
352 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
353 if (err)
354 goto done;
356 for (;;) {
357 struct got_object_id *id;
358 err = got_commit_graph_iter_next(&id, graph);
359 if (err) {
360 if (err->code == GOT_ERR_ITER_COMPLETED) {
361 err = NULL;
362 break;
364 else if (err->code != GOT_ERR_ITER_NEED_MORE)
365 break;
366 err = got_commit_graph_fetch_commits(graph, 1,
367 repo);
368 if (err)
369 break;
372 if (id) {
373 if (got_object_id_cmp(id, commit_id) == 0) {
374 is_same_branch = 1;
375 break;
379 done:
380 if (graph)
381 got_commit_graph_close(graph);
382 free(head_commit_id);
383 if (!err && !is_same_branch)
384 err = got_error(GOT_ERR_ANCESTRY);
385 return err;
388 static const struct got_error *
389 cmd_checkout(int argc, char *argv[])
391 const struct got_error *error = NULL;
392 struct got_repository *repo = NULL;
393 struct got_reference *head_ref = NULL;
394 struct got_worktree *worktree = NULL;
395 char *repo_path = NULL;
396 char *worktree_path = NULL;
397 const char *path_prefix = "";
398 const char *branch_name = GOT_REF_HEAD;
399 char *commit_id_str = NULL;
400 int ch, same_path_prefix;
402 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
403 switch (ch) {
404 case 'b':
405 branch_name = optarg;
406 break;
407 case 'c':
408 commit_id_str = strdup(optarg);
409 if (commit_id_str == NULL)
410 return got_error_from_errno("strdup");
411 break;
412 case 'p':
413 path_prefix = optarg;
414 break;
415 default:
416 usage_checkout();
417 /* NOTREACHED */
421 argc -= optind;
422 argv += optind;
424 #ifndef PROFILE
425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
426 "unveil", NULL) == -1)
427 err(1, "pledge");
428 #endif
429 if (argc == 1) {
430 char *cwd, *base, *dotgit;
431 repo_path = realpath(argv[0], NULL);
432 if (repo_path == NULL)
433 return got_error_from_errno2("realpath", argv[0]);
434 cwd = getcwd(NULL, 0);
435 if (cwd == NULL) {
436 error = got_error_from_errno("getcwd");
437 goto done;
439 if (path_prefix[0]) {
440 base = basename(path_prefix);
441 if (base == NULL) {
442 error = got_error_from_errno2("basename",
443 path_prefix);
444 goto done;
446 } else {
447 base = basename(repo_path);
448 if (base == NULL) {
449 error = got_error_from_errno2("basename",
450 repo_path);
451 goto done;
454 dotgit = strstr(base, ".git");
455 if (dotgit)
456 *dotgit = '\0';
457 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
458 error = got_error_from_errno("asprintf");
459 free(cwd);
460 goto done;
462 free(cwd);
463 } else if (argc == 2) {
464 repo_path = realpath(argv[0], NULL);
465 if (repo_path == NULL) {
466 error = got_error_from_errno2("realpath", argv[0]);
467 goto done;
469 worktree_path = realpath(argv[1], NULL);
470 if (worktree_path == NULL) {
471 error = got_error_from_errno2("realpath", argv[1]);
472 goto done;
474 } else
475 usage_checkout();
477 got_path_strip_trailing_slashes(repo_path);
478 got_path_strip_trailing_slashes(worktree_path);
480 error = got_repo_open(&repo, repo_path);
481 if (error != NULL)
482 goto done;
484 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
485 if (error)
486 goto done;
488 error = got_ref_open(&head_ref, repo, branch_name, 0);
489 if (error != NULL)
490 goto done;
492 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
493 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
494 goto done;
496 error = got_worktree_open(&worktree, worktree_path);
497 if (error != NULL)
498 goto done;
500 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
501 path_prefix);
502 if (error != NULL)
503 goto done;
504 if (!same_path_prefix) {
505 error = got_error(GOT_ERR_PATH_PREFIX);
506 goto done;
509 if (commit_id_str) {
510 struct got_object_id *commit_id;
511 error = got_object_resolve_id_str(&commit_id, repo,
512 commit_id_str);
513 if (error != NULL)
514 goto done;
515 error = check_linear_ancestry(commit_id,
516 got_worktree_get_base_commit_id(worktree), repo);
517 if (error != NULL) {
518 free(commit_id);
519 goto done;
521 error = check_same_branch(commit_id, head_ref, repo);
522 if (error)
523 goto done;
524 error = got_worktree_set_base_commit_id(worktree, repo,
525 commit_id);
526 free(commit_id);
527 if (error)
528 goto done;
531 error = got_worktree_checkout_files(worktree, "", repo,
532 checkout_progress, worktree_path, check_cancelled, NULL);
533 if (error != NULL)
534 goto done;
536 printf("Now shut up and hack\n");
538 done:
539 free(commit_id_str);
540 free(repo_path);
541 free(worktree_path);
542 return error;
545 __dead static void
546 usage_update(void)
548 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
549 getprogname());
550 exit(1);
553 static void
554 update_progress(void *arg, unsigned char status, const char *path)
556 int *did_something = arg;
558 if (status == GOT_STATUS_EXISTS)
559 return;
561 *did_something = 1;
562 while (path[0] == '/')
563 path++;
564 printf("%c %s\n", status, path);
567 static const struct got_error *
568 cmd_update(int argc, char *argv[])
570 const struct got_error *error = NULL;
571 struct got_repository *repo = NULL;
572 struct got_worktree *worktree = NULL;
573 char *worktree_path = NULL, *path = NULL;
574 struct got_object_id *commit_id = NULL;
575 char *commit_id_str = NULL;
576 const char *branch_name = NULL;
577 struct got_reference *head_ref = NULL;
578 int ch, did_something = 0;
580 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
581 switch (ch) {
582 case 'b':
583 branch_name = optarg;
584 break;
585 case 'c':
586 commit_id_str = strdup(optarg);
587 if (commit_id_str == NULL)
588 return got_error_from_errno("strdup");
589 break;
590 default:
591 usage_update();
592 /* NOTREACHED */
596 argc -= optind;
597 argv += optind;
599 #ifndef PROFILE
600 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
601 "unveil", NULL) == -1)
602 err(1, "pledge");
603 #endif
604 worktree_path = getcwd(NULL, 0);
605 if (worktree_path == NULL) {
606 error = got_error_from_errno("getcwd");
607 goto done;
609 error = got_worktree_open(&worktree, worktree_path);
610 if (error)
611 goto done;
613 if (argc == 0) {
614 path = strdup("");
615 if (path == NULL) {
616 error = got_error_from_errno("strdup");
617 goto done;
619 } else if (argc == 1) {
620 error = got_worktree_resolve_path(&path, worktree, argv[0]);
621 if (error)
622 goto done;
623 } else
624 usage_update();
626 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
627 if (error != NULL)
628 goto done;
630 error = apply_unveil(got_repo_get_path(repo), 0,
631 got_worktree_get_root_path(worktree), 0);
632 if (error)
633 goto done;
635 if (branch_name == NULL)
636 branch_name = got_worktree_get_head_ref_name(worktree);
637 error = got_ref_open(&head_ref, repo, branch_name, 0);
638 if (error != NULL)
639 goto done;
640 if (commit_id_str == NULL) {
641 error = got_ref_resolve(&commit_id, repo, head_ref);
642 if (error != NULL)
643 goto done;
644 error = got_object_id_str(&commit_id_str, commit_id);
645 if (error != NULL)
646 goto done;
647 } else {
648 error = got_object_resolve_id_str(&commit_id, repo,
649 commit_id_str);
650 if (error != NULL)
651 goto done;
654 if (strcmp(got_ref_get_name(head_ref),
655 got_worktree_get_head_ref_name(worktree)) != 0) {
656 struct got_object_id *head_commit_id;
657 if (strlen(path) != 0) {
658 fprintf(stderr, "%s: switching to a different "
659 "branch requires that the entire work tree "
660 "gets updated, not just '%s'\n",
661 getprogname(), path);
662 error = got_error(GOT_ERR_BAD_PATH);
663 goto done;
665 error = got_ref_resolve(&head_commit_id, repo, head_ref);
666 if (error)
667 goto done;
668 error = check_linear_ancestry(commit_id, head_commit_id, repo);
669 free(head_commit_id);
670 if (error != NULL)
671 goto done;
672 error = check_same_branch(commit_id, head_ref, repo);
673 if (error)
674 goto done;
675 error = got_worktree_set_head_ref(worktree, head_ref);
676 if (error)
677 goto done;
678 } else {
679 error = check_linear_ancestry(commit_id,
680 got_worktree_get_base_commit_id(worktree), repo);
681 if (error != NULL) {
682 if (error->code == GOT_ERR_ANCESTRY)
683 error = got_error(GOT_ERR_BRANCH_MOVED);
684 goto done;
686 error = check_same_branch(commit_id, head_ref, repo);
687 if (error)
688 goto done;
691 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
692 commit_id) != 0) {
693 error = got_worktree_set_base_commit_id(worktree, repo,
694 commit_id);
695 if (error)
696 goto done;
699 error = got_worktree_checkout_files(worktree, path, repo,
700 update_progress, &did_something, check_cancelled, NULL);
701 if (error != NULL)
702 goto done;
704 if (did_something)
705 printf("Updated to commit %s\n", commit_id_str);
706 else
707 printf("Already up-to-date\n");
708 done:
709 free(worktree_path);
710 free(path);
711 free(commit_id);
712 free(commit_id_str);
713 return error;
716 static const struct got_error *
717 print_patch(struct got_commit_object *commit, struct got_object_id *id,
718 int diff_context, struct got_repository *repo)
720 const struct got_error *err = NULL;
721 struct got_tree_object *tree1 = NULL, *tree2;
722 struct got_object_qid *qid;
723 char *id_str1 = NULL, *id_str2;
725 err = got_object_open_as_tree(&tree2, repo,
726 got_object_commit_get_tree_id(commit));
727 if (err)
728 return err;
730 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
731 if (qid != NULL) {
732 struct got_commit_object *pcommit;
734 err = got_object_open_as_commit(&pcommit, repo, qid->id);
735 if (err)
736 return err;
738 err = got_object_open_as_tree(&tree1, repo,
739 got_object_commit_get_tree_id(pcommit));
740 got_object_commit_close(pcommit);
741 if (err)
742 return err;
744 err = got_object_id_str(&id_str1, qid->id);
745 if (err)
746 return err;
749 err = got_object_id_str(&id_str2, id);
750 if (err)
751 goto done;
753 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
754 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
755 done:
756 if (tree1)
757 got_object_tree_close(tree1);
758 got_object_tree_close(tree2);
759 free(id_str1);
760 free(id_str2);
761 return err;
764 static char *
765 get_datestr(time_t *time, char *datebuf)
767 char *p, *s = ctime_r(time, datebuf);
768 p = strchr(s, '\n');
769 if (p)
770 *p = '\0';
771 return s;
774 static const struct got_error *
775 print_commit(struct got_commit_object *commit, struct got_object_id *id,
776 struct got_repository *repo, int show_patch, int diff_context,
777 struct got_reflist_head *refs)
779 const struct got_error *err = NULL;
780 char *id_str, *datestr, *logmsg0, *logmsg, *line;
781 char datebuf[26];
782 time_t committer_time;
783 const char *author, *committer;
784 char *refs_str = NULL;
785 struct got_reflist_entry *re;
787 SIMPLEQ_FOREACH(re, refs, entry) {
788 char *s;
789 const char *name;
790 if (got_object_id_cmp(re->id, id) != 0)
791 continue;
792 name = got_ref_get_name(re->ref);
793 if (strcmp(name, GOT_REF_HEAD) == 0)
794 continue;
795 if (strncmp(name, "refs/", 5) == 0)
796 name += 5;
797 if (strncmp(name, "got/", 4) == 0)
798 continue;
799 if (strncmp(name, "heads/", 6) == 0)
800 name += 6;
801 if (strncmp(name, "remotes/", 8) == 0)
802 name += 8;
803 s = refs_str;
804 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
805 name) == -1) {
806 err = got_error_from_errno("asprintf");
807 free(s);
808 break;
810 free(s);
812 err = got_object_id_str(&id_str, id);
813 if (err)
814 return err;
816 printf("-----------------------------------------------\n");
817 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
818 refs_str ? refs_str : "", refs_str ? ")" : "");
819 free(id_str);
820 id_str = NULL;
821 free(refs_str);
822 refs_str = NULL;
823 printf("from: %s\n", got_object_commit_get_author(commit));
824 committer_time = got_object_commit_get_committer_time(commit);
825 datestr = get_datestr(&committer_time, datebuf);
826 printf("date: %s UTC\n", datestr);
827 author = got_object_commit_get_author(commit);
828 committer = got_object_commit_get_committer(commit);
829 if (strcmp(author, committer) != 0)
830 printf("via: %s\n", committer);
831 if (got_object_commit_get_nparents(commit) > 1) {
832 const struct got_object_id_queue *parent_ids;
833 struct got_object_qid *qid;
834 int n = 1;
835 parent_ids = got_object_commit_get_parent_ids(commit);
836 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
837 err = got_object_id_str(&id_str, qid->id);
838 if (err)
839 return err;
840 printf("parent %d: %s\n", n++, id_str);
841 free(id_str);
845 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
846 if (logmsg0 == NULL)
847 return got_error_from_errno("strdup");
849 logmsg = logmsg0;
850 do {
851 line = strsep(&logmsg, "\n");
852 if (line)
853 printf(" %s\n", line);
854 } while (line);
855 free(logmsg0);
857 if (show_patch) {
858 err = print_patch(commit, id, diff_context, repo);
859 if (err == 0)
860 printf("\n");
863 if (fflush(stdout) != 0 && err == NULL)
864 err = got_error_from_errno("fflush");
865 return err;
868 static const struct got_error *
869 print_commits(struct got_object_id *root_id, struct got_repository *repo,
870 char *path, int show_patch, int diff_context, int limit,
871 int first_parent_traversal, struct got_reflist_head *refs)
873 const struct got_error *err;
874 struct got_commit_graph *graph;
876 err = got_commit_graph_open(&graph, root_id, path,
877 first_parent_traversal, repo);
878 if (err)
879 return err;
880 err = got_commit_graph_iter_start(graph, root_id, repo);
881 if (err)
882 goto done;
883 for (;;) {
884 struct got_commit_object *commit;
885 struct got_object_id *id;
887 if (sigint_received || sigpipe_received)
888 break;
890 err = got_commit_graph_iter_next(&id, graph);
891 if (err) {
892 if (err->code == GOT_ERR_ITER_COMPLETED) {
893 err = NULL;
894 break;
896 if (err->code != GOT_ERR_ITER_NEED_MORE)
897 break;
898 err = got_commit_graph_fetch_commits(graph, 1, repo);
899 if (err)
900 break;
901 else
902 continue;
904 if (id == NULL)
905 break;
907 err = got_object_open_as_commit(&commit, repo, id);
908 if (err)
909 break;
910 err = print_commit(commit, id, repo, show_patch, diff_context,
911 refs);
912 got_object_commit_close(commit);
913 if (err || (limit && --limit == 0))
914 break;
916 done:
917 got_commit_graph_close(graph);
918 return err;
921 __dead static void
922 usage_log(void)
924 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
925 "[-r repository-path] [path]\n", getprogname());
926 exit(1);
929 static const struct got_error *
930 cmd_log(int argc, char *argv[])
932 const struct got_error *error;
933 struct got_repository *repo = NULL;
934 struct got_worktree *worktree = NULL;
935 struct got_commit_object *commit = NULL;
936 struct got_object_id *id = NULL;
937 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
938 char *start_commit = NULL;
939 int diff_context = 3, ch;
940 int show_patch = 0, limit = 0, first_parent_traversal = 0;
941 const char *errstr;
942 struct got_reflist_head refs;
944 SIMPLEQ_INIT(&refs);
946 #ifndef PROFILE
947 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
948 NULL)
949 == -1)
950 err(1, "pledge");
951 #endif
953 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
954 switch (ch) {
955 case 'p':
956 show_patch = 1;
957 break;
958 case 'c':
959 start_commit = optarg;
960 break;
961 case 'C':
962 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
963 &errstr);
964 if (errstr != NULL)
965 err(1, "-C option %s", errstr);
966 break;
967 case 'l':
968 limit = strtonum(optarg, 1, INT_MAX, &errstr);
969 if (errstr != NULL)
970 err(1, "-l option %s", errstr);
971 break;
972 case 'f':
973 first_parent_traversal = 1;
974 break;
975 case 'r':
976 repo_path = realpath(optarg, NULL);
977 if (repo_path == NULL)
978 err(1, "-r option");
979 got_path_strip_trailing_slashes(repo_path);
980 break;
981 default:
982 usage_log();
983 /* NOTREACHED */
987 argc -= optind;
988 argv += optind;
990 cwd = getcwd(NULL, 0);
991 if (cwd == NULL) {
992 error = got_error_from_errno("getcwd");
993 goto done;
996 error = got_worktree_open(&worktree, cwd);
997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
998 goto done;
999 error = NULL;
1001 if (argc == 0) {
1002 path = strdup("");
1003 if (path == NULL) {
1004 error = got_error_from_errno("strdup");
1005 goto done;
1007 } else if (argc == 1) {
1008 if (worktree) {
1009 error = got_worktree_resolve_path(&path, worktree,
1010 argv[0]);
1011 if (error)
1012 goto done;
1013 } else {
1014 path = strdup(argv[0]);
1015 if (path == NULL) {
1016 error = got_error_from_errno("strdup");
1017 goto done;
1020 } else
1021 usage_log();
1023 if (repo_path == NULL) {
1024 repo_path = worktree ?
1025 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1027 if (repo_path == NULL) {
1028 error = got_error_from_errno("strdup");
1029 goto done;
1032 error = got_repo_open(&repo, repo_path);
1033 if (error != NULL)
1034 goto done;
1036 error = apply_unveil(got_repo_get_path(repo), 1,
1037 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1038 if (error)
1039 goto done;
1041 if (start_commit == NULL) {
1042 struct got_reference *head_ref;
1043 error = got_ref_open(&head_ref, repo,
1044 worktree ? got_worktree_get_head_ref_name(worktree)
1045 : GOT_REF_HEAD, 0);
1046 if (error != NULL)
1047 return error;
1048 error = got_ref_resolve(&id, repo, head_ref);
1049 got_ref_close(head_ref);
1050 if (error != NULL)
1051 return error;
1052 error = got_object_open_as_commit(&commit, repo, id);
1053 } else {
1054 struct got_reference *ref;
1055 error = got_ref_open(&ref, repo, start_commit, 0);
1056 if (error == NULL) {
1057 int obj_type;
1058 error = got_ref_resolve(&id, repo, ref);
1059 got_ref_close(ref);
1060 if (error != NULL)
1061 goto done;
1062 error = got_object_get_type(&obj_type, repo, id);
1063 if (error != NULL)
1064 goto done;
1065 if (obj_type == GOT_OBJ_TYPE_TAG) {
1066 struct got_tag_object *tag;
1067 error = got_object_open_as_tag(&tag, repo, id);
1068 if (error != NULL)
1069 goto done;
1070 if (got_object_tag_get_object_type(tag) !=
1071 GOT_OBJ_TYPE_COMMIT) {
1072 got_object_tag_close(tag);
1073 error = got_error(GOT_ERR_OBJ_TYPE);
1074 goto done;
1076 free(id);
1077 id = got_object_id_dup(
1078 got_object_tag_get_object_id(tag));
1079 if (id == NULL)
1080 error = got_error_from_errno(
1081 "got_object_id_dup");
1082 got_object_tag_close(tag);
1083 if (error)
1084 goto done;
1085 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1086 error = got_error(GOT_ERR_OBJ_TYPE);
1087 goto done;
1089 error = got_object_open_as_commit(&commit, repo, id);
1090 if (error != NULL)
1091 goto done;
1093 if (commit == NULL) {
1094 error = got_object_resolve_id_str(&id, repo,
1095 start_commit);
1096 if (error != NULL)
1097 return error;
1100 if (error != NULL)
1101 goto done;
1103 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1104 if (error != NULL)
1105 goto done;
1106 if (in_repo_path) {
1107 free(path);
1108 path = in_repo_path;
1111 error = got_ref_list(&refs, repo);
1112 if (error)
1113 goto done;
1115 error = print_commits(id, repo, path, show_patch,
1116 diff_context, limit, first_parent_traversal, &refs);
1117 done:
1118 free(path);
1119 free(repo_path);
1120 free(cwd);
1121 free(id);
1122 if (worktree)
1123 got_worktree_close(worktree);
1124 if (repo) {
1125 const struct got_error *repo_error;
1126 repo_error = got_repo_close(repo);
1127 if (error == NULL)
1128 error = repo_error;
1130 got_ref_list_free(&refs);
1131 return error;
1134 __dead static void
1135 usage_diff(void)
1137 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1138 "[object1 object2 | path]\n", getprogname());
1139 exit(1);
1142 struct print_diff_arg {
1143 struct got_repository *repo;
1144 struct got_worktree *worktree;
1145 int diff_context;
1146 const char *id_str;
1147 int header_shown;
1150 static const struct got_error *
1151 print_diff(void *arg, unsigned char status, const char *path,
1152 struct got_object_id *blob_id, struct got_object_id *commit_id)
1154 struct print_diff_arg *a = arg;
1155 const struct got_error *err = NULL;
1156 struct got_blob_object *blob1 = NULL;
1157 FILE *f2 = NULL;
1158 char *abspath = NULL;
1159 struct stat sb;
1161 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1162 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1163 return NULL;
1165 if (!a->header_shown) {
1166 printf("diff %s %s\n", a->id_str,
1167 got_worktree_get_root_path(a->worktree));
1168 a->header_shown = 1;
1171 if (status != GOT_STATUS_ADD) {
1172 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1173 if (err)
1174 goto done;
1178 if (status != GOT_STATUS_DELETE) {
1179 if (asprintf(&abspath, "%s/%s",
1180 got_worktree_get_root_path(a->worktree), path) == -1) {
1181 err = got_error_from_errno("asprintf");
1182 goto done;
1185 f2 = fopen(abspath, "r");
1186 if (f2 == NULL) {
1187 err = got_error_from_errno2("fopen", abspath);
1188 goto done;
1190 if (lstat(abspath, &sb) == -1) {
1191 err = got_error_from_errno2("lstat", abspath);
1192 goto done;
1194 } else
1195 sb.st_size = 0;
1197 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1198 stdout);
1199 done:
1200 if (blob1)
1201 got_object_blob_close(blob1);
1202 if (f2 && fclose(f2) != 0 && err == NULL)
1203 err = got_error_from_errno("fclose");
1204 free(abspath);
1205 return err;
1208 static const struct got_error *
1209 cmd_diff(int argc, char *argv[])
1211 const struct got_error *error;
1212 struct got_repository *repo = NULL;
1213 struct got_worktree *worktree = NULL;
1214 char *cwd = NULL, *repo_path = NULL;
1215 struct got_object_id *id1 = NULL, *id2 = NULL;
1216 char *id_str1 = NULL, *id_str2 = NULL;
1217 int type1, type2;
1218 int diff_context = 3, ch;
1219 const char *errstr;
1220 char *path = NULL;
1222 #ifndef PROFILE
1223 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1224 NULL) == -1)
1225 err(1, "pledge");
1226 #endif
1228 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1229 switch (ch) {
1230 case 'C':
1231 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1232 if (errstr != NULL)
1233 err(1, "-C option %s", errstr);
1234 break;
1235 case 'r':
1236 repo_path = realpath(optarg, NULL);
1237 if (repo_path == NULL)
1238 err(1, "-r option");
1239 got_path_strip_trailing_slashes(repo_path);
1240 break;
1241 default:
1242 usage_diff();
1243 /* NOTREACHED */
1247 argc -= optind;
1248 argv += optind;
1250 cwd = getcwd(NULL, 0);
1251 if (cwd == NULL) {
1252 error = got_error_from_errno("getcwd");
1253 goto done;
1255 error = got_worktree_open(&worktree, cwd);
1256 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1257 goto done;
1258 if (argc <= 1) {
1259 if (worktree == NULL) {
1260 error = got_error(GOT_ERR_NOT_WORKTREE);
1261 goto done;
1263 if (repo_path)
1264 errx(1,
1265 "-r option can't be used when diffing a work tree");
1266 repo_path = strdup(got_worktree_get_repo_path(worktree));
1267 if (repo_path == NULL) {
1268 error = got_error_from_errno("strdup");
1269 goto done;
1271 if (argc == 1) {
1272 error = got_worktree_resolve_path(&path, worktree,
1273 argv[0]);
1274 if (error)
1275 goto done;
1276 } else {
1277 path = strdup("");
1278 if (path == NULL) {
1279 error = got_error_from_errno("strdup");
1280 goto done;
1283 } else if (argc == 2) {
1284 id_str1 = argv[0];
1285 id_str2 = argv[1];
1286 } else
1287 usage_diff();
1289 if (repo_path == NULL) {
1290 repo_path = getcwd(NULL, 0);
1291 if (repo_path == NULL)
1292 return got_error_from_errno("getcwd");
1295 error = got_repo_open(&repo, repo_path);
1296 free(repo_path);
1297 if (error != NULL)
1298 goto done;
1300 error = apply_unveil(got_repo_get_path(repo), 1,
1301 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1302 if (error)
1303 goto done;
1305 if (worktree) {
1306 struct print_diff_arg arg;
1307 char *id_str;
1308 error = got_object_id_str(&id_str,
1309 got_worktree_get_base_commit_id(worktree));
1310 if (error)
1311 goto done;
1312 arg.repo = repo;
1313 arg.worktree = worktree;
1314 arg.diff_context = diff_context;
1315 arg.id_str = id_str;
1316 arg.header_shown = 0;
1318 error = got_worktree_status(worktree, path, repo, print_diff,
1319 &arg, check_cancelled, NULL);
1320 free(id_str);
1321 goto done;
1324 error = got_object_resolve_id_str(&id1, repo, id_str1);
1325 if (error)
1326 goto done;
1328 error = got_object_resolve_id_str(&id2, repo, id_str2);
1329 if (error)
1330 goto done;
1332 error = got_object_get_type(&type1, repo, id1);
1333 if (error)
1334 goto done;
1336 error = got_object_get_type(&type2, repo, id2);
1337 if (error)
1338 goto done;
1340 if (type1 != type2) {
1341 error = got_error(GOT_ERR_OBJ_TYPE);
1342 goto done;
1345 switch (type1) {
1346 case GOT_OBJ_TYPE_BLOB:
1347 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1348 diff_context, repo, stdout);
1349 break;
1350 case GOT_OBJ_TYPE_TREE:
1351 error = got_diff_objects_as_trees(id1, id2, "", "",
1352 diff_context, repo, stdout);
1353 break;
1354 case GOT_OBJ_TYPE_COMMIT:
1355 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1356 id_str2);
1357 error = got_diff_objects_as_commits(id1, id2, diff_context,
1358 repo, stdout);
1359 break;
1360 default:
1361 error = got_error(GOT_ERR_OBJ_TYPE);
1364 done:
1365 free(id1);
1366 free(id2);
1367 free(path);
1368 if (worktree)
1369 got_worktree_close(worktree);
1370 if (repo) {
1371 const struct got_error *repo_error;
1372 repo_error = got_repo_close(repo);
1373 if (error == NULL)
1374 error = repo_error;
1376 return error;
1379 __dead static void
1380 usage_blame(void)
1382 fprintf(stderr,
1383 "usage: %s blame [-c commit] [-r repository-path] path\n",
1384 getprogname());
1385 exit(1);
1388 static const struct got_error *
1389 cmd_blame(int argc, char *argv[])
1391 const struct got_error *error;
1392 struct got_repository *repo = NULL;
1393 struct got_worktree *worktree = NULL;
1394 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1395 struct got_object_id *commit_id = NULL;
1396 char *commit_id_str = NULL;
1397 int ch;
1399 #ifndef PROFILE
1400 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1401 NULL) == -1)
1402 err(1, "pledge");
1403 #endif
1405 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1406 switch (ch) {
1407 case 'c':
1408 commit_id_str = optarg;
1409 break;
1410 case 'r':
1411 repo_path = realpath(optarg, NULL);
1412 if (repo_path == NULL)
1413 err(1, "-r option");
1414 got_path_strip_trailing_slashes(repo_path);
1415 break;
1416 default:
1417 usage_blame();
1418 /* NOTREACHED */
1422 argc -= optind;
1423 argv += optind;
1425 if (argc == 1)
1426 path = argv[0];
1427 else
1428 usage_blame();
1430 cwd = getcwd(NULL, 0);
1431 if (cwd == NULL) {
1432 error = got_error_from_errno("getcwd");
1433 goto done;
1435 if (repo_path == NULL) {
1436 error = got_worktree_open(&worktree, cwd);
1437 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1438 goto done;
1439 else
1440 error = NULL;
1441 if (worktree) {
1442 repo_path =
1443 strdup(got_worktree_get_repo_path(worktree));
1444 if (repo_path == NULL)
1445 error = got_error_from_errno("strdup");
1446 if (error)
1447 goto done;
1448 } else {
1449 repo_path = strdup(cwd);
1450 if (repo_path == NULL) {
1451 error = got_error_from_errno("strdup");
1452 goto done;
1457 error = got_repo_open(&repo, repo_path);
1458 if (error != NULL)
1459 goto done;
1461 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1462 if (error)
1463 goto done;
1465 if (worktree) {
1466 const char *prefix = got_worktree_get_path_prefix(worktree);
1467 char *p, *worktree_subdir = cwd +
1468 strlen(got_worktree_get_root_path(worktree));
1469 if (asprintf(&p, "%s%s%s%s%s",
1470 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1471 worktree_subdir, worktree_subdir[0] ? "/" : "",
1472 path) == -1) {
1473 error = got_error_from_errno("asprintf");
1474 goto done;
1476 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1477 free(p);
1478 } else {
1479 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1481 if (error)
1482 goto done;
1484 if (commit_id_str == NULL) {
1485 struct got_reference *head_ref;
1486 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1487 if (error != NULL)
1488 goto done;
1489 error = got_ref_resolve(&commit_id, repo, head_ref);
1490 got_ref_close(head_ref);
1491 if (error != NULL)
1492 goto done;
1493 } else {
1494 error = got_object_resolve_id_str(&commit_id, repo,
1495 commit_id_str);
1496 if (error != NULL)
1497 goto done;
1500 error = got_blame(in_repo_path, commit_id, repo, stdout);
1501 done:
1502 free(in_repo_path);
1503 free(repo_path);
1504 free(cwd);
1505 free(commit_id);
1506 if (worktree)
1507 got_worktree_close(worktree);
1508 if (repo) {
1509 const struct got_error *repo_error;
1510 repo_error = got_repo_close(repo);
1511 if (error == NULL)
1512 error = repo_error;
1514 return error;
1517 __dead static void
1518 usage_tree(void)
1520 fprintf(stderr,
1521 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1522 getprogname());
1523 exit(1);
1526 static void
1527 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1528 const char *root_path)
1530 int is_root_path = (strcmp(path, root_path) == 0);
1532 path += strlen(root_path);
1533 while (path[0] == '/')
1534 path++;
1536 printf("%s%s%s%s%s\n", id ? id : "", path,
1537 is_root_path ? "" : "/", te->name,
1538 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1541 static const struct got_error *
1542 print_tree(const char *path, struct got_object_id *commit_id,
1543 int show_ids, int recurse, const char *root_path,
1544 struct got_repository *repo)
1546 const struct got_error *err = NULL;
1547 struct got_object_id *tree_id = NULL;
1548 struct got_tree_object *tree = NULL;
1549 const struct got_tree_entries *entries;
1550 struct got_tree_entry *te;
1552 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1553 if (err)
1554 goto done;
1556 err = got_object_open_as_tree(&tree, repo, tree_id);
1557 if (err)
1558 goto done;
1559 entries = got_object_tree_get_entries(tree);
1560 te = SIMPLEQ_FIRST(&entries->head);
1561 while (te) {
1562 char *id = NULL;
1564 if (sigint_received || sigpipe_received)
1565 break;
1567 if (show_ids) {
1568 char *id_str;
1569 err = got_object_id_str(&id_str, te->id);
1570 if (err)
1571 goto done;
1572 if (asprintf(&id, "%s ", id_str) == -1) {
1573 err = got_error_from_errno("asprintf");
1574 free(id_str);
1575 goto done;
1577 free(id_str);
1579 print_entry(te, id, path, root_path);
1580 free(id);
1582 if (recurse && S_ISDIR(te->mode)) {
1583 char *child_path;
1584 if (asprintf(&child_path, "%s%s%s", path,
1585 path[0] == '/' && path[1] == '\0' ? "" : "/",
1586 te->name) == -1) {
1587 err = got_error_from_errno("asprintf");
1588 goto done;
1590 err = print_tree(child_path, commit_id, show_ids, 1,
1591 root_path, repo);
1592 free(child_path);
1593 if (err)
1594 goto done;
1597 te = SIMPLEQ_NEXT(te, entry);
1599 done:
1600 if (tree)
1601 got_object_tree_close(tree);
1602 free(tree_id);
1603 return err;
1606 static const struct got_error *
1607 cmd_tree(int argc, char *argv[])
1609 const struct got_error *error;
1610 struct got_repository *repo = NULL;
1611 struct got_worktree *worktree = NULL;
1612 const char *path;
1613 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1614 struct got_object_id *commit_id = NULL;
1615 char *commit_id_str = NULL;
1616 int show_ids = 0, recurse = 0;
1617 int ch;
1619 #ifndef PROFILE
1620 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1621 NULL) == -1)
1622 err(1, "pledge");
1623 #endif
1625 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1626 switch (ch) {
1627 case 'c':
1628 commit_id_str = optarg;
1629 break;
1630 case 'r':
1631 repo_path = realpath(optarg, NULL);
1632 if (repo_path == NULL)
1633 err(1, "-r option");
1634 got_path_strip_trailing_slashes(repo_path);
1635 break;
1636 case 'i':
1637 show_ids = 1;
1638 break;
1639 case 'R':
1640 recurse = 1;
1641 break;
1642 default:
1643 usage_tree();
1644 /* NOTREACHED */
1648 argc -= optind;
1649 argv += optind;
1651 if (argc == 1)
1652 path = argv[0];
1653 else if (argc > 1)
1654 usage_tree();
1655 else
1656 path = NULL;
1658 cwd = getcwd(NULL, 0);
1659 if (cwd == NULL) {
1660 error = got_error_from_errno("getcwd");
1661 goto done;
1663 if (repo_path == NULL) {
1664 error = got_worktree_open(&worktree, cwd);
1665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1666 goto done;
1667 else
1668 error = NULL;
1669 if (worktree) {
1670 repo_path =
1671 strdup(got_worktree_get_repo_path(worktree));
1672 if (repo_path == NULL)
1673 error = got_error_from_errno("strdup");
1674 if (error)
1675 goto done;
1676 } else {
1677 repo_path = strdup(cwd);
1678 if (repo_path == NULL) {
1679 error = got_error_from_errno("strdup");
1680 goto done;
1685 error = got_repo_open(&repo, repo_path);
1686 if (error != NULL)
1687 goto done;
1689 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1690 if (error)
1691 goto done;
1693 if (path == NULL) {
1694 if (worktree) {
1695 char *p, *worktree_subdir = cwd +
1696 strlen(got_worktree_get_root_path(worktree));
1697 if (asprintf(&p, "%s/%s",
1698 got_worktree_get_path_prefix(worktree),
1699 worktree_subdir) == -1) {
1700 error = got_error_from_errno("asprintf");
1701 goto done;
1703 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1704 free(p);
1705 if (error)
1706 goto done;
1707 } else
1708 path = "/";
1710 if (in_repo_path == NULL) {
1711 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1712 if (error != NULL)
1713 goto done;
1716 if (commit_id_str == NULL) {
1717 struct got_reference *head_ref;
1718 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1719 if (error != NULL)
1720 goto done;
1721 error = got_ref_resolve(&commit_id, repo, head_ref);
1722 got_ref_close(head_ref);
1723 if (error != NULL)
1724 goto done;
1725 } else {
1726 error = got_object_resolve_id_str(&commit_id, repo,
1727 commit_id_str);
1728 if (error != NULL)
1729 goto done;
1732 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1733 in_repo_path, repo);
1734 done:
1735 free(in_repo_path);
1736 free(repo_path);
1737 free(cwd);
1738 free(commit_id);
1739 if (worktree)
1740 got_worktree_close(worktree);
1741 if (repo) {
1742 const struct got_error *repo_error;
1743 repo_error = got_repo_close(repo);
1744 if (error == NULL)
1745 error = repo_error;
1747 return error;
1750 __dead static void
1751 usage_status(void)
1753 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1754 exit(1);
1757 static const struct got_error *
1758 print_status(void *arg, unsigned char status, const char *path,
1759 struct got_object_id *blob_id, struct got_object_id *commit_id)
1761 printf("%c %s\n", status, path);
1762 return NULL;
1765 static const struct got_error *
1766 cmd_status(int argc, char *argv[])
1768 const struct got_error *error = NULL;
1769 struct got_repository *repo = NULL;
1770 struct got_worktree *worktree = NULL;
1771 char *cwd = NULL, *path = NULL;
1772 int ch;
1774 while ((ch = getopt(argc, argv, "")) != -1) {
1775 switch (ch) {
1776 default:
1777 usage_status();
1778 /* NOTREACHED */
1782 argc -= optind;
1783 argv += optind;
1785 #ifndef PROFILE
1786 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1787 NULL) == -1)
1788 err(1, "pledge");
1789 #endif
1790 cwd = getcwd(NULL, 0);
1791 if (cwd == NULL) {
1792 error = got_error_from_errno("getcwd");
1793 goto done;
1796 error = got_worktree_open(&worktree, cwd);
1797 if (error != NULL)
1798 goto done;
1800 if (argc == 0) {
1801 path = strdup("");
1802 if (path == NULL) {
1803 error = got_error_from_errno("strdup");
1804 goto done;
1806 } else if (argc == 1) {
1807 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1808 if (error)
1809 goto done;
1810 } else
1811 usage_status();
1813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1814 if (error != NULL)
1815 goto done;
1817 error = apply_unveil(got_repo_get_path(repo), 1,
1818 got_worktree_get_root_path(worktree), 0);
1819 if (error)
1820 goto done;
1822 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1823 check_cancelled, NULL);
1824 done:
1825 free(cwd);
1826 free(path);
1827 return error;
1830 __dead static void
1831 usage_ref(void)
1833 fprintf(stderr,
1834 "usage: %s ref [-r repository] -l | -d name | name target\n",
1835 getprogname());
1836 exit(1);
1839 static const struct got_error *
1840 list_refs(struct got_repository *repo)
1842 static const struct got_error *err = NULL;
1843 struct got_reflist_head refs;
1844 struct got_reflist_entry *re;
1846 SIMPLEQ_INIT(&refs);
1847 err = got_ref_list(&refs, repo);
1848 if (err)
1849 return err;
1851 SIMPLEQ_FOREACH(re, &refs, entry) {
1852 char *refstr;
1853 refstr = got_ref_to_str(re->ref);
1854 if (refstr == NULL)
1855 return got_error_from_errno("got_ref_to_str");
1856 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1857 free(refstr);
1860 got_ref_list_free(&refs);
1861 return NULL;
1864 static const struct got_error *
1865 delete_ref(struct got_repository *repo, const char *refname)
1867 const struct got_error *err = NULL;
1868 struct got_reference *ref;
1870 err = got_ref_open(&ref, repo, refname, 0);
1871 if (err)
1872 return err;
1874 err = got_ref_delete(ref, repo);
1875 got_ref_close(ref);
1876 return err;
1879 static const struct got_error *
1880 add_ref(struct got_repository *repo, const char *refname, const char *target)
1882 const struct got_error *err = NULL;
1883 struct got_object_id *id;
1884 struct got_reference *ref = NULL;
1886 err = got_object_resolve_id_str(&id, repo, target);
1887 if (err) {
1888 struct got_reference *target_ref;
1890 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1891 return err;
1892 err = got_ref_open(&target_ref, repo, target, 0);
1893 if (err)
1894 return err;
1895 err = got_ref_resolve(&id, repo, target_ref);
1896 got_ref_close(target_ref);
1897 if (err)
1898 return err;
1901 err = got_ref_alloc(&ref, refname, id);
1902 if (err)
1903 goto done;
1905 err = got_ref_write(ref, repo);
1906 done:
1907 if (ref)
1908 got_ref_close(ref);
1909 free(id);
1910 return err;
1913 static const struct got_error *
1914 cmd_ref(int argc, char *argv[])
1916 const struct got_error *error = NULL;
1917 struct got_repository *repo = NULL;
1918 struct got_worktree *worktree = NULL;
1919 char *cwd = NULL, *repo_path = NULL;
1920 int ch, do_list = 0;
1921 const char *delref = NULL;
1923 /* TODO: Add -s option for adding symbolic references. */
1924 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1925 switch (ch) {
1926 case 'd':
1927 delref = optarg;
1928 break;
1929 case 'r':
1930 repo_path = realpath(optarg, NULL);
1931 if (repo_path == NULL)
1932 err(1, "-r option");
1933 got_path_strip_trailing_slashes(repo_path);
1934 break;
1935 case 'l':
1936 do_list = 1;
1937 break;
1938 default:
1939 usage_ref();
1940 /* NOTREACHED */
1944 if (do_list && delref)
1945 errx(1, "-l and -d options are mutually exclusive\n");
1947 argc -= optind;
1948 argv += optind;
1950 if (do_list || delref) {
1951 if (argc > 0)
1952 usage_ref();
1953 } else if (argc != 2)
1954 usage_ref();
1956 #ifndef PROFILE
1957 if (do_list) {
1958 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1959 NULL) == -1)
1960 err(1, "pledge");
1961 } else {
1962 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1963 "sendfd unveil", NULL) == -1)
1964 err(1, "pledge");
1966 #endif
1967 cwd = getcwd(NULL, 0);
1968 if (cwd == NULL) {
1969 error = got_error_from_errno("getcwd");
1970 goto done;
1973 if (repo_path == NULL) {
1974 error = got_worktree_open(&worktree, cwd);
1975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1976 goto done;
1977 else
1978 error = NULL;
1979 if (worktree) {
1980 repo_path =
1981 strdup(got_worktree_get_repo_path(worktree));
1982 if (repo_path == NULL)
1983 error = got_error_from_errno("strdup");
1984 if (error)
1985 goto done;
1986 } else {
1987 repo_path = strdup(cwd);
1988 if (repo_path == NULL) {
1989 error = got_error_from_errno("strdup");
1990 goto done;
1995 error = got_repo_open(&repo, repo_path);
1996 if (error != NULL)
1997 goto done;
1999 error = apply_unveil(got_repo_get_path(repo), do_list,
2000 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2001 if (error)
2002 goto done;
2004 if (do_list)
2005 error = list_refs(repo);
2006 else if (delref)
2007 error = delete_ref(repo, delref);
2008 else
2009 error = add_ref(repo, argv[0], argv[1]);
2010 done:
2011 if (repo)
2012 got_repo_close(repo);
2013 if (worktree)
2014 got_worktree_close(worktree);
2015 free(cwd);
2016 free(repo_path);
2017 return error;
2020 __dead static void
2021 usage_add(void)
2023 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2024 exit(1);
2027 static const struct got_error *
2028 cmd_add(int argc, char *argv[])
2030 const struct got_error *error = NULL;
2031 struct got_repository *repo = NULL;
2032 struct got_worktree *worktree = NULL;
2033 char *cwd = NULL;
2034 struct got_pathlist_head paths;
2035 struct got_pathlist_entry *pe;
2036 int ch, x;
2038 TAILQ_INIT(&paths);
2040 while ((ch = getopt(argc, argv, "")) != -1) {
2041 switch (ch) {
2042 default:
2043 usage_add();
2044 /* NOTREACHED */
2048 argc -= optind;
2049 argv += optind;
2051 if (argc < 1)
2052 usage_add();
2054 /* make sure each file exists before doing anything halfway */
2055 for (x = 0; x < argc; x++) {
2056 char *path = realpath(argv[x], NULL);
2057 if (path == NULL) {
2058 error = got_error_from_errno2("realpath", argv[x]);
2059 goto done;
2061 free(path);
2064 cwd = getcwd(NULL, 0);
2065 if (cwd == NULL) {
2066 error = got_error_from_errno("getcwd");
2067 goto done;
2070 error = got_worktree_open(&worktree, cwd);
2071 if (error)
2072 goto done;
2074 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2075 if (error != NULL)
2076 goto done;
2078 error = apply_unveil(got_repo_get_path(repo), 1,
2079 got_worktree_get_root_path(worktree), 0);
2080 if (error)
2081 goto done;
2083 for (x = 0; x < argc; x++) {
2084 char *path = realpath(argv[x], NULL);
2085 if (path == NULL) {
2086 error = got_error_from_errno2("realpath", argv[x]);
2087 goto done;
2090 got_path_strip_trailing_slashes(path);
2091 error = got_pathlist_insert(&pe, &paths, path, NULL);
2092 if (error) {
2093 free(path);
2094 goto done;
2097 error = got_worktree_schedule_add(worktree, &paths, print_status,
2098 NULL, repo);
2099 done:
2100 if (repo)
2101 got_repo_close(repo);
2102 if (worktree)
2103 got_worktree_close(worktree);
2104 TAILQ_FOREACH(pe, &paths, entry)
2105 free((char *)pe->path);
2106 got_pathlist_free(&paths);
2107 free(cwd);
2108 return error;
2111 __dead static void
2112 usage_rm(void)
2114 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2115 exit(1);
2118 static const struct got_error *
2119 cmd_rm(int argc, char *argv[])
2121 const struct got_error *error = NULL;
2122 struct got_worktree *worktree = NULL;
2123 struct got_repository *repo = NULL;
2124 char *cwd = NULL, *path = NULL;
2125 int ch, delete_local_mods = 0;
2127 while ((ch = getopt(argc, argv, "f")) != -1) {
2128 switch (ch) {
2129 case 'f':
2130 delete_local_mods = 1;
2131 break;
2132 default:
2133 usage_add();
2134 /* NOTREACHED */
2138 argc -= optind;
2139 argv += optind;
2141 if (argc != 1)
2142 usage_rm();
2144 path = realpath(argv[0], NULL);
2145 if (path == NULL) {
2146 error = got_error_from_errno2("realpath", argv[0]);
2147 goto done;
2149 got_path_strip_trailing_slashes(path);
2151 cwd = getcwd(NULL, 0);
2152 if (cwd == NULL) {
2153 error = got_error_from_errno("getcwd");
2154 goto done;
2156 error = got_worktree_open(&worktree, cwd);
2157 if (error)
2158 goto done;
2160 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2161 if (error)
2162 goto done;
2164 error = apply_unveil(got_repo_get_path(repo), 1,
2165 got_worktree_get_root_path(worktree), 0);
2166 if (error)
2167 goto done;
2169 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2170 print_status, NULL, repo);
2171 if (error)
2172 goto done;
2173 done:
2174 if (repo)
2175 got_repo_close(repo);
2176 if (worktree)
2177 got_worktree_close(worktree);
2178 free(path);
2179 free(cwd);
2180 return error;
2183 __dead static void
2184 usage_revert(void)
2186 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2187 exit(1);
2190 static void
2191 revert_progress(void *arg, unsigned char status, const char *path)
2193 while (path[0] == '/')
2194 path++;
2195 printf("%c %s\n", status, path);
2198 static const struct got_error *
2199 cmd_revert(int argc, char *argv[])
2201 const struct got_error *error = NULL;
2202 struct got_worktree *worktree = NULL;
2203 struct got_repository *repo = NULL;
2204 char *cwd = NULL, *path = NULL;
2205 int ch;
2207 while ((ch = getopt(argc, argv, "")) != -1) {
2208 switch (ch) {
2209 default:
2210 usage_revert();
2211 /* NOTREACHED */
2215 argc -= optind;
2216 argv += optind;
2218 if (argc != 1)
2219 usage_revert();
2221 path = realpath(argv[0], NULL);
2222 if (path == NULL) {
2223 error = got_error_from_errno2("realpath", argv[0]);
2224 goto done;
2226 got_path_strip_trailing_slashes(path);
2228 cwd = getcwd(NULL, 0);
2229 if (cwd == NULL) {
2230 error = got_error_from_errno("getcwd");
2231 goto done;
2233 error = got_worktree_open(&worktree, cwd);
2234 if (error)
2235 goto done;
2237 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2238 if (error != NULL)
2239 goto done;
2241 error = apply_unveil(got_repo_get_path(repo), 1,
2242 got_worktree_get_root_path(worktree), 0);
2243 if (error)
2244 goto done;
2246 error = got_worktree_revert(worktree, path,
2247 revert_progress, NULL, repo);
2248 if (error)
2249 goto done;
2250 done:
2251 if (repo)
2252 got_repo_close(repo);
2253 if (worktree)
2254 got_worktree_close(worktree);
2255 free(path);
2256 free(cwd);
2257 return error;
2260 __dead static void
2261 usage_commit(void)
2263 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2264 exit(1);
2267 int
2268 spawn_editor(const char *editor, const char *file)
2270 pid_t pid;
2271 sig_t sighup, sigint, sigquit;
2272 int st = -1;
2274 sighup = signal(SIGHUP, SIG_IGN);
2275 sigint = signal(SIGINT, SIG_IGN);
2276 sigquit = signal(SIGQUIT, SIG_IGN);
2278 switch (pid = fork()) {
2279 case -1:
2280 goto doneediting;
2281 case 0:
2282 execl(editor, editor, file, (char *)NULL);
2283 _exit(127);
2286 while (waitpid(pid, &st, 0) == -1)
2287 if (errno != EINTR)
2288 break;
2290 doneediting:
2291 (void)signal(SIGHUP, sighup);
2292 (void)signal(SIGINT, sigint);
2293 (void)signal(SIGQUIT, sigquit);
2295 if (!WIFEXITED(st)) {
2296 errno = EINTR;
2297 return -1;
2300 return WEXITSTATUS(st);
2303 struct collect_commit_logmsg_arg {
2304 const char *cmdline_log;
2305 const char *editor;
2306 const char *worktree_path;
2307 const char *repo_path;
2308 char *logmsg_path;
2312 static const struct got_error *
2313 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2314 void *arg)
2316 const char *initial_content = "\n# changes to be committed:\n";
2317 struct got_pathlist_entry *pe;
2318 const struct got_error *err = NULL;
2319 char *template = NULL;
2320 struct collect_commit_logmsg_arg *a = arg;
2321 char buf[1024];
2322 struct stat st, st2;
2323 FILE *fp;
2324 size_t len;
2325 int fd, content_changed = 0;
2327 /* if a message was specified on the command line, just use it */
2328 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2329 len = strlen(a->cmdline_log) + 1;
2330 *logmsg = malloc(len + 1);
2331 if (*logmsg == NULL)
2332 return got_error_from_errno("malloc");
2333 strlcpy(*logmsg, a->cmdline_log, len);
2334 return NULL;
2337 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2338 return got_error_from_errno("asprintf");
2340 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2341 if (err)
2342 goto done;
2344 dprintf(fd, initial_content);
2346 TAILQ_FOREACH(pe, commitable_paths, entry) {
2347 struct got_commitable *ct = pe->data;
2348 dprintf(fd, "# %c %s\n",
2349 got_commitable_get_status(ct),
2350 got_commitable_get_path(ct));
2352 close(fd);
2354 if (stat(a->logmsg_path, &st) == -1) {
2355 err = got_error_from_errno2("stat", a->logmsg_path);
2356 goto done;
2359 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2360 err = got_error_from_errno("failed spawning editor");
2361 goto done;
2364 if (stat(a->logmsg_path, &st2) == -1) {
2365 err = got_error_from_errno("stat");
2366 goto done;
2369 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2370 unlink(a->logmsg_path);
2371 free(a->logmsg_path);
2372 a->logmsg_path = NULL;
2373 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2374 "no changes made to commit message, aborting");
2375 goto done;
2378 *logmsg = malloc(st2.st_size + 1);
2379 if (*logmsg == NULL) {
2380 err = got_error_from_errno("malloc");
2381 goto done;
2383 (*logmsg)[0] = '\0';
2384 len = 0;
2386 fp = fopen(a->logmsg_path, "r");
2387 if (fp == NULL) {
2388 err = got_error_from_errno("fopen");
2389 goto done;
2391 while (fgets(buf, sizeof(buf), fp) != NULL) {
2392 if (!content_changed && strcmp(buf, initial_content) != 0)
2393 content_changed = 1;
2394 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2395 continue; /* remove comments and leading empty lines */
2396 len = strlcat(*logmsg, buf, st2.st_size);
2398 fclose(fp);
2400 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2401 (*logmsg)[len - 1] = '\0';
2402 len--;
2405 if (len == 0 || !content_changed) {
2406 unlink(a->logmsg_path);
2407 free(a->logmsg_path);
2408 a->logmsg_path = NULL;
2409 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2410 "commit message cannot be empty, aborting");
2411 goto done;
2413 done:
2414 free(template);
2416 /* Editor is done; we can now apply unveil(2) */
2417 if (err == NULL)
2418 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2419 return err;
2422 static const struct got_error *
2423 cmd_commit(int argc, char *argv[])
2425 const struct got_error *error = NULL;
2426 struct got_worktree *worktree = NULL;
2427 struct got_repository *repo = NULL;
2428 char *cwd = NULL, *path = NULL, *id_str = NULL;
2429 struct got_object_id *id = NULL;
2430 const char *logmsg = NULL;
2431 const char *got_author = getenv("GOT_AUTHOR");
2432 struct collect_commit_logmsg_arg cl_arg;
2433 char *editor = NULL;
2434 int ch;
2436 while ((ch = getopt(argc, argv, "m:")) != -1) {
2437 switch (ch) {
2438 case 'm':
2439 logmsg = optarg;
2440 break;
2441 default:
2442 usage_commit();
2443 /* NOTREACHED */
2447 argc -= optind;
2448 argv += optind;
2450 if (argc == 1) {
2451 path = realpath(argv[0], NULL);
2452 if (path == NULL) {
2453 error = got_error_from_errno2("realpath", argv[0]);
2454 goto done;
2456 got_path_strip_trailing_slashes(path);
2457 } else if (argc != 0)
2458 usage_commit();
2460 if (got_author == NULL) {
2461 /* TODO: Look current user up in password database */
2462 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2463 goto done;
2466 cwd = getcwd(NULL, 0);
2467 if (cwd == NULL) {
2468 error = got_error_from_errno("getcwd");
2469 goto done;
2471 error = got_worktree_open(&worktree, cwd);
2472 if (error)
2473 goto done;
2475 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2476 if (error != NULL)
2477 goto done;
2480 * unveil(2) traverses exec(2); if an editor is used we have
2481 * to apply unveil after the log message has been written.
2483 if (logmsg == NULL || strlen(logmsg) == 0)
2484 error = get_editor(&editor);
2485 else
2486 error = apply_unveil(got_repo_get_path(repo), 0,
2487 got_worktree_get_root_path(worktree), 0);
2488 if (error)
2489 goto done;
2491 cl_arg.editor = editor;
2492 cl_arg.cmdline_log = logmsg;
2493 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2494 cl_arg.repo_path = got_repo_get_path(repo);
2495 cl_arg.logmsg_path = NULL;
2496 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2497 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2498 if (error) {
2499 if (cl_arg.logmsg_path)
2500 fprintf(stderr, "%s: log message preserved in %s\n",
2501 getprogname(), cl_arg.logmsg_path);
2502 goto done;
2505 if (cl_arg.logmsg_path)
2506 unlink(cl_arg.logmsg_path);
2508 error = got_object_id_str(&id_str, id);
2509 if (error)
2510 goto done;
2511 printf("created commit %s\n", id_str);
2512 done:
2513 if (repo)
2514 got_repo_close(repo);
2515 if (worktree)
2516 got_worktree_close(worktree);
2517 free(path);
2518 free(cwd);
2519 free(id_str);
2520 free(editor);
2521 return error;