Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23 #include <sys/wait.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <locale.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <libgen.h>
34 #include <time.h>
35 #include <paths.h>
37 #include "got_error.h"
38 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository.h"
41 #include "got_path.h"
42 #include "got_worktree.h"
43 #include "got_diff.h"
44 #include "got_commit_graph.h"
45 #include "got_blame.h"
46 #include "got_privsep.h"
47 #include "got_opentemp.h"
49 #ifndef nitems
50 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
51 #endif
53 static volatile sig_atomic_t sigint_received;
54 static volatile sig_atomic_t sigpipe_received;
56 static void
57 catch_sigint(int signo)
58 {
59 sigint_received = 1;
60 }
62 static void
63 catch_sigpipe(int signo)
64 {
65 sigpipe_received = 1;
66 }
69 struct cmd {
70 const char *cmd_name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 const char *cmd_descr;
74 };
76 __dead static void usage(void);
77 __dead static void usage_checkout(void);
78 __dead static void usage_update(void);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
83 __dead static void usage_status(void);
84 __dead static void usage_ref(void);
85 __dead static void usage_add(void);
86 __dead static void usage_rm(void);
87 __dead static void usage_revert(void);
88 __dead static void usage_commit(void);
90 static const struct got_error* cmd_checkout(int, char *[]);
91 static const struct got_error* cmd_update(int, char *[]);
92 static const struct got_error* cmd_log(int, char *[]);
93 static const struct got_error* cmd_diff(int, char *[]);
94 static const struct got_error* cmd_blame(int, char *[]);
95 static const struct got_error* cmd_tree(int, char *[]);
96 static const struct got_error* cmd_status(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
98 static const struct got_error* cmd_add(int, char *[]);
99 static const struct got_error* cmd_rm(int, char *[]);
100 static const struct got_error* cmd_revert(int, char *[]);
101 static const struct got_error* cmd_commit(int, char *[]);
103 static struct cmd got_commands[] = {
104 { "checkout", cmd_checkout, usage_checkout,
105 "check out a new work tree from a repository" },
106 { "update", cmd_update, usage_update,
107 "update a work tree to a different commit" },
108 { "log", cmd_log, usage_log,
109 "show repository history" },
110 { "diff", cmd_diff, usage_diff,
111 "compare files and directories" },
112 { "blame", cmd_blame, usage_blame,
113 "show when lines in a file were changed" },
114 { "tree", cmd_tree, usage_tree,
115 "list files and directories in repository" },
116 { "status", cmd_status, usage_status,
117 "show modification status of files" },
118 { "ref", cmd_ref, usage_ref,
119 "manage references in repository" },
120 { "add", cmd_add, usage_add,
121 "add new files to version control" },
122 { "rm", cmd_rm, usage_rm,
123 "remove a versioned file" },
124 { "revert", cmd_revert, usage_revert,
125 "revert uncommitted changes" },
126 { "commit", cmd_commit, usage_commit,
127 "write changes from work tree to repository" },
128 };
130 int
131 main(int argc, char *argv[])
133 struct cmd *cmd;
134 unsigned int i;
135 int ch;
136 int hflag = 0;
138 setlocale(LC_CTYPE, "");
140 while ((ch = getopt(argc, argv, "h")) != -1) {
141 switch (ch) {
142 case 'h':
143 hflag = 1;
144 break;
145 default:
146 usage();
147 /* NOTREACHED */
151 argc -= optind;
152 argv += optind;
153 optind = 0;
155 if (argc <= 0)
156 usage();
158 signal(SIGINT, catch_sigint);
159 signal(SIGPIPE, catch_sigpipe);
161 for (i = 0; i < nitems(got_commands); i++) {
162 const struct got_error *error;
164 cmd = &got_commands[i];
166 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
167 continue;
169 if (hflag)
170 got_commands[i].cmd_usage();
172 error = got_commands[i].cmd_main(argc, argv);
173 if (error && !(sigint_received || sigpipe_received)) {
174 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
175 return 1;
178 return 0;
181 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
182 return 1;
185 __dead static void
186 usage(void)
188 int i;
190 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
191 "Available commands:\n", getprogname());
192 for (i = 0; i < nitems(got_commands); i++) {
193 struct cmd *cmd = &got_commands[i];
194 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
196 exit(1);
199 static const struct got_error *
200 get_editor(char **editorp)
202 const char *editor;
204 editor = getenv("VISUAL");
205 if (editor == NULL)
206 editor = getenv("EDITOR");
207 if (editor == NULL)
208 editor = "/bin/ed";
210 *editorp = realpath(editor, NULL);
211 if (*editorp == NULL)
212 return got_error_from_errno("relpath");
214 return NULL;
217 static const struct got_error *
218 apply_unveil(const char *repo_path, int repo_read_only,
219 const char *worktree_path, int create_worktree,
220 int unveil_editor)
222 const struct got_error *error;
223 static char err_msg[MAXPATHLEN + 36];
225 if (create_worktree) {
226 /* Pre-create work tree path to avoid unveiling its parents. */
227 error = got_path_mkdir(worktree_path);
229 if (errno == EEXIST) {
230 if (got_path_dir_is_empty(worktree_path)) {
231 errno = 0;
232 error = NULL;
233 } else {
234 snprintf(err_msg, sizeof(err_msg),
235 "%s: directory exists but is not empty",
236 worktree_path);
237 error = got_error_msg(GOT_ERR_BAD_PATH,
238 err_msg);
242 if (error && (error->code != GOT_ERR_ERRNO || errno != EISDIR))
243 return error;
246 if (unveil_editor) {
247 char *editor;
248 error = get_editor(&editor);
249 if (error)
250 return error;
251 unveil(editor, "x");
252 free(editor);
255 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
256 return got_error_from_errno2("unveil", repo_path);
258 if (worktree_path && unveil(worktree_path, "rwc") != 0)
259 return got_error_from_errno2("unveil", worktree_path);
261 if (unveil("/tmp", "rwc") != 0)
262 return got_error_from_errno2("unveil", "/tmp");
264 error = got_privsep_unveil_exec_helpers();
265 if (error != NULL)
266 return error;
268 if (unveil(NULL, NULL) != 0)
269 return got_error_from_errno("unveil");
271 return NULL;
274 __dead static void
275 usage_checkout(void)
277 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
278 "[worktree-path]\n", getprogname());
279 exit(1);
282 static void
283 checkout_progress(void *arg, unsigned char status, const char *path)
285 char *worktree_path = arg;
287 while (path[0] == '/')
288 path++;
290 printf("%c %s/%s\n", status, worktree_path, path);
293 static const struct got_error *
294 check_cancelled(void *arg)
296 if (sigint_received || sigpipe_received)
297 return got_error(GOT_ERR_CANCELLED);
298 return NULL;
301 static const struct got_error *
302 check_linear_ancestry(struct got_worktree *worktree,
303 struct got_object_id *commit_id, struct got_repository *repo)
305 const struct got_error *err = NULL;
306 struct got_object_id *yca_id, *base_commit_id;
308 base_commit_id = got_worktree_get_base_commit_id(worktree);
309 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
310 commit_id, base_commit_id, repo);
311 if (err)
312 return err;
314 if (yca_id == NULL)
315 return got_error(GOT_ERR_ANCESTRY);
317 /*
318 * Require a straight line of history between the target commit
319 * and the work tree's base commit.
321 * Non-linear situation such as the this require a rebase:
323 * (commit) D F (base_commit)
324 * \ /
325 * C E
326 * \ /
327 * B (yca)
328 * |
329 * A
331 * 'got update' only handles linear cases:
332 * Update forwards in time: A (base/yca) - B - C - D (commit)
333 * Update backwards in time: D (base) - C - D - A (commit/yca)
334 */
335 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
336 got_object_id_cmp(base_commit_id, yca_id) != 0)
337 return got_error(GOT_ERR_ANCESTRY);
339 free(yca_id);
340 return NULL;
344 static const struct got_error *
345 cmd_checkout(int argc, char *argv[])
347 const struct got_error *error = NULL;
348 struct got_repository *repo = NULL;
349 struct got_reference *head_ref = NULL;
350 struct got_worktree *worktree = NULL;
351 char *repo_path = NULL;
352 char *worktree_path = NULL;
353 const char *path_prefix = "";
354 char *commit_id_str = NULL;
355 int ch, same_path_prefix;
357 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
358 switch (ch) {
359 case 'c':
360 commit_id_str = strdup(optarg);
361 if (commit_id_str == NULL)
362 return got_error_from_errno("strdup");
363 break;
364 case 'p':
365 path_prefix = optarg;
366 break;
367 default:
368 usage_checkout();
369 /* NOTREACHED */
373 argc -= optind;
374 argv += optind;
376 #ifndef PROFILE
377 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
378 "unveil", NULL) == -1)
379 err(1, "pledge");
380 #endif
381 if (argc == 1) {
382 char *cwd, *base, *dotgit;
383 repo_path = realpath(argv[0], NULL);
384 if (repo_path == NULL)
385 return got_error_from_errno2("realpath", argv[0]);
386 cwd = getcwd(NULL, 0);
387 if (cwd == NULL) {
388 error = got_error_from_errno("getcwd");
389 goto done;
391 if (path_prefix[0]) {
392 base = basename(path_prefix);
393 if (base == NULL) {
394 error = got_error_from_errno2("basename",
395 path_prefix);
396 goto done;
398 } else {
399 base = basename(repo_path);
400 if (base == NULL) {
401 error = got_error_from_errno2("basename",
402 repo_path);
403 goto done;
406 dotgit = strstr(base, ".git");
407 if (dotgit)
408 *dotgit = '\0';
409 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
410 error = got_error_from_errno("asprintf");
411 free(cwd);
412 goto done;
414 free(cwd);
415 } else if (argc == 2) {
416 repo_path = realpath(argv[0], NULL);
417 if (repo_path == NULL) {
418 error = got_error_from_errno2("realpath", argv[0]);
419 goto done;
421 worktree_path = realpath(argv[1], NULL);
422 if (worktree_path == NULL) {
423 error = got_error_from_errno2("realpath", argv[1]);
424 goto done;
426 } else
427 usage_checkout();
429 got_path_strip_trailing_slashes(repo_path);
430 got_path_strip_trailing_slashes(worktree_path);
432 error = got_repo_open(&repo, repo_path);
433 if (error != NULL)
434 goto done;
436 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1, 0);
437 if (error)
438 goto done;
440 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
441 if (error != NULL)
442 goto done;
444 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
445 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
446 goto done;
448 error = got_worktree_open(&worktree, worktree_path);
449 if (error != NULL)
450 goto done;
452 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
453 path_prefix);
454 if (error != NULL)
455 goto done;
456 if (!same_path_prefix) {
457 error = got_error(GOT_ERR_PATH_PREFIX);
458 goto done;
461 if (commit_id_str) {
462 struct got_object_id *commit_id;
463 error = got_object_resolve_id_str(&commit_id, repo,
464 commit_id_str);
465 if (error != NULL)
466 goto done;
467 error = check_linear_ancestry(worktree, commit_id, repo);
468 if (error != NULL) {
469 free(commit_id);
470 goto done;
472 error = got_worktree_set_base_commit_id(worktree, repo,
473 commit_id);
474 free(commit_id);
475 if (error)
476 goto done;
479 error = got_worktree_checkout_files(worktree, "", repo,
480 checkout_progress, worktree_path, check_cancelled, NULL);
481 if (error != NULL)
482 goto done;
484 printf("Now shut up and hack\n");
486 done:
487 free(commit_id_str);
488 free(repo_path);
489 free(worktree_path);
490 return error;
493 __dead static void
494 usage_update(void)
496 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
497 getprogname());
498 exit(1);
501 static void
502 update_progress(void *arg, unsigned char status, const char *path)
504 int *did_something = arg;
506 if (status == GOT_STATUS_EXISTS)
507 return;
509 *did_something = 1;
510 while (path[0] == '/')
511 path++;
512 printf("%c %s\n", status, path);
515 static const struct got_error *
516 cmd_update(int argc, char *argv[])
518 const struct got_error *error = NULL;
519 struct got_repository *repo = NULL;
520 struct got_worktree *worktree = NULL;
521 char *worktree_path = NULL, *path = NULL;
522 struct got_object_id *commit_id = NULL;
523 char *commit_id_str = NULL;
524 int ch, did_something = 0;
526 while ((ch = getopt(argc, argv, "c:")) != -1) {
527 switch (ch) {
528 case 'c':
529 commit_id_str = strdup(optarg);
530 if (commit_id_str == NULL)
531 return got_error_from_errno("strdup");
532 break;
533 default:
534 usage_update();
535 /* NOTREACHED */
539 argc -= optind;
540 argv += optind;
542 #ifndef PROFILE
543 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
544 "unveil", NULL) == -1)
545 err(1, "pledge");
546 #endif
547 worktree_path = getcwd(NULL, 0);
548 if (worktree_path == NULL) {
549 error = got_error_from_errno("getcwd");
550 goto done;
552 error = got_worktree_open(&worktree, worktree_path);
553 if (error)
554 goto done;
556 if (argc == 0) {
557 path = strdup("");
558 if (path == NULL) {
559 error = got_error_from_errno("strdup");
560 goto done;
562 } else if (argc == 1) {
563 error = got_worktree_resolve_path(&path, worktree, argv[0]);
564 if (error)
565 goto done;
566 } else
567 usage_update();
569 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
570 if (error != NULL)
571 goto done;
573 error = apply_unveil(got_repo_get_path(repo), 0,
574 got_worktree_get_root_path(worktree), 0, 0);
575 if (error)
576 goto done;
578 if (commit_id_str == NULL) {
579 struct got_reference *head_ref;
580 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
581 if (error != NULL)
582 goto done;
583 error = got_ref_resolve(&commit_id, repo, head_ref);
584 if (error != NULL)
585 goto done;
586 error = got_object_id_str(&commit_id_str, commit_id);
587 if (error != NULL)
588 goto done;
589 } else {
590 error = got_object_resolve_id_str(&commit_id, repo,
591 commit_id_str);
592 if (error != NULL)
593 goto done;
596 error = check_linear_ancestry(worktree, commit_id, repo);
597 if (error != NULL)
598 goto done;
600 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
601 commit_id) != 0) {
602 error = got_worktree_set_base_commit_id(worktree, repo,
603 commit_id);
604 if (error)
605 goto done;
608 error = got_worktree_checkout_files(worktree, path, repo,
609 update_progress, &did_something, check_cancelled, NULL);
610 if (error != NULL)
611 goto done;
613 if (did_something)
614 printf("Updated to commit %s\n", commit_id_str);
615 else
616 printf("Already up-to-date\n");
617 done:
618 free(worktree_path);
619 free(path);
620 free(commit_id);
621 free(commit_id_str);
622 return error;
625 static const struct got_error *
626 print_patch(struct got_commit_object *commit, struct got_object_id *id,
627 int diff_context, struct got_repository *repo)
629 const struct got_error *err = NULL;
630 struct got_tree_object *tree1 = NULL, *tree2;
631 struct got_object_qid *qid;
632 char *id_str1 = NULL, *id_str2;
634 err = got_object_open_as_tree(&tree2, repo,
635 got_object_commit_get_tree_id(commit));
636 if (err)
637 return err;
639 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
640 if (qid != NULL) {
641 struct got_commit_object *pcommit;
643 err = got_object_open_as_commit(&pcommit, repo, qid->id);
644 if (err)
645 return err;
647 err = got_object_open_as_tree(&tree1, repo,
648 got_object_commit_get_tree_id(pcommit));
649 got_object_commit_close(pcommit);
650 if (err)
651 return err;
653 err = got_object_id_str(&id_str1, qid->id);
654 if (err)
655 return err;
658 err = got_object_id_str(&id_str2, id);
659 if (err)
660 goto done;
662 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
663 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
664 done:
665 if (tree1)
666 got_object_tree_close(tree1);
667 got_object_tree_close(tree2);
668 free(id_str1);
669 free(id_str2);
670 return err;
673 static char *
674 get_datestr(time_t *time, char *datebuf)
676 char *p, *s = ctime_r(time, datebuf);
677 p = strchr(s, '\n');
678 if (p)
679 *p = '\0';
680 return s;
683 static const struct got_error *
684 print_commit(struct got_commit_object *commit, struct got_object_id *id,
685 struct got_repository *repo, int show_patch, int diff_context,
686 struct got_reflist_head *refs)
688 const struct got_error *err = NULL;
689 char *id_str, *datestr, *logmsg0, *logmsg, *line;
690 char datebuf[26];
691 time_t committer_time;
692 const char *author, *committer;
693 char *refs_str = NULL;
694 struct got_reflist_entry *re;
696 SIMPLEQ_FOREACH(re, refs, entry) {
697 char *s;
698 const char *name;
699 if (got_object_id_cmp(re->id, id) != 0)
700 continue;
701 name = got_ref_get_name(re->ref);
702 if (strcmp(name, GOT_REF_HEAD) == 0)
703 continue;
704 if (strncmp(name, "refs/", 5) == 0)
705 name += 5;
706 if (strncmp(name, "got/", 4) == 0)
707 continue;
708 if (strncmp(name, "heads/", 6) == 0)
709 name += 6;
710 if (strncmp(name, "remotes/", 8) == 0)
711 name += 8;
712 s = refs_str;
713 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
714 name) == -1) {
715 err = got_error_from_errno("asprintf");
716 free(s);
717 break;
719 free(s);
721 err = got_object_id_str(&id_str, id);
722 if (err)
723 return err;
725 printf("-----------------------------------------------\n");
726 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
727 refs_str ? refs_str : "", refs_str ? ")" : "");
728 free(id_str);
729 id_str = NULL;
730 free(refs_str);
731 refs_str = NULL;
732 printf("from: %s\n", got_object_commit_get_author(commit));
733 committer_time = got_object_commit_get_committer_time(commit);
734 datestr = get_datestr(&committer_time, datebuf);
735 printf("date: %s UTC\n", datestr);
736 author = got_object_commit_get_author(commit);
737 committer = got_object_commit_get_committer(commit);
738 if (strcmp(author, committer) != 0)
739 printf("via: %s\n", committer);
740 if (got_object_commit_get_nparents(commit) > 1) {
741 const struct got_object_id_queue *parent_ids;
742 struct got_object_qid *qid;
743 int n = 1;
744 parent_ids = got_object_commit_get_parent_ids(commit);
745 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
746 err = got_object_id_str(&id_str, qid->id);
747 if (err)
748 return err;
749 printf("parent %d: %s\n", n++, id_str);
750 free(id_str);
754 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
755 if (logmsg0 == NULL)
756 return got_error_from_errno("strdup");
758 logmsg = logmsg0;
759 do {
760 line = strsep(&logmsg, "\n");
761 if (line)
762 printf(" %s\n", line);
763 } while (line);
764 free(logmsg0);
766 if (show_patch) {
767 err = print_patch(commit, id, diff_context, repo);
768 if (err == 0)
769 printf("\n");
772 if (fflush(stdout) != 0 && err == NULL)
773 err = got_error_from_errno("fflush");
774 return err;
777 static const struct got_error *
778 print_commits(struct got_object_id *root_id, struct got_repository *repo,
779 char *path, int show_patch, int diff_context, int limit,
780 int first_parent_traversal, struct got_reflist_head *refs)
782 const struct got_error *err;
783 struct got_commit_graph *graph;
785 err = got_commit_graph_open(&graph, root_id, path,
786 first_parent_traversal, repo);
787 if (err)
788 return err;
789 err = got_commit_graph_iter_start(graph, root_id, repo);
790 if (err)
791 goto done;
792 for (;;) {
793 struct got_commit_object *commit;
794 struct got_object_id *id;
796 if (sigint_received || sigpipe_received)
797 break;
799 err = got_commit_graph_iter_next(&id, graph);
800 if (err) {
801 if (err->code == GOT_ERR_ITER_COMPLETED) {
802 err = NULL;
803 break;
805 if (err->code != GOT_ERR_ITER_NEED_MORE)
806 break;
807 err = got_commit_graph_fetch_commits(graph, 1, repo);
808 if (err)
809 break;
810 else
811 continue;
813 if (id == NULL)
814 break;
816 err = got_object_open_as_commit(&commit, repo, id);
817 if (err)
818 break;
819 err = print_commit(commit, id, repo, show_patch, diff_context,
820 refs);
821 got_object_commit_close(commit);
822 if (err || (limit && --limit == 0))
823 break;
825 done:
826 got_commit_graph_close(graph);
827 return err;
830 __dead static void
831 usage_log(void)
833 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
834 "[-r repository-path] [path]\n", getprogname());
835 exit(1);
838 static const struct got_error *
839 cmd_log(int argc, char *argv[])
841 const struct got_error *error;
842 struct got_repository *repo = NULL;
843 struct got_worktree *worktree = NULL;
844 struct got_commit_object *commit = NULL;
845 struct got_object_id *id = NULL;
846 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
847 char *start_commit = NULL;
848 int diff_context = 3, ch;
849 int show_patch = 0, limit = 0, first_parent_traversal = 0;
850 const char *errstr;
851 struct got_reflist_head refs;
853 SIMPLEQ_INIT(&refs);
855 #ifndef PROFILE
856 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
857 NULL)
858 == -1)
859 err(1, "pledge");
860 #endif
862 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
863 switch (ch) {
864 case 'p':
865 show_patch = 1;
866 break;
867 case 'c':
868 start_commit = optarg;
869 break;
870 case 'C':
871 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
872 &errstr);
873 if (errstr != NULL)
874 err(1, "-C option %s", errstr);
875 break;
876 case 'l':
877 limit = strtonum(optarg, 1, INT_MAX, &errstr);
878 if (errstr != NULL)
879 err(1, "-l option %s", errstr);
880 break;
881 case 'f':
882 first_parent_traversal = 1;
883 break;
884 case 'r':
885 repo_path = realpath(optarg, NULL);
886 if (repo_path == NULL)
887 err(1, "-r option");
888 got_path_strip_trailing_slashes(repo_path);
889 break;
890 default:
891 usage_log();
892 /* NOTREACHED */
896 argc -= optind;
897 argv += optind;
899 cwd = getcwd(NULL, 0);
900 if (cwd == NULL) {
901 error = got_error_from_errno("getcwd");
902 goto done;
905 error = got_worktree_open(&worktree, cwd);
906 if (error && error->code != GOT_ERR_NOT_WORKTREE)
907 goto done;
908 error = NULL;
910 if (argc == 0) {
911 path = strdup("");
912 if (path == NULL) {
913 error = got_error_from_errno("strdup");
914 goto done;
916 } else if (argc == 1) {
917 if (worktree) {
918 error = got_worktree_resolve_path(&path, worktree,
919 argv[0]);
920 if (error)
921 goto done;
922 } else {
923 path = strdup(argv[0]);
924 if (path == NULL) {
925 error = got_error_from_errno("strdup");
926 goto done;
929 } else
930 usage_log();
932 if (repo_path == NULL) {
933 repo_path = worktree ?
934 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
936 if (repo_path == NULL) {
937 error = got_error_from_errno("strdup");
938 goto done;
941 error = got_repo_open(&repo, repo_path);
942 if (error != NULL)
943 goto done;
945 error = apply_unveil(got_repo_get_path(repo), 1,
946 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
947 if (error)
948 goto done;
950 if (start_commit == NULL) {
951 struct got_reference *head_ref;
952 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
953 if (error != NULL)
954 return error;
955 error = got_ref_resolve(&id, repo, head_ref);
956 got_ref_close(head_ref);
957 if (error != NULL)
958 return error;
959 error = got_object_open_as_commit(&commit, repo, id);
960 } else {
961 struct got_reference *ref;
962 error = got_ref_open(&ref, repo, start_commit, 0);
963 if (error == NULL) {
964 int obj_type;
965 error = got_ref_resolve(&id, repo, ref);
966 got_ref_close(ref);
967 if (error != NULL)
968 goto done;
969 error = got_object_get_type(&obj_type, repo, id);
970 if (error != NULL)
971 goto done;
972 if (obj_type == GOT_OBJ_TYPE_TAG) {
973 struct got_tag_object *tag;
974 error = got_object_open_as_tag(&tag, repo, id);
975 if (error != NULL)
976 goto done;
977 if (got_object_tag_get_object_type(tag) !=
978 GOT_OBJ_TYPE_COMMIT) {
979 got_object_tag_close(tag);
980 error = got_error(GOT_ERR_OBJ_TYPE);
981 goto done;
983 free(id);
984 id = got_object_id_dup(
985 got_object_tag_get_object_id(tag));
986 if (id == NULL)
987 error = got_error_from_errno(
988 "got_object_id_dup");
989 got_object_tag_close(tag);
990 if (error)
991 goto done;
992 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
993 error = got_error(GOT_ERR_OBJ_TYPE);
994 goto done;
996 error = got_object_open_as_commit(&commit, repo, id);
997 if (error != NULL)
998 goto done;
1000 if (commit == NULL) {
1001 error = got_object_resolve_id_str(&id, repo,
1002 start_commit);
1003 if (error != NULL)
1004 return error;
1007 if (error != NULL)
1008 goto done;
1010 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1011 if (error != NULL)
1012 goto done;
1013 if (in_repo_path) {
1014 free(path);
1015 path = in_repo_path;
1018 error = got_ref_list(&refs, repo);
1019 if (error)
1020 goto done;
1022 error = print_commits(id, repo, path, show_patch,
1023 diff_context, limit, first_parent_traversal, &refs);
1024 done:
1025 free(path);
1026 free(repo_path);
1027 free(cwd);
1028 free(id);
1029 if (worktree)
1030 got_worktree_close(worktree);
1031 if (repo) {
1032 const struct got_error *repo_error;
1033 repo_error = got_repo_close(repo);
1034 if (error == NULL)
1035 error = repo_error;
1037 got_ref_list_free(&refs);
1038 return error;
1041 __dead static void
1042 usage_diff(void)
1044 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1045 "[object1 object2 | path]\n", getprogname());
1046 exit(1);
1049 struct print_diff_arg {
1050 struct got_repository *repo;
1051 struct got_worktree *worktree;
1052 int diff_context;
1053 const char *id_str;
1054 int header_shown;
1057 static const struct got_error *
1058 print_diff(void *arg, unsigned char status, const char *path,
1059 struct got_object_id *id)
1061 struct print_diff_arg *a = arg;
1062 const struct got_error *err = NULL;
1063 struct got_blob_object *blob1 = NULL;
1064 FILE *f2 = NULL;
1065 char *abspath = NULL;
1066 struct stat sb;
1068 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1069 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1070 return NULL;
1072 if (!a->header_shown) {
1073 printf("diff %s %s\n", a->id_str,
1074 got_worktree_get_root_path(a->worktree));
1075 a->header_shown = 1;
1078 if (status != GOT_STATUS_ADD) {
1079 err = got_object_open_as_blob(&blob1, a->repo, id, 8192);
1080 if (err)
1081 goto done;
1085 if (status != GOT_STATUS_DELETE) {
1086 if (asprintf(&abspath, "%s/%s",
1087 got_worktree_get_root_path(a->worktree), path) == -1) {
1088 err = got_error_from_errno("asprintf");
1089 goto done;
1092 f2 = fopen(abspath, "r");
1093 if (f2 == NULL) {
1094 err = got_error_from_errno2("fopen", abspath);
1095 goto done;
1097 if (lstat(abspath, &sb) == -1) {
1098 err = got_error_from_errno2("lstat", abspath);
1099 goto done;
1101 } else
1102 sb.st_size = 0;
1104 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1105 stdout);
1106 done:
1107 if (blob1)
1108 got_object_blob_close(blob1);
1109 if (f2 && fclose(f2) != 0 && err == NULL)
1110 err = got_error_from_errno("fclose");
1111 free(abspath);
1112 return err;
1115 static const struct got_error *
1116 cmd_diff(int argc, char *argv[])
1118 const struct got_error *error;
1119 struct got_repository *repo = NULL;
1120 struct got_worktree *worktree = NULL;
1121 char *cwd = NULL, *repo_path = NULL;
1122 struct got_object_id *id1 = NULL, *id2 = NULL;
1123 char *id_str1 = NULL, *id_str2 = NULL;
1124 int type1, type2;
1125 int diff_context = 3, ch;
1126 const char *errstr;
1127 char *path = NULL;
1129 #ifndef PROFILE
1130 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1131 NULL) == -1)
1132 err(1, "pledge");
1133 #endif
1135 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1136 switch (ch) {
1137 case 'C':
1138 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1139 if (errstr != NULL)
1140 err(1, "-C option %s", errstr);
1141 break;
1142 case 'r':
1143 repo_path = realpath(optarg, NULL);
1144 if (repo_path == NULL)
1145 err(1, "-r option");
1146 got_path_strip_trailing_slashes(repo_path);
1147 break;
1148 default:
1149 usage_diff();
1150 /* NOTREACHED */
1154 argc -= optind;
1155 argv += optind;
1157 cwd = getcwd(NULL, 0);
1158 if (cwd == NULL) {
1159 error = got_error_from_errno("getcwd");
1160 goto done;
1162 error = got_worktree_open(&worktree, cwd);
1163 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1164 goto done;
1165 if (argc <= 1) {
1166 if (worktree == NULL) {
1167 error = got_error(GOT_ERR_NOT_WORKTREE);
1168 goto done;
1170 if (repo_path)
1171 errx(1,
1172 "-r option can't be used when diffing a work tree");
1173 repo_path = strdup(got_worktree_get_repo_path(worktree));
1174 if (repo_path == NULL) {
1175 error = got_error_from_errno("strdup");
1176 goto done;
1178 if (argc == 1) {
1179 error = got_worktree_resolve_path(&path, worktree,
1180 argv[0]);
1181 if (error)
1182 goto done;
1183 } else {
1184 path = strdup("");
1185 if (path == NULL) {
1186 error = got_error_from_errno("strdup");
1187 goto done;
1190 } else if (argc == 2) {
1191 id_str1 = argv[0];
1192 id_str2 = argv[1];
1193 } else
1194 usage_diff();
1196 if (repo_path == NULL) {
1197 repo_path = getcwd(NULL, 0);
1198 if (repo_path == NULL)
1199 return got_error_from_errno("getcwd");
1202 error = got_repo_open(&repo, repo_path);
1203 free(repo_path);
1204 if (error != NULL)
1205 goto done;
1207 error = apply_unveil(got_repo_get_path(repo), 1,
1208 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
1209 if (error)
1210 goto done;
1212 if (worktree) {
1213 struct print_diff_arg arg;
1214 char *id_str;
1215 error = got_object_id_str(&id_str,
1216 got_worktree_get_base_commit_id(worktree));
1217 if (error)
1218 goto done;
1219 arg.repo = repo;
1220 arg.worktree = worktree;
1221 arg.diff_context = diff_context;
1222 arg.id_str = id_str;
1223 arg.header_shown = 0;
1225 error = got_worktree_status(worktree, path, repo, print_diff,
1226 &arg, check_cancelled, NULL);
1227 free(id_str);
1228 goto done;
1231 error = got_object_resolve_id_str(&id1, repo, id_str1);
1232 if (error)
1233 goto done;
1235 error = got_object_resolve_id_str(&id2, repo, id_str2);
1236 if (error)
1237 goto done;
1239 error = got_object_get_type(&type1, repo, id1);
1240 if (error)
1241 goto done;
1243 error = got_object_get_type(&type2, repo, id2);
1244 if (error)
1245 goto done;
1247 if (type1 != type2) {
1248 error = got_error(GOT_ERR_OBJ_TYPE);
1249 goto done;
1252 switch (type1) {
1253 case GOT_OBJ_TYPE_BLOB:
1254 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1255 diff_context, repo, stdout);
1256 break;
1257 case GOT_OBJ_TYPE_TREE:
1258 error = got_diff_objects_as_trees(id1, id2, "", "",
1259 diff_context, repo, stdout);
1260 break;
1261 case GOT_OBJ_TYPE_COMMIT:
1262 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1263 id_str2);
1264 error = got_diff_objects_as_commits(id1, id2, diff_context,
1265 repo, stdout);
1266 break;
1267 default:
1268 error = got_error(GOT_ERR_OBJ_TYPE);
1271 done:
1272 free(id1);
1273 free(id2);
1274 free(path);
1275 if (worktree)
1276 got_worktree_close(worktree);
1277 if (repo) {
1278 const struct got_error *repo_error;
1279 repo_error = got_repo_close(repo);
1280 if (error == NULL)
1281 error = repo_error;
1283 return error;
1286 __dead static void
1287 usage_blame(void)
1289 fprintf(stderr,
1290 "usage: %s blame [-c commit] [-r repository-path] path\n",
1291 getprogname());
1292 exit(1);
1295 static const struct got_error *
1296 cmd_blame(int argc, char *argv[])
1298 const struct got_error *error;
1299 struct got_repository *repo = NULL;
1300 struct got_worktree *worktree = NULL;
1301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1302 struct got_object_id *commit_id = NULL;
1303 char *commit_id_str = NULL;
1304 int ch;
1306 #ifndef PROFILE
1307 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1308 NULL) == -1)
1309 err(1, "pledge");
1310 #endif
1312 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1313 switch (ch) {
1314 case 'c':
1315 commit_id_str = optarg;
1316 break;
1317 case 'r':
1318 repo_path = realpath(optarg, NULL);
1319 if (repo_path == NULL)
1320 err(1, "-r option");
1321 got_path_strip_trailing_slashes(repo_path);
1322 break;
1323 default:
1324 usage_blame();
1325 /* NOTREACHED */
1329 argc -= optind;
1330 argv += optind;
1332 if (argc == 1)
1333 path = argv[0];
1334 else
1335 usage_blame();
1337 cwd = getcwd(NULL, 0);
1338 if (cwd == NULL) {
1339 error = got_error_from_errno("getcwd");
1340 goto done;
1342 if (repo_path == NULL) {
1343 error = got_worktree_open(&worktree, cwd);
1344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1345 goto done;
1346 else
1347 error = NULL;
1348 if (worktree) {
1349 repo_path =
1350 strdup(got_worktree_get_repo_path(worktree));
1351 if (repo_path == NULL)
1352 error = got_error_from_errno("strdup");
1353 if (error)
1354 goto done;
1355 } else {
1356 repo_path = strdup(cwd);
1357 if (repo_path == NULL) {
1358 error = got_error_from_errno("strdup");
1359 goto done;
1364 error = got_repo_open(&repo, repo_path);
1365 if (error != NULL)
1366 goto done;
1368 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, 0);
1369 if (error)
1370 goto done;
1372 if (worktree) {
1373 const char *prefix = got_worktree_get_path_prefix(worktree);
1374 char *p, *worktree_subdir = cwd +
1375 strlen(got_worktree_get_root_path(worktree));
1376 if (asprintf(&p, "%s%s%s%s%s",
1377 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1378 worktree_subdir, worktree_subdir[0] ? "/" : "",
1379 path) == -1) {
1380 error = got_error_from_errno("asprintf");
1381 goto done;
1383 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1384 free(p);
1385 } else {
1386 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1388 if (error)
1389 goto done;
1391 if (commit_id_str == NULL) {
1392 struct got_reference *head_ref;
1393 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1394 if (error != NULL)
1395 goto done;
1396 error = got_ref_resolve(&commit_id, repo, head_ref);
1397 got_ref_close(head_ref);
1398 if (error != NULL)
1399 goto done;
1400 } else {
1401 error = got_object_resolve_id_str(&commit_id, repo,
1402 commit_id_str);
1403 if (error != NULL)
1404 goto done;
1407 error = got_blame(in_repo_path, commit_id, repo, stdout);
1408 done:
1409 free(in_repo_path);
1410 free(repo_path);
1411 free(cwd);
1412 free(commit_id);
1413 if (worktree)
1414 got_worktree_close(worktree);
1415 if (repo) {
1416 const struct got_error *repo_error;
1417 repo_error = got_repo_close(repo);
1418 if (error == NULL)
1419 error = repo_error;
1421 return error;
1424 __dead static void
1425 usage_tree(void)
1427 fprintf(stderr,
1428 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1429 getprogname());
1430 exit(1);
1433 static void
1434 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1435 const char *root_path)
1437 int is_root_path = (strcmp(path, root_path) == 0);
1439 path += strlen(root_path);
1440 while (path[0] == '/')
1441 path++;
1443 printf("%s%s%s%s%s\n", id ? id : "", path,
1444 is_root_path ? "" : "/", te->name,
1445 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1448 static const struct got_error *
1449 print_tree(const char *path, struct got_object_id *commit_id,
1450 int show_ids, int recurse, const char *root_path,
1451 struct got_repository *repo)
1453 const struct got_error *err = NULL;
1454 struct got_object_id *tree_id = NULL;
1455 struct got_tree_object *tree = NULL;
1456 const struct got_tree_entries *entries;
1457 struct got_tree_entry *te;
1459 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1460 if (err)
1461 goto done;
1463 err = got_object_open_as_tree(&tree, repo, tree_id);
1464 if (err)
1465 goto done;
1466 entries = got_object_tree_get_entries(tree);
1467 te = SIMPLEQ_FIRST(&entries->head);
1468 while (te) {
1469 char *id = NULL;
1471 if (sigint_received || sigpipe_received)
1472 break;
1474 if (show_ids) {
1475 char *id_str;
1476 err = got_object_id_str(&id_str, te->id);
1477 if (err)
1478 goto done;
1479 if (asprintf(&id, "%s ", id_str) == -1) {
1480 err = got_error_from_errno("asprintf");
1481 free(id_str);
1482 goto done;
1484 free(id_str);
1486 print_entry(te, id, path, root_path);
1487 free(id);
1489 if (recurse && S_ISDIR(te->mode)) {
1490 char *child_path;
1491 if (asprintf(&child_path, "%s%s%s", path,
1492 path[0] == '/' && path[1] == '\0' ? "" : "/",
1493 te->name) == -1) {
1494 err = got_error_from_errno("asprintf");
1495 goto done;
1497 err = print_tree(child_path, commit_id, show_ids, 1,
1498 root_path, repo);
1499 free(child_path);
1500 if (err)
1501 goto done;
1504 te = SIMPLEQ_NEXT(te, entry);
1506 done:
1507 if (tree)
1508 got_object_tree_close(tree);
1509 free(tree_id);
1510 return err;
1513 static const struct got_error *
1514 cmd_tree(int argc, char *argv[])
1516 const struct got_error *error;
1517 struct got_repository *repo = NULL;
1518 struct got_worktree *worktree = NULL;
1519 const char *path;
1520 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1521 struct got_object_id *commit_id = NULL;
1522 char *commit_id_str = NULL;
1523 int show_ids = 0, recurse = 0;
1524 int ch;
1526 #ifndef PROFILE
1527 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1528 NULL) == -1)
1529 err(1, "pledge");
1530 #endif
1532 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1533 switch (ch) {
1534 case 'c':
1535 commit_id_str = optarg;
1536 break;
1537 case 'r':
1538 repo_path = realpath(optarg, NULL);
1539 if (repo_path == NULL)
1540 err(1, "-r option");
1541 got_path_strip_trailing_slashes(repo_path);
1542 break;
1543 case 'i':
1544 show_ids = 1;
1545 break;
1546 case 'R':
1547 recurse = 1;
1548 break;
1549 default:
1550 usage_tree();
1551 /* NOTREACHED */
1555 argc -= optind;
1556 argv += optind;
1558 if (argc == 1)
1559 path = argv[0];
1560 else if (argc > 1)
1561 usage_tree();
1562 else
1563 path = NULL;
1565 cwd = getcwd(NULL, 0);
1566 if (cwd == NULL) {
1567 error = got_error_from_errno("getcwd");
1568 goto done;
1570 if (repo_path == NULL) {
1571 error = got_worktree_open(&worktree, cwd);
1572 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1573 goto done;
1574 else
1575 error = NULL;
1576 if (worktree) {
1577 repo_path =
1578 strdup(got_worktree_get_repo_path(worktree));
1579 if (repo_path == NULL)
1580 error = got_error_from_errno("strdup");
1581 if (error)
1582 goto done;
1583 } else {
1584 repo_path = strdup(cwd);
1585 if (repo_path == NULL) {
1586 error = got_error_from_errno("strdup");
1587 goto done;
1592 error = got_repo_open(&repo, repo_path);
1593 if (error != NULL)
1594 goto done;
1596 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0, 0);
1597 if (error)
1598 goto done;
1600 if (path == NULL) {
1601 if (worktree) {
1602 char *p, *worktree_subdir = cwd +
1603 strlen(got_worktree_get_root_path(worktree));
1604 if (asprintf(&p, "%s/%s",
1605 got_worktree_get_path_prefix(worktree),
1606 worktree_subdir) == -1) {
1607 error = got_error_from_errno("asprintf");
1608 goto done;
1610 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1611 free(p);
1612 if (error)
1613 goto done;
1614 } else
1615 path = "/";
1617 if (in_repo_path == NULL) {
1618 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1619 if (error != NULL)
1620 goto done;
1623 if (commit_id_str == NULL) {
1624 struct got_reference *head_ref;
1625 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1626 if (error != NULL)
1627 goto done;
1628 error = got_ref_resolve(&commit_id, repo, head_ref);
1629 got_ref_close(head_ref);
1630 if (error != NULL)
1631 goto done;
1632 } else {
1633 error = got_object_resolve_id_str(&commit_id, repo,
1634 commit_id_str);
1635 if (error != NULL)
1636 goto done;
1639 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1640 in_repo_path, repo);
1641 done:
1642 free(in_repo_path);
1643 free(repo_path);
1644 free(cwd);
1645 free(commit_id);
1646 if (worktree)
1647 got_worktree_close(worktree);
1648 if (repo) {
1649 const struct got_error *repo_error;
1650 repo_error = got_repo_close(repo);
1651 if (error == NULL)
1652 error = repo_error;
1654 return error;
1657 __dead static void
1658 usage_status(void)
1660 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1661 exit(1);
1664 static const struct got_error *
1665 print_status(void *arg, unsigned char status, const char *path,
1666 struct got_object_id *id)
1668 printf("%c %s\n", status, path);
1669 return NULL;
1672 static const struct got_error *
1673 cmd_status(int argc, char *argv[])
1675 const struct got_error *error = NULL;
1676 struct got_repository *repo = NULL;
1677 struct got_worktree *worktree = NULL;
1678 char *cwd = NULL, *path = NULL;
1679 int ch;
1681 while ((ch = getopt(argc, argv, "")) != -1) {
1682 switch (ch) {
1683 default:
1684 usage_status();
1685 /* NOTREACHED */
1689 argc -= optind;
1690 argv += optind;
1692 #ifndef PROFILE
1693 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1694 NULL) == -1)
1695 err(1, "pledge");
1696 #endif
1697 cwd = getcwd(NULL, 0);
1698 if (cwd == NULL) {
1699 error = got_error_from_errno("getcwd");
1700 goto done;
1703 error = got_worktree_open(&worktree, cwd);
1704 if (error != NULL)
1705 goto done;
1707 if (argc == 0) {
1708 path = strdup("");
1709 if (path == NULL) {
1710 error = got_error_from_errno("strdup");
1711 goto done;
1713 } else if (argc == 1) {
1714 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1715 if (error)
1716 goto done;
1717 } else
1718 usage_status();
1720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1721 if (error != NULL)
1722 goto done;
1724 error = apply_unveil(got_repo_get_path(repo), 1,
1725 got_worktree_get_root_path(worktree), 0, 0);
1726 if (error)
1727 goto done;
1729 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1730 check_cancelled, NULL);
1731 done:
1732 free(cwd);
1733 free(path);
1734 return error;
1737 __dead static void
1738 usage_ref(void)
1740 fprintf(stderr,
1741 "usage: %s ref [-r repository] -l | -d name | name object\n",
1742 getprogname());
1743 exit(1);
1746 static const struct got_error *
1747 list_refs(struct got_repository *repo)
1749 static const struct got_error *err = NULL;
1750 struct got_reflist_head refs;
1751 struct got_reflist_entry *re;
1753 SIMPLEQ_INIT(&refs);
1754 err = got_ref_list(&refs, repo);
1755 if (err)
1756 return err;
1758 SIMPLEQ_FOREACH(re, &refs, entry) {
1759 char *refstr;
1760 refstr = got_ref_to_str(re->ref);
1761 if (refstr == NULL)
1762 return got_error_from_errno("got_ref_to_str");
1763 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1764 free(refstr);
1767 got_ref_list_free(&refs);
1768 return NULL;
1771 static const struct got_error *
1772 delete_ref(struct got_repository *repo, const char *refname)
1774 const struct got_error *err = NULL;
1775 struct got_reference *ref;
1777 err = got_ref_open(&ref, repo, refname, 0);
1778 if (err)
1779 return err;
1781 err = got_ref_delete(ref, repo);
1782 got_ref_close(ref);
1783 return err;
1786 static const struct got_error *
1787 add_ref(struct got_repository *repo, const char *refname, const char *id_str)
1789 const struct got_error *err = NULL;
1790 struct got_object_id *id;
1791 struct got_reference *ref = NULL;
1793 err = got_object_resolve_id_str(&id, repo, id_str);
1794 if (err)
1795 return err;
1797 err = got_ref_alloc(&ref, refname, id);
1798 if (err)
1799 goto done;
1801 err = got_ref_write(ref, repo);
1802 done:
1803 if (ref)
1804 got_ref_close(ref);
1805 free(id);
1806 return err;
1809 static const struct got_error *
1810 cmd_ref(int argc, char *argv[])
1812 const struct got_error *error = NULL;
1813 struct got_repository *repo = NULL;
1814 struct got_worktree *worktree = NULL;
1815 char *cwd = NULL, *repo_path = NULL;
1816 int ch, do_list = 0;
1817 const char *delref = NULL;
1819 /* TODO: Add -s option for adding symbolic references. */
1820 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1821 switch (ch) {
1822 case 'd':
1823 delref = optarg;
1824 break;
1825 case 'r':
1826 repo_path = realpath(optarg, NULL);
1827 if (repo_path == NULL)
1828 err(1, "-r option");
1829 got_path_strip_trailing_slashes(repo_path);
1830 break;
1831 case 'l':
1832 do_list = 1;
1833 break;
1834 default:
1835 usage_ref();
1836 /* NOTREACHED */
1840 if (do_list && delref)
1841 errx(1, "-l and -d options are mutually exclusive\n");
1843 argc -= optind;
1844 argv += optind;
1846 if (do_list || delref) {
1847 if (argc > 0)
1848 usage_ref();
1849 } else if (argc != 2)
1850 usage_ref();
1852 #ifndef PROFILE
1853 if (do_list) {
1854 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1855 NULL) == -1)
1856 err(1, "pledge");
1857 } else {
1858 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1859 "sendfd unveil", NULL) == -1)
1860 err(1, "pledge");
1862 #endif
1863 cwd = getcwd(NULL, 0);
1864 if (cwd == NULL) {
1865 error = got_error_from_errno("getcwd");
1866 goto done;
1869 if (repo_path == NULL) {
1870 error = got_worktree_open(&worktree, cwd);
1871 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1872 goto done;
1873 else
1874 error = NULL;
1875 if (worktree) {
1876 repo_path =
1877 strdup(got_worktree_get_repo_path(worktree));
1878 if (repo_path == NULL)
1879 error = got_error_from_errno("strdup");
1880 if (error)
1881 goto done;
1882 } else {
1883 repo_path = strdup(cwd);
1884 if (repo_path == NULL) {
1885 error = got_error_from_errno("strdup");
1886 goto done;
1891 error = got_repo_open(&repo, repo_path);
1892 if (error != NULL)
1893 goto done;
1895 error = apply_unveil(got_repo_get_path(repo), do_list,
1896 worktree ? got_worktree_get_root_path(worktree) : NULL, 0, 0);
1897 if (error)
1898 goto done;
1900 if (do_list)
1901 error = list_refs(repo);
1902 else if (delref)
1903 error = delete_ref(repo, delref);
1904 else
1905 error = add_ref(repo, argv[0], argv[1]);
1906 done:
1907 if (repo)
1908 got_repo_close(repo);
1909 if (worktree)
1910 got_worktree_close(worktree);
1911 free(cwd);
1912 free(repo_path);
1913 return error;
1916 __dead static void
1917 usage_add(void)
1919 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1920 exit(1);
1923 static const struct got_error *
1924 cmd_add(int argc, char *argv[])
1926 const struct got_error *error = NULL;
1927 struct got_repository *repo = NULL;
1928 struct got_worktree *worktree = NULL;
1929 char *cwd = NULL;
1930 struct got_pathlist_head paths;
1931 struct got_pathlist_entry *pe;
1932 int ch, x;
1934 TAILQ_INIT(&paths);
1936 while ((ch = getopt(argc, argv, "")) != -1) {
1937 switch (ch) {
1938 default:
1939 usage_add();
1940 /* NOTREACHED */
1944 argc -= optind;
1945 argv += optind;
1947 if (argc < 1)
1948 usage_add();
1950 /* make sure each file exists before doing anything halfway */
1951 for (x = 0; x < argc; x++) {
1952 char *path = realpath(argv[x], NULL);
1953 if (path == NULL) {
1954 error = got_error_from_errno2("realpath", argv[x]);
1955 goto done;
1957 free(path);
1960 cwd = getcwd(NULL, 0);
1961 if (cwd == NULL) {
1962 error = got_error_from_errno("getcwd");
1963 goto done;
1966 error = got_worktree_open(&worktree, cwd);
1967 if (error)
1968 goto done;
1970 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1971 if (error != NULL)
1972 goto done;
1974 error = apply_unveil(got_repo_get_path(repo), 1,
1975 got_worktree_get_root_path(worktree), 0, 0);
1976 if (error)
1977 goto done;
1979 for (x = 0; x < argc; x++) {
1980 char *path = realpath(argv[x], NULL);
1981 if (path == NULL) {
1982 error = got_error_from_errno2("realpath", argv[x]);
1983 goto done;
1986 got_path_strip_trailing_slashes(path);
1987 error = got_pathlist_insert(&pe, &paths, path, NULL);
1988 if (error) {
1989 free(path);
1990 goto done;
1993 error = got_worktree_schedule_add(worktree, &paths, print_status,
1994 NULL, repo);
1995 done:
1996 if (repo)
1997 got_repo_close(repo);
1998 if (worktree)
1999 got_worktree_close(worktree);
2000 TAILQ_FOREACH(pe, &paths, entry)
2001 free((char *)pe->path);
2002 got_pathlist_free(&paths);
2003 free(cwd);
2004 return error;
2007 __dead static void
2008 usage_rm(void)
2010 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2011 exit(1);
2014 static const struct got_error *
2015 cmd_rm(int argc, char *argv[])
2017 const struct got_error *error = NULL;
2018 struct got_worktree *worktree = NULL;
2019 struct got_repository *repo = NULL;
2020 char *cwd = NULL, *path = NULL;
2021 int ch, delete_local_mods = 0;
2023 while ((ch = getopt(argc, argv, "f")) != -1) {
2024 switch (ch) {
2025 case 'f':
2026 delete_local_mods = 1;
2027 break;
2028 default:
2029 usage_add();
2030 /* NOTREACHED */
2034 argc -= optind;
2035 argv += optind;
2037 if (argc != 1)
2038 usage_rm();
2040 path = realpath(argv[0], NULL);
2041 if (path == NULL) {
2042 error = got_error_from_errno2("realpath", argv[0]);
2043 goto done;
2045 got_path_strip_trailing_slashes(path);
2047 cwd = getcwd(NULL, 0);
2048 if (cwd == NULL) {
2049 error = got_error_from_errno("getcwd");
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)
2058 goto done;
2060 error = apply_unveil(got_repo_get_path(repo), 1,
2061 got_worktree_get_root_path(worktree), 0, 0);
2062 if (error)
2063 goto done;
2065 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2066 print_status, 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_revert(void)
2082 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2083 exit(1);
2086 static void
2087 revert_progress(void *arg, unsigned char status, const char *path)
2089 while (path[0] == '/')
2090 path++;
2091 printf("%c %s\n", status, path);
2094 static const struct got_error *
2095 cmd_revert(int argc, char *argv[])
2097 const struct got_error *error = NULL;
2098 struct got_worktree *worktree = NULL;
2099 struct got_repository *repo = NULL;
2100 char *cwd = NULL, *path = NULL;
2101 int ch;
2103 while ((ch = getopt(argc, argv, "")) != -1) {
2104 switch (ch) {
2105 default:
2106 usage_revert();
2107 /* NOTREACHED */
2111 argc -= optind;
2112 argv += optind;
2114 if (argc != 1)
2115 usage_revert();
2117 path = realpath(argv[0], NULL);
2118 if (path == NULL) {
2119 error = got_error_from_errno2("realpath", argv[0]);
2120 goto done;
2122 got_path_strip_trailing_slashes(path);
2124 cwd = getcwd(NULL, 0);
2125 if (cwd == NULL) {
2126 error = got_error_from_errno("getcwd");
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), 1,
2138 got_worktree_get_root_path(worktree), 0, 0);
2139 if (error)
2140 goto done;
2142 error = got_worktree_revert(worktree, path,
2143 revert_progress, NULL, repo);
2144 if (error)
2145 goto done;
2146 done:
2147 if (repo)
2148 got_repo_close(repo);
2149 if (worktree)
2150 got_worktree_close(worktree);
2151 free(path);
2152 free(cwd);
2153 return error;
2156 __dead static void
2157 usage_commit(void)
2159 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2160 exit(1);
2163 int
2164 spawn_editor(const char *editor, const char *file)
2166 pid_t pid;
2167 sig_t sighup, sigint, sigquit;
2168 int st = -1;
2170 sighup = signal(SIGHUP, SIG_IGN);
2171 sigint = signal(SIGINT, SIG_IGN);
2172 sigquit = signal(SIGQUIT, SIG_IGN);
2174 switch (pid = fork()) {
2175 case -1:
2176 goto doneediting;
2177 case 0:
2178 execl(editor, editor, file, (char *)NULL);
2179 _exit(127);
2182 while (waitpid(pid, &st, 0) == -1)
2183 if (errno != EINTR)
2184 break;
2186 doneediting:
2187 (void)signal(SIGHUP, sighup);
2188 (void)signal(SIGINT, sigint);
2189 (void)signal(SIGQUIT, sigquit);
2191 if (!WIFEXITED(st)) {
2192 errno = EINTR;
2193 return -1;
2196 return WEXITSTATUS(st);
2199 struct collect_commit_logmsg_arg {
2200 const char *cmdline_log;
2201 const char *editor;
2202 const char *worktree_path;
2203 char *logmsg_path;
2207 static const struct got_error *
2208 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2209 void *arg)
2211 const char *initial_content = "\n# changes to be committed:\n";
2212 struct got_pathlist_entry *pe;
2213 const struct got_error *err = NULL;
2214 char *template = NULL;
2215 struct collect_commit_logmsg_arg *a = arg;
2216 char buf[1024];
2217 struct stat st, st2;
2218 FILE *fp;
2219 size_t len;
2220 int fd, content_changed = 0;
2222 /* if a message was specified on the command line, just use it */
2223 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2224 len = strlen(a->cmdline_log) + 1;
2225 *logmsg = malloc(len + 1);
2226 if (*logmsg == NULL)
2227 return got_error_from_errno("malloc");
2228 strlcpy(*logmsg, a->cmdline_log, len);
2229 return NULL;
2232 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2233 return got_error_from_errno("asprintf");
2235 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2236 if (err)
2237 goto done;
2239 dprintf(fd, initial_content);
2241 TAILQ_FOREACH(pe, commitable_paths, entry) {
2242 struct got_commitable *ct = pe->data;
2243 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2245 close(fd);
2247 if (stat(a->logmsg_path, &st) == -1) {
2248 err = got_error_from_errno2("stat", a->logmsg_path);
2249 goto done;
2252 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2253 err = got_error_from_errno("failed spawning editor");
2254 goto done;
2257 if (stat(a->logmsg_path, &st2) == -1) {
2258 err = got_error_from_errno("stat");
2259 goto done;
2262 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2263 unlink(a->logmsg_path);
2264 free(a->logmsg_path);
2265 a->logmsg_path = NULL;
2266 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2267 "no changes made to commit message, aborting");
2268 goto done;
2271 /* remove comments */
2272 *logmsg = malloc(st2.st_size + 1);
2273 if (*logmsg == NULL) {
2274 err = got_error_from_errno("malloc");
2275 goto done;
2277 len = 0;
2279 fp = fopen(a->logmsg_path, "r");
2280 while (fgets(buf, sizeof(buf), fp) != NULL) {
2281 if (!content_changed && strcmp(buf, initial_content) != 0)
2282 content_changed = 1;
2283 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2284 continue;
2285 len = strlcat(*logmsg, buf, st2.st_size);
2287 fclose(fp);
2289 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2290 (*logmsg)[len - 1] = '\0';
2291 len--;
2294 if (len == 0 || !content_changed) {
2295 unlink(a->logmsg_path);
2296 free(a->logmsg_path);
2297 a->logmsg_path = NULL;
2298 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2299 "commit message cannot be empty, aborting");
2300 goto done;
2302 done:
2303 free(template);
2304 return err;
2307 static const struct got_error *
2308 cmd_commit(int argc, char *argv[])
2310 const struct got_error *error = NULL;
2311 struct got_worktree *worktree = NULL;
2312 struct got_repository *repo = NULL;
2313 char *cwd = NULL, *path = NULL, *id_str = NULL;
2314 struct got_object_id *id = NULL;
2315 const char *logmsg = NULL;
2316 const char *got_author = getenv("GOT_AUTHOR");
2317 struct collect_commit_logmsg_arg cl_arg;
2318 char *editor = NULL;
2319 int ch;
2321 while ((ch = getopt(argc, argv, "m:")) != -1) {
2322 switch (ch) {
2323 case 'm':
2324 logmsg = optarg;
2325 break;
2326 default:
2327 usage_commit();
2328 /* NOTREACHED */
2332 argc -= optind;
2333 argv += optind;
2335 if (argc == 1) {
2336 path = realpath(argv[0], NULL);
2337 if (path == NULL) {
2338 error = got_error_from_errno2("realpath", argv[0]);
2339 goto done;
2341 got_path_strip_trailing_slashes(path);
2342 } else if (argc != 0)
2343 usage_commit();
2345 if (got_author == NULL) {
2346 /* TODO: Look current user up in password database */
2347 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2348 goto done;
2351 cwd = getcwd(NULL, 0);
2352 if (cwd == NULL) {
2353 error = got_error_from_errno("getcwd");
2354 goto done;
2356 error = got_worktree_open(&worktree, cwd);
2357 if (error)
2358 goto done;
2360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2361 if (error != NULL)
2362 goto done;
2364 error = apply_unveil(got_repo_get_path(repo), 0,
2365 got_worktree_get_root_path(worktree), 0, 1);
2366 if (error)
2367 goto done;
2369 error = get_editor(&editor);
2370 if (error)
2371 goto done;
2372 cl_arg.editor = editor;
2373 cl_arg.cmdline_log = logmsg;
2374 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2375 cl_arg.logmsg_path = NULL;
2376 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2377 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2378 if (error) {
2379 if (cl_arg.logmsg_path)
2380 fprintf(stderr, "%s: log message preserved in %s\n",
2381 getprogname(), cl_arg.logmsg_path);
2382 goto done;
2385 if (cl_arg.logmsg_path)
2386 unlink(cl_arg.logmsg_path);
2388 error = got_object_id_str(&id_str, id);
2389 if (error)
2390 goto done;
2391 printf("created commit %s\n", id_str);
2392 done:
2393 if (repo)
2394 got_repo_close(repo);
2395 if (worktree)
2396 got_worktree_close(worktree);
2397 free(path);
2398 free(cwd);
2399 free(id_str);
2400 free(editor);
2401 return error;