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 [-p prefix] repository-path "
275 "[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 char *commit_id_str = NULL;
352 int ch, same_path_prefix;
354 while ((ch = getopt(argc, argv, "c:p:")) != -1) {
355 switch (ch) {
356 case 'c':
357 commit_id_str = strdup(optarg);
358 if (commit_id_str == NULL)
359 return got_error_from_errno("strdup");
360 break;
361 case 'p':
362 path_prefix = optarg;
363 break;
364 default:
365 usage_checkout();
366 /* NOTREACHED */
370 argc -= optind;
371 argv += optind;
373 #ifndef PROFILE
374 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
375 "unveil", NULL) == -1)
376 err(1, "pledge");
377 #endif
378 if (argc == 1) {
379 char *cwd, *base, *dotgit;
380 repo_path = realpath(argv[0], NULL);
381 if (repo_path == NULL)
382 return got_error_from_errno2("realpath", argv[0]);
383 cwd = getcwd(NULL, 0);
384 if (cwd == NULL) {
385 error = got_error_from_errno("getcwd");
386 goto done;
388 if (path_prefix[0]) {
389 base = basename(path_prefix);
390 if (base == NULL) {
391 error = got_error_from_errno2("basename",
392 path_prefix);
393 goto done;
395 } else {
396 base = basename(repo_path);
397 if (base == NULL) {
398 error = got_error_from_errno2("basename",
399 repo_path);
400 goto done;
403 dotgit = strstr(base, ".git");
404 if (dotgit)
405 *dotgit = '\0';
406 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
407 error = got_error_from_errno("asprintf");
408 free(cwd);
409 goto done;
411 free(cwd);
412 } else if (argc == 2) {
413 repo_path = realpath(argv[0], NULL);
414 if (repo_path == NULL) {
415 error = got_error_from_errno2("realpath", argv[0]);
416 goto done;
418 worktree_path = realpath(argv[1], NULL);
419 if (worktree_path == NULL) {
420 error = got_error_from_errno2("realpath", argv[1]);
421 goto done;
423 } else
424 usage_checkout();
426 got_path_strip_trailing_slashes(repo_path);
427 got_path_strip_trailing_slashes(worktree_path);
429 error = got_repo_open(&repo, repo_path);
430 if (error != NULL)
431 goto done;
433 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
434 if (error)
435 goto done;
437 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
438 if (error != NULL)
439 goto done;
441 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
442 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
443 goto done;
445 error = got_worktree_open(&worktree, worktree_path);
446 if (error != NULL)
447 goto done;
449 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
450 path_prefix);
451 if (error != NULL)
452 goto done;
453 if (!same_path_prefix) {
454 error = got_error(GOT_ERR_PATH_PREFIX);
455 goto done;
458 if (commit_id_str) {
459 struct got_object_id *commit_id;
460 error = got_object_resolve_id_str(&commit_id, repo,
461 commit_id_str);
462 if (error != NULL)
463 goto done;
464 error = check_linear_ancestry(worktree, commit_id, repo);
465 if (error != NULL) {
466 free(commit_id);
467 goto done;
469 error = got_worktree_set_base_commit_id(worktree, repo,
470 commit_id);
471 free(commit_id);
472 if (error)
473 goto done;
476 error = got_worktree_checkout_files(worktree, "", repo,
477 checkout_progress, worktree_path, check_cancelled, NULL);
478 if (error != NULL)
479 goto done;
481 printf("Now shut up and hack\n");
483 done:
484 free(commit_id_str);
485 free(repo_path);
486 free(worktree_path);
487 return error;
490 __dead static void
491 usage_update(void)
493 fprintf(stderr, "usage: %s update [-c commit] [path]\n",
494 getprogname());
495 exit(1);
498 static void
499 update_progress(void *arg, unsigned char status, const char *path)
501 int *did_something = arg;
503 if (status == GOT_STATUS_EXISTS)
504 return;
506 *did_something = 1;
507 while (path[0] == '/')
508 path++;
509 printf("%c %s\n", status, path);
512 static const struct got_error *
513 cmd_update(int argc, char *argv[])
515 const struct got_error *error = NULL;
516 struct got_repository *repo = NULL;
517 struct got_worktree *worktree = NULL;
518 char *worktree_path = NULL, *path = NULL;
519 struct got_object_id *commit_id = NULL;
520 char *commit_id_str = NULL;
521 int ch, did_something = 0;
523 while ((ch = getopt(argc, argv, "c:")) != -1) {
524 switch (ch) {
525 case 'c':
526 commit_id_str = strdup(optarg);
527 if (commit_id_str == NULL)
528 return got_error_from_errno("strdup");
529 break;
530 default:
531 usage_update();
532 /* NOTREACHED */
536 argc -= optind;
537 argv += optind;
539 #ifndef PROFILE
540 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
541 "unveil", NULL) == -1)
542 err(1, "pledge");
543 #endif
544 worktree_path = getcwd(NULL, 0);
545 if (worktree_path == NULL) {
546 error = got_error_from_errno("getcwd");
547 goto done;
549 error = got_worktree_open(&worktree, worktree_path);
550 if (error)
551 goto done;
553 if (argc == 0) {
554 path = strdup("");
555 if (path == NULL) {
556 error = got_error_from_errno("strdup");
557 goto done;
559 } else if (argc == 1) {
560 error = got_worktree_resolve_path(&path, worktree, argv[0]);
561 if (error)
562 goto done;
563 } else
564 usage_update();
566 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
567 if (error != NULL)
568 goto done;
570 error = apply_unveil(got_repo_get_path(repo), 0,
571 got_worktree_get_root_path(worktree), 0);
572 if (error)
573 goto done;
575 if (commit_id_str == NULL) {
576 struct got_reference *head_ref;
577 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
578 if (error != NULL)
579 goto done;
580 error = got_ref_resolve(&commit_id, repo, head_ref);
581 if (error != NULL)
582 goto done;
583 error = got_object_id_str(&commit_id_str, commit_id);
584 if (error != NULL)
585 goto done;
586 } else {
587 error = got_object_resolve_id_str(&commit_id, repo,
588 commit_id_str);
589 if (error != NULL)
590 goto done;
593 error = check_linear_ancestry(worktree, commit_id, repo);
594 if (error != NULL)
595 goto done;
597 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
598 commit_id) != 0) {
599 error = got_worktree_set_base_commit_id(worktree, repo,
600 commit_id);
601 if (error)
602 goto done;
605 error = got_worktree_checkout_files(worktree, path, repo,
606 update_progress, &did_something, check_cancelled, NULL);
607 if (error != NULL)
608 goto done;
610 if (did_something)
611 printf("Updated to commit %s\n", commit_id_str);
612 else
613 printf("Already up-to-date\n");
614 done:
615 free(worktree_path);
616 free(path);
617 free(commit_id);
618 free(commit_id_str);
619 return error;
622 static const struct got_error *
623 print_patch(struct got_commit_object *commit, struct got_object_id *id,
624 int diff_context, struct got_repository *repo)
626 const struct got_error *err = NULL;
627 struct got_tree_object *tree1 = NULL, *tree2;
628 struct got_object_qid *qid;
629 char *id_str1 = NULL, *id_str2;
631 err = got_object_open_as_tree(&tree2, repo,
632 got_object_commit_get_tree_id(commit));
633 if (err)
634 return err;
636 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
637 if (qid != NULL) {
638 struct got_commit_object *pcommit;
640 err = got_object_open_as_commit(&pcommit, repo, qid->id);
641 if (err)
642 return err;
644 err = got_object_open_as_tree(&tree1, repo,
645 got_object_commit_get_tree_id(pcommit));
646 got_object_commit_close(pcommit);
647 if (err)
648 return err;
650 err = got_object_id_str(&id_str1, qid->id);
651 if (err)
652 return err;
655 err = got_object_id_str(&id_str2, id);
656 if (err)
657 goto done;
659 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
660 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
661 done:
662 if (tree1)
663 got_object_tree_close(tree1);
664 got_object_tree_close(tree2);
665 free(id_str1);
666 free(id_str2);
667 return err;
670 static char *
671 get_datestr(time_t *time, char *datebuf)
673 char *p, *s = ctime_r(time, datebuf);
674 p = strchr(s, '\n');
675 if (p)
676 *p = '\0';
677 return s;
680 static const struct got_error *
681 print_commit(struct got_commit_object *commit, struct got_object_id *id,
682 struct got_repository *repo, int show_patch, int diff_context,
683 struct got_reflist_head *refs)
685 const struct got_error *err = NULL;
686 char *id_str, *datestr, *logmsg0, *logmsg, *line;
687 char datebuf[26];
688 time_t committer_time;
689 const char *author, *committer;
690 char *refs_str = NULL;
691 struct got_reflist_entry *re;
693 SIMPLEQ_FOREACH(re, refs, entry) {
694 char *s;
695 const char *name;
696 if (got_object_id_cmp(re->id, id) != 0)
697 continue;
698 name = got_ref_get_name(re->ref);
699 if (strcmp(name, GOT_REF_HEAD) == 0)
700 continue;
701 if (strncmp(name, "refs/", 5) == 0)
702 name += 5;
703 if (strncmp(name, "got/", 4) == 0)
704 continue;
705 if (strncmp(name, "heads/", 6) == 0)
706 name += 6;
707 if (strncmp(name, "remotes/", 8) == 0)
708 name += 8;
709 s = refs_str;
710 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
711 name) == -1) {
712 err = got_error_from_errno("asprintf");
713 free(s);
714 break;
716 free(s);
718 err = got_object_id_str(&id_str, id);
719 if (err)
720 return err;
722 printf("-----------------------------------------------\n");
723 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
724 refs_str ? refs_str : "", refs_str ? ")" : "");
725 free(id_str);
726 id_str = NULL;
727 free(refs_str);
728 refs_str = NULL;
729 printf("from: %s\n", got_object_commit_get_author(commit));
730 committer_time = got_object_commit_get_committer_time(commit);
731 datestr = get_datestr(&committer_time, datebuf);
732 printf("date: %s UTC\n", datestr);
733 author = got_object_commit_get_author(commit);
734 committer = got_object_commit_get_committer(commit);
735 if (strcmp(author, committer) != 0)
736 printf("via: %s\n", committer);
737 if (got_object_commit_get_nparents(commit) > 1) {
738 const struct got_object_id_queue *parent_ids;
739 struct got_object_qid *qid;
740 int n = 1;
741 parent_ids = got_object_commit_get_parent_ids(commit);
742 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
743 err = got_object_id_str(&id_str, qid->id);
744 if (err)
745 return err;
746 printf("parent %d: %s\n", n++, id_str);
747 free(id_str);
751 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
752 if (logmsg0 == NULL)
753 return got_error_from_errno("strdup");
755 logmsg = logmsg0;
756 do {
757 line = strsep(&logmsg, "\n");
758 if (line)
759 printf(" %s\n", line);
760 } while (line);
761 free(logmsg0);
763 if (show_patch) {
764 err = print_patch(commit, id, diff_context, repo);
765 if (err == 0)
766 printf("\n");
769 if (fflush(stdout) != 0 && err == NULL)
770 err = got_error_from_errno("fflush");
771 return err;
774 static const struct got_error *
775 print_commits(struct got_object_id *root_id, struct got_repository *repo,
776 char *path, int show_patch, int diff_context, int limit,
777 int first_parent_traversal, struct got_reflist_head *refs)
779 const struct got_error *err;
780 struct got_commit_graph *graph;
782 err = got_commit_graph_open(&graph, root_id, path,
783 first_parent_traversal, repo);
784 if (err)
785 return err;
786 err = got_commit_graph_iter_start(graph, root_id, repo);
787 if (err)
788 goto done;
789 for (;;) {
790 struct got_commit_object *commit;
791 struct got_object_id *id;
793 if (sigint_received || sigpipe_received)
794 break;
796 err = got_commit_graph_iter_next(&id, graph);
797 if (err) {
798 if (err->code == GOT_ERR_ITER_COMPLETED) {
799 err = NULL;
800 break;
802 if (err->code != GOT_ERR_ITER_NEED_MORE)
803 break;
804 err = got_commit_graph_fetch_commits(graph, 1, repo);
805 if (err)
806 break;
807 else
808 continue;
810 if (id == NULL)
811 break;
813 err = got_object_open_as_commit(&commit, repo, id);
814 if (err)
815 break;
816 err = print_commit(commit, id, repo, show_patch, diff_context,
817 refs);
818 got_object_commit_close(commit);
819 if (err || (limit && --limit == 0))
820 break;
822 done:
823 got_commit_graph_close(graph);
824 return err;
827 __dead static void
828 usage_log(void)
830 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
831 "[-r repository-path] [path]\n", getprogname());
832 exit(1);
835 static const struct got_error *
836 cmd_log(int argc, char *argv[])
838 const struct got_error *error;
839 struct got_repository *repo = NULL;
840 struct got_worktree *worktree = NULL;
841 struct got_commit_object *commit = NULL;
842 struct got_object_id *id = NULL;
843 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
844 char *start_commit = NULL;
845 int diff_context = 3, ch;
846 int show_patch = 0, limit = 0, first_parent_traversal = 0;
847 const char *errstr;
848 struct got_reflist_head refs;
850 SIMPLEQ_INIT(&refs);
852 #ifndef PROFILE
853 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
854 NULL)
855 == -1)
856 err(1, "pledge");
857 #endif
859 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
860 switch (ch) {
861 case 'p':
862 show_patch = 1;
863 break;
864 case 'c':
865 start_commit = optarg;
866 break;
867 case 'C':
868 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
869 &errstr);
870 if (errstr != NULL)
871 err(1, "-C option %s", errstr);
872 break;
873 case 'l':
874 limit = strtonum(optarg, 1, INT_MAX, &errstr);
875 if (errstr != NULL)
876 err(1, "-l option %s", errstr);
877 break;
878 case 'f':
879 first_parent_traversal = 1;
880 break;
881 case 'r':
882 repo_path = realpath(optarg, NULL);
883 if (repo_path == NULL)
884 err(1, "-r option");
885 got_path_strip_trailing_slashes(repo_path);
886 break;
887 default:
888 usage_log();
889 /* NOTREACHED */
893 argc -= optind;
894 argv += optind;
896 cwd = getcwd(NULL, 0);
897 if (cwd == NULL) {
898 error = got_error_from_errno("getcwd");
899 goto done;
902 error = got_worktree_open(&worktree, cwd);
903 if (error && error->code != GOT_ERR_NOT_WORKTREE)
904 goto done;
905 error = NULL;
907 if (argc == 0) {
908 path = strdup("");
909 if (path == NULL) {
910 error = got_error_from_errno("strdup");
911 goto done;
913 } else if (argc == 1) {
914 if (worktree) {
915 error = got_worktree_resolve_path(&path, worktree,
916 argv[0]);
917 if (error)
918 goto done;
919 } else {
920 path = strdup(argv[0]);
921 if (path == NULL) {
922 error = got_error_from_errno("strdup");
923 goto done;
926 } else
927 usage_log();
929 if (repo_path == NULL) {
930 repo_path = worktree ?
931 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
933 if (repo_path == NULL) {
934 error = got_error_from_errno("strdup");
935 goto done;
938 error = got_repo_open(&repo, repo_path);
939 if (error != NULL)
940 goto done;
942 error = apply_unveil(got_repo_get_path(repo), 1,
943 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
944 if (error)
945 goto done;
947 if (start_commit == NULL) {
948 struct got_reference *head_ref;
949 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
950 if (error != NULL)
951 return error;
952 error = got_ref_resolve(&id, repo, head_ref);
953 got_ref_close(head_ref);
954 if (error != NULL)
955 return error;
956 error = got_object_open_as_commit(&commit, repo, id);
957 } else {
958 struct got_reference *ref;
959 error = got_ref_open(&ref, repo, start_commit, 0);
960 if (error == NULL) {
961 int obj_type;
962 error = got_ref_resolve(&id, repo, ref);
963 got_ref_close(ref);
964 if (error != NULL)
965 goto done;
966 error = got_object_get_type(&obj_type, repo, id);
967 if (error != NULL)
968 goto done;
969 if (obj_type == GOT_OBJ_TYPE_TAG) {
970 struct got_tag_object *tag;
971 error = got_object_open_as_tag(&tag, repo, id);
972 if (error != NULL)
973 goto done;
974 if (got_object_tag_get_object_type(tag) !=
975 GOT_OBJ_TYPE_COMMIT) {
976 got_object_tag_close(tag);
977 error = got_error(GOT_ERR_OBJ_TYPE);
978 goto done;
980 free(id);
981 id = got_object_id_dup(
982 got_object_tag_get_object_id(tag));
983 if (id == NULL)
984 error = got_error_from_errno(
985 "got_object_id_dup");
986 got_object_tag_close(tag);
987 if (error)
988 goto done;
989 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
990 error = got_error(GOT_ERR_OBJ_TYPE);
991 goto done;
993 error = got_object_open_as_commit(&commit, repo, id);
994 if (error != NULL)
995 goto done;
997 if (commit == NULL) {
998 error = got_object_resolve_id_str(&id, repo,
999 start_commit);
1000 if (error != NULL)
1001 return error;
1004 if (error != NULL)
1005 goto done;
1007 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1008 if (error != NULL)
1009 goto done;
1010 if (in_repo_path) {
1011 free(path);
1012 path = in_repo_path;
1015 error = got_ref_list(&refs, repo);
1016 if (error)
1017 goto done;
1019 error = print_commits(id, repo, path, show_patch,
1020 diff_context, limit, first_parent_traversal, &refs);
1021 done:
1022 free(path);
1023 free(repo_path);
1024 free(cwd);
1025 free(id);
1026 if (worktree)
1027 got_worktree_close(worktree);
1028 if (repo) {
1029 const struct got_error *repo_error;
1030 repo_error = got_repo_close(repo);
1031 if (error == NULL)
1032 error = repo_error;
1034 got_ref_list_free(&refs);
1035 return error;
1038 __dead static void
1039 usage_diff(void)
1041 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1042 "[object1 object2 | path]\n", getprogname());
1043 exit(1);
1046 struct print_diff_arg {
1047 struct got_repository *repo;
1048 struct got_worktree *worktree;
1049 int diff_context;
1050 const char *id_str;
1051 int header_shown;
1054 static const struct got_error *
1055 print_diff(void *arg, unsigned char status, const char *path,
1056 struct got_object_id *blob_id, struct got_object_id *commit_id)
1058 struct print_diff_arg *a = arg;
1059 const struct got_error *err = NULL;
1060 struct got_blob_object *blob1 = NULL;
1061 FILE *f2 = NULL;
1062 char *abspath = NULL;
1063 struct stat sb;
1065 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1066 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1067 return NULL;
1069 if (!a->header_shown) {
1070 printf("diff %s %s\n", a->id_str,
1071 got_worktree_get_root_path(a->worktree));
1072 a->header_shown = 1;
1075 if (status != GOT_STATUS_ADD) {
1076 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1077 if (err)
1078 goto done;
1082 if (status != GOT_STATUS_DELETE) {
1083 if (asprintf(&abspath, "%s/%s",
1084 got_worktree_get_root_path(a->worktree), path) == -1) {
1085 err = got_error_from_errno("asprintf");
1086 goto done;
1089 f2 = fopen(abspath, "r");
1090 if (f2 == NULL) {
1091 err = got_error_from_errno2("fopen", abspath);
1092 goto done;
1094 if (lstat(abspath, &sb) == -1) {
1095 err = got_error_from_errno2("lstat", abspath);
1096 goto done;
1098 } else
1099 sb.st_size = 0;
1101 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1102 stdout);
1103 done:
1104 if (blob1)
1105 got_object_blob_close(blob1);
1106 if (f2 && fclose(f2) != 0 && err == NULL)
1107 err = got_error_from_errno("fclose");
1108 free(abspath);
1109 return err;
1112 static const struct got_error *
1113 cmd_diff(int argc, char *argv[])
1115 const struct got_error *error;
1116 struct got_repository *repo = NULL;
1117 struct got_worktree *worktree = NULL;
1118 char *cwd = NULL, *repo_path = NULL;
1119 struct got_object_id *id1 = NULL, *id2 = NULL;
1120 char *id_str1 = NULL, *id_str2 = NULL;
1121 int type1, type2;
1122 int diff_context = 3, ch;
1123 const char *errstr;
1124 char *path = NULL;
1126 #ifndef PROFILE
1127 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1128 NULL) == -1)
1129 err(1, "pledge");
1130 #endif
1132 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1133 switch (ch) {
1134 case 'C':
1135 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1136 if (errstr != NULL)
1137 err(1, "-C option %s", errstr);
1138 break;
1139 case 'r':
1140 repo_path = realpath(optarg, NULL);
1141 if (repo_path == NULL)
1142 err(1, "-r option");
1143 got_path_strip_trailing_slashes(repo_path);
1144 break;
1145 default:
1146 usage_diff();
1147 /* NOTREACHED */
1151 argc -= optind;
1152 argv += optind;
1154 cwd = getcwd(NULL, 0);
1155 if (cwd == NULL) {
1156 error = got_error_from_errno("getcwd");
1157 goto done;
1159 error = got_worktree_open(&worktree, cwd);
1160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1161 goto done;
1162 if (argc <= 1) {
1163 if (worktree == NULL) {
1164 error = got_error(GOT_ERR_NOT_WORKTREE);
1165 goto done;
1167 if (repo_path)
1168 errx(1,
1169 "-r option can't be used when diffing a work tree");
1170 repo_path = strdup(got_worktree_get_repo_path(worktree));
1171 if (repo_path == NULL) {
1172 error = got_error_from_errno("strdup");
1173 goto done;
1175 if (argc == 1) {
1176 error = got_worktree_resolve_path(&path, worktree,
1177 argv[0]);
1178 if (error)
1179 goto done;
1180 } else {
1181 path = strdup("");
1182 if (path == NULL) {
1183 error = got_error_from_errno("strdup");
1184 goto done;
1187 } else if (argc == 2) {
1188 id_str1 = argv[0];
1189 id_str2 = argv[1];
1190 } else
1191 usage_diff();
1193 if (repo_path == NULL) {
1194 repo_path = getcwd(NULL, 0);
1195 if (repo_path == NULL)
1196 return got_error_from_errno("getcwd");
1199 error = got_repo_open(&repo, repo_path);
1200 free(repo_path);
1201 if (error != NULL)
1202 goto done;
1204 error = apply_unveil(got_repo_get_path(repo), 1,
1205 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1206 if (error)
1207 goto done;
1209 if (worktree) {
1210 struct print_diff_arg arg;
1211 char *id_str;
1212 error = got_object_id_str(&id_str,
1213 got_worktree_get_base_commit_id(worktree));
1214 if (error)
1215 goto done;
1216 arg.repo = repo;
1217 arg.worktree = worktree;
1218 arg.diff_context = diff_context;
1219 arg.id_str = id_str;
1220 arg.header_shown = 0;
1222 error = got_worktree_status(worktree, path, repo, print_diff,
1223 &arg, check_cancelled, NULL);
1224 free(id_str);
1225 goto done;
1228 error = got_object_resolve_id_str(&id1, repo, id_str1);
1229 if (error)
1230 goto done;
1232 error = got_object_resolve_id_str(&id2, repo, id_str2);
1233 if (error)
1234 goto done;
1236 error = got_object_get_type(&type1, repo, id1);
1237 if (error)
1238 goto done;
1240 error = got_object_get_type(&type2, repo, id2);
1241 if (error)
1242 goto done;
1244 if (type1 != type2) {
1245 error = got_error(GOT_ERR_OBJ_TYPE);
1246 goto done;
1249 switch (type1) {
1250 case GOT_OBJ_TYPE_BLOB:
1251 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1252 diff_context, repo, stdout);
1253 break;
1254 case GOT_OBJ_TYPE_TREE:
1255 error = got_diff_objects_as_trees(id1, id2, "", "",
1256 diff_context, repo, stdout);
1257 break;
1258 case GOT_OBJ_TYPE_COMMIT:
1259 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1260 id_str2);
1261 error = got_diff_objects_as_commits(id1, id2, diff_context,
1262 repo, stdout);
1263 break;
1264 default:
1265 error = got_error(GOT_ERR_OBJ_TYPE);
1268 done:
1269 free(id1);
1270 free(id2);
1271 free(path);
1272 if (worktree)
1273 got_worktree_close(worktree);
1274 if (repo) {
1275 const struct got_error *repo_error;
1276 repo_error = got_repo_close(repo);
1277 if (error == NULL)
1278 error = repo_error;
1280 return error;
1283 __dead static void
1284 usage_blame(void)
1286 fprintf(stderr,
1287 "usage: %s blame [-c commit] [-r repository-path] path\n",
1288 getprogname());
1289 exit(1);
1292 static const struct got_error *
1293 cmd_blame(int argc, char *argv[])
1295 const struct got_error *error;
1296 struct got_repository *repo = NULL;
1297 struct got_worktree *worktree = NULL;
1298 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1299 struct got_object_id *commit_id = NULL;
1300 char *commit_id_str = NULL;
1301 int ch;
1303 #ifndef PROFILE
1304 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1305 NULL) == -1)
1306 err(1, "pledge");
1307 #endif
1309 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1310 switch (ch) {
1311 case 'c':
1312 commit_id_str = optarg;
1313 break;
1314 case 'r':
1315 repo_path = realpath(optarg, NULL);
1316 if (repo_path == NULL)
1317 err(1, "-r option");
1318 got_path_strip_trailing_slashes(repo_path);
1319 break;
1320 default:
1321 usage_blame();
1322 /* NOTREACHED */
1326 argc -= optind;
1327 argv += optind;
1329 if (argc == 1)
1330 path = argv[0];
1331 else
1332 usage_blame();
1334 cwd = getcwd(NULL, 0);
1335 if (cwd == NULL) {
1336 error = got_error_from_errno("getcwd");
1337 goto done;
1339 if (repo_path == NULL) {
1340 error = got_worktree_open(&worktree, cwd);
1341 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1342 goto done;
1343 else
1344 error = NULL;
1345 if (worktree) {
1346 repo_path =
1347 strdup(got_worktree_get_repo_path(worktree));
1348 if (repo_path == NULL)
1349 error = got_error_from_errno("strdup");
1350 if (error)
1351 goto done;
1352 } else {
1353 repo_path = strdup(cwd);
1354 if (repo_path == NULL) {
1355 error = got_error_from_errno("strdup");
1356 goto done;
1361 error = got_repo_open(&repo, repo_path);
1362 if (error != NULL)
1363 goto done;
1365 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1366 if (error)
1367 goto done;
1369 if (worktree) {
1370 const char *prefix = got_worktree_get_path_prefix(worktree);
1371 char *p, *worktree_subdir = cwd +
1372 strlen(got_worktree_get_root_path(worktree));
1373 if (asprintf(&p, "%s%s%s%s%s",
1374 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1375 worktree_subdir, worktree_subdir[0] ? "/" : "",
1376 path) == -1) {
1377 error = got_error_from_errno("asprintf");
1378 goto done;
1380 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1381 free(p);
1382 } else {
1383 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1385 if (error)
1386 goto done;
1388 if (commit_id_str == NULL) {
1389 struct got_reference *head_ref;
1390 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1391 if (error != NULL)
1392 goto done;
1393 error = got_ref_resolve(&commit_id, repo, head_ref);
1394 got_ref_close(head_ref);
1395 if (error != NULL)
1396 goto done;
1397 } else {
1398 error = got_object_resolve_id_str(&commit_id, repo,
1399 commit_id_str);
1400 if (error != NULL)
1401 goto done;
1404 error = got_blame(in_repo_path, commit_id, repo, stdout);
1405 done:
1406 free(in_repo_path);
1407 free(repo_path);
1408 free(cwd);
1409 free(commit_id);
1410 if (worktree)
1411 got_worktree_close(worktree);
1412 if (repo) {
1413 const struct got_error *repo_error;
1414 repo_error = got_repo_close(repo);
1415 if (error == NULL)
1416 error = repo_error;
1418 return error;
1421 __dead static void
1422 usage_tree(void)
1424 fprintf(stderr,
1425 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1426 getprogname());
1427 exit(1);
1430 static void
1431 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1432 const char *root_path)
1434 int is_root_path = (strcmp(path, root_path) == 0);
1436 path += strlen(root_path);
1437 while (path[0] == '/')
1438 path++;
1440 printf("%s%s%s%s%s\n", id ? id : "", path,
1441 is_root_path ? "" : "/", te->name,
1442 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1445 static const struct got_error *
1446 print_tree(const char *path, struct got_object_id *commit_id,
1447 int show_ids, int recurse, const char *root_path,
1448 struct got_repository *repo)
1450 const struct got_error *err = NULL;
1451 struct got_object_id *tree_id = NULL;
1452 struct got_tree_object *tree = NULL;
1453 const struct got_tree_entries *entries;
1454 struct got_tree_entry *te;
1456 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1457 if (err)
1458 goto done;
1460 err = got_object_open_as_tree(&tree, repo, tree_id);
1461 if (err)
1462 goto done;
1463 entries = got_object_tree_get_entries(tree);
1464 te = SIMPLEQ_FIRST(&entries->head);
1465 while (te) {
1466 char *id = NULL;
1468 if (sigint_received || sigpipe_received)
1469 break;
1471 if (show_ids) {
1472 char *id_str;
1473 err = got_object_id_str(&id_str, te->id);
1474 if (err)
1475 goto done;
1476 if (asprintf(&id, "%s ", id_str) == -1) {
1477 err = got_error_from_errno("asprintf");
1478 free(id_str);
1479 goto done;
1481 free(id_str);
1483 print_entry(te, id, path, root_path);
1484 free(id);
1486 if (recurse && S_ISDIR(te->mode)) {
1487 char *child_path;
1488 if (asprintf(&child_path, "%s%s%s", path,
1489 path[0] == '/' && path[1] == '\0' ? "" : "/",
1490 te->name) == -1) {
1491 err = got_error_from_errno("asprintf");
1492 goto done;
1494 err = print_tree(child_path, commit_id, show_ids, 1,
1495 root_path, repo);
1496 free(child_path);
1497 if (err)
1498 goto done;
1501 te = SIMPLEQ_NEXT(te, entry);
1503 done:
1504 if (tree)
1505 got_object_tree_close(tree);
1506 free(tree_id);
1507 return err;
1510 static const struct got_error *
1511 cmd_tree(int argc, char *argv[])
1513 const struct got_error *error;
1514 struct got_repository *repo = NULL;
1515 struct got_worktree *worktree = NULL;
1516 const char *path;
1517 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1518 struct got_object_id *commit_id = NULL;
1519 char *commit_id_str = NULL;
1520 int show_ids = 0, recurse = 0;
1521 int ch;
1523 #ifndef PROFILE
1524 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1525 NULL) == -1)
1526 err(1, "pledge");
1527 #endif
1529 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1530 switch (ch) {
1531 case 'c':
1532 commit_id_str = optarg;
1533 break;
1534 case 'r':
1535 repo_path = realpath(optarg, NULL);
1536 if (repo_path == NULL)
1537 err(1, "-r option");
1538 got_path_strip_trailing_slashes(repo_path);
1539 break;
1540 case 'i':
1541 show_ids = 1;
1542 break;
1543 case 'R':
1544 recurse = 1;
1545 break;
1546 default:
1547 usage_tree();
1548 /* NOTREACHED */
1552 argc -= optind;
1553 argv += optind;
1555 if (argc == 1)
1556 path = argv[0];
1557 else if (argc > 1)
1558 usage_tree();
1559 else
1560 path = NULL;
1562 cwd = getcwd(NULL, 0);
1563 if (cwd == NULL) {
1564 error = got_error_from_errno("getcwd");
1565 goto done;
1567 if (repo_path == NULL) {
1568 error = got_worktree_open(&worktree, cwd);
1569 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1570 goto done;
1571 else
1572 error = NULL;
1573 if (worktree) {
1574 repo_path =
1575 strdup(got_worktree_get_repo_path(worktree));
1576 if (repo_path == NULL)
1577 error = got_error_from_errno("strdup");
1578 if (error)
1579 goto done;
1580 } else {
1581 repo_path = strdup(cwd);
1582 if (repo_path == NULL) {
1583 error = got_error_from_errno("strdup");
1584 goto done;
1589 error = got_repo_open(&repo, repo_path);
1590 if (error != NULL)
1591 goto done;
1593 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1594 if (error)
1595 goto done;
1597 if (path == NULL) {
1598 if (worktree) {
1599 char *p, *worktree_subdir = cwd +
1600 strlen(got_worktree_get_root_path(worktree));
1601 if (asprintf(&p, "%s/%s",
1602 got_worktree_get_path_prefix(worktree),
1603 worktree_subdir) == -1) {
1604 error = got_error_from_errno("asprintf");
1605 goto done;
1607 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1608 free(p);
1609 if (error)
1610 goto done;
1611 } else
1612 path = "/";
1614 if (in_repo_path == NULL) {
1615 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1616 if (error != NULL)
1617 goto done;
1620 if (commit_id_str == NULL) {
1621 struct got_reference *head_ref;
1622 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1623 if (error != NULL)
1624 goto done;
1625 error = got_ref_resolve(&commit_id, repo, head_ref);
1626 got_ref_close(head_ref);
1627 if (error != NULL)
1628 goto done;
1629 } else {
1630 error = got_object_resolve_id_str(&commit_id, repo,
1631 commit_id_str);
1632 if (error != NULL)
1633 goto done;
1636 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1637 in_repo_path, repo);
1638 done:
1639 free(in_repo_path);
1640 free(repo_path);
1641 free(cwd);
1642 free(commit_id);
1643 if (worktree)
1644 got_worktree_close(worktree);
1645 if (repo) {
1646 const struct got_error *repo_error;
1647 repo_error = got_repo_close(repo);
1648 if (error == NULL)
1649 error = repo_error;
1651 return error;
1654 __dead static void
1655 usage_status(void)
1657 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1658 exit(1);
1661 static const struct got_error *
1662 print_status(void *arg, unsigned char status, const char *path,
1663 struct got_object_id *blob_id, struct got_object_id *commit_id)
1665 printf("%c %s\n", status, path);
1666 return NULL;
1669 static const struct got_error *
1670 cmd_status(int argc, char *argv[])
1672 const struct got_error *error = NULL;
1673 struct got_repository *repo = NULL;
1674 struct got_worktree *worktree = NULL;
1675 char *cwd = NULL, *path = NULL;
1676 int ch;
1678 while ((ch = getopt(argc, argv, "")) != -1) {
1679 switch (ch) {
1680 default:
1681 usage_status();
1682 /* NOTREACHED */
1686 argc -= optind;
1687 argv += optind;
1689 #ifndef PROFILE
1690 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1691 NULL) == -1)
1692 err(1, "pledge");
1693 #endif
1694 cwd = getcwd(NULL, 0);
1695 if (cwd == NULL) {
1696 error = got_error_from_errno("getcwd");
1697 goto done;
1700 error = got_worktree_open(&worktree, cwd);
1701 if (error != NULL)
1702 goto done;
1704 if (argc == 0) {
1705 path = strdup("");
1706 if (path == NULL) {
1707 error = got_error_from_errno("strdup");
1708 goto done;
1710 } else if (argc == 1) {
1711 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1712 if (error)
1713 goto done;
1714 } else
1715 usage_status();
1717 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1718 if (error != NULL)
1719 goto done;
1721 error = apply_unveil(got_repo_get_path(repo), 1,
1722 got_worktree_get_root_path(worktree), 0);
1723 if (error)
1724 goto done;
1726 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1727 check_cancelled, NULL);
1728 done:
1729 free(cwd);
1730 free(path);
1731 return error;
1734 __dead static void
1735 usage_ref(void)
1737 fprintf(stderr,
1738 "usage: %s ref [-r repository] -l | -d name | name target\n",
1739 getprogname());
1740 exit(1);
1743 static const struct got_error *
1744 list_refs(struct got_repository *repo)
1746 static const struct got_error *err = NULL;
1747 struct got_reflist_head refs;
1748 struct got_reflist_entry *re;
1750 SIMPLEQ_INIT(&refs);
1751 err = got_ref_list(&refs, repo);
1752 if (err)
1753 return err;
1755 SIMPLEQ_FOREACH(re, &refs, entry) {
1756 char *refstr;
1757 refstr = got_ref_to_str(re->ref);
1758 if (refstr == NULL)
1759 return got_error_from_errno("got_ref_to_str");
1760 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1761 free(refstr);
1764 got_ref_list_free(&refs);
1765 return NULL;
1768 static const struct got_error *
1769 delete_ref(struct got_repository *repo, const char *refname)
1771 const struct got_error *err = NULL;
1772 struct got_reference *ref;
1774 err = got_ref_open(&ref, repo, refname, 0);
1775 if (err)
1776 return err;
1778 err = got_ref_delete(ref, repo);
1779 got_ref_close(ref);
1780 return err;
1783 static const struct got_error *
1784 add_ref(struct got_repository *repo, const char *refname, const char *target)
1786 const struct got_error *err = NULL;
1787 struct got_object_id *id;
1788 struct got_reference *ref = NULL;
1790 err = got_object_resolve_id_str(&id, repo, target);
1791 if (err) {
1792 struct got_reference *target_ref;
1794 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1795 return err;
1796 err = got_ref_open(&target_ref, repo, target, 0);
1797 if (err)
1798 return err;
1799 err = got_ref_resolve(&id, repo, target_ref);
1800 got_ref_close(target_ref);
1801 if (err)
1802 return err;
1805 err = got_ref_alloc(&ref, refname, id);
1806 if (err)
1807 goto done;
1809 err = got_ref_write(ref, repo);
1810 done:
1811 if (ref)
1812 got_ref_close(ref);
1813 free(id);
1814 return err;
1817 static const struct got_error *
1818 cmd_ref(int argc, char *argv[])
1820 const struct got_error *error = NULL;
1821 struct got_repository *repo = NULL;
1822 struct got_worktree *worktree = NULL;
1823 char *cwd = NULL, *repo_path = NULL;
1824 int ch, do_list = 0;
1825 const char *delref = NULL;
1827 /* TODO: Add -s option for adding symbolic references. */
1828 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1829 switch (ch) {
1830 case 'd':
1831 delref = optarg;
1832 break;
1833 case 'r':
1834 repo_path = realpath(optarg, NULL);
1835 if (repo_path == NULL)
1836 err(1, "-r option");
1837 got_path_strip_trailing_slashes(repo_path);
1838 break;
1839 case 'l':
1840 do_list = 1;
1841 break;
1842 default:
1843 usage_ref();
1844 /* NOTREACHED */
1848 if (do_list && delref)
1849 errx(1, "-l and -d options are mutually exclusive\n");
1851 argc -= optind;
1852 argv += optind;
1854 if (do_list || delref) {
1855 if (argc > 0)
1856 usage_ref();
1857 } else if (argc != 2)
1858 usage_ref();
1860 #ifndef PROFILE
1861 if (do_list) {
1862 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1863 NULL) == -1)
1864 err(1, "pledge");
1865 } else {
1866 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1867 "sendfd unveil", NULL) == -1)
1868 err(1, "pledge");
1870 #endif
1871 cwd = getcwd(NULL, 0);
1872 if (cwd == NULL) {
1873 error = got_error_from_errno("getcwd");
1874 goto done;
1877 if (repo_path == NULL) {
1878 error = got_worktree_open(&worktree, cwd);
1879 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1880 goto done;
1881 else
1882 error = NULL;
1883 if (worktree) {
1884 repo_path =
1885 strdup(got_worktree_get_repo_path(worktree));
1886 if (repo_path == NULL)
1887 error = got_error_from_errno("strdup");
1888 if (error)
1889 goto done;
1890 } else {
1891 repo_path = strdup(cwd);
1892 if (repo_path == NULL) {
1893 error = got_error_from_errno("strdup");
1894 goto done;
1899 error = got_repo_open(&repo, repo_path);
1900 if (error != NULL)
1901 goto done;
1903 error = apply_unveil(got_repo_get_path(repo), do_list,
1904 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1905 if (error)
1906 goto done;
1908 if (do_list)
1909 error = list_refs(repo);
1910 else if (delref)
1911 error = delete_ref(repo, delref);
1912 else
1913 error = add_ref(repo, argv[0], argv[1]);
1914 done:
1915 if (repo)
1916 got_repo_close(repo);
1917 if (worktree)
1918 got_worktree_close(worktree);
1919 free(cwd);
1920 free(repo_path);
1921 return error;
1924 __dead static void
1925 usage_add(void)
1927 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1928 exit(1);
1931 static const struct got_error *
1932 cmd_add(int argc, char *argv[])
1934 const struct got_error *error = NULL;
1935 struct got_repository *repo = NULL;
1936 struct got_worktree *worktree = NULL;
1937 char *cwd = NULL;
1938 struct got_pathlist_head paths;
1939 struct got_pathlist_entry *pe;
1940 int ch, x;
1942 TAILQ_INIT(&paths);
1944 while ((ch = getopt(argc, argv, "")) != -1) {
1945 switch (ch) {
1946 default:
1947 usage_add();
1948 /* NOTREACHED */
1952 argc -= optind;
1953 argv += optind;
1955 if (argc < 1)
1956 usage_add();
1958 /* make sure each file exists before doing anything halfway */
1959 for (x = 0; x < argc; x++) {
1960 char *path = realpath(argv[x], NULL);
1961 if (path == NULL) {
1962 error = got_error_from_errno2("realpath", argv[x]);
1963 goto done;
1965 free(path);
1968 cwd = getcwd(NULL, 0);
1969 if (cwd == NULL) {
1970 error = got_error_from_errno("getcwd");
1971 goto done;
1974 error = got_worktree_open(&worktree, cwd);
1975 if (error)
1976 goto done;
1978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1979 if (error != NULL)
1980 goto done;
1982 error = apply_unveil(got_repo_get_path(repo), 1,
1983 got_worktree_get_root_path(worktree), 0);
1984 if (error)
1985 goto done;
1987 for (x = 0; x < argc; x++) {
1988 char *path = realpath(argv[x], NULL);
1989 if (path == NULL) {
1990 error = got_error_from_errno2("realpath", argv[x]);
1991 goto done;
1994 got_path_strip_trailing_slashes(path);
1995 error = got_pathlist_insert(&pe, &paths, path, NULL);
1996 if (error) {
1997 free(path);
1998 goto done;
2001 error = got_worktree_schedule_add(worktree, &paths, print_status,
2002 NULL, repo);
2003 done:
2004 if (repo)
2005 got_repo_close(repo);
2006 if (worktree)
2007 got_worktree_close(worktree);
2008 TAILQ_FOREACH(pe, &paths, entry)
2009 free((char *)pe->path);
2010 got_pathlist_free(&paths);
2011 free(cwd);
2012 return error;
2015 __dead static void
2016 usage_rm(void)
2018 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2019 exit(1);
2022 static const struct got_error *
2023 cmd_rm(int argc, char *argv[])
2025 const struct got_error *error = NULL;
2026 struct got_worktree *worktree = NULL;
2027 struct got_repository *repo = NULL;
2028 char *cwd = NULL, *path = NULL;
2029 int ch, delete_local_mods = 0;
2031 while ((ch = getopt(argc, argv, "f")) != -1) {
2032 switch (ch) {
2033 case 'f':
2034 delete_local_mods = 1;
2035 break;
2036 default:
2037 usage_add();
2038 /* NOTREACHED */
2042 argc -= optind;
2043 argv += optind;
2045 if (argc != 1)
2046 usage_rm();
2048 path = realpath(argv[0], NULL);
2049 if (path == NULL) {
2050 error = got_error_from_errno2("realpath", argv[0]);
2051 goto done;
2053 got_path_strip_trailing_slashes(path);
2055 cwd = getcwd(NULL, 0);
2056 if (cwd == NULL) {
2057 error = got_error_from_errno("getcwd");
2058 goto done;
2060 error = got_worktree_open(&worktree, cwd);
2061 if (error)
2062 goto done;
2064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2065 if (error)
2066 goto done;
2068 error = apply_unveil(got_repo_get_path(repo), 1,
2069 got_worktree_get_root_path(worktree), 0);
2070 if (error)
2071 goto done;
2073 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2074 print_status, NULL, repo);
2075 if (error)
2076 goto done;
2077 done:
2078 if (repo)
2079 got_repo_close(repo);
2080 if (worktree)
2081 got_worktree_close(worktree);
2082 free(path);
2083 free(cwd);
2084 return error;
2087 __dead static void
2088 usage_revert(void)
2090 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2091 exit(1);
2094 static void
2095 revert_progress(void *arg, unsigned char status, const char *path)
2097 while (path[0] == '/')
2098 path++;
2099 printf("%c %s\n", status, path);
2102 static const struct got_error *
2103 cmd_revert(int argc, char *argv[])
2105 const struct got_error *error = NULL;
2106 struct got_worktree *worktree = NULL;
2107 struct got_repository *repo = NULL;
2108 char *cwd = NULL, *path = NULL;
2109 int ch;
2111 while ((ch = getopt(argc, argv, "")) != -1) {
2112 switch (ch) {
2113 default:
2114 usage_revert();
2115 /* NOTREACHED */
2119 argc -= optind;
2120 argv += optind;
2122 if (argc != 1)
2123 usage_revert();
2125 path = realpath(argv[0], NULL);
2126 if (path == NULL) {
2127 error = got_error_from_errno2("realpath", argv[0]);
2128 goto done;
2130 got_path_strip_trailing_slashes(path);
2132 cwd = getcwd(NULL, 0);
2133 if (cwd == NULL) {
2134 error = got_error_from_errno("getcwd");
2135 goto done;
2137 error = got_worktree_open(&worktree, cwd);
2138 if (error)
2139 goto done;
2141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2142 if (error != NULL)
2143 goto done;
2145 error = apply_unveil(got_repo_get_path(repo), 1,
2146 got_worktree_get_root_path(worktree), 0);
2147 if (error)
2148 goto done;
2150 error = got_worktree_revert(worktree, path,
2151 revert_progress, NULL, repo);
2152 if (error)
2153 goto done;
2154 done:
2155 if (repo)
2156 got_repo_close(repo);
2157 if (worktree)
2158 got_worktree_close(worktree);
2159 free(path);
2160 free(cwd);
2161 return error;
2164 __dead static void
2165 usage_commit(void)
2167 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2168 exit(1);
2171 int
2172 spawn_editor(const char *editor, const char *file)
2174 pid_t pid;
2175 sig_t sighup, sigint, sigquit;
2176 int st = -1;
2178 sighup = signal(SIGHUP, SIG_IGN);
2179 sigint = signal(SIGINT, SIG_IGN);
2180 sigquit = signal(SIGQUIT, SIG_IGN);
2182 switch (pid = fork()) {
2183 case -1:
2184 goto doneediting;
2185 case 0:
2186 execl(editor, editor, file, (char *)NULL);
2187 _exit(127);
2190 while (waitpid(pid, &st, 0) == -1)
2191 if (errno != EINTR)
2192 break;
2194 doneediting:
2195 (void)signal(SIGHUP, sighup);
2196 (void)signal(SIGINT, sigint);
2197 (void)signal(SIGQUIT, sigquit);
2199 if (!WIFEXITED(st)) {
2200 errno = EINTR;
2201 return -1;
2204 return WEXITSTATUS(st);
2207 struct collect_commit_logmsg_arg {
2208 const char *cmdline_log;
2209 const char *editor;
2210 const char *worktree_path;
2211 const char *repo_path;
2212 char *logmsg_path;
2216 static const struct got_error *
2217 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2218 void *arg)
2220 const char *initial_content = "\n# changes to be committed:\n";
2221 struct got_pathlist_entry *pe;
2222 const struct got_error *err = NULL;
2223 char *template = NULL;
2224 struct collect_commit_logmsg_arg *a = arg;
2225 char buf[1024];
2226 struct stat st, st2;
2227 FILE *fp;
2228 size_t len;
2229 int fd, content_changed = 0;
2231 /* if a message was specified on the command line, just use it */
2232 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2233 len = strlen(a->cmdline_log) + 1;
2234 *logmsg = malloc(len + 1);
2235 if (*logmsg == NULL)
2236 return got_error_from_errno("malloc");
2237 strlcpy(*logmsg, a->cmdline_log, len);
2238 return NULL;
2241 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2242 return got_error_from_errno("asprintf");
2244 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2245 if (err)
2246 goto done;
2248 dprintf(fd, initial_content);
2250 TAILQ_FOREACH(pe, commitable_paths, entry) {
2251 struct got_commitable *ct = pe->data;
2252 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2254 close(fd);
2256 if (stat(a->logmsg_path, &st) == -1) {
2257 err = got_error_from_errno2("stat", a->logmsg_path);
2258 goto done;
2261 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2262 err = got_error_from_errno("failed spawning editor");
2263 goto done;
2266 if (stat(a->logmsg_path, &st2) == -1) {
2267 err = got_error_from_errno("stat");
2268 goto done;
2271 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2272 unlink(a->logmsg_path);
2273 free(a->logmsg_path);
2274 a->logmsg_path = NULL;
2275 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2276 "no changes made to commit message, aborting");
2277 goto done;
2280 /* remove comments */
2281 *logmsg = malloc(st2.st_size + 1);
2282 if (*logmsg == NULL) {
2283 err = got_error_from_errno("malloc");
2284 goto done;
2286 len = 0;
2288 fp = fopen(a->logmsg_path, "r");
2289 while (fgets(buf, sizeof(buf), fp) != NULL) {
2290 if (!content_changed && strcmp(buf, initial_content) != 0)
2291 content_changed = 1;
2292 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2293 continue;
2294 len = strlcat(*logmsg, buf, st2.st_size);
2296 fclose(fp);
2298 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2299 (*logmsg)[len - 1] = '\0';
2300 len--;
2303 if (len == 0 || !content_changed) {
2304 unlink(a->logmsg_path);
2305 free(a->logmsg_path);
2306 a->logmsg_path = NULL;
2307 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2308 "commit message cannot be empty, aborting");
2309 goto done;
2311 done:
2312 free(template);
2314 /* Editor is done; we can now apply unveil(2) */
2315 if (err == NULL)
2316 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2317 return err;
2320 static const struct got_error *
2321 cmd_commit(int argc, char *argv[])
2323 const struct got_error *error = NULL;
2324 struct got_worktree *worktree = NULL;
2325 struct got_repository *repo = NULL;
2326 char *cwd = NULL, *path = NULL, *id_str = NULL;
2327 struct got_object_id *id = NULL;
2328 const char *logmsg = NULL;
2329 const char *got_author = getenv("GOT_AUTHOR");
2330 struct collect_commit_logmsg_arg cl_arg;
2331 char *editor = NULL;
2332 int ch;
2334 while ((ch = getopt(argc, argv, "m:")) != -1) {
2335 switch (ch) {
2336 case 'm':
2337 logmsg = optarg;
2338 break;
2339 default:
2340 usage_commit();
2341 /* NOTREACHED */
2345 argc -= optind;
2346 argv += optind;
2348 if (argc == 1) {
2349 path = realpath(argv[0], NULL);
2350 if (path == NULL) {
2351 error = got_error_from_errno2("realpath", argv[0]);
2352 goto done;
2354 got_path_strip_trailing_slashes(path);
2355 } else if (argc != 0)
2356 usage_commit();
2358 if (got_author == NULL) {
2359 /* TODO: Look current user up in password database */
2360 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2361 goto done;
2364 cwd = getcwd(NULL, 0);
2365 if (cwd == NULL) {
2366 error = got_error_from_errno("getcwd");
2367 goto done;
2369 error = got_worktree_open(&worktree, cwd);
2370 if (error)
2371 goto done;
2373 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2374 if (error != NULL)
2375 goto done;
2378 * unveil(2) traverses exec(2); if an editor is used we have
2379 * to apply unveil after the log message has been written.
2381 if (logmsg == NULL || strlen(logmsg) == 0)
2382 error = get_editor(&editor);
2383 else
2384 error = apply_unveil(got_repo_get_path(repo), 0,
2385 got_worktree_get_root_path(worktree), 0);
2386 if (error)
2387 goto done;
2389 cl_arg.editor = editor;
2390 cl_arg.cmdline_log = logmsg;
2391 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2392 cl_arg.repo_path = got_repo_get_path(repo);
2393 cl_arg.logmsg_path = NULL;
2394 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2395 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2396 if (error) {
2397 if (cl_arg.logmsg_path)
2398 fprintf(stderr, "%s: log message preserved in %s\n",
2399 getprogname(), cl_arg.logmsg_path);
2400 goto done;
2403 if (cl_arg.logmsg_path)
2404 unlink(cl_arg.logmsg_path);
2406 error = got_object_id_str(&id_str, id);
2407 if (error)
2408 goto done;
2409 printf("created commit %s\n", id_str);
2410 done:
2411 if (repo)
2412 got_repo_close(repo);
2413 if (worktree)
2414 got_worktree_close(worktree);
2415 free(path);
2416 free(cwd);
2417 free(id_str);
2418 free(editor);
2419 return error;