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>
23 #include <err.h>
24 #include <errno.h>
25 #include <locale.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <libgen.h>
32 #include <time.h>
34 #include "got_error.h"
35 #include "got_object.h"
36 #include "got_reference.h"
37 #include "got_repository.h"
38 #include "got_worktree.h"
39 #include "got_diff.h"
40 #include "got_commit_graph.h"
41 #include "got_blame.h"
42 #include "got_privsep.h"
43 #include "got_path.h"
45 #ifndef nitems
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 #endif
49 static volatile sig_atomic_t sigint_received;
50 static volatile sig_atomic_t sigpipe_received;
52 static void
53 catch_sigint(int signo)
54 {
55 sigint_received = 1;
56 }
58 static void
59 catch_sigpipe(int signo)
60 {
61 sigpipe_received = 1;
62 }
65 struct cmd {
66 const char *cmd_name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *cmd_descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_checkout(void);
74 __dead static void usage_update(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
79 __dead static void usage_status(void);
80 __dead static void usage_ref(void);
81 __dead static void usage_add(void);
82 __dead static void usage_rm(void);
83 __dead static void usage_revert(void);
84 __dead static void usage_commit(void);
86 static const struct got_error* cmd_checkout(int, char *[]);
87 static const struct got_error* cmd_update(int, char *[]);
88 static const struct got_error* cmd_log(int, char *[]);
89 static const struct got_error* cmd_diff(int, char *[]);
90 static const struct got_error* cmd_blame(int, char *[]);
91 static const struct got_error* cmd_tree(int, char *[]);
92 static const struct got_error* cmd_status(int, char *[]);
93 static const struct got_error* cmd_ref(int, char *[]);
94 static const struct got_error* cmd_add(int, char *[]);
95 static const struct got_error* cmd_rm(int, char *[]);
96 static const struct got_error* cmd_revert(int, char *[]);
97 static const struct got_error* cmd_commit(int, char *[]);
99 static struct cmd got_commands[] = {
100 { "checkout", cmd_checkout, usage_checkout,
101 "check out a new work tree from a repository" },
102 { "update", cmd_update, usage_update,
103 "update a work tree to a different commit" },
104 { "log", cmd_log, usage_log,
105 "show repository history" },
106 { "diff", cmd_diff, usage_diff,
107 "compare files and directories" },
108 { "blame", cmd_blame, usage_blame,
109 "show when lines in a file were changed" },
110 { "tree", cmd_tree, usage_tree,
111 "list files and directories in repository" },
112 { "status", cmd_status, usage_status,
113 "show modification status of files" },
114 { "ref", cmd_ref, usage_ref,
115 "manage references in repository" },
116 { "add", cmd_add, usage_add,
117 "add a new file to version control" },
118 { "rm", cmd_rm, usage_rm,
119 "remove a versioned file" },
120 { "revert", cmd_revert, usage_revert,
121 "revert uncommitted changes" },
122 { "commit", cmd_commit, usage_commit,
123 "write changes from work tree to repository" },
124 };
126 int
127 main(int argc, char *argv[])
129 struct cmd *cmd;
130 unsigned int i;
131 int ch;
132 int hflag = 0;
134 setlocale(LC_CTYPE, "");
136 while ((ch = getopt(argc, argv, "h")) != -1) {
137 switch (ch) {
138 case 'h':
139 hflag = 1;
140 break;
141 default:
142 usage();
143 /* NOTREACHED */
147 argc -= optind;
148 argv += optind;
149 optind = 0;
151 if (argc <= 0)
152 usage();
154 signal(SIGINT, catch_sigint);
155 signal(SIGPIPE, catch_sigpipe);
157 for (i = 0; i < nitems(got_commands); i++) {
158 const struct got_error *error;
160 cmd = &got_commands[i];
162 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
163 continue;
165 if (hflag)
166 got_commands[i].cmd_usage();
168 error = got_commands[i].cmd_main(argc, argv);
169 if (error && !(sigint_received || sigpipe_received)) {
170 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
171 return 1;
174 return 0;
177 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
178 return 1;
181 __dead static void
182 usage(void)
184 int i;
186 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
187 "Available commands:\n", getprogname());
188 for (i = 0; i < nitems(got_commands); i++) {
189 struct cmd *cmd = &got_commands[i];
190 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
192 exit(1);
195 static const struct got_error *
196 apply_unveil(const char *repo_path, int repo_read_only,
197 const char *worktree_path, int create_worktree)
199 const struct got_error *error;
201 if (create_worktree) {
202 /* Pre-create work tree path to avoid unveiling its parents. */
203 error = got_path_mkdir(worktree_path);
204 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
205 return error;
208 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
209 return got_error_from_errno();
211 if (worktree_path && unveil(worktree_path, "rwc") != 0)
212 return got_error_from_errno();
214 if (unveil("/tmp", "rwc") != 0)
215 return got_error_from_errno();
217 error = got_privsep_unveil_exec_helpers();
218 if (error != NULL)
219 return error;
221 if (unveil(NULL, NULL) != 0)
222 return got_error_from_errno();
224 return NULL;
227 __dead static void
228 usage_checkout(void)
230 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
231 "[worktree-path]\n", getprogname());
232 exit(1);
235 static void
236 checkout_progress(void *arg, unsigned char status, const char *path)
238 char *worktree_path = arg;
240 while (path[0] == '/')
241 path++;
243 printf("%c %s/%s\n", status, worktree_path, path);
246 static const struct got_error *
247 check_cancelled(void *arg)
249 if (sigint_received || sigpipe_received)
250 return got_error(GOT_ERR_CANCELLED);
251 return NULL;
254 static const struct got_error *
255 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
256 struct got_repository *repo)
258 const struct got_error *err;
259 struct got_reference *head_ref = NULL;
260 struct got_object_id *head_commit_id = NULL;
261 struct got_commit_graph *graph = NULL;
263 err = got_ref_open(&head_ref, repo,
264 got_worktree_get_head_ref_name(worktree));
265 if (err)
266 return err;
268 /* TODO: Check the reflog. The head ref may have been rebased. */
269 err = got_ref_resolve(&head_commit_id, repo, head_ref);
270 if (err)
271 goto done;
273 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
274 if (err)
275 goto done;
277 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
278 if (err)
279 goto done;
280 while (1) {
281 struct got_object_id *id;
283 if (sigint_received || sigpipe_received)
284 break;
286 err = got_commit_graph_iter_next(&id, graph);
287 if (err) {
288 if (err->code == GOT_ERR_ITER_COMPLETED) {
289 err = got_error(GOT_ERR_ANCESTRY);
290 break;
292 if (err->code != GOT_ERR_ITER_NEED_MORE)
293 break;
294 err = got_commit_graph_fetch_commits(graph, 1, repo);
295 if (err)
296 break;
297 else
298 continue;
300 if (id == NULL)
301 break;
302 if (got_object_id_cmp(id, commit_id) == 0)
303 break;
305 done:
306 if (head_ref)
307 got_ref_close(head_ref);
308 if (graph)
309 got_commit_graph_close(graph);
310 return err;
314 static const struct got_error *
315 cmd_checkout(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 struct got_repository *repo = NULL;
319 struct got_reference *head_ref = NULL;
320 struct got_worktree *worktree = NULL;
321 char *repo_path = NULL;
322 char *worktree_path = NULL;
323 const char *path_prefix = "";
324 char *commit_id_str = NULL;
325 int ch, same_path_prefix;
327 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
328 switch (ch) {
329 case 'c':
330 commit_id_str = strdup(optarg);
331 if (commit_id_str == NULL)
332 return got_error_from_errno();
333 break;
334 case 'p':
335 path_prefix = optarg;
336 break;
337 default:
338 usage_checkout();
339 /* NOTREACHED */
343 argc -= optind;
344 argv += optind;
346 #ifndef PROFILE
347 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
348 "unveil", NULL) == -1)
349 err(1, "pledge");
350 #endif
351 if (argc == 1) {
352 char *cwd, *base, *dotgit;
353 repo_path = realpath(argv[0], NULL);
354 if (repo_path == NULL)
355 return got_error_from_errno();
356 cwd = getcwd(NULL, 0);
357 if (cwd == NULL) {
358 error = got_error_from_errno();
359 goto done;
361 if (path_prefix[0])
362 base = basename(path_prefix);
363 else
364 base = basename(repo_path);
365 if (base == NULL) {
366 error = got_error_from_errno();
367 goto done;
369 dotgit = strstr(base, ".git");
370 if (dotgit)
371 *dotgit = '\0';
372 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
373 error = got_error_from_errno();
374 free(cwd);
375 goto done;
377 free(cwd);
378 } else if (argc == 2) {
379 repo_path = realpath(argv[0], NULL);
380 if (repo_path == NULL) {
381 error = got_error_from_errno();
382 goto done;
384 worktree_path = realpath(argv[1], NULL);
385 if (worktree_path == NULL) {
386 error = got_error_from_errno();
387 goto done;
389 } else
390 usage_checkout();
392 got_path_strip_trailing_slashes(worktree_path);
394 error = got_repo_open(&repo, repo_path);
395 if (error != NULL)
396 goto done;
398 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
399 if (error)
400 goto done;
402 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
403 if (error != NULL)
404 goto done;
406 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
407 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
408 goto done;
410 error = got_worktree_open(&worktree, worktree_path);
411 if (error != NULL)
412 goto done;
414 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
415 path_prefix);
416 if (error != NULL)
417 goto done;
418 if (!same_path_prefix) {
419 error = got_error(GOT_ERR_PATH_PREFIX);
420 goto done;
423 if (commit_id_str) {
424 struct got_object_id *commit_id;
425 error = got_object_resolve_id_str(&commit_id, repo,
426 commit_id_str);
427 if (error != NULL)
428 goto done;
429 error = check_ancestry(worktree, commit_id, repo);
430 if (error != NULL) {
431 free(commit_id);
432 goto done;
434 error = got_worktree_set_base_commit_id(worktree, repo,
435 commit_id);
436 free(commit_id);
437 if (error)
438 goto done;
441 error = got_worktree_checkout_files(worktree, "", repo,
442 checkout_progress, worktree_path, check_cancelled, NULL);
443 if (error != NULL)
444 goto done;
446 printf("Now shut up and hack\n");
448 done:
449 free(commit_id_str);
450 free(repo_path);
451 free(worktree_path);
452 return error;
455 __dead static void
456 usage_update(void)
458 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
459 getprogname());
460 exit(1);
463 static void
464 update_progress(void *arg, unsigned char status, const char *path)
466 int *did_something = arg;
468 if (status == GOT_STATUS_EXISTS)
469 return;
471 *did_something = 1;
472 while (path[0] == '/')
473 path++;
474 printf("%c %s\n", status, path);
477 static const struct got_error *
478 cmd_update(int argc, char *argv[])
480 const struct got_error *error = NULL;
481 struct got_repository *repo = NULL;
482 struct got_worktree *worktree = NULL;
483 char *worktree_path = NULL, *path = NULL;
484 struct got_object_id *commit_id = NULL;
485 char *commit_id_str = NULL;
486 int ch, did_something = 0;
488 while ((ch = getopt(argc, argv, "c:")) != -1) {
489 switch (ch) {
490 case 'c':
491 commit_id_str = strdup(optarg);
492 if (commit_id_str == NULL)
493 return got_error_from_errno();
494 break;
495 default:
496 usage_update();
497 /* NOTREACHED */
501 argc -= optind;
502 argv += optind;
504 #ifndef PROFILE
505 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
506 "unveil", NULL) == -1)
507 err(1, "pledge");
508 #endif
509 worktree_path = getcwd(NULL, 0);
510 if (worktree_path == NULL) {
511 error = got_error_from_errno();
512 goto done;
514 error = got_worktree_open(&worktree, worktree_path);
515 if (error)
516 goto done;
518 if (argc == 0) {
519 path = strdup("");
520 if (path == NULL) {
521 error = got_error_from_errno();
522 goto done;
524 } else if (argc == 1) {
525 error = got_worktree_resolve_path(&path, worktree, argv[0]);
526 if (error)
527 goto done;
528 } else
529 usage_update();
531 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
532 if (error != NULL)
533 goto done;
535 error = apply_unveil(got_repo_get_path(repo), 0,
536 got_worktree_get_root_path(worktree), 0);
537 if (error)
538 goto done;
540 if (commit_id_str == NULL) {
541 struct got_reference *head_ref;
542 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
543 if (error != NULL)
544 goto done;
545 error = got_ref_resolve(&commit_id, repo, head_ref);
546 if (error != NULL)
547 goto done;
548 error = got_object_id_str(&commit_id_str, commit_id);
549 if (error != NULL)
550 goto done;
551 } else {
552 error = got_object_resolve_id_str(&commit_id, repo,
553 commit_id_str);
554 if (error != NULL)
555 goto done;
558 error = check_ancestry(worktree, commit_id, repo);
559 if (error != NULL)
560 goto done;
562 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
563 commit_id) != 0) {
564 error = got_worktree_set_base_commit_id(worktree, repo,
565 commit_id);
566 if (error)
567 goto done;
570 error = got_worktree_checkout_files(worktree, path, repo,
571 update_progress, &did_something, check_cancelled, NULL);
572 if (error != NULL)
573 goto done;
575 if (did_something)
576 printf("Updated to commit %s\n", commit_id_str);
577 else
578 printf("Already up-to-date\n");
579 done:
580 free(worktree_path);
581 free(path);
582 free(commit_id);
583 free(commit_id_str);
584 return error;
587 static const struct got_error *
588 print_patch(struct got_commit_object *commit, struct got_object_id *id,
589 int diff_context, struct got_repository *repo)
591 const struct got_error *err = NULL;
592 struct got_tree_object *tree1 = NULL, *tree2;
593 struct got_object_qid *qid;
594 char *id_str1 = NULL, *id_str2;
596 err = got_object_open_as_tree(&tree2, repo,
597 got_object_commit_get_tree_id(commit));
598 if (err)
599 return err;
601 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
602 if (qid != NULL) {
603 struct got_commit_object *pcommit;
605 err = got_object_open_as_commit(&pcommit, repo, qid->id);
606 if (err)
607 return err;
609 err = got_object_open_as_tree(&tree1, repo,
610 got_object_commit_get_tree_id(pcommit));
611 got_object_commit_close(pcommit);
612 if (err)
613 return err;
615 err = got_object_id_str(&id_str1, qid->id);
616 if (err)
617 return err;
620 err = got_object_id_str(&id_str2, id);
621 if (err)
622 goto done;
624 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
625 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
626 done:
627 if (tree1)
628 got_object_tree_close(tree1);
629 got_object_tree_close(tree2);
630 free(id_str1);
631 free(id_str2);
632 return err;
635 static char *
636 get_datestr(time_t *time, char *datebuf)
638 char *p, *s = ctime_r(time, datebuf);
639 p = strchr(s, '\n');
640 if (p)
641 *p = '\0';
642 return s;
645 static const struct got_error *
646 print_commit(struct got_commit_object *commit, struct got_object_id *id,
647 struct got_repository *repo, int show_patch, int diff_context,
648 struct got_reflist_head *refs)
650 const struct got_error *err = NULL;
651 char *id_str, *datestr, *logmsg0, *logmsg, *line;
652 char datebuf[26];
653 time_t committer_time;
654 const char *author, *committer;
655 char *refs_str = NULL;
656 struct got_reflist_entry *re;
658 SIMPLEQ_FOREACH(re, refs, entry) {
659 char *s;
660 const char *name;
661 if (got_object_id_cmp(re->id, id) != 0)
662 continue;
663 name = got_ref_get_name(re->ref);
664 if (strcmp(name, GOT_REF_HEAD) == 0)
665 continue;
666 if (strncmp(name, "refs/", 5) == 0)
667 name += 5;
668 if (strncmp(name, "got/", 4) == 0)
669 continue;
670 if (strncmp(name, "heads/", 6) == 0)
671 name += 6;
672 if (strncmp(name, "remotes/", 8) == 0)
673 name += 8;
674 s = refs_str;
675 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
676 name) == -1) {
677 err = got_error_from_errno();
678 free(s);
679 break;
681 free(s);
683 err = got_object_id_str(&id_str, id);
684 if (err)
685 return err;
687 printf("-----------------------------------------------\n");
688 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
689 refs_str ? refs_str : "", refs_str ? ")" : "");
690 free(id_str);
691 id_str = NULL;
692 free(refs_str);
693 refs_str = NULL;
694 printf("from: %s\n", got_object_commit_get_author(commit));
695 committer_time = got_object_commit_get_committer_time(commit);
696 datestr = get_datestr(&committer_time, datebuf);
697 printf("date: %s UTC\n", datestr);
698 author = got_object_commit_get_author(commit);
699 committer = got_object_commit_get_committer(commit);
700 if (strcmp(author, committer) != 0)
701 printf("via: %s\n", committer);
702 if (got_object_commit_get_nparents(commit) > 1) {
703 const struct got_object_id_queue *parent_ids;
704 struct got_object_qid *qid;
705 int n = 1;
706 parent_ids = got_object_commit_get_parent_ids(commit);
707 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
708 err = got_object_id_str(&id_str, qid->id);
709 if (err)
710 return err;
711 printf("parent %d: %s\n", n++, id_str);
712 free(id_str);
716 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
717 if (logmsg0 == NULL)
718 return got_error_from_errno();
720 logmsg = logmsg0;
721 do {
722 line = strsep(&logmsg, "\n");
723 if (line)
724 printf(" %s\n", line);
725 } while (line);
726 free(logmsg0);
728 if (show_patch) {
729 err = print_patch(commit, id, diff_context, repo);
730 if (err == 0)
731 printf("\n");
734 if (fflush(stdout) != 0 && err == NULL)
735 err = got_error_from_errno();
736 return err;
739 static const struct got_error *
740 print_commits(struct got_object_id *root_id, struct got_repository *repo,
741 char *path, int show_patch, int diff_context, int limit,
742 int first_parent_traversal, struct got_reflist_head *refs)
744 const struct got_error *err;
745 struct got_commit_graph *graph;
747 err = got_commit_graph_open(&graph, root_id, path,
748 first_parent_traversal, repo);
749 if (err)
750 return err;
751 err = got_commit_graph_iter_start(graph, root_id, repo);
752 if (err)
753 goto done;
754 while (1) {
755 struct got_commit_object *commit;
756 struct got_object_id *id;
758 if (sigint_received || sigpipe_received)
759 break;
761 err = got_commit_graph_iter_next(&id, graph);
762 if (err) {
763 if (err->code == GOT_ERR_ITER_COMPLETED) {
764 err = NULL;
765 break;
767 if (err->code != GOT_ERR_ITER_NEED_MORE)
768 break;
769 err = got_commit_graph_fetch_commits(graph, 1, repo);
770 if (err)
771 break;
772 else
773 continue;
775 if (id == NULL)
776 break;
778 err = got_object_open_as_commit(&commit, repo, id);
779 if (err)
780 break;
781 err = print_commit(commit, id, repo, show_patch, diff_context,
782 refs);
783 got_object_commit_close(commit);
784 if (err || (limit && --limit == 0))
785 break;
787 done:
788 got_commit_graph_close(graph);
789 return err;
792 __dead static void
793 usage_log(void)
795 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
796 "[-r repository-path] [path]\n", getprogname());
797 exit(1);
800 static const struct got_error *
801 cmd_log(int argc, char *argv[])
803 const struct got_error *error;
804 struct got_repository *repo = NULL;
805 struct got_worktree *worktree = NULL;
806 struct got_commit_object *commit = NULL;
807 struct got_object_id *id = NULL;
808 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
809 char *start_commit = NULL;
810 int diff_context = 3, ch;
811 int show_patch = 0, limit = 0, first_parent_traversal = 0;
812 const char *errstr;
813 struct got_reflist_head refs;
815 SIMPLEQ_INIT(&refs);
817 #ifndef PROFILE
818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
819 NULL)
820 == -1)
821 err(1, "pledge");
822 #endif
824 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
825 switch (ch) {
826 case 'p':
827 show_patch = 1;
828 break;
829 case 'c':
830 start_commit = optarg;
831 break;
832 case 'C':
833 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
834 &errstr);
835 if (errstr != NULL)
836 err(1, "-C option %s", errstr);
837 break;
838 case 'l':
839 limit = strtonum(optarg, 1, INT_MAX, &errstr);
840 if (errstr != NULL)
841 err(1, "-l option %s", errstr);
842 break;
843 case 'f':
844 first_parent_traversal = 1;
845 break;
846 case 'r':
847 repo_path = realpath(optarg, NULL);
848 if (repo_path == NULL)
849 err(1, "-r option");
850 break;
851 default:
852 usage_log();
853 /* NOTREACHED */
857 argc -= optind;
858 argv += optind;
860 cwd = getcwd(NULL, 0);
861 if (cwd == NULL) {
862 error = got_error_from_errno();
863 goto done;
866 error = got_worktree_open(&worktree, cwd);
867 if (error && error->code != GOT_ERR_NOT_WORKTREE)
868 goto done;
869 error = NULL;
871 if (argc == 0) {
872 path = strdup("");
873 if (path == NULL) {
874 error = got_error_from_errno();
875 goto done;
877 } else if (argc == 1) {
878 if (worktree) {
879 error = got_worktree_resolve_path(&path, worktree,
880 argv[0]);
881 if (error)
882 goto done;
883 } else {
884 path = strdup(argv[0]);
885 if (path == NULL) {
886 error = got_error_from_errno();
887 goto done;
890 } else
891 usage_log();
893 repo_path = worktree ?
894 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
895 if (repo_path == NULL) {
896 error = got_error_from_errno();
897 goto done;
900 error = got_repo_open(&repo, repo_path);
901 if (error != NULL)
902 goto done;
904 error = apply_unveil(got_repo_get_path(repo), 1,
905 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
906 if (error)
907 goto done;
909 if (start_commit == NULL) {
910 struct got_reference *head_ref;
911 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
912 if (error != NULL)
913 return error;
914 error = got_ref_resolve(&id, repo, head_ref);
915 got_ref_close(head_ref);
916 if (error != NULL)
917 return error;
918 error = got_object_open_as_commit(&commit, repo, id);
919 } else {
920 struct got_reference *ref;
921 error = got_ref_open(&ref, repo, start_commit);
922 if (error == NULL) {
923 int obj_type;
924 error = got_ref_resolve(&id, repo, ref);
925 got_ref_close(ref);
926 if (error != NULL)
927 goto done;
928 error = got_object_get_type(&obj_type, repo, id);
929 if (error != NULL)
930 goto done;
931 if (obj_type == GOT_OBJ_TYPE_TAG) {
932 struct got_tag_object *tag;
933 error = got_object_open_as_tag(&tag, repo, id);
934 if (error != NULL)
935 goto done;
936 if (got_object_tag_get_object_type(tag) !=
937 GOT_OBJ_TYPE_COMMIT) {
938 got_object_tag_close(tag);
939 error = got_error(GOT_ERR_OBJ_TYPE);
940 goto done;
942 free(id);
943 id = got_object_id_dup(
944 got_object_tag_get_object_id(tag));
945 if (id == NULL)
946 error = got_error_from_errno();
947 got_object_tag_close(tag);
948 if (error)
949 goto done;
950 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
951 error = got_error(GOT_ERR_OBJ_TYPE);
952 goto done;
954 error = got_object_open_as_commit(&commit, repo, id);
955 if (error != NULL)
956 goto done;
958 if (commit == NULL) {
959 error = got_object_resolve_id_str(&id, repo,
960 start_commit);
961 if (error != NULL)
962 return error;
965 if (error != NULL)
966 goto done;
968 error = got_repo_map_path(&in_repo_path, repo, path, 1);
969 if (error != NULL)
970 goto done;
971 if (in_repo_path) {
972 free(path);
973 path = in_repo_path;
976 error = got_ref_list(&refs, repo);
977 if (error)
978 goto done;
980 error = print_commits(id, repo, path, show_patch,
981 diff_context, limit, first_parent_traversal, &refs);
982 done:
983 free(path);
984 free(repo_path);
985 free(cwd);
986 free(id);
987 if (worktree)
988 got_worktree_close(worktree);
989 if (repo) {
990 const struct got_error *repo_error;
991 repo_error = got_repo_close(repo);
992 if (error == NULL)
993 error = repo_error;
995 got_ref_list_free(&refs);
996 return error;
999 __dead static void
1000 usage_diff(void)
1002 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1003 "[object1 object2 | path]\n", getprogname());
1004 exit(1);
1007 struct print_diff_arg {
1008 struct got_repository *repo;
1009 struct got_worktree *worktree;
1010 int diff_context;
1011 const char *id_str;
1012 int header_shown;
1015 static const struct got_error *
1016 print_diff(void *arg, unsigned char status, const char *path,
1017 struct got_object_id *id)
1019 struct print_diff_arg *a = arg;
1020 const struct got_error *err = NULL;
1021 struct got_blob_object *blob1 = NULL;
1022 FILE *f2 = NULL;
1023 char *abspath = NULL;
1024 struct stat sb;
1026 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1027 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1028 return NULL;
1030 if (!a->header_shown) {
1031 printf("diff %s %s\n", a->id_str,
1032 got_worktree_get_root_path(a->worktree));
1033 a->header_shown = 1;
1036 if (status != GOT_STATUS_ADD) {
1037 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1038 if (err)
1039 goto done;
1043 if (status != GOT_STATUS_DELETE) {
1044 if (asprintf(&abspath, "%s/%s",
1045 got_worktree_get_root_path(a->worktree), path) == -1) {
1046 err = got_error_from_errno();
1047 goto done;
1050 f2 = fopen(abspath, "r");
1051 if (f2 == NULL) {
1052 err = got_error_from_errno();
1053 goto done;
1055 if (lstat(abspath, &sb) == -1) {
1056 err = got_error_from_errno();
1057 goto done;
1059 } else
1060 sb.st_size = 0;
1062 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1063 stdout);
1064 done:
1065 if (blob1)
1066 got_object_blob_close(blob1);
1067 if (f2 && fclose(f2) != 0 && err == NULL)
1068 err = got_error_from_errno();
1069 free(abspath);
1070 return err;
1073 static const struct got_error *
1074 cmd_diff(int argc, char *argv[])
1076 const struct got_error *error;
1077 struct got_repository *repo = NULL;
1078 struct got_worktree *worktree = NULL;
1079 char *cwd = NULL, *repo_path = NULL;
1080 struct got_object_id *id1 = NULL, *id2 = NULL;
1081 char *id_str1 = NULL, *id_str2 = NULL;
1082 int type1, type2;
1083 int diff_context = 3, ch;
1084 const char *errstr;
1085 char *path = NULL;
1087 #ifndef PROFILE
1088 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1089 NULL) == -1)
1090 err(1, "pledge");
1091 #endif
1093 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1094 switch (ch) {
1095 case 'C':
1096 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1097 if (errstr != NULL)
1098 err(1, "-C option %s", errstr);
1099 break;
1100 case 'r':
1101 repo_path = realpath(optarg, NULL);
1102 if (repo_path == NULL)
1103 err(1, "-r option");
1104 break;
1105 default:
1106 usage_diff();
1107 /* NOTREACHED */
1111 argc -= optind;
1112 argv += optind;
1114 cwd = getcwd(NULL, 0);
1115 if (cwd == NULL) {
1116 error = got_error_from_errno();
1117 goto done;
1119 error = got_worktree_open(&worktree, cwd);
1120 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1121 goto done;
1122 if (argc <= 1) {
1123 if (worktree == NULL) {
1124 error = got_error(GOT_ERR_NOT_WORKTREE);
1125 goto done;
1127 if (repo_path)
1128 errx(1,
1129 "-r option can't be used when diffing a work tree");
1130 repo_path = strdup(got_worktree_get_repo_path(worktree));
1131 if (repo_path == NULL) {
1132 error = got_error_from_errno();
1133 goto done;
1135 if (argc == 1) {
1136 error = got_worktree_resolve_path(&path, worktree,
1137 argv[0]);
1138 if (error)
1139 goto done;
1140 } else {
1141 path = strdup("");
1142 if (path == NULL) {
1143 error = got_error_from_errno();
1144 goto done;
1147 } else if (argc == 2) {
1148 id_str1 = argv[0];
1149 id_str2 = argv[1];
1150 } else
1151 usage_diff();
1153 if (repo_path == NULL) {
1154 repo_path = getcwd(NULL, 0);
1155 if (repo_path == NULL)
1156 return got_error_from_errno();
1159 error = got_repo_open(&repo, repo_path);
1160 free(repo_path);
1161 if (error != NULL)
1162 goto done;
1164 error = apply_unveil(got_repo_get_path(repo), 1,
1165 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1166 if (error)
1167 goto done;
1169 if (worktree) {
1170 struct print_diff_arg arg;
1171 char *id_str;
1172 error = got_object_id_str(&id_str,
1173 got_worktree_get_base_commit_id(worktree));
1174 if (error)
1175 goto done;
1176 arg.repo = repo;
1177 arg.worktree = worktree;
1178 arg.diff_context = diff_context;
1179 arg.id_str = id_str;
1180 arg.header_shown = 0;
1182 error = got_worktree_status(worktree, path, repo, print_diff,
1183 &arg, check_cancelled, NULL);
1184 free(id_str);
1185 goto done;
1188 error = got_object_resolve_id_str(&id1, repo, id_str1);
1189 if (error)
1190 goto done;
1192 error = got_object_resolve_id_str(&id2, repo, id_str2);
1193 if (error)
1194 goto done;
1196 error = got_object_get_type(&type1, repo, id1);
1197 if (error)
1198 goto done;
1200 error = got_object_get_type(&type2, repo, id2);
1201 if (error)
1202 goto done;
1204 if (type1 != type2) {
1205 error = got_error(GOT_ERR_OBJ_TYPE);
1206 goto done;
1209 switch (type1) {
1210 case GOT_OBJ_TYPE_BLOB:
1211 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1212 diff_context, repo, stdout);
1213 break;
1214 case GOT_OBJ_TYPE_TREE:
1215 error = got_diff_objects_as_trees(id1, id2, "", "",
1216 diff_context, repo, stdout);
1217 break;
1218 case GOT_OBJ_TYPE_COMMIT:
1219 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1220 id_str2);
1221 error = got_diff_objects_as_commits(id1, id2, diff_context,
1222 repo, stdout);
1223 break;
1224 default:
1225 error = got_error(GOT_ERR_OBJ_TYPE);
1228 done:
1229 free(id1);
1230 free(id2);
1231 free(path);
1232 if (worktree)
1233 got_worktree_close(worktree);
1234 if (repo) {
1235 const struct got_error *repo_error;
1236 repo_error = got_repo_close(repo);
1237 if (error == NULL)
1238 error = repo_error;
1240 return error;
1243 __dead static void
1244 usage_blame(void)
1246 fprintf(stderr,
1247 "usage: %s blame [-c commit] [-r repository-path] path\n",
1248 getprogname());
1249 exit(1);
1252 static const struct got_error *
1253 cmd_blame(int argc, char *argv[])
1255 const struct got_error *error;
1256 struct got_repository *repo = NULL;
1257 struct got_worktree *worktree = NULL;
1258 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1259 struct got_object_id *commit_id = NULL;
1260 char *commit_id_str = NULL;
1261 int ch;
1263 #ifndef PROFILE
1264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1265 NULL) == -1)
1266 err(1, "pledge");
1267 #endif
1269 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1270 switch (ch) {
1271 case 'c':
1272 commit_id_str = optarg;
1273 break;
1274 case 'r':
1275 repo_path = realpath(optarg, NULL);
1276 if (repo_path == NULL)
1277 err(1, "-r option");
1278 break;
1279 default:
1280 usage_blame();
1281 /* NOTREACHED */
1285 argc -= optind;
1286 argv += optind;
1288 if (argc == 1)
1289 path = argv[0];
1290 else
1291 usage_blame();
1293 cwd = getcwd(NULL, 0);
1294 if (cwd == NULL) {
1295 error = got_error_from_errno();
1296 goto done;
1298 if (repo_path == NULL) {
1299 error = got_worktree_open(&worktree, cwd);
1300 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1301 goto done;
1302 else
1303 error = NULL;
1304 if (worktree) {
1305 repo_path =
1306 strdup(got_worktree_get_repo_path(worktree));
1307 if (repo_path == NULL)
1308 error = got_error_from_errno();
1309 if (error)
1310 goto done;
1311 } else {
1312 repo_path = strdup(cwd);
1313 if (repo_path == NULL) {
1314 error = got_error_from_errno();
1315 goto done;
1320 error = got_repo_open(&repo, repo_path);
1321 if (error != NULL)
1322 goto done;
1324 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1325 if (error)
1326 goto done;
1328 if (worktree) {
1329 const char *prefix = got_worktree_get_path_prefix(worktree);
1330 char *p, *worktree_subdir = cwd +
1331 strlen(got_worktree_get_root_path(worktree));
1332 if (asprintf(&p, "%s%s%s%s%s",
1333 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1334 worktree_subdir, worktree_subdir[0] ? "/" : "",
1335 path) == -1) {
1336 error = got_error_from_errno();
1337 goto done;
1339 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1340 free(p);
1341 } else {
1342 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1344 if (error)
1345 goto done;
1347 if (commit_id_str == NULL) {
1348 struct got_reference *head_ref;
1349 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1350 if (error != NULL)
1351 goto done;
1352 error = got_ref_resolve(&commit_id, repo, head_ref);
1353 got_ref_close(head_ref);
1354 if (error != NULL)
1355 goto done;
1356 } else {
1357 error = got_object_resolve_id_str(&commit_id, repo,
1358 commit_id_str);
1359 if (error != NULL)
1360 goto done;
1363 error = got_blame(in_repo_path, commit_id, repo, stdout);
1364 done:
1365 free(in_repo_path);
1366 free(repo_path);
1367 free(cwd);
1368 free(commit_id);
1369 if (worktree)
1370 got_worktree_close(worktree);
1371 if (repo) {
1372 const struct got_error *repo_error;
1373 repo_error = got_repo_close(repo);
1374 if (error == NULL)
1375 error = repo_error;
1377 return error;
1380 __dead static void
1381 usage_tree(void)
1383 fprintf(stderr,
1384 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1385 getprogname());
1386 exit(1);
1389 static void
1390 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1391 const char *root_path)
1393 int is_root_path = (strcmp(path, root_path) == 0);
1395 path += strlen(root_path);
1396 while (path[0] == '/')
1397 path++;
1399 printf("%s%s%s%s%s\n", id ? id : "", path,
1400 is_root_path ? "" : "/", te->name,
1401 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1404 static const struct got_error *
1405 print_tree(const char *path, struct got_object_id *commit_id,
1406 int show_ids, int recurse, const char *root_path,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_object_id *tree_id = NULL;
1411 struct got_tree_object *tree = NULL;
1412 const struct got_tree_entries *entries;
1413 struct got_tree_entry *te;
1415 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1416 if (err)
1417 goto done;
1419 err = got_object_open_as_tree(&tree, repo, tree_id);
1420 if (err)
1421 goto done;
1422 entries = got_object_tree_get_entries(tree);
1423 te = SIMPLEQ_FIRST(&entries->head);
1424 while (te) {
1425 char *id = NULL;
1427 if (sigint_received || sigpipe_received)
1428 break;
1430 if (show_ids) {
1431 char *id_str;
1432 err = got_object_id_str(&id_str, te->id);
1433 if (err)
1434 goto done;
1435 if (asprintf(&id, "%s ", id_str) == -1) {
1436 err = got_error_from_errno();
1437 free(id_str);
1438 goto done;
1440 free(id_str);
1442 print_entry(te, id, path, root_path);
1443 free(id);
1445 if (recurse && S_ISDIR(te->mode)) {
1446 char *child_path;
1447 if (asprintf(&child_path, "%s%s%s", path,
1448 path[0] == '/' && path[1] == '\0' ? "" : "/",
1449 te->name) == -1) {
1450 err = got_error_from_errno();
1451 goto done;
1453 err = print_tree(child_path, commit_id, show_ids, 1,
1454 root_path, repo);
1455 free(child_path);
1456 if (err)
1457 goto done;
1460 te = SIMPLEQ_NEXT(te, entry);
1462 done:
1463 if (tree)
1464 got_object_tree_close(tree);
1465 free(tree_id);
1466 return err;
1469 static const struct got_error *
1470 cmd_tree(int argc, char *argv[])
1472 const struct got_error *error;
1473 struct got_repository *repo = NULL;
1474 struct got_worktree *worktree = NULL;
1475 const char *path;
1476 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1477 struct got_object_id *commit_id = NULL;
1478 char *commit_id_str = NULL;
1479 int show_ids = 0, recurse = 0;
1480 int ch;
1482 #ifndef PROFILE
1483 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1484 NULL) == -1)
1485 err(1, "pledge");
1486 #endif
1488 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1489 switch (ch) {
1490 case 'c':
1491 commit_id_str = optarg;
1492 break;
1493 case 'r':
1494 repo_path = realpath(optarg, NULL);
1495 if (repo_path == NULL)
1496 err(1, "-r option");
1497 break;
1498 case 'i':
1499 show_ids = 1;
1500 break;
1501 case 'R':
1502 recurse = 1;
1503 break;
1504 default:
1505 usage_tree();
1506 /* NOTREACHED */
1510 argc -= optind;
1511 argv += optind;
1513 if (argc == 1)
1514 path = argv[0];
1515 else if (argc > 1)
1516 usage_tree();
1517 else
1518 path = NULL;
1520 cwd = getcwd(NULL, 0);
1521 if (cwd == NULL) {
1522 error = got_error_from_errno();
1523 goto done;
1525 if (repo_path == NULL) {
1526 error = got_worktree_open(&worktree, cwd);
1527 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1528 goto done;
1529 else
1530 error = NULL;
1531 if (worktree) {
1532 repo_path =
1533 strdup(got_worktree_get_repo_path(worktree));
1534 if (repo_path == NULL)
1535 error = got_error_from_errno();
1536 if (error)
1537 goto done;
1538 } else {
1539 repo_path = strdup(cwd);
1540 if (repo_path == NULL) {
1541 error = got_error_from_errno();
1542 goto done;
1547 error = got_repo_open(&repo, repo_path);
1548 if (error != NULL)
1549 goto done;
1551 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1552 if (error)
1553 goto done;
1555 if (path == NULL) {
1556 if (worktree) {
1557 char *p, *worktree_subdir = cwd +
1558 strlen(got_worktree_get_root_path(worktree));
1559 if (asprintf(&p, "%s/%s",
1560 got_worktree_get_path_prefix(worktree),
1561 worktree_subdir) == -1) {
1562 error = got_error_from_errno();
1563 goto done;
1565 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1566 free(p);
1567 if (error)
1568 goto done;
1569 } else
1570 path = "/";
1572 if (in_repo_path == NULL) {
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1574 if (error != NULL)
1575 goto done;
1578 if (commit_id_str == NULL) {
1579 struct got_reference *head_ref;
1580 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1581 if (error != NULL)
1582 goto done;
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1584 got_ref_close(head_ref);
1585 if (error != NULL)
1586 goto done;
1587 } else {
1588 error = got_object_resolve_id_str(&commit_id, repo,
1589 commit_id_str);
1590 if (error != NULL)
1591 goto done;
1594 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1595 in_repo_path, repo);
1596 done:
1597 free(in_repo_path);
1598 free(repo_path);
1599 free(cwd);
1600 free(commit_id);
1601 if (worktree)
1602 got_worktree_close(worktree);
1603 if (repo) {
1604 const struct got_error *repo_error;
1605 repo_error = got_repo_close(repo);
1606 if (error == NULL)
1607 error = repo_error;
1609 return error;
1612 __dead static void
1613 usage_status(void)
1615 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1616 exit(1);
1619 static const struct got_error *
1620 print_status(void *arg, unsigned char status, const char *path,
1621 struct got_object_id *id)
1623 printf("%c %s\n", status, path);
1624 return NULL;
1627 static const struct got_error *
1628 cmd_status(int argc, char *argv[])
1630 const struct got_error *error = NULL;
1631 struct got_repository *repo = NULL;
1632 struct got_worktree *worktree = NULL;
1633 char *cwd = NULL, *path = NULL;
1634 int ch;
1636 while ((ch = getopt(argc, argv, "")) != -1) {
1637 switch (ch) {
1638 default:
1639 usage_status();
1640 /* NOTREACHED */
1644 argc -= optind;
1645 argv += optind;
1647 #ifndef PROFILE
1648 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1649 NULL) == -1)
1650 err(1, "pledge");
1651 #endif
1652 cwd = getcwd(NULL, 0);
1653 if (cwd == NULL) {
1654 error = got_error_from_errno();
1655 goto done;
1658 error = got_worktree_open(&worktree, cwd);
1659 if (error != NULL)
1660 goto done;
1662 if (argc == 0) {
1663 path = strdup("");
1664 if (path == NULL) {
1665 error = got_error_from_errno();
1666 goto done;
1668 } else if (argc == 1) {
1669 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1670 if (error)
1671 goto done;
1672 } else
1673 usage_status();
1675 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1676 if (error != NULL)
1677 goto done;
1679 error = apply_unveil(got_repo_get_path(repo), 1,
1680 got_worktree_get_root_path(worktree), 0);
1681 if (error)
1682 goto done;
1684 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1685 check_cancelled, NULL);
1686 done:
1687 free(cwd);
1688 free(path);
1689 return error;
1692 __dead static void
1693 usage_ref(void)
1695 fprintf(stderr,
1696 "usage: %s ref [-r repository] -l | -d name | name object\n",
1697 getprogname());
1698 exit(1);
1701 static const struct got_error *
1702 list_refs(struct got_repository *repo)
1704 static const struct got_error *err = NULL;
1705 struct got_reflist_head refs;
1706 struct got_reflist_entry *re;
1708 SIMPLEQ_INIT(&refs);
1709 err = got_ref_list(&refs, repo);
1710 if (err)
1711 return err;
1713 SIMPLEQ_FOREACH(re, &refs, entry) {
1714 char *refstr;
1715 refstr = got_ref_to_str(re->ref);
1716 if (refstr == NULL)
1717 return got_error_from_errno();
1718 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1719 free(refstr);
1722 got_ref_list_free(&refs);
1723 return NULL;
1726 static const struct got_error *
1727 delete_ref(struct got_repository *repo, const char *refname)
1729 const struct got_error *err = NULL;
1730 struct got_reference *ref;
1732 err = got_ref_open(&ref, repo, refname);
1733 if (err)
1734 return err;
1736 err = got_ref_delete(ref, repo);
1737 got_ref_close(ref);
1738 return err;
1741 static const struct got_error *
1742 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1744 const struct got_error *err = NULL;
1745 struct got_object_id *id;
1746 struct got_reference *ref = NULL;
1748 err = got_object_resolve_id_str(&id, repo, id_str);
1749 if (err)
1750 return err;
1752 err = got_ref_alloc(&ref, refname, id);
1753 if (err)
1754 goto done;
1756 err = got_ref_write(ref, repo);
1757 done:
1758 if (ref)
1759 got_ref_close(ref);
1760 free(id);
1761 return err;
1764 static const struct got_error *
1765 cmd_ref(int argc, char *argv[])
1767 const struct got_error *error = NULL;
1768 struct got_repository *repo = NULL;
1769 struct got_worktree *worktree = NULL;
1770 char *cwd = NULL, *repo_path = NULL;
1771 int ch, do_list = 0;
1772 const char *delref = NULL;
1774 /* TODO: Add -s option for adding symbolic references. */
1775 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1776 switch (ch) {
1777 case 'd':
1778 delref = optarg;
1779 break;
1780 case 'r':
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 break;
1785 case 'l':
1786 do_list = 1;
1787 break;
1788 default:
1789 usage_ref();
1790 /* NOTREACHED */
1794 if (do_list && delref)
1795 errx(1, "-l and -d options are mutually exclusive\n");
1797 argc -= optind;
1798 argv += optind;
1800 if (do_list || delref) {
1801 if (argc > 0)
1802 usage_ref();
1803 } else if (argc != 2)
1804 usage_ref();
1806 #ifndef PROFILE
1807 if (do_list) {
1808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1809 NULL) == -1)
1810 err(1, "pledge");
1811 } else {
1812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1813 "sendfd unveil", NULL) == -1)
1814 err(1, "pledge");
1816 #endif
1817 cwd = getcwd(NULL, 0);
1818 if (cwd == NULL) {
1819 error = got_error_from_errno();
1820 goto done;
1823 if (repo_path == NULL) {
1824 error = got_worktree_open(&worktree, cwd);
1825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1826 goto done;
1827 else
1828 error = NULL;
1829 if (worktree) {
1830 repo_path =
1831 strdup(got_worktree_get_repo_path(worktree));
1832 if (repo_path == NULL)
1833 error = got_error_from_errno();
1834 if (error)
1835 goto done;
1836 } else {
1837 repo_path = strdup(cwd);
1838 if (repo_path == NULL) {
1839 error = got_error_from_errno();
1840 goto done;
1845 error = got_repo_open(&repo, repo_path);
1846 if (error != NULL)
1847 goto done;
1849 error = apply_unveil(got_repo_get_path(repo), do_list,
1850 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1851 if (error)
1852 goto done;
1854 if (do_list)
1855 error = list_refs(repo);
1856 else if (delref)
1857 error = delete_ref(repo, delref);
1858 else
1859 error = add_ref(repo, argv[0], argv[1]);
1860 done:
1861 if (repo)
1862 got_repo_close(repo);
1863 if (worktree)
1864 got_worktree_close(worktree);
1865 free(cwd);
1866 free(repo_path);
1867 return error;
1870 __dead static void
1871 usage_add(void)
1873 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1874 exit(1);
1877 static const struct got_error *
1878 cmd_add(int argc, char *argv[])
1880 const struct got_error *error = NULL;
1881 struct got_repository *repo = NULL;
1882 struct got_worktree *worktree = NULL;
1883 char *cwd = NULL, *path = NULL, *relpath = NULL;
1884 int ch;
1886 while ((ch = getopt(argc, argv, "")) != -1) {
1887 switch (ch) {
1888 default:
1889 usage_add();
1890 /* NOTREACHED */
1894 argc -= optind;
1895 argv += optind;
1897 if (argc != 1)
1898 usage_add();
1900 path = realpath(argv[0], NULL);
1901 if (path == NULL) {
1902 error = got_error_from_errno();
1903 goto done;
1906 cwd = getcwd(NULL, 0);
1907 if (cwd == NULL) {
1908 error = got_error_from_errno();
1909 goto done;
1911 error = got_worktree_open(&worktree, cwd);
1912 if (error)
1913 goto done;
1915 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1916 if (error != NULL)
1917 goto done;
1919 error = apply_unveil(got_repo_get_path(repo), 1,
1920 got_worktree_get_root_path(worktree), 0);
1921 if (error)
1922 goto done;
1924 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1925 repo);
1926 if (error)
1927 goto done;
1928 done:
1929 if (repo)
1930 got_repo_close(repo);
1931 if (worktree)
1932 got_worktree_close(worktree);
1933 free(path);
1934 free(relpath);
1935 free(cwd);
1936 return error;
1939 __dead static void
1940 usage_rm(void)
1942 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1943 exit(1);
1946 static const struct got_error *
1947 cmd_rm(int argc, char *argv[])
1949 const struct got_error *error = NULL;
1950 struct got_worktree *worktree = NULL;
1951 struct got_repository *repo = NULL;
1952 char *cwd = NULL, *path = NULL;
1953 int ch, delete_local_mods = 0;
1955 while ((ch = getopt(argc, argv, "f")) != -1) {
1956 switch (ch) {
1957 case 'f':
1958 delete_local_mods = 1;
1959 break;
1960 default:
1961 usage_add();
1962 /* NOTREACHED */
1966 argc -= optind;
1967 argv += optind;
1969 if (argc != 1)
1970 usage_rm();
1972 path = realpath(argv[0], NULL);
1973 if (path == NULL) {
1974 error = got_error_from_errno();
1975 goto done;
1978 cwd = getcwd(NULL, 0);
1979 if (cwd == NULL) {
1980 error = got_error_from_errno();
1981 goto done;
1983 error = got_worktree_open(&worktree, cwd);
1984 if (error)
1985 goto done;
1987 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1988 if (error != NULL)
1989 goto done;
1991 error = apply_unveil(got_repo_get_path(repo), 1,
1992 got_worktree_get_root_path(worktree), 0);
1993 if (error)
1994 goto done;
1996 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1997 print_status, NULL, repo);
1998 if (error)
1999 goto done;
2000 done:
2001 if (repo)
2002 got_repo_close(repo);
2003 if (worktree)
2004 got_worktree_close(worktree);
2005 free(path);
2006 free(cwd);
2007 return error;
2010 __dead static void
2011 usage_revert(void)
2013 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2014 exit(1);
2017 static void
2018 revert_progress(void *arg, unsigned char status, const char *path)
2020 while (path[0] == '/')
2021 path++;
2022 printf("%c %s\n", status, path);
2025 static const struct got_error *
2026 cmd_revert(int argc, char *argv[])
2028 const struct got_error *error = NULL;
2029 struct got_worktree *worktree = NULL;
2030 struct got_repository *repo = NULL;
2031 char *cwd = NULL, *path = NULL;
2032 int ch;
2034 while ((ch = getopt(argc, argv, "")) != -1) {
2035 switch (ch) {
2036 default:
2037 usage_revert();
2038 /* NOTREACHED */
2042 argc -= optind;
2043 argv += optind;
2045 if (argc != 1)
2046 usage_revert();
2048 path = realpath(argv[0], NULL);
2049 if (path == NULL) {
2050 error = got_error_from_errno();
2051 goto done;
2054 cwd = getcwd(NULL, 0);
2055 if (cwd == NULL) {
2056 error = got_error_from_errno();
2057 goto done;
2059 error = got_worktree_open(&worktree, cwd);
2060 if (error)
2061 goto done;
2063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2064 if (error != NULL)
2065 goto done;
2067 error = apply_unveil(got_repo_get_path(repo), 1,
2068 got_worktree_get_root_path(worktree), 0);
2069 if (error)
2070 goto done;
2072 error = got_worktree_revert(worktree, path,
2073 revert_progress, NULL, repo);
2074 if (error)
2075 goto done;
2076 done:
2077 if (repo)
2078 got_repo_close(repo);
2079 if (worktree)
2080 got_worktree_close(worktree);
2081 free(path);
2082 free(cwd);
2083 return error;
2086 __dead static void
2087 usage_commit(void)
2089 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2090 exit(1);
2093 static const struct got_error *
2094 cmd_commit(int argc, char *argv[])
2096 const struct got_error *error = NULL;
2097 struct got_worktree *worktree = NULL;
2098 struct got_repository *repo = NULL;
2099 char *cwd = NULL, *path = NULL, *id_str = NULL;
2100 struct got_object_id *id = NULL;
2101 const char *logmsg = "<no log message was specified>";
2102 const char *got_author = getenv("GOT_AUTHOR");
2103 int ch;
2105 while ((ch = getopt(argc, argv, "m:")) != -1) {
2106 switch (ch) {
2107 case 'm':
2108 logmsg = optarg;
2109 break;
2110 default:
2111 usage_commit();
2112 /* NOTREACHED */
2116 argc -= optind;
2117 argv += optind;
2119 if (argc == 1) {
2120 path = realpath(argv[0], NULL);
2121 if (path == NULL) {
2122 error = got_error_from_errno();
2123 goto done;
2125 } else if (argc != 0)
2126 usage_commit();
2128 if (got_author == NULL) {
2129 /* TODO: Look current user up in password database */
2130 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2131 goto done;
2134 cwd = getcwd(NULL, 0);
2135 if (cwd == NULL) {
2136 error = got_error_from_errno();
2137 goto done;
2139 error = got_worktree_open(&worktree, cwd);
2140 if (error)
2141 goto done;
2143 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2144 if (error != NULL)
2145 goto done;
2147 error = apply_unveil(got_repo_get_path(repo), 0,
2148 got_worktree_get_root_path(worktree), 0);
2149 if (error)
2150 goto done;
2152 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2153 logmsg, print_status, NULL, repo);
2154 if (error)
2155 goto done;
2157 error = got_object_id_str(&id_str, id);
2158 if (error)
2159 goto done;
2160 printf("created commit %s\n", id_str);
2161 done:
2162 if (repo)
2163 got_repo_close(repo);
2164 if (worktree)
2165 got_worktree_close(worktree);
2166 free(path);
2167 free(cwd);
2168 free(id_str);
2169 return error;