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 **editorp)
202 const char *editor;
204 editor = getenv("VISUAL");
205 if (editor == NULL)
206 editor = getenv("EDITOR");
207 if (editor == NULL)
208 editor = "/bin/ed";
210 *editorp = realpath(editor, NULL);
211 if (*editorp == NULL)
212 return got_error_from_errno("relpath");
214 return NULL;
217 static const struct got_error *
218 apply_unveil(const char *repo_path, int repo_read_only,
219 const char *worktree_path, int create_worktree,
220 int unveil_editor)
222 const struct got_error *err;
223 static char err_msg[MAXPATHLEN + 36];
225 if (create_worktree) {
226 /* Pre-create work tree path to avoid unveiling its parents. */
227 err = got_path_mkdir(worktree_path);
229 if (errno == EEXIST) {
230 if (got_path_dir_is_empty(worktree_path)) {
231 errno = 0;
232 err = NULL;
233 } else {
234 snprintf(err_msg, sizeof(err_msg),
235 "%s: directory exists but is not empty",
236 worktree_path);
237 err = got_error_msg(GOT_ERR_BAD_PATH,
238 err_msg);
242 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
243 return err;
246 if (unveil_editor) {
247 char *editor;
248 err = get_editor(&editor);
249 if (err)
250 return err;
251 if (unveil(editor, "x") != 0) {
252 err = got_error_from_errno2("unveil", editor);
253 free(editor);
254 return err;
256 free(editor);
259 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
260 return got_error_from_errno2("unveil", repo_path);
262 if (worktree_path && unveil(worktree_path, "rwc") != 0)
263 return got_error_from_errno2("unveil", worktree_path);
265 if (unveil("/tmp", "rwc") != 0)
266 return got_error_from_errno2("unveil", "/tmp");
268 err = got_privsep_unveil_exec_helpers();
269 if (err != NULL)
270 return err;
272 if (unveil(NULL, NULL) != 0)
273 return got_error_from_errno("unveil");
275 return NULL;
278 __dead static void
279 usage_checkout(void)
281 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
282 "[worktree-path]\n", getprogname());
283 exit(1);
286 static void
287 checkout_progress(void *arg, unsigned char status, const char *path)
289 char *worktree_path = arg;
291 while (path[0] == '/')
292 path++;
294 printf("%c %s/%s\n", status, worktree_path, path);
297 static const struct got_error *
298 check_cancelled(void *arg)
300 if (sigint_received || sigpipe_received)
301 return got_error(GOT_ERR_CANCELLED);
302 return NULL;
305 static const struct got_error *
306 check_linear_ancestry(struct got_worktree *worktree,
307 struct got_object_id *commit_id, struct got_repository *repo)
309 const struct got_error *err = NULL;
310 struct got_object_id *yca_id, *base_commit_id;
312 base_commit_id = got_worktree_get_base_commit_id(worktree);
313 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
314 commit_id, base_commit_id, repo);
315 if (err)
316 return err;
318 if (yca_id == NULL)
319 return got_error(GOT_ERR_ANCESTRY);
321 /*
322 * Require a straight line of history between the target commit
323 * and the work tree's base commit.
325 * Non-linear situation such as the this require a rebase:
327 * (commit) D F (base_commit)
328 * \ /
329 * C E
330 * \ /
331 * B (yca)
332 * |
333 * A
335 * 'got update' only handles linear cases:
336 * Update forwards in time: A (base/yca) - B - C - D (commit)
337 * Update backwards in time: D (base) - C - D - A (commit/yca)
338 */
339 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
340 got_object_id_cmp(base_commit_id, yca_id) != 0)
341 return got_error(GOT_ERR_ANCESTRY);
343 free(yca_id);
344 return NULL;
348 static const struct got_error *
349 cmd_checkout(int argc, char *argv[])
351 const struct got_error *error = NULL;
352 struct got_repository *repo = NULL;
353 struct got_reference *head_ref = NULL;
354 struct got_worktree *worktree = NULL;
355 char *repo_path = NULL;
356 char *worktree_path = NULL;
357 const char *path_prefix = "";
358 char *commit_id_str = NULL;
359 int ch, same_path_prefix;
361 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
362 switch (ch) {
363 case 'c':
364 commit_id_str = strdup(optarg);
365 if (commit_id_str == NULL)
366 return got_error_from_errno("strdup");
367 break;
368 case 'p':
369 path_prefix = optarg;
370 break;
371 default:
372 usage_checkout();
373 /* NOTREACHED */
377 argc -= optind;
378 argv += optind;
380 #ifndef PROFILE
381 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
382 "unveil", NULL) == -1)
383 err(1, "pledge");
384 #endif
385 if (argc == 1) {
386 char *cwd, *base, *dotgit;
387 repo_path = realpath(argv[0], NULL);
388 if (repo_path == NULL)
389 return got_error_from_errno2("realpath", argv[0]);
390 cwd = getcwd(NULL, 0);
391 if (cwd == NULL) {
392 error = got_error_from_errno("getcwd");
393 goto done;
395 if (path_prefix[0]) {
396 base = basename(path_prefix);
397 if (base == NULL) {
398 error = got_error_from_errno2("basename",
399 path_prefix);
400 goto done;
402 } else {
403 base = basename(repo_path);
404 if (base == NULL) {
405 error = got_error_from_errno2("basename",
406 repo_path);
407 goto done;
410 dotgit = strstr(base, ".git");
411 if (dotgit)
412 *dotgit = '\0';
413 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
414 error = got_error_from_errno("asprintf");
415 free(cwd);
416 goto done;
418 free(cwd);
419 } else if (argc == 2) {
420 repo_path = realpath(argv[0], NULL);
421 if (repo_path == NULL) {
422 error = got_error_from_errno2("realpath", argv[0]);
423 goto done;
425 worktree_path = realpath(argv[1], NULL);
426 if (worktree_path == NULL) {
427 error = got_error_from_errno2("realpath", argv[1]);
428 goto done;
430 } else
431 usage_checkout();
433 got_path_strip_trailing_slashes(repo_path);
434 got_path_strip_trailing_slashes(worktree_path);
436 error = got_repo_open(&repo, repo_path);
437 if (error != NULL)
438 goto done;
440 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1, 0);
441 if (error)
442 goto done;
444 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
445 if (error != NULL)
446 goto done;
448 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
449 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
450 goto done;
452 error = got_worktree_open(&worktree, worktree_path);
453 if (error != NULL)
454 goto done;
456 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
457 path_prefix);
458 if (error != NULL)
459 goto done;
460 if (!same_path_prefix) {
461 error = got_error(GOT_ERR_PATH_PREFIX);
462 goto done;
465 if (commit_id_str) {
466 struct got_object_id *commit_id;
467 error = got_object_resolve_id_str(&commit_id, repo,
468 commit_id_str);
469 if (error != NULL)
470 goto done;
471 error = check_linear_ancestry(worktree, commit_id, repo);
472 if (error != NULL) {
473 free(commit_id);
474 goto done;
476 error = got_worktree_set_base_commit_id(worktree, repo,
477 commit_id);
478 free(commit_id);
479 if (error)
480 goto done;
483 error = got_worktree_checkout_files(worktree, "", repo,
484 checkout_progress, worktree_path, check_cancelled, NULL);
485 if (error != NULL)
486 goto done;
488 printf("Now shut up and hack\n");
490 done:
491 free(commit_id_str);
492 free(repo_path);
493 free(worktree_path);
494 return error;
497 __dead static void
498 usage_update(void)
500 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
501 getprogname());
502 exit(1);
505 static void
506 update_progress(void *arg, unsigned char status, const char *path)
508 int *did_something = arg;
510 if (status == GOT_STATUS_EXISTS)
511 return;
513 *did_something = 1;
514 while (path[0] == '/')
515 path++;
516 printf("%c %s\n", status, path);
519 static const struct got_error *
520 cmd_update(int argc, char *argv[])
522 const struct got_error *error = NULL;
523 struct got_repository *repo = NULL;
524 struct got_worktree *worktree = NULL;
525 char *worktree_path = NULL, *path = NULL;
526 struct got_object_id *commit_id = NULL;
527 char *commit_id_str = NULL;
528 int ch, did_something = 0;
530 while ((ch = getopt(argc, argv, "c:")) != -1) {
531 switch (ch) {
532 case 'c':
533 commit_id_str = strdup(optarg);
534 if (commit_id_str == NULL)
535 return got_error_from_errno("strdup");
536 break;
537 default:
538 usage_update();
539 /* NOTREACHED */
543 argc -= optind;
544 argv += optind;
546 #ifndef PROFILE
547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
548 "unveil", NULL) == -1)
549 err(1, "pledge");
550 #endif
551 worktree_path = getcwd(NULL, 0);
552 if (worktree_path == NULL) {
553 error = got_error_from_errno("getcwd");
554 goto done;
556 error = got_worktree_open(&worktree, worktree_path);
557 if (error)
558 goto done;
560 if (argc == 0) {
561 path = strdup("");
562 if (path == NULL) {
563 error = got_error_from_errno("strdup");
564 goto done;
566 } else if (argc == 1) {
567 error = got_worktree_resolve_path(&path, worktree, argv[0]);
568 if (error)
569 goto done;
570 } else
571 usage_update();
573 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
574 if (error != NULL)
575 goto done;
577 error = apply_unveil(got_repo_get_path(repo), 0,
578 got_worktree_get_root_path(worktree), 0, 0);
579 if (error)
580 goto done;
582 if (commit_id_str == NULL) {
583 struct got_reference *head_ref;
584 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
585 if (error != NULL)
586 goto done;
587 error = got_ref_resolve(&commit_id, repo, head_ref);
588 if (error != NULL)
589 goto done;
590 error = got_object_id_str(&commit_id_str, commit_id);
591 if (error != NULL)
592 goto done;
593 } else {
594 error = got_object_resolve_id_str(&commit_id, repo,
595 commit_id_str);
596 if (error != NULL)
597 goto done;
600 error = check_linear_ancestry(worktree, commit_id, repo);
601 if (error != NULL)
602 goto done;
604 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
605 commit_id) != 0) {
606 error = got_worktree_set_base_commit_id(worktree, repo,
607 commit_id);
608 if (error)
609 goto done;
612 error = got_worktree_checkout_files(worktree, path, repo,
613 update_progress, &did_something, check_cancelled, NULL);
614 if (error != NULL)
615 goto done;
617 if (did_something)
618 printf("Updated to commit %s\n", commit_id_str);
619 else
620 printf("Already up-to-date\n");
621 done:
622 free(worktree_path);
623 free(path);
624 free(commit_id);
625 free(commit_id_str);
626 return error;
629 static const struct got_error *
630 print_patch(struct got_commit_object *commit, struct got_object_id *id,
631 int diff_context, struct got_repository *repo)
633 const struct got_error *err = NULL;
634 struct got_tree_object *tree1 = NULL, *tree2;
635 struct got_object_qid *qid;
636 char *id_str1 = NULL, *id_str2;
638 err = got_object_open_as_tree(&tree2, repo,
639 got_object_commit_get_tree_id(commit));
640 if (err)
641 return err;
643 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
644 if (qid != NULL) {
645 struct got_commit_object *pcommit;
647 err = got_object_open_as_commit(&pcommit, repo, qid->id);
648 if (err)
649 return err;
651 err = got_object_open_as_tree(&tree1, repo,
652 got_object_commit_get_tree_id(pcommit));
653 got_object_commit_close(pcommit);
654 if (err)
655 return err;
657 err = got_object_id_str(&id_str1, qid->id);
658 if (err)
659 return err;
662 err = got_object_id_str(&id_str2, id);
663 if (err)
664 goto done;
666 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
667 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
668 done:
669 if (tree1)
670 got_object_tree_close(tree1);
671 got_object_tree_close(tree2);
672 free(id_str1);
673 free(id_str2);
674 return err;
677 static char *
678 get_datestr(time_t *time, char *datebuf)
680 char *p, *s = ctime_r(time, datebuf);
681 p = strchr(s, '\n');
682 if (p)
683 *p = '\0';
684 return s;
687 static const struct got_error *
688 print_commit(struct got_commit_object *commit, struct got_object_id *id,
689 struct got_repository *repo, int show_patch, int diff_context,
690 struct got_reflist_head *refs)
692 const struct got_error *err = NULL;
693 char *id_str, *datestr, *logmsg0, *logmsg, *line;
694 char datebuf[26];
695 time_t committer_time;
696 const char *author, *committer;
697 char *refs_str = NULL;
698 struct got_reflist_entry *re;
700 SIMPLEQ_FOREACH(re, refs, entry) {
701 char *s;
702 const char *name;
703 if (got_object_id_cmp(re->id, id) != 0)
704 continue;
705 name = got_ref_get_name(re->ref);
706 if (strcmp(name, GOT_REF_HEAD) == 0)
707 continue;
708 if (strncmp(name, "refs/", 5) == 0)
709 name += 5;
710 if (strncmp(name, "got/", 4) == 0)
711 continue;
712 if (strncmp(name, "heads/", 6) == 0)
713 name += 6;
714 if (strncmp(name, "remotes/", 8) == 0)
715 name += 8;
716 s = refs_str;
717 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
718 name) == -1) {
719 err = got_error_from_errno("asprintf");
720 free(s);
721 break;
723 free(s);
725 err = got_object_id_str(&id_str, id);
726 if (err)
727 return err;
729 printf("-----------------------------------------------\n");
730 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
731 refs_str ? refs_str : "", refs_str ? ")" : "");
732 free(id_str);
733 id_str = NULL;
734 free(refs_str);
735 refs_str = NULL;
736 printf("from: %s\n", got_object_commit_get_author(commit));
737 committer_time = got_object_commit_get_committer_time(commit);
738 datestr = get_datestr(&committer_time, datebuf);
739 printf("date: %s UTC\n", datestr);
740 author = got_object_commit_get_author(commit);
741 committer = got_object_commit_get_committer(commit);
742 if (strcmp(author, committer) != 0)
743 printf("via: %s\n", committer);
744 if (got_object_commit_get_nparents(commit) > 1) {
745 const struct got_object_id_queue *parent_ids;
746 struct got_object_qid *qid;
747 int n = 1;
748 parent_ids = got_object_commit_get_parent_ids(commit);
749 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
750 err = got_object_id_str(&id_str, qid->id);
751 if (err)
752 return err;
753 printf("parent %d: %s\n", n++, id_str);
754 free(id_str);
758 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
759 if (logmsg0 == NULL)
760 return got_error_from_errno("strdup");
762 logmsg = logmsg0;
763 do {
764 line = strsep(&logmsg, "\n");
765 if (line)
766 printf(" %s\n", line);
767 } while (line);
768 free(logmsg0);
770 if (show_patch) {
771 err = print_patch(commit, id, diff_context, repo);
772 if (err == 0)
773 printf("\n");
776 if (fflush(stdout) != 0 && err == NULL)
777 err = got_error_from_errno("fflush");
778 return err;
781 static const struct got_error *
782 print_commits(struct got_object_id *root_id, struct got_repository *repo,
783 char *path, int show_patch, int diff_context, int limit,
784 int first_parent_traversal, struct got_reflist_head *refs)
786 const struct got_error *err;
787 struct got_commit_graph *graph;
789 err = got_commit_graph_open(&graph, root_id, path,
790 first_parent_traversal, repo);
791 if (err)
792 return err;
793 err = got_commit_graph_iter_start(graph, root_id, repo);
794 if (err)
795 goto done;
796 for (;;) {
797 struct got_commit_object *commit;
798 struct got_object_id *id;
800 if (sigint_received || sigpipe_received)
801 break;
803 err = got_commit_graph_iter_next(&id, graph);
804 if (err) {
805 if (err->code == GOT_ERR_ITER_COMPLETED) {
806 err = NULL;
807 break;
809 if (err->code != GOT_ERR_ITER_NEED_MORE)
810 break;
811 err = got_commit_graph_fetch_commits(graph, 1, repo);
812 if (err)
813 break;
814 else
815 continue;
817 if (id == NULL)
818 break;
820 err = got_object_open_as_commit(&commit, repo, id);
821 if (err)
822 break;
823 err = print_commit(commit, id, repo, show_patch, diff_context,
824 refs);
825 got_object_commit_close(commit);
826 if (err || (limit && --limit == 0))
827 break;
829 done:
830 got_commit_graph_close(graph);
831 return err;
834 __dead static void
835 usage_log(void)
837 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
838 "[-r repository-path] [path]\n", getprogname());
839 exit(1);
842 static const struct got_error *
843 cmd_log(int argc, char *argv[])
845 const struct got_error *error;
846 struct got_repository *repo = NULL;
847 struct got_worktree *worktree = NULL;
848 struct got_commit_object *commit = NULL;
849 struct got_object_id *id = NULL;
850 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
851 char *start_commit = NULL;
852 int diff_context = 3, ch;
853 int show_patch = 0, limit = 0, first_parent_traversal = 0;
854 const char *errstr;
855 struct got_reflist_head refs;
857 SIMPLEQ_INIT(&refs);
859 #ifndef PROFILE
860 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
861 NULL)
862 == -1)
863 err(1, "pledge");
864 #endif
866 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
867 switch (ch) {
868 case 'p':
869 show_patch = 1;
870 break;
871 case 'c':
872 start_commit = optarg;
873 break;
874 case 'C':
875 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
876 &errstr);
877 if (errstr != NULL)
878 err(1, "-C option %s", errstr);
879 break;
880 case 'l':
881 limit = strtonum(optarg, 1, INT_MAX, &errstr);
882 if (errstr != NULL)
883 err(1, "-l option %s", errstr);
884 break;
885 case 'f':
886 first_parent_traversal = 1;
887 break;
888 case 'r':
889 repo_path = realpath(optarg, NULL);
890 if (repo_path == NULL)
891 err(1, "-r option");
892 got_path_strip_trailing_slashes(repo_path);
893 break;
894 default:
895 usage_log();
896 /* NOTREACHED */
900 argc -= optind;
901 argv += optind;
903 cwd = getcwd(NULL, 0);
904 if (cwd == NULL) {
905 error = got_error_from_errno("getcwd");
906 goto done;
909 error = got_worktree_open(&worktree, cwd);
910 if (error && error->code != GOT_ERR_NOT_WORKTREE)
911 goto done;
912 error = NULL;
914 if (argc == 0) {
915 path = strdup("");
916 if (path == NULL) {
917 error = got_error_from_errno("strdup");
918 goto done;
920 } else if (argc == 1) {
921 if (worktree) {
922 error = got_worktree_resolve_path(&path, worktree,
923 argv[0]);
924 if (error)
925 goto done;
926 } else {
927 path = strdup(argv[0]);
928 if (path == NULL) {
929 error = got_error_from_errno("strdup");
930 goto done;
933 } else
934 usage_log();
936 if (repo_path == NULL) {
937 repo_path = worktree ?
938 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
940 if (repo_path == NULL) {
941 error = got_error_from_errno("strdup");
942 goto done;
945 error = got_repo_open(&repo, repo_path);
946 if (error != NULL)
947 goto done;
949 error = apply_unveil(got_repo_get_path(repo), 1,
950 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
951 if (error)
952 goto done;
954 if (start_commit == NULL) {
955 struct got_reference *head_ref;
956 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
957 if (error != NULL)
958 return error;
959 error = got_ref_resolve(&id, repo, head_ref);
960 got_ref_close(head_ref);
961 if (error != NULL)
962 return error;
963 error = got_object_open_as_commit(&commit, repo, id);
964 } else {
965 struct got_reference *ref;
966 error = got_ref_open(&ref, repo, start_commit, 0);
967 if (error == NULL) {
968 int obj_type;
969 error = got_ref_resolve(&id, repo, ref);
970 got_ref_close(ref);
971 if (error != NULL)
972 goto done;
973 error = got_object_get_type(&obj_type, repo, id);
974 if (error != NULL)
975 goto done;
976 if (obj_type == GOT_OBJ_TYPE_TAG) {
977 struct got_tag_object *tag;
978 error = got_object_open_as_tag(&tag, repo, id);
979 if (error != NULL)
980 goto done;
981 if (got_object_tag_get_object_type(tag) !=
982 GOT_OBJ_TYPE_COMMIT) {
983 got_object_tag_close(tag);
984 error = got_error(GOT_ERR_OBJ_TYPE);
985 goto done;
987 free(id);
988 id = got_object_id_dup(
989 got_object_tag_get_object_id(tag));
990 if (id == NULL)
991 error = got_error_from_errno(
992 "got_object_id_dup");
993 got_object_tag_close(tag);
994 if (error)
995 goto done;
996 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
997 error = got_error(GOT_ERR_OBJ_TYPE);
998 goto done;
1000 error = got_object_open_as_commit(&commit, repo, id);
1001 if (error != NULL)
1002 goto done;
1004 if (commit == NULL) {
1005 error = got_object_resolve_id_str(&id, repo,
1006 start_commit);
1007 if (error != NULL)
1008 return error;
1011 if (error != NULL)
1012 goto done;
1014 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1015 if (error != NULL)
1016 goto done;
1017 if (in_repo_path) {
1018 free(path);
1019 path = in_repo_path;
1022 error = got_ref_list(&refs, repo);
1023 if (error)
1024 goto done;
1026 error = print_commits(id, repo, path, show_patch,
1027 diff_context, limit, first_parent_traversal, &refs);
1028 done:
1029 free(path);
1030 free(repo_path);
1031 free(cwd);
1032 free(id);
1033 if (worktree)
1034 got_worktree_close(worktree);
1035 if (repo) {
1036 const struct got_error *repo_error;
1037 repo_error = got_repo_close(repo);
1038 if (error == NULL)
1039 error = repo_error;
1041 got_ref_list_free(&refs);
1042 return error;
1045 __dead static void
1046 usage_diff(void)
1048 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1049 "[object1 object2 | path]\n", getprogname());
1050 exit(1);
1053 struct print_diff_arg {
1054 struct got_repository *repo;
1055 struct got_worktree *worktree;
1056 int diff_context;
1057 const char *id_str;
1058 int header_shown;
1061 static const struct got_error *
1062 print_diff(void *arg, unsigned char status, const char *path,
1063 struct got_object_id *id)
1065 struct print_diff_arg *a = arg;
1066 const struct got_error *err = NULL;
1067 struct got_blob_object *blob1 = NULL;
1068 FILE *f2 = NULL;
1069 char *abspath = NULL;
1070 struct stat sb;
1072 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1073 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1074 return NULL;
1076 if (!a->header_shown) {
1077 printf("diff %s %s\n", a->id_str,
1078 got_worktree_get_root_path(a->worktree));
1079 a->header_shown = 1;
1082 if (status != GOT_STATUS_ADD) {
1083 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1084 if (err)
1085 goto done;
1089 if (status != GOT_STATUS_DELETE) {
1090 if (asprintf(&abspath, "%s/%s",
1091 got_worktree_get_root_path(a->worktree), path) == -1) {
1092 err = got_error_from_errno("asprintf");
1093 goto done;
1096 f2 = fopen(abspath, "r");
1097 if (f2 == NULL) {
1098 err = got_error_from_errno2("fopen", abspath);
1099 goto done;
1101 if (lstat(abspath, &sb) == -1) {
1102 err = got_error_from_errno2("lstat", abspath);
1103 goto done;
1105 } else
1106 sb.st_size = 0;
1108 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1109 stdout);
1110 done:
1111 if (blob1)
1112 got_object_blob_close(blob1);
1113 if (f2 && fclose(f2) != 0 && err == NULL)
1114 err = got_error_from_errno("fclose");
1115 free(abspath);
1116 return err;
1119 static const struct got_error *
1120 cmd_diff(int argc, char *argv[])
1122 const struct got_error *error;
1123 struct got_repository *repo = NULL;
1124 struct got_worktree *worktree = NULL;
1125 char *cwd = NULL, *repo_path = NULL;
1126 struct got_object_id *id1 = NULL, *id2 = NULL;
1127 char *id_str1 = NULL, *id_str2 = NULL;
1128 int type1, type2;
1129 int diff_context = 3, ch;
1130 const char *errstr;
1131 char *path = NULL;
1133 #ifndef PROFILE
1134 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1135 NULL) == -1)
1136 err(1, "pledge");
1137 #endif
1139 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1140 switch (ch) {
1141 case 'C':
1142 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1143 if (errstr != NULL)
1144 err(1, "-C option %s", errstr);
1145 break;
1146 case 'r':
1147 repo_path = realpath(optarg, NULL);
1148 if (repo_path == NULL)
1149 err(1, "-r option");
1150 got_path_strip_trailing_slashes(repo_path);
1151 break;
1152 default:
1153 usage_diff();
1154 /* NOTREACHED */
1158 argc -= optind;
1159 argv += optind;
1161 cwd = getcwd(NULL, 0);
1162 if (cwd == NULL) {
1163 error = got_error_from_errno("getcwd");
1164 goto done;
1166 error = got_worktree_open(&worktree, cwd);
1167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1168 goto done;
1169 if (argc <= 1) {
1170 if (worktree == NULL) {
1171 error = got_error(GOT_ERR_NOT_WORKTREE);
1172 goto done;
1174 if (repo_path)
1175 errx(1,
1176 "-r option can't be used when diffing a work tree");
1177 repo_path = strdup(got_worktree_get_repo_path(worktree));
1178 if (repo_path == NULL) {
1179 error = got_error_from_errno("strdup");
1180 goto done;
1182 if (argc == 1) {
1183 error = got_worktree_resolve_path(&path, worktree,
1184 argv[0]);
1185 if (error)
1186 goto done;
1187 } else {
1188 path = strdup("");
1189 if (path == NULL) {
1190 error = got_error_from_errno("strdup");
1191 goto done;
1194 } else if (argc == 2) {
1195 id_str1 = argv[0];
1196 id_str2 = argv[1];
1197 } else
1198 usage_diff();
1200 if (repo_path == NULL) {
1201 repo_path = getcwd(NULL, 0);
1202 if (repo_path == NULL)
1203 return got_error_from_errno("getcwd");
1206 error = got_repo_open(&repo, repo_path);
1207 free(repo_path);
1208 if (error != NULL)
1209 goto done;
1211 error = apply_unveil(got_repo_get_path(repo), 1,
1212 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
1213 if (error)
1214 goto done;
1216 if (worktree) {
1217 struct print_diff_arg arg;
1218 char *id_str;
1219 error = got_object_id_str(&id_str,
1220 got_worktree_get_base_commit_id(worktree));
1221 if (error)
1222 goto done;
1223 arg.repo = repo;
1224 arg.worktree = worktree;
1225 arg.diff_context = diff_context;
1226 arg.id_str = id_str;
1227 arg.header_shown = 0;
1229 error = got_worktree_status(worktree, path, repo, print_diff,
1230 &arg, check_cancelled, NULL);
1231 free(id_str);
1232 goto done;
1235 error = got_object_resolve_id_str(&id1, repo, id_str1);
1236 if (error)
1237 goto done;
1239 error = got_object_resolve_id_str(&id2, repo, id_str2);
1240 if (error)
1241 goto done;
1243 error = got_object_get_type(&type1, repo, id1);
1244 if (error)
1245 goto done;
1247 error = got_object_get_type(&type2, repo, id2);
1248 if (error)
1249 goto done;
1251 if (type1 != type2) {
1252 error = got_error(GOT_ERR_OBJ_TYPE);
1253 goto done;
1256 switch (type1) {
1257 case GOT_OBJ_TYPE_BLOB:
1258 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1259 diff_context, repo, stdout);
1260 break;
1261 case GOT_OBJ_TYPE_TREE:
1262 error = got_diff_objects_as_trees(id1, id2, "", "",
1263 diff_context, repo, stdout);
1264 break;
1265 case GOT_OBJ_TYPE_COMMIT:
1266 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1267 id_str2);
1268 error = got_diff_objects_as_commits(id1, id2, diff_context,
1269 repo, stdout);
1270 break;
1271 default:
1272 error = got_error(GOT_ERR_OBJ_TYPE);
1275 done:
1276 free(id1);
1277 free(id2);
1278 free(path);
1279 if (worktree)
1280 got_worktree_close(worktree);
1281 if (repo) {
1282 const struct got_error *repo_error;
1283 repo_error = got_repo_close(repo);
1284 if (error == NULL)
1285 error = repo_error;
1287 return error;
1290 __dead static void
1291 usage_blame(void)
1293 fprintf(stderr,
1294 "usage: %s blame [-c commit] [-r repository-path] path\n",
1295 getprogname());
1296 exit(1);
1299 static const struct got_error *
1300 cmd_blame(int argc, char *argv[])
1302 const struct got_error *error;
1303 struct got_repository *repo = NULL;
1304 struct got_worktree *worktree = NULL;
1305 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1306 struct got_object_id *commit_id = NULL;
1307 char *commit_id_str = NULL;
1308 int ch;
1310 #ifndef PROFILE
1311 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1312 NULL) == -1)
1313 err(1, "pledge");
1314 #endif
1316 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1317 switch (ch) {
1318 case 'c':
1319 commit_id_str = optarg;
1320 break;
1321 case 'r':
1322 repo_path = realpath(optarg, NULL);
1323 if (repo_path == NULL)
1324 err(1, "-r option");
1325 got_path_strip_trailing_slashes(repo_path);
1326 break;
1327 default:
1328 usage_blame();
1329 /* NOTREACHED */
1333 argc -= optind;
1334 argv += optind;
1336 if (argc == 1)
1337 path = argv[0];
1338 else
1339 usage_blame();
1341 cwd = getcwd(NULL, 0);
1342 if (cwd == NULL) {
1343 error = got_error_from_errno("getcwd");
1344 goto done;
1346 if (repo_path == NULL) {
1347 error = got_worktree_open(&worktree, cwd);
1348 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1349 goto done;
1350 else
1351 error = NULL;
1352 if (worktree) {
1353 repo_path =
1354 strdup(got_worktree_get_repo_path(worktree));
1355 if (repo_path == NULL)
1356 error = got_error_from_errno("strdup");
1357 if (error)
1358 goto done;
1359 } else {
1360 repo_path = strdup(cwd);
1361 if (repo_path == NULL) {
1362 error = got_error_from_errno("strdup");
1363 goto done;
1368 error = got_repo_open(&repo, repo_path);
1369 if (error != NULL)
1370 goto done;
1372 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, 0);
1373 if (error)
1374 goto done;
1376 if (worktree) {
1377 const char *prefix = got_worktree_get_path_prefix(worktree);
1378 char *p, *worktree_subdir = cwd +
1379 strlen(got_worktree_get_root_path(worktree));
1380 if (asprintf(&p, "%s%s%s%s%s",
1381 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1382 worktree_subdir, worktree_subdir[0] ? "/" : "",
1383 path) == -1) {
1384 error = got_error_from_errno("asprintf");
1385 goto done;
1387 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1388 free(p);
1389 } else {
1390 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1392 if (error)
1393 goto done;
1395 if (commit_id_str == NULL) {
1396 struct got_reference *head_ref;
1397 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1398 if (error != NULL)
1399 goto done;
1400 error = got_ref_resolve(&commit_id, repo, head_ref);
1401 got_ref_close(head_ref);
1402 if (error != NULL)
1403 goto done;
1404 } else {
1405 error = got_object_resolve_id_str(&commit_id, repo,
1406 commit_id_str);
1407 if (error != NULL)
1408 goto done;
1411 error = got_blame(in_repo_path, commit_id, repo, stdout);
1412 done:
1413 free(in_repo_path);
1414 free(repo_path);
1415 free(cwd);
1416 free(commit_id);
1417 if (worktree)
1418 got_worktree_close(worktree);
1419 if (repo) {
1420 const struct got_error *repo_error;
1421 repo_error = got_repo_close(repo);
1422 if (error == NULL)
1423 error = repo_error;
1425 return error;
1428 __dead static void
1429 usage_tree(void)
1431 fprintf(stderr,
1432 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1433 getprogname());
1434 exit(1);
1437 static void
1438 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1439 const char *root_path)
1441 int is_root_path = (strcmp(path, root_path) == 0);
1443 path += strlen(root_path);
1444 while (path[0] == '/')
1445 path++;
1447 printf("%s%s%s%s%s\n", id ? id : "", path,
1448 is_root_path ? "" : "/", te->name,
1449 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1452 static const struct got_error *
1453 print_tree(const char *path, struct got_object_id *commit_id,
1454 int show_ids, int recurse, const char *root_path,
1455 struct got_repository *repo)
1457 const struct got_error *err = NULL;
1458 struct got_object_id *tree_id = NULL;
1459 struct got_tree_object *tree = NULL;
1460 const struct got_tree_entries *entries;
1461 struct got_tree_entry *te;
1463 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1464 if (err)
1465 goto done;
1467 err = got_object_open_as_tree(&tree, repo, tree_id);
1468 if (err)
1469 goto done;
1470 entries = got_object_tree_get_entries(tree);
1471 te = SIMPLEQ_FIRST(&entries->head);
1472 while (te) {
1473 char *id = NULL;
1475 if (sigint_received || sigpipe_received)
1476 break;
1478 if (show_ids) {
1479 char *id_str;
1480 err = got_object_id_str(&id_str, te->id);
1481 if (err)
1482 goto done;
1483 if (asprintf(&id, "%s ", id_str) == -1) {
1484 err = got_error_from_errno("asprintf");
1485 free(id_str);
1486 goto done;
1488 free(id_str);
1490 print_entry(te, id, path, root_path);
1491 free(id);
1493 if (recurse && S_ISDIR(te->mode)) {
1494 char *child_path;
1495 if (asprintf(&child_path, "%s%s%s", path,
1496 path[0] == '/' && path[1] == '\0' ? "" : "/",
1497 te->name) == -1) {
1498 err = got_error_from_errno("asprintf");
1499 goto done;
1501 err = print_tree(child_path, commit_id, show_ids, 1,
1502 root_path, repo);
1503 free(child_path);
1504 if (err)
1505 goto done;
1508 te = SIMPLEQ_NEXT(te, entry);
1510 done:
1511 if (tree)
1512 got_object_tree_close(tree);
1513 free(tree_id);
1514 return err;
1517 static const struct got_error *
1518 cmd_tree(int argc, char *argv[])
1520 const struct got_error *error;
1521 struct got_repository *repo = NULL;
1522 struct got_worktree *worktree = NULL;
1523 const char *path;
1524 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1525 struct got_object_id *commit_id = NULL;
1526 char *commit_id_str = NULL;
1527 int show_ids = 0, recurse = 0;
1528 int ch;
1530 #ifndef PROFILE
1531 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1532 NULL) == -1)
1533 err(1, "pledge");
1534 #endif
1536 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1537 switch (ch) {
1538 case 'c':
1539 commit_id_str = optarg;
1540 break;
1541 case 'r':
1542 repo_path = realpath(optarg, NULL);
1543 if (repo_path == NULL)
1544 err(1, "-r option");
1545 got_path_strip_trailing_slashes(repo_path);
1546 break;
1547 case 'i':
1548 show_ids = 1;
1549 break;
1550 case 'R':
1551 recurse = 1;
1552 break;
1553 default:
1554 usage_tree();
1555 /* NOTREACHED */
1559 argc -= optind;
1560 argv += optind;
1562 if (argc == 1)
1563 path = argv[0];
1564 else if (argc > 1)
1565 usage_tree();
1566 else
1567 path = NULL;
1569 cwd = getcwd(NULL, 0);
1570 if (cwd == NULL) {
1571 error = got_error_from_errno("getcwd");
1572 goto done;
1574 if (repo_path == NULL) {
1575 error = got_worktree_open(&worktree, cwd);
1576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1577 goto done;
1578 else
1579 error = NULL;
1580 if (worktree) {
1581 repo_path =
1582 strdup(got_worktree_get_repo_path(worktree));
1583 if (repo_path == NULL)
1584 error = got_error_from_errno("strdup");
1585 if (error)
1586 goto done;
1587 } else {
1588 repo_path = strdup(cwd);
1589 if (repo_path == NULL) {
1590 error = got_error_from_errno("strdup");
1591 goto done;
1596 error = got_repo_open(&repo, repo_path);
1597 if (error != NULL)
1598 goto done;
1600 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, 0);
1601 if (error)
1602 goto done;
1604 if (path == NULL) {
1605 if (worktree) {
1606 char *p, *worktree_subdir = cwd +
1607 strlen(got_worktree_get_root_path(worktree));
1608 if (asprintf(&p, "%s/%s",
1609 got_worktree_get_path_prefix(worktree),
1610 worktree_subdir) == -1) {
1611 error = got_error_from_errno("asprintf");
1612 goto done;
1614 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1615 free(p);
1616 if (error)
1617 goto done;
1618 } else
1619 path = "/";
1621 if (in_repo_path == NULL) {
1622 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1623 if (error != NULL)
1624 goto done;
1627 if (commit_id_str == NULL) {
1628 struct got_reference *head_ref;
1629 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1630 if (error != NULL)
1631 goto done;
1632 error = got_ref_resolve(&commit_id, repo, head_ref);
1633 got_ref_close(head_ref);
1634 if (error != NULL)
1635 goto done;
1636 } else {
1637 error = got_object_resolve_id_str(&commit_id, repo,
1638 commit_id_str);
1639 if (error != NULL)
1640 goto done;
1643 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1644 in_repo_path, repo);
1645 done:
1646 free(in_repo_path);
1647 free(repo_path);
1648 free(cwd);
1649 free(commit_id);
1650 if (worktree)
1651 got_worktree_close(worktree);
1652 if (repo) {
1653 const struct got_error *repo_error;
1654 repo_error = got_repo_close(repo);
1655 if (error == NULL)
1656 error = repo_error;
1658 return error;
1661 __dead static void
1662 usage_status(void)
1664 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1665 exit(1);
1668 static const struct got_error *
1669 print_status(void *arg, unsigned char status, const char *path,
1670 struct got_object_id *id)
1672 printf("%c %s\n", status, path);
1673 return NULL;
1676 static const struct got_error *
1677 cmd_status(int argc, char *argv[])
1679 const struct got_error *error = NULL;
1680 struct got_repository *repo = NULL;
1681 struct got_worktree *worktree = NULL;
1682 char *cwd = NULL, *path = NULL;
1683 int ch;
1685 while ((ch = getopt(argc, argv, "")) != -1) {
1686 switch (ch) {
1687 default:
1688 usage_status();
1689 /* NOTREACHED */
1693 argc -= optind;
1694 argv += optind;
1696 #ifndef PROFILE
1697 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1698 NULL) == -1)
1699 err(1, "pledge");
1700 #endif
1701 cwd = getcwd(NULL, 0);
1702 if (cwd == NULL) {
1703 error = got_error_from_errno("getcwd");
1704 goto done;
1707 error = got_worktree_open(&worktree, cwd);
1708 if (error != NULL)
1709 goto done;
1711 if (argc == 0) {
1712 path = strdup("");
1713 if (path == NULL) {
1714 error = got_error_from_errno("strdup");
1715 goto done;
1717 } else if (argc == 1) {
1718 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1719 if (error)
1720 goto done;
1721 } else
1722 usage_status();
1724 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1725 if (error != NULL)
1726 goto done;
1728 error = apply_unveil(got_repo_get_path(repo), 1,
1729 got_worktree_get_root_path(worktree), 0, 0);
1730 if (error)
1731 goto done;
1733 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1734 check_cancelled, NULL);
1735 done:
1736 free(cwd);
1737 free(path);
1738 return error;
1741 __dead static void
1742 usage_ref(void)
1744 fprintf(stderr,
1745 "usage: %s ref [-r repository] -l | -d name | name object\n",
1746 getprogname());
1747 exit(1);
1750 static const struct got_error *
1751 list_refs(struct got_repository *repo)
1753 static const struct got_error *err = NULL;
1754 struct got_reflist_head refs;
1755 struct got_reflist_entry *re;
1757 SIMPLEQ_INIT(&refs);
1758 err = got_ref_list(&refs, repo);
1759 if (err)
1760 return err;
1762 SIMPLEQ_FOREACH(re, &refs, entry) {
1763 char *refstr;
1764 refstr = got_ref_to_str(re->ref);
1765 if (refstr == NULL)
1766 return got_error_from_errno("got_ref_to_str");
1767 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1768 free(refstr);
1771 got_ref_list_free(&refs);
1772 return NULL;
1775 static const struct got_error *
1776 delete_ref(struct got_repository *repo, const char *refname)
1778 const struct got_error *err = NULL;
1779 struct got_reference *ref;
1781 err = got_ref_open(&ref, repo, refname, 0);
1782 if (err)
1783 return err;
1785 err = got_ref_delete(ref, repo);
1786 got_ref_close(ref);
1787 return err;
1790 static const struct got_error *
1791 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1793 const struct got_error *err = NULL;
1794 struct got_object_id *id;
1795 struct got_reference *ref = NULL;
1797 err = got_object_resolve_id_str(&id, repo, id_str);
1798 if (err)
1799 return err;
1801 err = got_ref_alloc(&ref, refname, id);
1802 if (err)
1803 goto done;
1805 err = got_ref_write(ref, repo);
1806 done:
1807 if (ref)
1808 got_ref_close(ref);
1809 free(id);
1810 return err;
1813 static const struct got_error *
1814 cmd_ref(int argc, char *argv[])
1816 const struct got_error *error = NULL;
1817 struct got_repository *repo = NULL;
1818 struct got_worktree *worktree = NULL;
1819 char *cwd = NULL, *repo_path = NULL;
1820 int ch, do_list = 0;
1821 const char *delref = NULL;
1823 /* TODO: Add -s option for adding symbolic references. */
1824 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1825 switch (ch) {
1826 case 'd':
1827 delref = optarg;
1828 break;
1829 case 'r':
1830 repo_path = realpath(optarg, NULL);
1831 if (repo_path == NULL)
1832 err(1, "-r option");
1833 got_path_strip_trailing_slashes(repo_path);
1834 break;
1835 case 'l':
1836 do_list = 1;
1837 break;
1838 default:
1839 usage_ref();
1840 /* NOTREACHED */
1844 if (do_list && delref)
1845 errx(1, "-l and -d options are mutually exclusive\n");
1847 argc -= optind;
1848 argv += optind;
1850 if (do_list || delref) {
1851 if (argc > 0)
1852 usage_ref();
1853 } else if (argc != 2)
1854 usage_ref();
1856 #ifndef PROFILE
1857 if (do_list) {
1858 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1859 NULL) == -1)
1860 err(1, "pledge");
1861 } else {
1862 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1863 "sendfd unveil", NULL) == -1)
1864 err(1, "pledge");
1866 #endif
1867 cwd = getcwd(NULL, 0);
1868 if (cwd == NULL) {
1869 error = got_error_from_errno("getcwd");
1870 goto done;
1873 if (repo_path == NULL) {
1874 error = got_worktree_open(&worktree, cwd);
1875 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1876 goto done;
1877 else
1878 error = NULL;
1879 if (worktree) {
1880 repo_path =
1881 strdup(got_worktree_get_repo_path(worktree));
1882 if (repo_path == NULL)
1883 error = got_error_from_errno("strdup");
1884 if (error)
1885 goto done;
1886 } else {
1887 repo_path = strdup(cwd);
1888 if (repo_path == NULL) {
1889 error = got_error_from_errno("strdup");
1890 goto done;
1895 error = got_repo_open(&repo, repo_path);
1896 if (error != NULL)
1897 goto done;
1899 error = apply_unveil(got_repo_get_path(repo), do_list,
1900 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
1901 if (error)
1902 goto done;
1904 if (do_list)
1905 error = list_refs(repo);
1906 else if (delref)
1907 error = delete_ref(repo, delref);
1908 else
1909 error = add_ref(repo, argv[0], argv[1]);
1910 done:
1911 if (repo)
1912 got_repo_close(repo);
1913 if (worktree)
1914 got_worktree_close(worktree);
1915 free(cwd);
1916 free(repo_path);
1917 return error;
1920 __dead static void
1921 usage_add(void)
1923 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1924 exit(1);
1927 static const struct got_error *
1928 cmd_add(int argc, char *argv[])
1930 const struct got_error *error = NULL;
1931 struct got_repository *repo = NULL;
1932 struct got_worktree *worktree = NULL;
1933 char *cwd = NULL;
1934 struct got_pathlist_head paths;
1935 struct got_pathlist_entry *pe;
1936 int ch, x;
1938 TAILQ_INIT(&paths);
1940 while ((ch = getopt(argc, argv, "")) != -1) {
1941 switch (ch) {
1942 default:
1943 usage_add();
1944 /* NOTREACHED */
1948 argc -= optind;
1949 argv += optind;
1951 if (argc < 1)
1952 usage_add();
1954 /* make sure each file exists before doing anything halfway */
1955 for (x = 0; x < argc; x++) {
1956 char *path = realpath(argv[x], NULL);
1957 if (path == NULL) {
1958 error = got_error_from_errno2("realpath", argv[x]);
1959 goto done;
1961 free(path);
1964 cwd = getcwd(NULL, 0);
1965 if (cwd == NULL) {
1966 error = got_error_from_errno("getcwd");
1967 goto done;
1970 error = got_worktree_open(&worktree, cwd);
1971 if (error)
1972 goto done;
1974 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1975 if (error != NULL)
1976 goto done;
1978 error = apply_unveil(got_repo_get_path(repo), 1,
1979 got_worktree_get_root_path(worktree), 0, 0);
1980 if (error)
1981 goto done;
1983 for (x = 0; x < argc; x++) {
1984 char *path = realpath(argv[x], NULL);
1985 if (path == NULL) {
1986 error = got_error_from_errno2("realpath", argv[x]);
1987 goto done;
1990 got_path_strip_trailing_slashes(path);
1991 error = got_pathlist_insert(&pe, &paths, path, NULL);
1992 if (error) {
1993 free(path);
1994 goto done;
1997 error = got_worktree_schedule_add(worktree, &paths, print_status,
1998 NULL, repo);
1999 done:
2000 if (repo)
2001 got_repo_close(repo);
2002 if (worktree)
2003 got_worktree_close(worktree);
2004 TAILQ_FOREACH(pe, &paths, entry)
2005 free((char *)pe->path);
2006 got_pathlist_free(&paths);
2007 free(cwd);
2008 return error;
2011 __dead static void
2012 usage_rm(void)
2014 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2015 exit(1);
2018 static const struct got_error *
2019 cmd_rm(int argc, char *argv[])
2021 const struct got_error *error = NULL;
2022 struct got_worktree *worktree = NULL;
2023 struct got_repository *repo = NULL;
2024 char *cwd = NULL, *path = NULL;
2025 int ch, delete_local_mods = 0;
2027 while ((ch = getopt(argc, argv, "f")) != -1) {
2028 switch (ch) {
2029 case 'f':
2030 delete_local_mods = 1;
2031 break;
2032 default:
2033 usage_add();
2034 /* NOTREACHED */
2038 argc -= optind;
2039 argv += optind;
2041 if (argc != 1)
2042 usage_rm();
2044 path = realpath(argv[0], NULL);
2045 if (path == NULL) {
2046 error = got_error_from_errno2("realpath", argv[0]);
2047 goto done;
2049 got_path_strip_trailing_slashes(path);
2051 cwd = getcwd(NULL, 0);
2052 if (cwd == NULL) {
2053 error = got_error_from_errno("getcwd");
2054 goto done;
2056 error = got_worktree_open(&worktree, cwd);
2057 if (error)
2058 goto done;
2060 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2061 if (error)
2062 goto done;
2064 error = apply_unveil(got_repo_get_path(repo), 1,
2065 got_worktree_get_root_path(worktree), 0, 0);
2066 if (error)
2067 goto done;
2069 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2070 print_status, NULL, repo);
2071 if (error)
2072 goto done;
2073 done:
2074 if (repo)
2075 got_repo_close(repo);
2076 if (worktree)
2077 got_worktree_close(worktree);
2078 free(path);
2079 free(cwd);
2080 return error;
2083 __dead static void
2084 usage_revert(void)
2086 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2087 exit(1);
2090 static void
2091 revert_progress(void *arg, unsigned char status, const char *path)
2093 while (path[0] == '/')
2094 path++;
2095 printf("%c %s\n", status, path);
2098 static const struct got_error *
2099 cmd_revert(int argc, char *argv[])
2101 const struct got_error *error = NULL;
2102 struct got_worktree *worktree = NULL;
2103 struct got_repository *repo = NULL;
2104 char *cwd = NULL, *path = NULL;
2105 int ch;
2107 while ((ch = getopt(argc, argv, "")) != -1) {
2108 switch (ch) {
2109 default:
2110 usage_revert();
2111 /* NOTREACHED */
2115 argc -= optind;
2116 argv += optind;
2118 if (argc != 1)
2119 usage_revert();
2121 path = realpath(argv[0], NULL);
2122 if (path == NULL) {
2123 error = got_error_from_errno2("realpath", argv[0]);
2124 goto done;
2126 got_path_strip_trailing_slashes(path);
2128 cwd = getcwd(NULL, 0);
2129 if (cwd == NULL) {
2130 error = got_error_from_errno("getcwd");
2131 goto done;
2133 error = got_worktree_open(&worktree, cwd);
2134 if (error)
2135 goto done;
2137 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2138 if (error != NULL)
2139 goto done;
2141 error = apply_unveil(got_repo_get_path(repo), 1,
2142 got_worktree_get_root_path(worktree), 0, 0);
2143 if (error)
2144 goto done;
2146 error = got_worktree_revert(worktree, path,
2147 revert_progress, NULL, repo);
2148 if (error)
2149 goto done;
2150 done:
2151 if (repo)
2152 got_repo_close(repo);
2153 if (worktree)
2154 got_worktree_close(worktree);
2155 free(path);
2156 free(cwd);
2157 return error;
2160 __dead static void
2161 usage_commit(void)
2163 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2164 exit(1);
2167 int
2168 spawn_editor(const char *editor, const char *file)
2170 pid_t pid;
2171 sig_t sighup, sigint, sigquit;
2172 int st = -1;
2174 sighup = signal(SIGHUP, SIG_IGN);
2175 sigint = signal(SIGINT, SIG_IGN);
2176 sigquit = signal(SIGQUIT, SIG_IGN);
2178 switch (pid = fork()) {
2179 case -1:
2180 goto doneediting;
2181 case 0:
2182 execl(editor, editor, file, (char *)NULL);
2183 _exit(127);
2186 while (waitpid(pid, &st, 0) == -1)
2187 if (errno != EINTR)
2188 break;
2190 doneediting:
2191 (void)signal(SIGHUP, sighup);
2192 (void)signal(SIGINT, sigint);
2193 (void)signal(SIGQUIT, sigquit);
2195 if (!WIFEXITED(st)) {
2196 errno = EINTR;
2197 return -1;
2200 return WEXITSTATUS(st);
2203 struct collect_commit_logmsg_arg {
2204 const char *cmdline_log;
2205 const char *editor;
2206 const char *worktree_path;
2207 char *logmsg_path;
2211 static const struct got_error *
2212 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2213 void *arg)
2215 const char *initial_content = "\n# changes to be committed:\n";
2216 struct got_pathlist_entry *pe;
2217 const struct got_error *err = NULL;
2218 char *template = NULL;
2219 struct collect_commit_logmsg_arg *a = arg;
2220 char buf[1024];
2221 struct stat st, st2;
2222 FILE *fp;
2223 size_t len;
2224 int fd, content_changed = 0;
2226 /* if a message was specified on the command line, just use it */
2227 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2228 len = strlen(a->cmdline_log) + 1;
2229 *logmsg = malloc(len + 1);
2230 if (*logmsg == NULL)
2231 return got_error_from_errno("malloc");
2232 strlcpy(*logmsg, a->cmdline_log, len);
2233 return NULL;
2236 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2237 return got_error_from_errno("asprintf");
2239 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2240 if (err)
2241 goto done;
2243 dprintf(fd, initial_content);
2245 TAILQ_FOREACH(pe, commitable_paths, entry) {
2246 struct got_commitable *ct = pe->data;
2247 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2249 close(fd);
2251 if (stat(a->logmsg_path, &st) == -1) {
2252 err = got_error_from_errno2("stat", a->logmsg_path);
2253 goto done;
2256 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2257 err = got_error_from_errno("failed spawning editor");
2258 goto done;
2261 if (stat(a->logmsg_path, &st2) == -1) {
2262 err = got_error_from_errno("stat");
2263 goto done;
2266 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2267 unlink(a->logmsg_path);
2268 free(a->logmsg_path);
2269 a->logmsg_path = NULL;
2270 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2271 "no changes made to commit message, aborting");
2272 goto done;
2275 /* remove comments */
2276 *logmsg = malloc(st2.st_size + 1);
2277 if (*logmsg == NULL) {
2278 err = got_error_from_errno("malloc");
2279 goto done;
2281 len = 0;
2283 fp = fopen(a->logmsg_path, "r");
2284 while (fgets(buf, sizeof(buf), fp) != NULL) {
2285 if (!content_changed && strcmp(buf, initial_content) != 0)
2286 content_changed = 1;
2287 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2288 continue;
2289 len = strlcat(*logmsg, buf, st2.st_size);
2291 fclose(fp);
2293 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2294 (*logmsg)[len - 1] = '\0';
2295 len--;
2298 if (len == 0 || !content_changed) {
2299 unlink(a->logmsg_path);
2300 free(a->logmsg_path);
2301 a->logmsg_path = NULL;
2302 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2303 "commit message cannot be empty, aborting");
2304 goto done;
2306 done:
2307 free(template);
2308 return err;
2311 static const struct got_error *
2312 cmd_commit(int argc, char *argv[])
2314 const struct got_error *error = NULL;
2315 struct got_worktree *worktree = NULL;
2316 struct got_repository *repo = NULL;
2317 char *cwd = NULL, *path = NULL, *id_str = NULL;
2318 struct got_object_id *id = NULL;
2319 const char *logmsg = NULL;
2320 const char *got_author = getenv("GOT_AUTHOR");
2321 struct collect_commit_logmsg_arg cl_arg;
2322 char *editor = NULL;
2323 int ch;
2325 while ((ch = getopt(argc, argv, "m:")) != -1) {
2326 switch (ch) {
2327 case 'm':
2328 logmsg = optarg;
2329 break;
2330 default:
2331 usage_commit();
2332 /* NOTREACHED */
2336 argc -= optind;
2337 argv += optind;
2339 if (argc == 1) {
2340 path = realpath(argv[0], NULL);
2341 if (path == NULL) {
2342 error = got_error_from_errno2("realpath", argv[0]);
2343 goto done;
2345 got_path_strip_trailing_slashes(path);
2346 } else if (argc != 0)
2347 usage_commit();
2349 if (got_author == NULL) {
2350 /* TODO: Look current user up in password database */
2351 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2352 goto done;
2355 cwd = getcwd(NULL, 0);
2356 if (cwd == NULL) {
2357 error = got_error_from_errno("getcwd");
2358 goto done;
2360 error = got_worktree_open(&worktree, cwd);
2361 if (error)
2362 goto done;
2364 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2365 if (error != NULL)
2366 goto done;
2368 error = apply_unveil(got_repo_get_path(repo), 0,
2369 got_worktree_get_root_path(worktree), 0, 1);
2370 if (error)
2371 goto done;
2373 error = get_editor(&editor);
2374 if (error)
2375 goto done;
2376 cl_arg.editor = editor;
2377 cl_arg.cmdline_log = logmsg;
2378 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2379 cl_arg.logmsg_path = NULL;
2380 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2381 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2382 if (error) {
2383 if (cl_arg.logmsg_path)
2384 fprintf(stderr, "%s: log message preserved in %s\n",
2385 getprogname(), cl_arg.logmsg_path);
2386 goto done;
2389 if (cl_arg.logmsg_path)
2390 unlink(cl_arg.logmsg_path);
2392 error = got_object_id_str(&id_str, id);
2393 if (error)
2394 goto done;
2395 printf("created commit %s\n", id_str);
2396 done:
2397 if (repo)
2398 got_repo_close(repo);
2399 if (worktree)
2400 got_worktree_close(worktree);
2401 free(path);
2402 free(cwd);
2403 free(id_str);
2404 free(editor);
2405 return error;