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, char **editor)
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 (editor) {
253 err = get_editor(editor);
254 if (err)
255 return err;
256 if (unveil(*editor, "x") != 0) {
257 err = got_error_from_errno2("unveil", *editor);
258 free(*editor);
259 *editor = NULL;
260 return err;
264 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 return got_error_from_errno2("unveil", repo_path);
267 if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 return got_error_from_errno2("unveil", worktree_path);
270 if (unveil("/tmp", "rwc") != 0)
271 return got_error_from_errno2("unveil", "/tmp");
273 err = got_privsep_unveil_exec_helpers();
274 if (err != NULL)
275 return err;
277 if (unveil(NULL, NULL) != 0)
278 return got_error_from_errno("unveil");
280 return NULL;
283 __dead static void
284 usage_checkout(void)
286 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
287 "[worktree-path]\n", getprogname());
288 exit(1);
291 static void
292 checkout_progress(void *arg, unsigned char status, const char *path)
294 char *worktree_path = arg;
296 while (path[0] == '/')
297 path++;
299 printf("%c %s/%s\n", status, worktree_path, path);
302 static const struct got_error *
303 check_cancelled(void *arg)
305 if (sigint_received || sigpipe_received)
306 return got_error(GOT_ERR_CANCELLED);
307 return NULL;
310 static const struct got_error *
311 check_linear_ancestry(struct got_worktree *worktree,
312 struct got_object_id *commit_id, struct got_repository *repo)
314 const struct got_error *err = NULL;
315 struct got_object_id *yca_id, *base_commit_id;
317 base_commit_id = got_worktree_get_base_commit_id(worktree);
318 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
319 commit_id, base_commit_id, repo);
320 if (err)
321 return err;
323 if (yca_id == NULL)
324 return got_error(GOT_ERR_ANCESTRY);
326 /*
327 * Require a straight line of history between the target commit
328 * and the work tree's base commit.
330 * Non-linear situation such as the this require a rebase:
332 * (commit) D F (base_commit)
333 * \ /
334 * C E
335 * \ /
336 * B (yca)
337 * |
338 * A
340 * 'got update' only handles linear cases:
341 * Update forwards in time: A (base/yca) - B - C - D (commit)
342 * Update backwards in time: D (base) - C - D - A (commit/yca)
343 */
344 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
345 got_object_id_cmp(base_commit_id, yca_id) != 0)
346 return got_error(GOT_ERR_ANCESTRY);
348 free(yca_id);
349 return NULL;
353 static const struct got_error *
354 cmd_checkout(int argc, char *argv[])
356 const struct got_error *error = NULL;
357 struct got_repository *repo = NULL;
358 struct got_reference *head_ref = NULL;
359 struct got_worktree *worktree = NULL;
360 char *repo_path = NULL;
361 char *worktree_path = NULL;
362 const char *path_prefix = "";
363 char *commit_id_str = NULL;
364 int ch, same_path_prefix;
366 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
367 switch (ch) {
368 case 'c':
369 commit_id_str = strdup(optarg);
370 if (commit_id_str == NULL)
371 return got_error_from_errno("strdup");
372 break;
373 case 'p':
374 path_prefix = optarg;
375 break;
376 default:
377 usage_checkout();
378 /* NOTREACHED */
382 argc -= optind;
383 argv += optind;
385 #ifndef PROFILE
386 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
387 "unveil", NULL) == -1)
388 err(1, "pledge");
389 #endif
390 if (argc == 1) {
391 char *cwd, *base, *dotgit;
392 repo_path = realpath(argv[0], NULL);
393 if (repo_path == NULL)
394 return got_error_from_errno2("realpath", argv[0]);
395 cwd = getcwd(NULL, 0);
396 if (cwd == NULL) {
397 error = got_error_from_errno("getcwd");
398 goto done;
400 if (path_prefix[0]) {
401 base = basename(path_prefix);
402 if (base == NULL) {
403 error = got_error_from_errno2("basename",
404 path_prefix);
405 goto done;
407 } else {
408 base = basename(repo_path);
409 if (base == NULL) {
410 error = got_error_from_errno2("basename",
411 repo_path);
412 goto done;
415 dotgit = strstr(base, ".git");
416 if (dotgit)
417 *dotgit = '\0';
418 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
419 error = got_error_from_errno("asprintf");
420 free(cwd);
421 goto done;
423 free(cwd);
424 } else if (argc == 2) {
425 repo_path = realpath(argv[0], NULL);
426 if (repo_path == NULL) {
427 error = got_error_from_errno2("realpath", argv[0]);
428 goto done;
430 worktree_path = realpath(argv[1], NULL);
431 if (worktree_path == NULL) {
432 error = got_error_from_errno2("realpath", argv[1]);
433 goto done;
435 } else
436 usage_checkout();
438 got_path_strip_trailing_slashes(repo_path);
439 got_path_strip_trailing_slashes(worktree_path);
441 error = got_repo_open(&repo, repo_path);
442 if (error != NULL)
443 goto done;
445 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1,
446 NULL);
447 if (error)
448 goto done;
450 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
451 if (error != NULL)
452 goto done;
454 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
455 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
456 goto done;
458 error = got_worktree_open(&worktree, worktree_path);
459 if (error != NULL)
460 goto done;
462 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
463 path_prefix);
464 if (error != NULL)
465 goto done;
466 if (!same_path_prefix) {
467 error = got_error(GOT_ERR_PATH_PREFIX);
468 goto done;
471 if (commit_id_str) {
472 struct got_object_id *commit_id;
473 error = got_object_resolve_id_str(&commit_id, repo,
474 commit_id_str);
475 if (error != NULL)
476 goto done;
477 error = check_linear_ancestry(worktree, commit_id, repo);
478 if (error != NULL) {
479 free(commit_id);
480 goto done;
482 error = got_worktree_set_base_commit_id(worktree, repo,
483 commit_id);
484 free(commit_id);
485 if (error)
486 goto done;
489 error = got_worktree_checkout_files(worktree, "", repo,
490 checkout_progress, worktree_path, check_cancelled, NULL);
491 if (error != NULL)
492 goto done;
494 printf("Now shut up and hack\n");
496 done:
497 free(commit_id_str);
498 free(repo_path);
499 free(worktree_path);
500 return error;
503 __dead static void
504 usage_update(void)
506 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
507 getprogname());
508 exit(1);
511 static void
512 update_progress(void *arg, unsigned char status, const char *path)
514 int *did_something = arg;
516 if (status == GOT_STATUS_EXISTS)
517 return;
519 *did_something = 1;
520 while (path[0] == '/')
521 path++;
522 printf("%c %s\n", status, path);
525 static const struct got_error *
526 cmd_update(int argc, char *argv[])
528 const struct got_error *error = NULL;
529 struct got_repository *repo = NULL;
530 struct got_worktree *worktree = NULL;
531 char *worktree_path = NULL, *path = NULL;
532 struct got_object_id *commit_id = NULL;
533 char *commit_id_str = NULL;
534 int ch, did_something = 0;
536 while ((ch = getopt(argc, argv, "c:")) != -1) {
537 switch (ch) {
538 case 'c':
539 commit_id_str = strdup(optarg);
540 if (commit_id_str == NULL)
541 return got_error_from_errno("strdup");
542 break;
543 default:
544 usage_update();
545 /* NOTREACHED */
549 argc -= optind;
550 argv += optind;
552 #ifndef PROFILE
553 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
554 "unveil", NULL) == -1)
555 err(1, "pledge");
556 #endif
557 worktree_path = getcwd(NULL, 0);
558 if (worktree_path == NULL) {
559 error = got_error_from_errno("getcwd");
560 goto done;
562 error = got_worktree_open(&worktree, worktree_path);
563 if (error)
564 goto done;
566 if (argc == 0) {
567 path = strdup("");
568 if (path == NULL) {
569 error = got_error_from_errno("strdup");
570 goto done;
572 } else if (argc == 1) {
573 error = got_worktree_resolve_path(&path, worktree, argv[0]);
574 if (error)
575 goto done;
576 } else
577 usage_update();
579 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
580 if (error != NULL)
581 goto done;
583 error = apply_unveil(got_repo_get_path(repo), 0,
584 got_worktree_get_root_path(worktree), 0, NULL);
585 if (error)
586 goto done;
588 if (commit_id_str == NULL) {
589 struct got_reference *head_ref;
590 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
591 if (error != NULL)
592 goto done;
593 error = got_ref_resolve(&commit_id, repo, head_ref);
594 if (error != NULL)
595 goto done;
596 error = got_object_id_str(&commit_id_str, commit_id);
597 if (error != NULL)
598 goto done;
599 } else {
600 error = got_object_resolve_id_str(&commit_id, repo,
601 commit_id_str);
602 if (error != NULL)
603 goto done;
606 error = check_linear_ancestry(worktree, commit_id, repo);
607 if (error != NULL)
608 goto done;
610 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
611 commit_id) != 0) {
612 error = got_worktree_set_base_commit_id(worktree, repo,
613 commit_id);
614 if (error)
615 goto done;
618 error = got_worktree_checkout_files(worktree, path, repo,
619 update_progress, &did_something, check_cancelled, NULL);
620 if (error != NULL)
621 goto done;
623 if (did_something)
624 printf("Updated to commit %s\n", commit_id_str);
625 else
626 printf("Already up-to-date\n");
627 done:
628 free(worktree_path);
629 free(path);
630 free(commit_id);
631 free(commit_id_str);
632 return error;
635 static const struct got_error *
636 print_patch(struct got_commit_object *commit, struct got_object_id *id,
637 int diff_context, struct got_repository *repo)
639 const struct got_error *err = NULL;
640 struct got_tree_object *tree1 = NULL, *tree2;
641 struct got_object_qid *qid;
642 char *id_str1 = NULL, *id_str2;
644 err = got_object_open_as_tree(&tree2, repo,
645 got_object_commit_get_tree_id(commit));
646 if (err)
647 return err;
649 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
650 if (qid != NULL) {
651 struct got_commit_object *pcommit;
653 err = got_object_open_as_commit(&pcommit, repo, qid->id);
654 if (err)
655 return err;
657 err = got_object_open_as_tree(&tree1, repo,
658 got_object_commit_get_tree_id(pcommit));
659 got_object_commit_close(pcommit);
660 if (err)
661 return err;
663 err = got_object_id_str(&id_str1, qid->id);
664 if (err)
665 return err;
668 err = got_object_id_str(&id_str2, id);
669 if (err)
670 goto done;
672 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
673 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
674 done:
675 if (tree1)
676 got_object_tree_close(tree1);
677 got_object_tree_close(tree2);
678 free(id_str1);
679 free(id_str2);
680 return err;
683 static char *
684 get_datestr(time_t *time, char *datebuf)
686 char *p, *s = ctime_r(time, datebuf);
687 p = strchr(s, '\n');
688 if (p)
689 *p = '\0';
690 return s;
693 static const struct got_error *
694 print_commit(struct got_commit_object *commit, struct got_object_id *id,
695 struct got_repository *repo, int show_patch, int diff_context,
696 struct got_reflist_head *refs)
698 const struct got_error *err = NULL;
699 char *id_str, *datestr, *logmsg0, *logmsg, *line;
700 char datebuf[26];
701 time_t committer_time;
702 const char *author, *committer;
703 char *refs_str = NULL;
704 struct got_reflist_entry *re;
706 SIMPLEQ_FOREACH(re, refs, entry) {
707 char *s;
708 const char *name;
709 if (got_object_id_cmp(re->id, id) != 0)
710 continue;
711 name = got_ref_get_name(re->ref);
712 if (strcmp(name, GOT_REF_HEAD) == 0)
713 continue;
714 if (strncmp(name, "refs/", 5) == 0)
715 name += 5;
716 if (strncmp(name, "got/", 4) == 0)
717 continue;
718 if (strncmp(name, "heads/", 6) == 0)
719 name += 6;
720 if (strncmp(name, "remotes/", 8) == 0)
721 name += 8;
722 s = refs_str;
723 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
724 name) == -1) {
725 err = got_error_from_errno("asprintf");
726 free(s);
727 break;
729 free(s);
731 err = got_object_id_str(&id_str, id);
732 if (err)
733 return err;
735 printf("-----------------------------------------------\n");
736 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
737 refs_str ? refs_str : "", refs_str ? ")" : "");
738 free(id_str);
739 id_str = NULL;
740 free(refs_str);
741 refs_str = NULL;
742 printf("from: %s\n", got_object_commit_get_author(commit));
743 committer_time = got_object_commit_get_committer_time(commit);
744 datestr = get_datestr(&committer_time, datebuf);
745 printf("date: %s UTC\n", datestr);
746 author = got_object_commit_get_author(commit);
747 committer = got_object_commit_get_committer(commit);
748 if (strcmp(author, committer) != 0)
749 printf("via: %s\n", committer);
750 if (got_object_commit_get_nparents(commit) > 1) {
751 const struct got_object_id_queue *parent_ids;
752 struct got_object_qid *qid;
753 int n = 1;
754 parent_ids = got_object_commit_get_parent_ids(commit);
755 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
756 err = got_object_id_str(&id_str, qid->id);
757 if (err)
758 return err;
759 printf("parent %d: %s\n", n++, id_str);
760 free(id_str);
764 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
765 if (logmsg0 == NULL)
766 return got_error_from_errno("strdup");
768 logmsg = logmsg0;
769 do {
770 line = strsep(&logmsg, "\n");
771 if (line)
772 printf(" %s\n", line);
773 } while (line);
774 free(logmsg0);
776 if (show_patch) {
777 err = print_patch(commit, id, diff_context, repo);
778 if (err == 0)
779 printf("\n");
782 if (fflush(stdout) != 0 && err == NULL)
783 err = got_error_from_errno("fflush");
784 return err;
787 static const struct got_error *
788 print_commits(struct got_object_id *root_id, struct got_repository *repo,
789 char *path, int show_patch, int diff_context, int limit,
790 int first_parent_traversal, struct got_reflist_head *refs)
792 const struct got_error *err;
793 struct got_commit_graph *graph;
795 err = got_commit_graph_open(&graph, root_id, path,
796 first_parent_traversal, repo);
797 if (err)
798 return err;
799 err = got_commit_graph_iter_start(graph, root_id, repo);
800 if (err)
801 goto done;
802 for (;;) {
803 struct got_commit_object *commit;
804 struct got_object_id *id;
806 if (sigint_received || sigpipe_received)
807 break;
809 err = got_commit_graph_iter_next(&id, graph);
810 if (err) {
811 if (err->code == GOT_ERR_ITER_COMPLETED) {
812 err = NULL;
813 break;
815 if (err->code != GOT_ERR_ITER_NEED_MORE)
816 break;
817 err = got_commit_graph_fetch_commits(graph, 1, repo);
818 if (err)
819 break;
820 else
821 continue;
823 if (id == NULL)
824 break;
826 err = got_object_open_as_commit(&commit, repo, id);
827 if (err)
828 break;
829 err = print_commit(commit, id, repo, show_patch, diff_context,
830 refs);
831 got_object_commit_close(commit);
832 if (err || (limit && --limit == 0))
833 break;
835 done:
836 got_commit_graph_close(graph);
837 return err;
840 __dead static void
841 usage_log(void)
843 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
844 "[-r repository-path] [path]\n", getprogname());
845 exit(1);
848 static const struct got_error *
849 cmd_log(int argc, char *argv[])
851 const struct got_error *error;
852 struct got_repository *repo = NULL;
853 struct got_worktree *worktree = NULL;
854 struct got_commit_object *commit = NULL;
855 struct got_object_id *id = NULL;
856 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
857 char *start_commit = NULL;
858 int diff_context = 3, ch;
859 int show_patch = 0, limit = 0, first_parent_traversal = 0;
860 const char *errstr;
861 struct got_reflist_head refs;
863 SIMPLEQ_INIT(&refs);
865 #ifndef PROFILE
866 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
867 NULL)
868 == -1)
869 err(1, "pledge");
870 #endif
872 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
873 switch (ch) {
874 case 'p':
875 show_patch = 1;
876 break;
877 case 'c':
878 start_commit = optarg;
879 break;
880 case 'C':
881 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
882 &errstr);
883 if (errstr != NULL)
884 err(1, "-C option %s", errstr);
885 break;
886 case 'l':
887 limit = strtonum(optarg, 1, INT_MAX, &errstr);
888 if (errstr != NULL)
889 err(1, "-l option %s", errstr);
890 break;
891 case 'f':
892 first_parent_traversal = 1;
893 break;
894 case 'r':
895 repo_path = realpath(optarg, NULL);
896 if (repo_path == NULL)
897 err(1, "-r option");
898 got_path_strip_trailing_slashes(repo_path);
899 break;
900 default:
901 usage_log();
902 /* NOTREACHED */
906 argc -= optind;
907 argv += optind;
909 cwd = getcwd(NULL, 0);
910 if (cwd == NULL) {
911 error = got_error_from_errno("getcwd");
912 goto done;
915 error = got_worktree_open(&worktree, cwd);
916 if (error && error->code != GOT_ERR_NOT_WORKTREE)
917 goto done;
918 error = NULL;
920 if (argc == 0) {
921 path = strdup("");
922 if (path == NULL) {
923 error = got_error_from_errno("strdup");
924 goto done;
926 } else if (argc == 1) {
927 if (worktree) {
928 error = got_worktree_resolve_path(&path, worktree,
929 argv[0]);
930 if (error)
931 goto done;
932 } else {
933 path = strdup(argv[0]);
934 if (path == NULL) {
935 error = got_error_from_errno("strdup");
936 goto done;
939 } else
940 usage_log();
942 if (repo_path == NULL) {
943 repo_path = worktree ?
944 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
946 if (repo_path == NULL) {
947 error = got_error_from_errno("strdup");
948 goto done;
951 error = got_repo_open(&repo, repo_path);
952 if (error != NULL)
953 goto done;
955 error = apply_unveil(got_repo_get_path(repo), 1,
956 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
957 if (error)
958 goto done;
960 if (start_commit == NULL) {
961 struct got_reference *head_ref;
962 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
963 if (error != NULL)
964 return error;
965 error = got_ref_resolve(&id, repo, head_ref);
966 got_ref_close(head_ref);
967 if (error != NULL)
968 return error;
969 error = got_object_open_as_commit(&commit, repo, id);
970 } else {
971 struct got_reference *ref;
972 error = got_ref_open(&ref, repo, start_commit, 0);
973 if (error == NULL) {
974 int obj_type;
975 error = got_ref_resolve(&id, repo, ref);
976 got_ref_close(ref);
977 if (error != NULL)
978 goto done;
979 error = got_object_get_type(&obj_type, repo, id);
980 if (error != NULL)
981 goto done;
982 if (obj_type == GOT_OBJ_TYPE_TAG) {
983 struct got_tag_object *tag;
984 error = got_object_open_as_tag(&tag, repo, id);
985 if (error != NULL)
986 goto done;
987 if (got_object_tag_get_object_type(tag) !=
988 GOT_OBJ_TYPE_COMMIT) {
989 got_object_tag_close(tag);
990 error = got_error(GOT_ERR_OBJ_TYPE);
991 goto done;
993 free(id);
994 id = got_object_id_dup(
995 got_object_tag_get_object_id(tag));
996 if (id == NULL)
997 error = got_error_from_errno(
998 "got_object_id_dup");
999 got_object_tag_close(tag);
1000 if (error)
1001 goto done;
1002 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1003 error = got_error(GOT_ERR_OBJ_TYPE);
1004 goto done;
1006 error = got_object_open_as_commit(&commit, repo, id);
1007 if (error != NULL)
1008 goto done;
1010 if (commit == NULL) {
1011 error = got_object_resolve_id_str(&id, repo,
1012 start_commit);
1013 if (error != NULL)
1014 return error;
1017 if (error != NULL)
1018 goto done;
1020 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1021 if (error != NULL)
1022 goto done;
1023 if (in_repo_path) {
1024 free(path);
1025 path = in_repo_path;
1028 error = got_ref_list(&refs, repo);
1029 if (error)
1030 goto done;
1032 error = print_commits(id, repo, path, show_patch,
1033 diff_context, limit, first_parent_traversal, &refs);
1034 done:
1035 free(path);
1036 free(repo_path);
1037 free(cwd);
1038 free(id);
1039 if (worktree)
1040 got_worktree_close(worktree);
1041 if (repo) {
1042 const struct got_error *repo_error;
1043 repo_error = got_repo_close(repo);
1044 if (error == NULL)
1045 error = repo_error;
1047 got_ref_list_free(&refs);
1048 return error;
1051 __dead static void
1052 usage_diff(void)
1054 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1055 "[object1 object2 | path]\n", getprogname());
1056 exit(1);
1059 struct print_diff_arg {
1060 struct got_repository *repo;
1061 struct got_worktree *worktree;
1062 int diff_context;
1063 const char *id_str;
1064 int header_shown;
1067 static const struct got_error *
1068 print_diff(void *arg, unsigned char status, const char *path,
1069 struct got_object_id *id)
1071 struct print_diff_arg *a = arg;
1072 const struct got_error *err = NULL;
1073 struct got_blob_object *blob1 = NULL;
1074 FILE *f2 = NULL;
1075 char *abspath = NULL;
1076 struct stat sb;
1078 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1079 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1080 return NULL;
1082 if (!a->header_shown) {
1083 printf("diff %s %s\n", a->id_str,
1084 got_worktree_get_root_path(a->worktree));
1085 a->header_shown = 1;
1088 if (status != GOT_STATUS_ADD) {
1089 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1090 if (err)
1091 goto done;
1095 if (status != GOT_STATUS_DELETE) {
1096 if (asprintf(&abspath, "%s/%s",
1097 got_worktree_get_root_path(a->worktree), path) == -1) {
1098 err = got_error_from_errno("asprintf");
1099 goto done;
1102 f2 = fopen(abspath, "r");
1103 if (f2 == NULL) {
1104 err = got_error_from_errno2("fopen", abspath);
1105 goto done;
1107 if (lstat(abspath, &sb) == -1) {
1108 err = got_error_from_errno2("lstat", abspath);
1109 goto done;
1111 } else
1112 sb.st_size = 0;
1114 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1115 stdout);
1116 done:
1117 if (blob1)
1118 got_object_blob_close(blob1);
1119 if (f2 && fclose(f2) != 0 && err == NULL)
1120 err = got_error_from_errno("fclose");
1121 free(abspath);
1122 return err;
1125 static const struct got_error *
1126 cmd_diff(int argc, char *argv[])
1128 const struct got_error *error;
1129 struct got_repository *repo = NULL;
1130 struct got_worktree *worktree = NULL;
1131 char *cwd = NULL, *repo_path = NULL;
1132 struct got_object_id *id1 = NULL, *id2 = NULL;
1133 char *id_str1 = NULL, *id_str2 = NULL;
1134 int type1, type2;
1135 int diff_context = 3, ch;
1136 const char *errstr;
1137 char *path = NULL;
1139 #ifndef PROFILE
1140 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1141 NULL) == -1)
1142 err(1, "pledge");
1143 #endif
1145 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1146 switch (ch) {
1147 case 'C':
1148 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1149 if (errstr != NULL)
1150 err(1, "-C option %s", errstr);
1151 break;
1152 case 'r':
1153 repo_path = realpath(optarg, NULL);
1154 if (repo_path == NULL)
1155 err(1, "-r option");
1156 got_path_strip_trailing_slashes(repo_path);
1157 break;
1158 default:
1159 usage_diff();
1160 /* NOTREACHED */
1164 argc -= optind;
1165 argv += optind;
1167 cwd = getcwd(NULL, 0);
1168 if (cwd == NULL) {
1169 error = got_error_from_errno("getcwd");
1170 goto done;
1172 error = got_worktree_open(&worktree, cwd);
1173 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1174 goto done;
1175 if (argc <= 1) {
1176 if (worktree == NULL) {
1177 error = got_error(GOT_ERR_NOT_WORKTREE);
1178 goto done;
1180 if (repo_path)
1181 errx(1,
1182 "-r option can't be used when diffing a work tree");
1183 repo_path = strdup(got_worktree_get_repo_path(worktree));
1184 if (repo_path == NULL) {
1185 error = got_error_from_errno("strdup");
1186 goto done;
1188 if (argc == 1) {
1189 error = got_worktree_resolve_path(&path, worktree,
1190 argv[0]);
1191 if (error)
1192 goto done;
1193 } else {
1194 path = strdup("");
1195 if (path == NULL) {
1196 error = got_error_from_errno("strdup");
1197 goto done;
1200 } else if (argc == 2) {
1201 id_str1 = argv[0];
1202 id_str2 = argv[1];
1203 } else
1204 usage_diff();
1206 if (repo_path == NULL) {
1207 repo_path = getcwd(NULL, 0);
1208 if (repo_path == NULL)
1209 return got_error_from_errno("getcwd");
1212 error = got_repo_open(&repo, repo_path);
1213 free(repo_path);
1214 if (error != NULL)
1215 goto done;
1217 error = apply_unveil(got_repo_get_path(repo), 1,
1218 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
1219 if (error)
1220 goto done;
1222 if (worktree) {
1223 struct print_diff_arg arg;
1224 char *id_str;
1225 error = got_object_id_str(&id_str,
1226 got_worktree_get_base_commit_id(worktree));
1227 if (error)
1228 goto done;
1229 arg.repo = repo;
1230 arg.worktree = worktree;
1231 arg.diff_context = diff_context;
1232 arg.id_str = id_str;
1233 arg.header_shown = 0;
1235 error = got_worktree_status(worktree, path, repo, print_diff,
1236 &arg, check_cancelled, NULL);
1237 free(id_str);
1238 goto done;
1241 error = got_object_resolve_id_str(&id1, repo, id_str1);
1242 if (error)
1243 goto done;
1245 error = got_object_resolve_id_str(&id2, repo, id_str2);
1246 if (error)
1247 goto done;
1249 error = got_object_get_type(&type1, repo, id1);
1250 if (error)
1251 goto done;
1253 error = got_object_get_type(&type2, repo, id2);
1254 if (error)
1255 goto done;
1257 if (type1 != type2) {
1258 error = got_error(GOT_ERR_OBJ_TYPE);
1259 goto done;
1262 switch (type1) {
1263 case GOT_OBJ_TYPE_BLOB:
1264 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1265 diff_context, repo, stdout);
1266 break;
1267 case GOT_OBJ_TYPE_TREE:
1268 error = got_diff_objects_as_trees(id1, id2, "", "",
1269 diff_context, repo, stdout);
1270 break;
1271 case GOT_OBJ_TYPE_COMMIT:
1272 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1273 id_str2);
1274 error = got_diff_objects_as_commits(id1, id2, diff_context,
1275 repo, stdout);
1276 break;
1277 default:
1278 error = got_error(GOT_ERR_OBJ_TYPE);
1281 done:
1282 free(id1);
1283 free(id2);
1284 free(path);
1285 if (worktree)
1286 got_worktree_close(worktree);
1287 if (repo) {
1288 const struct got_error *repo_error;
1289 repo_error = got_repo_close(repo);
1290 if (error == NULL)
1291 error = repo_error;
1293 return error;
1296 __dead static void
1297 usage_blame(void)
1299 fprintf(stderr,
1300 "usage: %s blame [-c commit] [-r repository-path] path\n",
1301 getprogname());
1302 exit(1);
1305 static const struct got_error *
1306 cmd_blame(int argc, char *argv[])
1308 const struct got_error *error;
1309 struct got_repository *repo = NULL;
1310 struct got_worktree *worktree = NULL;
1311 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1312 struct got_object_id *commit_id = NULL;
1313 char *commit_id_str = NULL;
1314 int ch;
1316 #ifndef PROFILE
1317 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1318 NULL) == -1)
1319 err(1, "pledge");
1320 #endif
1322 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1323 switch (ch) {
1324 case 'c':
1325 commit_id_str = optarg;
1326 break;
1327 case 'r':
1328 repo_path = realpath(optarg, NULL);
1329 if (repo_path == NULL)
1330 err(1, "-r option");
1331 got_path_strip_trailing_slashes(repo_path);
1332 break;
1333 default:
1334 usage_blame();
1335 /* NOTREACHED */
1339 argc -= optind;
1340 argv += optind;
1342 if (argc == 1)
1343 path = argv[0];
1344 else
1345 usage_blame();
1347 cwd = getcwd(NULL, 0);
1348 if (cwd == NULL) {
1349 error = got_error_from_errno("getcwd");
1350 goto done;
1352 if (repo_path == NULL) {
1353 error = got_worktree_open(&worktree, cwd);
1354 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1355 goto done;
1356 else
1357 error = NULL;
1358 if (worktree) {
1359 repo_path =
1360 strdup(got_worktree_get_repo_path(worktree));
1361 if (repo_path == NULL)
1362 error = got_error_from_errno("strdup");
1363 if (error)
1364 goto done;
1365 } else {
1366 repo_path = strdup(cwd);
1367 if (repo_path == NULL) {
1368 error = got_error_from_errno("strdup");
1369 goto done;
1374 error = got_repo_open(&repo, repo_path);
1375 if (error != NULL)
1376 goto done;
1378 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, NULL);
1379 if (error)
1380 goto done;
1382 if (worktree) {
1383 const char *prefix = got_worktree_get_path_prefix(worktree);
1384 char *p, *worktree_subdir = cwd +
1385 strlen(got_worktree_get_root_path(worktree));
1386 if (asprintf(&p, "%s%s%s%s%s",
1387 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1388 worktree_subdir, worktree_subdir[0] ? "/" : "",
1389 path) == -1) {
1390 error = got_error_from_errno("asprintf");
1391 goto done;
1393 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1394 free(p);
1395 } else {
1396 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1398 if (error)
1399 goto done;
1401 if (commit_id_str == NULL) {
1402 struct got_reference *head_ref;
1403 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1404 if (error != NULL)
1405 goto done;
1406 error = got_ref_resolve(&commit_id, repo, head_ref);
1407 got_ref_close(head_ref);
1408 if (error != NULL)
1409 goto done;
1410 } else {
1411 error = got_object_resolve_id_str(&commit_id, repo,
1412 commit_id_str);
1413 if (error != NULL)
1414 goto done;
1417 error = got_blame(in_repo_path, commit_id, repo, stdout);
1418 done:
1419 free(in_repo_path);
1420 free(repo_path);
1421 free(cwd);
1422 free(commit_id);
1423 if (worktree)
1424 got_worktree_close(worktree);
1425 if (repo) {
1426 const struct got_error *repo_error;
1427 repo_error = got_repo_close(repo);
1428 if (error == NULL)
1429 error = repo_error;
1431 return error;
1434 __dead static void
1435 usage_tree(void)
1437 fprintf(stderr,
1438 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1439 getprogname());
1440 exit(1);
1443 static void
1444 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1445 const char *root_path)
1447 int is_root_path = (strcmp(path, root_path) == 0);
1449 path += strlen(root_path);
1450 while (path[0] == '/')
1451 path++;
1453 printf("%s%s%s%s%s\n", id ? id : "", path,
1454 is_root_path ? "" : "/", te->name,
1455 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1458 static const struct got_error *
1459 print_tree(const char *path, struct got_object_id *commit_id,
1460 int show_ids, int recurse, const char *root_path,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_object_id *tree_id = NULL;
1465 struct got_tree_object *tree = NULL;
1466 const struct got_tree_entries *entries;
1467 struct got_tree_entry *te;
1469 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1470 if (err)
1471 goto done;
1473 err = got_object_open_as_tree(&tree, repo, tree_id);
1474 if (err)
1475 goto done;
1476 entries = got_object_tree_get_entries(tree);
1477 te = SIMPLEQ_FIRST(&entries->head);
1478 while (te) {
1479 char *id = NULL;
1481 if (sigint_received || sigpipe_received)
1482 break;
1484 if (show_ids) {
1485 char *id_str;
1486 err = got_object_id_str(&id_str, te->id);
1487 if (err)
1488 goto done;
1489 if (asprintf(&id, "%s ", id_str) == -1) {
1490 err = got_error_from_errno("asprintf");
1491 free(id_str);
1492 goto done;
1494 free(id_str);
1496 print_entry(te, id, path, root_path);
1497 free(id);
1499 if (recurse && S_ISDIR(te->mode)) {
1500 char *child_path;
1501 if (asprintf(&child_path, "%s%s%s", path,
1502 path[0] == '/' && path[1] == '\0' ? "" : "/",
1503 te->name) == -1) {
1504 err = got_error_from_errno("asprintf");
1505 goto done;
1507 err = print_tree(child_path, commit_id, show_ids, 1,
1508 root_path, repo);
1509 free(child_path);
1510 if (err)
1511 goto done;
1514 te = SIMPLEQ_NEXT(te, entry);
1516 done:
1517 if (tree)
1518 got_object_tree_close(tree);
1519 free(tree_id);
1520 return err;
1523 static const struct got_error *
1524 cmd_tree(int argc, char *argv[])
1526 const struct got_error *error;
1527 struct got_repository *repo = NULL;
1528 struct got_worktree *worktree = NULL;
1529 const char *path;
1530 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1531 struct got_object_id *commit_id = NULL;
1532 char *commit_id_str = NULL;
1533 int show_ids = 0, recurse = 0;
1534 int ch;
1536 #ifndef PROFILE
1537 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1538 NULL) == -1)
1539 err(1, "pledge");
1540 #endif
1542 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1543 switch (ch) {
1544 case 'c':
1545 commit_id_str = optarg;
1546 break;
1547 case 'r':
1548 repo_path = realpath(optarg, NULL);
1549 if (repo_path == NULL)
1550 err(1, "-r option");
1551 got_path_strip_trailing_slashes(repo_path);
1552 break;
1553 case 'i':
1554 show_ids = 1;
1555 break;
1556 case 'R':
1557 recurse = 1;
1558 break;
1559 default:
1560 usage_tree();
1561 /* NOTREACHED */
1565 argc -= optind;
1566 argv += optind;
1568 if (argc == 1)
1569 path = argv[0];
1570 else if (argc > 1)
1571 usage_tree();
1572 else
1573 path = NULL;
1575 cwd = getcwd(NULL, 0);
1576 if (cwd == NULL) {
1577 error = got_error_from_errno("getcwd");
1578 goto done;
1580 if (repo_path == NULL) {
1581 error = got_worktree_open(&worktree, cwd);
1582 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1583 goto done;
1584 else
1585 error = NULL;
1586 if (worktree) {
1587 repo_path =
1588 strdup(got_worktree_get_repo_path(worktree));
1589 if (repo_path == NULL)
1590 error = got_error_from_errno("strdup");
1591 if (error)
1592 goto done;
1593 } else {
1594 repo_path = strdup(cwd);
1595 if (repo_path == NULL) {
1596 error = got_error_from_errno("strdup");
1597 goto done;
1602 error = got_repo_open(&repo, repo_path);
1603 if (error != NULL)
1604 goto done;
1606 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, NULL);
1607 if (error)
1608 goto done;
1610 if (path == NULL) {
1611 if (worktree) {
1612 char *p, *worktree_subdir = cwd +
1613 strlen(got_worktree_get_root_path(worktree));
1614 if (asprintf(&p, "%s/%s",
1615 got_worktree_get_path_prefix(worktree),
1616 worktree_subdir) == -1) {
1617 error = got_error_from_errno("asprintf");
1618 goto done;
1620 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1621 free(p);
1622 if (error)
1623 goto done;
1624 } else
1625 path = "/";
1627 if (in_repo_path == NULL) {
1628 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1629 if (error != NULL)
1630 goto done;
1633 if (commit_id_str == NULL) {
1634 struct got_reference *head_ref;
1635 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1636 if (error != NULL)
1637 goto done;
1638 error = got_ref_resolve(&commit_id, repo, head_ref);
1639 got_ref_close(head_ref);
1640 if (error != NULL)
1641 goto done;
1642 } else {
1643 error = got_object_resolve_id_str(&commit_id, repo,
1644 commit_id_str);
1645 if (error != NULL)
1646 goto done;
1649 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1650 in_repo_path, repo);
1651 done:
1652 free(in_repo_path);
1653 free(repo_path);
1654 free(cwd);
1655 free(commit_id);
1656 if (worktree)
1657 got_worktree_close(worktree);
1658 if (repo) {
1659 const struct got_error *repo_error;
1660 repo_error = got_repo_close(repo);
1661 if (error == NULL)
1662 error = repo_error;
1664 return error;
1667 __dead static void
1668 usage_status(void)
1670 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1671 exit(1);
1674 static const struct got_error *
1675 print_status(void *arg, unsigned char status, const char *path,
1676 struct got_object_id *id)
1678 printf("%c %s\n", status, path);
1679 return NULL;
1682 static const struct got_error *
1683 cmd_status(int argc, char *argv[])
1685 const struct got_error *error = NULL;
1686 struct got_repository *repo = NULL;
1687 struct got_worktree *worktree = NULL;
1688 char *cwd = NULL, *path = NULL;
1689 int ch;
1691 while ((ch = getopt(argc, argv, "")) != -1) {
1692 switch (ch) {
1693 default:
1694 usage_status();
1695 /* NOTREACHED */
1699 argc -= optind;
1700 argv += optind;
1702 #ifndef PROFILE
1703 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1704 NULL) == -1)
1705 err(1, "pledge");
1706 #endif
1707 cwd = getcwd(NULL, 0);
1708 if (cwd == NULL) {
1709 error = got_error_from_errno("getcwd");
1710 goto done;
1713 error = got_worktree_open(&worktree, cwd);
1714 if (error != NULL)
1715 goto done;
1717 if (argc == 0) {
1718 path = strdup("");
1719 if (path == NULL) {
1720 error = got_error_from_errno("strdup");
1721 goto done;
1723 } else if (argc == 1) {
1724 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1725 if (error)
1726 goto done;
1727 } else
1728 usage_status();
1730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1731 if (error != NULL)
1732 goto done;
1734 error = apply_unveil(got_repo_get_path(repo), 1,
1735 got_worktree_get_root_path(worktree), 0, NULL);
1736 if (error)
1737 goto done;
1739 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1740 check_cancelled, NULL);
1741 done:
1742 free(cwd);
1743 free(path);
1744 return error;
1747 __dead static void
1748 usage_ref(void)
1750 fprintf(stderr,
1751 "usage: %s ref [-r repository] -l | -d name | name object\n",
1752 getprogname());
1753 exit(1);
1756 static const struct got_error *
1757 list_refs(struct got_repository *repo)
1759 static const struct got_error *err = NULL;
1760 struct got_reflist_head refs;
1761 struct got_reflist_entry *re;
1763 SIMPLEQ_INIT(&refs);
1764 err = got_ref_list(&refs, repo);
1765 if (err)
1766 return err;
1768 SIMPLEQ_FOREACH(re, &refs, entry) {
1769 char *refstr;
1770 refstr = got_ref_to_str(re->ref);
1771 if (refstr == NULL)
1772 return got_error_from_errno("got_ref_to_str");
1773 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1774 free(refstr);
1777 got_ref_list_free(&refs);
1778 return NULL;
1781 static const struct got_error *
1782 delete_ref(struct got_repository *repo, const char *refname)
1784 const struct got_error *err = NULL;
1785 struct got_reference *ref;
1787 err = got_ref_open(&ref, repo, refname, 0);
1788 if (err)
1789 return err;
1791 err = got_ref_delete(ref, repo);
1792 got_ref_close(ref);
1793 return err;
1796 static const struct got_error *
1797 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1799 const struct got_error *err = NULL;
1800 struct got_object_id *id;
1801 struct got_reference *ref = NULL;
1803 err = got_object_resolve_id_str(&id, repo, id_str);
1804 if (err)
1805 return err;
1807 err = got_ref_alloc(&ref, refname, id);
1808 if (err)
1809 goto done;
1811 err = got_ref_write(ref, repo);
1812 done:
1813 if (ref)
1814 got_ref_close(ref);
1815 free(id);
1816 return err;
1819 static const struct got_error *
1820 cmd_ref(int argc, char *argv[])
1822 const struct got_error *error = NULL;
1823 struct got_repository *repo = NULL;
1824 struct got_worktree *worktree = NULL;
1825 char *cwd = NULL, *repo_path = NULL;
1826 int ch, do_list = 0;
1827 const char *delref = NULL;
1829 /* TODO: Add -s option for adding symbolic references. */
1830 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1831 switch (ch) {
1832 case 'd':
1833 delref = optarg;
1834 break;
1835 case 'r':
1836 repo_path = realpath(optarg, NULL);
1837 if (repo_path == NULL)
1838 err(1, "-r option");
1839 got_path_strip_trailing_slashes(repo_path);
1840 break;
1841 case 'l':
1842 do_list = 1;
1843 break;
1844 default:
1845 usage_ref();
1846 /* NOTREACHED */
1850 if (do_list && delref)
1851 errx(1, "-l and -d options are mutually exclusive\n");
1853 argc -= optind;
1854 argv += optind;
1856 if (do_list || delref) {
1857 if (argc > 0)
1858 usage_ref();
1859 } else if (argc != 2)
1860 usage_ref();
1862 #ifndef PROFILE
1863 if (do_list) {
1864 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1865 NULL) == -1)
1866 err(1, "pledge");
1867 } else {
1868 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1869 "sendfd unveil", NULL) == -1)
1870 err(1, "pledge");
1872 #endif
1873 cwd = getcwd(NULL, 0);
1874 if (cwd == NULL) {
1875 error = got_error_from_errno("getcwd");
1876 goto done;
1879 if (repo_path == NULL) {
1880 error = got_worktree_open(&worktree, cwd);
1881 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1882 goto done;
1883 else
1884 error = NULL;
1885 if (worktree) {
1886 repo_path =
1887 strdup(got_worktree_get_repo_path(worktree));
1888 if (repo_path == NULL)
1889 error = got_error_from_errno("strdup");
1890 if (error)
1891 goto done;
1892 } else {
1893 repo_path = strdup(cwd);
1894 if (repo_path == NULL) {
1895 error = got_error_from_errno("strdup");
1896 goto done;
1901 error = got_repo_open(&repo, repo_path);
1902 if (error != NULL)
1903 goto done;
1905 error = apply_unveil(got_repo_get_path(repo), do_list,
1906 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, NULL);
1907 if (error)
1908 goto done;
1910 if (do_list)
1911 error = list_refs(repo);
1912 else if (delref)
1913 error = delete_ref(repo, delref);
1914 else
1915 error = add_ref(repo, argv[0], argv[1]);
1916 done:
1917 if (repo)
1918 got_repo_close(repo);
1919 if (worktree)
1920 got_worktree_close(worktree);
1921 free(cwd);
1922 free(repo_path);
1923 return error;
1926 __dead static void
1927 usage_add(void)
1929 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1930 exit(1);
1933 static const struct got_error *
1934 cmd_add(int argc, char *argv[])
1936 const struct got_error *error = NULL;
1937 struct got_repository *repo = NULL;
1938 struct got_worktree *worktree = NULL;
1939 char *cwd = NULL;
1940 struct got_pathlist_head paths;
1941 struct got_pathlist_entry *pe;
1942 int ch, x;
1944 TAILQ_INIT(&paths);
1946 while ((ch = getopt(argc, argv, "")) != -1) {
1947 switch (ch) {
1948 default:
1949 usage_add();
1950 /* NOTREACHED */
1954 argc -= optind;
1955 argv += optind;
1957 if (argc < 1)
1958 usage_add();
1960 /* make sure each file exists before doing anything halfway */
1961 for (x = 0; x < argc; x++) {
1962 char *path = realpath(argv[x], NULL);
1963 if (path == NULL) {
1964 error = got_error_from_errno2("realpath", argv[x]);
1965 goto done;
1967 free(path);
1970 cwd = getcwd(NULL, 0);
1971 if (cwd == NULL) {
1972 error = got_error_from_errno("getcwd");
1973 goto done;
1976 error = got_worktree_open(&worktree, cwd);
1977 if (error)
1978 goto done;
1980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1981 if (error != NULL)
1982 goto done;
1984 error = apply_unveil(got_repo_get_path(repo), 1,
1985 got_worktree_get_root_path(worktree), 0, NULL);
1986 if (error)
1987 goto done;
1989 for (x = 0; x < argc; x++) {
1990 char *path = realpath(argv[x], NULL);
1991 if (path == NULL) {
1992 error = got_error_from_errno2("realpath", argv[x]);
1993 goto done;
1996 got_path_strip_trailing_slashes(path);
1997 error = got_pathlist_insert(&pe, &paths, path, NULL);
1998 if (error) {
1999 free(path);
2000 goto done;
2003 error = got_worktree_schedule_add(worktree, &paths, print_status,
2004 NULL, repo);
2005 done:
2006 if (repo)
2007 got_repo_close(repo);
2008 if (worktree)
2009 got_worktree_close(worktree);
2010 TAILQ_FOREACH(pe, &paths, entry)
2011 free((char *)pe->path);
2012 got_pathlist_free(&paths);
2013 free(cwd);
2014 return error;
2017 __dead static void
2018 usage_rm(void)
2020 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2021 exit(1);
2024 static const struct got_error *
2025 cmd_rm(int argc, char *argv[])
2027 const struct got_error *error = NULL;
2028 struct got_worktree *worktree = NULL;
2029 struct got_repository *repo = NULL;
2030 char *cwd = NULL, *path = NULL;
2031 int ch, delete_local_mods = 0;
2033 while ((ch = getopt(argc, argv, "f")) != -1) {
2034 switch (ch) {
2035 case 'f':
2036 delete_local_mods = 1;
2037 break;
2038 default:
2039 usage_add();
2040 /* NOTREACHED */
2044 argc -= optind;
2045 argv += optind;
2047 if (argc != 1)
2048 usage_rm();
2050 path = realpath(argv[0], NULL);
2051 if (path == NULL) {
2052 error = got_error_from_errno2("realpath", argv[0]);
2053 goto done;
2055 got_path_strip_trailing_slashes(path);
2057 cwd = getcwd(NULL, 0);
2058 if (cwd == NULL) {
2059 error = got_error_from_errno("getcwd");
2060 goto done;
2062 error = got_worktree_open(&worktree, cwd);
2063 if (error)
2064 goto done;
2066 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2067 if (error)
2068 goto done;
2070 error = apply_unveil(got_repo_get_path(repo), 1,
2071 got_worktree_get_root_path(worktree), 0, NULL);
2072 if (error)
2073 goto done;
2075 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2076 print_status, NULL, repo);
2077 if (error)
2078 goto done;
2079 done:
2080 if (repo)
2081 got_repo_close(repo);
2082 if (worktree)
2083 got_worktree_close(worktree);
2084 free(path);
2085 free(cwd);
2086 return error;
2089 __dead static void
2090 usage_revert(void)
2092 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2093 exit(1);
2096 static void
2097 revert_progress(void *arg, unsigned char status, const char *path)
2099 while (path[0] == '/')
2100 path++;
2101 printf("%c %s\n", status, path);
2104 static const struct got_error *
2105 cmd_revert(int argc, char *argv[])
2107 const struct got_error *error = NULL;
2108 struct got_worktree *worktree = NULL;
2109 struct got_repository *repo = NULL;
2110 char *cwd = NULL, *path = NULL;
2111 int ch;
2113 while ((ch = getopt(argc, argv, "")) != -1) {
2114 switch (ch) {
2115 default:
2116 usage_revert();
2117 /* NOTREACHED */
2121 argc -= optind;
2122 argv += optind;
2124 if (argc != 1)
2125 usage_revert();
2127 path = realpath(argv[0], NULL);
2128 if (path == NULL) {
2129 error = got_error_from_errno2("realpath", argv[0]);
2130 goto done;
2132 got_path_strip_trailing_slashes(path);
2134 cwd = getcwd(NULL, 0);
2135 if (cwd == NULL) {
2136 error = got_error_from_errno("getcwd");
2137 goto done;
2139 error = got_worktree_open(&worktree, cwd);
2140 if (error)
2141 goto done;
2143 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2144 if (error != NULL)
2145 goto done;
2147 error = apply_unveil(got_repo_get_path(repo), 1,
2148 got_worktree_get_root_path(worktree), 0, NULL);
2149 if (error)
2150 goto done;
2152 error = got_worktree_revert(worktree, path,
2153 revert_progress, NULL, repo);
2154 if (error)
2155 goto done;
2156 done:
2157 if (repo)
2158 got_repo_close(repo);
2159 if (worktree)
2160 got_worktree_close(worktree);
2161 free(path);
2162 free(cwd);
2163 return error;
2166 __dead static void
2167 usage_commit(void)
2169 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2170 exit(1);
2173 int
2174 spawn_editor(const char *editor, const char *file)
2176 pid_t pid;
2177 sig_t sighup, sigint, sigquit;
2178 int st = -1;
2180 sighup = signal(SIGHUP, SIG_IGN);
2181 sigint = signal(SIGINT, SIG_IGN);
2182 sigquit = signal(SIGQUIT, SIG_IGN);
2184 switch (pid = fork()) {
2185 case -1:
2186 goto doneediting;
2187 case 0:
2188 execl(editor, editor, file, (char *)NULL);
2189 _exit(127);
2192 while (waitpid(pid, &st, 0) == -1)
2193 if (errno != EINTR)
2194 break;
2196 doneediting:
2197 (void)signal(SIGHUP, sighup);
2198 (void)signal(SIGINT, sigint);
2199 (void)signal(SIGQUIT, sigquit);
2201 if (!WIFEXITED(st)) {
2202 errno = EINTR;
2203 return -1;
2206 return WEXITSTATUS(st);
2209 struct collect_commit_logmsg_arg {
2210 const char *cmdline_log;
2211 const char *editor;
2212 const char *worktree_path;
2213 char *logmsg_path;
2217 static const struct got_error *
2218 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2219 void *arg)
2221 const char *initial_content = "\n# changes to be committed:\n";
2222 struct got_pathlist_entry *pe;
2223 const struct got_error *err = NULL;
2224 char *template = NULL;
2225 struct collect_commit_logmsg_arg *a = arg;
2226 char buf[1024];
2227 struct stat st, st2;
2228 FILE *fp;
2229 size_t len;
2230 int fd, content_changed = 0;
2232 /* if a message was specified on the command line, just use it */
2233 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2234 len = strlen(a->cmdline_log) + 1;
2235 *logmsg = malloc(len + 1);
2236 if (*logmsg == NULL)
2237 return got_error_from_errno("malloc");
2238 strlcpy(*logmsg, a->cmdline_log, len);
2239 return NULL;
2242 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2243 return got_error_from_errno("asprintf");
2245 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2246 if (err)
2247 goto done;
2249 dprintf(fd, initial_content);
2251 TAILQ_FOREACH(pe, commitable_paths, entry) {
2252 struct got_commitable *ct = pe->data;
2253 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2255 close(fd);
2257 if (stat(a->logmsg_path, &st) == -1) {
2258 err = got_error_from_errno2("stat", a->logmsg_path);
2259 goto done;
2262 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2263 err = got_error_from_errno("failed spawning editor");
2264 goto done;
2267 if (stat(a->logmsg_path, &st2) == -1) {
2268 err = got_error_from_errno("stat");
2269 goto done;
2272 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2273 unlink(a->logmsg_path);
2274 free(a->logmsg_path);
2275 a->logmsg_path = NULL;
2276 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2277 "no changes made to commit message, aborting");
2278 goto done;
2281 /* remove comments */
2282 *logmsg = malloc(st2.st_size + 1);
2283 if (*logmsg == NULL) {
2284 err = got_error_from_errno("malloc");
2285 goto done;
2287 len = 0;
2289 fp = fopen(a->logmsg_path, "r");
2290 while (fgets(buf, sizeof(buf), fp) != NULL) {
2291 if (!content_changed && strcmp(buf, initial_content) != 0)
2292 content_changed = 1;
2293 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2294 continue;
2295 len = strlcat(*logmsg, buf, st2.st_size);
2297 fclose(fp);
2299 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2300 (*logmsg)[len - 1] = '\0';
2301 len--;
2304 if (len == 0 || !content_changed) {
2305 unlink(a->logmsg_path);
2306 free(a->logmsg_path);
2307 a->logmsg_path = NULL;
2308 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2309 "commit message cannot be empty, aborting");
2310 goto done;
2312 done:
2313 free(template);
2314 return err;
2317 static const struct got_error *
2318 cmd_commit(int argc, char *argv[])
2320 const struct got_error *error = NULL;
2321 struct got_worktree *worktree = NULL;
2322 struct got_repository *repo = NULL;
2323 char *cwd = NULL, *path = NULL, *id_str = NULL;
2324 struct got_object_id *id = NULL;
2325 const char *logmsg = NULL;
2326 const char *got_author = getenv("GOT_AUTHOR");
2327 struct collect_commit_logmsg_arg cl_arg;
2328 char *editor = NULL;
2329 int ch;
2331 while ((ch = getopt(argc, argv, "m:")) != -1) {
2332 switch (ch) {
2333 case 'm':
2334 logmsg = optarg;
2335 break;
2336 default:
2337 usage_commit();
2338 /* NOTREACHED */
2342 argc -= optind;
2343 argv += optind;
2345 if (argc == 1) {
2346 path = realpath(argv[0], NULL);
2347 if (path == NULL) {
2348 error = got_error_from_errno2("realpath", argv[0]);
2349 goto done;
2351 got_path_strip_trailing_slashes(path);
2352 } else if (argc != 0)
2353 usage_commit();
2355 if (got_author == NULL) {
2356 /* TODO: Look current user up in password database */
2357 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2358 goto done;
2361 cwd = getcwd(NULL, 0);
2362 if (cwd == NULL) {
2363 error = got_error_from_errno("getcwd");
2364 goto done;
2366 error = got_worktree_open(&worktree, cwd);
2367 if (error)
2368 goto done;
2370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2371 if (error != NULL)
2372 goto done;
2374 error = apply_unveil(got_repo_get_path(repo), 0,
2375 got_worktree_get_root_path(worktree), 0, &editor);
2376 if (error)
2377 goto done;
2379 cl_arg.editor = editor;
2380 cl_arg.cmdline_log = logmsg;
2381 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2382 cl_arg.logmsg_path = NULL;
2383 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2384 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2385 if (error) {
2386 if (cl_arg.logmsg_path)
2387 fprintf(stderr, "%s: log message preserved in %s\n",
2388 getprogname(), cl_arg.logmsg_path);
2389 goto done;
2392 if (cl_arg.logmsg_path)
2393 unlink(cl_arg.logmsg_path);
2395 error = got_object_id_str(&id_str, id);
2396 if (error)
2397 goto done;
2398 printf("created commit %s\n", id_str);
2399 done:
2400 if (repo)
2401 got_repo_close(repo);
2402 if (worktree)
2403 got_worktree_close(worktree);
2404 free(path);
2405 free(cwd);
2406 free(id_str);
2407 free(editor);
2408 return error;