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"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static volatile sig_atomic_t sigint_received;
49 static volatile sig_atomic_t sigpipe_received;
51 static void
52 catch_sigint(int signo)
53 {
54 sigint_received = 1;
55 }
57 static void
58 catch_sigpipe(int signo)
59 {
60 sigpipe_received = 1;
61 }
64 struct cmd {
65 const char *cmd_name;
66 const struct got_error *(*cmd_main)(int, char *[]);
67 void (*cmd_usage)(void);
68 const char *cmd_descr;
69 };
71 __dead static void usage(void);
72 __dead static void usage_checkout(void);
73 __dead static void usage_update(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
78 __dead static void usage_status(void);
79 __dead static void usage_ref(void);
80 __dead static void usage_add(void);
81 __dead static void usage_rm(void);
82 __dead static void usage_revert(void);
83 __dead static void usage_commit(void);
85 static const struct got_error* cmd_checkout(int, char *[]);
86 static const struct got_error* cmd_update(int, char *[]);
87 static const struct got_error* cmd_log(int, char *[]);
88 static const struct got_error* cmd_diff(int, char *[]);
89 static const struct got_error* cmd_blame(int, char *[]);
90 static const struct got_error* cmd_tree(int, char *[]);
91 static const struct got_error* cmd_status(int, char *[]);
92 static const struct got_error* cmd_ref(int, char *[]);
93 static const struct got_error* cmd_add(int, char *[]);
94 static const struct got_error* cmd_rm(int, char *[]);
95 static const struct got_error* cmd_revert(int, char *[]);
96 static const struct got_error* cmd_commit(int, char *[]);
98 static struct cmd got_commands[] = {
99 { "checkout", cmd_checkout, usage_checkout,
100 "check out a new work tree from a repository" },
101 { "update", cmd_update, usage_update,
102 "update a work tree to a different commit" },
103 { "log", cmd_log, usage_log,
104 "show repository history" },
105 { "diff", cmd_diff, usage_diff,
106 "compare files and directories" },
107 { "blame", cmd_blame, usage_blame,
108 "show when lines in a file were changed" },
109 { "tree", cmd_tree, usage_tree,
110 "list files and directories in repository" },
111 { "status", cmd_status, usage_status,
112 "show modification status of files" },
113 { "ref", cmd_ref, usage_ref,
114 "manage references in repository" },
115 { "add", cmd_add, usage_add,
116 "add a new file to version control" },
117 { "rm", cmd_rm, usage_rm,
118 "remove a versioned file" },
119 { "revert", cmd_revert, usage_revert,
120 "revert uncommitted changes" },
121 { "commit", cmd_commit, usage_commit,
122 "create blob from file (WIP; can't create commits yet)" },
123 };
125 int
126 main(int argc, char *argv[])
128 struct cmd *cmd;
129 unsigned int i;
130 int ch;
131 int hflag = 0;
133 setlocale(LC_CTYPE, "");
135 while ((ch = getopt(argc, argv, "h")) != -1) {
136 switch (ch) {
137 case 'h':
138 hflag = 1;
139 break;
140 default:
141 usage();
142 /* NOTREACHED */
146 argc -= optind;
147 argv += optind;
148 optind = 0;
150 if (argc <= 0)
151 usage();
153 signal(SIGINT, catch_sigint);
154 signal(SIGPIPE, catch_sigpipe);
156 for (i = 0; i < nitems(got_commands); i++) {
157 const struct got_error *error;
159 cmd = &got_commands[i];
161 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
162 continue;
164 if (hflag)
165 got_commands[i].cmd_usage();
167 error = got_commands[i].cmd_main(argc, argv);
168 if (error && !(sigint_received || sigpipe_received)) {
169 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
170 return 1;
173 return 0;
176 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
177 return 1;
180 __dead static void
181 usage(void)
183 int i;
185 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
186 "Available commands:\n", getprogname());
187 for (i = 0; i < nitems(got_commands); i++) {
188 struct cmd *cmd = &got_commands[i];
189 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
191 exit(1);
194 static const struct got_error *
195 apply_unveil(const char *repo_path, int repo_read_only,
196 const char *worktree_path)
198 const struct got_error *error;
200 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
201 return got_error_from_errno();
203 if (worktree_path && unveil(worktree_path, "rwc") != 0)
204 return got_error_from_errno();
206 if (unveil("/tmp", "rwc") != 0)
207 return got_error_from_errno();
209 error = got_privsep_unveil_exec_helpers();
210 if (error != NULL)
211 return error;
213 if (unveil(NULL, NULL) != 0)
214 return got_error_from_errno();
216 return NULL;
219 __dead static void
220 usage_checkout(void)
222 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
223 "[worktree-path]\n", getprogname());
224 exit(1);
227 static void
228 checkout_progress(void *arg, unsigned char status, const char *path)
230 char *worktree_path = arg;
232 while (path[0] == '/')
233 path++;
235 printf("%c %s/%s\n", status, worktree_path, path);
238 static const struct got_error *
239 check_cancelled(void *arg)
241 if (sigint_received || sigpipe_received)
242 return got_error(GOT_ERR_CANCELLED);
243 return NULL;
246 static const struct got_error *
247 check_ancestry(struct got_worktree *worktree, struct got_object_id *commit_id,
248 struct got_repository *repo)
250 const struct got_error *err;
251 struct got_reference *head_ref = NULL;
252 struct got_object_id *head_commit_id = NULL;
253 struct got_commit_graph *graph = NULL;
255 head_ref = got_worktree_get_head_ref(worktree);
256 if (head_ref == NULL)
257 return got_error_from_errno();
259 /* TODO: Check the reflog. The head ref may have been rebased. */
260 err = got_ref_resolve(&head_commit_id, repo, head_ref);
261 if (err)
262 goto done;
264 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
265 if (err)
266 goto done;
268 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
269 if (err)
270 goto done;
271 while (1) {
272 struct got_object_id *id;
274 if (sigint_received || sigpipe_received)
275 break;
277 err = got_commit_graph_iter_next(&id, graph);
278 if (err) {
279 if (err->code == GOT_ERR_ITER_COMPLETED) {
280 err = got_error(GOT_ERR_ANCESTRY);
281 break;
283 if (err->code != GOT_ERR_ITER_NEED_MORE)
284 break;
285 err = got_commit_graph_fetch_commits(graph, 1, repo);
286 if (err)
287 break;
288 else
289 continue;
291 if (id == NULL)
292 break;
293 if (got_object_id_cmp(id, commit_id) == 0)
294 break;
296 done:
297 if (head_ref)
298 got_ref_close(head_ref);
299 if (graph)
300 got_commit_graph_close(graph);
301 return err;
305 static const struct got_error *
306 cmd_checkout(int argc, char *argv[])
308 const struct got_error *error = NULL;
309 struct got_repository *repo = NULL;
310 struct got_reference *head_ref = NULL;
311 struct got_worktree *worktree = NULL;
312 char *repo_path = NULL;
313 char *worktree_path = NULL;
314 const char *path_prefix = "";
315 char *commit_id_str = NULL;
316 int ch, same_path_prefix;
318 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
319 switch (ch) {
320 case 'c':
321 commit_id_str = strdup(optarg);
322 if (commit_id_str == NULL)
323 return got_error_from_errno();
324 break;
325 case 'p':
326 path_prefix = optarg;
327 break;
328 default:
329 usage_checkout();
330 /* NOTREACHED */
334 argc -= optind;
335 argv += optind;
337 #ifndef PROFILE
338 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
339 "unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc == 1) {
343 char *cwd, *base, *dotgit;
344 repo_path = realpath(argv[0], NULL);
345 if (repo_path == NULL)
346 return got_error_from_errno();
347 cwd = getcwd(NULL, 0);
348 if (cwd == NULL) {
349 error = got_error_from_errno();
350 goto done;
352 if (path_prefix[0])
353 base = basename(path_prefix);
354 else
355 base = basename(repo_path);
356 if (base == NULL) {
357 error = got_error_from_errno();
358 goto done;
360 dotgit = strstr(base, ".git");
361 if (dotgit)
362 *dotgit = '\0';
363 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
364 error = got_error_from_errno();
365 free(cwd);
366 goto done;
368 free(cwd);
369 } else if (argc == 2) {
370 repo_path = realpath(argv[0], NULL);
371 if (repo_path == NULL) {
372 error = got_error_from_errno();
373 goto done;
375 worktree_path = realpath(argv[1], NULL);
376 if (worktree_path == NULL) {
377 error = got_error_from_errno();
378 goto done;
380 } else
381 usage_checkout();
383 error = got_repo_open(&repo, repo_path);
384 if (error != NULL)
385 goto done;
387 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
388 if (error)
389 goto done;
391 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
392 if (error != NULL)
393 goto done;
395 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
396 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
397 goto done;
399 error = got_worktree_open(&worktree, worktree_path);
400 if (error != NULL)
401 goto done;
403 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
404 path_prefix);
405 if (error != NULL)
406 goto done;
407 if (!same_path_prefix) {
408 error = got_error(GOT_ERR_PATH_PREFIX);
409 goto done;
412 if (commit_id_str) {
413 struct got_object_id *commit_id;
414 error = got_object_resolve_id_str(&commit_id, repo,
415 commit_id_str);
416 if (error != NULL)
417 goto done;
418 error = check_ancestry(worktree, commit_id, repo);
419 if (error != NULL) {
420 free(commit_id);
421 goto done;
423 error = got_worktree_set_base_commit_id(worktree, repo,
424 commit_id);
425 free(commit_id);
426 if (error)
427 goto done;
430 error = got_worktree_checkout_files(worktree, "", repo,
431 checkout_progress, worktree_path, check_cancelled, NULL);
432 if (error != NULL)
433 goto done;
435 printf("Now shut up and hack\n");
437 done:
438 free(commit_id_str);
439 free(repo_path);
440 free(worktree_path);
441 return error;
444 __dead static void
445 usage_update(void)
447 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
448 getprogname());
449 exit(1);
452 static void
453 update_progress(void *arg, unsigned char status, const char *path)
455 int *did_something = arg;
457 if (status == GOT_STATUS_EXISTS)
458 return;
460 *did_something = 1;
461 while (path[0] == '/')
462 path++;
463 printf("%c %s\n", status, path);
466 static const struct got_error *
467 cmd_update(int argc, char *argv[])
469 const struct got_error *error = NULL;
470 struct got_repository *repo = NULL;
471 struct got_worktree *worktree = NULL;
472 char *worktree_path = NULL, *path = NULL;
473 struct got_object_id *commit_id = NULL;
474 char *commit_id_str = NULL;
475 int ch, did_something = 0;
477 while ((ch = getopt(argc, argv, "c:")) != -1) {
478 switch (ch) {
479 case 'c':
480 commit_id_str = strdup(optarg);
481 if (commit_id_str == NULL)
482 return got_error_from_errno();
483 break;
484 default:
485 usage_update();
486 /* NOTREACHED */
490 argc -= optind;
491 argv += optind;
493 #ifndef PROFILE
494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
495 "unveil", NULL) == -1)
496 err(1, "pledge");
497 #endif
498 worktree_path = getcwd(NULL, 0);
499 if (worktree_path == NULL) {
500 error = got_error_from_errno();
501 goto done;
503 error = got_worktree_open(&worktree, worktree_path);
504 if (error)
505 goto done;
507 if (argc == 0) {
508 path = strdup("");
509 if (path == NULL) {
510 error = got_error_from_errno();
511 goto done;
513 } else if (argc == 1) {
514 error = got_worktree_resolve_path(&path, worktree, argv[0]);
515 if (error)
516 goto done;
517 } else
518 usage_update();
520 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
521 if (error != NULL)
522 goto done;
524 error = apply_unveil(got_repo_get_path(repo), 0,
525 got_worktree_get_root_path(worktree));
526 if (error)
527 goto done;
529 if (commit_id_str == NULL) {
530 struct got_reference *head_ref;
531 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
532 if (error != NULL)
533 goto done;
534 error = got_ref_resolve(&commit_id, repo, head_ref);
535 if (error != NULL)
536 goto done;
537 error = got_object_id_str(&commit_id_str, commit_id);
538 if (error != NULL)
539 goto done;
540 } else {
541 error = got_object_resolve_id_str(&commit_id, repo,
542 commit_id_str);
543 if (error != NULL)
544 goto done;
547 error = check_ancestry(worktree, commit_id, repo);
548 if (error != NULL)
549 goto done;
551 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
552 commit_id) != 0) {
553 error = got_worktree_set_base_commit_id(worktree, repo,
554 commit_id);
555 if (error)
556 goto done;
559 error = got_worktree_checkout_files(worktree, path, repo,
560 update_progress, &did_something, check_cancelled, NULL);
561 if (error != NULL)
562 goto done;
564 if (did_something)
565 printf("Updated to commit %s\n", commit_id_str);
566 else
567 printf("Already up-to-date\n");
568 done:
569 free(worktree_path);
570 free(path);
571 free(commit_id);
572 free(commit_id_str);
573 return error;
576 static const struct got_error *
577 print_patch(struct got_commit_object *commit, struct got_object_id *id,
578 int diff_context, struct got_repository *repo)
580 const struct got_error *err = NULL;
581 struct got_tree_object *tree1 = NULL, *tree2;
582 struct got_object_qid *qid;
583 char *id_str1 = NULL, *id_str2;
585 err = got_object_open_as_tree(&tree2, repo,
586 got_object_commit_get_tree_id(commit));
587 if (err)
588 return err;
590 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
591 if (qid != NULL) {
592 struct got_commit_object *pcommit;
594 err = got_object_open_as_commit(&pcommit, repo, qid->id);
595 if (err)
596 return err;
598 err = got_object_open_as_tree(&tree1, repo,
599 got_object_commit_get_tree_id(pcommit));
600 got_object_commit_close(pcommit);
601 if (err)
602 return err;
604 err = got_object_id_str(&id_str1, qid->id);
605 if (err)
606 return err;
609 err = got_object_id_str(&id_str2, id);
610 if (err)
611 goto done;
613 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
614 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
615 done:
616 if (tree1)
617 got_object_tree_close(tree1);
618 got_object_tree_close(tree2);
619 free(id_str1);
620 free(id_str2);
621 return err;
624 static char *
625 get_datestr(time_t *time, char *datebuf)
627 char *p, *s = ctime_r(time, datebuf);
628 p = strchr(s, '\n');
629 if (p)
630 *p = '\0';
631 return s;
634 static const struct got_error *
635 print_commit(struct got_commit_object *commit, struct got_object_id *id,
636 struct got_repository *repo, int show_patch, int diff_context,
637 struct got_reflist_head *refs)
639 const struct got_error *err = NULL;
640 char *id_str, *datestr, *logmsg0, *logmsg, *line;
641 char datebuf[26];
642 time_t committer_time;
643 const char *author, *committer;
644 char *refs_str = NULL;
645 struct got_reflist_entry *re;
647 SIMPLEQ_FOREACH(re, refs, entry) {
648 char *s;
649 const char *name;
650 if (got_object_id_cmp(re->id, id) != 0)
651 continue;
652 name = got_ref_get_name(re->ref);
653 if (strcmp(name, GOT_REF_HEAD) == 0)
654 continue;
655 if (strncmp(name, "refs/", 5) == 0)
656 name += 5;
657 if (strncmp(name, "got/", 4) == 0)
658 continue;
659 if (strncmp(name, "heads/", 6) == 0)
660 name += 6;
661 if (strncmp(name, "remotes/", 8) == 0)
662 name += 8;
663 s = refs_str;
664 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
665 name) == -1) {
666 err = got_error_from_errno();
667 free(s);
668 break;
670 free(s);
672 err = got_object_id_str(&id_str, id);
673 if (err)
674 return err;
676 printf("-----------------------------------------------\n");
677 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
678 refs_str ? refs_str : "", refs_str ? ")" : "");
679 free(id_str);
680 id_str = NULL;
681 free(refs_str);
682 refs_str = NULL;
683 printf("from: %s\n", got_object_commit_get_author(commit));
684 committer_time = got_object_commit_get_committer_time(commit);
685 datestr = get_datestr(&committer_time, datebuf);
686 printf("date: %s UTC\n", datestr);
687 author = got_object_commit_get_author(commit);
688 committer = got_object_commit_get_committer(commit);
689 if (strcmp(author, committer) != 0)
690 printf("via: %s\n", committer);
691 if (got_object_commit_get_nparents(commit) > 1) {
692 const struct got_object_id_queue *parent_ids;
693 struct got_object_qid *qid;
694 int n = 1;
695 parent_ids = got_object_commit_get_parent_ids(commit);
696 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
697 err = got_object_id_str(&id_str, qid->id);
698 if (err)
699 return err;
700 printf("parent %d: %s\n", n++, id_str);
701 free(id_str);
705 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
706 if (logmsg0 == NULL)
707 return got_error_from_errno();
709 logmsg = logmsg0;
710 do {
711 line = strsep(&logmsg, "\n");
712 if (line)
713 printf(" %s\n", line);
714 } while (line);
715 free(logmsg0);
717 if (show_patch) {
718 err = print_patch(commit, id, diff_context, repo);
719 if (err == 0)
720 printf("\n");
723 if (fflush(stdout) != 0 && err == NULL)
724 err = got_error_from_errno();
725 return err;
728 static const struct got_error *
729 print_commits(struct got_object_id *root_id, struct got_repository *repo,
730 char *path, int show_patch, int diff_context, int limit,
731 int first_parent_traversal, struct got_reflist_head *refs)
733 const struct got_error *err;
734 struct got_commit_graph *graph;
736 err = got_commit_graph_open(&graph, root_id, path,
737 first_parent_traversal, repo);
738 if (err)
739 return err;
740 err = got_commit_graph_iter_start(graph, root_id, repo);
741 if (err)
742 goto done;
743 while (1) {
744 struct got_commit_object *commit;
745 struct got_object_id *id;
747 if (sigint_received || sigpipe_received)
748 break;
750 err = got_commit_graph_iter_next(&id, graph);
751 if (err) {
752 if (err->code == GOT_ERR_ITER_COMPLETED) {
753 err = NULL;
754 break;
756 if (err->code != GOT_ERR_ITER_NEED_MORE)
757 break;
758 err = got_commit_graph_fetch_commits(graph, 1, repo);
759 if (err)
760 break;
761 else
762 continue;
764 if (id == NULL)
765 break;
767 err = got_object_open_as_commit(&commit, repo, id);
768 if (err)
769 break;
770 err = print_commit(commit, id, repo, show_patch, diff_context,
771 refs);
772 got_object_commit_close(commit);
773 if (err || (limit && --limit == 0))
774 break;
776 done:
777 got_commit_graph_close(graph);
778 return err;
781 __dead static void
782 usage_log(void)
784 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
785 "[-r repository-path] [path]\n", getprogname());
786 exit(1);
789 static const struct got_error *
790 cmd_log(int argc, char *argv[])
792 const struct got_error *error;
793 struct got_repository *repo = NULL;
794 struct got_worktree *worktree = NULL;
795 struct got_commit_object *commit = NULL;
796 struct got_object_id *id = NULL;
797 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
798 char *start_commit = NULL;
799 int diff_context = 3, ch;
800 int show_patch = 0, limit = 0, first_parent_traversal = 0;
801 const char *errstr;
802 struct got_reflist_head refs;
804 SIMPLEQ_INIT(&refs);
806 #ifndef PROFILE
807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
808 NULL)
809 == -1)
810 err(1, "pledge");
811 #endif
813 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
814 switch (ch) {
815 case 'p':
816 show_patch = 1;
817 break;
818 case 'c':
819 start_commit = optarg;
820 break;
821 case 'C':
822 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
823 &errstr);
824 if (errstr != NULL)
825 err(1, "-C option %s", errstr);
826 break;
827 case 'l':
828 limit = strtonum(optarg, 1, INT_MAX, &errstr);
829 if (errstr != NULL)
830 err(1, "-l option %s", errstr);
831 break;
832 case 'f':
833 first_parent_traversal = 1;
834 break;
835 case 'r':
836 repo_path = realpath(optarg, NULL);
837 if (repo_path == NULL)
838 err(1, "-r option");
839 break;
840 default:
841 usage_log();
842 /* NOTREACHED */
846 argc -= optind;
847 argv += optind;
849 cwd = getcwd(NULL, 0);
850 if (cwd == NULL) {
851 error = got_error_from_errno();
852 goto done;
855 error = got_worktree_open(&worktree, cwd);
856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
857 goto done;
858 error = NULL;
860 if (argc == 0) {
861 path = strdup("");
862 if (path == NULL) {
863 error = got_error_from_errno();
864 goto done;
866 } else if (argc == 1) {
867 if (worktree) {
868 error = got_worktree_resolve_path(&path, worktree,
869 argv[0]);
870 if (error)
871 goto done;
872 } else {
873 path = strdup(argv[0]);
874 if (path == NULL) {
875 error = got_error_from_errno();
876 goto done;
879 } else
880 usage_log();
882 repo_path = worktree ?
883 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
884 if (repo_path == NULL) {
885 error = got_error_from_errno();
886 goto done;
889 error = got_repo_open(&repo, repo_path);
890 if (error != NULL)
891 goto done;
893 error = apply_unveil(got_repo_get_path(repo), 1,
894 worktree ? got_worktree_get_root_path(worktree) : NULL);
895 if (error)
896 goto done;
898 if (start_commit == NULL) {
899 struct got_reference *head_ref;
900 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
901 if (error != NULL)
902 return error;
903 error = got_ref_resolve(&id, repo, head_ref);
904 got_ref_close(head_ref);
905 if (error != NULL)
906 return error;
907 error = got_object_open_as_commit(&commit, repo, id);
908 } else {
909 struct got_reference *ref;
910 error = got_ref_open(&ref, repo, start_commit);
911 if (error == NULL) {
912 int obj_type;
913 error = got_ref_resolve(&id, repo, ref);
914 got_ref_close(ref);
915 if (error != NULL)
916 goto done;
917 error = got_object_get_type(&obj_type, repo, id);
918 if (error != NULL)
919 goto done;
920 if (obj_type == GOT_OBJ_TYPE_TAG) {
921 struct got_tag_object *tag;
922 error = got_object_open_as_tag(&tag, repo, id);
923 if (error != NULL)
924 goto done;
925 if (got_object_tag_get_object_type(tag) !=
926 GOT_OBJ_TYPE_COMMIT) {
927 got_object_tag_close(tag);
928 error = got_error(GOT_ERR_OBJ_TYPE);
929 goto done;
931 free(id);
932 id = got_object_id_dup(
933 got_object_tag_get_object_id(tag));
934 if (id == NULL)
935 error = got_error_from_errno();
936 got_object_tag_close(tag);
937 if (error)
938 goto done;
939 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
940 error = got_error(GOT_ERR_OBJ_TYPE);
941 goto done;
943 error = got_object_open_as_commit(&commit, repo, id);
944 if (error != NULL)
945 goto done;
947 if (commit == NULL) {
948 error = got_object_resolve_id_str(&id, repo,
949 start_commit);
950 if (error != NULL)
951 return error;
954 if (error != NULL)
955 goto done;
957 error = got_repo_map_path(&in_repo_path, repo, path, 1);
958 if (error != NULL)
959 goto done;
960 if (in_repo_path) {
961 free(path);
962 path = in_repo_path;
965 error = got_ref_list(&refs, repo);
966 if (error)
967 goto done;
969 error = print_commits(id, repo, path, show_patch,
970 diff_context, limit, first_parent_traversal, &refs);
971 done:
972 free(path);
973 free(repo_path);
974 free(cwd);
975 free(id);
976 if (worktree)
977 got_worktree_close(worktree);
978 if (repo) {
979 const struct got_error *repo_error;
980 repo_error = got_repo_close(repo);
981 if (error == NULL)
982 error = repo_error;
984 got_ref_list_free(&refs);
985 return error;
988 __dead static void
989 usage_diff(void)
991 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
992 "[object1 object2 | path]\n", getprogname());
993 exit(1);
996 struct print_diff_arg {
997 struct got_repository *repo;
998 struct got_worktree *worktree;
999 int diff_context;
1000 const char *id_str;
1001 int header_shown;
1004 static const struct got_error *
1005 print_diff(void *arg, unsigned char status, const char *path,
1006 struct got_object_id *id)
1008 struct print_diff_arg *a = arg;
1009 const struct got_error *err = NULL;
1010 struct got_blob_object *blob1 = NULL;
1011 FILE *f2 = NULL;
1012 char *abspath = NULL;
1013 struct stat sb;
1015 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1016 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1017 return NULL;
1019 if (!a->header_shown) {
1020 printf("diff %s %s\n", a->id_str,
1021 got_worktree_get_root_path(a->worktree));
1022 a->header_shown = 1;
1025 if (status != GOT_STATUS_ADD) {
1026 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1027 if (err)
1028 goto done;
1032 if (status != GOT_STATUS_DELETE) {
1033 if (asprintf(&abspath, "%s/%s",
1034 got_worktree_get_root_path(a->worktree), path) == -1) {
1035 err = got_error_from_errno();
1036 goto done;
1039 f2 = fopen(abspath, "r");
1040 if (f2 == NULL) {
1041 err = got_error_from_errno();
1042 goto done;
1044 if (lstat(abspath, &sb) == -1) {
1045 err = got_error_from_errno();
1046 goto done;
1048 } else
1049 sb.st_size = 0;
1051 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1052 stdout);
1053 done:
1054 if (blob1)
1055 got_object_blob_close(blob1);
1056 if (f2 && fclose(f2) != 0 && err == NULL)
1057 err = got_error_from_errno();
1058 free(abspath);
1059 return err;
1062 static const struct got_error *
1063 cmd_diff(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 char *cwd = NULL, *repo_path = NULL;
1069 struct got_object_id *id1 = NULL, *id2 = NULL;
1070 char *id_str1 = NULL, *id_str2 = NULL;
1071 int type1, type2;
1072 int diff_context = 3, ch;
1073 const char *errstr;
1074 char *path = NULL;
1076 #ifndef PROFILE
1077 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1078 NULL) == -1)
1079 err(1, "pledge");
1080 #endif
1082 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1083 switch (ch) {
1084 case 'C':
1085 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1086 if (errstr != NULL)
1087 err(1, "-C option %s", errstr);
1088 break;
1089 case 'r':
1090 repo_path = realpath(optarg, NULL);
1091 if (repo_path == NULL)
1092 err(1, "-r option");
1093 break;
1094 default:
1095 usage_diff();
1096 /* NOTREACHED */
1100 argc -= optind;
1101 argv += optind;
1103 cwd = getcwd(NULL, 0);
1104 if (cwd == NULL) {
1105 error = got_error_from_errno();
1106 goto done;
1108 error = got_worktree_open(&worktree, cwd);
1109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1110 goto done;
1111 if (argc <= 1) {
1112 if (worktree == NULL) {
1113 error = got_error(GOT_ERR_NOT_WORKTREE);
1114 goto done;
1116 if (repo_path)
1117 errx(1,
1118 "-r option can't be used when diffing a work tree");
1119 repo_path = strdup(got_worktree_get_repo_path(worktree));
1120 if (repo_path == NULL) {
1121 error = got_error_from_errno();
1122 goto done;
1124 if (argc == 1) {
1125 error = got_worktree_resolve_path(&path, worktree,
1126 argv[0]);
1127 if (error)
1128 goto done;
1129 } else {
1130 path = strdup("");
1131 if (path == NULL) {
1132 error = got_error_from_errno();
1133 goto done;
1136 } else if (argc == 2) {
1137 id_str1 = argv[0];
1138 id_str2 = argv[1];
1139 } else
1140 usage_diff();
1142 if (repo_path == NULL) {
1143 repo_path = getcwd(NULL, 0);
1144 if (repo_path == NULL)
1145 return got_error_from_errno();
1148 error = got_repo_open(&repo, repo_path);
1149 free(repo_path);
1150 if (error != NULL)
1151 goto done;
1153 error = apply_unveil(got_repo_get_path(repo), 1,
1154 worktree ? got_worktree_get_root_path(worktree) : NULL);
1155 if (error)
1156 goto done;
1158 if (worktree) {
1159 struct print_diff_arg arg;
1160 char *id_str;
1161 error = got_object_id_str(&id_str,
1162 got_worktree_get_base_commit_id(worktree));
1163 if (error)
1164 goto done;
1165 arg.repo = repo;
1166 arg.worktree = worktree;
1167 arg.diff_context = diff_context;
1168 arg.id_str = id_str;
1169 arg.header_shown = 0;
1171 error = got_worktree_status(worktree, path, repo, print_diff,
1172 &arg, check_cancelled, NULL);
1173 free(id_str);
1174 goto done;
1177 error = got_object_resolve_id_str(&id1, repo, id_str1);
1178 if (error)
1179 goto done;
1181 error = got_object_resolve_id_str(&id2, repo, id_str2);
1182 if (error)
1183 goto done;
1185 error = got_object_get_type(&type1, repo, id1);
1186 if (error)
1187 goto done;
1189 error = got_object_get_type(&type2, repo, id2);
1190 if (error)
1191 goto done;
1193 if (type1 != type2) {
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1195 goto done;
1198 switch (type1) {
1199 case GOT_OBJ_TYPE_BLOB:
1200 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1201 diff_context, repo, stdout);
1202 break;
1203 case GOT_OBJ_TYPE_TREE:
1204 error = got_diff_objects_as_trees(id1, id2, "", "",
1205 diff_context, repo, stdout);
1206 break;
1207 case GOT_OBJ_TYPE_COMMIT:
1208 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1209 id_str2);
1210 error = got_diff_objects_as_commits(id1, id2, diff_context,
1211 repo, stdout);
1212 break;
1213 default:
1214 error = got_error(GOT_ERR_OBJ_TYPE);
1217 done:
1218 free(id1);
1219 free(id2);
1220 free(path);
1221 if (worktree)
1222 got_worktree_close(worktree);
1223 if (repo) {
1224 const struct got_error *repo_error;
1225 repo_error = got_repo_close(repo);
1226 if (error == NULL)
1227 error = repo_error;
1229 return error;
1232 __dead static void
1233 usage_blame(void)
1235 fprintf(stderr,
1236 "usage: %s blame [-c commit] [-r repository-path] path\n",
1237 getprogname());
1238 exit(1);
1241 static const struct got_error *
1242 cmd_blame(int argc, char *argv[])
1244 const struct got_error *error;
1245 struct got_repository *repo = NULL;
1246 struct got_worktree *worktree = NULL;
1247 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1248 struct got_object_id *commit_id = NULL;
1249 char *commit_id_str = NULL;
1250 int ch;
1252 #ifndef PROFILE
1253 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1254 NULL) == -1)
1255 err(1, "pledge");
1256 #endif
1258 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1259 switch (ch) {
1260 case 'c':
1261 commit_id_str = optarg;
1262 break;
1263 case 'r':
1264 repo_path = realpath(optarg, NULL);
1265 if (repo_path == NULL)
1266 err(1, "-r option");
1267 break;
1268 default:
1269 usage_blame();
1270 /* NOTREACHED */
1274 argc -= optind;
1275 argv += optind;
1277 if (argc == 1)
1278 path = argv[0];
1279 else
1280 usage_blame();
1282 cwd = getcwd(NULL, 0);
1283 if (cwd == NULL) {
1284 error = got_error_from_errno();
1285 goto done;
1287 if (repo_path == NULL) {
1288 error = got_worktree_open(&worktree, cwd);
1289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1290 goto done;
1291 else
1292 error = NULL;
1293 if (worktree) {
1294 repo_path =
1295 strdup(got_worktree_get_repo_path(worktree));
1296 if (repo_path == NULL)
1297 error = got_error_from_errno();
1298 if (error)
1299 goto done;
1300 } else {
1301 repo_path = strdup(cwd);
1302 if (repo_path == NULL) {
1303 error = got_error_from_errno();
1304 goto done;
1309 error = got_repo_open(&repo, repo_path);
1310 if (error != NULL)
1311 goto done;
1313 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1314 if (error)
1315 goto done;
1317 if (worktree) {
1318 const char *prefix = got_worktree_get_path_prefix(worktree);
1319 char *p, *worktree_subdir = cwd +
1320 strlen(got_worktree_get_root_path(worktree));
1321 if (asprintf(&p, "%s%s%s%s%s",
1322 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1323 worktree_subdir, worktree_subdir[0] ? "/" : "",
1324 path) == -1) {
1325 error = got_error_from_errno();
1326 goto done;
1328 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1329 free(p);
1330 } else {
1331 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1333 if (error)
1334 goto done;
1336 if (commit_id_str == NULL) {
1337 struct got_reference *head_ref;
1338 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1339 if (error != NULL)
1340 goto done;
1341 error = got_ref_resolve(&commit_id, repo, head_ref);
1342 got_ref_close(head_ref);
1343 if (error != NULL)
1344 goto done;
1345 } else {
1346 error = got_object_resolve_id_str(&commit_id, repo,
1347 commit_id_str);
1348 if (error != NULL)
1349 goto done;
1352 error = got_blame(in_repo_path, commit_id, repo, stdout);
1353 done:
1354 free(in_repo_path);
1355 free(repo_path);
1356 free(cwd);
1357 free(commit_id);
1358 if (worktree)
1359 got_worktree_close(worktree);
1360 if (repo) {
1361 const struct got_error *repo_error;
1362 repo_error = got_repo_close(repo);
1363 if (error == NULL)
1364 error = repo_error;
1366 return error;
1369 __dead static void
1370 usage_tree(void)
1372 fprintf(stderr,
1373 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1374 getprogname());
1375 exit(1);
1378 static void
1379 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1380 const char *root_path)
1382 int is_root_path = (strcmp(path, root_path) == 0);
1384 path += strlen(root_path);
1385 while (path[0] == '/')
1386 path++;
1388 printf("%s%s%s%s%s\n", id ? id : "", path,
1389 is_root_path ? "" : "/", te->name,
1390 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1393 static const struct got_error *
1394 print_tree(const char *path, struct got_object_id *commit_id,
1395 int show_ids, int recurse, const char *root_path,
1396 struct got_repository *repo)
1398 const struct got_error *err = NULL;
1399 struct got_object_id *tree_id = NULL;
1400 struct got_tree_object *tree = NULL;
1401 const struct got_tree_entries *entries;
1402 struct got_tree_entry *te;
1404 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1405 if (err)
1406 goto done;
1408 err = got_object_open_as_tree(&tree, repo, tree_id);
1409 if (err)
1410 goto done;
1411 entries = got_object_tree_get_entries(tree);
1412 te = SIMPLEQ_FIRST(&entries->head);
1413 while (te) {
1414 char *id = NULL;
1416 if (sigint_received || sigpipe_received)
1417 break;
1419 if (show_ids) {
1420 char *id_str;
1421 err = got_object_id_str(&id_str, te->id);
1422 if (err)
1423 goto done;
1424 if (asprintf(&id, "%s ", id_str) == -1) {
1425 err = got_error_from_errno();
1426 free(id_str);
1427 goto done;
1429 free(id_str);
1431 print_entry(te, id, path, root_path);
1432 free(id);
1434 if (recurse && S_ISDIR(te->mode)) {
1435 char *child_path;
1436 if (asprintf(&child_path, "%s%s%s", path,
1437 path[0] == '/' && path[1] == '\0' ? "" : "/",
1438 te->name) == -1) {
1439 err = got_error_from_errno();
1440 goto done;
1442 err = print_tree(child_path, commit_id, show_ids, 1,
1443 root_path, repo);
1444 free(child_path);
1445 if (err)
1446 goto done;
1449 te = SIMPLEQ_NEXT(te, entry);
1451 done:
1452 if (tree)
1453 got_object_tree_close(tree);
1454 free(tree_id);
1455 return err;
1458 static const struct got_error *
1459 cmd_tree(int argc, char *argv[])
1461 const struct got_error *error;
1462 struct got_repository *repo = NULL;
1463 struct got_worktree *worktree = NULL;
1464 const char *path;
1465 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1466 struct got_object_id *commit_id = NULL;
1467 char *commit_id_str = NULL;
1468 int show_ids = 0, recurse = 0;
1469 int ch;
1471 #ifndef PROFILE
1472 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1473 NULL) == -1)
1474 err(1, "pledge");
1475 #endif
1477 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1478 switch (ch) {
1479 case 'c':
1480 commit_id_str = optarg;
1481 break;
1482 case 'r':
1483 repo_path = realpath(optarg, NULL);
1484 if (repo_path == NULL)
1485 err(1, "-r option");
1486 break;
1487 case 'i':
1488 show_ids = 1;
1489 break;
1490 case 'R':
1491 recurse = 1;
1492 break;
1493 default:
1494 usage_tree();
1495 /* NOTREACHED */
1499 argc -= optind;
1500 argv += optind;
1502 if (argc == 1)
1503 path = argv[0];
1504 else if (argc > 1)
1505 usage_tree();
1506 else
1507 path = NULL;
1509 cwd = getcwd(NULL, 0);
1510 if (cwd == NULL) {
1511 error = got_error_from_errno();
1512 goto done;
1514 if (repo_path == NULL) {
1515 error = got_worktree_open(&worktree, cwd);
1516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1517 goto done;
1518 else
1519 error = NULL;
1520 if (worktree) {
1521 repo_path =
1522 strdup(got_worktree_get_repo_path(worktree));
1523 if (repo_path == NULL)
1524 error = got_error_from_errno();
1525 if (error)
1526 goto done;
1527 } else {
1528 repo_path = strdup(cwd);
1529 if (repo_path == NULL) {
1530 error = got_error_from_errno();
1531 goto done;
1536 error = got_repo_open(&repo, repo_path);
1537 if (error != NULL)
1538 goto done;
1540 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1541 if (error)
1542 goto done;
1544 if (path == NULL) {
1545 if (worktree) {
1546 char *p, *worktree_subdir = cwd +
1547 strlen(got_worktree_get_root_path(worktree));
1548 if (asprintf(&p, "%s/%s",
1549 got_worktree_get_path_prefix(worktree),
1550 worktree_subdir) == -1) {
1551 error = got_error_from_errno();
1552 goto done;
1554 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1555 free(p);
1556 if (error)
1557 goto done;
1558 } else
1559 path = "/";
1561 if (in_repo_path == NULL) {
1562 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1563 if (error != NULL)
1564 goto done;
1567 if (commit_id_str == NULL) {
1568 struct got_reference *head_ref;
1569 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1570 if (error != NULL)
1571 goto done;
1572 error = got_ref_resolve(&commit_id, repo, head_ref);
1573 got_ref_close(head_ref);
1574 if (error != NULL)
1575 goto done;
1576 } else {
1577 error = got_object_resolve_id_str(&commit_id, repo,
1578 commit_id_str);
1579 if (error != NULL)
1580 goto done;
1583 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1584 in_repo_path, repo);
1585 done:
1586 free(in_repo_path);
1587 free(repo_path);
1588 free(cwd);
1589 free(commit_id);
1590 if (worktree)
1591 got_worktree_close(worktree);
1592 if (repo) {
1593 const struct got_error *repo_error;
1594 repo_error = got_repo_close(repo);
1595 if (error == NULL)
1596 error = repo_error;
1598 return error;
1601 __dead static void
1602 usage_status(void)
1604 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1605 exit(1);
1608 static const struct got_error *
1609 print_status(void *arg, unsigned char status, const char *path,
1610 struct got_object_id *id)
1612 printf("%c %s\n", status, path);
1613 return NULL;
1616 static const struct got_error *
1617 cmd_status(int argc, char *argv[])
1619 const struct got_error *error = NULL;
1620 struct got_repository *repo = NULL;
1621 struct got_worktree *worktree = NULL;
1622 char *cwd = NULL, *path = NULL;
1623 int ch;
1625 while ((ch = getopt(argc, argv, "")) != -1) {
1626 switch (ch) {
1627 default:
1628 usage_status();
1629 /* NOTREACHED */
1633 argc -= optind;
1634 argv += optind;
1636 #ifndef PROFILE
1637 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1638 NULL) == -1)
1639 err(1, "pledge");
1640 #endif
1641 cwd = getcwd(NULL, 0);
1642 if (cwd == NULL) {
1643 error = got_error_from_errno();
1644 goto done;
1647 error = got_worktree_open(&worktree, cwd);
1648 if (error != NULL)
1649 goto done;
1651 if (argc == 0) {
1652 path = strdup("");
1653 if (path == NULL) {
1654 error = got_error_from_errno();
1655 goto done;
1657 } else if (argc == 1) {
1658 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1659 if (error)
1660 goto done;
1661 } else
1662 usage_status();
1664 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1665 if (error != NULL)
1666 goto done;
1668 error = apply_unveil(got_repo_get_path(repo), 1,
1669 got_worktree_get_root_path(worktree));
1670 if (error)
1671 goto done;
1673 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1674 check_cancelled, NULL);
1675 done:
1676 free(cwd);
1677 free(path);
1678 return error;
1681 __dead static void
1682 usage_ref(void)
1684 fprintf(stderr,
1685 "usage: %s ref [-r repository] -l | -d name | name object\n",
1686 getprogname());
1687 exit(1);
1690 static const struct got_error *
1691 list_refs(struct got_repository *repo)
1693 static const struct got_error *err = NULL;
1694 struct got_reflist_head refs;
1695 struct got_reflist_entry *re;
1697 SIMPLEQ_INIT(&refs);
1698 err = got_ref_list(&refs, repo);
1699 if (err)
1700 return err;
1702 SIMPLEQ_FOREACH(re, &refs, entry) {
1703 char *refstr;
1704 refstr = got_ref_to_str(re->ref);
1705 if (refstr == NULL)
1706 return got_error_from_errno();
1707 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1708 free(refstr);
1711 got_ref_list_free(&refs);
1712 return NULL;
1715 static const struct got_error *
1716 delete_ref(struct got_repository *repo, const char *refname)
1718 const struct got_error *err = NULL;
1719 struct got_reference *ref;
1721 err = got_ref_open(&ref, repo, refname);
1722 if (err)
1723 return err;
1725 err = got_ref_delete(ref, repo);
1726 got_ref_close(ref);
1727 return err;
1730 static const struct got_error *
1731 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1733 const struct got_error *err = NULL;
1734 struct got_object_id *id;
1735 struct got_reference *ref = NULL;
1737 err = got_object_resolve_id_str(&id, repo, id_str);
1738 if (err)
1739 return err;
1741 err = got_ref_alloc(&ref, refname, id);
1742 if (err)
1743 goto done;
1745 err = got_ref_write(ref, repo);
1746 done:
1747 if (ref)
1748 got_ref_close(ref);
1749 free(id);
1750 return err;
1753 static const struct got_error *
1754 cmd_ref(int argc, char *argv[])
1756 const struct got_error *error = NULL;
1757 struct got_repository *repo = NULL;
1758 struct got_worktree *worktree = NULL;
1759 char *cwd = NULL, *repo_path = NULL;
1760 int ch, do_list = 0;
1761 const char *delref = NULL;
1763 /* TODO: Add -s option for adding symbolic references. */
1764 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1765 switch (ch) {
1766 case 'd':
1767 delref = optarg;
1768 break;
1769 case 'r':
1770 repo_path = realpath(optarg, NULL);
1771 if (repo_path == NULL)
1772 err(1, "-r option");
1773 break;
1774 case 'l':
1775 do_list = 1;
1776 break;
1777 default:
1778 usage_ref();
1779 /* NOTREACHED */
1783 if (do_list && delref)
1784 errx(1, "-l and -d options are mutually exclusive\n");
1786 argc -= optind;
1787 argv += optind;
1789 if (do_list || delref) {
1790 if (argc > 0)
1791 usage_ref();
1792 } else if (argc != 2)
1793 usage_ref();
1795 #ifndef PROFILE
1796 if (do_list) {
1797 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1798 NULL) == -1)
1799 err(1, "pledge");
1800 } else {
1801 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1802 "sendfd unveil", NULL) == -1)
1803 err(1, "pledge");
1805 #endif
1806 cwd = getcwd(NULL, 0);
1807 if (cwd == NULL) {
1808 error = got_error_from_errno();
1809 goto done;
1812 if (repo_path == NULL) {
1813 error = got_worktree_open(&worktree, cwd);
1814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1815 goto done;
1816 else
1817 error = NULL;
1818 if (worktree) {
1819 repo_path =
1820 strdup(got_worktree_get_repo_path(worktree));
1821 if (repo_path == NULL)
1822 error = got_error_from_errno();
1823 if (error)
1824 goto done;
1825 } else {
1826 repo_path = strdup(cwd);
1827 if (repo_path == NULL) {
1828 error = got_error_from_errno();
1829 goto done;
1834 error = got_repo_open(&repo, repo_path);
1835 if (error != NULL)
1836 goto done;
1838 error = apply_unveil(got_repo_get_path(repo), do_list,
1839 worktree ? got_worktree_get_root_path(worktree) : NULL);
1840 if (error)
1841 goto done;
1843 if (do_list)
1844 error = list_refs(repo);
1845 else if (delref)
1846 error = delete_ref(repo, delref);
1847 else
1848 error = add_ref(repo, argv[0], argv[1]);
1849 done:
1850 if (repo)
1851 got_repo_close(repo);
1852 if (worktree)
1853 got_worktree_close(worktree);
1854 free(cwd);
1855 free(repo_path);
1856 return error;
1859 __dead static void
1860 usage_add(void)
1862 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1863 exit(1);
1866 static const struct got_error *
1867 cmd_add(int argc, char *argv[])
1869 const struct got_error *error = NULL;
1870 struct got_repository *repo = NULL;
1871 struct got_worktree *worktree = NULL;
1872 char *cwd = NULL, *path = NULL, *relpath = NULL;
1873 int ch;
1875 while ((ch = getopt(argc, argv, "")) != -1) {
1876 switch (ch) {
1877 default:
1878 usage_add();
1879 /* NOTREACHED */
1883 argc -= optind;
1884 argv += optind;
1886 if (argc != 1)
1887 usage_add();
1889 path = realpath(argv[0], NULL);
1890 if (path == NULL) {
1891 error = got_error_from_errno();
1892 goto done;
1895 cwd = getcwd(NULL, 0);
1896 if (cwd == NULL) {
1897 error = got_error_from_errno();
1898 goto done;
1900 error = got_worktree_open(&worktree, cwd);
1901 if (error)
1902 goto done;
1904 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1905 if (error != NULL)
1906 goto done;
1908 error = apply_unveil(got_repo_get_path(repo), 1,
1909 got_worktree_get_root_path(worktree));
1910 if (error)
1911 goto done;
1913 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1914 repo);
1915 if (error)
1916 goto done;
1917 done:
1918 if (repo)
1919 got_repo_close(repo);
1920 if (worktree)
1921 got_worktree_close(worktree);
1922 free(path);
1923 free(relpath);
1924 free(cwd);
1925 return error;
1928 __dead static void
1929 usage_rm(void)
1931 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1932 exit(1);
1935 static const struct got_error *
1936 cmd_rm(int argc, char *argv[])
1938 const struct got_error *error = NULL;
1939 struct got_worktree *worktree = NULL;
1940 struct got_repository *repo = NULL;
1941 char *cwd = NULL, *path = NULL;
1942 int ch, delete_local_mods = 0;
1944 while ((ch = getopt(argc, argv, "f")) != -1) {
1945 switch (ch) {
1946 case 'f':
1947 delete_local_mods = 1;
1948 break;
1949 default:
1950 usage_add();
1951 /* NOTREACHED */
1955 argc -= optind;
1956 argv += optind;
1958 if (argc != 1)
1959 usage_rm();
1961 path = realpath(argv[0], NULL);
1962 if (path == NULL) {
1963 error = got_error_from_errno();
1964 goto done;
1967 cwd = getcwd(NULL, 0);
1968 if (cwd == NULL) {
1969 error = got_error_from_errno();
1970 goto done;
1972 error = got_worktree_open(&worktree, cwd);
1973 if (error)
1974 goto done;
1976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1977 if (error != NULL)
1978 goto done;
1980 error = apply_unveil(got_repo_get_path(repo), 1,
1981 got_worktree_get_root_path(worktree));
1982 if (error)
1983 goto done;
1985 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1986 print_status, NULL, repo);
1987 if (error)
1988 goto done;
1989 done:
1990 if (repo)
1991 got_repo_close(repo);
1992 if (worktree)
1993 got_worktree_close(worktree);
1994 free(path);
1995 free(cwd);
1996 return error;
1999 __dead static void
2000 usage_revert(void)
2002 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2003 exit(1);
2006 static void
2007 revert_progress(void *arg, unsigned char status, const char *path)
2009 while (path[0] == '/')
2010 path++;
2011 printf("%c %s\n", status, path);
2014 static const struct got_error *
2015 cmd_revert(int argc, char *argv[])
2017 const struct got_error *error = NULL;
2018 struct got_worktree *worktree = NULL;
2019 struct got_repository *repo = NULL;
2020 char *cwd = NULL, *path = NULL;
2021 int ch;
2023 while ((ch = getopt(argc, argv, "")) != -1) {
2024 switch (ch) {
2025 default:
2026 usage_revert();
2027 /* NOTREACHED */
2031 argc -= optind;
2032 argv += optind;
2034 if (argc != 1)
2035 usage_revert();
2037 path = realpath(argv[0], NULL);
2038 if (path == NULL) {
2039 error = got_error_from_errno();
2040 goto done;
2043 cwd = getcwd(NULL, 0);
2044 if (cwd == NULL) {
2045 error = got_error_from_errno();
2046 goto done;
2048 error = got_worktree_open(&worktree, cwd);
2049 if (error)
2050 goto done;
2052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2053 if (error != NULL)
2054 goto done;
2056 error = apply_unveil(got_repo_get_path(repo), 1,
2057 got_worktree_get_root_path(worktree));
2058 if (error)
2059 goto done;
2061 error = got_worktree_revert(worktree, path,
2062 revert_progress, NULL, repo);
2063 if (error)
2064 goto done;
2065 done:
2066 if (repo)
2067 got_repo_close(repo);
2068 if (worktree)
2069 got_worktree_close(worktree);
2070 free(path);
2071 free(cwd);
2072 return error;
2075 __dead static void
2076 usage_commit(void)
2078 fprintf(stderr, "usage: %s commit file-path\n", getprogname());
2079 exit(1);
2082 static const struct got_error *
2083 cmd_commit(int argc, char *argv[])
2085 const struct got_error *error = NULL;
2086 struct got_worktree *worktree = NULL;
2087 struct got_repository *repo = NULL;
2088 char *cwd = NULL, *path = NULL, *id_str = NULL;
2089 struct got_object_id *id = NULL;
2090 const char *logmsg = "<no log message was specified>";
2091 int ch;
2093 while ((ch = getopt(argc, argv, "m:")) != -1) {
2094 switch (ch) {
2095 case 'm':
2096 logmsg = optarg;
2097 break;
2098 default:
2099 usage_commit();
2100 /* NOTREACHED */
2104 argc -= optind;
2105 argv += optind;
2107 if (argc == 1) {
2108 path = realpath(argv[0], NULL);
2109 if (path == NULL) {
2110 error = got_error_from_errno();
2111 goto done;
2113 } else if (argc != 0)
2114 usage_commit();
2117 cwd = getcwd(NULL, 0);
2118 if (cwd == NULL) {
2119 error = got_error_from_errno();
2120 goto done;
2122 error = got_worktree_open(&worktree, cwd);
2123 if (error)
2124 goto done;
2126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2127 if (error != NULL)
2128 goto done;
2130 error = apply_unveil(got_repo_get_path(repo), 0,
2131 got_worktree_get_root_path(worktree));
2132 if (error)
2133 goto done;
2135 error = got_worktree_commit(&id, worktree, path, logmsg, repo);
2136 if (error)
2137 goto done;
2139 error = got_object_id_str(&id_str, id);
2140 if (error)
2141 goto done;
2142 printf("created commit %s\n", id_str);
2143 done:
2144 if (repo)
2145 got_repo_close(repo);
2146 if (worktree)
2147 got_worktree_close(worktree);
2148 free(path);
2149 free(cwd);
2150 free(id_str);
2151 return error;