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 apply_unveil(const char *repo_path, int repo_read_only,
201 const char *worktree_path, int create_worktree)
203 const struct got_error *error;
204 static char err_msg[MAXPATHLEN + 36];
206 if (create_worktree) {
207 /* Pre-create work tree path to avoid unveiling its parents. */
208 error = got_path_mkdir(worktree_path);
210 if (errno == EEXIST) {
211 if (got_path_dir_is_empty(worktree_path)) {
212 errno = 0;
213 error = NULL;
214 } else {
215 snprintf(err_msg, sizeof(err_msg),
216 "%s: directory exists but is not empty",
217 worktree_path);
218 error = got_error_msg(GOT_ERR_BAD_PATH,
219 err_msg);
223 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
224 return error;
227 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
228 return got_error_prefix_errno2("unveil", repo_path);
230 if (worktree_path && unveil(worktree_path, "rwc") != 0)
231 return got_error_prefix_errno2("unveil", worktree_path);
233 if (unveil("/tmp", "rwc") != 0)
234 return got_error_prefix_errno2("unveil", "/tmp");
236 error = got_privsep_unveil_exec_helpers();
237 if (error != NULL)
238 return error;
240 if (unveil(NULL, NULL) != 0)
241 return got_error_prefix_errno("unveil");
243 return NULL;
246 __dead static void
247 usage_checkout(void)
249 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
250 "[worktree-path]\n", getprogname());
251 exit(1);
254 static void
255 checkout_progress(void *arg, unsigned char status, const char *path)
257 char *worktree_path = arg;
259 while (path[0] == '/')
260 path++;
262 printf("%c %s/%s\n", status, worktree_path, path);
265 static const struct got_error *
266 check_cancelled(void *arg)
268 if (sigint_received || sigpipe_received)
269 return got_error(GOT_ERR_CANCELLED);
270 return NULL;
273 static const struct got_error *
274 check_linear_ancestry(struct got_worktree *worktree,
275 struct got_object_id *commit_id, struct got_repository *repo)
277 const struct got_error *err = NULL;
278 struct got_object_id *yca_id, *base_commit_id;
280 base_commit_id = got_worktree_get_base_commit_id(worktree);
281 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
282 commit_id, base_commit_id, repo);
283 if (err)
284 return err;
286 if (yca_id == NULL)
287 return got_error(GOT_ERR_ANCESTRY);
289 /*
290 * Require a straight line of history between the target commit
291 * and the work tree's base commit.
293 * Non-linear situation such as the this require a rebase:
295 * (commit) D F (base_commit)
296 * \ /
297 * C E
298 * \ /
299 * B (yca)
300 * |
301 * A
303 * 'got update' only handles linear cases:
304 * Update forwards in time: A (base/yca) - B - C - D (commit)
305 * Update backwards in time: D (base) - C - D - A (commit/yca)
306 */
307 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
308 got_object_id_cmp(base_commit_id, yca_id) != 0)
309 return got_error(GOT_ERR_ANCESTRY);
311 free(yca_id);
312 return NULL;
316 static const struct got_error *
317 cmd_checkout(int argc, char *argv[])
319 const struct got_error *error = NULL;
320 struct got_repository *repo = NULL;
321 struct got_reference *head_ref = NULL;
322 struct got_worktree *worktree = NULL;
323 char *repo_path = NULL;
324 char *worktree_path = NULL;
325 const char *path_prefix = "";
326 char *commit_id_str = NULL;
327 int ch, same_path_prefix;
329 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
330 switch (ch) {
331 case 'c':
332 commit_id_str = strdup(optarg);
333 if (commit_id_str == NULL)
334 return got_error_prefix_errno("strdup");
335 break;
336 case 'p':
337 path_prefix = optarg;
338 break;
339 default:
340 usage_checkout();
341 /* NOTREACHED */
345 argc -= optind;
346 argv += optind;
348 #ifndef PROFILE
349 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
350 "unveil", NULL) == -1)
351 err(1, "pledge");
352 #endif
353 if (argc == 1) {
354 char *cwd, *base, *dotgit;
355 repo_path = realpath(argv[0], NULL);
356 if (repo_path == NULL)
357 return got_error_prefix_errno2("realpath", argv[0]);
358 cwd = getcwd(NULL, 0);
359 if (cwd == NULL) {
360 error = got_error_prefix_errno("getcwd");
361 goto done;
363 if (path_prefix[0]) {
364 base = basename(path_prefix);
365 if (base == NULL) {
366 error = got_error_prefix_errno2("basename",
367 path_prefix);
368 goto done;
370 } else {
371 base = basename(repo_path);
372 if (base == NULL) {
373 error = got_error_prefix_errno2("basename",
374 repo_path);
375 goto done;
378 dotgit = strstr(base, ".git");
379 if (dotgit)
380 *dotgit = '\0';
381 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
382 error = got_error_prefix_errno("asprintf");
383 free(cwd);
384 goto done;
386 free(cwd);
387 } else if (argc == 2) {
388 repo_path = realpath(argv[0], NULL);
389 if (repo_path == NULL) {
390 error = got_error_prefix_errno2("realpath", argv[0]);
391 goto done;
393 worktree_path = realpath(argv[1], NULL);
394 if (worktree_path == NULL) {
395 error = got_error_prefix_errno2("realpath", argv[1]);
396 goto done;
398 } else
399 usage_checkout();
401 got_path_strip_trailing_slashes(repo_path);
402 got_path_strip_trailing_slashes(worktree_path);
404 error = got_repo_open(&repo, repo_path);
405 if (error != NULL)
406 goto done;
408 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
409 if (error)
410 goto done;
412 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
413 if (error != NULL)
414 goto done;
416 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
417 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
418 goto done;
420 error = got_worktree_open(&worktree, worktree_path);
421 if (error != NULL)
422 goto done;
424 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
425 path_prefix);
426 if (error != NULL)
427 goto done;
428 if (!same_path_prefix) {
429 error = got_error(GOT_ERR_PATH_PREFIX);
430 goto done;
433 if (commit_id_str) {
434 struct got_object_id *commit_id;
435 error = got_object_resolve_id_str(&commit_id, repo,
436 commit_id_str);
437 if (error != NULL)
438 goto done;
439 error = check_linear_ancestry(worktree, commit_id, repo);
440 if (error != NULL) {
441 free(commit_id);
442 goto done;
444 error = got_worktree_set_base_commit_id(worktree, repo,
445 commit_id);
446 free(commit_id);
447 if (error)
448 goto done;
451 error = got_worktree_checkout_files(worktree, "", repo,
452 checkout_progress, worktree_path, check_cancelled, NULL);
453 if (error != NULL)
454 goto done;
456 printf("Now shut up and hack\n");
458 done:
459 free(commit_id_str);
460 free(repo_path);
461 free(worktree_path);
462 return error;
465 __dead static void
466 usage_update(void)
468 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
469 getprogname());
470 exit(1);
473 static void
474 update_progress(void *arg, unsigned char status, const char *path)
476 int *did_something = arg;
478 if (status == GOT_STATUS_EXISTS)
479 return;
481 *did_something = 1;
482 while (path[0] == '/')
483 path++;
484 printf("%c %s\n", status, path);
487 static const struct got_error *
488 cmd_update(int argc, char *argv[])
490 const struct got_error *error = NULL;
491 struct got_repository *repo = NULL;
492 struct got_worktree *worktree = NULL;
493 char *worktree_path = NULL, *path = NULL;
494 struct got_object_id *commit_id = NULL;
495 char *commit_id_str = NULL;
496 int ch, did_something = 0;
498 while ((ch = getopt(argc, argv, "c:")) != -1) {
499 switch (ch) {
500 case 'c':
501 commit_id_str = strdup(optarg);
502 if (commit_id_str == NULL)
503 return got_error_prefix_errno("strdup");
504 break;
505 default:
506 usage_update();
507 /* NOTREACHED */
511 argc -= optind;
512 argv += optind;
514 #ifndef PROFILE
515 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
516 "unveil", NULL) == -1)
517 err(1, "pledge");
518 #endif
519 worktree_path = getcwd(NULL, 0);
520 if (worktree_path == NULL) {
521 error = got_error_prefix_errno("getcwd");
522 goto done;
524 error = got_worktree_open(&worktree, worktree_path);
525 if (error)
526 goto done;
528 if (argc == 0) {
529 path = strdup("");
530 if (path == NULL) {
531 error = got_error_prefix_errno("strdup");
532 goto done;
534 } else if (argc == 1) {
535 error = got_worktree_resolve_path(&path, worktree, argv[0]);
536 if (error)
537 goto done;
538 } else
539 usage_update();
541 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
542 if (error != NULL)
543 goto done;
545 error = apply_unveil(got_repo_get_path(repo), 0,
546 got_worktree_get_root_path(worktree), 0);
547 if (error)
548 goto done;
550 if (commit_id_str == NULL) {
551 struct got_reference *head_ref;
552 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
553 if (error != NULL)
554 goto done;
555 error = got_ref_resolve(&commit_id, repo, head_ref);
556 if (error != NULL)
557 goto done;
558 error = got_object_id_str(&commit_id_str, commit_id);
559 if (error != NULL)
560 goto done;
561 } else {
562 error = got_object_resolve_id_str(&commit_id, repo,
563 commit_id_str);
564 if (error != NULL)
565 goto done;
568 error = check_linear_ancestry(worktree, commit_id, repo);
569 if (error != NULL)
570 goto done;
572 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
573 commit_id) != 0) {
574 error = got_worktree_set_base_commit_id(worktree, repo,
575 commit_id);
576 if (error)
577 goto done;
580 error = got_worktree_checkout_files(worktree, path, repo,
581 update_progress, &did_something, check_cancelled, NULL);
582 if (error != NULL)
583 goto done;
585 if (did_something)
586 printf("Updated to commit %s\n", commit_id_str);
587 else
588 printf("Already up-to-date\n");
589 done:
590 free(worktree_path);
591 free(path);
592 free(commit_id);
593 free(commit_id_str);
594 return error;
597 static const struct got_error *
598 print_patch(struct got_commit_object *commit, struct got_object_id *id,
599 int diff_context, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 struct got_tree_object *tree1 = NULL, *tree2;
603 struct got_object_qid *qid;
604 char *id_str1 = NULL, *id_str2;
606 err = got_object_open_as_tree(&tree2, repo,
607 got_object_commit_get_tree_id(commit));
608 if (err)
609 return err;
611 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
612 if (qid != NULL) {
613 struct got_commit_object *pcommit;
615 err = got_object_open_as_commit(&pcommit, repo, qid->id);
616 if (err)
617 return err;
619 err = got_object_open_as_tree(&tree1, repo,
620 got_object_commit_get_tree_id(pcommit));
621 got_object_commit_close(pcommit);
622 if (err)
623 return err;
625 err = got_object_id_str(&id_str1, qid->id);
626 if (err)
627 return err;
630 err = got_object_id_str(&id_str2, id);
631 if (err)
632 goto done;
634 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
635 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
636 done:
637 if (tree1)
638 got_object_tree_close(tree1);
639 got_object_tree_close(tree2);
640 free(id_str1);
641 free(id_str2);
642 return err;
645 static char *
646 get_datestr(time_t *time, char *datebuf)
648 char *p, *s = ctime_r(time, datebuf);
649 p = strchr(s, '\n');
650 if (p)
651 *p = '\0';
652 return s;
655 static const struct got_error *
656 print_commit(struct got_commit_object *commit, struct got_object_id *id,
657 struct got_repository *repo, int show_patch, int diff_context,
658 struct got_reflist_head *refs)
660 const struct got_error *err = NULL;
661 char *id_str, *datestr, *logmsg0, *logmsg, *line;
662 char datebuf[26];
663 time_t committer_time;
664 const char *author, *committer;
665 char *refs_str = NULL;
666 struct got_reflist_entry *re;
668 SIMPLEQ_FOREACH(re, refs, entry) {
669 char *s;
670 const char *name;
671 if (got_object_id_cmp(re->id, id) != 0)
672 continue;
673 name = got_ref_get_name(re->ref);
674 if (strcmp(name, GOT_REF_HEAD) == 0)
675 continue;
676 if (strncmp(name, "refs/", 5) == 0)
677 name += 5;
678 if (strncmp(name, "got/", 4) == 0)
679 continue;
680 if (strncmp(name, "heads/", 6) == 0)
681 name += 6;
682 if (strncmp(name, "remotes/", 8) == 0)
683 name += 8;
684 s = refs_str;
685 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
686 name) == -1) {
687 err = got_error_prefix_errno("asprintf");
688 free(s);
689 break;
691 free(s);
693 err = got_object_id_str(&id_str, id);
694 if (err)
695 return err;
697 printf("-----------------------------------------------\n");
698 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
699 refs_str ? refs_str : "", refs_str ? ")" : "");
700 free(id_str);
701 id_str = NULL;
702 free(refs_str);
703 refs_str = NULL;
704 printf("from: %s\n", got_object_commit_get_author(commit));
705 committer_time = got_object_commit_get_committer_time(commit);
706 datestr = get_datestr(&committer_time, datebuf);
707 printf("date: %s UTC\n", datestr);
708 author = got_object_commit_get_author(commit);
709 committer = got_object_commit_get_committer(commit);
710 if (strcmp(author, committer) != 0)
711 printf("via: %s\n", committer);
712 if (got_object_commit_get_nparents(commit) > 1) {
713 const struct got_object_id_queue *parent_ids;
714 struct got_object_qid *qid;
715 int n = 1;
716 parent_ids = got_object_commit_get_parent_ids(commit);
717 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
718 err = got_object_id_str(&id_str, qid->id);
719 if (err)
720 return err;
721 printf("parent %d: %s\n", n++, id_str);
722 free(id_str);
726 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
727 if (logmsg0 == NULL)
728 return got_error_prefix_errno("strdup");
730 logmsg = logmsg0;
731 do {
732 line = strsep(&logmsg, "\n");
733 if (line)
734 printf(" %s\n", line);
735 } while (line);
736 free(logmsg0);
738 if (show_patch) {
739 err = print_patch(commit, id, diff_context, repo);
740 if (err == 0)
741 printf("\n");
744 if (fflush(stdout) != 0 && err == NULL)
745 err = got_error_prefix_errno("fflush");
746 return err;
749 static const struct got_error *
750 print_commits(struct got_object_id *root_id, struct got_repository *repo,
751 char *path, int show_patch, int diff_context, int limit,
752 int first_parent_traversal, struct got_reflist_head *refs)
754 const struct got_error *err;
755 struct got_commit_graph *graph;
757 err = got_commit_graph_open(&graph, root_id, path,
758 first_parent_traversal, repo);
759 if (err)
760 return err;
761 err = got_commit_graph_iter_start(graph, root_id, repo);
762 if (err)
763 goto done;
764 for (;;) {
765 struct got_commit_object *commit;
766 struct got_object_id *id;
768 if (sigint_received || sigpipe_received)
769 break;
771 err = got_commit_graph_iter_next(&id, graph);
772 if (err) {
773 if (err->code == GOT_ERR_ITER_COMPLETED) {
774 err = NULL;
775 break;
777 if (err->code != GOT_ERR_ITER_NEED_MORE)
778 break;
779 err = got_commit_graph_fetch_commits(graph, 1, repo);
780 if (err)
781 break;
782 else
783 continue;
785 if (id == NULL)
786 break;
788 err = got_object_open_as_commit(&commit, repo, id);
789 if (err)
790 break;
791 err = print_commit(commit, id, repo, show_patch, diff_context,
792 refs);
793 got_object_commit_close(commit);
794 if (err || (limit && --limit == 0))
795 break;
797 done:
798 got_commit_graph_close(graph);
799 return err;
802 __dead static void
803 usage_log(void)
805 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
806 "[-r repository-path] [path]\n", getprogname());
807 exit(1);
810 static const struct got_error *
811 cmd_log(int argc, char *argv[])
813 const struct got_error *error;
814 struct got_repository *repo = NULL;
815 struct got_worktree *worktree = NULL;
816 struct got_commit_object *commit = NULL;
817 struct got_object_id *id = NULL;
818 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
819 char *start_commit = NULL;
820 int diff_context = 3, ch;
821 int show_patch = 0, limit = 0, first_parent_traversal = 0;
822 const char *errstr;
823 struct got_reflist_head refs;
825 SIMPLEQ_INIT(&refs);
827 #ifndef PROFILE
828 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
829 NULL)
830 == -1)
831 err(1, "pledge");
832 #endif
834 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
835 switch (ch) {
836 case 'p':
837 show_patch = 1;
838 break;
839 case 'c':
840 start_commit = optarg;
841 break;
842 case 'C':
843 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
844 &errstr);
845 if (errstr != NULL)
846 err(1, "-C option %s", errstr);
847 break;
848 case 'l':
849 limit = strtonum(optarg, 1, INT_MAX, &errstr);
850 if (errstr != NULL)
851 err(1, "-l option %s", errstr);
852 break;
853 case 'f':
854 first_parent_traversal = 1;
855 break;
856 case 'r':
857 repo_path = realpath(optarg, NULL);
858 if (repo_path == NULL)
859 err(1, "-r option");
860 got_path_strip_trailing_slashes(repo_path);
861 break;
862 default:
863 usage_log();
864 /* NOTREACHED */
868 argc -= optind;
869 argv += optind;
871 cwd = getcwd(NULL, 0);
872 if (cwd == NULL) {
873 error = got_error_prefix_errno("getcwd");
874 goto done;
877 error = got_worktree_open(&worktree, cwd);
878 if (error && error->code != GOT_ERR_NOT_WORKTREE)
879 goto done;
880 error = NULL;
882 if (argc == 0) {
883 path = strdup("");
884 if (path == NULL) {
885 error = got_error_prefix_errno("strdup");
886 goto done;
888 } else if (argc == 1) {
889 if (worktree) {
890 error = got_worktree_resolve_path(&path, worktree,
891 argv[0]);
892 if (error)
893 goto done;
894 } else {
895 path = strdup(argv[0]);
896 if (path == NULL) {
897 error = got_error_prefix_errno("strdup");
898 goto done;
901 } else
902 usage_log();
904 if (repo_path == NULL) {
905 repo_path = worktree ?
906 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
908 if (repo_path == NULL) {
909 error = got_error_prefix_errno("strdup");
910 goto done;
913 error = got_repo_open(&repo, repo_path);
914 if (error != NULL)
915 goto done;
917 error = apply_unveil(got_repo_get_path(repo), 1,
918 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
919 if (error)
920 goto done;
922 if (start_commit == NULL) {
923 struct got_reference *head_ref;
924 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
925 if (error != NULL)
926 return error;
927 error = got_ref_resolve(&id, repo, head_ref);
928 got_ref_close(head_ref);
929 if (error != NULL)
930 return error;
931 error = got_object_open_as_commit(&commit, repo, id);
932 } else {
933 struct got_reference *ref;
934 error = got_ref_open(&ref, repo, start_commit, 0);
935 if (error == NULL) {
936 int obj_type;
937 error = got_ref_resolve(&id, repo, ref);
938 got_ref_close(ref);
939 if (error != NULL)
940 goto done;
941 error = got_object_get_type(&obj_type, repo, id);
942 if (error != NULL)
943 goto done;
944 if (obj_type == GOT_OBJ_TYPE_TAG) {
945 struct got_tag_object *tag;
946 error = got_object_open_as_tag(&tag, repo, id);
947 if (error != NULL)
948 goto done;
949 if (got_object_tag_get_object_type(tag) !=
950 GOT_OBJ_TYPE_COMMIT) {
951 got_object_tag_close(tag);
952 error = got_error(GOT_ERR_OBJ_TYPE);
953 goto done;
955 free(id);
956 id = got_object_id_dup(
957 got_object_tag_get_object_id(tag));
958 if (id == NULL)
959 error = got_error_prefix_errno(
960 "got_object_id_dup");
961 got_object_tag_close(tag);
962 if (error)
963 goto done;
964 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
965 error = got_error(GOT_ERR_OBJ_TYPE);
966 goto done;
968 error = got_object_open_as_commit(&commit, repo, id);
969 if (error != NULL)
970 goto done;
972 if (commit == NULL) {
973 error = got_object_resolve_id_str(&id, repo,
974 start_commit);
975 if (error != NULL)
976 return error;
979 if (error != NULL)
980 goto done;
982 error = got_repo_map_path(&in_repo_path, repo, path, 1);
983 if (error != NULL)
984 goto done;
985 if (in_repo_path) {
986 free(path);
987 path = in_repo_path;
990 error = got_ref_list(&refs, repo);
991 if (error)
992 goto done;
994 error = print_commits(id, repo, path, show_patch,
995 diff_context, limit, first_parent_traversal, &refs);
996 done:
997 free(path);
998 free(repo_path);
999 free(cwd);
1000 free(id);
1001 if (worktree)
1002 got_worktree_close(worktree);
1003 if (repo) {
1004 const struct got_error *repo_error;
1005 repo_error = got_repo_close(repo);
1006 if (error == NULL)
1007 error = repo_error;
1009 got_ref_list_free(&refs);
1010 return error;
1013 __dead static void
1014 usage_diff(void)
1016 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1017 "[object1 object2 | path]\n", getprogname());
1018 exit(1);
1021 struct print_diff_arg {
1022 struct got_repository *repo;
1023 struct got_worktree *worktree;
1024 int diff_context;
1025 const char *id_str;
1026 int header_shown;
1029 static const struct got_error *
1030 print_diff(void *arg, unsigned char status, const char *path,
1031 struct got_object_id *id)
1033 struct print_diff_arg *a = arg;
1034 const struct got_error *err = NULL;
1035 struct got_blob_object *blob1 = NULL;
1036 FILE *f2 = NULL;
1037 char *abspath = NULL;
1038 struct stat sb;
1040 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1041 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1042 return NULL;
1044 if (!a->header_shown) {
1045 printf("diff %s %s\n", a->id_str,
1046 got_worktree_get_root_path(a->worktree));
1047 a->header_shown = 1;
1050 if (status != GOT_STATUS_ADD) {
1051 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1052 if (err)
1053 goto done;
1057 if (status != GOT_STATUS_DELETE) {
1058 if (asprintf(&abspath, "%s/%s",
1059 got_worktree_get_root_path(a->worktree), path) == -1) {
1060 err = got_error_prefix_errno("asprintf");
1061 goto done;
1064 f2 = fopen(abspath, "r");
1065 if (f2 == NULL) {
1066 err = got_error_prefix_errno2("fopen", abspath);
1067 goto done;
1069 if (lstat(abspath, &sb) == -1) {
1070 err = got_error_prefix_errno2("lstat", abspath);
1071 goto done;
1073 } else
1074 sb.st_size = 0;
1076 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1077 stdout);
1078 done:
1079 if (blob1)
1080 got_object_blob_close(blob1);
1081 if (f2 && fclose(f2) != 0 && err == NULL)
1082 err = got_error_prefix_errno("fclose");
1083 free(abspath);
1084 return err;
1087 static const struct got_error *
1088 cmd_diff(int argc, char *argv[])
1090 const struct got_error *error;
1091 struct got_repository *repo = NULL;
1092 struct got_worktree *worktree = NULL;
1093 char *cwd = NULL, *repo_path = NULL;
1094 struct got_object_id *id1 = NULL, *id2 = NULL;
1095 char *id_str1 = NULL, *id_str2 = NULL;
1096 int type1, type2;
1097 int diff_context = 3, ch;
1098 const char *errstr;
1099 char *path = NULL;
1101 #ifndef PROFILE
1102 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1103 NULL) == -1)
1104 err(1, "pledge");
1105 #endif
1107 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1108 switch (ch) {
1109 case 'C':
1110 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1111 if (errstr != NULL)
1112 err(1, "-C option %s", errstr);
1113 break;
1114 case 'r':
1115 repo_path = realpath(optarg, NULL);
1116 if (repo_path == NULL)
1117 err(1, "-r option");
1118 got_path_strip_trailing_slashes(repo_path);
1119 break;
1120 default:
1121 usage_diff();
1122 /* NOTREACHED */
1126 argc -= optind;
1127 argv += optind;
1129 cwd = getcwd(NULL, 0);
1130 if (cwd == NULL) {
1131 error = got_error_prefix_errno("getcwd");
1132 goto done;
1134 error = got_worktree_open(&worktree, cwd);
1135 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1136 goto done;
1137 if (argc <= 1) {
1138 if (worktree == NULL) {
1139 error = got_error(GOT_ERR_NOT_WORKTREE);
1140 goto done;
1142 if (repo_path)
1143 errx(1,
1144 "-r option can't be used when diffing a work tree");
1145 repo_path = strdup(got_worktree_get_repo_path(worktree));
1146 if (repo_path == NULL) {
1147 error = got_error_prefix_errno("strdup");
1148 goto done;
1150 if (argc == 1) {
1151 error = got_worktree_resolve_path(&path, worktree,
1152 argv[0]);
1153 if (error)
1154 goto done;
1155 } else {
1156 path = strdup("");
1157 if (path == NULL) {
1158 error = got_error_prefix_errno("strdup");
1159 goto done;
1162 } else if (argc == 2) {
1163 id_str1 = argv[0];
1164 id_str2 = argv[1];
1165 } else
1166 usage_diff();
1168 if (repo_path == NULL) {
1169 repo_path = getcwd(NULL, 0);
1170 if (repo_path == NULL)
1171 return got_error_prefix_errno("getcwd");
1174 error = got_repo_open(&repo, repo_path);
1175 free(repo_path);
1176 if (error != NULL)
1177 goto done;
1179 error = apply_unveil(got_repo_get_path(repo), 1,
1180 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1181 if (error)
1182 goto done;
1184 if (worktree) {
1185 struct print_diff_arg arg;
1186 char *id_str;
1187 error = got_object_id_str(&id_str,
1188 got_worktree_get_base_commit_id(worktree));
1189 if (error)
1190 goto done;
1191 arg.repo = repo;
1192 arg.worktree = worktree;
1193 arg.diff_context = diff_context;
1194 arg.id_str = id_str;
1195 arg.header_shown = 0;
1197 error = got_worktree_status(worktree, path, repo, print_diff,
1198 &arg, check_cancelled, NULL);
1199 free(id_str);
1200 goto done;
1203 error = got_object_resolve_id_str(&id1, repo, id_str1);
1204 if (error)
1205 goto done;
1207 error = got_object_resolve_id_str(&id2, repo, id_str2);
1208 if (error)
1209 goto done;
1211 error = got_object_get_type(&type1, repo, id1);
1212 if (error)
1213 goto done;
1215 error = got_object_get_type(&type2, repo, id2);
1216 if (error)
1217 goto done;
1219 if (type1 != type2) {
1220 error = got_error(GOT_ERR_OBJ_TYPE);
1221 goto done;
1224 switch (type1) {
1225 case GOT_OBJ_TYPE_BLOB:
1226 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1227 diff_context, repo, stdout);
1228 break;
1229 case GOT_OBJ_TYPE_TREE:
1230 error = got_diff_objects_as_trees(id1, id2, "", "",
1231 diff_context, repo, stdout);
1232 break;
1233 case GOT_OBJ_TYPE_COMMIT:
1234 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1235 id_str2);
1236 error = got_diff_objects_as_commits(id1, id2, diff_context,
1237 repo, stdout);
1238 break;
1239 default:
1240 error = got_error(GOT_ERR_OBJ_TYPE);
1243 done:
1244 free(id1);
1245 free(id2);
1246 free(path);
1247 if (worktree)
1248 got_worktree_close(worktree);
1249 if (repo) {
1250 const struct got_error *repo_error;
1251 repo_error = got_repo_close(repo);
1252 if (error == NULL)
1253 error = repo_error;
1255 return error;
1258 __dead static void
1259 usage_blame(void)
1261 fprintf(stderr,
1262 "usage: %s blame [-c commit] [-r repository-path] path\n",
1263 getprogname());
1264 exit(1);
1267 static const struct got_error *
1268 cmd_blame(int argc, char *argv[])
1270 const struct got_error *error;
1271 struct got_repository *repo = NULL;
1272 struct got_worktree *worktree = NULL;
1273 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1274 struct got_object_id *commit_id = NULL;
1275 char *commit_id_str = NULL;
1276 int ch;
1278 #ifndef PROFILE
1279 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1280 NULL) == -1)
1281 err(1, "pledge");
1282 #endif
1284 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1285 switch (ch) {
1286 case 'c':
1287 commit_id_str = optarg;
1288 break;
1289 case 'r':
1290 repo_path = realpath(optarg, NULL);
1291 if (repo_path == NULL)
1292 err(1, "-r option");
1293 got_path_strip_trailing_slashes(repo_path);
1294 break;
1295 default:
1296 usage_blame();
1297 /* NOTREACHED */
1301 argc -= optind;
1302 argv += optind;
1304 if (argc == 1)
1305 path = argv[0];
1306 else
1307 usage_blame();
1309 cwd = getcwd(NULL, 0);
1310 if (cwd == NULL) {
1311 error = got_error_prefix_errno("getcwd");
1312 goto done;
1314 if (repo_path == NULL) {
1315 error = got_worktree_open(&worktree, cwd);
1316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1317 goto done;
1318 else
1319 error = NULL;
1320 if (worktree) {
1321 repo_path =
1322 strdup(got_worktree_get_repo_path(worktree));
1323 if (repo_path == NULL)
1324 error = got_error_prefix_errno("strdup");
1325 if (error)
1326 goto done;
1327 } else {
1328 repo_path = strdup(cwd);
1329 if (repo_path == NULL) {
1330 error = got_error_prefix_errno("strdup");
1331 goto done;
1336 error = got_repo_open(&repo, repo_path);
1337 if (error != NULL)
1338 goto done;
1340 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1341 if (error)
1342 goto done;
1344 if (worktree) {
1345 const char *prefix = got_worktree_get_path_prefix(worktree);
1346 char *p, *worktree_subdir = cwd +
1347 strlen(got_worktree_get_root_path(worktree));
1348 if (asprintf(&p, "%s%s%s%s%s",
1349 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1350 worktree_subdir, worktree_subdir[0] ? "/" : "",
1351 path) == -1) {
1352 error = got_error_prefix_errno("asprintf");
1353 goto done;
1355 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1356 free(p);
1357 } else {
1358 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1360 if (error)
1361 goto done;
1363 if (commit_id_str == NULL) {
1364 struct got_reference *head_ref;
1365 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1366 if (error != NULL)
1367 goto done;
1368 error = got_ref_resolve(&commit_id, repo, head_ref);
1369 got_ref_close(head_ref);
1370 if (error != NULL)
1371 goto done;
1372 } else {
1373 error = got_object_resolve_id_str(&commit_id, repo,
1374 commit_id_str);
1375 if (error != NULL)
1376 goto done;
1379 error = got_blame(in_repo_path, commit_id, repo, stdout);
1380 done:
1381 free(in_repo_path);
1382 free(repo_path);
1383 free(cwd);
1384 free(commit_id);
1385 if (worktree)
1386 got_worktree_close(worktree);
1387 if (repo) {
1388 const struct got_error *repo_error;
1389 repo_error = got_repo_close(repo);
1390 if (error == NULL)
1391 error = repo_error;
1393 return error;
1396 __dead static void
1397 usage_tree(void)
1399 fprintf(stderr,
1400 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1401 getprogname());
1402 exit(1);
1405 static void
1406 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1407 const char *root_path)
1409 int is_root_path = (strcmp(path, root_path) == 0);
1411 path += strlen(root_path);
1412 while (path[0] == '/')
1413 path++;
1415 printf("%s%s%s%s%s\n", id ? id : "", path,
1416 is_root_path ? "" : "/", te->name,
1417 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1420 static const struct got_error *
1421 print_tree(const char *path, struct got_object_id *commit_id,
1422 int show_ids, int recurse, const char *root_path,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_object_id *tree_id = NULL;
1427 struct got_tree_object *tree = NULL;
1428 const struct got_tree_entries *entries;
1429 struct got_tree_entry *te;
1431 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1432 if (err)
1433 goto done;
1435 err = got_object_open_as_tree(&tree, repo, tree_id);
1436 if (err)
1437 goto done;
1438 entries = got_object_tree_get_entries(tree);
1439 te = SIMPLEQ_FIRST(&entries->head);
1440 while (te) {
1441 char *id = NULL;
1443 if (sigint_received || sigpipe_received)
1444 break;
1446 if (show_ids) {
1447 char *id_str;
1448 err = got_object_id_str(&id_str, te->id);
1449 if (err)
1450 goto done;
1451 if (asprintf(&id, "%s ", id_str) == -1) {
1452 err = got_error_prefix_errno("asprintf");
1453 free(id_str);
1454 goto done;
1456 free(id_str);
1458 print_entry(te, id, path, root_path);
1459 free(id);
1461 if (recurse && S_ISDIR(te->mode)) {
1462 char *child_path;
1463 if (asprintf(&child_path, "%s%s%s", path,
1464 path[0] == '/' && path[1] == '\0' ? "" : "/",
1465 te->name) == -1) {
1466 err = got_error_prefix_errno("asprintf");
1467 goto done;
1469 err = print_tree(child_path, commit_id, show_ids, 1,
1470 root_path, repo);
1471 free(child_path);
1472 if (err)
1473 goto done;
1476 te = SIMPLEQ_NEXT(te, entry);
1478 done:
1479 if (tree)
1480 got_object_tree_close(tree);
1481 free(tree_id);
1482 return err;
1485 static const struct got_error *
1486 cmd_tree(int argc, char *argv[])
1488 const struct got_error *error;
1489 struct got_repository *repo = NULL;
1490 struct got_worktree *worktree = NULL;
1491 const char *path;
1492 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1493 struct got_object_id *commit_id = NULL;
1494 char *commit_id_str = NULL;
1495 int show_ids = 0, recurse = 0;
1496 int ch;
1498 #ifndef PROFILE
1499 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1500 NULL) == -1)
1501 err(1, "pledge");
1502 #endif
1504 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1505 switch (ch) {
1506 case 'c':
1507 commit_id_str = optarg;
1508 break;
1509 case 'r':
1510 repo_path = realpath(optarg, NULL);
1511 if (repo_path == NULL)
1512 err(1, "-r option");
1513 got_path_strip_trailing_slashes(repo_path);
1514 break;
1515 case 'i':
1516 show_ids = 1;
1517 break;
1518 case 'R':
1519 recurse = 1;
1520 break;
1521 default:
1522 usage_tree();
1523 /* NOTREACHED */
1527 argc -= optind;
1528 argv += optind;
1530 if (argc == 1)
1531 path = argv[0];
1532 else if (argc > 1)
1533 usage_tree();
1534 else
1535 path = NULL;
1537 cwd = getcwd(NULL, 0);
1538 if (cwd == NULL) {
1539 error = got_error_prefix_errno("getcwd");
1540 goto done;
1542 if (repo_path == NULL) {
1543 error = got_worktree_open(&worktree, cwd);
1544 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1545 goto done;
1546 else
1547 error = NULL;
1548 if (worktree) {
1549 repo_path =
1550 strdup(got_worktree_get_repo_path(worktree));
1551 if (repo_path == NULL)
1552 error = got_error_prefix_errno("strdup");
1553 if (error)
1554 goto done;
1555 } else {
1556 repo_path = strdup(cwd);
1557 if (repo_path == NULL) {
1558 error = got_error_prefix_errno("strdup");
1559 goto done;
1564 error = got_repo_open(&repo, repo_path);
1565 if (error != NULL)
1566 goto done;
1568 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1569 if (error)
1570 goto done;
1572 if (path == NULL) {
1573 if (worktree) {
1574 char *p, *worktree_subdir = cwd +
1575 strlen(got_worktree_get_root_path(worktree));
1576 if (asprintf(&p, "%s/%s",
1577 got_worktree_get_path_prefix(worktree),
1578 worktree_subdir) == -1) {
1579 error = got_error_prefix_errno("asprintf");
1580 goto done;
1582 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1583 free(p);
1584 if (error)
1585 goto done;
1586 } else
1587 path = "/";
1589 if (in_repo_path == NULL) {
1590 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1591 if (error != NULL)
1592 goto done;
1595 if (commit_id_str == NULL) {
1596 struct got_reference *head_ref;
1597 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1598 if (error != NULL)
1599 goto done;
1600 error = got_ref_resolve(&commit_id, repo, head_ref);
1601 got_ref_close(head_ref);
1602 if (error != NULL)
1603 goto done;
1604 } else {
1605 error = got_object_resolve_id_str(&commit_id, repo,
1606 commit_id_str);
1607 if (error != NULL)
1608 goto done;
1611 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1612 in_repo_path, repo);
1613 done:
1614 free(in_repo_path);
1615 free(repo_path);
1616 free(cwd);
1617 free(commit_id);
1618 if (worktree)
1619 got_worktree_close(worktree);
1620 if (repo) {
1621 const struct got_error *repo_error;
1622 repo_error = got_repo_close(repo);
1623 if (error == NULL)
1624 error = repo_error;
1626 return error;
1629 __dead static void
1630 usage_status(void)
1632 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1633 exit(1);
1636 static const struct got_error *
1637 print_status(void *arg, unsigned char status, const char *path,
1638 struct got_object_id *id)
1640 printf("%c %s\n", status, path);
1641 return NULL;
1644 static const struct got_error *
1645 cmd_status(int argc, char *argv[])
1647 const struct got_error *error = NULL;
1648 struct got_repository *repo = NULL;
1649 struct got_worktree *worktree = NULL;
1650 char *cwd = NULL, *path = NULL;
1651 int ch;
1653 while ((ch = getopt(argc, argv, "")) != -1) {
1654 switch (ch) {
1655 default:
1656 usage_status();
1657 /* NOTREACHED */
1661 argc -= optind;
1662 argv += optind;
1664 #ifndef PROFILE
1665 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1666 NULL) == -1)
1667 err(1, "pledge");
1668 #endif
1669 cwd = getcwd(NULL, 0);
1670 if (cwd == NULL) {
1671 error = got_error_prefix_errno("getcwd");
1672 goto done;
1675 error = got_worktree_open(&worktree, cwd);
1676 if (error != NULL)
1677 goto done;
1679 if (argc == 0) {
1680 path = strdup("");
1681 if (path == NULL) {
1682 error = got_error_prefix_errno("strdup");
1683 goto done;
1685 } else if (argc == 1) {
1686 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1687 if (error)
1688 goto done;
1689 } else
1690 usage_status();
1692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1693 if (error != NULL)
1694 goto done;
1696 error = apply_unveil(got_repo_get_path(repo), 1,
1697 got_worktree_get_root_path(worktree), 0);
1698 if (error)
1699 goto done;
1701 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1702 check_cancelled, NULL);
1703 done:
1704 free(cwd);
1705 free(path);
1706 return error;
1709 __dead static void
1710 usage_ref(void)
1712 fprintf(stderr,
1713 "usage: %s ref [-r repository] -l | -d name | name object\n",
1714 getprogname());
1715 exit(1);
1718 static const struct got_error *
1719 list_refs(struct got_repository *repo)
1721 static const struct got_error *err = NULL;
1722 struct got_reflist_head refs;
1723 struct got_reflist_entry *re;
1725 SIMPLEQ_INIT(&refs);
1726 err = got_ref_list(&refs, repo);
1727 if (err)
1728 return err;
1730 SIMPLEQ_FOREACH(re, &refs, entry) {
1731 char *refstr;
1732 refstr = got_ref_to_str(re->ref);
1733 if (refstr == NULL)
1734 return got_error_prefix_errno("got_ref_to_str");
1735 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1736 free(refstr);
1739 got_ref_list_free(&refs);
1740 return NULL;
1743 static const struct got_error *
1744 delete_ref(struct got_repository *repo, const char *refname)
1746 const struct got_error *err = NULL;
1747 struct got_reference *ref;
1749 err = got_ref_open(&ref, repo, refname, 0);
1750 if (err)
1751 return err;
1753 err = got_ref_delete(ref, repo);
1754 got_ref_close(ref);
1755 return err;
1758 static const struct got_error *
1759 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1761 const struct got_error *err = NULL;
1762 struct got_object_id *id;
1763 struct got_reference *ref = NULL;
1765 err = got_object_resolve_id_str(&id, repo, id_str);
1766 if (err)
1767 return err;
1769 err = got_ref_alloc(&ref, refname, id);
1770 if (err)
1771 goto done;
1773 err = got_ref_write(ref, repo);
1774 done:
1775 if (ref)
1776 got_ref_close(ref);
1777 free(id);
1778 return err;
1781 static const struct got_error *
1782 cmd_ref(int argc, char *argv[])
1784 const struct got_error *error = NULL;
1785 struct got_repository *repo = NULL;
1786 struct got_worktree *worktree = NULL;
1787 char *cwd = NULL, *repo_path = NULL;
1788 int ch, do_list = 0;
1789 const char *delref = NULL;
1791 /* TODO: Add -s option for adding symbolic references. */
1792 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1793 switch (ch) {
1794 case 'd':
1795 delref = optarg;
1796 break;
1797 case 'r':
1798 repo_path = realpath(optarg, NULL);
1799 if (repo_path == NULL)
1800 err(1, "-r option");
1801 got_path_strip_trailing_slashes(repo_path);
1802 break;
1803 case 'l':
1804 do_list = 1;
1805 break;
1806 default:
1807 usage_ref();
1808 /* NOTREACHED */
1812 if (do_list && delref)
1813 errx(1, "-l and -d options are mutually exclusive\n");
1815 argc -= optind;
1816 argv += optind;
1818 if (do_list || delref) {
1819 if (argc > 0)
1820 usage_ref();
1821 } else if (argc != 2)
1822 usage_ref();
1824 #ifndef PROFILE
1825 if (do_list) {
1826 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1827 NULL) == -1)
1828 err(1, "pledge");
1829 } else {
1830 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1831 "sendfd unveil", NULL) == -1)
1832 err(1, "pledge");
1834 #endif
1835 cwd = getcwd(NULL, 0);
1836 if (cwd == NULL) {
1837 error = got_error_prefix_errno("getcwd");
1838 goto done;
1841 if (repo_path == NULL) {
1842 error = got_worktree_open(&worktree, cwd);
1843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1844 goto done;
1845 else
1846 error = NULL;
1847 if (worktree) {
1848 repo_path =
1849 strdup(got_worktree_get_repo_path(worktree));
1850 if (repo_path == NULL)
1851 error = got_error_prefix_errno("strdup");
1852 if (error)
1853 goto done;
1854 } else {
1855 repo_path = strdup(cwd);
1856 if (repo_path == NULL) {
1857 error = got_error_prefix_errno("strdup");
1858 goto done;
1863 error = got_repo_open(&repo, repo_path);
1864 if (error != NULL)
1865 goto done;
1867 error = apply_unveil(got_repo_get_path(repo), do_list,
1868 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1869 if (error)
1870 goto done;
1872 if (do_list)
1873 error = list_refs(repo);
1874 else if (delref)
1875 error = delete_ref(repo, delref);
1876 else
1877 error = add_ref(repo, argv[0], argv[1]);
1878 done:
1879 if (repo)
1880 got_repo_close(repo);
1881 if (worktree)
1882 got_worktree_close(worktree);
1883 free(cwd);
1884 free(repo_path);
1885 return error;
1888 __dead static void
1889 usage_add(void)
1891 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1892 exit(1);
1895 static const struct got_error *
1896 cmd_add(int argc, char *argv[])
1898 const struct got_error *error = NULL;
1899 struct got_repository *repo = NULL;
1900 struct got_worktree *worktree = NULL;
1901 char *cwd = NULL;
1902 struct got_pathlist_head paths;
1903 struct got_pathlist_entry *pe;
1904 int ch, x;
1906 TAILQ_INIT(&paths);
1908 while ((ch = getopt(argc, argv, "")) != -1) {
1909 switch (ch) {
1910 default:
1911 usage_add();
1912 /* NOTREACHED */
1916 argc -= optind;
1917 argv += optind;
1919 if (argc < 1)
1920 usage_add();
1922 /* make sure each file exists before doing anything halfway */
1923 for (x = 0; x < argc; x++) {
1924 char *path = realpath(argv[x], NULL);
1925 if (path == NULL) {
1926 error = got_error_prefix_errno2("realpath", argv[x]);
1927 goto done;
1929 free(path);
1932 cwd = getcwd(NULL, 0);
1933 if (cwd == NULL) {
1934 error = got_error_prefix_errno("getcwd");
1935 goto done;
1938 error = got_worktree_open(&worktree, cwd);
1939 if (error)
1940 goto done;
1942 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1943 if (error != NULL)
1944 goto done;
1946 error = apply_unveil(got_repo_get_path(repo), 1,
1947 got_worktree_get_root_path(worktree), 0);
1948 if (error)
1949 goto done;
1951 for (x = 0; x < argc; x++) {
1952 char *path = realpath(argv[x], NULL);
1953 if (path == NULL) {
1954 error = got_error_prefix_errno2("realpath", argv[x]);
1955 goto done;
1958 got_path_strip_trailing_slashes(path);
1959 error = got_pathlist_insert(&pe, &paths, path, NULL);
1960 if (error) {
1961 free(path);
1962 goto done;
1965 error = got_worktree_schedule_add(worktree, &paths, print_status,
1966 NULL, repo);
1967 done:
1968 if (repo)
1969 got_repo_close(repo);
1970 if (worktree)
1971 got_worktree_close(worktree);
1972 TAILQ_FOREACH(pe, &paths, entry)
1973 free((char *)pe->path);
1974 got_pathlist_free(&paths);
1975 free(cwd);
1976 return error;
1979 __dead static void
1980 usage_rm(void)
1982 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1983 exit(1);
1986 static const struct got_error *
1987 cmd_rm(int argc, char *argv[])
1989 const struct got_error *error = NULL;
1990 struct got_worktree *worktree = NULL;
1991 struct got_repository *repo = NULL;
1992 char *cwd = NULL, *path = NULL;
1993 int ch, delete_local_mods = 0;
1995 while ((ch = getopt(argc, argv, "f")) != -1) {
1996 switch (ch) {
1997 case 'f':
1998 delete_local_mods = 1;
1999 break;
2000 default:
2001 usage_add();
2002 /* NOTREACHED */
2006 argc -= optind;
2007 argv += optind;
2009 if (argc != 1)
2010 usage_rm();
2012 path = realpath(argv[0], NULL);
2013 if (path == NULL) {
2014 error = got_error_prefix_errno2("realpath", argv[0]);
2015 goto done;
2017 got_path_strip_trailing_slashes(path);
2019 cwd = getcwd(NULL, 0);
2020 if (cwd == NULL) {
2021 error = got_error_prefix_errno("getcwd");
2022 goto done;
2024 error = got_worktree_open(&worktree, cwd);
2025 if (error)
2026 goto done;
2028 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2029 if (error)
2030 goto done;
2032 error = apply_unveil(got_repo_get_path(repo), 1,
2033 got_worktree_get_root_path(worktree), 0);
2034 if (error)
2035 goto done;
2037 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2038 print_status, NULL, repo);
2039 if (error)
2040 goto done;
2041 done:
2042 if (repo)
2043 got_repo_close(repo);
2044 if (worktree)
2045 got_worktree_close(worktree);
2046 free(path);
2047 free(cwd);
2048 return error;
2051 __dead static void
2052 usage_revert(void)
2054 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2055 exit(1);
2058 static void
2059 revert_progress(void *arg, unsigned char status, const char *path)
2061 while (path[0] == '/')
2062 path++;
2063 printf("%c %s\n", status, path);
2066 static const struct got_error *
2067 cmd_revert(int argc, char *argv[])
2069 const struct got_error *error = NULL;
2070 struct got_worktree *worktree = NULL;
2071 struct got_repository *repo = NULL;
2072 char *cwd = NULL, *path = NULL;
2073 int ch;
2075 while ((ch = getopt(argc, argv, "")) != -1) {
2076 switch (ch) {
2077 default:
2078 usage_revert();
2079 /* NOTREACHED */
2083 argc -= optind;
2084 argv += optind;
2086 if (argc != 1)
2087 usage_revert();
2089 path = realpath(argv[0], NULL);
2090 if (path == NULL) {
2091 error = got_error_prefix_errno2("realpath", argv[0]);
2092 goto done;
2094 got_path_strip_trailing_slashes(path);
2096 cwd = getcwd(NULL, 0);
2097 if (cwd == NULL) {
2098 error = got_error_prefix_errno("getcwd");
2099 goto done;
2101 error = got_worktree_open(&worktree, cwd);
2102 if (error)
2103 goto done;
2105 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2106 if (error != NULL)
2107 goto done;
2109 error = apply_unveil(got_repo_get_path(repo), 1,
2110 got_worktree_get_root_path(worktree), 0);
2111 if (error)
2112 goto done;
2114 error = got_worktree_revert(worktree, path,
2115 revert_progress, NULL, repo);
2116 if (error)
2117 goto done;
2118 done:
2119 if (repo)
2120 got_repo_close(repo);
2121 if (worktree)
2122 got_worktree_close(worktree);
2123 free(path);
2124 free(cwd);
2125 return error;
2128 __dead static void
2129 usage_commit(void)
2131 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2132 exit(1);
2135 int
2136 spawn_editor(const char *file)
2138 char *argp[] = { "sh", "-c", NULL, NULL };
2139 char *editor, *editp;
2140 pid_t pid;
2141 sig_t sighup, sigint, sigquit;
2142 int st = -1;
2144 editor = getenv("VISUAL");
2145 if (editor == NULL)
2146 editor = getenv("EDITOR");
2147 if (editor == NULL)
2148 editor = "ed";
2150 if (asprintf(&editp, "%s %s", editor, file) == -1)
2151 return -1;
2153 argp[2] = editp;
2155 sighup = signal(SIGHUP, SIG_IGN);
2156 sigint = signal(SIGINT, SIG_IGN);
2157 sigquit = signal(SIGQUIT, SIG_IGN);
2159 switch (pid = fork()) {
2160 case -1:
2161 goto doneediting;
2162 case 0:
2163 execv(_PATH_BSHELL, argp);
2164 _exit(127);
2167 free(editp);
2169 while (waitpid(pid, &st, 0) == -1)
2170 if (errno != EINTR)
2171 break;
2173 doneediting:
2174 (void)signal(SIGHUP, sighup);
2175 (void)signal(SIGINT, sigint);
2176 (void)signal(SIGQUIT, sigquit);
2178 if (!WIFEXITED(st)) {
2179 errno = EINTR;
2180 return -1;
2183 return WEXITSTATUS(st);
2186 static const struct got_error *
2187 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2188 void *arg)
2190 struct got_pathlist_entry *pe;
2191 const struct got_error *err = NULL;
2192 char *tmpfile = NULL;
2193 char *cmdline_log = (char *)arg;
2194 char buf[1024];
2195 struct stat st, st2;
2196 FILE *fp;
2197 size_t len;
2198 int fd;
2200 /* if a message was specified on the command line, just use it */
2201 if (cmdline_log != NULL && strlen(cmdline_log) != 0) {
2202 len = strlen(cmdline_log) + 1;
2203 *logmsg = malloc(len + 1);
2204 if (*logmsg == NULL)
2205 return got_error_prefix_errno("malloc");
2206 strlcpy(*logmsg, cmdline_log, len);
2207 return NULL;
2210 err = got_opentemp_named_fd(&tmpfile, &fd, "got-XXXXXXXXXX");
2211 if (err)
2212 return err;
2214 dprintf(fd, "\n"
2215 "# changes to be committed:\n");
2217 TAILQ_FOREACH(pe, commitable_paths, entry) {
2218 struct got_commitable *ct = pe->data;
2219 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2221 close(fd);
2223 if (stat(tmpfile, &st) == -1) {
2224 err = got_error_prefix_errno2("stat", tmpfile);
2225 goto done;
2228 if (spawn_editor(tmpfile) == -1) {
2229 err = got_error_prefix_errno("failed spawning editor");
2230 goto done;
2233 if (stat(tmpfile, &st2) == -1) {
2234 err = got_error_prefix_errno("stat");
2235 goto done;
2238 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2239 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2240 "no changes made to commit message, aborting");
2241 goto done;
2244 /* remove comments */
2245 *logmsg = malloc(st2.st_size + 1);
2246 if (*logmsg == NULL) {
2247 err = got_error_prefix_errno("malloc");
2248 goto done;
2250 len = 0;
2252 fp = fopen(tmpfile, "r");
2253 while (fgets(buf, sizeof(buf), fp) != NULL) {
2254 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2255 continue;
2256 len = strlcat(*logmsg, buf, st2.st_size);
2258 fclose(fp);
2260 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2261 (*logmsg)[len - 1] = '\0';
2262 len--;
2265 if (len == 0) {
2266 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2267 "commit message cannot be empty, aborting");
2268 goto done;
2270 done:
2271 if (tmpfile) {
2272 unlink(tmpfile);
2273 free(tmpfile);
2276 return err;
2279 static const struct got_error *
2280 cmd_commit(int argc, char *argv[])
2282 const struct got_error *error = NULL;
2283 struct got_worktree *worktree = NULL;
2284 struct got_repository *repo = NULL;
2285 char *cwd = NULL, *path = NULL, *id_str = NULL;
2286 struct got_object_id *id = NULL;
2287 const char *logmsg = NULL;
2288 const char *got_author = getenv("GOT_AUTHOR");
2289 int ch;
2291 while ((ch = getopt(argc, argv, "m:")) != -1) {
2292 switch (ch) {
2293 case 'm':
2294 logmsg = optarg;
2295 break;
2296 default:
2297 usage_commit();
2298 /* NOTREACHED */
2302 argc -= optind;
2303 argv += optind;
2305 if (argc == 1) {
2306 path = realpath(argv[0], NULL);
2307 if (path == NULL) {
2308 error = got_error_prefix_errno2("realpath", argv[0]);
2309 goto done;
2311 got_path_strip_trailing_slashes(path);
2312 } else if (argc != 0)
2313 usage_commit();
2315 if (got_author == NULL) {
2316 /* TODO: Look current user up in password database */
2317 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2318 goto done;
2321 cwd = getcwd(NULL, 0);
2322 if (cwd == NULL) {
2323 error = got_error_prefix_errno("getcwd");
2324 goto done;
2326 error = got_worktree_open(&worktree, cwd);
2327 if (error)
2328 goto done;
2330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2331 if (error != NULL)
2332 goto done;
2334 #if 0
2335 error = apply_unveil(got_repo_get_path(repo), 0,
2336 got_worktree_get_root_path(worktree), 0);
2337 if (error)
2338 goto done;
2339 #endif
2341 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2342 collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2343 if (error)
2344 goto done;
2346 error = got_object_id_str(&id_str, id);
2347 if (error)
2348 goto done;
2349 printf("created commit %s\n", id_str);
2350 done:
2351 if (repo)
2352 got_repo_close(repo);
2353 if (worktree)
2354 got_worktree_close(worktree);
2355 free(path);
2356 free(cwd);
2357 free(id_str);
2358 return error;