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, worktree ?
2627 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2628 if (error != NULL)
2629 goto done;
2630 error = got_ref_resolve(&commit_id, repo, head_ref);
2631 got_ref_close(head_ref);
2632 if (error != NULL)
2633 goto done;
2634 } else {
2635 error = got_repo_match_object_id(&commit_id, NULL,
2636 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2637 if (error)
2638 goto done;
2641 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2642 if (error)
2643 goto done;
2645 error = got_object_get_type(&obj_type, repo, obj_id);
2646 if (error)
2647 goto done;
2649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2650 error = got_error(GOT_ERR_OBJ_TYPE);
2651 goto done;
2654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2655 if (error)
2656 goto done;
2657 bca.f = got_opentemp();
2658 if (bca.f == NULL) {
2659 error = got_error_from_errno("got_opentemp");
2660 goto done;
2662 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2663 &bca.line_offsets, bca.f, blob);
2664 if (error || bca.nlines == 0)
2665 goto done;
2667 /* Don't include \n at EOF in the blame line count. */
2668 if (bca.line_offsets[bca.nlines - 1] == filesize)
2669 bca.nlines--;
2671 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2672 if (bca.lines == NULL) {
2673 error = got_error_from_errno("calloc");
2674 goto done;
2676 bca.lineno_cur = 1;
2677 bca.nlines_prec = 0;
2678 i = bca.nlines;
2679 while (i > 0) {
2680 i /= 10;
2681 bca.nlines_prec++;
2683 bca.repo = repo;
2685 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2686 check_cancelled, NULL);
2687 done:
2688 free(in_repo_path);
2689 free(repo_path);
2690 free(cwd);
2691 free(commit_id);
2692 free(obj_id);
2693 if (blob)
2694 got_object_blob_close(blob);
2695 if (worktree)
2696 got_worktree_close(worktree);
2697 if (repo) {
2698 const struct got_error *repo_error;
2699 repo_error = got_repo_close(repo);
2700 if (error == NULL)
2701 error = repo_error;
2703 if (bca.lines) {
2704 for (i = 0; i < bca.nlines; i++) {
2705 struct blame_line *bline = &bca.lines[i];
2706 free(bline->id_str);
2707 free(bline->committer);
2709 free(bca.lines);
2711 free(bca.line_offsets);
2712 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2713 error = got_error_from_errno("fclose");
2714 return error;
2717 __dead static void
2718 usage_tree(void)
2720 fprintf(stderr,
2721 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2722 getprogname());
2723 exit(1);
2726 static void
2727 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2728 const char *root_path)
2730 int is_root_path = (strcmp(path, root_path) == 0);
2731 const char *modestr = "";
2732 mode_t mode = got_tree_entry_get_mode(te);
2734 path += strlen(root_path);
2735 while (path[0] == '/')
2736 path++;
2738 if (got_object_tree_entry_is_submodule(te))
2739 modestr = "$";
2740 else if (S_ISLNK(mode))
2741 modestr = "@";
2742 else if (S_ISDIR(mode))
2743 modestr = "/";
2744 else if (mode & S_IXUSR)
2745 modestr = "*";
2747 printf("%s%s%s%s%s\n", id ? id : "", path,
2748 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2751 static const struct got_error *
2752 print_tree(const char *path, struct got_object_id *commit_id,
2753 int show_ids, int recurse, const char *root_path,
2754 struct got_repository *repo)
2756 const struct got_error *err = NULL;
2757 struct got_object_id *tree_id = NULL;
2758 struct got_tree_object *tree = NULL;
2759 int nentries, i;
2761 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2762 if (err)
2763 goto done;
2765 err = got_object_open_as_tree(&tree, repo, tree_id);
2766 if (err)
2767 goto done;
2768 nentries = got_object_tree_get_nentries(tree);
2769 for (i = 0; i < nentries; i++) {
2770 struct got_tree_entry *te;
2771 char *id = NULL;
2773 if (sigint_received || sigpipe_received)
2774 break;
2776 te = got_object_tree_get_entry(tree, i);
2777 if (show_ids) {
2778 char *id_str;
2779 err = got_object_id_str(&id_str,
2780 got_tree_entry_get_id(te));
2781 if (err)
2782 goto done;
2783 if (asprintf(&id, "%s ", id_str) == -1) {
2784 err = got_error_from_errno("asprintf");
2785 free(id_str);
2786 goto done;
2788 free(id_str);
2790 print_entry(te, id, path, root_path);
2791 free(id);
2793 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2794 char *child_path;
2795 if (asprintf(&child_path, "%s%s%s", path,
2796 path[0] == '/' && path[1] == '\0' ? "" : "/",
2797 got_tree_entry_get_name(te)) == -1) {
2798 err = got_error_from_errno("asprintf");
2799 goto done;
2801 err = print_tree(child_path, commit_id, show_ids, 1,
2802 root_path, repo);
2803 free(child_path);
2804 if (err)
2805 goto done;
2808 done:
2809 if (tree)
2810 got_object_tree_close(tree);
2811 free(tree_id);
2812 return err;
2815 static const struct got_error *
2816 cmd_tree(int argc, char *argv[])
2818 const struct got_error *error;
2819 struct got_repository *repo = NULL;
2820 struct got_worktree *worktree = NULL;
2821 const char *path;
2822 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2823 struct got_object_id *commit_id = NULL;
2824 char *commit_id_str = NULL;
2825 int show_ids = 0, recurse = 0;
2826 int ch;
2828 #ifndef PROFILE
2829 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2830 NULL) == -1)
2831 err(1, "pledge");
2832 #endif
2834 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2835 switch (ch) {
2836 case 'c':
2837 commit_id_str = optarg;
2838 break;
2839 case 'r':
2840 repo_path = realpath(optarg, NULL);
2841 if (repo_path == NULL)
2842 return got_error_from_errno2("realpath",
2843 optarg);
2844 got_path_strip_trailing_slashes(repo_path);
2845 break;
2846 case 'i':
2847 show_ids = 1;
2848 break;
2849 case 'R':
2850 recurse = 1;
2851 break;
2852 default:
2853 usage_tree();
2854 /* NOTREACHED */
2858 argc -= optind;
2859 argv += optind;
2861 if (argc == 1)
2862 path = argv[0];
2863 else if (argc > 1)
2864 usage_tree();
2865 else
2866 path = NULL;
2868 cwd = getcwd(NULL, 0);
2869 if (cwd == NULL) {
2870 error = got_error_from_errno("getcwd");
2871 goto done;
2873 if (repo_path == NULL) {
2874 error = got_worktree_open(&worktree, cwd);
2875 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2876 goto done;
2877 else
2878 error = NULL;
2879 if (worktree) {
2880 repo_path =
2881 strdup(got_worktree_get_repo_path(worktree));
2882 if (repo_path == NULL)
2883 error = got_error_from_errno("strdup");
2884 if (error)
2885 goto done;
2886 } else {
2887 repo_path = strdup(cwd);
2888 if (repo_path == NULL) {
2889 error = got_error_from_errno("strdup");
2890 goto done;
2895 error = got_repo_open(&repo, repo_path, NULL);
2896 if (error != NULL)
2897 goto done;
2899 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2900 if (error)
2901 goto done;
2903 if (path == NULL) {
2904 if (worktree) {
2905 char *p, *worktree_subdir = cwd +
2906 strlen(got_worktree_get_root_path(worktree));
2907 if (asprintf(&p, "%s/%s",
2908 got_worktree_get_path_prefix(worktree),
2909 worktree_subdir) == -1) {
2910 error = got_error_from_errno("asprintf");
2911 goto done;
2913 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2914 free(p);
2915 if (error)
2916 goto done;
2917 } else
2918 path = "/";
2920 if (in_repo_path == NULL) {
2921 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2922 if (error != NULL)
2923 goto done;
2926 if (commit_id_str == NULL) {
2927 struct got_reference *head_ref;
2928 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2929 if (error != NULL)
2930 goto done;
2931 error = got_ref_resolve(&commit_id, repo, head_ref);
2932 got_ref_close(head_ref);
2933 if (error != NULL)
2934 goto done;
2935 } else {
2936 error = got_repo_match_object_id(&commit_id, NULL,
2937 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2938 if (error)
2939 goto done;
2942 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2943 in_repo_path, repo);
2944 done:
2945 free(in_repo_path);
2946 free(repo_path);
2947 free(cwd);
2948 free(commit_id);
2949 if (worktree)
2950 got_worktree_close(worktree);
2951 if (repo) {
2952 const struct got_error *repo_error;
2953 repo_error = got_repo_close(repo);
2954 if (error == NULL)
2955 error = repo_error;
2957 return error;
2960 __dead static void
2961 usage_status(void)
2963 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2964 exit(1);
2967 static const struct got_error *
2968 print_status(void *arg, unsigned char status, unsigned char staged_status,
2969 const char *path, struct got_object_id *blob_id,
2970 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2971 int dirfd, const char *de_name)
2973 if (status == staged_status && (status == GOT_STATUS_DELETE))
2974 status = GOT_STATUS_NO_CHANGE;
2975 printf("%c%c %s\n", status, staged_status, path);
2976 return NULL;
2979 static const struct got_error *
2980 cmd_status(int argc, char *argv[])
2982 const struct got_error *error = NULL;
2983 struct got_repository *repo = NULL;
2984 struct got_worktree *worktree = NULL;
2985 char *cwd = NULL;
2986 struct got_pathlist_head paths;
2987 struct got_pathlist_entry *pe;
2988 int ch;
2990 TAILQ_INIT(&paths);
2992 while ((ch = getopt(argc, argv, "")) != -1) {
2993 switch (ch) {
2994 default:
2995 usage_status();
2996 /* NOTREACHED */
3000 argc -= optind;
3001 argv += optind;
3003 #ifndef PROFILE
3004 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3005 NULL) == -1)
3006 err(1, "pledge");
3007 #endif
3008 cwd = getcwd(NULL, 0);
3009 if (cwd == NULL) {
3010 error = got_error_from_errno("getcwd");
3011 goto done;
3014 error = got_worktree_open(&worktree, cwd);
3015 if (error != NULL)
3016 goto done;
3018 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3019 NULL);
3020 if (error != NULL)
3021 goto done;
3023 error = apply_unveil(got_repo_get_path(repo), 1,
3024 got_worktree_get_root_path(worktree));
3025 if (error)
3026 goto done;
3028 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3029 if (error)
3030 goto done;
3032 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3033 check_cancelled, NULL);
3034 done:
3035 TAILQ_FOREACH(pe, &paths, entry)
3036 free((char *)pe->path);
3037 got_pathlist_free(&paths);
3038 free(cwd);
3039 return error;
3042 __dead static void
3043 usage_ref(void)
3045 fprintf(stderr,
3046 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3047 getprogname());
3048 exit(1);
3051 static const struct got_error *
3052 list_refs(struct got_repository *repo)
3054 static const struct got_error *err = NULL;
3055 struct got_reflist_head refs;
3056 struct got_reflist_entry *re;
3058 SIMPLEQ_INIT(&refs);
3059 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3060 if (err)
3061 return err;
3063 SIMPLEQ_FOREACH(re, &refs, entry) {
3064 char *refstr;
3065 refstr = got_ref_to_str(re->ref);
3066 if (refstr == NULL)
3067 return got_error_from_errno("got_ref_to_str");
3068 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3069 free(refstr);
3072 got_ref_list_free(&refs);
3073 return NULL;
3076 static const struct got_error *
3077 delete_ref(struct got_repository *repo, const char *refname)
3079 const struct got_error *err = NULL;
3080 struct got_reference *ref;
3082 err = got_ref_open(&ref, repo, refname, 0);
3083 if (err)
3084 return err;
3086 err = got_ref_delete(ref, repo);
3087 got_ref_close(ref);
3088 return err;
3091 static const struct got_error *
3092 add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 const struct got_error *err = NULL;
3095 struct got_object_id *id;
3096 struct got_reference *ref = NULL;
3099 * Don't let the user create a reference name with a leading '-'.
3100 * While technically a valid reference name, this case is usually
3101 * an unintended typo.
3103 if (refname[0] == '-')
3104 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3106 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3107 repo);
3108 if (err) {
3109 struct got_reference *target_ref;
3111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3112 return err;
3113 err = got_ref_open(&target_ref, repo, target, 0);
3114 if (err)
3115 return err;
3116 err = got_ref_resolve(&id, repo, target_ref);
3117 got_ref_close(target_ref);
3118 if (err)
3119 return err;
3122 err = got_ref_alloc(&ref, refname, id);
3123 if (err)
3124 goto done;
3126 err = got_ref_write(ref, repo);
3127 done:
3128 if (ref)
3129 got_ref_close(ref);
3130 free(id);
3131 return err;
3134 static const struct got_error *
3135 add_symref(struct got_repository *repo, const char *refname, const char *target)
3137 const struct got_error *err = NULL;
3138 struct got_reference *ref = NULL;
3139 struct got_reference *target_ref = NULL;
3142 * Don't let the user create a reference name with a leading '-'.
3143 * While technically a valid reference name, this case is usually
3144 * an unintended typo.
3146 if (refname[0] == '-')
3147 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3149 err = got_ref_open(&target_ref, repo, target, 0);
3150 if (err)
3151 return err;
3153 err = got_ref_alloc_symref(&ref, refname, target_ref);
3154 if (err)
3155 goto done;
3157 err = got_ref_write(ref, repo);
3158 done:
3159 if (target_ref)
3160 got_ref_close(target_ref);
3161 if (ref)
3162 got_ref_close(ref);
3163 return err;
3166 static const struct got_error *
3167 cmd_ref(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct got_repository *repo = NULL;
3171 struct got_worktree *worktree = NULL;
3172 char *cwd = NULL, *repo_path = NULL;
3173 int ch, do_list = 0, create_symref = 0;
3174 const char *delref = NULL;
3176 /* TODO: Add -s option for adding symbolic references. */
3177 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3178 switch (ch) {
3179 case 'd':
3180 delref = optarg;
3181 break;
3182 case 'r':
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 return got_error_from_errno2("realpath",
3186 optarg);
3187 got_path_strip_trailing_slashes(repo_path);
3188 break;
3189 case 'l':
3190 do_list = 1;
3191 break;
3192 case 's':
3193 create_symref = 1;
3194 break;
3195 default:
3196 usage_ref();
3197 /* NOTREACHED */
3201 if (do_list && delref)
3202 errx(1, "-l and -d options are mutually exclusive\n");
3204 argc -= optind;
3205 argv += optind;
3207 if (do_list || delref) {
3208 if (create_symref)
3209 errx(1, "-s option cannot be used together with the "
3210 "-l or -d options");
3211 if (argc > 0)
3212 usage_ref();
3213 } else if (argc != 2)
3214 usage_ref();
3216 #ifndef PROFILE
3217 if (do_list) {
3218 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3219 NULL) == -1)
3220 err(1, "pledge");
3221 } else {
3222 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3223 "sendfd unveil", NULL) == -1)
3224 err(1, "pledge");
3226 #endif
3227 cwd = getcwd(NULL, 0);
3228 if (cwd == NULL) {
3229 error = got_error_from_errno("getcwd");
3230 goto done;
3233 if (repo_path == NULL) {
3234 error = got_worktree_open(&worktree, cwd);
3235 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3236 goto done;
3237 else
3238 error = NULL;
3239 if (worktree) {
3240 repo_path =
3241 strdup(got_worktree_get_repo_path(worktree));
3242 if (repo_path == NULL)
3243 error = got_error_from_errno("strdup");
3244 if (error)
3245 goto done;
3246 } else {
3247 repo_path = strdup(cwd);
3248 if (repo_path == NULL) {
3249 error = got_error_from_errno("strdup");
3250 goto done;
3255 error = got_repo_open(&repo, repo_path, NULL);
3256 if (error != NULL)
3257 goto done;
3259 error = apply_unveil(got_repo_get_path(repo), do_list,
3260 worktree ? got_worktree_get_root_path(worktree) : NULL);
3261 if (error)
3262 goto done;
3264 if (do_list)
3265 error = list_refs(repo);
3266 else if (delref)
3267 error = delete_ref(repo, delref);
3268 else if (create_symref)
3269 error = add_symref(repo, argv[0], argv[1]);
3270 else
3271 error = add_ref(repo, argv[0], argv[1]);
3272 done:
3273 if (repo)
3274 got_repo_close(repo);
3275 if (worktree)
3276 got_worktree_close(worktree);
3277 free(cwd);
3278 free(repo_path);
3279 return error;
3282 __dead static void
3283 usage_branch(void)
3285 fprintf(stderr,
3286 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3287 "[name]\n", getprogname());
3288 exit(1);
3291 static const struct got_error *
3292 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3293 struct got_reference *ref)
3295 const struct got_error *err = NULL;
3296 const char *refname, *marker = " ";
3297 char *refstr;
3299 refname = got_ref_get_name(ref);
3300 if (worktree && strcmp(refname,
3301 got_worktree_get_head_ref_name(worktree)) == 0) {
3302 struct got_object_id *id = NULL;
3304 err = got_ref_resolve(&id, repo, ref);
3305 if (err)
3306 return err;
3307 if (got_object_id_cmp(id,
3308 got_worktree_get_base_commit_id(worktree)) == 0)
3309 marker = "* ";
3310 else
3311 marker = "~ ";
3312 free(id);
3315 if (strncmp(refname, "refs/heads/", 11) == 0)
3316 refname += 11;
3317 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3318 refname += 18;
3320 refstr = got_ref_to_str(ref);
3321 if (refstr == NULL)
3322 return got_error_from_errno("got_ref_to_str");
3324 printf("%s%s: %s\n", marker, refname, refstr);
3325 free(refstr);
3326 return NULL;
3329 static const struct got_error *
3330 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3332 const char *refname;
3334 if (worktree == NULL)
3335 return got_error(GOT_ERR_NOT_WORKTREE);
3337 refname = got_worktree_get_head_ref_name(worktree);
3339 if (strncmp(refname, "refs/heads/", 11) == 0)
3340 refname += 11;
3341 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3342 refname += 18;
3344 printf("%s\n", refname);
3346 return NULL;
3349 static const struct got_error *
3350 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3352 static const struct got_error *err = NULL;
3353 struct got_reflist_head refs;
3354 struct got_reflist_entry *re;
3355 struct got_reference *temp_ref = NULL;
3356 int rebase_in_progress, histedit_in_progress;
3358 SIMPLEQ_INIT(&refs);
3360 if (worktree) {
3361 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3362 worktree);
3363 if (err)
3364 return err;
3366 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3367 worktree);
3368 if (err)
3369 return err;
3371 if (rebase_in_progress || histedit_in_progress) {
3372 err = got_ref_open(&temp_ref, repo,
3373 got_worktree_get_head_ref_name(worktree), 0);
3374 if (err)
3375 return err;
3376 list_branch(repo, worktree, temp_ref);
3377 got_ref_close(temp_ref);
3381 err = got_ref_list(&refs, repo, "refs/heads",
3382 got_ref_cmp_by_name, NULL);
3383 if (err)
3384 return err;
3386 SIMPLEQ_FOREACH(re, &refs, entry)
3387 list_branch(repo, worktree, re->ref);
3389 got_ref_list_free(&refs);
3390 return NULL;
3393 static const struct got_error *
3394 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3395 const char *branch_name)
3397 const struct got_error *err = NULL;
3398 struct got_reference *ref = NULL;
3399 char *refname;
3401 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3402 return got_error_from_errno("asprintf");
3404 err = got_ref_open(&ref, repo, refname, 0);
3405 if (err)
3406 goto done;
3408 if (worktree &&
3409 strcmp(got_worktree_get_head_ref_name(worktree),
3410 got_ref_get_name(ref)) == 0) {
3411 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3412 "will not delete this work tree's current branch");
3413 goto done;
3416 err = got_ref_delete(ref, repo);
3417 done:
3418 if (ref)
3419 got_ref_close(ref);
3420 free(refname);
3421 return err;
3424 static const struct got_error *
3425 add_branch(struct got_repository *repo, const char *branch_name,
3426 struct got_object_id *base_commit_id)
3428 const struct got_error *err = NULL;
3429 struct got_reference *ref = NULL;
3430 char *base_refname = NULL, *refname = NULL;
3433 * Don't let the user create a branch name with a leading '-'.
3434 * While technically a valid reference name, this case is usually
3435 * an unintended typo.
3437 if (branch_name[0] == '-')
3438 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3440 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3441 err = got_error_from_errno("asprintf");
3442 goto done;
3445 err = got_ref_open(&ref, repo, refname, 0);
3446 if (err == NULL) {
3447 err = got_error(GOT_ERR_BRANCH_EXISTS);
3448 goto done;
3449 } else if (err->code != GOT_ERR_NOT_REF)
3450 goto done;
3452 err = got_ref_alloc(&ref, refname, base_commit_id);
3453 if (err)
3454 goto done;
3456 err = got_ref_write(ref, repo);
3457 done:
3458 if (ref)
3459 got_ref_close(ref);
3460 free(base_refname);
3461 free(refname);
3462 return err;
3465 static const struct got_error *
3466 cmd_branch(int argc, char *argv[])
3468 const struct got_error *error = NULL;
3469 struct got_repository *repo = NULL;
3470 struct got_worktree *worktree = NULL;
3471 char *cwd = NULL, *repo_path = NULL;
3472 int ch, do_list = 0, do_show = 0;
3473 const char *delref = NULL, *commit_id_arg = NULL;
3475 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3476 switch (ch) {
3477 case 'c':
3478 commit_id_arg = optarg;
3479 break;
3480 case 'd':
3481 delref = optarg;
3482 break;
3483 case 'r':
3484 repo_path = realpath(optarg, NULL);
3485 if (repo_path == NULL)
3486 return got_error_from_errno2("realpath",
3487 optarg);
3488 got_path_strip_trailing_slashes(repo_path);
3489 break;
3490 case 'l':
3491 do_list = 1;
3492 break;
3493 default:
3494 usage_branch();
3495 /* NOTREACHED */
3499 if (do_list && delref)
3500 errx(1, "-l and -d options are mutually exclusive\n");
3502 argc -= optind;
3503 argv += optind;
3505 if (!do_list && !delref && argc == 0)
3506 do_show = 1;
3508 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3509 errx(1, "-c option can only be used when creating a branch");
3511 if (do_list || delref) {
3512 if (argc > 0)
3513 usage_branch();
3514 } else if (!do_show && argc != 1)
3515 usage_branch();
3517 #ifndef PROFILE
3518 if (do_list || do_show) {
3519 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3520 NULL) == -1)
3521 err(1, "pledge");
3522 } else {
3523 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3524 "sendfd unveil", NULL) == -1)
3525 err(1, "pledge");
3527 #endif
3528 cwd = getcwd(NULL, 0);
3529 if (cwd == NULL) {
3530 error = got_error_from_errno("getcwd");
3531 goto done;
3534 if (repo_path == NULL) {
3535 error = got_worktree_open(&worktree, cwd);
3536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3537 goto done;
3538 else
3539 error = NULL;
3540 if (worktree) {
3541 repo_path =
3542 strdup(got_worktree_get_repo_path(worktree));
3543 if (repo_path == NULL)
3544 error = got_error_from_errno("strdup");
3545 if (error)
3546 goto done;
3547 } else {
3548 repo_path = strdup(cwd);
3549 if (repo_path == NULL) {
3550 error = got_error_from_errno("strdup");
3551 goto done;
3556 error = got_repo_open(&repo, repo_path, NULL);
3557 if (error != NULL)
3558 goto done;
3560 error = apply_unveil(got_repo_get_path(repo), do_list,
3561 worktree ? got_worktree_get_root_path(worktree) : NULL);
3562 if (error)
3563 goto done;
3565 if (do_show)
3566 error = show_current_branch(repo, worktree);
3567 else if (do_list)
3568 error = list_branches(repo, worktree);
3569 else if (delref)
3570 error = delete_branch(repo, worktree, delref);
3571 else {
3572 struct got_object_id *commit_id;
3573 if (commit_id_arg == NULL)
3574 commit_id_arg = worktree ?
3575 got_worktree_get_head_ref_name(worktree) :
3576 GOT_REF_HEAD;
3577 error = got_repo_match_object_id(&commit_id, NULL,
3578 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3579 if (error)
3580 goto done;
3581 error = add_branch(repo, argv[0], commit_id);
3582 free(commit_id);
3584 done:
3585 if (repo)
3586 got_repo_close(repo);
3587 if (worktree)
3588 got_worktree_close(worktree);
3589 free(cwd);
3590 free(repo_path);
3591 return error;
3595 __dead static void
3596 usage_tag(void)
3598 fprintf(stderr,
3599 "usage: %s tag [-r repository] | -l | "
3600 "[-m message] name [commit]\n", getprogname());
3601 exit(1);
3604 #if 0
3605 static const struct got_error *
3606 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3608 const struct got_error *err = NULL;
3609 struct got_reflist_entry *re, *se, *new;
3610 struct got_object_id *re_id, *se_id;
3611 struct got_tag_object *re_tag, *se_tag;
3612 time_t re_time, se_time;
3614 SIMPLEQ_FOREACH(re, tags, entry) {
3615 se = SIMPLEQ_FIRST(sorted);
3616 if (se == NULL) {
3617 err = got_reflist_entry_dup(&new, re);
3618 if (err)
3619 return err;
3620 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3621 continue;
3622 } else {
3623 err = got_ref_resolve(&re_id, repo, re->ref);
3624 if (err)
3625 break;
3626 err = got_object_open_as_tag(&re_tag, repo, re_id);
3627 free(re_id);
3628 if (err)
3629 break;
3630 re_time = got_object_tag_get_tagger_time(re_tag);
3631 got_object_tag_close(re_tag);
3634 while (se) {
3635 err = got_ref_resolve(&se_id, repo, re->ref);
3636 if (err)
3637 break;
3638 err = got_object_open_as_tag(&se_tag, repo, se_id);
3639 free(se_id);
3640 if (err)
3641 break;
3642 se_time = got_object_tag_get_tagger_time(se_tag);
3643 got_object_tag_close(se_tag);
3645 if (se_time > re_time) {
3646 err = got_reflist_entry_dup(&new, re);
3647 if (err)
3648 return err;
3649 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3650 break;
3652 se = SIMPLEQ_NEXT(se, entry);
3653 continue;
3656 done:
3657 return err;
3659 #endif
3661 static const struct got_error *
3662 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3664 static const struct got_error *err = NULL;
3665 struct got_reflist_head refs;
3666 struct got_reflist_entry *re;
3668 SIMPLEQ_INIT(&refs);
3670 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3671 if (err)
3672 return err;
3674 SIMPLEQ_FOREACH(re, &refs, entry) {
3675 const char *refname;
3676 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3677 char datebuf[26];
3678 const char *tagger;
3679 time_t tagger_time;
3680 struct got_object_id *id;
3681 struct got_tag_object *tag;
3682 struct got_commit_object *commit = NULL;
3684 refname = got_ref_get_name(re->ref);
3685 if (strncmp(refname, "refs/tags/", 10) != 0)
3686 continue;
3687 refname += 10;
3688 refstr = got_ref_to_str(re->ref);
3689 if (refstr == NULL) {
3690 err = got_error_from_errno("got_ref_to_str");
3691 break;
3693 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3694 free(refstr);
3696 err = got_ref_resolve(&id, repo, re->ref);
3697 if (err)
3698 break;
3699 err = got_object_open_as_tag(&tag, repo, id);
3700 if (err) {
3701 if (err->code != GOT_ERR_OBJ_TYPE) {
3702 free(id);
3703 break;
3705 /* "lightweight" tag */
3706 err = got_object_open_as_commit(&commit, repo, id);
3707 if (err) {
3708 free(id);
3709 break;
3711 tagger = got_object_commit_get_committer(commit);
3712 tagger_time =
3713 got_object_commit_get_committer_time(commit);
3714 err = got_object_id_str(&id_str, id);
3715 free(id);
3716 if (err)
3717 break;
3718 } else {
3719 free(id);
3720 tagger = got_object_tag_get_tagger(tag);
3721 tagger_time = got_object_tag_get_tagger_time(tag);
3722 err = got_object_id_str(&id_str,
3723 got_object_tag_get_object_id(tag));
3724 if (err)
3725 break;
3727 printf("from: %s\n", tagger);
3728 datestr = get_datestr(&tagger_time, datebuf);
3729 if (datestr)
3730 printf("date: %s UTC\n", datestr);
3731 if (commit)
3732 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3733 else {
3734 switch (got_object_tag_get_object_type(tag)) {
3735 case GOT_OBJ_TYPE_BLOB:
3736 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3737 id_str);
3738 break;
3739 case GOT_OBJ_TYPE_TREE:
3740 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3741 id_str);
3742 break;
3743 case GOT_OBJ_TYPE_COMMIT:
3744 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3745 id_str);
3746 break;
3747 case GOT_OBJ_TYPE_TAG:
3748 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3749 id_str);
3750 break;
3751 default:
3752 break;
3755 free(id_str);
3756 if (commit) {
3757 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3758 if (err)
3759 break;
3760 got_object_commit_close(commit);
3761 } else {
3762 tagmsg0 = strdup(got_object_tag_get_message(tag));
3763 got_object_tag_close(tag);
3764 if (tagmsg0 == NULL) {
3765 err = got_error_from_errno("strdup");
3766 break;
3770 tagmsg = tagmsg0;
3771 do {
3772 line = strsep(&tagmsg, "\n");
3773 if (line)
3774 printf(" %s\n", line);
3775 } while (line);
3776 free(tagmsg0);
3779 got_ref_list_free(&refs);
3780 return NULL;
3783 static const struct got_error *
3784 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3785 const char *tag_name, const char *repo_path)
3787 const struct got_error *err = NULL;
3788 char *template = NULL, *initial_content = NULL;
3789 char *editor = NULL;
3790 int fd = -1;
3792 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3793 err = got_error_from_errno("asprintf");
3794 goto done;
3797 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3798 commit_id_str, tag_name) == -1) {
3799 err = got_error_from_errno("asprintf");
3800 goto done;
3803 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3804 if (err)
3805 goto done;
3807 dprintf(fd, initial_content);
3808 close(fd);
3810 err = get_editor(&editor);
3811 if (err)
3812 goto done;
3813 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3814 done:
3815 free(initial_content);
3816 free(template);
3817 free(editor);
3819 /* Editor is done; we can now apply unveil(2) */
3820 if (err == NULL) {
3821 err = apply_unveil(repo_path, 0, NULL);
3822 if (err) {
3823 free(*tagmsg);
3824 *tagmsg = NULL;
3827 return err;
3830 static const struct got_error *
3831 add_tag(struct got_repository *repo, const char *tag_name,
3832 const char *commit_arg, const char *tagmsg_arg)
3834 const struct got_error *err = NULL;
3835 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3836 char *label = NULL, *commit_id_str = NULL;
3837 struct got_reference *ref = NULL;
3838 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3839 char *tagmsg_path = NULL, *tag_id_str = NULL;
3840 int preserve_tagmsg = 0;
3843 * Don't let the user create a tag name with a leading '-'.
3844 * While technically a valid reference name, this case is usually
3845 * an unintended typo.
3847 if (tag_name[0] == '-')
3848 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3850 err = get_author(&tagger, repo);
3851 if (err)
3852 return err;
3854 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3855 GOT_OBJ_TYPE_COMMIT, 1, repo);
3856 if (err)
3857 goto done;
3859 err = got_object_id_str(&commit_id_str, commit_id);
3860 if (err)
3861 goto done;
3863 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3864 refname = strdup(tag_name);
3865 if (refname == NULL) {
3866 err = got_error_from_errno("strdup");
3867 goto done;
3869 tag_name += 10;
3870 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3871 err = got_error_from_errno("asprintf");
3872 goto done;
3875 err = got_ref_open(&ref, repo, refname, 0);
3876 if (err == NULL) {
3877 err = got_error(GOT_ERR_TAG_EXISTS);
3878 goto done;
3879 } else if (err->code != GOT_ERR_NOT_REF)
3880 goto done;
3882 if (tagmsg_arg == NULL) {
3883 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3884 tag_name, got_repo_get_path(repo));
3885 if (err) {
3886 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3887 tagmsg_path != NULL)
3888 preserve_tagmsg = 1;
3889 goto done;
3893 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3894 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3895 if (err) {
3896 if (tagmsg_path)
3897 preserve_tagmsg = 1;
3898 goto done;
3901 err = got_ref_alloc(&ref, refname, tag_id);
3902 if (err) {
3903 if (tagmsg_path)
3904 preserve_tagmsg = 1;
3905 goto done;
3908 err = got_ref_write(ref, repo);
3909 if (err) {
3910 if (tagmsg_path)
3911 preserve_tagmsg = 1;
3912 goto done;
3915 err = got_object_id_str(&tag_id_str, tag_id);
3916 if (err) {
3917 if (tagmsg_path)
3918 preserve_tagmsg = 1;
3919 goto done;
3921 printf("Created tag %s\n", tag_id_str);
3922 done:
3923 if (preserve_tagmsg) {
3924 fprintf(stderr, "%s: tag message preserved in %s\n",
3925 getprogname(), tagmsg_path);
3926 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3927 err = got_error_from_errno2("unlink", tagmsg_path);
3928 free(tag_id_str);
3929 if (ref)
3930 got_ref_close(ref);
3931 free(commit_id);
3932 free(commit_id_str);
3933 free(refname);
3934 free(tagmsg);
3935 free(tagmsg_path);
3936 free(tagger);
3937 return err;
3940 static const struct got_error *
3941 cmd_tag(int argc, char *argv[])
3943 const struct got_error *error = NULL;
3944 struct got_repository *repo = NULL;
3945 struct got_worktree *worktree = NULL;
3946 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3947 char *gitconfig_path = NULL;
3948 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3949 int ch, do_list = 0;
3951 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3952 switch (ch) {
3953 case 'm':
3954 tagmsg = optarg;
3955 break;
3956 case 'r':
3957 repo_path = realpath(optarg, NULL);
3958 if (repo_path == NULL)
3959 return got_error_from_errno2("realpath",
3960 optarg);
3961 got_path_strip_trailing_slashes(repo_path);
3962 break;
3963 case 'l':
3964 do_list = 1;
3965 break;
3966 default:
3967 usage_tag();
3968 /* NOTREACHED */
3972 argc -= optind;
3973 argv += optind;
3975 if (do_list) {
3976 if (tagmsg)
3977 errx(1, "-l and -m options are mutually exclusive\n");
3978 if (argc > 0)
3979 usage_tag();
3980 } else if (argc < 1 || argc > 2)
3981 usage_tag();
3982 else if (argc > 1)
3983 commit_id_arg = argv[1];
3984 tag_name = argv[0];
3986 #ifndef PROFILE
3987 if (do_list) {
3988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3989 NULL) == -1)
3990 err(1, "pledge");
3991 } else {
3992 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3993 "sendfd unveil", NULL) == -1)
3994 err(1, "pledge");
3996 #endif
3997 cwd = getcwd(NULL, 0);
3998 if (cwd == NULL) {
3999 error = got_error_from_errno("getcwd");
4000 goto done;
4003 if (repo_path == NULL) {
4004 error = got_worktree_open(&worktree, cwd);
4005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4006 goto done;
4007 else
4008 error = NULL;
4009 if (worktree) {
4010 repo_path =
4011 strdup(got_worktree_get_repo_path(worktree));
4012 if (repo_path == NULL)
4013 error = got_error_from_errno("strdup");
4014 if (error)
4015 goto done;
4016 } else {
4017 repo_path = strdup(cwd);
4018 if (repo_path == NULL) {
4019 error = got_error_from_errno("strdup");
4020 goto done;
4025 if (do_list) {
4026 error = got_repo_open(&repo, repo_path, NULL);
4027 if (error != NULL)
4028 goto done;
4029 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4030 if (error)
4031 goto done;
4032 error = list_tags(repo, worktree);
4033 } else {
4034 error = get_gitconfig_path(&gitconfig_path);
4035 if (error)
4036 goto done;
4037 error = got_repo_open(&repo, repo_path, gitconfig_path);
4038 if (error != NULL)
4039 goto done;
4041 if (tagmsg) {
4042 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4043 if (error)
4044 goto done;
4047 if (commit_id_arg == NULL) {
4048 struct got_reference *head_ref;
4049 struct got_object_id *commit_id;
4050 error = got_ref_open(&head_ref, repo,
4051 worktree ? got_worktree_get_head_ref_name(worktree)
4052 : GOT_REF_HEAD, 0);
4053 if (error)
4054 goto done;
4055 error = got_ref_resolve(&commit_id, repo, head_ref);
4056 got_ref_close(head_ref);
4057 if (error)
4058 goto done;
4059 error = got_object_id_str(&commit_id_str, commit_id);
4060 free(commit_id);
4061 if (error)
4062 goto done;
4065 error = add_tag(repo, tag_name,
4066 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4068 done:
4069 if (repo)
4070 got_repo_close(repo);
4071 if (worktree)
4072 got_worktree_close(worktree);
4073 free(cwd);
4074 free(repo_path);
4075 free(gitconfig_path);
4076 free(commit_id_str);
4077 return error;
4080 __dead static void
4081 usage_add(void)
4083 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4084 getprogname());
4085 exit(1);
4088 static const struct got_error *
4089 add_progress(void *arg, unsigned char status, const char *path)
4091 while (path[0] == '/')
4092 path++;
4093 printf("%c %s\n", status, path);
4094 return NULL;
4097 static const struct got_error *
4098 cmd_add(int argc, char *argv[])
4100 const struct got_error *error = NULL;
4101 struct got_repository *repo = NULL;
4102 struct got_worktree *worktree = NULL;
4103 char *cwd = NULL;
4104 struct got_pathlist_head paths;
4105 struct got_pathlist_entry *pe;
4106 int ch, can_recurse = 0, no_ignores = 0;
4108 TAILQ_INIT(&paths);
4110 while ((ch = getopt(argc, argv, "IR")) != -1) {
4111 switch (ch) {
4112 case 'I':
4113 no_ignores = 1;
4114 break;
4115 case 'R':
4116 can_recurse = 1;
4117 break;
4118 default:
4119 usage_add();
4120 /* NOTREACHED */
4124 argc -= optind;
4125 argv += optind;
4127 #ifndef PROFILE
4128 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4129 NULL) == -1)
4130 err(1, "pledge");
4131 #endif
4132 if (argc < 1)
4133 usage_add();
4135 cwd = getcwd(NULL, 0);
4136 if (cwd == NULL) {
4137 error = got_error_from_errno("getcwd");
4138 goto done;
4141 error = got_worktree_open(&worktree, cwd);
4142 if (error)
4143 goto done;
4145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4146 NULL);
4147 if (error != NULL)
4148 goto done;
4150 error = apply_unveil(got_repo_get_path(repo), 1,
4151 got_worktree_get_root_path(worktree));
4152 if (error)
4153 goto done;
4155 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4156 if (error)
4157 goto done;
4159 if (!can_recurse && no_ignores) {
4160 error = got_error_msg(GOT_ERR_BAD_PATH,
4161 "disregarding ignores requires -R option");
4162 goto done;
4166 if (!can_recurse) {
4167 char *ondisk_path;
4168 struct stat sb;
4169 TAILQ_FOREACH(pe, &paths, entry) {
4170 if (asprintf(&ondisk_path, "%s/%s",
4171 got_worktree_get_root_path(worktree),
4172 pe->path) == -1) {
4173 error = got_error_from_errno("asprintf");
4174 goto done;
4176 if (lstat(ondisk_path, &sb) == -1) {
4177 if (errno == ENOENT) {
4178 free(ondisk_path);
4179 continue;
4181 error = got_error_from_errno2("lstat",
4182 ondisk_path);
4183 free(ondisk_path);
4184 goto done;
4186 free(ondisk_path);
4187 if (S_ISDIR(sb.st_mode)) {
4188 error = got_error_msg(GOT_ERR_BAD_PATH,
4189 "adding directories requires -R option");
4190 goto done;
4195 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4196 NULL, repo, no_ignores);
4197 done:
4198 if (repo)
4199 got_repo_close(repo);
4200 if (worktree)
4201 got_worktree_close(worktree);
4202 TAILQ_FOREACH(pe, &paths, entry)
4203 free((char *)pe->path);
4204 got_pathlist_free(&paths);
4205 free(cwd);
4206 return error;
4209 __dead static void
4210 usage_remove(void)
4212 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4213 getprogname());
4214 exit(1);
4217 static const struct got_error *
4218 print_remove_status(void *arg, unsigned char status,
4219 unsigned char staged_status, const char *path)
4221 while (path[0] == '/')
4222 path++;
4223 if (status == GOT_STATUS_NONEXISTENT)
4224 return NULL;
4225 if (status == staged_status && (status == GOT_STATUS_DELETE))
4226 status = GOT_STATUS_NO_CHANGE;
4227 printf("%c%c %s\n", status, staged_status, path);
4228 return NULL;
4231 static const struct got_error *
4232 cmd_remove(int argc, char *argv[])
4234 const struct got_error *error = NULL;
4235 struct got_worktree *worktree = NULL;
4236 struct got_repository *repo = NULL;
4237 char *cwd = NULL;
4238 struct got_pathlist_head paths;
4239 struct got_pathlist_entry *pe;
4240 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4242 TAILQ_INIT(&paths);
4244 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4245 switch (ch) {
4246 case 'f':
4247 delete_local_mods = 1;
4248 break;
4249 case 'k':
4250 keep_on_disk = 1;
4251 break;
4252 case 'R':
4253 can_recurse = 1;
4254 break;
4255 default:
4256 usage_remove();
4257 /* NOTREACHED */
4261 argc -= optind;
4262 argv += optind;
4264 #ifndef PROFILE
4265 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4266 NULL) == -1)
4267 err(1, "pledge");
4268 #endif
4269 if (argc < 1)
4270 usage_remove();
4272 cwd = getcwd(NULL, 0);
4273 if (cwd == NULL) {
4274 error = got_error_from_errno("getcwd");
4275 goto done;
4277 error = got_worktree_open(&worktree, cwd);
4278 if (error)
4279 goto done;
4281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4282 NULL);
4283 if (error)
4284 goto done;
4286 error = apply_unveil(got_repo_get_path(repo), 1,
4287 got_worktree_get_root_path(worktree));
4288 if (error)
4289 goto done;
4291 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4292 if (error)
4293 goto done;
4295 if (!can_recurse) {
4296 char *ondisk_path;
4297 struct stat sb;
4298 TAILQ_FOREACH(pe, &paths, entry) {
4299 if (asprintf(&ondisk_path, "%s/%s",
4300 got_worktree_get_root_path(worktree),
4301 pe->path) == -1) {
4302 error = got_error_from_errno("asprintf");
4303 goto done;
4305 if (lstat(ondisk_path, &sb) == -1) {
4306 if (errno == ENOENT) {
4307 free(ondisk_path);
4308 continue;
4310 error = got_error_from_errno2("lstat",
4311 ondisk_path);
4312 free(ondisk_path);
4313 goto done;
4315 free(ondisk_path);
4316 if (S_ISDIR(sb.st_mode)) {
4317 error = got_error_msg(GOT_ERR_BAD_PATH,
4318 "removing directories requires -R option");
4319 goto done;
4324 error = got_worktree_schedule_delete(worktree, &paths,
4325 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4326 done:
4327 if (repo)
4328 got_repo_close(repo);
4329 if (worktree)
4330 got_worktree_close(worktree);
4331 TAILQ_FOREACH(pe, &paths, entry)
4332 free((char *)pe->path);
4333 got_pathlist_free(&paths);
4334 free(cwd);
4335 return error;
4338 __dead static void
4339 usage_revert(void)
4341 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4342 "path ...\n", getprogname());
4343 exit(1);
4346 static const struct got_error *
4347 revert_progress(void *arg, unsigned char status, const char *path)
4349 if (status == GOT_STATUS_UNVERSIONED)
4350 return NULL;
4352 while (path[0] == '/')
4353 path++;
4354 printf("%c %s\n", status, path);
4355 return NULL;
4358 struct choose_patch_arg {
4359 FILE *patch_script_file;
4360 const char *action;
4363 static const struct got_error *
4364 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4365 int nchanges, const char *action)
4367 char *line = NULL;
4368 size_t linesize = 0;
4369 ssize_t linelen;
4371 switch (status) {
4372 case GOT_STATUS_ADD:
4373 printf("A %s\n%s this addition? [y/n] ", path, action);
4374 break;
4375 case GOT_STATUS_DELETE:
4376 printf("D %s\n%s this deletion? [y/n] ", path, action);
4377 break;
4378 case GOT_STATUS_MODIFY:
4379 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4380 return got_error_from_errno("fseek");
4381 printf(GOT_COMMIT_SEP_STR);
4382 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4383 printf("%s", line);
4384 if (ferror(patch_file))
4385 return got_error_from_errno("getline");
4386 printf(GOT_COMMIT_SEP_STR);
4387 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4388 path, n, nchanges, action);
4389 break;
4390 default:
4391 return got_error_path(path, GOT_ERR_FILE_STATUS);
4394 return NULL;
4397 static const struct got_error *
4398 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4399 FILE *patch_file, int n, int nchanges)
4401 const struct got_error *err = NULL;
4402 char *line = NULL;
4403 size_t linesize = 0;
4404 ssize_t linelen;
4405 int resp = ' ';
4406 struct choose_patch_arg *a = arg;
4408 *choice = GOT_PATCH_CHOICE_NONE;
4410 if (a->patch_script_file) {
4411 char *nl;
4412 err = show_change(status, path, patch_file, n, nchanges,
4413 a->action);
4414 if (err)
4415 return err;
4416 linelen = getline(&line, &linesize, a->patch_script_file);
4417 if (linelen == -1) {
4418 if (ferror(a->patch_script_file))
4419 return got_error_from_errno("getline");
4420 return NULL;
4422 nl = strchr(line, '\n');
4423 if (nl)
4424 *nl = '\0';
4425 if (strcmp(line, "y") == 0) {
4426 *choice = GOT_PATCH_CHOICE_YES;
4427 printf("y\n");
4428 } else if (strcmp(line, "n") == 0) {
4429 *choice = GOT_PATCH_CHOICE_NO;
4430 printf("n\n");
4431 } else if (strcmp(line, "q") == 0 &&
4432 status == GOT_STATUS_MODIFY) {
4433 *choice = GOT_PATCH_CHOICE_QUIT;
4434 printf("q\n");
4435 } else
4436 printf("invalid response '%s'\n", line);
4437 free(line);
4438 return NULL;
4441 while (resp != 'y' && resp != 'n' && resp != 'q') {
4442 err = show_change(status, path, patch_file, n, nchanges,
4443 a->action);
4444 if (err)
4445 return err;
4446 resp = getchar();
4447 if (resp == '\n')
4448 resp = getchar();
4449 if (status == GOT_STATUS_MODIFY) {
4450 if (resp != 'y' && resp != 'n' && resp != 'q') {
4451 printf("invalid response '%c'\n", resp);
4452 resp = ' ';
4454 } else if (resp != 'y' && resp != 'n') {
4455 printf("invalid response '%c'\n", resp);
4456 resp = ' ';
4460 if (resp == 'y')
4461 *choice = GOT_PATCH_CHOICE_YES;
4462 else if (resp == 'n')
4463 *choice = GOT_PATCH_CHOICE_NO;
4464 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4465 *choice = GOT_PATCH_CHOICE_QUIT;
4467 return NULL;
4471 static const struct got_error *
4472 cmd_revert(int argc, char *argv[])
4474 const struct got_error *error = NULL;
4475 struct got_worktree *worktree = NULL;
4476 struct got_repository *repo = NULL;
4477 char *cwd = NULL, *path = NULL;
4478 struct got_pathlist_head paths;
4479 struct got_pathlist_entry *pe;
4480 int ch, can_recurse = 0, pflag = 0;
4481 FILE *patch_script_file = NULL;
4482 const char *patch_script_path = NULL;
4483 struct choose_patch_arg cpa;
4485 TAILQ_INIT(&paths);
4487 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4488 switch (ch) {
4489 case 'p':
4490 pflag = 1;
4491 break;
4492 case 'F':
4493 patch_script_path = optarg;
4494 break;
4495 case 'R':
4496 can_recurse = 1;
4497 break;
4498 default:
4499 usage_revert();
4500 /* NOTREACHED */
4504 argc -= optind;
4505 argv += optind;
4507 #ifndef PROFILE
4508 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4509 "unveil", NULL) == -1)
4510 err(1, "pledge");
4511 #endif
4512 if (argc < 1)
4513 usage_revert();
4514 if (patch_script_path && !pflag)
4515 errx(1, "-F option can only be used together with -p option");
4517 cwd = getcwd(NULL, 0);
4518 if (cwd == NULL) {
4519 error = got_error_from_errno("getcwd");
4520 goto done;
4522 error = got_worktree_open(&worktree, cwd);
4523 if (error)
4524 goto done;
4526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4527 NULL);
4528 if (error != NULL)
4529 goto done;
4531 if (patch_script_path) {
4532 patch_script_file = fopen(patch_script_path, "r");
4533 if (patch_script_file == NULL) {
4534 error = got_error_from_errno2("fopen",
4535 patch_script_path);
4536 goto done;
4539 error = apply_unveil(got_repo_get_path(repo), 1,
4540 got_worktree_get_root_path(worktree));
4541 if (error)
4542 goto done;
4544 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4545 if (error)
4546 goto done;
4548 if (!can_recurse) {
4549 char *ondisk_path;
4550 struct stat sb;
4551 TAILQ_FOREACH(pe, &paths, entry) {
4552 if (asprintf(&ondisk_path, "%s/%s",
4553 got_worktree_get_root_path(worktree),
4554 pe->path) == -1) {
4555 error = got_error_from_errno("asprintf");
4556 goto done;
4558 if (lstat(ondisk_path, &sb) == -1) {
4559 if (errno == ENOENT) {
4560 free(ondisk_path);
4561 continue;
4563 error = got_error_from_errno2("lstat",
4564 ondisk_path);
4565 free(ondisk_path);
4566 goto done;
4568 free(ondisk_path);
4569 if (S_ISDIR(sb.st_mode)) {
4570 error = got_error_msg(GOT_ERR_BAD_PATH,
4571 "reverting directories requires -R option");
4572 goto done;
4577 cpa.patch_script_file = patch_script_file;
4578 cpa.action = "revert";
4579 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4580 pflag ? choose_patch : NULL, &cpa, repo);
4581 done:
4582 if (patch_script_file && fclose(patch_script_file) == EOF &&
4583 error == NULL)
4584 error = got_error_from_errno2("fclose", patch_script_path);
4585 if (repo)
4586 got_repo_close(repo);
4587 if (worktree)
4588 got_worktree_close(worktree);
4589 free(path);
4590 free(cwd);
4591 return error;
4594 __dead static void
4595 usage_commit(void)
4597 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4598 getprogname());
4599 exit(1);
4602 struct collect_commit_logmsg_arg {
4603 const char *cmdline_log;
4604 const char *editor;
4605 const char *worktree_path;
4606 const char *branch_name;
4607 const char *repo_path;
4608 char *logmsg_path;
4612 static const struct got_error *
4613 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4614 void *arg)
4616 char *initial_content = NULL;
4617 struct got_pathlist_entry *pe;
4618 const struct got_error *err = NULL;
4619 char *template = NULL;
4620 struct collect_commit_logmsg_arg *a = arg;
4621 int fd;
4622 size_t len;
4624 /* if a message was specified on the command line, just use it */
4625 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4626 len = strlen(a->cmdline_log) + 1;
4627 *logmsg = malloc(len + 1);
4628 if (*logmsg == NULL)
4629 return got_error_from_errno("malloc");
4630 strlcpy(*logmsg, a->cmdline_log, len);
4631 return NULL;
4634 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4635 return got_error_from_errno("asprintf");
4637 if (asprintf(&initial_content,
4638 "\n# changes to be committed on branch %s:\n",
4639 a->branch_name) == -1)
4640 return got_error_from_errno("asprintf");
4642 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4643 if (err)
4644 goto done;
4646 dprintf(fd, initial_content);
4648 TAILQ_FOREACH(pe, commitable_paths, entry) {
4649 struct got_commitable *ct = pe->data;
4650 dprintf(fd, "# %c %s\n",
4651 got_commitable_get_status(ct),
4652 got_commitable_get_path(ct));
4654 close(fd);
4656 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4657 done:
4658 free(initial_content);
4659 free(template);
4661 /* Editor is done; we can now apply unveil(2) */
4662 if (err == NULL) {
4663 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4664 if (err) {
4665 free(*logmsg);
4666 *logmsg = NULL;
4669 return err;
4672 static const struct got_error *
4673 cmd_commit(int argc, char *argv[])
4675 const struct got_error *error = NULL;
4676 struct got_worktree *worktree = NULL;
4677 struct got_repository *repo = NULL;
4678 char *cwd = NULL, *id_str = NULL;
4679 struct got_object_id *id = NULL;
4680 const char *logmsg = NULL;
4681 struct collect_commit_logmsg_arg cl_arg;
4682 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4683 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4684 struct got_pathlist_head paths;
4686 TAILQ_INIT(&paths);
4687 cl_arg.logmsg_path = NULL;
4689 while ((ch = getopt(argc, argv, "m:")) != -1) {
4690 switch (ch) {
4691 case 'm':
4692 logmsg = optarg;
4693 break;
4694 default:
4695 usage_commit();
4696 /* NOTREACHED */
4700 argc -= optind;
4701 argv += optind;
4703 #ifndef PROFILE
4704 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4705 "unveil", NULL) == -1)
4706 err(1, "pledge");
4707 #endif
4708 cwd = getcwd(NULL, 0);
4709 if (cwd == NULL) {
4710 error = got_error_from_errno("getcwd");
4711 goto done;
4713 error = got_worktree_open(&worktree, cwd);
4714 if (error)
4715 goto done;
4717 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4718 if (error)
4719 goto done;
4720 if (rebase_in_progress) {
4721 error = got_error(GOT_ERR_REBASING);
4722 goto done;
4725 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4726 worktree);
4727 if (error)
4728 goto done;
4730 error = get_gitconfig_path(&gitconfig_path);
4731 if (error)
4732 goto done;
4733 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4734 gitconfig_path);
4735 if (error != NULL)
4736 goto done;
4738 error = get_author(&author, repo);
4739 if (error)
4740 return error;
4743 * unveil(2) traverses exec(2); if an editor is used we have
4744 * to apply unveil after the log message has been written.
4746 if (logmsg == NULL || strlen(logmsg) == 0)
4747 error = get_editor(&editor);
4748 else
4749 error = apply_unveil(got_repo_get_path(repo), 0,
4750 got_worktree_get_root_path(worktree));
4751 if (error)
4752 goto done;
4754 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4755 if (error)
4756 goto done;
4758 cl_arg.editor = editor;
4759 cl_arg.cmdline_log = logmsg;
4760 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4761 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4762 if (!histedit_in_progress) {
4763 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4764 error = got_error(GOT_ERR_COMMIT_BRANCH);
4765 goto done;
4767 cl_arg.branch_name += 11;
4769 cl_arg.repo_path = got_repo_get_path(repo);
4770 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4771 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4772 if (error) {
4773 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4774 cl_arg.logmsg_path != NULL)
4775 preserve_logmsg = 1;
4776 goto done;
4779 error = got_object_id_str(&id_str, id);
4780 if (error)
4781 goto done;
4782 printf("Created commit %s\n", id_str);
4783 done:
4784 if (preserve_logmsg) {
4785 fprintf(stderr, "%s: log message preserved in %s\n",
4786 getprogname(), cl_arg.logmsg_path);
4787 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4788 error == NULL)
4789 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4790 free(cl_arg.logmsg_path);
4791 if (repo)
4792 got_repo_close(repo);
4793 if (worktree)
4794 got_worktree_close(worktree);
4795 free(cwd);
4796 free(id_str);
4797 free(gitconfig_path);
4798 free(editor);
4799 free(author);
4800 return error;
4803 __dead static void
4804 usage_cherrypick(void)
4806 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4807 exit(1);
4810 static const struct got_error *
4811 cmd_cherrypick(int argc, char *argv[])
4813 const struct got_error *error = NULL;
4814 struct got_worktree *worktree = NULL;
4815 struct got_repository *repo = NULL;
4816 char *cwd = NULL, *commit_id_str = NULL;
4817 struct got_object_id *commit_id = NULL;
4818 struct got_commit_object *commit = NULL;
4819 struct got_object_qid *pid;
4820 struct got_reference *head_ref = NULL;
4821 int ch, did_something = 0;
4823 while ((ch = getopt(argc, argv, "")) != -1) {
4824 switch (ch) {
4825 default:
4826 usage_cherrypick();
4827 /* NOTREACHED */
4831 argc -= optind;
4832 argv += optind;
4834 #ifndef PROFILE
4835 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4836 "unveil", NULL) == -1)
4837 err(1, "pledge");
4838 #endif
4839 if (argc != 1)
4840 usage_cherrypick();
4842 cwd = getcwd(NULL, 0);
4843 if (cwd == NULL) {
4844 error = got_error_from_errno("getcwd");
4845 goto done;
4847 error = got_worktree_open(&worktree, cwd);
4848 if (error)
4849 goto done;
4851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4852 NULL);
4853 if (error != NULL)
4854 goto done;
4856 error = apply_unveil(got_repo_get_path(repo), 0,
4857 got_worktree_get_root_path(worktree));
4858 if (error)
4859 goto done;
4861 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4862 GOT_OBJ_TYPE_COMMIT, repo);
4863 if (error != NULL) {
4864 struct got_reference *ref;
4865 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4866 goto done;
4867 error = got_ref_open(&ref, repo, argv[0], 0);
4868 if (error != NULL)
4869 goto done;
4870 error = got_ref_resolve(&commit_id, repo, ref);
4871 got_ref_close(ref);
4872 if (error != NULL)
4873 goto done;
4875 error = got_object_id_str(&commit_id_str, commit_id);
4876 if (error)
4877 goto done;
4879 error = got_ref_open(&head_ref, repo,
4880 got_worktree_get_head_ref_name(worktree), 0);
4881 if (error != NULL)
4882 goto done;
4884 error = check_same_branch(commit_id, head_ref, NULL, repo);
4885 if (error) {
4886 if (error->code != GOT_ERR_ANCESTRY)
4887 goto done;
4888 error = NULL;
4889 } else {
4890 error = got_error(GOT_ERR_SAME_BRANCH);
4891 goto done;
4894 error = got_object_open_as_commit(&commit, repo, commit_id);
4895 if (error)
4896 goto done;
4897 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4898 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4899 commit_id, repo, update_progress, &did_something, check_cancelled,
4900 NULL);
4901 if (error != NULL)
4902 goto done;
4904 if (did_something)
4905 printf("Merged commit %s\n", commit_id_str);
4906 done:
4907 if (commit)
4908 got_object_commit_close(commit);
4909 free(commit_id_str);
4910 if (head_ref)
4911 got_ref_close(head_ref);
4912 if (worktree)
4913 got_worktree_close(worktree);
4914 if (repo)
4915 got_repo_close(repo);
4916 return error;
4919 __dead static void
4920 usage_backout(void)
4922 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4923 exit(1);
4926 static const struct got_error *
4927 cmd_backout(int argc, char *argv[])
4929 const struct got_error *error = NULL;
4930 struct got_worktree *worktree = NULL;
4931 struct got_repository *repo = NULL;
4932 char *cwd = NULL, *commit_id_str = NULL;
4933 struct got_object_id *commit_id = NULL;
4934 struct got_commit_object *commit = NULL;
4935 struct got_object_qid *pid;
4936 struct got_reference *head_ref = NULL;
4937 int ch, did_something = 0;
4939 while ((ch = getopt(argc, argv, "")) != -1) {
4940 switch (ch) {
4941 default:
4942 usage_backout();
4943 /* NOTREACHED */
4947 argc -= optind;
4948 argv += optind;
4950 #ifndef PROFILE
4951 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4952 "unveil", NULL) == -1)
4953 err(1, "pledge");
4954 #endif
4955 if (argc != 1)
4956 usage_backout();
4958 cwd = getcwd(NULL, 0);
4959 if (cwd == NULL) {
4960 error = got_error_from_errno("getcwd");
4961 goto done;
4963 error = got_worktree_open(&worktree, cwd);
4964 if (error)
4965 goto done;
4967 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4968 NULL);
4969 if (error != NULL)
4970 goto done;
4972 error = apply_unveil(got_repo_get_path(repo), 0,
4973 got_worktree_get_root_path(worktree));
4974 if (error)
4975 goto done;
4977 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4978 GOT_OBJ_TYPE_COMMIT, repo);
4979 if (error != NULL) {
4980 struct got_reference *ref;
4981 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4982 goto done;
4983 error = got_ref_open(&ref, repo, argv[0], 0);
4984 if (error != NULL)
4985 goto done;
4986 error = got_ref_resolve(&commit_id, repo, ref);
4987 got_ref_close(ref);
4988 if (error != NULL)
4989 goto done;
4991 error = got_object_id_str(&commit_id_str, commit_id);
4992 if (error)
4993 goto done;
4995 error = got_ref_open(&head_ref, repo,
4996 got_worktree_get_head_ref_name(worktree), 0);
4997 if (error != NULL)
4998 goto done;
5000 error = check_same_branch(commit_id, head_ref, NULL, repo);
5001 if (error)
5002 goto done;
5004 error = got_object_open_as_commit(&commit, repo, commit_id);
5005 if (error)
5006 goto done;
5007 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5008 if (pid == NULL) {
5009 error = got_error(GOT_ERR_ROOT_COMMIT);
5010 goto done;
5013 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5014 update_progress, &did_something, check_cancelled, NULL);
5015 if (error != NULL)
5016 goto done;
5018 if (did_something)
5019 printf("Backed out commit %s\n", commit_id_str);
5020 done:
5021 if (commit)
5022 got_object_commit_close(commit);
5023 free(commit_id_str);
5024 if (head_ref)
5025 got_ref_close(head_ref);
5026 if (worktree)
5027 got_worktree_close(worktree);
5028 if (repo)
5029 got_repo_close(repo);
5030 return error;
5033 __dead static void
5034 usage_rebase(void)
5036 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5037 getprogname());
5038 exit(1);
5041 void
5042 trim_logmsg(char *logmsg, int limit)
5044 char *nl;
5045 size_t len;
5047 len = strlen(logmsg);
5048 if (len > limit)
5049 len = limit;
5050 logmsg[len] = '\0';
5051 nl = strchr(logmsg, '\n');
5052 if (nl)
5053 *nl = '\0';
5056 static const struct got_error *
5057 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5059 const struct got_error *err;
5060 char *logmsg0 = NULL;
5061 const char *s;
5063 err = got_object_commit_get_logmsg(&logmsg0, commit);
5064 if (err)
5065 return err;
5067 s = logmsg0;
5068 while (isspace((unsigned char)s[0]))
5069 s++;
5071 *logmsg = strdup(s);
5072 if (*logmsg == NULL) {
5073 err = got_error_from_errno("strdup");
5074 goto done;
5077 trim_logmsg(*logmsg, limit);
5078 done:
5079 free(logmsg0);
5080 return err;
5083 static const struct got_error *
5084 show_rebase_progress(struct got_commit_object *commit,
5085 struct got_object_id *old_id, struct got_object_id *new_id)
5087 const struct got_error *err;
5088 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5090 err = got_object_id_str(&old_id_str, old_id);
5091 if (err)
5092 goto done;
5094 if (new_id) {
5095 err = got_object_id_str(&new_id_str, new_id);
5096 if (err)
5097 goto done;
5100 old_id_str[12] = '\0';
5101 if (new_id_str)
5102 new_id_str[12] = '\0';
5104 err = get_short_logmsg(&logmsg, 42, commit);
5105 if (err)
5106 goto done;
5108 printf("%s -> %s: %s\n", old_id_str,
5109 new_id_str ? new_id_str : "no-op change", logmsg);
5110 done:
5111 free(old_id_str);
5112 free(new_id_str);
5113 return err;
5116 static const struct got_error *
5117 rebase_progress(void *arg, unsigned char status, const char *path)
5119 unsigned char *rebase_status = arg;
5121 while (path[0] == '/')
5122 path++;
5123 printf("%c %s\n", status, path);
5125 if (*rebase_status == GOT_STATUS_CONFLICT)
5126 return NULL;
5127 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5128 *rebase_status = status;
5129 return NULL;
5132 static const struct got_error *
5133 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5134 struct got_reference *branch, struct got_reference *new_base_branch,
5135 struct got_reference *tmp_branch, struct got_repository *repo)
5137 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5138 return got_worktree_rebase_complete(worktree, fileindex,
5139 new_base_branch, tmp_branch, branch, repo);
5142 static const struct got_error *
5143 rebase_commit(struct got_pathlist_head *merged_paths,
5144 struct got_worktree *worktree, struct got_fileindex *fileindex,
5145 struct got_reference *tmp_branch,
5146 struct got_object_id *commit_id, struct got_repository *repo)
5148 const struct got_error *error;
5149 struct got_commit_object *commit;
5150 struct got_object_id *new_commit_id;
5152 error = got_object_open_as_commit(&commit, repo, commit_id);
5153 if (error)
5154 return error;
5156 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5157 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5158 if (error) {
5159 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5160 goto done;
5161 error = show_rebase_progress(commit, commit_id, NULL);
5162 } else {
5163 error = show_rebase_progress(commit, commit_id, new_commit_id);
5164 free(new_commit_id);
5166 done:
5167 got_object_commit_close(commit);
5168 return error;
5171 struct check_path_prefix_arg {
5172 const char *path_prefix;
5173 size_t len;
5174 int errcode;
5177 static const struct got_error *
5178 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5179 struct got_blob_object *blob2, struct got_object_id *id1,
5180 struct got_object_id *id2, const char *path1, const char *path2,
5181 mode_t mode1, mode_t mode2, struct got_repository *repo)
5183 struct check_path_prefix_arg *a = arg;
5185 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5186 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5187 return got_error(a->errcode);
5189 return NULL;
5192 static const struct got_error *
5193 check_path_prefix(struct got_object_id *parent_id,
5194 struct got_object_id *commit_id, const char *path_prefix,
5195 int errcode, struct got_repository *repo)
5197 const struct got_error *err;
5198 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5199 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5200 struct check_path_prefix_arg cpp_arg;
5202 if (got_path_is_root_dir(path_prefix))
5203 return NULL;
5205 err = got_object_open_as_commit(&commit, repo, commit_id);
5206 if (err)
5207 goto done;
5209 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5210 if (err)
5211 goto done;
5213 err = got_object_open_as_tree(&tree1, repo,
5214 got_object_commit_get_tree_id(parent_commit));
5215 if (err)
5216 goto done;
5218 err = got_object_open_as_tree(&tree2, repo,
5219 got_object_commit_get_tree_id(commit));
5220 if (err)
5221 goto done;
5223 cpp_arg.path_prefix = path_prefix;
5224 while (cpp_arg.path_prefix[0] == '/')
5225 cpp_arg.path_prefix++;
5226 cpp_arg.len = strlen(cpp_arg.path_prefix);
5227 cpp_arg.errcode = errcode;
5228 err = got_diff_tree(tree1, tree2, "", "", repo,
5229 check_path_prefix_in_diff, &cpp_arg, 0);
5230 done:
5231 if (tree1)
5232 got_object_tree_close(tree1);
5233 if (tree2)
5234 got_object_tree_close(tree2);
5235 if (commit)
5236 got_object_commit_close(commit);
5237 if (parent_commit)
5238 got_object_commit_close(parent_commit);
5239 return err;
5242 static const struct got_error *
5243 collect_commits(struct got_object_id_queue *commits,
5244 struct got_object_id *initial_commit_id,
5245 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5246 const char *path_prefix, int path_prefix_errcode,
5247 struct got_repository *repo)
5249 const struct got_error *err = NULL;
5250 struct got_commit_graph *graph = NULL;
5251 struct got_object_id *parent_id = NULL;
5252 struct got_object_qid *qid;
5253 struct got_object_id *commit_id = initial_commit_id;
5255 err = got_commit_graph_open(&graph, "/", 1);
5256 if (err)
5257 return err;
5259 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5260 check_cancelled, NULL);
5261 if (err)
5262 goto done;
5263 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5264 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5265 check_cancelled, NULL);
5266 if (err) {
5267 if (err->code == GOT_ERR_ITER_COMPLETED) {
5268 err = got_error_msg(GOT_ERR_ANCESTRY,
5269 "ran out of commits to rebase before "
5270 "youngest common ancestor commit has "
5271 "been reached?!?");
5273 goto done;
5274 } else {
5275 err = check_path_prefix(parent_id, commit_id,
5276 path_prefix, path_prefix_errcode, repo);
5277 if (err)
5278 goto done;
5280 err = got_object_qid_alloc(&qid, commit_id);
5281 if (err)
5282 goto done;
5283 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5284 commit_id = parent_id;
5287 done:
5288 got_commit_graph_close(graph);
5289 return err;
5292 static const struct got_error *
5293 cmd_rebase(int argc, char *argv[])
5295 const struct got_error *error = NULL;
5296 struct got_worktree *worktree = NULL;
5297 struct got_repository *repo = NULL;
5298 struct got_fileindex *fileindex = NULL;
5299 char *cwd = NULL;
5300 struct got_reference *branch = NULL;
5301 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5302 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5303 struct got_object_id *resume_commit_id = NULL;
5304 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5305 struct got_commit_object *commit = NULL;
5306 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5307 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5308 struct got_object_id_queue commits;
5309 struct got_pathlist_head merged_paths;
5310 const struct got_object_id_queue *parent_ids;
5311 struct got_object_qid *qid, *pid;
5313 SIMPLEQ_INIT(&commits);
5314 TAILQ_INIT(&merged_paths);
5316 while ((ch = getopt(argc, argv, "ac")) != -1) {
5317 switch (ch) {
5318 case 'a':
5319 abort_rebase = 1;
5320 break;
5321 case 'c':
5322 continue_rebase = 1;
5323 break;
5324 default:
5325 usage_rebase();
5326 /* NOTREACHED */
5330 argc -= optind;
5331 argv += optind;
5333 #ifndef PROFILE
5334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5335 "unveil", NULL) == -1)
5336 err(1, "pledge");
5337 #endif
5338 if (abort_rebase && continue_rebase)
5339 usage_rebase();
5340 else if (abort_rebase || continue_rebase) {
5341 if (argc != 0)
5342 usage_rebase();
5343 } else if (argc != 1)
5344 usage_rebase();
5346 cwd = getcwd(NULL, 0);
5347 if (cwd == NULL) {
5348 error = got_error_from_errno("getcwd");
5349 goto done;
5351 error = got_worktree_open(&worktree, cwd);
5352 if (error)
5353 goto done;
5355 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5356 NULL);
5357 if (error != NULL)
5358 goto done;
5360 error = apply_unveil(got_repo_get_path(repo), 0,
5361 got_worktree_get_root_path(worktree));
5362 if (error)
5363 goto done;
5365 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5366 if (error)
5367 goto done;
5369 if (abort_rebase) {
5370 int did_something;
5371 if (!rebase_in_progress) {
5372 error = got_error(GOT_ERR_NOT_REBASING);
5373 goto done;
5375 error = got_worktree_rebase_continue(&resume_commit_id,
5376 &new_base_branch, &tmp_branch, &branch, &fileindex,
5377 worktree, repo);
5378 if (error)
5379 goto done;
5380 printf("Switching work tree to %s\n",
5381 got_ref_get_symref_target(new_base_branch));
5382 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5383 new_base_branch, update_progress, &did_something);
5384 if (error)
5385 goto done;
5386 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5387 goto done; /* nothing else to do */
5390 if (continue_rebase) {
5391 if (!rebase_in_progress) {
5392 error = got_error(GOT_ERR_NOT_REBASING);
5393 goto done;
5395 error = got_worktree_rebase_continue(&resume_commit_id,
5396 &new_base_branch, &tmp_branch, &branch, &fileindex,
5397 worktree, repo);
5398 if (error)
5399 goto done;
5401 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5402 resume_commit_id, repo);
5403 if (error)
5404 goto done;
5406 yca_id = got_object_id_dup(resume_commit_id);
5407 if (yca_id == NULL) {
5408 error = got_error_from_errno("got_object_id_dup");
5409 goto done;
5411 } else {
5412 error = got_ref_open(&branch, repo, argv[0], 0);
5413 if (error != NULL)
5414 goto done;
5417 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5418 if (error)
5419 goto done;
5421 if (!continue_rebase) {
5422 struct got_object_id *base_commit_id;
5424 base_commit_id = got_worktree_get_base_commit_id(worktree);
5425 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5426 base_commit_id, branch_head_commit_id, repo,
5427 check_cancelled, NULL);
5428 if (error)
5429 goto done;
5430 if (yca_id == NULL) {
5431 error = got_error_msg(GOT_ERR_ANCESTRY,
5432 "specified branch shares no common ancestry "
5433 "with work tree's branch");
5434 goto done;
5437 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5438 if (error) {
5439 if (error->code != GOT_ERR_ANCESTRY)
5440 goto done;
5441 error = NULL;
5442 } else {
5443 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5444 "specified branch resolves to a commit which "
5445 "is already contained in work tree's branch");
5446 goto done;
5448 error = got_worktree_rebase_prepare(&new_base_branch,
5449 &tmp_branch, &fileindex, worktree, branch, repo);
5450 if (error)
5451 goto done;
5454 commit_id = branch_head_commit_id;
5455 error = got_object_open_as_commit(&commit, repo, commit_id);
5456 if (error)
5457 goto done;
5459 parent_ids = got_object_commit_get_parent_ids(commit);
5460 pid = SIMPLEQ_FIRST(parent_ids);
5461 if (pid == NULL) {
5462 if (!continue_rebase) {
5463 int did_something;
5464 error = got_worktree_rebase_abort(worktree, fileindex,
5465 repo, new_base_branch, update_progress,
5466 &did_something);
5467 if (error)
5468 goto done;
5469 printf("Rebase of %s aborted\n",
5470 got_ref_get_name(branch));
5472 error = got_error(GOT_ERR_EMPTY_REBASE);
5473 goto done;
5475 error = collect_commits(&commits, commit_id, pid->id,
5476 yca_id, got_worktree_get_path_prefix(worktree),
5477 GOT_ERR_REBASE_PATH, repo);
5478 got_object_commit_close(commit);
5479 commit = NULL;
5480 if (error)
5481 goto done;
5483 if (SIMPLEQ_EMPTY(&commits)) {
5484 if (continue_rebase) {
5485 error = rebase_complete(worktree, fileindex,
5486 branch, new_base_branch, tmp_branch, repo);
5487 goto done;
5488 } else {
5489 /* Fast-forward the reference of the branch. */
5490 struct got_object_id *new_head_commit_id;
5491 char *id_str;
5492 error = got_ref_resolve(&new_head_commit_id, repo,
5493 new_base_branch);
5494 if (error)
5495 goto done;
5496 error = got_object_id_str(&id_str, new_head_commit_id);
5497 printf("Forwarding %s to commit %s\n",
5498 got_ref_get_name(branch), id_str);
5499 free(id_str);
5500 error = got_ref_change_ref(branch,
5501 new_head_commit_id);
5502 if (error)
5503 goto done;
5507 pid = NULL;
5508 SIMPLEQ_FOREACH(qid, &commits, entry) {
5509 commit_id = qid->id;
5510 parent_id = pid ? pid->id : yca_id;
5511 pid = qid;
5513 error = got_worktree_rebase_merge_files(&merged_paths,
5514 worktree, fileindex, parent_id, commit_id, repo,
5515 rebase_progress, &rebase_status, check_cancelled, NULL);
5516 if (error)
5517 goto done;
5519 if (rebase_status == GOT_STATUS_CONFLICT) {
5520 got_worktree_rebase_pathlist_free(&merged_paths);
5521 break;
5524 error = rebase_commit(&merged_paths, worktree, fileindex,
5525 tmp_branch, commit_id, repo);
5526 got_worktree_rebase_pathlist_free(&merged_paths);
5527 if (error)
5528 goto done;
5531 if (rebase_status == GOT_STATUS_CONFLICT) {
5532 error = got_worktree_rebase_postpone(worktree, fileindex);
5533 if (error)
5534 goto done;
5535 error = got_error_msg(GOT_ERR_CONFLICTS,
5536 "conflicts must be resolved before rebasing can continue");
5537 } else
5538 error = rebase_complete(worktree, fileindex, branch,
5539 new_base_branch, tmp_branch, repo);
5540 done:
5541 got_object_id_queue_free(&commits);
5542 free(branch_head_commit_id);
5543 free(resume_commit_id);
5544 free(yca_id);
5545 if (commit)
5546 got_object_commit_close(commit);
5547 if (branch)
5548 got_ref_close(branch);
5549 if (new_base_branch)
5550 got_ref_close(new_base_branch);
5551 if (tmp_branch)
5552 got_ref_close(tmp_branch);
5553 if (worktree)
5554 got_worktree_close(worktree);
5555 if (repo)
5556 got_repo_close(repo);
5557 return error;
5560 __dead static void
5561 usage_histedit(void)
5563 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5564 getprogname());
5565 exit(1);
5568 #define GOT_HISTEDIT_PICK 'p'
5569 #define GOT_HISTEDIT_EDIT 'e'
5570 #define GOT_HISTEDIT_FOLD 'f'
5571 #define GOT_HISTEDIT_DROP 'd'
5572 #define GOT_HISTEDIT_MESG 'm'
5574 static struct got_histedit_cmd {
5575 unsigned char code;
5576 const char *name;
5577 const char *desc;
5578 } got_histedit_cmds[] = {
5579 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5580 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5581 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5582 "be used" },
5583 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5584 { GOT_HISTEDIT_MESG, "mesg",
5585 "single-line log message for commit above (open editor if empty)" },
5588 struct got_histedit_list_entry {
5589 TAILQ_ENTRY(got_histedit_list_entry) entry;
5590 struct got_object_id *commit_id;
5591 const struct got_histedit_cmd *cmd;
5592 char *logmsg;
5594 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5596 static const struct got_error *
5597 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5598 FILE *f, struct got_repository *repo)
5600 const struct got_error *err = NULL;
5601 char *logmsg = NULL, *id_str = NULL;
5602 struct got_commit_object *commit = NULL;
5603 int n;
5605 err = got_object_open_as_commit(&commit, repo, commit_id);
5606 if (err)
5607 goto done;
5609 err = get_short_logmsg(&logmsg, 34, commit);
5610 if (err)
5611 goto done;
5613 err = got_object_id_str(&id_str, commit_id);
5614 if (err)
5615 goto done;
5617 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5618 if (n < 0)
5619 err = got_ferror(f, GOT_ERR_IO);
5620 done:
5621 if (commit)
5622 got_object_commit_close(commit);
5623 free(id_str);
5624 free(logmsg);
5625 return err;
5628 static const struct got_error *
5629 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5630 struct got_repository *repo)
5632 const struct got_error *err = NULL;
5633 struct got_object_qid *qid;
5635 if (SIMPLEQ_EMPTY(commits))
5636 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5638 SIMPLEQ_FOREACH(qid, commits, entry) {
5639 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5640 f, repo);
5641 if (err)
5642 break;
5645 return err;
5648 static const struct got_error *
5649 write_cmd_list(FILE *f, const char *branch_name,
5650 struct got_object_id_queue *commits)
5652 const struct got_error *err = NULL;
5653 int n, i;
5654 char *id_str;
5655 struct got_object_qid *qid;
5657 qid = SIMPLEQ_FIRST(commits);
5658 err = got_object_id_str(&id_str, qid->id);
5659 if (err)
5660 return err;
5662 n = fprintf(f,
5663 "# Editing the history of branch '%s' starting at\n"
5664 "# commit %s\n"
5665 "# Commits will be processed in order from top to "
5666 "bottom of this file.\n", branch_name, id_str);
5667 if (n < 0) {
5668 err = got_ferror(f, GOT_ERR_IO);
5669 goto done;
5672 n = fprintf(f, "# Available histedit commands:\n");
5673 if (n < 0) {
5674 err = got_ferror(f, GOT_ERR_IO);
5675 goto done;
5678 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5679 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5680 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5681 cmd->desc);
5682 if (n < 0) {
5683 err = got_ferror(f, GOT_ERR_IO);
5684 break;
5687 done:
5688 free(id_str);
5689 return err;
5692 static const struct got_error *
5693 histedit_syntax_error(int lineno)
5695 static char msg[42];
5696 int ret;
5698 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5699 lineno);
5700 if (ret == -1 || ret >= sizeof(msg))
5701 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5703 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5706 static const struct got_error *
5707 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5708 char *logmsg, struct got_repository *repo)
5710 const struct got_error *err;
5711 struct got_commit_object *folded_commit = NULL;
5712 char *id_str, *folded_logmsg = NULL;
5714 err = got_object_id_str(&id_str, hle->commit_id);
5715 if (err)
5716 return err;
5718 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5719 if (err)
5720 goto done;
5722 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5723 if (err)
5724 goto done;
5725 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5726 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5727 folded_logmsg) == -1) {
5728 err = got_error_from_errno("asprintf");
5730 done:
5731 if (folded_commit)
5732 got_object_commit_close(folded_commit);
5733 free(id_str);
5734 free(folded_logmsg);
5735 return err;
5738 static struct got_histedit_list_entry *
5739 get_folded_commits(struct got_histedit_list_entry *hle)
5741 struct got_histedit_list_entry *prev, *folded = NULL;
5743 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5744 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5745 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5746 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5747 folded = prev;
5748 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5751 return folded;
5754 static const struct got_error *
5755 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5756 struct got_repository *repo)
5758 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5759 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5760 const struct got_error *err = NULL;
5761 struct got_commit_object *commit = NULL;
5762 int fd;
5763 struct got_histedit_list_entry *folded = NULL;
5765 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5766 if (err)
5767 return err;
5769 folded = get_folded_commits(hle);
5770 if (folded) {
5771 while (folded != hle) {
5772 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5773 folded = TAILQ_NEXT(folded, entry);
5774 continue;
5776 err = append_folded_commit_msg(&new_msg, folded,
5777 logmsg, repo);
5778 if (err)
5779 goto done;
5780 free(logmsg);
5781 logmsg = new_msg;
5782 folded = TAILQ_NEXT(folded, entry);
5786 err = got_object_id_str(&id_str, hle->commit_id);
5787 if (err)
5788 goto done;
5789 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5790 if (err)
5791 goto done;
5792 if (asprintf(&new_msg,
5793 "%s\n# original log message of commit %s: %s",
5794 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5795 err = got_error_from_errno("asprintf");
5796 goto done;
5798 free(logmsg);
5799 logmsg = new_msg;
5801 err = got_object_id_str(&id_str, hle->commit_id);
5802 if (err)
5803 goto done;
5805 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5806 if (err)
5807 goto done;
5809 dprintf(fd, logmsg);
5810 close(fd);
5812 err = get_editor(&editor);
5813 if (err)
5814 goto done;
5816 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5817 if (err) {
5818 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5819 goto done;
5820 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5822 done:
5823 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5824 err = got_error_from_errno2("unlink", logmsg_path);
5825 free(logmsg_path);
5826 free(logmsg);
5827 free(orig_logmsg);
5828 free(editor);
5829 if (commit)
5830 got_object_commit_close(commit);
5831 return err;
5834 static const struct got_error *
5835 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5836 FILE *f, struct got_repository *repo)
5838 const struct got_error *err = NULL;
5839 char *line = NULL, *p, *end;
5840 size_t size;
5841 ssize_t len;
5842 int lineno = 0, i;
5843 const struct got_histedit_cmd *cmd;
5844 struct got_object_id *commit_id = NULL;
5845 struct got_histedit_list_entry *hle = NULL;
5847 for (;;) {
5848 len = getline(&line, &size, f);
5849 if (len == -1) {
5850 const struct got_error *getline_err;
5851 if (feof(f))
5852 break;
5853 getline_err = got_error_from_errno("getline");
5854 err = got_ferror(f, getline_err->code);
5855 break;
5857 lineno++;
5858 p = line;
5859 while (isspace((unsigned char)p[0]))
5860 p++;
5861 if (p[0] == '#' || p[0] == '\0') {
5862 free(line);
5863 line = NULL;
5864 continue;
5866 cmd = NULL;
5867 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5868 cmd = &got_histedit_cmds[i];
5869 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5870 isspace((unsigned char)p[strlen(cmd->name)])) {
5871 p += strlen(cmd->name);
5872 break;
5874 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5875 p++;
5876 break;
5879 if (i == nitems(got_histedit_cmds)) {
5880 err = histedit_syntax_error(lineno);
5881 break;
5883 while (isspace((unsigned char)p[0]))
5884 p++;
5885 if (cmd->code == GOT_HISTEDIT_MESG) {
5886 if (hle == NULL || hle->logmsg != NULL) {
5887 err = got_error(GOT_ERR_HISTEDIT_CMD);
5888 break;
5890 if (p[0] == '\0') {
5891 err = histedit_edit_logmsg(hle, repo);
5892 if (err)
5893 break;
5894 } else {
5895 hle->logmsg = strdup(p);
5896 if (hle->logmsg == NULL) {
5897 err = got_error_from_errno("strdup");
5898 break;
5901 free(line);
5902 line = NULL;
5903 continue;
5904 } else {
5905 end = p;
5906 while (end[0] && !isspace((unsigned char)end[0]))
5907 end++;
5908 *end = '\0';
5910 err = got_object_resolve_id_str(&commit_id, repo, p);
5911 if (err) {
5912 /* override error code */
5913 err = histedit_syntax_error(lineno);
5914 break;
5917 hle = malloc(sizeof(*hle));
5918 if (hle == NULL) {
5919 err = got_error_from_errno("malloc");
5920 break;
5922 hle->cmd = cmd;
5923 hle->commit_id = commit_id;
5924 hle->logmsg = NULL;
5925 commit_id = NULL;
5926 free(line);
5927 line = NULL;
5928 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5931 free(line);
5932 free(commit_id);
5933 return err;
5936 static const struct got_error *
5937 histedit_check_script(struct got_histedit_list *histedit_cmds,
5938 struct got_object_id_queue *commits, struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 struct got_object_qid *qid;
5942 struct got_histedit_list_entry *hle;
5943 static char msg[80];
5944 char *id_str;
5946 if (TAILQ_EMPTY(histedit_cmds))
5947 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5948 "histedit script contains no commands");
5949 if (SIMPLEQ_EMPTY(commits))
5950 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5952 SIMPLEQ_FOREACH(qid, commits, entry) {
5953 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5954 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5955 break;
5957 if (hle == NULL) {
5958 err = got_object_id_str(&id_str, qid->id);
5959 if (err)
5960 return err;
5961 snprintf(msg, sizeof(msg),
5962 "commit %s missing from histedit script", id_str);
5963 free(id_str);
5964 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5968 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5969 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5970 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5971 "last commit in histedit script cannot be folded");
5973 return NULL;
5976 static const struct got_error *
5977 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5978 const char *path, struct got_object_id_queue *commits,
5979 struct got_repository *repo)
5981 const struct got_error *err = NULL;
5982 char *editor;
5983 FILE *f = NULL;
5985 err = get_editor(&editor);
5986 if (err)
5987 return err;
5989 if (spawn_editor(editor, path) == -1) {
5990 err = got_error_from_errno("failed spawning editor");
5991 goto done;
5994 f = fopen(path, "r");
5995 if (f == NULL) {
5996 err = got_error_from_errno("fopen");
5997 goto done;
5999 err = histedit_parse_list(histedit_cmds, f, repo);
6000 if (err)
6001 goto done;
6003 err = histedit_check_script(histedit_cmds, commits, repo);
6004 done:
6005 if (f && fclose(f) != 0 && err == NULL)
6006 err = got_error_from_errno("fclose");
6007 free(editor);
6008 return err;
6011 static const struct got_error *
6012 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6013 struct got_object_id_queue *, const char *, const char *,
6014 struct got_repository *);
6016 static const struct got_error *
6017 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6018 struct got_object_id_queue *commits, const char *branch_name,
6019 struct got_repository *repo)
6021 const struct got_error *err;
6022 FILE *f = NULL;
6023 char *path = NULL;
6025 err = got_opentemp_named(&path, &f, "got-histedit");
6026 if (err)
6027 return err;
6029 err = write_cmd_list(f, branch_name, commits);
6030 if (err)
6031 goto done;
6033 err = histedit_write_commit_list(commits, f, repo);
6034 if (err)
6035 goto done;
6037 if (fclose(f) != 0) {
6038 err = got_error_from_errno("fclose");
6039 goto done;
6041 f = NULL;
6043 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6044 if (err) {
6045 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6046 err->code != GOT_ERR_HISTEDIT_CMD)
6047 goto done;
6048 err = histedit_edit_list_retry(histedit_cmds, err,
6049 commits, path, branch_name, repo);
6051 done:
6052 if (f && fclose(f) != 0 && err == NULL)
6053 err = got_error_from_errno("fclose");
6054 if (path && unlink(path) != 0 && err == NULL)
6055 err = got_error_from_errno2("unlink", path);
6056 free(path);
6057 return err;
6060 static const struct got_error *
6061 histedit_save_list(struct got_histedit_list *histedit_cmds,
6062 struct got_worktree *worktree, struct got_repository *repo)
6064 const struct got_error *err = NULL;
6065 char *path = NULL;
6066 FILE *f = NULL;
6067 struct got_histedit_list_entry *hle;
6068 struct got_commit_object *commit = NULL;
6070 err = got_worktree_get_histedit_script_path(&path, worktree);
6071 if (err)
6072 return err;
6074 f = fopen(path, "w");
6075 if (f == NULL) {
6076 err = got_error_from_errno2("fopen", path);
6077 goto done;
6079 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6080 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6081 repo);
6082 if (err)
6083 break;
6085 if (hle->logmsg) {
6086 int n = fprintf(f, "%c %s\n",
6087 GOT_HISTEDIT_MESG, hle->logmsg);
6088 if (n < 0) {
6089 err = got_ferror(f, GOT_ERR_IO);
6090 break;
6094 done:
6095 if (f && fclose(f) != 0 && err == NULL)
6096 err = got_error_from_errno("fclose");
6097 free(path);
6098 if (commit)
6099 got_object_commit_close(commit);
6100 return err;
6103 void
6104 histedit_free_list(struct got_histedit_list *histedit_cmds)
6106 struct got_histedit_list_entry *hle;
6108 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6109 TAILQ_REMOVE(histedit_cmds, hle, entry);
6110 free(hle);
6114 static const struct got_error *
6115 histedit_load_list(struct got_histedit_list *histedit_cmds,
6116 const char *path, struct got_repository *repo)
6118 const struct got_error *err = NULL;
6119 FILE *f = NULL;
6121 f = fopen(path, "r");
6122 if (f == NULL) {
6123 err = got_error_from_errno2("fopen", path);
6124 goto done;
6127 err = histedit_parse_list(histedit_cmds, f, repo);
6128 done:
6129 if (f && fclose(f) != 0 && err == NULL)
6130 err = got_error_from_errno("fclose");
6131 return err;
6134 static const struct got_error *
6135 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6136 const struct got_error *edit_err, struct got_object_id_queue *commits,
6137 const char *path, const char *branch_name, struct got_repository *repo)
6139 const struct got_error *err = NULL, *prev_err = edit_err;
6140 int resp = ' ';
6142 while (resp != 'c' && resp != 'r' && resp != 'a') {
6143 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6144 "or (a)bort: ", getprogname(), prev_err->msg);
6145 resp = getchar();
6146 if (resp == '\n')
6147 resp = getchar();
6148 if (resp == 'c') {
6149 histedit_free_list(histedit_cmds);
6150 err = histedit_run_editor(histedit_cmds, path, commits,
6151 repo);
6152 if (err) {
6153 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6154 err->code != GOT_ERR_HISTEDIT_CMD)
6155 break;
6156 prev_err = err;
6157 resp = ' ';
6158 continue;
6160 break;
6161 } else if (resp == 'r') {
6162 histedit_free_list(histedit_cmds);
6163 err = histedit_edit_script(histedit_cmds,
6164 commits, branch_name, repo);
6165 if (err) {
6166 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6167 err->code != GOT_ERR_HISTEDIT_CMD)
6168 break;
6169 prev_err = err;
6170 resp = ' ';
6171 continue;
6173 break;
6174 } else if (resp == 'a') {
6175 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6176 break;
6177 } else
6178 printf("invalid response '%c'\n", resp);
6181 return err;
6184 static const struct got_error *
6185 histedit_complete(struct got_worktree *worktree,
6186 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6187 struct got_reference *branch, struct got_repository *repo)
6189 printf("Switching work tree to %s\n",
6190 got_ref_get_symref_target(branch));
6191 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6192 branch, repo);
6195 static const struct got_error *
6196 show_histedit_progress(struct got_commit_object *commit,
6197 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6199 const struct got_error *err;
6200 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6202 err = got_object_id_str(&old_id_str, hle->commit_id);
6203 if (err)
6204 goto done;
6206 if (new_id) {
6207 err = got_object_id_str(&new_id_str, new_id);
6208 if (err)
6209 goto done;
6212 old_id_str[12] = '\0';
6213 if (new_id_str)
6214 new_id_str[12] = '\0';
6216 if (hle->logmsg) {
6217 logmsg = strdup(hle->logmsg);
6218 if (logmsg == NULL) {
6219 err = got_error_from_errno("strdup");
6220 goto done;
6222 trim_logmsg(logmsg, 42);
6223 } else {
6224 err = get_short_logmsg(&logmsg, 42, commit);
6225 if (err)
6226 goto done;
6229 switch (hle->cmd->code) {
6230 case GOT_HISTEDIT_PICK:
6231 case GOT_HISTEDIT_EDIT:
6232 printf("%s -> %s: %s\n", old_id_str,
6233 new_id_str ? new_id_str : "no-op change", logmsg);
6234 break;
6235 case GOT_HISTEDIT_DROP:
6236 case GOT_HISTEDIT_FOLD:
6237 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6238 logmsg);
6239 break;
6240 default:
6241 break;
6243 done:
6244 free(old_id_str);
6245 free(new_id_str);
6246 return err;
6249 static const struct got_error *
6250 histedit_commit(struct got_pathlist_head *merged_paths,
6251 struct got_worktree *worktree, struct got_fileindex *fileindex,
6252 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6253 struct got_repository *repo)
6255 const struct got_error *err;
6256 struct got_commit_object *commit;
6257 struct got_object_id *new_commit_id;
6259 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6260 && hle->logmsg == NULL) {
6261 err = histedit_edit_logmsg(hle, repo);
6262 if (err)
6263 return err;
6266 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6267 if (err)
6268 return err;
6270 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6271 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6272 hle->logmsg, repo);
6273 if (err) {
6274 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6275 goto done;
6276 err = show_histedit_progress(commit, hle, NULL);
6277 } else {
6278 err = show_histedit_progress(commit, hle, new_commit_id);
6279 free(new_commit_id);
6281 done:
6282 got_object_commit_close(commit);
6283 return err;
6286 static const struct got_error *
6287 histedit_skip_commit(struct got_histedit_list_entry *hle,
6288 struct got_worktree *worktree, struct got_repository *repo)
6290 const struct got_error *error;
6291 struct got_commit_object *commit;
6293 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6294 repo);
6295 if (error)
6296 return error;
6298 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6299 if (error)
6300 return error;
6302 error = show_histedit_progress(commit, hle, NULL);
6303 got_object_commit_close(commit);
6304 return error;
6307 static const struct got_error *
6308 check_local_changes(void *arg, unsigned char status,
6309 unsigned char staged_status, const char *path,
6310 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6311 struct got_object_id *commit_id, int dirfd, const char *de_name)
6313 int *have_local_changes = arg;
6315 switch (status) {
6316 case GOT_STATUS_ADD:
6317 case GOT_STATUS_DELETE:
6318 case GOT_STATUS_MODIFY:
6319 case GOT_STATUS_CONFLICT:
6320 *have_local_changes = 1;
6321 return got_error(GOT_ERR_CANCELLED);
6322 default:
6323 break;
6326 switch (staged_status) {
6327 case GOT_STATUS_ADD:
6328 case GOT_STATUS_DELETE:
6329 case GOT_STATUS_MODIFY:
6330 *have_local_changes = 1;
6331 return got_error(GOT_ERR_CANCELLED);
6332 default:
6333 break;
6336 return NULL;
6339 static const struct got_error *
6340 cmd_histedit(int argc, char *argv[])
6342 const struct got_error *error = NULL;
6343 struct got_worktree *worktree = NULL;
6344 struct got_fileindex *fileindex = NULL;
6345 struct got_repository *repo = NULL;
6346 char *cwd = NULL;
6347 struct got_reference *branch = NULL;
6348 struct got_reference *tmp_branch = NULL;
6349 struct got_object_id *resume_commit_id = NULL;
6350 struct got_object_id *base_commit_id = NULL;
6351 struct got_object_id *head_commit_id = NULL;
6352 struct got_commit_object *commit = NULL;
6353 int ch, rebase_in_progress = 0, did_something;
6354 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6355 const char *edit_script_path = NULL;
6356 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6357 struct got_object_id_queue commits;
6358 struct got_pathlist_head merged_paths;
6359 const struct got_object_id_queue *parent_ids;
6360 struct got_object_qid *pid;
6361 struct got_histedit_list histedit_cmds;
6362 struct got_histedit_list_entry *hle;
6364 SIMPLEQ_INIT(&commits);
6365 TAILQ_INIT(&histedit_cmds);
6366 TAILQ_INIT(&merged_paths);
6368 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6369 switch (ch) {
6370 case 'a':
6371 abort_edit = 1;
6372 break;
6373 case 'c':
6374 continue_edit = 1;
6375 break;
6376 case 'F':
6377 edit_script_path = optarg;
6378 break;
6379 default:
6380 usage_histedit();
6381 /* NOTREACHED */
6385 argc -= optind;
6386 argv += optind;
6388 #ifndef PROFILE
6389 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6390 "unveil", NULL) == -1)
6391 err(1, "pledge");
6392 #endif
6393 if (abort_edit && continue_edit)
6394 usage_histedit();
6395 if (argc != 0)
6396 usage_histedit();
6399 * This command cannot apply unveil(2) in all cases because the
6400 * user may choose to run an editor to edit the histedit script
6401 * and to edit individual commit log messages.
6402 * unveil(2) traverses exec(2); if an editor is used we have to
6403 * apply unveil after edit script and log messages have been written.
6404 * XXX TODO: Make use of unveil(2) where possible.
6407 cwd = getcwd(NULL, 0);
6408 if (cwd == NULL) {
6409 error = got_error_from_errno("getcwd");
6410 goto done;
6412 error = got_worktree_open(&worktree, cwd);
6413 if (error)
6414 goto done;
6416 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6417 NULL);
6418 if (error != NULL)
6419 goto done;
6421 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6422 if (error)
6423 goto done;
6424 if (rebase_in_progress) {
6425 error = got_error(GOT_ERR_REBASING);
6426 goto done;
6429 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6430 if (error)
6431 goto done;
6433 if (edit_in_progress && abort_edit) {
6434 error = got_worktree_histedit_continue(&resume_commit_id,
6435 &tmp_branch, &branch, &base_commit_id, &fileindex,
6436 worktree, repo);
6437 if (error)
6438 goto done;
6439 printf("Switching work tree to %s\n",
6440 got_ref_get_symref_target(branch));
6441 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6442 branch, base_commit_id, update_progress, &did_something);
6443 if (error)
6444 goto done;
6445 printf("Histedit of %s aborted\n",
6446 got_ref_get_symref_target(branch));
6447 goto done; /* nothing else to do */
6448 } else if (abort_edit) {
6449 error = got_error(GOT_ERR_NOT_HISTEDIT);
6450 goto done;
6453 if (continue_edit) {
6454 char *path;
6456 if (!edit_in_progress) {
6457 error = got_error(GOT_ERR_NOT_HISTEDIT);
6458 goto done;
6461 error = got_worktree_get_histedit_script_path(&path, worktree);
6462 if (error)
6463 goto done;
6465 error = histedit_load_list(&histedit_cmds, path, repo);
6466 free(path);
6467 if (error)
6468 goto done;
6470 error = got_worktree_histedit_continue(&resume_commit_id,
6471 &tmp_branch, &branch, &base_commit_id, &fileindex,
6472 worktree, repo);
6473 if (error)
6474 goto done;
6476 error = got_ref_resolve(&head_commit_id, repo, branch);
6477 if (error)
6478 goto done;
6480 error = got_object_open_as_commit(&commit, repo,
6481 head_commit_id);
6482 if (error)
6483 goto done;
6484 parent_ids = got_object_commit_get_parent_ids(commit);
6485 pid = SIMPLEQ_FIRST(parent_ids);
6486 if (pid == NULL) {
6487 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6488 goto done;
6490 error = collect_commits(&commits, head_commit_id, pid->id,
6491 base_commit_id, got_worktree_get_path_prefix(worktree),
6492 GOT_ERR_HISTEDIT_PATH, repo);
6493 got_object_commit_close(commit);
6494 commit = NULL;
6495 if (error)
6496 goto done;
6497 } else {
6498 if (edit_in_progress) {
6499 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6500 goto done;
6503 error = got_ref_open(&branch, repo,
6504 got_worktree_get_head_ref_name(worktree), 0);
6505 if (error != NULL)
6506 goto done;
6508 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6509 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6510 "will not edit commit history of a branch outside "
6511 "the \"refs/heads/\" reference namespace");
6512 goto done;
6515 error = got_ref_resolve(&head_commit_id, repo, branch);
6516 got_ref_close(branch);
6517 branch = NULL;
6518 if (error)
6519 goto done;
6521 error = got_object_open_as_commit(&commit, repo,
6522 head_commit_id);
6523 if (error)
6524 goto done;
6525 parent_ids = got_object_commit_get_parent_ids(commit);
6526 pid = SIMPLEQ_FIRST(parent_ids);
6527 if (pid == NULL) {
6528 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6529 goto done;
6531 error = collect_commits(&commits, head_commit_id, pid->id,
6532 got_worktree_get_base_commit_id(worktree),
6533 got_worktree_get_path_prefix(worktree),
6534 GOT_ERR_HISTEDIT_PATH, repo);
6535 got_object_commit_close(commit);
6536 commit = NULL;
6537 if (error)
6538 goto done;
6540 if (SIMPLEQ_EMPTY(&commits)) {
6541 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6542 goto done;
6545 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6546 &base_commit_id, &fileindex, worktree, repo);
6547 if (error)
6548 goto done;
6550 if (edit_script_path) {
6551 error = histedit_load_list(&histedit_cmds,
6552 edit_script_path, repo);
6553 if (error) {
6554 got_worktree_histedit_abort(worktree, fileindex,
6555 repo, branch, base_commit_id,
6556 update_progress, &did_something);
6557 goto done;
6559 } else {
6560 const char *branch_name;
6561 branch_name = got_ref_get_symref_target(branch);
6562 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6563 branch_name += 11;
6564 error = histedit_edit_script(&histedit_cmds, &commits,
6565 branch_name, repo);
6566 if (error) {
6567 got_worktree_histedit_abort(worktree, fileindex,
6568 repo, branch, base_commit_id,
6569 update_progress, &did_something);
6570 goto done;
6575 error = histedit_save_list(&histedit_cmds, worktree,
6576 repo);
6577 if (error) {
6578 got_worktree_histedit_abort(worktree, fileindex,
6579 repo, branch, base_commit_id,
6580 update_progress, &did_something);
6581 goto done;
6586 error = histedit_check_script(&histedit_cmds, &commits, repo);
6587 if (error)
6588 goto done;
6590 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6591 if (resume_commit_id) {
6592 if (got_object_id_cmp(hle->commit_id,
6593 resume_commit_id) != 0)
6594 continue;
6596 resume_commit_id = NULL;
6597 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6598 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6599 error = histedit_skip_commit(hle, worktree,
6600 repo);
6601 if (error)
6602 goto done;
6603 } else {
6604 struct got_pathlist_head paths;
6605 int have_changes = 0;
6607 TAILQ_INIT(&paths);
6608 error = got_pathlist_append(&paths, "", NULL);
6609 if (error)
6610 goto done;
6611 error = got_worktree_status(worktree, &paths,
6612 repo, check_local_changes, &have_changes,
6613 check_cancelled, NULL);
6614 got_pathlist_free(&paths);
6615 if (error) {
6616 if (error->code != GOT_ERR_CANCELLED)
6617 goto done;
6618 if (sigint_received || sigpipe_received)
6619 goto done;
6621 if (have_changes) {
6622 error = histedit_commit(NULL, worktree,
6623 fileindex, tmp_branch, hle, repo);
6624 if (error)
6625 goto done;
6626 } else {
6627 error = got_object_open_as_commit(
6628 &commit, repo, hle->commit_id);
6629 if (error)
6630 goto done;
6631 error = show_histedit_progress(commit,
6632 hle, NULL);
6633 got_object_commit_close(commit);
6634 commit = NULL;
6635 if (error)
6636 goto done;
6639 continue;
6642 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6643 error = histedit_skip_commit(hle, worktree, repo);
6644 if (error)
6645 goto done;
6646 continue;
6649 error = got_object_open_as_commit(&commit, repo,
6650 hle->commit_id);
6651 if (error)
6652 goto done;
6653 parent_ids = got_object_commit_get_parent_ids(commit);
6654 pid = SIMPLEQ_FIRST(parent_ids);
6656 error = got_worktree_histedit_merge_files(&merged_paths,
6657 worktree, fileindex, pid->id, hle->commit_id, repo,
6658 rebase_progress, &rebase_status, check_cancelled, NULL);
6659 if (error)
6660 goto done;
6661 got_object_commit_close(commit);
6662 commit = NULL;
6664 if (rebase_status == GOT_STATUS_CONFLICT) {
6665 got_worktree_rebase_pathlist_free(&merged_paths);
6666 break;
6669 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6670 char *id_str;
6671 error = got_object_id_str(&id_str, hle->commit_id);
6672 if (error)
6673 goto done;
6674 printf("Stopping histedit for amending commit %s\n",
6675 id_str);
6676 free(id_str);
6677 got_worktree_rebase_pathlist_free(&merged_paths);
6678 error = got_worktree_histedit_postpone(worktree,
6679 fileindex);
6680 goto done;
6683 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6684 error = histedit_skip_commit(hle, worktree, repo);
6685 if (error)
6686 goto done;
6687 continue;
6690 error = histedit_commit(&merged_paths, worktree, fileindex,
6691 tmp_branch, hle, repo);
6692 got_worktree_rebase_pathlist_free(&merged_paths);
6693 if (error)
6694 goto done;
6697 if (rebase_status == GOT_STATUS_CONFLICT) {
6698 error = got_worktree_histedit_postpone(worktree, fileindex);
6699 if (error)
6700 goto done;
6701 error = got_error_msg(GOT_ERR_CONFLICTS,
6702 "conflicts must be resolved before rebasing can continue");
6703 } else
6704 error = histedit_complete(worktree, fileindex, tmp_branch,
6705 branch, repo);
6706 done:
6707 got_object_id_queue_free(&commits);
6708 histedit_free_list(&histedit_cmds);
6709 free(head_commit_id);
6710 free(base_commit_id);
6711 free(resume_commit_id);
6712 if (commit)
6713 got_object_commit_close(commit);
6714 if (branch)
6715 got_ref_close(branch);
6716 if (tmp_branch)
6717 got_ref_close(tmp_branch);
6718 if (worktree)
6719 got_worktree_close(worktree);
6720 if (repo)
6721 got_repo_close(repo);
6722 return error;
6725 __dead static void
6726 usage_integrate(void)
6728 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6729 exit(1);
6732 static const struct got_error *
6733 cmd_integrate(int argc, char *argv[])
6735 const struct got_error *error = NULL;
6736 struct got_repository *repo = NULL;
6737 struct got_worktree *worktree = NULL;
6738 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6739 const char *branch_arg = NULL;
6740 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6741 struct got_fileindex *fileindex = NULL;
6742 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6743 int ch, did_something = 0;
6745 while ((ch = getopt(argc, argv, "")) != -1) {
6746 switch (ch) {
6747 default:
6748 usage_integrate();
6749 /* NOTREACHED */
6753 argc -= optind;
6754 argv += optind;
6756 if (argc != 1)
6757 usage_integrate();
6758 branch_arg = argv[0];
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6761 "unveil", NULL) == -1)
6762 err(1, "pledge");
6764 cwd = getcwd(NULL, 0);
6765 if (cwd == NULL) {
6766 error = got_error_from_errno("getcwd");
6767 goto done;
6770 error = got_worktree_open(&worktree, cwd);
6771 if (error)
6772 goto done;
6774 error = check_rebase_or_histedit_in_progress(worktree);
6775 if (error)
6776 goto done;
6778 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6779 NULL);
6780 if (error != NULL)
6781 goto done;
6783 error = apply_unveil(got_repo_get_path(repo), 0,
6784 got_worktree_get_root_path(worktree));
6785 if (error)
6786 goto done;
6788 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6789 error = got_error_from_errno("asprintf");
6790 goto done;
6793 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6794 &base_branch_ref, worktree, refname, repo);
6795 if (error)
6796 goto done;
6798 refname = strdup(got_ref_get_name(branch_ref));
6799 if (refname == NULL) {
6800 error = got_error_from_errno("strdup");
6801 got_worktree_integrate_abort(worktree, fileindex, repo,
6802 branch_ref, base_branch_ref);
6803 goto done;
6805 base_refname = strdup(got_ref_get_name(base_branch_ref));
6806 if (base_refname == NULL) {
6807 error = got_error_from_errno("strdup");
6808 got_worktree_integrate_abort(worktree, fileindex, repo,
6809 branch_ref, base_branch_ref);
6810 goto done;
6813 error = got_ref_resolve(&commit_id, repo, branch_ref);
6814 if (error)
6815 goto done;
6817 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6818 if (error)
6819 goto done;
6821 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6822 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6823 "specified branch has already been integrated");
6824 got_worktree_integrate_abort(worktree, fileindex, repo,
6825 branch_ref, base_branch_ref);
6826 goto done;
6829 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6830 if (error) {
6831 if (error->code == GOT_ERR_ANCESTRY)
6832 error = got_error(GOT_ERR_REBASE_REQUIRED);
6833 got_worktree_integrate_abort(worktree, fileindex, repo,
6834 branch_ref, base_branch_ref);
6835 goto done;
6838 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6839 branch_ref, base_branch_ref, update_progress, &did_something,
6840 check_cancelled, NULL);
6841 if (error)
6842 goto done;
6844 printf("Integrated %s into %s\n", refname, base_refname);
6845 done:
6846 if (repo)
6847 got_repo_close(repo);
6848 if (worktree)
6849 got_worktree_close(worktree);
6850 free(cwd);
6851 free(base_commit_id);
6852 free(commit_id);
6853 free(refname);
6854 free(base_refname);
6855 return error;
6858 __dead static void
6859 usage_stage(void)
6861 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6862 "[file-path ...]\n",
6863 getprogname());
6864 exit(1);
6867 static const struct got_error *
6868 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6869 const char *path, struct got_object_id *blob_id,
6870 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6871 int dirfd, const char *de_name)
6873 const struct got_error *err = NULL;
6874 char *id_str = NULL;
6876 if (staged_status != GOT_STATUS_ADD &&
6877 staged_status != GOT_STATUS_MODIFY &&
6878 staged_status != GOT_STATUS_DELETE)
6879 return NULL;
6881 if (staged_status == GOT_STATUS_ADD ||
6882 staged_status == GOT_STATUS_MODIFY)
6883 err = got_object_id_str(&id_str, staged_blob_id);
6884 else
6885 err = got_object_id_str(&id_str, blob_id);
6886 if (err)
6887 return err;
6889 printf("%s %c %s\n", id_str, staged_status, path);
6890 free(id_str);
6891 return NULL;
6894 static const struct got_error *
6895 cmd_stage(int argc, char *argv[])
6897 const struct got_error *error = NULL;
6898 struct got_repository *repo = NULL;
6899 struct got_worktree *worktree = NULL;
6900 char *cwd = NULL;
6901 struct got_pathlist_head paths;
6902 struct got_pathlist_entry *pe;
6903 int ch, list_stage = 0, pflag = 0;
6904 FILE *patch_script_file = NULL;
6905 const char *patch_script_path = NULL;
6906 struct choose_patch_arg cpa;
6908 TAILQ_INIT(&paths);
6910 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6911 switch (ch) {
6912 case 'l':
6913 list_stage = 1;
6914 break;
6915 case 'p':
6916 pflag = 1;
6917 break;
6918 case 'F':
6919 patch_script_path = optarg;
6920 break;
6921 default:
6922 usage_stage();
6923 /* NOTREACHED */
6927 argc -= optind;
6928 argv += optind;
6930 #ifndef PROFILE
6931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6932 "unveil", NULL) == -1)
6933 err(1, "pledge");
6934 #endif
6935 if (list_stage && (pflag || patch_script_path))
6936 errx(1, "-l option cannot be used with other options");
6937 if (patch_script_path && !pflag)
6938 errx(1, "-F option can only be used together with -p option");
6940 cwd = getcwd(NULL, 0);
6941 if (cwd == NULL) {
6942 error = got_error_from_errno("getcwd");
6943 goto done;
6946 error = got_worktree_open(&worktree, cwd);
6947 if (error)
6948 goto done;
6950 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6951 NULL);
6952 if (error != NULL)
6953 goto done;
6955 if (patch_script_path) {
6956 patch_script_file = fopen(patch_script_path, "r");
6957 if (patch_script_file == NULL) {
6958 error = got_error_from_errno2("fopen",
6959 patch_script_path);
6960 goto done;
6963 error = apply_unveil(got_repo_get_path(repo), 0,
6964 got_worktree_get_root_path(worktree));
6965 if (error)
6966 goto done;
6968 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6969 if (error)
6970 goto done;
6972 if (list_stage)
6973 error = got_worktree_status(worktree, &paths, repo,
6974 print_stage, NULL, check_cancelled, NULL);
6975 else {
6976 cpa.patch_script_file = patch_script_file;
6977 cpa.action = "stage";
6978 error = got_worktree_stage(worktree, &paths,
6979 pflag ? NULL : print_status, NULL,
6980 pflag ? choose_patch : NULL, &cpa, repo);
6982 done:
6983 if (patch_script_file && fclose(patch_script_file) == EOF &&
6984 error == NULL)
6985 error = got_error_from_errno2("fclose", patch_script_path);
6986 if (repo)
6987 got_repo_close(repo);
6988 if (worktree)
6989 got_worktree_close(worktree);
6990 TAILQ_FOREACH(pe, &paths, entry)
6991 free((char *)pe->path);
6992 got_pathlist_free(&paths);
6993 free(cwd);
6994 return error;
6997 __dead static void
6998 usage_unstage(void)
7000 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7001 "[file-path ...]\n",
7002 getprogname());
7003 exit(1);
7007 static const struct got_error *
7008 cmd_unstage(int argc, char *argv[])
7010 const struct got_error *error = NULL;
7011 struct got_repository *repo = NULL;
7012 struct got_worktree *worktree = NULL;
7013 char *cwd = NULL;
7014 struct got_pathlist_head paths;
7015 struct got_pathlist_entry *pe;
7016 int ch, did_something = 0, pflag = 0;
7017 FILE *patch_script_file = NULL;
7018 const char *patch_script_path = NULL;
7019 struct choose_patch_arg cpa;
7021 TAILQ_INIT(&paths);
7023 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7024 switch (ch) {
7025 case 'p':
7026 pflag = 1;
7027 break;
7028 case 'F':
7029 patch_script_path = optarg;
7030 break;
7031 default:
7032 usage_unstage();
7033 /* NOTREACHED */
7037 argc -= optind;
7038 argv += optind;
7040 #ifndef PROFILE
7041 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7042 "unveil", NULL) == -1)
7043 err(1, "pledge");
7044 #endif
7045 if (patch_script_path && !pflag)
7046 errx(1, "-F option can only be used together with -p option");
7048 cwd = getcwd(NULL, 0);
7049 if (cwd == NULL) {
7050 error = got_error_from_errno("getcwd");
7051 goto done;
7054 error = got_worktree_open(&worktree, cwd);
7055 if (error)
7056 goto done;
7058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7059 NULL);
7060 if (error != NULL)
7061 goto done;
7063 if (patch_script_path) {
7064 patch_script_file = fopen(patch_script_path, "r");
7065 if (patch_script_file == NULL) {
7066 error = got_error_from_errno2("fopen",
7067 patch_script_path);
7068 goto done;
7072 error = apply_unveil(got_repo_get_path(repo), 0,
7073 got_worktree_get_root_path(worktree));
7074 if (error)
7075 goto done;
7077 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7078 if (error)
7079 goto done;
7081 cpa.patch_script_file = patch_script_file;
7082 cpa.action = "unstage";
7083 error = got_worktree_unstage(worktree, &paths, update_progress,
7084 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7085 done:
7086 if (patch_script_file && fclose(patch_script_file) == EOF &&
7087 error == NULL)
7088 error = got_error_from_errno2("fclose", patch_script_path);
7089 if (repo)
7090 got_repo_close(repo);
7091 if (worktree)
7092 got_worktree_close(worktree);
7093 TAILQ_FOREACH(pe, &paths, entry)
7094 free((char *)pe->path);
7095 got_pathlist_free(&paths);
7096 free(cwd);
7097 return error;
7100 __dead static void
7101 usage_cat(void)
7103 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7104 "arg1 [arg2 ...]\n", getprogname());
7105 exit(1);
7108 static const struct got_error *
7109 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7111 const struct got_error *err;
7112 struct got_blob_object *blob;
7114 err = got_object_open_as_blob(&blob, repo, id, 8192);
7115 if (err)
7116 return err;
7118 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7119 got_object_blob_close(blob);
7120 return err;
7123 static const struct got_error *
7124 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7126 const struct got_error *err;
7127 struct got_tree_object *tree;
7128 int nentries, i;
7130 err = got_object_open_as_tree(&tree, repo, id);
7131 if (err)
7132 return err;
7134 nentries = got_object_tree_get_nentries(tree);
7135 for (i = 0; i < nentries; i++) {
7136 struct got_tree_entry *te;
7137 char *id_str;
7138 if (sigint_received || sigpipe_received)
7139 break;
7140 te = got_object_tree_get_entry(tree, i);
7141 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7142 if (err)
7143 break;
7144 fprintf(outfile, "%s %.7o %s\n", id_str,
7145 got_tree_entry_get_mode(te),
7146 got_tree_entry_get_name(te));
7147 free(id_str);
7150 got_object_tree_close(tree);
7151 return err;
7154 static const struct got_error *
7155 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7157 const struct got_error *err;
7158 struct got_commit_object *commit;
7159 const struct got_object_id_queue *parent_ids;
7160 struct got_object_qid *pid;
7161 char *id_str = NULL;
7162 const char *logmsg = NULL;
7164 err = got_object_open_as_commit(&commit, repo, id);
7165 if (err)
7166 return err;
7168 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7169 if (err)
7170 goto done;
7172 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7173 parent_ids = got_object_commit_get_parent_ids(commit);
7174 fprintf(outfile, "numparents %d\n",
7175 got_object_commit_get_nparents(commit));
7176 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7177 char *pid_str;
7178 err = got_object_id_str(&pid_str, pid->id);
7179 if (err)
7180 goto done;
7181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7182 free(pid_str);
7184 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7185 got_object_commit_get_author(commit),
7186 got_object_commit_get_author_time(commit));
7188 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7189 got_object_commit_get_author(commit),
7190 got_object_commit_get_committer_time(commit));
7192 logmsg = got_object_commit_get_logmsg_raw(commit);
7193 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7194 fprintf(outfile, "%s", logmsg);
7195 done:
7196 free(id_str);
7197 got_object_commit_close(commit);
7198 return err;
7201 static const struct got_error *
7202 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7204 const struct got_error *err;
7205 struct got_tag_object *tag;
7206 char *id_str = NULL;
7207 const char *tagmsg = NULL;
7209 err = got_object_open_as_tag(&tag, repo, id);
7210 if (err)
7211 return err;
7213 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7214 if (err)
7215 goto done;
7217 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7219 switch (got_object_tag_get_object_type(tag)) {
7220 case GOT_OBJ_TYPE_BLOB:
7221 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7222 GOT_OBJ_LABEL_BLOB);
7223 break;
7224 case GOT_OBJ_TYPE_TREE:
7225 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7226 GOT_OBJ_LABEL_TREE);
7227 break;
7228 case GOT_OBJ_TYPE_COMMIT:
7229 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7230 GOT_OBJ_LABEL_COMMIT);
7231 break;
7232 case GOT_OBJ_TYPE_TAG:
7233 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7234 GOT_OBJ_LABEL_TAG);
7235 break;
7236 default:
7237 break;
7240 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7241 got_object_tag_get_name(tag));
7243 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7244 got_object_tag_get_tagger(tag),
7245 got_object_tag_get_tagger_time(tag));
7247 tagmsg = got_object_tag_get_message(tag);
7248 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7249 fprintf(outfile, "%s", tagmsg);
7250 done:
7251 free(id_str);
7252 got_object_tag_close(tag);
7253 return err;
7256 static const struct got_error *
7257 cmd_cat(int argc, char *argv[])
7259 const struct got_error *error;
7260 struct got_repository *repo = NULL;
7261 struct got_worktree *worktree = NULL;
7262 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7263 const char *commit_id_str = NULL;
7264 struct got_object_id *id = NULL, *commit_id = NULL;
7265 int ch, obj_type, i, force_path = 0;
7267 #ifndef PROFILE
7268 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7269 NULL) == -1)
7270 err(1, "pledge");
7271 #endif
7273 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7274 switch (ch) {
7275 case 'c':
7276 commit_id_str = optarg;
7277 break;
7278 case 'r':
7279 repo_path = realpath(optarg, NULL);
7280 if (repo_path == NULL)
7281 return got_error_from_errno2("realpath",
7282 optarg);
7283 got_path_strip_trailing_slashes(repo_path);
7284 break;
7285 case 'P':
7286 force_path = 1;
7287 break;
7288 default:
7289 usage_cat();
7290 /* NOTREACHED */
7294 argc -= optind;
7295 argv += optind;
7297 cwd = getcwd(NULL, 0);
7298 if (cwd == NULL) {
7299 error = got_error_from_errno("getcwd");
7300 goto done;
7302 error = got_worktree_open(&worktree, cwd);
7303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7304 goto done;
7305 if (worktree) {
7306 if (repo_path == NULL) {
7307 repo_path = strdup(
7308 got_worktree_get_repo_path(worktree));
7309 if (repo_path == NULL) {
7310 error = got_error_from_errno("strdup");
7311 goto done;
7316 if (repo_path == NULL) {
7317 repo_path = getcwd(NULL, 0);
7318 if (repo_path == NULL)
7319 return got_error_from_errno("getcwd");
7322 error = got_repo_open(&repo, repo_path, NULL);
7323 free(repo_path);
7324 if (error != NULL)
7325 goto done;
7327 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7328 if (error)
7329 goto done;
7331 if (commit_id_str == NULL)
7332 commit_id_str = GOT_REF_HEAD;
7333 error = got_repo_match_object_id(&commit_id, NULL,
7334 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7335 if (error)
7336 goto done;
7338 for (i = 0; i < argc; i++) {
7339 if (force_path) {
7340 error = got_object_id_by_path(&id, repo, commit_id,
7341 argv[i]);
7342 if (error)
7343 break;
7344 } else {
7345 error = got_repo_match_object_id(&id, &label, argv[i],
7346 GOT_OBJ_TYPE_ANY, 0, repo);
7347 if (error) {
7348 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7349 error->code != GOT_ERR_NOT_REF)
7350 break;
7351 error = got_object_id_by_path(&id, repo,
7352 commit_id, argv[i]);
7353 if (error)
7354 break;
7358 error = got_object_get_type(&obj_type, repo, id);
7359 if (error)
7360 break;
7362 switch (obj_type) {
7363 case GOT_OBJ_TYPE_BLOB:
7364 error = cat_blob(id, repo, stdout);
7365 break;
7366 case GOT_OBJ_TYPE_TREE:
7367 error = cat_tree(id, repo, stdout);
7368 break;
7369 case GOT_OBJ_TYPE_COMMIT:
7370 error = cat_commit(id, repo, stdout);
7371 break;
7372 case GOT_OBJ_TYPE_TAG:
7373 error = cat_tag(id, repo, stdout);
7374 break;
7375 default:
7376 error = got_error(GOT_ERR_OBJ_TYPE);
7377 break;
7379 if (error)
7380 break;
7381 free(label);
7382 label = NULL;
7383 free(id);
7384 id = NULL;
7386 done:
7387 free(label);
7388 free(id);
7389 free(commit_id);
7390 if (worktree)
7391 got_worktree_close(worktree);
7392 if (repo) {
7393 const struct got_error *repo_error;
7394 repo_error = got_repo_close(repo);
7395 if (error == NULL)
7396 error = repo_error;
7398 return error;