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 "write changes from work tree to repository" },
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 err = got_ref_open(&head_ref, repo,
256 got_worktree_get_head_ref_name(worktree));
257 if (err)
258 return err;
260 /* TODO: Check the reflog. The head ref may have been rebased. */
261 err = got_ref_resolve(&head_commit_id, repo, head_ref);
262 if (err)
263 goto done;
265 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
266 if (err)
267 goto done;
269 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
270 if (err)
271 goto done;
272 while (1) {
273 struct got_object_id *id;
275 if (sigint_received || sigpipe_received)
276 break;
278 err = got_commit_graph_iter_next(&id, graph);
279 if (err) {
280 if (err->code == GOT_ERR_ITER_COMPLETED) {
281 err = got_error(GOT_ERR_ANCESTRY);
282 break;
284 if (err->code != GOT_ERR_ITER_NEED_MORE)
285 break;
286 err = got_commit_graph_fetch_commits(graph, 1, repo);
287 if (err)
288 break;
289 else
290 continue;
292 if (id == NULL)
293 break;
294 if (got_object_id_cmp(id, commit_id) == 0)
295 break;
297 done:
298 if (head_ref)
299 got_ref_close(head_ref);
300 if (graph)
301 got_commit_graph_close(graph);
302 return err;
306 static const struct got_error *
307 cmd_checkout(int argc, char *argv[])
309 const struct got_error *error = NULL;
310 struct got_repository *repo = NULL;
311 struct got_reference *head_ref = NULL;
312 struct got_worktree *worktree = NULL;
313 char *repo_path = NULL;
314 char *worktree_path = NULL;
315 const char *path_prefix = "";
316 char *commit_id_str = NULL;
317 int ch, same_path_prefix, x;
319 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
320 switch (ch) {
321 case 'c':
322 commit_id_str = strdup(optarg);
323 if (commit_id_str == NULL)
324 return got_error_from_errno();
325 break;
326 case 'p':
327 path_prefix = optarg;
328 break;
329 default:
330 usage_checkout();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
340 "unveil", NULL) == -1)
341 err(1, "pledge");
342 #endif
343 if (argc == 1) {
344 char *cwd, *base, *dotgit;
345 repo_path = realpath(argv[0], NULL);
346 if (repo_path == NULL)
347 return got_error_from_errno();
348 cwd = getcwd(NULL, 0);
349 if (cwd == NULL) {
350 error = got_error_from_errno();
351 goto done;
353 if (path_prefix[0])
354 base = basename(path_prefix);
355 else
356 base = basename(repo_path);
357 if (base == NULL) {
358 error = got_error_from_errno();
359 goto done;
361 dotgit = strstr(base, ".git");
362 if (dotgit)
363 *dotgit = '\0';
364 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
365 error = got_error_from_errno();
366 free(cwd);
367 goto done;
369 free(cwd);
370 } else if (argc == 2) {
371 repo_path = realpath(argv[0], NULL);
372 if (repo_path == NULL) {
373 error = got_error_from_errno();
374 goto done;
376 worktree_path = realpath(argv[1], NULL);
377 if (worktree_path == NULL) {
378 error = got_error_from_errno();
379 goto done;
381 } else
382 usage_checkout();
384 if (worktree_path[x = strlen(worktree_path) - 1] == '/')
385 worktree_path[x] = '\0';
387 error = got_repo_open(&repo, repo_path);
388 if (error != NULL)
389 goto done;
391 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
392 if (error)
393 goto done;
395 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
396 if (error != NULL)
397 goto done;
399 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
400 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
401 goto done;
403 error = got_worktree_open(&worktree, worktree_path);
404 if (error != NULL)
405 goto done;
407 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
408 path_prefix);
409 if (error != NULL)
410 goto done;
411 if (!same_path_prefix) {
412 error = got_error(GOT_ERR_PATH_PREFIX);
413 goto done;
416 if (commit_id_str) {
417 struct got_object_id *commit_id;
418 error = got_object_resolve_id_str(&commit_id, repo,
419 commit_id_str);
420 if (error != NULL)
421 goto done;
422 error = check_ancestry(worktree, commit_id, repo);
423 if (error != NULL) {
424 free(commit_id);
425 goto done;
427 error = got_worktree_set_base_commit_id(worktree, repo,
428 commit_id);
429 free(commit_id);
430 if (error)
431 goto done;
434 error = got_worktree_checkout_files(worktree, "", repo,
435 checkout_progress, worktree_path, check_cancelled, NULL);
436 if (error != NULL)
437 goto done;
439 printf("Now shut up and hack\n");
441 done:
442 free(commit_id_str);
443 free(repo_path);
444 free(worktree_path);
445 return error;
448 __dead static void
449 usage_update(void)
451 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
452 getprogname());
453 exit(1);
456 static void
457 update_progress(void *arg, unsigned char status, const char *path)
459 int *did_something = arg;
461 if (status == GOT_STATUS_EXISTS)
462 return;
464 *did_something = 1;
465 while (path[0] == '/')
466 path++;
467 printf("%c %s\n", status, path);
470 static const struct got_error *
471 cmd_update(int argc, char *argv[])
473 const struct got_error *error = NULL;
474 struct got_repository *repo = NULL;
475 struct got_worktree *worktree = NULL;
476 char *worktree_path = NULL, *path = NULL;
477 struct got_object_id *commit_id = NULL;
478 char *commit_id_str = NULL;
479 int ch, did_something = 0;
481 while ((ch = getopt(argc, argv, "c:")) != -1) {
482 switch (ch) {
483 case 'c':
484 commit_id_str = strdup(optarg);
485 if (commit_id_str == NULL)
486 return got_error_from_errno();
487 break;
488 default:
489 usage_update();
490 /* NOTREACHED */
494 argc -= optind;
495 argv += optind;
497 #ifndef PROFILE
498 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
499 "unveil", NULL) == -1)
500 err(1, "pledge");
501 #endif
502 worktree_path = getcwd(NULL, 0);
503 if (worktree_path == NULL) {
504 error = got_error_from_errno();
505 goto done;
507 error = got_worktree_open(&worktree, worktree_path);
508 if (error)
509 goto done;
511 if (argc == 0) {
512 path = strdup("");
513 if (path == NULL) {
514 error = got_error_from_errno();
515 goto done;
517 } else if (argc == 1) {
518 error = got_worktree_resolve_path(&path, worktree, argv[0]);
519 if (error)
520 goto done;
521 } else
522 usage_update();
524 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
525 if (error != NULL)
526 goto done;
528 error = apply_unveil(got_repo_get_path(repo), 0,
529 got_worktree_get_root_path(worktree));
530 if (error)
531 goto done;
533 if (commit_id_str == NULL) {
534 struct got_reference *head_ref;
535 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
536 if (error != NULL)
537 goto done;
538 error = got_ref_resolve(&commit_id, repo, head_ref);
539 if (error != NULL)
540 goto done;
541 error = got_object_id_str(&commit_id_str, commit_id);
542 if (error != NULL)
543 goto done;
544 } else {
545 error = got_object_resolve_id_str(&commit_id, repo,
546 commit_id_str);
547 if (error != NULL)
548 goto done;
551 error = check_ancestry(worktree, commit_id, repo);
552 if (error != NULL)
553 goto done;
555 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
556 commit_id) != 0) {
557 error = got_worktree_set_base_commit_id(worktree, repo,
558 commit_id);
559 if (error)
560 goto done;
563 error = got_worktree_checkout_files(worktree, path, repo,
564 update_progress, &did_something, check_cancelled, NULL);
565 if (error != NULL)
566 goto done;
568 if (did_something)
569 printf("Updated to commit %s\n", commit_id_str);
570 else
571 printf("Already up-to-date\n");
572 done:
573 free(worktree_path);
574 free(path);
575 free(commit_id);
576 free(commit_id_str);
577 return error;
580 static const struct got_error *
581 print_patch(struct got_commit_object *commit, struct got_object_id *id,
582 int diff_context, struct got_repository *repo)
584 const struct got_error *err = NULL;
585 struct got_tree_object *tree1 = NULL, *tree2;
586 struct got_object_qid *qid;
587 char *id_str1 = NULL, *id_str2;
589 err = got_object_open_as_tree(&tree2, repo,
590 got_object_commit_get_tree_id(commit));
591 if (err)
592 return err;
594 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
595 if (qid != NULL) {
596 struct got_commit_object *pcommit;
598 err = got_object_open_as_commit(&pcommit, repo, qid->id);
599 if (err)
600 return err;
602 err = got_object_open_as_tree(&tree1, repo,
603 got_object_commit_get_tree_id(pcommit));
604 got_object_commit_close(pcommit);
605 if (err)
606 return err;
608 err = got_object_id_str(&id_str1, qid->id);
609 if (err)
610 return err;
613 err = got_object_id_str(&id_str2, id);
614 if (err)
615 goto done;
617 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
618 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
619 done:
620 if (tree1)
621 got_object_tree_close(tree1);
622 got_object_tree_close(tree2);
623 free(id_str1);
624 free(id_str2);
625 return err;
628 static char *
629 get_datestr(time_t *time, char *datebuf)
631 char *p, *s = ctime_r(time, datebuf);
632 p = strchr(s, '\n');
633 if (p)
634 *p = '\0';
635 return s;
638 static const struct got_error *
639 print_commit(struct got_commit_object *commit, struct got_object_id *id,
640 struct got_repository *repo, int show_patch, int diff_context,
641 struct got_reflist_head *refs)
643 const struct got_error *err = NULL;
644 char *id_str, *datestr, *logmsg0, *logmsg, *line;
645 char datebuf[26];
646 time_t committer_time;
647 const char *author, *committer;
648 char *refs_str = NULL;
649 struct got_reflist_entry *re;
651 SIMPLEQ_FOREACH(re, refs, entry) {
652 char *s;
653 const char *name;
654 if (got_object_id_cmp(re->id, id) != 0)
655 continue;
656 name = got_ref_get_name(re->ref);
657 if (strcmp(name, GOT_REF_HEAD) == 0)
658 continue;
659 if (strncmp(name, "refs/", 5) == 0)
660 name += 5;
661 if (strncmp(name, "got/", 4) == 0)
662 continue;
663 if (strncmp(name, "heads/", 6) == 0)
664 name += 6;
665 if (strncmp(name, "remotes/", 8) == 0)
666 name += 8;
667 s = refs_str;
668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
669 name) == -1) {
670 err = got_error_from_errno();
671 free(s);
672 break;
674 free(s);
676 err = got_object_id_str(&id_str, id);
677 if (err)
678 return err;
680 printf("-----------------------------------------------\n");
681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
682 refs_str ? refs_str : "", refs_str ? ")" : "");
683 free(id_str);
684 id_str = NULL;
685 free(refs_str);
686 refs_str = NULL;
687 printf("from: %s\n", got_object_commit_get_author(commit));
688 committer_time = got_object_commit_get_committer_time(commit);
689 datestr = get_datestr(&committer_time, datebuf);
690 printf("date: %s UTC\n", datestr);
691 author = got_object_commit_get_author(commit);
692 committer = got_object_commit_get_committer(commit);
693 if (strcmp(author, committer) != 0)
694 printf("via: %s\n", committer);
695 if (got_object_commit_get_nparents(commit) > 1) {
696 const struct got_object_id_queue *parent_ids;
697 struct got_object_qid *qid;
698 int n = 1;
699 parent_ids = got_object_commit_get_parent_ids(commit);
700 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
701 err = got_object_id_str(&id_str, qid->id);
702 if (err)
703 return err;
704 printf("parent %d: %s\n", n++, id_str);
705 free(id_str);
709 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
710 if (logmsg0 == NULL)
711 return got_error_from_errno();
713 logmsg = logmsg0;
714 do {
715 line = strsep(&logmsg, "\n");
716 if (line)
717 printf(" %s\n", line);
718 } while (line);
719 free(logmsg0);
721 if (show_patch) {
722 err = print_patch(commit, id, diff_context, repo);
723 if (err == 0)
724 printf("\n");
727 if (fflush(stdout) != 0 && err == NULL)
728 err = got_error_from_errno();
729 return err;
732 static const struct got_error *
733 print_commits(struct got_object_id *root_id, struct got_repository *repo,
734 char *path, int show_patch, int diff_context, int limit,
735 int first_parent_traversal, struct got_reflist_head *refs)
737 const struct got_error *err;
738 struct got_commit_graph *graph;
740 err = got_commit_graph_open(&graph, root_id, path,
741 first_parent_traversal, repo);
742 if (err)
743 return err;
744 err = got_commit_graph_iter_start(graph, root_id, repo);
745 if (err)
746 goto done;
747 while (1) {
748 struct got_commit_object *commit;
749 struct got_object_id *id;
751 if (sigint_received || sigpipe_received)
752 break;
754 err = got_commit_graph_iter_next(&id, graph);
755 if (err) {
756 if (err->code == GOT_ERR_ITER_COMPLETED) {
757 err = NULL;
758 break;
760 if (err->code != GOT_ERR_ITER_NEED_MORE)
761 break;
762 err = got_commit_graph_fetch_commits(graph, 1, repo);
763 if (err)
764 break;
765 else
766 continue;
768 if (id == NULL)
769 break;
771 err = got_object_open_as_commit(&commit, repo, id);
772 if (err)
773 break;
774 err = print_commit(commit, id, repo, show_patch, diff_context,
775 refs);
776 got_object_commit_close(commit);
777 if (err || (limit && --limit == 0))
778 break;
780 done:
781 got_commit_graph_close(graph);
782 return err;
785 __dead static void
786 usage_log(void)
788 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
789 "[-r repository-path] [path]\n", getprogname());
790 exit(1);
793 static const struct got_error *
794 cmd_log(int argc, char *argv[])
796 const struct got_error *error;
797 struct got_repository *repo = NULL;
798 struct got_worktree *worktree = NULL;
799 struct got_commit_object *commit = NULL;
800 struct got_object_id *id = NULL;
801 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
802 char *start_commit = NULL;
803 int diff_context = 3, ch;
804 int show_patch = 0, limit = 0, first_parent_traversal = 0;
805 const char *errstr;
806 struct got_reflist_head refs;
808 SIMPLEQ_INIT(&refs);
810 #ifndef PROFILE
811 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
812 NULL)
813 == -1)
814 err(1, "pledge");
815 #endif
817 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
818 switch (ch) {
819 case 'p':
820 show_patch = 1;
821 break;
822 case 'c':
823 start_commit = optarg;
824 break;
825 case 'C':
826 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
827 &errstr);
828 if (errstr != NULL)
829 err(1, "-C option %s", errstr);
830 break;
831 case 'l':
832 limit = strtonum(optarg, 1, INT_MAX, &errstr);
833 if (errstr != NULL)
834 err(1, "-l option %s", errstr);
835 break;
836 case 'f':
837 first_parent_traversal = 1;
838 break;
839 case 'r':
840 repo_path = realpath(optarg, NULL);
841 if (repo_path == NULL)
842 err(1, "-r option");
843 break;
844 default:
845 usage_log();
846 /* NOTREACHED */
850 argc -= optind;
851 argv += optind;
853 cwd = getcwd(NULL, 0);
854 if (cwd == NULL) {
855 error = got_error_from_errno();
856 goto done;
859 error = got_worktree_open(&worktree, cwd);
860 if (error && error->code != GOT_ERR_NOT_WORKTREE)
861 goto done;
862 error = NULL;
864 if (argc == 0) {
865 path = strdup("");
866 if (path == NULL) {
867 error = got_error_from_errno();
868 goto done;
870 } else if (argc == 1) {
871 if (worktree) {
872 error = got_worktree_resolve_path(&path, worktree,
873 argv[0]);
874 if (error)
875 goto done;
876 } else {
877 path = strdup(argv[0]);
878 if (path == NULL) {
879 error = got_error_from_errno();
880 goto done;
883 } else
884 usage_log();
886 repo_path = worktree ?
887 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
888 if (repo_path == NULL) {
889 error = got_error_from_errno();
890 goto done;
893 error = got_repo_open(&repo, repo_path);
894 if (error != NULL)
895 goto done;
897 error = apply_unveil(got_repo_get_path(repo), 1,
898 worktree ? got_worktree_get_root_path(worktree) : NULL);
899 if (error)
900 goto done;
902 if (start_commit == NULL) {
903 struct got_reference *head_ref;
904 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
905 if (error != NULL)
906 return error;
907 error = got_ref_resolve(&id, repo, head_ref);
908 got_ref_close(head_ref);
909 if (error != NULL)
910 return error;
911 error = got_object_open_as_commit(&commit, repo, id);
912 } else {
913 struct got_reference *ref;
914 error = got_ref_open(&ref, repo, start_commit);
915 if (error == NULL) {
916 int obj_type;
917 error = got_ref_resolve(&id, repo, ref);
918 got_ref_close(ref);
919 if (error != NULL)
920 goto done;
921 error = got_object_get_type(&obj_type, repo, id);
922 if (error != NULL)
923 goto done;
924 if (obj_type == GOT_OBJ_TYPE_TAG) {
925 struct got_tag_object *tag;
926 error = got_object_open_as_tag(&tag, repo, id);
927 if (error != NULL)
928 goto done;
929 if (got_object_tag_get_object_type(tag) !=
930 GOT_OBJ_TYPE_COMMIT) {
931 got_object_tag_close(tag);
932 error = got_error(GOT_ERR_OBJ_TYPE);
933 goto done;
935 free(id);
936 id = got_object_id_dup(
937 got_object_tag_get_object_id(tag));
938 if (id == NULL)
939 error = got_error_from_errno();
940 got_object_tag_close(tag);
941 if (error)
942 goto done;
943 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
944 error = got_error(GOT_ERR_OBJ_TYPE);
945 goto done;
947 error = got_object_open_as_commit(&commit, repo, id);
948 if (error != NULL)
949 goto done;
951 if (commit == NULL) {
952 error = got_object_resolve_id_str(&id, repo,
953 start_commit);
954 if (error != NULL)
955 return error;
958 if (error != NULL)
959 goto done;
961 error = got_repo_map_path(&in_repo_path, repo, path, 1);
962 if (error != NULL)
963 goto done;
964 if (in_repo_path) {
965 free(path);
966 path = in_repo_path;
969 error = got_ref_list(&refs, repo);
970 if (error)
971 goto done;
973 error = print_commits(id, repo, path, show_patch,
974 diff_context, limit, first_parent_traversal, &refs);
975 done:
976 free(path);
977 free(repo_path);
978 free(cwd);
979 free(id);
980 if (worktree)
981 got_worktree_close(worktree);
982 if (repo) {
983 const struct got_error *repo_error;
984 repo_error = got_repo_close(repo);
985 if (error == NULL)
986 error = repo_error;
988 got_ref_list_free(&refs);
989 return error;
992 __dead static void
993 usage_diff(void)
995 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
996 "[object1 object2 | path]\n", getprogname());
997 exit(1);
1000 struct print_diff_arg {
1001 struct got_repository *repo;
1002 struct got_worktree *worktree;
1003 int diff_context;
1004 const char *id_str;
1005 int header_shown;
1008 static const struct got_error *
1009 print_diff(void *arg, unsigned char status, const char *path,
1010 struct got_object_id *id)
1012 struct print_diff_arg *a = arg;
1013 const struct got_error *err = NULL;
1014 struct got_blob_object *blob1 = NULL;
1015 FILE *f2 = NULL;
1016 char *abspath = NULL;
1017 struct stat sb;
1019 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1020 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1021 return NULL;
1023 if (!a->header_shown) {
1024 printf("diff %s %s\n", a->id_str,
1025 got_worktree_get_root_path(a->worktree));
1026 a->header_shown = 1;
1029 if (status != GOT_STATUS_ADD) {
1030 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1031 if (err)
1032 goto done;
1036 if (status != GOT_STATUS_DELETE) {
1037 if (asprintf(&abspath, "%s/%s",
1038 got_worktree_get_root_path(a->worktree), path) == -1) {
1039 err = got_error_from_errno();
1040 goto done;
1043 f2 = fopen(abspath, "r");
1044 if (f2 == NULL) {
1045 err = got_error_from_errno();
1046 goto done;
1048 if (lstat(abspath, &sb) == -1) {
1049 err = got_error_from_errno();
1050 goto done;
1052 } else
1053 sb.st_size = 0;
1055 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1056 stdout);
1057 done:
1058 if (blob1)
1059 got_object_blob_close(blob1);
1060 if (f2 && fclose(f2) != 0 && err == NULL)
1061 err = got_error_from_errno();
1062 free(abspath);
1063 return err;
1066 static const struct got_error *
1067 cmd_diff(int argc, char *argv[])
1069 const struct got_error *error;
1070 struct got_repository *repo = NULL;
1071 struct got_worktree *worktree = NULL;
1072 char *cwd = NULL, *repo_path = NULL;
1073 struct got_object_id *id1 = NULL, *id2 = NULL;
1074 char *id_str1 = NULL, *id_str2 = NULL;
1075 int type1, type2;
1076 int diff_context = 3, ch;
1077 const char *errstr;
1078 char *path = NULL;
1080 #ifndef PROFILE
1081 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1082 NULL) == -1)
1083 err(1, "pledge");
1084 #endif
1086 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1087 switch (ch) {
1088 case 'C':
1089 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1090 if (errstr != NULL)
1091 err(1, "-C option %s", errstr);
1092 break;
1093 case 'r':
1094 repo_path = realpath(optarg, NULL);
1095 if (repo_path == NULL)
1096 err(1, "-r option");
1097 break;
1098 default:
1099 usage_diff();
1100 /* NOTREACHED */
1104 argc -= optind;
1105 argv += optind;
1107 cwd = getcwd(NULL, 0);
1108 if (cwd == NULL) {
1109 error = got_error_from_errno();
1110 goto done;
1112 error = got_worktree_open(&worktree, cwd);
1113 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1114 goto done;
1115 if (argc <= 1) {
1116 if (worktree == NULL) {
1117 error = got_error(GOT_ERR_NOT_WORKTREE);
1118 goto done;
1120 if (repo_path)
1121 errx(1,
1122 "-r option can't be used when diffing a work tree");
1123 repo_path = strdup(got_worktree_get_repo_path(worktree));
1124 if (repo_path == NULL) {
1125 error = got_error_from_errno();
1126 goto done;
1128 if (argc == 1) {
1129 error = got_worktree_resolve_path(&path, worktree,
1130 argv[0]);
1131 if (error)
1132 goto done;
1133 } else {
1134 path = strdup("");
1135 if (path == NULL) {
1136 error = got_error_from_errno();
1137 goto done;
1140 } else if (argc == 2) {
1141 id_str1 = argv[0];
1142 id_str2 = argv[1];
1143 } else
1144 usage_diff();
1146 if (repo_path == NULL) {
1147 repo_path = getcwd(NULL, 0);
1148 if (repo_path == NULL)
1149 return got_error_from_errno();
1152 error = got_repo_open(&repo, repo_path);
1153 free(repo_path);
1154 if (error != NULL)
1155 goto done;
1157 error = apply_unveil(got_repo_get_path(repo), 1,
1158 worktree ? got_worktree_get_root_path(worktree) : NULL);
1159 if (error)
1160 goto done;
1162 if (worktree) {
1163 struct print_diff_arg arg;
1164 char *id_str;
1165 error = got_object_id_str(&id_str,
1166 got_worktree_get_base_commit_id(worktree));
1167 if (error)
1168 goto done;
1169 arg.repo = repo;
1170 arg.worktree = worktree;
1171 arg.diff_context = diff_context;
1172 arg.id_str = id_str;
1173 arg.header_shown = 0;
1175 error = got_worktree_status(worktree, path, repo, print_diff,
1176 &arg, check_cancelled, NULL);
1177 free(id_str);
1178 goto done;
1181 error = got_object_resolve_id_str(&id1, repo, id_str1);
1182 if (error)
1183 goto done;
1185 error = got_object_resolve_id_str(&id2, repo, id_str2);
1186 if (error)
1187 goto done;
1189 error = got_object_get_type(&type1, repo, id1);
1190 if (error)
1191 goto done;
1193 error = got_object_get_type(&type2, repo, id2);
1194 if (error)
1195 goto done;
1197 if (type1 != type2) {
1198 error = got_error(GOT_ERR_OBJ_TYPE);
1199 goto done;
1202 switch (type1) {
1203 case GOT_OBJ_TYPE_BLOB:
1204 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1205 diff_context, repo, stdout);
1206 break;
1207 case GOT_OBJ_TYPE_TREE:
1208 error = got_diff_objects_as_trees(id1, id2, "", "",
1209 diff_context, repo, stdout);
1210 break;
1211 case GOT_OBJ_TYPE_COMMIT:
1212 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1213 id_str2);
1214 error = got_diff_objects_as_commits(id1, id2, diff_context,
1215 repo, stdout);
1216 break;
1217 default:
1218 error = got_error(GOT_ERR_OBJ_TYPE);
1221 done:
1222 free(id1);
1223 free(id2);
1224 free(path);
1225 if (worktree)
1226 got_worktree_close(worktree);
1227 if (repo) {
1228 const struct got_error *repo_error;
1229 repo_error = got_repo_close(repo);
1230 if (error == NULL)
1231 error = repo_error;
1233 return error;
1236 __dead static void
1237 usage_blame(void)
1239 fprintf(stderr,
1240 "usage: %s blame [-c commit] [-r repository-path] path\n",
1241 getprogname());
1242 exit(1);
1245 static const struct got_error *
1246 cmd_blame(int argc, char *argv[])
1248 const struct got_error *error;
1249 struct got_repository *repo = NULL;
1250 struct got_worktree *worktree = NULL;
1251 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1252 struct got_object_id *commit_id = NULL;
1253 char *commit_id_str = NULL;
1254 int ch;
1256 #ifndef PROFILE
1257 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1258 NULL) == -1)
1259 err(1, "pledge");
1260 #endif
1262 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1263 switch (ch) {
1264 case 'c':
1265 commit_id_str = optarg;
1266 break;
1267 case 'r':
1268 repo_path = realpath(optarg, NULL);
1269 if (repo_path == NULL)
1270 err(1, "-r option");
1271 break;
1272 default:
1273 usage_blame();
1274 /* NOTREACHED */
1278 argc -= optind;
1279 argv += optind;
1281 if (argc == 1)
1282 path = argv[0];
1283 else
1284 usage_blame();
1286 cwd = getcwd(NULL, 0);
1287 if (cwd == NULL) {
1288 error = got_error_from_errno();
1289 goto done;
1291 if (repo_path == NULL) {
1292 error = got_worktree_open(&worktree, cwd);
1293 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1294 goto done;
1295 else
1296 error = NULL;
1297 if (worktree) {
1298 repo_path =
1299 strdup(got_worktree_get_repo_path(worktree));
1300 if (repo_path == NULL)
1301 error = got_error_from_errno();
1302 if (error)
1303 goto done;
1304 } else {
1305 repo_path = strdup(cwd);
1306 if (repo_path == NULL) {
1307 error = got_error_from_errno();
1308 goto done;
1313 error = got_repo_open(&repo, repo_path);
1314 if (error != NULL)
1315 goto done;
1317 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1318 if (error)
1319 goto done;
1321 if (worktree) {
1322 const char *prefix = got_worktree_get_path_prefix(worktree);
1323 char *p, *worktree_subdir = cwd +
1324 strlen(got_worktree_get_root_path(worktree));
1325 if (asprintf(&p, "%s%s%s%s%s",
1326 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1327 worktree_subdir, worktree_subdir[0] ? "/" : "",
1328 path) == -1) {
1329 error = got_error_from_errno();
1330 goto done;
1332 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1333 free(p);
1334 } else {
1335 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1337 if (error)
1338 goto done;
1340 if (commit_id_str == NULL) {
1341 struct got_reference *head_ref;
1342 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1343 if (error != NULL)
1344 goto done;
1345 error = got_ref_resolve(&commit_id, repo, head_ref);
1346 got_ref_close(head_ref);
1347 if (error != NULL)
1348 goto done;
1349 } else {
1350 error = got_object_resolve_id_str(&commit_id, repo,
1351 commit_id_str);
1352 if (error != NULL)
1353 goto done;
1356 error = got_blame(in_repo_path, commit_id, repo, stdout);
1357 done:
1358 free(in_repo_path);
1359 free(repo_path);
1360 free(cwd);
1361 free(commit_id);
1362 if (worktree)
1363 got_worktree_close(worktree);
1364 if (repo) {
1365 const struct got_error *repo_error;
1366 repo_error = got_repo_close(repo);
1367 if (error == NULL)
1368 error = repo_error;
1370 return error;
1373 __dead static void
1374 usage_tree(void)
1376 fprintf(stderr,
1377 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1378 getprogname());
1379 exit(1);
1382 static void
1383 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1384 const char *root_path)
1386 int is_root_path = (strcmp(path, root_path) == 0);
1388 path += strlen(root_path);
1389 while (path[0] == '/')
1390 path++;
1392 printf("%s%s%s%s%s\n", id ? id : "", path,
1393 is_root_path ? "" : "/", te->name,
1394 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1397 static const struct got_error *
1398 print_tree(const char *path, struct got_object_id *commit_id,
1399 int show_ids, int recurse, const char *root_path,
1400 struct got_repository *repo)
1402 const struct got_error *err = NULL;
1403 struct got_object_id *tree_id = NULL;
1404 struct got_tree_object *tree = NULL;
1405 const struct got_tree_entries *entries;
1406 struct got_tree_entry *te;
1408 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1409 if (err)
1410 goto done;
1412 err = got_object_open_as_tree(&tree, repo, tree_id);
1413 if (err)
1414 goto done;
1415 entries = got_object_tree_get_entries(tree);
1416 te = SIMPLEQ_FIRST(&entries->head);
1417 while (te) {
1418 char *id = NULL;
1420 if (sigint_received || sigpipe_received)
1421 break;
1423 if (show_ids) {
1424 char *id_str;
1425 err = got_object_id_str(&id_str, te->id);
1426 if (err)
1427 goto done;
1428 if (asprintf(&id, "%s ", id_str) == -1) {
1429 err = got_error_from_errno();
1430 free(id_str);
1431 goto done;
1433 free(id_str);
1435 print_entry(te, id, path, root_path);
1436 free(id);
1438 if (recurse && S_ISDIR(te->mode)) {
1439 char *child_path;
1440 if (asprintf(&child_path, "%s%s%s", path,
1441 path[0] == '/' && path[1] == '\0' ? "" : "/",
1442 te->name) == -1) {
1443 err = got_error_from_errno();
1444 goto done;
1446 err = print_tree(child_path, commit_id, show_ids, 1,
1447 root_path, repo);
1448 free(child_path);
1449 if (err)
1450 goto done;
1453 te = SIMPLEQ_NEXT(te, entry);
1455 done:
1456 if (tree)
1457 got_object_tree_close(tree);
1458 free(tree_id);
1459 return err;
1462 static const struct got_error *
1463 cmd_tree(int argc, char *argv[])
1465 const struct got_error *error;
1466 struct got_repository *repo = NULL;
1467 struct got_worktree *worktree = NULL;
1468 const char *path;
1469 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1470 struct got_object_id *commit_id = NULL;
1471 char *commit_id_str = NULL;
1472 int show_ids = 0, recurse = 0;
1473 int ch;
1475 #ifndef PROFILE
1476 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1477 NULL) == -1)
1478 err(1, "pledge");
1479 #endif
1481 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1482 switch (ch) {
1483 case 'c':
1484 commit_id_str = optarg;
1485 break;
1486 case 'r':
1487 repo_path = realpath(optarg, NULL);
1488 if (repo_path == NULL)
1489 err(1, "-r option");
1490 break;
1491 case 'i':
1492 show_ids = 1;
1493 break;
1494 case 'R':
1495 recurse = 1;
1496 break;
1497 default:
1498 usage_tree();
1499 /* NOTREACHED */
1503 argc -= optind;
1504 argv += optind;
1506 if (argc == 1)
1507 path = argv[0];
1508 else if (argc > 1)
1509 usage_tree();
1510 else
1511 path = NULL;
1513 cwd = getcwd(NULL, 0);
1514 if (cwd == NULL) {
1515 error = got_error_from_errno();
1516 goto done;
1518 if (repo_path == NULL) {
1519 error = got_worktree_open(&worktree, cwd);
1520 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1521 goto done;
1522 else
1523 error = NULL;
1524 if (worktree) {
1525 repo_path =
1526 strdup(got_worktree_get_repo_path(worktree));
1527 if (repo_path == NULL)
1528 error = got_error_from_errno();
1529 if (error)
1530 goto done;
1531 } else {
1532 repo_path = strdup(cwd);
1533 if (repo_path == NULL) {
1534 error = got_error_from_errno();
1535 goto done;
1540 error = got_repo_open(&repo, repo_path);
1541 if (error != NULL)
1542 goto done;
1544 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1545 if (error)
1546 goto done;
1548 if (path == NULL) {
1549 if (worktree) {
1550 char *p, *worktree_subdir = cwd +
1551 strlen(got_worktree_get_root_path(worktree));
1552 if (asprintf(&p, "%s/%s",
1553 got_worktree_get_path_prefix(worktree),
1554 worktree_subdir) == -1) {
1555 error = got_error_from_errno();
1556 goto done;
1558 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1559 free(p);
1560 if (error)
1561 goto done;
1562 } else
1563 path = "/";
1565 if (in_repo_path == NULL) {
1566 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1567 if (error != NULL)
1568 goto done;
1571 if (commit_id_str == NULL) {
1572 struct got_reference *head_ref;
1573 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1574 if (error != NULL)
1575 goto done;
1576 error = got_ref_resolve(&commit_id, repo, head_ref);
1577 got_ref_close(head_ref);
1578 if (error != NULL)
1579 goto done;
1580 } else {
1581 error = got_object_resolve_id_str(&commit_id, repo,
1582 commit_id_str);
1583 if (error != NULL)
1584 goto done;
1587 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1588 in_repo_path, repo);
1589 done:
1590 free(in_repo_path);
1591 free(repo_path);
1592 free(cwd);
1593 free(commit_id);
1594 if (worktree)
1595 got_worktree_close(worktree);
1596 if (repo) {
1597 const struct got_error *repo_error;
1598 repo_error = got_repo_close(repo);
1599 if (error == NULL)
1600 error = repo_error;
1602 return error;
1605 __dead static void
1606 usage_status(void)
1608 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1609 exit(1);
1612 static const struct got_error *
1613 print_status(void *arg, unsigned char status, const char *path,
1614 struct got_object_id *id)
1616 printf("%c %s\n", status, path);
1617 return NULL;
1620 static const struct got_error *
1621 cmd_status(int argc, char *argv[])
1623 const struct got_error *error = NULL;
1624 struct got_repository *repo = NULL;
1625 struct got_worktree *worktree = NULL;
1626 char *cwd = NULL, *path = NULL;
1627 int ch;
1629 while ((ch = getopt(argc, argv, "")) != -1) {
1630 switch (ch) {
1631 default:
1632 usage_status();
1633 /* NOTREACHED */
1637 argc -= optind;
1638 argv += optind;
1640 #ifndef PROFILE
1641 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1642 NULL) == -1)
1643 err(1, "pledge");
1644 #endif
1645 cwd = getcwd(NULL, 0);
1646 if (cwd == NULL) {
1647 error = got_error_from_errno();
1648 goto done;
1651 error = got_worktree_open(&worktree, cwd);
1652 if (error != NULL)
1653 goto done;
1655 if (argc == 0) {
1656 path = strdup("");
1657 if (path == NULL) {
1658 error = got_error_from_errno();
1659 goto done;
1661 } else if (argc == 1) {
1662 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1663 if (error)
1664 goto done;
1665 } else
1666 usage_status();
1668 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1669 if (error != NULL)
1670 goto done;
1672 error = apply_unveil(got_repo_get_path(repo), 1,
1673 got_worktree_get_root_path(worktree));
1674 if (error)
1675 goto done;
1677 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1678 check_cancelled, NULL);
1679 done:
1680 free(cwd);
1681 free(path);
1682 return error;
1685 __dead static void
1686 usage_ref(void)
1688 fprintf(stderr,
1689 "usage: %s ref [-r repository] -l | -d name | name object\n",
1690 getprogname());
1691 exit(1);
1694 static const struct got_error *
1695 list_refs(struct got_repository *repo)
1697 static const struct got_error *err = NULL;
1698 struct got_reflist_head refs;
1699 struct got_reflist_entry *re;
1701 SIMPLEQ_INIT(&refs);
1702 err = got_ref_list(&refs, repo);
1703 if (err)
1704 return err;
1706 SIMPLEQ_FOREACH(re, &refs, entry) {
1707 char *refstr;
1708 refstr = got_ref_to_str(re->ref);
1709 if (refstr == NULL)
1710 return got_error_from_errno();
1711 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1712 free(refstr);
1715 got_ref_list_free(&refs);
1716 return NULL;
1719 static const struct got_error *
1720 delete_ref(struct got_repository *repo, const char *refname)
1722 const struct got_error *err = NULL;
1723 struct got_reference *ref;
1725 err = got_ref_open(&ref, repo, refname);
1726 if (err)
1727 return err;
1729 err = got_ref_delete(ref, repo);
1730 got_ref_close(ref);
1731 return err;
1734 static const struct got_error *
1735 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1737 const struct got_error *err = NULL;
1738 struct got_object_id *id;
1739 struct got_reference *ref = NULL;
1741 err = got_object_resolve_id_str(&id, repo, id_str);
1742 if (err)
1743 return err;
1745 err = got_ref_alloc(&ref, refname, id);
1746 if (err)
1747 goto done;
1749 err = got_ref_write(ref, repo);
1750 done:
1751 if (ref)
1752 got_ref_close(ref);
1753 free(id);
1754 return err;
1757 static const struct got_error *
1758 cmd_ref(int argc, char *argv[])
1760 const struct got_error *error = NULL;
1761 struct got_repository *repo = NULL;
1762 struct got_worktree *worktree = NULL;
1763 char *cwd = NULL, *repo_path = NULL;
1764 int ch, do_list = 0;
1765 const char *delref = NULL;
1767 /* TODO: Add -s option for adding symbolic references. */
1768 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1769 switch (ch) {
1770 case 'd':
1771 delref = optarg;
1772 break;
1773 case 'r':
1774 repo_path = realpath(optarg, NULL);
1775 if (repo_path == NULL)
1776 err(1, "-r option");
1777 break;
1778 case 'l':
1779 do_list = 1;
1780 break;
1781 default:
1782 usage_ref();
1783 /* NOTREACHED */
1787 if (do_list && delref)
1788 errx(1, "-l and -d options are mutually exclusive\n");
1790 argc -= optind;
1791 argv += optind;
1793 if (do_list || delref) {
1794 if (argc > 0)
1795 usage_ref();
1796 } else if (argc != 2)
1797 usage_ref();
1799 #ifndef PROFILE
1800 if (do_list) {
1801 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1802 NULL) == -1)
1803 err(1, "pledge");
1804 } else {
1805 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1806 "sendfd unveil", NULL) == -1)
1807 err(1, "pledge");
1809 #endif
1810 cwd = getcwd(NULL, 0);
1811 if (cwd == NULL) {
1812 error = got_error_from_errno();
1813 goto done;
1816 if (repo_path == NULL) {
1817 error = got_worktree_open(&worktree, cwd);
1818 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1819 goto done;
1820 else
1821 error = NULL;
1822 if (worktree) {
1823 repo_path =
1824 strdup(got_worktree_get_repo_path(worktree));
1825 if (repo_path == NULL)
1826 error = got_error_from_errno();
1827 if (error)
1828 goto done;
1829 } else {
1830 repo_path = strdup(cwd);
1831 if (repo_path == NULL) {
1832 error = got_error_from_errno();
1833 goto done;
1838 error = got_repo_open(&repo, repo_path);
1839 if (error != NULL)
1840 goto done;
1842 error = apply_unveil(got_repo_get_path(repo), do_list,
1843 worktree ? got_worktree_get_root_path(worktree) : NULL);
1844 if (error)
1845 goto done;
1847 if (do_list)
1848 error = list_refs(repo);
1849 else if (delref)
1850 error = delete_ref(repo, delref);
1851 else
1852 error = add_ref(repo, argv[0], argv[1]);
1853 done:
1854 if (repo)
1855 got_repo_close(repo);
1856 if (worktree)
1857 got_worktree_close(worktree);
1858 free(cwd);
1859 free(repo_path);
1860 return error;
1863 __dead static void
1864 usage_add(void)
1866 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1867 exit(1);
1870 static const struct got_error *
1871 cmd_add(int argc, char *argv[])
1873 const struct got_error *error = NULL;
1874 struct got_repository *repo = NULL;
1875 struct got_worktree *worktree = NULL;
1876 char *cwd = NULL, *path = NULL, *relpath = NULL;
1877 int ch;
1879 while ((ch = getopt(argc, argv, "")) != -1) {
1880 switch (ch) {
1881 default:
1882 usage_add();
1883 /* NOTREACHED */
1887 argc -= optind;
1888 argv += optind;
1890 if (argc != 1)
1891 usage_add();
1893 path = realpath(argv[0], NULL);
1894 if (path == NULL) {
1895 error = got_error_from_errno();
1896 goto done;
1899 cwd = getcwd(NULL, 0);
1900 if (cwd == NULL) {
1901 error = got_error_from_errno();
1902 goto done;
1904 error = got_worktree_open(&worktree, cwd);
1905 if (error)
1906 goto done;
1908 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1909 if (error != NULL)
1910 goto done;
1912 error = apply_unveil(got_repo_get_path(repo), 1,
1913 got_worktree_get_root_path(worktree));
1914 if (error)
1915 goto done;
1917 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1918 repo);
1919 if (error)
1920 goto done;
1921 done:
1922 if (repo)
1923 got_repo_close(repo);
1924 if (worktree)
1925 got_worktree_close(worktree);
1926 free(path);
1927 free(relpath);
1928 free(cwd);
1929 return error;
1932 __dead static void
1933 usage_rm(void)
1935 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1936 exit(1);
1939 static const struct got_error *
1940 cmd_rm(int argc, char *argv[])
1942 const struct got_error *error = NULL;
1943 struct got_worktree *worktree = NULL;
1944 struct got_repository *repo = NULL;
1945 char *cwd = NULL, *path = NULL;
1946 int ch, delete_local_mods = 0;
1948 while ((ch = getopt(argc, argv, "f")) != -1) {
1949 switch (ch) {
1950 case 'f':
1951 delete_local_mods = 1;
1952 break;
1953 default:
1954 usage_add();
1955 /* NOTREACHED */
1959 argc -= optind;
1960 argv += optind;
1962 if (argc != 1)
1963 usage_rm();
1965 path = realpath(argv[0], NULL);
1966 if (path == NULL) {
1967 error = got_error_from_errno();
1968 goto done;
1971 cwd = getcwd(NULL, 0);
1972 if (cwd == NULL) {
1973 error = got_error_from_errno();
1974 goto done;
1976 error = got_worktree_open(&worktree, cwd);
1977 if (error)
1978 goto done;
1980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1981 if (error != NULL)
1982 goto done;
1984 error = apply_unveil(got_repo_get_path(repo), 1,
1985 got_worktree_get_root_path(worktree));
1986 if (error)
1987 goto done;
1989 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1990 print_status, NULL, repo);
1991 if (error)
1992 goto done;
1993 done:
1994 if (repo)
1995 got_repo_close(repo);
1996 if (worktree)
1997 got_worktree_close(worktree);
1998 free(path);
1999 free(cwd);
2000 return error;
2003 __dead static void
2004 usage_revert(void)
2006 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2007 exit(1);
2010 static void
2011 revert_progress(void *arg, unsigned char status, const char *path)
2013 while (path[0] == '/')
2014 path++;
2015 printf("%c %s\n", status, path);
2018 static const struct got_error *
2019 cmd_revert(int argc, char *argv[])
2021 const struct got_error *error = NULL;
2022 struct got_worktree *worktree = NULL;
2023 struct got_repository *repo = NULL;
2024 char *cwd = NULL, *path = NULL;
2025 int ch;
2027 while ((ch = getopt(argc, argv, "")) != -1) {
2028 switch (ch) {
2029 default:
2030 usage_revert();
2031 /* NOTREACHED */
2035 argc -= optind;
2036 argv += optind;
2038 if (argc != 1)
2039 usage_revert();
2041 path = realpath(argv[0], NULL);
2042 if (path == NULL) {
2043 error = got_error_from_errno();
2044 goto done;
2047 cwd = getcwd(NULL, 0);
2048 if (cwd == NULL) {
2049 error = got_error_from_errno();
2050 goto done;
2052 error = got_worktree_open(&worktree, cwd);
2053 if (error)
2054 goto done;
2056 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2057 if (error != NULL)
2058 goto done;
2060 error = apply_unveil(got_repo_get_path(repo), 1,
2061 got_worktree_get_root_path(worktree));
2062 if (error)
2063 goto done;
2065 error = got_worktree_revert(worktree, path,
2066 revert_progress, NULL, repo);
2067 if (error)
2068 goto done;
2069 done:
2070 if (repo)
2071 got_repo_close(repo);
2072 if (worktree)
2073 got_worktree_close(worktree);
2074 free(path);
2075 free(cwd);
2076 return error;
2079 __dead static void
2080 usage_commit(void)
2082 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2083 exit(1);
2086 static const struct got_error *
2087 cmd_commit(int argc, char *argv[])
2089 const struct got_error *error = NULL;
2090 struct got_worktree *worktree = NULL;
2091 struct got_repository *repo = NULL;
2092 char *cwd = NULL, *path = NULL, *id_str = NULL;
2093 struct got_object_id *id = NULL;
2094 const char *logmsg = "<no log message was specified>";
2095 const char *got_author = getenv("GOT_AUTHOR");
2096 int ch;
2098 while ((ch = getopt(argc, argv, "m:")) != -1) {
2099 switch (ch) {
2100 case 'm':
2101 logmsg = optarg;
2102 break;
2103 default:
2104 usage_commit();
2105 /* NOTREACHED */
2109 argc -= optind;
2110 argv += optind;
2112 if (argc == 1) {
2113 path = realpath(argv[0], NULL);
2114 if (path == NULL) {
2115 error = got_error_from_errno();
2116 goto done;
2118 } else if (argc != 0)
2119 usage_commit();
2121 if (got_author == NULL) {
2122 /* TODO: Look current user up in password database */
2123 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2124 goto done;
2127 cwd = getcwd(NULL, 0);
2128 if (cwd == NULL) {
2129 error = got_error_from_errno();
2130 goto done;
2132 error = got_worktree_open(&worktree, cwd);
2133 if (error)
2134 goto done;
2136 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2137 if (error != NULL)
2138 goto done;
2140 error = apply_unveil(got_repo_get_path(repo), 0,
2141 got_worktree_get_root_path(worktree));
2142 if (error)
2143 goto done;
2145 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2146 logmsg, print_status, NULL, repo);
2147 if (error)
2148 goto done;
2150 error = got_object_id_str(&id_str, id);
2151 if (error)
2152 goto done;
2153 printf("created commit %s\n", id_str);
2154 done:
2155 if (repo)
2156 got_repo_close(repo);
2157 if (worktree)
2158 got_worktree_close(worktree);
2159 free(path);
2160 free(cwd);
2161 free(id_str);
2162 return error;