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;
229 static char err_msg[MAXPATHLEN + 36];
231 if (create_worktree) {
232 /* Pre-create work tree path to avoid unveiling its parents. */
233 err = got_path_mkdir(worktree_path);
235 if (errno == EEXIST) {
236 if (got_path_dir_is_empty(worktree_path)) {
237 errno = 0;
238 err = NULL;
239 } else {
240 snprintf(err_msg, sizeof(err_msg),
241 "%s: directory exists but is not empty",
242 worktree_path);
243 err = got_error_msg(GOT_ERR_BAD_PATH,
244 err_msg);
248 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
249 return err;
252 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
253 return got_error_from_errno2("unveil", repo_path);
255 if (worktree_path && unveil(worktree_path, "rwc") != 0)
256 return got_error_from_errno2("unveil", worktree_path);
258 if (unveil("/tmp", "rwc") != 0)
259 return got_error_from_errno2("unveil", "/tmp");
261 err = got_privsep_unveil_exec_helpers();
262 if (err != NULL)
263 return err;
265 if (unveil(NULL, NULL) != 0)
266 return got_error_from_errno("unveil");
268 return NULL;
271 __dead static void
272 usage_checkout(void)
274 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
275 "[-p prefix] repository-path [worktree-path]\n", getprogname());
276 exit(1);
279 static void
280 checkout_progress(void *arg, unsigned char status, const char *path)
282 char *worktree_path = arg;
284 while (path[0] == '/')
285 path++;
287 printf("%c %s/%s\n", status, worktree_path, path);
290 static const struct got_error *
291 check_cancelled(void *arg)
293 if (sigint_received || sigpipe_received)
294 return got_error(GOT_ERR_CANCELLED);
295 return NULL;
298 static const struct got_error *
299 check_linear_ancestry(struct got_object_id *commit_id,
300 struct got_object_id *base_commit_id, struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_object_id *yca_id;
305 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
306 commit_id, base_commit_id, repo);
307 if (err)
308 return err;
310 if (yca_id == NULL)
311 return got_error(GOT_ERR_ANCESTRY);
313 /*
314 * Require a straight line of history between the target commit
315 * and the work tree's base commit.
317 * Non-linear situations such as this require a rebase:
319 * (commit) D F (base_commit)
320 * \ /
321 * C E
322 * \ /
323 * B (yca)
324 * |
325 * A
327 * 'got update' only handles linear cases:
328 * Update forwards in time: A (base/yca) - B - C - D (commit)
329 * Update backwards in time: D (base) - C - B - A (commit/yca)
330 */
331 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
332 got_object_id_cmp(base_commit_id, yca_id) != 0)
333 return got_error(GOT_ERR_ANCESTRY);
335 free(yca_id);
336 return NULL;
339 static const struct got_error *
340 check_same_branch(struct got_object_id *commit_id,
341 struct got_reference *head_ref, struct got_repository *repo)
343 const struct got_error *err = NULL;
344 struct got_commit_graph *graph = NULL;
345 struct got_object_id *head_commit_id = NULL;
346 int is_same_branch = 0;
348 err = got_ref_resolve(&head_commit_id, repo, head_ref);
349 if (err)
350 goto done;
352 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
353 if (err)
354 goto done;
356 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
357 if (err)
358 goto done;
360 for (;;) {
361 struct got_object_id *id;
362 err = got_commit_graph_iter_next(&id, graph);
363 if (err) {
364 if (err->code == GOT_ERR_ITER_COMPLETED) {
365 err = NULL;
366 break;
368 else if (err->code != GOT_ERR_ITER_NEED_MORE)
369 break;
370 err = got_commit_graph_fetch_commits(graph, 1,
371 repo);
372 if (err)
373 break;
376 if (id) {
377 if (got_object_id_cmp(id, commit_id) == 0) {
378 is_same_branch = 1;
379 break;
383 done:
384 if (graph)
385 got_commit_graph_close(graph);
386 free(head_commit_id);
387 if (!err && !is_same_branch)
388 err = got_error(GOT_ERR_ANCESTRY);
389 return err;
392 static const struct got_error *
393 cmd_checkout(int argc, char *argv[])
395 const struct got_error *error = NULL;
396 struct got_repository *repo = NULL;
397 struct got_reference *head_ref = NULL;
398 struct got_worktree *worktree = NULL;
399 char *repo_path = NULL;
400 char *worktree_path = NULL;
401 const char *path_prefix = "";
402 const char *branch_name = GOT_REF_HEAD;
403 char *commit_id_str = NULL;
404 int ch, same_path_prefix;
406 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
407 switch (ch) {
408 case 'b':
409 branch_name = optarg;
410 break;
411 case 'c':
412 commit_id_str = strdup(optarg);
413 if (commit_id_str == NULL)
414 return got_error_from_errno("strdup");
415 break;
416 case 'p':
417 path_prefix = optarg;
418 break;
419 default:
420 usage_checkout();
421 /* NOTREACHED */
425 argc -= optind;
426 argv += optind;
428 #ifndef PROFILE
429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
430 "unveil", NULL) == -1)
431 err(1, "pledge");
432 #endif
433 if (argc == 1) {
434 char *cwd, *base, *dotgit;
435 repo_path = realpath(argv[0], NULL);
436 if (repo_path == NULL)
437 return got_error_from_errno2("realpath", argv[0]);
438 cwd = getcwd(NULL, 0);
439 if (cwd == NULL) {
440 error = got_error_from_errno("getcwd");
441 goto done;
443 if (path_prefix[0]) {
444 base = basename(path_prefix);
445 if (base == NULL) {
446 error = got_error_from_errno2("basename",
447 path_prefix);
448 goto done;
450 } else {
451 base = basename(repo_path);
452 if (base == NULL) {
453 error = got_error_from_errno2("basename",
454 repo_path);
455 goto done;
458 dotgit = strstr(base, ".git");
459 if (dotgit)
460 *dotgit = '\0';
461 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
462 error = got_error_from_errno("asprintf");
463 free(cwd);
464 goto done;
466 free(cwd);
467 } else if (argc == 2) {
468 repo_path = realpath(argv[0], NULL);
469 if (repo_path == NULL) {
470 error = got_error_from_errno2("realpath", argv[0]);
471 goto done;
473 worktree_path = realpath(argv[1], NULL);
474 if (worktree_path == NULL) {
475 error = got_error_from_errno2("realpath", argv[1]);
476 goto done;
478 } else
479 usage_checkout();
481 got_path_strip_trailing_slashes(repo_path);
482 got_path_strip_trailing_slashes(worktree_path);
484 error = got_repo_open(&repo, repo_path);
485 if (error != NULL)
486 goto done;
488 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
489 if (error)
490 goto done;
492 error = got_ref_open(&head_ref, repo, branch_name, 0);
493 if (error != NULL)
494 goto done;
496 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
497 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
498 goto done;
500 error = got_worktree_open(&worktree, worktree_path);
501 if (error != NULL)
502 goto done;
504 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
505 path_prefix);
506 if (error != NULL)
507 goto done;
508 if (!same_path_prefix) {
509 error = got_error(GOT_ERR_PATH_PREFIX);
510 goto done;
513 if (commit_id_str) {
514 struct got_object_id *commit_id;
515 error = got_object_resolve_id_str(&commit_id, repo,
516 commit_id_str);
517 if (error != NULL)
518 goto done;
519 error = check_linear_ancestry(commit_id,
520 got_worktree_get_base_commit_id(worktree), repo);
521 if (error != NULL) {
522 free(commit_id);
523 goto done;
525 error = check_same_branch(commit_id, head_ref, repo);
526 if (error)
527 goto done;
528 error = got_worktree_set_base_commit_id(worktree, repo,
529 commit_id);
530 free(commit_id);
531 if (error)
532 goto done;
535 error = got_worktree_checkout_files(worktree, "", repo,
536 checkout_progress, worktree_path, check_cancelled, NULL);
537 if (error != NULL)
538 goto done;
540 printf("Now shut up and hack\n");
542 done:
543 free(commit_id_str);
544 free(repo_path);
545 free(worktree_path);
546 return error;
549 __dead static void
550 usage_update(void)
552 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
553 getprogname());
554 exit(1);
557 static void
558 update_progress(void *arg, unsigned char status, const char *path)
560 int *did_something = arg;
562 if (status == GOT_STATUS_EXISTS)
563 return;
565 *did_something = 1;
566 while (path[0] == '/')
567 path++;
568 printf("%c %s\n", status, path);
571 static const struct got_error *
572 cmd_update(int argc, char *argv[])
574 const struct got_error *error = NULL;
575 struct got_repository *repo = NULL;
576 struct got_worktree *worktree = NULL;
577 char *worktree_path = NULL, *path = NULL;
578 struct got_object_id *commit_id = NULL;
579 char *commit_id_str = NULL;
580 const char *branch_name = NULL;
581 struct got_reference *head_ref = NULL;
582 int ch, did_something = 0;
584 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
585 switch (ch) {
586 case 'b':
587 branch_name = optarg;
588 break;
589 case 'c':
590 commit_id_str = strdup(optarg);
591 if (commit_id_str == NULL)
592 return got_error_from_errno("strdup");
593 break;
594 default:
595 usage_update();
596 /* NOTREACHED */
600 argc -= optind;
601 argv += optind;
603 #ifndef PROFILE
604 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
605 "unveil", NULL) == -1)
606 err(1, "pledge");
607 #endif
608 worktree_path = getcwd(NULL, 0);
609 if (worktree_path == NULL) {
610 error = got_error_from_errno("getcwd");
611 goto done;
613 error = got_worktree_open(&worktree, worktree_path);
614 if (error)
615 goto done;
617 if (argc == 0) {
618 path = strdup("");
619 if (path == NULL) {
620 error = got_error_from_errno("strdup");
621 goto done;
623 } else if (argc == 1) {
624 error = got_worktree_resolve_path(&path, worktree, argv[0]);
625 if (error)
626 goto done;
627 } else
628 usage_update();
630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
631 if (error != NULL)
632 goto done;
634 error = apply_unveil(got_repo_get_path(repo), 0,
635 got_worktree_get_root_path(worktree), 0);
636 if (error)
637 goto done;
639 if (branch_name == NULL)
640 branch_name = got_worktree_get_head_ref_name(worktree);
641 error = got_ref_open(&head_ref, repo, branch_name, 0);
642 if (error != NULL)
643 goto done;
644 if (commit_id_str == NULL) {
645 error = got_ref_resolve(&commit_id, repo, head_ref);
646 if (error != NULL)
647 goto done;
648 error = got_object_id_str(&commit_id_str, commit_id);
649 if (error != NULL)
650 goto done;
651 } else {
652 error = got_object_resolve_id_str(&commit_id, repo,
653 commit_id_str);
654 if (error != NULL)
655 goto done;
658 if (strcmp(got_ref_get_name(head_ref),
659 got_worktree_get_head_ref_name(worktree)) != 0) {
660 struct got_object_id *head_commit_id;
661 if (strlen(path) != 0) {
662 fprintf(stderr, "%s: switching to a different "
663 "branch requires that the entire work tree "
664 "gets updated, not just '%s'\n",
665 getprogname(), path);
666 error = got_error(GOT_ERR_BAD_PATH);
667 goto done;
669 error = got_ref_resolve(&head_commit_id, repo, head_ref);
670 if (error)
671 goto done;
672 error = check_linear_ancestry(commit_id, head_commit_id, repo);
673 free(head_commit_id);
674 if (error != NULL)
675 goto done;
676 error = check_same_branch(commit_id, head_ref, repo);
677 if (error)
678 goto done;
679 error = got_worktree_set_head_ref(worktree, head_ref);
680 if (error)
681 goto done;
682 } else {
683 error = check_linear_ancestry(commit_id,
684 got_worktree_get_base_commit_id(worktree), repo);
685 if (error != NULL) {
686 if (error->code == GOT_ERR_ANCESTRY)
687 error = got_error(GOT_ERR_BRANCH_MOVED);
688 goto done;
690 error = check_same_branch(commit_id, head_ref, repo);
691 if (error)
692 goto done;
695 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
696 commit_id) != 0) {
697 error = got_worktree_set_base_commit_id(worktree, repo,
698 commit_id);
699 if (error)
700 goto done;
703 error = got_worktree_checkout_files(worktree, path, repo,
704 update_progress, &did_something, check_cancelled, NULL);
705 if (error != NULL)
706 goto done;
708 if (did_something)
709 printf("Updated to commit %s\n", commit_id_str);
710 else
711 printf("Already up-to-date\n");
712 done:
713 free(worktree_path);
714 free(path);
715 free(commit_id);
716 free(commit_id_str);
717 return error;
720 static const struct got_error *
721 print_patch(struct got_commit_object *commit, struct got_object_id *id,
722 int diff_context, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_tree_object *tree1 = NULL, *tree2;
726 struct got_object_qid *qid;
727 char *id_str1 = NULL, *id_str2;
729 err = got_object_open_as_tree(&tree2, repo,
730 got_object_commit_get_tree_id(commit));
731 if (err)
732 return err;
734 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
735 if (qid != NULL) {
736 struct got_commit_object *pcommit;
738 err = got_object_open_as_commit(&pcommit, repo, qid->id);
739 if (err)
740 return err;
742 err = got_object_open_as_tree(&tree1, repo,
743 got_object_commit_get_tree_id(pcommit));
744 got_object_commit_close(pcommit);
745 if (err)
746 return err;
748 err = got_object_id_str(&id_str1, qid->id);
749 if (err)
750 return err;
753 err = got_object_id_str(&id_str2, id);
754 if (err)
755 goto done;
757 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
758 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
759 done:
760 if (tree1)
761 got_object_tree_close(tree1);
762 got_object_tree_close(tree2);
763 free(id_str1);
764 free(id_str2);
765 return err;
768 static char *
769 get_datestr(time_t *time, char *datebuf)
771 char *p, *s = ctime_r(time, datebuf);
772 p = strchr(s, '\n');
773 if (p)
774 *p = '\0';
775 return s;
778 static const struct got_error *
779 print_commit(struct got_commit_object *commit, struct got_object_id *id,
780 struct got_repository *repo, int show_patch, int diff_context,
781 struct got_reflist_head *refs)
783 const struct got_error *err = NULL;
784 char *id_str, *datestr, *logmsg0, *logmsg, *line;
785 char datebuf[26];
786 time_t committer_time;
787 const char *author, *committer;
788 char *refs_str = NULL;
789 struct got_reflist_entry *re;
791 SIMPLEQ_FOREACH(re, refs, entry) {
792 char *s;
793 const char *name;
794 if (got_object_id_cmp(re->id, id) != 0)
795 continue;
796 name = got_ref_get_name(re->ref);
797 if (strcmp(name, GOT_REF_HEAD) == 0)
798 continue;
799 if (strncmp(name, "refs/", 5) == 0)
800 name += 5;
801 if (strncmp(name, "got/", 4) == 0)
802 continue;
803 if (strncmp(name, "heads/", 6) == 0)
804 name += 6;
805 if (strncmp(name, "remotes/", 8) == 0)
806 name += 8;
807 s = refs_str;
808 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
809 name) == -1) {
810 err = got_error_from_errno("asprintf");
811 free(s);
812 break;
814 free(s);
816 err = got_object_id_str(&id_str, id);
817 if (err)
818 return err;
820 printf("-----------------------------------------------\n");
821 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
822 refs_str ? refs_str : "", refs_str ? ")" : "");
823 free(id_str);
824 id_str = NULL;
825 free(refs_str);
826 refs_str = NULL;
827 printf("from: %s\n", got_object_commit_get_author(commit));
828 committer_time = got_object_commit_get_committer_time(commit);
829 datestr = get_datestr(&committer_time, datebuf);
830 printf("date: %s UTC\n", datestr);
831 author = got_object_commit_get_author(commit);
832 committer = got_object_commit_get_committer(commit);
833 if (strcmp(author, committer) != 0)
834 printf("via: %s\n", committer);
835 if (got_object_commit_get_nparents(commit) > 1) {
836 const struct got_object_id_queue *parent_ids;
837 struct got_object_qid *qid;
838 int n = 1;
839 parent_ids = got_object_commit_get_parent_ids(commit);
840 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
841 err = got_object_id_str(&id_str, qid->id);
842 if (err)
843 return err;
844 printf("parent %d: %s\n", n++, id_str);
845 free(id_str);
849 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
850 if (logmsg0 == NULL)
851 return got_error_from_errno("strdup");
853 logmsg = logmsg0;
854 do {
855 line = strsep(&logmsg, "\n");
856 if (line)
857 printf(" %s\n", line);
858 } while (line);
859 free(logmsg0);
861 if (show_patch) {
862 err = print_patch(commit, id, diff_context, repo);
863 if (err == 0)
864 printf("\n");
867 if (fflush(stdout) != 0 && err == NULL)
868 err = got_error_from_errno("fflush");
869 return err;
872 static const struct got_error *
873 print_commits(struct got_object_id *root_id, struct got_repository *repo,
874 char *path, int show_patch, int diff_context, int limit,
875 int first_parent_traversal, struct got_reflist_head *refs)
877 const struct got_error *err;
878 struct got_commit_graph *graph;
880 err = got_commit_graph_open(&graph, root_id, path,
881 first_parent_traversal, repo);
882 if (err)
883 return err;
884 err = got_commit_graph_iter_start(graph, root_id, repo);
885 if (err)
886 goto done;
887 for (;;) {
888 struct got_commit_object *commit;
889 struct got_object_id *id;
891 if (sigint_received || sigpipe_received)
892 break;
894 err = got_commit_graph_iter_next(&id, graph);
895 if (err) {
896 if (err->code == GOT_ERR_ITER_COMPLETED) {
897 err = NULL;
898 break;
900 if (err->code != GOT_ERR_ITER_NEED_MORE)
901 break;
902 err = got_commit_graph_fetch_commits(graph, 1, repo);
903 if (err)
904 break;
905 else
906 continue;
908 if (id == NULL)
909 break;
911 err = got_object_open_as_commit(&commit, repo, id);
912 if (err)
913 break;
914 err = print_commit(commit, id, repo, show_patch, diff_context,
915 refs);
916 got_object_commit_close(commit);
917 if (err || (limit && --limit == 0))
918 break;
920 done:
921 got_commit_graph_close(graph);
922 return err;
925 __dead static void
926 usage_log(void)
928 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
929 "[-r repository-path] [path]\n", getprogname());
930 exit(1);
933 static const struct got_error *
934 cmd_log(int argc, char *argv[])
936 const struct got_error *error;
937 struct got_repository *repo = NULL;
938 struct got_worktree *worktree = NULL;
939 struct got_commit_object *commit = NULL;
940 struct got_object_id *id = NULL;
941 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
942 char *start_commit = NULL;
943 int diff_context = 3, ch;
944 int show_patch = 0, limit = 0, first_parent_traversal = 0;
945 const char *errstr;
946 struct got_reflist_head refs;
948 SIMPLEQ_INIT(&refs);
950 #ifndef PROFILE
951 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
952 NULL)
953 == -1)
954 err(1, "pledge");
955 #endif
957 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
958 switch (ch) {
959 case 'p':
960 show_patch = 1;
961 break;
962 case 'c':
963 start_commit = optarg;
964 break;
965 case 'C':
966 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
967 &errstr);
968 if (errstr != NULL)
969 err(1, "-C option %s", errstr);
970 break;
971 case 'l':
972 limit = strtonum(optarg, 1, INT_MAX, &errstr);
973 if (errstr != NULL)
974 err(1, "-l option %s", errstr);
975 break;
976 case 'f':
977 first_parent_traversal = 1;
978 break;
979 case 'r':
980 repo_path = realpath(optarg, NULL);
981 if (repo_path == NULL)
982 err(1, "-r option");
983 got_path_strip_trailing_slashes(repo_path);
984 break;
985 default:
986 usage_log();
987 /* NOTREACHED */
991 argc -= optind;
992 argv += optind;
994 cwd = getcwd(NULL, 0);
995 if (cwd == NULL) {
996 error = got_error_from_errno("getcwd");
997 goto done;
1000 error = got_worktree_open(&worktree, cwd);
1001 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1002 goto done;
1003 error = NULL;
1005 if (argc == 0) {
1006 path = strdup("");
1007 if (path == NULL) {
1008 error = got_error_from_errno("strdup");
1009 goto done;
1011 } else if (argc == 1) {
1012 if (worktree) {
1013 error = got_worktree_resolve_path(&path, worktree,
1014 argv[0]);
1015 if (error)
1016 goto done;
1017 } else {
1018 path = strdup(argv[0]);
1019 if (path == NULL) {
1020 error = got_error_from_errno("strdup");
1021 goto done;
1024 } else
1025 usage_log();
1027 if (repo_path == NULL) {
1028 repo_path = worktree ?
1029 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1031 if (repo_path == NULL) {
1032 error = got_error_from_errno("strdup");
1033 goto done;
1036 error = got_repo_open(&repo, repo_path);
1037 if (error != NULL)
1038 goto done;
1040 error = apply_unveil(got_repo_get_path(repo), 1,
1041 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1042 if (error)
1043 goto done;
1045 if (start_commit == NULL) {
1046 struct got_reference *head_ref;
1047 error = got_ref_open(&head_ref, repo,
1048 worktree ? got_worktree_get_head_ref_name(worktree)
1049 : GOT_REF_HEAD, 0);
1050 if (error != NULL)
1051 return error;
1052 error = got_ref_resolve(&id, repo, head_ref);
1053 got_ref_close(head_ref);
1054 if (error != NULL)
1055 return error;
1056 error = got_object_open_as_commit(&commit, repo, id);
1057 } else {
1058 struct got_reference *ref;
1059 error = got_ref_open(&ref, repo, start_commit, 0);
1060 if (error == NULL) {
1061 int obj_type;
1062 error = got_ref_resolve(&id, repo, ref);
1063 got_ref_close(ref);
1064 if (error != NULL)
1065 goto done;
1066 error = got_object_get_type(&obj_type, repo, id);
1067 if (error != NULL)
1068 goto done;
1069 if (obj_type == GOT_OBJ_TYPE_TAG) {
1070 struct got_tag_object *tag;
1071 error = got_object_open_as_tag(&tag, repo, id);
1072 if (error != NULL)
1073 goto done;
1074 if (got_object_tag_get_object_type(tag) !=
1075 GOT_OBJ_TYPE_COMMIT) {
1076 got_object_tag_close(tag);
1077 error = got_error(GOT_ERR_OBJ_TYPE);
1078 goto done;
1080 free(id);
1081 id = got_object_id_dup(
1082 got_object_tag_get_object_id(tag));
1083 if (id == NULL)
1084 error = got_error_from_errno(
1085 "got_object_id_dup");
1086 got_object_tag_close(tag);
1087 if (error)
1088 goto done;
1089 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1090 error = got_error(GOT_ERR_OBJ_TYPE);
1091 goto done;
1093 error = got_object_open_as_commit(&commit, repo, id);
1094 if (error != NULL)
1095 goto done;
1097 if (commit == NULL) {
1098 error = got_object_resolve_id_str(&id, repo,
1099 start_commit);
1100 if (error != NULL)
1101 return error;
1104 if (error != NULL)
1105 goto done;
1107 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1108 if (error != NULL)
1109 goto done;
1110 if (in_repo_path) {
1111 free(path);
1112 path = in_repo_path;
1115 error = got_ref_list(&refs, repo);
1116 if (error)
1117 goto done;
1119 error = print_commits(id, repo, path, show_patch,
1120 diff_context, limit, first_parent_traversal, &refs);
1121 done:
1122 free(path);
1123 free(repo_path);
1124 free(cwd);
1125 free(id);
1126 if (worktree)
1127 got_worktree_close(worktree);
1128 if (repo) {
1129 const struct got_error *repo_error;
1130 repo_error = got_repo_close(repo);
1131 if (error == NULL)
1132 error = repo_error;
1134 got_ref_list_free(&refs);
1135 return error;
1138 __dead static void
1139 usage_diff(void)
1141 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1142 "[object1 object2 | path]\n", getprogname());
1143 exit(1);
1146 struct print_diff_arg {
1147 struct got_repository *repo;
1148 struct got_worktree *worktree;
1149 int diff_context;
1150 const char *id_str;
1151 int header_shown;
1154 static const struct got_error *
1155 print_diff(void *arg, unsigned char status, const char *path,
1156 struct got_object_id *blob_id, struct got_object_id *commit_id)
1158 struct print_diff_arg *a = arg;
1159 const struct got_error *err = NULL;
1160 struct got_blob_object *blob1 = NULL;
1161 FILE *f2 = NULL;
1162 char *abspath = NULL;
1163 struct stat sb;
1165 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1166 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1167 return NULL;
1169 if (!a->header_shown) {
1170 printf("diff %s %s\n", a->id_str,
1171 got_worktree_get_root_path(a->worktree));
1172 a->header_shown = 1;
1175 if (status != GOT_STATUS_ADD) {
1176 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1177 if (err)
1178 goto done;
1182 if (status != GOT_STATUS_DELETE) {
1183 if (asprintf(&abspath, "%s/%s",
1184 got_worktree_get_root_path(a->worktree), path) == -1) {
1185 err = got_error_from_errno("asprintf");
1186 goto done;
1189 f2 = fopen(abspath, "r");
1190 if (f2 == NULL) {
1191 err = got_error_from_errno2("fopen", abspath);
1192 goto done;
1194 if (lstat(abspath, &sb) == -1) {
1195 err = got_error_from_errno2("lstat", abspath);
1196 goto done;
1198 } else
1199 sb.st_size = 0;
1201 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1202 stdout);
1203 done:
1204 if (blob1)
1205 got_object_blob_close(blob1);
1206 if (f2 && fclose(f2) != 0 && err == NULL)
1207 err = got_error_from_errno("fclose");
1208 free(abspath);
1209 return err;
1212 static const struct got_error *
1213 cmd_diff(int argc, char *argv[])
1215 const struct got_error *error;
1216 struct got_repository *repo = NULL;
1217 struct got_worktree *worktree = NULL;
1218 char *cwd = NULL, *repo_path = NULL;
1219 struct got_object_id *id1 = NULL, *id2 = NULL;
1220 char *id_str1 = NULL, *id_str2 = NULL;
1221 int type1, type2;
1222 int diff_context = 3, ch;
1223 const char *errstr;
1224 char *path = NULL;
1226 #ifndef PROFILE
1227 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1228 NULL) == -1)
1229 err(1, "pledge");
1230 #endif
1232 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1233 switch (ch) {
1234 case 'C':
1235 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1236 if (errstr != NULL)
1237 err(1, "-C option %s", errstr);
1238 break;
1239 case 'r':
1240 repo_path = realpath(optarg, NULL);
1241 if (repo_path == NULL)
1242 err(1, "-r option");
1243 got_path_strip_trailing_slashes(repo_path);
1244 break;
1245 default:
1246 usage_diff();
1247 /* NOTREACHED */
1251 argc -= optind;
1252 argv += optind;
1254 cwd = getcwd(NULL, 0);
1255 if (cwd == NULL) {
1256 error = got_error_from_errno("getcwd");
1257 goto done;
1259 error = got_worktree_open(&worktree, cwd);
1260 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1261 goto done;
1262 if (argc <= 1) {
1263 if (worktree == NULL) {
1264 error = got_error(GOT_ERR_NOT_WORKTREE);
1265 goto done;
1267 if (repo_path)
1268 errx(1,
1269 "-r option can't be used when diffing a work tree");
1270 repo_path = strdup(got_worktree_get_repo_path(worktree));
1271 if (repo_path == NULL) {
1272 error = got_error_from_errno("strdup");
1273 goto done;
1275 if (argc == 1) {
1276 error = got_worktree_resolve_path(&path, worktree,
1277 argv[0]);
1278 if (error)
1279 goto done;
1280 } else {
1281 path = strdup("");
1282 if (path == NULL) {
1283 error = got_error_from_errno("strdup");
1284 goto done;
1287 } else if (argc == 2) {
1288 id_str1 = argv[0];
1289 id_str2 = argv[1];
1290 } else
1291 usage_diff();
1293 if (repo_path == NULL) {
1294 repo_path = getcwd(NULL, 0);
1295 if (repo_path == NULL)
1296 return got_error_from_errno("getcwd");
1299 error = got_repo_open(&repo, repo_path);
1300 free(repo_path);
1301 if (error != NULL)
1302 goto done;
1304 error = apply_unveil(got_repo_get_path(repo), 1,
1305 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1306 if (error)
1307 goto done;
1309 if (worktree) {
1310 struct print_diff_arg arg;
1311 char *id_str;
1312 error = got_object_id_str(&id_str,
1313 got_worktree_get_base_commit_id(worktree));
1314 if (error)
1315 goto done;
1316 arg.repo = repo;
1317 arg.worktree = worktree;
1318 arg.diff_context = diff_context;
1319 arg.id_str = id_str;
1320 arg.header_shown = 0;
1322 error = got_worktree_status(worktree, path, repo, print_diff,
1323 &arg, check_cancelled, NULL);
1324 free(id_str);
1325 goto done;
1328 error = got_object_resolve_id_str(&id1, repo, id_str1);
1329 if (error)
1330 goto done;
1332 error = got_object_resolve_id_str(&id2, repo, id_str2);
1333 if (error)
1334 goto done;
1336 error = got_object_get_type(&type1, repo, id1);
1337 if (error)
1338 goto done;
1340 error = got_object_get_type(&type2, repo, id2);
1341 if (error)
1342 goto done;
1344 if (type1 != type2) {
1345 error = got_error(GOT_ERR_OBJ_TYPE);
1346 goto done;
1349 switch (type1) {
1350 case GOT_OBJ_TYPE_BLOB:
1351 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1352 diff_context, repo, stdout);
1353 break;
1354 case GOT_OBJ_TYPE_TREE:
1355 error = got_diff_objects_as_trees(id1, id2, "", "",
1356 diff_context, repo, stdout);
1357 break;
1358 case GOT_OBJ_TYPE_COMMIT:
1359 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1360 id_str2);
1361 error = got_diff_objects_as_commits(id1, id2, diff_context,
1362 repo, stdout);
1363 break;
1364 default:
1365 error = got_error(GOT_ERR_OBJ_TYPE);
1368 done:
1369 free(id1);
1370 free(id2);
1371 free(path);
1372 if (worktree)
1373 got_worktree_close(worktree);
1374 if (repo) {
1375 const struct got_error *repo_error;
1376 repo_error = got_repo_close(repo);
1377 if (error == NULL)
1378 error = repo_error;
1380 return error;
1383 __dead static void
1384 usage_blame(void)
1386 fprintf(stderr,
1387 "usage: %s blame [-c commit] [-r repository-path] path\n",
1388 getprogname());
1389 exit(1);
1392 static const struct got_error *
1393 cmd_blame(int argc, char *argv[])
1395 const struct got_error *error;
1396 struct got_repository *repo = NULL;
1397 struct got_worktree *worktree = NULL;
1398 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1399 struct got_object_id *commit_id = NULL;
1400 char *commit_id_str = NULL;
1401 int ch;
1403 #ifndef PROFILE
1404 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1405 NULL) == -1)
1406 err(1, "pledge");
1407 #endif
1409 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1410 switch (ch) {
1411 case 'c':
1412 commit_id_str = optarg;
1413 break;
1414 case 'r':
1415 repo_path = realpath(optarg, NULL);
1416 if (repo_path == NULL)
1417 err(1, "-r option");
1418 got_path_strip_trailing_slashes(repo_path);
1419 break;
1420 default:
1421 usage_blame();
1422 /* NOTREACHED */
1426 argc -= optind;
1427 argv += optind;
1429 if (argc == 1)
1430 path = argv[0];
1431 else
1432 usage_blame();
1434 cwd = getcwd(NULL, 0);
1435 if (cwd == NULL) {
1436 error = got_error_from_errno("getcwd");
1437 goto done;
1439 if (repo_path == NULL) {
1440 error = got_worktree_open(&worktree, cwd);
1441 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1442 goto done;
1443 else
1444 error = NULL;
1445 if (worktree) {
1446 repo_path =
1447 strdup(got_worktree_get_repo_path(worktree));
1448 if (repo_path == NULL)
1449 error = got_error_from_errno("strdup");
1450 if (error)
1451 goto done;
1452 } else {
1453 repo_path = strdup(cwd);
1454 if (repo_path == NULL) {
1455 error = got_error_from_errno("strdup");
1456 goto done;
1461 error = got_repo_open(&repo, repo_path);
1462 if (error != NULL)
1463 goto done;
1465 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1466 if (error)
1467 goto done;
1469 if (worktree) {
1470 const char *prefix = got_worktree_get_path_prefix(worktree);
1471 char *p, *worktree_subdir = cwd +
1472 strlen(got_worktree_get_root_path(worktree));
1473 if (asprintf(&p, "%s%s%s%s%s",
1474 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1475 worktree_subdir, worktree_subdir[0] ? "/" : "",
1476 path) == -1) {
1477 error = got_error_from_errno("asprintf");
1478 goto done;
1480 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1481 free(p);
1482 } else {
1483 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1485 if (error)
1486 goto done;
1488 if (commit_id_str == NULL) {
1489 struct got_reference *head_ref;
1490 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1491 if (error != NULL)
1492 goto done;
1493 error = got_ref_resolve(&commit_id, repo, head_ref);
1494 got_ref_close(head_ref);
1495 if (error != NULL)
1496 goto done;
1497 } else {
1498 error = got_object_resolve_id_str(&commit_id, repo,
1499 commit_id_str);
1500 if (error != NULL)
1501 goto done;
1504 error = got_blame(in_repo_path, commit_id, repo, stdout);
1505 done:
1506 free(in_repo_path);
1507 free(repo_path);
1508 free(cwd);
1509 free(commit_id);
1510 if (worktree)
1511 got_worktree_close(worktree);
1512 if (repo) {
1513 const struct got_error *repo_error;
1514 repo_error = got_repo_close(repo);
1515 if (error == NULL)
1516 error = repo_error;
1518 return error;
1521 __dead static void
1522 usage_tree(void)
1524 fprintf(stderr,
1525 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1526 getprogname());
1527 exit(1);
1530 static void
1531 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1532 const char *root_path)
1534 int is_root_path = (strcmp(path, root_path) == 0);
1536 path += strlen(root_path);
1537 while (path[0] == '/')
1538 path++;
1540 printf("%s%s%s%s%s\n", id ? id : "", path,
1541 is_root_path ? "" : "/", te->name,
1542 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1545 static const struct got_error *
1546 print_tree(const char *path, struct got_object_id *commit_id,
1547 int show_ids, int recurse, const char *root_path,
1548 struct got_repository *repo)
1550 const struct got_error *err = NULL;
1551 struct got_object_id *tree_id = NULL;
1552 struct got_tree_object *tree = NULL;
1553 const struct got_tree_entries *entries;
1554 struct got_tree_entry *te;
1556 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1557 if (err)
1558 goto done;
1560 err = got_object_open_as_tree(&tree, repo, tree_id);
1561 if (err)
1562 goto done;
1563 entries = got_object_tree_get_entries(tree);
1564 te = SIMPLEQ_FIRST(&entries->head);
1565 while (te) {
1566 char *id = NULL;
1568 if (sigint_received || sigpipe_received)
1569 break;
1571 if (show_ids) {
1572 char *id_str;
1573 err = got_object_id_str(&id_str, te->id);
1574 if (err)
1575 goto done;
1576 if (asprintf(&id, "%s ", id_str) == -1) {
1577 err = got_error_from_errno("asprintf");
1578 free(id_str);
1579 goto done;
1581 free(id_str);
1583 print_entry(te, id, path, root_path);
1584 free(id);
1586 if (recurse && S_ISDIR(te->mode)) {
1587 char *child_path;
1588 if (asprintf(&child_path, "%s%s%s", path,
1589 path[0] == '/' && path[1] == '\0' ? "" : "/",
1590 te->name) == -1) {
1591 err = got_error_from_errno("asprintf");
1592 goto done;
1594 err = print_tree(child_path, commit_id, show_ids, 1,
1595 root_path, repo);
1596 free(child_path);
1597 if (err)
1598 goto done;
1601 te = SIMPLEQ_NEXT(te, entry);
1603 done:
1604 if (tree)
1605 got_object_tree_close(tree);
1606 free(tree_id);
1607 return err;
1610 static const struct got_error *
1611 cmd_tree(int argc, char *argv[])
1613 const struct got_error *error;
1614 struct got_repository *repo = NULL;
1615 struct got_worktree *worktree = NULL;
1616 const char *path;
1617 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1618 struct got_object_id *commit_id = NULL;
1619 char *commit_id_str = NULL;
1620 int show_ids = 0, recurse = 0;
1621 int ch;
1623 #ifndef PROFILE
1624 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1625 NULL) == -1)
1626 err(1, "pledge");
1627 #endif
1629 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1630 switch (ch) {
1631 case 'c':
1632 commit_id_str = optarg;
1633 break;
1634 case 'r':
1635 repo_path = realpath(optarg, NULL);
1636 if (repo_path == NULL)
1637 err(1, "-r option");
1638 got_path_strip_trailing_slashes(repo_path);
1639 break;
1640 case 'i':
1641 show_ids = 1;
1642 break;
1643 case 'R':
1644 recurse = 1;
1645 break;
1646 default:
1647 usage_tree();
1648 /* NOTREACHED */
1652 argc -= optind;
1653 argv += optind;
1655 if (argc == 1)
1656 path = argv[0];
1657 else if (argc > 1)
1658 usage_tree();
1659 else
1660 path = NULL;
1662 cwd = getcwd(NULL, 0);
1663 if (cwd == NULL) {
1664 error = got_error_from_errno("getcwd");
1665 goto done;
1667 if (repo_path == NULL) {
1668 error = got_worktree_open(&worktree, cwd);
1669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1670 goto done;
1671 else
1672 error = NULL;
1673 if (worktree) {
1674 repo_path =
1675 strdup(got_worktree_get_repo_path(worktree));
1676 if (repo_path == NULL)
1677 error = got_error_from_errno("strdup");
1678 if (error)
1679 goto done;
1680 } else {
1681 repo_path = strdup(cwd);
1682 if (repo_path == NULL) {
1683 error = got_error_from_errno("strdup");
1684 goto done;
1689 error = got_repo_open(&repo, repo_path);
1690 if (error != NULL)
1691 goto done;
1693 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1694 if (error)
1695 goto done;
1697 if (path == NULL) {
1698 if (worktree) {
1699 char *p, *worktree_subdir = cwd +
1700 strlen(got_worktree_get_root_path(worktree));
1701 if (asprintf(&p, "%s/%s",
1702 got_worktree_get_path_prefix(worktree),
1703 worktree_subdir) == -1) {
1704 error = got_error_from_errno("asprintf");
1705 goto done;
1707 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1708 free(p);
1709 if (error)
1710 goto done;
1711 } else
1712 path = "/";
1714 if (in_repo_path == NULL) {
1715 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1716 if (error != NULL)
1717 goto done;
1720 if (commit_id_str == NULL) {
1721 struct got_reference *head_ref;
1722 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1723 if (error != NULL)
1724 goto done;
1725 error = got_ref_resolve(&commit_id, repo, head_ref);
1726 got_ref_close(head_ref);
1727 if (error != NULL)
1728 goto done;
1729 } else {
1730 error = got_object_resolve_id_str(&commit_id, repo,
1731 commit_id_str);
1732 if (error != NULL)
1733 goto done;
1736 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1737 in_repo_path, repo);
1738 done:
1739 free(in_repo_path);
1740 free(repo_path);
1741 free(cwd);
1742 free(commit_id);
1743 if (worktree)
1744 got_worktree_close(worktree);
1745 if (repo) {
1746 const struct got_error *repo_error;
1747 repo_error = got_repo_close(repo);
1748 if (error == NULL)
1749 error = repo_error;
1751 return error;
1754 __dead static void
1755 usage_status(void)
1757 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1758 exit(1);
1761 static const struct got_error *
1762 print_status(void *arg, unsigned char status, const char *path,
1763 struct got_object_id *blob_id, struct got_object_id *commit_id)
1765 printf("%c %s\n", status, path);
1766 return NULL;
1769 static const struct got_error *
1770 cmd_status(int argc, char *argv[])
1772 const struct got_error *error = NULL;
1773 struct got_repository *repo = NULL;
1774 struct got_worktree *worktree = NULL;
1775 char *cwd = NULL, *path = NULL;
1776 int ch;
1778 while ((ch = getopt(argc, argv, "")) != -1) {
1779 switch (ch) {
1780 default:
1781 usage_status();
1782 /* NOTREACHED */
1786 argc -= optind;
1787 argv += optind;
1789 #ifndef PROFILE
1790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1791 NULL) == -1)
1792 err(1, "pledge");
1793 #endif
1794 cwd = getcwd(NULL, 0);
1795 if (cwd == NULL) {
1796 error = got_error_from_errno("getcwd");
1797 goto done;
1800 error = got_worktree_open(&worktree, cwd);
1801 if (error != NULL)
1802 goto done;
1804 if (argc == 0) {
1805 path = strdup("");
1806 if (path == NULL) {
1807 error = got_error_from_errno("strdup");
1808 goto done;
1810 } else if (argc == 1) {
1811 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1812 if (error)
1813 goto done;
1814 } else
1815 usage_status();
1817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1818 if (error != NULL)
1819 goto done;
1821 error = apply_unveil(got_repo_get_path(repo), 1,
1822 got_worktree_get_root_path(worktree), 0);
1823 if (error)
1824 goto done;
1826 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1827 check_cancelled, NULL);
1828 done:
1829 free(cwd);
1830 free(path);
1831 return error;
1834 __dead static void
1835 usage_ref(void)
1837 fprintf(stderr,
1838 "usage: %s ref [-r repository] -l | -d name | name target\n",
1839 getprogname());
1840 exit(1);
1843 static const struct got_error *
1844 list_refs(struct got_repository *repo)
1846 static const struct got_error *err = NULL;
1847 struct got_reflist_head refs;
1848 struct got_reflist_entry *re;
1850 SIMPLEQ_INIT(&refs);
1851 err = got_ref_list(&refs, repo);
1852 if (err)
1853 return err;
1855 SIMPLEQ_FOREACH(re, &refs, entry) {
1856 char *refstr;
1857 refstr = got_ref_to_str(re->ref);
1858 if (refstr == NULL)
1859 return got_error_from_errno("got_ref_to_str");
1860 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1861 free(refstr);
1864 got_ref_list_free(&refs);
1865 return NULL;
1868 static const struct got_error *
1869 delete_ref(struct got_repository *repo, const char *refname)
1871 const struct got_error *err = NULL;
1872 struct got_reference *ref;
1874 err = got_ref_open(&ref, repo, refname, 0);
1875 if (err)
1876 return err;
1878 err = got_ref_delete(ref, repo);
1879 got_ref_close(ref);
1880 return err;
1883 static const struct got_error *
1884 add_ref(struct got_repository *repo, const char *refname, const char *target)
1886 const struct got_error *err = NULL;
1887 struct got_object_id *id;
1888 struct got_reference *ref = NULL;
1890 err = got_object_resolve_id_str(&id, repo, target);
1891 if (err) {
1892 struct got_reference *target_ref;
1894 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1895 return err;
1896 err = got_ref_open(&target_ref, repo, target, 0);
1897 if (err)
1898 return err;
1899 err = got_ref_resolve(&id, repo, target_ref);
1900 got_ref_close(target_ref);
1901 if (err)
1902 return err;
1905 err = got_ref_alloc(&ref, refname, id);
1906 if (err)
1907 goto done;
1909 err = got_ref_write(ref, repo);
1910 done:
1911 if (ref)
1912 got_ref_close(ref);
1913 free(id);
1914 return err;
1917 static const struct got_error *
1918 cmd_ref(int argc, char *argv[])
1920 const struct got_error *error = NULL;
1921 struct got_repository *repo = NULL;
1922 struct got_worktree *worktree = NULL;
1923 char *cwd = NULL, *repo_path = NULL;
1924 int ch, do_list = 0;
1925 const char *delref = NULL;
1927 /* TODO: Add -s option for adding symbolic references. */
1928 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1929 switch (ch) {
1930 case 'd':
1931 delref = optarg;
1932 break;
1933 case 'r':
1934 repo_path = realpath(optarg, NULL);
1935 if (repo_path == NULL)
1936 err(1, "-r option");
1937 got_path_strip_trailing_slashes(repo_path);
1938 break;
1939 case 'l':
1940 do_list = 1;
1941 break;
1942 default:
1943 usage_ref();
1944 /* NOTREACHED */
1948 if (do_list && delref)
1949 errx(1, "-l and -d options are mutually exclusive\n");
1951 argc -= optind;
1952 argv += optind;
1954 if (do_list || delref) {
1955 if (argc > 0)
1956 usage_ref();
1957 } else if (argc != 2)
1958 usage_ref();
1960 #ifndef PROFILE
1961 if (do_list) {
1962 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1963 NULL) == -1)
1964 err(1, "pledge");
1965 } else {
1966 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1967 "sendfd unveil", NULL) == -1)
1968 err(1, "pledge");
1970 #endif
1971 cwd = getcwd(NULL, 0);
1972 if (cwd == NULL) {
1973 error = got_error_from_errno("getcwd");
1974 goto done;
1977 if (repo_path == NULL) {
1978 error = got_worktree_open(&worktree, cwd);
1979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1980 goto done;
1981 else
1982 error = NULL;
1983 if (worktree) {
1984 repo_path =
1985 strdup(got_worktree_get_repo_path(worktree));
1986 if (repo_path == NULL)
1987 error = got_error_from_errno("strdup");
1988 if (error)
1989 goto done;
1990 } else {
1991 repo_path = strdup(cwd);
1992 if (repo_path == NULL) {
1993 error = got_error_from_errno("strdup");
1994 goto done;
1999 error = got_repo_open(&repo, repo_path);
2000 if (error != NULL)
2001 goto done;
2003 error = apply_unveil(got_repo_get_path(repo), do_list,
2004 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2005 if (error)
2006 goto done;
2008 if (do_list)
2009 error = list_refs(repo);
2010 else if (delref)
2011 error = delete_ref(repo, delref);
2012 else
2013 error = add_ref(repo, argv[0], argv[1]);
2014 done:
2015 if (repo)
2016 got_repo_close(repo);
2017 if (worktree)
2018 got_worktree_close(worktree);
2019 free(cwd);
2020 free(repo_path);
2021 return error;
2024 __dead static void
2025 usage_add(void)
2027 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2028 exit(1);
2031 static const struct got_error *
2032 cmd_add(int argc, char *argv[])
2034 const struct got_error *error = NULL;
2035 struct got_repository *repo = NULL;
2036 struct got_worktree *worktree = NULL;
2037 char *cwd = NULL;
2038 struct got_pathlist_head paths;
2039 struct got_pathlist_entry *pe;
2040 int ch, x;
2042 TAILQ_INIT(&paths);
2044 while ((ch = getopt(argc, argv, "")) != -1) {
2045 switch (ch) {
2046 default:
2047 usage_add();
2048 /* NOTREACHED */
2052 argc -= optind;
2053 argv += optind;
2055 if (argc < 1)
2056 usage_add();
2058 /* make sure each file exists before doing anything halfway */
2059 for (x = 0; x < argc; x++) {
2060 char *path = realpath(argv[x], NULL);
2061 if (path == NULL) {
2062 error = got_error_from_errno2("realpath", argv[x]);
2063 goto done;
2065 free(path);
2068 cwd = getcwd(NULL, 0);
2069 if (cwd == NULL) {
2070 error = got_error_from_errno("getcwd");
2071 goto done;
2074 error = got_worktree_open(&worktree, cwd);
2075 if (error)
2076 goto done;
2078 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2079 if (error != NULL)
2080 goto done;
2082 error = apply_unveil(got_repo_get_path(repo), 1,
2083 got_worktree_get_root_path(worktree), 0);
2084 if (error)
2085 goto done;
2087 for (x = 0; x < argc; x++) {
2088 char *path = realpath(argv[x], NULL);
2089 if (path == NULL) {
2090 error = got_error_from_errno2("realpath", argv[x]);
2091 goto done;
2094 got_path_strip_trailing_slashes(path);
2095 error = got_pathlist_insert(&pe, &paths, path, NULL);
2096 if (error) {
2097 free(path);
2098 goto done;
2101 error = got_worktree_schedule_add(worktree, &paths, print_status,
2102 NULL, repo);
2103 done:
2104 if (repo)
2105 got_repo_close(repo);
2106 if (worktree)
2107 got_worktree_close(worktree);
2108 TAILQ_FOREACH(pe, &paths, entry)
2109 free((char *)pe->path);
2110 got_pathlist_free(&paths);
2111 free(cwd);
2112 return error;
2115 __dead static void
2116 usage_rm(void)
2118 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2119 exit(1);
2122 static const struct got_error *
2123 cmd_rm(int argc, char *argv[])
2125 const struct got_error *error = NULL;
2126 struct got_worktree *worktree = NULL;
2127 struct got_repository *repo = NULL;
2128 char *cwd = NULL, *path = NULL;
2129 int ch, delete_local_mods = 0;
2131 while ((ch = getopt(argc, argv, "f")) != -1) {
2132 switch (ch) {
2133 case 'f':
2134 delete_local_mods = 1;
2135 break;
2136 default:
2137 usage_add();
2138 /* NOTREACHED */
2142 argc -= optind;
2143 argv += optind;
2145 if (argc != 1)
2146 usage_rm();
2148 path = realpath(argv[0], NULL);
2149 if (path == NULL) {
2150 error = got_error_from_errno2("realpath", argv[0]);
2151 goto done;
2153 got_path_strip_trailing_slashes(path);
2155 cwd = getcwd(NULL, 0);
2156 if (cwd == NULL) {
2157 error = got_error_from_errno("getcwd");
2158 goto done;
2160 error = got_worktree_open(&worktree, cwd);
2161 if (error)
2162 goto done;
2164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2165 if (error)
2166 goto done;
2168 error = apply_unveil(got_repo_get_path(repo), 1,
2169 got_worktree_get_root_path(worktree), 0);
2170 if (error)
2171 goto done;
2173 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2174 print_status, NULL, repo);
2175 if (error)
2176 goto done;
2177 done:
2178 if (repo)
2179 got_repo_close(repo);
2180 if (worktree)
2181 got_worktree_close(worktree);
2182 free(path);
2183 free(cwd);
2184 return error;
2187 __dead static void
2188 usage_revert(void)
2190 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2191 exit(1);
2194 static void
2195 revert_progress(void *arg, unsigned char status, const char *path)
2197 while (path[0] == '/')
2198 path++;
2199 printf("%c %s\n", status, path);
2202 static const struct got_error *
2203 cmd_revert(int argc, char *argv[])
2205 const struct got_error *error = NULL;
2206 struct got_worktree *worktree = NULL;
2207 struct got_repository *repo = NULL;
2208 char *cwd = NULL, *path = NULL;
2209 int ch;
2211 while ((ch = getopt(argc, argv, "")) != -1) {
2212 switch (ch) {
2213 default:
2214 usage_revert();
2215 /* NOTREACHED */
2219 argc -= optind;
2220 argv += optind;
2222 if (argc != 1)
2223 usage_revert();
2225 path = realpath(argv[0], NULL);
2226 if (path == NULL) {
2227 error = got_error_from_errno2("realpath", argv[0]);
2228 goto done;
2230 got_path_strip_trailing_slashes(path);
2232 cwd = getcwd(NULL, 0);
2233 if (cwd == NULL) {
2234 error = got_error_from_errno("getcwd");
2235 goto done;
2237 error = got_worktree_open(&worktree, cwd);
2238 if (error)
2239 goto done;
2241 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2242 if (error != NULL)
2243 goto done;
2245 error = apply_unveil(got_repo_get_path(repo), 1,
2246 got_worktree_get_root_path(worktree), 0);
2247 if (error)
2248 goto done;
2250 error = got_worktree_revert(worktree, path,
2251 revert_progress, NULL, repo);
2252 if (error)
2253 goto done;
2254 done:
2255 if (repo)
2256 got_repo_close(repo);
2257 if (worktree)
2258 got_worktree_close(worktree);
2259 free(path);
2260 free(cwd);
2261 return error;
2264 __dead static void
2265 usage_commit(void)
2267 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2268 exit(1);
2271 int
2272 spawn_editor(const char *editor, const char *file)
2274 pid_t pid;
2275 sig_t sighup, sigint, sigquit;
2276 int st = -1;
2278 sighup = signal(SIGHUP, SIG_IGN);
2279 sigint = signal(SIGINT, SIG_IGN);
2280 sigquit = signal(SIGQUIT, SIG_IGN);
2282 switch (pid = fork()) {
2283 case -1:
2284 goto doneediting;
2285 case 0:
2286 execl(editor, editor, file, (char *)NULL);
2287 _exit(127);
2290 while (waitpid(pid, &st, 0) == -1)
2291 if (errno != EINTR)
2292 break;
2294 doneediting:
2295 (void)signal(SIGHUP, sighup);
2296 (void)signal(SIGINT, sigint);
2297 (void)signal(SIGQUIT, sigquit);
2299 if (!WIFEXITED(st)) {
2300 errno = EINTR;
2301 return -1;
2304 return WEXITSTATUS(st);
2307 struct collect_commit_logmsg_arg {
2308 const char *cmdline_log;
2309 const char *editor;
2310 const char *worktree_path;
2311 const char *repo_path;
2312 char *logmsg_path;
2316 static const struct got_error *
2317 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2318 void *arg)
2320 const char *initial_content = "\n# changes to be committed:\n";
2321 struct got_pathlist_entry *pe;
2322 const struct got_error *err = NULL;
2323 char *template = NULL;
2324 struct collect_commit_logmsg_arg *a = arg;
2325 char buf[1024];
2326 struct stat st, st2;
2327 FILE *fp;
2328 size_t len;
2329 int fd, content_changed = 0;
2331 /* if a message was specified on the command line, just use it */
2332 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2333 len = strlen(a->cmdline_log) + 1;
2334 *logmsg = malloc(len + 1);
2335 if (*logmsg == NULL)
2336 return got_error_from_errno("malloc");
2337 strlcpy(*logmsg, a->cmdline_log, len);
2338 return NULL;
2341 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2342 return got_error_from_errno("asprintf");
2344 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2345 if (err)
2346 goto done;
2348 dprintf(fd, initial_content);
2350 TAILQ_FOREACH(pe, commitable_paths, entry) {
2351 struct got_commitable *ct = pe->data;
2352 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2354 close(fd);
2356 if (stat(a->logmsg_path, &st) == -1) {
2357 err = got_error_from_errno2("stat", a->logmsg_path);
2358 goto done;
2361 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2362 err = got_error_from_errno("failed spawning editor");
2363 goto done;
2366 if (stat(a->logmsg_path, &st2) == -1) {
2367 err = got_error_from_errno("stat");
2368 goto done;
2371 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2372 unlink(a->logmsg_path);
2373 free(a->logmsg_path);
2374 a->logmsg_path = NULL;
2375 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2376 "no changes made to commit message, aborting");
2377 goto done;
2380 /* remove comments */
2381 *logmsg = malloc(st2.st_size + 1);
2382 if (*logmsg == NULL) {
2383 err = got_error_from_errno("malloc");
2384 goto done;
2386 len = 0;
2388 fp = fopen(a->logmsg_path, "r");
2389 while (fgets(buf, sizeof(buf), fp) != NULL) {
2390 if (!content_changed && strcmp(buf, initial_content) != 0)
2391 content_changed = 1;
2392 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2393 continue;
2394 len = strlcat(*logmsg, buf, st2.st_size);
2396 fclose(fp);
2398 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2399 (*logmsg)[len - 1] = '\0';
2400 len--;
2403 if (len == 0 || !content_changed) {
2404 unlink(a->logmsg_path);
2405 free(a->logmsg_path);
2406 a->logmsg_path = NULL;
2407 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2408 "commit message cannot be empty, aborting");
2409 goto done;
2411 done:
2412 free(template);
2414 /* Editor is done; we can now apply unveil(2) */
2415 if (err == NULL)
2416 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2417 return err;
2420 static const struct got_error *
2421 cmd_commit(int argc, char *argv[])
2423 const struct got_error *error = NULL;
2424 struct got_worktree *worktree = NULL;
2425 struct got_repository *repo = NULL;
2426 char *cwd = NULL, *path = NULL, *id_str = NULL;
2427 struct got_object_id *id = NULL;
2428 const char *logmsg = NULL;
2429 const char *got_author = getenv("GOT_AUTHOR");
2430 struct collect_commit_logmsg_arg cl_arg;
2431 char *editor = NULL;
2432 int ch;
2434 while ((ch = getopt(argc, argv, "m:")) != -1) {
2435 switch (ch) {
2436 case 'm':
2437 logmsg = optarg;
2438 break;
2439 default:
2440 usage_commit();
2441 /* NOTREACHED */
2445 argc -= optind;
2446 argv += optind;
2448 if (argc == 1) {
2449 path = realpath(argv[0], NULL);
2450 if (path == NULL) {
2451 error = got_error_from_errno2("realpath", argv[0]);
2452 goto done;
2454 got_path_strip_trailing_slashes(path);
2455 } else if (argc != 0)
2456 usage_commit();
2458 if (got_author == NULL) {
2459 /* TODO: Look current user up in password database */
2460 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2461 goto done;
2464 cwd = getcwd(NULL, 0);
2465 if (cwd == NULL) {
2466 error = got_error_from_errno("getcwd");
2467 goto done;
2469 error = got_worktree_open(&worktree, cwd);
2470 if (error)
2471 goto done;
2473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2474 if (error != NULL)
2475 goto done;
2478 * unveil(2) traverses exec(2); if an editor is used we have
2479 * to apply unveil after the log message has been written.
2481 if (logmsg == NULL || strlen(logmsg) == 0)
2482 error = get_editor(&editor);
2483 else
2484 error = apply_unveil(got_repo_get_path(repo), 0,
2485 got_worktree_get_root_path(worktree), 0);
2486 if (error)
2487 goto done;
2489 cl_arg.editor = editor;
2490 cl_arg.cmdline_log = logmsg;
2491 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2492 cl_arg.repo_path = got_repo_get_path(repo);
2493 cl_arg.logmsg_path = NULL;
2494 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2495 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2496 if (error) {
2497 if (cl_arg.logmsg_path)
2498 fprintf(stderr, "%s: log message preserved in %s\n",
2499 getprogname(), cl_arg.logmsg_path);
2500 goto done;
2503 if (cl_arg.logmsg_path)
2504 unlink(cl_arg.logmsg_path);
2506 error = got_object_id_str(&id_str, id);
2507 if (error)
2508 goto done;
2509 printf("created commit %s\n", id_str);
2510 done:
2511 if (repo)
2512 got_repo_close(repo);
2513 if (worktree)
2514 got_worktree_close(worktree);
2515 free(path);
2516 free(cwd);
2517 free(id_str);
2518 free(editor);
2519 return error;