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"
48 #ifndef nitems
49 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
50 #endif
52 static volatile sig_atomic_t sigint_received;
53 static volatile sig_atomic_t sigpipe_received;
55 static void
56 catch_sigint(int signo)
57 {
58 sigint_received = 1;
59 }
61 static void
62 catch_sigpipe(int signo)
63 {
64 sigpipe_received = 1;
65 }
68 struct cmd {
69 const char *cmd_name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 const char *cmd_descr;
73 };
75 __dead static void usage(void);
76 __dead static void usage_checkout(void);
77 __dead static void usage_update(void);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
82 __dead static void usage_status(void);
83 __dead static void usage_ref(void);
84 __dead static void usage_add(void);
85 __dead static void usage_rm(void);
86 __dead static void usage_revert(void);
87 __dead static void usage_commit(void);
89 static const struct got_error* cmd_checkout(int, char *[]);
90 static const struct got_error* cmd_update(int, char *[]);
91 static const struct got_error* cmd_log(int, char *[]);
92 static const struct got_error* cmd_diff(int, char *[]);
93 static const struct got_error* cmd_blame(int, char *[]);
94 static const struct got_error* cmd_tree(int, char *[]);
95 static const struct got_error* cmd_status(int, char *[]);
96 static const struct got_error* cmd_ref(int, char *[]);
97 static const struct got_error* cmd_add(int, char *[]);
98 static const struct got_error* cmd_rm(int, char *[]);
99 static const struct got_error* cmd_revert(int, char *[]);
100 static const struct got_error* cmd_commit(int, char *[]);
102 static struct cmd got_commands[] = {
103 { "checkout", cmd_checkout, usage_checkout,
104 "check out a new work tree from a repository" },
105 { "update", cmd_update, usage_update,
106 "update a work tree to a different commit" },
107 { "log", cmd_log, usage_log,
108 "show repository history" },
109 { "diff", cmd_diff, usage_diff,
110 "compare files and directories" },
111 { "blame", cmd_blame, usage_blame,
112 "show when lines in a file were changed" },
113 { "tree", cmd_tree, usage_tree,
114 "list files and directories in repository" },
115 { "status", cmd_status, usage_status,
116 "show modification status of files" },
117 { "ref", cmd_ref, usage_ref,
118 "manage references in repository" },
119 { "add", cmd_add, usage_add,
120 "add new files to version control" },
121 { "rm", cmd_rm, usage_rm,
122 "remove a versioned file" },
123 { "revert", cmd_revert, usage_revert,
124 "revert uncommitted changes" },
125 { "commit", cmd_commit, usage_commit,
126 "write changes from work tree to repository" },
127 };
129 int
130 main(int argc, char *argv[])
132 struct cmd *cmd;
133 unsigned int i;
134 int ch;
135 int hflag = 0;
137 setlocale(LC_CTYPE, "");
139 while ((ch = getopt(argc, argv, "h")) != -1) {
140 switch (ch) {
141 case 'h':
142 hflag = 1;
143 break;
144 default:
145 usage();
146 /* NOTREACHED */
150 argc -= optind;
151 argv += optind;
152 optind = 0;
154 if (argc <= 0)
155 usage();
157 signal(SIGINT, catch_sigint);
158 signal(SIGPIPE, catch_sigpipe);
160 for (i = 0; i < nitems(got_commands); i++) {
161 const struct got_error *error;
163 cmd = &got_commands[i];
165 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
166 continue;
168 if (hflag)
169 got_commands[i].cmd_usage();
171 error = got_commands[i].cmd_main(argc, argv);
172 if (error && !(sigint_received || sigpipe_received)) {
173 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
174 return 1;
177 return 0;
180 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
181 return 1;
184 __dead static void
185 usage(void)
187 int i;
189 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
190 "Available commands:\n", getprogname());
191 for (i = 0; i < nitems(got_commands); i++) {
192 struct cmd *cmd = &got_commands[i];
193 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
195 exit(1);
198 static const struct got_error *
199 apply_unveil(const char *repo_path, int repo_read_only,
200 const char *worktree_path, int create_worktree)
202 const struct got_error *error;
203 static char err_msg[MAXPATHLEN + 36];
205 if (create_worktree) {
206 /* Pre-create work tree path to avoid unveiling its parents. */
207 error = got_path_mkdir(worktree_path);
209 if (errno == EEXIST) {
210 if (got_path_dir_is_empty(worktree_path)) {
211 errno = 0;
212 error = NULL;
213 } else {
214 snprintf(err_msg, sizeof(err_msg),
215 "%s: directory exists but is not empty",
216 worktree_path);
217 error = got_error_msg(GOT_ERR_BAD_PATH,
218 err_msg);
222 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
223 return error;
226 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
227 return got_error_prefix_errno2("unveil", repo_path);
229 if (worktree_path && unveil(worktree_path, "rwc") != 0)
230 return got_error_prefix_errno2("unveil", worktree_path);
232 if (unveil("/tmp", "rwc") != 0)
233 return got_error_prefix_errno2("unveil", "/tmp");
235 error = got_privsep_unveil_exec_helpers();
236 if (error != NULL)
237 return error;
239 if (unveil(NULL, NULL) != 0)
240 return got_error_prefix_errno("unveil");
242 return NULL;
245 __dead static void
246 usage_checkout(void)
248 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
249 "[worktree-path]\n", getprogname());
250 exit(1);
253 static void
254 checkout_progress(void *arg, unsigned char status, const char *path)
256 char *worktree_path = arg;
258 while (path[0] == '/')
259 path++;
261 printf("%c %s/%s\n", status, worktree_path, path);
264 static const struct got_error *
265 check_cancelled(void *arg)
267 if (sigint_received || sigpipe_received)
268 return got_error(GOT_ERR_CANCELLED);
269 return NULL;
272 static const struct got_error *
273 check_linear_ancestry(struct got_worktree *worktree,
274 struct got_object_id *commit_id, struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_object_id *yca_id, *base_commit_id;
279 base_commit_id = got_worktree_get_base_commit_id(worktree);
280 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
281 commit_id, base_commit_id, repo);
282 if (err)
283 return err;
285 if (yca_id == NULL)
286 return got_error(GOT_ERR_ANCESTRY);
288 /*
289 * Require a straight line of history between the target commit
290 * and the work tree's base commit.
292 * Non-linear situation such as the this require a rebase:
294 * (commit) D F (base_commit)
295 * \ /
296 * C E
297 * \ /
298 * B (yca)
299 * |
300 * A
302 * 'got update' only handles linear cases:
303 * Update forwards in time: A (base/yca) - B - C - D (commit)
304 * Update backwards in time: D (base) - C - D - A (commit/yca)
305 */
306 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
307 got_object_id_cmp(base_commit_id, yca_id) != 0)
308 return got_error(GOT_ERR_ANCESTRY);
310 free(yca_id);
311 return NULL;
315 static const struct got_error *
316 cmd_checkout(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 struct got_repository *repo = NULL;
320 struct got_reference *head_ref = NULL;
321 struct got_worktree *worktree = NULL;
322 char *repo_path = NULL;
323 char *worktree_path = NULL;
324 const char *path_prefix = "";
325 char *commit_id_str = NULL;
326 int ch, same_path_prefix;
328 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
329 switch (ch) {
330 case 'c':
331 commit_id_str = strdup(optarg);
332 if (commit_id_str == NULL)
333 return got_error_prefix_errno("strdup");
334 break;
335 case 'p':
336 path_prefix = optarg;
337 break;
338 default:
339 usage_checkout();
340 /* NOTREACHED */
344 argc -= optind;
345 argv += optind;
347 #ifndef PROFILE
348 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
349 "unveil", NULL) == -1)
350 err(1, "pledge");
351 #endif
352 if (argc == 1) {
353 char *cwd, *base, *dotgit;
354 repo_path = realpath(argv[0], NULL);
355 if (repo_path == NULL)
356 return got_error_prefix_errno2("realpath", argv[0]);
357 cwd = getcwd(NULL, 0);
358 if (cwd == NULL) {
359 error = got_error_prefix_errno("getcwd");
360 goto done;
362 if (path_prefix[0]) {
363 base = basename(path_prefix);
364 if (base == NULL) {
365 error = got_error_prefix_errno2("basename",
366 path_prefix);
367 goto done;
369 } else {
370 base = basename(repo_path);
371 if (base == NULL) {
372 error = got_error_prefix_errno2("basename",
373 repo_path);
374 goto done;
377 dotgit = strstr(base, ".git");
378 if (dotgit)
379 *dotgit = '\0';
380 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
381 error = got_error_prefix_errno("asprintf");
382 free(cwd);
383 goto done;
385 free(cwd);
386 } else if (argc == 2) {
387 repo_path = realpath(argv[0], NULL);
388 if (repo_path == NULL) {
389 error = got_error_prefix_errno2("realpath", argv[0]);
390 goto done;
392 worktree_path = realpath(argv[1], NULL);
393 if (worktree_path == NULL) {
394 error = got_error_prefix_errno2("realpath", argv[1]);
395 goto done;
397 } else
398 usage_checkout();
400 got_path_strip_trailing_slashes(repo_path);
401 got_path_strip_trailing_slashes(worktree_path);
403 error = got_repo_open(&repo, repo_path);
404 if (error != NULL)
405 goto done;
407 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
408 if (error)
409 goto done;
411 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
412 if (error != NULL)
413 goto done;
415 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
416 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
417 goto done;
419 error = got_worktree_open(&worktree, worktree_path);
420 if (error != NULL)
421 goto done;
423 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
424 path_prefix);
425 if (error != NULL)
426 goto done;
427 if (!same_path_prefix) {
428 error = got_error(GOT_ERR_PATH_PREFIX);
429 goto done;
432 if (commit_id_str) {
433 struct got_object_id *commit_id;
434 error = got_object_resolve_id_str(&commit_id, repo,
435 commit_id_str);
436 if (error != NULL)
437 goto done;
438 error = check_linear_ancestry(worktree, commit_id, repo);
439 if (error != NULL) {
440 free(commit_id);
441 goto done;
443 error = got_worktree_set_base_commit_id(worktree, repo,
444 commit_id);
445 free(commit_id);
446 if (error)
447 goto done;
450 error = got_worktree_checkout_files(worktree, "", repo,
451 checkout_progress, worktree_path, check_cancelled, NULL);
452 if (error != NULL)
453 goto done;
455 printf("Now shut up and hack\n");
457 done:
458 free(commit_id_str);
459 free(repo_path);
460 free(worktree_path);
461 return error;
464 __dead static void
465 usage_update(void)
467 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
468 getprogname());
469 exit(1);
472 static void
473 update_progress(void *arg, unsigned char status, const char *path)
475 int *did_something = arg;
477 if (status == GOT_STATUS_EXISTS)
478 return;
480 *did_something = 1;
481 while (path[0] == '/')
482 path++;
483 printf("%c %s\n", status, path);
486 static const struct got_error *
487 cmd_update(int argc, char *argv[])
489 const struct got_error *error = NULL;
490 struct got_repository *repo = NULL;
491 struct got_worktree *worktree = NULL;
492 char *worktree_path = NULL, *path = NULL;
493 struct got_object_id *commit_id = NULL;
494 char *commit_id_str = NULL;
495 int ch, did_something = 0;
497 while ((ch = getopt(argc, argv, "c:")) != -1) {
498 switch (ch) {
499 case 'c':
500 commit_id_str = strdup(optarg);
501 if (commit_id_str == NULL)
502 return got_error_prefix_errno("strdup");
503 break;
504 default:
505 usage_update();
506 /* NOTREACHED */
510 argc -= optind;
511 argv += optind;
513 #ifndef PROFILE
514 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
515 "unveil", NULL) == -1)
516 err(1, "pledge");
517 #endif
518 worktree_path = getcwd(NULL, 0);
519 if (worktree_path == NULL) {
520 error = got_error_prefix_errno("getcwd");
521 goto done;
523 error = got_worktree_open(&worktree, worktree_path);
524 if (error)
525 goto done;
527 if (argc == 0) {
528 path = strdup("");
529 if (path == NULL) {
530 error = got_error_prefix_errno("strdup");
531 goto done;
533 } else if (argc == 1) {
534 error = got_worktree_resolve_path(&path, worktree, argv[0]);
535 if (error)
536 goto done;
537 } else
538 usage_update();
540 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
541 if (error != NULL)
542 goto done;
544 error = apply_unveil(got_repo_get_path(repo), 0,
545 got_worktree_get_root_path(worktree), 0);
546 if (error)
547 goto done;
549 if (commit_id_str == NULL) {
550 struct got_reference *head_ref;
551 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
552 if (error != NULL)
553 goto done;
554 error = got_ref_resolve(&commit_id, repo, head_ref);
555 if (error != NULL)
556 goto done;
557 error = got_object_id_str(&commit_id_str, commit_id);
558 if (error != NULL)
559 goto done;
560 } else {
561 error = got_object_resolve_id_str(&commit_id, repo,
562 commit_id_str);
563 if (error != NULL)
564 goto done;
567 error = check_linear_ancestry(worktree, commit_id, repo);
568 if (error != NULL)
569 goto done;
571 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
572 commit_id) != 0) {
573 error = got_worktree_set_base_commit_id(worktree, repo,
574 commit_id);
575 if (error)
576 goto done;
579 error = got_worktree_checkout_files(worktree, path, repo,
580 update_progress, &did_something, check_cancelled, NULL);
581 if (error != NULL)
582 goto done;
584 if (did_something)
585 printf("Updated to commit %s\n", commit_id_str);
586 else
587 printf("Already up-to-date\n");
588 done:
589 free(worktree_path);
590 free(path);
591 free(commit_id);
592 free(commit_id_str);
593 return error;
596 static const struct got_error *
597 print_patch(struct got_commit_object *commit, struct got_object_id *id,
598 int diff_context, struct got_repository *repo)
600 const struct got_error *err = NULL;
601 struct got_tree_object *tree1 = NULL, *tree2;
602 struct got_object_qid *qid;
603 char *id_str1 = NULL, *id_str2;
605 err = got_object_open_as_tree(&tree2, repo,
606 got_object_commit_get_tree_id(commit));
607 if (err)
608 return err;
610 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
611 if (qid != NULL) {
612 struct got_commit_object *pcommit;
614 err = got_object_open_as_commit(&pcommit, repo, qid->id);
615 if (err)
616 return err;
618 err = got_object_open_as_tree(&tree1, repo,
619 got_object_commit_get_tree_id(pcommit));
620 got_object_commit_close(pcommit);
621 if (err)
622 return err;
624 err = got_object_id_str(&id_str1, qid->id);
625 if (err)
626 return err;
629 err = got_object_id_str(&id_str2, id);
630 if (err)
631 goto done;
633 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
634 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
635 done:
636 if (tree1)
637 got_object_tree_close(tree1);
638 got_object_tree_close(tree2);
639 free(id_str1);
640 free(id_str2);
641 return err;
644 static char *
645 get_datestr(time_t *time, char *datebuf)
647 char *p, *s = ctime_r(time, datebuf);
648 p = strchr(s, '\n');
649 if (p)
650 *p = '\0';
651 return s;
654 static const struct got_error *
655 print_commit(struct got_commit_object *commit, struct got_object_id *id,
656 struct got_repository *repo, int show_patch, int diff_context,
657 struct got_reflist_head *refs)
659 const struct got_error *err = NULL;
660 char *id_str, *datestr, *logmsg0, *logmsg, *line;
661 char datebuf[26];
662 time_t committer_time;
663 const char *author, *committer;
664 char *refs_str = NULL;
665 struct got_reflist_entry *re;
667 SIMPLEQ_FOREACH(re, refs, entry) {
668 char *s;
669 const char *name;
670 if (got_object_id_cmp(re->id, id) != 0)
671 continue;
672 name = got_ref_get_name(re->ref);
673 if (strcmp(name, GOT_REF_HEAD) == 0)
674 continue;
675 if (strncmp(name, "refs/", 5) == 0)
676 name += 5;
677 if (strncmp(name, "got/", 4) == 0)
678 continue;
679 if (strncmp(name, "heads/", 6) == 0)
680 name += 6;
681 if (strncmp(name, "remotes/", 8) == 0)
682 name += 8;
683 s = refs_str;
684 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
685 name) == -1) {
686 err = got_error_prefix_errno("asprintf");
687 free(s);
688 break;
690 free(s);
692 err = got_object_id_str(&id_str, id);
693 if (err)
694 return err;
696 printf("-----------------------------------------------\n");
697 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
698 refs_str ? refs_str : "", refs_str ? ")" : "");
699 free(id_str);
700 id_str = NULL;
701 free(refs_str);
702 refs_str = NULL;
703 printf("from: %s\n", got_object_commit_get_author(commit));
704 committer_time = got_object_commit_get_committer_time(commit);
705 datestr = get_datestr(&committer_time, datebuf);
706 printf("date: %s UTC\n", datestr);
707 author = got_object_commit_get_author(commit);
708 committer = got_object_commit_get_committer(commit);
709 if (strcmp(author, committer) != 0)
710 printf("via: %s\n", committer);
711 if (got_object_commit_get_nparents(commit) > 1) {
712 const struct got_object_id_queue *parent_ids;
713 struct got_object_qid *qid;
714 int n = 1;
715 parent_ids = got_object_commit_get_parent_ids(commit);
716 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
717 err = got_object_id_str(&id_str, qid->id);
718 if (err)
719 return err;
720 printf("parent %d: %s\n", n++, id_str);
721 free(id_str);
725 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
726 if (logmsg0 == NULL)
727 return got_error_prefix_errno("strdup");
729 logmsg = logmsg0;
730 do {
731 line = strsep(&logmsg, "\n");
732 if (line)
733 printf(" %s\n", line);
734 } while (line);
735 free(logmsg0);
737 if (show_patch) {
738 err = print_patch(commit, id, diff_context, repo);
739 if (err == 0)
740 printf("\n");
743 if (fflush(stdout) != 0 && err == NULL)
744 err = got_error_prefix_errno("fflush");
745 return err;
748 static const struct got_error *
749 print_commits(struct got_object_id *root_id, struct got_repository *repo,
750 char *path, int show_patch, int diff_context, int limit,
751 int first_parent_traversal, struct got_reflist_head *refs)
753 const struct got_error *err;
754 struct got_commit_graph *graph;
756 err = got_commit_graph_open(&graph, root_id, path,
757 first_parent_traversal, repo);
758 if (err)
759 return err;
760 err = got_commit_graph_iter_start(graph, root_id, repo);
761 if (err)
762 goto done;
763 for (;;) {
764 struct got_commit_object *commit;
765 struct got_object_id *id;
767 if (sigint_received || sigpipe_received)
768 break;
770 err = got_commit_graph_iter_next(&id, graph);
771 if (err) {
772 if (err->code == GOT_ERR_ITER_COMPLETED) {
773 err = NULL;
774 break;
776 if (err->code != GOT_ERR_ITER_NEED_MORE)
777 break;
778 err = got_commit_graph_fetch_commits(graph, 1, repo);
779 if (err)
780 break;
781 else
782 continue;
784 if (id == NULL)
785 break;
787 err = got_object_open_as_commit(&commit, repo, id);
788 if (err)
789 break;
790 err = print_commit(commit, id, repo, show_patch, diff_context,
791 refs);
792 got_object_commit_close(commit);
793 if (err || (limit && --limit == 0))
794 break;
796 done:
797 got_commit_graph_close(graph);
798 return err;
801 __dead static void
802 usage_log(void)
804 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
805 "[-r repository-path] [path]\n", getprogname());
806 exit(1);
809 static const struct got_error *
810 cmd_log(int argc, char *argv[])
812 const struct got_error *error;
813 struct got_repository *repo = NULL;
814 struct got_worktree *worktree = NULL;
815 struct got_commit_object *commit = NULL;
816 struct got_object_id *id = NULL;
817 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
818 char *start_commit = NULL;
819 int diff_context = 3, ch;
820 int show_patch = 0, limit = 0, first_parent_traversal = 0;
821 const char *errstr;
822 struct got_reflist_head refs;
824 SIMPLEQ_INIT(&refs);
826 #ifndef PROFILE
827 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
828 NULL)
829 == -1)
830 err(1, "pledge");
831 #endif
833 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
834 switch (ch) {
835 case 'p':
836 show_patch = 1;
837 break;
838 case 'c':
839 start_commit = optarg;
840 break;
841 case 'C':
842 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
843 &errstr);
844 if (errstr != NULL)
845 err(1, "-C option %s", errstr);
846 break;
847 case 'l':
848 limit = strtonum(optarg, 1, INT_MAX, &errstr);
849 if (errstr != NULL)
850 err(1, "-l option %s", errstr);
851 break;
852 case 'f':
853 first_parent_traversal = 1;
854 break;
855 case 'r':
856 repo_path = realpath(optarg, NULL);
857 if (repo_path == NULL)
858 err(1, "-r option");
859 got_path_strip_trailing_slashes(repo_path);
860 break;
861 default:
862 usage_log();
863 /* NOTREACHED */
867 argc -= optind;
868 argv += optind;
870 cwd = getcwd(NULL, 0);
871 if (cwd == NULL) {
872 error = got_error_prefix_errno("getcwd");
873 goto done;
876 error = got_worktree_open(&worktree, cwd);
877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
878 goto done;
879 error = NULL;
881 if (argc == 0) {
882 path = strdup("");
883 if (path == NULL) {
884 error = got_error_prefix_errno("strdup");
885 goto done;
887 } else if (argc == 1) {
888 if (worktree) {
889 error = got_worktree_resolve_path(&path, worktree,
890 argv[0]);
891 if (error)
892 goto done;
893 } else {
894 path = strdup(argv[0]);
895 if (path == NULL) {
896 error = got_error_prefix_errno("strdup");
897 goto done;
900 } else
901 usage_log();
903 if (repo_path == NULL) {
904 repo_path = worktree ?
905 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
907 if (repo_path == NULL) {
908 error = got_error_prefix_errno("strdup");
909 goto done;
912 error = got_repo_open(&repo, repo_path);
913 if (error != NULL)
914 goto done;
916 error = apply_unveil(got_repo_get_path(repo), 1,
917 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
918 if (error)
919 goto done;
921 if (start_commit == NULL) {
922 struct got_reference *head_ref;
923 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
924 if (error != NULL)
925 return error;
926 error = got_ref_resolve(&id, repo, head_ref);
927 got_ref_close(head_ref);
928 if (error != NULL)
929 return error;
930 error = got_object_open_as_commit(&commit, repo, id);
931 } else {
932 struct got_reference *ref;
933 error = got_ref_open(&ref, repo, start_commit, 0);
934 if (error == NULL) {
935 int obj_type;
936 error = got_ref_resolve(&id, repo, ref);
937 got_ref_close(ref);
938 if (error != NULL)
939 goto done;
940 error = got_object_get_type(&obj_type, repo, id);
941 if (error != NULL)
942 goto done;
943 if (obj_type == GOT_OBJ_TYPE_TAG) {
944 struct got_tag_object *tag;
945 error = got_object_open_as_tag(&tag, repo, id);
946 if (error != NULL)
947 goto done;
948 if (got_object_tag_get_object_type(tag) !=
949 GOT_OBJ_TYPE_COMMIT) {
950 got_object_tag_close(tag);
951 error = got_error(GOT_ERR_OBJ_TYPE);
952 goto done;
954 free(id);
955 id = got_object_id_dup(
956 got_object_tag_get_object_id(tag));
957 if (id == NULL)
958 error = got_error_prefix_errno(
959 "got_object_id_dup");
960 got_object_tag_close(tag);
961 if (error)
962 goto done;
963 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
964 error = got_error(GOT_ERR_OBJ_TYPE);
965 goto done;
967 error = got_object_open_as_commit(&commit, repo, id);
968 if (error != NULL)
969 goto done;
971 if (commit == NULL) {
972 error = got_object_resolve_id_str(&id, repo,
973 start_commit);
974 if (error != NULL)
975 return error;
978 if (error != NULL)
979 goto done;
981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
982 if (error != NULL)
983 goto done;
984 if (in_repo_path) {
985 free(path);
986 path = in_repo_path;
989 error = got_ref_list(&refs, repo);
990 if (error)
991 goto done;
993 error = print_commits(id, repo, path, show_patch,
994 diff_context, limit, first_parent_traversal, &refs);
995 done:
996 free(path);
997 free(repo_path);
998 free(cwd);
999 free(id);
1000 if (worktree)
1001 got_worktree_close(worktree);
1002 if (repo) {
1003 const struct got_error *repo_error;
1004 repo_error = got_repo_close(repo);
1005 if (error == NULL)
1006 error = repo_error;
1008 got_ref_list_free(&refs);
1009 return error;
1012 __dead static void
1013 usage_diff(void)
1015 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1016 "[object1 object2 | path]\n", getprogname());
1017 exit(1);
1020 struct print_diff_arg {
1021 struct got_repository *repo;
1022 struct got_worktree *worktree;
1023 int diff_context;
1024 const char *id_str;
1025 int header_shown;
1028 static const struct got_error *
1029 print_diff(void *arg, unsigned char status, const char *path,
1030 struct got_object_id *id)
1032 struct print_diff_arg *a = arg;
1033 const struct got_error *err = NULL;
1034 struct got_blob_object *blob1 = NULL;
1035 FILE *f2 = NULL;
1036 char *abspath = NULL;
1037 struct stat sb;
1039 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1040 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1041 return NULL;
1043 if (!a->header_shown) {
1044 printf("diff %s %s\n", a->id_str,
1045 got_worktree_get_root_path(a->worktree));
1046 a->header_shown = 1;
1049 if (status != GOT_STATUS_ADD) {
1050 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1051 if (err)
1052 goto done;
1056 if (status != GOT_STATUS_DELETE) {
1057 if (asprintf(&abspath, "%s/%s",
1058 got_worktree_get_root_path(a->worktree), path) == -1) {
1059 err = got_error_prefix_errno("asprintf");
1060 goto done;
1063 f2 = fopen(abspath, "r");
1064 if (f2 == NULL) {
1065 err = got_error_prefix_errno2("fopen", abspath);
1066 goto done;
1068 if (lstat(abspath, &sb) == -1) {
1069 err = got_error_prefix_errno2("lstat", abspath);
1070 goto done;
1072 } else
1073 sb.st_size = 0;
1075 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1076 stdout);
1077 done:
1078 if (blob1)
1079 got_object_blob_close(blob1);
1080 if (f2 && fclose(f2) != 0 && err == NULL)
1081 err = got_error_prefix_errno("fclose");
1082 free(abspath);
1083 return err;
1086 static const struct got_error *
1087 cmd_diff(int argc, char *argv[])
1089 const struct got_error *error;
1090 struct got_repository *repo = NULL;
1091 struct got_worktree *worktree = NULL;
1092 char *cwd = NULL, *repo_path = NULL;
1093 struct got_object_id *id1 = NULL, *id2 = NULL;
1094 char *id_str1 = NULL, *id_str2 = NULL;
1095 int type1, type2;
1096 int diff_context = 3, ch;
1097 const char *errstr;
1098 char *path = NULL;
1100 #ifndef PROFILE
1101 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1102 NULL) == -1)
1103 err(1, "pledge");
1104 #endif
1106 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1107 switch (ch) {
1108 case 'C':
1109 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1110 if (errstr != NULL)
1111 err(1, "-C option %s", errstr);
1112 break;
1113 case 'r':
1114 repo_path = realpath(optarg, NULL);
1115 if (repo_path == NULL)
1116 err(1, "-r option");
1117 got_path_strip_trailing_slashes(repo_path);
1118 break;
1119 default:
1120 usage_diff();
1121 /* NOTREACHED */
1125 argc -= optind;
1126 argv += optind;
1128 cwd = getcwd(NULL, 0);
1129 if (cwd == NULL) {
1130 error = got_error_prefix_errno("getcwd");
1131 goto done;
1133 error = got_worktree_open(&worktree, cwd);
1134 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1135 goto done;
1136 if (argc <= 1) {
1137 if (worktree == NULL) {
1138 error = got_error(GOT_ERR_NOT_WORKTREE);
1139 goto done;
1141 if (repo_path)
1142 errx(1,
1143 "-r option can't be used when diffing a work tree");
1144 repo_path = strdup(got_worktree_get_repo_path(worktree));
1145 if (repo_path == NULL) {
1146 error = got_error_prefix_errno("strdup");
1147 goto done;
1149 if (argc == 1) {
1150 error = got_worktree_resolve_path(&path, worktree,
1151 argv[0]);
1152 if (error)
1153 goto done;
1154 } else {
1155 path = strdup("");
1156 if (path == NULL) {
1157 error = got_error_prefix_errno("strdup");
1158 goto done;
1161 } else if (argc == 2) {
1162 id_str1 = argv[0];
1163 id_str2 = argv[1];
1164 } else
1165 usage_diff();
1167 if (repo_path == NULL) {
1168 repo_path = getcwd(NULL, 0);
1169 if (repo_path == NULL)
1170 return got_error_prefix_errno("getcwd");
1173 error = got_repo_open(&repo, repo_path);
1174 free(repo_path);
1175 if (error != NULL)
1176 goto done;
1178 error = apply_unveil(got_repo_get_path(repo), 1,
1179 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1180 if (error)
1181 goto done;
1183 if (worktree) {
1184 struct print_diff_arg arg;
1185 char *id_str;
1186 error = got_object_id_str(&id_str,
1187 got_worktree_get_base_commit_id(worktree));
1188 if (error)
1189 goto done;
1190 arg.repo = repo;
1191 arg.worktree = worktree;
1192 arg.diff_context = diff_context;
1193 arg.id_str = id_str;
1194 arg.header_shown = 0;
1196 error = got_worktree_status(worktree, path, repo, print_diff,
1197 &arg, check_cancelled, NULL);
1198 free(id_str);
1199 goto done;
1202 error = got_object_resolve_id_str(&id1, repo, id_str1);
1203 if (error)
1204 goto done;
1206 error = got_object_resolve_id_str(&id2, repo, id_str2);
1207 if (error)
1208 goto done;
1210 error = got_object_get_type(&type1, repo, id1);
1211 if (error)
1212 goto done;
1214 error = got_object_get_type(&type2, repo, id2);
1215 if (error)
1216 goto done;
1218 if (type1 != type2) {
1219 error = got_error(GOT_ERR_OBJ_TYPE);
1220 goto done;
1223 switch (type1) {
1224 case GOT_OBJ_TYPE_BLOB:
1225 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1226 diff_context, repo, stdout);
1227 break;
1228 case GOT_OBJ_TYPE_TREE:
1229 error = got_diff_objects_as_trees(id1, id2, "", "",
1230 diff_context, repo, stdout);
1231 break;
1232 case GOT_OBJ_TYPE_COMMIT:
1233 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1234 id_str2);
1235 error = got_diff_objects_as_commits(id1, id2, diff_context,
1236 repo, stdout);
1237 break;
1238 default:
1239 error = got_error(GOT_ERR_OBJ_TYPE);
1242 done:
1243 free(id1);
1244 free(id2);
1245 free(path);
1246 if (worktree)
1247 got_worktree_close(worktree);
1248 if (repo) {
1249 const struct got_error *repo_error;
1250 repo_error = got_repo_close(repo);
1251 if (error == NULL)
1252 error = repo_error;
1254 return error;
1257 __dead static void
1258 usage_blame(void)
1260 fprintf(stderr,
1261 "usage: %s blame [-c commit] [-r repository-path] path\n",
1262 getprogname());
1263 exit(1);
1266 static const struct got_error *
1267 cmd_blame(int argc, char *argv[])
1269 const struct got_error *error;
1270 struct got_repository *repo = NULL;
1271 struct got_worktree *worktree = NULL;
1272 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1273 struct got_object_id *commit_id = NULL;
1274 char *commit_id_str = NULL;
1275 int ch;
1277 #ifndef PROFILE
1278 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1279 NULL) == -1)
1280 err(1, "pledge");
1281 #endif
1283 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1284 switch (ch) {
1285 case 'c':
1286 commit_id_str = optarg;
1287 break;
1288 case 'r':
1289 repo_path = realpath(optarg, NULL);
1290 if (repo_path == NULL)
1291 err(1, "-r option");
1292 got_path_strip_trailing_slashes(repo_path);
1293 break;
1294 default:
1295 usage_blame();
1296 /* NOTREACHED */
1300 argc -= optind;
1301 argv += optind;
1303 if (argc == 1)
1304 path = argv[0];
1305 else
1306 usage_blame();
1308 cwd = getcwd(NULL, 0);
1309 if (cwd == NULL) {
1310 error = got_error_prefix_errno("getcwd");
1311 goto done;
1313 if (repo_path == NULL) {
1314 error = got_worktree_open(&worktree, cwd);
1315 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1316 goto done;
1317 else
1318 error = NULL;
1319 if (worktree) {
1320 repo_path =
1321 strdup(got_worktree_get_repo_path(worktree));
1322 if (repo_path == NULL)
1323 error = got_error_prefix_errno("strdup");
1324 if (error)
1325 goto done;
1326 } else {
1327 repo_path = strdup(cwd);
1328 if (repo_path == NULL) {
1329 error = got_error_prefix_errno("strdup");
1330 goto done;
1335 error = got_repo_open(&repo, repo_path);
1336 if (error != NULL)
1337 goto done;
1339 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1340 if (error)
1341 goto done;
1343 if (worktree) {
1344 const char *prefix = got_worktree_get_path_prefix(worktree);
1345 char *p, *worktree_subdir = cwd +
1346 strlen(got_worktree_get_root_path(worktree));
1347 if (asprintf(&p, "%s%s%s%s%s",
1348 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1349 worktree_subdir, worktree_subdir[0] ? "/" : "",
1350 path) == -1) {
1351 error = got_error_prefix_errno("asprintf");
1352 goto done;
1354 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1355 free(p);
1356 } else {
1357 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1359 if (error)
1360 goto done;
1362 if (commit_id_str == NULL) {
1363 struct got_reference *head_ref;
1364 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1365 if (error != NULL)
1366 goto done;
1367 error = got_ref_resolve(&commit_id, repo, head_ref);
1368 got_ref_close(head_ref);
1369 if (error != NULL)
1370 goto done;
1371 } else {
1372 error = got_object_resolve_id_str(&commit_id, repo,
1373 commit_id_str);
1374 if (error != NULL)
1375 goto done;
1378 error = got_blame(in_repo_path, commit_id, repo, stdout);
1379 done:
1380 free(in_repo_path);
1381 free(repo_path);
1382 free(cwd);
1383 free(commit_id);
1384 if (worktree)
1385 got_worktree_close(worktree);
1386 if (repo) {
1387 const struct got_error *repo_error;
1388 repo_error = got_repo_close(repo);
1389 if (error == NULL)
1390 error = repo_error;
1392 return error;
1395 __dead static void
1396 usage_tree(void)
1398 fprintf(stderr,
1399 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1400 getprogname());
1401 exit(1);
1404 static void
1405 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1406 const char *root_path)
1408 int is_root_path = (strcmp(path, root_path) == 0);
1410 path += strlen(root_path);
1411 while (path[0] == '/')
1412 path++;
1414 printf("%s%s%s%s%s\n", id ? id : "", path,
1415 is_root_path ? "" : "/", te->name,
1416 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1419 static const struct got_error *
1420 print_tree(const char *path, struct got_object_id *commit_id,
1421 int show_ids, int recurse, const char *root_path,
1422 struct got_repository *repo)
1424 const struct got_error *err = NULL;
1425 struct got_object_id *tree_id = NULL;
1426 struct got_tree_object *tree = NULL;
1427 const struct got_tree_entries *entries;
1428 struct got_tree_entry *te;
1430 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1431 if (err)
1432 goto done;
1434 err = got_object_open_as_tree(&tree, repo, tree_id);
1435 if (err)
1436 goto done;
1437 entries = got_object_tree_get_entries(tree);
1438 te = SIMPLEQ_FIRST(&entries->head);
1439 while (te) {
1440 char *id = NULL;
1442 if (sigint_received || sigpipe_received)
1443 break;
1445 if (show_ids) {
1446 char *id_str;
1447 err = got_object_id_str(&id_str, te->id);
1448 if (err)
1449 goto done;
1450 if (asprintf(&id, "%s ", id_str) == -1) {
1451 err = got_error_prefix_errno("asprintf");
1452 free(id_str);
1453 goto done;
1455 free(id_str);
1457 print_entry(te, id, path, root_path);
1458 free(id);
1460 if (recurse && S_ISDIR(te->mode)) {
1461 char *child_path;
1462 if (asprintf(&child_path, "%s%s%s", path,
1463 path[0] == '/' && path[1] == '\0' ? "" : "/",
1464 te->name) == -1) {
1465 err = got_error_prefix_errno("asprintf");
1466 goto done;
1468 err = print_tree(child_path, commit_id, show_ids, 1,
1469 root_path, repo);
1470 free(child_path);
1471 if (err)
1472 goto done;
1475 te = SIMPLEQ_NEXT(te, entry);
1477 done:
1478 if (tree)
1479 got_object_tree_close(tree);
1480 free(tree_id);
1481 return err;
1484 static const struct got_error *
1485 cmd_tree(int argc, char *argv[])
1487 const struct got_error *error;
1488 struct got_repository *repo = NULL;
1489 struct got_worktree *worktree = NULL;
1490 const char *path;
1491 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1492 struct got_object_id *commit_id = NULL;
1493 char *commit_id_str = NULL;
1494 int show_ids = 0, recurse = 0;
1495 int ch;
1497 #ifndef PROFILE
1498 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1499 NULL) == -1)
1500 err(1, "pledge");
1501 #endif
1503 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1504 switch (ch) {
1505 case 'c':
1506 commit_id_str = optarg;
1507 break;
1508 case 'r':
1509 repo_path = realpath(optarg, NULL);
1510 if (repo_path == NULL)
1511 err(1, "-r option");
1512 got_path_strip_trailing_slashes(repo_path);
1513 break;
1514 case 'i':
1515 show_ids = 1;
1516 break;
1517 case 'R':
1518 recurse = 1;
1519 break;
1520 default:
1521 usage_tree();
1522 /* NOTREACHED */
1526 argc -= optind;
1527 argv += optind;
1529 if (argc == 1)
1530 path = argv[0];
1531 else if (argc > 1)
1532 usage_tree();
1533 else
1534 path = NULL;
1536 cwd = getcwd(NULL, 0);
1537 if (cwd == NULL) {
1538 error = got_error_prefix_errno("getcwd");
1539 goto done;
1541 if (repo_path == NULL) {
1542 error = got_worktree_open(&worktree, cwd);
1543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1544 goto done;
1545 else
1546 error = NULL;
1547 if (worktree) {
1548 repo_path =
1549 strdup(got_worktree_get_repo_path(worktree));
1550 if (repo_path == NULL)
1551 error = got_error_prefix_errno("strdup");
1552 if (error)
1553 goto done;
1554 } else {
1555 repo_path = strdup(cwd);
1556 if (repo_path == NULL) {
1557 error = got_error_prefix_errno("strdup");
1558 goto done;
1563 error = got_repo_open(&repo, repo_path);
1564 if (error != NULL)
1565 goto done;
1567 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1568 if (error)
1569 goto done;
1571 if (path == NULL) {
1572 if (worktree) {
1573 char *p, *worktree_subdir = cwd +
1574 strlen(got_worktree_get_root_path(worktree));
1575 if (asprintf(&p, "%s/%s",
1576 got_worktree_get_path_prefix(worktree),
1577 worktree_subdir) == -1) {
1578 error = got_error_prefix_errno("asprintf");
1579 goto done;
1581 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1582 free(p);
1583 if (error)
1584 goto done;
1585 } else
1586 path = "/";
1588 if (in_repo_path == NULL) {
1589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1590 if (error != NULL)
1591 goto done;
1594 if (commit_id_str == NULL) {
1595 struct got_reference *head_ref;
1596 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1597 if (error != NULL)
1598 goto done;
1599 error = got_ref_resolve(&commit_id, repo, head_ref);
1600 got_ref_close(head_ref);
1601 if (error != NULL)
1602 goto done;
1603 } else {
1604 error = got_object_resolve_id_str(&commit_id, repo,
1605 commit_id_str);
1606 if (error != NULL)
1607 goto done;
1610 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1611 in_repo_path, repo);
1612 done:
1613 free(in_repo_path);
1614 free(repo_path);
1615 free(cwd);
1616 free(commit_id);
1617 if (worktree)
1618 got_worktree_close(worktree);
1619 if (repo) {
1620 const struct got_error *repo_error;
1621 repo_error = got_repo_close(repo);
1622 if (error == NULL)
1623 error = repo_error;
1625 return error;
1628 __dead static void
1629 usage_status(void)
1631 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1632 exit(1);
1635 static const struct got_error *
1636 print_status(void *arg, unsigned char status, const char *path,
1637 struct got_object_id *id)
1639 printf("%c %s\n", status, path);
1640 return NULL;
1643 static const struct got_error *
1644 cmd_status(int argc, char *argv[])
1646 const struct got_error *error = NULL;
1647 struct got_repository *repo = NULL;
1648 struct got_worktree *worktree = NULL;
1649 char *cwd = NULL, *path = NULL;
1650 int ch;
1652 while ((ch = getopt(argc, argv, "")) != -1) {
1653 switch (ch) {
1654 default:
1655 usage_status();
1656 /* NOTREACHED */
1660 argc -= optind;
1661 argv += optind;
1663 #ifndef PROFILE
1664 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1665 NULL) == -1)
1666 err(1, "pledge");
1667 #endif
1668 cwd = getcwd(NULL, 0);
1669 if (cwd == NULL) {
1670 error = got_error_prefix_errno("getcwd");
1671 goto done;
1674 error = got_worktree_open(&worktree, cwd);
1675 if (error != NULL)
1676 goto done;
1678 if (argc == 0) {
1679 path = strdup("");
1680 if (path == NULL) {
1681 error = got_error_prefix_errno("strdup");
1682 goto done;
1684 } else if (argc == 1) {
1685 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1686 if (error)
1687 goto done;
1688 } else
1689 usage_status();
1691 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1692 if (error != NULL)
1693 goto done;
1695 error = apply_unveil(got_repo_get_path(repo), 1,
1696 got_worktree_get_root_path(worktree), 0);
1697 if (error)
1698 goto done;
1700 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1701 check_cancelled, NULL);
1702 done:
1703 free(cwd);
1704 free(path);
1705 return error;
1708 __dead static void
1709 usage_ref(void)
1711 fprintf(stderr,
1712 "usage: %s ref [-r repository] -l | -d name | name object\n",
1713 getprogname());
1714 exit(1);
1717 static const struct got_error *
1718 list_refs(struct got_repository *repo)
1720 static const struct got_error *err = NULL;
1721 struct got_reflist_head refs;
1722 struct got_reflist_entry *re;
1724 SIMPLEQ_INIT(&refs);
1725 err = got_ref_list(&refs, repo);
1726 if (err)
1727 return err;
1729 SIMPLEQ_FOREACH(re, &refs, entry) {
1730 char *refstr;
1731 refstr = got_ref_to_str(re->ref);
1732 if (refstr == NULL)
1733 return got_error_prefix_errno("got_ref_to_str");
1734 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1735 free(refstr);
1738 got_ref_list_free(&refs);
1739 return NULL;
1742 static const struct got_error *
1743 delete_ref(struct got_repository *repo, const char *refname)
1745 const struct got_error *err = NULL;
1746 struct got_reference *ref;
1748 err = got_ref_open(&ref, repo, refname, 0);
1749 if (err)
1750 return err;
1752 err = got_ref_delete(ref, repo);
1753 got_ref_close(ref);
1754 return err;
1757 static const struct got_error *
1758 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1760 const struct got_error *err = NULL;
1761 struct got_object_id *id;
1762 struct got_reference *ref = NULL;
1764 err = got_object_resolve_id_str(&id, repo, id_str);
1765 if (err)
1766 return err;
1768 err = got_ref_alloc(&ref, refname, id);
1769 if (err)
1770 goto done;
1772 err = got_ref_write(ref, repo);
1773 done:
1774 if (ref)
1775 got_ref_close(ref);
1776 free(id);
1777 return err;
1780 static const struct got_error *
1781 cmd_ref(int argc, char *argv[])
1783 const struct got_error *error = NULL;
1784 struct got_repository *repo = NULL;
1785 struct got_worktree *worktree = NULL;
1786 char *cwd = NULL, *repo_path = NULL;
1787 int ch, do_list = 0;
1788 const char *delref = NULL;
1790 /* TODO: Add -s option for adding symbolic references. */
1791 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1792 switch (ch) {
1793 case 'd':
1794 delref = optarg;
1795 break;
1796 case 'r':
1797 repo_path = realpath(optarg, NULL);
1798 if (repo_path == NULL)
1799 err(1, "-r option");
1800 got_path_strip_trailing_slashes(repo_path);
1801 break;
1802 case 'l':
1803 do_list = 1;
1804 break;
1805 default:
1806 usage_ref();
1807 /* NOTREACHED */
1811 if (do_list && delref)
1812 errx(1, "-l and -d options are mutually exclusive\n");
1814 argc -= optind;
1815 argv += optind;
1817 if (do_list || delref) {
1818 if (argc > 0)
1819 usage_ref();
1820 } else if (argc != 2)
1821 usage_ref();
1823 #ifndef PROFILE
1824 if (do_list) {
1825 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1826 NULL) == -1)
1827 err(1, "pledge");
1828 } else {
1829 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1830 "sendfd unveil", NULL) == -1)
1831 err(1, "pledge");
1833 #endif
1834 cwd = getcwd(NULL, 0);
1835 if (cwd == NULL) {
1836 error = got_error_prefix_errno("getcwd");
1837 goto done;
1840 if (repo_path == NULL) {
1841 error = got_worktree_open(&worktree, cwd);
1842 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1843 goto done;
1844 else
1845 error = NULL;
1846 if (worktree) {
1847 repo_path =
1848 strdup(got_worktree_get_repo_path(worktree));
1849 if (repo_path == NULL)
1850 error = got_error_prefix_errno("strdup");
1851 if (error)
1852 goto done;
1853 } else {
1854 repo_path = strdup(cwd);
1855 if (repo_path == NULL) {
1856 error = got_error_prefix_errno("strdup");
1857 goto done;
1862 error = got_repo_open(&repo, repo_path);
1863 if (error != NULL)
1864 goto done;
1866 error = apply_unveil(got_repo_get_path(repo), do_list,
1867 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1868 if (error)
1869 goto done;
1871 if (do_list)
1872 error = list_refs(repo);
1873 else if (delref)
1874 error = delete_ref(repo, delref);
1875 else
1876 error = add_ref(repo, argv[0], argv[1]);
1877 done:
1878 if (repo)
1879 got_repo_close(repo);
1880 if (worktree)
1881 got_worktree_close(worktree);
1882 free(cwd);
1883 free(repo_path);
1884 return error;
1887 __dead static void
1888 usage_add(void)
1890 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1891 exit(1);
1894 static const struct got_error *
1895 cmd_add(int argc, char *argv[])
1897 const struct got_error *error = NULL;
1898 struct got_repository *repo = NULL;
1899 struct got_worktree *worktree = NULL;
1900 char *cwd = NULL;
1901 struct got_pathlist_head paths;
1902 struct got_pathlist_entry *pe;
1903 int ch, x;
1905 TAILQ_INIT(&paths);
1907 while ((ch = getopt(argc, argv, "")) != -1) {
1908 switch (ch) {
1909 default:
1910 usage_add();
1911 /* NOTREACHED */
1915 argc -= optind;
1916 argv += optind;
1918 if (argc < 1)
1919 usage_add();
1921 /* make sure each file exists before doing anything halfway */
1922 for (x = 0; x < argc; x++) {
1923 char *path = realpath(argv[x], NULL);
1924 if (path == NULL) {
1925 error = got_error_prefix_errno2("realpath", argv[x]);
1926 goto done;
1928 free(path);
1931 cwd = getcwd(NULL, 0);
1932 if (cwd == NULL) {
1933 error = got_error_prefix_errno("getcwd");
1934 goto done;
1937 error = got_worktree_open(&worktree, cwd);
1938 if (error)
1939 goto done;
1941 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1942 if (error != NULL)
1943 goto done;
1945 error = apply_unveil(got_repo_get_path(repo), 1,
1946 got_worktree_get_root_path(worktree), 0);
1947 if (error)
1948 goto done;
1950 for (x = 0; x < argc; x++) {
1951 char *path = realpath(argv[x], NULL);
1952 if (path == NULL) {
1953 error = got_error_prefix_errno2("realpath", argv[x]);
1954 goto done;
1957 got_path_strip_trailing_slashes(path);
1958 error = got_pathlist_insert(&pe, &paths, path, NULL);
1959 if (error) {
1960 free(path);
1961 goto done;
1964 error = got_worktree_schedule_add(worktree, &paths, print_status,
1965 NULL, repo);
1966 done:
1967 if (repo)
1968 got_repo_close(repo);
1969 if (worktree)
1970 got_worktree_close(worktree);
1971 TAILQ_FOREACH(pe, &paths, entry)
1972 free((char *)pe->path);
1973 got_pathlist_free(&paths);
1974 free(cwd);
1975 return error;
1978 __dead static void
1979 usage_rm(void)
1981 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1982 exit(1);
1985 static const struct got_error *
1986 cmd_rm(int argc, char *argv[])
1988 const struct got_error *error = NULL;
1989 struct got_worktree *worktree = NULL;
1990 struct got_repository *repo = NULL;
1991 char *cwd = NULL, *path = NULL;
1992 int ch, delete_local_mods = 0;
1994 while ((ch = getopt(argc, argv, "f")) != -1) {
1995 switch (ch) {
1996 case 'f':
1997 delete_local_mods = 1;
1998 break;
1999 default:
2000 usage_add();
2001 /* NOTREACHED */
2005 argc -= optind;
2006 argv += optind;
2008 if (argc != 1)
2009 usage_rm();
2011 path = realpath(argv[0], NULL);
2012 if (path == NULL) {
2013 error = got_error_prefix_errno2("realpath", argv[0]);
2014 goto done;
2016 got_path_strip_trailing_slashes(path);
2018 cwd = getcwd(NULL, 0);
2019 if (cwd == NULL) {
2020 error = got_error_prefix_errno("getcwd");
2021 goto done;
2023 error = got_worktree_open(&worktree, cwd);
2024 if (error)
2025 goto done;
2027 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2028 if (error)
2029 goto done;
2031 error = apply_unveil(got_repo_get_path(repo), 1,
2032 got_worktree_get_root_path(worktree), 0);
2033 if (error)
2034 goto done;
2036 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2037 print_status, NULL, repo);
2038 if (error)
2039 goto done;
2040 done:
2041 if (repo)
2042 got_repo_close(repo);
2043 if (worktree)
2044 got_worktree_close(worktree);
2045 free(path);
2046 free(cwd);
2047 return error;
2050 __dead static void
2051 usage_revert(void)
2053 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2054 exit(1);
2057 static void
2058 revert_progress(void *arg, unsigned char status, const char *path)
2060 while (path[0] == '/')
2061 path++;
2062 printf("%c %s\n", status, path);
2065 static const struct got_error *
2066 cmd_revert(int argc, char *argv[])
2068 const struct got_error *error = NULL;
2069 struct got_worktree *worktree = NULL;
2070 struct got_repository *repo = NULL;
2071 char *cwd = NULL, *path = NULL;
2072 int ch;
2074 while ((ch = getopt(argc, argv, "")) != -1) {
2075 switch (ch) {
2076 default:
2077 usage_revert();
2078 /* NOTREACHED */
2082 argc -= optind;
2083 argv += optind;
2085 if (argc != 1)
2086 usage_revert();
2088 path = realpath(argv[0], NULL);
2089 if (path == NULL) {
2090 error = got_error_prefix_errno2("realpath", argv[0]);
2091 goto done;
2093 got_path_strip_trailing_slashes(path);
2095 cwd = getcwd(NULL, 0);
2096 if (cwd == NULL) {
2097 error = got_error_prefix_errno("getcwd");
2098 goto done;
2100 error = got_worktree_open(&worktree, cwd);
2101 if (error)
2102 goto done;
2104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2105 if (error != NULL)
2106 goto done;
2108 error = apply_unveil(got_repo_get_path(repo), 1,
2109 got_worktree_get_root_path(worktree), 0);
2110 if (error)
2111 goto done;
2113 error = got_worktree_revert(worktree, path,
2114 revert_progress, NULL, repo);
2115 if (error)
2116 goto done;
2117 done:
2118 if (repo)
2119 got_repo_close(repo);
2120 if (worktree)
2121 got_worktree_close(worktree);
2122 free(path);
2123 free(cwd);
2124 return error;
2127 __dead static void
2128 usage_commit(void)
2130 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2131 exit(1);
2134 int
2135 spawn_editor(const char *file)
2137 char *argp[] = { "sh", "-c", NULL, NULL };
2138 char *editor, *editp;
2139 pid_t pid;
2140 sig_t sighup, sigint, sigquit;
2141 int st = -1;
2143 editor = getenv("VISUAL");
2144 if (editor == NULL)
2145 editor = getenv("EDITOR");
2146 if (editor == NULL)
2147 editor = "ed";
2149 if (asprintf(&editp, "%s %s", editor, file) == -1)
2150 return -1;
2152 argp[2] = editp;
2154 sighup = signal(SIGHUP, SIG_IGN);
2155 sigint = signal(SIGINT, SIG_IGN);
2156 sigquit = signal(SIGQUIT, SIG_IGN);
2158 switch (pid = fork()) {
2159 case -1:
2160 goto doneediting;
2161 case 0:
2162 execv(_PATH_BSHELL, argp);
2163 _exit(127);
2166 free(editp);
2168 while (waitpid(pid, &st, 0) == -1)
2169 if (errno != EINTR)
2170 break;
2172 doneediting:
2173 (void)signal(SIGHUP, sighup);
2174 (void)signal(SIGINT, sigint);
2175 (void)signal(SIGQUIT, sigquit);
2177 if (!WIFEXITED(st)) {
2178 errno = EINTR;
2179 return -1;
2182 return WEXITSTATUS(st);
2185 static const struct got_error *
2186 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2187 void *arg)
2189 struct got_pathlist_entry *pe;
2190 const struct got_error *err = NULL;
2191 char *tmppath = 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 strlcpy(*logmsg, cmdline_log, len);
2205 return NULL;
2208 tmppath = getenv("TMPDIR");
2209 if (tmppath == NULL || strlen(tmppath) == 0)
2210 tmppath = "/tmp";
2212 if (asprintf(&tmpfile, "%s/got-XXXXXXXXXX", tmppath) == -1) {
2213 err = got_error_prefix_errno("asprintf");
2214 return err;
2217 fd = mkstemp(tmpfile);
2218 if (fd < 0) {
2219 err = got_error_prefix_errno("mktemp");
2220 free(tmpfile);
2221 return err;
2224 dprintf(fd, "\n"
2225 "# changes to be committed:\n");
2227 TAILQ_FOREACH(pe, commitable_paths, entry) {
2228 struct got_commitable *ct = pe->data;
2229 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2231 close(fd);
2233 if (stat(tmpfile, &st) == -1) {
2234 err = got_error_prefix_errno2("stat", tmpfile);
2235 goto done;
2238 if (spawn_editor(tmpfile) == -1) {
2239 err = got_error_prefix_errno("failed spawning editor");
2240 goto done;
2243 if (stat(tmpfile, &st2) == -1) {
2244 err = got_error_prefix_errno("stat");
2245 goto done;
2248 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2249 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2250 "no changes made to commit message, aborting");
2251 goto done;
2254 /* remove comments */
2255 *logmsg = malloc(st2.st_size + 1);
2256 len = 0;
2258 fp = fopen(tmpfile, "r");
2259 while (fgets(buf, sizeof(buf), fp) != NULL) {
2260 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2261 continue;
2262 len = strlcat(*logmsg, buf, st2.st_size);
2264 fclose(fp);
2266 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2267 (*logmsg)[len - 1] = '\0';
2268 len--;
2271 if (len == 0) {
2272 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2273 "commit message cannot be empty, aborting");
2274 goto done;
2277 goto done;
2279 done:
2280 if (tmpfile) {
2281 unlink(tmpfile);
2282 free(tmpfile);
2285 return err;
2288 static const struct got_error *
2289 cmd_commit(int argc, char *argv[])
2291 const struct got_error *error = NULL;
2292 struct got_worktree *worktree = NULL;
2293 struct got_repository *repo = NULL;
2294 char *cwd = NULL, *path = NULL, *id_str = NULL;
2295 struct got_object_id *id = NULL;
2296 const char *logmsg = NULL;
2297 const char *got_author = getenv("GOT_AUTHOR");
2298 int ch;
2300 while ((ch = getopt(argc, argv, "m:")) != -1) {
2301 switch (ch) {
2302 case 'm':
2303 logmsg = optarg;
2304 break;
2305 default:
2306 usage_commit();
2307 /* NOTREACHED */
2311 argc -= optind;
2312 argv += optind;
2314 if (argc == 1) {
2315 path = realpath(argv[0], NULL);
2316 if (path == NULL) {
2317 error = got_error_prefix_errno2("realpath", argv[0]);
2318 goto done;
2320 got_path_strip_trailing_slashes(path);
2321 } else if (argc != 0)
2322 usage_commit();
2324 if (got_author == NULL) {
2325 /* TODO: Look current user up in password database */
2326 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2327 goto done;
2330 cwd = getcwd(NULL, 0);
2331 if (cwd == NULL) {
2332 error = got_error_prefix_errno("getcwd");
2333 goto done;
2335 error = got_worktree_open(&worktree, cwd);
2336 if (error)
2337 goto done;
2339 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2340 if (error != NULL)
2341 goto done;
2343 #if 0
2344 error = apply_unveil(got_repo_get_path(repo), 0,
2345 got_worktree_get_root_path(worktree), 0);
2346 if (error)
2347 goto done;
2348 #endif
2350 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2351 collect_commit_logmsg, (void *)logmsg, print_status, NULL, repo);
2352 if (error)
2353 goto done;
2355 error = got_object_id_str(&id_str, id);
2356 if (error)
2357 goto done;
2358 printf("created commit %s\n", id_str);
2359 done:
2360 if (repo)
2361 got_repo_close(repo);
2362 if (worktree)
2363 got_worktree_close(worktree);
2364 free(path);
2365 free(cwd);
2366 free(id_str);
2367 return error;