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, "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, GOT_REF_HEAD, 0);
1048 if (error != NULL)
1049 return error;
1050 error = got_ref_resolve(&id, repo, head_ref);
1051 got_ref_close(head_ref);
1052 if (error != NULL)
1053 return error;
1054 error = got_object_open_as_commit(&commit, repo, id);
1055 } else {
1056 struct got_reference *ref;
1057 error = got_ref_open(&ref, repo, start_commit, 0);
1058 if (error == NULL) {
1059 int obj_type;
1060 error = got_ref_resolve(&id, repo, ref);
1061 got_ref_close(ref);
1062 if (error != NULL)
1063 goto done;
1064 error = got_object_get_type(&obj_type, repo, id);
1065 if (error != NULL)
1066 goto done;
1067 if (obj_type == GOT_OBJ_TYPE_TAG) {
1068 struct got_tag_object *tag;
1069 error = got_object_open_as_tag(&tag, repo, id);
1070 if (error != NULL)
1071 goto done;
1072 if (got_object_tag_get_object_type(tag) !=
1073 GOT_OBJ_TYPE_COMMIT) {
1074 got_object_tag_close(tag);
1075 error = got_error(GOT_ERR_OBJ_TYPE);
1076 goto done;
1078 free(id);
1079 id = got_object_id_dup(
1080 got_object_tag_get_object_id(tag));
1081 if (id == NULL)
1082 error = got_error_from_errno(
1083 "got_object_id_dup");
1084 got_object_tag_close(tag);
1085 if (error)
1086 goto done;
1087 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1088 error = got_error(GOT_ERR_OBJ_TYPE);
1089 goto done;
1091 error = got_object_open_as_commit(&commit, repo, id);
1092 if (error != NULL)
1093 goto done;
1095 if (commit == NULL) {
1096 error = got_object_resolve_id_str(&id, repo,
1097 start_commit);
1098 if (error != NULL)
1099 return error;
1102 if (error != NULL)
1103 goto done;
1105 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1106 if (error != NULL)
1107 goto done;
1108 if (in_repo_path) {
1109 free(path);
1110 path = in_repo_path;
1113 error = got_ref_list(&refs, repo);
1114 if (error)
1115 goto done;
1117 error = print_commits(id, repo, path, show_patch,
1118 diff_context, limit, first_parent_traversal, &refs);
1119 done:
1120 free(path);
1121 free(repo_path);
1122 free(cwd);
1123 free(id);
1124 if (worktree)
1125 got_worktree_close(worktree);
1126 if (repo) {
1127 const struct got_error *repo_error;
1128 repo_error = got_repo_close(repo);
1129 if (error == NULL)
1130 error = repo_error;
1132 got_ref_list_free(&refs);
1133 return error;
1136 __dead static void
1137 usage_diff(void)
1139 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1140 "[object1 object2 | path]\n", getprogname());
1141 exit(1);
1144 struct print_diff_arg {
1145 struct got_repository *repo;
1146 struct got_worktree *worktree;
1147 int diff_context;
1148 const char *id_str;
1149 int header_shown;
1152 static const struct got_error *
1153 print_diff(void *arg, unsigned char status, const char *path,
1154 struct got_object_id *blob_id, struct got_object_id *commit_id)
1156 struct print_diff_arg *a = arg;
1157 const struct got_error *err = NULL;
1158 struct got_blob_object *blob1 = NULL;
1159 FILE *f2 = NULL;
1160 char *abspath = NULL;
1161 struct stat sb;
1163 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1164 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1165 return NULL;
1167 if (!a->header_shown) {
1168 printf("diff %s %s\n", a->id_str,
1169 got_worktree_get_root_path(a->worktree));
1170 a->header_shown = 1;
1173 if (status != GOT_STATUS_ADD) {
1174 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1175 if (err)
1176 goto done;
1180 if (status != GOT_STATUS_DELETE) {
1181 if (asprintf(&abspath, "%s/%s",
1182 got_worktree_get_root_path(a->worktree), path) == -1) {
1183 err = got_error_from_errno("asprintf");
1184 goto done;
1187 f2 = fopen(abspath, "r");
1188 if (f2 == NULL) {
1189 err = got_error_from_errno2("fopen", abspath);
1190 goto done;
1192 if (lstat(abspath, &sb) == -1) {
1193 err = got_error_from_errno2("lstat", abspath);
1194 goto done;
1196 } else
1197 sb.st_size = 0;
1199 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1200 stdout);
1201 done:
1202 if (blob1)
1203 got_object_blob_close(blob1);
1204 if (f2 && fclose(f2) != 0 && err == NULL)
1205 err = got_error_from_errno("fclose");
1206 free(abspath);
1207 return err;
1210 static const struct got_error *
1211 cmd_diff(int argc, char *argv[])
1213 const struct got_error *error;
1214 struct got_repository *repo = NULL;
1215 struct got_worktree *worktree = NULL;
1216 char *cwd = NULL, *repo_path = NULL;
1217 struct got_object_id *id1 = NULL, *id2 = NULL;
1218 char *id_str1 = NULL, *id_str2 = NULL;
1219 int type1, type2;
1220 int diff_context = 3, ch;
1221 const char *errstr;
1222 char *path = NULL;
1224 #ifndef PROFILE
1225 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1226 NULL) == -1)
1227 err(1, "pledge");
1228 #endif
1230 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1231 switch (ch) {
1232 case 'C':
1233 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1234 if (errstr != NULL)
1235 err(1, "-C option %s", errstr);
1236 break;
1237 case 'r':
1238 repo_path = realpath(optarg, NULL);
1239 if (repo_path == NULL)
1240 err(1, "-r option");
1241 got_path_strip_trailing_slashes(repo_path);
1242 break;
1243 default:
1244 usage_diff();
1245 /* NOTREACHED */
1249 argc -= optind;
1250 argv += optind;
1252 cwd = getcwd(NULL, 0);
1253 if (cwd == NULL) {
1254 error = got_error_from_errno("getcwd");
1255 goto done;
1257 error = got_worktree_open(&worktree, cwd);
1258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1259 goto done;
1260 if (argc <= 1) {
1261 if (worktree == NULL) {
1262 error = got_error(GOT_ERR_NOT_WORKTREE);
1263 goto done;
1265 if (repo_path)
1266 errx(1,
1267 "-r option can't be used when diffing a work tree");
1268 repo_path = strdup(got_worktree_get_repo_path(worktree));
1269 if (repo_path == NULL) {
1270 error = got_error_from_errno("strdup");
1271 goto done;
1273 if (argc == 1) {
1274 error = got_worktree_resolve_path(&path, worktree,
1275 argv[0]);
1276 if (error)
1277 goto done;
1278 } else {
1279 path = strdup("");
1280 if (path == NULL) {
1281 error = got_error_from_errno("strdup");
1282 goto done;
1285 } else if (argc == 2) {
1286 id_str1 = argv[0];
1287 id_str2 = argv[1];
1288 } else
1289 usage_diff();
1291 if (repo_path == NULL) {
1292 repo_path = getcwd(NULL, 0);
1293 if (repo_path == NULL)
1294 return got_error_from_errno("getcwd");
1297 error = got_repo_open(&repo, repo_path);
1298 free(repo_path);
1299 if (error != NULL)
1300 goto done;
1302 error = apply_unveil(got_repo_get_path(repo), 1,
1303 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1304 if (error)
1305 goto done;
1307 if (worktree) {
1308 struct print_diff_arg arg;
1309 char *id_str;
1310 error = got_object_id_str(&id_str,
1311 got_worktree_get_base_commit_id(worktree));
1312 if (error)
1313 goto done;
1314 arg.repo = repo;
1315 arg.worktree = worktree;
1316 arg.diff_context = diff_context;
1317 arg.id_str = id_str;
1318 arg.header_shown = 0;
1320 error = got_worktree_status(worktree, path, repo, print_diff,
1321 &arg, check_cancelled, NULL);
1322 free(id_str);
1323 goto done;
1326 error = got_object_resolve_id_str(&id1, repo, id_str1);
1327 if (error)
1328 goto done;
1330 error = got_object_resolve_id_str(&id2, repo, id_str2);
1331 if (error)
1332 goto done;
1334 error = got_object_get_type(&type1, repo, id1);
1335 if (error)
1336 goto done;
1338 error = got_object_get_type(&type2, repo, id2);
1339 if (error)
1340 goto done;
1342 if (type1 != type2) {
1343 error = got_error(GOT_ERR_OBJ_TYPE);
1344 goto done;
1347 switch (type1) {
1348 case GOT_OBJ_TYPE_BLOB:
1349 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1350 diff_context, repo, stdout);
1351 break;
1352 case GOT_OBJ_TYPE_TREE:
1353 error = got_diff_objects_as_trees(id1, id2, "", "",
1354 diff_context, repo, stdout);
1355 break;
1356 case GOT_OBJ_TYPE_COMMIT:
1357 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1358 id_str2);
1359 error = got_diff_objects_as_commits(id1, id2, diff_context,
1360 repo, stdout);
1361 break;
1362 default:
1363 error = got_error(GOT_ERR_OBJ_TYPE);
1366 done:
1367 free(id1);
1368 free(id2);
1369 free(path);
1370 if (worktree)
1371 got_worktree_close(worktree);
1372 if (repo) {
1373 const struct got_error *repo_error;
1374 repo_error = got_repo_close(repo);
1375 if (error == NULL)
1376 error = repo_error;
1378 return error;
1381 __dead static void
1382 usage_blame(void)
1384 fprintf(stderr,
1385 "usage: %s blame [-c commit] [-r repository-path] path\n",
1386 getprogname());
1387 exit(1);
1390 static const struct got_error *
1391 cmd_blame(int argc, char *argv[])
1393 const struct got_error *error;
1394 struct got_repository *repo = NULL;
1395 struct got_worktree *worktree = NULL;
1396 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1397 struct got_object_id *commit_id = NULL;
1398 char *commit_id_str = NULL;
1399 int ch;
1401 #ifndef PROFILE
1402 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1403 NULL) == -1)
1404 err(1, "pledge");
1405 #endif
1407 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1408 switch (ch) {
1409 case 'c':
1410 commit_id_str = optarg;
1411 break;
1412 case 'r':
1413 repo_path = realpath(optarg, NULL);
1414 if (repo_path == NULL)
1415 err(1, "-r option");
1416 got_path_strip_trailing_slashes(repo_path);
1417 break;
1418 default:
1419 usage_blame();
1420 /* NOTREACHED */
1424 argc -= optind;
1425 argv += optind;
1427 if (argc == 1)
1428 path = argv[0];
1429 else
1430 usage_blame();
1432 cwd = getcwd(NULL, 0);
1433 if (cwd == NULL) {
1434 error = got_error_from_errno("getcwd");
1435 goto done;
1437 if (repo_path == NULL) {
1438 error = got_worktree_open(&worktree, cwd);
1439 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1440 goto done;
1441 else
1442 error = NULL;
1443 if (worktree) {
1444 repo_path =
1445 strdup(got_worktree_get_repo_path(worktree));
1446 if (repo_path == NULL)
1447 error = got_error_from_errno("strdup");
1448 if (error)
1449 goto done;
1450 } else {
1451 repo_path = strdup(cwd);
1452 if (repo_path == NULL) {
1453 error = got_error_from_errno("strdup");
1454 goto done;
1459 error = got_repo_open(&repo, repo_path);
1460 if (error != NULL)
1461 goto done;
1463 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1464 if (error)
1465 goto done;
1467 if (worktree) {
1468 const char *prefix = got_worktree_get_path_prefix(worktree);
1469 char *p, *worktree_subdir = cwd +
1470 strlen(got_worktree_get_root_path(worktree));
1471 if (asprintf(&p, "%s%s%s%s%s",
1472 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1473 worktree_subdir, worktree_subdir[0] ? "/" : "",
1474 path) == -1) {
1475 error = got_error_from_errno("asprintf");
1476 goto done;
1478 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1479 free(p);
1480 } else {
1481 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1483 if (error)
1484 goto done;
1486 if (commit_id_str == NULL) {
1487 struct got_reference *head_ref;
1488 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1489 if (error != NULL)
1490 goto done;
1491 error = got_ref_resolve(&commit_id, repo, head_ref);
1492 got_ref_close(head_ref);
1493 if (error != NULL)
1494 goto done;
1495 } else {
1496 error = got_object_resolve_id_str(&commit_id, repo,
1497 commit_id_str);
1498 if (error != NULL)
1499 goto done;
1502 error = got_blame(in_repo_path, commit_id, repo, stdout);
1503 done:
1504 free(in_repo_path);
1505 free(repo_path);
1506 free(cwd);
1507 free(commit_id);
1508 if (worktree)
1509 got_worktree_close(worktree);
1510 if (repo) {
1511 const struct got_error *repo_error;
1512 repo_error = got_repo_close(repo);
1513 if (error == NULL)
1514 error = repo_error;
1516 return error;
1519 __dead static void
1520 usage_tree(void)
1522 fprintf(stderr,
1523 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1524 getprogname());
1525 exit(1);
1528 static void
1529 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1530 const char *root_path)
1532 int is_root_path = (strcmp(path, root_path) == 0);
1534 path += strlen(root_path);
1535 while (path[0] == '/')
1536 path++;
1538 printf("%s%s%s%s%s\n", id ? id : "", path,
1539 is_root_path ? "" : "/", te->name,
1540 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1543 static const struct got_error *
1544 print_tree(const char *path, struct got_object_id *commit_id,
1545 int show_ids, int recurse, const char *root_path,
1546 struct got_repository *repo)
1548 const struct got_error *err = NULL;
1549 struct got_object_id *tree_id = NULL;
1550 struct got_tree_object *tree = NULL;
1551 const struct got_tree_entries *entries;
1552 struct got_tree_entry *te;
1554 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1555 if (err)
1556 goto done;
1558 err = got_object_open_as_tree(&tree, repo, tree_id);
1559 if (err)
1560 goto done;
1561 entries = got_object_tree_get_entries(tree);
1562 te = SIMPLEQ_FIRST(&entries->head);
1563 while (te) {
1564 char *id = NULL;
1566 if (sigint_received || sigpipe_received)
1567 break;
1569 if (show_ids) {
1570 char *id_str;
1571 err = got_object_id_str(&id_str, te->id);
1572 if (err)
1573 goto done;
1574 if (asprintf(&id, "%s ", id_str) == -1) {
1575 err = got_error_from_errno("asprintf");
1576 free(id_str);
1577 goto done;
1579 free(id_str);
1581 print_entry(te, id, path, root_path);
1582 free(id);
1584 if (recurse && S_ISDIR(te->mode)) {
1585 char *child_path;
1586 if (asprintf(&child_path, "%s%s%s", path,
1587 path[0] == '/' && path[1] == '\0' ? "" : "/",
1588 te->name) == -1) {
1589 err = got_error_from_errno("asprintf");
1590 goto done;
1592 err = print_tree(child_path, commit_id, show_ids, 1,
1593 root_path, repo);
1594 free(child_path);
1595 if (err)
1596 goto done;
1599 te = SIMPLEQ_NEXT(te, entry);
1601 done:
1602 if (tree)
1603 got_object_tree_close(tree);
1604 free(tree_id);
1605 return err;
1608 static const struct got_error *
1609 cmd_tree(int argc, char *argv[])
1611 const struct got_error *error;
1612 struct got_repository *repo = NULL;
1613 struct got_worktree *worktree = NULL;
1614 const char *path;
1615 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1616 struct got_object_id *commit_id = NULL;
1617 char *commit_id_str = NULL;
1618 int show_ids = 0, recurse = 0;
1619 int ch;
1621 #ifndef PROFILE
1622 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1623 NULL) == -1)
1624 err(1, "pledge");
1625 #endif
1627 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1628 switch (ch) {
1629 case 'c':
1630 commit_id_str = optarg;
1631 break;
1632 case 'r':
1633 repo_path = realpath(optarg, NULL);
1634 if (repo_path == NULL)
1635 err(1, "-r option");
1636 got_path_strip_trailing_slashes(repo_path);
1637 break;
1638 case 'i':
1639 show_ids = 1;
1640 break;
1641 case 'R':
1642 recurse = 1;
1643 break;
1644 default:
1645 usage_tree();
1646 /* NOTREACHED */
1650 argc -= optind;
1651 argv += optind;
1653 if (argc == 1)
1654 path = argv[0];
1655 else if (argc > 1)
1656 usage_tree();
1657 else
1658 path = NULL;
1660 cwd = getcwd(NULL, 0);
1661 if (cwd == NULL) {
1662 error = got_error_from_errno("getcwd");
1663 goto done;
1665 if (repo_path == NULL) {
1666 error = got_worktree_open(&worktree, cwd);
1667 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1668 goto done;
1669 else
1670 error = NULL;
1671 if (worktree) {
1672 repo_path =
1673 strdup(got_worktree_get_repo_path(worktree));
1674 if (repo_path == NULL)
1675 error = got_error_from_errno("strdup");
1676 if (error)
1677 goto done;
1678 } else {
1679 repo_path = strdup(cwd);
1680 if (repo_path == NULL) {
1681 error = got_error_from_errno("strdup");
1682 goto done;
1687 error = got_repo_open(&repo, repo_path);
1688 if (error != NULL)
1689 goto done;
1691 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1692 if (error)
1693 goto done;
1695 if (path == NULL) {
1696 if (worktree) {
1697 char *p, *worktree_subdir = cwd +
1698 strlen(got_worktree_get_root_path(worktree));
1699 if (asprintf(&p, "%s/%s",
1700 got_worktree_get_path_prefix(worktree),
1701 worktree_subdir) == -1) {
1702 error = got_error_from_errno("asprintf");
1703 goto done;
1705 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1706 free(p);
1707 if (error)
1708 goto done;
1709 } else
1710 path = "/";
1712 if (in_repo_path == NULL) {
1713 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1714 if (error != NULL)
1715 goto done;
1718 if (commit_id_str == NULL) {
1719 struct got_reference *head_ref;
1720 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1721 if (error != NULL)
1722 goto done;
1723 error = got_ref_resolve(&commit_id, repo, head_ref);
1724 got_ref_close(head_ref);
1725 if (error != NULL)
1726 goto done;
1727 } else {
1728 error = got_object_resolve_id_str(&commit_id, repo,
1729 commit_id_str);
1730 if (error != NULL)
1731 goto done;
1734 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1735 in_repo_path, repo);
1736 done:
1737 free(in_repo_path);
1738 free(repo_path);
1739 free(cwd);
1740 free(commit_id);
1741 if (worktree)
1742 got_worktree_close(worktree);
1743 if (repo) {
1744 const struct got_error *repo_error;
1745 repo_error = got_repo_close(repo);
1746 if (error == NULL)
1747 error = repo_error;
1749 return error;
1752 __dead static void
1753 usage_status(void)
1755 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1756 exit(1);
1759 static const struct got_error *
1760 print_status(void *arg, unsigned char status, const char *path,
1761 struct got_object_id *blob_id, struct got_object_id *commit_id)
1763 printf("%c %s\n", status, path);
1764 return NULL;
1767 static const struct got_error *
1768 cmd_status(int argc, char *argv[])
1770 const struct got_error *error = NULL;
1771 struct got_repository *repo = NULL;
1772 struct got_worktree *worktree = NULL;
1773 char *cwd = NULL, *path = NULL;
1774 int ch;
1776 while ((ch = getopt(argc, argv, "")) != -1) {
1777 switch (ch) {
1778 default:
1779 usage_status();
1780 /* NOTREACHED */
1784 argc -= optind;
1785 argv += optind;
1787 #ifndef PROFILE
1788 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1789 NULL) == -1)
1790 err(1, "pledge");
1791 #endif
1792 cwd = getcwd(NULL, 0);
1793 if (cwd == NULL) {
1794 error = got_error_from_errno("getcwd");
1795 goto done;
1798 error = got_worktree_open(&worktree, cwd);
1799 if (error != NULL)
1800 goto done;
1802 if (argc == 0) {
1803 path = strdup("");
1804 if (path == NULL) {
1805 error = got_error_from_errno("strdup");
1806 goto done;
1808 } else if (argc == 1) {
1809 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1810 if (error)
1811 goto done;
1812 } else
1813 usage_status();
1815 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1816 if (error != NULL)
1817 goto done;
1819 error = apply_unveil(got_repo_get_path(repo), 1,
1820 got_worktree_get_root_path(worktree), 0);
1821 if (error)
1822 goto done;
1824 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1825 check_cancelled, NULL);
1826 done:
1827 free(cwd);
1828 free(path);
1829 return error;
1832 __dead static void
1833 usage_ref(void)
1835 fprintf(stderr,
1836 "usage: %s ref [-r repository] -l | -d name | name target\n",
1837 getprogname());
1838 exit(1);
1841 static const struct got_error *
1842 list_refs(struct got_repository *repo)
1844 static const struct got_error *err = NULL;
1845 struct got_reflist_head refs;
1846 struct got_reflist_entry *re;
1848 SIMPLEQ_INIT(&refs);
1849 err = got_ref_list(&refs, repo);
1850 if (err)
1851 return err;
1853 SIMPLEQ_FOREACH(re, &refs, entry) {
1854 char *refstr;
1855 refstr = got_ref_to_str(re->ref);
1856 if (refstr == NULL)
1857 return got_error_from_errno("got_ref_to_str");
1858 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1859 free(refstr);
1862 got_ref_list_free(&refs);
1863 return NULL;
1866 static const struct got_error *
1867 delete_ref(struct got_repository *repo, const char *refname)
1869 const struct got_error *err = NULL;
1870 struct got_reference *ref;
1872 err = got_ref_open(&ref, repo, refname, 0);
1873 if (err)
1874 return err;
1876 err = got_ref_delete(ref, repo);
1877 got_ref_close(ref);
1878 return err;
1881 static const struct got_error *
1882 add_ref(struct got_repository *repo, const char *refname, const char *target)
1884 const struct got_error *err = NULL;
1885 struct got_object_id *id;
1886 struct got_reference *ref = NULL;
1888 err = got_object_resolve_id_str(&id, repo, target);
1889 if (err) {
1890 struct got_reference *target_ref;
1892 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1893 return err;
1894 err = got_ref_open(&target_ref, repo, target, 0);
1895 if (err)
1896 return err;
1897 err = got_ref_resolve(&id, repo, target_ref);
1898 got_ref_close(target_ref);
1899 if (err)
1900 return err;
1903 err = got_ref_alloc(&ref, refname, id);
1904 if (err)
1905 goto done;
1907 err = got_ref_write(ref, repo);
1908 done:
1909 if (ref)
1910 got_ref_close(ref);
1911 free(id);
1912 return err;
1915 static const struct got_error *
1916 cmd_ref(int argc, char *argv[])
1918 const struct got_error *error = NULL;
1919 struct got_repository *repo = NULL;
1920 struct got_worktree *worktree = NULL;
1921 char *cwd = NULL, *repo_path = NULL;
1922 int ch, do_list = 0;
1923 const char *delref = NULL;
1925 /* TODO: Add -s option for adding symbolic references. */
1926 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1927 switch (ch) {
1928 case 'd':
1929 delref = optarg;
1930 break;
1931 case 'r':
1932 repo_path = realpath(optarg, NULL);
1933 if (repo_path == NULL)
1934 err(1, "-r option");
1935 got_path_strip_trailing_slashes(repo_path);
1936 break;
1937 case 'l':
1938 do_list = 1;
1939 break;
1940 default:
1941 usage_ref();
1942 /* NOTREACHED */
1946 if (do_list && delref)
1947 errx(1, "-l and -d options are mutually exclusive\n");
1949 argc -= optind;
1950 argv += optind;
1952 if (do_list || delref) {
1953 if (argc > 0)
1954 usage_ref();
1955 } else if (argc != 2)
1956 usage_ref();
1958 #ifndef PROFILE
1959 if (do_list) {
1960 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1961 NULL) == -1)
1962 err(1, "pledge");
1963 } else {
1964 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1965 "sendfd unveil", NULL) == -1)
1966 err(1, "pledge");
1968 #endif
1969 cwd = getcwd(NULL, 0);
1970 if (cwd == NULL) {
1971 error = got_error_from_errno("getcwd");
1972 goto done;
1975 if (repo_path == NULL) {
1976 error = got_worktree_open(&worktree, cwd);
1977 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1978 goto done;
1979 else
1980 error = NULL;
1981 if (worktree) {
1982 repo_path =
1983 strdup(got_worktree_get_repo_path(worktree));
1984 if (repo_path == NULL)
1985 error = got_error_from_errno("strdup");
1986 if (error)
1987 goto done;
1988 } else {
1989 repo_path = strdup(cwd);
1990 if (repo_path == NULL) {
1991 error = got_error_from_errno("strdup");
1992 goto done;
1997 error = got_repo_open(&repo, repo_path);
1998 if (error != NULL)
1999 goto done;
2001 error = apply_unveil(got_repo_get_path(repo), do_list,
2002 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2003 if (error)
2004 goto done;
2006 if (do_list)
2007 error = list_refs(repo);
2008 else if (delref)
2009 error = delete_ref(repo, delref);
2010 else
2011 error = add_ref(repo, argv[0], argv[1]);
2012 done:
2013 if (repo)
2014 got_repo_close(repo);
2015 if (worktree)
2016 got_worktree_close(worktree);
2017 free(cwd);
2018 free(repo_path);
2019 return error;
2022 __dead static void
2023 usage_add(void)
2025 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2026 exit(1);
2029 static const struct got_error *
2030 cmd_add(int argc, char *argv[])
2032 const struct got_error *error = NULL;
2033 struct got_repository *repo = NULL;
2034 struct got_worktree *worktree = NULL;
2035 char *cwd = NULL;
2036 struct got_pathlist_head paths;
2037 struct got_pathlist_entry *pe;
2038 int ch, x;
2040 TAILQ_INIT(&paths);
2042 while ((ch = getopt(argc, argv, "")) != -1) {
2043 switch (ch) {
2044 default:
2045 usage_add();
2046 /* NOTREACHED */
2050 argc -= optind;
2051 argv += optind;
2053 if (argc < 1)
2054 usage_add();
2056 /* make sure each file exists before doing anything halfway */
2057 for (x = 0; x < argc; x++) {
2058 char *path = realpath(argv[x], NULL);
2059 if (path == NULL) {
2060 error = got_error_from_errno2("realpath", argv[x]);
2061 goto done;
2063 free(path);
2066 cwd = getcwd(NULL, 0);
2067 if (cwd == NULL) {
2068 error = got_error_from_errno("getcwd");
2069 goto done;
2072 error = got_worktree_open(&worktree, cwd);
2073 if (error)
2074 goto done;
2076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2077 if (error != NULL)
2078 goto done;
2080 error = apply_unveil(got_repo_get_path(repo), 1,
2081 got_worktree_get_root_path(worktree), 0);
2082 if (error)
2083 goto done;
2085 for (x = 0; x < argc; x++) {
2086 char *path = realpath(argv[x], NULL);
2087 if (path == NULL) {
2088 error = got_error_from_errno2("realpath", argv[x]);
2089 goto done;
2092 got_path_strip_trailing_slashes(path);
2093 error = got_pathlist_insert(&pe, &paths, path, NULL);
2094 if (error) {
2095 free(path);
2096 goto done;
2099 error = got_worktree_schedule_add(worktree, &paths, print_status,
2100 NULL, repo);
2101 done:
2102 if (repo)
2103 got_repo_close(repo);
2104 if (worktree)
2105 got_worktree_close(worktree);
2106 TAILQ_FOREACH(pe, &paths, entry)
2107 free((char *)pe->path);
2108 got_pathlist_free(&paths);
2109 free(cwd);
2110 return error;
2113 __dead static void
2114 usage_rm(void)
2116 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2117 exit(1);
2120 static const struct got_error *
2121 cmd_rm(int argc, char *argv[])
2123 const struct got_error *error = NULL;
2124 struct got_worktree *worktree = NULL;
2125 struct got_repository *repo = NULL;
2126 char *cwd = NULL, *path = NULL;
2127 int ch, delete_local_mods = 0;
2129 while ((ch = getopt(argc, argv, "f")) != -1) {
2130 switch (ch) {
2131 case 'f':
2132 delete_local_mods = 1;
2133 break;
2134 default:
2135 usage_add();
2136 /* NOTREACHED */
2140 argc -= optind;
2141 argv += optind;
2143 if (argc != 1)
2144 usage_rm();
2146 path = realpath(argv[0], NULL);
2147 if (path == NULL) {
2148 error = got_error_from_errno2("realpath", argv[0]);
2149 goto done;
2151 got_path_strip_trailing_slashes(path);
2153 cwd = getcwd(NULL, 0);
2154 if (cwd == NULL) {
2155 error = got_error_from_errno("getcwd");
2156 goto done;
2158 error = got_worktree_open(&worktree, cwd);
2159 if (error)
2160 goto done;
2162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2163 if (error)
2164 goto done;
2166 error = apply_unveil(got_repo_get_path(repo), 1,
2167 got_worktree_get_root_path(worktree), 0);
2168 if (error)
2169 goto done;
2171 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2172 print_status, NULL, repo);
2173 if (error)
2174 goto done;
2175 done:
2176 if (repo)
2177 got_repo_close(repo);
2178 if (worktree)
2179 got_worktree_close(worktree);
2180 free(path);
2181 free(cwd);
2182 return error;
2185 __dead static void
2186 usage_revert(void)
2188 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2189 exit(1);
2192 static void
2193 revert_progress(void *arg, unsigned char status, const char *path)
2195 while (path[0] == '/')
2196 path++;
2197 printf("%c %s\n", status, path);
2200 static const struct got_error *
2201 cmd_revert(int argc, char *argv[])
2203 const struct got_error *error = NULL;
2204 struct got_worktree *worktree = NULL;
2205 struct got_repository *repo = NULL;
2206 char *cwd = NULL, *path = NULL;
2207 int ch;
2209 while ((ch = getopt(argc, argv, "")) != -1) {
2210 switch (ch) {
2211 default:
2212 usage_revert();
2213 /* NOTREACHED */
2217 argc -= optind;
2218 argv += optind;
2220 if (argc != 1)
2221 usage_revert();
2223 path = realpath(argv[0], NULL);
2224 if (path == NULL) {
2225 error = got_error_from_errno2("realpath", argv[0]);
2226 goto done;
2228 got_path_strip_trailing_slashes(path);
2230 cwd = getcwd(NULL, 0);
2231 if (cwd == NULL) {
2232 error = got_error_from_errno("getcwd");
2233 goto done;
2235 error = got_worktree_open(&worktree, cwd);
2236 if (error)
2237 goto done;
2239 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2240 if (error != NULL)
2241 goto done;
2243 error = apply_unveil(got_repo_get_path(repo), 1,
2244 got_worktree_get_root_path(worktree), 0);
2245 if (error)
2246 goto done;
2248 error = got_worktree_revert(worktree, path,
2249 revert_progress, NULL, repo);
2250 if (error)
2251 goto done;
2252 done:
2253 if (repo)
2254 got_repo_close(repo);
2255 if (worktree)
2256 got_worktree_close(worktree);
2257 free(path);
2258 free(cwd);
2259 return error;
2262 __dead static void
2263 usage_commit(void)
2265 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2266 exit(1);
2269 int
2270 spawn_editor(const char *editor, const char *file)
2272 pid_t pid;
2273 sig_t sighup, sigint, sigquit;
2274 int st = -1;
2276 sighup = signal(SIGHUP, SIG_IGN);
2277 sigint = signal(SIGINT, SIG_IGN);
2278 sigquit = signal(SIGQUIT, SIG_IGN);
2280 switch (pid = fork()) {
2281 case -1:
2282 goto doneediting;
2283 case 0:
2284 execl(editor, editor, file, (char *)NULL);
2285 _exit(127);
2288 while (waitpid(pid, &st, 0) == -1)
2289 if (errno != EINTR)
2290 break;
2292 doneediting:
2293 (void)signal(SIGHUP, sighup);
2294 (void)signal(SIGINT, sigint);
2295 (void)signal(SIGQUIT, sigquit);
2297 if (!WIFEXITED(st)) {
2298 errno = EINTR;
2299 return -1;
2302 return WEXITSTATUS(st);
2305 struct collect_commit_logmsg_arg {
2306 const char *cmdline_log;
2307 const char *editor;
2308 const char *worktree_path;
2309 const char *repo_path;
2310 char *logmsg_path;
2314 static const struct got_error *
2315 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2316 void *arg)
2318 const char *initial_content = "\n# changes to be committed:\n";
2319 struct got_pathlist_entry *pe;
2320 const struct got_error *err = NULL;
2321 char *template = NULL;
2322 struct collect_commit_logmsg_arg *a = arg;
2323 char buf[1024];
2324 struct stat st, st2;
2325 FILE *fp;
2326 size_t len;
2327 int fd, content_changed = 0;
2329 /* if a message was specified on the command line, just use it */
2330 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2331 len = strlen(a->cmdline_log) + 1;
2332 *logmsg = malloc(len + 1);
2333 if (*logmsg == NULL)
2334 return got_error_from_errno("malloc");
2335 strlcpy(*logmsg, a->cmdline_log, len);
2336 return NULL;
2339 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2340 return got_error_from_errno("asprintf");
2342 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2343 if (err)
2344 goto done;
2346 dprintf(fd, initial_content);
2348 TAILQ_FOREACH(pe, commitable_paths, entry) {
2349 struct got_commitable *ct = pe->data;
2350 dprintf(fd, "# %c %s\n", ct->status, pe->path);
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 /* remove comments */
2379 *logmsg = malloc(st2.st_size + 1);
2380 if (*logmsg == NULL) {
2381 err = got_error_from_errno("malloc");
2382 goto done;
2384 len = 0;
2386 fp = fopen(a->logmsg_path, "r");
2387 while (fgets(buf, sizeof(buf), fp) != NULL) {
2388 if (!content_changed && strcmp(buf, initial_content) != 0)
2389 content_changed = 1;
2390 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2391 continue;
2392 len = strlcat(*logmsg, buf, st2.st_size);
2394 fclose(fp);
2396 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2397 (*logmsg)[len - 1] = '\0';
2398 len--;
2401 if (len == 0 || !content_changed) {
2402 unlink(a->logmsg_path);
2403 free(a->logmsg_path);
2404 a->logmsg_path = NULL;
2405 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2406 "commit message cannot be empty, aborting");
2407 goto done;
2409 done:
2410 free(template);
2412 /* Editor is done; we can now apply unveil(2) */
2413 if (err == NULL)
2414 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2415 return err;
2418 static const struct got_error *
2419 cmd_commit(int argc, char *argv[])
2421 const struct got_error *error = NULL;
2422 struct got_worktree *worktree = NULL;
2423 struct got_repository *repo = NULL;
2424 char *cwd = NULL, *path = NULL, *id_str = NULL;
2425 struct got_object_id *id = NULL;
2426 const char *logmsg = NULL;
2427 const char *got_author = getenv("GOT_AUTHOR");
2428 struct collect_commit_logmsg_arg cl_arg;
2429 char *editor = NULL;
2430 int ch;
2432 while ((ch = getopt(argc, argv, "m:")) != -1) {
2433 switch (ch) {
2434 case 'm':
2435 logmsg = optarg;
2436 break;
2437 default:
2438 usage_commit();
2439 /* NOTREACHED */
2443 argc -= optind;
2444 argv += optind;
2446 if (argc == 1) {
2447 path = realpath(argv[0], NULL);
2448 if (path == NULL) {
2449 error = got_error_from_errno2("realpath", argv[0]);
2450 goto done;
2452 got_path_strip_trailing_slashes(path);
2453 } else if (argc != 0)
2454 usage_commit();
2456 if (got_author == NULL) {
2457 /* TODO: Look current user up in password database */
2458 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2459 goto done;
2462 cwd = getcwd(NULL, 0);
2463 if (cwd == NULL) {
2464 error = got_error_from_errno("getcwd");
2465 goto done;
2467 error = got_worktree_open(&worktree, cwd);
2468 if (error)
2469 goto done;
2471 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2472 if (error != NULL)
2473 goto done;
2476 * unveil(2) traverses exec(2); if an editor is used we have
2477 * to apply unveil after the log message has been written.
2479 if (logmsg == NULL || strlen(logmsg) == 0)
2480 error = get_editor(&editor);
2481 else
2482 error = apply_unveil(got_repo_get_path(repo), 0,
2483 got_worktree_get_root_path(worktree), 0);
2484 if (error)
2485 goto done;
2487 cl_arg.editor = editor;
2488 cl_arg.cmdline_log = logmsg;
2489 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2490 cl_arg.repo_path = got_repo_get_path(repo);
2491 cl_arg.logmsg_path = NULL;
2492 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2493 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2494 if (error) {
2495 if (cl_arg.logmsg_path)
2496 fprintf(stderr, "%s: log message preserved in %s\n",
2497 getprogname(), cl_arg.logmsg_path);
2498 goto done;
2501 if (cl_arg.logmsg_path)
2502 unlink(cl_arg.logmsg_path);
2504 error = got_object_id_str(&id_str, id);
2505 if (error)
2506 goto done;
2507 printf("created commit %s\n", id_str);
2508 done:
2509 if (repo)
2510 got_repo_close(repo);
2511 if (worktree)
2512 got_worktree_close(worktree);
2513 free(path);
2514 free(cwd);
2515 free(id_str);
2516 free(editor);
2517 return error;