Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/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 <limits.h>
27 #include <locale.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <libgen.h>
35 #include <time.h>
36 #include <paths.h>
37 #include <regex.h>
39 #include "got_version.h"
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_path.h"
45 #include "got_cancel.h"
46 #include "got_worktree.h"
47 #include "got_diff.h"
48 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_opentemp.h"
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 static volatile sig_atomic_t sigint_received;
58 static volatile sig_atomic_t sigpipe_received;
60 static void
61 catch_sigint(int signo)
62 {
63 sigint_received = 1;
64 }
66 static void
67 catch_sigpipe(int signo)
68 {
69 sigpipe_received = 1;
70 }
73 struct got_cmd {
74 const char *cmd_name;
75 const struct got_error *(*cmd_main)(int, char *[]);
76 void (*cmd_usage)(void);
77 const char *cmd_alias;
78 };
80 __dead static void usage(int);
81 __dead static void usage_init(void);
82 __dead static void usage_import(void);
83 __dead static void usage_checkout(void);
84 __dead static void usage_update(void);
85 __dead static void usage_log(void);
86 __dead static void usage_diff(void);
87 __dead static void usage_blame(void);
88 __dead static void usage_tree(void);
89 __dead static void usage_status(void);
90 __dead static void usage_ref(void);
91 __dead static void usage_branch(void);
92 __dead static void usage_tag(void);
93 __dead static void usage_add(void);
94 __dead static void usage_remove(void);
95 __dead static void usage_revert(void);
96 __dead static void usage_commit(void);
97 __dead static void usage_cherrypick(void);
98 __dead static void usage_backout(void);
99 __dead static void usage_rebase(void);
100 __dead static void usage_histedit(void);
101 __dead static void usage_integrate(void);
102 __dead static void usage_stage(void);
103 __dead static void usage_unstage(void);
104 __dead static void usage_cat(void);
106 static const struct got_error* cmd_init(int, char *[]);
107 static const struct got_error* cmd_import(int, char *[]);
108 static const struct got_error* cmd_checkout(int, char *[]);
109 static const struct got_error* cmd_update(int, char *[]);
110 static const struct got_error* cmd_log(int, char *[]);
111 static const struct got_error* cmd_diff(int, char *[]);
112 static const struct got_error* cmd_blame(int, char *[]);
113 static const struct got_error* cmd_tree(int, char *[]);
114 static const struct got_error* cmd_status(int, char *[]);
115 static const struct got_error* cmd_ref(int, char *[]);
116 static const struct got_error* cmd_branch(int, char *[]);
117 static const struct got_error* cmd_tag(int, char *[]);
118 static const struct got_error* cmd_add(int, char *[]);
119 static const struct got_error* cmd_remove(int, char *[]);
120 static const struct got_error* cmd_revert(int, char *[]);
121 static const struct got_error* cmd_commit(int, char *[]);
122 static const struct got_error* cmd_cherrypick(int, char *[]);
123 static const struct got_error* cmd_backout(int, char *[]);
124 static const struct got_error* cmd_rebase(int, char *[]);
125 static const struct got_error* cmd_histedit(int, char *[]);
126 static const struct got_error* cmd_integrate(int, char *[]);
127 static const struct got_error* cmd_stage(int, char *[]);
128 static const struct got_error* cmd_unstage(int, char *[]);
129 static const struct got_error* cmd_cat(int, char *[]);
131 static struct got_cmd got_commands[] = {
132 { "init", cmd_init, usage_init, "in" },
133 { "import", cmd_import, usage_import, "im" },
134 { "checkout", cmd_checkout, usage_checkout, "co" },
135 { "update", cmd_update, usage_update, "up" },
136 { "log", cmd_log, usage_log, "" },
137 { "diff", cmd_diff, usage_diff, "di" },
138 { "blame", cmd_blame, usage_blame, "bl" },
139 { "tree", cmd_tree, usage_tree, "tr" },
140 { "status", cmd_status, usage_status, "st" },
141 { "ref", cmd_ref, usage_ref, "" },
142 { "branch", cmd_branch, usage_branch, "br" },
143 { "tag", cmd_tag, usage_tag, "" },
144 { "add", cmd_add, usage_add, "" },
145 { "remove", cmd_remove, usage_remove, "rm" },
146 { "revert", cmd_revert, usage_revert, "rv" },
147 { "commit", cmd_commit, usage_commit, "ci" },
148 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
149 { "backout", cmd_backout, usage_backout, "bo" },
150 { "rebase", cmd_rebase, usage_rebase, "rb" },
151 { "histedit", cmd_histedit, usage_histedit, "he" },
152 { "integrate", cmd_integrate, usage_integrate,"ig" },
153 { "stage", cmd_stage, usage_stage, "sg" },
154 { "unstage", cmd_unstage, usage_unstage, "ug" },
155 { "cat", cmd_cat, usage_cat, "" },
156 };
158 static void
159 list_commands(void)
161 int i;
163 fprintf(stderr, "commands:");
164 for (i = 0; i < nitems(got_commands); i++) {
165 struct got_cmd *cmd = &got_commands[i];
166 fprintf(stderr, " %s", cmd->cmd_name);
168 fputc('\n', stderr);
171 int
172 main(int argc, char *argv[])
174 struct got_cmd *cmd;
175 unsigned int i;
176 int ch;
177 int hflag = 0, Vflag = 0;
179 setlocale(LC_CTYPE, "");
181 while ((ch = getopt(argc, argv, "hV")) != -1) {
182 switch (ch) {
183 case 'h':
184 hflag = 1;
185 break;
186 case 'V':
187 Vflag = 1;
188 break;
189 default:
190 usage(hflag);
191 /* NOTREACHED */
195 argc -= optind;
196 argv += optind;
197 optind = 0;
199 if (Vflag) {
200 got_version_print_str();
201 return 1;
204 if (argc <= 0)
205 usage(hflag);
207 signal(SIGINT, catch_sigint);
208 signal(SIGPIPE, catch_sigpipe);
210 for (i = 0; i < nitems(got_commands); i++) {
211 const struct got_error *error;
213 cmd = &got_commands[i];
215 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
216 strcmp(cmd->cmd_alias, argv[0]) != 0)
217 continue;
219 if (hflag)
220 got_commands[i].cmd_usage();
222 error = got_commands[i].cmd_main(argc, argv);
223 if (error && error->code != GOT_ERR_CANCELLED &&
224 error->code != GOT_ERR_PRIVSEP_EXIT &&
225 !(sigpipe_received &&
226 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
227 !(sigint_received &&
228 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
229 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 return 1;
233 return 0;
236 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
237 list_commands();
238 return 1;
241 __dead static void
242 usage(int hflag)
244 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 getprogname());
246 if (hflag)
247 list_commands();
248 exit(1);
251 static const struct got_error *
252 get_editor(char **abspath)
254 const struct got_error *err = NULL;
255 const char *editor;
257 *abspath = NULL;
259 editor = getenv("VISUAL");
260 if (editor == NULL)
261 editor = getenv("EDITOR");
263 if (editor) {
264 err = got_path_find_prog(abspath, editor);
265 if (err)
266 return err;
269 if (*abspath == NULL) {
270 *abspath = strdup("/bin/ed");
271 if (*abspath == NULL)
272 return got_error_from_errno("strdup");
275 return NULL;
278 static const struct got_error *
279 apply_unveil(const char *repo_path, int repo_read_only,
280 const char *worktree_path)
282 const struct got_error *err;
284 #ifdef PROFILE
285 if (unveil("gmon.out", "rwc") != 0)
286 return got_error_from_errno2("unveil", "gmon.out");
287 #endif
288 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
289 return got_error_from_errno2("unveil", repo_path);
291 if (worktree_path && unveil(worktree_path, "rwc") != 0)
292 return got_error_from_errno2("unveil", worktree_path);
294 if (unveil("/tmp", "rwc") != 0)
295 return got_error_from_errno2("unveil", "/tmp");
297 err = got_privsep_unveil_exec_helpers();
298 if (err != NULL)
299 return err;
301 if (unveil(NULL, NULL) != 0)
302 return got_error_from_errno("unveil");
304 return NULL;
307 __dead static void
308 usage_init(void)
310 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
311 exit(1);
314 static const struct got_error *
315 cmd_init(int argc, char *argv[])
317 const struct got_error *error = NULL;
318 char *repo_path = NULL;
319 int ch;
321 while ((ch = getopt(argc, argv, "")) != -1) {
322 switch (ch) {
323 default:
324 usage_init();
325 /* NOTREACHED */
329 argc -= optind;
330 argv += optind;
332 #ifndef PROFILE
333 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
334 err(1, "pledge");
335 #endif
336 if (argc != 1)
337 usage_init();
339 repo_path = strdup(argv[0]);
340 if (repo_path == NULL)
341 return got_error_from_errno("strdup");
343 got_path_strip_trailing_slashes(repo_path);
345 error = got_path_mkdir(repo_path);
346 if (error &&
347 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
348 goto done;
350 error = apply_unveil(repo_path, 0, NULL);
351 if (error)
352 goto done;
354 error = got_repo_init(repo_path);
355 if (error != NULL)
356 goto done;
358 done:
359 free(repo_path);
360 return error;
363 __dead static void
364 usage_import(void)
366 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
367 "[-r repository-path] [-I pattern] path\n", getprogname());
368 exit(1);
371 int
372 spawn_editor(const char *editor, const char *file)
374 pid_t pid;
375 sig_t sighup, sigint, sigquit;
376 int st = -1;
378 sighup = signal(SIGHUP, SIG_IGN);
379 sigint = signal(SIGINT, SIG_IGN);
380 sigquit = signal(SIGQUIT, SIG_IGN);
382 switch (pid = fork()) {
383 case -1:
384 goto doneediting;
385 case 0:
386 execl(editor, editor, file, (char *)NULL);
387 _exit(127);
390 while (waitpid(pid, &st, 0) == -1)
391 if (errno != EINTR)
392 break;
394 doneediting:
395 (void)signal(SIGHUP, sighup);
396 (void)signal(SIGINT, sigint);
397 (void)signal(SIGQUIT, sigquit);
399 if (!WIFEXITED(st)) {
400 errno = EINTR;
401 return -1;
404 return WEXITSTATUS(st);
407 static const struct got_error *
408 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
409 const char *initial_content)
411 const struct got_error *err = NULL;
412 char buf[1024];
413 struct stat st, st2;
414 FILE *fp;
415 int content_changed = 0;
416 size_t len;
418 *logmsg = NULL;
420 if (stat(logmsg_path, &st) == -1)
421 return got_error_from_errno2("stat", logmsg_path);
423 if (spawn_editor(editor, logmsg_path) == -1)
424 return got_error_from_errno("failed spawning editor");
426 if (stat(logmsg_path, &st2) == -1)
427 return got_error_from_errno("stat");
429 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
430 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
431 "no changes made to commit message, aborting");
433 *logmsg = malloc(st2.st_size + 1);
434 if (*logmsg == NULL)
435 return got_error_from_errno("malloc");
436 (*logmsg)[0] = '\0';
437 len = 0;
439 fp = fopen(logmsg_path, "r");
440 if (fp == NULL) {
441 err = got_error_from_errno("fopen");
442 goto done;
444 while (fgets(buf, sizeof(buf), fp) != NULL) {
445 if (!content_changed && strcmp(buf, initial_content) != 0)
446 content_changed = 1;
447 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
448 continue; /* remove comments and leading empty lines */
449 len = strlcat(*logmsg, buf, st2.st_size);
451 fclose(fp);
453 while (len > 0 && (*logmsg)[len - 1] == '\n') {
454 (*logmsg)[len - 1] = '\0';
455 len--;
458 if (len == 0 || !content_changed)
459 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
460 "commit message cannot be empty, aborting");
461 done:
462 if (err) {
463 free(*logmsg);
464 *logmsg = NULL;
466 return err;
469 static const struct got_error *
470 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
471 const char *path_dir, const char *branch_name)
473 char *initial_content = NULL;
474 const struct got_error *err = NULL;
475 int fd;
477 if (asprintf(&initial_content,
478 "\n# %s to be imported to branch %s\n", path_dir,
479 branch_name) == -1)
480 return got_error_from_errno("asprintf");
482 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
483 if (err)
484 goto done;
486 dprintf(fd, initial_content);
487 close(fd);
489 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 done:
491 free(initial_content);
492 return err;
495 static const struct got_error *
496 import_progress(void *arg, const char *path)
498 printf("A %s\n", path);
499 return NULL;
502 static const struct got_error *
503 get_author(char **author, struct got_repository *repo)
505 const struct got_error *err = NULL;
506 const char *got_author, *name, *email;
508 *author = NULL;
510 name = got_repo_get_gitconfig_author_name(repo);
511 email = got_repo_get_gitconfig_author_email(repo);
512 if (name && email) {
513 if (asprintf(author, "%s <%s>", name, email) == -1)
514 return got_error_from_errno("asprintf");
515 return NULL;
518 got_author = getenv("GOT_AUTHOR");
519 if (got_author == NULL) {
520 name = got_repo_get_global_gitconfig_author_name(repo);
521 email = got_repo_get_global_gitconfig_author_email(repo);
522 if (name && email) {
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
525 return NULL;
527 /* TODO: Look up user in password database? */
528 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
531 *author = strdup(got_author);
532 if (*author == NULL)
533 return got_error_from_errno("strdup");
535 /*
536 * Really dumb email address check; we're only doing this to
537 * avoid git's object parser breaking on commits we create.
538 */
539 while (*got_author && *got_author != '<')
540 got_author++;
541 if (*got_author != '<') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
543 goto done;
545 while (*got_author && *got_author != '@')
546 got_author++;
547 if (*got_author != '@') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
549 goto done;
551 while (*got_author && *got_author != '>')
552 got_author++;
553 if (*got_author != '>')
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 done:
556 if (err) {
557 free(*author);
558 *author = NULL;
560 return err;
563 static const struct got_error *
564 get_gitconfig_path(char **gitconfig_path)
566 const char *homedir = getenv("HOME");
568 *gitconfig_path = NULL;
569 if (homedir) {
570 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
571 return got_error_from_errno("asprintf");
574 return NULL;
577 static const struct got_error *
578 cmd_import(int argc, char *argv[])
580 const struct got_error *error = NULL;
581 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
582 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
583 const char *branch_name = "main";
584 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
585 struct got_repository *repo = NULL;
586 struct got_reference *branch_ref = NULL, *head_ref = NULL;
587 struct got_object_id *new_commit_id = NULL;
588 int ch;
589 struct got_pathlist_head ignores;
590 struct got_pathlist_entry *pe;
591 int preserve_logmsg = 0;
593 TAILQ_INIT(&ignores);
595 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
596 switch (ch) {
597 case 'b':
598 branch_name = optarg;
599 break;
600 case 'm':
601 logmsg = strdup(optarg);
602 if (logmsg == NULL) {
603 error = got_error_from_errno("strdup");
604 goto done;
606 break;
607 case 'r':
608 repo_path = realpath(optarg, NULL);
609 if (repo_path == NULL) {
610 error = got_error_from_errno2("realpath",
611 optarg);
612 goto done;
614 break;
615 case 'I':
616 if (optarg[0] == '\0')
617 break;
618 error = got_pathlist_insert(&pe, &ignores, optarg,
619 NULL);
620 if (error)
621 goto done;
622 break;
623 default:
624 usage_import();
625 /* NOTREACHED */
629 argc -= optind;
630 argv += optind;
632 #ifndef PROFILE
633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
634 "unveil",
635 NULL) == -1)
636 err(1, "pledge");
637 #endif
638 if (argc != 1)
639 usage_import();
641 if (repo_path == NULL) {
642 repo_path = getcwd(NULL, 0);
643 if (repo_path == NULL)
644 return got_error_from_errno("getcwd");
646 got_path_strip_trailing_slashes(repo_path);
647 error = get_gitconfig_path(&gitconfig_path);
648 if (error)
649 goto done;
650 error = got_repo_open(&repo, repo_path, gitconfig_path);
651 if (error)
652 goto done;
654 error = get_author(&author, repo);
655 if (error)
656 return error;
658 /*
659 * Don't let the user create a branch name with a leading '-'.
660 * While technically a valid reference name, this case is usually
661 * an unintended typo.
662 */
663 if (branch_name[0] == '-')
664 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
666 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
667 error = got_error_from_errno("asprintf");
668 goto done;
671 error = got_ref_open(&branch_ref, repo, refname, 0);
672 if (error) {
673 if (error->code != GOT_ERR_NOT_REF)
674 goto done;
675 } else {
676 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
677 "import target branch already exists");
678 goto done;
681 path_dir = realpath(argv[0], NULL);
682 if (path_dir == NULL) {
683 error = got_error_from_errno2("realpath", argv[0]);
684 goto done;
686 got_path_strip_trailing_slashes(path_dir);
688 /*
689 * unveil(2) traverses exec(2); if an editor is used we have
690 * to apply unveil after the log message has been written.
691 */
692 if (logmsg == NULL || strlen(logmsg) == 0) {
693 error = get_editor(&editor);
694 if (error)
695 goto done;
696 free(logmsg);
697 error = collect_import_msg(&logmsg, &logmsg_path, editor,
698 path_dir, refname);
699 if (error) {
700 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
701 logmsg_path != NULL)
702 preserve_logmsg = 1;
703 goto done;
707 if (unveil(path_dir, "r") != 0) {
708 error = got_error_from_errno2("unveil", path_dir);
709 if (logmsg_path)
710 preserve_logmsg = 1;
711 goto done;
714 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
715 if (error) {
716 if (logmsg_path)
717 preserve_logmsg = 1;
718 goto done;
721 error = got_repo_import(&new_commit_id, path_dir, logmsg,
722 author, &ignores, repo, import_progress, NULL);
723 if (error) {
724 if (logmsg_path)
725 preserve_logmsg = 1;
726 goto done;
729 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
730 if (error) {
731 if (logmsg_path)
732 preserve_logmsg = 1;
733 goto done;
736 error = got_ref_write(branch_ref, repo);
737 if (error) {
738 if (logmsg_path)
739 preserve_logmsg = 1;
740 goto done;
743 error = got_object_id_str(&id_str, new_commit_id);
744 if (error) {
745 if (logmsg_path)
746 preserve_logmsg = 1;
747 goto done;
750 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
751 if (error) {
752 if (error->code != GOT_ERR_NOT_REF) {
753 if (logmsg_path)
754 preserve_logmsg = 1;
755 goto done;
758 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
759 branch_ref);
760 if (error) {
761 if (logmsg_path)
762 preserve_logmsg = 1;
763 goto done;
766 error = got_ref_write(head_ref, repo);
767 if (error) {
768 if (logmsg_path)
769 preserve_logmsg = 1;
770 goto done;
774 printf("Created branch %s with commit %s\n",
775 got_ref_get_name(branch_ref), id_str);
776 done:
777 if (preserve_logmsg) {
778 fprintf(stderr, "%s: log message preserved in %s\n",
779 getprogname(), logmsg_path);
780 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
781 error = got_error_from_errno2("unlink", logmsg_path);
782 free(logmsg);
783 free(logmsg_path);
784 free(repo_path);
785 free(editor);
786 free(refname);
787 free(new_commit_id);
788 free(id_str);
789 free(author);
790 free(gitconfig_path);
791 if (branch_ref)
792 got_ref_close(branch_ref);
793 if (head_ref)
794 got_ref_close(head_ref);
795 return error;
798 __dead static void
799 usage_checkout(void)
801 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
802 "[-p prefix] repository-path [worktree-path]\n", getprogname());
803 exit(1);
806 static const struct got_error *
807 checkout_progress(void *arg, unsigned char status, const char *path)
809 char *worktree_path = arg;
811 /* Base commit bump happens silently. */
812 if (status == GOT_STATUS_BUMP_BASE)
813 return NULL;
815 while (path[0] == '/')
816 path++;
818 printf("%c %s/%s\n", status, worktree_path, path);
819 return NULL;
822 static const struct got_error *
823 check_cancelled(void *arg)
825 if (sigint_received || sigpipe_received)
826 return got_error(GOT_ERR_CANCELLED);
827 return NULL;
830 static const struct got_error *
831 check_linear_ancestry(struct got_object_id *commit_id,
832 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
833 struct got_repository *repo)
835 const struct got_error *err = NULL;
836 struct got_object_id *yca_id;
838 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
839 commit_id, base_commit_id, repo, check_cancelled, NULL);
840 if (err)
841 return err;
843 if (yca_id == NULL)
844 return got_error(GOT_ERR_ANCESTRY);
846 /*
847 * Require a straight line of history between the target commit
848 * and the work tree's base commit.
850 * Non-linear situations such as this require a rebase:
852 * (commit) D F (base_commit)
853 * \ /
854 * C E
855 * \ /
856 * B (yca)
857 * |
858 * A
860 * 'got update' only handles linear cases:
861 * Update forwards in time: A (base/yca) - B - C - D (commit)
862 * Update backwards in time: D (base) - C - B - A (commit/yca)
863 */
864 if (allow_forwards_in_time_only) {
865 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
866 return got_error(GOT_ERR_ANCESTRY);
867 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
868 got_object_id_cmp(base_commit_id, yca_id) != 0)
869 return got_error(GOT_ERR_ANCESTRY);
871 free(yca_id);
872 return NULL;
875 static const struct got_error *
876 check_same_branch(struct got_object_id *commit_id,
877 struct got_reference *head_ref, struct got_object_id *yca_id,
878 struct got_repository *repo)
880 const struct got_error *err = NULL;
881 struct got_commit_graph *graph = NULL;
882 struct got_object_id *head_commit_id = NULL;
883 int is_same_branch = 0;
885 err = got_ref_resolve(&head_commit_id, repo, head_ref);
886 if (err)
887 goto done;
889 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
890 is_same_branch = 1;
891 goto done;
893 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
894 is_same_branch = 1;
895 goto done;
898 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
899 if (err)
900 goto done;
902 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
903 check_cancelled, NULL);
904 if (err)
905 goto done;
907 for (;;) {
908 struct got_object_id *id;
909 err = got_commit_graph_iter_next(&id, graph);
910 if (err) {
911 if (err->code == GOT_ERR_ITER_COMPLETED) {
912 err = NULL;
913 break;
914 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 break;
916 err = got_commit_graph_fetch_commits(graph, 1,
917 repo, check_cancelled, NULL);
918 if (err)
919 break;
922 if (id) {
923 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 break;
925 if (got_object_id_cmp(id, commit_id) == 0) {
926 is_same_branch = 1;
927 break;
931 done:
932 if (graph)
933 got_commit_graph_close(graph);
934 free(head_commit_id);
935 if (!err && !is_same_branch)
936 err = got_error(GOT_ERR_ANCESTRY);
937 return err;
940 static const struct got_error *
941 resolve_commit_arg(struct got_object_id **commit_id,
942 const char *commit_id_arg, struct got_repository *repo)
944 const struct got_error *err;
945 struct got_reference *ref;
946 struct got_tag_object *tag;
948 err = got_repo_object_match_tag(&tag, commit_id_arg,
949 GOT_OBJ_TYPE_COMMIT, repo);
950 if (err == NULL) {
951 *commit_id = got_object_id_dup(
952 got_object_tag_get_object_id(tag));
953 if (*commit_id == NULL)
954 err = got_error_from_errno("got_object_id_dup");
955 got_object_tag_close(tag);
956 return err;
957 } else if (err->code != GOT_ERR_NO_OBJ)
958 return err;
960 err = got_ref_open(&ref, repo, commit_id_arg, 0);
961 if (err == NULL) {
962 err = got_ref_resolve(commit_id, repo, ref);
963 got_ref_close(ref);
964 } else {
965 if (err->code != GOT_ERR_NOT_REF)
966 return err;
967 err = got_repo_match_object_id_prefix(commit_id,
968 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
970 return err;
973 static const struct got_error *
974 cmd_checkout(int argc, char *argv[])
976 const struct got_error *error = NULL;
977 struct got_repository *repo = NULL;
978 struct got_reference *head_ref = NULL;
979 struct got_worktree *worktree = NULL;
980 char *repo_path = NULL;
981 char *worktree_path = NULL;
982 const char *path_prefix = "";
983 const char *branch_name = GOT_REF_HEAD;
984 char *commit_id_str = NULL;
985 int ch, same_path_prefix;
986 struct got_pathlist_head paths;
988 TAILQ_INIT(&paths);
990 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
991 switch (ch) {
992 case 'b':
993 branch_name = optarg;
994 break;
995 case 'c':
996 commit_id_str = strdup(optarg);
997 if (commit_id_str == NULL)
998 return got_error_from_errno("strdup");
999 break;
1000 case 'p':
1001 path_prefix = optarg;
1002 break;
1003 default:
1004 usage_checkout();
1005 /* NOTREACHED */
1009 argc -= optind;
1010 argv += optind;
1012 #ifndef PROFILE
1013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1014 "unveil", NULL) == -1)
1015 err(1, "pledge");
1016 #endif
1017 if (argc == 1) {
1018 char *cwd, *base, *dotgit;
1019 repo_path = realpath(argv[0], NULL);
1020 if (repo_path == NULL)
1021 return got_error_from_errno2("realpath", argv[0]);
1022 cwd = getcwd(NULL, 0);
1023 if (cwd == NULL) {
1024 error = got_error_from_errno("getcwd");
1025 goto done;
1027 if (path_prefix[0]) {
1028 base = basename(path_prefix);
1029 if (base == NULL) {
1030 error = got_error_from_errno2("basename",
1031 path_prefix);
1032 goto done;
1034 } else {
1035 base = basename(repo_path);
1036 if (base == NULL) {
1037 error = got_error_from_errno2("basename",
1038 repo_path);
1039 goto done;
1042 dotgit = strstr(base, ".git");
1043 if (dotgit)
1044 *dotgit = '\0';
1045 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1046 error = got_error_from_errno("asprintf");
1047 free(cwd);
1048 goto done;
1050 free(cwd);
1051 } else if (argc == 2) {
1052 repo_path = realpath(argv[0], NULL);
1053 if (repo_path == NULL) {
1054 error = got_error_from_errno2("realpath", argv[0]);
1055 goto done;
1057 worktree_path = realpath(argv[1], NULL);
1058 if (worktree_path == NULL) {
1059 if (errno != ENOENT) {
1060 error = got_error_from_errno2("realpath",
1061 argv[1]);
1062 goto done;
1064 worktree_path = strdup(argv[1]);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("strdup");
1067 goto done;
1070 } else
1071 usage_checkout();
1073 got_path_strip_trailing_slashes(repo_path);
1074 got_path_strip_trailing_slashes(worktree_path);
1076 error = got_repo_open(&repo, repo_path, NULL);
1077 if (error != NULL)
1078 goto done;
1080 /* Pre-create work tree path for unveil(2) */
1081 error = got_path_mkdir(worktree_path);
1082 if (error) {
1083 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1084 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1085 goto done;
1086 if (!got_path_dir_is_empty(worktree_path)) {
1087 error = got_error_path(worktree_path,
1088 GOT_ERR_DIR_NOT_EMPTY);
1089 goto done;
1093 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1094 if (error)
1095 goto done;
1097 error = got_ref_open(&head_ref, repo, branch_name, 0);
1098 if (error != NULL)
1099 goto done;
1101 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1102 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1103 goto done;
1105 error = got_worktree_open(&worktree, worktree_path);
1106 if (error != NULL)
1107 goto done;
1109 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1110 path_prefix);
1111 if (error != NULL)
1112 goto done;
1113 if (!same_path_prefix) {
1114 error = got_error(GOT_ERR_PATH_PREFIX);
1115 goto done;
1118 if (commit_id_str) {
1119 struct got_object_id *commit_id;
1120 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1121 if (error)
1122 goto done;
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), 0, repo);
1125 if (error != NULL) {
1126 free(commit_id);
1127 goto done;
1129 error = check_same_branch(commit_id, head_ref, NULL, repo);
1130 if (error)
1131 goto done;
1132 error = got_worktree_set_base_commit_id(worktree, repo,
1133 commit_id);
1134 free(commit_id);
1135 if (error)
1136 goto done;
1139 error = got_pathlist_append(&paths, "", NULL);
1140 if (error)
1141 goto done;
1142 error = got_worktree_checkout_files(worktree, &paths, repo,
1143 checkout_progress, worktree_path, check_cancelled, NULL);
1144 if (error != NULL)
1145 goto done;
1147 printf("Now shut up and hack\n");
1149 done:
1150 got_pathlist_free(&paths);
1151 free(commit_id_str);
1152 free(repo_path);
1153 free(worktree_path);
1154 return error;
1157 __dead static void
1158 usage_update(void)
1160 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1161 getprogname());
1162 exit(1);
1165 static const struct got_error *
1166 update_progress(void *arg, unsigned char status, const char *path)
1168 int *did_something = arg;
1170 if (status == GOT_STATUS_EXISTS)
1171 return NULL;
1173 *did_something = 1;
1175 /* Base commit bump happens silently. */
1176 if (status == GOT_STATUS_BUMP_BASE)
1177 return NULL;
1179 while (path[0] == '/')
1180 path++;
1181 printf("%c %s\n", status, path);
1182 return NULL;
1185 static const struct got_error *
1186 switch_head_ref(struct got_reference *head_ref,
1187 struct got_object_id *commit_id, struct got_worktree *worktree,
1188 struct got_repository *repo)
1190 const struct got_error *err = NULL;
1191 char *base_id_str;
1192 int ref_has_moved = 0;
1194 /* Trivial case: switching between two different references. */
1195 if (strcmp(got_ref_get_name(head_ref),
1196 got_worktree_get_head_ref_name(worktree)) != 0) {
1197 printf("Switching work tree from %s to %s\n",
1198 got_worktree_get_head_ref_name(worktree),
1199 got_ref_get_name(head_ref));
1200 return got_worktree_set_head_ref(worktree, head_ref);
1203 err = check_linear_ancestry(commit_id,
1204 got_worktree_get_base_commit_id(worktree), 0, repo);
1205 if (err) {
1206 if (err->code != GOT_ERR_ANCESTRY)
1207 return err;
1208 ref_has_moved = 1;
1210 if (!ref_has_moved)
1211 return NULL;
1213 /* Switching to a rebased branch with the same reference name. */
1214 err = got_object_id_str(&base_id_str,
1215 got_worktree_get_base_commit_id(worktree));
1216 if (err)
1217 return err;
1218 printf("Reference %s now points at a different branch\n",
1219 got_worktree_get_head_ref_name(worktree));
1220 printf("Switching work tree from %s to %s\n", base_id_str,
1221 got_worktree_get_head_ref_name(worktree));
1222 return NULL;
1225 static const struct got_error *
1226 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1228 const struct got_error *err;
1229 int in_progress;
1231 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1232 if (err)
1233 return err;
1234 if (in_progress)
1235 return got_error(GOT_ERR_REBASING);
1237 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1238 if (err)
1239 return err;
1240 if (in_progress)
1241 return got_error(GOT_ERR_HISTEDIT_BUSY);
1243 return NULL;
1246 static const struct got_error *
1247 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1248 char *argv[], struct got_worktree *worktree)
1250 const struct got_error *err = NULL;
1251 char *path;
1252 int i;
1254 if (argc == 0) {
1255 path = strdup("");
1256 if (path == NULL)
1257 return got_error_from_errno("strdup");
1258 return got_pathlist_append(paths, path, NULL);
1261 for (i = 0; i < argc; i++) {
1262 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1263 if (err)
1264 break;
1265 err = got_pathlist_append(paths, path, NULL);
1266 if (err) {
1267 free(path);
1268 break;
1272 return err;
1275 static const struct got_error *
1276 cmd_update(int argc, char *argv[])
1278 const struct got_error *error = NULL;
1279 struct got_repository *repo = NULL;
1280 struct got_worktree *worktree = NULL;
1281 char *worktree_path = NULL;
1282 struct got_object_id *commit_id = NULL;
1283 char *commit_id_str = NULL;
1284 const char *branch_name = NULL;
1285 struct got_reference *head_ref = NULL;
1286 struct got_pathlist_head paths;
1287 struct got_pathlist_entry *pe;
1288 int ch, did_something = 0;
1290 TAILQ_INIT(&paths);
1292 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1293 switch (ch) {
1294 case 'b':
1295 branch_name = optarg;
1296 break;
1297 case 'c':
1298 commit_id_str = strdup(optarg);
1299 if (commit_id_str == NULL)
1300 return got_error_from_errno("strdup");
1301 break;
1302 default:
1303 usage_update();
1304 /* NOTREACHED */
1308 argc -= optind;
1309 argv += optind;
1311 #ifndef PROFILE
1312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1313 "unveil", NULL) == -1)
1314 err(1, "pledge");
1315 #endif
1316 worktree_path = getcwd(NULL, 0);
1317 if (worktree_path == NULL) {
1318 error = got_error_from_errno("getcwd");
1319 goto done;
1321 error = got_worktree_open(&worktree, worktree_path);
1322 if (error)
1323 goto done;
1325 error = check_rebase_or_histedit_in_progress(worktree);
1326 if (error)
1327 goto done;
1329 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1330 NULL);
1331 if (error != NULL)
1332 goto done;
1334 error = apply_unveil(got_repo_get_path(repo), 0,
1335 got_worktree_get_root_path(worktree));
1336 if (error)
1337 goto done;
1339 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1340 if (error)
1341 goto done;
1343 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1344 got_worktree_get_head_ref_name(worktree), 0);
1345 if (error != NULL)
1346 goto done;
1347 if (commit_id_str == NULL) {
1348 error = got_ref_resolve(&commit_id, repo, head_ref);
1349 if (error != NULL)
1350 goto done;
1351 error = got_object_id_str(&commit_id_str, commit_id);
1352 if (error != NULL)
1353 goto done;
1354 } else {
1355 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1356 free(commit_id_str);
1357 commit_id_str = NULL;
1358 if (error)
1359 goto done;
1360 error = got_object_id_str(&commit_id_str, commit_id);
1361 if (error)
1362 goto done;
1365 if (branch_name) {
1366 struct got_object_id *head_commit_id;
1367 TAILQ_FOREACH(pe, &paths, entry) {
1368 if (pe->path_len == 0)
1369 continue;
1370 error = got_error_msg(GOT_ERR_BAD_PATH,
1371 "switching between branches requires that "
1372 "the entire work tree gets updated");
1373 goto done;
1375 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1376 if (error)
1377 goto done;
1378 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 repo);
1380 free(head_commit_id);
1381 if (error != NULL)
1382 goto done;
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1384 if (error)
1385 goto done;
1386 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1387 if (error)
1388 goto done;
1389 } else {
1390 error = check_linear_ancestry(commit_id,
1391 got_worktree_get_base_commit_id(worktree), 0, repo);
1392 if (error != NULL) {
1393 if (error->code == GOT_ERR_ANCESTRY)
1394 error = got_error(GOT_ERR_BRANCH_MOVED);
1395 goto done;
1397 error = check_same_branch(commit_id, head_ref, NULL, repo);
1398 if (error)
1399 goto done;
1402 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1403 commit_id) != 0) {
1404 error = got_worktree_set_base_commit_id(worktree, repo,
1405 commit_id);
1406 if (error)
1407 goto done;
1410 error = got_worktree_checkout_files(worktree, &paths, repo,
1411 update_progress, &did_something, check_cancelled, NULL);
1412 if (error != NULL)
1413 goto done;
1415 if (did_something)
1416 printf("Updated to commit %s\n", commit_id_str);
1417 else
1418 printf("Already up-to-date\n");
1419 done:
1420 free(worktree_path);
1421 TAILQ_FOREACH(pe, &paths, entry)
1422 free((char *)pe->path);
1423 got_pathlist_free(&paths);
1424 free(commit_id);
1425 free(commit_id_str);
1426 return error;
1429 static const struct got_error *
1430 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1431 const char *path, int diff_context, int ignore_whitespace,
1432 struct got_repository *repo)
1434 const struct got_error *err = NULL;
1435 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1437 if (blob_id1) {
1438 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 if (err)
1440 goto done;
1443 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1444 if (err)
1445 goto done;
1447 while (path[0] == '/')
1448 path++;
1449 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1450 ignore_whitespace, stdout);
1451 done:
1452 if (blob1)
1453 got_object_blob_close(blob1);
1454 got_object_blob_close(blob2);
1455 return err;
1458 static const struct got_error *
1459 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1460 const char *path, int diff_context, int ignore_whitespace,
1461 struct got_repository *repo)
1463 const struct got_error *err = NULL;
1464 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1465 struct got_diff_blob_output_unidiff_arg arg;
1467 if (tree_id1) {
1468 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 if (err)
1470 goto done;
1473 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1474 if (err)
1475 goto done;
1477 arg.diff_context = diff_context;
1478 arg.ignore_whitespace = ignore_whitespace;
1479 arg.outfile = stdout;
1480 while (path[0] == '/')
1481 path++;
1482 err = got_diff_tree(tree1, tree2, path, path, repo,
1483 got_diff_blob_output_unidiff, &arg, 1);
1484 done:
1485 if (tree1)
1486 got_object_tree_close(tree1);
1487 if (tree2)
1488 got_object_tree_close(tree2);
1489 return err;
1492 static const struct got_error *
1493 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1494 const char *path, int diff_context, struct got_repository *repo)
1496 const struct got_error *err = NULL;
1497 struct got_commit_object *pcommit = NULL;
1498 char *id_str1 = NULL, *id_str2 = NULL;
1499 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1500 struct got_object_qid *qid;
1502 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 if (qid != NULL) {
1504 err = got_object_open_as_commit(&pcommit, repo,
1505 qid->id);
1506 if (err)
1507 return err;
1510 if (path && path[0] != '\0') {
1511 int obj_type;
1512 err = got_object_id_by_path(&obj_id2, repo, id, path);
1513 if (err)
1514 goto done;
1515 err = got_object_id_str(&id_str2, obj_id2);
1516 if (err) {
1517 free(obj_id2);
1518 goto done;
1520 if (pcommit) {
1521 err = got_object_id_by_path(&obj_id1, repo,
1522 qid->id, path);
1523 if (err) {
1524 free(obj_id2);
1525 goto done;
1527 err = got_object_id_str(&id_str1, obj_id1);
1528 if (err) {
1529 free(obj_id2);
1530 goto done;
1533 err = got_object_get_type(&obj_type, repo, obj_id2);
1534 if (err) {
1535 free(obj_id2);
1536 goto done;
1538 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1539 switch (obj_type) {
1540 case GOT_OBJ_TYPE_BLOB:
1541 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1542 0, repo);
1543 break;
1544 case GOT_OBJ_TYPE_TREE:
1545 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1546 0, repo);
1547 break;
1548 default:
1549 err = got_error(GOT_ERR_OBJ_TYPE);
1550 break;
1552 free(obj_id1);
1553 free(obj_id2);
1554 } else {
1555 obj_id2 = got_object_commit_get_tree_id(commit);
1556 err = got_object_id_str(&id_str2, obj_id2);
1557 if (err)
1558 goto done;
1559 obj_id1 = got_object_commit_get_tree_id(pcommit);
1560 err = got_object_id_str(&id_str1, obj_id1);
1561 if (err)
1562 goto done;
1563 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1564 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1567 done:
1568 free(id_str1);
1569 free(id_str2);
1570 if (pcommit)
1571 got_object_commit_close(pcommit);
1572 return err;
1575 static char *
1576 get_datestr(time_t *time, char *datebuf)
1578 struct tm mytm, *tm;
1579 char *p, *s;
1581 tm = gmtime_r(time, &mytm);
1582 if (tm == NULL)
1583 return NULL;
1584 s = asctime_r(tm, datebuf);
1585 if (s == NULL)
1586 return NULL;
1587 p = strchr(s, '\n');
1588 if (p)
1589 *p = '\0';
1590 return s;
1593 static const struct got_error *
1594 match_logmsg(int *have_match, struct got_object_id *id,
1595 struct got_commit_object *commit, regex_t *regex)
1597 const struct got_error *err = NULL;
1598 regmatch_t regmatch;
1599 char *id_str = NULL, *logmsg = NULL;
1601 *have_match = 0;
1603 err = got_object_id_str(&id_str, id);
1604 if (err)
1605 return err;
1607 err = got_object_commit_get_logmsg(&logmsg, commit);
1608 if (err)
1609 goto done;
1611 if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1612 *have_match = 1;
1613 done:
1614 free(id_str);
1615 free(logmsg);
1616 return err;
1619 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1621 static const struct got_error *
1622 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1623 struct got_repository *repo, const char *path, int show_patch,
1624 int diff_context, struct got_reflist_head *refs)
1626 const struct got_error *err = NULL;
1627 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1628 char datebuf[26];
1629 time_t committer_time;
1630 const char *author, *committer;
1631 char *refs_str = NULL;
1632 struct got_reflist_entry *re;
1634 SIMPLEQ_FOREACH(re, refs, entry) {
1635 char *s;
1636 const char *name;
1637 struct got_tag_object *tag = NULL;
1638 int cmp;
1640 name = got_ref_get_name(re->ref);
1641 if (strcmp(name, GOT_REF_HEAD) == 0)
1642 continue;
1643 if (strncmp(name, "refs/", 5) == 0)
1644 name += 5;
1645 if (strncmp(name, "got/", 4) == 0)
1646 continue;
1647 if (strncmp(name, "heads/", 6) == 0)
1648 name += 6;
1649 if (strncmp(name, "remotes/", 8) == 0)
1650 name += 8;
1651 if (strncmp(name, "tags/", 5) == 0) {
1652 err = got_object_open_as_tag(&tag, repo, re->id);
1653 if (err) {
1654 if (err->code != GOT_ERR_OBJ_TYPE)
1655 return err;
1656 /* Ref points at something other than a tag. */
1657 err = NULL;
1658 tag = NULL;
1661 cmp = got_object_id_cmp(tag ?
1662 got_object_tag_get_object_id(tag) : re->id, id);
1663 if (tag)
1664 got_object_tag_close(tag);
1665 if (cmp != 0)
1666 continue;
1667 s = refs_str;
1668 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1669 name) == -1) {
1670 err = got_error_from_errno("asprintf");
1671 free(s);
1672 return err;
1674 free(s);
1676 err = got_object_id_str(&id_str, id);
1677 if (err)
1678 return err;
1680 printf(GOT_COMMIT_SEP_STR);
1681 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1682 refs_str ? refs_str : "", refs_str ? ")" : "");
1683 free(id_str);
1684 id_str = NULL;
1685 free(refs_str);
1686 refs_str = NULL;
1687 printf("from: %s\n", got_object_commit_get_author(commit));
1688 committer_time = got_object_commit_get_committer_time(commit);
1689 datestr = get_datestr(&committer_time, datebuf);
1690 if (datestr)
1691 printf("date: %s UTC\n", datestr);
1692 author = got_object_commit_get_author(commit);
1693 committer = got_object_commit_get_committer(commit);
1694 if (strcmp(author, committer) != 0)
1695 printf("via: %s\n", committer);
1696 if (got_object_commit_get_nparents(commit) > 1) {
1697 const struct got_object_id_queue *parent_ids;
1698 struct got_object_qid *qid;
1699 int n = 1;
1700 parent_ids = got_object_commit_get_parent_ids(commit);
1701 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1702 err = got_object_id_str(&id_str, qid->id);
1703 if (err)
1704 return err;
1705 printf("parent %d: %s\n", n++, id_str);
1706 free(id_str);
1710 err = got_object_commit_get_logmsg(&logmsg0, commit);
1711 if (err)
1712 return err;
1714 logmsg = logmsg0;
1715 do {
1716 line = strsep(&logmsg, "\n");
1717 if (line)
1718 printf(" %s\n", line);
1719 } while (line);
1720 free(logmsg0);
1722 if (show_patch) {
1723 err = print_patch(commit, id, path, diff_context, repo);
1724 if (err == 0)
1725 printf("\n");
1728 if (fflush(stdout) != 0 && err == NULL)
1729 err = got_error_from_errno("fflush");
1730 return err;
1733 static const struct got_error *
1734 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1735 char *path, int show_patch, char *search_pattern, int diff_context,
1736 int limit, int first_parent_traversal, struct got_reflist_head *refs)
1738 const struct got_error *err;
1739 struct got_commit_graph *graph;
1740 regex_t regex;
1741 int have_match;
1743 if (search_pattern &&
1744 regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1745 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1747 err = got_commit_graph_open(&graph, root_id, path,
1748 first_parent_traversal, repo);
1749 if (err)
1750 return err;
1751 err = got_commit_graph_iter_start(graph, root_id, repo,
1752 check_cancelled, NULL);
1753 if (err)
1754 goto done;
1755 for (;;) {
1756 struct got_commit_object *commit;
1757 struct got_object_id *id;
1759 if (sigint_received || sigpipe_received)
1760 break;
1762 err = got_commit_graph_iter_next(&id, graph);
1763 if (err) {
1764 if (err->code == GOT_ERR_ITER_COMPLETED) {
1765 err = NULL;
1766 break;
1768 if (err->code != GOT_ERR_ITER_NEED_MORE)
1769 break;
1770 err = got_commit_graph_fetch_commits(graph, 1, repo,
1771 check_cancelled, NULL);
1772 if (err)
1773 break;
1774 else
1775 continue;
1777 if (id == NULL)
1778 break;
1780 err = got_object_open_as_commit(&commit, repo, id);
1781 if (err)
1782 break;
1784 if (search_pattern) {
1785 err = match_logmsg(&have_match, id, commit, &regex);
1786 if (err) {
1787 got_object_commit_close(commit);
1788 break;
1790 if (have_match == 0) {
1791 got_object_commit_close(commit);
1792 continue;
1796 err = print_commit(commit, id, repo, path, show_patch,
1797 diff_context, refs);
1798 got_object_commit_close(commit);
1799 if (err || (limit && --limit == 0))
1800 break;
1802 done:
1803 if (search_pattern)
1804 regfree(&regex);
1805 got_commit_graph_close(graph);
1806 return err;
1809 __dead static void
1810 usage_log(void)
1812 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1813 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1814 exit(1);
1817 static int
1818 get_default_log_limit(void)
1820 const char *got_default_log_limit;
1821 long long n;
1822 const char *errstr;
1824 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1825 if (got_default_log_limit == NULL)
1826 return 0;
1827 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1828 if (errstr != NULL)
1829 return 0;
1830 return n;
1833 static const struct got_error *
1834 cmd_log(int argc, char *argv[])
1836 const struct got_error *error;
1837 struct got_repository *repo = NULL;
1838 struct got_worktree *worktree = NULL;
1839 struct got_commit_object *commit = NULL;
1840 struct got_object_id *id = NULL;
1841 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1842 char *start_commit = NULL, *search_pattern = NULL;
1843 int diff_context = -1, ch;
1844 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1845 const char *errstr;
1846 struct got_reflist_head refs;
1848 SIMPLEQ_INIT(&refs);
1850 #ifndef PROFILE
1851 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1852 NULL)
1853 == -1)
1854 err(1, "pledge");
1855 #endif
1857 limit = get_default_log_limit();
1859 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1860 switch (ch) {
1861 case 'p':
1862 show_patch = 1;
1863 break;
1864 case 'c':
1865 start_commit = optarg;
1866 break;
1867 case 'C':
1868 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1869 &errstr);
1870 if (errstr != NULL)
1871 err(1, "-C option %s", errstr);
1872 break;
1873 case 'l':
1874 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1875 if (errstr != NULL)
1876 err(1, "-l option %s", errstr);
1877 break;
1878 case 'f':
1879 first_parent_traversal = 1;
1880 break;
1881 case 'r':
1882 repo_path = realpath(optarg, NULL);
1883 if (repo_path == NULL)
1884 return got_error_from_errno2("realpath",
1885 optarg);
1886 got_path_strip_trailing_slashes(repo_path);
1887 break;
1888 case 's':
1889 search_pattern = optarg;
1890 break;
1891 default:
1892 usage_log();
1893 /* NOTREACHED */
1897 argc -= optind;
1898 argv += optind;
1900 if (diff_context == -1)
1901 diff_context = 3;
1902 else if (!show_patch)
1903 errx(1, "-C reguires -p");
1905 cwd = getcwd(NULL, 0);
1906 if (cwd == NULL) {
1907 error = got_error_from_errno("getcwd");
1908 goto done;
1911 error = got_worktree_open(&worktree, cwd);
1912 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 goto done;
1914 error = NULL;
1916 if (argc == 0) {
1917 path = strdup("");
1918 if (path == NULL) {
1919 error = got_error_from_errno("strdup");
1920 goto done;
1922 } else if (argc == 1) {
1923 if (worktree) {
1924 error = got_worktree_resolve_path(&path, worktree,
1925 argv[0]);
1926 if (error)
1927 goto done;
1928 } else {
1929 path = strdup(argv[0]);
1930 if (path == NULL) {
1931 error = got_error_from_errno("strdup");
1932 goto done;
1935 } else
1936 usage_log();
1938 if (repo_path == NULL) {
1939 repo_path = worktree ?
1940 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1942 if (repo_path == NULL) {
1943 error = got_error_from_errno("strdup");
1944 goto done;
1947 error = got_repo_open(&repo, repo_path, NULL);
1948 if (error != NULL)
1949 goto done;
1951 error = apply_unveil(got_repo_get_path(repo), 1,
1952 worktree ? got_worktree_get_root_path(worktree) : NULL);
1953 if (error)
1954 goto done;
1956 if (start_commit == NULL) {
1957 struct got_reference *head_ref;
1958 error = got_ref_open(&head_ref, repo,
1959 worktree ? got_worktree_get_head_ref_name(worktree)
1960 : GOT_REF_HEAD, 0);
1961 if (error != NULL)
1962 return error;
1963 error = got_ref_resolve(&id, repo, head_ref);
1964 got_ref_close(head_ref);
1965 if (error != NULL)
1966 return error;
1967 error = got_object_open_as_commit(&commit, repo, id);
1968 } else {
1969 struct got_reference *ref;
1970 error = got_ref_open(&ref, repo, start_commit, 0);
1971 if (error == NULL) {
1972 int obj_type;
1973 error = got_ref_resolve(&id, repo, ref);
1974 got_ref_close(ref);
1975 if (error != NULL)
1976 goto done;
1977 error = got_object_get_type(&obj_type, repo, id);
1978 if (error != NULL)
1979 goto done;
1980 if (obj_type == GOT_OBJ_TYPE_TAG) {
1981 struct got_tag_object *tag;
1982 error = got_object_open_as_tag(&tag, repo, id);
1983 if (error != NULL)
1984 goto done;
1985 if (got_object_tag_get_object_type(tag) !=
1986 GOT_OBJ_TYPE_COMMIT) {
1987 got_object_tag_close(tag);
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1989 goto done;
1991 free(id);
1992 id = got_object_id_dup(
1993 got_object_tag_get_object_id(tag));
1994 if (id == NULL)
1995 error = got_error_from_errno(
1996 "got_object_id_dup");
1997 got_object_tag_close(tag);
1998 if (error)
1999 goto done;
2000 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2001 error = got_error(GOT_ERR_OBJ_TYPE);
2002 goto done;
2004 error = got_object_open_as_commit(&commit, repo, id);
2005 if (error != NULL)
2006 goto done;
2008 if (commit == NULL) {
2009 error = got_repo_match_object_id_prefix(&id,
2010 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2011 if (error != NULL)
2012 return error;
2015 if (error != NULL)
2016 goto done;
2018 if (worktree) {
2019 const char *prefix = got_worktree_get_path_prefix(worktree);
2020 char *p;
2021 if (asprintf(&p, "%s%s%s", prefix,
2022 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2023 error = got_error_from_errno("asprintf");
2024 goto done;
2026 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2027 free(p);
2028 } else
2029 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2030 if (error != NULL)
2031 goto done;
2032 if (in_repo_path) {
2033 free(path);
2034 path = in_repo_path;
2037 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2038 if (error)
2039 goto done;
2041 error = print_commits(id, repo, path, show_patch, search_pattern,
2042 diff_context, limit, first_parent_traversal, &refs);
2043 done:
2044 free(path);
2045 free(repo_path);
2046 free(cwd);
2047 free(id);
2048 if (worktree)
2049 got_worktree_close(worktree);
2050 if (repo) {
2051 const struct got_error *repo_error;
2052 repo_error = got_repo_close(repo);
2053 if (error == NULL)
2054 error = repo_error;
2056 got_ref_list_free(&refs);
2057 return error;
2060 __dead static void
2061 usage_diff(void)
2063 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2064 "[-w] [object1 object2 | path]\n", getprogname());
2065 exit(1);
2068 struct print_diff_arg {
2069 struct got_repository *repo;
2070 struct got_worktree *worktree;
2071 int diff_context;
2072 const char *id_str;
2073 int header_shown;
2074 int diff_staged;
2075 int ignore_whitespace;
2078 static const struct got_error *
2079 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2080 const char *path, struct got_object_id *blob_id,
2081 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2083 struct print_diff_arg *a = arg;
2084 const struct got_error *err = NULL;
2085 struct got_blob_object *blob1 = NULL;
2086 FILE *f2 = NULL;
2087 char *abspath = NULL, *label1 = NULL;
2088 struct stat sb;
2090 if (a->diff_staged) {
2091 if (staged_status != GOT_STATUS_MODIFY &&
2092 staged_status != GOT_STATUS_ADD &&
2093 staged_status != GOT_STATUS_DELETE)
2094 return NULL;
2095 } else {
2096 if (staged_status == GOT_STATUS_DELETE)
2097 return NULL;
2098 if (status == GOT_STATUS_NONEXISTENT)
2099 return got_error_set_errno(ENOENT, path);
2100 if (status != GOT_STATUS_MODIFY &&
2101 status != GOT_STATUS_ADD &&
2102 status != GOT_STATUS_DELETE &&
2103 status != GOT_STATUS_CONFLICT)
2104 return NULL;
2107 if (!a->header_shown) {
2108 printf("diff %s %s%s\n", a->id_str,
2109 got_worktree_get_root_path(a->worktree),
2110 a->diff_staged ? " (staged changes)" : "");
2111 a->header_shown = 1;
2114 if (a->diff_staged) {
2115 const char *label1 = NULL, *label2 = NULL;
2116 switch (staged_status) {
2117 case GOT_STATUS_MODIFY:
2118 label1 = path;
2119 label2 = path;
2120 break;
2121 case GOT_STATUS_ADD:
2122 label2 = path;
2123 break;
2124 case GOT_STATUS_DELETE:
2125 label1 = path;
2126 break;
2127 default:
2128 return got_error(GOT_ERR_FILE_STATUS);
2130 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2131 label1, label2, a->diff_context, a->ignore_whitespace,
2132 a->repo, stdout);
2135 if (staged_status == GOT_STATUS_ADD ||
2136 staged_status == GOT_STATUS_MODIFY) {
2137 char *id_str;
2138 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2139 8192);
2140 if (err)
2141 goto done;
2142 err = got_object_id_str(&id_str, staged_blob_id);
2143 if (err)
2144 goto done;
2145 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2146 err = got_error_from_errno("asprintf");
2147 free(id_str);
2148 goto done;
2150 free(id_str);
2151 } else if (status != GOT_STATUS_ADD) {
2152 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2153 if (err)
2154 goto done;
2157 if (status != GOT_STATUS_DELETE) {
2158 if (asprintf(&abspath, "%s/%s",
2159 got_worktree_get_root_path(a->worktree), path) == -1) {
2160 err = got_error_from_errno("asprintf");
2161 goto done;
2164 f2 = fopen(abspath, "r");
2165 if (f2 == NULL) {
2166 err = got_error_from_errno2("fopen", abspath);
2167 goto done;
2169 if (lstat(abspath, &sb) == -1) {
2170 err = got_error_from_errno2("lstat", abspath);
2171 goto done;
2173 } else
2174 sb.st_size = 0;
2176 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2177 a->diff_context, a->ignore_whitespace, stdout);
2178 done:
2179 if (blob1)
2180 got_object_blob_close(blob1);
2181 if (f2 && fclose(f2) != 0 && err == NULL)
2182 err = got_error_from_errno("fclose");
2183 free(abspath);
2184 return err;
2187 static const struct got_error *
2188 match_object_id(struct got_object_id **id, char **label,
2189 const char *id_str, int obj_type, int resolve_tags,
2190 struct got_repository *repo)
2192 const struct got_error *err;
2193 struct got_tag_object *tag;
2194 struct got_reference *ref = NULL;
2196 *id = NULL;
2197 *label = NULL;
2199 if (resolve_tags) {
2200 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2201 repo);
2202 if (err == NULL) {
2203 *id = got_object_id_dup(
2204 got_object_tag_get_object_id(tag));
2205 if (*id == NULL)
2206 err = got_error_from_errno("got_object_id_dup");
2207 else if (asprintf(label, "refs/tags/%s",
2208 got_object_tag_get_name(tag)) == -1) {
2209 err = got_error_from_errno("asprintf");
2210 free(*id);
2211 *id = NULL;
2213 got_object_tag_close(tag);
2214 return err;
2215 } else if (err->code != GOT_ERR_NO_OBJ)
2216 return err;
2219 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2220 if (err) {
2221 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2222 return err;
2223 err = got_ref_open(&ref, repo, id_str, 0);
2224 if (err != NULL)
2225 goto done;
2226 *label = strdup(got_ref_get_name(ref));
2227 if (*label == NULL) {
2228 err = got_error_from_errno("strdup");
2229 goto done;
2231 err = got_ref_resolve(id, repo, ref);
2232 } else {
2233 err = got_object_id_str(label, *id);
2234 if (*label == NULL) {
2235 err = got_error_from_errno("strdup");
2236 goto done;
2239 done:
2240 if (ref)
2241 got_ref_close(ref);
2242 return err;
2246 static const struct got_error *
2247 cmd_diff(int argc, char *argv[])
2249 const struct got_error *error;
2250 struct got_repository *repo = NULL;
2251 struct got_worktree *worktree = NULL;
2252 char *cwd = NULL, *repo_path = NULL;
2253 struct got_object_id *id1 = NULL, *id2 = NULL;
2254 const char *id_str1 = NULL, *id_str2 = NULL;
2255 char *label1 = NULL, *label2 = NULL;
2256 int type1, type2;
2257 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2258 const char *errstr;
2259 char *path = NULL;
2261 #ifndef PROFILE
2262 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2263 NULL) == -1)
2264 err(1, "pledge");
2265 #endif
2267 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2268 switch (ch) {
2269 case 'C':
2270 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2271 if (errstr != NULL)
2272 err(1, "-C option %s", errstr);
2273 break;
2274 case 'r':
2275 repo_path = realpath(optarg, NULL);
2276 if (repo_path == NULL)
2277 return got_error_from_errno2("realpath",
2278 optarg);
2279 got_path_strip_trailing_slashes(repo_path);
2280 break;
2281 case 's':
2282 diff_staged = 1;
2283 break;
2284 case 'w':
2285 ignore_whitespace = 1;
2286 break;
2287 default:
2288 usage_diff();
2289 /* NOTREACHED */
2293 argc -= optind;
2294 argv += optind;
2296 cwd = getcwd(NULL, 0);
2297 if (cwd == NULL) {
2298 error = got_error_from_errno("getcwd");
2299 goto done;
2301 error = got_worktree_open(&worktree, cwd);
2302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2303 goto done;
2304 if (argc <= 1) {
2305 if (worktree == NULL) {
2306 error = got_error(GOT_ERR_NOT_WORKTREE);
2307 goto done;
2309 if (repo_path)
2310 errx(1,
2311 "-r option can't be used when diffing a work tree");
2312 repo_path = strdup(got_worktree_get_repo_path(worktree));
2313 if (repo_path == NULL) {
2314 error = got_error_from_errno("strdup");
2315 goto done;
2317 if (argc == 1) {
2318 error = got_worktree_resolve_path(&path, worktree,
2319 argv[0]);
2320 if (error)
2321 goto done;
2322 } else {
2323 path = strdup("");
2324 if (path == NULL) {
2325 error = got_error_from_errno("strdup");
2326 goto done;
2329 } else if (argc == 2) {
2330 if (diff_staged)
2331 errx(1, "-s option can't be used when diffing "
2332 "objects in repository");
2333 id_str1 = argv[0];
2334 id_str2 = argv[1];
2335 if (worktree && repo_path == NULL) {
2336 repo_path =
2337 strdup(got_worktree_get_repo_path(worktree));
2338 if (repo_path == NULL) {
2339 error = got_error_from_errno("strdup");
2340 goto done;
2343 } else
2344 usage_diff();
2346 if (repo_path == NULL) {
2347 repo_path = getcwd(NULL, 0);
2348 if (repo_path == NULL)
2349 return got_error_from_errno("getcwd");
2352 error = got_repo_open(&repo, repo_path, NULL);
2353 free(repo_path);
2354 if (error != NULL)
2355 goto done;
2357 error = apply_unveil(got_repo_get_path(repo), 1,
2358 worktree ? got_worktree_get_root_path(worktree) : NULL);
2359 if (error)
2360 goto done;
2362 if (argc <= 1) {
2363 struct print_diff_arg arg;
2364 struct got_pathlist_head paths;
2365 char *id_str;
2367 TAILQ_INIT(&paths);
2369 error = got_object_id_str(&id_str,
2370 got_worktree_get_base_commit_id(worktree));
2371 if (error)
2372 goto done;
2373 arg.repo = repo;
2374 arg.worktree = worktree;
2375 arg.diff_context = diff_context;
2376 arg.id_str = id_str;
2377 arg.header_shown = 0;
2378 arg.diff_staged = diff_staged;
2379 arg.ignore_whitespace = ignore_whitespace;
2381 error = got_pathlist_append(&paths, path, NULL);
2382 if (error)
2383 goto done;
2385 error = got_worktree_status(worktree, &paths, repo, print_diff,
2386 &arg, check_cancelled, NULL);
2387 free(id_str);
2388 got_pathlist_free(&paths);
2389 goto done;
2392 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2393 repo);
2394 if (error)
2395 goto done;
2397 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2398 repo);
2399 if (error)
2400 goto done;
2402 error = got_object_get_type(&type1, repo, id1);
2403 if (error)
2404 goto done;
2406 error = got_object_get_type(&type2, repo, id2);
2407 if (error)
2408 goto done;
2410 if (type1 != type2) {
2411 error = got_error(GOT_ERR_OBJ_TYPE);
2412 goto done;
2415 switch (type1) {
2416 case GOT_OBJ_TYPE_BLOB:
2417 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2418 diff_context, ignore_whitespace, repo, stdout);
2419 break;
2420 case GOT_OBJ_TYPE_TREE:
2421 error = got_diff_objects_as_trees(id1, id2, "", "",
2422 diff_context, ignore_whitespace, repo, stdout);
2423 break;
2424 case GOT_OBJ_TYPE_COMMIT:
2425 printf("diff %s %s\n", label1, label2);
2426 error = got_diff_objects_as_commits(id1, id2, diff_context,
2427 ignore_whitespace, repo, stdout);
2428 break;
2429 default:
2430 error = got_error(GOT_ERR_OBJ_TYPE);
2433 done:
2434 free(label1);
2435 free(label2);
2436 free(id1);
2437 free(id2);
2438 free(path);
2439 if (worktree)
2440 got_worktree_close(worktree);
2441 if (repo) {
2442 const struct got_error *repo_error;
2443 repo_error = got_repo_close(repo);
2444 if (error == NULL)
2445 error = repo_error;
2447 return error;
2450 __dead static void
2451 usage_blame(void)
2453 fprintf(stderr,
2454 "usage: %s blame [-c commit] [-r repository-path] path\n",
2455 getprogname());
2456 exit(1);
2459 struct blame_line {
2460 int annotated;
2461 char *id_str;
2462 char *committer;
2463 char datebuf[11]; /* YYYY-MM-DD + NUL */
2466 struct blame_cb_args {
2467 struct blame_line *lines;
2468 int nlines;
2469 int nlines_prec;
2470 int lineno_cur;
2471 off_t *line_offsets;
2472 FILE *f;
2473 struct got_repository *repo;
2476 static const struct got_error *
2477 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2479 const struct got_error *err = NULL;
2480 struct blame_cb_args *a = arg;
2481 struct blame_line *bline;
2482 char *line = NULL;
2483 size_t linesize = 0;
2484 struct got_commit_object *commit = NULL;
2485 off_t offset;
2486 struct tm tm;
2487 time_t committer_time;
2489 if (nlines != a->nlines ||
2490 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2491 return got_error(GOT_ERR_RANGE);
2493 if (sigint_received)
2494 return got_error(GOT_ERR_ITER_COMPLETED);
2496 if (lineno == -1)
2497 return NULL; /* no change in this commit */
2499 /* Annotate this line. */
2500 bline = &a->lines[lineno - 1];
2501 if (bline->annotated)
2502 return NULL;
2503 err = got_object_id_str(&bline->id_str, id);
2504 if (err)
2505 return err;
2507 err = got_object_open_as_commit(&commit, a->repo, id);
2508 if (err)
2509 goto done;
2511 bline->committer = strdup(got_object_commit_get_committer(commit));
2512 if (bline->committer == NULL) {
2513 err = got_error_from_errno("strdup");
2514 goto done;
2517 committer_time = got_object_commit_get_committer_time(commit);
2518 if (localtime_r(&committer_time, &tm) == NULL)
2519 return got_error_from_errno("localtime_r");
2520 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2521 &tm) >= sizeof(bline->datebuf)) {
2522 err = got_error(GOT_ERR_NO_SPACE);
2523 goto done;
2525 bline->annotated = 1;
2527 /* Print lines annotated so far. */
2528 bline = &a->lines[a->lineno_cur - 1];
2529 if (!bline->annotated)
2530 goto done;
2532 offset = a->line_offsets[a->lineno_cur - 1];
2533 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2534 err = got_error_from_errno("fseeko");
2535 goto done;
2538 while (bline->annotated) {
2539 char *smallerthan, *at, *nl, *committer;
2540 size_t len;
2542 if (getline(&line, &linesize, a->f) == -1) {
2543 if (ferror(a->f))
2544 err = got_error_from_errno("getline");
2545 break;
2548 committer = bline->committer;
2549 smallerthan = strchr(committer, '<');
2550 if (smallerthan && smallerthan[1] != '\0')
2551 committer = smallerthan + 1;
2552 at = strchr(committer, '@');
2553 if (at)
2554 *at = '\0';
2555 len = strlen(committer);
2556 if (len >= 9)
2557 committer[8] = '\0';
2559 nl = strchr(line, '\n');
2560 if (nl)
2561 *nl = '\0';
2562 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2563 bline->id_str, bline->datebuf, committer, line);
2565 a->lineno_cur++;
2566 bline = &a->lines[a->lineno_cur - 1];
2568 done:
2569 if (commit)
2570 got_object_commit_close(commit);
2571 free(line);
2572 return err;
2575 static const struct got_error *
2576 cmd_blame(int argc, char *argv[])
2578 const struct got_error *error;
2579 struct got_repository *repo = NULL;
2580 struct got_worktree *worktree = NULL;
2581 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2582 struct got_object_id *obj_id = NULL;
2583 struct got_object_id *commit_id = NULL;
2584 struct got_blob_object *blob = NULL;
2585 char *commit_id_str = NULL;
2586 struct blame_cb_args bca;
2587 int ch, obj_type, i;
2588 size_t filesize;
2590 memset(&bca, 0, sizeof(bca));
2592 #ifndef PROFILE
2593 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2594 NULL) == -1)
2595 err(1, "pledge");
2596 #endif
2598 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2599 switch (ch) {
2600 case 'c':
2601 commit_id_str = optarg;
2602 break;
2603 case 'r':
2604 repo_path = realpath(optarg, NULL);
2605 if (repo_path == NULL)
2606 return got_error_from_errno2("realpath",
2607 optarg);
2608 got_path_strip_trailing_slashes(repo_path);
2609 break;
2610 default:
2611 usage_blame();
2612 /* NOTREACHED */
2616 argc -= optind;
2617 argv += optind;
2619 if (argc == 1)
2620 path = argv[0];
2621 else
2622 usage_blame();
2624 cwd = getcwd(NULL, 0);
2625 if (cwd == NULL) {
2626 error = got_error_from_errno("getcwd");
2627 goto done;
2629 if (repo_path == NULL) {
2630 error = got_worktree_open(&worktree, cwd);
2631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2632 goto done;
2633 else
2634 error = NULL;
2635 if (worktree) {
2636 repo_path =
2637 strdup(got_worktree_get_repo_path(worktree));
2638 if (repo_path == NULL)
2639 error = got_error_from_errno("strdup");
2640 if (error)
2641 goto done;
2642 } else {
2643 repo_path = strdup(cwd);
2644 if (repo_path == NULL) {
2645 error = got_error_from_errno("strdup");
2646 goto done;
2651 error = got_repo_open(&repo, repo_path, NULL);
2652 if (error != NULL)
2653 goto done;
2655 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2656 if (error)
2657 goto done;
2659 if (worktree) {
2660 const char *prefix = got_worktree_get_path_prefix(worktree);
2661 char *p, *worktree_subdir = cwd +
2662 strlen(got_worktree_get_root_path(worktree));
2663 if (asprintf(&p, "%s%s%s%s%s",
2664 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2665 worktree_subdir, worktree_subdir[0] ? "/" : "",
2666 path) == -1) {
2667 error = got_error_from_errno("asprintf");
2668 goto done;
2670 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2671 free(p);
2672 } else {
2673 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2675 if (error)
2676 goto done;
2678 if (commit_id_str == NULL) {
2679 struct got_reference *head_ref;
2680 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2681 if (error != NULL)
2682 goto done;
2683 error = got_ref_resolve(&commit_id, repo, head_ref);
2684 got_ref_close(head_ref);
2685 if (error != NULL)
2686 goto done;
2687 } else {
2688 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2689 if (error)
2690 goto done;
2693 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2694 if (error)
2695 goto done;
2696 if (obj_id == NULL) {
2697 error = got_error(GOT_ERR_NO_OBJ);
2698 goto done;
2701 error = got_object_get_type(&obj_type, repo, obj_id);
2702 if (error)
2703 goto done;
2705 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2706 error = got_error(GOT_ERR_OBJ_TYPE);
2707 goto done;
2710 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2711 if (error)
2712 goto done;
2713 bca.f = got_opentemp();
2714 if (bca.f == NULL) {
2715 error = got_error_from_errno("got_opentemp");
2716 goto done;
2718 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2719 &bca.line_offsets, bca.f, blob);
2720 if (error || bca.nlines == 0)
2721 goto done;
2723 /* Don't include \n at EOF in the blame line count. */
2724 if (bca.line_offsets[bca.nlines - 1] == filesize)
2725 bca.nlines--;
2727 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2728 if (bca.lines == NULL) {
2729 error = got_error_from_errno("calloc");
2730 goto done;
2732 bca.lineno_cur = 1;
2733 bca.nlines_prec = 0;
2734 i = bca.nlines;
2735 while (i > 0) {
2736 i /= 10;
2737 bca.nlines_prec++;
2739 bca.repo = repo;
2741 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2742 check_cancelled, NULL);
2743 if (error)
2744 goto done;
2745 done:
2746 free(in_repo_path);
2747 free(repo_path);
2748 free(cwd);
2749 free(commit_id);
2750 free(obj_id);
2751 if (blob)
2752 got_object_blob_close(blob);
2753 if (worktree)
2754 got_worktree_close(worktree);
2755 if (repo) {
2756 const struct got_error *repo_error;
2757 repo_error = got_repo_close(repo);
2758 if (error == NULL)
2759 error = repo_error;
2761 if (bca.lines) {
2762 for (i = 0; i < bca.nlines; i++) {
2763 struct blame_line *bline = &bca.lines[i];
2764 free(bline->id_str);
2765 free(bline->committer);
2767 free(bca.lines);
2769 free(bca.line_offsets);
2770 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2771 error = got_error_from_errno("fclose");
2772 return error;
2775 __dead static void
2776 usage_tree(void)
2778 fprintf(stderr,
2779 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2780 getprogname());
2781 exit(1);
2784 static void
2785 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2786 const char *root_path)
2788 int is_root_path = (strcmp(path, root_path) == 0);
2789 const char *modestr = "";
2790 mode_t mode = got_tree_entry_get_mode(te);
2792 path += strlen(root_path);
2793 while (path[0] == '/')
2794 path++;
2796 if (got_object_tree_entry_is_submodule(te))
2797 modestr = "$";
2798 else if (S_ISLNK(mode))
2799 modestr = "@";
2800 else if (S_ISDIR(mode))
2801 modestr = "/";
2802 else if (mode & S_IXUSR)
2803 modestr = "*";
2805 printf("%s%s%s%s%s\n", id ? id : "", path,
2806 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2809 static const struct got_error *
2810 print_tree(const char *path, struct got_object_id *commit_id,
2811 int show_ids, int recurse, const char *root_path,
2812 struct got_repository *repo)
2814 const struct got_error *err = NULL;
2815 struct got_object_id *tree_id = NULL;
2816 struct got_tree_object *tree = NULL;
2817 int nentries, i;
2819 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2820 if (err)
2821 goto done;
2823 err = got_object_open_as_tree(&tree, repo, tree_id);
2824 if (err)
2825 goto done;
2826 nentries = got_object_tree_get_nentries(tree);
2827 for (i = 0; i < nentries; i++) {
2828 struct got_tree_entry *te;
2829 char *id = NULL;
2831 if (sigint_received || sigpipe_received)
2832 break;
2834 te = got_object_tree_get_entry(tree, i);
2835 if (show_ids) {
2836 char *id_str;
2837 err = got_object_id_str(&id_str,
2838 got_tree_entry_get_id(te));
2839 if (err)
2840 goto done;
2841 if (asprintf(&id, "%s ", id_str) == -1) {
2842 err = got_error_from_errno("asprintf");
2843 free(id_str);
2844 goto done;
2846 free(id_str);
2848 print_entry(te, id, path, root_path);
2849 free(id);
2851 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2852 char *child_path;
2853 if (asprintf(&child_path, "%s%s%s", path,
2854 path[0] == '/' && path[1] == '\0' ? "" : "/",
2855 got_tree_entry_get_name(te)) == -1) {
2856 err = got_error_from_errno("asprintf");
2857 goto done;
2859 err = print_tree(child_path, commit_id, show_ids, 1,
2860 root_path, repo);
2861 free(child_path);
2862 if (err)
2863 goto done;
2866 done:
2867 if (tree)
2868 got_object_tree_close(tree);
2869 free(tree_id);
2870 return err;
2873 static const struct got_error *
2874 cmd_tree(int argc, char *argv[])
2876 const struct got_error *error;
2877 struct got_repository *repo = NULL;
2878 struct got_worktree *worktree = NULL;
2879 const char *path;
2880 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2881 struct got_object_id *commit_id = NULL;
2882 char *commit_id_str = NULL;
2883 int show_ids = 0, recurse = 0;
2884 int ch;
2886 #ifndef PROFILE
2887 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2888 NULL) == -1)
2889 err(1, "pledge");
2890 #endif
2892 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2893 switch (ch) {
2894 case 'c':
2895 commit_id_str = optarg;
2896 break;
2897 case 'r':
2898 repo_path = realpath(optarg, NULL);
2899 if (repo_path == NULL)
2900 return got_error_from_errno2("realpath",
2901 optarg);
2902 got_path_strip_trailing_slashes(repo_path);
2903 break;
2904 case 'i':
2905 show_ids = 1;
2906 break;
2907 case 'R':
2908 recurse = 1;
2909 break;
2910 default:
2911 usage_tree();
2912 /* NOTREACHED */
2916 argc -= optind;
2917 argv += optind;
2919 if (argc == 1)
2920 path = argv[0];
2921 else if (argc > 1)
2922 usage_tree();
2923 else
2924 path = NULL;
2926 cwd = getcwd(NULL, 0);
2927 if (cwd == NULL) {
2928 error = got_error_from_errno("getcwd");
2929 goto done;
2931 if (repo_path == NULL) {
2932 error = got_worktree_open(&worktree, cwd);
2933 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2934 goto done;
2935 else
2936 error = NULL;
2937 if (worktree) {
2938 repo_path =
2939 strdup(got_worktree_get_repo_path(worktree));
2940 if (repo_path == NULL)
2941 error = got_error_from_errno("strdup");
2942 if (error)
2943 goto done;
2944 } else {
2945 repo_path = strdup(cwd);
2946 if (repo_path == NULL) {
2947 error = got_error_from_errno("strdup");
2948 goto done;
2953 error = got_repo_open(&repo, repo_path, NULL);
2954 if (error != NULL)
2955 goto done;
2957 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2958 if (error)
2959 goto done;
2961 if (path == NULL) {
2962 if (worktree) {
2963 char *p, *worktree_subdir = cwd +
2964 strlen(got_worktree_get_root_path(worktree));
2965 if (asprintf(&p, "%s/%s",
2966 got_worktree_get_path_prefix(worktree),
2967 worktree_subdir) == -1) {
2968 error = got_error_from_errno("asprintf");
2969 goto done;
2971 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2972 free(p);
2973 if (error)
2974 goto done;
2975 } else
2976 path = "/";
2978 if (in_repo_path == NULL) {
2979 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2980 if (error != NULL)
2981 goto done;
2984 if (commit_id_str == NULL) {
2985 struct got_reference *head_ref;
2986 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2987 if (error != NULL)
2988 goto done;
2989 error = got_ref_resolve(&commit_id, repo, head_ref);
2990 got_ref_close(head_ref);
2991 if (error != NULL)
2992 goto done;
2993 } else {
2994 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2995 if (error)
2996 goto done;
2999 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3000 in_repo_path, repo);
3001 done:
3002 free(in_repo_path);
3003 free(repo_path);
3004 free(cwd);
3005 free(commit_id);
3006 if (worktree)
3007 got_worktree_close(worktree);
3008 if (repo) {
3009 const struct got_error *repo_error;
3010 repo_error = got_repo_close(repo);
3011 if (error == NULL)
3012 error = repo_error;
3014 return error;
3017 __dead static void
3018 usage_status(void)
3020 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3021 exit(1);
3024 static const struct got_error *
3025 print_status(void *arg, unsigned char status, unsigned char staged_status,
3026 const char *path, struct got_object_id *blob_id,
3027 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
3029 if (status == staged_status && (status == GOT_STATUS_DELETE))
3030 status = GOT_STATUS_NO_CHANGE;
3031 printf("%c%c %s\n", status, staged_status, path);
3032 return NULL;
3035 static const struct got_error *
3036 cmd_status(int argc, char *argv[])
3038 const struct got_error *error = NULL;
3039 struct got_repository *repo = NULL;
3040 struct got_worktree *worktree = NULL;
3041 char *cwd = NULL;
3042 struct got_pathlist_head paths;
3043 struct got_pathlist_entry *pe;
3044 int ch;
3046 TAILQ_INIT(&paths);
3048 while ((ch = getopt(argc, argv, "")) != -1) {
3049 switch (ch) {
3050 default:
3051 usage_status();
3052 /* NOTREACHED */
3056 argc -= optind;
3057 argv += optind;
3059 #ifndef PROFILE
3060 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3061 NULL) == -1)
3062 err(1, "pledge");
3063 #endif
3064 cwd = getcwd(NULL, 0);
3065 if (cwd == NULL) {
3066 error = got_error_from_errno("getcwd");
3067 goto done;
3070 error = got_worktree_open(&worktree, cwd);
3071 if (error != NULL)
3072 goto done;
3074 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3075 NULL);
3076 if (error != NULL)
3077 goto done;
3079 error = apply_unveil(got_repo_get_path(repo), 1,
3080 got_worktree_get_root_path(worktree));
3081 if (error)
3082 goto done;
3084 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3085 if (error)
3086 goto done;
3088 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3089 check_cancelled, NULL);
3090 done:
3091 TAILQ_FOREACH(pe, &paths, entry)
3092 free((char *)pe->path);
3093 got_pathlist_free(&paths);
3094 free(cwd);
3095 return error;
3098 __dead static void
3099 usage_ref(void)
3101 fprintf(stderr,
3102 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3103 getprogname());
3104 exit(1);
3107 static const struct got_error *
3108 list_refs(struct got_repository *repo)
3110 static const struct got_error *err = NULL;
3111 struct got_reflist_head refs;
3112 struct got_reflist_entry *re;
3114 SIMPLEQ_INIT(&refs);
3115 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3116 if (err)
3117 return err;
3119 SIMPLEQ_FOREACH(re, &refs, entry) {
3120 char *refstr;
3121 refstr = got_ref_to_str(re->ref);
3122 if (refstr == NULL)
3123 return got_error_from_errno("got_ref_to_str");
3124 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3125 free(refstr);
3128 got_ref_list_free(&refs);
3129 return NULL;
3132 static const struct got_error *
3133 delete_ref(struct got_repository *repo, const char *refname)
3135 const struct got_error *err = NULL;
3136 struct got_reference *ref;
3138 err = got_ref_open(&ref, repo, refname, 0);
3139 if (err)
3140 return err;
3142 err = got_ref_delete(ref, repo);
3143 got_ref_close(ref);
3144 return err;
3147 static const struct got_error *
3148 add_ref(struct got_repository *repo, const char *refname, const char *target)
3150 const struct got_error *err = NULL;
3151 struct got_object_id *id;
3152 struct got_reference *ref = NULL;
3155 * Don't let the user create a reference name with a leading '-'.
3156 * While technically a valid reference name, this case is usually
3157 * an unintended typo.
3159 if (refname[0] == '-')
3160 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3162 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3163 repo);
3164 if (err) {
3165 struct got_reference *target_ref;
3167 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3168 return err;
3169 err = got_ref_open(&target_ref, repo, target, 0);
3170 if (err)
3171 return err;
3172 err = got_ref_resolve(&id, repo, target_ref);
3173 got_ref_close(target_ref);
3174 if (err)
3175 return err;
3178 err = got_ref_alloc(&ref, refname, id);
3179 if (err)
3180 goto done;
3182 err = got_ref_write(ref, repo);
3183 done:
3184 if (ref)
3185 got_ref_close(ref);
3186 free(id);
3187 return err;
3190 static const struct got_error *
3191 add_symref(struct got_repository *repo, const char *refname, const char *target)
3193 const struct got_error *err = NULL;
3194 struct got_reference *ref = NULL;
3195 struct got_reference *target_ref = NULL;
3198 * Don't let the user create a reference name with a leading '-'.
3199 * While technically a valid reference name, this case is usually
3200 * an unintended typo.
3202 if (refname[0] == '-')
3203 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3205 err = got_ref_open(&target_ref, repo, target, 0);
3206 if (err)
3207 return err;
3209 err = got_ref_alloc_symref(&ref, refname, target_ref);
3210 if (err)
3211 goto done;
3213 err = got_ref_write(ref, repo);
3214 done:
3215 if (target_ref)
3216 got_ref_close(target_ref);
3217 if (ref)
3218 got_ref_close(ref);
3219 return err;
3222 static const struct got_error *
3223 cmd_ref(int argc, char *argv[])
3225 const struct got_error *error = NULL;
3226 struct got_repository *repo = NULL;
3227 struct got_worktree *worktree = NULL;
3228 char *cwd = NULL, *repo_path = NULL;
3229 int ch, do_list = 0, create_symref = 0;
3230 const char *delref = NULL;
3232 /* TODO: Add -s option for adding symbolic references. */
3233 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3234 switch (ch) {
3235 case 'd':
3236 delref = optarg;
3237 break;
3238 case 'r':
3239 repo_path = realpath(optarg, NULL);
3240 if (repo_path == NULL)
3241 return got_error_from_errno2("realpath",
3242 optarg);
3243 got_path_strip_trailing_slashes(repo_path);
3244 break;
3245 case 'l':
3246 do_list = 1;
3247 break;
3248 case 's':
3249 create_symref = 1;
3250 break;
3251 default:
3252 usage_ref();
3253 /* NOTREACHED */
3257 if (do_list && delref)
3258 errx(1, "-l and -d options are mutually exclusive\n");
3260 argc -= optind;
3261 argv += optind;
3263 if (do_list || delref) {
3264 if (create_symref)
3265 errx(1, "-s option cannot be used together with the "
3266 "-l or -d options");
3267 if (argc > 0)
3268 usage_ref();
3269 } else if (argc != 2)
3270 usage_ref();
3272 #ifndef PROFILE
3273 if (do_list) {
3274 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3275 NULL) == -1)
3276 err(1, "pledge");
3277 } else {
3278 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3279 "sendfd unveil", NULL) == -1)
3280 err(1, "pledge");
3282 #endif
3283 cwd = getcwd(NULL, 0);
3284 if (cwd == NULL) {
3285 error = got_error_from_errno("getcwd");
3286 goto done;
3289 if (repo_path == NULL) {
3290 error = got_worktree_open(&worktree, cwd);
3291 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3292 goto done;
3293 else
3294 error = NULL;
3295 if (worktree) {
3296 repo_path =
3297 strdup(got_worktree_get_repo_path(worktree));
3298 if (repo_path == NULL)
3299 error = got_error_from_errno("strdup");
3300 if (error)
3301 goto done;
3302 } else {
3303 repo_path = strdup(cwd);
3304 if (repo_path == NULL) {
3305 error = got_error_from_errno("strdup");
3306 goto done;
3311 error = got_repo_open(&repo, repo_path, NULL);
3312 if (error != NULL)
3313 goto done;
3315 error = apply_unveil(got_repo_get_path(repo), do_list,
3316 worktree ? got_worktree_get_root_path(worktree) : NULL);
3317 if (error)
3318 goto done;
3320 if (do_list)
3321 error = list_refs(repo);
3322 else if (delref)
3323 error = delete_ref(repo, delref);
3324 else if (create_symref)
3325 error = add_symref(repo, argv[0], argv[1]);
3326 else
3327 error = add_ref(repo, argv[0], argv[1]);
3328 done:
3329 if (repo)
3330 got_repo_close(repo);
3331 if (worktree)
3332 got_worktree_close(worktree);
3333 free(cwd);
3334 free(repo_path);
3335 return error;
3338 __dead static void
3339 usage_branch(void)
3341 fprintf(stderr,
3342 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3343 "[name]\n", getprogname());
3344 exit(1);
3347 static const struct got_error *
3348 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3349 struct got_reference *ref)
3351 const struct got_error *err = NULL;
3352 const char *refname, *marker = " ";
3353 char *refstr;
3355 refname = got_ref_get_name(ref);
3356 if (worktree && strcmp(refname,
3357 got_worktree_get_head_ref_name(worktree)) == 0) {
3358 struct got_object_id *id = NULL;
3360 err = got_ref_resolve(&id, repo, ref);
3361 if (err)
3362 return err;
3363 if (got_object_id_cmp(id,
3364 got_worktree_get_base_commit_id(worktree)) == 0)
3365 marker = "* ";
3366 else
3367 marker = "~ ";
3368 free(id);
3371 if (strncmp(refname, "refs/heads/", 11) == 0)
3372 refname += 11;
3373 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3374 refname += 18;
3376 refstr = got_ref_to_str(ref);
3377 if (refstr == NULL)
3378 return got_error_from_errno("got_ref_to_str");
3380 printf("%s%s: %s\n", marker, refname, refstr);
3381 free(refstr);
3382 return NULL;
3385 static const struct got_error *
3386 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3388 const char *refname;
3390 if (worktree == NULL)
3391 return got_error(GOT_ERR_NOT_WORKTREE);
3393 refname = got_worktree_get_head_ref_name(worktree);
3395 if (strncmp(refname, "refs/heads/", 11) == 0)
3396 refname += 11;
3397 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3398 refname += 18;
3400 printf("%s\n", refname);
3402 return NULL;
3405 static const struct got_error *
3406 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3408 static const struct got_error *err = NULL;
3409 struct got_reflist_head refs;
3410 struct got_reflist_entry *re;
3411 struct got_reference *temp_ref = NULL;
3412 int rebase_in_progress, histedit_in_progress;
3414 SIMPLEQ_INIT(&refs);
3416 if (worktree) {
3417 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3418 worktree);
3419 if (err)
3420 return err;
3422 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3423 worktree);
3424 if (err)
3425 return err;
3427 if (rebase_in_progress || histedit_in_progress) {
3428 err = got_ref_open(&temp_ref, repo,
3429 got_worktree_get_head_ref_name(worktree), 0);
3430 if (err)
3431 return err;
3432 list_branch(repo, worktree, temp_ref);
3433 got_ref_close(temp_ref);
3437 err = got_ref_list(&refs, repo, "refs/heads",
3438 got_ref_cmp_by_name, NULL);
3439 if (err)
3440 return err;
3442 SIMPLEQ_FOREACH(re, &refs, entry)
3443 list_branch(repo, worktree, re->ref);
3445 got_ref_list_free(&refs);
3446 return NULL;
3449 static const struct got_error *
3450 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3451 const char *branch_name)
3453 const struct got_error *err = NULL;
3454 struct got_reference *ref = NULL;
3455 char *refname;
3457 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3458 return got_error_from_errno("asprintf");
3460 err = got_ref_open(&ref, repo, refname, 0);
3461 if (err)
3462 goto done;
3464 if (worktree &&
3465 strcmp(got_worktree_get_head_ref_name(worktree),
3466 got_ref_get_name(ref)) == 0) {
3467 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3468 "will not delete this work tree's current branch");
3469 goto done;
3472 err = got_ref_delete(ref, repo);
3473 done:
3474 if (ref)
3475 got_ref_close(ref);
3476 free(refname);
3477 return err;
3480 static const struct got_error *
3481 add_branch(struct got_repository *repo, const char *branch_name,
3482 struct got_object_id *base_commit_id)
3484 const struct got_error *err = NULL;
3485 struct got_reference *ref = NULL;
3486 char *base_refname = NULL, *refname = NULL;
3489 * Don't let the user create a branch name with a leading '-'.
3490 * While technically a valid reference name, this case is usually
3491 * an unintended typo.
3493 if (branch_name[0] == '-')
3494 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3496 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3497 err = got_error_from_errno("asprintf");
3498 goto done;
3501 err = got_ref_open(&ref, repo, refname, 0);
3502 if (err == NULL) {
3503 err = got_error(GOT_ERR_BRANCH_EXISTS);
3504 goto done;
3505 } else if (err->code != GOT_ERR_NOT_REF)
3506 goto done;
3508 err = got_ref_alloc(&ref, refname, base_commit_id);
3509 if (err)
3510 goto done;
3512 err = got_ref_write(ref, repo);
3513 done:
3514 if (ref)
3515 got_ref_close(ref);
3516 free(base_refname);
3517 free(refname);
3518 return err;
3521 static const struct got_error *
3522 cmd_branch(int argc, char *argv[])
3524 const struct got_error *error = NULL;
3525 struct got_repository *repo = NULL;
3526 struct got_worktree *worktree = NULL;
3527 char *cwd = NULL, *repo_path = NULL;
3528 int ch, do_list = 0, do_show = 0;
3529 const char *delref = NULL, *commit_id_arg = NULL;
3531 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3532 switch (ch) {
3533 case 'c':
3534 commit_id_arg = optarg;
3535 break;
3536 case 'd':
3537 delref = optarg;
3538 break;
3539 case 'r':
3540 repo_path = realpath(optarg, NULL);
3541 if (repo_path == NULL)
3542 return got_error_from_errno2("realpath",
3543 optarg);
3544 got_path_strip_trailing_slashes(repo_path);
3545 break;
3546 case 'l':
3547 do_list = 1;
3548 break;
3549 default:
3550 usage_branch();
3551 /* NOTREACHED */
3555 if (do_list && delref)
3556 errx(1, "-l and -d options are mutually exclusive\n");
3558 argc -= optind;
3559 argv += optind;
3561 if (!do_list && !delref && argc == 0)
3562 do_show = 1;
3564 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3565 errx(1, "-c option can only be used when creating a branch");
3567 if (do_list || delref) {
3568 if (argc > 0)
3569 usage_branch();
3570 } else if (!do_show && argc != 1)
3571 usage_branch();
3573 #ifndef PROFILE
3574 if (do_list || do_show) {
3575 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3576 NULL) == -1)
3577 err(1, "pledge");
3578 } else {
3579 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3580 "sendfd unveil", NULL) == -1)
3581 err(1, "pledge");
3583 #endif
3584 cwd = getcwd(NULL, 0);
3585 if (cwd == NULL) {
3586 error = got_error_from_errno("getcwd");
3587 goto done;
3590 if (repo_path == NULL) {
3591 error = got_worktree_open(&worktree, cwd);
3592 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3593 goto done;
3594 else
3595 error = NULL;
3596 if (worktree) {
3597 repo_path =
3598 strdup(got_worktree_get_repo_path(worktree));
3599 if (repo_path == NULL)
3600 error = got_error_from_errno("strdup");
3601 if (error)
3602 goto done;
3603 } else {
3604 repo_path = strdup(cwd);
3605 if (repo_path == NULL) {
3606 error = got_error_from_errno("strdup");
3607 goto done;
3612 error = got_repo_open(&repo, repo_path, NULL);
3613 if (error != NULL)
3614 goto done;
3616 error = apply_unveil(got_repo_get_path(repo), do_list,
3617 worktree ? got_worktree_get_root_path(worktree) : NULL);
3618 if (error)
3619 goto done;
3621 if (do_show)
3622 error = show_current_branch(repo, worktree);
3623 else if (do_list)
3624 error = list_branches(repo, worktree);
3625 else if (delref)
3626 error = delete_branch(repo, worktree, delref);
3627 else {
3628 struct got_object_id *commit_id;
3629 if (commit_id_arg == NULL)
3630 commit_id_arg = worktree ?
3631 got_worktree_get_head_ref_name(worktree) :
3632 GOT_REF_HEAD;
3633 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3634 if (error)
3635 goto done;
3636 error = add_branch(repo, argv[0], commit_id);
3637 free(commit_id);
3639 done:
3640 if (repo)
3641 got_repo_close(repo);
3642 if (worktree)
3643 got_worktree_close(worktree);
3644 free(cwd);
3645 free(repo_path);
3646 return error;
3650 __dead static void
3651 usage_tag(void)
3653 fprintf(stderr,
3654 "usage: %s tag [-r repository] | -l | "
3655 "[-m message] name [commit]\n", getprogname());
3656 exit(1);
3659 #if 0
3660 static const struct got_error *
3661 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3663 const struct got_error *err = NULL;
3664 struct got_reflist_entry *re, *se, *new;
3665 struct got_object_id *re_id, *se_id;
3666 struct got_tag_object *re_tag, *se_tag;
3667 time_t re_time, se_time;
3669 SIMPLEQ_FOREACH(re, tags, entry) {
3670 se = SIMPLEQ_FIRST(sorted);
3671 if (se == NULL) {
3672 err = got_reflist_entry_dup(&new, re);
3673 if (err)
3674 return err;
3675 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3676 continue;
3677 } else {
3678 err = got_ref_resolve(&re_id, repo, re->ref);
3679 if (err)
3680 break;
3681 err = got_object_open_as_tag(&re_tag, repo, re_id);
3682 free(re_id);
3683 if (err)
3684 break;
3685 re_time = got_object_tag_get_tagger_time(re_tag);
3686 got_object_tag_close(re_tag);
3689 while (se) {
3690 err = got_ref_resolve(&se_id, repo, re->ref);
3691 if (err)
3692 break;
3693 err = got_object_open_as_tag(&se_tag, repo, se_id);
3694 free(se_id);
3695 if (err)
3696 break;
3697 se_time = got_object_tag_get_tagger_time(se_tag);
3698 got_object_tag_close(se_tag);
3700 if (se_time > re_time) {
3701 err = got_reflist_entry_dup(&new, re);
3702 if (err)
3703 return err;
3704 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3705 break;
3707 se = SIMPLEQ_NEXT(se, entry);
3708 continue;
3711 done:
3712 return err;
3714 #endif
3716 static const struct got_error *
3717 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3718 struct got_reference *ref2)
3720 const struct got_error *err = NULL;
3721 struct got_repository *repo = arg;
3722 struct got_object_id *id1, *id2 = NULL;
3723 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3724 time_t time1, time2;
3726 *cmp = 0;
3728 err = got_ref_resolve(&id1, repo, ref1);
3729 if (err)
3730 return err;
3731 err = got_object_open_as_tag(&tag1, repo, id1);
3732 if (err)
3733 goto done;
3735 err = got_ref_resolve(&id2, repo, ref2);
3736 if (err)
3737 goto done;
3738 err = got_object_open_as_tag(&tag2, repo, id2);
3739 if (err)
3740 goto done;
3742 time1 = got_object_tag_get_tagger_time(tag1);
3743 time2 = got_object_tag_get_tagger_time(tag2);
3745 /* Put latest tags first. */
3746 if (time1 < time2)
3747 *cmp = 1;
3748 else if (time1 > time2)
3749 *cmp = -1;
3750 else
3751 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3752 done:
3753 free(id1);
3754 free(id2);
3755 if (tag1)
3756 got_object_tag_close(tag1);
3757 if (tag2)
3758 got_object_tag_close(tag2);
3759 return err;
3762 static const struct got_error *
3763 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3765 static const struct got_error *err = NULL;
3766 struct got_reflist_head refs;
3767 struct got_reflist_entry *re;
3769 SIMPLEQ_INIT(&refs);
3771 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3772 if (err)
3773 return err;
3775 SIMPLEQ_FOREACH(re, &refs, entry) {
3776 const char *refname;
3777 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3778 char datebuf[26];
3779 time_t tagger_time;
3780 struct got_object_id *id;
3781 struct got_tag_object *tag;
3783 refname = got_ref_get_name(re->ref);
3784 if (strncmp(refname, "refs/tags/", 10) != 0)
3785 continue;
3786 refname += 10;
3787 refstr = got_ref_to_str(re->ref);
3788 if (refstr == NULL) {
3789 err = got_error_from_errno("got_ref_to_str");
3790 break;
3792 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3793 free(refstr);
3795 err = got_ref_resolve(&id, repo, re->ref);
3796 if (err)
3797 break;
3798 err = got_object_open_as_tag(&tag, repo, id);
3799 free(id);
3800 if (err)
3801 break;
3802 printf("from: %s\n", got_object_tag_get_tagger(tag));
3803 tagger_time = got_object_tag_get_tagger_time(tag);
3804 datestr = get_datestr(&tagger_time, datebuf);
3805 if (datestr)
3806 printf("date: %s UTC\n", datestr);
3807 err = got_object_id_str(&id_str,
3808 got_object_tag_get_object_id(tag));
3809 if (err)
3810 break;
3811 switch (got_object_tag_get_object_type(tag)) {
3812 case GOT_OBJ_TYPE_BLOB:
3813 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3814 break;
3815 case GOT_OBJ_TYPE_TREE:
3816 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3817 break;
3818 case GOT_OBJ_TYPE_COMMIT:
3819 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3820 break;
3821 case GOT_OBJ_TYPE_TAG:
3822 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3823 break;
3824 default:
3825 break;
3827 free(id_str);
3828 tagmsg0 = strdup(got_object_tag_get_message(tag));
3829 got_object_tag_close(tag);
3830 if (tagmsg0 == NULL) {
3831 err = got_error_from_errno("strdup");
3832 break;
3835 tagmsg = tagmsg0;
3836 do {
3837 line = strsep(&tagmsg, "\n");
3838 if (line)
3839 printf(" %s\n", line);
3840 } while (line);
3841 free(tagmsg0);
3844 got_ref_list_free(&refs);
3845 return NULL;
3848 static const struct got_error *
3849 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3850 const char *tag_name, const char *repo_path)
3852 const struct got_error *err = NULL;
3853 char *template = NULL, *initial_content = NULL;
3854 char *editor = NULL;
3855 int fd = -1;
3857 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3858 err = got_error_from_errno("asprintf");
3859 goto done;
3862 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3863 commit_id_str, tag_name) == -1) {
3864 err = got_error_from_errno("asprintf");
3865 goto done;
3868 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3869 if (err)
3870 goto done;
3872 dprintf(fd, initial_content);
3873 close(fd);
3875 err = get_editor(&editor);
3876 if (err)
3877 goto done;
3878 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3879 done:
3880 free(initial_content);
3881 free(template);
3882 free(editor);
3884 /* Editor is done; we can now apply unveil(2) */
3885 if (err == NULL) {
3886 err = apply_unveil(repo_path, 0, NULL);
3887 if (err) {
3888 free(*tagmsg);
3889 *tagmsg = NULL;
3892 return err;
3895 static const struct got_error *
3896 add_tag(struct got_repository *repo, const char *tag_name,
3897 const char *commit_arg, const char *tagmsg_arg)
3899 const struct got_error *err = NULL;
3900 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3901 char *label = NULL, *commit_id_str = NULL;
3902 struct got_reference *ref = NULL;
3903 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3904 char *tagmsg_path = NULL, *tag_id_str = NULL;
3905 int preserve_tagmsg = 0;
3908 * Don't let the user create a tag name with a leading '-'.
3909 * While technically a valid reference name, this case is usually
3910 * an unintended typo.
3912 if (tag_name[0] == '-')
3913 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3915 err = get_author(&tagger, repo);
3916 if (err)
3917 return err;
3919 err = match_object_id(&commit_id, &label, commit_arg,
3920 GOT_OBJ_TYPE_COMMIT, 1, repo);
3921 if (err)
3922 goto done;
3924 err = got_object_id_str(&commit_id_str, commit_id);
3925 if (err)
3926 goto done;
3928 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3929 refname = strdup(tag_name);
3930 if (refname == NULL) {
3931 err = got_error_from_errno("strdup");
3932 goto done;
3934 tag_name += 10;
3935 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3936 err = got_error_from_errno("asprintf");
3937 goto done;
3940 err = got_ref_open(&ref, repo, refname, 0);
3941 if (err == NULL) {
3942 err = got_error(GOT_ERR_TAG_EXISTS);
3943 goto done;
3944 } else if (err->code != GOT_ERR_NOT_REF)
3945 goto done;
3947 if (tagmsg_arg == NULL) {
3948 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3949 tag_name, got_repo_get_path(repo));
3950 if (err) {
3951 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3952 tagmsg_path != NULL)
3953 preserve_tagmsg = 1;
3954 goto done;
3958 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3959 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3960 if (err) {
3961 if (tagmsg_path)
3962 preserve_tagmsg = 1;
3963 goto done;
3966 err = got_ref_alloc(&ref, refname, tag_id);
3967 if (err) {
3968 if (tagmsg_path)
3969 preserve_tagmsg = 1;
3970 goto done;
3973 err = got_ref_write(ref, repo);
3974 if (err) {
3975 if (tagmsg_path)
3976 preserve_tagmsg = 1;
3977 goto done;
3980 err = got_object_id_str(&tag_id_str, tag_id);
3981 if (err) {
3982 if (tagmsg_path)
3983 preserve_tagmsg = 1;
3984 goto done;
3986 printf("Created tag %s\n", tag_id_str);
3987 done:
3988 if (preserve_tagmsg) {
3989 fprintf(stderr, "%s: tag message preserved in %s\n",
3990 getprogname(), tagmsg_path);
3991 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3992 err = got_error_from_errno2("unlink", tagmsg_path);
3993 free(tag_id_str);
3994 if (ref)
3995 got_ref_close(ref);
3996 free(commit_id);
3997 free(commit_id_str);
3998 free(refname);
3999 free(tagmsg);
4000 free(tagmsg_path);
4001 free(tagger);
4002 return err;
4005 static const struct got_error *
4006 cmd_tag(int argc, char *argv[])
4008 const struct got_error *error = NULL;
4009 struct got_repository *repo = NULL;
4010 struct got_worktree *worktree = NULL;
4011 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4012 char *gitconfig_path = NULL;
4013 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4014 int ch, do_list = 0;
4016 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4017 switch (ch) {
4018 case 'm':
4019 tagmsg = optarg;
4020 break;
4021 case 'r':
4022 repo_path = realpath(optarg, NULL);
4023 if (repo_path == NULL)
4024 return got_error_from_errno2("realpath",
4025 optarg);
4026 got_path_strip_trailing_slashes(repo_path);
4027 break;
4028 case 'l':
4029 do_list = 1;
4030 break;
4031 default:
4032 usage_tag();
4033 /* NOTREACHED */
4037 argc -= optind;
4038 argv += optind;
4040 if (do_list) {
4041 if (tagmsg)
4042 errx(1, "-l and -m options are mutually exclusive\n");
4043 if (argc > 0)
4044 usage_tag();
4045 } else if (argc < 1 || argc > 2)
4046 usage_tag();
4047 else if (argc > 1)
4048 commit_id_arg = argv[1];
4049 tag_name = argv[0];
4051 #ifndef PROFILE
4052 if (do_list) {
4053 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4054 NULL) == -1)
4055 err(1, "pledge");
4056 } else {
4057 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4058 "sendfd unveil", NULL) == -1)
4059 err(1, "pledge");
4061 #endif
4062 cwd = getcwd(NULL, 0);
4063 if (cwd == NULL) {
4064 error = got_error_from_errno("getcwd");
4065 goto done;
4068 if (repo_path == NULL) {
4069 error = got_worktree_open(&worktree, cwd);
4070 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4071 goto done;
4072 else
4073 error = NULL;
4074 if (worktree) {
4075 repo_path =
4076 strdup(got_worktree_get_repo_path(worktree));
4077 if (repo_path == NULL)
4078 error = got_error_from_errno("strdup");
4079 if (error)
4080 goto done;
4081 } else {
4082 repo_path = strdup(cwd);
4083 if (repo_path == NULL) {
4084 error = got_error_from_errno("strdup");
4085 goto done;
4090 if (do_list) {
4091 error = got_repo_open(&repo, repo_path, NULL);
4092 if (error != NULL)
4093 goto done;
4094 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4095 if (error)
4096 goto done;
4097 error = list_tags(repo, worktree);
4098 } else {
4099 error = get_gitconfig_path(&gitconfig_path);
4100 if (error)
4101 goto done;
4102 error = got_repo_open(&repo, repo_path, gitconfig_path);
4103 if (error != NULL)
4104 goto done;
4106 if (tagmsg) {
4107 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4108 if (error)
4109 goto done;
4112 if (commit_id_arg == NULL) {
4113 struct got_reference *head_ref;
4114 struct got_object_id *commit_id;
4115 error = got_ref_open(&head_ref, repo,
4116 worktree ? got_worktree_get_head_ref_name(worktree)
4117 : GOT_REF_HEAD, 0);
4118 if (error)
4119 goto done;
4120 error = got_ref_resolve(&commit_id, repo, head_ref);
4121 got_ref_close(head_ref);
4122 if (error)
4123 goto done;
4124 error = got_object_id_str(&commit_id_str, commit_id);
4125 free(commit_id);
4126 if (error)
4127 goto done;
4130 error = add_tag(repo, tag_name,
4131 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4133 done:
4134 if (repo)
4135 got_repo_close(repo);
4136 if (worktree)
4137 got_worktree_close(worktree);
4138 free(cwd);
4139 free(repo_path);
4140 free(gitconfig_path);
4141 free(commit_id_str);
4142 return error;
4145 __dead static void
4146 usage_add(void)
4148 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4149 exit(1);
4152 static const struct got_error *
4153 add_progress(void *arg, unsigned char status, const char *path)
4155 while (path[0] == '/')
4156 path++;
4157 printf("%c %s\n", status, path);
4158 return NULL;
4161 static const struct got_error *
4162 cmd_add(int argc, char *argv[])
4164 const struct got_error *error = NULL;
4165 struct got_repository *repo = NULL;
4166 struct got_worktree *worktree = NULL;
4167 char *cwd = NULL;
4168 struct got_pathlist_head paths;
4169 struct got_pathlist_entry *pe;
4170 int ch, can_recurse = 0;
4172 TAILQ_INIT(&paths);
4174 while ((ch = getopt(argc, argv, "R")) != -1) {
4175 switch (ch) {
4176 case 'R':
4177 can_recurse = 1;
4178 break;
4179 default:
4180 usage_add();
4181 /* NOTREACHED */
4185 argc -= optind;
4186 argv += optind;
4188 #ifndef PROFILE
4189 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4190 NULL) == -1)
4191 err(1, "pledge");
4192 #endif
4193 if (argc < 1)
4194 usage_add();
4196 cwd = getcwd(NULL, 0);
4197 if (cwd == NULL) {
4198 error = got_error_from_errno("getcwd");
4199 goto done;
4202 error = got_worktree_open(&worktree, cwd);
4203 if (error)
4204 goto done;
4206 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4207 NULL);
4208 if (error != NULL)
4209 goto done;
4211 error = apply_unveil(got_repo_get_path(repo), 1,
4212 got_worktree_get_root_path(worktree));
4213 if (error)
4214 goto done;
4216 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4217 if (error)
4218 goto done;
4220 if (!can_recurse) {
4221 char *ondisk_path;
4222 struct stat sb;
4223 TAILQ_FOREACH(pe, &paths, entry) {
4224 if (asprintf(&ondisk_path, "%s/%s",
4225 got_worktree_get_root_path(worktree),
4226 pe->path) == -1) {
4227 error = got_error_from_errno("asprintf");
4228 goto done;
4230 if (lstat(ondisk_path, &sb) == -1) {
4231 if (errno == ENOENT) {
4232 free(ondisk_path);
4233 continue;
4235 error = got_error_from_errno2("lstat",
4236 ondisk_path);
4237 free(ondisk_path);
4238 goto done;
4240 free(ondisk_path);
4241 if (S_ISDIR(sb.st_mode)) {
4242 error = got_error_msg(GOT_ERR_BAD_PATH,
4243 "adding directories requires -R option");
4244 goto done;
4248 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4249 NULL, repo);
4250 done:
4251 if (repo)
4252 got_repo_close(repo);
4253 if (worktree)
4254 got_worktree_close(worktree);
4255 TAILQ_FOREACH(pe, &paths, entry)
4256 free((char *)pe->path);
4257 got_pathlist_free(&paths);
4258 free(cwd);
4259 return error;
4262 __dead static void
4263 usage_remove(void)
4265 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4266 exit(1);
4269 static const struct got_error *
4270 print_remove_status(void *arg, unsigned char status,
4271 unsigned char staged_status, const char *path,
4272 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4273 struct got_object_id *commit_id)
4275 if (status == GOT_STATUS_NONEXISTENT)
4276 return NULL;
4277 if (status == staged_status && (status == GOT_STATUS_DELETE))
4278 status = GOT_STATUS_NO_CHANGE;
4279 printf("%c%c %s\n", status, staged_status, path);
4280 return NULL;
4283 static const struct got_error *
4284 cmd_remove(int argc, char *argv[])
4286 const struct got_error *error = NULL;
4287 struct got_worktree *worktree = NULL;
4288 struct got_repository *repo = NULL;
4289 char *cwd = NULL;
4290 struct got_pathlist_head paths;
4291 struct got_pathlist_entry *pe;
4292 int ch, delete_local_mods = 0;
4294 TAILQ_INIT(&paths);
4296 while ((ch = getopt(argc, argv, "f")) != -1) {
4297 switch (ch) {
4298 case 'f':
4299 delete_local_mods = 1;
4300 break;
4301 default:
4302 usage_add();
4303 /* NOTREACHED */
4307 argc -= optind;
4308 argv += optind;
4310 #ifndef PROFILE
4311 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4312 NULL) == -1)
4313 err(1, "pledge");
4314 #endif
4315 if (argc < 1)
4316 usage_remove();
4318 cwd = getcwd(NULL, 0);
4319 if (cwd == NULL) {
4320 error = got_error_from_errno("getcwd");
4321 goto done;
4323 error = got_worktree_open(&worktree, cwd);
4324 if (error)
4325 goto done;
4327 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4328 NULL);
4329 if (error)
4330 goto done;
4332 error = apply_unveil(got_repo_get_path(repo), 1,
4333 got_worktree_get_root_path(worktree));
4334 if (error)
4335 goto done;
4337 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4338 if (error)
4339 goto done;
4341 error = got_worktree_schedule_delete(worktree, &paths,
4342 delete_local_mods, print_remove_status, NULL, repo);
4343 if (error)
4344 goto done;
4345 done:
4346 if (repo)
4347 got_repo_close(repo);
4348 if (worktree)
4349 got_worktree_close(worktree);
4350 TAILQ_FOREACH(pe, &paths, entry)
4351 free((char *)pe->path);
4352 got_pathlist_free(&paths);
4353 free(cwd);
4354 return error;
4357 __dead static void
4358 usage_revert(void)
4360 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4361 "path ...\n", getprogname());
4362 exit(1);
4365 static const struct got_error *
4366 revert_progress(void *arg, unsigned char status, const char *path)
4368 while (path[0] == '/')
4369 path++;
4370 printf("%c %s\n", status, path);
4371 return NULL;
4374 struct choose_patch_arg {
4375 FILE *patch_script_file;
4376 const char *action;
4379 static const struct got_error *
4380 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4381 int nchanges, const char *action)
4383 char *line = NULL;
4384 size_t linesize = 0;
4385 ssize_t linelen;
4387 switch (status) {
4388 case GOT_STATUS_ADD:
4389 printf("A %s\n%s this addition? [y/n] ", path, action);
4390 break;
4391 case GOT_STATUS_DELETE:
4392 printf("D %s\n%s this deletion? [y/n] ", path, action);
4393 break;
4394 case GOT_STATUS_MODIFY:
4395 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4396 return got_error_from_errno("fseek");
4397 printf(GOT_COMMIT_SEP_STR);
4398 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4399 printf("%s", line);
4400 if (ferror(patch_file))
4401 return got_error_from_errno("getline");
4402 printf(GOT_COMMIT_SEP_STR);
4403 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4404 path, n, nchanges, action);
4405 break;
4406 default:
4407 return got_error_path(path, GOT_ERR_FILE_STATUS);
4410 return NULL;
4413 static const struct got_error *
4414 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4415 FILE *patch_file, int n, int nchanges)
4417 const struct got_error *err = NULL;
4418 char *line = NULL;
4419 size_t linesize = 0;
4420 ssize_t linelen;
4421 int resp = ' ';
4422 struct choose_patch_arg *a = arg;
4424 *choice = GOT_PATCH_CHOICE_NONE;
4426 if (a->patch_script_file) {
4427 char *nl;
4428 err = show_change(status, path, patch_file, n, nchanges,
4429 a->action);
4430 if (err)
4431 return err;
4432 linelen = getline(&line, &linesize, a->patch_script_file);
4433 if (linelen == -1) {
4434 if (ferror(a->patch_script_file))
4435 return got_error_from_errno("getline");
4436 return NULL;
4438 nl = strchr(line, '\n');
4439 if (nl)
4440 *nl = '\0';
4441 if (strcmp(line, "y") == 0) {
4442 *choice = GOT_PATCH_CHOICE_YES;
4443 printf("y\n");
4444 } else if (strcmp(line, "n") == 0) {
4445 *choice = GOT_PATCH_CHOICE_NO;
4446 printf("n\n");
4447 } else if (strcmp(line, "q") == 0 &&
4448 status == GOT_STATUS_MODIFY) {
4449 *choice = GOT_PATCH_CHOICE_QUIT;
4450 printf("q\n");
4451 } else
4452 printf("invalid response '%s'\n", line);
4453 free(line);
4454 return NULL;
4457 while (resp != 'y' && resp != 'n' && resp != 'q') {
4458 err = show_change(status, path, patch_file, n, nchanges,
4459 a->action);
4460 if (err)
4461 return err;
4462 resp = getchar();
4463 if (resp == '\n')
4464 resp = getchar();
4465 if (status == GOT_STATUS_MODIFY) {
4466 if (resp != 'y' && resp != 'n' && resp != 'q') {
4467 printf("invalid response '%c'\n", resp);
4468 resp = ' ';
4470 } else if (resp != 'y' && resp != 'n') {
4471 printf("invalid response '%c'\n", resp);
4472 resp = ' ';
4476 if (resp == 'y')
4477 *choice = GOT_PATCH_CHOICE_YES;
4478 else if (resp == 'n')
4479 *choice = GOT_PATCH_CHOICE_NO;
4480 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4481 *choice = GOT_PATCH_CHOICE_QUIT;
4483 return NULL;
4487 static const struct got_error *
4488 cmd_revert(int argc, char *argv[])
4490 const struct got_error *error = NULL;
4491 struct got_worktree *worktree = NULL;
4492 struct got_repository *repo = NULL;
4493 char *cwd = NULL, *path = NULL;
4494 struct got_pathlist_head paths;
4495 struct got_pathlist_entry *pe;
4496 int ch, can_recurse = 0, pflag = 0;
4497 FILE *patch_script_file = NULL;
4498 const char *patch_script_path = NULL;
4499 struct choose_patch_arg cpa;
4501 TAILQ_INIT(&paths);
4503 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4504 switch (ch) {
4505 case 'p':
4506 pflag = 1;
4507 break;
4508 case 'F':
4509 patch_script_path = optarg;
4510 break;
4511 case 'R':
4512 can_recurse = 1;
4513 break;
4514 default:
4515 usage_revert();
4516 /* NOTREACHED */
4520 argc -= optind;
4521 argv += optind;
4523 #ifndef PROFILE
4524 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4525 "unveil", NULL) == -1)
4526 err(1, "pledge");
4527 #endif
4528 if (argc < 1)
4529 usage_revert();
4530 if (patch_script_path && !pflag)
4531 errx(1, "-F option can only be used together with -p option");
4533 cwd = getcwd(NULL, 0);
4534 if (cwd == NULL) {
4535 error = got_error_from_errno("getcwd");
4536 goto done;
4538 error = got_worktree_open(&worktree, cwd);
4539 if (error)
4540 goto done;
4542 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4543 NULL);
4544 if (error != NULL)
4545 goto done;
4547 if (patch_script_path) {
4548 patch_script_file = fopen(patch_script_path, "r");
4549 if (patch_script_file == NULL) {
4550 error = got_error_from_errno2("fopen",
4551 patch_script_path);
4552 goto done;
4555 error = apply_unveil(got_repo_get_path(repo), 1,
4556 got_worktree_get_root_path(worktree));
4557 if (error)
4558 goto done;
4560 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4561 if (error)
4562 goto done;
4564 if (!can_recurse) {
4565 char *ondisk_path;
4566 struct stat sb;
4567 TAILQ_FOREACH(pe, &paths, entry) {
4568 if (asprintf(&ondisk_path, "%s/%s",
4569 got_worktree_get_root_path(worktree),
4570 pe->path) == -1) {
4571 error = got_error_from_errno("asprintf");
4572 goto done;
4574 if (lstat(ondisk_path, &sb) == -1) {
4575 if (errno == ENOENT) {
4576 free(ondisk_path);
4577 continue;
4579 error = got_error_from_errno2("lstat",
4580 ondisk_path);
4581 free(ondisk_path);
4582 goto done;
4584 free(ondisk_path);
4585 if (S_ISDIR(sb.st_mode)) {
4586 error = got_error_msg(GOT_ERR_BAD_PATH,
4587 "reverting directories requires -R option");
4588 goto done;
4593 cpa.patch_script_file = patch_script_file;
4594 cpa.action = "revert";
4595 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4596 pflag ? choose_patch : NULL, &cpa, repo);
4597 if (error)
4598 goto done;
4599 done:
4600 if (patch_script_file && fclose(patch_script_file) == EOF &&
4601 error == NULL)
4602 error = got_error_from_errno2("fclose", patch_script_path);
4603 if (repo)
4604 got_repo_close(repo);
4605 if (worktree)
4606 got_worktree_close(worktree);
4607 free(path);
4608 free(cwd);
4609 return error;
4612 __dead static void
4613 usage_commit(void)
4615 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4616 getprogname());
4617 exit(1);
4620 struct collect_commit_logmsg_arg {
4621 const char *cmdline_log;
4622 const char *editor;
4623 const char *worktree_path;
4624 const char *branch_name;
4625 const char *repo_path;
4626 char *logmsg_path;
4630 static const struct got_error *
4631 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4632 void *arg)
4634 char *initial_content = NULL;
4635 struct got_pathlist_entry *pe;
4636 const struct got_error *err = NULL;
4637 char *template = NULL;
4638 struct collect_commit_logmsg_arg *a = arg;
4639 int fd;
4640 size_t len;
4642 /* if a message was specified on the command line, just use it */
4643 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4644 len = strlen(a->cmdline_log) + 1;
4645 *logmsg = malloc(len + 1);
4646 if (*logmsg == NULL)
4647 return got_error_from_errno("malloc");
4648 strlcpy(*logmsg, a->cmdline_log, len);
4649 return NULL;
4652 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4653 return got_error_from_errno("asprintf");
4655 if (asprintf(&initial_content,
4656 "\n# changes to be committed on branch %s:\n",
4657 a->branch_name) == -1)
4658 return got_error_from_errno("asprintf");
4660 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4661 if (err)
4662 goto done;
4664 dprintf(fd, initial_content);
4666 TAILQ_FOREACH(pe, commitable_paths, entry) {
4667 struct got_commitable *ct = pe->data;
4668 dprintf(fd, "# %c %s\n",
4669 got_commitable_get_status(ct),
4670 got_commitable_get_path(ct));
4672 close(fd);
4674 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4675 done:
4676 free(initial_content);
4677 free(template);
4679 /* Editor is done; we can now apply unveil(2) */
4680 if (err == NULL) {
4681 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4682 if (err) {
4683 free(*logmsg);
4684 *logmsg = NULL;
4687 return err;
4690 static const struct got_error *
4691 cmd_commit(int argc, char *argv[])
4693 const struct got_error *error = NULL;
4694 struct got_worktree *worktree = NULL;
4695 struct got_repository *repo = NULL;
4696 char *cwd = NULL, *id_str = NULL;
4697 struct got_object_id *id = NULL;
4698 const char *logmsg = NULL;
4699 struct collect_commit_logmsg_arg cl_arg;
4700 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4701 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4702 struct got_pathlist_head paths;
4704 TAILQ_INIT(&paths);
4705 cl_arg.logmsg_path = NULL;
4707 while ((ch = getopt(argc, argv, "m:")) != -1) {
4708 switch (ch) {
4709 case 'm':
4710 logmsg = optarg;
4711 break;
4712 default:
4713 usage_commit();
4714 /* NOTREACHED */
4718 argc -= optind;
4719 argv += optind;
4721 #ifndef PROFILE
4722 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4723 "unveil", NULL) == -1)
4724 err(1, "pledge");
4725 #endif
4726 cwd = getcwd(NULL, 0);
4727 if (cwd == NULL) {
4728 error = got_error_from_errno("getcwd");
4729 goto done;
4731 error = got_worktree_open(&worktree, cwd);
4732 if (error)
4733 goto done;
4735 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4736 if (error)
4737 goto done;
4738 if (rebase_in_progress) {
4739 error = got_error(GOT_ERR_REBASING);
4740 goto done;
4743 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4744 worktree);
4745 if (error)
4746 goto done;
4748 error = get_gitconfig_path(&gitconfig_path);
4749 if (error)
4750 goto done;
4751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4752 gitconfig_path);
4753 if (error != NULL)
4754 goto done;
4756 error = get_author(&author, repo);
4757 if (error)
4758 return error;
4761 * unveil(2) traverses exec(2); if an editor is used we have
4762 * to apply unveil after the log message has been written.
4764 if (logmsg == NULL || strlen(logmsg) == 0)
4765 error = get_editor(&editor);
4766 else
4767 error = apply_unveil(got_repo_get_path(repo), 0,
4768 got_worktree_get_root_path(worktree));
4769 if (error)
4770 goto done;
4772 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4773 if (error)
4774 goto done;
4776 cl_arg.editor = editor;
4777 cl_arg.cmdline_log = logmsg;
4778 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4779 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4780 if (!histedit_in_progress) {
4781 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4782 error = got_error(GOT_ERR_COMMIT_BRANCH);
4783 goto done;
4785 cl_arg.branch_name += 11;
4787 cl_arg.repo_path = got_repo_get_path(repo);
4788 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4789 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4790 if (error) {
4791 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4792 cl_arg.logmsg_path != NULL)
4793 preserve_logmsg = 1;
4794 goto done;
4797 error = got_object_id_str(&id_str, id);
4798 if (error)
4799 goto done;
4800 printf("Created commit %s\n", id_str);
4801 done:
4802 if (preserve_logmsg) {
4803 fprintf(stderr, "%s: log message preserved in %s\n",
4804 getprogname(), cl_arg.logmsg_path);
4805 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4806 error == NULL)
4807 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4808 free(cl_arg.logmsg_path);
4809 if (repo)
4810 got_repo_close(repo);
4811 if (worktree)
4812 got_worktree_close(worktree);
4813 free(cwd);
4814 free(id_str);
4815 free(gitconfig_path);
4816 free(editor);
4817 free(author);
4818 return error;
4821 __dead static void
4822 usage_cherrypick(void)
4824 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4825 exit(1);
4828 static const struct got_error *
4829 cmd_cherrypick(int argc, char *argv[])
4831 const struct got_error *error = NULL;
4832 struct got_worktree *worktree = NULL;
4833 struct got_repository *repo = NULL;
4834 char *cwd = NULL, *commit_id_str = NULL;
4835 struct got_object_id *commit_id = NULL;
4836 struct got_commit_object *commit = NULL;
4837 struct got_object_qid *pid;
4838 struct got_reference *head_ref = NULL;
4839 int ch, did_something = 0;
4841 while ((ch = getopt(argc, argv, "")) != -1) {
4842 switch (ch) {
4843 default:
4844 usage_cherrypick();
4845 /* NOTREACHED */
4849 argc -= optind;
4850 argv += optind;
4852 #ifndef PROFILE
4853 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4854 "unveil", NULL) == -1)
4855 err(1, "pledge");
4856 #endif
4857 if (argc != 1)
4858 usage_cherrypick();
4860 cwd = getcwd(NULL, 0);
4861 if (cwd == NULL) {
4862 error = got_error_from_errno("getcwd");
4863 goto done;
4865 error = got_worktree_open(&worktree, cwd);
4866 if (error)
4867 goto done;
4869 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4870 NULL);
4871 if (error != NULL)
4872 goto done;
4874 error = apply_unveil(got_repo_get_path(repo), 0,
4875 got_worktree_get_root_path(worktree));
4876 if (error)
4877 goto done;
4879 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4880 GOT_OBJ_TYPE_COMMIT, repo);
4881 if (error != NULL) {
4882 struct got_reference *ref;
4883 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4884 goto done;
4885 error = got_ref_open(&ref, repo, argv[0], 0);
4886 if (error != NULL)
4887 goto done;
4888 error = got_ref_resolve(&commit_id, repo, ref);
4889 got_ref_close(ref);
4890 if (error != NULL)
4891 goto done;
4893 error = got_object_id_str(&commit_id_str, commit_id);
4894 if (error)
4895 goto done;
4897 error = got_ref_open(&head_ref, repo,
4898 got_worktree_get_head_ref_name(worktree), 0);
4899 if (error != NULL)
4900 goto done;
4902 error = check_same_branch(commit_id, head_ref, NULL, repo);
4903 if (error) {
4904 if (error->code != GOT_ERR_ANCESTRY)
4905 goto done;
4906 error = NULL;
4907 } else {
4908 error = got_error(GOT_ERR_SAME_BRANCH);
4909 goto done;
4912 error = got_object_open_as_commit(&commit, repo, commit_id);
4913 if (error)
4914 goto done;
4915 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4916 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4917 commit_id, repo, update_progress, &did_something, check_cancelled,
4918 NULL);
4919 if (error != NULL)
4920 goto done;
4922 if (did_something)
4923 printf("Merged commit %s\n", commit_id_str);
4924 done:
4925 if (commit)
4926 got_object_commit_close(commit);
4927 free(commit_id_str);
4928 if (head_ref)
4929 got_ref_close(head_ref);
4930 if (worktree)
4931 got_worktree_close(worktree);
4932 if (repo)
4933 got_repo_close(repo);
4934 return error;
4937 __dead static void
4938 usage_backout(void)
4940 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4941 exit(1);
4944 static const struct got_error *
4945 cmd_backout(int argc, char *argv[])
4947 const struct got_error *error = NULL;
4948 struct got_worktree *worktree = NULL;
4949 struct got_repository *repo = NULL;
4950 char *cwd = NULL, *commit_id_str = NULL;
4951 struct got_object_id *commit_id = NULL;
4952 struct got_commit_object *commit = NULL;
4953 struct got_object_qid *pid;
4954 struct got_reference *head_ref = NULL;
4955 int ch, did_something = 0;
4957 while ((ch = getopt(argc, argv, "")) != -1) {
4958 switch (ch) {
4959 default:
4960 usage_backout();
4961 /* NOTREACHED */
4965 argc -= optind;
4966 argv += optind;
4968 #ifndef PROFILE
4969 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4970 "unveil", NULL) == -1)
4971 err(1, "pledge");
4972 #endif
4973 if (argc != 1)
4974 usage_backout();
4976 cwd = getcwd(NULL, 0);
4977 if (cwd == NULL) {
4978 error = got_error_from_errno("getcwd");
4979 goto done;
4981 error = got_worktree_open(&worktree, cwd);
4982 if (error)
4983 goto done;
4985 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4986 NULL);
4987 if (error != NULL)
4988 goto done;
4990 error = apply_unveil(got_repo_get_path(repo), 0,
4991 got_worktree_get_root_path(worktree));
4992 if (error)
4993 goto done;
4995 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4996 GOT_OBJ_TYPE_COMMIT, repo);
4997 if (error != NULL) {
4998 struct got_reference *ref;
4999 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5000 goto done;
5001 error = got_ref_open(&ref, repo, argv[0], 0);
5002 if (error != NULL)
5003 goto done;
5004 error = got_ref_resolve(&commit_id, repo, ref);
5005 got_ref_close(ref);
5006 if (error != NULL)
5007 goto done;
5009 error = got_object_id_str(&commit_id_str, commit_id);
5010 if (error)
5011 goto done;
5013 error = got_ref_open(&head_ref, repo,
5014 got_worktree_get_head_ref_name(worktree), 0);
5015 if (error != NULL)
5016 goto done;
5018 error = check_same_branch(commit_id, head_ref, NULL, repo);
5019 if (error)
5020 goto done;
5022 error = got_object_open_as_commit(&commit, repo, commit_id);
5023 if (error)
5024 goto done;
5025 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5026 if (pid == NULL) {
5027 error = got_error(GOT_ERR_ROOT_COMMIT);
5028 goto done;
5031 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5032 update_progress, &did_something, check_cancelled, NULL);
5033 if (error != NULL)
5034 goto done;
5036 if (did_something)
5037 printf("Backed out commit %s\n", commit_id_str);
5038 done:
5039 if (commit)
5040 got_object_commit_close(commit);
5041 free(commit_id_str);
5042 if (head_ref)
5043 got_ref_close(head_ref);
5044 if (worktree)
5045 got_worktree_close(worktree);
5046 if (repo)
5047 got_repo_close(repo);
5048 return error;
5051 __dead static void
5052 usage_rebase(void)
5054 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5055 getprogname());
5056 exit(1);
5059 void
5060 trim_logmsg(char *logmsg, int limit)
5062 char *nl;
5063 size_t len;
5065 len = strlen(logmsg);
5066 if (len > limit)
5067 len = limit;
5068 logmsg[len] = '\0';
5069 nl = strchr(logmsg, '\n');
5070 if (nl)
5071 *nl = '\0';
5074 static const struct got_error *
5075 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5077 const struct got_error *err;
5078 char *logmsg0 = NULL;
5079 const char *s;
5081 err = got_object_commit_get_logmsg(&logmsg0, commit);
5082 if (err)
5083 return err;
5085 s = logmsg0;
5086 while (isspace((unsigned char)s[0]))
5087 s++;
5089 *logmsg = strdup(s);
5090 if (*logmsg == NULL) {
5091 err = got_error_from_errno("strdup");
5092 goto done;
5095 trim_logmsg(*logmsg, limit);
5096 done:
5097 free(logmsg0);
5098 return err;
5101 static const struct got_error *
5102 show_rebase_progress(struct got_commit_object *commit,
5103 struct got_object_id *old_id, struct got_object_id *new_id)
5105 const struct got_error *err;
5106 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5108 err = got_object_id_str(&old_id_str, old_id);
5109 if (err)
5110 goto done;
5112 if (new_id) {
5113 err = got_object_id_str(&new_id_str, new_id);
5114 if (err)
5115 goto done;
5118 old_id_str[12] = '\0';
5119 if (new_id_str)
5120 new_id_str[12] = '\0';
5122 err = get_short_logmsg(&logmsg, 42, commit);
5123 if (err)
5124 goto done;
5126 printf("%s -> %s: %s\n", old_id_str,
5127 new_id_str ? new_id_str : "no-op change", logmsg);
5128 done:
5129 free(old_id_str);
5130 free(new_id_str);
5131 return err;
5134 static const struct got_error *
5135 rebase_progress(void *arg, unsigned char status, const char *path)
5137 unsigned char *rebase_status = arg;
5139 while (path[0] == '/')
5140 path++;
5141 printf("%c %s\n", status, path);
5143 if (*rebase_status == GOT_STATUS_CONFLICT)
5144 return NULL;
5145 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5146 *rebase_status = status;
5147 return NULL;
5150 static const struct got_error *
5151 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5152 struct got_reference *branch, struct got_reference *new_base_branch,
5153 struct got_reference *tmp_branch, struct got_repository *repo)
5155 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5156 return got_worktree_rebase_complete(worktree, fileindex,
5157 new_base_branch, tmp_branch, branch, repo);
5160 static const struct got_error *
5161 rebase_commit(struct got_pathlist_head *merged_paths,
5162 struct got_worktree *worktree, struct got_fileindex *fileindex,
5163 struct got_reference *tmp_branch,
5164 struct got_object_id *commit_id, struct got_repository *repo)
5166 const struct got_error *error;
5167 struct got_commit_object *commit;
5168 struct got_object_id *new_commit_id;
5170 error = got_object_open_as_commit(&commit, repo, commit_id);
5171 if (error)
5172 return error;
5174 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5175 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5176 if (error) {
5177 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5178 goto done;
5179 error = show_rebase_progress(commit, commit_id, NULL);
5180 } else {
5181 error = show_rebase_progress(commit, commit_id, new_commit_id);
5182 free(new_commit_id);
5184 done:
5185 got_object_commit_close(commit);
5186 return error;
5189 struct check_path_prefix_arg {
5190 const char *path_prefix;
5191 size_t len;
5192 int errcode;
5195 static const struct got_error *
5196 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5197 struct got_blob_object *blob2, struct got_object_id *id1,
5198 struct got_object_id *id2, const char *path1, const char *path2,
5199 mode_t mode1, mode_t mode2, struct got_repository *repo)
5201 struct check_path_prefix_arg *a = arg;
5203 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5204 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5205 return got_error(a->errcode);
5207 return NULL;
5210 static const struct got_error *
5211 check_path_prefix(struct got_object_id *parent_id,
5212 struct got_object_id *commit_id, const char *path_prefix,
5213 int errcode, struct got_repository *repo)
5215 const struct got_error *err;
5216 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5217 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5218 struct check_path_prefix_arg cpp_arg;
5220 if (got_path_is_root_dir(path_prefix))
5221 return NULL;
5223 err = got_object_open_as_commit(&commit, repo, commit_id);
5224 if (err)
5225 goto done;
5227 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5228 if (err)
5229 goto done;
5231 err = got_object_open_as_tree(&tree1, repo,
5232 got_object_commit_get_tree_id(parent_commit));
5233 if (err)
5234 goto done;
5236 err = got_object_open_as_tree(&tree2, repo,
5237 got_object_commit_get_tree_id(commit));
5238 if (err)
5239 goto done;
5241 cpp_arg.path_prefix = path_prefix;
5242 while (cpp_arg.path_prefix[0] == '/')
5243 cpp_arg.path_prefix++;
5244 cpp_arg.len = strlen(cpp_arg.path_prefix);
5245 cpp_arg.errcode = errcode;
5246 err = got_diff_tree(tree1, tree2, "", "", repo,
5247 check_path_prefix_in_diff, &cpp_arg, 0);
5248 done:
5249 if (tree1)
5250 got_object_tree_close(tree1);
5251 if (tree2)
5252 got_object_tree_close(tree2);
5253 if (commit)
5254 got_object_commit_close(commit);
5255 if (parent_commit)
5256 got_object_commit_close(parent_commit);
5257 return err;
5260 static const struct got_error *
5261 collect_commits(struct got_object_id_queue *commits,
5262 struct got_object_id *initial_commit_id,
5263 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5264 const char *path_prefix, int path_prefix_errcode,
5265 struct got_repository *repo)
5267 const struct got_error *err = NULL;
5268 struct got_commit_graph *graph = NULL;
5269 struct got_object_id *parent_id = NULL;
5270 struct got_object_qid *qid;
5271 struct got_object_id *commit_id = initial_commit_id;
5273 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5274 if (err)
5275 return err;
5277 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5278 check_cancelled, NULL);
5279 if (err)
5280 goto done;
5281 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5282 err = got_commit_graph_iter_next(&parent_id, graph);
5283 if (err) {
5284 if (err->code == GOT_ERR_ITER_COMPLETED) {
5285 err = got_error_msg(GOT_ERR_ANCESTRY,
5286 "ran out of commits to rebase before "
5287 "youngest common ancestor commit has "
5288 "been reached?!?");
5289 goto done;
5290 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5291 goto done;
5292 err = got_commit_graph_fetch_commits(graph, 1, repo,
5293 check_cancelled, NULL);
5294 if (err)
5295 goto done;
5296 } else {
5297 err = check_path_prefix(parent_id, commit_id,
5298 path_prefix, path_prefix_errcode, repo);
5299 if (err)
5300 goto done;
5302 err = got_object_qid_alloc(&qid, commit_id);
5303 if (err)
5304 goto done;
5305 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5306 commit_id = parent_id;
5309 done:
5310 got_commit_graph_close(graph);
5311 return err;
5314 static const struct got_error *
5315 cmd_rebase(int argc, char *argv[])
5317 const struct got_error *error = NULL;
5318 struct got_worktree *worktree = NULL;
5319 struct got_repository *repo = NULL;
5320 struct got_fileindex *fileindex = NULL;
5321 char *cwd = NULL;
5322 struct got_reference *branch = NULL;
5323 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5324 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5325 struct got_object_id *resume_commit_id = NULL;
5326 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5327 struct got_commit_object *commit = NULL;
5328 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5329 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5330 struct got_object_id_queue commits;
5331 struct got_pathlist_head merged_paths;
5332 const struct got_object_id_queue *parent_ids;
5333 struct got_object_qid *qid, *pid;
5335 SIMPLEQ_INIT(&commits);
5336 TAILQ_INIT(&merged_paths);
5338 while ((ch = getopt(argc, argv, "ac")) != -1) {
5339 switch (ch) {
5340 case 'a':
5341 abort_rebase = 1;
5342 break;
5343 case 'c':
5344 continue_rebase = 1;
5345 break;
5346 default:
5347 usage_rebase();
5348 /* NOTREACHED */
5352 argc -= optind;
5353 argv += optind;
5355 #ifndef PROFILE
5356 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5357 "unveil", NULL) == -1)
5358 err(1, "pledge");
5359 #endif
5360 if (abort_rebase && continue_rebase)
5361 usage_rebase();
5362 else if (abort_rebase || continue_rebase) {
5363 if (argc != 0)
5364 usage_rebase();
5365 } else if (argc != 1)
5366 usage_rebase();
5368 cwd = getcwd(NULL, 0);
5369 if (cwd == NULL) {
5370 error = got_error_from_errno("getcwd");
5371 goto done;
5373 error = got_worktree_open(&worktree, cwd);
5374 if (error)
5375 goto done;
5377 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5378 NULL);
5379 if (error != NULL)
5380 goto done;
5382 error = apply_unveil(got_repo_get_path(repo), 0,
5383 got_worktree_get_root_path(worktree));
5384 if (error)
5385 goto done;
5387 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5388 if (error)
5389 goto done;
5391 if (abort_rebase) {
5392 int did_something;
5393 if (!rebase_in_progress) {
5394 error = got_error(GOT_ERR_NOT_REBASING);
5395 goto done;
5397 error = got_worktree_rebase_continue(&resume_commit_id,
5398 &new_base_branch, &tmp_branch, &branch, &fileindex,
5399 worktree, repo);
5400 if (error)
5401 goto done;
5402 printf("Switching work tree to %s\n",
5403 got_ref_get_symref_target(new_base_branch));
5404 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5405 new_base_branch, update_progress, &did_something);
5406 if (error)
5407 goto done;
5408 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5409 goto done; /* nothing else to do */
5412 if (continue_rebase) {
5413 if (!rebase_in_progress) {
5414 error = got_error(GOT_ERR_NOT_REBASING);
5415 goto done;
5417 error = got_worktree_rebase_continue(&resume_commit_id,
5418 &new_base_branch, &tmp_branch, &branch, &fileindex,
5419 worktree, repo);
5420 if (error)
5421 goto done;
5423 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5424 resume_commit_id, repo);
5425 if (error)
5426 goto done;
5428 yca_id = got_object_id_dup(resume_commit_id);
5429 if (yca_id == NULL) {
5430 error = got_error_from_errno("got_object_id_dup");
5431 goto done;
5433 } else {
5434 error = got_ref_open(&branch, repo, argv[0], 0);
5435 if (error != NULL)
5436 goto done;
5439 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5440 if (error)
5441 goto done;
5443 if (!continue_rebase) {
5444 struct got_object_id *base_commit_id;
5446 base_commit_id = got_worktree_get_base_commit_id(worktree);
5447 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5448 base_commit_id, branch_head_commit_id, repo,
5449 check_cancelled, NULL);
5450 if (error)
5451 goto done;
5452 if (yca_id == NULL) {
5453 error = got_error_msg(GOT_ERR_ANCESTRY,
5454 "specified branch shares no common ancestry "
5455 "with work tree's branch");
5456 goto done;
5459 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5460 if (error) {
5461 if (error->code != GOT_ERR_ANCESTRY)
5462 goto done;
5463 error = NULL;
5464 } else {
5465 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5466 "specified branch resolves to a commit which "
5467 "is already contained in work tree's branch");
5468 goto done;
5470 error = got_worktree_rebase_prepare(&new_base_branch,
5471 &tmp_branch, &fileindex, worktree, branch, repo);
5472 if (error)
5473 goto done;
5476 commit_id = branch_head_commit_id;
5477 error = got_object_open_as_commit(&commit, repo, commit_id);
5478 if (error)
5479 goto done;
5481 parent_ids = got_object_commit_get_parent_ids(commit);
5482 pid = SIMPLEQ_FIRST(parent_ids);
5483 if (pid == NULL) {
5484 if (!continue_rebase) {
5485 int did_something;
5486 error = got_worktree_rebase_abort(worktree, fileindex,
5487 repo, new_base_branch, update_progress,
5488 &did_something);
5489 if (error)
5490 goto done;
5491 printf("Rebase of %s aborted\n",
5492 got_ref_get_name(branch));
5494 error = got_error(GOT_ERR_EMPTY_REBASE);
5495 goto done;
5497 error = collect_commits(&commits, commit_id, pid->id,
5498 yca_id, got_worktree_get_path_prefix(worktree),
5499 GOT_ERR_REBASE_PATH, repo);
5500 got_object_commit_close(commit);
5501 commit = NULL;
5502 if (error)
5503 goto done;
5505 if (SIMPLEQ_EMPTY(&commits)) {
5506 if (continue_rebase) {
5507 error = rebase_complete(worktree, fileindex,
5508 branch, new_base_branch, tmp_branch, repo);
5509 goto done;
5510 } else {
5511 /* Fast-forward the reference of the branch. */
5512 struct got_object_id *new_head_commit_id;
5513 char *id_str;
5514 error = got_ref_resolve(&new_head_commit_id, repo,
5515 new_base_branch);
5516 if (error)
5517 goto done;
5518 error = got_object_id_str(&id_str, new_head_commit_id);
5519 printf("Forwarding %s to commit %s\n",
5520 got_ref_get_name(branch), id_str);
5521 free(id_str);
5522 error = got_ref_change_ref(branch,
5523 new_head_commit_id);
5524 if (error)
5525 goto done;
5529 pid = NULL;
5530 SIMPLEQ_FOREACH(qid, &commits, entry) {
5531 commit_id = qid->id;
5532 parent_id = pid ? pid->id : yca_id;
5533 pid = qid;
5535 error = got_worktree_rebase_merge_files(&merged_paths,
5536 worktree, fileindex, parent_id, commit_id, repo,
5537 rebase_progress, &rebase_status, check_cancelled, NULL);
5538 if (error)
5539 goto done;
5541 if (rebase_status == GOT_STATUS_CONFLICT) {
5542 got_worktree_rebase_pathlist_free(&merged_paths);
5543 break;
5546 error = rebase_commit(&merged_paths, worktree, fileindex,
5547 tmp_branch, commit_id, repo);
5548 got_worktree_rebase_pathlist_free(&merged_paths);
5549 if (error)
5550 goto done;
5553 if (rebase_status == GOT_STATUS_CONFLICT) {
5554 error = got_worktree_rebase_postpone(worktree, fileindex);
5555 if (error)
5556 goto done;
5557 error = got_error_msg(GOT_ERR_CONFLICTS,
5558 "conflicts must be resolved before rebasing can continue");
5559 } else
5560 error = rebase_complete(worktree, fileindex, branch,
5561 new_base_branch, tmp_branch, repo);
5562 done:
5563 got_object_id_queue_free(&commits);
5564 free(branch_head_commit_id);
5565 free(resume_commit_id);
5566 free(yca_id);
5567 if (commit)
5568 got_object_commit_close(commit);
5569 if (branch)
5570 got_ref_close(branch);
5571 if (new_base_branch)
5572 got_ref_close(new_base_branch);
5573 if (tmp_branch)
5574 got_ref_close(tmp_branch);
5575 if (worktree)
5576 got_worktree_close(worktree);
5577 if (repo)
5578 got_repo_close(repo);
5579 return error;
5582 __dead static void
5583 usage_histedit(void)
5585 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5586 getprogname());
5587 exit(1);
5590 #define GOT_HISTEDIT_PICK 'p'
5591 #define GOT_HISTEDIT_EDIT 'e'
5592 #define GOT_HISTEDIT_FOLD 'f'
5593 #define GOT_HISTEDIT_DROP 'd'
5594 #define GOT_HISTEDIT_MESG 'm'
5596 static struct got_histedit_cmd {
5597 unsigned char code;
5598 const char *name;
5599 const char *desc;
5600 } got_histedit_cmds[] = {
5601 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5602 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5603 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5604 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5605 { GOT_HISTEDIT_MESG, "mesg",
5606 "single-line log message for commit above (open editor if empty)" },
5609 struct got_histedit_list_entry {
5610 TAILQ_ENTRY(got_histedit_list_entry) entry;
5611 struct got_object_id *commit_id;
5612 const struct got_histedit_cmd *cmd;
5613 char *logmsg;
5615 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5617 static const struct got_error *
5618 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5619 FILE *f, struct got_repository *repo)
5621 const struct got_error *err = NULL;
5622 char *logmsg = NULL, *id_str = NULL;
5623 struct got_commit_object *commit = NULL;
5624 int n;
5626 err = got_object_open_as_commit(&commit, repo, commit_id);
5627 if (err)
5628 goto done;
5630 err = get_short_logmsg(&logmsg, 34, commit);
5631 if (err)
5632 goto done;
5634 err = got_object_id_str(&id_str, commit_id);
5635 if (err)
5636 goto done;
5638 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5639 if (n < 0)
5640 err = got_ferror(f, GOT_ERR_IO);
5641 done:
5642 if (commit)
5643 got_object_commit_close(commit);
5644 free(id_str);
5645 free(logmsg);
5646 return err;
5649 static const struct got_error *
5650 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5651 struct got_repository *repo)
5653 const struct got_error *err = NULL;
5654 struct got_object_qid *qid;
5656 if (SIMPLEQ_EMPTY(commits))
5657 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5659 SIMPLEQ_FOREACH(qid, commits, entry) {
5660 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5661 f, repo);
5662 if (err)
5663 break;
5666 return err;
5669 static const struct got_error *
5670 write_cmd_list(FILE *f)
5672 const struct got_error *err = NULL;
5673 int n, i;
5675 n = fprintf(f, "# Available histedit commands:\n");
5676 if (n < 0)
5677 return got_ferror(f, GOT_ERR_IO);
5679 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5680 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5681 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5682 cmd->desc);
5683 if (n < 0) {
5684 err = got_ferror(f, GOT_ERR_IO);
5685 break;
5688 n = fprintf(f, "# Commits will be processed in order from top to "
5689 "bottom of this file.\n");
5690 if (n < 0)
5691 return got_ferror(f, GOT_ERR_IO);
5692 return err;
5695 static const struct got_error *
5696 histedit_syntax_error(int lineno)
5698 static char msg[42];
5699 int ret;
5701 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5702 lineno);
5703 if (ret == -1 || ret >= sizeof(msg))
5704 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5706 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5709 static const struct got_error *
5710 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5711 char *logmsg, struct got_repository *repo)
5713 const struct got_error *err;
5714 struct got_commit_object *folded_commit = NULL;
5715 char *id_str, *folded_logmsg = NULL;
5717 err = got_object_id_str(&id_str, hle->commit_id);
5718 if (err)
5719 return err;
5721 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5722 if (err)
5723 goto done;
5725 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5726 if (err)
5727 goto done;
5728 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5729 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5730 folded_logmsg) == -1) {
5731 err = got_error_from_errno("asprintf");
5732 goto done;
5734 done:
5735 if (folded_commit)
5736 got_object_commit_close(folded_commit);
5737 free(id_str);
5738 free(folded_logmsg);
5739 return err;
5742 static struct got_histedit_list_entry *
5743 get_folded_commits(struct got_histedit_list_entry *hle)
5745 struct got_histedit_list_entry *prev, *folded = NULL;
5747 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5748 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5749 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5750 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5751 folded = prev;
5752 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5755 return folded;
5758 static const struct got_error *
5759 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5760 struct got_repository *repo)
5762 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5763 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5764 const struct got_error *err = NULL;
5765 struct got_commit_object *commit = NULL;
5766 int fd;
5767 struct got_histedit_list_entry *folded = NULL;
5769 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5770 if (err)
5771 return err;
5773 folded = get_folded_commits(hle);
5774 if (folded) {
5775 while (folded != hle) {
5776 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5777 folded = TAILQ_NEXT(folded, entry);
5778 continue;
5780 err = append_folded_commit_msg(&new_msg, folded,
5781 logmsg, repo);
5782 if (err)
5783 goto done;
5784 free(logmsg);
5785 logmsg = new_msg;
5786 folded = TAILQ_NEXT(folded, entry);
5790 err = got_object_id_str(&id_str, hle->commit_id);
5791 if (err)
5792 goto done;
5793 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5794 if (err)
5795 goto done;
5796 if (asprintf(&new_msg,
5797 "%s\n# original log message of commit %s: %s",
5798 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5799 err = got_error_from_errno("asprintf");
5800 goto done;
5802 free(logmsg);
5803 logmsg = new_msg;
5805 err = got_object_id_str(&id_str, hle->commit_id);
5806 if (err)
5807 goto done;
5809 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5810 if (err)
5811 goto done;
5813 dprintf(fd, logmsg);
5814 close(fd);
5816 err = get_editor(&editor);
5817 if (err)
5818 goto done;
5820 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5821 if (err) {
5822 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5823 goto done;
5824 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5826 done:
5827 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5828 err = got_error_from_errno2("unlink", logmsg_path);
5829 free(logmsg_path);
5830 free(logmsg);
5831 free(orig_logmsg);
5832 free(editor);
5833 if (commit)
5834 got_object_commit_close(commit);
5835 return err;
5838 static const struct got_error *
5839 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5840 FILE *f, struct got_repository *repo)
5842 const struct got_error *err = NULL;
5843 char *line = NULL, *p, *end;
5844 size_t size;
5845 ssize_t len;
5846 int lineno = 0, i;
5847 const struct got_histedit_cmd *cmd;
5848 struct got_object_id *commit_id = NULL;
5849 struct got_histedit_list_entry *hle = NULL;
5851 for (;;) {
5852 len = getline(&line, &size, f);
5853 if (len == -1) {
5854 const struct got_error *getline_err;
5855 if (feof(f))
5856 break;
5857 getline_err = got_error_from_errno("getline");
5858 err = got_ferror(f, getline_err->code);
5859 break;
5861 lineno++;
5862 p = line;
5863 while (isspace((unsigned char)p[0]))
5864 p++;
5865 if (p[0] == '#' || p[0] == '\0') {
5866 free(line);
5867 line = NULL;
5868 continue;
5870 cmd = NULL;
5871 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5872 cmd = &got_histedit_cmds[i];
5873 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5874 isspace((unsigned char)p[strlen(cmd->name)])) {
5875 p += strlen(cmd->name);
5876 break;
5878 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5879 p++;
5880 break;
5883 if (i == nitems(got_histedit_cmds)) {
5884 err = histedit_syntax_error(lineno);
5885 break;
5887 while (isspace((unsigned char)p[0]))
5888 p++;
5889 if (cmd->code == GOT_HISTEDIT_MESG) {
5890 if (hle == NULL || hle->logmsg != NULL) {
5891 err = got_error(GOT_ERR_HISTEDIT_CMD);
5892 break;
5894 if (p[0] == '\0') {
5895 err = histedit_edit_logmsg(hle, repo);
5896 if (err)
5897 break;
5898 } else {
5899 hle->logmsg = strdup(p);
5900 if (hle->logmsg == NULL) {
5901 err = got_error_from_errno("strdup");
5902 break;
5905 free(line);
5906 line = NULL;
5907 continue;
5908 } else {
5909 end = p;
5910 while (end[0] && !isspace((unsigned char)end[0]))
5911 end++;
5912 *end = '\0';
5914 err = got_object_resolve_id_str(&commit_id, repo, p);
5915 if (err) {
5916 /* override error code */
5917 err = histedit_syntax_error(lineno);
5918 break;
5921 hle = malloc(sizeof(*hle));
5922 if (hle == NULL) {
5923 err = got_error_from_errno("malloc");
5924 break;
5926 hle->cmd = cmd;
5927 hle->commit_id = commit_id;
5928 hle->logmsg = NULL;
5929 commit_id = NULL;
5930 free(line);
5931 line = NULL;
5932 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5935 free(line);
5936 free(commit_id);
5937 return err;
5940 static const struct got_error *
5941 histedit_check_script(struct got_histedit_list *histedit_cmds,
5942 struct got_object_id_queue *commits, struct got_repository *repo)
5944 const struct got_error *err = NULL;
5945 struct got_object_qid *qid;
5946 struct got_histedit_list_entry *hle;
5947 static char msg[80];
5948 char *id_str;
5950 if (TAILQ_EMPTY(histedit_cmds))
5951 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5952 "histedit script contains no commands");
5953 if (SIMPLEQ_EMPTY(commits))
5954 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5956 SIMPLEQ_FOREACH(qid, commits, entry) {
5957 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5958 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5959 break;
5961 if (hle == NULL) {
5962 err = got_object_id_str(&id_str, qid->id);
5963 if (err)
5964 return err;
5965 snprintf(msg, sizeof(msg),
5966 "commit %s missing from histedit script", id_str);
5967 free(id_str);
5968 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5972 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5973 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5974 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5975 "last commit in histedit script cannot be folded");
5977 return NULL;
5980 static const struct got_error *
5981 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5982 const char *path, struct got_object_id_queue *commits,
5983 struct got_repository *repo)
5985 const struct got_error *err = NULL;
5986 char *editor;
5987 FILE *f = NULL;
5989 err = get_editor(&editor);
5990 if (err)
5991 return err;
5993 if (spawn_editor(editor, path) == -1) {
5994 err = got_error_from_errno("failed spawning editor");
5995 goto done;
5998 f = fopen(path, "r");
5999 if (f == NULL) {
6000 err = got_error_from_errno("fopen");
6001 goto done;
6003 err = histedit_parse_list(histedit_cmds, f, repo);
6004 if (err)
6005 goto done;
6007 err = histedit_check_script(histedit_cmds, commits, repo);
6008 done:
6009 if (f && fclose(f) != 0 && err == NULL)
6010 err = got_error_from_errno("fclose");
6011 free(editor);
6012 return err;
6015 static const struct got_error *
6016 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6017 struct got_object_id_queue *, const char *, struct got_repository *);
6019 static const struct got_error *
6020 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6021 struct got_object_id_queue *commits, struct got_repository *repo)
6023 const struct got_error *err;
6024 FILE *f = NULL;
6025 char *path = NULL;
6027 err = got_opentemp_named(&path, &f, "got-histedit");
6028 if (err)
6029 return err;
6031 err = write_cmd_list(f);
6032 if (err)
6033 goto done;
6035 err = histedit_write_commit_list(commits, f, repo);
6036 if (err)
6037 goto done;
6039 if (fclose(f) != 0) {
6040 err = got_error_from_errno("fclose");
6041 goto done;
6043 f = NULL;
6045 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6046 if (err) {
6047 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6048 err->code != GOT_ERR_HISTEDIT_CMD)
6049 goto done;
6050 err = histedit_edit_list_retry(histedit_cmds, err,
6051 commits, path, repo);
6053 done:
6054 if (f && fclose(f) != 0 && err == NULL)
6055 err = got_error_from_errno("fclose");
6056 if (path && unlink(path) != 0 && err == NULL)
6057 err = got_error_from_errno2("unlink", path);
6058 free(path);
6059 return err;
6062 static const struct got_error *
6063 histedit_save_list(struct got_histedit_list *histedit_cmds,
6064 struct got_worktree *worktree, struct got_repository *repo)
6066 const struct got_error *err = NULL;
6067 char *path = NULL;
6068 FILE *f = NULL;
6069 struct got_histedit_list_entry *hle;
6070 struct got_commit_object *commit = NULL;
6072 err = got_worktree_get_histedit_script_path(&path, worktree);
6073 if (err)
6074 return err;
6076 f = fopen(path, "w");
6077 if (f == NULL) {
6078 err = got_error_from_errno2("fopen", path);
6079 goto done;
6081 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6082 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6083 repo);
6084 if (err)
6085 break;
6087 if (hle->logmsg) {
6088 int n = fprintf(f, "%c %s\n",
6089 GOT_HISTEDIT_MESG, hle->logmsg);
6090 if (n < 0) {
6091 err = got_ferror(f, GOT_ERR_IO);
6092 break;
6096 done:
6097 if (f && fclose(f) != 0 && err == NULL)
6098 err = got_error_from_errno("fclose");
6099 free(path);
6100 if (commit)
6101 got_object_commit_close(commit);
6102 return err;
6105 void
6106 histedit_free_list(struct got_histedit_list *histedit_cmds)
6108 struct got_histedit_list_entry *hle;
6110 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6111 TAILQ_REMOVE(histedit_cmds, hle, entry);
6112 free(hle);
6116 static const struct got_error *
6117 histedit_load_list(struct got_histedit_list *histedit_cmds,
6118 const char *path, struct got_repository *repo)
6120 const struct got_error *err = NULL;
6121 FILE *f = NULL;
6123 f = fopen(path, "r");
6124 if (f == NULL) {
6125 err = got_error_from_errno2("fopen", path);
6126 goto done;
6129 err = histedit_parse_list(histedit_cmds, f, repo);
6130 done:
6131 if (f && fclose(f) != 0 && err == NULL)
6132 err = got_error_from_errno("fclose");
6133 return err;
6136 static const struct got_error *
6137 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6138 const struct got_error *edit_err, struct got_object_id_queue *commits,
6139 const char *path, struct got_repository *repo)
6141 const struct got_error *err = NULL, *prev_err = edit_err;
6142 int resp = ' ';
6144 while (resp != 'c' && resp != 'r' && resp != 'a') {
6145 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6146 "or (a)bort: ", getprogname(), prev_err->msg);
6147 resp = getchar();
6148 if (resp == '\n')
6149 resp = getchar();
6150 if (resp == 'c') {
6151 histedit_free_list(histedit_cmds);
6152 err = histedit_run_editor(histedit_cmds, path, commits,
6153 repo);
6154 if (err) {
6155 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6156 err->code != GOT_ERR_HISTEDIT_CMD)
6157 break;
6158 prev_err = err;
6159 resp = ' ';
6160 continue;
6162 break;
6163 } else if (resp == 'r') {
6164 histedit_free_list(histedit_cmds);
6165 err = histedit_edit_script(histedit_cmds,
6166 commits, repo);
6167 if (err) {
6168 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6169 err->code != GOT_ERR_HISTEDIT_CMD)
6170 break;
6171 prev_err = err;
6172 resp = ' ';
6173 continue;
6175 break;
6176 } else if (resp == 'a') {
6177 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6178 break;
6179 } else
6180 printf("invalid response '%c'\n", resp);
6183 return err;
6186 static const struct got_error *
6187 histedit_complete(struct got_worktree *worktree,
6188 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6189 struct got_reference *branch, struct got_repository *repo)
6191 printf("Switching work tree to %s\n",
6192 got_ref_get_symref_target(branch));
6193 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6194 branch, repo);
6197 static const struct got_error *
6198 show_histedit_progress(struct got_commit_object *commit,
6199 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6201 const struct got_error *err;
6202 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6204 err = got_object_id_str(&old_id_str, hle->commit_id);
6205 if (err)
6206 goto done;
6208 if (new_id) {
6209 err = got_object_id_str(&new_id_str, new_id);
6210 if (err)
6211 goto done;
6214 old_id_str[12] = '\0';
6215 if (new_id_str)
6216 new_id_str[12] = '\0';
6218 if (hle->logmsg) {
6219 logmsg = strdup(hle->logmsg);
6220 if (logmsg == NULL) {
6221 err = got_error_from_errno("strdup");
6222 goto done;
6224 trim_logmsg(logmsg, 42);
6225 } else {
6226 err = get_short_logmsg(&logmsg, 42, commit);
6227 if (err)
6228 goto done;
6231 switch (hle->cmd->code) {
6232 case GOT_HISTEDIT_PICK:
6233 case GOT_HISTEDIT_EDIT:
6234 printf("%s -> %s: %s\n", old_id_str,
6235 new_id_str ? new_id_str : "no-op change", logmsg);
6236 break;
6237 case GOT_HISTEDIT_DROP:
6238 case GOT_HISTEDIT_FOLD:
6239 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6240 logmsg);
6241 break;
6242 default:
6243 break;
6246 done:
6247 free(old_id_str);
6248 free(new_id_str);
6249 return err;
6252 static const struct got_error *
6253 histedit_commit(struct got_pathlist_head *merged_paths,
6254 struct got_worktree *worktree, struct got_fileindex *fileindex,
6255 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6256 struct got_repository *repo)
6258 const struct got_error *err;
6259 struct got_commit_object *commit;
6260 struct got_object_id *new_commit_id;
6262 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6263 && hle->logmsg == NULL) {
6264 err = histedit_edit_logmsg(hle, repo);
6265 if (err)
6266 return err;
6269 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6270 if (err)
6271 return err;
6273 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6274 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6275 hle->logmsg, repo);
6276 if (err) {
6277 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6278 goto done;
6279 err = show_histedit_progress(commit, hle, NULL);
6280 } else {
6281 err = show_histedit_progress(commit, hle, new_commit_id);
6282 free(new_commit_id);
6284 done:
6285 got_object_commit_close(commit);
6286 return err;
6289 static const struct got_error *
6290 histedit_skip_commit(struct got_histedit_list_entry *hle,
6291 struct got_worktree *worktree, struct got_repository *repo)
6293 const struct got_error *error;
6294 struct got_commit_object *commit;
6296 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6297 repo);
6298 if (error)
6299 return error;
6301 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6302 if (error)
6303 return error;
6305 error = show_histedit_progress(commit, hle, NULL);
6306 got_object_commit_close(commit);
6307 return error;
6310 static const struct got_error *
6311 cmd_histedit(int argc, char *argv[])
6313 const struct got_error *error = NULL;
6314 struct got_worktree *worktree = NULL;
6315 struct got_fileindex *fileindex = NULL;
6316 struct got_repository *repo = NULL;
6317 char *cwd = NULL;
6318 struct got_reference *branch = NULL;
6319 struct got_reference *tmp_branch = NULL;
6320 struct got_object_id *resume_commit_id = NULL;
6321 struct got_object_id *base_commit_id = NULL;
6322 struct got_object_id *head_commit_id = NULL;
6323 struct got_commit_object *commit = NULL;
6324 int ch, rebase_in_progress = 0, did_something;
6325 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6326 const char *edit_script_path = NULL;
6327 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6328 struct got_object_id_queue commits;
6329 struct got_pathlist_head merged_paths;
6330 const struct got_object_id_queue *parent_ids;
6331 struct got_object_qid *pid;
6332 struct got_histedit_list histedit_cmds;
6333 struct got_histedit_list_entry *hle;
6335 SIMPLEQ_INIT(&commits);
6336 TAILQ_INIT(&histedit_cmds);
6337 TAILQ_INIT(&merged_paths);
6339 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6340 switch (ch) {
6341 case 'a':
6342 abort_edit = 1;
6343 break;
6344 case 'c':
6345 continue_edit = 1;
6346 break;
6347 case 'F':
6348 edit_script_path = optarg;
6349 break;
6350 default:
6351 usage_histedit();
6352 /* NOTREACHED */
6356 argc -= optind;
6357 argv += optind;
6359 #ifndef PROFILE
6360 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6361 "unveil", NULL) == -1)
6362 err(1, "pledge");
6363 #endif
6364 if (abort_edit && continue_edit)
6365 usage_histedit();
6366 if (argc != 0)
6367 usage_histedit();
6370 * This command cannot apply unveil(2) in all cases because the
6371 * user may choose to run an editor to edit the histedit script
6372 * and to edit individual commit log messages.
6373 * unveil(2) traverses exec(2); if an editor is used we have to
6374 * apply unveil after edit script and log messages have been written.
6375 * XXX TODO: Make use of unveil(2) where possible.
6378 cwd = getcwd(NULL, 0);
6379 if (cwd == NULL) {
6380 error = got_error_from_errno("getcwd");
6381 goto done;
6383 error = got_worktree_open(&worktree, cwd);
6384 if (error)
6385 goto done;
6387 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6388 NULL);
6389 if (error != NULL)
6390 goto done;
6392 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6393 if (error)
6394 goto done;
6395 if (rebase_in_progress) {
6396 error = got_error(GOT_ERR_REBASING);
6397 goto done;
6400 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6401 if (error)
6402 goto done;
6404 if (edit_in_progress && abort_edit) {
6405 error = got_worktree_histedit_continue(&resume_commit_id,
6406 &tmp_branch, &branch, &base_commit_id, &fileindex,
6407 worktree, repo);
6408 if (error)
6409 goto done;
6410 printf("Switching work tree to %s\n",
6411 got_ref_get_symref_target(branch));
6412 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6413 branch, base_commit_id, update_progress, &did_something);
6414 if (error)
6415 goto done;
6416 printf("Histedit of %s aborted\n",
6417 got_ref_get_symref_target(branch));
6418 goto done; /* nothing else to do */
6419 } else if (abort_edit) {
6420 error = got_error(GOT_ERR_NOT_HISTEDIT);
6421 goto done;
6424 if (continue_edit) {
6425 char *path;
6427 if (!edit_in_progress) {
6428 error = got_error(GOT_ERR_NOT_HISTEDIT);
6429 goto done;
6432 error = got_worktree_get_histedit_script_path(&path, worktree);
6433 if (error)
6434 goto done;
6436 error = histedit_load_list(&histedit_cmds, path, repo);
6437 free(path);
6438 if (error)
6439 goto done;
6441 error = got_worktree_histedit_continue(&resume_commit_id,
6442 &tmp_branch, &branch, &base_commit_id, &fileindex,
6443 worktree, repo);
6444 if (error)
6445 goto done;
6447 error = got_ref_resolve(&head_commit_id, repo, branch);
6448 if (error)
6449 goto done;
6451 error = got_object_open_as_commit(&commit, repo,
6452 head_commit_id);
6453 if (error)
6454 goto done;
6455 parent_ids = got_object_commit_get_parent_ids(commit);
6456 pid = SIMPLEQ_FIRST(parent_ids);
6457 if (pid == NULL) {
6458 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6459 goto done;
6461 error = collect_commits(&commits, head_commit_id, pid->id,
6462 base_commit_id, got_worktree_get_path_prefix(worktree),
6463 GOT_ERR_HISTEDIT_PATH, repo);
6464 got_object_commit_close(commit);
6465 commit = NULL;
6466 if (error)
6467 goto done;
6468 } else {
6469 if (edit_in_progress) {
6470 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6471 goto done;
6474 error = got_ref_open(&branch, repo,
6475 got_worktree_get_head_ref_name(worktree), 0);
6476 if (error != NULL)
6477 goto done;
6479 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6480 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6481 "will not edit commit history of a branch outside "
6482 "the \"refs/heads/\" reference namespace");
6483 goto done;
6486 error = got_ref_resolve(&head_commit_id, repo, branch);
6487 got_ref_close(branch);
6488 branch = NULL;
6489 if (error)
6490 goto done;
6492 error = got_object_open_as_commit(&commit, repo,
6493 head_commit_id);
6494 if (error)
6495 goto done;
6496 parent_ids = got_object_commit_get_parent_ids(commit);
6497 pid = SIMPLEQ_FIRST(parent_ids);
6498 if (pid == NULL) {
6499 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6500 goto done;
6502 error = collect_commits(&commits, head_commit_id, pid->id,
6503 got_worktree_get_base_commit_id(worktree),
6504 got_worktree_get_path_prefix(worktree),
6505 GOT_ERR_HISTEDIT_PATH, repo);
6506 got_object_commit_close(commit);
6507 commit = NULL;
6508 if (error)
6509 goto done;
6511 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6512 &base_commit_id, &fileindex, worktree, repo);
6513 if (error)
6514 goto done;
6516 if (edit_script_path) {
6517 error = histedit_load_list(&histedit_cmds,
6518 edit_script_path, repo);
6519 if (error) {
6520 got_worktree_histedit_abort(worktree, fileindex,
6521 repo, branch, base_commit_id,
6522 update_progress, &did_something);
6523 goto done;
6525 } else {
6526 error = histedit_edit_script(&histedit_cmds, &commits,
6527 repo);
6528 if (error) {
6529 got_worktree_histedit_abort(worktree, fileindex,
6530 repo, branch, base_commit_id,
6531 update_progress, &did_something);
6532 goto done;
6537 error = histedit_save_list(&histedit_cmds, worktree,
6538 repo);
6539 if (error) {
6540 got_worktree_histedit_abort(worktree, fileindex,
6541 repo, branch, base_commit_id,
6542 update_progress, &did_something);
6543 goto done;
6548 error = histedit_check_script(&histedit_cmds, &commits, repo);
6549 if (error)
6550 goto done;
6552 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6553 if (resume_commit_id) {
6554 if (got_object_id_cmp(hle->commit_id,
6555 resume_commit_id) != 0)
6556 continue;
6558 resume_commit_id = NULL;
6559 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6560 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6561 error = histedit_skip_commit(hle, worktree,
6562 repo);
6563 } else {
6564 error = histedit_commit(NULL, worktree,
6565 fileindex, tmp_branch, hle, repo);
6567 if (error)
6568 goto done;
6569 continue;
6572 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6573 error = histedit_skip_commit(hle, worktree, repo);
6574 if (error)
6575 goto done;
6576 continue;
6579 error = got_object_open_as_commit(&commit, repo,
6580 hle->commit_id);
6581 if (error)
6582 goto done;
6583 parent_ids = got_object_commit_get_parent_ids(commit);
6584 pid = SIMPLEQ_FIRST(parent_ids);
6586 error = got_worktree_histedit_merge_files(&merged_paths,
6587 worktree, fileindex, pid->id, hle->commit_id, repo,
6588 rebase_progress, &rebase_status, check_cancelled, NULL);
6589 if (error)
6590 goto done;
6591 got_object_commit_close(commit);
6592 commit = NULL;
6594 if (rebase_status == GOT_STATUS_CONFLICT) {
6595 got_worktree_rebase_pathlist_free(&merged_paths);
6596 break;
6599 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6600 char *id_str;
6601 error = got_object_id_str(&id_str, hle->commit_id);
6602 if (error)
6603 goto done;
6604 printf("Stopping histedit for amending commit %s\n",
6605 id_str);
6606 free(id_str);
6607 got_worktree_rebase_pathlist_free(&merged_paths);
6608 error = got_worktree_histedit_postpone(worktree,
6609 fileindex);
6610 goto done;
6613 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6614 error = histedit_skip_commit(hle, worktree, repo);
6615 if (error)
6616 goto done;
6617 continue;
6620 error = histedit_commit(&merged_paths, worktree, fileindex,
6621 tmp_branch, hle, repo);
6622 got_worktree_rebase_pathlist_free(&merged_paths);
6623 if (error)
6624 goto done;
6627 if (rebase_status == GOT_STATUS_CONFLICT) {
6628 error = got_worktree_histedit_postpone(worktree, fileindex);
6629 if (error)
6630 goto done;
6631 error = got_error_msg(GOT_ERR_CONFLICTS,
6632 "conflicts must be resolved before rebasing can continue");
6633 } else
6634 error = histedit_complete(worktree, fileindex, tmp_branch,
6635 branch, repo);
6636 done:
6637 got_object_id_queue_free(&commits);
6638 histedit_free_list(&histedit_cmds);
6639 free(head_commit_id);
6640 free(base_commit_id);
6641 free(resume_commit_id);
6642 if (commit)
6643 got_object_commit_close(commit);
6644 if (branch)
6645 got_ref_close(branch);
6646 if (tmp_branch)
6647 got_ref_close(tmp_branch);
6648 if (worktree)
6649 got_worktree_close(worktree);
6650 if (repo)
6651 got_repo_close(repo);
6652 return error;
6655 __dead static void
6656 usage_integrate(void)
6658 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6659 exit(1);
6662 static const struct got_error *
6663 cmd_integrate(int argc, char *argv[])
6665 const struct got_error *error = NULL;
6666 struct got_repository *repo = NULL;
6667 struct got_worktree *worktree = NULL;
6668 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6669 const char *branch_arg = NULL;
6670 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6671 struct got_fileindex *fileindex = NULL;
6672 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6673 int ch, did_something = 0;
6675 while ((ch = getopt(argc, argv, "")) != -1) {
6676 switch (ch) {
6677 default:
6678 usage_integrate();
6679 /* NOTREACHED */
6683 argc -= optind;
6684 argv += optind;
6686 if (argc != 1)
6687 usage_integrate();
6688 branch_arg = argv[0];
6690 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6691 "unveil", NULL) == -1)
6692 err(1, "pledge");
6694 cwd = getcwd(NULL, 0);
6695 if (cwd == NULL) {
6696 error = got_error_from_errno("getcwd");
6697 goto done;
6700 error = got_worktree_open(&worktree, cwd);
6701 if (error)
6702 goto done;
6704 error = check_rebase_or_histedit_in_progress(worktree);
6705 if (error)
6706 goto done;
6708 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6709 NULL);
6710 if (error != NULL)
6711 goto done;
6713 error = apply_unveil(got_repo_get_path(repo), 0,
6714 got_worktree_get_root_path(worktree));
6715 if (error)
6716 goto done;
6718 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6719 error = got_error_from_errno("asprintf");
6720 goto done;
6723 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6724 &base_branch_ref, worktree, refname, repo);
6725 if (error)
6726 goto done;
6728 refname = strdup(got_ref_get_name(branch_ref));
6729 if (refname == NULL) {
6730 error = got_error_from_errno("strdup");
6731 got_worktree_integrate_abort(worktree, fileindex, repo,
6732 branch_ref, base_branch_ref);
6733 goto done;
6735 base_refname = strdup(got_ref_get_name(base_branch_ref));
6736 if (base_refname == NULL) {
6737 error = got_error_from_errno("strdup");
6738 got_worktree_integrate_abort(worktree, fileindex, repo,
6739 branch_ref, base_branch_ref);
6740 goto done;
6743 error = got_ref_resolve(&commit_id, repo, branch_ref);
6744 if (error)
6745 goto done;
6747 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6748 if (error)
6749 goto done;
6751 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6752 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6753 "specified branch has already been integrated");
6754 got_worktree_integrate_abort(worktree, fileindex, repo,
6755 branch_ref, base_branch_ref);
6756 goto done;
6759 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6760 if (error) {
6761 if (error->code == GOT_ERR_ANCESTRY)
6762 error = got_error(GOT_ERR_REBASE_REQUIRED);
6763 got_worktree_integrate_abort(worktree, fileindex, repo,
6764 branch_ref, base_branch_ref);
6765 goto done;
6768 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6769 branch_ref, base_branch_ref, update_progress, &did_something,
6770 check_cancelled, NULL);
6771 if (error)
6772 goto done;
6774 printf("Integrated %s into %s\n", refname, base_refname);
6775 done:
6776 if (repo)
6777 got_repo_close(repo);
6778 if (worktree)
6779 got_worktree_close(worktree);
6780 free(cwd);
6781 free(base_commit_id);
6782 free(commit_id);
6783 free(refname);
6784 free(base_refname);
6785 return error;
6788 __dead static void
6789 usage_stage(void)
6791 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6792 "[file-path ...]\n",
6793 getprogname());
6794 exit(1);
6797 static const struct got_error *
6798 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6799 const char *path, struct got_object_id *blob_id,
6800 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6802 const struct got_error *err = NULL;
6803 char *id_str = NULL;
6805 if (staged_status != GOT_STATUS_ADD &&
6806 staged_status != GOT_STATUS_MODIFY &&
6807 staged_status != GOT_STATUS_DELETE)
6808 return NULL;
6810 if (staged_status == GOT_STATUS_ADD ||
6811 staged_status == GOT_STATUS_MODIFY)
6812 err = got_object_id_str(&id_str, staged_blob_id);
6813 else
6814 err = got_object_id_str(&id_str, blob_id);
6815 if (err)
6816 return err;
6818 printf("%s %c %s\n", id_str, staged_status, path);
6819 free(id_str);
6820 return NULL;
6823 static const struct got_error *
6824 cmd_stage(int argc, char *argv[])
6826 const struct got_error *error = NULL;
6827 struct got_repository *repo = NULL;
6828 struct got_worktree *worktree = NULL;
6829 char *cwd = NULL;
6830 struct got_pathlist_head paths;
6831 struct got_pathlist_entry *pe;
6832 int ch, list_stage = 0, pflag = 0;
6833 FILE *patch_script_file = NULL;
6834 const char *patch_script_path = NULL;
6835 struct choose_patch_arg cpa;
6837 TAILQ_INIT(&paths);
6839 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6840 switch (ch) {
6841 case 'l':
6842 list_stage = 1;
6843 break;
6844 case 'p':
6845 pflag = 1;
6846 break;
6847 case 'F':
6848 patch_script_path = optarg;
6849 break;
6850 default:
6851 usage_stage();
6852 /* NOTREACHED */
6856 argc -= optind;
6857 argv += optind;
6859 #ifndef PROFILE
6860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6861 "unveil", NULL) == -1)
6862 err(1, "pledge");
6863 #endif
6864 if (list_stage && (pflag || patch_script_path))
6865 errx(1, "-l option cannot be used with other options");
6866 if (patch_script_path && !pflag)
6867 errx(1, "-F option can only be used together with -p option");
6869 cwd = getcwd(NULL, 0);
6870 if (cwd == NULL) {
6871 error = got_error_from_errno("getcwd");
6872 goto done;
6875 error = got_worktree_open(&worktree, cwd);
6876 if (error)
6877 goto done;
6879 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6880 NULL);
6881 if (error != NULL)
6882 goto done;
6884 if (patch_script_path) {
6885 patch_script_file = fopen(patch_script_path, "r");
6886 if (patch_script_file == NULL) {
6887 error = got_error_from_errno2("fopen",
6888 patch_script_path);
6889 goto done;
6892 error = apply_unveil(got_repo_get_path(repo), 0,
6893 got_worktree_get_root_path(worktree));
6894 if (error)
6895 goto done;
6897 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6898 if (error)
6899 goto done;
6901 if (list_stage)
6902 error = got_worktree_status(worktree, &paths, repo,
6903 print_stage, NULL, check_cancelled, NULL);
6904 else {
6905 cpa.patch_script_file = patch_script_file;
6906 cpa.action = "stage";
6907 error = got_worktree_stage(worktree, &paths,
6908 pflag ? NULL : print_status, NULL,
6909 pflag ? choose_patch : NULL, &cpa, repo);
6911 done:
6912 if (patch_script_file && fclose(patch_script_file) == EOF &&
6913 error == NULL)
6914 error = got_error_from_errno2("fclose", patch_script_path);
6915 if (repo)
6916 got_repo_close(repo);
6917 if (worktree)
6918 got_worktree_close(worktree);
6919 TAILQ_FOREACH(pe, &paths, entry)
6920 free((char *)pe->path);
6921 got_pathlist_free(&paths);
6922 free(cwd);
6923 return error;
6926 __dead static void
6927 usage_unstage(void)
6929 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6930 "[file-path ...]\n",
6931 getprogname());
6932 exit(1);
6936 static const struct got_error *
6937 cmd_unstage(int argc, char *argv[])
6939 const struct got_error *error = NULL;
6940 struct got_repository *repo = NULL;
6941 struct got_worktree *worktree = NULL;
6942 char *cwd = NULL;
6943 struct got_pathlist_head paths;
6944 struct got_pathlist_entry *pe;
6945 int ch, did_something = 0, pflag = 0;
6946 FILE *patch_script_file = NULL;
6947 const char *patch_script_path = NULL;
6948 struct choose_patch_arg cpa;
6950 TAILQ_INIT(&paths);
6952 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6953 switch (ch) {
6954 case 'p':
6955 pflag = 1;
6956 break;
6957 case 'F':
6958 patch_script_path = optarg;
6959 break;
6960 default:
6961 usage_unstage();
6962 /* NOTREACHED */
6966 argc -= optind;
6967 argv += optind;
6969 #ifndef PROFILE
6970 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6971 "unveil", NULL) == -1)
6972 err(1, "pledge");
6973 #endif
6974 if (patch_script_path && !pflag)
6975 errx(1, "-F option can only be used together with -p option");
6977 cwd = getcwd(NULL, 0);
6978 if (cwd == NULL) {
6979 error = got_error_from_errno("getcwd");
6980 goto done;
6983 error = got_worktree_open(&worktree, cwd);
6984 if (error)
6985 goto done;
6987 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6988 NULL);
6989 if (error != NULL)
6990 goto done;
6992 if (patch_script_path) {
6993 patch_script_file = fopen(patch_script_path, "r");
6994 if (patch_script_file == NULL) {
6995 error = got_error_from_errno2("fopen",
6996 patch_script_path);
6997 goto done;
7001 error = apply_unveil(got_repo_get_path(repo), 0,
7002 got_worktree_get_root_path(worktree));
7003 if (error)
7004 goto done;
7006 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7007 if (error)
7008 goto done;
7010 cpa.patch_script_file = patch_script_file;
7011 cpa.action = "unstage";
7012 error = got_worktree_unstage(worktree, &paths, update_progress,
7013 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7014 done:
7015 if (patch_script_file && fclose(patch_script_file) == EOF &&
7016 error == NULL)
7017 error = got_error_from_errno2("fclose", patch_script_path);
7018 if (repo)
7019 got_repo_close(repo);
7020 if (worktree)
7021 got_worktree_close(worktree);
7022 TAILQ_FOREACH(pe, &paths, entry)
7023 free((char *)pe->path);
7024 got_pathlist_free(&paths);
7025 free(cwd);
7026 return error;
7029 __dead static void
7030 usage_cat(void)
7032 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7033 "arg1 [arg2 ...]\n", getprogname());
7034 exit(1);
7037 static const struct got_error *
7038 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7040 const struct got_error *err;
7041 struct got_blob_object *blob;
7043 err = got_object_open_as_blob(&blob, repo, id, 8192);
7044 if (err)
7045 return err;
7047 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7048 got_object_blob_close(blob);
7049 return err;
7052 static const struct got_error *
7053 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7055 const struct got_error *err;
7056 struct got_tree_object *tree;
7057 int nentries, i;
7059 err = got_object_open_as_tree(&tree, repo, id);
7060 if (err)
7061 return err;
7063 nentries = got_object_tree_get_nentries(tree);
7064 for (i = 0; i < nentries; i++) {
7065 struct got_tree_entry *te;
7066 char *id_str;
7067 if (sigint_received || sigpipe_received)
7068 break;
7069 te = got_object_tree_get_entry(tree, i);
7070 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7071 if (err)
7072 break;
7073 fprintf(outfile, "%s %.7o %s\n", id_str,
7074 got_tree_entry_get_mode(te),
7075 got_tree_entry_get_name(te));
7076 free(id_str);
7079 got_object_tree_close(tree);
7080 return err;
7083 static const struct got_error *
7084 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7086 const struct got_error *err;
7087 struct got_commit_object *commit;
7088 const struct got_object_id_queue *parent_ids;
7089 struct got_object_qid *pid;
7090 char *id_str = NULL;
7091 const char *logmsg = NULL;
7093 err = got_object_open_as_commit(&commit, repo, id);
7094 if (err)
7095 return err;
7097 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7098 if (err)
7099 goto done;
7101 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7102 parent_ids = got_object_commit_get_parent_ids(commit);
7103 fprintf(outfile, "numparents %d\n",
7104 got_object_commit_get_nparents(commit));
7105 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7106 char *pid_str;
7107 err = got_object_id_str(&pid_str, pid->id);
7108 if (err)
7109 goto done;
7110 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7111 free(pid_str);
7113 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7114 got_object_commit_get_author(commit),
7115 got_object_commit_get_author_time(commit));
7117 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7118 got_object_commit_get_author(commit),
7119 got_object_commit_get_committer_time(commit));
7121 logmsg = got_object_commit_get_logmsg_raw(commit);
7122 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7123 fprintf(outfile, "%s", logmsg);
7124 done:
7125 free(id_str);
7126 got_object_commit_close(commit);
7127 return err;
7130 static const struct got_error *
7131 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7133 const struct got_error *err;
7134 struct got_tag_object *tag;
7135 char *id_str = NULL;
7136 const char *tagmsg = NULL;
7138 err = got_object_open_as_tag(&tag, repo, id);
7139 if (err)
7140 return err;
7142 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7143 if (err)
7144 goto done;
7146 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7148 switch (got_object_tag_get_object_type(tag)) {
7149 case GOT_OBJ_TYPE_BLOB:
7150 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7151 GOT_OBJ_LABEL_BLOB);
7152 break;
7153 case GOT_OBJ_TYPE_TREE:
7154 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7155 GOT_OBJ_LABEL_TREE);
7156 break;
7157 case GOT_OBJ_TYPE_COMMIT:
7158 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7159 GOT_OBJ_LABEL_COMMIT);
7160 break;
7161 case GOT_OBJ_TYPE_TAG:
7162 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7163 GOT_OBJ_LABEL_TAG);
7164 break;
7165 default:
7166 break;
7169 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7170 got_object_tag_get_name(tag));
7172 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7173 got_object_tag_get_tagger(tag),
7174 got_object_tag_get_tagger_time(tag));
7176 tagmsg = got_object_tag_get_message(tag);
7177 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7178 fprintf(outfile, "%s", tagmsg);
7179 done:
7180 free(id_str);
7181 got_object_tag_close(tag);
7182 return err;
7185 static const struct got_error *
7186 cmd_cat(int argc, char *argv[])
7188 const struct got_error *error;
7189 struct got_repository *repo = NULL;
7190 struct got_worktree *worktree = NULL;
7191 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7192 const char *commit_id_str = NULL;
7193 struct got_object_id *id = NULL, *commit_id = NULL;
7194 int ch, obj_type, i, force_path = 0;
7196 #ifndef PROFILE
7197 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7198 NULL) == -1)
7199 err(1, "pledge");
7200 #endif
7202 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7203 switch (ch) {
7204 case 'c':
7205 commit_id_str = optarg;
7206 break;
7207 case 'r':
7208 repo_path = realpath(optarg, NULL);
7209 if (repo_path == NULL)
7210 return got_error_from_errno2("realpath",
7211 optarg);
7212 got_path_strip_trailing_slashes(repo_path);
7213 break;
7214 case 'P':
7215 force_path = 1;
7216 break;
7217 default:
7218 usage_cat();
7219 /* NOTREACHED */
7223 argc -= optind;
7224 argv += optind;
7226 cwd = getcwd(NULL, 0);
7227 if (cwd == NULL) {
7228 error = got_error_from_errno("getcwd");
7229 goto done;
7231 error = got_worktree_open(&worktree, cwd);
7232 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7233 goto done;
7234 if (worktree) {
7235 if (repo_path == NULL) {
7236 repo_path = strdup(
7237 got_worktree_get_repo_path(worktree));
7238 if (repo_path == NULL) {
7239 error = got_error_from_errno("strdup");
7240 goto done;
7245 if (repo_path == NULL) {
7246 repo_path = getcwd(NULL, 0);
7247 if (repo_path == NULL)
7248 return got_error_from_errno("getcwd");
7251 error = got_repo_open(&repo, repo_path, NULL);
7252 free(repo_path);
7253 if (error != NULL)
7254 goto done;
7256 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7257 if (error)
7258 goto done;
7260 if (commit_id_str == NULL)
7261 commit_id_str = GOT_REF_HEAD;
7262 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7263 if (error)
7264 goto done;
7266 for (i = 0; i < argc; i++) {
7267 if (force_path) {
7268 error = got_object_id_by_path(&id, repo, commit_id,
7269 argv[i]);
7270 if (error)
7271 break;
7272 } else {
7273 error = match_object_id(&id, &label, argv[i],
7274 GOT_OBJ_TYPE_ANY, 0, repo);
7275 if (error) {
7276 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7277 error->code != GOT_ERR_NOT_REF)
7278 break;
7279 error = got_object_id_by_path(&id, repo,
7280 commit_id, argv[i]);
7281 if (error)
7282 break;
7286 error = got_object_get_type(&obj_type, repo, id);
7287 if (error)
7288 break;
7290 switch (obj_type) {
7291 case GOT_OBJ_TYPE_BLOB:
7292 error = cat_blob(id, repo, stdout);
7293 break;
7294 case GOT_OBJ_TYPE_TREE:
7295 error = cat_tree(id, repo, stdout);
7296 break;
7297 case GOT_OBJ_TYPE_COMMIT:
7298 error = cat_commit(id, repo, stdout);
7299 break;
7300 case GOT_OBJ_TYPE_TAG:
7301 error = cat_tag(id, repo, stdout);
7302 break;
7303 default:
7304 error = got_error(GOT_ERR_OBJ_TYPE);
7305 break;
7307 if (error)
7308 break;
7309 free(label);
7310 label = NULL;
7311 free(id);
7312 id = NULL;
7315 done:
7316 free(label);
7317 free(id);
7318 free(commit_id);
7319 if (worktree)
7320 got_worktree_close(worktree);
7321 if (repo) {
7322 const struct got_error *repo_error;
7323 repo_error = got_repo_close(repo);
7324 if (error == NULL)
7325 error = repo_error;
7327 return error;