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_object_id *commit_id,
300 struct got_object_id *base_commit_id, struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_object_id *yca_id;
305 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
306 commit_id, base_commit_id, repo);
307 if (err)
308 return err;
310 if (yca_id == NULL)
311 return got_error(GOT_ERR_ANCESTRY);
313 /*
314 * Require a straight line of history between the target commit
315 * and the work tree's base commit.
317 * Non-linear situation such as the this require a rebase:
319 * (commit) D F (base_commit)
320 * \ /
321 * C E
322 * \ /
323 * B (yca)
324 * |
325 * A
327 * 'got update' only handles linear cases:
328 * Update forwards in time: A (base/yca) - B - C - D (commit)
329 * Update backwards in time: D (base) - C - D - A (commit/yca)
330 */
331 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
332 got_object_id_cmp(base_commit_id, yca_id) != 0)
333 return got_error(GOT_ERR_ANCESTRY);
335 free(yca_id);
336 return NULL;
340 static const struct got_error *
341 cmd_checkout(int argc, char *argv[])
343 const struct got_error *error = NULL;
344 struct got_repository *repo = NULL;
345 struct got_reference *head_ref = NULL;
346 struct got_worktree *worktree = NULL;
347 char *repo_path = NULL;
348 char *worktree_path = NULL;
349 const char *path_prefix = "";
350 const char *branch_name = GOT_REF_HEAD;
351 char *commit_id_str = NULL;
352 int ch, same_path_prefix;
354 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
355 switch (ch) {
356 case 'b':
357 branch_name = optarg;
358 break;
359 case 'c':
360 commit_id_str = strdup(optarg);
361 if (commit_id_str == NULL)
362 return got_error_from_errno("strdup");
363 break;
364 case 'p':
365 path_prefix = optarg;
366 break;
367 default:
368 usage_checkout();
369 /* NOTREACHED */
373 argc -= optind;
374 argv += optind;
376 #ifndef PROFILE
377 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
378 "unveil", NULL) == -1)
379 err(1, "pledge");
380 #endif
381 if (argc == 1) {
382 char *cwd, *base, *dotgit;
383 repo_path = realpath(argv[0], NULL);
384 if (repo_path == NULL)
385 return got_error_from_errno2("realpath", argv[0]);
386 cwd = getcwd(NULL, 0);
387 if (cwd == NULL) {
388 error = got_error_from_errno("getcwd");
389 goto done;
391 if (path_prefix[0]) {
392 base = basename(path_prefix);
393 if (base == NULL) {
394 error = got_error_from_errno2("basename",
395 path_prefix);
396 goto done;
398 } else {
399 base = basename(repo_path);
400 if (base == NULL) {
401 error = got_error_from_errno2("basename",
402 repo_path);
403 goto done;
406 dotgit = strstr(base, ".git");
407 if (dotgit)
408 *dotgit = '\0';
409 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
410 error = got_error_from_errno("asprintf");
411 free(cwd);
412 goto done;
414 free(cwd);
415 } else if (argc == 2) {
416 repo_path = realpath(argv[0], NULL);
417 if (repo_path == NULL) {
418 error = got_error_from_errno2("realpath", argv[0]);
419 goto done;
421 worktree_path = realpath(argv[1], NULL);
422 if (worktree_path == NULL) {
423 error = got_error_from_errno2("realpath", argv[1]);
424 goto done;
426 } else
427 usage_checkout();
429 got_path_strip_trailing_slashes(repo_path);
430 got_path_strip_trailing_slashes(worktree_path);
432 error = got_repo_open(&repo, repo_path);
433 if (error != NULL)
434 goto done;
436 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
437 if (error)
438 goto done;
440 error = got_ref_open(&head_ref, repo, branch_name, 0);
441 if (error != NULL)
442 goto done;
444 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
445 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
446 goto done;
448 error = got_worktree_open(&worktree, worktree_path);
449 if (error != NULL)
450 goto done;
452 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
453 path_prefix);
454 if (error != NULL)
455 goto done;
456 if (!same_path_prefix) {
457 error = got_error(GOT_ERR_PATH_PREFIX);
458 goto done;
461 if (commit_id_str) {
462 struct got_object_id *commit_id;
463 error = got_object_resolve_id_str(&commit_id, repo,
464 commit_id_str);
465 if (error != NULL)
466 goto done;
467 error = check_linear_ancestry(commit_id,
468 got_worktree_get_base_commit_id(worktree), 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 [-b branch] [-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 const char *branch_name = NULL;
526 struct got_reference *head_ref = NULL;
527 int ch, did_something = 0;
529 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
530 switch (ch) {
531 case 'b':
532 branch_name = optarg;
533 break;
534 case 'c':
535 commit_id_str = strdup(optarg);
536 if (commit_id_str == NULL)
537 return got_error_from_errno("strdup");
538 break;
539 default:
540 usage_update();
541 /* NOTREACHED */
545 argc -= optind;
546 argv += optind;
548 #ifndef PROFILE
549 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
550 "unveil", NULL) == -1)
551 err(1, "pledge");
552 #endif
553 worktree_path = getcwd(NULL, 0);
554 if (worktree_path == NULL) {
555 error = got_error_from_errno("getcwd");
556 goto done;
558 error = got_worktree_open(&worktree, worktree_path);
559 if (error)
560 goto done;
562 if (argc == 0) {
563 path = strdup("");
564 if (path == NULL) {
565 error = got_error_from_errno("strdup");
566 goto done;
568 } else if (argc == 1) {
569 error = got_worktree_resolve_path(&path, worktree, argv[0]);
570 if (error)
571 goto done;
572 } else
573 usage_update();
575 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
576 if (error != NULL)
577 goto done;
579 error = apply_unveil(got_repo_get_path(repo), 0,
580 got_worktree_get_root_path(worktree), 0);
581 if (error)
582 goto done;
584 if (branch_name == NULL)
585 branch_name = got_worktree_get_head_ref_name(worktree);
586 error = got_ref_open(&head_ref, repo, branch_name, 0);
587 if (error != NULL)
588 goto done;
589 if (commit_id_str == NULL) {
590 error = got_ref_resolve(&commit_id, repo, head_ref);
591 if (error != NULL)
592 goto done;
593 error = got_object_id_str(&commit_id_str, commit_id);
594 if (error != NULL)
595 goto done;
596 } else {
597 error = got_object_resolve_id_str(&commit_id, repo,
598 commit_id_str);
599 if (error != NULL)
600 goto done;
603 if (strcmp(got_ref_get_name(head_ref),
604 got_worktree_get_head_ref_name(worktree)) != 0) {
605 struct got_object_id *head_commit_id;
606 if (strlen(path) != 0) {
607 fprintf(stderr, "%s: switching to a different "
608 "branch requires that the entire work tree "
609 "gets updated, not just '%s'\n",
610 getprogname(), path);
611 error = got_error(GOT_ERR_BAD_PATH);
612 goto done;
614 error = got_ref_resolve(&head_commit_id, repo, head_ref);
615 if (error)
616 goto done;
617 error = check_linear_ancestry(commit_id, head_commit_id, repo);
618 if (error != NULL)
619 goto done;
620 error = got_worktree_set_head_ref(worktree, head_ref);
621 if (error)
622 goto done;
623 } else {
624 error = check_linear_ancestry(commit_id,
625 got_worktree_get_base_commit_id(worktree), repo);
626 if (error != NULL)
627 goto done;
630 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
631 commit_id) != 0) {
632 error = got_worktree_set_base_commit_id(worktree, repo,
633 commit_id);
634 if (error)
635 goto done;
638 error = got_worktree_checkout_files(worktree, path, repo,
639 update_progress, &did_something, check_cancelled, NULL);
640 if (error != NULL)
641 goto done;
643 if (did_something)
644 printf("Updated to commit %s\n", commit_id_str);
645 else
646 printf("Already up-to-date\n");
647 done:
648 free(worktree_path);
649 free(path);
650 free(commit_id);
651 free(commit_id_str);
652 return error;
655 static const struct got_error *
656 print_patch(struct got_commit_object *commit, struct got_object_id *id,
657 int diff_context, struct got_repository *repo)
659 const struct got_error *err = NULL;
660 struct got_tree_object *tree1 = NULL, *tree2;
661 struct got_object_qid *qid;
662 char *id_str1 = NULL, *id_str2;
664 err = got_object_open_as_tree(&tree2, repo,
665 got_object_commit_get_tree_id(commit));
666 if (err)
667 return err;
669 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
670 if (qid != NULL) {
671 struct got_commit_object *pcommit;
673 err = got_object_open_as_commit(&pcommit, repo, qid->id);
674 if (err)
675 return err;
677 err = got_object_open_as_tree(&tree1, repo,
678 got_object_commit_get_tree_id(pcommit));
679 got_object_commit_close(pcommit);
680 if (err)
681 return err;
683 err = got_object_id_str(&id_str1, qid->id);
684 if (err)
685 return err;
688 err = got_object_id_str(&id_str2, id);
689 if (err)
690 goto done;
692 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
693 err = got_diff_tree(tree1, tree2, "", "", diff_context, repo, stdout);
694 done:
695 if (tree1)
696 got_object_tree_close(tree1);
697 got_object_tree_close(tree2);
698 free(id_str1);
699 free(id_str2);
700 return err;
703 static char *
704 get_datestr(time_t *time, char *datebuf)
706 char *p, *s = ctime_r(time, datebuf);
707 p = strchr(s, '\n');
708 if (p)
709 *p = '\0';
710 return s;
713 static const struct got_error *
714 print_commit(struct got_commit_object *commit, struct got_object_id *id,
715 struct got_repository *repo, int show_patch, int diff_context,
716 struct got_reflist_head *refs)
718 const struct got_error *err = NULL;
719 char *id_str, *datestr, *logmsg0, *logmsg, *line;
720 char datebuf[26];
721 time_t committer_time;
722 const char *author, *committer;
723 char *refs_str = NULL;
724 struct got_reflist_entry *re;
726 SIMPLEQ_FOREACH(re, refs, entry) {
727 char *s;
728 const char *name;
729 if (got_object_id_cmp(re->id, id) != 0)
730 continue;
731 name = got_ref_get_name(re->ref);
732 if (strcmp(name, GOT_REF_HEAD) == 0)
733 continue;
734 if (strncmp(name, "refs/", 5) == 0)
735 name += 5;
736 if (strncmp(name, "got/", 4) == 0)
737 continue;
738 if (strncmp(name, "heads/", 6) == 0)
739 name += 6;
740 if (strncmp(name, "remotes/", 8) == 0)
741 name += 8;
742 s = refs_str;
743 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
744 name) == -1) {
745 err = got_error_from_errno("asprintf");
746 free(s);
747 break;
749 free(s);
751 err = got_object_id_str(&id_str, id);
752 if (err)
753 return err;
755 printf("-----------------------------------------------\n");
756 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
757 refs_str ? refs_str : "", refs_str ? ")" : "");
758 free(id_str);
759 id_str = NULL;
760 free(refs_str);
761 refs_str = NULL;
762 printf("from: %s\n", got_object_commit_get_author(commit));
763 committer_time = got_object_commit_get_committer_time(commit);
764 datestr = get_datestr(&committer_time, datebuf);
765 printf("date: %s UTC\n", datestr);
766 author = got_object_commit_get_author(commit);
767 committer = got_object_commit_get_committer(commit);
768 if (strcmp(author, committer) != 0)
769 printf("via: %s\n", committer);
770 if (got_object_commit_get_nparents(commit) > 1) {
771 const struct got_object_id_queue *parent_ids;
772 struct got_object_qid *qid;
773 int n = 1;
774 parent_ids = got_object_commit_get_parent_ids(commit);
775 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
776 err = got_object_id_str(&id_str, qid->id);
777 if (err)
778 return err;
779 printf("parent %d: %s\n", n++, id_str);
780 free(id_str);
784 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
785 if (logmsg0 == NULL)
786 return got_error_from_errno("strdup");
788 logmsg = logmsg0;
789 do {
790 line = strsep(&logmsg, "\n");
791 if (line)
792 printf(" %s\n", line);
793 } while (line);
794 free(logmsg0);
796 if (show_patch) {
797 err = print_patch(commit, id, diff_context, repo);
798 if (err == 0)
799 printf("\n");
802 if (fflush(stdout) != 0 && err == NULL)
803 err = got_error_from_errno("fflush");
804 return err;
807 static const struct got_error *
808 print_commits(struct got_object_id *root_id, struct got_repository *repo,
809 char *path, int show_patch, int diff_context, int limit,
810 int first_parent_traversal, struct got_reflist_head *refs)
812 const struct got_error *err;
813 struct got_commit_graph *graph;
815 err = got_commit_graph_open(&graph, root_id, path,
816 first_parent_traversal, repo);
817 if (err)
818 return err;
819 err = got_commit_graph_iter_start(graph, root_id, repo);
820 if (err)
821 goto done;
822 for (;;) {
823 struct got_commit_object *commit;
824 struct got_object_id *id;
826 if (sigint_received || sigpipe_received)
827 break;
829 err = got_commit_graph_iter_next(&id, graph);
830 if (err) {
831 if (err->code == GOT_ERR_ITER_COMPLETED) {
832 err = NULL;
833 break;
835 if (err->code != GOT_ERR_ITER_NEED_MORE)
836 break;
837 err = got_commit_graph_fetch_commits(graph, 1, repo);
838 if (err)
839 break;
840 else
841 continue;
843 if (id == NULL)
844 break;
846 err = got_object_open_as_commit(&commit, repo, id);
847 if (err)
848 break;
849 err = print_commit(commit, id, repo, show_patch, diff_context,
850 refs);
851 got_object_commit_close(commit);
852 if (err || (limit && --limit == 0))
853 break;
855 done:
856 got_commit_graph_close(graph);
857 return err;
860 __dead static void
861 usage_log(void)
863 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
864 "[-r repository-path] [path]\n", getprogname());
865 exit(1);
868 static const struct got_error *
869 cmd_log(int argc, char *argv[])
871 const struct got_error *error;
872 struct got_repository *repo = NULL;
873 struct got_worktree *worktree = NULL;
874 struct got_commit_object *commit = NULL;
875 struct got_object_id *id = NULL;
876 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
877 char *start_commit = NULL;
878 int diff_context = 3, ch;
879 int show_patch = 0, limit = 0, first_parent_traversal = 0;
880 const char *errstr;
881 struct got_reflist_head refs;
883 SIMPLEQ_INIT(&refs);
885 #ifndef PROFILE
886 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
887 NULL)
888 == -1)
889 err(1, "pledge");
890 #endif
892 while ((ch = getopt(argc, argv, "pc:C:l:fr:")) != -1) {
893 switch (ch) {
894 case 'p':
895 show_patch = 1;
896 break;
897 case 'c':
898 start_commit = optarg;
899 break;
900 case 'C':
901 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
902 &errstr);
903 if (errstr != NULL)
904 err(1, "-C option %s", errstr);
905 break;
906 case 'l':
907 limit = strtonum(optarg, 1, INT_MAX, &errstr);
908 if (errstr != NULL)
909 err(1, "-l option %s", errstr);
910 break;
911 case 'f':
912 first_parent_traversal = 1;
913 break;
914 case 'r':
915 repo_path = realpath(optarg, NULL);
916 if (repo_path == NULL)
917 err(1, "-r option");
918 got_path_strip_trailing_slashes(repo_path);
919 break;
920 default:
921 usage_log();
922 /* NOTREACHED */
926 argc -= optind;
927 argv += optind;
929 cwd = getcwd(NULL, 0);
930 if (cwd == NULL) {
931 error = got_error_from_errno("getcwd");
932 goto done;
935 error = got_worktree_open(&worktree, cwd);
936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
937 goto done;
938 error = NULL;
940 if (argc == 0) {
941 path = strdup("");
942 if (path == NULL) {
943 error = got_error_from_errno("strdup");
944 goto done;
946 } else if (argc == 1) {
947 if (worktree) {
948 error = got_worktree_resolve_path(&path, worktree,
949 argv[0]);
950 if (error)
951 goto done;
952 } else {
953 path = strdup(argv[0]);
954 if (path == NULL) {
955 error = got_error_from_errno("strdup");
956 goto done;
959 } else
960 usage_log();
962 if (repo_path == NULL) {
963 repo_path = worktree ?
964 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
966 if (repo_path == NULL) {
967 error = got_error_from_errno("strdup");
968 goto done;
971 error = got_repo_open(&repo, repo_path);
972 if (error != NULL)
973 goto done;
975 error = apply_unveil(got_repo_get_path(repo), 1,
976 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
977 if (error)
978 goto done;
980 if (start_commit == NULL) {
981 struct got_reference *head_ref;
982 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
983 if (error != NULL)
984 return error;
985 error = got_ref_resolve(&id, repo, head_ref);
986 got_ref_close(head_ref);
987 if (error != NULL)
988 return error;
989 error = got_object_open_as_commit(&commit, repo, id);
990 } else {
991 struct got_reference *ref;
992 error = got_ref_open(&ref, repo, start_commit, 0);
993 if (error == NULL) {
994 int obj_type;
995 error = got_ref_resolve(&id, repo, ref);
996 got_ref_close(ref);
997 if (error != NULL)
998 goto done;
999 error = got_object_get_type(&obj_type, repo, id);
1000 if (error != NULL)
1001 goto done;
1002 if (obj_type == GOT_OBJ_TYPE_TAG) {
1003 struct got_tag_object *tag;
1004 error = got_object_open_as_tag(&tag, repo, id);
1005 if (error != NULL)
1006 goto done;
1007 if (got_object_tag_get_object_type(tag) !=
1008 GOT_OBJ_TYPE_COMMIT) {
1009 got_object_tag_close(tag);
1010 error = got_error(GOT_ERR_OBJ_TYPE);
1011 goto done;
1013 free(id);
1014 id = got_object_id_dup(
1015 got_object_tag_get_object_id(tag));
1016 if (id == NULL)
1017 error = got_error_from_errno(
1018 "got_object_id_dup");
1019 got_object_tag_close(tag);
1020 if (error)
1021 goto done;
1022 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1023 error = got_error(GOT_ERR_OBJ_TYPE);
1024 goto done;
1026 error = got_object_open_as_commit(&commit, repo, id);
1027 if (error != NULL)
1028 goto done;
1030 if (commit == NULL) {
1031 error = got_object_resolve_id_str(&id, repo,
1032 start_commit);
1033 if (error != NULL)
1034 return error;
1037 if (error != NULL)
1038 goto done;
1040 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1041 if (error != NULL)
1042 goto done;
1043 if (in_repo_path) {
1044 free(path);
1045 path = in_repo_path;
1048 error = got_ref_list(&refs, repo);
1049 if (error)
1050 goto done;
1052 error = print_commits(id, repo, path, show_patch,
1053 diff_context, limit, first_parent_traversal, &refs);
1054 done:
1055 free(path);
1056 free(repo_path);
1057 free(cwd);
1058 free(id);
1059 if (worktree)
1060 got_worktree_close(worktree);
1061 if (repo) {
1062 const struct got_error *repo_error;
1063 repo_error = got_repo_close(repo);
1064 if (error == NULL)
1065 error = repo_error;
1067 got_ref_list_free(&refs);
1068 return error;
1071 __dead static void
1072 usage_diff(void)
1074 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1075 "[object1 object2 | path]\n", getprogname());
1076 exit(1);
1079 struct print_diff_arg {
1080 struct got_repository *repo;
1081 struct got_worktree *worktree;
1082 int diff_context;
1083 const char *id_str;
1084 int header_shown;
1087 static const struct got_error *
1088 print_diff(void *arg, unsigned char status, const char *path,
1089 struct got_object_id *blob_id, struct got_object_id *commit_id)
1091 struct print_diff_arg *a = arg;
1092 const struct got_error *err = NULL;
1093 struct got_blob_object *blob1 = NULL;
1094 FILE *f2 = NULL;
1095 char *abspath = NULL;
1096 struct stat sb;
1098 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1099 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1100 return NULL;
1102 if (!a->header_shown) {
1103 printf("diff %s %s\n", a->id_str,
1104 got_worktree_get_root_path(a->worktree));
1105 a->header_shown = 1;
1108 if (status != GOT_STATUS_ADD) {
1109 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1110 if (err)
1111 goto done;
1115 if (status != GOT_STATUS_DELETE) {
1116 if (asprintf(&abspath, "%s/%s",
1117 got_worktree_get_root_path(a->worktree), path) == -1) {
1118 err = got_error_from_errno("asprintf");
1119 goto done;
1122 f2 = fopen(abspath, "r");
1123 if (f2 == NULL) {
1124 err = got_error_from_errno2("fopen", abspath);
1125 goto done;
1127 if (lstat(abspath, &sb) == -1) {
1128 err = got_error_from_errno2("lstat", abspath);
1129 goto done;
1131 } else
1132 sb.st_size = 0;
1134 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1135 stdout);
1136 done:
1137 if (blob1)
1138 got_object_blob_close(blob1);
1139 if (f2 && fclose(f2) != 0 && err == NULL)
1140 err = got_error_from_errno("fclose");
1141 free(abspath);
1142 return err;
1145 static const struct got_error *
1146 cmd_diff(int argc, char *argv[])
1148 const struct got_error *error;
1149 struct got_repository *repo = NULL;
1150 struct got_worktree *worktree = NULL;
1151 char *cwd = NULL, *repo_path = NULL;
1152 struct got_object_id *id1 = NULL, *id2 = NULL;
1153 char *id_str1 = NULL, *id_str2 = NULL;
1154 int type1, type2;
1155 int diff_context = 3, ch;
1156 const char *errstr;
1157 char *path = NULL;
1159 #ifndef PROFILE
1160 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1161 NULL) == -1)
1162 err(1, "pledge");
1163 #endif
1165 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1166 switch (ch) {
1167 case 'C':
1168 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1169 if (errstr != NULL)
1170 err(1, "-C option %s", errstr);
1171 break;
1172 case 'r':
1173 repo_path = realpath(optarg, NULL);
1174 if (repo_path == NULL)
1175 err(1, "-r option");
1176 got_path_strip_trailing_slashes(repo_path);
1177 break;
1178 default:
1179 usage_diff();
1180 /* NOTREACHED */
1184 argc -= optind;
1185 argv += optind;
1187 cwd = getcwd(NULL, 0);
1188 if (cwd == NULL) {
1189 error = got_error_from_errno("getcwd");
1190 goto done;
1192 error = got_worktree_open(&worktree, cwd);
1193 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1194 goto done;
1195 if (argc <= 1) {
1196 if (worktree == NULL) {
1197 error = got_error(GOT_ERR_NOT_WORKTREE);
1198 goto done;
1200 if (repo_path)
1201 errx(1,
1202 "-r option can't be used when diffing a work tree");
1203 repo_path = strdup(got_worktree_get_repo_path(worktree));
1204 if (repo_path == NULL) {
1205 error = got_error_from_errno("strdup");
1206 goto done;
1208 if (argc == 1) {
1209 error = got_worktree_resolve_path(&path, worktree,
1210 argv[0]);
1211 if (error)
1212 goto done;
1213 } else {
1214 path = strdup("");
1215 if (path == NULL) {
1216 error = got_error_from_errno("strdup");
1217 goto done;
1220 } else if (argc == 2) {
1221 id_str1 = argv[0];
1222 id_str2 = argv[1];
1223 } else
1224 usage_diff();
1226 if (repo_path == NULL) {
1227 repo_path = getcwd(NULL, 0);
1228 if (repo_path == NULL)
1229 return got_error_from_errno("getcwd");
1232 error = got_repo_open(&repo, repo_path);
1233 free(repo_path);
1234 if (error != NULL)
1235 goto done;
1237 error = apply_unveil(got_repo_get_path(repo), 1,
1238 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1239 if (error)
1240 goto done;
1242 if (worktree) {
1243 struct print_diff_arg arg;
1244 char *id_str;
1245 error = got_object_id_str(&id_str,
1246 got_worktree_get_base_commit_id(worktree));
1247 if (error)
1248 goto done;
1249 arg.repo = repo;
1250 arg.worktree = worktree;
1251 arg.diff_context = diff_context;
1252 arg.id_str = id_str;
1253 arg.header_shown = 0;
1255 error = got_worktree_status(worktree, path, repo, print_diff,
1256 &arg, check_cancelled, NULL);
1257 free(id_str);
1258 goto done;
1261 error = got_object_resolve_id_str(&id1, repo, id_str1);
1262 if (error)
1263 goto done;
1265 error = got_object_resolve_id_str(&id2, repo, id_str2);
1266 if (error)
1267 goto done;
1269 error = got_object_get_type(&type1, repo, id1);
1270 if (error)
1271 goto done;
1273 error = got_object_get_type(&type2, repo, id2);
1274 if (error)
1275 goto done;
1277 if (type1 != type2) {
1278 error = got_error(GOT_ERR_OBJ_TYPE);
1279 goto done;
1282 switch (type1) {
1283 case GOT_OBJ_TYPE_BLOB:
1284 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1285 diff_context, repo, stdout);
1286 break;
1287 case GOT_OBJ_TYPE_TREE:
1288 error = got_diff_objects_as_trees(id1, id2, "", "",
1289 diff_context, repo, stdout);
1290 break;
1291 case GOT_OBJ_TYPE_COMMIT:
1292 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null",
1293 id_str2);
1294 error = got_diff_objects_as_commits(id1, id2, diff_context,
1295 repo, stdout);
1296 break;
1297 default:
1298 error = got_error(GOT_ERR_OBJ_TYPE);
1301 done:
1302 free(id1);
1303 free(id2);
1304 free(path);
1305 if (worktree)
1306 got_worktree_close(worktree);
1307 if (repo) {
1308 const struct got_error *repo_error;
1309 repo_error = got_repo_close(repo);
1310 if (error == NULL)
1311 error = repo_error;
1313 return error;
1316 __dead static void
1317 usage_blame(void)
1319 fprintf(stderr,
1320 "usage: %s blame [-c commit] [-r repository-path] path\n",
1321 getprogname());
1322 exit(1);
1325 static const struct got_error *
1326 cmd_blame(int argc, char *argv[])
1328 const struct got_error *error;
1329 struct got_repository *repo = NULL;
1330 struct got_worktree *worktree = NULL;
1331 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1332 struct got_object_id *commit_id = NULL;
1333 char *commit_id_str = NULL;
1334 int ch;
1336 #ifndef PROFILE
1337 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1338 NULL) == -1)
1339 err(1, "pledge");
1340 #endif
1342 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1343 switch (ch) {
1344 case 'c':
1345 commit_id_str = optarg;
1346 break;
1347 case 'r':
1348 repo_path = realpath(optarg, NULL);
1349 if (repo_path == NULL)
1350 err(1, "-r option");
1351 got_path_strip_trailing_slashes(repo_path);
1352 break;
1353 default:
1354 usage_blame();
1355 /* NOTREACHED */
1359 argc -= optind;
1360 argv += optind;
1362 if (argc == 1)
1363 path = argv[0];
1364 else
1365 usage_blame();
1367 cwd = getcwd(NULL, 0);
1368 if (cwd == NULL) {
1369 error = got_error_from_errno("getcwd");
1370 goto done;
1372 if (repo_path == NULL) {
1373 error = got_worktree_open(&worktree, cwd);
1374 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1375 goto done;
1376 else
1377 error = NULL;
1378 if (worktree) {
1379 repo_path =
1380 strdup(got_worktree_get_repo_path(worktree));
1381 if (repo_path == NULL)
1382 error = got_error_from_errno("strdup");
1383 if (error)
1384 goto done;
1385 } else {
1386 repo_path = strdup(cwd);
1387 if (repo_path == NULL) {
1388 error = got_error_from_errno("strdup");
1389 goto done;
1394 error = got_repo_open(&repo, repo_path);
1395 if (error != NULL)
1396 goto done;
1398 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1399 if (error)
1400 goto done;
1402 if (worktree) {
1403 const char *prefix = got_worktree_get_path_prefix(worktree);
1404 char *p, *worktree_subdir = cwd +
1405 strlen(got_worktree_get_root_path(worktree));
1406 if (asprintf(&p, "%s%s%s%s%s",
1407 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1408 worktree_subdir, worktree_subdir[0] ? "/" : "",
1409 path) == -1) {
1410 error = got_error_from_errno("asprintf");
1411 goto done;
1413 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1414 free(p);
1415 } else {
1416 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1418 if (error)
1419 goto done;
1421 if (commit_id_str == NULL) {
1422 struct got_reference *head_ref;
1423 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1424 if (error != NULL)
1425 goto done;
1426 error = got_ref_resolve(&commit_id, repo, head_ref);
1427 got_ref_close(head_ref);
1428 if (error != NULL)
1429 goto done;
1430 } else {
1431 error = got_object_resolve_id_str(&commit_id, repo,
1432 commit_id_str);
1433 if (error != NULL)
1434 goto done;
1437 error = got_blame(in_repo_path, commit_id, repo, stdout);
1438 done:
1439 free(in_repo_path);
1440 free(repo_path);
1441 free(cwd);
1442 free(commit_id);
1443 if (worktree)
1444 got_worktree_close(worktree);
1445 if (repo) {
1446 const struct got_error *repo_error;
1447 repo_error = got_repo_close(repo);
1448 if (error == NULL)
1449 error = repo_error;
1451 return error;
1454 __dead static void
1455 usage_tree(void)
1457 fprintf(stderr,
1458 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1459 getprogname());
1460 exit(1);
1463 static void
1464 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1465 const char *root_path)
1467 int is_root_path = (strcmp(path, root_path) == 0);
1469 path += strlen(root_path);
1470 while (path[0] == '/')
1471 path++;
1473 printf("%s%s%s%s%s\n", id ? id : "", path,
1474 is_root_path ? "" : "/", te->name,
1475 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1478 static const struct got_error *
1479 print_tree(const char *path, struct got_object_id *commit_id,
1480 int show_ids, int recurse, const char *root_path,
1481 struct got_repository *repo)
1483 const struct got_error *err = NULL;
1484 struct got_object_id *tree_id = NULL;
1485 struct got_tree_object *tree = NULL;
1486 const struct got_tree_entries *entries;
1487 struct got_tree_entry *te;
1489 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1490 if (err)
1491 goto done;
1493 err = got_object_open_as_tree(&tree, repo, tree_id);
1494 if (err)
1495 goto done;
1496 entries = got_object_tree_get_entries(tree);
1497 te = SIMPLEQ_FIRST(&entries->head);
1498 while (te) {
1499 char *id = NULL;
1501 if (sigint_received || sigpipe_received)
1502 break;
1504 if (show_ids) {
1505 char *id_str;
1506 err = got_object_id_str(&id_str, te->id);
1507 if (err)
1508 goto done;
1509 if (asprintf(&id, "%s ", id_str) == -1) {
1510 err = got_error_from_errno("asprintf");
1511 free(id_str);
1512 goto done;
1514 free(id_str);
1516 print_entry(te, id, path, root_path);
1517 free(id);
1519 if (recurse && S_ISDIR(te->mode)) {
1520 char *child_path;
1521 if (asprintf(&child_path, "%s%s%s", path,
1522 path[0] == '/' && path[1] == '\0' ? "" : "/",
1523 te->name) == -1) {
1524 err = got_error_from_errno("asprintf");
1525 goto done;
1527 err = print_tree(child_path, commit_id, show_ids, 1,
1528 root_path, repo);
1529 free(child_path);
1530 if (err)
1531 goto done;
1534 te = SIMPLEQ_NEXT(te, entry);
1536 done:
1537 if (tree)
1538 got_object_tree_close(tree);
1539 free(tree_id);
1540 return err;
1543 static const struct got_error *
1544 cmd_tree(int argc, char *argv[])
1546 const struct got_error *error;
1547 struct got_repository *repo = NULL;
1548 struct got_worktree *worktree = NULL;
1549 const char *path;
1550 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1551 struct got_object_id *commit_id = NULL;
1552 char *commit_id_str = NULL;
1553 int show_ids = 0, recurse = 0;
1554 int ch;
1556 #ifndef PROFILE
1557 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1558 NULL) == -1)
1559 err(1, "pledge");
1560 #endif
1562 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1563 switch (ch) {
1564 case 'c':
1565 commit_id_str = optarg;
1566 break;
1567 case 'r':
1568 repo_path = realpath(optarg, NULL);
1569 if (repo_path == NULL)
1570 err(1, "-r option");
1571 got_path_strip_trailing_slashes(repo_path);
1572 break;
1573 case 'i':
1574 show_ids = 1;
1575 break;
1576 case 'R':
1577 recurse = 1;
1578 break;
1579 default:
1580 usage_tree();
1581 /* NOTREACHED */
1585 argc -= optind;
1586 argv += optind;
1588 if (argc == 1)
1589 path = argv[0];
1590 else if (argc > 1)
1591 usage_tree();
1592 else
1593 path = NULL;
1595 cwd = getcwd(NULL, 0);
1596 if (cwd == NULL) {
1597 error = got_error_from_errno("getcwd");
1598 goto done;
1600 if (repo_path == NULL) {
1601 error = got_worktree_open(&worktree, cwd);
1602 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1603 goto done;
1604 else
1605 error = NULL;
1606 if (worktree) {
1607 repo_path =
1608 strdup(got_worktree_get_repo_path(worktree));
1609 if (repo_path == NULL)
1610 error = got_error_from_errno("strdup");
1611 if (error)
1612 goto done;
1613 } else {
1614 repo_path = strdup(cwd);
1615 if (repo_path == NULL) {
1616 error = got_error_from_errno("strdup");
1617 goto done;
1622 error = got_repo_open(&repo, repo_path);
1623 if (error != NULL)
1624 goto done;
1626 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1627 if (error)
1628 goto done;
1630 if (path == NULL) {
1631 if (worktree) {
1632 char *p, *worktree_subdir = cwd +
1633 strlen(got_worktree_get_root_path(worktree));
1634 if (asprintf(&p, "%s/%s",
1635 got_worktree_get_path_prefix(worktree),
1636 worktree_subdir) == -1) {
1637 error = got_error_from_errno("asprintf");
1638 goto done;
1640 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1641 free(p);
1642 if (error)
1643 goto done;
1644 } else
1645 path = "/";
1647 if (in_repo_path == NULL) {
1648 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1649 if (error != NULL)
1650 goto done;
1653 if (commit_id_str == NULL) {
1654 struct got_reference *head_ref;
1655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1656 if (error != NULL)
1657 goto done;
1658 error = got_ref_resolve(&commit_id, repo, head_ref);
1659 got_ref_close(head_ref);
1660 if (error != NULL)
1661 goto done;
1662 } else {
1663 error = got_object_resolve_id_str(&commit_id, repo,
1664 commit_id_str);
1665 if (error != NULL)
1666 goto done;
1669 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1670 in_repo_path, repo);
1671 done:
1672 free(in_repo_path);
1673 free(repo_path);
1674 free(cwd);
1675 free(commit_id);
1676 if (worktree)
1677 got_worktree_close(worktree);
1678 if (repo) {
1679 const struct got_error *repo_error;
1680 repo_error = got_repo_close(repo);
1681 if (error == NULL)
1682 error = repo_error;
1684 return error;
1687 __dead static void
1688 usage_status(void)
1690 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1691 exit(1);
1694 static const struct got_error *
1695 print_status(void *arg, unsigned char status, const char *path,
1696 struct got_object_id *blob_id, struct got_object_id *commit_id)
1698 printf("%c %s\n", status, path);
1699 return NULL;
1702 static const struct got_error *
1703 cmd_status(int argc, char *argv[])
1705 const struct got_error *error = NULL;
1706 struct got_repository *repo = NULL;
1707 struct got_worktree *worktree = NULL;
1708 char *cwd = NULL, *path = NULL;
1709 int ch;
1711 while ((ch = getopt(argc, argv, "")) != -1) {
1712 switch (ch) {
1713 default:
1714 usage_status();
1715 /* NOTREACHED */
1719 argc -= optind;
1720 argv += optind;
1722 #ifndef PROFILE
1723 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1724 NULL) == -1)
1725 err(1, "pledge");
1726 #endif
1727 cwd = getcwd(NULL, 0);
1728 if (cwd == NULL) {
1729 error = got_error_from_errno("getcwd");
1730 goto done;
1733 error = got_worktree_open(&worktree, cwd);
1734 if (error != NULL)
1735 goto done;
1737 if (argc == 0) {
1738 path = strdup("");
1739 if (path == NULL) {
1740 error = got_error_from_errno("strdup");
1741 goto done;
1743 } else if (argc == 1) {
1744 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1745 if (error)
1746 goto done;
1747 } else
1748 usage_status();
1750 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1751 if (error != NULL)
1752 goto done;
1754 error = apply_unveil(got_repo_get_path(repo), 1,
1755 got_worktree_get_root_path(worktree), 0);
1756 if (error)
1757 goto done;
1759 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1760 check_cancelled, NULL);
1761 done:
1762 free(cwd);
1763 free(path);
1764 return error;
1767 __dead static void
1768 usage_ref(void)
1770 fprintf(stderr,
1771 "usage: %s ref [-r repository] -l | -d name | name target\n",
1772 getprogname());
1773 exit(1);
1776 static const struct got_error *
1777 list_refs(struct got_repository *repo)
1779 static const struct got_error *err = NULL;
1780 struct got_reflist_head refs;
1781 struct got_reflist_entry *re;
1783 SIMPLEQ_INIT(&refs);
1784 err = got_ref_list(&refs, repo);
1785 if (err)
1786 return err;
1788 SIMPLEQ_FOREACH(re, &refs, entry) {
1789 char *refstr;
1790 refstr = got_ref_to_str(re->ref);
1791 if (refstr == NULL)
1792 return got_error_from_errno("got_ref_to_str");
1793 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
1794 free(refstr);
1797 got_ref_list_free(&refs);
1798 return NULL;
1801 static const struct got_error *
1802 delete_ref(struct got_repository *repo, const char *refname)
1804 const struct got_error *err = NULL;
1805 struct got_reference *ref;
1807 err = got_ref_open(&ref, repo, refname, 0);
1808 if (err)
1809 return err;
1811 err = got_ref_delete(ref, repo);
1812 got_ref_close(ref);
1813 return err;
1816 static const struct got_error *
1817 add_ref(struct got_repository *repo, const char *refname, const char *target)
1819 const struct got_error *err = NULL;
1820 struct got_object_id *id;
1821 struct got_reference *ref = NULL;
1823 err = got_object_resolve_id_str(&id, repo, target);
1824 if (err) {
1825 struct got_reference *target_ref;
1827 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1828 return err;
1829 err = got_ref_open(&target_ref, repo, target, 0);
1830 if (err)
1831 return err;
1832 err = got_ref_resolve(&id, repo, target_ref);
1833 got_ref_close(target_ref);
1834 if (err)
1835 return err;
1838 err = got_ref_alloc(&ref, refname, id);
1839 if (err)
1840 goto done;
1842 err = got_ref_write(ref, repo);
1843 done:
1844 if (ref)
1845 got_ref_close(ref);
1846 free(id);
1847 return err;
1850 static const struct got_error *
1851 cmd_ref(int argc, char *argv[])
1853 const struct got_error *error = NULL;
1854 struct got_repository *repo = NULL;
1855 struct got_worktree *worktree = NULL;
1856 char *cwd = NULL, *repo_path = NULL;
1857 int ch, do_list = 0;
1858 const char *delref = NULL;
1860 /* TODO: Add -s option for adding symbolic references. */
1861 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
1862 switch (ch) {
1863 case 'd':
1864 delref = optarg;
1865 break;
1866 case 'r':
1867 repo_path = realpath(optarg, NULL);
1868 if (repo_path == NULL)
1869 err(1, "-r option");
1870 got_path_strip_trailing_slashes(repo_path);
1871 break;
1872 case 'l':
1873 do_list = 1;
1874 break;
1875 default:
1876 usage_ref();
1877 /* NOTREACHED */
1881 if (do_list && delref)
1882 errx(1, "-l and -d options are mutually exclusive\n");
1884 argc -= optind;
1885 argv += optind;
1887 if (do_list || delref) {
1888 if (argc > 0)
1889 usage_ref();
1890 } else if (argc != 2)
1891 usage_ref();
1893 #ifndef PROFILE
1894 if (do_list) {
1895 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
1896 NULL) == -1)
1897 err(1, "pledge");
1898 } else {
1899 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1900 "sendfd unveil", NULL) == -1)
1901 err(1, "pledge");
1903 #endif
1904 cwd = getcwd(NULL, 0);
1905 if (cwd == NULL) {
1906 error = got_error_from_errno("getcwd");
1907 goto done;
1910 if (repo_path == NULL) {
1911 error = got_worktree_open(&worktree, cwd);
1912 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 goto done;
1914 else
1915 error = NULL;
1916 if (worktree) {
1917 repo_path =
1918 strdup(got_worktree_get_repo_path(worktree));
1919 if (repo_path == NULL)
1920 error = got_error_from_errno("strdup");
1921 if (error)
1922 goto done;
1923 } else {
1924 repo_path = strdup(cwd);
1925 if (repo_path == NULL) {
1926 error = got_error_from_errno("strdup");
1927 goto done;
1932 error = got_repo_open(&repo, repo_path);
1933 if (error != NULL)
1934 goto done;
1936 error = apply_unveil(got_repo_get_path(repo), do_list,
1937 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1938 if (error)
1939 goto done;
1941 if (do_list)
1942 error = list_refs(repo);
1943 else if (delref)
1944 error = delete_ref(repo, delref);
1945 else
1946 error = add_ref(repo, argv[0], argv[1]);
1947 done:
1948 if (repo)
1949 got_repo_close(repo);
1950 if (worktree)
1951 got_worktree_close(worktree);
1952 free(cwd);
1953 free(repo_path);
1954 return error;
1957 __dead static void
1958 usage_add(void)
1960 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
1961 exit(1);
1964 static const struct got_error *
1965 cmd_add(int argc, char *argv[])
1967 const struct got_error *error = NULL;
1968 struct got_repository *repo = NULL;
1969 struct got_worktree *worktree = NULL;
1970 char *cwd = NULL;
1971 struct got_pathlist_head paths;
1972 struct got_pathlist_entry *pe;
1973 int ch, x;
1975 TAILQ_INIT(&paths);
1977 while ((ch = getopt(argc, argv, "")) != -1) {
1978 switch (ch) {
1979 default:
1980 usage_add();
1981 /* NOTREACHED */
1985 argc -= optind;
1986 argv += optind;
1988 if (argc < 1)
1989 usage_add();
1991 /* make sure each file exists before doing anything halfway */
1992 for (x = 0; x < argc; x++) {
1993 char *path = realpath(argv[x], NULL);
1994 if (path == NULL) {
1995 error = got_error_from_errno2("realpath", argv[x]);
1996 goto done;
1998 free(path);
2001 cwd = getcwd(NULL, 0);
2002 if (cwd == NULL) {
2003 error = got_error_from_errno("getcwd");
2004 goto done;
2007 error = got_worktree_open(&worktree, cwd);
2008 if (error)
2009 goto done;
2011 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2012 if (error != NULL)
2013 goto done;
2015 error = apply_unveil(got_repo_get_path(repo), 1,
2016 got_worktree_get_root_path(worktree), 0);
2017 if (error)
2018 goto done;
2020 for (x = 0; x < argc; x++) {
2021 char *path = realpath(argv[x], NULL);
2022 if (path == NULL) {
2023 error = got_error_from_errno2("realpath", argv[x]);
2024 goto done;
2027 got_path_strip_trailing_slashes(path);
2028 error = got_pathlist_insert(&pe, &paths, path, NULL);
2029 if (error) {
2030 free(path);
2031 goto done;
2034 error = got_worktree_schedule_add(worktree, &paths, print_status,
2035 NULL, repo);
2036 done:
2037 if (repo)
2038 got_repo_close(repo);
2039 if (worktree)
2040 got_worktree_close(worktree);
2041 TAILQ_FOREACH(pe, &paths, entry)
2042 free((char *)pe->path);
2043 got_pathlist_free(&paths);
2044 free(cwd);
2045 return error;
2048 __dead static void
2049 usage_rm(void)
2051 fprintf(stderr, "usage: %s rm [-f] file-path\n", getprogname());
2052 exit(1);
2055 static const struct got_error *
2056 cmd_rm(int argc, char *argv[])
2058 const struct got_error *error = NULL;
2059 struct got_worktree *worktree = NULL;
2060 struct got_repository *repo = NULL;
2061 char *cwd = NULL, *path = NULL;
2062 int ch, delete_local_mods = 0;
2064 while ((ch = getopt(argc, argv, "f")) != -1) {
2065 switch (ch) {
2066 case 'f':
2067 delete_local_mods = 1;
2068 break;
2069 default:
2070 usage_add();
2071 /* NOTREACHED */
2075 argc -= optind;
2076 argv += optind;
2078 if (argc != 1)
2079 usage_rm();
2081 path = realpath(argv[0], NULL);
2082 if (path == NULL) {
2083 error = got_error_from_errno2("realpath", argv[0]);
2084 goto done;
2086 got_path_strip_trailing_slashes(path);
2088 cwd = getcwd(NULL, 0);
2089 if (cwd == NULL) {
2090 error = got_error_from_errno("getcwd");
2091 goto done;
2093 error = got_worktree_open(&worktree, cwd);
2094 if (error)
2095 goto done;
2097 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2098 if (error)
2099 goto done;
2101 error = apply_unveil(got_repo_get_path(repo), 1,
2102 got_worktree_get_root_path(worktree), 0);
2103 if (error)
2104 goto done;
2106 error = got_worktree_schedule_delete(worktree, path, delete_local_mods,
2107 print_status, NULL, repo);
2108 if (error)
2109 goto done;
2110 done:
2111 if (repo)
2112 got_repo_close(repo);
2113 if (worktree)
2114 got_worktree_close(worktree);
2115 free(path);
2116 free(cwd);
2117 return error;
2120 __dead static void
2121 usage_revert(void)
2123 fprintf(stderr, "usage: %s revert file-path\n", getprogname());
2124 exit(1);
2127 static void
2128 revert_progress(void *arg, unsigned char status, const char *path)
2130 while (path[0] == '/')
2131 path++;
2132 printf("%c %s\n", status, path);
2135 static const struct got_error *
2136 cmd_revert(int argc, char *argv[])
2138 const struct got_error *error = NULL;
2139 struct got_worktree *worktree = NULL;
2140 struct got_repository *repo = NULL;
2141 char *cwd = NULL, *path = NULL;
2142 int ch;
2144 while ((ch = getopt(argc, argv, "")) != -1) {
2145 switch (ch) {
2146 default:
2147 usage_revert();
2148 /* NOTREACHED */
2152 argc -= optind;
2153 argv += optind;
2155 if (argc != 1)
2156 usage_revert();
2158 path = realpath(argv[0], NULL);
2159 if (path == NULL) {
2160 error = got_error_from_errno2("realpath", argv[0]);
2161 goto done;
2163 got_path_strip_trailing_slashes(path);
2165 cwd = getcwd(NULL, 0);
2166 if (cwd == NULL) {
2167 error = got_error_from_errno("getcwd");
2168 goto done;
2170 error = got_worktree_open(&worktree, cwd);
2171 if (error)
2172 goto done;
2174 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2175 if (error != NULL)
2176 goto done;
2178 error = apply_unveil(got_repo_get_path(repo), 1,
2179 got_worktree_get_root_path(worktree), 0);
2180 if (error)
2181 goto done;
2183 error = got_worktree_revert(worktree, path,
2184 revert_progress, NULL, repo);
2185 if (error)
2186 goto done;
2187 done:
2188 if (repo)
2189 got_repo_close(repo);
2190 if (worktree)
2191 got_worktree_close(worktree);
2192 free(path);
2193 free(cwd);
2194 return error;
2197 __dead static void
2198 usage_commit(void)
2200 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2201 exit(1);
2204 int
2205 spawn_editor(const char *editor, const char *file)
2207 pid_t pid;
2208 sig_t sighup, sigint, sigquit;
2209 int st = -1;
2211 sighup = signal(SIGHUP, SIG_IGN);
2212 sigint = signal(SIGINT, SIG_IGN);
2213 sigquit = signal(SIGQUIT, SIG_IGN);
2215 switch (pid = fork()) {
2216 case -1:
2217 goto doneediting;
2218 case 0:
2219 execl(editor, editor, file, (char *)NULL);
2220 _exit(127);
2223 while (waitpid(pid, &st, 0) == -1)
2224 if (errno != EINTR)
2225 break;
2227 doneediting:
2228 (void)signal(SIGHUP, sighup);
2229 (void)signal(SIGINT, sigint);
2230 (void)signal(SIGQUIT, sigquit);
2232 if (!WIFEXITED(st)) {
2233 errno = EINTR;
2234 return -1;
2237 return WEXITSTATUS(st);
2240 struct collect_commit_logmsg_arg {
2241 const char *cmdline_log;
2242 const char *editor;
2243 const char *worktree_path;
2244 const char *repo_path;
2245 char *logmsg_path;
2249 static const struct got_error *
2250 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2251 void *arg)
2253 const char *initial_content = "\n# changes to be committed:\n";
2254 struct got_pathlist_entry *pe;
2255 const struct got_error *err = NULL;
2256 char *template = NULL;
2257 struct collect_commit_logmsg_arg *a = arg;
2258 char buf[1024];
2259 struct stat st, st2;
2260 FILE *fp;
2261 size_t len;
2262 int fd, content_changed = 0;
2264 /* if a message was specified on the command line, just use it */
2265 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2266 len = strlen(a->cmdline_log) + 1;
2267 *logmsg = malloc(len + 1);
2268 if (*logmsg == NULL)
2269 return got_error_from_errno("malloc");
2270 strlcpy(*logmsg, a->cmdline_log, len);
2271 return NULL;
2274 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2275 return got_error_from_errno("asprintf");
2277 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2278 if (err)
2279 goto done;
2281 dprintf(fd, initial_content);
2283 TAILQ_FOREACH(pe, commitable_paths, entry) {
2284 struct got_commitable *ct = pe->data;
2285 dprintf(fd, "# %c %s\n", ct->status, pe->path);
2287 close(fd);
2289 if (stat(a->logmsg_path, &st) == -1) {
2290 err = got_error_from_errno2("stat", a->logmsg_path);
2291 goto done;
2294 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2295 err = got_error_from_errno("failed spawning editor");
2296 goto done;
2299 if (stat(a->logmsg_path, &st2) == -1) {
2300 err = got_error_from_errno("stat");
2301 goto done;
2304 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2305 unlink(a->logmsg_path);
2306 free(a->logmsg_path);
2307 a->logmsg_path = NULL;
2308 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2309 "no changes made to commit message, aborting");
2310 goto done;
2313 /* remove comments */
2314 *logmsg = malloc(st2.st_size + 1);
2315 if (*logmsg == NULL) {
2316 err = got_error_from_errno("malloc");
2317 goto done;
2319 len = 0;
2321 fp = fopen(a->logmsg_path, "r");
2322 while (fgets(buf, sizeof(buf), fp) != NULL) {
2323 if (!content_changed && strcmp(buf, initial_content) != 0)
2324 content_changed = 1;
2325 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2326 continue;
2327 len = strlcat(*logmsg, buf, st2.st_size);
2329 fclose(fp);
2331 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2332 (*logmsg)[len - 1] = '\0';
2333 len--;
2336 if (len == 0 || !content_changed) {
2337 unlink(a->logmsg_path);
2338 free(a->logmsg_path);
2339 a->logmsg_path = NULL;
2340 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2341 "commit message cannot be empty, aborting");
2342 goto done;
2344 done:
2345 free(template);
2347 /* Editor is done; we can now apply unveil(2) */
2348 if (err == NULL)
2349 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2350 return err;
2353 static const struct got_error *
2354 cmd_commit(int argc, char *argv[])
2356 const struct got_error *error = NULL;
2357 struct got_worktree *worktree = NULL;
2358 struct got_repository *repo = NULL;
2359 char *cwd = NULL, *path = NULL, *id_str = NULL;
2360 struct got_object_id *id = NULL;
2361 const char *logmsg = NULL;
2362 const char *got_author = getenv("GOT_AUTHOR");
2363 struct collect_commit_logmsg_arg cl_arg;
2364 char *editor = NULL;
2365 int ch;
2367 while ((ch = getopt(argc, argv, "m:")) != -1) {
2368 switch (ch) {
2369 case 'm':
2370 logmsg = optarg;
2371 break;
2372 default:
2373 usage_commit();
2374 /* NOTREACHED */
2378 argc -= optind;
2379 argv += optind;
2381 if (argc == 1) {
2382 path = realpath(argv[0], NULL);
2383 if (path == NULL) {
2384 error = got_error_from_errno2("realpath", argv[0]);
2385 goto done;
2387 got_path_strip_trailing_slashes(path);
2388 } else if (argc != 0)
2389 usage_commit();
2391 if (got_author == NULL) {
2392 /* TODO: Look current user up in password database */
2393 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2394 goto done;
2397 cwd = getcwd(NULL, 0);
2398 if (cwd == NULL) {
2399 error = got_error_from_errno("getcwd");
2400 goto done;
2402 error = got_worktree_open(&worktree, cwd);
2403 if (error)
2404 goto done;
2406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2407 if (error != NULL)
2408 goto done;
2411 * unveil(2) traverses exec(2); if an editor is used we have
2412 * to apply unveil after the log message has been written.
2414 if (logmsg == NULL || strlen(logmsg) == 0)
2415 error = get_editor(&editor);
2416 else
2417 error = apply_unveil(got_repo_get_path(repo), 0,
2418 got_worktree_get_root_path(worktree), 0);
2419 if (error)
2420 goto done;
2422 cl_arg.editor = editor;
2423 cl_arg.cmdline_log = logmsg;
2424 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2425 cl_arg.repo_path = got_repo_get_path(repo);
2426 cl_arg.logmsg_path = NULL;
2427 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2428 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2429 if (error) {
2430 if (cl_arg.logmsg_path)
2431 fprintf(stderr, "%s: log message preserved in %s\n",
2432 getprogname(), cl_arg.logmsg_path);
2433 goto done;
2436 if (cl_arg.logmsg_path)
2437 unlink(cl_arg.logmsg_path);
2439 error = got_object_id_str(&id_str, id);
2440 if (error)
2441 goto done;
2442 printf("created commit %s\n", id_str);
2443 done:
2444 if (repo)
2445 got_repo_close(repo);
2446 if (worktree)
2447 got_worktree_close(worktree);
2448 free(path);
2449 free(cwd);
2450 free(id_str);
2451 free(editor);
2452 return error;