Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 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/types.h>
20 #include <sys/stat.h>
21 #include <sys/param.h>
22 #include <sys/wait.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <locale.h>
29 #include <ctype.h>
30 #include <signal.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 #include <time.h>
37 #include <paths.h>
38 #include <regex.h>
39 #include <getopt.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_path.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
49 #include "got_diff.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
55 #ifndef nitems
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 #endif
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
62 static void
63 catch_sigint(int signo)
64 {
65 sigint_received = 1;
66 }
68 static void
69 catch_sigpipe(int signo)
70 {
71 sigpipe_received = 1;
72 }
75 struct got_cmd {
76 const char *cmd_name;
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
80 };
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
158 };
160 static void
161 list_commands(void)
163 int i;
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
170 fputc('\n', stderr);
173 int
174 main(int argc, char *argv[])
176 struct got_cmd *cmd;
177 unsigned int i;
178 int ch;
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
182 { NULL, 0, NULL, 0}
183 };
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 switch (ch) {
189 case 'h':
190 hflag = 1;
191 break;
192 case 'V':
193 Vflag = 1;
194 break;
195 default:
196 usage(hflag);
197 /* NOTREACHED */
201 argc -= optind;
202 argv += optind;
203 optind = 0;
205 if (Vflag) {
206 got_version_print_str();
207 return 1;
210 if (argc <= 0)
211 usage(hflag);
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
223 continue;
225 if (hflag)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 !(sigint_received &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 return 1;
239 return 0;
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 list_commands();
244 return 1;
247 __dead static void
248 usage(int hflag)
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 getprogname());
252 if (hflag)
253 list_commands();
254 exit(1);
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
261 const char *editor;
263 *abspath = NULL;
265 editor = getenv("VISUAL");
266 if (editor == NULL)
267 editor = getenv("EDITOR");
269 if (editor) {
270 err = got_path_find_prog(abspath, editor);
271 if (err)
272 return err;
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
281 return NULL;
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
290 #ifdef PROFILE
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
293 #endif
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
304 if (err != NULL)
305 return err;
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
310 return NULL;
313 __dead static void
314 usage_init(void)
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 exit(1);
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
325 int ch;
327 while ((ch = getopt(argc, argv, "")) != -1) {
328 switch (ch) {
329 default:
330 usage_init();
331 /* NOTREACHED */
335 argc -= optind;
336 argv += optind;
338 #ifndef PROFILE
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 err(1, "pledge");
341 #endif
342 if (argc != 1)
343 usage_init();
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
352 if (error &&
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 goto done;
356 error = apply_unveil(repo_path, 0, NULL);
357 if (error)
358 goto done;
360 error = got_repo_init(repo_path);
361 done:
362 free(repo_path);
363 return error;
366 __dead static void
367 usage_import(void)
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
371 exit(1);
374 int
375 spawn_editor(const char *editor, const char *file)
377 pid_t pid;
378 sig_t sighup, sigint, sigquit;
379 int st = -1;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
386 case -1:
387 goto doneediting;
388 case 0:
389 execl(editor, editor, file, (char *)NULL);
390 _exit(127);
393 while (waitpid(pid, &st, 0) == -1)
394 if (errno != EINTR)
395 break;
397 doneediting:
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
403 errno = EINTR;
404 return -1;
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
415 char buf[1024];
416 struct stat st, st2;
417 FILE *fp;
418 int content_changed = 0;
419 size_t len;
421 *logmsg = NULL;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
437 if (*logmsg == NULL)
438 return got_error_from_errno("malloc");
439 (*logmsg)[0] = '\0';
440 len = 0;
442 fp = fopen(logmsg_path, "r");
443 if (fp == NULL) {
444 err = got_error_from_errno("fopen");
445 goto done;
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
449 content_changed = 1;
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
454 fclose(fp);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
458 len--;
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
464 done:
465 if (err) {
466 free(*logmsg);
467 *logmsg = NULL;
469 return err;
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
478 int fd;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
482 branch_name) == -1)
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
486 if (err)
487 goto done;
489 dprintf(fd, initial_content);
490 close(fd);
492 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
493 done:
494 free(initial_content);
495 return err;
498 static const struct got_error *
499 import_progress(void *arg, const char *path)
501 printf("A %s\n", path);
502 return NULL;
505 static const struct got_error *
506 get_author(char **author, struct got_repository *repo)
508 const struct got_error *err = NULL;
509 const char *got_author, *name, *email;
511 *author = NULL;
513 name = got_repo_get_gitconfig_author_name(repo);
514 email = got_repo_get_gitconfig_author_email(repo);
515 if (name && email) {
516 if (asprintf(author, "%s <%s>", name, email) == -1)
517 return got_error_from_errno("asprintf");
518 return NULL;
521 got_author = getenv("GOT_AUTHOR");
522 if (got_author == NULL) {
523 name = got_repo_get_global_gitconfig_author_name(repo);
524 email = got_repo_get_global_gitconfig_author_email(repo);
525 if (name && email) {
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
528 return NULL;
530 /* TODO: Look up user in password database? */
531 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 *author = strdup(got_author);
535 if (*author == NULL)
536 return got_error_from_errno("strdup");
538 /*
539 * Really dumb email address check; we're only doing this to
540 * avoid git's object parser breaking on commits we create.
541 */
542 while (*got_author && *got_author != '<')
543 got_author++;
544 if (*got_author != '<') {
545 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 goto done;
548 while (*got_author && *got_author != '@')
549 got_author++;
550 if (*got_author != '@') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 goto done;
554 while (*got_author && *got_author != '>')
555 got_author++;
556 if (*got_author != '>')
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 done:
559 if (err) {
560 free(*author);
561 *author = NULL;
563 return err;
566 static const struct got_error *
567 get_gitconfig_path(char **gitconfig_path)
569 const char *homedir = getenv("HOME");
571 *gitconfig_path = NULL;
572 if (homedir) {
573 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 return got_error_from_errno("asprintf");
577 return NULL;
580 static const struct got_error *
581 cmd_import(int argc, char *argv[])
583 const struct got_error *error = NULL;
584 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 const char *branch_name = "main";
587 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 struct got_repository *repo = NULL;
589 struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 struct got_object_id *new_commit_id = NULL;
591 int ch;
592 struct got_pathlist_head ignores;
593 struct got_pathlist_entry *pe;
594 int preserve_logmsg = 0;
596 TAILQ_INIT(&ignores);
598 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 switch (ch) {
600 case 'b':
601 branch_name = optarg;
602 break;
603 case 'm':
604 logmsg = strdup(optarg);
605 if (logmsg == NULL) {
606 error = got_error_from_errno("strdup");
607 goto done;
609 break;
610 case 'r':
611 repo_path = realpath(optarg, NULL);
612 if (repo_path == NULL) {
613 error = got_error_from_errno2("realpath",
614 optarg);
615 goto done;
617 break;
618 case 'I':
619 if (optarg[0] == '\0')
620 break;
621 error = got_pathlist_insert(&pe, &ignores, optarg,
622 NULL);
623 if (error)
624 goto done;
625 break;
626 default:
627 usage_import();
628 /* NOTREACHED */
632 argc -= optind;
633 argv += optind;
635 #ifndef PROFILE
636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
637 "unveil",
638 NULL) == -1)
639 err(1, "pledge");
640 #endif
641 if (argc != 1)
642 usage_import();
644 if (repo_path == NULL) {
645 repo_path = getcwd(NULL, 0);
646 if (repo_path == NULL)
647 return got_error_from_errno("getcwd");
649 got_path_strip_trailing_slashes(repo_path);
650 error = get_gitconfig_path(&gitconfig_path);
651 if (error)
652 goto done;
653 error = got_repo_open(&repo, repo_path, gitconfig_path);
654 if (error)
655 goto done;
657 error = get_author(&author, repo);
658 if (error)
659 return error;
661 /*
662 * Don't let the user create a branch name with a leading '-'.
663 * While technically a valid reference name, this case is usually
664 * an unintended typo.
665 */
666 if (branch_name[0] == '-')
667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 error = got_error_from_errno("asprintf");
671 goto done;
674 error = got_ref_open(&branch_ref, repo, refname, 0);
675 if (error) {
676 if (error->code != GOT_ERR_NOT_REF)
677 goto done;
678 } else {
679 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 "import target branch already exists");
681 goto done;
684 path_dir = realpath(argv[0], NULL);
685 if (path_dir == NULL) {
686 error = got_error_from_errno2("realpath", argv[0]);
687 goto done;
689 got_path_strip_trailing_slashes(path_dir);
691 /*
692 * unveil(2) traverses exec(2); if an editor is used we have
693 * to apply unveil after the log message has been written.
694 */
695 if (logmsg == NULL || strlen(logmsg) == 0) {
696 error = get_editor(&editor);
697 if (error)
698 goto done;
699 free(logmsg);
700 error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 path_dir, refname);
702 if (error) {
703 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
704 logmsg_path != NULL)
705 preserve_logmsg = 1;
706 goto done;
710 if (unveil(path_dir, "r") != 0) {
711 error = got_error_from_errno2("unveil", path_dir);
712 if (logmsg_path)
713 preserve_logmsg = 1;
714 goto done;
717 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
718 if (error) {
719 if (logmsg_path)
720 preserve_logmsg = 1;
721 goto done;
724 error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 author, &ignores, repo, import_progress, NULL);
726 if (error) {
727 if (logmsg_path)
728 preserve_logmsg = 1;
729 goto done;
732 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
733 if (error) {
734 if (logmsg_path)
735 preserve_logmsg = 1;
736 goto done;
739 error = got_ref_write(branch_ref, repo);
740 if (error) {
741 if (logmsg_path)
742 preserve_logmsg = 1;
743 goto done;
746 error = got_object_id_str(&id_str, new_commit_id);
747 if (error) {
748 if (logmsg_path)
749 preserve_logmsg = 1;
750 goto done;
753 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
754 if (error) {
755 if (error->code != GOT_ERR_NOT_REF) {
756 if (logmsg_path)
757 preserve_logmsg = 1;
758 goto done;
761 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
762 branch_ref);
763 if (error) {
764 if (logmsg_path)
765 preserve_logmsg = 1;
766 goto done;
769 error = got_ref_write(head_ref, repo);
770 if (error) {
771 if (logmsg_path)
772 preserve_logmsg = 1;
773 goto done;
777 printf("Created branch %s with commit %s\n",
778 got_ref_get_name(branch_ref), id_str);
779 done:
780 if (preserve_logmsg) {
781 fprintf(stderr, "%s: log message preserved in %s\n",
782 getprogname(), logmsg_path);
783 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 error = got_error_from_errno2("unlink", logmsg_path);
785 free(logmsg);
786 free(logmsg_path);
787 free(repo_path);
788 free(editor);
789 free(refname);
790 free(new_commit_id);
791 free(id_str);
792 free(author);
793 free(gitconfig_path);
794 if (branch_ref)
795 got_ref_close(branch_ref);
796 if (head_ref)
797 got_ref_close(head_ref);
798 return error;
801 __dead static void
802 usage_checkout(void)
804 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 "[-p prefix] repository-path [worktree-path]\n", getprogname());
806 exit(1);
809 static void
810 show_worktree_base_ref_warning(void)
812 fprintf(stderr, "%s: warning: could not create a reference "
813 "to the work tree's base commit; the commit could be "
814 "garbage-collected by Git; making the repository "
815 "writable and running 'got update' will prevent this\n",
816 getprogname());
819 struct got_checkout_progress_arg {
820 const char *worktree_path;
821 int had_base_commit_ref_error;
822 };
824 static const struct got_error *
825 checkout_progress(void *arg, unsigned char status, const char *path)
827 struct got_checkout_progress_arg *a = arg;
829 /* Base commit bump happens silently. */
830 if (status == GOT_STATUS_BUMP_BASE)
831 return NULL;
833 if (status == GOT_STATUS_BASE_REF_ERR) {
834 a->had_base_commit_ref_error = 1;
835 return NULL;
838 while (path[0] == '/')
839 path++;
841 printf("%c %s/%s\n", status, a->worktree_path, path);
842 return NULL;
845 static const struct got_error *
846 check_cancelled(void *arg)
848 if (sigint_received || sigpipe_received)
849 return got_error(GOT_ERR_CANCELLED);
850 return NULL;
853 static const struct got_error *
854 check_linear_ancestry(struct got_object_id *commit_id,
855 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 struct got_repository *repo)
858 const struct got_error *err = NULL;
859 struct got_object_id *yca_id;
861 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 commit_id, base_commit_id, repo, check_cancelled, NULL);
863 if (err)
864 return err;
866 if (yca_id == NULL)
867 return got_error(GOT_ERR_ANCESTRY);
869 /*
870 * Require a straight line of history between the target commit
871 * and the work tree's base commit.
873 * Non-linear situations such as this require a rebase:
875 * (commit) D F (base_commit)
876 * \ /
877 * C E
878 * \ /
879 * B (yca)
880 * |
881 * A
883 * 'got update' only handles linear cases:
884 * Update forwards in time: A (base/yca) - B - C - D (commit)
885 * Update backwards in time: D (base) - C - B - A (commit/yca)
886 */
887 if (allow_forwards_in_time_only) {
888 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 return got_error(GOT_ERR_ANCESTRY);
890 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
894 free(yca_id);
895 return NULL;
898 static const struct got_error *
899 check_same_branch(struct got_object_id *commit_id,
900 struct got_reference *head_ref, struct got_object_id *yca_id,
901 struct got_repository *repo)
903 const struct got_error *err = NULL;
904 struct got_commit_graph *graph = NULL;
905 struct got_object_id *head_commit_id = NULL;
906 int is_same_branch = 0;
908 err = got_ref_resolve(&head_commit_id, repo, head_ref);
909 if (err)
910 goto done;
912 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
913 is_same_branch = 1;
914 goto done;
916 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
917 is_same_branch = 1;
918 goto done;
921 err = got_commit_graph_open(&graph, "/", 1);
922 if (err)
923 goto done;
925 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 check_cancelled, NULL);
927 if (err)
928 goto done;
930 for (;;) {
931 struct got_object_id *id;
932 err = got_commit_graph_iter_next(&id, graph, repo,
933 check_cancelled, NULL);
934 if (err) {
935 if (err->code == GOT_ERR_ITER_COMPLETED)
936 err = NULL;
937 break;
940 if (id) {
941 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
942 break;
943 if (got_object_id_cmp(id, commit_id) == 0) {
944 is_same_branch = 1;
945 break;
949 done:
950 if (graph)
951 got_commit_graph_close(graph);
952 free(head_commit_id);
953 if (!err && !is_same_branch)
954 err = got_error(GOT_ERR_ANCESTRY);
955 return err;
958 static const struct got_error *
959 cmd_checkout(int argc, char *argv[])
961 const struct got_error *error = NULL;
962 struct got_repository *repo = NULL;
963 struct got_reference *head_ref = NULL;
964 struct got_worktree *worktree = NULL;
965 char *repo_path = NULL;
966 char *worktree_path = NULL;
967 const char *path_prefix = "";
968 const char *branch_name = GOT_REF_HEAD;
969 char *commit_id_str = NULL;
970 int ch, same_path_prefix, allow_nonempty = 0;
971 struct got_pathlist_head paths;
972 struct got_checkout_progress_arg cpa;
974 TAILQ_INIT(&paths);
976 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
977 switch (ch) {
978 case 'b':
979 branch_name = optarg;
980 break;
981 case 'c':
982 commit_id_str = strdup(optarg);
983 if (commit_id_str == NULL)
984 return got_error_from_errno("strdup");
985 break;
986 case 'E':
987 allow_nonempty = 1;
988 break;
989 case 'p':
990 path_prefix = optarg;
991 break;
992 default:
993 usage_checkout();
994 /* NOTREACHED */
998 argc -= optind;
999 argv += optind;
1001 #ifndef PROFILE
1002 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1003 "unveil", NULL) == -1)
1004 err(1, "pledge");
1005 #endif
1006 if (argc == 1) {
1007 char *cwd, *base, *dotgit;
1008 repo_path = realpath(argv[0], NULL);
1009 if (repo_path == NULL)
1010 return got_error_from_errno2("realpath", argv[0]);
1011 cwd = getcwd(NULL, 0);
1012 if (cwd == NULL) {
1013 error = got_error_from_errno("getcwd");
1014 goto done;
1016 if (path_prefix[0]) {
1017 base = basename(path_prefix);
1018 if (base == NULL) {
1019 error = got_error_from_errno2("basename",
1020 path_prefix);
1021 goto done;
1023 } else {
1024 base = basename(repo_path);
1025 if (base == NULL) {
1026 error = got_error_from_errno2("basename",
1027 repo_path);
1028 goto done;
1031 dotgit = strstr(base, ".git");
1032 if (dotgit)
1033 *dotgit = '\0';
1034 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1035 error = got_error_from_errno("asprintf");
1036 free(cwd);
1037 goto done;
1039 free(cwd);
1040 } else if (argc == 2) {
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL) {
1043 error = got_error_from_errno2("realpath", argv[0]);
1044 goto done;
1046 worktree_path = realpath(argv[1], NULL);
1047 if (worktree_path == NULL) {
1048 if (errno != ENOENT) {
1049 error = got_error_from_errno2("realpath",
1050 argv[1]);
1051 goto done;
1053 worktree_path = strdup(argv[1]);
1054 if (worktree_path == NULL) {
1055 error = got_error_from_errno("strdup");
1056 goto done;
1059 } else
1060 usage_checkout();
1062 got_path_strip_trailing_slashes(repo_path);
1063 got_path_strip_trailing_slashes(worktree_path);
1065 error = got_repo_open(&repo, repo_path, NULL);
1066 if (error != NULL)
1067 goto done;
1069 /* Pre-create work tree path for unveil(2) */
1070 error = got_path_mkdir(worktree_path);
1071 if (error) {
1072 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1073 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1074 goto done;
1075 if (!allow_nonempty &&
1076 !got_path_dir_is_empty(worktree_path)) {
1077 error = got_error_path(worktree_path,
1078 GOT_ERR_DIR_NOT_EMPTY);
1079 goto done;
1083 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1084 if (error)
1085 goto done;
1087 error = got_ref_open(&head_ref, repo, branch_name, 0);
1088 if (error != NULL)
1089 goto done;
1091 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1092 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1093 goto done;
1095 error = got_worktree_open(&worktree, worktree_path);
1096 if (error != NULL)
1097 goto done;
1099 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1100 path_prefix);
1101 if (error != NULL)
1102 goto done;
1103 if (!same_path_prefix) {
1104 error = got_error(GOT_ERR_PATH_PREFIX);
1105 goto done;
1108 if (commit_id_str) {
1109 struct got_object_id *commit_id;
1110 error = got_repo_match_object_id(&commit_id, NULL,
1111 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1112 if (error)
1113 goto done;
1114 error = check_linear_ancestry(commit_id,
1115 got_worktree_get_base_commit_id(worktree), 0, repo);
1116 if (error != NULL) {
1117 free(commit_id);
1118 goto done;
1120 error = check_same_branch(commit_id, head_ref, NULL, repo);
1121 if (error)
1122 goto done;
1123 error = got_worktree_set_base_commit_id(worktree, repo,
1124 commit_id);
1125 free(commit_id);
1126 if (error)
1127 goto done;
1130 error = got_pathlist_append(&paths, "", NULL);
1131 if (error)
1132 goto done;
1133 cpa.worktree_path = worktree_path;
1134 cpa.had_base_commit_ref_error = 0;
1135 error = got_worktree_checkout_files(worktree, &paths, repo,
1136 checkout_progress, &cpa, check_cancelled, NULL);
1137 if (error != NULL)
1138 goto done;
1140 printf("Now shut up and hack\n");
1141 if (cpa.had_base_commit_ref_error)
1142 show_worktree_base_ref_warning();
1143 done:
1144 got_pathlist_free(&paths);
1145 free(commit_id_str);
1146 free(repo_path);
1147 free(worktree_path);
1148 return error;
1151 __dead static void
1152 usage_update(void)
1154 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1155 getprogname());
1156 exit(1);
1159 static const struct got_error *
1160 update_progress(void *arg, unsigned char status, const char *path)
1162 int *did_something = arg;
1164 if (status == GOT_STATUS_EXISTS ||
1165 status == GOT_STATUS_BASE_REF_ERR)
1166 return NULL;
1168 *did_something = 1;
1170 /* Base commit bump happens silently. */
1171 if (status == GOT_STATUS_BUMP_BASE)
1172 return NULL;
1174 while (path[0] == '/')
1175 path++;
1176 printf("%c %s\n", status, path);
1177 return NULL;
1180 static const struct got_error *
1181 switch_head_ref(struct got_reference *head_ref,
1182 struct got_object_id *commit_id, struct got_worktree *worktree,
1183 struct got_repository *repo)
1185 const struct got_error *err = NULL;
1186 char *base_id_str;
1187 int ref_has_moved = 0;
1189 /* Trivial case: switching between two different references. */
1190 if (strcmp(got_ref_get_name(head_ref),
1191 got_worktree_get_head_ref_name(worktree)) != 0) {
1192 printf("Switching work tree from %s to %s\n",
1193 got_worktree_get_head_ref_name(worktree),
1194 got_ref_get_name(head_ref));
1195 return got_worktree_set_head_ref(worktree, head_ref);
1198 err = check_linear_ancestry(commit_id,
1199 got_worktree_get_base_commit_id(worktree), 0, repo);
1200 if (err) {
1201 if (err->code != GOT_ERR_ANCESTRY)
1202 return err;
1203 ref_has_moved = 1;
1205 if (!ref_has_moved)
1206 return NULL;
1208 /* Switching to a rebased branch with the same reference name. */
1209 err = got_object_id_str(&base_id_str,
1210 got_worktree_get_base_commit_id(worktree));
1211 if (err)
1212 return err;
1213 printf("Reference %s now points at a different branch\n",
1214 got_worktree_get_head_ref_name(worktree));
1215 printf("Switching work tree from %s to %s\n", base_id_str,
1216 got_worktree_get_head_ref_name(worktree));
1217 return NULL;
1220 static const struct got_error *
1221 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1223 const struct got_error *err;
1224 int in_progress;
1226 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1227 if (err)
1228 return err;
1229 if (in_progress)
1230 return got_error(GOT_ERR_REBASING);
1232 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1233 if (err)
1234 return err;
1235 if (in_progress)
1236 return got_error(GOT_ERR_HISTEDIT_BUSY);
1238 return NULL;
1241 static const struct got_error *
1242 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1243 char *argv[], struct got_worktree *worktree)
1245 const struct got_error *err = NULL;
1246 char *path;
1247 int i;
1249 if (argc == 0) {
1250 path = strdup("");
1251 if (path == NULL)
1252 return got_error_from_errno("strdup");
1253 return got_pathlist_append(paths, path, NULL);
1256 for (i = 0; i < argc; i++) {
1257 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1258 if (err)
1259 break;
1260 err = got_pathlist_append(paths, path, NULL);
1261 if (err) {
1262 free(path);
1263 break;
1267 return err;
1270 static const struct got_error *
1271 cmd_update(int argc, char *argv[])
1273 const struct got_error *error = NULL;
1274 struct got_repository *repo = NULL;
1275 struct got_worktree *worktree = NULL;
1276 char *worktree_path = NULL;
1277 struct got_object_id *commit_id = NULL;
1278 char *commit_id_str = NULL;
1279 const char *branch_name = NULL;
1280 struct got_reference *head_ref = NULL;
1281 struct got_pathlist_head paths;
1282 struct got_pathlist_entry *pe;
1283 int ch, did_something = 0;
1285 TAILQ_INIT(&paths);
1287 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1288 switch (ch) {
1289 case 'b':
1290 branch_name = optarg;
1291 break;
1292 case 'c':
1293 commit_id_str = strdup(optarg);
1294 if (commit_id_str == NULL)
1295 return got_error_from_errno("strdup");
1296 break;
1297 default:
1298 usage_update();
1299 /* NOTREACHED */
1303 argc -= optind;
1304 argv += optind;
1306 #ifndef PROFILE
1307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1308 "unveil", NULL) == -1)
1309 err(1, "pledge");
1310 #endif
1311 worktree_path = getcwd(NULL, 0);
1312 if (worktree_path == NULL) {
1313 error = got_error_from_errno("getcwd");
1314 goto done;
1316 error = got_worktree_open(&worktree, worktree_path);
1317 if (error)
1318 goto done;
1320 error = check_rebase_or_histedit_in_progress(worktree);
1321 if (error)
1322 goto done;
1324 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1325 NULL);
1326 if (error != NULL)
1327 goto done;
1329 error = apply_unveil(got_repo_get_path(repo), 0,
1330 got_worktree_get_root_path(worktree));
1331 if (error)
1332 goto done;
1334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1335 if (error)
1336 goto done;
1338 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1339 got_worktree_get_head_ref_name(worktree), 0);
1340 if (error != NULL)
1341 goto done;
1342 if (commit_id_str == NULL) {
1343 error = got_ref_resolve(&commit_id, repo, head_ref);
1344 if (error != NULL)
1345 goto done;
1346 error = got_object_id_str(&commit_id_str, commit_id);
1347 if (error != NULL)
1348 goto done;
1349 } else {
1350 error = got_repo_match_object_id(&commit_id, NULL,
1351 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1352 free(commit_id_str);
1353 commit_id_str = NULL;
1354 if (error)
1355 goto done;
1356 error = got_object_id_str(&commit_id_str, commit_id);
1357 if (error)
1358 goto done;
1361 if (branch_name) {
1362 struct got_object_id *head_commit_id;
1363 TAILQ_FOREACH(pe, &paths, entry) {
1364 if (pe->path_len == 0)
1365 continue;
1366 error = got_error_msg(GOT_ERR_BAD_PATH,
1367 "switching between branches requires that "
1368 "the entire work tree gets updated");
1369 goto done;
1371 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1372 if (error)
1373 goto done;
1374 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1375 repo);
1376 free(head_commit_id);
1377 if (error != NULL)
1378 goto done;
1379 error = check_same_branch(commit_id, head_ref, NULL, repo);
1380 if (error)
1381 goto done;
1382 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1383 if (error)
1384 goto done;
1385 } else {
1386 error = check_linear_ancestry(commit_id,
1387 got_worktree_get_base_commit_id(worktree), 0, repo);
1388 if (error != NULL) {
1389 if (error->code == GOT_ERR_ANCESTRY)
1390 error = got_error(GOT_ERR_BRANCH_MOVED);
1391 goto done;
1393 error = check_same_branch(commit_id, head_ref, NULL, repo);
1394 if (error)
1395 goto done;
1398 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1399 commit_id) != 0) {
1400 error = got_worktree_set_base_commit_id(worktree, repo,
1401 commit_id);
1402 if (error)
1403 goto done;
1406 error = got_worktree_checkout_files(worktree, &paths, repo,
1407 update_progress, &did_something, check_cancelled, NULL);
1408 if (error != NULL)
1409 goto done;
1411 if (did_something)
1412 printf("Updated to commit %s\n", commit_id_str);
1413 else
1414 printf("Already up-to-date\n");
1415 done:
1416 free(worktree_path);
1417 TAILQ_FOREACH(pe, &paths, entry)
1418 free((char *)pe->path);
1419 got_pathlist_free(&paths);
1420 free(commit_id);
1421 free(commit_id_str);
1422 return error;
1425 static const struct got_error *
1426 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1427 const char *path, int diff_context, int ignore_whitespace,
1428 struct got_repository *repo)
1430 const struct got_error *err = NULL;
1431 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1433 if (blob_id1) {
1434 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1435 if (err)
1436 goto done;
1439 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1440 if (err)
1441 goto done;
1443 while (path[0] == '/')
1444 path++;
1445 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1446 ignore_whitespace, stdout);
1447 done:
1448 if (blob1)
1449 got_object_blob_close(blob1);
1450 got_object_blob_close(blob2);
1451 return err;
1454 static const struct got_error *
1455 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1456 const char *path, int diff_context, int ignore_whitespace,
1457 struct got_repository *repo)
1459 const struct got_error *err = NULL;
1460 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1461 struct got_diff_blob_output_unidiff_arg arg;
1463 if (tree_id1) {
1464 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1465 if (err)
1466 goto done;
1469 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1470 if (err)
1471 goto done;
1473 arg.diff_context = diff_context;
1474 arg.ignore_whitespace = ignore_whitespace;
1475 arg.outfile = stdout;
1476 while (path[0] == '/')
1477 path++;
1478 err = got_diff_tree(tree1, tree2, path, path, repo,
1479 got_diff_blob_output_unidiff, &arg, 1);
1480 done:
1481 if (tree1)
1482 got_object_tree_close(tree1);
1483 if (tree2)
1484 got_object_tree_close(tree2);
1485 return err;
1488 static const struct got_error *
1489 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1490 const char *path, int diff_context, struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 struct got_commit_object *pcommit = NULL;
1494 char *id_str1 = NULL, *id_str2 = NULL;
1495 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1496 struct got_object_qid *qid;
1498 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1499 if (qid != NULL) {
1500 err = got_object_open_as_commit(&pcommit, repo,
1501 qid->id);
1502 if (err)
1503 return err;
1506 if (path && path[0] != '\0') {
1507 int obj_type;
1508 err = got_object_id_by_path(&obj_id2, repo, id, path);
1509 if (err)
1510 goto done;
1511 err = got_object_id_str(&id_str2, obj_id2);
1512 if (err) {
1513 free(obj_id2);
1514 goto done;
1516 if (pcommit) {
1517 err = got_object_id_by_path(&obj_id1, repo,
1518 qid->id, path);
1519 if (err) {
1520 free(obj_id2);
1521 goto done;
1523 err = got_object_id_str(&id_str1, obj_id1);
1524 if (err) {
1525 free(obj_id2);
1526 goto done;
1529 err = got_object_get_type(&obj_type, repo, obj_id2);
1530 if (err) {
1531 free(obj_id2);
1532 goto done;
1534 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1535 switch (obj_type) {
1536 case GOT_OBJ_TYPE_BLOB:
1537 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1538 0, repo);
1539 break;
1540 case GOT_OBJ_TYPE_TREE:
1541 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1542 0, repo);
1543 break;
1544 default:
1545 err = got_error(GOT_ERR_OBJ_TYPE);
1546 break;
1548 free(obj_id1);
1549 free(obj_id2);
1550 } else {
1551 obj_id2 = got_object_commit_get_tree_id(commit);
1552 err = got_object_id_str(&id_str2, obj_id2);
1553 if (err)
1554 goto done;
1555 obj_id1 = got_object_commit_get_tree_id(pcommit);
1556 err = got_object_id_str(&id_str1, obj_id1);
1557 if (err)
1558 goto done;
1559 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1560 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1562 done:
1563 free(id_str1);
1564 free(id_str2);
1565 if (pcommit)
1566 got_object_commit_close(pcommit);
1567 return err;
1570 static char *
1571 get_datestr(time_t *time, char *datebuf)
1573 struct tm mytm, *tm;
1574 char *p, *s;
1576 tm = gmtime_r(time, &mytm);
1577 if (tm == NULL)
1578 return NULL;
1579 s = asctime_r(tm, datebuf);
1580 if (s == NULL)
1581 return NULL;
1582 p = strchr(s, '\n');
1583 if (p)
1584 *p = '\0';
1585 return s;
1588 static const struct got_error *
1589 match_logmsg(int *have_match, struct got_object_id *id,
1590 struct got_commit_object *commit, regex_t *regex)
1592 const struct got_error *err = NULL;
1593 regmatch_t regmatch;
1594 char *id_str = NULL, *logmsg = NULL;
1596 *have_match = 0;
1598 err = got_object_id_str(&id_str, id);
1599 if (err)
1600 return err;
1602 err = got_object_commit_get_logmsg(&logmsg, commit);
1603 if (err)
1604 goto done;
1606 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1607 *have_match = 1;
1608 done:
1609 free(id_str);
1610 free(logmsg);
1611 return err;
1614 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1616 static const struct got_error *
1617 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1618 struct got_repository *repo, const char *path, int show_patch,
1619 int diff_context, struct got_reflist_head *refs)
1621 const struct got_error *err = NULL;
1622 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1623 char datebuf[26];
1624 time_t committer_time;
1625 const char *author, *committer;
1626 char *refs_str = NULL;
1627 struct got_reflist_entry *re;
1629 SIMPLEQ_FOREACH(re, refs, entry) {
1630 char *s;
1631 const char *name;
1632 struct got_tag_object *tag = NULL;
1633 int cmp;
1635 name = got_ref_get_name(re->ref);
1636 if (strcmp(name, GOT_REF_HEAD) == 0)
1637 continue;
1638 if (strncmp(name, "refs/", 5) == 0)
1639 name += 5;
1640 if (strncmp(name, "got/", 4) == 0)
1641 continue;
1642 if (strncmp(name, "heads/", 6) == 0)
1643 name += 6;
1644 if (strncmp(name, "remotes/", 8) == 0)
1645 name += 8;
1646 if (strncmp(name, "tags/", 5) == 0) {
1647 err = got_object_open_as_tag(&tag, repo, re->id);
1648 if (err) {
1649 if (err->code != GOT_ERR_OBJ_TYPE)
1650 return err;
1651 /* Ref points at something other than a tag. */
1652 err = NULL;
1653 tag = NULL;
1656 cmp = got_object_id_cmp(tag ?
1657 got_object_tag_get_object_id(tag) : re->id, id);
1658 if (tag)
1659 got_object_tag_close(tag);
1660 if (cmp != 0)
1661 continue;
1662 s = refs_str;
1663 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1664 name) == -1) {
1665 err = got_error_from_errno("asprintf");
1666 free(s);
1667 return err;
1669 free(s);
1671 err = got_object_id_str(&id_str, id);
1672 if (err)
1673 return err;
1675 printf(GOT_COMMIT_SEP_STR);
1676 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1677 refs_str ? refs_str : "", refs_str ? ")" : "");
1678 free(id_str);
1679 id_str = NULL;
1680 free(refs_str);
1681 refs_str = NULL;
1682 printf("from: %s\n", got_object_commit_get_author(commit));
1683 committer_time = got_object_commit_get_committer_time(commit);
1684 datestr = get_datestr(&committer_time, datebuf);
1685 if (datestr)
1686 printf("date: %s UTC\n", datestr);
1687 author = got_object_commit_get_author(commit);
1688 committer = got_object_commit_get_committer(commit);
1689 if (strcmp(author, committer) != 0)
1690 printf("via: %s\n", committer);
1691 if (got_object_commit_get_nparents(commit) > 1) {
1692 const struct got_object_id_queue *parent_ids;
1693 struct got_object_qid *qid;
1694 int n = 1;
1695 parent_ids = got_object_commit_get_parent_ids(commit);
1696 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1697 err = got_object_id_str(&id_str, qid->id);
1698 if (err)
1699 return err;
1700 printf("parent %d: %s\n", n++, id_str);
1701 free(id_str);
1705 err = got_object_commit_get_logmsg(&logmsg0, commit);
1706 if (err)
1707 return err;
1709 logmsg = logmsg0;
1710 do {
1711 line = strsep(&logmsg, "\n");
1712 if (line)
1713 printf(" %s\n", line);
1714 } while (line);
1715 free(logmsg0);
1717 if (show_patch) {
1718 err = print_patch(commit, id, path, diff_context, repo);
1719 if (err == 0)
1720 printf("\n");
1723 if (fflush(stdout) != 0 && err == NULL)
1724 err = got_error_from_errno("fflush");
1725 return err;
1728 static const struct got_error *
1729 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1730 const char *path, int show_patch, const char *search_pattern,
1731 int diff_context, int limit, int log_branches,
1732 struct got_reflist_head *refs)
1734 const struct got_error *err;
1735 struct got_commit_graph *graph;
1736 regex_t regex;
1737 int have_match;
1739 if (search_pattern &&
1740 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1741 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1743 err = got_commit_graph_open(&graph, path, !log_branches);
1744 if (err)
1745 return err;
1746 err = got_commit_graph_iter_start(graph, root_id, repo,
1747 check_cancelled, NULL);
1748 if (err)
1749 goto done;
1750 for (;;) {
1751 struct got_commit_object *commit;
1752 struct got_object_id *id;
1754 if (sigint_received || sigpipe_received)
1755 break;
1757 err = got_commit_graph_iter_next(&id, graph, repo,
1758 check_cancelled, NULL);
1759 if (err) {
1760 if (err->code == GOT_ERR_ITER_COMPLETED)
1761 err = NULL;
1762 break;
1764 if (id == NULL)
1765 break;
1767 err = got_object_open_as_commit(&commit, repo, id);
1768 if (err)
1769 break;
1771 if (search_pattern) {
1772 err = match_logmsg(&have_match, id, commit, &regex);
1773 if (err) {
1774 got_object_commit_close(commit);
1775 break;
1777 if (have_match == 0) {
1778 got_object_commit_close(commit);
1779 continue;
1783 err = print_commit(commit, id, repo, path, show_patch,
1784 diff_context, refs);
1785 got_object_commit_close(commit);
1786 if (err || (limit && --limit == 0))
1787 break;
1789 done:
1790 if (search_pattern)
1791 regfree(&regex);
1792 got_commit_graph_close(graph);
1793 return err;
1796 __dead static void
1797 usage_log(void)
1799 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1800 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1801 exit(1);
1804 static int
1805 get_default_log_limit(void)
1807 const char *got_default_log_limit;
1808 long long n;
1809 const char *errstr;
1811 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1812 if (got_default_log_limit == NULL)
1813 return 0;
1814 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1815 if (errstr != NULL)
1816 return 0;
1817 return n;
1820 static const struct got_error *
1821 cmd_log(int argc, char *argv[])
1823 const struct got_error *error;
1824 struct got_repository *repo = NULL;
1825 struct got_worktree *worktree = NULL;
1826 struct got_commit_object *commit = NULL;
1827 struct got_object_id *id = NULL;
1828 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1829 const char *start_commit = NULL, *search_pattern = NULL;
1830 int diff_context = -1, ch;
1831 int show_patch = 0, limit = 0, log_branches = 0;
1832 const char *errstr;
1833 struct got_reflist_head refs;
1835 SIMPLEQ_INIT(&refs);
1837 #ifndef PROFILE
1838 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1839 NULL)
1840 == -1)
1841 err(1, "pledge");
1842 #endif
1844 limit = get_default_log_limit();
1846 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1847 switch (ch) {
1848 case 'p':
1849 show_patch = 1;
1850 break;
1851 case 'c':
1852 start_commit = optarg;
1853 break;
1854 case 'C':
1855 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1856 &errstr);
1857 if (errstr != NULL)
1858 err(1, "-C option %s", errstr);
1859 break;
1860 case 'l':
1861 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1862 if (errstr != NULL)
1863 err(1, "-l option %s", errstr);
1864 break;
1865 case 'b':
1866 log_branches = 1;
1867 break;
1868 case 'r':
1869 repo_path = realpath(optarg, NULL);
1870 if (repo_path == NULL)
1871 return got_error_from_errno2("realpath",
1872 optarg);
1873 got_path_strip_trailing_slashes(repo_path);
1874 break;
1875 case 's':
1876 search_pattern = optarg;
1877 break;
1878 default:
1879 usage_log();
1880 /* NOTREACHED */
1884 argc -= optind;
1885 argv += optind;
1887 if (diff_context == -1)
1888 diff_context = 3;
1889 else if (!show_patch)
1890 errx(1, "-C reguires -p");
1892 cwd = getcwd(NULL, 0);
1893 if (cwd == NULL) {
1894 error = got_error_from_errno("getcwd");
1895 goto done;
1898 error = got_worktree_open(&worktree, cwd);
1899 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1900 goto done;
1901 error = NULL;
1903 if (argc == 0) {
1904 path = strdup("");
1905 if (path == NULL) {
1906 error = got_error_from_errno("strdup");
1907 goto done;
1909 } else if (argc == 1) {
1910 if (worktree) {
1911 error = got_worktree_resolve_path(&path, worktree,
1912 argv[0]);
1913 if (error)
1914 goto done;
1915 } else {
1916 path = strdup(argv[0]);
1917 if (path == NULL) {
1918 error = got_error_from_errno("strdup");
1919 goto done;
1922 } else
1923 usage_log();
1925 if (repo_path == NULL) {
1926 repo_path = worktree ?
1927 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1929 if (repo_path == NULL) {
1930 error = got_error_from_errno("strdup");
1931 goto done;
1934 error = got_repo_open(&repo, repo_path, NULL);
1935 if (error != NULL)
1936 goto done;
1938 error = apply_unveil(got_repo_get_path(repo), 1,
1939 worktree ? got_worktree_get_root_path(worktree) : NULL);
1940 if (error)
1941 goto done;
1943 if (start_commit == NULL) {
1944 struct got_reference *head_ref;
1945 error = got_ref_open(&head_ref, repo,
1946 worktree ? got_worktree_get_head_ref_name(worktree)
1947 : GOT_REF_HEAD, 0);
1948 if (error != NULL)
1949 return error;
1950 error = got_ref_resolve(&id, repo, head_ref);
1951 got_ref_close(head_ref);
1952 if (error != NULL)
1953 return error;
1954 error = got_object_open_as_commit(&commit, repo, id);
1955 } else {
1956 struct got_reference *ref;
1957 error = got_ref_open(&ref, repo, start_commit, 0);
1958 if (error == NULL) {
1959 int obj_type;
1960 error = got_ref_resolve(&id, repo, ref);
1961 got_ref_close(ref);
1962 if (error != NULL)
1963 goto done;
1964 error = got_object_get_type(&obj_type, repo, id);
1965 if (error != NULL)
1966 goto done;
1967 if (obj_type == GOT_OBJ_TYPE_TAG) {
1968 struct got_tag_object *tag;
1969 error = got_object_open_as_tag(&tag, repo, id);
1970 if (error != NULL)
1971 goto done;
1972 if (got_object_tag_get_object_type(tag) !=
1973 GOT_OBJ_TYPE_COMMIT) {
1974 got_object_tag_close(tag);
1975 error = got_error(GOT_ERR_OBJ_TYPE);
1976 goto done;
1978 free(id);
1979 id = got_object_id_dup(
1980 got_object_tag_get_object_id(tag));
1981 if (id == NULL)
1982 error = got_error_from_errno(
1983 "got_object_id_dup");
1984 got_object_tag_close(tag);
1985 if (error)
1986 goto done;
1987 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1989 goto done;
1991 error = got_object_open_as_commit(&commit, repo, id);
1992 if (error != NULL)
1993 goto done;
1995 if (commit == NULL) {
1996 error = got_repo_match_object_id_prefix(&id,
1997 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1998 if (error != NULL)
1999 return error;
2002 if (error != NULL)
2003 goto done;
2005 if (worktree) {
2006 const char *prefix = got_worktree_get_path_prefix(worktree);
2007 char *p;
2008 if (asprintf(&p, "%s%s%s", prefix,
2009 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2010 error = got_error_from_errno("asprintf");
2011 goto done;
2013 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2014 free(p);
2015 } else
2016 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2017 if (error != NULL)
2018 goto done;
2019 if (in_repo_path) {
2020 free(path);
2021 path = in_repo_path;
2024 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2025 if (error)
2026 goto done;
2028 error = print_commits(id, repo, path, show_patch, search_pattern,
2029 diff_context, limit, log_branches, &refs);
2030 done:
2031 free(path);
2032 free(repo_path);
2033 free(cwd);
2034 free(id);
2035 if (worktree)
2036 got_worktree_close(worktree);
2037 if (repo) {
2038 const struct got_error *repo_error;
2039 repo_error = got_repo_close(repo);
2040 if (error == NULL)
2041 error = repo_error;
2043 got_ref_list_free(&refs);
2044 return error;
2047 __dead static void
2048 usage_diff(void)
2050 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2051 "[-w] [object1 object2 | path]\n", getprogname());
2052 exit(1);
2055 struct print_diff_arg {
2056 struct got_repository *repo;
2057 struct got_worktree *worktree;
2058 int diff_context;
2059 const char *id_str;
2060 int header_shown;
2061 int diff_staged;
2062 int ignore_whitespace;
2065 static const struct got_error *
2066 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2067 const char *path, struct got_object_id *blob_id,
2068 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2069 int dirfd, const char *de_name)
2071 struct print_diff_arg *a = arg;
2072 const struct got_error *err = NULL;
2073 struct got_blob_object *blob1 = NULL;
2074 int fd = -1;
2075 FILE *f2 = NULL;
2076 char *abspath = NULL, *label1 = NULL;
2077 struct stat sb;
2079 if (a->diff_staged) {
2080 if (staged_status != GOT_STATUS_MODIFY &&
2081 staged_status != GOT_STATUS_ADD &&
2082 staged_status != GOT_STATUS_DELETE)
2083 return NULL;
2084 } else {
2085 if (staged_status == GOT_STATUS_DELETE)
2086 return NULL;
2087 if (status == GOT_STATUS_NONEXISTENT)
2088 return got_error_set_errno(ENOENT, path);
2089 if (status != GOT_STATUS_MODIFY &&
2090 status != GOT_STATUS_ADD &&
2091 status != GOT_STATUS_DELETE &&
2092 status != GOT_STATUS_CONFLICT)
2093 return NULL;
2096 if (!a->header_shown) {
2097 printf("diff %s %s%s\n", a->id_str,
2098 got_worktree_get_root_path(a->worktree),
2099 a->diff_staged ? " (staged changes)" : "");
2100 a->header_shown = 1;
2103 if (a->diff_staged) {
2104 const char *label1 = NULL, *label2 = NULL;
2105 switch (staged_status) {
2106 case GOT_STATUS_MODIFY:
2107 label1 = path;
2108 label2 = path;
2109 break;
2110 case GOT_STATUS_ADD:
2111 label2 = path;
2112 break;
2113 case GOT_STATUS_DELETE:
2114 label1 = path;
2115 break;
2116 default:
2117 return got_error(GOT_ERR_FILE_STATUS);
2119 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2120 label1, label2, a->diff_context, a->ignore_whitespace,
2121 a->repo, stdout);
2124 if (staged_status == GOT_STATUS_ADD ||
2125 staged_status == GOT_STATUS_MODIFY) {
2126 char *id_str;
2127 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2128 8192);
2129 if (err)
2130 goto done;
2131 err = got_object_id_str(&id_str, staged_blob_id);
2132 if (err)
2133 goto done;
2134 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2135 err = got_error_from_errno("asprintf");
2136 free(id_str);
2137 goto done;
2139 free(id_str);
2140 } else if (status != GOT_STATUS_ADD) {
2141 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2142 if (err)
2143 goto done;
2146 if (status != GOT_STATUS_DELETE) {
2147 if (asprintf(&abspath, "%s/%s",
2148 got_worktree_get_root_path(a->worktree), path) == -1) {
2149 err = got_error_from_errno("asprintf");
2150 goto done;
2153 if (dirfd != -1) {
2154 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2155 if (fd == -1) {
2156 err = got_error_from_errno2("openat", abspath);
2157 goto done;
2159 } else {
2160 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2161 if (fd == -1) {
2162 err = got_error_from_errno2("open", abspath);
2163 goto done;
2166 if (fstat(fd, &sb) == -1) {
2167 err = got_error_from_errno2("fstat", abspath);
2168 goto done;
2170 f2 = fdopen(fd, "r");
2171 if (f2 == NULL) {
2172 err = got_error_from_errno2("fdopen", abspath);
2173 goto done;
2175 fd = -1;
2176 } else
2177 sb.st_size = 0;
2179 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2180 a->diff_context, a->ignore_whitespace, stdout);
2181 done:
2182 if (blob1)
2183 got_object_blob_close(blob1);
2184 if (f2 && fclose(f2) == EOF && err == NULL)
2185 err = got_error_from_errno("fclose");
2186 if (fd != -1 && close(fd) == -1 && err == NULL)
2187 err = got_error_from_errno("close");
2188 free(abspath);
2189 return err;
2192 static const struct got_error *
2193 cmd_diff(int argc, char *argv[])
2195 const struct got_error *error;
2196 struct got_repository *repo = NULL;
2197 struct got_worktree *worktree = NULL;
2198 char *cwd = NULL, *repo_path = NULL;
2199 struct got_object_id *id1 = NULL, *id2 = NULL;
2200 const char *id_str1 = NULL, *id_str2 = NULL;
2201 char *label1 = NULL, *label2 = NULL;
2202 int type1, type2;
2203 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2204 const char *errstr;
2205 char *path = NULL;
2207 #ifndef PROFILE
2208 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2209 NULL) == -1)
2210 err(1, "pledge");
2211 #endif
2213 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2214 switch (ch) {
2215 case 'C':
2216 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2217 &errstr);
2218 if (errstr != NULL)
2219 err(1, "-C option %s", errstr);
2220 break;
2221 case 'r':
2222 repo_path = realpath(optarg, NULL);
2223 if (repo_path == NULL)
2224 return got_error_from_errno2("realpath",
2225 optarg);
2226 got_path_strip_trailing_slashes(repo_path);
2227 break;
2228 case 's':
2229 diff_staged = 1;
2230 break;
2231 case 'w':
2232 ignore_whitespace = 1;
2233 break;
2234 default:
2235 usage_diff();
2236 /* NOTREACHED */
2240 argc -= optind;
2241 argv += optind;
2243 cwd = getcwd(NULL, 0);
2244 if (cwd == NULL) {
2245 error = got_error_from_errno("getcwd");
2246 goto done;
2248 error = got_worktree_open(&worktree, cwd);
2249 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2250 goto done;
2251 if (argc <= 1) {
2252 if (worktree == NULL) {
2253 error = got_error(GOT_ERR_NOT_WORKTREE);
2254 goto done;
2256 if (repo_path)
2257 errx(1,
2258 "-r option can't be used when diffing a work tree");
2259 repo_path = strdup(got_worktree_get_repo_path(worktree));
2260 if (repo_path == NULL) {
2261 error = got_error_from_errno("strdup");
2262 goto done;
2264 if (argc == 1) {
2265 error = got_worktree_resolve_path(&path, worktree,
2266 argv[0]);
2267 if (error)
2268 goto done;
2269 } else {
2270 path = strdup("");
2271 if (path == NULL) {
2272 error = got_error_from_errno("strdup");
2273 goto done;
2276 } else if (argc == 2) {
2277 if (diff_staged)
2278 errx(1, "-s option can't be used when diffing "
2279 "objects in repository");
2280 id_str1 = argv[0];
2281 id_str2 = argv[1];
2282 if (worktree && repo_path == NULL) {
2283 repo_path =
2284 strdup(got_worktree_get_repo_path(worktree));
2285 if (repo_path == NULL) {
2286 error = got_error_from_errno("strdup");
2287 goto done;
2290 } else
2291 usage_diff();
2293 if (repo_path == NULL) {
2294 repo_path = getcwd(NULL, 0);
2295 if (repo_path == NULL)
2296 return got_error_from_errno("getcwd");
2299 error = got_repo_open(&repo, repo_path, NULL);
2300 free(repo_path);
2301 if (error != NULL)
2302 goto done;
2304 error = apply_unveil(got_repo_get_path(repo), 1,
2305 worktree ? got_worktree_get_root_path(worktree) : NULL);
2306 if (error)
2307 goto done;
2309 if (argc <= 1) {
2310 struct print_diff_arg arg;
2311 struct got_pathlist_head paths;
2312 char *id_str;
2314 TAILQ_INIT(&paths);
2316 error = got_object_id_str(&id_str,
2317 got_worktree_get_base_commit_id(worktree));
2318 if (error)
2319 goto done;
2320 arg.repo = repo;
2321 arg.worktree = worktree;
2322 arg.diff_context = diff_context;
2323 arg.id_str = id_str;
2324 arg.header_shown = 0;
2325 arg.diff_staged = diff_staged;
2326 arg.ignore_whitespace = ignore_whitespace;
2328 error = got_pathlist_append(&paths, path, NULL);
2329 if (error)
2330 goto done;
2332 error = got_worktree_status(worktree, &paths, repo, print_diff,
2333 &arg, check_cancelled, NULL);
2334 free(id_str);
2335 got_pathlist_free(&paths);
2336 goto done;
2339 error = got_repo_match_object_id(&id1, &label1, id_str1,
2340 GOT_OBJ_TYPE_ANY, 1, repo);
2341 if (error)
2342 goto done;
2344 error = got_repo_match_object_id(&id2, &label2, id_str2,
2345 GOT_OBJ_TYPE_ANY, 1, repo);
2346 if (error)
2347 goto done;
2349 error = got_object_get_type(&type1, repo, id1);
2350 if (error)
2351 goto done;
2353 error = got_object_get_type(&type2, repo, id2);
2354 if (error)
2355 goto done;
2357 if (type1 != type2) {
2358 error = got_error(GOT_ERR_OBJ_TYPE);
2359 goto done;
2362 switch (type1) {
2363 case GOT_OBJ_TYPE_BLOB:
2364 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2365 diff_context, ignore_whitespace, repo, stdout);
2366 break;
2367 case GOT_OBJ_TYPE_TREE:
2368 error = got_diff_objects_as_trees(id1, id2, "", "",
2369 diff_context, ignore_whitespace, repo, stdout);
2370 break;
2371 case GOT_OBJ_TYPE_COMMIT:
2372 printf("diff %s %s\n", label1, label2);
2373 error = got_diff_objects_as_commits(id1, id2, diff_context,
2374 ignore_whitespace, repo, stdout);
2375 break;
2376 default:
2377 error = got_error(GOT_ERR_OBJ_TYPE);
2379 done:
2380 free(label1);
2381 free(label2);
2382 free(id1);
2383 free(id2);
2384 free(path);
2385 if (worktree)
2386 got_worktree_close(worktree);
2387 if (repo) {
2388 const struct got_error *repo_error;
2389 repo_error = got_repo_close(repo);
2390 if (error == NULL)
2391 error = repo_error;
2393 return error;
2396 __dead static void
2397 usage_blame(void)
2399 fprintf(stderr,
2400 "usage: %s blame [-c commit] [-r repository-path] path\n",
2401 getprogname());
2402 exit(1);
2405 struct blame_line {
2406 int annotated;
2407 char *id_str;
2408 char *committer;
2409 char datebuf[11]; /* YYYY-MM-DD + NUL */
2412 struct blame_cb_args {
2413 struct blame_line *lines;
2414 int nlines;
2415 int nlines_prec;
2416 int lineno_cur;
2417 off_t *line_offsets;
2418 FILE *f;
2419 struct got_repository *repo;
2422 static const struct got_error *
2423 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2425 const struct got_error *err = NULL;
2426 struct blame_cb_args *a = arg;
2427 struct blame_line *bline;
2428 char *line = NULL;
2429 size_t linesize = 0;
2430 struct got_commit_object *commit = NULL;
2431 off_t offset;
2432 struct tm tm;
2433 time_t committer_time;
2435 if (nlines != a->nlines ||
2436 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2437 return got_error(GOT_ERR_RANGE);
2439 if (sigint_received)
2440 return got_error(GOT_ERR_ITER_COMPLETED);
2442 if (lineno == -1)
2443 return NULL; /* no change in this commit */
2445 /* Annotate this line. */
2446 bline = &a->lines[lineno - 1];
2447 if (bline->annotated)
2448 return NULL;
2449 err = got_object_id_str(&bline->id_str, id);
2450 if (err)
2451 return err;
2453 err = got_object_open_as_commit(&commit, a->repo, id);
2454 if (err)
2455 goto done;
2457 bline->committer = strdup(got_object_commit_get_committer(commit));
2458 if (bline->committer == NULL) {
2459 err = got_error_from_errno("strdup");
2460 goto done;
2463 committer_time = got_object_commit_get_committer_time(commit);
2464 if (localtime_r(&committer_time, &tm) == NULL)
2465 return got_error_from_errno("localtime_r");
2466 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2467 &tm) >= sizeof(bline->datebuf)) {
2468 err = got_error(GOT_ERR_NO_SPACE);
2469 goto done;
2471 bline->annotated = 1;
2473 /* Print lines annotated so far. */
2474 bline = &a->lines[a->lineno_cur - 1];
2475 if (!bline->annotated)
2476 goto done;
2478 offset = a->line_offsets[a->lineno_cur - 1];
2479 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2480 err = got_error_from_errno("fseeko");
2481 goto done;
2484 while (bline->annotated) {
2485 char *smallerthan, *at, *nl, *committer;
2486 size_t len;
2488 if (getline(&line, &linesize, a->f) == -1) {
2489 if (ferror(a->f))
2490 err = got_error_from_errno("getline");
2491 break;
2494 committer = bline->committer;
2495 smallerthan = strchr(committer, '<');
2496 if (smallerthan && smallerthan[1] != '\0')
2497 committer = smallerthan + 1;
2498 at = strchr(committer, '@');
2499 if (at)
2500 *at = '\0';
2501 len = strlen(committer);
2502 if (len >= 9)
2503 committer[8] = '\0';
2505 nl = strchr(line, '\n');
2506 if (nl)
2507 *nl = '\0';
2508 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2509 bline->id_str, bline->datebuf, committer, line);
2511 a->lineno_cur++;
2512 bline = &a->lines[a->lineno_cur - 1];
2514 done:
2515 if (commit)
2516 got_object_commit_close(commit);
2517 free(line);
2518 return err;
2521 static const struct got_error *
2522 cmd_blame(int argc, char *argv[])
2524 const struct got_error *error;
2525 struct got_repository *repo = NULL;
2526 struct got_worktree *worktree = NULL;
2527 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2528 struct got_object_id *obj_id = NULL;
2529 struct got_object_id *commit_id = NULL;
2530 struct got_blob_object *blob = NULL;
2531 char *commit_id_str = NULL;
2532 struct blame_cb_args bca;
2533 int ch, obj_type, i;
2534 size_t filesize;
2536 memset(&bca, 0, sizeof(bca));
2538 #ifndef PROFILE
2539 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2540 NULL) == -1)
2541 err(1, "pledge");
2542 #endif
2544 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2545 switch (ch) {
2546 case 'c':
2547 commit_id_str = optarg;
2548 break;
2549 case 'r':
2550 repo_path = realpath(optarg, NULL);
2551 if (repo_path == NULL)
2552 return got_error_from_errno2("realpath",
2553 optarg);
2554 got_path_strip_trailing_slashes(repo_path);
2555 break;
2556 default:
2557 usage_blame();
2558 /* NOTREACHED */
2562 argc -= optind;
2563 argv += optind;
2565 if (argc == 1)
2566 path = argv[0];
2567 else
2568 usage_blame();
2570 cwd = getcwd(NULL, 0);
2571 if (cwd == NULL) {
2572 error = got_error_from_errno("getcwd");
2573 goto done;
2575 if (repo_path == NULL) {
2576 error = got_worktree_open(&worktree, cwd);
2577 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2578 goto done;
2579 else
2580 error = NULL;
2581 if (worktree) {
2582 repo_path =
2583 strdup(got_worktree_get_repo_path(worktree));
2584 if (repo_path == NULL)
2585 error = got_error_from_errno("strdup");
2586 if (error)
2587 goto done;
2588 } else {
2589 repo_path = strdup(cwd);
2590 if (repo_path == NULL) {
2591 error = got_error_from_errno("strdup");
2592 goto done;
2597 error = got_repo_open(&repo, repo_path, NULL);
2598 if (error != NULL)
2599 goto done;
2601 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2602 if (error)
2603 goto done;
2605 if (worktree) {
2606 const char *prefix = got_worktree_get_path_prefix(worktree);
2607 char *p, *worktree_subdir = cwd +
2608 strlen(got_worktree_get_root_path(worktree));
2609 if (asprintf(&p, "%s%s%s%s%s",
2610 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2611 worktree_subdir, worktree_subdir[0] ? "/" : "",
2612 path) == -1) {
2613 error = got_error_from_errno("asprintf");
2614 goto done;
2616 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2617 free(p);
2618 } else {
2619 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2621 if (error)
2622 goto done;
2624 if (commit_id_str == NULL) {
2625 struct got_reference *head_ref;
2626 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2627 if (error != NULL)
2628 goto done;
2629 error = got_ref_resolve(&commit_id, repo, head_ref);
2630 got_ref_close(head_ref);
2631 if (error != NULL)
2632 goto done;
2633 } else {
2634 error = got_repo_match_object_id(&commit_id, NULL,
2635 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2636 if (error)
2637 goto done;
2640 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2641 if (error)
2642 goto done;
2644 error = got_object_get_type(&obj_type, repo, obj_id);
2645 if (error)
2646 goto done;
2648 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2649 error = got_error(GOT_ERR_OBJ_TYPE);
2650 goto done;
2653 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2654 if (error)
2655 goto done;
2656 bca.f = got_opentemp();
2657 if (bca.f == NULL) {
2658 error = got_error_from_errno("got_opentemp");
2659 goto done;
2661 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2662 &bca.line_offsets, bca.f, blob);
2663 if (error || bca.nlines == 0)
2664 goto done;
2666 /* Don't include \n at EOF in the blame line count. */
2667 if (bca.line_offsets[bca.nlines - 1] == filesize)
2668 bca.nlines--;
2670 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2671 if (bca.lines == NULL) {
2672 error = got_error_from_errno("calloc");
2673 goto done;
2675 bca.lineno_cur = 1;
2676 bca.nlines_prec = 0;
2677 i = bca.nlines;
2678 while (i > 0) {
2679 i /= 10;
2680 bca.nlines_prec++;
2682 bca.repo = repo;
2684 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2685 check_cancelled, NULL);
2686 done:
2687 free(in_repo_path);
2688 free(repo_path);
2689 free(cwd);
2690 free(commit_id);
2691 free(obj_id);
2692 if (blob)
2693 got_object_blob_close(blob);
2694 if (worktree)
2695 got_worktree_close(worktree);
2696 if (repo) {
2697 const struct got_error *repo_error;
2698 repo_error = got_repo_close(repo);
2699 if (error == NULL)
2700 error = repo_error;
2702 if (bca.lines) {
2703 for (i = 0; i < bca.nlines; i++) {
2704 struct blame_line *bline = &bca.lines[i];
2705 free(bline->id_str);
2706 free(bline->committer);
2708 free(bca.lines);
2710 free(bca.line_offsets);
2711 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2712 error = got_error_from_errno("fclose");
2713 return error;
2716 __dead static void
2717 usage_tree(void)
2719 fprintf(stderr,
2720 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2721 getprogname());
2722 exit(1);
2725 static void
2726 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2727 const char *root_path)
2729 int is_root_path = (strcmp(path, root_path) == 0);
2730 const char *modestr = "";
2731 mode_t mode = got_tree_entry_get_mode(te);
2733 path += strlen(root_path);
2734 while (path[0] == '/')
2735 path++;
2737 if (got_object_tree_entry_is_submodule(te))
2738 modestr = "$";
2739 else if (S_ISLNK(mode))
2740 modestr = "@";
2741 else if (S_ISDIR(mode))
2742 modestr = "/";
2743 else if (mode & S_IXUSR)
2744 modestr = "*";
2746 printf("%s%s%s%s%s\n", id ? id : "", path,
2747 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2750 static const struct got_error *
2751 print_tree(const char *path, struct got_object_id *commit_id,
2752 int show_ids, int recurse, const char *root_path,
2753 struct got_repository *repo)
2755 const struct got_error *err = NULL;
2756 struct got_object_id *tree_id = NULL;
2757 struct got_tree_object *tree = NULL;
2758 int nentries, i;
2760 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2761 if (err)
2762 goto done;
2764 err = got_object_open_as_tree(&tree, repo, tree_id);
2765 if (err)
2766 goto done;
2767 nentries = got_object_tree_get_nentries(tree);
2768 for (i = 0; i < nentries; i++) {
2769 struct got_tree_entry *te;
2770 char *id = NULL;
2772 if (sigint_received || sigpipe_received)
2773 break;
2775 te = got_object_tree_get_entry(tree, i);
2776 if (show_ids) {
2777 char *id_str;
2778 err = got_object_id_str(&id_str,
2779 got_tree_entry_get_id(te));
2780 if (err)
2781 goto done;
2782 if (asprintf(&id, "%s ", id_str) == -1) {
2783 err = got_error_from_errno("asprintf");
2784 free(id_str);
2785 goto done;
2787 free(id_str);
2789 print_entry(te, id, path, root_path);
2790 free(id);
2792 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2793 char *child_path;
2794 if (asprintf(&child_path, "%s%s%s", path,
2795 path[0] == '/' && path[1] == '\0' ? "" : "/",
2796 got_tree_entry_get_name(te)) == -1) {
2797 err = got_error_from_errno("asprintf");
2798 goto done;
2800 err = print_tree(child_path, commit_id, show_ids, 1,
2801 root_path, repo);
2802 free(child_path);
2803 if (err)
2804 goto done;
2807 done:
2808 if (tree)
2809 got_object_tree_close(tree);
2810 free(tree_id);
2811 return err;
2814 static const struct got_error *
2815 cmd_tree(int argc, char *argv[])
2817 const struct got_error *error;
2818 struct got_repository *repo = NULL;
2819 struct got_worktree *worktree = NULL;
2820 const char *path;
2821 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2822 struct got_object_id *commit_id = NULL;
2823 char *commit_id_str = NULL;
2824 int show_ids = 0, recurse = 0;
2825 int ch;
2827 #ifndef PROFILE
2828 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2829 NULL) == -1)
2830 err(1, "pledge");
2831 #endif
2833 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2834 switch (ch) {
2835 case 'c':
2836 commit_id_str = optarg;
2837 break;
2838 case 'r':
2839 repo_path = realpath(optarg, NULL);
2840 if (repo_path == NULL)
2841 return got_error_from_errno2("realpath",
2842 optarg);
2843 got_path_strip_trailing_slashes(repo_path);
2844 break;
2845 case 'i':
2846 show_ids = 1;
2847 break;
2848 case 'R':
2849 recurse = 1;
2850 break;
2851 default:
2852 usage_tree();
2853 /* NOTREACHED */
2857 argc -= optind;
2858 argv += optind;
2860 if (argc == 1)
2861 path = argv[0];
2862 else if (argc > 1)
2863 usage_tree();
2864 else
2865 path = NULL;
2867 cwd = getcwd(NULL, 0);
2868 if (cwd == NULL) {
2869 error = got_error_from_errno("getcwd");
2870 goto done;
2872 if (repo_path == NULL) {
2873 error = got_worktree_open(&worktree, cwd);
2874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2875 goto done;
2876 else
2877 error = NULL;
2878 if (worktree) {
2879 repo_path =
2880 strdup(got_worktree_get_repo_path(worktree));
2881 if (repo_path == NULL)
2882 error = got_error_from_errno("strdup");
2883 if (error)
2884 goto done;
2885 } else {
2886 repo_path = strdup(cwd);
2887 if (repo_path == NULL) {
2888 error = got_error_from_errno("strdup");
2889 goto done;
2894 error = got_repo_open(&repo, repo_path, NULL);
2895 if (error != NULL)
2896 goto done;
2898 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2899 if (error)
2900 goto done;
2902 if (path == NULL) {
2903 if (worktree) {
2904 char *p, *worktree_subdir = cwd +
2905 strlen(got_worktree_get_root_path(worktree));
2906 if (asprintf(&p, "%s/%s",
2907 got_worktree_get_path_prefix(worktree),
2908 worktree_subdir) == -1) {
2909 error = got_error_from_errno("asprintf");
2910 goto done;
2912 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2913 free(p);
2914 if (error)
2915 goto done;
2916 } else
2917 path = "/";
2919 if (in_repo_path == NULL) {
2920 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2921 if (error != NULL)
2922 goto done;
2925 if (commit_id_str == NULL) {
2926 struct got_reference *head_ref;
2927 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2928 if (error != NULL)
2929 goto done;
2930 error = got_ref_resolve(&commit_id, repo, head_ref);
2931 got_ref_close(head_ref);
2932 if (error != NULL)
2933 goto done;
2934 } else {
2935 error = got_repo_match_object_id(&commit_id, NULL,
2936 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2937 if (error)
2938 goto done;
2941 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2942 in_repo_path, repo);
2943 done:
2944 free(in_repo_path);
2945 free(repo_path);
2946 free(cwd);
2947 free(commit_id);
2948 if (worktree)
2949 got_worktree_close(worktree);
2950 if (repo) {
2951 const struct got_error *repo_error;
2952 repo_error = got_repo_close(repo);
2953 if (error == NULL)
2954 error = repo_error;
2956 return error;
2959 __dead static void
2960 usage_status(void)
2962 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2963 exit(1);
2966 static const struct got_error *
2967 print_status(void *arg, unsigned char status, unsigned char staged_status,
2968 const char *path, struct got_object_id *blob_id,
2969 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2970 int dirfd, const char *de_name)
2972 if (status == staged_status && (status == GOT_STATUS_DELETE))
2973 status = GOT_STATUS_NO_CHANGE;
2974 printf("%c%c %s\n", status, staged_status, path);
2975 return NULL;
2978 static const struct got_error *
2979 cmd_status(int argc, char *argv[])
2981 const struct got_error *error = NULL;
2982 struct got_repository *repo = NULL;
2983 struct got_worktree *worktree = NULL;
2984 char *cwd = NULL;
2985 struct got_pathlist_head paths;
2986 struct got_pathlist_entry *pe;
2987 int ch;
2989 TAILQ_INIT(&paths);
2991 while ((ch = getopt(argc, argv, "")) != -1) {
2992 switch (ch) {
2993 default:
2994 usage_status();
2995 /* NOTREACHED */
2999 argc -= optind;
3000 argv += optind;
3002 #ifndef PROFILE
3003 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3004 NULL) == -1)
3005 err(1, "pledge");
3006 #endif
3007 cwd = getcwd(NULL, 0);
3008 if (cwd == NULL) {
3009 error = got_error_from_errno("getcwd");
3010 goto done;
3013 error = got_worktree_open(&worktree, cwd);
3014 if (error != NULL)
3015 goto done;
3017 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3018 NULL);
3019 if (error != NULL)
3020 goto done;
3022 error = apply_unveil(got_repo_get_path(repo), 1,
3023 got_worktree_get_root_path(worktree));
3024 if (error)
3025 goto done;
3027 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3028 if (error)
3029 goto done;
3031 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3032 check_cancelled, NULL);
3033 done:
3034 TAILQ_FOREACH(pe, &paths, entry)
3035 free((char *)pe->path);
3036 got_pathlist_free(&paths);
3037 free(cwd);
3038 return error;
3041 __dead static void
3042 usage_ref(void)
3044 fprintf(stderr,
3045 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3046 getprogname());
3047 exit(1);
3050 static const struct got_error *
3051 list_refs(struct got_repository *repo)
3053 static const struct got_error *err = NULL;
3054 struct got_reflist_head refs;
3055 struct got_reflist_entry *re;
3057 SIMPLEQ_INIT(&refs);
3058 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3059 if (err)
3060 return err;
3062 SIMPLEQ_FOREACH(re, &refs, entry) {
3063 char *refstr;
3064 refstr = got_ref_to_str(re->ref);
3065 if (refstr == NULL)
3066 return got_error_from_errno("got_ref_to_str");
3067 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3068 free(refstr);
3071 got_ref_list_free(&refs);
3072 return NULL;
3075 static const struct got_error *
3076 delete_ref(struct got_repository *repo, const char *refname)
3078 const struct got_error *err = NULL;
3079 struct got_reference *ref;
3081 err = got_ref_open(&ref, repo, refname, 0);
3082 if (err)
3083 return err;
3085 err = got_ref_delete(ref, repo);
3086 got_ref_close(ref);
3087 return err;
3090 static const struct got_error *
3091 add_ref(struct got_repository *repo, const char *refname, const char *target)
3093 const struct got_error *err = NULL;
3094 struct got_object_id *id;
3095 struct got_reference *ref = NULL;
3098 * Don't let the user create a reference name with a leading '-'.
3099 * While technically a valid reference name, this case is usually
3100 * an unintended typo.
3102 if (refname[0] == '-')
3103 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3105 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3106 repo);
3107 if (err) {
3108 struct got_reference *target_ref;
3110 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3111 return err;
3112 err = got_ref_open(&target_ref, repo, target, 0);
3113 if (err)
3114 return err;
3115 err = got_ref_resolve(&id, repo, target_ref);
3116 got_ref_close(target_ref);
3117 if (err)
3118 return err;
3121 err = got_ref_alloc(&ref, refname, id);
3122 if (err)
3123 goto done;
3125 err = got_ref_write(ref, repo);
3126 done:
3127 if (ref)
3128 got_ref_close(ref);
3129 free(id);
3130 return err;
3133 static const struct got_error *
3134 add_symref(struct got_repository *repo, const char *refname, const char *target)
3136 const struct got_error *err = NULL;
3137 struct got_reference *ref = NULL;
3138 struct got_reference *target_ref = NULL;
3141 * Don't let the user create a reference name with a leading '-'.
3142 * While technically a valid reference name, this case is usually
3143 * an unintended typo.
3145 if (refname[0] == '-')
3146 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3148 err = got_ref_open(&target_ref, repo, target, 0);
3149 if (err)
3150 return err;
3152 err = got_ref_alloc_symref(&ref, refname, target_ref);
3153 if (err)
3154 goto done;
3156 err = got_ref_write(ref, repo);
3157 done:
3158 if (target_ref)
3159 got_ref_close(target_ref);
3160 if (ref)
3161 got_ref_close(ref);
3162 return err;
3165 static const struct got_error *
3166 cmd_ref(int argc, char *argv[])
3168 const struct got_error *error = NULL;
3169 struct got_repository *repo = NULL;
3170 struct got_worktree *worktree = NULL;
3171 char *cwd = NULL, *repo_path = NULL;
3172 int ch, do_list = 0, create_symref = 0;
3173 const char *delref = NULL;
3175 /* TODO: Add -s option for adding symbolic references. */
3176 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3177 switch (ch) {
3178 case 'd':
3179 delref = optarg;
3180 break;
3181 case 'r':
3182 repo_path = realpath(optarg, NULL);
3183 if (repo_path == NULL)
3184 return got_error_from_errno2("realpath",
3185 optarg);
3186 got_path_strip_trailing_slashes(repo_path);
3187 break;
3188 case 'l':
3189 do_list = 1;
3190 break;
3191 case 's':
3192 create_symref = 1;
3193 break;
3194 default:
3195 usage_ref();
3196 /* NOTREACHED */
3200 if (do_list && delref)
3201 errx(1, "-l and -d options are mutually exclusive\n");
3203 argc -= optind;
3204 argv += optind;
3206 if (do_list || delref) {
3207 if (create_symref)
3208 errx(1, "-s option cannot be used together with the "
3209 "-l or -d options");
3210 if (argc > 0)
3211 usage_ref();
3212 } else if (argc != 2)
3213 usage_ref();
3215 #ifndef PROFILE
3216 if (do_list) {
3217 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3218 NULL) == -1)
3219 err(1, "pledge");
3220 } else {
3221 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3222 "sendfd unveil", NULL) == -1)
3223 err(1, "pledge");
3225 #endif
3226 cwd = getcwd(NULL, 0);
3227 if (cwd == NULL) {
3228 error = got_error_from_errno("getcwd");
3229 goto done;
3232 if (repo_path == NULL) {
3233 error = got_worktree_open(&worktree, cwd);
3234 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3235 goto done;
3236 else
3237 error = NULL;
3238 if (worktree) {
3239 repo_path =
3240 strdup(got_worktree_get_repo_path(worktree));
3241 if (repo_path == NULL)
3242 error = got_error_from_errno("strdup");
3243 if (error)
3244 goto done;
3245 } else {
3246 repo_path = strdup(cwd);
3247 if (repo_path == NULL) {
3248 error = got_error_from_errno("strdup");
3249 goto done;
3254 error = got_repo_open(&repo, repo_path, NULL);
3255 if (error != NULL)
3256 goto done;
3258 error = apply_unveil(got_repo_get_path(repo), do_list,
3259 worktree ? got_worktree_get_root_path(worktree) : NULL);
3260 if (error)
3261 goto done;
3263 if (do_list)
3264 error = list_refs(repo);
3265 else if (delref)
3266 error = delete_ref(repo, delref);
3267 else if (create_symref)
3268 error = add_symref(repo, argv[0], argv[1]);
3269 else
3270 error = add_ref(repo, argv[0], argv[1]);
3271 done:
3272 if (repo)
3273 got_repo_close(repo);
3274 if (worktree)
3275 got_worktree_close(worktree);
3276 free(cwd);
3277 free(repo_path);
3278 return error;
3281 __dead static void
3282 usage_branch(void)
3284 fprintf(stderr,
3285 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3286 "[name]\n", getprogname());
3287 exit(1);
3290 static const struct got_error *
3291 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3292 struct got_reference *ref)
3294 const struct got_error *err = NULL;
3295 const char *refname, *marker = " ";
3296 char *refstr;
3298 refname = got_ref_get_name(ref);
3299 if (worktree && strcmp(refname,
3300 got_worktree_get_head_ref_name(worktree)) == 0) {
3301 struct got_object_id *id = NULL;
3303 err = got_ref_resolve(&id, repo, ref);
3304 if (err)
3305 return err;
3306 if (got_object_id_cmp(id,
3307 got_worktree_get_base_commit_id(worktree)) == 0)
3308 marker = "* ";
3309 else
3310 marker = "~ ";
3311 free(id);
3314 if (strncmp(refname, "refs/heads/", 11) == 0)
3315 refname += 11;
3316 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3317 refname += 18;
3319 refstr = got_ref_to_str(ref);
3320 if (refstr == NULL)
3321 return got_error_from_errno("got_ref_to_str");
3323 printf("%s%s: %s\n", marker, refname, refstr);
3324 free(refstr);
3325 return NULL;
3328 static const struct got_error *
3329 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3331 const char *refname;
3333 if (worktree == NULL)
3334 return got_error(GOT_ERR_NOT_WORKTREE);
3336 refname = got_worktree_get_head_ref_name(worktree);
3338 if (strncmp(refname, "refs/heads/", 11) == 0)
3339 refname += 11;
3340 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3341 refname += 18;
3343 printf("%s\n", refname);
3345 return NULL;
3348 static const struct got_error *
3349 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3351 static const struct got_error *err = NULL;
3352 struct got_reflist_head refs;
3353 struct got_reflist_entry *re;
3354 struct got_reference *temp_ref = NULL;
3355 int rebase_in_progress, histedit_in_progress;
3357 SIMPLEQ_INIT(&refs);
3359 if (worktree) {
3360 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3361 worktree);
3362 if (err)
3363 return err;
3365 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3366 worktree);
3367 if (err)
3368 return err;
3370 if (rebase_in_progress || histedit_in_progress) {
3371 err = got_ref_open(&temp_ref, repo,
3372 got_worktree_get_head_ref_name(worktree), 0);
3373 if (err)
3374 return err;
3375 list_branch(repo, worktree, temp_ref);
3376 got_ref_close(temp_ref);
3380 err = got_ref_list(&refs, repo, "refs/heads",
3381 got_ref_cmp_by_name, NULL);
3382 if (err)
3383 return err;
3385 SIMPLEQ_FOREACH(re, &refs, entry)
3386 list_branch(repo, worktree, re->ref);
3388 got_ref_list_free(&refs);
3389 return NULL;
3392 static const struct got_error *
3393 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3394 const char *branch_name)
3396 const struct got_error *err = NULL;
3397 struct got_reference *ref = NULL;
3398 char *refname;
3400 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3401 return got_error_from_errno("asprintf");
3403 err = got_ref_open(&ref, repo, refname, 0);
3404 if (err)
3405 goto done;
3407 if (worktree &&
3408 strcmp(got_worktree_get_head_ref_name(worktree),
3409 got_ref_get_name(ref)) == 0) {
3410 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3411 "will not delete this work tree's current branch");
3412 goto done;
3415 err = got_ref_delete(ref, repo);
3416 done:
3417 if (ref)
3418 got_ref_close(ref);
3419 free(refname);
3420 return err;
3423 static const struct got_error *
3424 add_branch(struct got_repository *repo, const char *branch_name,
3425 struct got_object_id *base_commit_id)
3427 const struct got_error *err = NULL;
3428 struct got_reference *ref = NULL;
3429 char *base_refname = NULL, *refname = NULL;
3432 * Don't let the user create a branch name with a leading '-'.
3433 * While technically a valid reference name, this case is usually
3434 * an unintended typo.
3436 if (branch_name[0] == '-')
3437 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3439 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3440 err = got_error_from_errno("asprintf");
3441 goto done;
3444 err = got_ref_open(&ref, repo, refname, 0);
3445 if (err == NULL) {
3446 err = got_error(GOT_ERR_BRANCH_EXISTS);
3447 goto done;
3448 } else if (err->code != GOT_ERR_NOT_REF)
3449 goto done;
3451 err = got_ref_alloc(&ref, refname, base_commit_id);
3452 if (err)
3453 goto done;
3455 err = got_ref_write(ref, repo);
3456 done:
3457 if (ref)
3458 got_ref_close(ref);
3459 free(base_refname);
3460 free(refname);
3461 return err;
3464 static const struct got_error *
3465 cmd_branch(int argc, char *argv[])
3467 const struct got_error *error = NULL;
3468 struct got_repository *repo = NULL;
3469 struct got_worktree *worktree = NULL;
3470 char *cwd = NULL, *repo_path = NULL;
3471 int ch, do_list = 0, do_show = 0;
3472 const char *delref = NULL, *commit_id_arg = NULL;
3474 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3475 switch (ch) {
3476 case 'c':
3477 commit_id_arg = optarg;
3478 break;
3479 case 'd':
3480 delref = optarg;
3481 break;
3482 case 'r':
3483 repo_path = realpath(optarg, NULL);
3484 if (repo_path == NULL)
3485 return got_error_from_errno2("realpath",
3486 optarg);
3487 got_path_strip_trailing_slashes(repo_path);
3488 break;
3489 case 'l':
3490 do_list = 1;
3491 break;
3492 default:
3493 usage_branch();
3494 /* NOTREACHED */
3498 if (do_list && delref)
3499 errx(1, "-l and -d options are mutually exclusive\n");
3501 argc -= optind;
3502 argv += optind;
3504 if (!do_list && !delref && argc == 0)
3505 do_show = 1;
3507 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3508 errx(1, "-c option can only be used when creating a branch");
3510 if (do_list || delref) {
3511 if (argc > 0)
3512 usage_branch();
3513 } else if (!do_show && argc != 1)
3514 usage_branch();
3516 #ifndef PROFILE
3517 if (do_list || do_show) {
3518 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3519 NULL) == -1)
3520 err(1, "pledge");
3521 } else {
3522 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3523 "sendfd unveil", NULL) == -1)
3524 err(1, "pledge");
3526 #endif
3527 cwd = getcwd(NULL, 0);
3528 if (cwd == NULL) {
3529 error = got_error_from_errno("getcwd");
3530 goto done;
3533 if (repo_path == NULL) {
3534 error = got_worktree_open(&worktree, cwd);
3535 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3536 goto done;
3537 else
3538 error = NULL;
3539 if (worktree) {
3540 repo_path =
3541 strdup(got_worktree_get_repo_path(worktree));
3542 if (repo_path == NULL)
3543 error = got_error_from_errno("strdup");
3544 if (error)
3545 goto done;
3546 } else {
3547 repo_path = strdup(cwd);
3548 if (repo_path == NULL) {
3549 error = got_error_from_errno("strdup");
3550 goto done;
3555 error = got_repo_open(&repo, repo_path, NULL);
3556 if (error != NULL)
3557 goto done;
3559 error = apply_unveil(got_repo_get_path(repo), do_list,
3560 worktree ? got_worktree_get_root_path(worktree) : NULL);
3561 if (error)
3562 goto done;
3564 if (do_show)
3565 error = show_current_branch(repo, worktree);
3566 else if (do_list)
3567 error = list_branches(repo, worktree);
3568 else if (delref)
3569 error = delete_branch(repo, worktree, delref);
3570 else {
3571 struct got_object_id *commit_id;
3572 if (commit_id_arg == NULL)
3573 commit_id_arg = worktree ?
3574 got_worktree_get_head_ref_name(worktree) :
3575 GOT_REF_HEAD;
3576 error = got_repo_match_object_id(&commit_id, NULL,
3577 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3578 if (error)
3579 goto done;
3580 error = add_branch(repo, argv[0], commit_id);
3581 free(commit_id);
3583 done:
3584 if (repo)
3585 got_repo_close(repo);
3586 if (worktree)
3587 got_worktree_close(worktree);
3588 free(cwd);
3589 free(repo_path);
3590 return error;
3594 __dead static void
3595 usage_tag(void)
3597 fprintf(stderr,
3598 "usage: %s tag [-r repository] | -l | "
3599 "[-m message] name [commit]\n", getprogname());
3600 exit(1);
3603 #if 0
3604 static const struct got_error *
3605 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3607 const struct got_error *err = NULL;
3608 struct got_reflist_entry *re, *se, *new;
3609 struct got_object_id *re_id, *se_id;
3610 struct got_tag_object *re_tag, *se_tag;
3611 time_t re_time, se_time;
3613 SIMPLEQ_FOREACH(re, tags, entry) {
3614 se = SIMPLEQ_FIRST(sorted);
3615 if (se == NULL) {
3616 err = got_reflist_entry_dup(&new, re);
3617 if (err)
3618 return err;
3619 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3620 continue;
3621 } else {
3622 err = got_ref_resolve(&re_id, repo, re->ref);
3623 if (err)
3624 break;
3625 err = got_object_open_as_tag(&re_tag, repo, re_id);
3626 free(re_id);
3627 if (err)
3628 break;
3629 re_time = got_object_tag_get_tagger_time(re_tag);
3630 got_object_tag_close(re_tag);
3633 while (se) {
3634 err = got_ref_resolve(&se_id, repo, re->ref);
3635 if (err)
3636 break;
3637 err = got_object_open_as_tag(&se_tag, repo, se_id);
3638 free(se_id);
3639 if (err)
3640 break;
3641 se_time = got_object_tag_get_tagger_time(se_tag);
3642 got_object_tag_close(se_tag);
3644 if (se_time > re_time) {
3645 err = got_reflist_entry_dup(&new, re);
3646 if (err)
3647 return err;
3648 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3649 break;
3651 se = SIMPLEQ_NEXT(se, entry);
3652 continue;
3655 done:
3656 return err;
3658 #endif
3660 static const struct got_error *
3661 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3663 static const struct got_error *err = NULL;
3664 struct got_reflist_head refs;
3665 struct got_reflist_entry *re;
3667 SIMPLEQ_INIT(&refs);
3669 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3670 if (err)
3671 return err;
3673 SIMPLEQ_FOREACH(re, &refs, entry) {
3674 const char *refname;
3675 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3676 char datebuf[26];
3677 const char *tagger;
3678 time_t tagger_time;
3679 struct got_object_id *id;
3680 struct got_tag_object *tag;
3681 struct got_commit_object *commit = NULL;
3683 refname = got_ref_get_name(re->ref);
3684 if (strncmp(refname, "refs/tags/", 10) != 0)
3685 continue;
3686 refname += 10;
3687 refstr = got_ref_to_str(re->ref);
3688 if (refstr == NULL) {
3689 err = got_error_from_errno("got_ref_to_str");
3690 break;
3692 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3693 free(refstr);
3695 err = got_ref_resolve(&id, repo, re->ref);
3696 if (err)
3697 break;
3698 err = got_object_open_as_tag(&tag, repo, id);
3699 if (err) {
3700 if (err->code != GOT_ERR_OBJ_TYPE) {
3701 free(id);
3702 break;
3704 /* "lightweight" tag */
3705 err = got_object_open_as_commit(&commit, repo, id);
3706 if (err) {
3707 free(id);
3708 break;
3710 tagger = got_object_commit_get_committer(commit);
3711 tagger_time =
3712 got_object_commit_get_committer_time(commit);
3713 err = got_object_id_str(&id_str, id);
3714 free(id);
3715 if (err)
3716 break;
3717 } else {
3718 free(id);
3719 tagger = got_object_tag_get_tagger(tag);
3720 tagger_time = got_object_tag_get_tagger_time(tag);
3721 err = got_object_id_str(&id_str,
3722 got_object_tag_get_object_id(tag));
3723 if (err)
3724 break;
3726 printf("from: %s\n", tagger);
3727 datestr = get_datestr(&tagger_time, datebuf);
3728 if (datestr)
3729 printf("date: %s UTC\n", datestr);
3730 if (commit)
3731 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3732 else {
3733 switch (got_object_tag_get_object_type(tag)) {
3734 case GOT_OBJ_TYPE_BLOB:
3735 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3736 id_str);
3737 break;
3738 case GOT_OBJ_TYPE_TREE:
3739 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3740 id_str);
3741 break;
3742 case GOT_OBJ_TYPE_COMMIT:
3743 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3744 id_str);
3745 break;
3746 case GOT_OBJ_TYPE_TAG:
3747 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3748 id_str);
3749 break;
3750 default:
3751 break;
3754 free(id_str);
3755 if (commit) {
3756 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3757 if (err)
3758 break;
3759 got_object_commit_close(commit);
3760 } else {
3761 tagmsg0 = strdup(got_object_tag_get_message(tag));
3762 got_object_tag_close(tag);
3763 if (tagmsg0 == NULL) {
3764 err = got_error_from_errno("strdup");
3765 break;
3769 tagmsg = tagmsg0;
3770 do {
3771 line = strsep(&tagmsg, "\n");
3772 if (line)
3773 printf(" %s\n", line);
3774 } while (line);
3775 free(tagmsg0);
3778 got_ref_list_free(&refs);
3779 return NULL;
3782 static const struct got_error *
3783 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3784 const char *tag_name, const char *repo_path)
3786 const struct got_error *err = NULL;
3787 char *template = NULL, *initial_content = NULL;
3788 char *editor = NULL;
3789 int fd = -1;
3791 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3792 err = got_error_from_errno("asprintf");
3793 goto done;
3796 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3797 commit_id_str, tag_name) == -1) {
3798 err = got_error_from_errno("asprintf");
3799 goto done;
3802 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3803 if (err)
3804 goto done;
3806 dprintf(fd, initial_content);
3807 close(fd);
3809 err = get_editor(&editor);
3810 if (err)
3811 goto done;
3812 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3813 done:
3814 free(initial_content);
3815 free(template);
3816 free(editor);
3818 /* Editor is done; we can now apply unveil(2) */
3819 if (err == NULL) {
3820 err = apply_unveil(repo_path, 0, NULL);
3821 if (err) {
3822 free(*tagmsg);
3823 *tagmsg = NULL;
3826 return err;
3829 static const struct got_error *
3830 add_tag(struct got_repository *repo, const char *tag_name,
3831 const char *commit_arg, const char *tagmsg_arg)
3833 const struct got_error *err = NULL;
3834 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3835 char *label = NULL, *commit_id_str = NULL;
3836 struct got_reference *ref = NULL;
3837 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3838 char *tagmsg_path = NULL, *tag_id_str = NULL;
3839 int preserve_tagmsg = 0;
3842 * Don't let the user create a tag name with a leading '-'.
3843 * While technically a valid reference name, this case is usually
3844 * an unintended typo.
3846 if (tag_name[0] == '-')
3847 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3849 err = get_author(&tagger, repo);
3850 if (err)
3851 return err;
3853 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3854 GOT_OBJ_TYPE_COMMIT, 1, repo);
3855 if (err)
3856 goto done;
3858 err = got_object_id_str(&commit_id_str, commit_id);
3859 if (err)
3860 goto done;
3862 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3863 refname = strdup(tag_name);
3864 if (refname == NULL) {
3865 err = got_error_from_errno("strdup");
3866 goto done;
3868 tag_name += 10;
3869 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3870 err = got_error_from_errno("asprintf");
3871 goto done;
3874 err = got_ref_open(&ref, repo, refname, 0);
3875 if (err == NULL) {
3876 err = got_error(GOT_ERR_TAG_EXISTS);
3877 goto done;
3878 } else if (err->code != GOT_ERR_NOT_REF)
3879 goto done;
3881 if (tagmsg_arg == NULL) {
3882 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3883 tag_name, got_repo_get_path(repo));
3884 if (err) {
3885 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3886 tagmsg_path != NULL)
3887 preserve_tagmsg = 1;
3888 goto done;
3892 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3893 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3894 if (err) {
3895 if (tagmsg_path)
3896 preserve_tagmsg = 1;
3897 goto done;
3900 err = got_ref_alloc(&ref, refname, tag_id);
3901 if (err) {
3902 if (tagmsg_path)
3903 preserve_tagmsg = 1;
3904 goto done;
3907 err = got_ref_write(ref, repo);
3908 if (err) {
3909 if (tagmsg_path)
3910 preserve_tagmsg = 1;
3911 goto done;
3914 err = got_object_id_str(&tag_id_str, tag_id);
3915 if (err) {
3916 if (tagmsg_path)
3917 preserve_tagmsg = 1;
3918 goto done;
3920 printf("Created tag %s\n", tag_id_str);
3921 done:
3922 if (preserve_tagmsg) {
3923 fprintf(stderr, "%s: tag message preserved in %s\n",
3924 getprogname(), tagmsg_path);
3925 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3926 err = got_error_from_errno2("unlink", tagmsg_path);
3927 free(tag_id_str);
3928 if (ref)
3929 got_ref_close(ref);
3930 free(commit_id);
3931 free(commit_id_str);
3932 free(refname);
3933 free(tagmsg);
3934 free(tagmsg_path);
3935 free(tagger);
3936 return err;
3939 static const struct got_error *
3940 cmd_tag(int argc, char *argv[])
3942 const struct got_error *error = NULL;
3943 struct got_repository *repo = NULL;
3944 struct got_worktree *worktree = NULL;
3945 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3946 char *gitconfig_path = NULL;
3947 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3948 int ch, do_list = 0;
3950 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3951 switch (ch) {
3952 case 'm':
3953 tagmsg = optarg;
3954 break;
3955 case 'r':
3956 repo_path = realpath(optarg, NULL);
3957 if (repo_path == NULL)
3958 return got_error_from_errno2("realpath",
3959 optarg);
3960 got_path_strip_trailing_slashes(repo_path);
3961 break;
3962 case 'l':
3963 do_list = 1;
3964 break;
3965 default:
3966 usage_tag();
3967 /* NOTREACHED */
3971 argc -= optind;
3972 argv += optind;
3974 if (do_list) {
3975 if (tagmsg)
3976 errx(1, "-l and -m options are mutually exclusive\n");
3977 if (argc > 0)
3978 usage_tag();
3979 } else if (argc < 1 || argc > 2)
3980 usage_tag();
3981 else if (argc > 1)
3982 commit_id_arg = argv[1];
3983 tag_name = argv[0];
3985 #ifndef PROFILE
3986 if (do_list) {
3987 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3988 NULL) == -1)
3989 err(1, "pledge");
3990 } else {
3991 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3992 "sendfd unveil", NULL) == -1)
3993 err(1, "pledge");
3995 #endif
3996 cwd = getcwd(NULL, 0);
3997 if (cwd == NULL) {
3998 error = got_error_from_errno("getcwd");
3999 goto done;
4002 if (repo_path == NULL) {
4003 error = got_worktree_open(&worktree, cwd);
4004 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4005 goto done;
4006 else
4007 error = NULL;
4008 if (worktree) {
4009 repo_path =
4010 strdup(got_worktree_get_repo_path(worktree));
4011 if (repo_path == NULL)
4012 error = got_error_from_errno("strdup");
4013 if (error)
4014 goto done;
4015 } else {
4016 repo_path = strdup(cwd);
4017 if (repo_path == NULL) {
4018 error = got_error_from_errno("strdup");
4019 goto done;
4024 if (do_list) {
4025 error = got_repo_open(&repo, repo_path, NULL);
4026 if (error != NULL)
4027 goto done;
4028 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4029 if (error)
4030 goto done;
4031 error = list_tags(repo, worktree);
4032 } else {
4033 error = get_gitconfig_path(&gitconfig_path);
4034 if (error)
4035 goto done;
4036 error = got_repo_open(&repo, repo_path, gitconfig_path);
4037 if (error != NULL)
4038 goto done;
4040 if (tagmsg) {
4041 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4042 if (error)
4043 goto done;
4046 if (commit_id_arg == NULL) {
4047 struct got_reference *head_ref;
4048 struct got_object_id *commit_id;
4049 error = got_ref_open(&head_ref, repo,
4050 worktree ? got_worktree_get_head_ref_name(worktree)
4051 : GOT_REF_HEAD, 0);
4052 if (error)
4053 goto done;
4054 error = got_ref_resolve(&commit_id, repo, head_ref);
4055 got_ref_close(head_ref);
4056 if (error)
4057 goto done;
4058 error = got_object_id_str(&commit_id_str, commit_id);
4059 free(commit_id);
4060 if (error)
4061 goto done;
4064 error = add_tag(repo, tag_name,
4065 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4067 done:
4068 if (repo)
4069 got_repo_close(repo);
4070 if (worktree)
4071 got_worktree_close(worktree);
4072 free(cwd);
4073 free(repo_path);
4074 free(gitconfig_path);
4075 free(commit_id_str);
4076 return error;
4079 __dead static void
4080 usage_add(void)
4082 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4083 getprogname());
4084 exit(1);
4087 static const struct got_error *
4088 add_progress(void *arg, unsigned char status, const char *path)
4090 while (path[0] == '/')
4091 path++;
4092 printf("%c %s\n", status, path);
4093 return NULL;
4096 static const struct got_error *
4097 cmd_add(int argc, char *argv[])
4099 const struct got_error *error = NULL;
4100 struct got_repository *repo = NULL;
4101 struct got_worktree *worktree = NULL;
4102 char *cwd = NULL;
4103 struct got_pathlist_head paths;
4104 struct got_pathlist_entry *pe;
4105 int ch, can_recurse = 0, no_ignores = 0;
4107 TAILQ_INIT(&paths);
4109 while ((ch = getopt(argc, argv, "IR")) != -1) {
4110 switch (ch) {
4111 case 'I':
4112 no_ignores = 1;
4113 break;
4114 case 'R':
4115 can_recurse = 1;
4116 break;
4117 default:
4118 usage_add();
4119 /* NOTREACHED */
4123 argc -= optind;
4124 argv += optind;
4126 #ifndef PROFILE
4127 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4128 NULL) == -1)
4129 err(1, "pledge");
4130 #endif
4131 if (argc < 1)
4132 usage_add();
4134 cwd = getcwd(NULL, 0);
4135 if (cwd == NULL) {
4136 error = got_error_from_errno("getcwd");
4137 goto done;
4140 error = got_worktree_open(&worktree, cwd);
4141 if (error)
4142 goto done;
4144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4145 NULL);
4146 if (error != NULL)
4147 goto done;
4149 error = apply_unveil(got_repo_get_path(repo), 1,
4150 got_worktree_get_root_path(worktree));
4151 if (error)
4152 goto done;
4154 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4155 if (error)
4156 goto done;
4158 if (!can_recurse && no_ignores) {
4159 error = got_error_msg(GOT_ERR_BAD_PATH,
4160 "disregarding ignores requires -R option");
4161 goto done;
4165 if (!can_recurse) {
4166 char *ondisk_path;
4167 struct stat sb;
4168 TAILQ_FOREACH(pe, &paths, entry) {
4169 if (asprintf(&ondisk_path, "%s/%s",
4170 got_worktree_get_root_path(worktree),
4171 pe->path) == -1) {
4172 error = got_error_from_errno("asprintf");
4173 goto done;
4175 if (lstat(ondisk_path, &sb) == -1) {
4176 if (errno == ENOENT) {
4177 free(ondisk_path);
4178 continue;
4180 error = got_error_from_errno2("lstat",
4181 ondisk_path);
4182 free(ondisk_path);
4183 goto done;
4185 free(ondisk_path);
4186 if (S_ISDIR(sb.st_mode)) {
4187 error = got_error_msg(GOT_ERR_BAD_PATH,
4188 "adding directories requires -R option");
4189 goto done;
4194 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4195 NULL, repo, no_ignores);
4196 done:
4197 if (repo)
4198 got_repo_close(repo);
4199 if (worktree)
4200 got_worktree_close(worktree);
4201 TAILQ_FOREACH(pe, &paths, entry)
4202 free((char *)pe->path);
4203 got_pathlist_free(&paths);
4204 free(cwd);
4205 return error;
4208 __dead static void
4209 usage_remove(void)
4211 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4212 getprogname());
4213 exit(1);
4216 static const struct got_error *
4217 print_remove_status(void *arg, unsigned char status,
4218 unsigned char staged_status, const char *path)
4220 while (path[0] == '/')
4221 path++;
4222 if (status == GOT_STATUS_NONEXISTENT)
4223 return NULL;
4224 if (status == staged_status && (status == GOT_STATUS_DELETE))
4225 status = GOT_STATUS_NO_CHANGE;
4226 printf("%c%c %s\n", status, staged_status, path);
4227 return NULL;
4230 static const struct got_error *
4231 cmd_remove(int argc, char *argv[])
4233 const struct got_error *error = NULL;
4234 struct got_worktree *worktree = NULL;
4235 struct got_repository *repo = NULL;
4236 char *cwd = NULL;
4237 struct got_pathlist_head paths;
4238 struct got_pathlist_entry *pe;
4239 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4241 TAILQ_INIT(&paths);
4243 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4244 switch (ch) {
4245 case 'f':
4246 delete_local_mods = 1;
4247 break;
4248 case 'k':
4249 keep_on_disk = 1;
4250 break;
4251 case 'R':
4252 can_recurse = 1;
4253 break;
4254 default:
4255 usage_remove();
4256 /* NOTREACHED */
4260 argc -= optind;
4261 argv += optind;
4263 #ifndef PROFILE
4264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4265 NULL) == -1)
4266 err(1, "pledge");
4267 #endif
4268 if (argc < 1)
4269 usage_remove();
4271 cwd = getcwd(NULL, 0);
4272 if (cwd == NULL) {
4273 error = got_error_from_errno("getcwd");
4274 goto done;
4276 error = got_worktree_open(&worktree, cwd);
4277 if (error)
4278 goto done;
4280 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4281 NULL);
4282 if (error)
4283 goto done;
4285 error = apply_unveil(got_repo_get_path(repo), 1,
4286 got_worktree_get_root_path(worktree));
4287 if (error)
4288 goto done;
4290 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4291 if (error)
4292 goto done;
4294 if (!can_recurse) {
4295 char *ondisk_path;
4296 struct stat sb;
4297 TAILQ_FOREACH(pe, &paths, entry) {
4298 if (asprintf(&ondisk_path, "%s/%s",
4299 got_worktree_get_root_path(worktree),
4300 pe->path) == -1) {
4301 error = got_error_from_errno("asprintf");
4302 goto done;
4304 if (lstat(ondisk_path, &sb) == -1) {
4305 if (errno == ENOENT) {
4306 free(ondisk_path);
4307 continue;
4309 error = got_error_from_errno2("lstat",
4310 ondisk_path);
4311 free(ondisk_path);
4312 goto done;
4314 free(ondisk_path);
4315 if (S_ISDIR(sb.st_mode)) {
4316 error = got_error_msg(GOT_ERR_BAD_PATH,
4317 "removing directories requires -R option");
4318 goto done;
4323 error = got_worktree_schedule_delete(worktree, &paths,
4324 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4325 done:
4326 if (repo)
4327 got_repo_close(repo);
4328 if (worktree)
4329 got_worktree_close(worktree);
4330 TAILQ_FOREACH(pe, &paths, entry)
4331 free((char *)pe->path);
4332 got_pathlist_free(&paths);
4333 free(cwd);
4334 return error;
4337 __dead static void
4338 usage_revert(void)
4340 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4341 "path ...\n", getprogname());
4342 exit(1);
4345 static const struct got_error *
4346 revert_progress(void *arg, unsigned char status, const char *path)
4348 if (status == GOT_STATUS_UNVERSIONED)
4349 return NULL;
4351 while (path[0] == '/')
4352 path++;
4353 printf("%c %s\n", status, path);
4354 return NULL;
4357 struct choose_patch_arg {
4358 FILE *patch_script_file;
4359 const char *action;
4362 static const struct got_error *
4363 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4364 int nchanges, const char *action)
4366 char *line = NULL;
4367 size_t linesize = 0;
4368 ssize_t linelen;
4370 switch (status) {
4371 case GOT_STATUS_ADD:
4372 printf("A %s\n%s this addition? [y/n] ", path, action);
4373 break;
4374 case GOT_STATUS_DELETE:
4375 printf("D %s\n%s this deletion? [y/n] ", path, action);
4376 break;
4377 case GOT_STATUS_MODIFY:
4378 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4379 return got_error_from_errno("fseek");
4380 printf(GOT_COMMIT_SEP_STR);
4381 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4382 printf("%s", line);
4383 if (ferror(patch_file))
4384 return got_error_from_errno("getline");
4385 printf(GOT_COMMIT_SEP_STR);
4386 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4387 path, n, nchanges, action);
4388 break;
4389 default:
4390 return got_error_path(path, GOT_ERR_FILE_STATUS);
4393 return NULL;
4396 static const struct got_error *
4397 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4398 FILE *patch_file, int n, int nchanges)
4400 const struct got_error *err = NULL;
4401 char *line = NULL;
4402 size_t linesize = 0;
4403 ssize_t linelen;
4404 int resp = ' ';
4405 struct choose_patch_arg *a = arg;
4407 *choice = GOT_PATCH_CHOICE_NONE;
4409 if (a->patch_script_file) {
4410 char *nl;
4411 err = show_change(status, path, patch_file, n, nchanges,
4412 a->action);
4413 if (err)
4414 return err;
4415 linelen = getline(&line, &linesize, a->patch_script_file);
4416 if (linelen == -1) {
4417 if (ferror(a->patch_script_file))
4418 return got_error_from_errno("getline");
4419 return NULL;
4421 nl = strchr(line, '\n');
4422 if (nl)
4423 *nl = '\0';
4424 if (strcmp(line, "y") == 0) {
4425 *choice = GOT_PATCH_CHOICE_YES;
4426 printf("y\n");
4427 } else if (strcmp(line, "n") == 0) {
4428 *choice = GOT_PATCH_CHOICE_NO;
4429 printf("n\n");
4430 } else if (strcmp(line, "q") == 0 &&
4431 status == GOT_STATUS_MODIFY) {
4432 *choice = GOT_PATCH_CHOICE_QUIT;
4433 printf("q\n");
4434 } else
4435 printf("invalid response '%s'\n", line);
4436 free(line);
4437 return NULL;
4440 while (resp != 'y' && resp != 'n' && resp != 'q') {
4441 err = show_change(status, path, patch_file, n, nchanges,
4442 a->action);
4443 if (err)
4444 return err;
4445 resp = getchar();
4446 if (resp == '\n')
4447 resp = getchar();
4448 if (status == GOT_STATUS_MODIFY) {
4449 if (resp != 'y' && resp != 'n' && resp != 'q') {
4450 printf("invalid response '%c'\n", resp);
4451 resp = ' ';
4453 } else if (resp != 'y' && resp != 'n') {
4454 printf("invalid response '%c'\n", resp);
4455 resp = ' ';
4459 if (resp == 'y')
4460 *choice = GOT_PATCH_CHOICE_YES;
4461 else if (resp == 'n')
4462 *choice = GOT_PATCH_CHOICE_NO;
4463 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4464 *choice = GOT_PATCH_CHOICE_QUIT;
4466 return NULL;
4470 static const struct got_error *
4471 cmd_revert(int argc, char *argv[])
4473 const struct got_error *error = NULL;
4474 struct got_worktree *worktree = NULL;
4475 struct got_repository *repo = NULL;
4476 char *cwd = NULL, *path = NULL;
4477 struct got_pathlist_head paths;
4478 struct got_pathlist_entry *pe;
4479 int ch, can_recurse = 0, pflag = 0;
4480 FILE *patch_script_file = NULL;
4481 const char *patch_script_path = NULL;
4482 struct choose_patch_arg cpa;
4484 TAILQ_INIT(&paths);
4486 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4487 switch (ch) {
4488 case 'p':
4489 pflag = 1;
4490 break;
4491 case 'F':
4492 patch_script_path = optarg;
4493 break;
4494 case 'R':
4495 can_recurse = 1;
4496 break;
4497 default:
4498 usage_revert();
4499 /* NOTREACHED */
4503 argc -= optind;
4504 argv += optind;
4506 #ifndef PROFILE
4507 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4508 "unveil", NULL) == -1)
4509 err(1, "pledge");
4510 #endif
4511 if (argc < 1)
4512 usage_revert();
4513 if (patch_script_path && !pflag)
4514 errx(1, "-F option can only be used together with -p option");
4516 cwd = getcwd(NULL, 0);
4517 if (cwd == NULL) {
4518 error = got_error_from_errno("getcwd");
4519 goto done;
4521 error = got_worktree_open(&worktree, cwd);
4522 if (error)
4523 goto done;
4525 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4526 NULL);
4527 if (error != NULL)
4528 goto done;
4530 if (patch_script_path) {
4531 patch_script_file = fopen(patch_script_path, "r");
4532 if (patch_script_file == NULL) {
4533 error = got_error_from_errno2("fopen",
4534 patch_script_path);
4535 goto done;
4538 error = apply_unveil(got_repo_get_path(repo), 1,
4539 got_worktree_get_root_path(worktree));
4540 if (error)
4541 goto done;
4543 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4544 if (error)
4545 goto done;
4547 if (!can_recurse) {
4548 char *ondisk_path;
4549 struct stat sb;
4550 TAILQ_FOREACH(pe, &paths, entry) {
4551 if (asprintf(&ondisk_path, "%s/%s",
4552 got_worktree_get_root_path(worktree),
4553 pe->path) == -1) {
4554 error = got_error_from_errno("asprintf");
4555 goto done;
4557 if (lstat(ondisk_path, &sb) == -1) {
4558 if (errno == ENOENT) {
4559 free(ondisk_path);
4560 continue;
4562 error = got_error_from_errno2("lstat",
4563 ondisk_path);
4564 free(ondisk_path);
4565 goto done;
4567 free(ondisk_path);
4568 if (S_ISDIR(sb.st_mode)) {
4569 error = got_error_msg(GOT_ERR_BAD_PATH,
4570 "reverting directories requires -R option");
4571 goto done;
4576 cpa.patch_script_file = patch_script_file;
4577 cpa.action = "revert";
4578 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4579 pflag ? choose_patch : NULL, &cpa, repo);
4580 done:
4581 if (patch_script_file && fclose(patch_script_file) == EOF &&
4582 error == NULL)
4583 error = got_error_from_errno2("fclose", patch_script_path);
4584 if (repo)
4585 got_repo_close(repo);
4586 if (worktree)
4587 got_worktree_close(worktree);
4588 free(path);
4589 free(cwd);
4590 return error;
4593 __dead static void
4594 usage_commit(void)
4596 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4597 getprogname());
4598 exit(1);
4601 struct collect_commit_logmsg_arg {
4602 const char *cmdline_log;
4603 const char *editor;
4604 const char *worktree_path;
4605 const char *branch_name;
4606 const char *repo_path;
4607 char *logmsg_path;
4611 static const struct got_error *
4612 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4613 void *arg)
4615 char *initial_content = NULL;
4616 struct got_pathlist_entry *pe;
4617 const struct got_error *err = NULL;
4618 char *template = NULL;
4619 struct collect_commit_logmsg_arg *a = arg;
4620 int fd;
4621 size_t len;
4623 /* if a message was specified on the command line, just use it */
4624 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4625 len = strlen(a->cmdline_log) + 1;
4626 *logmsg = malloc(len + 1);
4627 if (*logmsg == NULL)
4628 return got_error_from_errno("malloc");
4629 strlcpy(*logmsg, a->cmdline_log, len);
4630 return NULL;
4633 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4634 return got_error_from_errno("asprintf");
4636 if (asprintf(&initial_content,
4637 "\n# changes to be committed on branch %s:\n",
4638 a->branch_name) == -1)
4639 return got_error_from_errno("asprintf");
4641 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4642 if (err)
4643 goto done;
4645 dprintf(fd, initial_content);
4647 TAILQ_FOREACH(pe, commitable_paths, entry) {
4648 struct got_commitable *ct = pe->data;
4649 dprintf(fd, "# %c %s\n",
4650 got_commitable_get_status(ct),
4651 got_commitable_get_path(ct));
4653 close(fd);
4655 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4656 done:
4657 free(initial_content);
4658 free(template);
4660 /* Editor is done; we can now apply unveil(2) */
4661 if (err == NULL) {
4662 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4663 if (err) {
4664 free(*logmsg);
4665 *logmsg = NULL;
4668 return err;
4671 static const struct got_error *
4672 cmd_commit(int argc, char *argv[])
4674 const struct got_error *error = NULL;
4675 struct got_worktree *worktree = NULL;
4676 struct got_repository *repo = NULL;
4677 char *cwd = NULL, *id_str = NULL;
4678 struct got_object_id *id = NULL;
4679 const char *logmsg = NULL;
4680 struct collect_commit_logmsg_arg cl_arg;
4681 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4682 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4683 struct got_pathlist_head paths;
4685 TAILQ_INIT(&paths);
4686 cl_arg.logmsg_path = NULL;
4688 while ((ch = getopt(argc, argv, "m:")) != -1) {
4689 switch (ch) {
4690 case 'm':
4691 logmsg = optarg;
4692 break;
4693 default:
4694 usage_commit();
4695 /* NOTREACHED */
4699 argc -= optind;
4700 argv += optind;
4702 #ifndef PROFILE
4703 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4704 "unveil", NULL) == -1)
4705 err(1, "pledge");
4706 #endif
4707 cwd = getcwd(NULL, 0);
4708 if (cwd == NULL) {
4709 error = got_error_from_errno("getcwd");
4710 goto done;
4712 error = got_worktree_open(&worktree, cwd);
4713 if (error)
4714 goto done;
4716 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4717 if (error)
4718 goto done;
4719 if (rebase_in_progress) {
4720 error = got_error(GOT_ERR_REBASING);
4721 goto done;
4724 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4725 worktree);
4726 if (error)
4727 goto done;
4729 error = get_gitconfig_path(&gitconfig_path);
4730 if (error)
4731 goto done;
4732 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4733 gitconfig_path);
4734 if (error != NULL)
4735 goto done;
4737 error = get_author(&author, repo);
4738 if (error)
4739 return error;
4742 * unveil(2) traverses exec(2); if an editor is used we have
4743 * to apply unveil after the log message has been written.
4745 if (logmsg == NULL || strlen(logmsg) == 0)
4746 error = get_editor(&editor);
4747 else
4748 error = apply_unveil(got_repo_get_path(repo), 0,
4749 got_worktree_get_root_path(worktree));
4750 if (error)
4751 goto done;
4753 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4754 if (error)
4755 goto done;
4757 cl_arg.editor = editor;
4758 cl_arg.cmdline_log = logmsg;
4759 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4760 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4761 if (!histedit_in_progress) {
4762 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4763 error = got_error(GOT_ERR_COMMIT_BRANCH);
4764 goto done;
4766 cl_arg.branch_name += 11;
4768 cl_arg.repo_path = got_repo_get_path(repo);
4769 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4770 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4771 if (error) {
4772 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4773 cl_arg.logmsg_path != NULL)
4774 preserve_logmsg = 1;
4775 goto done;
4778 error = got_object_id_str(&id_str, id);
4779 if (error)
4780 goto done;
4781 printf("Created commit %s\n", id_str);
4782 done:
4783 if (preserve_logmsg) {
4784 fprintf(stderr, "%s: log message preserved in %s\n",
4785 getprogname(), cl_arg.logmsg_path);
4786 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4787 error == NULL)
4788 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4789 free(cl_arg.logmsg_path);
4790 if (repo)
4791 got_repo_close(repo);
4792 if (worktree)
4793 got_worktree_close(worktree);
4794 free(cwd);
4795 free(id_str);
4796 free(gitconfig_path);
4797 free(editor);
4798 free(author);
4799 return error;
4802 __dead static void
4803 usage_cherrypick(void)
4805 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4806 exit(1);
4809 static const struct got_error *
4810 cmd_cherrypick(int argc, char *argv[])
4812 const struct got_error *error = NULL;
4813 struct got_worktree *worktree = NULL;
4814 struct got_repository *repo = NULL;
4815 char *cwd = NULL, *commit_id_str = NULL;
4816 struct got_object_id *commit_id = NULL;
4817 struct got_commit_object *commit = NULL;
4818 struct got_object_qid *pid;
4819 struct got_reference *head_ref = NULL;
4820 int ch, did_something = 0;
4822 while ((ch = getopt(argc, argv, "")) != -1) {
4823 switch (ch) {
4824 default:
4825 usage_cherrypick();
4826 /* NOTREACHED */
4830 argc -= optind;
4831 argv += optind;
4833 #ifndef PROFILE
4834 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4835 "unveil", NULL) == -1)
4836 err(1, "pledge");
4837 #endif
4838 if (argc != 1)
4839 usage_cherrypick();
4841 cwd = getcwd(NULL, 0);
4842 if (cwd == NULL) {
4843 error = got_error_from_errno("getcwd");
4844 goto done;
4846 error = got_worktree_open(&worktree, cwd);
4847 if (error)
4848 goto done;
4850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4851 NULL);
4852 if (error != NULL)
4853 goto done;
4855 error = apply_unveil(got_repo_get_path(repo), 0,
4856 got_worktree_get_root_path(worktree));
4857 if (error)
4858 goto done;
4860 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4861 GOT_OBJ_TYPE_COMMIT, repo);
4862 if (error != NULL) {
4863 struct got_reference *ref;
4864 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4865 goto done;
4866 error = got_ref_open(&ref, repo, argv[0], 0);
4867 if (error != NULL)
4868 goto done;
4869 error = got_ref_resolve(&commit_id, repo, ref);
4870 got_ref_close(ref);
4871 if (error != NULL)
4872 goto done;
4874 error = got_object_id_str(&commit_id_str, commit_id);
4875 if (error)
4876 goto done;
4878 error = got_ref_open(&head_ref, repo,
4879 got_worktree_get_head_ref_name(worktree), 0);
4880 if (error != NULL)
4881 goto done;
4883 error = check_same_branch(commit_id, head_ref, NULL, repo);
4884 if (error) {
4885 if (error->code != GOT_ERR_ANCESTRY)
4886 goto done;
4887 error = NULL;
4888 } else {
4889 error = got_error(GOT_ERR_SAME_BRANCH);
4890 goto done;
4893 error = got_object_open_as_commit(&commit, repo, commit_id);
4894 if (error)
4895 goto done;
4896 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4897 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4898 commit_id, repo, update_progress, &did_something, check_cancelled,
4899 NULL);
4900 if (error != NULL)
4901 goto done;
4903 if (did_something)
4904 printf("Merged commit %s\n", commit_id_str);
4905 done:
4906 if (commit)
4907 got_object_commit_close(commit);
4908 free(commit_id_str);
4909 if (head_ref)
4910 got_ref_close(head_ref);
4911 if (worktree)
4912 got_worktree_close(worktree);
4913 if (repo)
4914 got_repo_close(repo);
4915 return error;
4918 __dead static void
4919 usage_backout(void)
4921 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4922 exit(1);
4925 static const struct got_error *
4926 cmd_backout(int argc, char *argv[])
4928 const struct got_error *error = NULL;
4929 struct got_worktree *worktree = NULL;
4930 struct got_repository *repo = NULL;
4931 char *cwd = NULL, *commit_id_str = NULL;
4932 struct got_object_id *commit_id = NULL;
4933 struct got_commit_object *commit = NULL;
4934 struct got_object_qid *pid;
4935 struct got_reference *head_ref = NULL;
4936 int ch, did_something = 0;
4938 while ((ch = getopt(argc, argv, "")) != -1) {
4939 switch (ch) {
4940 default:
4941 usage_backout();
4942 /* NOTREACHED */
4946 argc -= optind;
4947 argv += optind;
4949 #ifndef PROFILE
4950 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4951 "unveil", NULL) == -1)
4952 err(1, "pledge");
4953 #endif
4954 if (argc != 1)
4955 usage_backout();
4957 cwd = getcwd(NULL, 0);
4958 if (cwd == NULL) {
4959 error = got_error_from_errno("getcwd");
4960 goto done;
4962 error = got_worktree_open(&worktree, cwd);
4963 if (error)
4964 goto done;
4966 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4967 NULL);
4968 if (error != NULL)
4969 goto done;
4971 error = apply_unveil(got_repo_get_path(repo), 0,
4972 got_worktree_get_root_path(worktree));
4973 if (error)
4974 goto done;
4976 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4977 GOT_OBJ_TYPE_COMMIT, repo);
4978 if (error != NULL) {
4979 struct got_reference *ref;
4980 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4981 goto done;
4982 error = got_ref_open(&ref, repo, argv[0], 0);
4983 if (error != NULL)
4984 goto done;
4985 error = got_ref_resolve(&commit_id, repo, ref);
4986 got_ref_close(ref);
4987 if (error != NULL)
4988 goto done;
4990 error = got_object_id_str(&commit_id_str, commit_id);
4991 if (error)
4992 goto done;
4994 error = got_ref_open(&head_ref, repo,
4995 got_worktree_get_head_ref_name(worktree), 0);
4996 if (error != NULL)
4997 goto done;
4999 error = check_same_branch(commit_id, head_ref, NULL, repo);
5000 if (error)
5001 goto done;
5003 error = got_object_open_as_commit(&commit, repo, commit_id);
5004 if (error)
5005 goto done;
5006 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5007 if (pid == NULL) {
5008 error = got_error(GOT_ERR_ROOT_COMMIT);
5009 goto done;
5012 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5013 update_progress, &did_something, check_cancelled, NULL);
5014 if (error != NULL)
5015 goto done;
5017 if (did_something)
5018 printf("Backed out commit %s\n", commit_id_str);
5019 done:
5020 if (commit)
5021 got_object_commit_close(commit);
5022 free(commit_id_str);
5023 if (head_ref)
5024 got_ref_close(head_ref);
5025 if (worktree)
5026 got_worktree_close(worktree);
5027 if (repo)
5028 got_repo_close(repo);
5029 return error;
5032 __dead static void
5033 usage_rebase(void)
5035 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5036 getprogname());
5037 exit(1);
5040 void
5041 trim_logmsg(char *logmsg, int limit)
5043 char *nl;
5044 size_t len;
5046 len = strlen(logmsg);
5047 if (len > limit)
5048 len = limit;
5049 logmsg[len] = '\0';
5050 nl = strchr(logmsg, '\n');
5051 if (nl)
5052 *nl = '\0';
5055 static const struct got_error *
5056 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5058 const struct got_error *err;
5059 char *logmsg0 = NULL;
5060 const char *s;
5062 err = got_object_commit_get_logmsg(&logmsg0, commit);
5063 if (err)
5064 return err;
5066 s = logmsg0;
5067 while (isspace((unsigned char)s[0]))
5068 s++;
5070 *logmsg = strdup(s);
5071 if (*logmsg == NULL) {
5072 err = got_error_from_errno("strdup");
5073 goto done;
5076 trim_logmsg(*logmsg, limit);
5077 done:
5078 free(logmsg0);
5079 return err;
5082 static const struct got_error *
5083 show_rebase_progress(struct got_commit_object *commit,
5084 struct got_object_id *old_id, struct got_object_id *new_id)
5086 const struct got_error *err;
5087 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5089 err = got_object_id_str(&old_id_str, old_id);
5090 if (err)
5091 goto done;
5093 if (new_id) {
5094 err = got_object_id_str(&new_id_str, new_id);
5095 if (err)
5096 goto done;
5099 old_id_str[12] = '\0';
5100 if (new_id_str)
5101 new_id_str[12] = '\0';
5103 err = get_short_logmsg(&logmsg, 42, commit);
5104 if (err)
5105 goto done;
5107 printf("%s -> %s: %s\n", old_id_str,
5108 new_id_str ? new_id_str : "no-op change", logmsg);
5109 done:
5110 free(old_id_str);
5111 free(new_id_str);
5112 return err;
5115 static const struct got_error *
5116 rebase_progress(void *arg, unsigned char status, const char *path)
5118 unsigned char *rebase_status = arg;
5120 while (path[0] == '/')
5121 path++;
5122 printf("%c %s\n", status, path);
5124 if (*rebase_status == GOT_STATUS_CONFLICT)
5125 return NULL;
5126 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5127 *rebase_status = status;
5128 return NULL;
5131 static const struct got_error *
5132 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5133 struct got_reference *branch, struct got_reference *new_base_branch,
5134 struct got_reference *tmp_branch, struct got_repository *repo)
5136 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5137 return got_worktree_rebase_complete(worktree, fileindex,
5138 new_base_branch, tmp_branch, branch, repo);
5141 static const struct got_error *
5142 rebase_commit(struct got_pathlist_head *merged_paths,
5143 struct got_worktree *worktree, struct got_fileindex *fileindex,
5144 struct got_reference *tmp_branch,
5145 struct got_object_id *commit_id, struct got_repository *repo)
5147 const struct got_error *error;
5148 struct got_commit_object *commit;
5149 struct got_object_id *new_commit_id;
5151 error = got_object_open_as_commit(&commit, repo, commit_id);
5152 if (error)
5153 return error;
5155 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5156 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5157 if (error) {
5158 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5159 goto done;
5160 error = show_rebase_progress(commit, commit_id, NULL);
5161 } else {
5162 error = show_rebase_progress(commit, commit_id, new_commit_id);
5163 free(new_commit_id);
5165 done:
5166 got_object_commit_close(commit);
5167 return error;
5170 struct check_path_prefix_arg {
5171 const char *path_prefix;
5172 size_t len;
5173 int errcode;
5176 static const struct got_error *
5177 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5178 struct got_blob_object *blob2, struct got_object_id *id1,
5179 struct got_object_id *id2, const char *path1, const char *path2,
5180 mode_t mode1, mode_t mode2, struct got_repository *repo)
5182 struct check_path_prefix_arg *a = arg;
5184 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5185 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5186 return got_error(a->errcode);
5188 return NULL;
5191 static const struct got_error *
5192 check_path_prefix(struct got_object_id *parent_id,
5193 struct got_object_id *commit_id, const char *path_prefix,
5194 int errcode, struct got_repository *repo)
5196 const struct got_error *err;
5197 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5198 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5199 struct check_path_prefix_arg cpp_arg;
5201 if (got_path_is_root_dir(path_prefix))
5202 return NULL;
5204 err = got_object_open_as_commit(&commit, repo, commit_id);
5205 if (err)
5206 goto done;
5208 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5209 if (err)
5210 goto done;
5212 err = got_object_open_as_tree(&tree1, repo,
5213 got_object_commit_get_tree_id(parent_commit));
5214 if (err)
5215 goto done;
5217 err = got_object_open_as_tree(&tree2, repo,
5218 got_object_commit_get_tree_id(commit));
5219 if (err)
5220 goto done;
5222 cpp_arg.path_prefix = path_prefix;
5223 while (cpp_arg.path_prefix[0] == '/')
5224 cpp_arg.path_prefix++;
5225 cpp_arg.len = strlen(cpp_arg.path_prefix);
5226 cpp_arg.errcode = errcode;
5227 err = got_diff_tree(tree1, tree2, "", "", repo,
5228 check_path_prefix_in_diff, &cpp_arg, 0);
5229 done:
5230 if (tree1)
5231 got_object_tree_close(tree1);
5232 if (tree2)
5233 got_object_tree_close(tree2);
5234 if (commit)
5235 got_object_commit_close(commit);
5236 if (parent_commit)
5237 got_object_commit_close(parent_commit);
5238 return err;
5241 static const struct got_error *
5242 collect_commits(struct got_object_id_queue *commits,
5243 struct got_object_id *initial_commit_id,
5244 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5245 const char *path_prefix, int path_prefix_errcode,
5246 struct got_repository *repo)
5248 const struct got_error *err = NULL;
5249 struct got_commit_graph *graph = NULL;
5250 struct got_object_id *parent_id = NULL;
5251 struct got_object_qid *qid;
5252 struct got_object_id *commit_id = initial_commit_id;
5254 err = got_commit_graph_open(&graph, "/", 1);
5255 if (err)
5256 return err;
5258 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5259 check_cancelled, NULL);
5260 if (err)
5261 goto done;
5262 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5263 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5264 check_cancelled, NULL);
5265 if (err) {
5266 if (err->code == GOT_ERR_ITER_COMPLETED) {
5267 err = got_error_msg(GOT_ERR_ANCESTRY,
5268 "ran out of commits to rebase before "
5269 "youngest common ancestor commit has "
5270 "been reached?!?");
5272 goto done;
5273 } else {
5274 err = check_path_prefix(parent_id, commit_id,
5275 path_prefix, path_prefix_errcode, repo);
5276 if (err)
5277 goto done;
5279 err = got_object_qid_alloc(&qid, commit_id);
5280 if (err)
5281 goto done;
5282 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5283 commit_id = parent_id;
5286 done:
5287 got_commit_graph_close(graph);
5288 return err;
5291 static const struct got_error *
5292 cmd_rebase(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_worktree *worktree = NULL;
5296 struct got_repository *repo = NULL;
5297 struct got_fileindex *fileindex = NULL;
5298 char *cwd = NULL;
5299 struct got_reference *branch = NULL;
5300 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5301 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5302 struct got_object_id *resume_commit_id = NULL;
5303 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5304 struct got_commit_object *commit = NULL;
5305 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5306 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5307 struct got_object_id_queue commits;
5308 struct got_pathlist_head merged_paths;
5309 const struct got_object_id_queue *parent_ids;
5310 struct got_object_qid *qid, *pid;
5312 SIMPLEQ_INIT(&commits);
5313 TAILQ_INIT(&merged_paths);
5315 while ((ch = getopt(argc, argv, "ac")) != -1) {
5316 switch (ch) {
5317 case 'a':
5318 abort_rebase = 1;
5319 break;
5320 case 'c':
5321 continue_rebase = 1;
5322 break;
5323 default:
5324 usage_rebase();
5325 /* NOTREACHED */
5329 argc -= optind;
5330 argv += optind;
5332 #ifndef PROFILE
5333 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5334 "unveil", NULL) == -1)
5335 err(1, "pledge");
5336 #endif
5337 if (abort_rebase && continue_rebase)
5338 usage_rebase();
5339 else if (abort_rebase || continue_rebase) {
5340 if (argc != 0)
5341 usage_rebase();
5342 } else if (argc != 1)
5343 usage_rebase();
5345 cwd = getcwd(NULL, 0);
5346 if (cwd == NULL) {
5347 error = got_error_from_errno("getcwd");
5348 goto done;
5350 error = got_worktree_open(&worktree, cwd);
5351 if (error)
5352 goto done;
5354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5355 NULL);
5356 if (error != NULL)
5357 goto done;
5359 error = apply_unveil(got_repo_get_path(repo), 0,
5360 got_worktree_get_root_path(worktree));
5361 if (error)
5362 goto done;
5364 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5365 if (error)
5366 goto done;
5368 if (abort_rebase) {
5369 int did_something;
5370 if (!rebase_in_progress) {
5371 error = got_error(GOT_ERR_NOT_REBASING);
5372 goto done;
5374 error = got_worktree_rebase_continue(&resume_commit_id,
5375 &new_base_branch, &tmp_branch, &branch, &fileindex,
5376 worktree, repo);
5377 if (error)
5378 goto done;
5379 printf("Switching work tree to %s\n",
5380 got_ref_get_symref_target(new_base_branch));
5381 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5382 new_base_branch, update_progress, &did_something);
5383 if (error)
5384 goto done;
5385 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5386 goto done; /* nothing else to do */
5389 if (continue_rebase) {
5390 if (!rebase_in_progress) {
5391 error = got_error(GOT_ERR_NOT_REBASING);
5392 goto done;
5394 error = got_worktree_rebase_continue(&resume_commit_id,
5395 &new_base_branch, &tmp_branch, &branch, &fileindex,
5396 worktree, repo);
5397 if (error)
5398 goto done;
5400 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5401 resume_commit_id, repo);
5402 if (error)
5403 goto done;
5405 yca_id = got_object_id_dup(resume_commit_id);
5406 if (yca_id == NULL) {
5407 error = got_error_from_errno("got_object_id_dup");
5408 goto done;
5410 } else {
5411 error = got_ref_open(&branch, repo, argv[0], 0);
5412 if (error != NULL)
5413 goto done;
5416 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5417 if (error)
5418 goto done;
5420 if (!continue_rebase) {
5421 struct got_object_id *base_commit_id;
5423 base_commit_id = got_worktree_get_base_commit_id(worktree);
5424 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5425 base_commit_id, branch_head_commit_id, repo,
5426 check_cancelled, NULL);
5427 if (error)
5428 goto done;
5429 if (yca_id == NULL) {
5430 error = got_error_msg(GOT_ERR_ANCESTRY,
5431 "specified branch shares no common ancestry "
5432 "with work tree's branch");
5433 goto done;
5436 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5437 if (error) {
5438 if (error->code != GOT_ERR_ANCESTRY)
5439 goto done;
5440 error = NULL;
5441 } else {
5442 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5443 "specified branch resolves to a commit which "
5444 "is already contained in work tree's branch");
5445 goto done;
5447 error = got_worktree_rebase_prepare(&new_base_branch,
5448 &tmp_branch, &fileindex, worktree, branch, repo);
5449 if (error)
5450 goto done;
5453 commit_id = branch_head_commit_id;
5454 error = got_object_open_as_commit(&commit, repo, commit_id);
5455 if (error)
5456 goto done;
5458 parent_ids = got_object_commit_get_parent_ids(commit);
5459 pid = SIMPLEQ_FIRST(parent_ids);
5460 if (pid == NULL) {
5461 if (!continue_rebase) {
5462 int did_something;
5463 error = got_worktree_rebase_abort(worktree, fileindex,
5464 repo, new_base_branch, update_progress,
5465 &did_something);
5466 if (error)
5467 goto done;
5468 printf("Rebase of %s aborted\n",
5469 got_ref_get_name(branch));
5471 error = got_error(GOT_ERR_EMPTY_REBASE);
5472 goto done;
5474 error = collect_commits(&commits, commit_id, pid->id,
5475 yca_id, got_worktree_get_path_prefix(worktree),
5476 GOT_ERR_REBASE_PATH, repo);
5477 got_object_commit_close(commit);
5478 commit = NULL;
5479 if (error)
5480 goto done;
5482 if (SIMPLEQ_EMPTY(&commits)) {
5483 if (continue_rebase) {
5484 error = rebase_complete(worktree, fileindex,
5485 branch, new_base_branch, tmp_branch, repo);
5486 goto done;
5487 } else {
5488 /* Fast-forward the reference of the branch. */
5489 struct got_object_id *new_head_commit_id;
5490 char *id_str;
5491 error = got_ref_resolve(&new_head_commit_id, repo,
5492 new_base_branch);
5493 if (error)
5494 goto done;
5495 error = got_object_id_str(&id_str, new_head_commit_id);
5496 printf("Forwarding %s to commit %s\n",
5497 got_ref_get_name(branch), id_str);
5498 free(id_str);
5499 error = got_ref_change_ref(branch,
5500 new_head_commit_id);
5501 if (error)
5502 goto done;
5506 pid = NULL;
5507 SIMPLEQ_FOREACH(qid, &commits, entry) {
5508 commit_id = qid->id;
5509 parent_id = pid ? pid->id : yca_id;
5510 pid = qid;
5512 error = got_worktree_rebase_merge_files(&merged_paths,
5513 worktree, fileindex, parent_id, commit_id, repo,
5514 rebase_progress, &rebase_status, check_cancelled, NULL);
5515 if (error)
5516 goto done;
5518 if (rebase_status == GOT_STATUS_CONFLICT) {
5519 got_worktree_rebase_pathlist_free(&merged_paths);
5520 break;
5523 error = rebase_commit(&merged_paths, worktree, fileindex,
5524 tmp_branch, commit_id, repo);
5525 got_worktree_rebase_pathlist_free(&merged_paths);
5526 if (error)
5527 goto done;
5530 if (rebase_status == GOT_STATUS_CONFLICT) {
5531 error = got_worktree_rebase_postpone(worktree, fileindex);
5532 if (error)
5533 goto done;
5534 error = got_error_msg(GOT_ERR_CONFLICTS,
5535 "conflicts must be resolved before rebasing can continue");
5536 } else
5537 error = rebase_complete(worktree, fileindex, branch,
5538 new_base_branch, tmp_branch, repo);
5539 done:
5540 got_object_id_queue_free(&commits);
5541 free(branch_head_commit_id);
5542 free(resume_commit_id);
5543 free(yca_id);
5544 if (commit)
5545 got_object_commit_close(commit);
5546 if (branch)
5547 got_ref_close(branch);
5548 if (new_base_branch)
5549 got_ref_close(new_base_branch);
5550 if (tmp_branch)
5551 got_ref_close(tmp_branch);
5552 if (worktree)
5553 got_worktree_close(worktree);
5554 if (repo)
5555 got_repo_close(repo);
5556 return error;
5559 __dead static void
5560 usage_histedit(void)
5562 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5563 getprogname());
5564 exit(1);
5567 #define GOT_HISTEDIT_PICK 'p'
5568 #define GOT_HISTEDIT_EDIT 'e'
5569 #define GOT_HISTEDIT_FOLD 'f'
5570 #define GOT_HISTEDIT_DROP 'd'
5571 #define GOT_HISTEDIT_MESG 'm'
5573 static struct got_histedit_cmd {
5574 unsigned char code;
5575 const char *name;
5576 const char *desc;
5577 } got_histedit_cmds[] = {
5578 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5579 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5580 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5581 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5582 { GOT_HISTEDIT_MESG, "mesg",
5583 "single-line log message for commit above (open editor if empty)" },
5586 struct got_histedit_list_entry {
5587 TAILQ_ENTRY(got_histedit_list_entry) entry;
5588 struct got_object_id *commit_id;
5589 const struct got_histedit_cmd *cmd;
5590 char *logmsg;
5592 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5594 static const struct got_error *
5595 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5596 FILE *f, struct got_repository *repo)
5598 const struct got_error *err = NULL;
5599 char *logmsg = NULL, *id_str = NULL;
5600 struct got_commit_object *commit = NULL;
5601 int n;
5603 err = got_object_open_as_commit(&commit, repo, commit_id);
5604 if (err)
5605 goto done;
5607 err = get_short_logmsg(&logmsg, 34, commit);
5608 if (err)
5609 goto done;
5611 err = got_object_id_str(&id_str, commit_id);
5612 if (err)
5613 goto done;
5615 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5616 if (n < 0)
5617 err = got_ferror(f, GOT_ERR_IO);
5618 done:
5619 if (commit)
5620 got_object_commit_close(commit);
5621 free(id_str);
5622 free(logmsg);
5623 return err;
5626 static const struct got_error *
5627 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5628 struct got_repository *repo)
5630 const struct got_error *err = NULL;
5631 struct got_object_qid *qid;
5633 if (SIMPLEQ_EMPTY(commits))
5634 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5636 SIMPLEQ_FOREACH(qid, commits, entry) {
5637 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5638 f, repo);
5639 if (err)
5640 break;
5643 return err;
5646 static const struct got_error *
5647 write_cmd_list(FILE *f)
5649 const struct got_error *err = NULL;
5650 int n, i;
5652 n = fprintf(f, "# Available histedit commands:\n");
5653 if (n < 0)
5654 return got_ferror(f, GOT_ERR_IO);
5656 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5657 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5658 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5659 cmd->desc);
5660 if (n < 0) {
5661 err = got_ferror(f, GOT_ERR_IO);
5662 break;
5665 n = fprintf(f, "# Commits will be processed in order from top to "
5666 "bottom of this file.\n");
5667 if (n < 0)
5668 return got_ferror(f, GOT_ERR_IO);
5669 return err;
5672 static const struct got_error *
5673 histedit_syntax_error(int lineno)
5675 static char msg[42];
5676 int ret;
5678 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5679 lineno);
5680 if (ret == -1 || ret >= sizeof(msg))
5681 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5683 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5686 static const struct got_error *
5687 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5688 char *logmsg, struct got_repository *repo)
5690 const struct got_error *err;
5691 struct got_commit_object *folded_commit = NULL;
5692 char *id_str, *folded_logmsg = NULL;
5694 err = got_object_id_str(&id_str, hle->commit_id);
5695 if (err)
5696 return err;
5698 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5699 if (err)
5700 goto done;
5702 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5703 if (err)
5704 goto done;
5705 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5706 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5707 folded_logmsg) == -1) {
5708 err = got_error_from_errno("asprintf");
5710 done:
5711 if (folded_commit)
5712 got_object_commit_close(folded_commit);
5713 free(id_str);
5714 free(folded_logmsg);
5715 return err;
5718 static struct got_histedit_list_entry *
5719 get_folded_commits(struct got_histedit_list_entry *hle)
5721 struct got_histedit_list_entry *prev, *folded = NULL;
5723 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5724 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5725 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5726 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5727 folded = prev;
5728 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5731 return folded;
5734 static const struct got_error *
5735 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5736 struct got_repository *repo)
5738 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5739 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5740 const struct got_error *err = NULL;
5741 struct got_commit_object *commit = NULL;
5742 int fd;
5743 struct got_histedit_list_entry *folded = NULL;
5745 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5746 if (err)
5747 return err;
5749 folded = get_folded_commits(hle);
5750 if (folded) {
5751 while (folded != hle) {
5752 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5753 folded = TAILQ_NEXT(folded, entry);
5754 continue;
5756 err = append_folded_commit_msg(&new_msg, folded,
5757 logmsg, repo);
5758 if (err)
5759 goto done;
5760 free(logmsg);
5761 logmsg = new_msg;
5762 folded = TAILQ_NEXT(folded, entry);
5766 err = got_object_id_str(&id_str, hle->commit_id);
5767 if (err)
5768 goto done;
5769 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5770 if (err)
5771 goto done;
5772 if (asprintf(&new_msg,
5773 "%s\n# original log message of commit %s: %s",
5774 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5775 err = got_error_from_errno("asprintf");
5776 goto done;
5778 free(logmsg);
5779 logmsg = new_msg;
5781 err = got_object_id_str(&id_str, hle->commit_id);
5782 if (err)
5783 goto done;
5785 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5786 if (err)
5787 goto done;
5789 dprintf(fd, logmsg);
5790 close(fd);
5792 err = get_editor(&editor);
5793 if (err)
5794 goto done;
5796 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5797 if (err) {
5798 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5799 goto done;
5800 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5802 done:
5803 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5804 err = got_error_from_errno2("unlink", logmsg_path);
5805 free(logmsg_path);
5806 free(logmsg);
5807 free(orig_logmsg);
5808 free(editor);
5809 if (commit)
5810 got_object_commit_close(commit);
5811 return err;
5814 static const struct got_error *
5815 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5816 FILE *f, struct got_repository *repo)
5818 const struct got_error *err = NULL;
5819 char *line = NULL, *p, *end;
5820 size_t size;
5821 ssize_t len;
5822 int lineno = 0, i;
5823 const struct got_histedit_cmd *cmd;
5824 struct got_object_id *commit_id = NULL;
5825 struct got_histedit_list_entry *hle = NULL;
5827 for (;;) {
5828 len = getline(&line, &size, f);
5829 if (len == -1) {
5830 const struct got_error *getline_err;
5831 if (feof(f))
5832 break;
5833 getline_err = got_error_from_errno("getline");
5834 err = got_ferror(f, getline_err->code);
5835 break;
5837 lineno++;
5838 p = line;
5839 while (isspace((unsigned char)p[0]))
5840 p++;
5841 if (p[0] == '#' || p[0] == '\0') {
5842 free(line);
5843 line = NULL;
5844 continue;
5846 cmd = NULL;
5847 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5848 cmd = &got_histedit_cmds[i];
5849 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5850 isspace((unsigned char)p[strlen(cmd->name)])) {
5851 p += strlen(cmd->name);
5852 break;
5854 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5855 p++;
5856 break;
5859 if (i == nitems(got_histedit_cmds)) {
5860 err = histedit_syntax_error(lineno);
5861 break;
5863 while (isspace((unsigned char)p[0]))
5864 p++;
5865 if (cmd->code == GOT_HISTEDIT_MESG) {
5866 if (hle == NULL || hle->logmsg != NULL) {
5867 err = got_error(GOT_ERR_HISTEDIT_CMD);
5868 break;
5870 if (p[0] == '\0') {
5871 err = histedit_edit_logmsg(hle, repo);
5872 if (err)
5873 break;
5874 } else {
5875 hle->logmsg = strdup(p);
5876 if (hle->logmsg == NULL) {
5877 err = got_error_from_errno("strdup");
5878 break;
5881 free(line);
5882 line = NULL;
5883 continue;
5884 } else {
5885 end = p;
5886 while (end[0] && !isspace((unsigned char)end[0]))
5887 end++;
5888 *end = '\0';
5890 err = got_object_resolve_id_str(&commit_id, repo, p);
5891 if (err) {
5892 /* override error code */
5893 err = histedit_syntax_error(lineno);
5894 break;
5897 hle = malloc(sizeof(*hle));
5898 if (hle == NULL) {
5899 err = got_error_from_errno("malloc");
5900 break;
5902 hle->cmd = cmd;
5903 hle->commit_id = commit_id;
5904 hle->logmsg = NULL;
5905 commit_id = NULL;
5906 free(line);
5907 line = NULL;
5908 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5911 free(line);
5912 free(commit_id);
5913 return err;
5916 static const struct got_error *
5917 histedit_check_script(struct got_histedit_list *histedit_cmds,
5918 struct got_object_id_queue *commits, struct got_repository *repo)
5920 const struct got_error *err = NULL;
5921 struct got_object_qid *qid;
5922 struct got_histedit_list_entry *hle;
5923 static char msg[80];
5924 char *id_str;
5926 if (TAILQ_EMPTY(histedit_cmds))
5927 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5928 "histedit script contains no commands");
5929 if (SIMPLEQ_EMPTY(commits))
5930 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5932 SIMPLEQ_FOREACH(qid, commits, entry) {
5933 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5934 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5935 break;
5937 if (hle == NULL) {
5938 err = got_object_id_str(&id_str, qid->id);
5939 if (err)
5940 return err;
5941 snprintf(msg, sizeof(msg),
5942 "commit %s missing from histedit script", id_str);
5943 free(id_str);
5944 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5948 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5949 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5950 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5951 "last commit in histedit script cannot be folded");
5953 return NULL;
5956 static const struct got_error *
5957 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5958 const char *path, struct got_object_id_queue *commits,
5959 struct got_repository *repo)
5961 const struct got_error *err = NULL;
5962 char *editor;
5963 FILE *f = NULL;
5965 err = get_editor(&editor);
5966 if (err)
5967 return err;
5969 if (spawn_editor(editor, path) == -1) {
5970 err = got_error_from_errno("failed spawning editor");
5971 goto done;
5974 f = fopen(path, "r");
5975 if (f == NULL) {
5976 err = got_error_from_errno("fopen");
5977 goto done;
5979 err = histedit_parse_list(histedit_cmds, f, repo);
5980 if (err)
5981 goto done;
5983 err = histedit_check_script(histedit_cmds, commits, repo);
5984 done:
5985 if (f && fclose(f) != 0 && err == NULL)
5986 err = got_error_from_errno("fclose");
5987 free(editor);
5988 return err;
5991 static const struct got_error *
5992 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5993 struct got_object_id_queue *, const char *, struct got_repository *);
5995 static const struct got_error *
5996 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5997 struct got_object_id_queue *commits, struct got_repository *repo)
5999 const struct got_error *err;
6000 FILE *f = NULL;
6001 char *path = NULL;
6003 err = got_opentemp_named(&path, &f, "got-histedit");
6004 if (err)
6005 return err;
6007 err = write_cmd_list(f);
6008 if (err)
6009 goto done;
6011 err = histedit_write_commit_list(commits, f, repo);
6012 if (err)
6013 goto done;
6015 if (fclose(f) != 0) {
6016 err = got_error_from_errno("fclose");
6017 goto done;
6019 f = NULL;
6021 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6022 if (err) {
6023 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6024 err->code != GOT_ERR_HISTEDIT_CMD)
6025 goto done;
6026 err = histedit_edit_list_retry(histedit_cmds, err,
6027 commits, path, repo);
6029 done:
6030 if (f && fclose(f) != 0 && err == NULL)
6031 err = got_error_from_errno("fclose");
6032 if (path && unlink(path) != 0 && err == NULL)
6033 err = got_error_from_errno2("unlink", path);
6034 free(path);
6035 return err;
6038 static const struct got_error *
6039 histedit_save_list(struct got_histedit_list *histedit_cmds,
6040 struct got_worktree *worktree, struct got_repository *repo)
6042 const struct got_error *err = NULL;
6043 char *path = NULL;
6044 FILE *f = NULL;
6045 struct got_histedit_list_entry *hle;
6046 struct got_commit_object *commit = NULL;
6048 err = got_worktree_get_histedit_script_path(&path, worktree);
6049 if (err)
6050 return err;
6052 f = fopen(path, "w");
6053 if (f == NULL) {
6054 err = got_error_from_errno2("fopen", path);
6055 goto done;
6057 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6058 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6059 repo);
6060 if (err)
6061 break;
6063 if (hle->logmsg) {
6064 int n = fprintf(f, "%c %s\n",
6065 GOT_HISTEDIT_MESG, hle->logmsg);
6066 if (n < 0) {
6067 err = got_ferror(f, GOT_ERR_IO);
6068 break;
6072 done:
6073 if (f && fclose(f) != 0 && err == NULL)
6074 err = got_error_from_errno("fclose");
6075 free(path);
6076 if (commit)
6077 got_object_commit_close(commit);
6078 return err;
6081 void
6082 histedit_free_list(struct got_histedit_list *histedit_cmds)
6084 struct got_histedit_list_entry *hle;
6086 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6087 TAILQ_REMOVE(histedit_cmds, hle, entry);
6088 free(hle);
6092 static const struct got_error *
6093 histedit_load_list(struct got_histedit_list *histedit_cmds,
6094 const char *path, struct got_repository *repo)
6096 const struct got_error *err = NULL;
6097 FILE *f = NULL;
6099 f = fopen(path, "r");
6100 if (f == NULL) {
6101 err = got_error_from_errno2("fopen", path);
6102 goto done;
6105 err = histedit_parse_list(histedit_cmds, f, repo);
6106 done:
6107 if (f && fclose(f) != 0 && err == NULL)
6108 err = got_error_from_errno("fclose");
6109 return err;
6112 static const struct got_error *
6113 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6114 const struct got_error *edit_err, struct got_object_id_queue *commits,
6115 const char *path, struct got_repository *repo)
6117 const struct got_error *err = NULL, *prev_err = edit_err;
6118 int resp = ' ';
6120 while (resp != 'c' && resp != 'r' && resp != 'a') {
6121 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6122 "or (a)bort: ", getprogname(), prev_err->msg);
6123 resp = getchar();
6124 if (resp == '\n')
6125 resp = getchar();
6126 if (resp == 'c') {
6127 histedit_free_list(histedit_cmds);
6128 err = histedit_run_editor(histedit_cmds, path, commits,
6129 repo);
6130 if (err) {
6131 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6132 err->code != GOT_ERR_HISTEDIT_CMD)
6133 break;
6134 prev_err = err;
6135 resp = ' ';
6136 continue;
6138 break;
6139 } else if (resp == 'r') {
6140 histedit_free_list(histedit_cmds);
6141 err = histedit_edit_script(histedit_cmds,
6142 commits, repo);
6143 if (err) {
6144 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6145 err->code != GOT_ERR_HISTEDIT_CMD)
6146 break;
6147 prev_err = err;
6148 resp = ' ';
6149 continue;
6151 break;
6152 } else if (resp == 'a') {
6153 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6154 break;
6155 } else
6156 printf("invalid response '%c'\n", resp);
6159 return err;
6162 static const struct got_error *
6163 histedit_complete(struct got_worktree *worktree,
6164 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6165 struct got_reference *branch, struct got_repository *repo)
6167 printf("Switching work tree to %s\n",
6168 got_ref_get_symref_target(branch));
6169 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6170 branch, repo);
6173 static const struct got_error *
6174 show_histedit_progress(struct got_commit_object *commit,
6175 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6177 const struct got_error *err;
6178 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6180 err = got_object_id_str(&old_id_str, hle->commit_id);
6181 if (err)
6182 goto done;
6184 if (new_id) {
6185 err = got_object_id_str(&new_id_str, new_id);
6186 if (err)
6187 goto done;
6190 old_id_str[12] = '\0';
6191 if (new_id_str)
6192 new_id_str[12] = '\0';
6194 if (hle->logmsg) {
6195 logmsg = strdup(hle->logmsg);
6196 if (logmsg == NULL) {
6197 err = got_error_from_errno("strdup");
6198 goto done;
6200 trim_logmsg(logmsg, 42);
6201 } else {
6202 err = get_short_logmsg(&logmsg, 42, commit);
6203 if (err)
6204 goto done;
6207 switch (hle->cmd->code) {
6208 case GOT_HISTEDIT_PICK:
6209 case GOT_HISTEDIT_EDIT:
6210 printf("%s -> %s: %s\n", old_id_str,
6211 new_id_str ? new_id_str : "no-op change", logmsg);
6212 break;
6213 case GOT_HISTEDIT_DROP:
6214 case GOT_HISTEDIT_FOLD:
6215 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6216 logmsg);
6217 break;
6218 default:
6219 break;
6221 done:
6222 free(old_id_str);
6223 free(new_id_str);
6224 return err;
6227 static const struct got_error *
6228 histedit_commit(struct got_pathlist_head *merged_paths,
6229 struct got_worktree *worktree, struct got_fileindex *fileindex,
6230 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6231 struct got_repository *repo)
6233 const struct got_error *err;
6234 struct got_commit_object *commit;
6235 struct got_object_id *new_commit_id;
6237 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6238 && hle->logmsg == NULL) {
6239 err = histedit_edit_logmsg(hle, repo);
6240 if (err)
6241 return err;
6244 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6245 if (err)
6246 return err;
6248 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6249 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6250 hle->logmsg, repo);
6251 if (err) {
6252 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6253 goto done;
6254 err = show_histedit_progress(commit, hle, NULL);
6255 } else {
6256 err = show_histedit_progress(commit, hle, new_commit_id);
6257 free(new_commit_id);
6259 done:
6260 got_object_commit_close(commit);
6261 return err;
6264 static const struct got_error *
6265 histedit_skip_commit(struct got_histedit_list_entry *hle,
6266 struct got_worktree *worktree, struct got_repository *repo)
6268 const struct got_error *error;
6269 struct got_commit_object *commit;
6271 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6272 repo);
6273 if (error)
6274 return error;
6276 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6277 if (error)
6278 return error;
6280 error = show_histedit_progress(commit, hle, NULL);
6281 got_object_commit_close(commit);
6282 return error;
6285 static const struct got_error *
6286 cmd_histedit(int argc, char *argv[])
6288 const struct got_error *error = NULL;
6289 struct got_worktree *worktree = NULL;
6290 struct got_fileindex *fileindex = NULL;
6291 struct got_repository *repo = NULL;
6292 char *cwd = NULL;
6293 struct got_reference *branch = NULL;
6294 struct got_reference *tmp_branch = NULL;
6295 struct got_object_id *resume_commit_id = NULL;
6296 struct got_object_id *base_commit_id = NULL;
6297 struct got_object_id *head_commit_id = NULL;
6298 struct got_commit_object *commit = NULL;
6299 int ch, rebase_in_progress = 0, did_something;
6300 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6301 const char *edit_script_path = NULL;
6302 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6303 struct got_object_id_queue commits;
6304 struct got_pathlist_head merged_paths;
6305 const struct got_object_id_queue *parent_ids;
6306 struct got_object_qid *pid;
6307 struct got_histedit_list histedit_cmds;
6308 struct got_histedit_list_entry *hle;
6310 SIMPLEQ_INIT(&commits);
6311 TAILQ_INIT(&histedit_cmds);
6312 TAILQ_INIT(&merged_paths);
6314 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6315 switch (ch) {
6316 case 'a':
6317 abort_edit = 1;
6318 break;
6319 case 'c':
6320 continue_edit = 1;
6321 break;
6322 case 'F':
6323 edit_script_path = optarg;
6324 break;
6325 default:
6326 usage_histedit();
6327 /* NOTREACHED */
6331 argc -= optind;
6332 argv += optind;
6334 #ifndef PROFILE
6335 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6336 "unveil", NULL) == -1)
6337 err(1, "pledge");
6338 #endif
6339 if (abort_edit && continue_edit)
6340 usage_histedit();
6341 if (argc != 0)
6342 usage_histedit();
6345 * This command cannot apply unveil(2) in all cases because the
6346 * user may choose to run an editor to edit the histedit script
6347 * and to edit individual commit log messages.
6348 * unveil(2) traverses exec(2); if an editor is used we have to
6349 * apply unveil after edit script and log messages have been written.
6350 * XXX TODO: Make use of unveil(2) where possible.
6353 cwd = getcwd(NULL, 0);
6354 if (cwd == NULL) {
6355 error = got_error_from_errno("getcwd");
6356 goto done;
6358 error = got_worktree_open(&worktree, cwd);
6359 if (error)
6360 goto done;
6362 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6363 NULL);
6364 if (error != NULL)
6365 goto done;
6367 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6368 if (error)
6369 goto done;
6370 if (rebase_in_progress) {
6371 error = got_error(GOT_ERR_REBASING);
6372 goto done;
6375 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6376 if (error)
6377 goto done;
6379 if (edit_in_progress && abort_edit) {
6380 error = got_worktree_histedit_continue(&resume_commit_id,
6381 &tmp_branch, &branch, &base_commit_id, &fileindex,
6382 worktree, repo);
6383 if (error)
6384 goto done;
6385 printf("Switching work tree to %s\n",
6386 got_ref_get_symref_target(branch));
6387 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6388 branch, base_commit_id, update_progress, &did_something);
6389 if (error)
6390 goto done;
6391 printf("Histedit of %s aborted\n",
6392 got_ref_get_symref_target(branch));
6393 goto done; /* nothing else to do */
6394 } else if (abort_edit) {
6395 error = got_error(GOT_ERR_NOT_HISTEDIT);
6396 goto done;
6399 if (continue_edit) {
6400 char *path;
6402 if (!edit_in_progress) {
6403 error = got_error(GOT_ERR_NOT_HISTEDIT);
6404 goto done;
6407 error = got_worktree_get_histedit_script_path(&path, worktree);
6408 if (error)
6409 goto done;
6411 error = histedit_load_list(&histedit_cmds, path, repo);
6412 free(path);
6413 if (error)
6414 goto done;
6416 error = got_worktree_histedit_continue(&resume_commit_id,
6417 &tmp_branch, &branch, &base_commit_id, &fileindex,
6418 worktree, repo);
6419 if (error)
6420 goto done;
6422 error = got_ref_resolve(&head_commit_id, repo, branch);
6423 if (error)
6424 goto done;
6426 error = got_object_open_as_commit(&commit, repo,
6427 head_commit_id);
6428 if (error)
6429 goto done;
6430 parent_ids = got_object_commit_get_parent_ids(commit);
6431 pid = SIMPLEQ_FIRST(parent_ids);
6432 if (pid == NULL) {
6433 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6434 goto done;
6436 error = collect_commits(&commits, head_commit_id, pid->id,
6437 base_commit_id, got_worktree_get_path_prefix(worktree),
6438 GOT_ERR_HISTEDIT_PATH, repo);
6439 got_object_commit_close(commit);
6440 commit = NULL;
6441 if (error)
6442 goto done;
6443 } else {
6444 if (edit_in_progress) {
6445 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6446 goto done;
6449 error = got_ref_open(&branch, repo,
6450 got_worktree_get_head_ref_name(worktree), 0);
6451 if (error != NULL)
6452 goto done;
6454 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6455 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6456 "will not edit commit history of a branch outside "
6457 "the \"refs/heads/\" reference namespace");
6458 goto done;
6461 error = got_ref_resolve(&head_commit_id, repo, branch);
6462 got_ref_close(branch);
6463 branch = NULL;
6464 if (error)
6465 goto done;
6467 error = got_object_open_as_commit(&commit, repo,
6468 head_commit_id);
6469 if (error)
6470 goto done;
6471 parent_ids = got_object_commit_get_parent_ids(commit);
6472 pid = SIMPLEQ_FIRST(parent_ids);
6473 if (pid == NULL) {
6474 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6475 goto done;
6477 error = collect_commits(&commits, head_commit_id, pid->id,
6478 got_worktree_get_base_commit_id(worktree),
6479 got_worktree_get_path_prefix(worktree),
6480 GOT_ERR_HISTEDIT_PATH, repo);
6481 got_object_commit_close(commit);
6482 commit = NULL;
6483 if (error)
6484 goto done;
6486 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6487 &base_commit_id, &fileindex, worktree, repo);
6488 if (error)
6489 goto done;
6491 if (edit_script_path) {
6492 error = histedit_load_list(&histedit_cmds,
6493 edit_script_path, repo);
6494 if (error) {
6495 got_worktree_histedit_abort(worktree, fileindex,
6496 repo, branch, base_commit_id,
6497 update_progress, &did_something);
6498 goto done;
6500 } else {
6501 error = histedit_edit_script(&histedit_cmds, &commits,
6502 repo);
6503 if (error) {
6504 got_worktree_histedit_abort(worktree, fileindex,
6505 repo, branch, base_commit_id,
6506 update_progress, &did_something);
6507 goto done;
6512 error = histedit_save_list(&histedit_cmds, worktree,
6513 repo);
6514 if (error) {
6515 got_worktree_histedit_abort(worktree, fileindex,
6516 repo, branch, base_commit_id,
6517 update_progress, &did_something);
6518 goto done;
6523 error = histedit_check_script(&histedit_cmds, &commits, repo);
6524 if (error)
6525 goto done;
6527 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6528 if (resume_commit_id) {
6529 if (got_object_id_cmp(hle->commit_id,
6530 resume_commit_id) != 0)
6531 continue;
6533 resume_commit_id = NULL;
6534 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6535 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6536 error = histedit_skip_commit(hle, worktree,
6537 repo);
6538 } else {
6539 error = histedit_commit(NULL, worktree,
6540 fileindex, tmp_branch, hle, repo);
6542 if (error)
6543 goto done;
6544 continue;
6547 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6548 error = histedit_skip_commit(hle, worktree, repo);
6549 if (error)
6550 goto done;
6551 continue;
6554 error = got_object_open_as_commit(&commit, repo,
6555 hle->commit_id);
6556 if (error)
6557 goto done;
6558 parent_ids = got_object_commit_get_parent_ids(commit);
6559 pid = SIMPLEQ_FIRST(parent_ids);
6561 error = got_worktree_histedit_merge_files(&merged_paths,
6562 worktree, fileindex, pid->id, hle->commit_id, repo,
6563 rebase_progress, &rebase_status, check_cancelled, NULL);
6564 if (error)
6565 goto done;
6566 got_object_commit_close(commit);
6567 commit = NULL;
6569 if (rebase_status == GOT_STATUS_CONFLICT) {
6570 got_worktree_rebase_pathlist_free(&merged_paths);
6571 break;
6574 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6575 char *id_str;
6576 error = got_object_id_str(&id_str, hle->commit_id);
6577 if (error)
6578 goto done;
6579 printf("Stopping histedit for amending commit %s\n",
6580 id_str);
6581 free(id_str);
6582 got_worktree_rebase_pathlist_free(&merged_paths);
6583 error = got_worktree_histedit_postpone(worktree,
6584 fileindex);
6585 goto done;
6588 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6589 error = histedit_skip_commit(hle, worktree, repo);
6590 if (error)
6591 goto done;
6592 continue;
6595 error = histedit_commit(&merged_paths, worktree, fileindex,
6596 tmp_branch, hle, repo);
6597 got_worktree_rebase_pathlist_free(&merged_paths);
6598 if (error)
6599 goto done;
6602 if (rebase_status == GOT_STATUS_CONFLICT) {
6603 error = got_worktree_histedit_postpone(worktree, fileindex);
6604 if (error)
6605 goto done;
6606 error = got_error_msg(GOT_ERR_CONFLICTS,
6607 "conflicts must be resolved before rebasing can continue");
6608 } else
6609 error = histedit_complete(worktree, fileindex, tmp_branch,
6610 branch, repo);
6611 done:
6612 got_object_id_queue_free(&commits);
6613 histedit_free_list(&histedit_cmds);
6614 free(head_commit_id);
6615 free(base_commit_id);
6616 free(resume_commit_id);
6617 if (commit)
6618 got_object_commit_close(commit);
6619 if (branch)
6620 got_ref_close(branch);
6621 if (tmp_branch)
6622 got_ref_close(tmp_branch);
6623 if (worktree)
6624 got_worktree_close(worktree);
6625 if (repo)
6626 got_repo_close(repo);
6627 return error;
6630 __dead static void
6631 usage_integrate(void)
6633 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6634 exit(1);
6637 static const struct got_error *
6638 cmd_integrate(int argc, char *argv[])
6640 const struct got_error *error = NULL;
6641 struct got_repository *repo = NULL;
6642 struct got_worktree *worktree = NULL;
6643 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6644 const char *branch_arg = NULL;
6645 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6646 struct got_fileindex *fileindex = NULL;
6647 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6648 int ch, did_something = 0;
6650 while ((ch = getopt(argc, argv, "")) != -1) {
6651 switch (ch) {
6652 default:
6653 usage_integrate();
6654 /* NOTREACHED */
6658 argc -= optind;
6659 argv += optind;
6661 if (argc != 1)
6662 usage_integrate();
6663 branch_arg = argv[0];
6665 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6666 "unveil", NULL) == -1)
6667 err(1, "pledge");
6669 cwd = getcwd(NULL, 0);
6670 if (cwd == NULL) {
6671 error = got_error_from_errno("getcwd");
6672 goto done;
6675 error = got_worktree_open(&worktree, cwd);
6676 if (error)
6677 goto done;
6679 error = check_rebase_or_histedit_in_progress(worktree);
6680 if (error)
6681 goto done;
6683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6684 NULL);
6685 if (error != NULL)
6686 goto done;
6688 error = apply_unveil(got_repo_get_path(repo), 0,
6689 got_worktree_get_root_path(worktree));
6690 if (error)
6691 goto done;
6693 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6694 error = got_error_from_errno("asprintf");
6695 goto done;
6698 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6699 &base_branch_ref, worktree, refname, repo);
6700 if (error)
6701 goto done;
6703 refname = strdup(got_ref_get_name(branch_ref));
6704 if (refname == NULL) {
6705 error = got_error_from_errno("strdup");
6706 got_worktree_integrate_abort(worktree, fileindex, repo,
6707 branch_ref, base_branch_ref);
6708 goto done;
6710 base_refname = strdup(got_ref_get_name(base_branch_ref));
6711 if (base_refname == NULL) {
6712 error = got_error_from_errno("strdup");
6713 got_worktree_integrate_abort(worktree, fileindex, repo,
6714 branch_ref, base_branch_ref);
6715 goto done;
6718 error = got_ref_resolve(&commit_id, repo, branch_ref);
6719 if (error)
6720 goto done;
6722 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6723 if (error)
6724 goto done;
6726 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6727 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6728 "specified branch has already been integrated");
6729 got_worktree_integrate_abort(worktree, fileindex, repo,
6730 branch_ref, base_branch_ref);
6731 goto done;
6734 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6735 if (error) {
6736 if (error->code == GOT_ERR_ANCESTRY)
6737 error = got_error(GOT_ERR_REBASE_REQUIRED);
6738 got_worktree_integrate_abort(worktree, fileindex, repo,
6739 branch_ref, base_branch_ref);
6740 goto done;
6743 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6744 branch_ref, base_branch_ref, update_progress, &did_something,
6745 check_cancelled, NULL);
6746 if (error)
6747 goto done;
6749 printf("Integrated %s into %s\n", refname, base_refname);
6750 done:
6751 if (repo)
6752 got_repo_close(repo);
6753 if (worktree)
6754 got_worktree_close(worktree);
6755 free(cwd);
6756 free(base_commit_id);
6757 free(commit_id);
6758 free(refname);
6759 free(base_refname);
6760 return error;
6763 __dead static void
6764 usage_stage(void)
6766 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6767 "[file-path ...]\n",
6768 getprogname());
6769 exit(1);
6772 static const struct got_error *
6773 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6774 const char *path, struct got_object_id *blob_id,
6775 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6776 int dirfd, const char *de_name)
6778 const struct got_error *err = NULL;
6779 char *id_str = NULL;
6781 if (staged_status != GOT_STATUS_ADD &&
6782 staged_status != GOT_STATUS_MODIFY &&
6783 staged_status != GOT_STATUS_DELETE)
6784 return NULL;
6786 if (staged_status == GOT_STATUS_ADD ||
6787 staged_status == GOT_STATUS_MODIFY)
6788 err = got_object_id_str(&id_str, staged_blob_id);
6789 else
6790 err = got_object_id_str(&id_str, blob_id);
6791 if (err)
6792 return err;
6794 printf("%s %c %s\n", id_str, staged_status, path);
6795 free(id_str);
6796 return NULL;
6799 static const struct got_error *
6800 cmd_stage(int argc, char *argv[])
6802 const struct got_error *error = NULL;
6803 struct got_repository *repo = NULL;
6804 struct got_worktree *worktree = NULL;
6805 char *cwd = NULL;
6806 struct got_pathlist_head paths;
6807 struct got_pathlist_entry *pe;
6808 int ch, list_stage = 0, pflag = 0;
6809 FILE *patch_script_file = NULL;
6810 const char *patch_script_path = NULL;
6811 struct choose_patch_arg cpa;
6813 TAILQ_INIT(&paths);
6815 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6816 switch (ch) {
6817 case 'l':
6818 list_stage = 1;
6819 break;
6820 case 'p':
6821 pflag = 1;
6822 break;
6823 case 'F':
6824 patch_script_path = optarg;
6825 break;
6826 default:
6827 usage_stage();
6828 /* NOTREACHED */
6832 argc -= optind;
6833 argv += optind;
6835 #ifndef PROFILE
6836 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6837 "unveil", NULL) == -1)
6838 err(1, "pledge");
6839 #endif
6840 if (list_stage && (pflag || patch_script_path))
6841 errx(1, "-l option cannot be used with other options");
6842 if (patch_script_path && !pflag)
6843 errx(1, "-F option can only be used together with -p option");
6845 cwd = getcwd(NULL, 0);
6846 if (cwd == NULL) {
6847 error = got_error_from_errno("getcwd");
6848 goto done;
6851 error = got_worktree_open(&worktree, cwd);
6852 if (error)
6853 goto done;
6855 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6856 NULL);
6857 if (error != NULL)
6858 goto done;
6860 if (patch_script_path) {
6861 patch_script_file = fopen(patch_script_path, "r");
6862 if (patch_script_file == NULL) {
6863 error = got_error_from_errno2("fopen",
6864 patch_script_path);
6865 goto done;
6868 error = apply_unveil(got_repo_get_path(repo), 0,
6869 got_worktree_get_root_path(worktree));
6870 if (error)
6871 goto done;
6873 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6874 if (error)
6875 goto done;
6877 if (list_stage)
6878 error = got_worktree_status(worktree, &paths, repo,
6879 print_stage, NULL, check_cancelled, NULL);
6880 else {
6881 cpa.patch_script_file = patch_script_file;
6882 cpa.action = "stage";
6883 error = got_worktree_stage(worktree, &paths,
6884 pflag ? NULL : print_status, NULL,
6885 pflag ? choose_patch : NULL, &cpa, repo);
6887 done:
6888 if (patch_script_file && fclose(patch_script_file) == EOF &&
6889 error == NULL)
6890 error = got_error_from_errno2("fclose", patch_script_path);
6891 if (repo)
6892 got_repo_close(repo);
6893 if (worktree)
6894 got_worktree_close(worktree);
6895 TAILQ_FOREACH(pe, &paths, entry)
6896 free((char *)pe->path);
6897 got_pathlist_free(&paths);
6898 free(cwd);
6899 return error;
6902 __dead static void
6903 usage_unstage(void)
6905 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6906 "[file-path ...]\n",
6907 getprogname());
6908 exit(1);
6912 static const struct got_error *
6913 cmd_unstage(int argc, char *argv[])
6915 const struct got_error *error = NULL;
6916 struct got_repository *repo = NULL;
6917 struct got_worktree *worktree = NULL;
6918 char *cwd = NULL;
6919 struct got_pathlist_head paths;
6920 struct got_pathlist_entry *pe;
6921 int ch, did_something = 0, pflag = 0;
6922 FILE *patch_script_file = NULL;
6923 const char *patch_script_path = NULL;
6924 struct choose_patch_arg cpa;
6926 TAILQ_INIT(&paths);
6928 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6929 switch (ch) {
6930 case 'p':
6931 pflag = 1;
6932 break;
6933 case 'F':
6934 patch_script_path = optarg;
6935 break;
6936 default:
6937 usage_unstage();
6938 /* NOTREACHED */
6942 argc -= optind;
6943 argv += optind;
6945 #ifndef PROFILE
6946 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6947 "unveil", NULL) == -1)
6948 err(1, "pledge");
6949 #endif
6950 if (patch_script_path && !pflag)
6951 errx(1, "-F option can only be used together with -p option");
6953 cwd = getcwd(NULL, 0);
6954 if (cwd == NULL) {
6955 error = got_error_from_errno("getcwd");
6956 goto done;
6959 error = got_worktree_open(&worktree, cwd);
6960 if (error)
6961 goto done;
6963 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6964 NULL);
6965 if (error != NULL)
6966 goto done;
6968 if (patch_script_path) {
6969 patch_script_file = fopen(patch_script_path, "r");
6970 if (patch_script_file == NULL) {
6971 error = got_error_from_errno2("fopen",
6972 patch_script_path);
6973 goto done;
6977 error = apply_unveil(got_repo_get_path(repo), 0,
6978 got_worktree_get_root_path(worktree));
6979 if (error)
6980 goto done;
6982 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6983 if (error)
6984 goto done;
6986 cpa.patch_script_file = patch_script_file;
6987 cpa.action = "unstage";
6988 error = got_worktree_unstage(worktree, &paths, update_progress,
6989 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6990 done:
6991 if (patch_script_file && fclose(patch_script_file) == EOF &&
6992 error == NULL)
6993 error = got_error_from_errno2("fclose", patch_script_path);
6994 if (repo)
6995 got_repo_close(repo);
6996 if (worktree)
6997 got_worktree_close(worktree);
6998 TAILQ_FOREACH(pe, &paths, entry)
6999 free((char *)pe->path);
7000 got_pathlist_free(&paths);
7001 free(cwd);
7002 return error;
7005 __dead static void
7006 usage_cat(void)
7008 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7009 "arg1 [arg2 ...]\n", getprogname());
7010 exit(1);
7013 static const struct got_error *
7014 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7016 const struct got_error *err;
7017 struct got_blob_object *blob;
7019 err = got_object_open_as_blob(&blob, repo, id, 8192);
7020 if (err)
7021 return err;
7023 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7024 got_object_blob_close(blob);
7025 return err;
7028 static const struct got_error *
7029 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7031 const struct got_error *err;
7032 struct got_tree_object *tree;
7033 int nentries, i;
7035 err = got_object_open_as_tree(&tree, repo, id);
7036 if (err)
7037 return err;
7039 nentries = got_object_tree_get_nentries(tree);
7040 for (i = 0; i < nentries; i++) {
7041 struct got_tree_entry *te;
7042 char *id_str;
7043 if (sigint_received || sigpipe_received)
7044 break;
7045 te = got_object_tree_get_entry(tree, i);
7046 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7047 if (err)
7048 break;
7049 fprintf(outfile, "%s %.7o %s\n", id_str,
7050 got_tree_entry_get_mode(te),
7051 got_tree_entry_get_name(te));
7052 free(id_str);
7055 got_object_tree_close(tree);
7056 return err;
7059 static const struct got_error *
7060 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7062 const struct got_error *err;
7063 struct got_commit_object *commit;
7064 const struct got_object_id_queue *parent_ids;
7065 struct got_object_qid *pid;
7066 char *id_str = NULL;
7067 const char *logmsg = NULL;
7069 err = got_object_open_as_commit(&commit, repo, id);
7070 if (err)
7071 return err;
7073 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7074 if (err)
7075 goto done;
7077 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7078 parent_ids = got_object_commit_get_parent_ids(commit);
7079 fprintf(outfile, "numparents %d\n",
7080 got_object_commit_get_nparents(commit));
7081 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7082 char *pid_str;
7083 err = got_object_id_str(&pid_str, pid->id);
7084 if (err)
7085 goto done;
7086 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7087 free(pid_str);
7089 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7090 got_object_commit_get_author(commit),
7091 got_object_commit_get_author_time(commit));
7093 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7094 got_object_commit_get_author(commit),
7095 got_object_commit_get_committer_time(commit));
7097 logmsg = got_object_commit_get_logmsg_raw(commit);
7098 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7099 fprintf(outfile, "%s", logmsg);
7100 done:
7101 free(id_str);
7102 got_object_commit_close(commit);
7103 return err;
7106 static const struct got_error *
7107 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7109 const struct got_error *err;
7110 struct got_tag_object *tag;
7111 char *id_str = NULL;
7112 const char *tagmsg = NULL;
7114 err = got_object_open_as_tag(&tag, repo, id);
7115 if (err)
7116 return err;
7118 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7119 if (err)
7120 goto done;
7122 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7124 switch (got_object_tag_get_object_type(tag)) {
7125 case GOT_OBJ_TYPE_BLOB:
7126 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7127 GOT_OBJ_LABEL_BLOB);
7128 break;
7129 case GOT_OBJ_TYPE_TREE:
7130 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7131 GOT_OBJ_LABEL_TREE);
7132 break;
7133 case GOT_OBJ_TYPE_COMMIT:
7134 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7135 GOT_OBJ_LABEL_COMMIT);
7136 break;
7137 case GOT_OBJ_TYPE_TAG:
7138 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7139 GOT_OBJ_LABEL_TAG);
7140 break;
7141 default:
7142 break;
7145 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7146 got_object_tag_get_name(tag));
7148 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7149 got_object_tag_get_tagger(tag),
7150 got_object_tag_get_tagger_time(tag));
7152 tagmsg = got_object_tag_get_message(tag);
7153 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7154 fprintf(outfile, "%s", tagmsg);
7155 done:
7156 free(id_str);
7157 got_object_tag_close(tag);
7158 return err;
7161 static const struct got_error *
7162 cmd_cat(int argc, char *argv[])
7164 const struct got_error *error;
7165 struct got_repository *repo = NULL;
7166 struct got_worktree *worktree = NULL;
7167 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7168 const char *commit_id_str = NULL;
7169 struct got_object_id *id = NULL, *commit_id = NULL;
7170 int ch, obj_type, i, force_path = 0;
7172 #ifndef PROFILE
7173 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7174 NULL) == -1)
7175 err(1, "pledge");
7176 #endif
7178 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7179 switch (ch) {
7180 case 'c':
7181 commit_id_str = optarg;
7182 break;
7183 case 'r':
7184 repo_path = realpath(optarg, NULL);
7185 if (repo_path == NULL)
7186 return got_error_from_errno2("realpath",
7187 optarg);
7188 got_path_strip_trailing_slashes(repo_path);
7189 break;
7190 case 'P':
7191 force_path = 1;
7192 break;
7193 default:
7194 usage_cat();
7195 /* NOTREACHED */
7199 argc -= optind;
7200 argv += optind;
7202 cwd = getcwd(NULL, 0);
7203 if (cwd == NULL) {
7204 error = got_error_from_errno("getcwd");
7205 goto done;
7207 error = got_worktree_open(&worktree, cwd);
7208 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7209 goto done;
7210 if (worktree) {
7211 if (repo_path == NULL) {
7212 repo_path = strdup(
7213 got_worktree_get_repo_path(worktree));
7214 if (repo_path == NULL) {
7215 error = got_error_from_errno("strdup");
7216 goto done;
7221 if (repo_path == NULL) {
7222 repo_path = getcwd(NULL, 0);
7223 if (repo_path == NULL)
7224 return got_error_from_errno("getcwd");
7227 error = got_repo_open(&repo, repo_path, NULL);
7228 free(repo_path);
7229 if (error != NULL)
7230 goto done;
7232 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7233 if (error)
7234 goto done;
7236 if (commit_id_str == NULL)
7237 commit_id_str = GOT_REF_HEAD;
7238 error = got_repo_match_object_id(&commit_id, NULL,
7239 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7240 if (error)
7241 goto done;
7243 for (i = 0; i < argc; i++) {
7244 if (force_path) {
7245 error = got_object_id_by_path(&id, repo, commit_id,
7246 argv[i]);
7247 if (error)
7248 break;
7249 } else {
7250 error = got_repo_match_object_id(&id, &label, argv[i],
7251 GOT_OBJ_TYPE_ANY, 0, repo);
7252 if (error) {
7253 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7254 error->code != GOT_ERR_NOT_REF)
7255 break;
7256 error = got_object_id_by_path(&id, repo,
7257 commit_id, argv[i]);
7258 if (error)
7259 break;
7263 error = got_object_get_type(&obj_type, repo, id);
7264 if (error)
7265 break;
7267 switch (obj_type) {
7268 case GOT_OBJ_TYPE_BLOB:
7269 error = cat_blob(id, repo, stdout);
7270 break;
7271 case GOT_OBJ_TYPE_TREE:
7272 error = cat_tree(id, repo, stdout);
7273 break;
7274 case GOT_OBJ_TYPE_COMMIT:
7275 error = cat_commit(id, repo, stdout);
7276 break;
7277 case GOT_OBJ_TYPE_TAG:
7278 error = cat_tag(id, repo, stdout);
7279 break;
7280 default:
7281 error = got_error(GOT_ERR_OBJ_TYPE);
7282 break;
7284 if (error)
7285 break;
7286 free(label);
7287 label = NULL;
7288 free(id);
7289 id = NULL;
7291 done:
7292 free(label);
7293 free(id);
7294 free(commit_id);
7295 if (worktree)
7296 got_worktree_close(worktree);
7297 if (repo) {
7298 const struct got_error *repo_error;
7299 repo_error = got_repo_close(repo);
7300 if (error == NULL)
7301 error = repo_error;
7303 return error;