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;
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 error = got_repo_open(&repo, repo_path);
385 if (error != NULL)
386 goto done;
388 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
389 if (error)
390 goto done;
392 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
393 if (error != NULL)
394 goto done;
396 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
397 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
398 goto done;
400 error = got_worktree_open(&worktree, worktree_path);
401 if (error != NULL)
402 goto done;
404 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
405 path_prefix);
406 if (error != NULL)
407 goto done;
408 if (!same_path_prefix) {
409 error = got_error(GOT_ERR_PATH_PREFIX);
410 goto done;
413 if (commit_id_str) {
414 struct got_object_id *commit_id;
415 error = got_object_resolve_id_str(&commit_id, repo,
416 commit_id_str);
417 if (error != NULL)
418 goto done;
419 error = check_ancestry(worktree, commit_id, repo);
420 if (error != NULL) {
421 free(commit_id);
422 goto done;
424 error = got_worktree_set_base_commit_id(worktree, repo,
425 commit_id);
426 free(commit_id);
427 if (error)
428 goto done;
431 error = got_worktree_checkout_files(worktree, "", repo,
432 checkout_progress, worktree_path, check_cancelled, NULL);
433 if (error != NULL)
434 goto done;
436 printf("Now shut up and hack\n");
438 done:
439 free(commit_id_str);
440 free(repo_path);
441 free(worktree_path);
442 return error;
445 __dead static void
446 usage_update(void)
448 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
449 getprogname());
450 exit(1);
453 static void
454 update_progress(void *arg, unsigned char status, const char *path)
456 int *did_something = arg;
458 if (status == GOT_STATUS_EXISTS)
459 return;
461 *did_something = 1;
462 while (path[0] == '/')
463 path++;
464 printf("%c %s\n", status, path);
467 static const struct got_error *
468 cmd_update(int argc, char *argv[])
470 const struct got_error *error = NULL;
471 struct got_repository *repo = NULL;
472 struct got_worktree *worktree = NULL;
473 char *worktree_path = NULL, *path = NULL;
474 struct got_object_id *commit_id = NULL;
475 char *commit_id_str = NULL;
476 int ch, did_something = 0;
478 while ((ch = getopt(argc, argv, "c:")) != -1) {
479 switch (ch) {
480 case 'c':
481 commit_id_str = strdup(optarg);
482 if (commit_id_str == NULL)
483 return got_error_from_errno();
484 break;
485 default:
486 usage_update();
487 /* NOTREACHED */
491 argc -= optind;
492 argv += optind;
494 #ifndef PROFILE
495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
496 "unveil", NULL) == -1)
497 err(1, "pledge");
498 #endif
499 worktree_path = getcwd(NULL, 0);
500 if (worktree_path == NULL) {
501 error = got_error_from_errno();
502 goto done;
504 error = got_worktree_open(&worktree, worktree_path);
505 if (error)
506 goto done;
508 if (argc == 0) {
509 path = strdup("");
510 if (path == NULL) {
511 error = got_error_from_errno();
512 goto done;
514 } else if (argc == 1) {
515 error = got_worktree_resolve_path(&path, worktree, argv[0]);
516 if (error)
517 goto done;
518 } else
519 usage_update();
521 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
522 if (error != NULL)
523 goto done;
525 error = apply_unveil(got_repo_get_path(repo), 0,
526 got_worktree_get_root_path(worktree));
527 if (error)
528 goto done;
530 if (commit_id_str == NULL) {
531 struct got_reference *head_ref;
532 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
533 if (error != NULL)
534 goto done;
535 error = got_ref_resolve(&commit_id, repo, head_ref);
536 if (error != NULL)
537 goto done;
538 error = got_object_id_str(&commit_id_str, commit_id);
539 if (error != NULL)
540 goto done;
541 } else {
542 error = got_object_resolve_id_str(&commit_id, repo,
543 commit_id_str);
544 if (error != NULL)
545 goto done;
548 error = check_ancestry(worktree, commit_id, repo);
549 if (error != NULL)
550 goto done;
552 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
553 commit_id) != 0) {
554 error = got_worktree_set_base_commit_id(worktree, repo,
555 commit_id);
556 if (error)
557 goto done;
560 error = got_worktree_checkout_files(worktree, path, repo,
561 update_progress, &did_something, check_cancelled, NULL);
562 if (error != NULL)
563 goto done;
565 if (did_something)
566 printf("Updated to commit %s\n", commit_id_str);
567 else
568 printf("Already up-to-date\n");
569 done:
570 free(worktree_path);
571 free(path);
572 free(commit_id);
573 free(commit_id_str);
574 return error;
577 static const struct got_error *
578 print_patch(struct got_commit_object *commit, struct got_object_id *id,
579 int diff_context, struct got_repository *repo)
581 const struct got_error *err = NULL;
582 struct got_tree_object *tree1 = NULL, *tree2;
583 struct got_object_qid *qid;
584 char *id_str1 = NULL, *id_str2;
586 err = got_object_open_as_tree(&tree2, repo,
587 got_object_commit_get_tree_id(commit));
588 if (err)
589 return err;
591 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
592 if (qid != NULL) {
593 struct got_commit_object *pcommit;
595 err = got_object_open_as_commit(&pcommit, repo, qid->id);
596 if (err)
597 return err;
599 err = got_object_open_as_tree(&tree1, repo,
600 got_object_commit_get_tree_id(pcommit));
601 got_object_commit_close(pcommit);
602 if (err)
603 return err;
605 err = got_object_id_str(&id_str1, qid->id);
606 if (err)
607 return err;
610 err = got_object_id_str(&id_str2, id);
611 if (err)
612 goto done;
614 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
615 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
616 done:
617 if (tree1)
618 got_object_tree_close(tree1);
619 got_object_tree_close(tree2);
620 free(id_str1);
621 free(id_str2);
622 return err;
625 static char *
626 get_datestr(time_t *time, char *datebuf)
628 char *p, *s = ctime_r(time, datebuf);
629 p = strchr(s, '\n');
630 if (p)
631 *p = '\0';
632 return s;
635 static const struct got_error *
636 print_commit(struct got_commit_object *commit, struct got_object_id *id,
637 struct got_repository *repo, int show_patch, int diff_context,
638 struct got_reflist_head *refs)
640 const struct got_error *err = NULL;
641 char *id_str, *datestr, *logmsg0, *logmsg, *line;
642 char datebuf[26];
643 time_t committer_time;
644 const char *author, *committer;
645 char *refs_str = NULL;
646 struct got_reflist_entry *re;
648 SIMPLEQ_FOREACH(re, refs, entry) {
649 char *s;
650 const char *name;
651 if (got_object_id_cmp(re->id, id) != 0)
652 continue;
653 name = got_ref_get_name(re->ref);
654 if (strcmp(name, GOT_REF_HEAD) == 0)
655 continue;
656 if (strncmp(name, "refs/", 5) == 0)
657 name += 5;
658 if (strncmp(name, "got/", 4) == 0)
659 continue;
660 if (strncmp(name, "heads/", 6) == 0)
661 name += 6;
662 if (strncmp(name, "remotes/", 8) == 0)
663 name += 8;
664 s = refs_str;
665 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
666 name) == -1) {
667 err = got_error_from_errno();
668 free(s);
669 break;
671 free(s);
673 err = got_object_id_str(&id_str, id);
674 if (err)
675 return err;
677 printf("-----------------------------------------------\n");
678 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
679 refs_str ? refs_str : "", refs_str ? ")" : "");
680 free(id_str);
681 id_str = NULL;
682 free(refs_str);
683 refs_str = NULL;
684 printf("from: %s\n", got_object_commit_get_author(commit));
685 committer_time = got_object_commit_get_committer_time(commit);
686 datestr = get_datestr(&committer_time, datebuf);
687 printf("date: %s UTC\n", datestr);
688 author = got_object_commit_get_author(commit);
689 committer = got_object_commit_get_committer(commit);
690 if (strcmp(author, committer) != 0)
691 printf("via: %s\n", committer);
692 if (got_object_commit_get_nparents(commit) > 1) {
693 const struct got_object_id_queue *parent_ids;
694 struct got_object_qid *qid;
695 int n = 1;
696 parent_ids = got_object_commit_get_parent_ids(commit);
697 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
698 err = got_object_id_str(&id_str, qid->id);
699 if (err)
700 return err;
701 printf("parent %d: %s\n", n++, id_str);
702 free(id_str);
706 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
707 if (logmsg0 == NULL)
708 return got_error_from_errno();
710 logmsg = logmsg0;
711 do {
712 line = strsep(&logmsg, "\n");
713 if (line)
714 printf(" %s\n", line);
715 } while (line);
716 free(logmsg0);
718 if (show_patch) {
719 err = print_patch(commit, id, diff_context, repo);
720 if (err == 0)
721 printf("\n");
724 if (fflush(stdout) != 0 && err == NULL)
725 err = got_error_from_errno();
726 return err;
729 static const struct got_error *
730 print_commits(struct got_object_id *root_id, struct got_repository *repo,
731 char *path, int show_patch, int diff_context, int limit,
732 int first_parent_traversal, struct got_reflist_head *refs)
734 const struct got_error *err;
735 struct got_commit_graph *graph;
737 err = got_commit_graph_open(&graph, root_id, path,
738 first_parent_traversal, repo);
739 if (err)
740 return err;
741 err = got_commit_graph_iter_start(graph, root_id, repo);
742 if (err)
743 goto done;
744 while (1) {
745 struct got_commit_object *commit;
746 struct got_object_id *id;
748 if (sigint_received || sigpipe_received)
749 break;
751 err = got_commit_graph_iter_next(&id, graph);
752 if (err) {
753 if (err->code == GOT_ERR_ITER_COMPLETED) {
754 err = NULL;
755 break;
757 if (err->code != GOT_ERR_ITER_NEED_MORE)
758 break;
759 err = got_commit_graph_fetch_commits(graph, 1, repo);
760 if (err)
761 break;
762 else
763 continue;
765 if (id == NULL)
766 break;
768 err = got_object_open_as_commit(&commit, repo, id);
769 if (err)
770 break;
771 err = print_commit(commit, id, repo, show_patch, diff_context,
772 refs);
773 got_object_commit_close(commit);
774 if (err || (limit && --limit == 0))
775 break;
777 done:
778 got_commit_graph_close(graph);
779 return err;
782 __dead static void
783 usage_log(void)
785 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
786 "[-r repository-path] [path]\n", getprogname());
787 exit(1);
790 static const struct got_error *
791 cmd_log(int argc, char *argv[])
793 const struct got_error *error;
794 struct got_repository *repo = NULL;
795 struct got_worktree *worktree = NULL;
796 struct got_commit_object *commit = NULL;
797 struct got_object_id *id = NULL;
798 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
799 char *start_commit = NULL;
800 int diff_context = 3, ch;
801 int show_patch = 0, limit = 0, first_parent_traversal = 0;
802 const char *errstr;
803 struct got_reflist_head refs;
805 SIMPLEQ_INIT(&refs);
807 #ifndef PROFILE
808 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
809 NULL)
810 == -1)
811 err(1, "pledge");
812 #endif
814 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
815 switch (ch) {
816 case 'p':
817 show_patch = 1;
818 break;
819 case 'c':
820 start_commit = optarg;
821 break;
822 case 'C':
823 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
824 &errstr);
825 if (errstr != NULL)
826 err(1, "-C option %s", errstr);
827 break;
828 case 'l':
829 limit = strtonum(optarg, 1, INT_MAX, &errstr);
830 if (errstr != NULL)
831 err(1, "-l option %s", errstr);
832 break;
833 case 'f':
834 first_parent_traversal = 1;
835 break;
836 case 'r':
837 repo_path = realpath(optarg, NULL);
838 if (repo_path == NULL)
839 err(1, "-r option");
840 break;
841 default:
842 usage_log();
843 /* NOTREACHED */
847 argc -= optind;
848 argv += optind;
850 cwd = getcwd(NULL, 0);
851 if (cwd == NULL) {
852 error = got_error_from_errno();
853 goto done;
856 error = got_worktree_open(&worktree, cwd);
857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
858 goto done;
859 error = NULL;
861 if (argc == 0) {
862 path = strdup("");
863 if (path == NULL) {
864 error = got_error_from_errno();
865 goto done;
867 } else if (argc == 1) {
868 if (worktree) {
869 error = got_worktree_resolve_path(&path, worktree,
870 argv[0]);
871 if (error)
872 goto done;
873 } else {
874 path = strdup(argv[0]);
875 if (path == NULL) {
876 error = got_error_from_errno();
877 goto done;
880 } else
881 usage_log();
883 repo_path = worktree ?
884 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
885 if (repo_path == NULL) {
886 error = got_error_from_errno();
887 goto done;
890 error = got_repo_open(&repo, repo_path);
891 if (error != NULL)
892 goto done;
894 error = apply_unveil(got_repo_get_path(repo), 1,
895 worktree ? got_worktree_get_root_path(worktree) : NULL);
896 if (error)
897 goto done;
899 if (start_commit == NULL) {
900 struct got_reference *head_ref;
901 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
902 if (error != NULL)
903 return error;
904 error = got_ref_resolve(&id, repo, head_ref);
905 got_ref_close(head_ref);
906 if (error != NULL)
907 return error;
908 error = got_object_open_as_commit(&commit, repo, id);
909 } else {
910 struct got_reference *ref;
911 error = got_ref_open(&ref, repo, start_commit);
912 if (error == NULL) {
913 int obj_type;
914 error = got_ref_resolve(&id, repo, ref);
915 got_ref_close(ref);
916 if (error != NULL)
917 goto done;
918 error = got_object_get_type(&obj_type, repo, id);
919 if (error != NULL)
920 goto done;
921 if (obj_type == GOT_OBJ_TYPE_TAG) {
922 struct got_tag_object *tag;
923 error = got_object_open_as_tag(&tag, repo, id);
924 if (error != NULL)
925 goto done;
926 if (got_object_tag_get_object_type(tag) !=
927 GOT_OBJ_TYPE_COMMIT) {
928 got_object_tag_close(tag);
929 error = got_error(GOT_ERR_OBJ_TYPE);
930 goto done;
932 free(id);
933 id = got_object_id_dup(
934 got_object_tag_get_object_id(tag));
935 if (id == NULL)
936 error = got_error_from_errno();
937 got_object_tag_close(tag);
938 if (error)
939 goto done;
940 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
941 error = got_error(GOT_ERR_OBJ_TYPE);
942 goto done;
944 error = got_object_open_as_commit(&commit, repo, id);
945 if (error != NULL)
946 goto done;
948 if (commit == NULL) {
949 error = got_object_resolve_id_str(&id, repo,
950 start_commit);
951 if (error != NULL)
952 return error;
955 if (error != NULL)
956 goto done;
958 error = got_repo_map_path(&in_repo_path, repo, path, 1);
959 if (error != NULL)
960 goto done;
961 if (in_repo_path) {
962 free(path);
963 path = in_repo_path;
966 error = got_ref_list(&refs, repo);
967 if (error)
968 goto done;
970 error = print_commits(id, repo, path, show_patch,
971 diff_context, limit, first_parent_traversal, &refs);
972 done:
973 free(path);
974 free(repo_path);
975 free(cwd);
976 free(id);
977 if (worktree)
978 got_worktree_close(worktree);
979 if (repo) {
980 const struct got_error *repo_error;
981 repo_error = got_repo_close(repo);
982 if (error == NULL)
983 error = repo_error;
985 got_ref_list_free(&refs);
986 return error;
989 __dead static void
990 usage_diff(void)
992 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
993 "[object1 object2 | path]\n", getprogname());
994 exit(1);
997 struct print_diff_arg {
998 struct got_repository *repo;
999 struct got_worktree *worktree;
1000 int diff_context;
1001 const char *id_str;
1002 int header_shown;
1005 static const struct got_error *
1006 print_diff(void *arg, unsigned char status, const char *path,
1007 struct got_object_id *id)
1009 struct print_diff_arg *a = arg;
1010 const struct got_error *err = NULL;
1011 struct got_blob_object *blob1 = NULL;
1012 FILE *f2 = NULL;
1013 char *abspath = NULL;
1014 struct stat sb;
1016 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1017 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1018 return NULL;
1020 if (!a->header_shown) {
1021 printf("diff %s %s\n", a->id_str,
1022 got_worktree_get_root_path(a->worktree));
1023 a->header_shown = 1;
1026 if (status != GOT_STATUS_ADD) {
1027 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1028 if (err)
1029 goto done;
1033 if (status != GOT_STATUS_DELETE) {
1034 if (asprintf(&abspath, "%s/%s",
1035 got_worktree_get_root_path(a->worktree), path) == -1) {
1036 err = got_error_from_errno();
1037 goto done;
1040 f2 = fopen(abspath, "r");
1041 if (f2 == NULL) {
1042 err = got_error_from_errno();
1043 goto done;
1045 if (lstat(abspath, &sb) == -1) {
1046 err = got_error_from_errno();
1047 goto done;
1049 } else
1050 sb.st_size = 0;
1052 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1053 stdout);
1054 done:
1055 if (blob1)
1056 got_object_blob_close(blob1);
1057 if (f2 && fclose(f2) != 0 && err == NULL)
1058 err = got_error_from_errno();
1059 free(abspath);
1060 return err;
1063 static const struct got_error *
1064 cmd_diff(int argc, char *argv[])
1066 const struct got_error *error;
1067 struct got_repository *repo = NULL;
1068 struct got_worktree *worktree = NULL;
1069 char *cwd = NULL, *repo_path = NULL;
1070 struct got_object_id *id1 = NULL, *id2 = NULL;
1071 char *id_str1 = NULL, *id_str2 = NULL;
1072 int type1, type2;
1073 int diff_context = 3, ch;
1074 const char *errstr;
1075 char *path = NULL;
1077 #ifndef PROFILE
1078 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1079 NULL) == -1)
1080 err(1, "pledge");
1081 #endif
1083 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1084 switch (ch) {
1085 case 'C':
1086 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1087 if (errstr != NULL)
1088 err(1, "-C option %s", errstr);
1089 break;
1090 case 'r':
1091 repo_path = realpath(optarg, NULL);
1092 if (repo_path == NULL)
1093 err(1, "-r option");
1094 break;
1095 default:
1096 usage_diff();
1097 /* NOTREACHED */
1101 argc -= optind;
1102 argv += optind;
1104 cwd = getcwd(NULL, 0);
1105 if (cwd == NULL) {
1106 error = got_error_from_errno();
1107 goto done;
1109 error = got_worktree_open(&worktree, cwd);
1110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1111 goto done;
1112 if (argc <= 1) {
1113 if (worktree == NULL) {
1114 error = got_error(GOT_ERR_NOT_WORKTREE);
1115 goto done;
1117 if (repo_path)
1118 errx(1,
1119 "-r option can't be used when diffing a work tree");
1120 repo_path = strdup(got_worktree_get_repo_path(worktree));
1121 if (repo_path == NULL) {
1122 error = got_error_from_errno();
1123 goto done;
1125 if (argc == 1) {
1126 error = got_worktree_resolve_path(&path, worktree,
1127 argv[0]);
1128 if (error)
1129 goto done;
1130 } else {
1131 path = strdup("");
1132 if (path == NULL) {
1133 error = got_error_from_errno();
1134 goto done;
1137 } else if (argc == 2) {
1138 id_str1 = argv[0];
1139 id_str2 = argv[1];
1140 } else
1141 usage_diff();
1143 if (repo_path == NULL) {
1144 repo_path = getcwd(NULL, 0);
1145 if (repo_path == NULL)
1146 return got_error_from_errno();
1149 error = got_repo_open(&repo, repo_path);
1150 free(repo_path);
1151 if (error != NULL)
1152 goto done;
1154 error = apply_unveil(got_repo_get_path(repo), 1,
1155 worktree ? got_worktree_get_root_path(worktree) : NULL);
1156 if (error)
1157 goto done;
1159 if (worktree) {
1160 struct print_diff_arg arg;
1161 char *id_str;
1162 error = got_object_id_str(&id_str,
1163 got_worktree_get_base_commit_id(worktree));
1164 if (error)
1165 goto done;
1166 arg.repo = repo;
1167 arg.worktree = worktree;
1168 arg.diff_context = diff_context;
1169 arg.id_str = id_str;
1170 arg.header_shown = 0;
1172 error = got_worktree_status(worktree, path, repo, print_diff,
1173 &arg, check_cancelled, NULL);
1174 free(id_str);
1175 goto done;
1178 error = got_object_resolve_id_str(&id1, repo, id_str1);
1179 if (error)
1180 goto done;
1182 error = got_object_resolve_id_str(&id2, repo, id_str2);
1183 if (error)
1184 goto done;
1186 error = got_object_get_type(&type1, repo, id1);
1187 if (error)
1188 goto done;
1190 error = got_object_get_type(&type2, repo, id2);
1191 if (error)
1192 goto done;
1194 if (type1 != type2) {
1195 error = got_error(GOT_ERR_OBJ_TYPE);
1196 goto done;
1199 switch (type1) {
1200 case GOT_OBJ_TYPE_BLOB:
1201 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1202 diff_context, repo, stdout);
1203 break;
1204 case GOT_OBJ_TYPE_TREE:
1205 error = got_diff_objects_as_trees(id1, id2, "", "",
1206 diff_context, repo, stdout);
1207 break;
1208 case GOT_OBJ_TYPE_COMMIT:
1209 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1210 id_str2);
1211 error = got_diff_objects_as_commits(id1, id2, diff_context,
1212 repo, stdout);
1213 break;
1214 default:
1215 error = got_error(GOT_ERR_OBJ_TYPE);
1218 done:
1219 free(id1);
1220 free(id2);
1221 free(path);
1222 if (worktree)
1223 got_worktree_close(worktree);
1224 if (repo) {
1225 const struct got_error *repo_error;
1226 repo_error = got_repo_close(repo);
1227 if (error == NULL)
1228 error = repo_error;
1230 return error;
1233 __dead static void
1234 usage_blame(void)
1236 fprintf(stderr,
1237 "usage: %s blame [-c commit] [-r repository-path] path\n",
1238 getprogname());
1239 exit(1);
1242 static const struct got_error *
1243 cmd_blame(int argc, char *argv[])
1245 const struct got_error *error;
1246 struct got_repository *repo = NULL;
1247 struct got_worktree *worktree = NULL;
1248 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1249 struct got_object_id *commit_id = NULL;
1250 char *commit_id_str = NULL;
1251 int ch;
1253 #ifndef PROFILE
1254 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1255 NULL) == -1)
1256 err(1, "pledge");
1257 #endif
1259 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1260 switch (ch) {
1261 case 'c':
1262 commit_id_str = optarg;
1263 break;
1264 case 'r':
1265 repo_path = realpath(optarg, NULL);
1266 if (repo_path == NULL)
1267 err(1, "-r option");
1268 break;
1269 default:
1270 usage_blame();
1271 /* NOTREACHED */
1275 argc -= optind;
1276 argv += optind;
1278 if (argc == 1)
1279 path = argv[0];
1280 else
1281 usage_blame();
1283 cwd = getcwd(NULL, 0);
1284 if (cwd == NULL) {
1285 error = got_error_from_errno();
1286 goto done;
1288 if (repo_path == NULL) {
1289 error = got_worktree_open(&worktree, cwd);
1290 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1291 goto done;
1292 else
1293 error = NULL;
1294 if (worktree) {
1295 repo_path =
1296 strdup(got_worktree_get_repo_path(worktree));
1297 if (repo_path == NULL)
1298 error = got_error_from_errno();
1299 if (error)
1300 goto done;
1301 } else {
1302 repo_path = strdup(cwd);
1303 if (repo_path == NULL) {
1304 error = got_error_from_errno();
1305 goto done;
1310 error = got_repo_open(&repo, repo_path);
1311 if (error != NULL)
1312 goto done;
1314 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1315 if (error)
1316 goto done;
1318 if (worktree) {
1319 const char *prefix = got_worktree_get_path_prefix(worktree);
1320 char *p, *worktree_subdir = cwd +
1321 strlen(got_worktree_get_root_path(worktree));
1322 if (asprintf(&p, "%s%s%s%s%s",
1323 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1324 worktree_subdir, worktree_subdir[0] ? "/" : "",
1325 path) == -1) {
1326 error = got_error_from_errno();
1327 goto done;
1329 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1330 free(p);
1331 } else {
1332 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1334 if (error)
1335 goto done;
1337 if (commit_id_str == NULL) {
1338 struct got_reference *head_ref;
1339 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1340 if (error != NULL)
1341 goto done;
1342 error = got_ref_resolve(&commit_id, repo, head_ref);
1343 got_ref_close(head_ref);
1344 if (error != NULL)
1345 goto done;
1346 } else {
1347 error = got_object_resolve_id_str(&commit_id, repo,
1348 commit_id_str);
1349 if (error != NULL)
1350 goto done;
1353 error = got_blame(in_repo_path, commit_id, repo, stdout);
1354 done:
1355 free(in_repo_path);
1356 free(repo_path);
1357 free(cwd);
1358 free(commit_id);
1359 if (worktree)
1360 got_worktree_close(worktree);
1361 if (repo) {
1362 const struct got_error *repo_error;
1363 repo_error = got_repo_close(repo);
1364 if (error == NULL)
1365 error = repo_error;
1367 return error;
1370 __dead static void
1371 usage_tree(void)
1373 fprintf(stderr,
1374 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1375 getprogname());
1376 exit(1);
1379 static void
1380 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1381 const char *root_path)
1383 int is_root_path = (strcmp(path, root_path) == 0);
1385 path += strlen(root_path);
1386 while (path[0] == '/')
1387 path++;
1389 printf("%s%s%s%s%s\n", id ? id : "", path,
1390 is_root_path ? "" : "/", te->name,
1391 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1394 static const struct got_error *
1395 print_tree(const char *path, struct got_object_id *commit_id,
1396 int show_ids, int recurse, const char *root_path,
1397 struct got_repository *repo)
1399 const struct got_error *err = NULL;
1400 struct got_object_id *tree_id = NULL;
1401 struct got_tree_object *tree = NULL;
1402 const struct got_tree_entries *entries;
1403 struct got_tree_entry *te;
1405 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1406 if (err)
1407 goto done;
1409 err = got_object_open_as_tree(&tree, repo, tree_id);
1410 if (err)
1411 goto done;
1412 entries = got_object_tree_get_entries(tree);
1413 te = SIMPLEQ_FIRST(&entries->head);
1414 while (te) {
1415 char *id = NULL;
1417 if (sigint_received || sigpipe_received)
1418 break;
1420 if (show_ids) {
1421 char *id_str;
1422 err = got_object_id_str(&id_str, te->id);
1423 if (err)
1424 goto done;
1425 if (asprintf(&id, "%s ", id_str) == -1) {
1426 err = got_error_from_errno();
1427 free(id_str);
1428 goto done;
1430 free(id_str);
1432 print_entry(te, id, path, root_path);
1433 free(id);
1435 if (recurse && S_ISDIR(te->mode)) {
1436 char *child_path;
1437 if (asprintf(&child_path, "%s%s%s", path,
1438 path[0] == '/' && path[1] == '\0' ? "" : "/",
1439 te->name) == -1) {
1440 err = got_error_from_errno();
1441 goto done;
1443 err = print_tree(child_path, commit_id, show_ids, 1,
1444 root_path, repo);
1445 free(child_path);
1446 if (err)
1447 goto done;
1450 te = SIMPLEQ_NEXT(te, entry);
1452 done:
1453 if (tree)
1454 got_object_tree_close(tree);
1455 free(tree_id);
1456 return err;
1459 static const struct got_error *
1460 cmd_tree(int argc, char *argv[])
1462 const struct got_error *error;
1463 struct got_repository *repo = NULL;
1464 struct got_worktree *worktree = NULL;
1465 const char *path;
1466 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1467 struct got_object_id *commit_id = NULL;
1468 char *commit_id_str = NULL;
1469 int show_ids = 0, recurse = 0;
1470 int ch;
1472 #ifndef PROFILE
1473 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1474 NULL) == -1)
1475 err(1, "pledge");
1476 #endif
1478 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1479 switch (ch) {
1480 case 'c':
1481 commit_id_str = optarg;
1482 break;
1483 case 'r':
1484 repo_path = realpath(optarg, NULL);
1485 if (repo_path == NULL)
1486 err(1, "-r option");
1487 break;
1488 case 'i':
1489 show_ids = 1;
1490 break;
1491 case 'R':
1492 recurse = 1;
1493 break;
1494 default:
1495 usage_tree();
1496 /* NOTREACHED */
1500 argc -= optind;
1501 argv += optind;
1503 if (argc == 1)
1504 path = argv[0];
1505 else if (argc > 1)
1506 usage_tree();
1507 else
1508 path = NULL;
1510 cwd = getcwd(NULL, 0);
1511 if (cwd == NULL) {
1512 error = got_error_from_errno();
1513 goto done;
1515 if (repo_path == NULL) {
1516 error = got_worktree_open(&worktree, cwd);
1517 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1518 goto done;
1519 else
1520 error = NULL;
1521 if (worktree) {
1522 repo_path =
1523 strdup(got_worktree_get_repo_path(worktree));
1524 if (repo_path == NULL)
1525 error = got_error_from_errno();
1526 if (error)
1527 goto done;
1528 } else {
1529 repo_path = strdup(cwd);
1530 if (repo_path == NULL) {
1531 error = got_error_from_errno();
1532 goto done;
1537 error = got_repo_open(&repo, repo_path);
1538 if (error != NULL)
1539 goto done;
1541 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1542 if (error)
1543 goto done;
1545 if (path == NULL) {
1546 if (worktree) {
1547 char *p, *worktree_subdir = cwd +
1548 strlen(got_worktree_get_root_path(worktree));
1549 if (asprintf(&p, "%s/%s",
1550 got_worktree_get_path_prefix(worktree),
1551 worktree_subdir) == -1) {
1552 error = got_error_from_errno();
1553 goto done;
1555 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1556 free(p);
1557 if (error)
1558 goto done;
1559 } else
1560 path = "/";
1562 if (in_repo_path == NULL) {
1563 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1564 if (error != NULL)
1565 goto done;
1568 if (commit_id_str == NULL) {
1569 struct got_reference *head_ref;
1570 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1571 if (error != NULL)
1572 goto done;
1573 error = got_ref_resolve(&commit_id, repo, head_ref);
1574 got_ref_close(head_ref);
1575 if (error != NULL)
1576 goto done;
1577 } else {
1578 error = got_object_resolve_id_str(&commit_id, repo,
1579 commit_id_str);
1580 if (error != NULL)
1581 goto done;
1584 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1585 in_repo_path, repo);
1586 done:
1587 free(in_repo_path);
1588 free(repo_path);
1589 free(cwd);
1590 free(commit_id);
1591 if (worktree)
1592 got_worktree_close(worktree);
1593 if (repo) {
1594 const struct got_error *repo_error;
1595 repo_error = got_repo_close(repo);
1596 if (error == NULL)
1597 error = repo_error;
1599 return error;
1602 __dead static void
1603 usage_status(void)
1605 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1606 exit(1);
1609 static const struct got_error *
1610 print_status(void *arg, unsigned char status, const char *path,
1611 struct got_object_id *id)
1613 printf("%c %s\n", status, path);
1614 return NULL;
1617 static const struct got_error *
1618 cmd_status(int argc, char *argv[])
1620 const struct got_error *error = NULL;
1621 struct got_repository *repo = NULL;
1622 struct got_worktree *worktree = NULL;
1623 char *cwd = NULL, *path = NULL;
1624 int ch;
1626 while ((ch = getopt(argc, argv, "")) != -1) {
1627 switch (ch) {
1628 default:
1629 usage_status();
1630 /* NOTREACHED */
1634 argc -= optind;
1635 argv += optind;
1637 #ifndef PROFILE
1638 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1639 NULL) == -1)
1640 err(1, "pledge");
1641 #endif
1642 cwd = getcwd(NULL, 0);
1643 if (cwd == NULL) {
1644 error = got_error_from_errno();
1645 goto done;
1648 error = got_worktree_open(&worktree, cwd);
1649 if (error != NULL)
1650 goto done;
1652 if (argc == 0) {
1653 path = strdup("");
1654 if (path == NULL) {
1655 error = got_error_from_errno();
1656 goto done;
1658 } else if (argc == 1) {
1659 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1660 if (error)
1661 goto done;
1662 } else
1663 usage_status();
1665 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1666 if (error != NULL)
1667 goto done;
1669 error = apply_unveil(got_repo_get_path(repo), 1,
1670 got_worktree_get_root_path(worktree));
1671 if (error)
1672 goto done;
1674 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1675 check_cancelled, NULL);
1676 done:
1677 free(cwd);
1678 free(path);
1679 return error;
1682 __dead static void
1683 usage_ref(void)
1685 fprintf(stderr,
1686 "usage: %s ref [-r repository] -l | -d name | name object\n",
1687 getprogname());
1688 exit(1);
1691 static const struct got_error *
1692 list_refs(struct got_repository *repo)
1694 static const struct got_error *err = NULL;
1695 struct got_reflist_head refs;
1696 struct got_reflist_entry *re;
1698 SIMPLEQ_INIT(&refs);
1699 err = got_ref_list(&refs, repo);
1700 if (err)
1701 return err;
1703 SIMPLEQ_FOREACH(re, &refs, entry) {
1704 char *refstr;
1705 refstr = got_ref_to_str(re->ref);
1706 if (refstr == NULL)
1707 return got_error_from_errno();
1708 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1709 free(refstr);
1712 got_ref_list_free(&refs);
1713 return NULL;
1716 static const struct got_error *
1717 delete_ref(struct got_repository *repo, const char *refname)
1719 const struct got_error *err = NULL;
1720 struct got_reference *ref;
1722 err = got_ref_open(&ref, repo, refname);
1723 if (err)
1724 return err;
1726 err = got_ref_delete(ref, repo);
1727 got_ref_close(ref);
1728 return err;
1731 static const struct got_error *
1732 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1734 const struct got_error *err = NULL;
1735 struct got_object_id *id;
1736 struct got_reference *ref = NULL;
1738 err = got_object_resolve_id_str(&id, repo, id_str);
1739 if (err)
1740 return err;
1742 err = got_ref_alloc(&ref, refname, id);
1743 if (err)
1744 goto done;
1746 err = got_ref_write(ref, repo);
1747 done:
1748 if (ref)
1749 got_ref_close(ref);
1750 free(id);
1751 return err;
1754 static const struct got_error *
1755 cmd_ref(int argc, char *argv[])
1757 const struct got_error *error = NULL;
1758 struct got_repository *repo = NULL;
1759 struct got_worktree *worktree = NULL;
1760 char *cwd = NULL, *repo_path = NULL;
1761 int ch, do_list = 0;
1762 const char *delref = NULL;
1764 /* TODO: Add -s option for adding symbolic references. */
1765 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1766 switch (ch) {
1767 case 'd':
1768 delref = optarg;
1769 break;
1770 case 'r':
1771 repo_path = realpath(optarg, NULL);
1772 if (repo_path == NULL)
1773 err(1, "-r option");
1774 break;
1775 case 'l':
1776 do_list = 1;
1777 break;
1778 default:
1779 usage_ref();
1780 /* NOTREACHED */
1784 if (do_list && delref)
1785 errx(1, "-l and -d options are mutually exclusive\n");
1787 argc -= optind;
1788 argv += optind;
1790 if (do_list || delref) {
1791 if (argc > 0)
1792 usage_ref();
1793 } else if (argc != 2)
1794 usage_ref();
1796 #ifndef PROFILE
1797 if (do_list) {
1798 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1799 NULL) == -1)
1800 err(1, "pledge");
1801 } else {
1802 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1803 "sendfd unveil", NULL) == -1)
1804 err(1, "pledge");
1806 #endif
1807 cwd = getcwd(NULL, 0);
1808 if (cwd == NULL) {
1809 error = got_error_from_errno();
1810 goto done;
1813 if (repo_path == NULL) {
1814 error = got_worktree_open(&worktree, cwd);
1815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1816 goto done;
1817 else
1818 error = NULL;
1819 if (worktree) {
1820 repo_path =
1821 strdup(got_worktree_get_repo_path(worktree));
1822 if (repo_path == NULL)
1823 error = got_error_from_errno();
1824 if (error)
1825 goto done;
1826 } else {
1827 repo_path = strdup(cwd);
1828 if (repo_path == NULL) {
1829 error = got_error_from_errno();
1830 goto done;
1835 error = got_repo_open(&repo, repo_path);
1836 if (error != NULL)
1837 goto done;
1839 error = apply_unveil(got_repo_get_path(repo), do_list,
1840 worktree ? got_worktree_get_root_path(worktree) : NULL);
1841 if (error)
1842 goto done;
1844 if (do_list)
1845 error = list_refs(repo);
1846 else if (delref)
1847 error = delete_ref(repo, delref);
1848 else
1849 error = add_ref(repo, argv[0], argv[1]);
1850 done:
1851 if (repo)
1852 got_repo_close(repo);
1853 if (worktree)
1854 got_worktree_close(worktree);
1855 free(cwd);
1856 free(repo_path);
1857 return error;
1860 __dead static void
1861 usage_add(void)
1863 fprintf(stderr, "usage: %s add file-path\n", getprogname());
1864 exit(1);
1867 static const struct got_error *
1868 cmd_add(int argc, char *argv[])
1870 const struct got_error *error = NULL;
1871 struct got_repository *repo = NULL;
1872 struct got_worktree *worktree = NULL;
1873 char *cwd = NULL, *path = NULL, *relpath = NULL;
1874 int ch;
1876 while ((ch = getopt(argc, argv, "")) != -1) {
1877 switch (ch) {
1878 default:
1879 usage_add();
1880 /* NOTREACHED */
1884 argc -= optind;
1885 argv += optind;
1887 if (argc != 1)
1888 usage_add();
1890 path = realpath(argv[0], NULL);
1891 if (path == NULL) {
1892 error = got_error_from_errno();
1893 goto done;
1896 cwd = getcwd(NULL, 0);
1897 if (cwd == NULL) {
1898 error = got_error_from_errno();
1899 goto done;
1901 error = got_worktree_open(&worktree, cwd);
1902 if (error)
1903 goto done;
1905 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1906 if (error != NULL)
1907 goto done;
1909 error = apply_unveil(got_repo_get_path(repo), 1,
1910 got_worktree_get_root_path(worktree));
1911 if (error)
1912 goto done;
1914 error = got_worktree_schedule_add(worktree, path, print_status, NULL,
1915 repo);
1916 if (error)
1917 goto done;
1918 done:
1919 if (repo)
1920 got_repo_close(repo);
1921 if (worktree)
1922 got_worktree_close(worktree);
1923 free(path);
1924 free(relpath);
1925 free(cwd);
1926 return error;
1929 __dead static void
1930 usage_rm(void)
1932 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
1933 exit(1);
1936 static const struct got_error *
1937 cmd_rm(int argc, char *argv[])
1939 const struct got_error *error = NULL;
1940 struct got_worktree *worktree = NULL;
1941 struct got_repository *repo = NULL;
1942 char *cwd = NULL, *path = NULL;
1943 int ch, delete_local_mods = 0;
1945 while ((ch = getopt(argc, argv, "f")) != -1) {
1946 switch (ch) {
1947 case 'f':
1948 delete_local_mods = 1;
1949 break;
1950 default:
1951 usage_add();
1952 /* NOTREACHED */
1956 argc -= optind;
1957 argv += optind;
1959 if (argc != 1)
1960 usage_rm();
1962 path = realpath(argv[0], NULL);
1963 if (path == NULL) {
1964 error = got_error_from_errno();
1965 goto done;
1968 cwd = getcwd(NULL, 0);
1969 if (cwd == NULL) {
1970 error = got_error_from_errno();
1971 goto done;
1973 error = got_worktree_open(&worktree, cwd);
1974 if (error)
1975 goto done;
1977 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1978 if (error != NULL)
1979 goto done;
1981 error = apply_unveil(got_repo_get_path(repo), 1,
1982 got_worktree_get_root_path(worktree));
1983 if (error)
1984 goto done;
1986 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
1987 print_status, NULL, repo);
1988 if (error)
1989 goto done;
1990 done:
1991 if (repo)
1992 got_repo_close(repo);
1993 if (worktree)
1994 got_worktree_close(worktree);
1995 free(path);
1996 free(cwd);
1997 return error;
2000 __dead static void
2001 usage_revert(void)
2003 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2004 exit(1);
2007 static void
2008 revert_progress(void *arg, unsigned char status, const char *path)
2010 while (path[0] == '/')
2011 path++;
2012 printf("%c %s\n", status, path);
2015 static const struct got_error *
2016 cmd_revert(int argc, char *argv[])
2018 const struct got_error *error = NULL;
2019 struct got_worktree *worktree = NULL;
2020 struct got_repository *repo = NULL;
2021 char *cwd = NULL, *path = NULL;
2022 int ch;
2024 while ((ch = getopt(argc, argv, "")) != -1) {
2025 switch (ch) {
2026 default:
2027 usage_revert();
2028 /* NOTREACHED */
2032 argc -= optind;
2033 argv += optind;
2035 if (argc != 1)
2036 usage_revert();
2038 path = realpath(argv[0], NULL);
2039 if (path == NULL) {
2040 error = got_error_from_errno();
2041 goto done;
2044 cwd = getcwd(NULL, 0);
2045 if (cwd == NULL) {
2046 error = got_error_from_errno();
2047 goto done;
2049 error = got_worktree_open(&worktree, cwd);
2050 if (error)
2051 goto done;
2053 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2054 if (error != NULL)
2055 goto done;
2057 error = apply_unveil(got_repo_get_path(repo), 1,
2058 got_worktree_get_root_path(worktree));
2059 if (error)
2060 goto done;
2062 error = got_worktree_revert(worktree, path,
2063 revert_progress, NULL, repo);
2064 if (error)
2065 goto done;
2066 done:
2067 if (repo)
2068 got_repo_close(repo);
2069 if (worktree)
2070 got_worktree_close(worktree);
2071 free(path);
2072 free(cwd);
2073 return error;
2076 __dead static void
2077 usage_commit(void)
2079 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2080 exit(1);
2083 static const struct got_error *
2084 cmd_commit(int argc, char *argv[])
2086 const struct got_error *error = NULL;
2087 struct got_worktree *worktree = NULL;
2088 struct got_repository *repo = NULL;
2089 char *cwd = NULL, *path = NULL, *id_str = NULL;
2090 struct got_object_id *id = NULL;
2091 const char *logmsg = "<no log message was specified>";
2092 const char *got_author = getenv("GOT_AUTHOR");
2093 int ch;
2095 while ((ch = getopt(argc, argv, "m:")) != -1) {
2096 switch (ch) {
2097 case 'm':
2098 logmsg = optarg;
2099 break;
2100 default:
2101 usage_commit();
2102 /* NOTREACHED */
2106 argc -= optind;
2107 argv += optind;
2109 if (argc == 1) {
2110 path = realpath(argv[0], NULL);
2111 if (path == NULL) {
2112 error = got_error_from_errno();
2113 goto done;
2115 } else if (argc != 0)
2116 usage_commit();
2118 if (got_author == NULL) {
2119 /* TODO: Look current user up in password database */
2120 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2121 goto done;
2124 cwd = getcwd(NULL, 0);
2125 if (cwd == NULL) {
2126 error = got_error_from_errno();
2127 goto done;
2129 error = got_worktree_open(&worktree, cwd);
2130 if (error)
2131 goto done;
2133 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2134 if (error != NULL)
2135 goto done;
2137 error = apply_unveil(got_repo_get_path(repo), 0,
2138 got_worktree_get_root_path(worktree));
2139 if (error)
2140 goto done;
2142 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2143 logmsg, print_status, NULL, repo);
2144 if (error)
2145 goto done;
2147 error = got_object_id_str(&id_str, id);
2148 if (error)
2149 goto done;
2150 printf("created commit %s\n", id_str);
2151 done:
2152 if (repo)
2153 got_repo_close(repo);
2154 if (worktree)
2155 got_worktree_close(worktree);
2156 free(path);
2157 free(cwd);
2158 free(id_str);
2159 return error;