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 **abspath)
202 const struct got_error *err = NULL;
203 const char *editor;
205 editor = getenv("VISUAL");
206 if (editor == NULL)
207 editor = getenv("EDITOR");
209 if (editor) {
210 err = got_path_find_prog(abspath, editor);
211 if (err)
212 return err;
215 if (*abspath == NULL) {
216 *abspath = strdup("/bin/ed");
217 if (*abspath == NULL)
218 return got_error_from_errno("strdup");
221 return NULL;
224 static const struct got_error *
225 apply_unveil(const char *repo_path, int repo_read_only,
226 const char *worktree_path, int create_worktree)
228 const struct got_error *err;
229 static char err_msg[MAXPATHLEN + 36];
231 if (create_worktree) {
232 /* Pre-create work tree path to avoid unveiling its parents. */
233 err = got_path_mkdir(worktree_path);
235 if (errno == EEXIST) {
236 if (got_path_dir_is_empty(worktree_path)) {
237 errno = 0;
238 err = NULL;
239 } else {
240 snprintf(err_msg, sizeof(err_msg),
241 "%s: directory exists but is not empty",
242 worktree_path);
243 err = got_error_msg(GOT_ERR_BAD_PATH,
244 err_msg);
248 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
249 return err;
252 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
253 return got_error_from_errno2("unveil", repo_path);
255 if (worktree_path && unveil(worktree_path, "rwc") != 0)
256 return got_error_from_errno2("unveil", worktree_path);
258 if (unveil("/tmp", "rwc") != 0)
259 return got_error_from_errno2("unveil", "/tmp");
261 err = got_privsep_unveil_exec_helpers();
262 if (err != NULL)
263 return err;
265 if (unveil(NULL, NULL) != 0)
266 return got_error_from_errno("unveil");
268 return NULL;
271 __dead static void
272 usage_checkout(void)
274 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
275 "[-p prefix] repository-path [worktree-path]\n", getprogname());
276 exit(1);
279 static void
280 checkout_progress(void *arg, unsigned char status, const char *path)
282 char *worktree_path = arg;
284 while (path[0] == '/')
285 path++;
287 printf("%c %s/%s\n", status, worktree_path, path);
290 static const struct got_error *
291 check_cancelled(void *arg)
293 if (sigint_received || sigpipe_received)
294 return got_error(GOT_ERR_CANCELLED);
295 return NULL;
298 static const struct got_error *
299 check_linear_ancestry(struct got_worktree *worktree,
300 struct got_object_id *commit_id, struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_object_id *yca_id, *base_commit_id;
305 base_commit_id = got_worktree_get_base_commit_id(worktree);
306 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
307 commit_id, base_commit_id, repo);
308 if (err)
309 return err;
311 if (yca_id == NULL)
312 return got_error(GOT_ERR_ANCESTRY);
314 /*
315 * Require a straight line of history between the target commit
316 * and the work tree's base commit.
318 * Non-linear situation such as the this require a rebase:
320 * (commit) D F (base_commit)
321 * \ /
322 * C E
323 * \ /
324 * B (yca)
325 * |
326 * A
328 * 'got update' only handles linear cases:
329 * Update forwards in time: A (base/yca) - B - C - D (commit)
330 * Update backwards in time: D (base) - C - D - A (commit/yca)
331 */
332 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
333 got_object_id_cmp(base_commit_id, yca_id) != 0)
334 return got_error(GOT_ERR_ANCESTRY);
336 free(yca_id);
337 return NULL;
341 static const struct got_error *
342 cmd_checkout(int argc, char *argv[])
344 const struct got_error *error = NULL;
345 struct got_repository *repo = NULL;
346 struct got_reference *head_ref = NULL;
347 struct got_worktree *worktree = NULL;
348 char *repo_path = NULL;
349 char *worktree_path = NULL;
350 const char *path_prefix = "";
351 const char *branch_name = GOT_REF_HEAD;
352 char *commit_id_str = NULL;
353 int ch, same_path_prefix;
355 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
356 switch (ch) {
357 case 'b':
358 branch_name = optarg;
359 break;
360 case 'c':
361 commit_id_str = strdup(optarg);
362 if (commit_id_str == NULL)
363 return got_error_from_errno("strdup");
364 break;
365 case 'p':
366 path_prefix = optarg;
367 break;
368 default:
369 usage_checkout();
370 /* NOTREACHED */
374 argc -= optind;
375 argv += optind;
377 #ifndef PROFILE
378 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
379 "unveil", NULL) == -1)
380 err(1, "pledge");
381 #endif
382 if (argc == 1) {
383 char *cwd, *base, *dotgit;
384 repo_path = realpath(argv[0], NULL);
385 if (repo_path == NULL)
386 return got_error_from_errno2("realpath", argv[0]);
387 cwd = getcwd(NULL, 0);
388 if (cwd == NULL) {
389 error = got_error_from_errno("getcwd");
390 goto done;
392 if (path_prefix[0]) {
393 base = basename(path_prefix);
394 if (base == NULL) {
395 error = got_error_from_errno2("basename",
396 path_prefix);
397 goto done;
399 } else {
400 base = basename(repo_path);
401 if (base == NULL) {
402 error = got_error_from_errno2("basename",
403 repo_path);
404 goto done;
407 dotgit = strstr(base, ".git");
408 if (dotgit)
409 *dotgit = '\0';
410 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
411 error = got_error_from_errno("asprintf");
412 free(cwd);
413 goto done;
415 free(cwd);
416 } else if (argc == 2) {
417 repo_path = realpath(argv[0], NULL);
418 if (repo_path == NULL) {
419 error = got_error_from_errno2("realpath", argv[0]);
420 goto done;
422 worktree_path = realpath(argv[1], NULL);
423 if (worktree_path == NULL) {
424 error = got_error_from_errno2("realpath", argv[1]);
425 goto done;
427 } else
428 usage_checkout();
430 got_path_strip_trailing_slashes(repo_path);
431 got_path_strip_trailing_slashes(worktree_path);
433 error = got_repo_open(&repo, repo_path);
434 if (error != NULL)
435 goto done;
437 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
438 if (error)
439 goto done;
441 error = got_ref_open(&head_ref, repo, branch_name, 0);
442 if (error != NULL)
443 goto done;
445 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
446 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
447 goto done;
449 error = got_worktree_open(&worktree, worktree_path);
450 if (error != NULL)
451 goto done;
453 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
454 path_prefix);
455 if (error != NULL)
456 goto done;
457 if (!same_path_prefix) {
458 error = got_error(GOT_ERR_PATH_PREFIX);
459 goto done;
462 if (commit_id_str) {
463 struct got_object_id *commit_id;
464 error = got_object_resolve_id_str(&commit_id, repo,
465 commit_id_str);
466 if (error != NULL)
467 goto done;
468 error = check_linear_ancestry(worktree, commit_id, repo);
469 if (error != NULL) {
470 free(commit_id);
471 goto done;
473 error = got_worktree_set_base_commit_id(worktree, repo,
474 commit_id);
475 free(commit_id);
476 if (error)
477 goto done;
480 error = got_worktree_checkout_files(worktree, "", repo,
481 checkout_progress, worktree_path, check_cancelled, NULL);
482 if (error != NULL)
483 goto done;
485 printf("Now shut up and hack\n");
487 done:
488 free(commit_id_str);
489 free(repo_path);
490 free(worktree_path);
491 return error;
494 __dead static void
495 usage_update(void)
497 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
498 getprogname());
499 exit(1);
502 static void
503 update_progress(void *arg, unsigned char status, const char *path)
505 int *did_something = arg;
507 if (status == GOT_STATUS_EXISTS)
508 return;
510 *did_something = 1;
511 while (path[0] == '/')
512 path++;
513 printf("%c %s\n", status, path);
516 static const struct got_error *
517 cmd_update(int argc, char *argv[])
519 const struct got_error *error = NULL;
520 struct got_repository *repo = NULL;
521 struct got_worktree *worktree = NULL;
522 char *worktree_path = NULL, *path = NULL;
523 struct got_object_id *commit_id = NULL;
524 char *commit_id_str = NULL;
525 int ch, did_something = 0;
527 while ((ch = getopt(argc, argv, "c:")) != -1) {
528 switch (ch) {
529 case 'c':
530 commit_id_str = strdup(optarg);
531 if (commit_id_str == NULL)
532 return got_error_from_errno("strdup");
533 break;
534 default:
535 usage_update();
536 /* NOTREACHED */
540 argc -= optind;
541 argv += optind;
543 #ifndef PROFILE
544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
545 "unveil", NULL) == -1)
546 err(1, "pledge");
547 #endif
548 worktree_path = getcwd(NULL, 0);
549 if (worktree_path == NULL) {
550 error = got_error_from_errno("getcwd");
551 goto done;
553 error = got_worktree_open(&worktree, worktree_path);
554 if (error)
555 goto done;
557 if (argc == 0) {
558 path = strdup("");
559 if (path == NULL) {
560 error = got_error_from_errno("strdup");
561 goto done;
563 } else if (argc == 1) {
564 error = got_worktree_resolve_path(&path, worktree, argv[0]);
565 if (error)
566 goto done;
567 } else
568 usage_update();
570 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
571 if (error != NULL)
572 goto done;
574 error = apply_unveil(got_repo_get_path(repo), 0,
575 got_worktree_get_root_path(worktree), 0);
576 if (error)
577 goto done;
579 if (commit_id_str == NULL) {
580 struct got_reference *head_ref;
581 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
582 if (error != NULL)
583 goto done;
584 error = got_ref_resolve(&commit_id, repo, head_ref);
585 if (error != NULL)
586 goto done;
587 error = got_object_id_str(&commit_id_str, commit_id);
588 if (error != NULL)
589 goto done;
590 } else {
591 error = got_object_resolve_id_str(&commit_id, repo,
592 commit_id_str);
593 if (error != NULL)
594 goto done;
597 error = check_linear_ancestry(worktree, commit_id, repo);
598 if (error != NULL)
599 goto done;
601 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
602 commit_id) != 0) {
603 error = got_worktree_set_base_commit_id(worktree, repo,
604 commit_id);
605 if (error)
606 goto done;
609 error = got_worktree_checkout_files(worktree, path, repo,
610 update_progress, &did_something, check_cancelled, NULL);
611 if (error != NULL)
612 goto done;
614 if (did_something)
615 printf("Updated to commit %s\n", commit_id_str);
616 else
617 printf("Already up-to-date\n");
618 done:
619 free(worktree_path);
620 free(path);
621 free(commit_id);
622 free(commit_id_str);
623 return error;
626 static const struct got_error *
627 print_patch(struct got_commit_object *commit, struct got_object_id *id,
628 int diff_context, struct got_repository *repo)
630 const struct got_error *err = NULL;
631 struct got_tree_object *tree1 = NULL, *tree2;
632 struct got_object_qid *qid;
633 char *id_str1 = NULL, *id_str2;
635 err = got_object_open_as_tree(&tree2, repo,
636 got_object_commit_get_tree_id(commit));
637 if (err)
638 return err;
640 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
641 if (qid != NULL) {
642 struct got_commit_object *pcommit;
644 err = got_object_open_as_commit(&pcommit, repo, qid->id);
645 if (err)
646 return err;
648 err = got_object_open_as_tree(&tree1, repo,
649 got_object_commit_get_tree_id(pcommit));
650 got_object_commit_close(pcommit);
651 if (err)
652 return err;
654 err = got_object_id_str(&id_str1, qid->id);
655 if (err)
656 return err;
659 err = got_object_id_str(&id_str2, id);
660 if (err)
661 goto done;
663 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
664 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
665 done:
666 if (tree1)
667 got_object_tree_close(tree1);
668 got_object_tree_close(tree2);
669 free(id_str1);
670 free(id_str2);
671 return err;
674 static char *
675 get_datestr(time_t *time, char *datebuf)
677 char *p, *s = ctime_r(time, datebuf);
678 p = strchr(s, '\n');
679 if (p)
680 *p = '\0';
681 return s;
684 static const struct got_error *
685 print_commit(struct got_commit_object *commit, struct got_object_id *id,
686 struct got_repository *repo, int show_patch, int diff_context,
687 struct got_reflist_head *refs)
689 const struct got_error *err = NULL;
690 char *id_str, *datestr, *logmsg0, *logmsg, *line;
691 char datebuf[26];
692 time_t committer_time;
693 const char *author, *committer;
694 char *refs_str = NULL;
695 struct got_reflist_entry *re;
697 SIMPLEQ_FOREACH(re, refs, entry) {
698 char *s;
699 const char *name;
700 if (got_object_id_cmp(re->id, id) != 0)
701 continue;
702 name = got_ref_get_name(re->ref);
703 if (strcmp(name, GOT_REF_HEAD) == 0)
704 continue;
705 if (strncmp(name, "refs/", 5) == 0)
706 name += 5;
707 if (strncmp(name, "got/", 4) == 0)
708 continue;
709 if (strncmp(name, "heads/", 6) == 0)
710 name += 6;
711 if (strncmp(name, "remotes/", 8) == 0)
712 name += 8;
713 s = refs_str;
714 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
715 name) == -1) {
716 err = got_error_from_errno("asprintf");
717 free(s);
718 break;
720 free(s);
722 err = got_object_id_str(&id_str, id);
723 if (err)
724 return err;
726 printf("-----------------------------------------------\n");
727 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
728 refs_str ? refs_str : "", refs_str ? ")" : "");
729 free(id_str);
730 id_str = NULL;
731 free(refs_str);
732 refs_str = NULL;
733 printf("from: %s\n", got_object_commit_get_author(commit));
734 committer_time = got_object_commit_get_committer_time(commit);
735 datestr = get_datestr(&committer_time, datebuf);
736 printf("date: %s UTC\n", datestr);
737 author = got_object_commit_get_author(commit);
738 committer = got_object_commit_get_committer(commit);
739 if (strcmp(author, committer) != 0)
740 printf("via: %s\n", committer);
741 if (got_object_commit_get_nparents(commit) > 1) {
742 const struct got_object_id_queue *parent_ids;
743 struct got_object_qid *qid;
744 int n = 1;
745 parent_ids = got_object_commit_get_parent_ids(commit);
746 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
747 err = got_object_id_str(&id_str, qid->id);
748 if (err)
749 return err;
750 printf("parent %d: %s\n", n++, id_str);
751 free(id_str);
755 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
756 if (logmsg0 == NULL)
757 return got_error_from_errno("strdup");
759 logmsg = logmsg0;
760 do {
761 line = strsep(&logmsg, "\n");
762 if (line)
763 printf(" %s\n", line);
764 } while (line);
765 free(logmsg0);
767 if (show_patch) {
768 err = print_patch(commit, id, diff_context, repo);
769 if (err == 0)
770 printf("\n");
773 if (fflush(stdout) != 0 && err == NULL)
774 err = got_error_from_errno("fflush");
775 return err;
778 static const struct got_error *
779 print_commits(struct got_object_id *root_id, struct got_repository *repo,
780 char *path, int show_patch, int diff_context, int limit,
781 int first_parent_traversal, struct got_reflist_head *refs)
783 const struct got_error *err;
784 struct got_commit_graph *graph;
786 err = got_commit_graph_open(&graph, root_id, path,
787 first_parent_traversal, repo);
788 if (err)
789 return err;
790 err = got_commit_graph_iter_start(graph, root_id, repo);
791 if (err)
792 goto done;
793 for (;;) {
794 struct got_commit_object *commit;
795 struct got_object_id *id;
797 if (sigint_received || sigpipe_received)
798 break;
800 err = got_commit_graph_iter_next(&id, graph);
801 if (err) {
802 if (err->code == GOT_ERR_ITER_COMPLETED) {
803 err = NULL;
804 break;
806 if (err->code != GOT_ERR_ITER_NEED_MORE)
807 break;
808 err = got_commit_graph_fetch_commits(graph, 1, repo);
809 if (err)
810 break;
811 else
812 continue;
814 if (id == NULL)
815 break;
817 err = got_object_open_as_commit(&commit, repo, id);
818 if (err)
819 break;
820 err = print_commit(commit, id, repo, show_patch, diff_context,
821 refs);
822 got_object_commit_close(commit);
823 if (err || (limit && --limit == 0))
824 break;
826 done:
827 got_commit_graph_close(graph);
828 return err;
831 __dead static void
832 usage_log(void)
834 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
835 "[-r repository-path] [path]\n", getprogname());
836 exit(1);
839 static const struct got_error *
840 cmd_log(int argc, char *argv[])
842 const struct got_error *error;
843 struct got_repository *repo = NULL;
844 struct got_worktree *worktree = NULL;
845 struct got_commit_object *commit = NULL;
846 struct got_object_id *id = NULL;
847 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
848 char *start_commit = NULL;
849 int diff_context = 3, ch;
850 int show_patch = 0, limit = 0, first_parent_traversal = 0;
851 const char *errstr;
852 struct got_reflist_head refs;
854 SIMPLEQ_INIT(&refs);
856 #ifndef PROFILE
857 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
858 NULL)
859 == -1)
860 err(1, "pledge");
861 #endif
863 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
864 switch (ch) {
865 case 'p':
866 show_patch = 1;
867 break;
868 case 'c':
869 start_commit = optarg;
870 break;
871 case 'C':
872 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
873 &errstr);
874 if (errstr != NULL)
875 err(1, "-C option %s", errstr);
876 break;
877 case 'l':
878 limit = strtonum(optarg, 1, INT_MAX, &errstr);
879 if (errstr != NULL)
880 err(1, "-l option %s", errstr);
881 break;
882 case 'f':
883 first_parent_traversal = 1;
884 break;
885 case 'r':
886 repo_path = realpath(optarg, NULL);
887 if (repo_path == NULL)
888 err(1, "-r option");
889 got_path_strip_trailing_slashes(repo_path);
890 break;
891 default:
892 usage_log();
893 /* NOTREACHED */
897 argc -= optind;
898 argv += optind;
900 cwd = getcwd(NULL, 0);
901 if (cwd == NULL) {
902 error = got_error_from_errno("getcwd");
903 goto done;
906 error = got_worktree_open(&worktree, cwd);
907 if (error && error->code != GOT_ERR_NOT_WORKTREE)
908 goto done;
909 error = NULL;
911 if (argc == 0) {
912 path = strdup("");
913 if (path == NULL) {
914 error = got_error_from_errno("strdup");
915 goto done;
917 } else if (argc == 1) {
918 if (worktree) {
919 error = got_worktree_resolve_path(&path, worktree,
920 argv[0]);
921 if (error)
922 goto done;
923 } else {
924 path = strdup(argv[0]);
925 if (path == NULL) {
926 error = got_error_from_errno("strdup");
927 goto done;
930 } else
931 usage_log();
933 if (repo_path == NULL) {
934 repo_path = worktree ?
935 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
937 if (repo_path == NULL) {
938 error = got_error_from_errno("strdup");
939 goto done;
942 error = got_repo_open(&repo, repo_path);
943 if (error != NULL)
944 goto done;
946 error = apply_unveil(got_repo_get_path(repo), 1,
947 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
948 if (error)
949 goto done;
951 if (start_commit == NULL) {
952 struct got_reference *head_ref;
953 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
954 if (error != NULL)
955 return error;
956 error = got_ref_resolve(&id, repo, head_ref);
957 got_ref_close(head_ref);
958 if (error != NULL)
959 return error;
960 error = got_object_open_as_commit(&commit, repo, id);
961 } else {
962 struct got_reference *ref;
963 error = got_ref_open(&ref, repo, start_commit, 0);
964 if (error == NULL) {
965 int obj_type;
966 error = got_ref_resolve(&id, repo, ref);
967 got_ref_close(ref);
968 if (error != NULL)
969 goto done;
970 error = got_object_get_type(&obj_type, repo, id);
971 if (error != NULL)
972 goto done;
973 if (obj_type == GOT_OBJ_TYPE_TAG) {
974 struct got_tag_object *tag;
975 error = got_object_open_as_tag(&tag, repo, id);
976 if (error != NULL)
977 goto done;
978 if (got_object_tag_get_object_type(tag) !=
979 GOT_OBJ_TYPE_COMMIT) {
980 got_object_tag_close(tag);
981 error = got_error(GOT_ERR_OBJ_TYPE);
982 goto done;
984 free(id);
985 id = got_object_id_dup(
986 got_object_tag_get_object_id(tag));
987 if (id == NULL)
988 error = got_error_from_errno(
989 "got_object_id_dup");
990 got_object_tag_close(tag);
991 if (error)
992 goto done;
993 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
994 error = got_error(GOT_ERR_OBJ_TYPE);
995 goto done;
997 error = got_object_open_as_commit(&commit, repo, id);
998 if (error != NULL)
999 goto done;
1001 if (commit == NULL) {
1002 error = got_object_resolve_id_str(&id, repo,
1003 start_commit);
1004 if (error != NULL)
1005 return error;
1008 if (error != NULL)
1009 goto done;
1011 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1012 if (error != NULL)
1013 goto done;
1014 if (in_repo_path) {
1015 free(path);
1016 path = in_repo_path;
1019 error = got_ref_list(&refs, repo);
1020 if (error)
1021 goto done;
1023 error = print_commits(id, repo, path, show_patch,
1024 diff_context, limit, first_parent_traversal, &refs);
1025 done:
1026 free(path);
1027 free(repo_path);
1028 free(cwd);
1029 free(id);
1030 if (worktree)
1031 got_worktree_close(worktree);
1032 if (repo) {
1033 const struct got_error *repo_error;
1034 repo_error = got_repo_close(repo);
1035 if (error == NULL)
1036 error = repo_error;
1038 got_ref_list_free(&refs);
1039 return error;
1042 __dead static void
1043 usage_diff(void)
1045 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1046 "[object1 object2 | path]\n", getprogname());
1047 exit(1);
1050 struct print_diff_arg {
1051 struct got_repository *repo;
1052 struct got_worktree *worktree;
1053 int diff_context;
1054 const char *id_str;
1055 int header_shown;
1058 static const struct got_error *
1059 print_diff(void *arg, unsigned char status, const char *path,
1060 struct got_object_id *blob_id, struct got_object_id *commit_id)
1062 struct print_diff_arg *a = arg;
1063 const struct got_error *err = NULL;
1064 struct got_blob_object *blob1 = NULL;
1065 FILE *f2 = NULL;
1066 char *abspath = NULL;
1067 struct stat sb;
1069 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1070 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1071 return NULL;
1073 if (!a->header_shown) {
1074 printf("diff %s %s\n", a->id_str,
1075 got_worktree_get_root_path(a->worktree));
1076 a->header_shown = 1;
1079 if (status != GOT_STATUS_ADD) {
1080 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1081 if (err)
1082 goto done;
1086 if (status != GOT_STATUS_DELETE) {
1087 if (asprintf(&abspath, "%s/%s",
1088 got_worktree_get_root_path(a->worktree), path) == -1) {
1089 err = got_error_from_errno("asprintf");
1090 goto done;
1093 f2 = fopen(abspath, "r");
1094 if (f2 == NULL) {
1095 err = got_error_from_errno2("fopen", abspath);
1096 goto done;
1098 if (lstat(abspath, &sb) == -1) {
1099 err = got_error_from_errno2("lstat", abspath);
1100 goto done;
1102 } else
1103 sb.st_size = 0;
1105 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1106 stdout);
1107 done:
1108 if (blob1)
1109 got_object_blob_close(blob1);
1110 if (f2 && fclose(f2) != 0 && err == NULL)
1111 err = got_error_from_errno("fclose");
1112 free(abspath);
1113 return err;
1116 static const struct got_error *
1117 cmd_diff(int argc, char *argv[])
1119 const struct got_error *error;
1120 struct got_repository *repo = NULL;
1121 struct got_worktree *worktree = NULL;
1122 char *cwd = NULL, *repo_path = NULL;
1123 struct got_object_id *id1 = NULL, *id2 = NULL;
1124 char *id_str1 = NULL, *id_str2 = NULL;
1125 int type1, type2;
1126 int diff_context = 3, ch;
1127 const char *errstr;
1128 char *path = NULL;
1130 #ifndef PROFILE
1131 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1132 NULL) == -1)
1133 err(1, "pledge");
1134 #endif
1136 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1137 switch (ch) {
1138 case 'C':
1139 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1140 if (errstr != NULL)
1141 err(1, "-C option %s", errstr);
1142 break;
1143 case 'r':
1144 repo_path = realpath(optarg, NULL);
1145 if (repo_path == NULL)
1146 err(1, "-r option");
1147 got_path_strip_trailing_slashes(repo_path);
1148 break;
1149 default:
1150 usage_diff();
1151 /* NOTREACHED */
1155 argc -= optind;
1156 argv += optind;
1158 cwd = getcwd(NULL, 0);
1159 if (cwd == NULL) {
1160 error = got_error_from_errno("getcwd");
1161 goto done;
1163 error = got_worktree_open(&worktree, cwd);
1164 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1165 goto done;
1166 if (argc <= 1) {
1167 if (worktree == NULL) {
1168 error = got_error(GOT_ERR_NOT_WORKTREE);
1169 goto done;
1171 if (repo_path)
1172 errx(1,
1173 "-r option can't be used when diffing a work tree");
1174 repo_path = strdup(got_worktree_get_repo_path(worktree));
1175 if (repo_path == NULL) {
1176 error = got_error_from_errno("strdup");
1177 goto done;
1179 if (argc == 1) {
1180 error = got_worktree_resolve_path(&path, worktree,
1181 argv[0]);
1182 if (error)
1183 goto done;
1184 } else {
1185 path = strdup("");
1186 if (path == NULL) {
1187 error = got_error_from_errno("strdup");
1188 goto done;
1191 } else if (argc == 2) {
1192 id_str1 = argv[0];
1193 id_str2 = argv[1];
1194 } else
1195 usage_diff();
1197 if (repo_path == NULL) {
1198 repo_path = getcwd(NULL, 0);
1199 if (repo_path == NULL)
1200 return got_error_from_errno("getcwd");
1203 error = got_repo_open(&repo, repo_path);
1204 free(repo_path);
1205 if (error != NULL)
1206 goto done;
1208 error = apply_unveil(got_repo_get_path(repo), 1,
1209 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1210 if (error)
1211 goto done;
1213 if (worktree) {
1214 struct print_diff_arg arg;
1215 char *id_str;
1216 error = got_object_id_str(&id_str,
1217 got_worktree_get_base_commit_id(worktree));
1218 if (error)
1219 goto done;
1220 arg.repo = repo;
1221 arg.worktree = worktree;
1222 arg.diff_context = diff_context;
1223 arg.id_str = id_str;
1224 arg.header_shown = 0;
1226 error = got_worktree_status(worktree, path, repo, print_diff,
1227 &arg, check_cancelled, NULL);
1228 free(id_str);
1229 goto done;
1232 error = got_object_resolve_id_str(&id1, repo, id_str1);
1233 if (error)
1234 goto done;
1236 error = got_object_resolve_id_str(&id2, repo, id_str2);
1237 if (error)
1238 goto done;
1240 error = got_object_get_type(&type1, repo, id1);
1241 if (error)
1242 goto done;
1244 error = got_object_get_type(&type2, repo, id2);
1245 if (error)
1246 goto done;
1248 if (type1 != type2) {
1249 error = got_error(GOT_ERR_OBJ_TYPE);
1250 goto done;
1253 switch (type1) {
1254 case GOT_OBJ_TYPE_BLOB:
1255 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1256 diff_context, repo, stdout);
1257 break;
1258 case GOT_OBJ_TYPE_TREE:
1259 error = got_diff_objects_as_trees(id1, id2, "", "",
1260 diff_context, repo, stdout);
1261 break;
1262 case GOT_OBJ_TYPE_COMMIT:
1263 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1264 id_str2);
1265 error = got_diff_objects_as_commits(id1, id2, diff_context,
1266 repo, stdout);
1267 break;
1268 default:
1269 error = got_error(GOT_ERR_OBJ_TYPE);
1272 done:
1273 free(id1);
1274 free(id2);
1275 free(path);
1276 if (worktree)
1277 got_worktree_close(worktree);
1278 if (repo) {
1279 const struct got_error *repo_error;
1280 repo_error = got_repo_close(repo);
1281 if (error == NULL)
1282 error = repo_error;
1284 return error;
1287 __dead static void
1288 usage_blame(void)
1290 fprintf(stderr,
1291 "usage: %s blame [-c commit] [-r repository-path] path\n",
1292 getprogname());
1293 exit(1);
1296 static const struct got_error *
1297 cmd_blame(int argc, char *argv[])
1299 const struct got_error *error;
1300 struct got_repository *repo = NULL;
1301 struct got_worktree *worktree = NULL;
1302 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1303 struct got_object_id *commit_id = NULL;
1304 char *commit_id_str = NULL;
1305 int ch;
1307 #ifndef PROFILE
1308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1309 NULL) == -1)
1310 err(1, "pledge");
1311 #endif
1313 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1314 switch (ch) {
1315 case 'c':
1316 commit_id_str = optarg;
1317 break;
1318 case 'r':
1319 repo_path = realpath(optarg, NULL);
1320 if (repo_path == NULL)
1321 err(1, "-r option");
1322 got_path_strip_trailing_slashes(repo_path);
1323 break;
1324 default:
1325 usage_blame();
1326 /* NOTREACHED */
1330 argc -= optind;
1331 argv += optind;
1333 if (argc == 1)
1334 path = argv[0];
1335 else
1336 usage_blame();
1338 cwd = getcwd(NULL, 0);
1339 if (cwd == NULL) {
1340 error = got_error_from_errno("getcwd");
1341 goto done;
1343 if (repo_path == NULL) {
1344 error = got_worktree_open(&worktree, cwd);
1345 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1346 goto done;
1347 else
1348 error = NULL;
1349 if (worktree) {
1350 repo_path =
1351 strdup(got_worktree_get_repo_path(worktree));
1352 if (repo_path == NULL)
1353 error = got_error_from_errno("strdup");
1354 if (error)
1355 goto done;
1356 } else {
1357 repo_path = strdup(cwd);
1358 if (repo_path == NULL) {
1359 error = got_error_from_errno("strdup");
1360 goto done;
1365 error = got_repo_open(&repo, repo_path);
1366 if (error != NULL)
1367 goto done;
1369 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1370 if (error)
1371 goto done;
1373 if (worktree) {
1374 const char *prefix = got_worktree_get_path_prefix(worktree);
1375 char *p, *worktree_subdir = cwd +
1376 strlen(got_worktree_get_root_path(worktree));
1377 if (asprintf(&p, "%s%s%s%s%s",
1378 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1379 worktree_subdir, worktree_subdir[0] ? "/" : "",
1380 path) == -1) {
1381 error = got_error_from_errno("asprintf");
1382 goto done;
1384 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1385 free(p);
1386 } else {
1387 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1389 if (error)
1390 goto done;
1392 if (commit_id_str == NULL) {
1393 struct got_reference *head_ref;
1394 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1395 if (error != NULL)
1396 goto done;
1397 error = got_ref_resolve(&commit_id, repo, head_ref);
1398 got_ref_close(head_ref);
1399 if (error != NULL)
1400 goto done;
1401 } else {
1402 error = got_object_resolve_id_str(&commit_id, repo,
1403 commit_id_str);
1404 if (error != NULL)
1405 goto done;
1408 error = got_blame(in_repo_path, commit_id, repo, stdout);
1409 done:
1410 free(in_repo_path);
1411 free(repo_path);
1412 free(cwd);
1413 free(commit_id);
1414 if (worktree)
1415 got_worktree_close(worktree);
1416 if (repo) {
1417 const struct got_error *repo_error;
1418 repo_error = got_repo_close(repo);
1419 if (error == NULL)
1420 error = repo_error;
1422 return error;
1425 __dead static void
1426 usage_tree(void)
1428 fprintf(stderr,
1429 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1430 getprogname());
1431 exit(1);
1434 static void
1435 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1436 const char *root_path)
1438 int is_root_path = (strcmp(path, root_path) == 0);
1440 path += strlen(root_path);
1441 while (path[0] == '/')
1442 path++;
1444 printf("%s%s%s%s%s\n", id ? id : "", path,
1445 is_root_path ? "" : "/", te->name,
1446 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1449 static const struct got_error *
1450 print_tree(const char *path, struct got_object_id *commit_id,
1451 int show_ids, int recurse, const char *root_path,
1452 struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_object_id *tree_id = NULL;
1456 struct got_tree_object *tree = NULL;
1457 const struct got_tree_entries *entries;
1458 struct got_tree_entry *te;
1460 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1461 if (err)
1462 goto done;
1464 err = got_object_open_as_tree(&tree, repo, tree_id);
1465 if (err)
1466 goto done;
1467 entries = got_object_tree_get_entries(tree);
1468 te = SIMPLEQ_FIRST(&entries->head);
1469 while (te) {
1470 char *id = NULL;
1472 if (sigint_received || sigpipe_received)
1473 break;
1475 if (show_ids) {
1476 char *id_str;
1477 err = got_object_id_str(&id_str, te->id);
1478 if (err)
1479 goto done;
1480 if (asprintf(&id, "%s ", id_str) == -1) {
1481 err = got_error_from_errno("asprintf");
1482 free(id_str);
1483 goto done;
1485 free(id_str);
1487 print_entry(te, id, path, root_path);
1488 free(id);
1490 if (recurse && S_ISDIR(te->mode)) {
1491 char *child_path;
1492 if (asprintf(&child_path, "%s%s%s", path,
1493 path[0] == '/' && path[1] == '\0' ? "" : "/",
1494 te->name) == -1) {
1495 err = got_error_from_errno("asprintf");
1496 goto done;
1498 err = print_tree(child_path, commit_id, show_ids, 1,
1499 root_path, repo);
1500 free(child_path);
1501 if (err)
1502 goto done;
1505 te = SIMPLEQ_NEXT(te, entry);
1507 done:
1508 if (tree)
1509 got_object_tree_close(tree);
1510 free(tree_id);
1511 return err;
1514 static const struct got_error *
1515 cmd_tree(int argc, char *argv[])
1517 const struct got_error *error;
1518 struct got_repository *repo = NULL;
1519 struct got_worktree *worktree = NULL;
1520 const char *path;
1521 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1522 struct got_object_id *commit_id = NULL;
1523 char *commit_id_str = NULL;
1524 int show_ids = 0, recurse = 0;
1525 int ch;
1527 #ifndef PROFILE
1528 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1529 NULL) == -1)
1530 err(1, "pledge");
1531 #endif
1533 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1534 switch (ch) {
1535 case 'c':
1536 commit_id_str = optarg;
1537 break;
1538 case 'r':
1539 repo_path = realpath(optarg, NULL);
1540 if (repo_path == NULL)
1541 err(1, "-r option");
1542 got_path_strip_trailing_slashes(repo_path);
1543 break;
1544 case 'i':
1545 show_ids = 1;
1546 break;
1547 case 'R':
1548 recurse = 1;
1549 break;
1550 default:
1551 usage_tree();
1552 /* NOTREACHED */
1556 argc -= optind;
1557 argv += optind;
1559 if (argc == 1)
1560 path = argv[0];
1561 else if (argc > 1)
1562 usage_tree();
1563 else
1564 path = NULL;
1566 cwd = getcwd(NULL, 0);
1567 if (cwd == NULL) {
1568 error = got_error_from_errno("getcwd");
1569 goto done;
1571 if (repo_path == NULL) {
1572 error = got_worktree_open(&worktree, cwd);
1573 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1574 goto done;
1575 else
1576 error = NULL;
1577 if (worktree) {
1578 repo_path =
1579 strdup(got_worktree_get_repo_path(worktree));
1580 if (repo_path == NULL)
1581 error = got_error_from_errno("strdup");
1582 if (error)
1583 goto done;
1584 } else {
1585 repo_path = strdup(cwd);
1586 if (repo_path == NULL) {
1587 error = got_error_from_errno("strdup");
1588 goto done;
1593 error = got_repo_open(&repo, repo_path);
1594 if (error != NULL)
1595 goto done;
1597 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1598 if (error)
1599 goto done;
1601 if (path == NULL) {
1602 if (worktree) {
1603 char *p, *worktree_subdir = cwd +
1604 strlen(got_worktree_get_root_path(worktree));
1605 if (asprintf(&p, "%s/%s",
1606 got_worktree_get_path_prefix(worktree),
1607 worktree_subdir) == -1) {
1608 error = got_error_from_errno("asprintf");
1609 goto done;
1611 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1612 free(p);
1613 if (error)
1614 goto done;
1615 } else
1616 path = "/";
1618 if (in_repo_path == NULL) {
1619 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1620 if (error != NULL)
1621 goto done;
1624 if (commit_id_str == NULL) {
1625 struct got_reference *head_ref;
1626 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1627 if (error != NULL)
1628 goto done;
1629 error = got_ref_resolve(&commit_id, repo, head_ref);
1630 got_ref_close(head_ref);
1631 if (error != NULL)
1632 goto done;
1633 } else {
1634 error = got_object_resolve_id_str(&commit_id, repo,
1635 commit_id_str);
1636 if (error != NULL)
1637 goto done;
1640 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1641 in_repo_path, repo);
1642 done:
1643 free(in_repo_path);
1644 free(repo_path);
1645 free(cwd);
1646 free(commit_id);
1647 if (worktree)
1648 got_worktree_close(worktree);
1649 if (repo) {
1650 const struct got_error *repo_error;
1651 repo_error = got_repo_close(repo);
1652 if (error == NULL)
1653 error = repo_error;
1655 return error;
1658 __dead static void
1659 usage_status(void)
1661 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1662 exit(1);
1665 static const struct got_error *
1666 print_status(void *arg, unsigned char status, const char *path,
1667 struct got_object_id *blob_id, struct got_object_id *commit_id)
1669 printf("%c %s\n", status, path);
1670 return NULL;
1673 static const struct got_error *
1674 cmd_status(int argc, char *argv[])
1676 const struct got_error *error = NULL;
1677 struct got_repository *repo = NULL;
1678 struct got_worktree *worktree = NULL;
1679 char *cwd = NULL, *path = NULL;
1680 int ch;
1682 while ((ch = getopt(argc, argv, "")) != -1) {
1683 switch (ch) {
1684 default:
1685 usage_status();
1686 /* NOTREACHED */
1690 argc -= optind;
1691 argv += optind;
1693 #ifndef PROFILE
1694 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1695 NULL) == -1)
1696 err(1, "pledge");
1697 #endif
1698 cwd = getcwd(NULL, 0);
1699 if (cwd == NULL) {
1700 error = got_error_from_errno("getcwd");
1701 goto done;
1704 error = got_worktree_open(&worktree, cwd);
1705 if (error != NULL)
1706 goto done;
1708 if (argc == 0) {
1709 path = strdup("");
1710 if (path == NULL) {
1711 error = got_error_from_errno("strdup");
1712 goto done;
1714 } else if (argc == 1) {
1715 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1716 if (error)
1717 goto done;
1718 } else
1719 usage_status();
1721 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1722 if (error != NULL)
1723 goto done;
1725 error = apply_unveil(got_repo_get_path(repo), 1,
1726 got_worktree_get_root_path(worktree), 0);
1727 if (error)
1728 goto done;
1730 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1731 check_cancelled, NULL);
1732 done:
1733 free(cwd);
1734 free(path);
1735 return error;
1738 __dead static void
1739 usage_ref(void)
1741 fprintf(stderr,
1742 "usage: %s ref [-r repository] -l | -d name | name target\n",
1743 getprogname());
1744 exit(1);
1747 static const struct got_error *
1748 list_refs(struct got_repository *repo)
1750 static const struct got_error *err = NULL;
1751 struct got_reflist_head refs;
1752 struct got_reflist_entry *re;
1754 SIMPLEQ_INIT(&refs);
1755 err = got_ref_list(&refs, repo);
1756 if (err)
1757 return err;
1759 SIMPLEQ_FOREACH(re, &refs, entry) {
1760 char *refstr;
1761 refstr = got_ref_to_str(re->ref);
1762 if (refstr == NULL)
1763 return got_error_from_errno("got_ref_to_str");
1764 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1765 free(refstr);
1768 got_ref_list_free(&refs);
1769 return NULL;
1772 static const struct got_error *
1773 delete_ref(struct got_repository *repo, const char *refname)
1775 const struct got_error *err = NULL;
1776 struct got_reference *ref;
1778 err = got_ref_open(&ref, repo, refname, 0);
1779 if (err)
1780 return err;
1782 err = got_ref_delete(ref, repo);
1783 got_ref_close(ref);
1784 return err;
1787 static const struct got_error *
1788 add_ref(struct got_repository *repo, const char *refname, const char *target)
1790 const struct got_error *err = NULL;
1791 struct got_object_id *id;
1792 struct got_reference *ref = NULL;
1794 err = got_object_resolve_id_str(&id, repo, target);
1795 if (err) {
1796 struct got_reference *target_ref;
1798 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1799 return err;
1800 err = got_ref_open(&target_ref, repo, target, 0);
1801 if (err)
1802 return err;
1803 err = got_ref_resolve(&id, repo, target_ref);
1804 got_ref_close(target_ref);
1805 if (err)
1806 return err;
1809 err = got_ref_alloc(&ref, refname, id);
1810 if (err)
1811 goto done;
1813 err = got_ref_write(ref, repo);
1814 done:
1815 if (ref)
1816 got_ref_close(ref);
1817 free(id);
1818 return err;
1821 static const struct got_error *
1822 cmd_ref(int argc, char *argv[])
1824 const struct got_error *error = NULL;
1825 struct got_repository *repo = NULL;
1826 struct got_worktree *worktree = NULL;
1827 char *cwd = NULL, *repo_path = NULL;
1828 int ch, do_list = 0;
1829 const char *delref = NULL;
1831 /* TODO: Add -s option for adding symbolic references. */
1832 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1833 switch (ch) {
1834 case 'd':
1835 delref = optarg;
1836 break;
1837 case 'r':
1838 repo_path = realpath(optarg, NULL);
1839 if (repo_path == NULL)
1840 err(1, "-r option");
1841 got_path_strip_trailing_slashes(repo_path);
1842 break;
1843 case 'l':
1844 do_list = 1;
1845 break;
1846 default:
1847 usage_ref();
1848 /* NOTREACHED */
1852 if (do_list && delref)
1853 errx(1, "-l and -d options are mutually exclusive\n");
1855 argc -= optind;
1856 argv += optind;
1858 if (do_list || delref) {
1859 if (argc > 0)
1860 usage_ref();
1861 } else if (argc != 2)
1862 usage_ref();
1864 #ifndef PROFILE
1865 if (do_list) {
1866 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1867 NULL) == -1)
1868 err(1, "pledge");
1869 } else {
1870 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1871 "sendfd unveil", NULL) == -1)
1872 err(1, "pledge");
1874 #endif
1875 cwd = getcwd(NULL, 0);
1876 if (cwd == NULL) {
1877 error = got_error_from_errno("getcwd");
1878 goto done;
1881 if (repo_path == NULL) {
1882 error = got_worktree_open(&worktree, cwd);
1883 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1884 goto done;
1885 else
1886 error = NULL;
1887 if (worktree) {
1888 repo_path =
1889 strdup(got_worktree_get_repo_path(worktree));
1890 if (repo_path == NULL)
1891 error = got_error_from_errno("strdup");
1892 if (error)
1893 goto done;
1894 } else {
1895 repo_path = strdup(cwd);
1896 if (repo_path == NULL) {
1897 error = got_error_from_errno("strdup");
1898 goto done;
1903 error = got_repo_open(&repo, repo_path);
1904 if (error != NULL)
1905 goto done;
1907 error = apply_unveil(got_repo_get_path(repo), do_list,
1908 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1909 if (error)
1910 goto done;
1912 if (do_list)
1913 error = list_refs(repo);
1914 else if (delref)
1915 error = delete_ref(repo, delref);
1916 else
1917 error = add_ref(repo, argv[0], argv[1]);
1918 done:
1919 if (repo)
1920 got_repo_close(repo);
1921 if (worktree)
1922 got_worktree_close(worktree);
1923 free(cwd);
1924 free(repo_path);
1925 return error;
1928 __dead static void
1929 usage_add(void)
1931 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1932 exit(1);
1935 static const struct got_error *
1936 cmd_add(int argc, char *argv[])
1938 const struct got_error *error = NULL;
1939 struct got_repository *repo = NULL;
1940 struct got_worktree *worktree = NULL;
1941 char *cwd = NULL;
1942 struct got_pathlist_head paths;
1943 struct got_pathlist_entry *pe;
1944 int ch, x;
1946 TAILQ_INIT(&paths);
1948 while ((ch = getopt(argc, argv, "")) != -1) {
1949 switch (ch) {
1950 default:
1951 usage_add();
1952 /* NOTREACHED */
1956 argc -= optind;
1957 argv += optind;
1959 if (argc < 1)
1960 usage_add();
1962 /* make sure each file exists before doing anything halfway */
1963 for (x = 0; x < argc; x++) {
1964 char *path = realpath(argv[x], NULL);
1965 if (path == NULL) {
1966 error = got_error_from_errno2("realpath", argv[x]);
1967 goto done;
1969 free(path);
1972 cwd = getcwd(NULL, 0);
1973 if (cwd == NULL) {
1974 error = got_error_from_errno("getcwd");
1975 goto done;
1978 error = got_worktree_open(&worktree, cwd);
1979 if (error)
1980 goto done;
1982 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1983 if (error != NULL)
1984 goto done;
1986 error = apply_unveil(got_repo_get_path(repo), 1,
1987 got_worktree_get_root_path(worktree), 0);
1988 if (error)
1989 goto done;
1991 for (x = 0; x < argc; x++) {
1992 char *path = realpath(argv[x], NULL);
1993 if (path == NULL) {
1994 error = got_error_from_errno2("realpath", argv[x]);
1995 goto done;
1998 got_path_strip_trailing_slashes(path);
1999 error = got_pathlist_insert(&pe, &paths, path, NULL);
2000 if (error) {
2001 free(path);
2002 goto done;
2005 error = got_worktree_schedule_add(worktree, &paths, print_status,
2006 NULL, repo);
2007 done:
2008 if (repo)
2009 got_repo_close(repo);
2010 if (worktree)
2011 got_worktree_close(worktree);
2012 TAILQ_FOREACH(pe, &paths, entry)
2013 free((char *)pe->path);
2014 got_pathlist_free(&paths);
2015 free(cwd);
2016 return error;
2019 __dead static void
2020 usage_rm(void)
2022 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2023 exit(1);
2026 static const struct got_error *
2027 cmd_rm(int argc, char *argv[])
2029 const struct got_error *error = NULL;
2030 struct got_worktree *worktree = NULL;
2031 struct got_repository *repo = NULL;
2032 char *cwd = NULL, *path = NULL;
2033 int ch, delete_local_mods = 0;
2035 while ((ch = getopt(argc, argv, "f")) != -1) {
2036 switch (ch) {
2037 case 'f':
2038 delete_local_mods = 1;
2039 break;
2040 default:
2041 usage_add();
2042 /* NOTREACHED */
2046 argc -= optind;
2047 argv += optind;
2049 if (argc != 1)
2050 usage_rm();
2052 path = realpath(argv[0], NULL);
2053 if (path == NULL) {
2054 error = got_error_from_errno2("realpath", argv[0]);
2055 goto done;
2057 got_path_strip_trailing_slashes(path);
2059 cwd = getcwd(NULL, 0);
2060 if (cwd == NULL) {
2061 error = got_error_from_errno("getcwd");
2062 goto done;
2064 error = got_worktree_open(&worktree, cwd);
2065 if (error)
2066 goto done;
2068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2069 if (error)
2070 goto done;
2072 error = apply_unveil(got_repo_get_path(repo), 1,
2073 got_worktree_get_root_path(worktree), 0);
2074 if (error)
2075 goto done;
2077 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2078 print_status, NULL, repo);
2079 if (error)
2080 goto done;
2081 done:
2082 if (repo)
2083 got_repo_close(repo);
2084 if (worktree)
2085 got_worktree_close(worktree);
2086 free(path);
2087 free(cwd);
2088 return error;
2091 __dead static void
2092 usage_revert(void)
2094 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2095 exit(1);
2098 static void
2099 revert_progress(void *arg, unsigned char status, const char *path)
2101 while (path[0] == '/')
2102 path++;
2103 printf("%c %s\n", status, path);
2106 static const struct got_error *
2107 cmd_revert(int argc, char *argv[])
2109 const struct got_error *error = NULL;
2110 struct got_worktree *worktree = NULL;
2111 struct got_repository *repo = NULL;
2112 char *cwd = NULL, *path = NULL;
2113 int ch;
2115 while ((ch = getopt(argc, argv, "")) != -1) {
2116 switch (ch) {
2117 default:
2118 usage_revert();
2119 /* NOTREACHED */
2123 argc -= optind;
2124 argv += optind;
2126 if (argc != 1)
2127 usage_revert();
2129 path = realpath(argv[0], NULL);
2130 if (path == NULL) {
2131 error = got_error_from_errno2("realpath", argv[0]);
2132 goto done;
2134 got_path_strip_trailing_slashes(path);
2136 cwd = getcwd(NULL, 0);
2137 if (cwd == NULL) {
2138 error = got_error_from_errno("getcwd");
2139 goto done;
2141 error = got_worktree_open(&worktree, cwd);
2142 if (error)
2143 goto done;
2145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2146 if (error != NULL)
2147 goto done;
2149 error = apply_unveil(got_repo_get_path(repo), 1,
2150 got_worktree_get_root_path(worktree), 0);
2151 if (error)
2152 goto done;
2154 error = got_worktree_revert(worktree, path,
2155 revert_progress, NULL, repo);
2156 if (error)
2157 goto done;
2158 done:
2159 if (repo)
2160 got_repo_close(repo);
2161 if (worktree)
2162 got_worktree_close(worktree);
2163 free(path);
2164 free(cwd);
2165 return error;
2168 __dead static void
2169 usage_commit(void)
2171 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2172 exit(1);
2175 int
2176 spawn_editor(const char *editor, const char *file)
2178 pid_t pid;
2179 sig_t sighup, sigint, sigquit;
2180 int st = -1;
2182 sighup = signal(SIGHUP, SIG_IGN);
2183 sigint = signal(SIGINT, SIG_IGN);
2184 sigquit = signal(SIGQUIT, SIG_IGN);
2186 switch (pid = fork()) {
2187 case -1:
2188 goto doneediting;
2189 case 0:
2190 execl(editor, editor, file, (char *)NULL);
2191 _exit(127);
2194 while (waitpid(pid, &st, 0) == -1)
2195 if (errno != EINTR)
2196 break;
2198 doneediting:
2199 (void)signal(SIGHUP, sighup);
2200 (void)signal(SIGINT, sigint);
2201 (void)signal(SIGQUIT, sigquit);
2203 if (!WIFEXITED(st)) {
2204 errno = EINTR;
2205 return -1;
2208 return WEXITSTATUS(st);
2211 struct collect_commit_logmsg_arg {
2212 const char *cmdline_log;
2213 const char *editor;
2214 const char *worktree_path;
2215 const char *repo_path;
2216 char *logmsg_path;
2220 static const struct got_error *
2221 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2222 void *arg)
2224 const char *initial_content = "\n# changes to be committed:\n";
2225 struct got_pathlist_entry *pe;
2226 const struct got_error *err = NULL;
2227 char *template = NULL;
2228 struct collect_commit_logmsg_arg *a = arg;
2229 char buf[1024];
2230 struct stat st, st2;
2231 FILE *fp;
2232 size_t len;
2233 int fd, content_changed = 0;
2235 /* if a message was specified on the command line, just use it */
2236 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2237 len = strlen(a->cmdline_log) + 1;
2238 *logmsg = malloc(len + 1);
2239 if (*logmsg == NULL)
2240 return got_error_from_errno("malloc");
2241 strlcpy(*logmsg, a->cmdline_log, len);
2242 return NULL;
2245 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2246 return got_error_from_errno("asprintf");
2248 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2249 if (err)
2250 goto done;
2252 dprintf(fd, initial_content);
2254 TAILQ_FOREACH(pe, commitable_paths, entry) {
2255 struct got_commitable *ct = pe->data;
2256 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2258 close(fd);
2260 if (stat(a->logmsg_path, &st) == -1) {
2261 err = got_error_from_errno2("stat", a->logmsg_path);
2262 goto done;
2265 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2266 err = got_error_from_errno("failed spawning editor");
2267 goto done;
2270 if (stat(a->logmsg_path, &st2) == -1) {
2271 err = got_error_from_errno("stat");
2272 goto done;
2275 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2276 unlink(a->logmsg_path);
2277 free(a->logmsg_path);
2278 a->logmsg_path = NULL;
2279 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2280 "no changes made to commit message, aborting");
2281 goto done;
2284 /* remove comments */
2285 *logmsg = malloc(st2.st_size + 1);
2286 if (*logmsg == NULL) {
2287 err = got_error_from_errno("malloc");
2288 goto done;
2290 len = 0;
2292 fp = fopen(a->logmsg_path, "r");
2293 while (fgets(buf, sizeof(buf), fp) != NULL) {
2294 if (!content_changed && strcmp(buf, initial_content) != 0)
2295 content_changed = 1;
2296 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2297 continue;
2298 len = strlcat(*logmsg, buf, st2.st_size);
2300 fclose(fp);
2302 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2303 (*logmsg)[len - 1] = '\0';
2304 len--;
2307 if (len == 0 || !content_changed) {
2308 unlink(a->logmsg_path);
2309 free(a->logmsg_path);
2310 a->logmsg_path = NULL;
2311 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2312 "commit message cannot be empty, aborting");
2313 goto done;
2315 done:
2316 free(template);
2318 /* Editor is done; we can now apply unveil(2) */
2319 if (err == NULL)
2320 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2321 return err;
2324 static const struct got_error *
2325 cmd_commit(int argc, char *argv[])
2327 const struct got_error *error = NULL;
2328 struct got_worktree *worktree = NULL;
2329 struct got_repository *repo = NULL;
2330 char *cwd = NULL, *path = NULL, *id_str = NULL;
2331 struct got_object_id *id = NULL;
2332 const char *logmsg = NULL;
2333 const char *got_author = getenv("GOT_AUTHOR");
2334 struct collect_commit_logmsg_arg cl_arg;
2335 char *editor = NULL;
2336 int ch;
2338 while ((ch = getopt(argc, argv, "m:")) != -1) {
2339 switch (ch) {
2340 case 'm':
2341 logmsg = optarg;
2342 break;
2343 default:
2344 usage_commit();
2345 /* NOTREACHED */
2349 argc -= optind;
2350 argv += optind;
2352 if (argc == 1) {
2353 path = realpath(argv[0], NULL);
2354 if (path == NULL) {
2355 error = got_error_from_errno2("realpath", argv[0]);
2356 goto done;
2358 got_path_strip_trailing_slashes(path);
2359 } else if (argc != 0)
2360 usage_commit();
2362 if (got_author == NULL) {
2363 /* TODO: Look current user up in password database */
2364 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2365 goto done;
2368 cwd = getcwd(NULL, 0);
2369 if (cwd == NULL) {
2370 error = got_error_from_errno("getcwd");
2371 goto done;
2373 error = got_worktree_open(&worktree, cwd);
2374 if (error)
2375 goto done;
2377 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2378 if (error != NULL)
2379 goto done;
2382 * unveil(2) traverses exec(2); if an editor is used we have
2383 * to apply unveil after the log message has been written.
2385 if (logmsg == NULL || strlen(logmsg) == 0)
2386 error = get_editor(&editor);
2387 else
2388 error = apply_unveil(got_repo_get_path(repo), 0,
2389 got_worktree_get_root_path(worktree), 0);
2390 if (error)
2391 goto done;
2393 cl_arg.editor = editor;
2394 cl_arg.cmdline_log = logmsg;
2395 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2396 cl_arg.repo_path = got_repo_get_path(repo);
2397 cl_arg.logmsg_path = NULL;
2398 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2399 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2400 if (error) {
2401 if (cl_arg.logmsg_path)
2402 fprintf(stderr, "%s: log message preserved in %s\n",
2403 getprogname(), cl_arg.logmsg_path);
2404 goto done;
2407 if (cl_arg.logmsg_path)
2408 unlink(cl_arg.logmsg_path);
2410 error = got_object_id_str(&id_str, id);
2411 if (error)
2412 goto done;
2413 printf("created commit %s\n", id_str);
2414 done:
2415 if (repo)
2416 got_repo_close(repo);
2417 if (worktree)
2418 got_worktree_close(worktree);
2419 free(path);
2420 free(cwd);
2421 free(id_str);
2422 free(editor);
2423 return error;