Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <fcntl.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>
38 #include <getopt.h>
40 #include "got_compat.h"
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_path.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
50 #include "got_diff.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_send.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
57 #include "got_gotconfig.h"
58 #include "got_dial.h"
59 #include "got_patch.h"
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 static volatile sig_atomic_t sigint_received;
66 static volatile sig_atomic_t sigpipe_received;
68 static void
69 catch_sigint(int signo)
70 {
71 sigint_received = 1;
72 }
74 static void
75 catch_sigpipe(int signo)
76 {
77 sigpipe_received = 1;
78 }
81 struct got_cmd {
82 const char *cmd_name;
83 const struct got_error *(*cmd_main)(int, char *[]);
84 void (*cmd_usage)(void);
85 const char *cmd_alias;
86 };
88 __dead static void usage(int, int);
89 __dead static void usage_init(void);
90 __dead static void usage_import(void);
91 __dead static void usage_clone(void);
92 __dead static void usage_fetch(void);
93 __dead static void usage_checkout(void);
94 __dead static void usage_update(void);
95 __dead static void usage_log(void);
96 __dead static void usage_diff(void);
97 __dead static void usage_blame(void);
98 __dead static void usage_tree(void);
99 __dead static void usage_status(void);
100 __dead static void usage_ref(void);
101 __dead static void usage_branch(void);
102 __dead static void usage_tag(void);
103 __dead static void usage_add(void);
104 __dead static void usage_remove(void);
105 __dead static void usage_patch(void);
106 __dead static void usage_revert(void);
107 __dead static void usage_commit(void);
108 __dead static void usage_send(void);
109 __dead static void usage_cherrypick(void);
110 __dead static void usage_backout(void);
111 __dead static void usage_rebase(void);
112 __dead static void usage_histedit(void);
113 __dead static void usage_integrate(void);
114 __dead static void usage_merge(void);
115 __dead static void usage_stage(void);
116 __dead static void usage_unstage(void);
117 __dead static void usage_cat(void);
118 __dead static void usage_info(void);
120 static const struct got_error* cmd_init(int, char *[]);
121 static const struct got_error* cmd_import(int, char *[]);
122 static const struct got_error* cmd_clone(int, char *[]);
123 static const struct got_error* cmd_fetch(int, char *[]);
124 static const struct got_error* cmd_checkout(int, char *[]);
125 static const struct got_error* cmd_update(int, char *[]);
126 static const struct got_error* cmd_log(int, char *[]);
127 static const struct got_error* cmd_diff(int, char *[]);
128 static const struct got_error* cmd_blame(int, char *[]);
129 static const struct got_error* cmd_tree(int, char *[]);
130 static const struct got_error* cmd_status(int, char *[]);
131 static const struct got_error* cmd_ref(int, char *[]);
132 static const struct got_error* cmd_branch(int, char *[]);
133 static const struct got_error* cmd_tag(int, char *[]);
134 static const struct got_error* cmd_add(int, char *[]);
135 static const struct got_error* cmd_remove(int, char *[]);
136 static const struct got_error* cmd_patch(int, char *[]);
137 static const struct got_error* cmd_revert(int, char *[]);
138 static const struct got_error* cmd_commit(int, char *[]);
139 static const struct got_error* cmd_send(int, char *[]);
140 static const struct got_error* cmd_cherrypick(int, char *[]);
141 static const struct got_error* cmd_backout(int, char *[]);
142 static const struct got_error* cmd_rebase(int, char *[]);
143 static const struct got_error* cmd_histedit(int, char *[]);
144 static const struct got_error* cmd_integrate(int, char *[]);
145 static const struct got_error* cmd_merge(int, char *[]);
146 static const struct got_error* cmd_stage(int, char *[]);
147 static const struct got_error* cmd_unstage(int, char *[]);
148 static const struct got_error* cmd_cat(int, char *[]);
149 static const struct got_error* cmd_info(int, char *[]);
151 static const struct got_cmd got_commands[] = {
152 { "init", cmd_init, usage_init, "" },
153 { "import", cmd_import, usage_import, "im" },
154 { "clone", cmd_clone, usage_clone, "cl" },
155 { "fetch", cmd_fetch, usage_fetch, "fe" },
156 { "checkout", cmd_checkout, usage_checkout, "co" },
157 { "update", cmd_update, usage_update, "up" },
158 { "log", cmd_log, usage_log, "" },
159 { "diff", cmd_diff, usage_diff, "di" },
160 { "blame", cmd_blame, usage_blame, "bl" },
161 { "tree", cmd_tree, usage_tree, "tr" },
162 { "status", cmd_status, usage_status, "st" },
163 { "ref", cmd_ref, usage_ref, "" },
164 { "branch", cmd_branch, usage_branch, "br" },
165 { "tag", cmd_tag, usage_tag, "" },
166 { "add", cmd_add, usage_add, "" },
167 { "remove", cmd_remove, usage_remove, "rm" },
168 { "patch", cmd_patch, usage_patch, "pa" },
169 { "revert", cmd_revert, usage_revert, "rv" },
170 { "commit", cmd_commit, usage_commit, "ci" },
171 { "send", cmd_send, usage_send, "se" },
172 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
173 { "backout", cmd_backout, usage_backout, "bo" },
174 { "rebase", cmd_rebase, usage_rebase, "rb" },
175 { "histedit", cmd_histedit, usage_histedit, "he" },
176 { "integrate", cmd_integrate, usage_integrate,"ig" },
177 { "merge", cmd_merge, usage_merge, "mg" },
178 { "stage", cmd_stage, usage_stage, "sg" },
179 { "unstage", cmd_unstage, usage_unstage, "ug" },
180 { "cat", cmd_cat, usage_cat, "" },
181 { "info", cmd_info, usage_info, "" },
182 };
184 static void
185 list_commands(FILE *fp)
187 size_t i;
189 fprintf(fp, "commands:");
190 for (i = 0; i < nitems(got_commands); i++) {
191 const struct got_cmd *cmd = &got_commands[i];
192 fprintf(fp, " %s", cmd->cmd_name);
194 fputc('\n', fp);
197 __dead static void
198 option_conflict(char a, char b)
200 errx(1, "-%c and -%c options are mutually exclusive", a, b);
203 int
204 main(int argc, char *argv[])
206 const struct got_cmd *cmd;
207 size_t i;
208 int ch;
209 int hflag = 0, Vflag = 0;
210 static const struct option longopts[] = {
211 { "version", no_argument, NULL, 'V' },
212 { NULL, 0, NULL, 0 }
213 };
215 setlocale(LC_CTYPE, "");
217 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
218 switch (ch) {
219 case 'h':
220 hflag = 1;
221 break;
222 case 'V':
223 Vflag = 1;
224 break;
225 default:
226 usage(hflag, 1);
227 /* NOTREACHED */
231 argc -= optind;
232 argv += optind;
233 optind = 1;
234 optreset = 1;
236 if (Vflag) {
237 got_version_print_str();
238 return 0;
241 if (argc <= 0)
242 usage(hflag, hflag ? 0 : 1);
244 signal(SIGINT, catch_sigint);
245 signal(SIGPIPE, catch_sigpipe);
247 for (i = 0; i < nitems(got_commands); i++) {
248 const struct got_error *error;
250 cmd = &got_commands[i];
252 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
253 strcmp(cmd->cmd_alias, argv[0]) != 0)
254 continue;
256 if (hflag)
257 cmd->cmd_usage();
259 error = cmd->cmd_main(argc, argv);
260 if (error && error->code != GOT_ERR_CANCELLED &&
261 error->code != GOT_ERR_PRIVSEP_EXIT &&
262 !(sigpipe_received &&
263 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
264 !(sigint_received &&
265 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
266 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
267 return 1;
270 return 0;
273 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
274 list_commands(stderr);
275 return 1;
278 __dead static void
279 usage(int hflag, int status)
281 FILE *fp = (status == 0) ? stdout : stderr;
283 fprintf(fp, "usage: %s [-h] [-V | --version] command [arg ...]\n",
284 getprogname());
285 if (hflag)
286 list_commands(fp);
287 exit(status);
290 static const struct got_error *
291 get_editor(char **abspath)
293 const struct got_error *err = NULL;
294 const char *editor;
296 *abspath = NULL;
298 editor = getenv("VISUAL");
299 if (editor == NULL)
300 editor = getenv("EDITOR");
302 if (editor) {
303 err = got_path_find_prog(abspath, editor);
304 if (err)
305 return err;
308 if (*abspath == NULL) {
309 *abspath = strdup("/bin/ed");
310 if (*abspath == NULL)
311 return got_error_from_errno("strdup");
314 return NULL;
317 static const struct got_error *
318 apply_unveil(const char *repo_path, int repo_read_only,
319 const char *worktree_path)
321 const struct got_error *err;
323 #ifdef PROFILE
324 if (unveil("gmon.out", "rwc") != 0)
325 return got_error_from_errno2("unveil", "gmon.out");
326 #endif
327 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
328 return got_error_from_errno2("unveil", repo_path);
330 if (worktree_path && unveil(worktree_path, "rwc") != 0)
331 return got_error_from_errno2("unveil", worktree_path);
333 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
334 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
336 err = got_privsep_unveil_exec_helpers();
337 if (err != NULL)
338 return err;
340 if (unveil(NULL, NULL) != 0)
341 return got_error_from_errno("unveil");
343 return NULL;
346 __dead static void
347 usage_init(void)
349 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
350 exit(1);
353 static const struct got_error *
354 cmd_init(int argc, char *argv[])
356 const struct got_error *error = NULL;
357 char *repo_path = NULL;
358 int ch;
360 while ((ch = getopt(argc, argv, "")) != -1) {
361 switch (ch) {
362 default:
363 usage_init();
364 /* NOTREACHED */
368 argc -= optind;
369 argv += optind;
371 #ifndef PROFILE
372 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
373 err(1, "pledge");
374 #endif
375 if (argc != 1)
376 usage_init();
378 repo_path = strdup(argv[0]);
379 if (repo_path == NULL)
380 return got_error_from_errno("strdup");
382 got_path_strip_trailing_slashes(repo_path);
384 error = got_path_mkdir(repo_path);
385 if (error &&
386 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
387 goto done;
389 error = apply_unveil(repo_path, 0, NULL);
390 if (error)
391 goto done;
393 error = got_repo_init(repo_path);
394 done:
395 free(repo_path);
396 return error;
399 __dead static void
400 usage_import(void)
402 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
403 "[-r repository-path] [-I pattern] path\n", getprogname());
404 exit(1);
407 static int
408 spawn_editor(const char *editor, const char *file)
410 pid_t pid;
411 sig_t sighup, sigint, sigquit;
412 int st = -1;
414 sighup = signal(SIGHUP, SIG_IGN);
415 sigint = signal(SIGINT, SIG_IGN);
416 sigquit = signal(SIGQUIT, SIG_IGN);
418 switch (pid = fork()) {
419 case -1:
420 goto doneediting;
421 case 0:
422 execl(editor, editor, file, (char *)NULL);
423 _exit(127);
426 while (waitpid(pid, &st, 0) == -1)
427 if (errno != EINTR)
428 break;
430 doneediting:
431 (void)signal(SIGHUP, sighup);
432 (void)signal(SIGINT, sigint);
433 (void)signal(SIGQUIT, sigquit);
435 if (!WIFEXITED(st)) {
436 errno = EINTR;
437 return -1;
440 return WEXITSTATUS(st);
443 static const struct got_error *
444 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
445 const char *initial_content, size_t initial_content_len,
446 int require_modification)
448 const struct got_error *err = NULL;
449 char *line = NULL;
450 size_t linesize = 0;
451 ssize_t linelen;
452 struct stat st, st2;
453 FILE *fp = NULL;
454 size_t len, logmsg_len;
455 char *initial_content_stripped = NULL, *buf = NULL, *s;
457 *logmsg = NULL;
459 if (stat(logmsg_path, &st) == -1)
460 return got_error_from_errno2("stat", logmsg_path);
462 if (spawn_editor(editor, logmsg_path) == -1)
463 return got_error_from_errno("failed spawning editor");
465 if (stat(logmsg_path, &st2) == -1)
466 return got_error_from_errno("stat");
468 if (require_modification &&
469 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
470 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
471 "no changes made to commit message, aborting");
473 /*
474 * Set up a stripped version of the initial content without comments
475 * and blank lines. We need this in order to check if the message
476 * has in fact been edited.
477 */
478 initial_content_stripped = malloc(initial_content_len + 1);
479 if (initial_content_stripped == NULL)
480 return got_error_from_errno("malloc");
481 initial_content_stripped[0] = '\0';
483 buf = strdup(initial_content);
484 if (buf == NULL) {
485 err = got_error_from_errno("strdup");
486 goto done;
488 s = buf;
489 len = 0;
490 while ((line = strsep(&s, "\n")) != NULL) {
491 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
492 continue; /* remove comments and leading empty lines */
493 len = strlcat(initial_content_stripped, line,
494 initial_content_len + 1);
495 if (len >= initial_content_len + 1) {
496 err = got_error(GOT_ERR_NO_SPACE);
497 goto done;
500 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
501 initial_content_stripped[len - 1] = '\0';
502 len--;
505 logmsg_len = st2.st_size;
506 *logmsg = malloc(logmsg_len + 1);
507 if (*logmsg == NULL)
508 return got_error_from_errno("malloc");
509 (*logmsg)[0] = '\0';
511 fp = fopen(logmsg_path, "re");
512 if (fp == NULL) {
513 err = got_error_from_errno("fopen");
514 goto done;
517 len = 0;
518 while ((linelen = getline(&line, &linesize, fp)) != -1) {
519 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
520 continue; /* remove comments and leading empty lines */
521 len = strlcat(*logmsg, line, logmsg_len + 1);
522 if (len >= logmsg_len + 1) {
523 err = got_error(GOT_ERR_NO_SPACE);
524 goto done;
527 free(line);
528 if (ferror(fp)) {
529 err = got_ferror(fp, GOT_ERR_IO);
530 goto done;
532 while (len > 0 && (*logmsg)[len - 1] == '\n') {
533 (*logmsg)[len - 1] = '\0';
534 len--;
537 if (len == 0) {
538 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
539 "commit message cannot be empty, aborting");
540 goto done;
542 if (require_modification &&
543 strcmp(*logmsg, initial_content_stripped) == 0)
544 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
545 "no changes made to commit message, aborting");
546 done:
547 free(initial_content_stripped);
548 free(buf);
549 if (fp && fclose(fp) == EOF && err == NULL)
550 err = got_error_from_errno("fclose");
551 if (err) {
552 free(*logmsg);
553 *logmsg = NULL;
555 return err;
558 static const struct got_error *
559 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
560 const char *path_dir, const char *branch_name)
562 char *initial_content = NULL;
563 const struct got_error *err = NULL;
564 int initial_content_len;
565 int fd = -1;
567 initial_content_len = asprintf(&initial_content,
568 "\n# %s to be imported to branch %s\n", path_dir,
569 branch_name);
570 if (initial_content_len == -1)
571 return got_error_from_errno("asprintf");
573 err = got_opentemp_named_fd(logmsg_path, &fd,
574 GOT_TMPDIR_STR "/got-importmsg");
575 if (err)
576 goto done;
578 if (write(fd, initial_content, initial_content_len) == -1) {
579 err = got_error_from_errno2("write", *logmsg_path);
580 goto done;
583 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
584 initial_content_len, 1);
585 done:
586 if (fd != -1 && close(fd) == -1 && err == NULL)
587 err = got_error_from_errno2("close", *logmsg_path);
588 free(initial_content);
589 if (err) {
590 free(*logmsg_path);
591 *logmsg_path = NULL;
593 return err;
596 static const struct got_error *
597 import_progress(void *arg, const char *path)
599 printf("A %s\n", path);
600 return NULL;
603 static int
604 valid_author(const char *author)
606 /*
607 * Really dumb email address check; we're only doing this to
608 * avoid git's object parser breaking on commits we create.
609 */
610 while (*author && *author != '<')
611 author++;
612 if (*author != '<')
613 return 0;
614 while (*author && *author != '@')
615 author++;
616 if (*author != '@')
617 return 0;
618 while (*author && *author != '>')
619 author++;
620 return *author == '>';
623 static const struct got_error *
624 get_author(char **author, struct got_repository *repo,
625 struct got_worktree *worktree)
627 const struct got_error *err = NULL;
628 const char *got_author = NULL, *name, *email;
629 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
631 *author = NULL;
633 if (worktree)
634 worktree_conf = got_worktree_get_gotconfig(worktree);
635 repo_conf = got_repo_get_gotconfig(repo);
637 /*
638 * Priority of potential author information sources, from most
639 * significant to least significant:
640 * 1) work tree's .got/got.conf file
641 * 2) repository's got.conf file
642 * 3) repository's git config file
643 * 4) environment variables
644 * 5) global git config files (in user's home directory or /etc)
645 */
647 if (worktree_conf)
648 got_author = got_gotconfig_get_author(worktree_conf);
649 if (got_author == NULL)
650 got_author = got_gotconfig_get_author(repo_conf);
651 if (got_author == NULL) {
652 name = got_repo_get_gitconfig_author_name(repo);
653 email = got_repo_get_gitconfig_author_email(repo);
654 if (name && email) {
655 if (asprintf(author, "%s <%s>", name, email) == -1)
656 return got_error_from_errno("asprintf");
657 return NULL;
660 got_author = getenv("GOT_AUTHOR");
661 if (got_author == NULL) {
662 name = got_repo_get_global_gitconfig_author_name(repo);
663 email = got_repo_get_global_gitconfig_author_email(
664 repo);
665 if (name && email) {
666 if (asprintf(author, "%s <%s>", name, email)
667 == -1)
668 return got_error_from_errno("asprintf");
669 return NULL;
671 /* TODO: Look up user in password database? */
672 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
676 *author = strdup(got_author);
677 if (*author == NULL)
678 return got_error_from_errno("strdup");
680 if (!valid_author(*author)) {
681 err = got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", *author);
682 free(*author);
683 *author = NULL;
685 return err;
688 static const struct got_error *
689 get_gitconfig_path(char **gitconfig_path)
691 const char *homedir = getenv("HOME");
693 *gitconfig_path = NULL;
694 if (homedir) {
695 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
696 return got_error_from_errno("asprintf");
699 return NULL;
702 static const struct got_error *
703 cmd_import(int argc, char *argv[])
705 const struct got_error *error = NULL;
706 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
707 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
708 const char *branch_name = "main";
709 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
710 struct got_repository *repo = NULL;
711 struct got_reference *branch_ref = NULL, *head_ref = NULL;
712 struct got_object_id *new_commit_id = NULL;
713 int ch;
714 struct got_pathlist_head ignores;
715 struct got_pathlist_entry *pe;
716 int preserve_logmsg = 0;
717 int *pack_fds = NULL;
719 TAILQ_INIT(&ignores);
721 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
722 switch (ch) {
723 case 'b':
724 branch_name = optarg;
725 break;
726 case 'm':
727 logmsg = strdup(optarg);
728 if (logmsg == NULL) {
729 error = got_error_from_errno("strdup");
730 goto done;
732 break;
733 case 'r':
734 repo_path = realpath(optarg, NULL);
735 if (repo_path == NULL) {
736 error = got_error_from_errno2("realpath",
737 optarg);
738 goto done;
740 break;
741 case 'I':
742 if (optarg[0] == '\0')
743 break;
744 error = got_pathlist_insert(&pe, &ignores, optarg,
745 NULL);
746 if (error)
747 goto done;
748 break;
749 default:
750 usage_import();
751 /* NOTREACHED */
755 argc -= optind;
756 argv += optind;
758 #ifndef PROFILE
759 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
760 "unveil",
761 NULL) == -1)
762 err(1, "pledge");
763 #endif
764 if (argc != 1)
765 usage_import();
767 if (repo_path == NULL) {
768 repo_path = getcwd(NULL, 0);
769 if (repo_path == NULL)
770 return got_error_from_errno("getcwd");
772 got_path_strip_trailing_slashes(repo_path);
773 error = get_gitconfig_path(&gitconfig_path);
774 if (error)
775 goto done;
776 error = got_repo_pack_fds_open(&pack_fds);
777 if (error != NULL)
778 goto done;
779 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
780 if (error)
781 goto done;
783 error = get_author(&author, repo, NULL);
784 if (error)
785 return error;
787 /*
788 * Don't let the user create a branch name with a leading '-'.
789 * While technically a valid reference name, this case is usually
790 * an unintended typo.
791 */
792 if (branch_name[0] == '-')
793 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
795 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
796 error = got_error_from_errno("asprintf");
797 goto done;
800 error = got_ref_open(&branch_ref, repo, refname, 0);
801 if (error) {
802 if (error->code != GOT_ERR_NOT_REF)
803 goto done;
804 } else {
805 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
806 "import target branch already exists");
807 goto done;
810 path_dir = realpath(argv[0], NULL);
811 if (path_dir == NULL) {
812 error = got_error_from_errno2("realpath", argv[0]);
813 goto done;
815 got_path_strip_trailing_slashes(path_dir);
817 /*
818 * unveil(2) traverses exec(2); if an editor is used we have
819 * to apply unveil after the log message has been written.
820 */
821 if (logmsg == NULL || strlen(logmsg) == 0) {
822 error = get_editor(&editor);
823 if (error)
824 goto done;
825 free(logmsg);
826 error = collect_import_msg(&logmsg, &logmsg_path, editor,
827 path_dir, refname);
828 if (error) {
829 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
830 logmsg_path != NULL)
831 preserve_logmsg = 1;
832 goto done;
836 if (unveil(path_dir, "r") != 0) {
837 error = got_error_from_errno2("unveil", path_dir);
838 if (logmsg_path)
839 preserve_logmsg = 1;
840 goto done;
843 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
844 if (error) {
845 if (logmsg_path)
846 preserve_logmsg = 1;
847 goto done;
850 error = got_repo_import(&new_commit_id, path_dir, logmsg,
851 author, &ignores, repo, import_progress, NULL);
852 if (error) {
853 if (logmsg_path)
854 preserve_logmsg = 1;
855 goto done;
858 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
859 if (error) {
860 if (logmsg_path)
861 preserve_logmsg = 1;
862 goto done;
865 error = got_ref_write(branch_ref, repo);
866 if (error) {
867 if (logmsg_path)
868 preserve_logmsg = 1;
869 goto done;
872 error = got_object_id_str(&id_str, new_commit_id);
873 if (error) {
874 if (logmsg_path)
875 preserve_logmsg = 1;
876 goto done;
879 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
880 if (error) {
881 if (error->code != GOT_ERR_NOT_REF) {
882 if (logmsg_path)
883 preserve_logmsg = 1;
884 goto done;
887 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
888 branch_ref);
889 if (error) {
890 if (logmsg_path)
891 preserve_logmsg = 1;
892 goto done;
895 error = got_ref_write(head_ref, repo);
896 if (error) {
897 if (logmsg_path)
898 preserve_logmsg = 1;
899 goto done;
903 printf("Created branch %s with commit %s\n",
904 got_ref_get_name(branch_ref), id_str);
905 done:
906 if (pack_fds) {
907 const struct got_error *pack_err =
908 got_repo_pack_fds_close(pack_fds);
909 if (error == NULL)
910 error = pack_err;
912 if (preserve_logmsg) {
913 fprintf(stderr, "%s: log message preserved in %s\n",
914 getprogname(), logmsg_path);
915 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
916 error = got_error_from_errno2("unlink", logmsg_path);
917 free(logmsg);
918 free(logmsg_path);
919 free(repo_path);
920 free(editor);
921 free(refname);
922 free(new_commit_id);
923 free(id_str);
924 free(author);
925 free(gitconfig_path);
926 if (branch_ref)
927 got_ref_close(branch_ref);
928 if (head_ref)
929 got_ref_close(head_ref);
930 return error;
933 __dead static void
934 usage_clone(void)
936 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
937 "[-R reference] repository-url [directory]\n", getprogname());
938 exit(1);
941 struct got_fetch_progress_arg {
942 char last_scaled_size[FMT_SCALED_STRSIZE];
943 int last_p_indexed;
944 int last_p_resolved;
945 int verbosity;
947 struct got_repository *repo;
949 int create_configs;
950 int configs_created;
951 struct {
952 struct got_pathlist_head *symrefs;
953 struct got_pathlist_head *wanted_branches;
954 struct got_pathlist_head *wanted_refs;
955 const char *proto;
956 const char *host;
957 const char *port;
958 const char *remote_repo_path;
959 const char *git_url;
960 int fetch_all_branches;
961 int mirror_references;
962 } config_info;
963 };
965 /* XXX forward declaration */
966 static const struct got_error *
967 create_config_files(const char *proto, const char *host, const char *port,
968 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
969 int mirror_references, struct got_pathlist_head *symrefs,
970 struct got_pathlist_head *wanted_branches,
971 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
973 static const struct got_error *
974 fetch_progress(void *arg, const char *message, off_t packfile_size,
975 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
977 const struct got_error *err = NULL;
978 struct got_fetch_progress_arg *a = arg;
979 char scaled_size[FMT_SCALED_STRSIZE];
980 int p_indexed, p_resolved;
981 int print_size = 0, print_indexed = 0, print_resolved = 0;
983 /*
984 * In order to allow a failed clone to be resumed with 'got fetch'
985 * we try to create configuration files as soon as possible.
986 * Once the server has sent information about its default branch
987 * we have all required information.
988 */
989 if (a->create_configs && !a->configs_created &&
990 !TAILQ_EMPTY(a->config_info.symrefs)) {
991 err = create_config_files(a->config_info.proto,
992 a->config_info.host, a->config_info.port,
993 a->config_info.remote_repo_path,
994 a->config_info.git_url,
995 a->config_info.fetch_all_branches,
996 a->config_info.mirror_references,
997 a->config_info.symrefs,
998 a->config_info.wanted_branches,
999 a->config_info.wanted_refs, a->repo);
1000 if (err)
1001 return err;
1002 a->configs_created = 1;
1005 if (a->verbosity < 0)
1006 return NULL;
1008 if (message && message[0] != '\0') {
1009 printf("\rserver: %s", message);
1010 fflush(stdout);
1011 return NULL;
1014 if (packfile_size > 0 || nobj_indexed > 0) {
1015 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1016 (a->last_scaled_size[0] == '\0' ||
1017 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1018 print_size = 1;
1019 if (strlcpy(a->last_scaled_size, scaled_size,
1020 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1021 return got_error(GOT_ERR_NO_SPACE);
1023 if (nobj_indexed > 0) {
1024 p_indexed = (nobj_indexed * 100) / nobj_total;
1025 if (p_indexed != a->last_p_indexed) {
1026 a->last_p_indexed = p_indexed;
1027 print_indexed = 1;
1028 print_size = 1;
1031 if (nobj_resolved > 0) {
1032 p_resolved = (nobj_resolved * 100) /
1033 (nobj_total - nobj_loose);
1034 if (p_resolved != a->last_p_resolved) {
1035 a->last_p_resolved = p_resolved;
1036 print_resolved = 1;
1037 print_indexed = 1;
1038 print_size = 1;
1043 if (print_size || print_indexed || print_resolved)
1044 printf("\r");
1045 if (print_size)
1046 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1047 if (print_indexed)
1048 printf("; indexing %d%%", p_indexed);
1049 if (print_resolved)
1050 printf("; resolving deltas %d%%", p_resolved);
1051 if (print_size || print_indexed || print_resolved)
1052 fflush(stdout);
1054 return NULL;
1057 static const struct got_error *
1058 create_symref(const char *refname, struct got_reference *target_ref,
1059 int verbosity, struct got_repository *repo)
1061 const struct got_error *err;
1062 struct got_reference *head_symref;
1064 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1065 if (err)
1066 return err;
1068 err = got_ref_write(head_symref, repo);
1069 if (err == NULL && verbosity > 0) {
1070 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1071 got_ref_get_name(target_ref));
1073 got_ref_close(head_symref);
1074 return err;
1077 static const struct got_error *
1078 list_remote_refs(struct got_pathlist_head *symrefs,
1079 struct got_pathlist_head *refs)
1081 const struct got_error *err;
1082 struct got_pathlist_entry *pe;
1084 TAILQ_FOREACH(pe, symrefs, entry) {
1085 const char *refname = pe->path;
1086 const char *targetref = pe->data;
1088 printf("%s: %s\n", refname, targetref);
1091 TAILQ_FOREACH(pe, refs, entry) {
1092 const char *refname = pe->path;
1093 struct got_object_id *id = pe->data;
1094 char *id_str;
1096 err = got_object_id_str(&id_str, id);
1097 if (err)
1098 return err;
1099 printf("%s: %s\n", refname, id_str);
1100 free(id_str);
1103 return NULL;
1106 static const struct got_error *
1107 create_ref(const char *refname, struct got_object_id *id,
1108 int verbosity, struct got_repository *repo)
1110 const struct got_error *err = NULL;
1111 struct got_reference *ref;
1112 char *id_str;
1114 err = got_object_id_str(&id_str, id);
1115 if (err)
1116 return err;
1118 err = got_ref_alloc(&ref, refname, id);
1119 if (err)
1120 goto done;
1122 err = got_ref_write(ref, repo);
1123 got_ref_close(ref);
1125 if (err == NULL && verbosity >= 0)
1126 printf("Created reference %s: %s\n", refname, id_str);
1127 done:
1128 free(id_str);
1129 return err;
1132 static int
1133 match_wanted_ref(const char *refname, const char *wanted_ref)
1135 if (strncmp(refname, "refs/", 5) != 0)
1136 return 0;
1137 refname += 5;
1140 * Prevent fetching of references that won't make any
1141 * sense outside of the remote repository's context.
1143 if (strncmp(refname, "got/", 4) == 0)
1144 return 0;
1145 if (strncmp(refname, "remotes/", 8) == 0)
1146 return 0;
1148 if (strncmp(wanted_ref, "refs/", 5) == 0)
1149 wanted_ref += 5;
1151 /* Allow prefix match. */
1152 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1153 return 1;
1155 /* Allow exact match. */
1156 return (strcmp(refname, wanted_ref) == 0);
1159 static int
1160 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1162 struct got_pathlist_entry *pe;
1164 TAILQ_FOREACH(pe, wanted_refs, entry) {
1165 if (match_wanted_ref(refname, pe->path))
1166 return 1;
1169 return 0;
1172 static const struct got_error *
1173 create_wanted_ref(const char *refname, struct got_object_id *id,
1174 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1176 const struct got_error *err;
1177 char *remote_refname;
1179 if (strncmp("refs/", refname, 5) == 0)
1180 refname += 5;
1182 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1183 remote_repo_name, refname) == -1)
1184 return got_error_from_errno("asprintf");
1186 err = create_ref(remote_refname, id, verbosity, repo);
1187 free(remote_refname);
1188 return err;
1191 static const struct got_error *
1192 create_gotconfig(const char *proto, const char *host, const char *port,
1193 const char *remote_repo_path, const char *default_branch,
1194 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1195 struct got_pathlist_head *wanted_refs, int mirror_references,
1196 struct got_repository *repo)
1198 const struct got_error *err = NULL;
1199 char *gotconfig_path = NULL;
1200 char *gotconfig = NULL;
1201 FILE *gotconfig_file = NULL;
1202 const char *branchname = NULL;
1203 char *branches = NULL, *refs = NULL;
1204 ssize_t n;
1206 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1207 struct got_pathlist_entry *pe;
1208 TAILQ_FOREACH(pe, wanted_branches, entry) {
1209 char *s;
1210 branchname = pe->path;
1211 if (strncmp(branchname, "refs/heads/", 11) == 0)
1212 branchname += 11;
1213 if (asprintf(&s, "%s\"%s\" ",
1214 branches ? branches : "", branchname) == -1) {
1215 err = got_error_from_errno("asprintf");
1216 goto done;
1218 free(branches);
1219 branches = s;
1221 } else if (!fetch_all_branches && default_branch) {
1222 branchname = default_branch;
1223 if (strncmp(branchname, "refs/heads/", 11) == 0)
1224 branchname += 11;
1225 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1226 err = got_error_from_errno("asprintf");
1227 goto done;
1230 if (!TAILQ_EMPTY(wanted_refs)) {
1231 struct got_pathlist_entry *pe;
1232 TAILQ_FOREACH(pe, wanted_refs, entry) {
1233 char *s;
1234 const char *refname = pe->path;
1235 if (strncmp(refname, "refs/", 5) == 0)
1236 branchname += 5;
1237 if (asprintf(&s, "%s\"%s\" ",
1238 refs ? refs : "", refname) == -1) {
1239 err = got_error_from_errno("asprintf");
1240 goto done;
1242 free(refs);
1243 refs = s;
1247 /* Create got.conf(5). */
1248 gotconfig_path = got_repo_get_path_gotconfig(repo);
1249 if (gotconfig_path == NULL) {
1250 err = got_error_from_errno("got_repo_get_path_gotconfig");
1251 goto done;
1253 gotconfig_file = fopen(gotconfig_path, "ae");
1254 if (gotconfig_file == NULL) {
1255 err = got_error_from_errno2("fopen", gotconfig_path);
1256 goto done;
1258 if (asprintf(&gotconfig,
1259 "remote \"%s\" {\n"
1260 "\tserver %s\n"
1261 "\tprotocol %s\n"
1262 "%s%s%s"
1263 "\trepository \"%s\"\n"
1264 "%s%s%s"
1265 "%s%s%s"
1266 "%s"
1267 "%s"
1268 "}\n",
1269 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1270 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1271 remote_repo_path, branches ? "\tbranch { " : "",
1272 branches ? branches : "", branches ? "}\n" : "",
1273 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1274 mirror_references ? "\tmirror-references yes\n" : "",
1275 fetch_all_branches ? "\tfetch-all-branches yes\n" : "") == -1) {
1276 err = got_error_from_errno("asprintf");
1277 goto done;
1279 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1280 if (n != strlen(gotconfig)) {
1281 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1282 goto done;
1285 done:
1286 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1287 err = got_error_from_errno2("fclose", gotconfig_path);
1288 free(gotconfig_path);
1289 free(branches);
1290 return err;
1293 static const struct got_error *
1294 create_gitconfig(const char *git_url, const char *default_branch,
1295 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1296 struct got_pathlist_head *wanted_refs, int mirror_references,
1297 struct got_repository *repo)
1299 const struct got_error *err = NULL;
1300 char *gitconfig_path = NULL;
1301 char *gitconfig = NULL;
1302 FILE *gitconfig_file = NULL;
1303 char *branches = NULL, *refs = NULL;
1304 const char *branchname;
1305 ssize_t n;
1307 /* Create a config file Git can understand. */
1308 gitconfig_path = got_repo_get_path_gitconfig(repo);
1309 if (gitconfig_path == NULL) {
1310 err = got_error_from_errno("got_repo_get_path_gitconfig");
1311 goto done;
1313 gitconfig_file = fopen(gitconfig_path, "ae");
1314 if (gitconfig_file == NULL) {
1315 err = got_error_from_errno2("fopen", gitconfig_path);
1316 goto done;
1318 if (fetch_all_branches) {
1319 if (mirror_references) {
1320 if (asprintf(&branches,
1321 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1322 err = got_error_from_errno("asprintf");
1323 goto done;
1325 } else if (asprintf(&branches,
1326 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1327 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1328 err = got_error_from_errno("asprintf");
1329 goto done;
1331 } else if (!TAILQ_EMPTY(wanted_branches)) {
1332 struct got_pathlist_entry *pe;
1333 TAILQ_FOREACH(pe, wanted_branches, entry) {
1334 char *s;
1335 branchname = pe->path;
1336 if (strncmp(branchname, "refs/heads/", 11) == 0)
1337 branchname += 11;
1338 if (mirror_references) {
1339 if (asprintf(&s,
1340 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1341 branches ? branches : "",
1342 branchname, branchname) == -1) {
1343 err = got_error_from_errno("asprintf");
1344 goto done;
1346 } else if (asprintf(&s,
1347 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1348 branches ? branches : "",
1349 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1350 branchname) == -1) {
1351 err = got_error_from_errno("asprintf");
1352 goto done;
1354 free(branches);
1355 branches = s;
1357 } else {
1359 * If the server specified a default branch, use just that one.
1360 * Otherwise fall back to fetching all branches on next fetch.
1362 if (default_branch) {
1363 branchname = default_branch;
1364 if (strncmp(branchname, "refs/heads/", 11) == 0)
1365 branchname += 11;
1366 } else
1367 branchname = "*"; /* fall back to all branches */
1368 if (mirror_references) {
1369 if (asprintf(&branches,
1370 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1371 branchname, branchname) == -1) {
1372 err = got_error_from_errno("asprintf");
1373 goto done;
1375 } else if (asprintf(&branches,
1376 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1377 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1378 branchname) == -1) {
1379 err = got_error_from_errno("asprintf");
1380 goto done;
1383 if (!TAILQ_EMPTY(wanted_refs)) {
1384 struct got_pathlist_entry *pe;
1385 TAILQ_FOREACH(pe, wanted_refs, entry) {
1386 char *s;
1387 const char *refname = pe->path;
1388 if (strncmp(refname, "refs/", 5) == 0)
1389 refname += 5;
1390 if (mirror_references) {
1391 if (asprintf(&s,
1392 "%s\tfetch = refs/%s:refs/%s\n",
1393 refs ? refs : "", refname, refname) == -1) {
1394 err = got_error_from_errno("asprintf");
1395 goto done;
1397 } else if (asprintf(&s,
1398 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1399 refs ? refs : "",
1400 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1401 refname) == -1) {
1402 err = got_error_from_errno("asprintf");
1403 goto done;
1405 free(refs);
1406 refs = s;
1410 if (asprintf(&gitconfig,
1411 "[remote \"%s\"]\n"
1412 "\turl = %s\n"
1413 "%s"
1414 "%s"
1415 "\tfetch = refs/tags/*:refs/tags/*\n",
1416 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1417 refs ? refs : "") == -1) {
1418 err = got_error_from_errno("asprintf");
1419 goto done;
1421 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1422 if (n != strlen(gitconfig)) {
1423 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1424 goto done;
1426 done:
1427 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1428 err = got_error_from_errno2("fclose", gitconfig_path);
1429 free(gitconfig_path);
1430 free(branches);
1431 return err;
1434 static const struct got_error *
1435 create_config_files(const char *proto, const char *host, const char *port,
1436 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1437 int mirror_references, struct got_pathlist_head *symrefs,
1438 struct got_pathlist_head *wanted_branches,
1439 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1441 const struct got_error *err = NULL;
1442 const char *default_branch = NULL;
1443 struct got_pathlist_entry *pe;
1446 * If we asked for a set of wanted branches then use the first
1447 * one of those.
1449 if (!TAILQ_EMPTY(wanted_branches)) {
1450 pe = TAILQ_FIRST(wanted_branches);
1451 default_branch = pe->path;
1452 } else {
1453 /* First HEAD ref listed by server is the default branch. */
1454 TAILQ_FOREACH(pe, symrefs, entry) {
1455 const char *refname = pe->path;
1456 const char *target = pe->data;
1458 if (strcmp(refname, GOT_REF_HEAD) != 0)
1459 continue;
1461 default_branch = target;
1462 break;
1466 /* Create got.conf(5). */
1467 err = create_gotconfig(proto, host, port, remote_repo_path,
1468 default_branch, fetch_all_branches, wanted_branches,
1469 wanted_refs, mirror_references, repo);
1470 if (err)
1471 return err;
1473 /* Create a config file Git can understand. */
1474 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1475 wanted_branches, wanted_refs, mirror_references, repo);
1478 static const struct got_error *
1479 cmd_clone(int argc, char *argv[])
1481 const struct got_error *error = NULL;
1482 const char *uri, *dirname;
1483 char *proto, *host, *port, *repo_name, *server_path;
1484 char *default_destdir = NULL, *id_str = NULL;
1485 const char *repo_path;
1486 struct got_repository *repo = NULL;
1487 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1488 struct got_pathlist_entry *pe;
1489 struct got_object_id *pack_hash = NULL;
1490 int ch, fetchfd = -1, fetchstatus;
1491 pid_t fetchpid = -1;
1492 struct got_fetch_progress_arg fpa;
1493 char *git_url = NULL;
1494 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1495 int list_refs_only = 0;
1496 int *pack_fds = NULL;
1498 TAILQ_INIT(&refs);
1499 TAILQ_INIT(&symrefs);
1500 TAILQ_INIT(&wanted_branches);
1501 TAILQ_INIT(&wanted_refs);
1503 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1504 switch (ch) {
1505 case 'a':
1506 fetch_all_branches = 1;
1507 break;
1508 case 'b':
1509 error = got_pathlist_append(&wanted_branches,
1510 optarg, NULL);
1511 if (error)
1512 return error;
1513 break;
1514 case 'l':
1515 list_refs_only = 1;
1516 break;
1517 case 'm':
1518 mirror_references = 1;
1519 break;
1520 case 'v':
1521 if (verbosity < 0)
1522 verbosity = 0;
1523 else if (verbosity < 3)
1524 verbosity++;
1525 break;
1526 case 'q':
1527 verbosity = -1;
1528 break;
1529 case 'R':
1530 error = got_pathlist_append(&wanted_refs,
1531 optarg, NULL);
1532 if (error)
1533 return error;
1534 break;
1535 default:
1536 usage_clone();
1537 break;
1540 argc -= optind;
1541 argv += optind;
1543 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1544 option_conflict('a', 'b');
1545 if (list_refs_only) {
1546 if (!TAILQ_EMPTY(&wanted_branches))
1547 option_conflict('l', 'b');
1548 if (fetch_all_branches)
1549 option_conflict('l', 'a');
1550 if (mirror_references)
1551 option_conflict('l', 'm');
1552 if (!TAILQ_EMPTY(&wanted_refs))
1553 option_conflict('l', 'R');
1556 uri = argv[0];
1558 if (argc == 1)
1559 dirname = NULL;
1560 else if (argc == 2)
1561 dirname = argv[1];
1562 else
1563 usage_clone();
1565 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1566 &repo_name, uri);
1567 if (error)
1568 goto done;
1570 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1571 host, port ? ":" : "", port ? port : "",
1572 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1573 error = got_error_from_errno("asprintf");
1574 goto done;
1577 if (strcmp(proto, "git") == 0) {
1578 #ifndef PROFILE
1579 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1580 "sendfd dns inet unveil", NULL) == -1)
1581 err(1, "pledge");
1582 #endif
1583 } else if (strcmp(proto, "git+ssh") == 0 ||
1584 strcmp(proto, "ssh") == 0) {
1585 #ifndef PROFILE
1586 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1587 "sendfd unveil", NULL) == -1)
1588 err(1, "pledge");
1589 #endif
1590 } else if (strcmp(proto, "http") == 0 ||
1591 strcmp(proto, "git+http") == 0) {
1592 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1593 goto done;
1594 } else {
1595 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1596 goto done;
1598 if (dirname == NULL) {
1599 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1600 error = got_error_from_errno("asprintf");
1601 goto done;
1603 repo_path = default_destdir;
1604 } else
1605 repo_path = dirname;
1607 if (!list_refs_only) {
1608 error = got_path_mkdir(repo_path);
1609 if (error &&
1610 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1611 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1612 goto done;
1613 if (!got_path_dir_is_empty(repo_path)) {
1614 error = got_error_path(repo_path,
1615 GOT_ERR_DIR_NOT_EMPTY);
1616 goto done;
1620 error = got_dial_apply_unveil(proto);
1621 if (error)
1622 goto done;
1624 error = apply_unveil(repo_path, 0, NULL);
1625 if (error)
1626 goto done;
1628 if (verbosity >= 0)
1629 printf("Connecting to %s%s%s\n", host,
1630 port ? ":" : "", port ? port : "");
1632 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1633 server_path, verbosity);
1634 if (error)
1635 goto done;
1637 if (!list_refs_only) {
1638 error = got_repo_init(repo_path);
1639 if (error)
1640 goto done;
1641 error = got_repo_pack_fds_open(&pack_fds);
1642 if (error != NULL)
1643 goto done;
1644 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1645 if (error)
1646 goto done;
1649 fpa.last_scaled_size[0] = '\0';
1650 fpa.last_p_indexed = -1;
1651 fpa.last_p_resolved = -1;
1652 fpa.verbosity = verbosity;
1653 fpa.create_configs = 1;
1654 fpa.configs_created = 0;
1655 fpa.repo = repo;
1656 fpa.config_info.symrefs = &symrefs;
1657 fpa.config_info.wanted_branches = &wanted_branches;
1658 fpa.config_info.wanted_refs = &wanted_refs;
1659 fpa.config_info.proto = proto;
1660 fpa.config_info.host = host;
1661 fpa.config_info.port = port;
1662 fpa.config_info.remote_repo_path = server_path;
1663 fpa.config_info.git_url = git_url;
1664 fpa.config_info.fetch_all_branches = fetch_all_branches;
1665 fpa.config_info.mirror_references = mirror_references;
1666 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1667 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1668 fetch_all_branches, &wanted_branches, &wanted_refs,
1669 list_refs_only, verbosity, fetchfd, repo,
1670 fetch_progress, &fpa);
1671 if (error)
1672 goto done;
1674 if (list_refs_only) {
1675 error = list_remote_refs(&symrefs, &refs);
1676 goto done;
1679 if (pack_hash == NULL) {
1680 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1681 "server sent an empty pack file");
1682 goto done;
1684 error = got_object_id_str(&id_str, pack_hash);
1685 if (error)
1686 goto done;
1687 if (verbosity >= 0)
1688 printf("\nFetched %s.pack\n", id_str);
1689 free(id_str);
1691 /* Set up references provided with the pack file. */
1692 TAILQ_FOREACH(pe, &refs, entry) {
1693 const char *refname = pe->path;
1694 struct got_object_id *id = pe->data;
1695 char *remote_refname;
1697 if (is_wanted_ref(&wanted_refs, refname) &&
1698 !mirror_references) {
1699 error = create_wanted_ref(refname, id,
1700 GOT_FETCH_DEFAULT_REMOTE_NAME,
1701 verbosity - 1, repo);
1702 if (error)
1703 goto done;
1704 continue;
1707 error = create_ref(refname, id, verbosity - 1, repo);
1708 if (error)
1709 goto done;
1711 if (mirror_references)
1712 continue;
1714 if (strncmp("refs/heads/", refname, 11) != 0)
1715 continue;
1717 if (asprintf(&remote_refname,
1718 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1719 refname + 11) == -1) {
1720 error = got_error_from_errno("asprintf");
1721 goto done;
1723 error = create_ref(remote_refname, id, verbosity - 1, repo);
1724 free(remote_refname);
1725 if (error)
1726 goto done;
1729 /* Set the HEAD reference if the server provided one. */
1730 TAILQ_FOREACH(pe, &symrefs, entry) {
1731 struct got_reference *target_ref;
1732 const char *refname = pe->path;
1733 const char *target = pe->data;
1734 char *remote_refname = NULL, *remote_target = NULL;
1736 if (strcmp(refname, GOT_REF_HEAD) != 0)
1737 continue;
1739 error = got_ref_open(&target_ref, repo, target, 0);
1740 if (error) {
1741 if (error->code == GOT_ERR_NOT_REF) {
1742 error = NULL;
1743 continue;
1745 goto done;
1748 error = create_symref(refname, target_ref, verbosity, repo);
1749 got_ref_close(target_ref);
1750 if (error)
1751 goto done;
1753 if (mirror_references)
1754 continue;
1756 if (strncmp("refs/heads/", target, 11) != 0)
1757 continue;
1759 if (asprintf(&remote_refname,
1760 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1761 refname) == -1) {
1762 error = got_error_from_errno("asprintf");
1763 goto done;
1765 if (asprintf(&remote_target,
1766 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1767 target + 11) == -1) {
1768 error = got_error_from_errno("asprintf");
1769 free(remote_refname);
1770 goto done;
1772 error = got_ref_open(&target_ref, repo, remote_target, 0);
1773 if (error) {
1774 free(remote_refname);
1775 free(remote_target);
1776 if (error->code == GOT_ERR_NOT_REF) {
1777 error = NULL;
1778 continue;
1780 goto done;
1782 error = create_symref(remote_refname, target_ref,
1783 verbosity - 1, repo);
1784 free(remote_refname);
1785 free(remote_target);
1786 got_ref_close(target_ref);
1787 if (error)
1788 goto done;
1790 if (pe == NULL) {
1792 * We failed to set the HEAD reference. If we asked for
1793 * a set of wanted branches use the first of one of those
1794 * which could be fetched instead.
1796 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1797 const char *target = pe->path;
1798 struct got_reference *target_ref;
1800 error = got_ref_open(&target_ref, repo, target, 0);
1801 if (error) {
1802 if (error->code == GOT_ERR_NOT_REF) {
1803 error = NULL;
1804 continue;
1806 goto done;
1809 error = create_symref(GOT_REF_HEAD, target_ref,
1810 verbosity, repo);
1811 got_ref_close(target_ref);
1812 if (error)
1813 goto done;
1814 break;
1818 if (verbosity >= 0)
1819 printf("Created %s repository '%s'\n",
1820 mirror_references ? "mirrored" : "cloned", repo_path);
1821 done:
1822 if (pack_fds) {
1823 const struct got_error *pack_err =
1824 got_repo_pack_fds_close(pack_fds);
1825 if (error == NULL)
1826 error = pack_err;
1828 if (fetchpid > 0) {
1829 if (kill(fetchpid, SIGTERM) == -1)
1830 error = got_error_from_errno("kill");
1831 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1832 error = got_error_from_errno("waitpid");
1834 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1835 error = got_error_from_errno("close");
1836 if (repo) {
1837 const struct got_error *close_err = got_repo_close(repo);
1838 if (error == NULL)
1839 error = close_err;
1841 TAILQ_FOREACH(pe, &refs, entry) {
1842 free((void *)pe->path);
1843 free(pe->data);
1845 got_pathlist_free(&refs);
1846 TAILQ_FOREACH(pe, &symrefs, entry) {
1847 free((void *)pe->path);
1848 free(pe->data);
1850 got_pathlist_free(&symrefs);
1851 got_pathlist_free(&wanted_branches);
1852 got_pathlist_free(&wanted_refs);
1853 free(pack_hash);
1854 free(proto);
1855 free(host);
1856 free(port);
1857 free(server_path);
1858 free(repo_name);
1859 free(default_destdir);
1860 free(git_url);
1861 return error;
1864 static const struct got_error *
1865 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1866 int replace_tags, int verbosity, struct got_repository *repo)
1868 const struct got_error *err = NULL;
1869 char *new_id_str = NULL;
1870 struct got_object_id *old_id = NULL;
1872 err = got_object_id_str(&new_id_str, new_id);
1873 if (err)
1874 goto done;
1876 if (!replace_tags &&
1877 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1878 err = got_ref_resolve(&old_id, repo, ref);
1879 if (err)
1880 goto done;
1881 if (got_object_id_cmp(old_id, new_id) == 0)
1882 goto done;
1883 if (verbosity >= 0) {
1884 printf("Rejecting update of existing tag %s: %s\n",
1885 got_ref_get_name(ref), new_id_str);
1887 goto done;
1890 if (got_ref_is_symbolic(ref)) {
1891 if (verbosity >= 0) {
1892 printf("Replacing reference %s: %s\n",
1893 got_ref_get_name(ref),
1894 got_ref_get_symref_target(ref));
1896 err = got_ref_change_symref_to_ref(ref, new_id);
1897 if (err)
1898 goto done;
1899 err = got_ref_write(ref, repo);
1900 if (err)
1901 goto done;
1902 } else {
1903 err = got_ref_resolve(&old_id, repo, ref);
1904 if (err)
1905 goto done;
1906 if (got_object_id_cmp(old_id, new_id) == 0)
1907 goto done;
1909 err = got_ref_change_ref(ref, new_id);
1910 if (err)
1911 goto done;
1912 err = got_ref_write(ref, repo);
1913 if (err)
1914 goto done;
1917 if (verbosity >= 0)
1918 printf("Updated %s: %s\n", got_ref_get_name(ref),
1919 new_id_str);
1920 done:
1921 free(old_id);
1922 free(new_id_str);
1923 return err;
1926 static const struct got_error *
1927 update_symref(const char *refname, struct got_reference *target_ref,
1928 int verbosity, struct got_repository *repo)
1930 const struct got_error *err = NULL, *unlock_err;
1931 struct got_reference *symref;
1932 int symref_is_locked = 0;
1934 err = got_ref_open(&symref, repo, refname, 1);
1935 if (err) {
1936 if (err->code != GOT_ERR_NOT_REF)
1937 return err;
1938 err = got_ref_alloc_symref(&symref, refname, target_ref);
1939 if (err)
1940 goto done;
1942 err = got_ref_write(symref, repo);
1943 if (err)
1944 goto done;
1946 if (verbosity >= 0)
1947 printf("Created reference %s: %s\n",
1948 got_ref_get_name(symref),
1949 got_ref_get_symref_target(symref));
1950 } else {
1951 symref_is_locked = 1;
1953 if (strcmp(got_ref_get_symref_target(symref),
1954 got_ref_get_name(target_ref)) == 0)
1955 goto done;
1957 err = got_ref_change_symref(symref,
1958 got_ref_get_name(target_ref));
1959 if (err)
1960 goto done;
1962 err = got_ref_write(symref, repo);
1963 if (err)
1964 goto done;
1966 if (verbosity >= 0)
1967 printf("Updated %s: %s\n", got_ref_get_name(symref),
1968 got_ref_get_symref_target(symref));
1971 done:
1972 if (symref_is_locked) {
1973 unlock_err = got_ref_unlock(symref);
1974 if (unlock_err && err == NULL)
1975 err = unlock_err;
1977 got_ref_close(symref);
1978 return err;
1981 __dead static void
1982 usage_fetch(void)
1984 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1985 "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
1986 "[remote-repository-name]\n",
1987 getprogname());
1988 exit(1);
1991 static const struct got_error *
1992 delete_missing_ref(struct got_reference *ref,
1993 int verbosity, struct got_repository *repo)
1995 const struct got_error *err = NULL;
1996 struct got_object_id *id = NULL;
1997 char *id_str = NULL;
1999 if (got_ref_is_symbolic(ref)) {
2000 err = got_ref_delete(ref, repo);
2001 if (err)
2002 return err;
2003 if (verbosity >= 0) {
2004 printf("Deleted %s: %s\n",
2005 got_ref_get_name(ref),
2006 got_ref_get_symref_target(ref));
2008 } else {
2009 err = got_ref_resolve(&id, repo, ref);
2010 if (err)
2011 return err;
2012 err = got_object_id_str(&id_str, id);
2013 if (err)
2014 goto done;
2016 err = got_ref_delete(ref, repo);
2017 if (err)
2018 goto done;
2019 if (verbosity >= 0) {
2020 printf("Deleted %s: %s\n",
2021 got_ref_get_name(ref), id_str);
2024 done:
2025 free(id);
2026 free(id_str);
2027 return NULL;
2030 static const struct got_error *
2031 delete_missing_refs(struct got_pathlist_head *their_refs,
2032 struct got_pathlist_head *their_symrefs,
2033 const struct got_remote_repo *remote,
2034 int verbosity, struct got_repository *repo)
2036 const struct got_error *err = NULL, *unlock_err;
2037 struct got_reflist_head my_refs;
2038 struct got_reflist_entry *re;
2039 struct got_pathlist_entry *pe;
2040 char *remote_namespace = NULL;
2041 char *local_refname = NULL;
2043 TAILQ_INIT(&my_refs);
2045 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2046 == -1)
2047 return got_error_from_errno("asprintf");
2049 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2050 if (err)
2051 goto done;
2053 TAILQ_FOREACH(re, &my_refs, entry) {
2054 const char *refname = got_ref_get_name(re->ref);
2055 const char *their_refname;
2057 if (remote->mirror_references) {
2058 their_refname = refname;
2059 } else {
2060 if (strncmp(refname, remote_namespace,
2061 strlen(remote_namespace)) == 0) {
2062 if (strcmp(refname + strlen(remote_namespace),
2063 GOT_REF_HEAD) == 0)
2064 continue;
2065 if (asprintf(&local_refname, "refs/heads/%s",
2066 refname + strlen(remote_namespace)) == -1) {
2067 err = got_error_from_errno("asprintf");
2068 goto done;
2070 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2071 continue;
2073 their_refname = local_refname;
2076 TAILQ_FOREACH(pe, their_refs, entry) {
2077 if (strcmp(their_refname, pe->path) == 0)
2078 break;
2080 if (pe != NULL)
2081 continue;
2083 TAILQ_FOREACH(pe, their_symrefs, entry) {
2084 if (strcmp(their_refname, pe->path) == 0)
2085 break;
2087 if (pe != NULL)
2088 continue;
2090 err = delete_missing_ref(re->ref, verbosity, repo);
2091 if (err)
2092 break;
2094 if (local_refname) {
2095 struct got_reference *ref;
2096 err = got_ref_open(&ref, repo, local_refname, 1);
2097 if (err) {
2098 if (err->code != GOT_ERR_NOT_REF)
2099 break;
2100 free(local_refname);
2101 local_refname = NULL;
2102 continue;
2104 err = delete_missing_ref(ref, verbosity, repo);
2105 if (err)
2106 break;
2107 unlock_err = got_ref_unlock(ref);
2108 got_ref_close(ref);
2109 if (unlock_err && err == NULL) {
2110 err = unlock_err;
2111 break;
2114 free(local_refname);
2115 local_refname = NULL;
2118 done:
2119 free(remote_namespace);
2120 free(local_refname);
2121 return err;
2124 static const struct got_error *
2125 update_wanted_ref(const char *refname, struct got_object_id *id,
2126 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2128 const struct got_error *err, *unlock_err;
2129 char *remote_refname;
2130 struct got_reference *ref;
2132 if (strncmp("refs/", refname, 5) == 0)
2133 refname += 5;
2135 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2136 remote_repo_name, refname) == -1)
2137 return got_error_from_errno("asprintf");
2139 err = got_ref_open(&ref, repo, remote_refname, 1);
2140 if (err) {
2141 if (err->code != GOT_ERR_NOT_REF)
2142 goto done;
2143 err = create_ref(remote_refname, id, verbosity, repo);
2144 } else {
2145 err = update_ref(ref, id, 0, verbosity, repo);
2146 unlock_err = got_ref_unlock(ref);
2147 if (unlock_err && err == NULL)
2148 err = unlock_err;
2149 got_ref_close(ref);
2151 done:
2152 free(remote_refname);
2153 return err;
2156 static const struct got_error *
2157 delete_ref(struct got_repository *repo, struct got_reference *ref)
2159 const struct got_error *err = NULL;
2160 struct got_object_id *id = NULL;
2161 char *id_str = NULL;
2162 const char *target;
2164 if (got_ref_is_symbolic(ref)) {
2165 target = got_ref_get_symref_target(ref);
2166 } else {
2167 err = got_ref_resolve(&id, repo, ref);
2168 if (err)
2169 goto done;
2170 err = got_object_id_str(&id_str, id);
2171 if (err)
2172 goto done;
2173 target = id_str;
2176 err = got_ref_delete(ref, repo);
2177 if (err)
2178 goto done;
2180 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2181 done:
2182 free(id);
2183 free(id_str);
2184 return err;
2187 static const struct got_error *
2188 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2190 const struct got_error *err = NULL;
2191 struct got_reflist_head refs;
2192 struct got_reflist_entry *re;
2193 char *prefix;
2195 TAILQ_INIT(&refs);
2197 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2198 err = got_error_from_errno("asprintf");
2199 goto done;
2201 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2202 if (err)
2203 goto done;
2205 TAILQ_FOREACH(re, &refs, entry)
2206 delete_ref(repo, re->ref);
2207 done:
2208 got_ref_list_free(&refs);
2209 return err;
2212 static const struct got_error *
2213 cmd_fetch(int argc, char *argv[])
2215 const struct got_error *error = NULL, *unlock_err;
2216 char *cwd = NULL, *repo_path = NULL;
2217 const char *remote_name;
2218 char *proto = NULL, *host = NULL, *port = NULL;
2219 char *repo_name = NULL, *server_path = NULL;
2220 const struct got_remote_repo *remotes, *remote = NULL;
2221 int nremotes;
2222 char *id_str = NULL;
2223 struct got_repository *repo = NULL;
2224 struct got_worktree *worktree = NULL;
2225 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2226 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2227 struct got_pathlist_entry *pe;
2228 struct got_object_id *pack_hash = NULL;
2229 int i, ch, fetchfd = -1, fetchstatus;
2230 pid_t fetchpid = -1;
2231 struct got_fetch_progress_arg fpa;
2232 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2233 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2234 int *pack_fds = NULL;
2236 TAILQ_INIT(&refs);
2237 TAILQ_INIT(&symrefs);
2238 TAILQ_INIT(&wanted_branches);
2239 TAILQ_INIT(&wanted_refs);
2241 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
2242 switch (ch) {
2243 case 'a':
2244 fetch_all_branches = 1;
2245 break;
2246 case 'b':
2247 error = got_pathlist_append(&wanted_branches,
2248 optarg, NULL);
2249 if (error)
2250 return error;
2251 break;
2252 case 'd':
2253 delete_refs = 1;
2254 break;
2255 case 'l':
2256 list_refs_only = 1;
2257 break;
2258 case 'r':
2259 repo_path = realpath(optarg, NULL);
2260 if (repo_path == NULL)
2261 return got_error_from_errno2("realpath",
2262 optarg);
2263 got_path_strip_trailing_slashes(repo_path);
2264 break;
2265 case 't':
2266 replace_tags = 1;
2267 break;
2268 case 'v':
2269 if (verbosity < 0)
2270 verbosity = 0;
2271 else if (verbosity < 3)
2272 verbosity++;
2273 break;
2274 case 'q':
2275 verbosity = -1;
2276 break;
2277 case 'R':
2278 error = got_pathlist_append(&wanted_refs,
2279 optarg, NULL);
2280 if (error)
2281 return error;
2282 break;
2283 case 'X':
2284 delete_remote = 1;
2285 break;
2286 default:
2287 usage_fetch();
2288 break;
2291 argc -= optind;
2292 argv += optind;
2294 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2295 option_conflict('a', 'b');
2296 if (list_refs_only) {
2297 if (!TAILQ_EMPTY(&wanted_branches))
2298 option_conflict('l', 'b');
2299 if (fetch_all_branches)
2300 option_conflict('l', 'a');
2301 if (delete_refs)
2302 option_conflict('l', 'd');
2303 if (delete_remote)
2304 option_conflict('l', 'X');
2306 if (delete_remote) {
2307 if (fetch_all_branches)
2308 option_conflict('X', 'a');
2309 if (!TAILQ_EMPTY(&wanted_branches))
2310 option_conflict('X', 'b');
2311 if (delete_refs)
2312 option_conflict('X', 'd');
2313 if (replace_tags)
2314 option_conflict('X', 't');
2315 if (!TAILQ_EMPTY(&wanted_refs))
2316 option_conflict('X', 'R');
2319 if (argc == 0) {
2320 if (delete_remote)
2321 errx(1, "-X option requires a remote name");
2322 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2323 } else if (argc == 1)
2324 remote_name = argv[0];
2325 else
2326 usage_fetch();
2328 cwd = getcwd(NULL, 0);
2329 if (cwd == NULL) {
2330 error = got_error_from_errno("getcwd");
2331 goto done;
2334 error = got_repo_pack_fds_open(&pack_fds);
2335 if (error != NULL)
2336 goto done;
2338 if (repo_path == NULL) {
2339 error = got_worktree_open(&worktree, cwd);
2340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2341 goto done;
2342 else
2343 error = NULL;
2344 if (worktree) {
2345 repo_path =
2346 strdup(got_worktree_get_repo_path(worktree));
2347 if (repo_path == NULL)
2348 error = got_error_from_errno("strdup");
2349 if (error)
2350 goto done;
2351 } else {
2352 repo_path = strdup(cwd);
2353 if (repo_path == NULL) {
2354 error = got_error_from_errno("strdup");
2355 goto done;
2360 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2361 if (error)
2362 goto done;
2364 if (delete_remote) {
2365 error = delete_refs_for_remote(repo, remote_name);
2366 goto done; /* nothing else to do */
2369 if (worktree) {
2370 worktree_conf = got_worktree_get_gotconfig(worktree);
2371 if (worktree_conf) {
2372 got_gotconfig_get_remotes(&nremotes, &remotes,
2373 worktree_conf);
2374 for (i = 0; i < nremotes; i++) {
2375 if (strcmp(remotes[i].name, remote_name) == 0) {
2376 remote = &remotes[i];
2377 break;
2382 if (remote == NULL) {
2383 repo_conf = got_repo_get_gotconfig(repo);
2384 if (repo_conf) {
2385 got_gotconfig_get_remotes(&nremotes, &remotes,
2386 repo_conf);
2387 for (i = 0; i < nremotes; i++) {
2388 if (strcmp(remotes[i].name, remote_name) == 0) {
2389 remote = &remotes[i];
2390 break;
2395 if (remote == NULL) {
2396 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2397 for (i = 0; i < nremotes; i++) {
2398 if (strcmp(remotes[i].name, remote_name) == 0) {
2399 remote = &remotes[i];
2400 break;
2404 if (remote == NULL) {
2405 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2406 goto done;
2409 if (TAILQ_EMPTY(&wanted_branches)) {
2410 if (!fetch_all_branches)
2411 fetch_all_branches = remote->fetch_all_branches;
2412 for (i = 0; i < remote->nfetch_branches; i++) {
2413 got_pathlist_append(&wanted_branches,
2414 remote->fetch_branches[i], NULL);
2417 if (TAILQ_EMPTY(&wanted_refs)) {
2418 for (i = 0; i < remote->nfetch_refs; i++) {
2419 got_pathlist_append(&wanted_refs,
2420 remote->fetch_refs[i], NULL);
2424 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2425 &repo_name, remote->fetch_url);
2426 if (error)
2427 goto done;
2429 if (strcmp(proto, "git") == 0) {
2430 #ifndef PROFILE
2431 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2432 "sendfd dns inet unveil", NULL) == -1)
2433 err(1, "pledge");
2434 #endif
2435 } else if (strcmp(proto, "git+ssh") == 0 ||
2436 strcmp(proto, "ssh") == 0) {
2437 #ifndef PROFILE
2438 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2439 "sendfd unveil", NULL) == -1)
2440 err(1, "pledge");
2441 #endif
2442 } else if (strcmp(proto, "http") == 0 ||
2443 strcmp(proto, "git+http") == 0) {
2444 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2445 goto done;
2446 } else {
2447 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2448 goto done;
2451 error = got_dial_apply_unveil(proto);
2452 if (error)
2453 goto done;
2455 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2456 if (error)
2457 goto done;
2459 if (verbosity >= 0)
2460 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2461 port ? ":" : "", port ? port : "");
2463 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2464 server_path, verbosity);
2465 if (error)
2466 goto done;
2468 fpa.last_scaled_size[0] = '\0';
2469 fpa.last_p_indexed = -1;
2470 fpa.last_p_resolved = -1;
2471 fpa.verbosity = verbosity;
2472 fpa.repo = repo;
2473 fpa.create_configs = 0;
2474 fpa.configs_created = 0;
2475 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2476 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2477 remote->mirror_references, fetch_all_branches, &wanted_branches,
2478 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2479 fetch_progress, &fpa);
2480 if (error)
2481 goto done;
2483 if (list_refs_only) {
2484 error = list_remote_refs(&symrefs, &refs);
2485 goto done;
2488 if (pack_hash == NULL) {
2489 if (verbosity >= 0)
2490 printf("Already up-to-date\n");
2491 } else if (verbosity >= 0) {
2492 error = got_object_id_str(&id_str, pack_hash);
2493 if (error)
2494 goto done;
2495 printf("\nFetched %s.pack\n", id_str);
2496 free(id_str);
2497 id_str = NULL;
2500 /* Update references provided with the pack file. */
2501 TAILQ_FOREACH(pe, &refs, entry) {
2502 const char *refname = pe->path;
2503 struct got_object_id *id = pe->data;
2504 struct got_reference *ref;
2505 char *remote_refname;
2507 if (is_wanted_ref(&wanted_refs, refname) &&
2508 !remote->mirror_references) {
2509 error = update_wanted_ref(refname, id,
2510 remote->name, verbosity, repo);
2511 if (error)
2512 goto done;
2513 continue;
2516 if (remote->mirror_references ||
2517 strncmp("refs/tags/", refname, 10) == 0) {
2518 error = got_ref_open(&ref, repo, refname, 1);
2519 if (error) {
2520 if (error->code != GOT_ERR_NOT_REF)
2521 goto done;
2522 error = create_ref(refname, id, verbosity,
2523 repo);
2524 if (error)
2525 goto done;
2526 } else {
2527 error = update_ref(ref, id, replace_tags,
2528 verbosity, repo);
2529 unlock_err = got_ref_unlock(ref);
2530 if (unlock_err && error == NULL)
2531 error = unlock_err;
2532 got_ref_close(ref);
2533 if (error)
2534 goto done;
2536 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2537 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2538 remote_name, refname + 11) == -1) {
2539 error = got_error_from_errno("asprintf");
2540 goto done;
2543 error = got_ref_open(&ref, repo, remote_refname, 1);
2544 if (error) {
2545 if (error->code != GOT_ERR_NOT_REF)
2546 goto done;
2547 error = create_ref(remote_refname, id,
2548 verbosity, repo);
2549 if (error)
2550 goto done;
2551 } else {
2552 error = update_ref(ref, id, replace_tags,
2553 verbosity, repo);
2554 unlock_err = got_ref_unlock(ref);
2555 if (unlock_err && error == NULL)
2556 error = unlock_err;
2557 got_ref_close(ref);
2558 if (error)
2559 goto done;
2562 /* Also create a local branch if none exists yet. */
2563 error = got_ref_open(&ref, repo, refname, 1);
2564 if (error) {
2565 if (error->code != GOT_ERR_NOT_REF)
2566 goto done;
2567 error = create_ref(refname, id, verbosity,
2568 repo);
2569 if (error)
2570 goto done;
2571 } else {
2572 unlock_err = got_ref_unlock(ref);
2573 if (unlock_err && error == NULL)
2574 error = unlock_err;
2575 got_ref_close(ref);
2579 if (delete_refs) {
2580 error = delete_missing_refs(&refs, &symrefs, remote,
2581 verbosity, repo);
2582 if (error)
2583 goto done;
2586 if (!remote->mirror_references) {
2587 /* Update remote HEAD reference if the server provided one. */
2588 TAILQ_FOREACH(pe, &symrefs, entry) {
2589 struct got_reference *target_ref;
2590 const char *refname = pe->path;
2591 const char *target = pe->data;
2592 char *remote_refname = NULL, *remote_target = NULL;
2594 if (strcmp(refname, GOT_REF_HEAD) != 0)
2595 continue;
2597 if (strncmp("refs/heads/", target, 11) != 0)
2598 continue;
2600 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2601 remote->name, refname) == -1) {
2602 error = got_error_from_errno("asprintf");
2603 goto done;
2605 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2606 remote->name, target + 11) == -1) {
2607 error = got_error_from_errno("asprintf");
2608 free(remote_refname);
2609 goto done;
2612 error = got_ref_open(&target_ref, repo, remote_target,
2613 0);
2614 if (error) {
2615 free(remote_refname);
2616 free(remote_target);
2617 if (error->code == GOT_ERR_NOT_REF) {
2618 error = NULL;
2619 continue;
2621 goto done;
2623 error = update_symref(remote_refname, target_ref,
2624 verbosity, repo);
2625 free(remote_refname);
2626 free(remote_target);
2627 got_ref_close(target_ref);
2628 if (error)
2629 goto done;
2632 done:
2633 if (fetchpid > 0) {
2634 if (kill(fetchpid, SIGTERM) == -1)
2635 error = got_error_from_errno("kill");
2636 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2637 error = got_error_from_errno("waitpid");
2639 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2640 error = got_error_from_errno("close");
2641 if (repo) {
2642 const struct got_error *close_err = got_repo_close(repo);
2643 if (error == NULL)
2644 error = close_err;
2646 if (worktree)
2647 got_worktree_close(worktree);
2648 if (pack_fds) {
2649 const struct got_error *pack_err =
2650 got_repo_pack_fds_close(pack_fds);
2651 if (error == NULL)
2652 error = pack_err;
2654 TAILQ_FOREACH(pe, &refs, entry) {
2655 free((void *)pe->path);
2656 free(pe->data);
2658 got_pathlist_free(&refs);
2659 TAILQ_FOREACH(pe, &symrefs, entry) {
2660 free((void *)pe->path);
2661 free(pe->data);
2663 got_pathlist_free(&symrefs);
2664 got_pathlist_free(&wanted_branches);
2665 got_pathlist_free(&wanted_refs);
2666 free(id_str);
2667 free(cwd);
2668 free(repo_path);
2669 free(pack_hash);
2670 free(proto);
2671 free(host);
2672 free(port);
2673 free(server_path);
2674 free(repo_name);
2675 return error;
2679 __dead static void
2680 usage_checkout(void)
2682 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2683 "[-p prefix] [-q] repository-path [worktree-path]\n",
2684 getprogname());
2685 exit(1);
2688 static void
2689 show_worktree_base_ref_warning(void)
2691 fprintf(stderr, "%s: warning: could not create a reference "
2692 "to the work tree's base commit; the commit could be "
2693 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2694 "repository writable and running 'got update' will prevent this\n",
2695 getprogname());
2698 struct got_checkout_progress_arg {
2699 const char *worktree_path;
2700 int had_base_commit_ref_error;
2701 int verbosity;
2704 static const struct got_error *
2705 checkout_progress(void *arg, unsigned char status, const char *path)
2707 struct got_checkout_progress_arg *a = arg;
2709 /* Base commit bump happens silently. */
2710 if (status == GOT_STATUS_BUMP_BASE)
2711 return NULL;
2713 if (status == GOT_STATUS_BASE_REF_ERR) {
2714 a->had_base_commit_ref_error = 1;
2715 return NULL;
2718 while (path[0] == '/')
2719 path++;
2721 if (a->verbosity >= 0)
2722 printf("%c %s/%s\n", status, a->worktree_path, path);
2724 return NULL;
2727 static const struct got_error *
2728 check_cancelled(void *arg)
2730 if (sigint_received || sigpipe_received)
2731 return got_error(GOT_ERR_CANCELLED);
2732 return NULL;
2735 static const struct got_error *
2736 check_linear_ancestry(struct got_object_id *commit_id,
2737 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2738 struct got_repository *repo)
2740 const struct got_error *err = NULL;
2741 struct got_object_id *yca_id;
2743 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2744 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2745 if (err)
2746 return err;
2748 if (yca_id == NULL)
2749 return got_error(GOT_ERR_ANCESTRY);
2752 * Require a straight line of history between the target commit
2753 * and the work tree's base commit.
2755 * Non-linear situations such as this require a rebase:
2757 * (commit) D F (base_commit)
2758 * \ /
2759 * C E
2760 * \ /
2761 * B (yca)
2762 * |
2763 * A
2765 * 'got update' only handles linear cases:
2766 * Update forwards in time: A (base/yca) - B - C - D (commit)
2767 * Update backwards in time: D (base) - C - B - A (commit/yca)
2769 if (allow_forwards_in_time_only) {
2770 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2771 return got_error(GOT_ERR_ANCESTRY);
2772 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2773 got_object_id_cmp(base_commit_id, yca_id) != 0)
2774 return got_error(GOT_ERR_ANCESTRY);
2776 free(yca_id);
2777 return NULL;
2780 static const struct got_error *
2781 check_same_branch(struct got_object_id *commit_id,
2782 struct got_reference *head_ref, struct got_object_id *yca_id,
2783 struct got_repository *repo)
2785 const struct got_error *err = NULL;
2786 struct got_commit_graph *graph = NULL;
2787 struct got_object_id *head_commit_id = NULL;
2788 int is_same_branch = 0;
2790 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2791 if (err)
2792 goto done;
2794 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2795 is_same_branch = 1;
2796 goto done;
2798 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2799 is_same_branch = 1;
2800 goto done;
2803 err = got_commit_graph_open(&graph, "/", 1);
2804 if (err)
2805 goto done;
2807 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2808 check_cancelled, NULL);
2809 if (err)
2810 goto done;
2812 for (;;) {
2813 struct got_object_id *id;
2814 err = got_commit_graph_iter_next(&id, graph, repo,
2815 check_cancelled, NULL);
2816 if (err) {
2817 if (err->code == GOT_ERR_ITER_COMPLETED)
2818 err = NULL;
2819 break;
2822 if (id) {
2823 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2824 break;
2825 if (got_object_id_cmp(id, commit_id) == 0) {
2826 is_same_branch = 1;
2827 break;
2831 done:
2832 if (graph)
2833 got_commit_graph_close(graph);
2834 free(head_commit_id);
2835 if (!err && !is_same_branch)
2836 err = got_error(GOT_ERR_ANCESTRY);
2837 return err;
2840 static const struct got_error *
2841 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2843 static char msg[512];
2844 const char *branch_name;
2846 if (got_ref_is_symbolic(ref))
2847 branch_name = got_ref_get_symref_target(ref);
2848 else
2849 branch_name = got_ref_get_name(ref);
2851 if (strncmp("refs/heads/", branch_name, 11) == 0)
2852 branch_name += 11;
2854 snprintf(msg, sizeof(msg),
2855 "target commit is not contained in branch '%s'; "
2856 "the branch to use must be specified with -b; "
2857 "if necessary a new branch can be created for "
2858 "this commit with 'got branch -c %s BRANCH_NAME'",
2859 branch_name, commit_id_str);
2861 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2864 static const struct got_error *
2865 cmd_checkout(int argc, char *argv[])
2867 const struct got_error *error = NULL;
2868 struct got_repository *repo = NULL;
2869 struct got_reference *head_ref = NULL, *ref = NULL;
2870 struct got_worktree *worktree = NULL;
2871 char *repo_path = NULL;
2872 char *worktree_path = NULL;
2873 const char *path_prefix = "";
2874 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2875 char *commit_id_str = NULL;
2876 struct got_object_id *commit_id = NULL;
2877 char *cwd = NULL;
2878 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2879 struct got_pathlist_head paths;
2880 struct got_checkout_progress_arg cpa;
2881 int *pack_fds = NULL;
2883 TAILQ_INIT(&paths);
2885 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2886 switch (ch) {
2887 case 'b':
2888 branch_name = optarg;
2889 break;
2890 case 'c':
2891 commit_id_str = strdup(optarg);
2892 if (commit_id_str == NULL)
2893 return got_error_from_errno("strdup");
2894 break;
2895 case 'E':
2896 allow_nonempty = 1;
2897 break;
2898 case 'p':
2899 path_prefix = optarg;
2900 break;
2901 case 'q':
2902 verbosity = -1;
2903 break;
2904 default:
2905 usage_checkout();
2906 /* NOTREACHED */
2910 argc -= optind;
2911 argv += optind;
2913 #ifndef PROFILE
2914 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2915 "unveil", NULL) == -1)
2916 err(1, "pledge");
2917 #endif
2918 if (argc == 1) {
2919 char *base, *dotgit;
2920 const char *path;
2921 repo_path = realpath(argv[0], NULL);
2922 if (repo_path == NULL)
2923 return got_error_from_errno2("realpath", argv[0]);
2924 cwd = getcwd(NULL, 0);
2925 if (cwd == NULL) {
2926 error = got_error_from_errno("getcwd");
2927 goto done;
2929 if (path_prefix[0])
2930 path = path_prefix;
2931 else
2932 path = repo_path;
2933 error = got_path_basename(&base, path);
2934 if (error)
2935 goto done;
2936 dotgit = strstr(base, ".git");
2937 if (dotgit)
2938 *dotgit = '\0';
2939 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2940 error = got_error_from_errno("asprintf");
2941 free(base);
2942 goto done;
2944 free(base);
2945 } else if (argc == 2) {
2946 repo_path = realpath(argv[0], NULL);
2947 if (repo_path == NULL) {
2948 error = got_error_from_errno2("realpath", argv[0]);
2949 goto done;
2951 worktree_path = realpath(argv[1], NULL);
2952 if (worktree_path == NULL) {
2953 if (errno != ENOENT) {
2954 error = got_error_from_errno2("realpath",
2955 argv[1]);
2956 goto done;
2958 worktree_path = strdup(argv[1]);
2959 if (worktree_path == NULL) {
2960 error = got_error_from_errno("strdup");
2961 goto done;
2964 } else
2965 usage_checkout();
2967 got_path_strip_trailing_slashes(repo_path);
2968 got_path_strip_trailing_slashes(worktree_path);
2970 error = got_repo_pack_fds_open(&pack_fds);
2971 if (error != NULL)
2972 goto done;
2974 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2975 if (error != NULL)
2976 goto done;
2978 /* Pre-create work tree path for unveil(2) */
2979 error = got_path_mkdir(worktree_path);
2980 if (error) {
2981 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2982 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2983 goto done;
2984 if (!allow_nonempty &&
2985 !got_path_dir_is_empty(worktree_path)) {
2986 error = got_error_path(worktree_path,
2987 GOT_ERR_DIR_NOT_EMPTY);
2988 goto done;
2992 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2993 if (error)
2994 goto done;
2996 error = got_ref_open(&head_ref, repo, branch_name, 0);
2997 if (error != NULL)
2998 goto done;
3000 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3001 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3002 goto done;
3004 error = got_worktree_open(&worktree, worktree_path);
3005 if (error != NULL)
3006 goto done;
3008 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3009 path_prefix);
3010 if (error != NULL)
3011 goto done;
3012 if (!same_path_prefix) {
3013 error = got_error(GOT_ERR_PATH_PREFIX);
3014 goto done;
3017 if (commit_id_str) {
3018 struct got_reflist_head refs;
3019 TAILQ_INIT(&refs);
3020 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3021 NULL);
3022 if (error)
3023 goto done;
3024 error = got_repo_match_object_id(&commit_id, NULL,
3025 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3026 got_ref_list_free(&refs);
3027 if (error)
3028 goto done;
3029 error = check_linear_ancestry(commit_id,
3030 got_worktree_get_base_commit_id(worktree), 0, repo);
3031 if (error != NULL) {
3032 if (error->code == GOT_ERR_ANCESTRY) {
3033 error = checkout_ancestry_error(
3034 head_ref, commit_id_str);
3036 goto done;
3038 error = check_same_branch(commit_id, head_ref, NULL, repo);
3039 if (error) {
3040 if (error->code == GOT_ERR_ANCESTRY) {
3041 error = checkout_ancestry_error(
3042 head_ref, commit_id_str);
3044 goto done;
3046 error = got_worktree_set_base_commit_id(worktree, repo,
3047 commit_id);
3048 if (error)
3049 goto done;
3050 /* Expand potentially abbreviated commit ID string. */
3051 free(commit_id_str);
3052 error = got_object_id_str(&commit_id_str, commit_id);
3053 if (error)
3054 goto done;
3055 } else {
3056 commit_id = got_object_id_dup(
3057 got_worktree_get_base_commit_id(worktree));
3058 if (commit_id == NULL) {
3059 error = got_error_from_errno("got_object_id_dup");
3060 goto done;
3062 error = got_object_id_str(&commit_id_str, commit_id);
3063 if (error)
3064 goto done;
3067 error = got_pathlist_append(&paths, "", NULL);
3068 if (error)
3069 goto done;
3070 cpa.worktree_path = worktree_path;
3071 cpa.had_base_commit_ref_error = 0;
3072 cpa.verbosity = verbosity;
3073 error = got_worktree_checkout_files(worktree, &paths, repo,
3074 checkout_progress, &cpa, check_cancelled, NULL);
3075 if (error != NULL)
3076 goto done;
3078 if (got_ref_is_symbolic(head_ref)) {
3079 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3080 if (error)
3081 goto done;
3082 refname = got_ref_get_name(ref);
3083 } else
3084 refname = got_ref_get_name(head_ref);
3085 printf("Checked out %s: %s\n", refname, commit_id_str);
3086 printf("Now shut up and hack\n");
3087 if (cpa.had_base_commit_ref_error)
3088 show_worktree_base_ref_warning();
3089 done:
3090 if (pack_fds) {
3091 const struct got_error *pack_err =
3092 got_repo_pack_fds_close(pack_fds);
3093 if (error == NULL)
3094 error = pack_err;
3096 if (head_ref)
3097 got_ref_close(head_ref);
3098 if (ref)
3099 got_ref_close(ref);
3100 got_pathlist_free(&paths);
3101 free(commit_id_str);
3102 free(commit_id);
3103 free(repo_path);
3104 free(worktree_path);
3105 free(cwd);
3106 return error;
3109 struct got_update_progress_arg {
3110 int did_something;
3111 int conflicts;
3112 int obstructed;
3113 int not_updated;
3114 int missing;
3115 int not_deleted;
3116 int unversioned;
3117 int verbosity;
3120 static void
3121 print_update_progress_stats(struct got_update_progress_arg *upa)
3123 if (!upa->did_something)
3124 return;
3126 if (upa->conflicts > 0)
3127 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3128 if (upa->obstructed > 0)
3129 printf("File paths obstructed by a non-regular file: %d\n",
3130 upa->obstructed);
3131 if (upa->not_updated > 0)
3132 printf("Files not updated because of existing merge "
3133 "conflicts: %d\n", upa->not_updated);
3137 * The meaning of some status codes differs between merge-style operations and
3138 * update operations. For example, the ! status code means "file was missing"
3139 * if changes were merged into the work tree, and "missing file was restored"
3140 * if the work tree was updated. This function should be used by any operation
3141 * which merges changes into the work tree without updating the work tree.
3143 static void
3144 print_merge_progress_stats(struct got_update_progress_arg *upa)
3146 if (!upa->did_something)
3147 return;
3149 if (upa->conflicts > 0)
3150 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3151 if (upa->obstructed > 0)
3152 printf("File paths obstructed by a non-regular file: %d\n",
3153 upa->obstructed);
3154 if (upa->missing > 0)
3155 printf("Files which had incoming changes but could not be "
3156 "found in the work tree: %d\n", upa->missing);
3157 if (upa->not_deleted > 0)
3158 printf("Files not deleted due to differences in deleted "
3159 "content: %d\n", upa->not_deleted);
3160 if (upa->unversioned > 0)
3161 printf("Files not merged because an unversioned file was "
3162 "found in the work tree: %d\n", upa->unversioned);
3165 __dead static void
3166 usage_update(void)
3168 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [-q] "
3169 "[path ...]\n",
3170 getprogname());
3171 exit(1);
3174 static const struct got_error *
3175 update_progress(void *arg, unsigned char status, const char *path)
3177 struct got_update_progress_arg *upa = arg;
3179 if (status == GOT_STATUS_EXISTS ||
3180 status == GOT_STATUS_BASE_REF_ERR)
3181 return NULL;
3183 upa->did_something = 1;
3185 /* Base commit bump happens silently. */
3186 if (status == GOT_STATUS_BUMP_BASE)
3187 return NULL;
3189 if (status == GOT_STATUS_CONFLICT)
3190 upa->conflicts++;
3191 if (status == GOT_STATUS_OBSTRUCTED)
3192 upa->obstructed++;
3193 if (status == GOT_STATUS_CANNOT_UPDATE)
3194 upa->not_updated++;
3195 if (status == GOT_STATUS_MISSING)
3196 upa->missing++;
3197 if (status == GOT_STATUS_CANNOT_DELETE)
3198 upa->not_deleted++;
3199 if (status == GOT_STATUS_UNVERSIONED)
3200 upa->unversioned++;
3202 while (path[0] == '/')
3203 path++;
3204 if (upa->verbosity >= 0)
3205 printf("%c %s\n", status, path);
3207 return NULL;
3210 static const struct got_error *
3211 switch_head_ref(struct got_reference *head_ref,
3212 struct got_object_id *commit_id, struct got_worktree *worktree,
3213 struct got_repository *repo)
3215 const struct got_error *err = NULL;
3216 char *base_id_str;
3217 int ref_has_moved = 0;
3219 /* Trivial case: switching between two different references. */
3220 if (strcmp(got_ref_get_name(head_ref),
3221 got_worktree_get_head_ref_name(worktree)) != 0) {
3222 printf("Switching work tree from %s to %s\n",
3223 got_worktree_get_head_ref_name(worktree),
3224 got_ref_get_name(head_ref));
3225 return got_worktree_set_head_ref(worktree, head_ref);
3228 err = check_linear_ancestry(commit_id,
3229 got_worktree_get_base_commit_id(worktree), 0, repo);
3230 if (err) {
3231 if (err->code != GOT_ERR_ANCESTRY)
3232 return err;
3233 ref_has_moved = 1;
3235 if (!ref_has_moved)
3236 return NULL;
3238 /* Switching to a rebased branch with the same reference name. */
3239 err = got_object_id_str(&base_id_str,
3240 got_worktree_get_base_commit_id(worktree));
3241 if (err)
3242 return err;
3243 printf("Reference %s now points at a different branch\n",
3244 got_worktree_get_head_ref_name(worktree));
3245 printf("Switching work tree from %s to %s\n", base_id_str,
3246 got_worktree_get_head_ref_name(worktree));
3247 return NULL;
3250 static const struct got_error *
3251 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3253 const struct got_error *err;
3254 int in_progress;
3256 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3257 if (err)
3258 return err;
3259 if (in_progress)
3260 return got_error(GOT_ERR_REBASING);
3262 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3263 if (err)
3264 return err;
3265 if (in_progress)
3266 return got_error(GOT_ERR_HISTEDIT_BUSY);
3268 return NULL;
3271 static const struct got_error *
3272 check_merge_in_progress(struct got_worktree *worktree,
3273 struct got_repository *repo)
3275 const struct got_error *err;
3276 int in_progress;
3278 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3279 if (err)
3280 return err;
3281 if (in_progress)
3282 return got_error(GOT_ERR_MERGE_BUSY);
3284 return NULL;
3287 static const struct got_error *
3288 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3289 char *argv[], struct got_worktree *worktree)
3291 const struct got_error *err = NULL;
3292 char *path;
3293 struct got_pathlist_entry *new;
3294 int i;
3296 if (argc == 0) {
3297 path = strdup("");
3298 if (path == NULL)
3299 return got_error_from_errno("strdup");
3300 return got_pathlist_append(paths, path, NULL);
3303 for (i = 0; i < argc; i++) {
3304 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3305 if (err)
3306 break;
3307 err = got_pathlist_insert(&new, paths, path, NULL);
3308 if (err || new == NULL /* duplicate */) {
3309 free(path);
3310 if (err)
3311 break;
3315 return err;
3318 static const struct got_error *
3319 wrap_not_worktree_error(const struct got_error *orig_err,
3320 const char *cmdname, const char *path)
3322 const struct got_error *err;
3323 struct got_repository *repo;
3324 static char msg[512];
3325 int *pack_fds = NULL;
3327 err = got_repo_pack_fds_open(&pack_fds);
3328 if (err)
3329 return err;
3331 err = got_repo_open(&repo, path, NULL, pack_fds);
3332 if (err)
3333 return orig_err;
3335 snprintf(msg, sizeof(msg),
3336 "'got %s' needs a work tree in addition to a git repository\n"
3337 "Work trees can be checked out from this Git repository with "
3338 "'got checkout'.\n"
3339 "The got(1) manual page contains more information.", cmdname);
3340 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3341 got_repo_close(repo);
3342 if (pack_fds) {
3343 const struct got_error *pack_err =
3344 got_repo_pack_fds_close(pack_fds);
3345 if (err == NULL)
3346 err = pack_err;
3348 return err;
3351 static const struct got_error *
3352 cmd_update(int argc, char *argv[])
3354 const struct got_error *error = NULL;
3355 struct got_repository *repo = NULL;
3356 struct got_worktree *worktree = NULL;
3357 char *worktree_path = NULL;
3358 struct got_object_id *commit_id = NULL;
3359 char *commit_id_str = NULL;
3360 const char *branch_name = NULL;
3361 struct got_reference *head_ref = NULL;
3362 struct got_pathlist_head paths;
3363 struct got_pathlist_entry *pe;
3364 int ch, verbosity = 0;
3365 struct got_update_progress_arg upa;
3366 int *pack_fds = NULL;
3368 TAILQ_INIT(&paths);
3370 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3371 switch (ch) {
3372 case 'b':
3373 branch_name = optarg;
3374 break;
3375 case 'c':
3376 commit_id_str = strdup(optarg);
3377 if (commit_id_str == NULL)
3378 return got_error_from_errno("strdup");
3379 break;
3380 case 'q':
3381 verbosity = -1;
3382 break;
3383 default:
3384 usage_update();
3385 /* NOTREACHED */
3389 argc -= optind;
3390 argv += optind;
3392 #ifndef PROFILE
3393 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3394 "unveil", NULL) == -1)
3395 err(1, "pledge");
3396 #endif
3397 worktree_path = getcwd(NULL, 0);
3398 if (worktree_path == NULL) {
3399 error = got_error_from_errno("getcwd");
3400 goto done;
3403 error = got_repo_pack_fds_open(&pack_fds);
3404 if (error != NULL)
3405 goto done;
3407 error = got_worktree_open(&worktree, worktree_path);
3408 if (error) {
3409 if (error->code == GOT_ERR_NOT_WORKTREE)
3410 error = wrap_not_worktree_error(error, "update",
3411 worktree_path);
3412 goto done;
3415 error = check_rebase_or_histedit_in_progress(worktree);
3416 if (error)
3417 goto done;
3419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3420 NULL, pack_fds);
3421 if (error != NULL)
3422 goto done;
3424 error = apply_unveil(got_repo_get_path(repo), 0,
3425 got_worktree_get_root_path(worktree));
3426 if (error)
3427 goto done;
3429 error = check_merge_in_progress(worktree, repo);
3430 if (error)
3431 goto done;
3433 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3434 if (error)
3435 goto done;
3437 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3438 got_worktree_get_head_ref_name(worktree), 0);
3439 if (error != NULL)
3440 goto done;
3441 if (commit_id_str == NULL) {
3442 error = got_ref_resolve(&commit_id, repo, head_ref);
3443 if (error != NULL)
3444 goto done;
3445 error = got_object_id_str(&commit_id_str, commit_id);
3446 if (error != NULL)
3447 goto done;
3448 } else {
3449 struct got_reflist_head refs;
3450 TAILQ_INIT(&refs);
3451 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3452 NULL);
3453 if (error)
3454 goto done;
3455 error = got_repo_match_object_id(&commit_id, NULL,
3456 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3457 got_ref_list_free(&refs);
3458 free(commit_id_str);
3459 commit_id_str = NULL;
3460 if (error)
3461 goto done;
3462 error = got_object_id_str(&commit_id_str, commit_id);
3463 if (error)
3464 goto done;
3467 if (branch_name) {
3468 struct got_object_id *head_commit_id;
3469 TAILQ_FOREACH(pe, &paths, entry) {
3470 if (pe->path_len == 0)
3471 continue;
3472 error = got_error_msg(GOT_ERR_BAD_PATH,
3473 "switching between branches requires that "
3474 "the entire work tree gets updated");
3475 goto done;
3477 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3478 if (error)
3479 goto done;
3480 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3481 repo);
3482 free(head_commit_id);
3483 if (error != NULL)
3484 goto done;
3485 error = check_same_branch(commit_id, head_ref, NULL, repo);
3486 if (error)
3487 goto done;
3488 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3489 if (error)
3490 goto done;
3491 } else {
3492 error = check_linear_ancestry(commit_id,
3493 got_worktree_get_base_commit_id(worktree), 0, repo);
3494 if (error != NULL) {
3495 if (error->code == GOT_ERR_ANCESTRY)
3496 error = got_error(GOT_ERR_BRANCH_MOVED);
3497 goto done;
3499 error = check_same_branch(commit_id, head_ref, NULL, repo);
3500 if (error)
3501 goto done;
3504 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3505 commit_id) != 0) {
3506 error = got_worktree_set_base_commit_id(worktree, repo,
3507 commit_id);
3508 if (error)
3509 goto done;
3512 memset(&upa, 0, sizeof(upa));
3513 upa.verbosity = verbosity;
3514 error = got_worktree_checkout_files(worktree, &paths, repo,
3515 update_progress, &upa, check_cancelled, NULL);
3516 if (error != NULL)
3517 goto done;
3519 if (upa.did_something) {
3520 printf("Updated to %s: %s\n",
3521 got_worktree_get_head_ref_name(worktree), commit_id_str);
3522 } else
3523 printf("Already up-to-date\n");
3525 print_update_progress_stats(&upa);
3526 done:
3527 if (pack_fds) {
3528 const struct got_error *pack_err =
3529 got_repo_pack_fds_close(pack_fds);
3530 if (error == NULL)
3531 error = pack_err;
3533 free(worktree_path);
3534 TAILQ_FOREACH(pe, &paths, entry)
3535 free((char *)pe->path);
3536 got_pathlist_free(&paths);
3537 free(commit_id);
3538 free(commit_id_str);
3539 return error;
3542 static const struct got_error *
3543 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3544 const char *path, int diff_context, int ignore_whitespace,
3545 int force_text_diff, struct got_repository *repo, FILE *outfile)
3547 const struct got_error *err = NULL;
3548 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3549 FILE *f1 = NULL, *f2 = NULL;
3550 int fd1 = -1, fd2 = -1;
3552 fd1 = got_opentempfd();
3553 if (fd1 == -1)
3554 return got_error_from_errno("got_opentempfd");
3555 fd2 = got_opentempfd();
3556 if (fd2 == -1) {
3557 err = got_error_from_errno("got_opentempfd");
3558 goto done;
3561 if (blob_id1) {
3562 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3563 fd1);
3564 if (err)
3565 goto done;
3566 f1 = got_opentemp();
3567 if (f1 == NULL) {
3568 err = got_error_from_errno("got_opentemp");
3569 goto done;
3573 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3574 if (err)
3575 goto done;
3577 f2 = got_opentemp();
3578 if (f2 == NULL) {
3579 err = got_error_from_errno("got_opentemp");
3580 goto done;
3583 while (path[0] == '/')
3584 path++;
3585 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3586 diff_context, ignore_whitespace, force_text_diff, outfile);
3587 done:
3588 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3589 err = got_error_from_errno("close");
3590 if (blob1)
3591 got_object_blob_close(blob1);
3592 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3593 err = got_error_from_errno("close");
3594 got_object_blob_close(blob2);
3595 if (f1 && fclose(f1) == EOF && err == NULL)
3596 err = got_error_from_errno("fclose");
3597 if (f2 && fclose(f2) == EOF && err == NULL)
3598 err = got_error_from_errno("fclose");
3599 return err;
3602 static const struct got_error *
3603 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3604 const char *path, int diff_context, int ignore_whitespace,
3605 int force_text_diff, struct got_repository *repo, FILE *outfile)
3607 const struct got_error *err = NULL;
3608 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3609 struct got_diff_blob_output_unidiff_arg arg;
3610 FILE *f1 = NULL, *f2 = NULL;
3612 if (tree_id1) {
3613 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3614 if (err)
3615 goto done;
3616 f1 = got_opentemp();
3617 if (f1 == NULL) {
3618 err = got_error_from_errno("got_opentemp");
3619 goto done;
3623 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3624 if (err)
3625 goto done;
3627 f2 = got_opentemp();
3628 if (f2 == NULL) {
3629 err = got_error_from_errno("got_opentemp");
3630 goto done;
3633 arg.diff_context = diff_context;
3634 arg.ignore_whitespace = ignore_whitespace;
3635 arg.force_text_diff = force_text_diff;
3636 arg.outfile = outfile;
3637 arg.line_offsets = NULL;
3638 arg.nlines = 0;
3639 while (path[0] == '/')
3640 path++;
3641 err = got_diff_tree(tree1, tree2, f1, f2, path, path, repo,
3642 got_diff_blob_output_unidiff, &arg, 1);
3643 done:
3644 if (tree1)
3645 got_object_tree_close(tree1);
3646 if (tree2)
3647 got_object_tree_close(tree2);
3648 if (f1 && fclose(f1) == EOF && err == NULL)
3649 err = got_error_from_errno("fclose");
3650 if (f2 && fclose(f2) == EOF && err == NULL)
3651 err = got_error_from_errno("fclose");
3652 return err;
3655 static const struct got_error *
3656 get_changed_paths(struct got_pathlist_head *paths,
3657 struct got_commit_object *commit, struct got_repository *repo)
3659 const struct got_error *err = NULL;
3660 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3661 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3662 struct got_object_qid *qid;
3664 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3665 if (qid != NULL) {
3666 struct got_commit_object *pcommit;
3667 err = got_object_open_as_commit(&pcommit, repo,
3668 &qid->id);
3669 if (err)
3670 return err;
3672 tree_id1 = got_object_id_dup(
3673 got_object_commit_get_tree_id(pcommit));
3674 if (tree_id1 == NULL) {
3675 got_object_commit_close(pcommit);
3676 return got_error_from_errno("got_object_id_dup");
3678 got_object_commit_close(pcommit);
3682 if (tree_id1) {
3683 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3684 if (err)
3685 goto done;
3688 tree_id2 = got_object_commit_get_tree_id(commit);
3689 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3690 if (err)
3691 goto done;
3693 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3694 got_diff_tree_collect_changed_paths, paths, 0);
3695 done:
3696 if (tree1)
3697 got_object_tree_close(tree1);
3698 if (tree2)
3699 got_object_tree_close(tree2);
3700 free(tree_id1);
3701 return err;
3704 static const struct got_error *
3705 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3706 const char *path, int diff_context, struct got_repository *repo,
3707 FILE *outfile)
3709 const struct got_error *err = NULL;
3710 struct got_commit_object *pcommit = NULL;
3711 char *id_str1 = NULL, *id_str2 = NULL;
3712 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3713 struct got_object_qid *qid;
3715 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3716 if (qid != NULL) {
3717 err = got_object_open_as_commit(&pcommit, repo,
3718 &qid->id);
3719 if (err)
3720 return err;
3721 err = got_object_id_str(&id_str1, &qid->id);
3722 if (err)
3723 goto done;
3726 err = got_object_id_str(&id_str2, id);
3727 if (err)
3728 goto done;
3730 if (path && path[0] != '\0') {
3731 int obj_type;
3732 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3733 if (err)
3734 goto done;
3735 if (pcommit) {
3736 err = got_object_id_by_path(&obj_id1, repo,
3737 pcommit, path);
3738 if (err) {
3739 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3740 free(obj_id2);
3741 goto done;
3745 err = got_object_get_type(&obj_type, repo, obj_id2);
3746 if (err) {
3747 free(obj_id2);
3748 goto done;
3750 fprintf(outfile,
3751 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3752 fprintf(outfile, "commit - %s\n",
3753 id_str1 ? id_str1 : "/dev/null");
3754 fprintf(outfile, "commit + %s\n", id_str2);
3755 switch (obj_type) {
3756 case GOT_OBJ_TYPE_BLOB:
3757 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3758 0, 0, repo, outfile);
3759 break;
3760 case GOT_OBJ_TYPE_TREE:
3761 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3762 0, 0, repo, outfile);
3763 break;
3764 default:
3765 err = got_error(GOT_ERR_OBJ_TYPE);
3766 break;
3768 free(obj_id1);
3769 free(obj_id2);
3770 } else {
3771 obj_id2 = got_object_commit_get_tree_id(commit);
3772 if (pcommit)
3773 obj_id1 = got_object_commit_get_tree_id(pcommit);
3774 fprintf(outfile,
3775 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3776 fprintf(outfile, "commit - %s\n",
3777 id_str1 ? id_str1 : "/dev/null");
3778 fprintf(outfile, "commit + %s\n", id_str2);
3779 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3780 repo, outfile);
3782 done:
3783 free(id_str1);
3784 free(id_str2);
3785 if (pcommit)
3786 got_object_commit_close(pcommit);
3787 return err;
3790 static char *
3791 get_datestr(time_t *time, char *datebuf)
3793 struct tm mytm, *tm;
3794 char *p, *s;
3796 tm = gmtime_r(time, &mytm);
3797 if (tm == NULL)
3798 return NULL;
3799 s = asctime_r(tm, datebuf);
3800 if (s == NULL)
3801 return NULL;
3802 p = strchr(s, '\n');
3803 if (p)
3804 *p = '\0';
3805 return s;
3808 static const struct got_error *
3809 match_commit(int *have_match, struct got_object_id *id,
3810 struct got_commit_object *commit, regex_t *regex)
3812 const struct got_error *err = NULL;
3813 regmatch_t regmatch;
3814 char *id_str = NULL, *logmsg = NULL;
3816 *have_match = 0;
3818 err = got_object_id_str(&id_str, id);
3819 if (err)
3820 return err;
3822 err = got_object_commit_get_logmsg(&logmsg, commit);
3823 if (err)
3824 goto done;
3826 if (regexec(regex, got_object_commit_get_author(commit), 1,
3827 &regmatch, 0) == 0 ||
3828 regexec(regex, got_object_commit_get_committer(commit), 1,
3829 &regmatch, 0) == 0 ||
3830 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
3831 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3832 *have_match = 1;
3833 done:
3834 free(id_str);
3835 free(logmsg);
3836 return err;
3839 static void
3840 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3841 regex_t *regex)
3843 regmatch_t regmatch;
3844 struct got_pathlist_entry *pe;
3846 *have_match = 0;
3848 TAILQ_FOREACH(pe, changed_paths, entry) {
3849 if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3850 *have_match = 1;
3851 break;
3856 static const struct got_error *
3857 match_patch(int *have_match, struct got_commit_object *commit,
3858 struct got_object_id *id, const char *path, int diff_context,
3859 struct got_repository *repo, regex_t *regex, FILE *f)
3861 const struct got_error *err = NULL;
3862 char *line = NULL;
3863 size_t linesize = 0;
3864 ssize_t linelen;
3865 regmatch_t regmatch;
3867 *have_match = 0;
3869 err = got_opentemp_truncate(f);
3870 if (err)
3871 return err;
3873 err = print_patch(commit, id, path, diff_context, repo, f);
3874 if (err)
3875 goto done;
3877 if (fseeko(f, 0L, SEEK_SET) == -1) {
3878 err = got_error_from_errno("fseeko");
3879 goto done;
3882 while ((linelen = getline(&line, &linesize, f)) != -1) {
3883 if (regexec(regex, line, 1, &regmatch, 0) == 0) {
3884 *have_match = 1;
3885 break;
3888 done:
3889 free(line);
3890 return err;
3893 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3895 static const struct got_error*
3896 build_refs_str(char **refs_str, struct got_reflist_head *refs,
3897 struct got_object_id *id, struct got_repository *repo,
3898 int local_only)
3900 static const struct got_error *err = NULL;
3901 struct got_reflist_entry *re;
3902 char *s;
3903 const char *name;
3905 *refs_str = NULL;
3907 TAILQ_FOREACH(re, refs, entry) {
3908 struct got_tag_object *tag = NULL;
3909 struct got_object_id *ref_id;
3910 int cmp;
3912 name = got_ref_get_name(re->ref);
3913 if (strcmp(name, GOT_REF_HEAD) == 0)
3914 continue;
3915 if (strncmp(name, "refs/", 5) == 0)
3916 name += 5;
3917 if (strncmp(name, "got/", 4) == 0)
3918 continue;
3919 if (strncmp(name, "heads/", 6) == 0)
3920 name += 6;
3921 if (strncmp(name, "remotes/", 8) == 0) {
3922 if (local_only)
3923 continue;
3924 name += 8;
3925 s = strstr(name, "/" GOT_REF_HEAD);
3926 if (s != NULL && s[strlen(s)] == '\0')
3927 continue;
3929 err = got_ref_resolve(&ref_id, repo, re->ref);
3930 if (err)
3931 break;
3932 if (strncmp(name, "tags/", 5) == 0) {
3933 err = got_object_open_as_tag(&tag, repo, ref_id);
3934 if (err) {
3935 if (err->code != GOT_ERR_OBJ_TYPE) {
3936 free(ref_id);
3937 break;
3939 /* Ref points at something other than a tag. */
3940 err = NULL;
3941 tag = NULL;
3944 cmp = got_object_id_cmp(tag ?
3945 got_object_tag_get_object_id(tag) : ref_id, id);
3946 free(ref_id);
3947 if (tag)
3948 got_object_tag_close(tag);
3949 if (cmp != 0)
3950 continue;
3951 s = *refs_str;
3952 if (asprintf(refs_str, "%s%s%s", s ? s : "",
3953 s ? ", " : "", name) == -1) {
3954 err = got_error_from_errno("asprintf");
3955 free(s);
3956 *refs_str = NULL;
3957 break;
3959 free(s);
3962 return err;
3965 static const struct got_error *
3966 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
3967 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
3969 const struct got_error *err = NULL;
3970 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
3971 char *comma, *s, *nl;
3972 struct got_reflist_head *refs;
3973 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
3974 struct tm tm;
3975 time_t committer_time;
3977 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
3978 if (refs) {
3979 err = build_refs_str(&ref_str, refs, id, repo, 1);
3980 if (err)
3981 return err;
3983 /* Display the first matching ref only. */
3984 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
3985 *comma = '\0';
3988 if (ref_str == NULL) {
3989 err = got_object_id_str(&id_str, id);
3990 if (err)
3991 return err;
3994 committer_time = got_object_commit_get_committer_time(commit);
3995 if (gmtime_r(&committer_time, &tm) == NULL) {
3996 err = got_error_from_errno("gmtime_r");
3997 goto done;
3999 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4000 err = got_error(GOT_ERR_NO_SPACE);
4001 goto done;
4004 err = got_object_commit_get_logmsg(&logmsg0, commit);
4005 if (err)
4006 goto done;
4008 s = logmsg0;
4009 while (isspace((unsigned char)s[0]))
4010 s++;
4012 nl = strchr(s, '\n');
4013 if (nl) {
4014 *nl = '\0';
4017 if (ref_str)
4018 printf("%s%-7s %s\n", datebuf, ref_str, s);
4019 else
4020 printf("%s%.7s %s\n", datebuf, id_str, s);
4022 if (fflush(stdout) != 0 && err == NULL)
4023 err = got_error_from_errno("fflush");
4024 done:
4025 free(id_str);
4026 free(ref_str);
4027 free(logmsg0);
4028 return err;
4031 static const struct got_error *
4032 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4033 struct got_repository *repo, const char *path,
4034 struct got_pathlist_head *changed_paths, int show_patch,
4035 int diff_context, struct got_reflist_object_id_map *refs_idmap,
4036 const char *custom_refs_str)
4038 const struct got_error *err = NULL;
4039 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4040 char datebuf[26];
4041 time_t committer_time;
4042 const char *author, *committer;
4043 char *refs_str = NULL;
4045 err = got_object_id_str(&id_str, id);
4046 if (err)
4047 return err;
4049 if (custom_refs_str == NULL) {
4050 struct got_reflist_head *refs;
4051 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4052 if (refs) {
4053 err = build_refs_str(&refs_str, refs, id, repo, 0);
4054 if (err)
4055 goto done;
4059 printf(GOT_COMMIT_SEP_STR);
4060 if (custom_refs_str)
4061 printf("commit %s (%s)\n", id_str, custom_refs_str);
4062 else
4063 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
4064 refs_str ? refs_str : "", refs_str ? ")" : "");
4065 free(id_str);
4066 id_str = NULL;
4067 free(refs_str);
4068 refs_str = NULL;
4069 printf("from: %s\n", got_object_commit_get_author(commit));
4070 committer_time = got_object_commit_get_committer_time(commit);
4071 datestr = get_datestr(&committer_time, datebuf);
4072 if (datestr)
4073 printf("date: %s UTC\n", datestr);
4074 author = got_object_commit_get_author(commit);
4075 committer = got_object_commit_get_committer(commit);
4076 if (strcmp(author, committer) != 0)
4077 printf("via: %s\n", committer);
4078 if (got_object_commit_get_nparents(commit) > 1) {
4079 const struct got_object_id_queue *parent_ids;
4080 struct got_object_qid *qid;
4081 int n = 1;
4082 parent_ids = got_object_commit_get_parent_ids(commit);
4083 STAILQ_FOREACH(qid, parent_ids, entry) {
4084 err = got_object_id_str(&id_str, &qid->id);
4085 if (err)
4086 goto done;
4087 printf("parent %d: %s\n", n++, id_str);
4088 free(id_str);
4089 id_str = NULL;
4093 err = got_object_commit_get_logmsg(&logmsg0, commit);
4094 if (err)
4095 goto done;
4097 logmsg = logmsg0;
4098 do {
4099 line = strsep(&logmsg, "\n");
4100 if (line)
4101 printf(" %s\n", line);
4102 } while (line);
4103 free(logmsg0);
4105 if (changed_paths) {
4106 struct got_pathlist_entry *pe;
4107 TAILQ_FOREACH(pe, changed_paths, entry) {
4108 struct got_diff_changed_path *cp = pe->data;
4109 printf(" %c %s\n", cp->status, pe->path);
4111 printf("\n");
4113 if (show_patch) {
4114 err = print_patch(commit, id, path, diff_context, repo, stdout);
4115 if (err == 0)
4116 printf("\n");
4119 if (fflush(stdout) != 0 && err == NULL)
4120 err = got_error_from_errno("fflush");
4121 done:
4122 free(id_str);
4123 free(refs_str);
4124 return err;
4127 static const struct got_error *
4128 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4129 struct got_repository *repo, const char *path, int show_changed_paths,
4130 int show_patch, const char *search_pattern, int diff_context, int limit,
4131 int log_branches, int reverse_display_order,
4132 struct got_reflist_object_id_map *refs_idmap, int one_line,
4133 FILE *tmpfile)
4135 const struct got_error *err;
4136 struct got_commit_graph *graph;
4137 regex_t regex;
4138 int have_match;
4139 struct got_object_id_queue reversed_commits;
4140 struct got_object_qid *qid;
4141 struct got_commit_object *commit;
4142 struct got_pathlist_head changed_paths;
4143 struct got_pathlist_entry *pe;
4145 STAILQ_INIT(&reversed_commits);
4146 TAILQ_INIT(&changed_paths);
4148 if (search_pattern && regcomp(&regex, search_pattern,
4149 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4150 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4152 err = got_commit_graph_open(&graph, path, !log_branches);
4153 if (err)
4154 return err;
4155 err = got_commit_graph_iter_start(graph, root_id, repo,
4156 check_cancelled, NULL);
4157 if (err)
4158 goto done;
4159 for (;;) {
4160 struct got_object_id *id;
4162 if (sigint_received || sigpipe_received)
4163 break;
4165 err = got_commit_graph_iter_next(&id, graph, repo,
4166 check_cancelled, NULL);
4167 if (err) {
4168 if (err->code == GOT_ERR_ITER_COMPLETED)
4169 err = NULL;
4170 break;
4172 if (id == NULL)
4173 break;
4175 err = got_object_open_as_commit(&commit, repo, id);
4176 if (err)
4177 break;
4179 if (show_changed_paths && !reverse_display_order) {
4180 err = get_changed_paths(&changed_paths, commit, repo);
4181 if (err)
4182 break;
4185 if (search_pattern) {
4186 err = match_commit(&have_match, id, commit, &regex);
4187 if (err) {
4188 got_object_commit_close(commit);
4189 break;
4191 if (have_match == 0 && show_changed_paths)
4192 match_changed_paths(&have_match,
4193 &changed_paths, &regex);
4194 if (have_match == 0 && show_patch) {
4195 err = match_patch(&have_match, commit, id,
4196 path, diff_context, repo, &regex,
4197 tmpfile);
4198 if (err)
4199 break;
4201 if (have_match == 0) {
4202 got_object_commit_close(commit);
4203 TAILQ_FOREACH(pe, &changed_paths, entry) {
4204 free((char *)pe->path);
4205 free(pe->data);
4207 got_pathlist_free(&changed_paths);
4208 continue;
4212 if (reverse_display_order) {
4213 err = got_object_qid_alloc(&qid, id);
4214 if (err)
4215 break;
4216 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4217 got_object_commit_close(commit);
4218 } else {
4219 if (one_line)
4220 err = print_commit_oneline(commit, id,
4221 repo, refs_idmap);
4222 else
4223 err = print_commit(commit, id, repo, path,
4224 show_changed_paths ? &changed_paths : NULL,
4225 show_patch, diff_context, refs_idmap, NULL);
4226 got_object_commit_close(commit);
4227 if (err)
4228 break;
4230 if ((limit && --limit == 0) ||
4231 (end_id && got_object_id_cmp(id, end_id) == 0))
4232 break;
4234 TAILQ_FOREACH(pe, &changed_paths, entry) {
4235 free((char *)pe->path);
4236 free(pe->data);
4238 got_pathlist_free(&changed_paths);
4240 if (reverse_display_order) {
4241 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4242 err = got_object_open_as_commit(&commit, repo,
4243 &qid->id);
4244 if (err)
4245 break;
4246 if (show_changed_paths) {
4247 err = get_changed_paths(&changed_paths,
4248 commit, repo);
4249 if (err)
4250 break;
4252 if (one_line)
4253 err = print_commit_oneline(commit, &qid->id,
4254 repo, refs_idmap);
4255 else
4256 err = print_commit(commit, &qid->id, repo, path,
4257 show_changed_paths ? &changed_paths : NULL,
4258 show_patch, diff_context, refs_idmap, NULL);
4259 got_object_commit_close(commit);
4260 if (err)
4261 break;
4262 TAILQ_FOREACH(pe, &changed_paths, entry) {
4263 free((char *)pe->path);
4264 free(pe->data);
4266 got_pathlist_free(&changed_paths);
4269 done:
4270 while (!STAILQ_EMPTY(&reversed_commits)) {
4271 qid = STAILQ_FIRST(&reversed_commits);
4272 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4273 got_object_qid_free(qid);
4275 TAILQ_FOREACH(pe, &changed_paths, entry) {
4276 free((char *)pe->path);
4277 free(pe->data);
4279 got_pathlist_free(&changed_paths);
4280 if (search_pattern)
4281 regfree(&regex);
4282 got_commit_graph_close(graph);
4283 return err;
4286 __dead static void
4287 usage_log(void)
4289 fprintf(stderr, "usage: %s log [-b] [-p] [-P] [-s] [-c commit] "
4290 "[-C number] [ -l N ] [-x commit] [-S search-pattern] "
4291 "[-r repository-path] [-R] [path]\n", getprogname());
4292 exit(1);
4295 static int
4296 get_default_log_limit(void)
4298 const char *got_default_log_limit;
4299 long long n;
4300 const char *errstr;
4302 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4303 if (got_default_log_limit == NULL)
4304 return 0;
4305 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4306 if (errstr != NULL)
4307 return 0;
4308 return n;
4311 static const struct got_error *
4312 cmd_log(int argc, char *argv[])
4314 const struct got_error *error;
4315 struct got_repository *repo = NULL;
4316 struct got_worktree *worktree = NULL;
4317 struct got_object_id *start_id = NULL, *end_id = NULL;
4318 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4319 const char *start_commit = NULL, *end_commit = NULL;
4320 const char *search_pattern = NULL;
4321 int diff_context = -1, ch;
4322 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4323 int reverse_display_order = 0, one_line = 0;
4324 const char *errstr;
4325 struct got_reflist_head refs;
4326 struct got_reflist_object_id_map *refs_idmap = NULL;
4327 FILE *tmpfile = NULL;
4328 int *pack_fds = NULL;
4330 TAILQ_INIT(&refs);
4332 #ifndef PROFILE
4333 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4334 NULL)
4335 == -1)
4336 err(1, "pledge");
4337 #endif
4339 limit = get_default_log_limit();
4341 while ((ch = getopt(argc, argv, "bpPc:C:l:r:RsS:x:")) != -1) {
4342 switch (ch) {
4343 case 'p':
4344 show_patch = 1;
4345 break;
4346 case 'P':
4347 show_changed_paths = 1;
4348 break;
4349 case 'c':
4350 start_commit = optarg;
4351 break;
4352 case 'C':
4353 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4354 &errstr);
4355 if (errstr != NULL)
4356 errx(1, "number of context lines is %s: %s",
4357 errstr, optarg);
4358 break;
4359 case 'l':
4360 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4361 if (errstr != NULL)
4362 errx(1, "number of commits is %s: %s",
4363 errstr, optarg);
4364 break;
4365 case 'b':
4366 log_branches = 1;
4367 break;
4368 case 'r':
4369 repo_path = realpath(optarg, NULL);
4370 if (repo_path == NULL)
4371 return got_error_from_errno2("realpath",
4372 optarg);
4373 got_path_strip_trailing_slashes(repo_path);
4374 break;
4375 case 'R':
4376 reverse_display_order = 1;
4377 break;
4378 case 's':
4379 one_line = 1;
4380 break;
4381 case 'S':
4382 search_pattern = optarg;
4383 break;
4384 case 'x':
4385 end_commit = optarg;
4386 break;
4387 default:
4388 usage_log();
4389 /* NOTREACHED */
4393 argc -= optind;
4394 argv += optind;
4396 if (diff_context == -1)
4397 diff_context = 3;
4398 else if (!show_patch)
4399 errx(1, "-C requires -p");
4401 if (one_line && (show_patch || show_changed_paths))
4402 errx(1, "cannot use -s with -p or -P");
4404 cwd = getcwd(NULL, 0);
4405 if (cwd == NULL) {
4406 error = got_error_from_errno("getcwd");
4407 goto done;
4410 error = got_repo_pack_fds_open(&pack_fds);
4411 if (error != NULL)
4412 goto done;
4414 if (repo_path == NULL) {
4415 error = got_worktree_open(&worktree, cwd);
4416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4417 goto done;
4418 error = NULL;
4421 if (argc == 1) {
4422 if (worktree) {
4423 error = got_worktree_resolve_path(&path, worktree,
4424 argv[0]);
4425 if (error)
4426 goto done;
4427 } else {
4428 path = strdup(argv[0]);
4429 if (path == NULL) {
4430 error = got_error_from_errno("strdup");
4431 goto done;
4434 } else if (argc != 0)
4435 usage_log();
4437 if (repo_path == NULL) {
4438 repo_path = worktree ?
4439 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4441 if (repo_path == NULL) {
4442 error = got_error_from_errno("strdup");
4443 goto done;
4446 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4447 if (error != NULL)
4448 goto done;
4450 error = apply_unveil(got_repo_get_path(repo), 1,
4451 worktree ? got_worktree_get_root_path(worktree) : NULL);
4452 if (error)
4453 goto done;
4455 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4456 if (error)
4457 goto done;
4459 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4460 if (error)
4461 goto done;
4463 if (start_commit == NULL) {
4464 struct got_reference *head_ref;
4465 struct got_commit_object *commit = NULL;
4466 error = got_ref_open(&head_ref, repo,
4467 worktree ? got_worktree_get_head_ref_name(worktree)
4468 : GOT_REF_HEAD, 0);
4469 if (error != NULL)
4470 goto done;
4471 error = got_ref_resolve(&start_id, repo, head_ref);
4472 got_ref_close(head_ref);
4473 if (error != NULL)
4474 goto done;
4475 error = got_object_open_as_commit(&commit, repo,
4476 start_id);
4477 if (error != NULL)
4478 goto done;
4479 got_object_commit_close(commit);
4480 } else {
4481 error = got_repo_match_object_id(&start_id, NULL,
4482 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4483 if (error != NULL)
4484 goto done;
4486 if (end_commit != NULL) {
4487 error = got_repo_match_object_id(&end_id, NULL,
4488 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4489 if (error != NULL)
4490 goto done;
4493 if (worktree) {
4495 * If a path was specified on the command line it was resolved
4496 * to a path in the work tree above. Prepend the work tree's
4497 * path prefix to obtain the corresponding in-repository path.
4499 if (path) {
4500 const char *prefix;
4501 prefix = got_worktree_get_path_prefix(worktree);
4502 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4503 (path[0] != '\0') ? "/" : "", path) == -1) {
4504 error = got_error_from_errno("asprintf");
4505 goto done;
4508 } else
4509 error = got_repo_map_path(&in_repo_path, repo,
4510 path ? path : "");
4511 if (error != NULL)
4512 goto done;
4513 if (in_repo_path) {
4514 free(path);
4515 path = in_repo_path;
4518 if (worktree) {
4519 /* Release work tree lock. */
4520 got_worktree_close(worktree);
4521 worktree = NULL;
4524 if (search_pattern && show_patch) {
4525 tmpfile = got_opentemp();
4526 if (tmpfile == NULL) {
4527 error = got_error_from_errno("got_opentemp");
4528 goto done;
4532 error = print_commits(start_id, end_id, repo, path ? path : "",
4533 show_changed_paths, show_patch, search_pattern, diff_context,
4534 limit, log_branches, reverse_display_order, refs_idmap, one_line,
4535 tmpfile);
4536 done:
4537 free(path);
4538 free(repo_path);
4539 free(cwd);
4540 if (worktree)
4541 got_worktree_close(worktree);
4542 if (repo) {
4543 const struct got_error *close_err = got_repo_close(repo);
4544 if (error == NULL)
4545 error = close_err;
4547 if (pack_fds) {
4548 const struct got_error *pack_err =
4549 got_repo_pack_fds_close(pack_fds);
4550 if (error == NULL)
4551 error = pack_err;
4553 if (refs_idmap)
4554 got_reflist_object_id_map_free(refs_idmap);
4555 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4556 error = got_error_from_errno("fclose");
4557 got_ref_list_free(&refs);
4558 return error;
4561 __dead static void
4562 usage_diff(void)
4564 fprintf(stderr, "usage: %s diff [-a] [-c commit] [-C number] "
4565 "[-r repository-path] [-s] [-w] [-P] "
4566 "[object1 object2 | path ...]\n", getprogname());
4567 exit(1);
4570 struct print_diff_arg {
4571 struct got_repository *repo;
4572 struct got_worktree *worktree;
4573 int diff_context;
4574 const char *id_str;
4575 int header_shown;
4576 int diff_staged;
4577 int ignore_whitespace;
4578 int force_text_diff;
4582 * Create a file which contains the target path of a symlink so we can feed
4583 * it as content to the diff engine.
4585 static const struct got_error *
4586 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4587 const char *abspath)
4589 const struct got_error *err = NULL;
4590 char target_path[PATH_MAX];
4591 ssize_t target_len, outlen;
4593 *fd = -1;
4595 if (dirfd != -1) {
4596 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4597 if (target_len == -1)
4598 return got_error_from_errno2("readlinkat", abspath);
4599 } else {
4600 target_len = readlink(abspath, target_path, PATH_MAX);
4601 if (target_len == -1)
4602 return got_error_from_errno2("readlink", abspath);
4605 *fd = got_opentempfd();
4606 if (*fd == -1)
4607 return got_error_from_errno("got_opentempfd");
4609 outlen = write(*fd, target_path, target_len);
4610 if (outlen == -1) {
4611 err = got_error_from_errno("got_opentempfd");
4612 goto done;
4615 if (lseek(*fd, 0, SEEK_SET) == -1) {
4616 err = got_error_from_errno2("lseek", abspath);
4617 goto done;
4619 done:
4620 if (err) {
4621 close(*fd);
4622 *fd = -1;
4624 return err;
4627 static const struct got_error *
4628 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4629 const char *path, struct got_object_id *blob_id,
4630 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4631 int dirfd, const char *de_name)
4633 struct print_diff_arg *a = arg;
4634 const struct got_error *err = NULL;
4635 struct got_blob_object *blob1 = NULL;
4636 int fd = -1, fd1 = -1;
4637 FILE *f1 = NULL, *f2 = NULL;
4638 char *abspath = NULL, *label1 = NULL;
4639 struct stat sb;
4640 off_t size1 = 0;
4642 if (a->diff_staged) {
4643 if (staged_status != GOT_STATUS_MODIFY &&
4644 staged_status != GOT_STATUS_ADD &&
4645 staged_status != GOT_STATUS_DELETE)
4646 return NULL;
4647 } else {
4648 if (staged_status == GOT_STATUS_DELETE)
4649 return NULL;
4650 if (status == GOT_STATUS_NONEXISTENT)
4651 return got_error_set_errno(ENOENT, path);
4652 if (status != GOT_STATUS_MODIFY &&
4653 status != GOT_STATUS_ADD &&
4654 status != GOT_STATUS_DELETE &&
4655 status != GOT_STATUS_CONFLICT)
4656 return NULL;
4659 if (!a->header_shown) {
4660 printf("diff %s%s\n", a->diff_staged ? "-s " : "",
4661 got_worktree_get_root_path(a->worktree));
4662 printf("commit - %s\n", a->id_str);
4663 printf("path + %s%s\n",
4664 got_worktree_get_root_path(a->worktree),
4665 a->diff_staged ? " (staged changes)" : "");
4666 a->header_shown = 1;
4669 if (a->diff_staged) {
4670 const char *label1 = NULL, *label2 = NULL;
4671 switch (staged_status) {
4672 case GOT_STATUS_MODIFY:
4673 label1 = path;
4674 label2 = path;
4675 break;
4676 case GOT_STATUS_ADD:
4677 label2 = path;
4678 break;
4679 case GOT_STATUS_DELETE:
4680 label1 = path;
4681 break;
4682 default:
4683 return got_error(GOT_ERR_FILE_STATUS);
4685 f1 = got_opentemp();
4686 if (f1 == NULL) {
4687 err = got_error_from_errno("got_opentemp");
4688 goto done;
4690 f2 = got_opentemp();
4691 if (f2 == NULL) {
4692 err = got_error_from_errno("got_opentemp");
4693 goto done;
4695 err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
4696 blob_id, staged_blob_id, label1, label2, a->diff_context,
4697 a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
4698 goto done;
4701 fd1 = got_opentempfd();
4702 if (fd1 == -1) {
4703 err = got_error_from_errno("got_opentempfd");
4704 goto done;
4707 if (staged_status == GOT_STATUS_ADD ||
4708 staged_status == GOT_STATUS_MODIFY) {
4709 char *id_str;
4710 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4711 8192, fd1);
4712 if (err)
4713 goto done;
4714 err = got_object_id_str(&id_str, staged_blob_id);
4715 if (err)
4716 goto done;
4717 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4718 err = got_error_from_errno("asprintf");
4719 free(id_str);
4720 goto done;
4722 free(id_str);
4723 } else if (status != GOT_STATUS_ADD) {
4724 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4725 fd1);
4726 if (err)
4727 goto done;
4730 if (status != GOT_STATUS_DELETE) {
4731 if (asprintf(&abspath, "%s/%s",
4732 got_worktree_get_root_path(a->worktree), path) == -1) {
4733 err = got_error_from_errno("asprintf");
4734 goto done;
4737 if (dirfd != -1) {
4738 fd = openat(dirfd, de_name,
4739 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4740 if (fd == -1) {
4741 if (!got_err_open_nofollow_on_symlink()) {
4742 err = got_error_from_errno2("openat",
4743 abspath);
4744 goto done;
4746 err = get_symlink_target_file(&fd, dirfd,
4747 de_name, abspath);
4748 if (err)
4749 goto done;
4751 } else {
4752 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4753 if (fd == -1) {
4754 if (!got_err_open_nofollow_on_symlink()) {
4755 err = got_error_from_errno2("open",
4756 abspath);
4757 goto done;
4759 err = get_symlink_target_file(&fd, dirfd,
4760 de_name, abspath);
4761 if (err)
4762 goto done;
4765 if (fstat(fd, &sb) == -1) {
4766 err = got_error_from_errno2("fstat", abspath);
4767 goto done;
4769 f2 = fdopen(fd, "r");
4770 if (f2 == NULL) {
4771 err = got_error_from_errno2("fdopen", abspath);
4772 goto done;
4774 fd = -1;
4775 } else
4776 sb.st_size = 0;
4778 if (blob1) {
4779 f1 = got_opentemp();
4780 if (f1 == NULL) {
4781 err = got_error_from_errno("got_opentemp");
4782 goto done;
4784 err = got_object_blob_dump_to_file(&size1, NULL, NULL, f1,
4785 blob1);
4786 if (err)
4787 goto done;
4790 err = got_diff_blob_file(blob1, f1, size1, label1, f2, sb.st_size,
4791 path, a->diff_context, a->ignore_whitespace, a->force_text_diff,
4792 stdout);
4793 done:
4794 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
4795 err = got_error_from_errno("close");
4796 if (blob1)
4797 got_object_blob_close(blob1);
4798 if (f1 && fclose(f1) == EOF && err == NULL)
4799 err = got_error_from_errno("fclose");
4800 if (f2 && fclose(f2) == EOF && err == NULL)
4801 err = got_error_from_errno("fclose");
4802 if (fd != -1 && close(fd) == -1 && err == NULL)
4803 err = got_error_from_errno("close");
4804 free(abspath);
4805 return err;
4808 static const struct got_error *
4809 cmd_diff(int argc, char *argv[])
4811 const struct got_error *error;
4812 struct got_repository *repo = NULL;
4813 struct got_worktree *worktree = NULL;
4814 char *cwd = NULL, *repo_path = NULL;
4815 const char *commit_args[2] = { NULL, NULL };
4816 int ncommit_args = 0;
4817 struct got_object_id *ids[2] = { NULL, NULL };
4818 char *labels[2] = { NULL, NULL };
4819 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
4820 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
4821 int force_text_diff = 0, force_path = 0, rflag = 0;
4822 const char *errstr;
4823 struct got_reflist_head refs;
4824 struct got_pathlist_head paths;
4825 struct got_pathlist_entry *pe;
4826 FILE *f1 = NULL, *f2 = NULL;
4827 int *pack_fds = NULL;
4829 TAILQ_INIT(&refs);
4830 TAILQ_INIT(&paths);
4832 #ifndef PROFILE
4833 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4834 NULL) == -1)
4835 err(1, "pledge");
4836 #endif
4838 while ((ch = getopt(argc, argv, "ac:C:r:swP")) != -1) {
4839 switch (ch) {
4840 case 'a':
4841 force_text_diff = 1;
4842 break;
4843 case 'c':
4844 if (ncommit_args >= 2)
4845 errx(1, "too many -c options used");
4846 commit_args[ncommit_args++] = optarg;
4847 break;
4848 case 'C':
4849 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4850 &errstr);
4851 if (errstr != NULL)
4852 errx(1, "number of context lines is %s: %s",
4853 errstr, optarg);
4854 break;
4855 case 'r':
4856 repo_path = realpath(optarg, NULL);
4857 if (repo_path == NULL)
4858 return got_error_from_errno2("realpath",
4859 optarg);
4860 got_path_strip_trailing_slashes(repo_path);
4861 rflag = 1;
4862 break;
4863 case 's':
4864 diff_staged = 1;
4865 break;
4866 case 'w':
4867 ignore_whitespace = 1;
4868 break;
4869 case 'P':
4870 force_path = 1;
4871 break;
4872 default:
4873 usage_diff();
4874 /* NOTREACHED */
4878 argc -= optind;
4879 argv += optind;
4881 cwd = getcwd(NULL, 0);
4882 if (cwd == NULL) {
4883 error = got_error_from_errno("getcwd");
4884 goto done;
4887 error = got_repo_pack_fds_open(&pack_fds);
4888 if (error != NULL)
4889 goto done;
4891 if (repo_path == NULL) {
4892 error = got_worktree_open(&worktree, cwd);
4893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4894 goto done;
4895 else
4896 error = NULL;
4897 if (worktree) {
4898 repo_path =
4899 strdup(got_worktree_get_repo_path(worktree));
4900 if (repo_path == NULL) {
4901 error = got_error_from_errno("strdup");
4902 goto done;
4904 } else {
4905 repo_path = strdup(cwd);
4906 if (repo_path == NULL) {
4907 error = got_error_from_errno("strdup");
4908 goto done;
4913 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4914 free(repo_path);
4915 if (error != NULL)
4916 goto done;
4918 if (rflag || worktree == NULL || ncommit_args > 0) {
4919 if (force_path) {
4920 error = got_error_msg(GOT_ERR_NOT_IMPL,
4921 "-P option can only be used when diffing "
4922 "a work tree");
4923 goto done;
4925 if (diff_staged) {
4926 error = got_error_msg(GOT_ERR_NOT_IMPL,
4927 "-s option can only be used when diffing "
4928 "a work tree");
4929 goto done;
4933 error = apply_unveil(got_repo_get_path(repo), 1,
4934 worktree ? got_worktree_get_root_path(worktree) : NULL);
4935 if (error)
4936 goto done;
4938 if ((!force_path && argc == 2) || ncommit_args > 0) {
4939 int obj_type = (ncommit_args > 0 ?
4940 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
4941 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
4942 NULL);
4943 if (error)
4944 goto done;
4945 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
4946 const char *arg;
4947 if (ncommit_args > 0)
4948 arg = commit_args[i];
4949 else
4950 arg = argv[i];
4951 error = got_repo_match_object_id(&ids[i], &labels[i],
4952 arg, obj_type, &refs, repo);
4953 if (error) {
4954 if (error->code != GOT_ERR_NOT_REF &&
4955 error->code != GOT_ERR_NO_OBJ)
4956 goto done;
4957 if (ncommit_args > 0)
4958 goto done;
4959 error = NULL;
4960 break;
4965 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
4966 struct print_diff_arg arg;
4967 char *id_str;
4969 if (worktree == NULL) {
4970 if (argc == 2 && ids[0] == NULL) {
4971 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
4972 goto done;
4973 } else if (argc == 2 && ids[1] == NULL) {
4974 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
4975 goto done;
4976 } else if (argc > 0) {
4977 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
4978 "%s", "specified paths cannot be resolved");
4979 goto done;
4980 } else {
4981 error = got_error(GOT_ERR_NOT_WORKTREE);
4982 goto done;
4986 error = get_worktree_paths_from_argv(&paths, argc, argv,
4987 worktree);
4988 if (error)
4989 goto done;
4991 error = got_object_id_str(&id_str,
4992 got_worktree_get_base_commit_id(worktree));
4993 if (error)
4994 goto done;
4995 arg.repo = repo;
4996 arg.worktree = worktree;
4997 arg.diff_context = diff_context;
4998 arg.id_str = id_str;
4999 arg.header_shown = 0;
5000 arg.diff_staged = diff_staged;
5001 arg.ignore_whitespace = ignore_whitespace;
5002 arg.force_text_diff = force_text_diff;
5004 error = got_worktree_status(worktree, &paths, repo, 0,
5005 print_diff, &arg, check_cancelled, NULL);
5006 free(id_str);
5007 goto done;
5010 if (ncommit_args == 1) {
5011 struct got_commit_object *commit;
5012 error = got_object_open_as_commit(&commit, repo, ids[0]);
5013 if (error)
5014 goto done;
5016 labels[1] = labels[0];
5017 ids[1] = ids[0];
5018 if (got_object_commit_get_nparents(commit) > 0) {
5019 const struct got_object_id_queue *pids;
5020 struct got_object_qid *pid;
5021 pids = got_object_commit_get_parent_ids(commit);
5022 pid = STAILQ_FIRST(pids);
5023 ids[0] = got_object_id_dup(&pid->id);
5024 if (ids[0] == NULL) {
5025 error = got_error_from_errno(
5026 "got_object_id_dup");
5027 got_object_commit_close(commit);
5028 goto done;
5030 error = got_object_id_str(&labels[0], ids[0]);
5031 if (error) {
5032 got_object_commit_close(commit);
5033 goto done;
5035 } else {
5036 ids[0] = NULL;
5037 labels[0] = strdup("/dev/null");
5038 if (labels[0] == NULL) {
5039 error = got_error_from_errno("strdup");
5040 got_object_commit_close(commit);
5041 goto done;
5045 got_object_commit_close(commit);
5048 if (ncommit_args == 0 && argc > 2) {
5049 error = got_error_msg(GOT_ERR_BAD_PATH,
5050 "path arguments cannot be used when diffing two objects");
5051 goto done;
5054 if (ids[0]) {
5055 error = got_object_get_type(&type1, repo, ids[0]);
5056 if (error)
5057 goto done;
5060 error = got_object_get_type(&type2, repo, ids[1]);
5061 if (error)
5062 goto done;
5063 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5064 error = got_error(GOT_ERR_OBJ_TYPE);
5065 goto done;
5067 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 0) {
5068 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5069 "path arguments cannot be used when diffing blobs");
5070 goto done;
5073 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5074 char *in_repo_path;
5075 struct got_pathlist_entry *new;
5076 if (worktree) {
5077 const char *prefix;
5078 char *p;
5079 error = got_worktree_resolve_path(&p, worktree,
5080 argv[i]);
5081 if (error)
5082 goto done;
5083 prefix = got_worktree_get_path_prefix(worktree);
5084 while (prefix[0] == '/')
5085 prefix++;
5086 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5087 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5088 p) == -1) {
5089 error = got_error_from_errno("asprintf");
5090 free(p);
5091 goto done;
5093 free(p);
5094 } else {
5095 char *mapped_path, *s;
5096 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5097 if (error)
5098 goto done;
5099 s = mapped_path;
5100 while (s[0] == '/')
5101 s++;
5102 in_repo_path = strdup(s);
5103 if (in_repo_path == NULL) {
5104 error = got_error_from_errno("asprintf");
5105 free(mapped_path);
5106 goto done;
5108 free(mapped_path);
5111 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5112 if (error || new == NULL /* duplicate */)
5113 free(in_repo_path);
5114 if (error)
5115 goto done;
5118 if (worktree) {
5119 /* Release work tree lock. */
5120 got_worktree_close(worktree);
5121 worktree = NULL;
5124 f1 = got_opentemp();
5125 if (f1 == NULL) {
5126 error = got_error_from_errno("got_opentemp");
5127 goto done;
5130 f2 = got_opentemp();
5131 if (f2 == NULL) {
5132 error = got_error_from_errno("got_opentemp");
5133 goto done;
5136 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5137 case GOT_OBJ_TYPE_BLOB:
5138 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5139 ids[0], ids[1], NULL, NULL, diff_context,
5140 ignore_whitespace, force_text_diff, repo, stdout);
5141 break;
5142 case GOT_OBJ_TYPE_TREE:
5143 error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
5144 ids[0], ids[1], &paths, "", "", diff_context,
5145 ignore_whitespace, force_text_diff, repo, stdout);
5146 break;
5147 case GOT_OBJ_TYPE_COMMIT:
5148 printf("diff %s %s\n", labels[0], labels[1]);
5149 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5150 ids[0], ids[1], &paths, diff_context, ignore_whitespace,
5151 force_text_diff, repo, stdout);
5152 break;
5153 default:
5154 error = got_error(GOT_ERR_OBJ_TYPE);
5156 done:
5157 free(labels[0]);
5158 free(labels[1]);
5159 free(ids[0]);
5160 free(ids[1]);
5161 if (worktree)
5162 got_worktree_close(worktree);
5163 if (repo) {
5164 const struct got_error *close_err = got_repo_close(repo);
5165 if (error == NULL)
5166 error = close_err;
5168 if (pack_fds) {
5169 const struct got_error *pack_err =
5170 got_repo_pack_fds_close(pack_fds);
5171 if (error == NULL)
5172 error = pack_err;
5174 TAILQ_FOREACH(pe, &paths, entry)
5175 free((char *)pe->path);
5176 got_pathlist_free(&paths);
5177 got_ref_list_free(&refs);
5178 if (f1 && fclose(f1) == EOF && error == NULL)
5179 error = got_error_from_errno("fclose");
5180 if (f2 && fclose(f2) == EOF && error == NULL)
5181 error = got_error_from_errno("fclose");
5182 return error;
5185 __dead static void
5186 usage_blame(void)
5188 fprintf(stderr,
5189 "usage: %s blame [-c commit] [-r repository-path] path\n",
5190 getprogname());
5191 exit(1);
5194 struct blame_line {
5195 int annotated;
5196 char *id_str;
5197 char *committer;
5198 char datebuf[11]; /* YYYY-MM-DD + NUL */
5201 struct blame_cb_args {
5202 struct blame_line *lines;
5203 int nlines;
5204 int nlines_prec;
5205 int lineno_cur;
5206 off_t *line_offsets;
5207 FILE *f;
5208 struct got_repository *repo;
5211 static const struct got_error *
5212 blame_cb(void *arg, int nlines, int lineno,
5213 struct got_commit_object *commit, struct got_object_id *id)
5215 const struct got_error *err = NULL;
5216 struct blame_cb_args *a = arg;
5217 struct blame_line *bline;
5218 char *line = NULL;
5219 size_t linesize = 0;
5220 off_t offset;
5221 struct tm tm;
5222 time_t committer_time;
5224 if (nlines != a->nlines ||
5225 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5226 return got_error(GOT_ERR_RANGE);
5228 if (sigint_received)
5229 return got_error(GOT_ERR_ITER_COMPLETED);
5231 if (lineno == -1)
5232 return NULL; /* no change in this commit */
5234 /* Annotate this line. */
5235 bline = &a->lines[lineno - 1];
5236 if (bline->annotated)
5237 return NULL;
5238 err = got_object_id_str(&bline->id_str, id);
5239 if (err)
5240 return err;
5242 bline->committer = strdup(got_object_commit_get_committer(commit));
5243 if (bline->committer == NULL) {
5244 err = got_error_from_errno("strdup");
5245 goto done;
5248 committer_time = got_object_commit_get_committer_time(commit);
5249 if (gmtime_r(&committer_time, &tm) == NULL)
5250 return got_error_from_errno("gmtime_r");
5251 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5252 &tm) == 0) {
5253 err = got_error(GOT_ERR_NO_SPACE);
5254 goto done;
5256 bline->annotated = 1;
5258 /* Print lines annotated so far. */
5259 bline = &a->lines[a->lineno_cur - 1];
5260 if (!bline->annotated)
5261 goto done;
5263 offset = a->line_offsets[a->lineno_cur - 1];
5264 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5265 err = got_error_from_errno("fseeko");
5266 goto done;
5269 while (bline->annotated) {
5270 char *smallerthan, *at, *nl, *committer;
5271 size_t len;
5273 if (getline(&line, &linesize, a->f) == -1) {
5274 if (ferror(a->f))
5275 err = got_error_from_errno("getline");
5276 break;
5279 committer = bline->committer;
5280 smallerthan = strchr(committer, '<');
5281 if (smallerthan && smallerthan[1] != '\0')
5282 committer = smallerthan + 1;
5283 at = strchr(committer, '@');
5284 if (at)
5285 *at = '\0';
5286 len = strlen(committer);
5287 if (len >= 9)
5288 committer[8] = '\0';
5290 nl = strchr(line, '\n');
5291 if (nl)
5292 *nl = '\0';
5293 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5294 bline->id_str, bline->datebuf, committer, line);
5296 a->lineno_cur++;
5297 bline = &a->lines[a->lineno_cur - 1];
5299 done:
5300 free(line);
5301 return err;
5304 static const struct got_error *
5305 cmd_blame(int argc, char *argv[])
5307 const struct got_error *error;
5308 struct got_repository *repo = NULL;
5309 struct got_worktree *worktree = NULL;
5310 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5311 char *link_target = NULL;
5312 struct got_object_id *obj_id = NULL;
5313 struct got_object_id *commit_id = NULL;
5314 struct got_commit_object *commit = NULL;
5315 struct got_blob_object *blob = NULL;
5316 char *commit_id_str = NULL;
5317 struct blame_cb_args bca;
5318 int ch, obj_type, i, fd = -1;
5319 off_t filesize;
5320 int *pack_fds = NULL;
5322 fd = got_opentempfd();
5323 if (fd == -1)
5324 return got_error_from_errno("got_opentempfd");
5326 memset(&bca, 0, sizeof(bca));
5328 #ifndef PROFILE
5329 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5330 NULL) == -1)
5331 err(1, "pledge");
5332 #endif
5334 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5335 switch (ch) {
5336 case 'c':
5337 commit_id_str = optarg;
5338 break;
5339 case 'r':
5340 repo_path = realpath(optarg, NULL);
5341 if (repo_path == NULL)
5342 return got_error_from_errno2("realpath",
5343 optarg);
5344 got_path_strip_trailing_slashes(repo_path);
5345 break;
5346 default:
5347 usage_blame();
5348 /* NOTREACHED */
5352 argc -= optind;
5353 argv += optind;
5355 if (argc == 1)
5356 path = argv[0];
5357 else
5358 usage_blame();
5360 cwd = getcwd(NULL, 0);
5361 if (cwd == NULL) {
5362 error = got_error_from_errno("getcwd");
5363 goto done;
5366 error = got_repo_pack_fds_open(&pack_fds);
5367 if (error != NULL)
5368 goto done;
5370 if (repo_path == NULL) {
5371 error = got_worktree_open(&worktree, cwd);
5372 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5373 goto done;
5374 else
5375 error = NULL;
5376 if (worktree) {
5377 repo_path =
5378 strdup(got_worktree_get_repo_path(worktree));
5379 if (repo_path == NULL) {
5380 error = got_error_from_errno("strdup");
5381 if (error)
5382 goto done;
5384 } else {
5385 repo_path = strdup(cwd);
5386 if (repo_path == NULL) {
5387 error = got_error_from_errno("strdup");
5388 goto done;
5393 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5394 if (error != NULL)
5395 goto done;
5397 if (worktree) {
5398 const char *prefix = got_worktree_get_path_prefix(worktree);
5399 char *p;
5401 error = got_worktree_resolve_path(&p, worktree, path);
5402 if (error)
5403 goto done;
5404 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5405 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5406 p) == -1) {
5407 error = got_error_from_errno("asprintf");
5408 free(p);
5409 goto done;
5411 free(p);
5412 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5413 } else {
5414 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5415 if (error)
5416 goto done;
5417 error = got_repo_map_path(&in_repo_path, repo, path);
5419 if (error)
5420 goto done;
5422 if (commit_id_str == NULL) {
5423 struct got_reference *head_ref;
5424 error = got_ref_open(&head_ref, repo, worktree ?
5425 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5426 if (error != NULL)
5427 goto done;
5428 error = got_ref_resolve(&commit_id, repo, head_ref);
5429 got_ref_close(head_ref);
5430 if (error != NULL)
5431 goto done;
5432 } else {
5433 struct got_reflist_head refs;
5434 TAILQ_INIT(&refs);
5435 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5436 NULL);
5437 if (error)
5438 goto done;
5439 error = got_repo_match_object_id(&commit_id, NULL,
5440 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5441 got_ref_list_free(&refs);
5442 if (error)
5443 goto done;
5446 if (worktree) {
5447 /* Release work tree lock. */
5448 got_worktree_close(worktree);
5449 worktree = NULL;
5452 error = got_object_open_as_commit(&commit, repo, commit_id);
5453 if (error)
5454 goto done;
5456 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5457 commit, repo);
5458 if (error)
5459 goto done;
5461 error = got_object_id_by_path(&obj_id, repo, commit,
5462 link_target ? link_target : in_repo_path);
5463 if (error)
5464 goto done;
5466 error = got_object_get_type(&obj_type, repo, obj_id);
5467 if (error)
5468 goto done;
5470 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5471 error = got_error_path(link_target ? link_target : in_repo_path,
5472 GOT_ERR_OBJ_TYPE);
5473 goto done;
5476 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
5477 if (error)
5478 goto done;
5479 bca.f = got_opentemp();
5480 if (bca.f == NULL) {
5481 error = got_error_from_errno("got_opentemp");
5482 goto done;
5484 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5485 &bca.line_offsets, bca.f, blob);
5486 if (error || bca.nlines == 0)
5487 goto done;
5489 /* Don't include \n at EOF in the blame line count. */
5490 if (bca.line_offsets[bca.nlines - 1] == filesize)
5491 bca.nlines--;
5493 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5494 if (bca.lines == NULL) {
5495 error = got_error_from_errno("calloc");
5496 goto done;
5498 bca.lineno_cur = 1;
5499 bca.nlines_prec = 0;
5500 i = bca.nlines;
5501 while (i > 0) {
5502 i /= 10;
5503 bca.nlines_prec++;
5505 bca.repo = repo;
5507 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5508 repo, blame_cb, &bca, check_cancelled, NULL);
5509 done:
5510 free(in_repo_path);
5511 free(link_target);
5512 free(repo_path);
5513 free(cwd);
5514 free(commit_id);
5515 free(obj_id);
5516 if (commit)
5517 got_object_commit_close(commit);
5518 if (fd != -1 && close(fd) == -1 && error == NULL)
5519 error = got_error_from_errno("close");
5520 if (blob)
5521 got_object_blob_close(blob);
5522 if (worktree)
5523 got_worktree_close(worktree);
5524 if (repo) {
5525 const struct got_error *close_err = got_repo_close(repo);
5526 if (error == NULL)
5527 error = close_err;
5529 if (pack_fds) {
5530 const struct got_error *pack_err =
5531 got_repo_pack_fds_close(pack_fds);
5532 if (error == NULL)
5533 error = pack_err;
5535 if (bca.lines) {
5536 for (i = 0; i < bca.nlines; i++) {
5537 struct blame_line *bline = &bca.lines[i];
5538 free(bline->id_str);
5539 free(bline->committer);
5541 free(bca.lines);
5543 free(bca.line_offsets);
5544 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5545 error = got_error_from_errno("fclose");
5546 return error;
5549 __dead static void
5550 usage_tree(void)
5552 fprintf(stderr,
5553 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
5554 getprogname());
5555 exit(1);
5558 static const struct got_error *
5559 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5560 const char *root_path, struct got_repository *repo)
5562 const struct got_error *err = NULL;
5563 int is_root_path = (strcmp(path, root_path) == 0);
5564 const char *modestr = "";
5565 mode_t mode = got_tree_entry_get_mode(te);
5566 char *link_target = NULL;
5568 path += strlen(root_path);
5569 while (path[0] == '/')
5570 path++;
5572 if (got_object_tree_entry_is_submodule(te))
5573 modestr = "$";
5574 else if (S_ISLNK(mode)) {
5575 int i;
5577 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5578 if (err)
5579 return err;
5580 for (i = 0; i < strlen(link_target); i++) {
5581 if (!isprint((unsigned char)link_target[i]))
5582 link_target[i] = '?';
5585 modestr = "@";
5587 else if (S_ISDIR(mode))
5588 modestr = "/";
5589 else if (mode & S_IXUSR)
5590 modestr = "*";
5592 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5593 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5594 link_target ? " -> ": "", link_target ? link_target : "");
5596 free(link_target);
5597 return NULL;
5600 static const struct got_error *
5601 print_tree(const char *path, struct got_commit_object *commit,
5602 int show_ids, int recurse, const char *root_path,
5603 struct got_repository *repo)
5605 const struct got_error *err = NULL;
5606 struct got_object_id *tree_id = NULL;
5607 struct got_tree_object *tree = NULL;
5608 int nentries, i;
5610 err = got_object_id_by_path(&tree_id, repo, commit, path);
5611 if (err)
5612 goto done;
5614 err = got_object_open_as_tree(&tree, repo, tree_id);
5615 if (err)
5616 goto done;
5617 nentries = got_object_tree_get_nentries(tree);
5618 for (i = 0; i < nentries; i++) {
5619 struct got_tree_entry *te;
5620 char *id = NULL;
5622 if (sigint_received || sigpipe_received)
5623 break;
5625 te = got_object_tree_get_entry(tree, i);
5626 if (show_ids) {
5627 char *id_str;
5628 err = got_object_id_str(&id_str,
5629 got_tree_entry_get_id(te));
5630 if (err)
5631 goto done;
5632 if (asprintf(&id, "%s ", id_str) == -1) {
5633 err = got_error_from_errno("asprintf");
5634 free(id_str);
5635 goto done;
5637 free(id_str);
5639 err = print_entry(te, id, path, root_path, repo);
5640 free(id);
5641 if (err)
5642 goto done;
5644 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5645 char *child_path;
5646 if (asprintf(&child_path, "%s%s%s", path,
5647 path[0] == '/' && path[1] == '\0' ? "" : "/",
5648 got_tree_entry_get_name(te)) == -1) {
5649 err = got_error_from_errno("asprintf");
5650 goto done;
5652 err = print_tree(child_path, commit, show_ids, 1,
5653 root_path, repo);
5654 free(child_path);
5655 if (err)
5656 goto done;
5659 done:
5660 if (tree)
5661 got_object_tree_close(tree);
5662 free(tree_id);
5663 return err;
5666 static const struct got_error *
5667 cmd_tree(int argc, char *argv[])
5669 const struct got_error *error;
5670 struct got_repository *repo = NULL;
5671 struct got_worktree *worktree = NULL;
5672 const char *path, *refname = NULL;
5673 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5674 struct got_object_id *commit_id = NULL;
5675 struct got_commit_object *commit = NULL;
5676 char *commit_id_str = NULL;
5677 int show_ids = 0, recurse = 0;
5678 int ch;
5679 int *pack_fds = NULL;
5681 #ifndef PROFILE
5682 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5683 NULL) == -1)
5684 err(1, "pledge");
5685 #endif
5687 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
5688 switch (ch) {
5689 case 'c':
5690 commit_id_str = optarg;
5691 break;
5692 case 'r':
5693 repo_path = realpath(optarg, NULL);
5694 if (repo_path == NULL)
5695 return got_error_from_errno2("realpath",
5696 optarg);
5697 got_path_strip_trailing_slashes(repo_path);
5698 break;
5699 case 'i':
5700 show_ids = 1;
5701 break;
5702 case 'R':
5703 recurse = 1;
5704 break;
5705 default:
5706 usage_tree();
5707 /* NOTREACHED */
5711 argc -= optind;
5712 argv += optind;
5714 if (argc == 1)
5715 path = argv[0];
5716 else if (argc > 1)
5717 usage_tree();
5718 else
5719 path = NULL;
5721 cwd = getcwd(NULL, 0);
5722 if (cwd == NULL) {
5723 error = got_error_from_errno("getcwd");
5724 goto done;
5727 error = got_repo_pack_fds_open(&pack_fds);
5728 if (error != NULL)
5729 goto done;
5731 if (repo_path == NULL) {
5732 error = got_worktree_open(&worktree, cwd);
5733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5734 goto done;
5735 else
5736 error = NULL;
5737 if (worktree) {
5738 repo_path =
5739 strdup(got_worktree_get_repo_path(worktree));
5740 if (repo_path == NULL)
5741 error = got_error_from_errno("strdup");
5742 if (error)
5743 goto done;
5744 } else {
5745 repo_path = strdup(cwd);
5746 if (repo_path == NULL) {
5747 error = got_error_from_errno("strdup");
5748 goto done;
5753 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5754 if (error != NULL)
5755 goto done;
5757 if (worktree) {
5758 const char *prefix = got_worktree_get_path_prefix(worktree);
5759 char *p;
5761 if (path == NULL)
5762 path = "";
5763 error = got_worktree_resolve_path(&p, worktree, path);
5764 if (error)
5765 goto done;
5766 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5767 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5768 p) == -1) {
5769 error = got_error_from_errno("asprintf");
5770 free(p);
5771 goto done;
5773 free(p);
5774 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5775 if (error)
5776 goto done;
5777 } else {
5778 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5779 if (error)
5780 goto done;
5781 if (path == NULL)
5782 path = "/";
5783 error = got_repo_map_path(&in_repo_path, repo, path);
5784 if (error != NULL)
5785 goto done;
5788 if (commit_id_str == NULL) {
5789 struct got_reference *head_ref;
5790 if (worktree)
5791 refname = got_worktree_get_head_ref_name(worktree);
5792 else
5793 refname = GOT_REF_HEAD;
5794 error = got_ref_open(&head_ref, repo, refname, 0);
5795 if (error != NULL)
5796 goto done;
5797 error = got_ref_resolve(&commit_id, repo, head_ref);
5798 got_ref_close(head_ref);
5799 if (error != NULL)
5800 goto done;
5801 } else {
5802 struct got_reflist_head refs;
5803 TAILQ_INIT(&refs);
5804 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5805 NULL);
5806 if (error)
5807 goto done;
5808 error = got_repo_match_object_id(&commit_id, NULL,
5809 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5810 got_ref_list_free(&refs);
5811 if (error)
5812 goto done;
5815 if (worktree) {
5816 /* Release work tree lock. */
5817 got_worktree_close(worktree);
5818 worktree = NULL;
5821 error = got_object_open_as_commit(&commit, repo, commit_id);
5822 if (error)
5823 goto done;
5825 error = print_tree(in_repo_path, commit, show_ids, recurse,
5826 in_repo_path, repo);
5827 done:
5828 free(in_repo_path);
5829 free(repo_path);
5830 free(cwd);
5831 free(commit_id);
5832 if (commit)
5833 got_object_commit_close(commit);
5834 if (worktree)
5835 got_worktree_close(worktree);
5836 if (repo) {
5837 const struct got_error *close_err = got_repo_close(repo);
5838 if (error == NULL)
5839 error = close_err;
5841 if (pack_fds) {
5842 const struct got_error *pack_err =
5843 got_repo_pack_fds_close(pack_fds);
5844 if (error == NULL)
5845 error = pack_err;
5847 return error;
5850 __dead static void
5851 usage_status(void)
5853 fprintf(stderr, "usage: %s status [-I] [-s status-codes ] "
5854 "[-S status-codes] [path ...]\n", getprogname());
5855 exit(1);
5858 struct got_status_arg {
5859 char *status_codes;
5860 int suppress;
5863 static const struct got_error *
5864 print_status(void *arg, unsigned char status, unsigned char staged_status,
5865 const char *path, struct got_object_id *blob_id,
5866 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
5867 int dirfd, const char *de_name)
5869 struct got_status_arg *st = arg;
5871 if (status == staged_status && (status == GOT_STATUS_DELETE))
5872 status = GOT_STATUS_NO_CHANGE;
5873 if (st != NULL && st->status_codes) {
5874 size_t ncodes = strlen(st->status_codes);
5875 int i, j = 0;
5877 for (i = 0; i < ncodes ; i++) {
5878 if (st->suppress) {
5879 if (status == st->status_codes[i] ||
5880 staged_status == st->status_codes[i]) {
5881 j++;
5882 continue;
5884 } else {
5885 if (status == st->status_codes[i] ||
5886 staged_status == st->status_codes[i])
5887 break;
5891 if (st->suppress && j == 0)
5892 goto print;
5894 if (i == ncodes)
5895 return NULL;
5897 print:
5898 printf("%c%c %s\n", status, staged_status, path);
5899 return NULL;
5902 static const struct got_error *
5903 cmd_status(int argc, char *argv[])
5905 const struct got_error *error = NULL;
5906 struct got_repository *repo = NULL;
5907 struct got_worktree *worktree = NULL;
5908 struct got_status_arg st;
5909 char *cwd = NULL;
5910 struct got_pathlist_head paths;
5911 struct got_pathlist_entry *pe;
5912 int ch, i, no_ignores = 0;
5913 int *pack_fds = NULL;
5915 TAILQ_INIT(&paths);
5917 memset(&st, 0, sizeof(st));
5918 st.status_codes = NULL;
5919 st.suppress = 0;
5921 while ((ch = getopt(argc, argv, "Is:S:")) != -1) {
5922 switch (ch) {
5923 case 'I':
5924 no_ignores = 1;
5925 break;
5926 case 'S':
5927 if (st.status_codes != NULL && st.suppress == 0)
5928 option_conflict('S', 's');
5929 st.suppress = 1;
5930 /* fallthrough */
5931 case 's':
5932 for (i = 0; i < strlen(optarg); i++) {
5933 switch (optarg[i]) {
5934 case GOT_STATUS_MODIFY:
5935 case GOT_STATUS_ADD:
5936 case GOT_STATUS_DELETE:
5937 case GOT_STATUS_CONFLICT:
5938 case GOT_STATUS_MISSING:
5939 case GOT_STATUS_OBSTRUCTED:
5940 case GOT_STATUS_UNVERSIONED:
5941 case GOT_STATUS_MODE_CHANGE:
5942 case GOT_STATUS_NONEXISTENT:
5943 break;
5944 default:
5945 errx(1, "invalid status code '%c'",
5946 optarg[i]);
5949 if (ch == 's' && st.suppress)
5950 option_conflict('s', 'S');
5951 st.status_codes = optarg;
5952 break;
5953 default:
5954 usage_status();
5955 /* NOTREACHED */
5959 argc -= optind;
5960 argv += optind;
5962 #ifndef PROFILE
5963 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5964 NULL) == -1)
5965 err(1, "pledge");
5966 #endif
5967 cwd = getcwd(NULL, 0);
5968 if (cwd == NULL) {
5969 error = got_error_from_errno("getcwd");
5970 goto done;
5973 error = got_repo_pack_fds_open(&pack_fds);
5974 if (error != NULL)
5975 goto done;
5977 error = got_worktree_open(&worktree, cwd);
5978 if (error) {
5979 if (error->code == GOT_ERR_NOT_WORKTREE)
5980 error = wrap_not_worktree_error(error, "status", cwd);
5981 goto done;
5984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5985 NULL, pack_fds);
5986 if (error != NULL)
5987 goto done;
5989 error = apply_unveil(got_repo_get_path(repo), 1,
5990 got_worktree_get_root_path(worktree));
5991 if (error)
5992 goto done;
5994 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5995 if (error)
5996 goto done;
5998 error = got_worktree_status(worktree, &paths, repo, no_ignores,
5999 print_status, &st, check_cancelled, NULL);
6000 done:
6001 if (pack_fds) {
6002 const struct got_error *pack_err =
6003 got_repo_pack_fds_close(pack_fds);
6004 if (error == NULL)
6005 error = pack_err;
6008 TAILQ_FOREACH(pe, &paths, entry)
6009 free((char *)pe->path);
6010 got_pathlist_free(&paths);
6011 free(cwd);
6012 return error;
6015 __dead static void
6016 usage_ref(void)
6018 fprintf(stderr,
6019 "usage: %s ref [-r repository] [-l] [-t] [-c object] "
6020 "[-s reference] [-d] [name]\n",
6021 getprogname());
6022 exit(1);
6025 static const struct got_error *
6026 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6028 static const struct got_error *err = NULL;
6029 struct got_reflist_head refs;
6030 struct got_reflist_entry *re;
6032 TAILQ_INIT(&refs);
6033 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6034 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6035 repo);
6036 if (err)
6037 return err;
6039 TAILQ_FOREACH(re, &refs, entry) {
6040 char *refstr;
6041 refstr = got_ref_to_str(re->ref);
6042 if (refstr == NULL) {
6043 err = got_error_from_errno("got_ref_to_str");
6044 break;
6046 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6047 free(refstr);
6050 got_ref_list_free(&refs);
6051 return err;
6054 static const struct got_error *
6055 delete_ref_by_name(struct got_repository *repo, const char *refname)
6057 const struct got_error *err;
6058 struct got_reference *ref;
6060 err = got_ref_open(&ref, repo, refname, 0);
6061 if (err)
6062 return err;
6064 err = delete_ref(repo, ref);
6065 got_ref_close(ref);
6066 return err;
6069 static const struct got_error *
6070 add_ref(struct got_repository *repo, const char *refname, const char *target)
6072 const struct got_error *err = NULL;
6073 struct got_object_id *id = NULL;
6074 struct got_reference *ref = NULL;
6075 struct got_reflist_head refs;
6078 * Don't let the user create a reference name with a leading '-'.
6079 * While technically a valid reference name, this case is usually
6080 * an unintended typo.
6082 if (refname[0] == '-')
6083 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6085 TAILQ_INIT(&refs);
6086 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6087 if (err)
6088 goto done;
6089 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6090 &refs, repo);
6091 got_ref_list_free(&refs);
6092 if (err)
6093 goto done;
6095 err = got_ref_alloc(&ref, refname, id);
6096 if (err)
6097 goto done;
6099 err = got_ref_write(ref, repo);
6100 done:
6101 if (ref)
6102 got_ref_close(ref);
6103 free(id);
6104 return err;
6107 static const struct got_error *
6108 add_symref(struct got_repository *repo, const char *refname, const char *target)
6110 const struct got_error *err = NULL;
6111 struct got_reference *ref = NULL;
6112 struct got_reference *target_ref = NULL;
6115 * Don't let the user create a reference name with a leading '-'.
6116 * While technically a valid reference name, this case is usually
6117 * an unintended typo.
6119 if (refname[0] == '-')
6120 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6122 err = got_ref_open(&target_ref, repo, target, 0);
6123 if (err)
6124 return err;
6126 err = got_ref_alloc_symref(&ref, refname, target_ref);
6127 if (err)
6128 goto done;
6130 err = got_ref_write(ref, repo);
6131 done:
6132 if (target_ref)
6133 got_ref_close(target_ref);
6134 if (ref)
6135 got_ref_close(ref);
6136 return err;
6139 static const struct got_error *
6140 cmd_ref(int argc, char *argv[])
6142 const struct got_error *error = NULL;
6143 struct got_repository *repo = NULL;
6144 struct got_worktree *worktree = NULL;
6145 char *cwd = NULL, *repo_path = NULL;
6146 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6147 const char *obj_arg = NULL, *symref_target= NULL;
6148 char *refname = NULL;
6149 int *pack_fds = NULL;
6151 while ((ch = getopt(argc, argv, "c:dr:ls:t")) != -1) {
6152 switch (ch) {
6153 case 'c':
6154 obj_arg = optarg;
6155 break;
6156 case 'd':
6157 do_delete = 1;
6158 break;
6159 case 'r':
6160 repo_path = realpath(optarg, NULL);
6161 if (repo_path == NULL)
6162 return got_error_from_errno2("realpath",
6163 optarg);
6164 got_path_strip_trailing_slashes(repo_path);
6165 break;
6166 case 'l':
6167 do_list = 1;
6168 break;
6169 case 's':
6170 symref_target = optarg;
6171 break;
6172 case 't':
6173 sort_by_time = 1;
6174 break;
6175 default:
6176 usage_ref();
6177 /* NOTREACHED */
6181 if (obj_arg && do_list)
6182 option_conflict('c', 'l');
6183 if (obj_arg && do_delete)
6184 option_conflict('c', 'd');
6185 if (obj_arg && symref_target)
6186 option_conflict('c', 's');
6187 if (symref_target && do_delete)
6188 option_conflict('s', 'd');
6189 if (symref_target && do_list)
6190 option_conflict('s', 'l');
6191 if (do_delete && do_list)
6192 option_conflict('d', 'l');
6193 if (sort_by_time && !do_list)
6194 errx(1, "-t option requires -l option");
6196 argc -= optind;
6197 argv += optind;
6199 if (do_list) {
6200 if (argc != 0 && argc != 1)
6201 usage_ref();
6202 if (argc == 1) {
6203 refname = strdup(argv[0]);
6204 if (refname == NULL) {
6205 error = got_error_from_errno("strdup");
6206 goto done;
6209 } else {
6210 if (argc != 1)
6211 usage_ref();
6212 refname = strdup(argv[0]);
6213 if (refname == NULL) {
6214 error = got_error_from_errno("strdup");
6215 goto done;
6219 if (refname)
6220 got_path_strip_trailing_slashes(refname);
6222 #ifndef PROFILE
6223 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6224 "sendfd unveil", NULL) == -1)
6225 err(1, "pledge");
6226 #endif
6227 cwd = getcwd(NULL, 0);
6228 if (cwd == NULL) {
6229 error = got_error_from_errno("getcwd");
6230 goto done;
6233 error = got_repo_pack_fds_open(&pack_fds);
6234 if (error != NULL)
6235 goto done;
6237 if (repo_path == NULL) {
6238 error = got_worktree_open(&worktree, cwd);
6239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6240 goto done;
6241 else
6242 error = NULL;
6243 if (worktree) {
6244 repo_path =
6245 strdup(got_worktree_get_repo_path(worktree));
6246 if (repo_path == NULL)
6247 error = got_error_from_errno("strdup");
6248 if (error)
6249 goto done;
6250 } else {
6251 repo_path = strdup(cwd);
6252 if (repo_path == NULL) {
6253 error = got_error_from_errno("strdup");
6254 goto done;
6259 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6260 if (error != NULL)
6261 goto done;
6263 #ifndef PROFILE
6264 if (do_list) {
6265 /* Remove "cpath" promise. */
6266 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6267 NULL) == -1)
6268 err(1, "pledge");
6270 #endif
6272 error = apply_unveil(got_repo_get_path(repo), do_list,
6273 worktree ? got_worktree_get_root_path(worktree) : NULL);
6274 if (error)
6275 goto done;
6277 if (do_list)
6278 error = list_refs(repo, refname, sort_by_time);
6279 else if (do_delete)
6280 error = delete_ref_by_name(repo, refname);
6281 else if (symref_target)
6282 error = add_symref(repo, refname, symref_target);
6283 else {
6284 if (obj_arg == NULL)
6285 usage_ref();
6286 error = add_ref(repo, refname, obj_arg);
6288 done:
6289 free(refname);
6290 if (repo) {
6291 const struct got_error *close_err = got_repo_close(repo);
6292 if (error == NULL)
6293 error = close_err;
6295 if (worktree)
6296 got_worktree_close(worktree);
6297 if (pack_fds) {
6298 const struct got_error *pack_err =
6299 got_repo_pack_fds_close(pack_fds);
6300 if (error == NULL)
6301 error = pack_err;
6303 free(cwd);
6304 free(repo_path);
6305 return error;
6308 __dead static void
6309 usage_branch(void)
6311 fprintf(stderr,
6312 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-t] "
6313 "[-n] [name]\n", getprogname());
6314 exit(1);
6317 static const struct got_error *
6318 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6319 struct got_reference *ref)
6321 const struct got_error *err = NULL;
6322 const char *refname, *marker = " ";
6323 char *refstr;
6325 refname = got_ref_get_name(ref);
6326 if (worktree && strcmp(refname,
6327 got_worktree_get_head_ref_name(worktree)) == 0) {
6328 struct got_object_id *id = NULL;
6330 err = got_ref_resolve(&id, repo, ref);
6331 if (err)
6332 return err;
6333 if (got_object_id_cmp(id,
6334 got_worktree_get_base_commit_id(worktree)) == 0)
6335 marker = "* ";
6336 else
6337 marker = "~ ";
6338 free(id);
6341 if (strncmp(refname, "refs/heads/", 11) == 0)
6342 refname += 11;
6343 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6344 refname += 18;
6345 if (strncmp(refname, "refs/remotes/", 13) == 0)
6346 refname += 13;
6348 refstr = got_ref_to_str(ref);
6349 if (refstr == NULL)
6350 return got_error_from_errno("got_ref_to_str");
6352 printf("%s%s: %s\n", marker, refname, refstr);
6353 free(refstr);
6354 return NULL;
6357 static const struct got_error *
6358 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6360 const char *refname;
6362 if (worktree == NULL)
6363 return got_error(GOT_ERR_NOT_WORKTREE);
6365 refname = got_worktree_get_head_ref_name(worktree);
6367 if (strncmp(refname, "refs/heads/", 11) == 0)
6368 refname += 11;
6369 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6370 refname += 18;
6372 printf("%s\n", refname);
6374 return NULL;
6377 static const struct got_error *
6378 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6379 int sort_by_time)
6381 static const struct got_error *err = NULL;
6382 struct got_reflist_head refs;
6383 struct got_reflist_entry *re;
6384 struct got_reference *temp_ref = NULL;
6385 int rebase_in_progress, histedit_in_progress;
6387 TAILQ_INIT(&refs);
6389 if (worktree) {
6390 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6391 worktree);
6392 if (err)
6393 return err;
6395 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6396 worktree);
6397 if (err)
6398 return err;
6400 if (rebase_in_progress || histedit_in_progress) {
6401 err = got_ref_open(&temp_ref, repo,
6402 got_worktree_get_head_ref_name(worktree), 0);
6403 if (err)
6404 return err;
6405 list_branch(repo, worktree, temp_ref);
6406 got_ref_close(temp_ref);
6410 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6411 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6412 repo);
6413 if (err)
6414 return err;
6416 TAILQ_FOREACH(re, &refs, entry)
6417 list_branch(repo, worktree, re->ref);
6419 got_ref_list_free(&refs);
6421 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6422 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6423 repo);
6424 if (err)
6425 return err;
6427 TAILQ_FOREACH(re, &refs, entry)
6428 list_branch(repo, worktree, re->ref);
6430 got_ref_list_free(&refs);
6432 return NULL;
6435 static const struct got_error *
6436 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6437 const char *branch_name)
6439 const struct got_error *err = NULL;
6440 struct got_reference *ref = NULL;
6441 char *refname, *remote_refname = NULL;
6443 if (strncmp(branch_name, "refs/", 5) == 0)
6444 branch_name += 5;
6445 if (strncmp(branch_name, "heads/", 6) == 0)
6446 branch_name += 6;
6447 else if (strncmp(branch_name, "remotes/", 8) == 0)
6448 branch_name += 8;
6450 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6451 return got_error_from_errno("asprintf");
6453 if (asprintf(&remote_refname, "refs/remotes/%s",
6454 branch_name) == -1) {
6455 err = got_error_from_errno("asprintf");
6456 goto done;
6459 err = got_ref_open(&ref, repo, refname, 0);
6460 if (err) {
6461 const struct got_error *err2;
6462 if (err->code != GOT_ERR_NOT_REF)
6463 goto done;
6465 * Keep 'err' intact such that if neither branch exists
6466 * we report "refs/heads" rather than "refs/remotes" in
6467 * our error message.
6469 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6470 if (err2)
6471 goto done;
6472 err = NULL;
6475 if (worktree &&
6476 strcmp(got_worktree_get_head_ref_name(worktree),
6477 got_ref_get_name(ref)) == 0) {
6478 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6479 "will not delete this work tree's current branch");
6480 goto done;
6483 err = delete_ref(repo, ref);
6484 done:
6485 if (ref)
6486 got_ref_close(ref);
6487 free(refname);
6488 free(remote_refname);
6489 return err;
6492 static const struct got_error *
6493 add_branch(struct got_repository *repo, const char *branch_name,
6494 struct got_object_id *base_commit_id)
6496 const struct got_error *err = NULL;
6497 struct got_reference *ref = NULL;
6498 char *base_refname = NULL, *refname = NULL;
6501 * Don't let the user create a branch name with a leading '-'.
6502 * While technically a valid reference name, this case is usually
6503 * an unintended typo.
6505 if (branch_name[0] == '-')
6506 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6508 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6509 branch_name += 11;
6511 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6512 err = got_error_from_errno("asprintf");
6513 goto done;
6516 err = got_ref_open(&ref, repo, refname, 0);
6517 if (err == NULL) {
6518 err = got_error(GOT_ERR_BRANCH_EXISTS);
6519 goto done;
6520 } else if (err->code != GOT_ERR_NOT_REF)
6521 goto done;
6523 err = got_ref_alloc(&ref, refname, base_commit_id);
6524 if (err)
6525 goto done;
6527 err = got_ref_write(ref, repo);
6528 done:
6529 if (ref)
6530 got_ref_close(ref);
6531 free(base_refname);
6532 free(refname);
6533 return err;
6536 static const struct got_error *
6537 cmd_branch(int argc, char *argv[])
6539 const struct got_error *error = NULL;
6540 struct got_repository *repo = NULL;
6541 struct got_worktree *worktree = NULL;
6542 char *cwd = NULL, *repo_path = NULL;
6543 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6544 const char *delref = NULL, *commit_id_arg = NULL;
6545 struct got_reference *ref = NULL;
6546 struct got_pathlist_head paths;
6547 struct got_pathlist_entry *pe;
6548 struct got_object_id *commit_id = NULL;
6549 char *commit_id_str = NULL;
6550 int *pack_fds = NULL;
6552 TAILQ_INIT(&paths);
6554 while ((ch = getopt(argc, argv, "c:d:r:lnt")) != -1) {
6555 switch (ch) {
6556 case 'c':
6557 commit_id_arg = optarg;
6558 break;
6559 case 'd':
6560 delref = optarg;
6561 break;
6562 case 'r':
6563 repo_path = realpath(optarg, NULL);
6564 if (repo_path == NULL)
6565 return got_error_from_errno2("realpath",
6566 optarg);
6567 got_path_strip_trailing_slashes(repo_path);
6568 break;
6569 case 'l':
6570 do_list = 1;
6571 break;
6572 case 'n':
6573 do_update = 0;
6574 break;
6575 case 't':
6576 sort_by_time = 1;
6577 break;
6578 default:
6579 usage_branch();
6580 /* NOTREACHED */
6584 if (do_list && delref)
6585 option_conflict('l', 'd');
6586 if (sort_by_time && !do_list)
6587 errx(1, "-t option requires -l option");
6589 argc -= optind;
6590 argv += optind;
6592 if (!do_list && !delref && argc == 0)
6593 do_show = 1;
6595 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6596 errx(1, "-c option can only be used when creating a branch");
6598 if (do_list || delref) {
6599 if (argc > 0)
6600 usage_branch();
6601 } else if (!do_show && argc != 1)
6602 usage_branch();
6604 #ifndef PROFILE
6605 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6606 "sendfd unveil", NULL) == -1)
6607 err(1, "pledge");
6608 #endif
6609 cwd = getcwd(NULL, 0);
6610 if (cwd == NULL) {
6611 error = got_error_from_errno("getcwd");
6612 goto done;
6615 error = got_repo_pack_fds_open(&pack_fds);
6616 if (error != NULL)
6617 goto done;
6619 if (repo_path == NULL) {
6620 error = got_worktree_open(&worktree, cwd);
6621 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6622 goto done;
6623 else
6624 error = NULL;
6625 if (worktree) {
6626 repo_path =
6627 strdup(got_worktree_get_repo_path(worktree));
6628 if (repo_path == NULL)
6629 error = got_error_from_errno("strdup");
6630 if (error)
6631 goto done;
6632 } else {
6633 repo_path = strdup(cwd);
6634 if (repo_path == NULL) {
6635 error = got_error_from_errno("strdup");
6636 goto done;
6641 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6642 if (error != NULL)
6643 goto done;
6645 #ifndef PROFILE
6646 if (do_list || do_show) {
6647 /* Remove "cpath" promise. */
6648 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6649 NULL) == -1)
6650 err(1, "pledge");
6652 #endif
6654 error = apply_unveil(got_repo_get_path(repo), do_list,
6655 worktree ? got_worktree_get_root_path(worktree) : NULL);
6656 if (error)
6657 goto done;
6659 if (do_show)
6660 error = show_current_branch(repo, worktree);
6661 else if (do_list)
6662 error = list_branches(repo, worktree, sort_by_time);
6663 else if (delref)
6664 error = delete_branch(repo, worktree, delref);
6665 else {
6666 struct got_reflist_head refs;
6667 TAILQ_INIT(&refs);
6668 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6669 NULL);
6670 if (error)
6671 goto done;
6672 if (commit_id_arg == NULL)
6673 commit_id_arg = worktree ?
6674 got_worktree_get_head_ref_name(worktree) :
6675 GOT_REF_HEAD;
6676 error = got_repo_match_object_id(&commit_id, NULL,
6677 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6678 got_ref_list_free(&refs);
6679 if (error)
6680 goto done;
6681 error = add_branch(repo, argv[0], commit_id);
6682 if (error)
6683 goto done;
6684 if (worktree && do_update) {
6685 struct got_update_progress_arg upa;
6686 char *branch_refname = NULL;
6688 error = got_object_id_str(&commit_id_str, commit_id);
6689 if (error)
6690 goto done;
6691 error = get_worktree_paths_from_argv(&paths, 0, NULL,
6692 worktree);
6693 if (error)
6694 goto done;
6695 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
6696 == -1) {
6697 error = got_error_from_errno("asprintf");
6698 goto done;
6700 error = got_ref_open(&ref, repo, branch_refname, 0);
6701 free(branch_refname);
6702 if (error)
6703 goto done;
6704 error = switch_head_ref(ref, commit_id, worktree,
6705 repo);
6706 if (error)
6707 goto done;
6708 error = got_worktree_set_base_commit_id(worktree, repo,
6709 commit_id);
6710 if (error)
6711 goto done;
6712 memset(&upa, 0, sizeof(upa));
6713 error = got_worktree_checkout_files(worktree, &paths,
6714 repo, update_progress, &upa, check_cancelled,
6715 NULL);
6716 if (error)
6717 goto done;
6718 if (upa.did_something) {
6719 printf("Updated to %s: %s\n",
6720 got_worktree_get_head_ref_name(worktree),
6721 commit_id_str);
6723 print_update_progress_stats(&upa);
6726 done:
6727 if (ref)
6728 got_ref_close(ref);
6729 if (repo) {
6730 const struct got_error *close_err = got_repo_close(repo);
6731 if (error == NULL)
6732 error = close_err;
6734 if (worktree)
6735 got_worktree_close(worktree);
6736 if (pack_fds) {
6737 const struct got_error *pack_err =
6738 got_repo_pack_fds_close(pack_fds);
6739 if (error == NULL)
6740 error = pack_err;
6742 free(cwd);
6743 free(repo_path);
6744 free(commit_id);
6745 free(commit_id_str);
6746 TAILQ_FOREACH(pe, &paths, entry)
6747 free((char *)pe->path);
6748 got_pathlist_free(&paths);
6749 return error;
6753 __dead static void
6754 usage_tag(void)
6756 fprintf(stderr,
6757 "usage: %s tag [-c commit] [-r repository] [-l] "
6758 "[-m message] name\n", getprogname());
6759 exit(1);
6762 #if 0
6763 static const struct got_error *
6764 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
6766 const struct got_error *err = NULL;
6767 struct got_reflist_entry *re, *se, *new;
6768 struct got_object_id *re_id, *se_id;
6769 struct got_tag_object *re_tag, *se_tag;
6770 time_t re_time, se_time;
6772 STAILQ_FOREACH(re, tags, entry) {
6773 se = STAILQ_FIRST(sorted);
6774 if (se == NULL) {
6775 err = got_reflist_entry_dup(&new, re);
6776 if (err)
6777 return err;
6778 STAILQ_INSERT_HEAD(sorted, new, entry);
6779 continue;
6780 } else {
6781 err = got_ref_resolve(&re_id, repo, re->ref);
6782 if (err)
6783 break;
6784 err = got_object_open_as_tag(&re_tag, repo, re_id);
6785 free(re_id);
6786 if (err)
6787 break;
6788 re_time = got_object_tag_get_tagger_time(re_tag);
6789 got_object_tag_close(re_tag);
6792 while (se) {
6793 err = got_ref_resolve(&se_id, repo, re->ref);
6794 if (err)
6795 break;
6796 err = got_object_open_as_tag(&se_tag, repo, se_id);
6797 free(se_id);
6798 if (err)
6799 break;
6800 se_time = got_object_tag_get_tagger_time(se_tag);
6801 got_object_tag_close(se_tag);
6803 if (se_time > re_time) {
6804 err = got_reflist_entry_dup(&new, re);
6805 if (err)
6806 return err;
6807 STAILQ_INSERT_AFTER(sorted, se, new, entry);
6808 break;
6810 se = STAILQ_NEXT(se, entry);
6811 continue;
6814 done:
6815 return err;
6817 #endif
6819 static const struct got_error *
6820 list_tags(struct got_repository *repo)
6822 static const struct got_error *err = NULL;
6823 struct got_reflist_head refs;
6824 struct got_reflist_entry *re;
6826 TAILQ_INIT(&refs);
6828 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
6829 if (err)
6830 return err;
6832 TAILQ_FOREACH(re, &refs, entry) {
6833 const char *refname;
6834 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
6835 char datebuf[26];
6836 const char *tagger;
6837 time_t tagger_time;
6838 struct got_object_id *id;
6839 struct got_tag_object *tag;
6840 struct got_commit_object *commit = NULL;
6842 refname = got_ref_get_name(re->ref);
6843 if (strncmp(refname, "refs/tags/", 10) != 0)
6844 continue;
6845 refname += 10;
6846 refstr = got_ref_to_str(re->ref);
6847 if (refstr == NULL) {
6848 err = got_error_from_errno("got_ref_to_str");
6849 break;
6851 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
6852 free(refstr);
6854 err = got_ref_resolve(&id, repo, re->ref);
6855 if (err)
6856 break;
6857 err = got_object_open_as_tag(&tag, repo, id);
6858 if (err) {
6859 if (err->code != GOT_ERR_OBJ_TYPE) {
6860 free(id);
6861 break;
6863 /* "lightweight" tag */
6864 err = got_object_open_as_commit(&commit, repo, id);
6865 if (err) {
6866 free(id);
6867 break;
6869 tagger = got_object_commit_get_committer(commit);
6870 tagger_time =
6871 got_object_commit_get_committer_time(commit);
6872 err = got_object_id_str(&id_str, id);
6873 free(id);
6874 if (err)
6875 break;
6876 } else {
6877 free(id);
6878 tagger = got_object_tag_get_tagger(tag);
6879 tagger_time = got_object_tag_get_tagger_time(tag);
6880 err = got_object_id_str(&id_str,
6881 got_object_tag_get_object_id(tag));
6882 if (err)
6883 break;
6885 printf("from: %s\n", tagger);
6886 datestr = get_datestr(&tagger_time, datebuf);
6887 if (datestr)
6888 printf("date: %s UTC\n", datestr);
6889 if (commit)
6890 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
6891 else {
6892 switch (got_object_tag_get_object_type(tag)) {
6893 case GOT_OBJ_TYPE_BLOB:
6894 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
6895 id_str);
6896 break;
6897 case GOT_OBJ_TYPE_TREE:
6898 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
6899 id_str);
6900 break;
6901 case GOT_OBJ_TYPE_COMMIT:
6902 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
6903 id_str);
6904 break;
6905 case GOT_OBJ_TYPE_TAG:
6906 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
6907 id_str);
6908 break;
6909 default:
6910 break;
6913 free(id_str);
6914 if (commit) {
6915 err = got_object_commit_get_logmsg(&tagmsg0, commit);
6916 if (err)
6917 break;
6918 got_object_commit_close(commit);
6919 } else {
6920 tagmsg0 = strdup(got_object_tag_get_message(tag));
6921 got_object_tag_close(tag);
6922 if (tagmsg0 == NULL) {
6923 err = got_error_from_errno("strdup");
6924 break;
6928 tagmsg = tagmsg0;
6929 do {
6930 line = strsep(&tagmsg, "\n");
6931 if (line)
6932 printf(" %s\n", line);
6933 } while (line);
6934 free(tagmsg0);
6937 got_ref_list_free(&refs);
6938 return NULL;
6941 static const struct got_error *
6942 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
6943 const char *tag_name, const char *repo_path)
6945 const struct got_error *err = NULL;
6946 char *template = NULL, *initial_content = NULL;
6947 char *editor = NULL;
6948 int initial_content_len;
6949 int fd = -1;
6951 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
6952 err = got_error_from_errno("asprintf");
6953 goto done;
6956 initial_content_len = asprintf(&initial_content,
6957 "\n# tagging commit %s as %s\n",
6958 commit_id_str, tag_name);
6959 if (initial_content_len == -1) {
6960 err = got_error_from_errno("asprintf");
6961 goto done;
6964 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
6965 if (err)
6966 goto done;
6968 if (write(fd, initial_content, initial_content_len) == -1) {
6969 err = got_error_from_errno2("write", *tagmsg_path);
6970 goto done;
6973 err = get_editor(&editor);
6974 if (err)
6975 goto done;
6976 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
6977 initial_content_len, 1);
6978 done:
6979 free(initial_content);
6980 free(template);
6981 free(editor);
6983 if (fd != -1 && close(fd) == -1 && err == NULL)
6984 err = got_error_from_errno2("close", *tagmsg_path);
6986 /* Editor is done; we can now apply unveil(2) */
6987 if (err == NULL)
6988 err = apply_unveil(repo_path, 0, NULL);
6989 if (err) {
6990 free(*tagmsg);
6991 *tagmsg = NULL;
6993 return err;
6996 static const struct got_error *
6997 add_tag(struct got_repository *repo, const char *tagger,
6998 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
7000 const struct got_error *err = NULL;
7001 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7002 char *label = NULL, *commit_id_str = NULL;
7003 struct got_reference *ref = NULL;
7004 char *refname = NULL, *tagmsg = NULL;
7005 char *tagmsg_path = NULL, *tag_id_str = NULL;
7006 int preserve_tagmsg = 0;
7007 struct got_reflist_head refs;
7009 TAILQ_INIT(&refs);
7012 * Don't let the user create a tag name with a leading '-'.
7013 * While technically a valid reference name, this case is usually
7014 * an unintended typo.
7016 if (tag_name[0] == '-')
7017 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7019 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7020 if (err)
7021 goto done;
7023 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7024 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7025 if (err)
7026 goto done;
7028 err = got_object_id_str(&commit_id_str, commit_id);
7029 if (err)
7030 goto done;
7032 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7033 refname = strdup(tag_name);
7034 if (refname == NULL) {
7035 err = got_error_from_errno("strdup");
7036 goto done;
7038 tag_name += 10;
7039 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
7040 err = got_error_from_errno("asprintf");
7041 goto done;
7044 err = got_ref_open(&ref, repo, refname, 0);
7045 if (err == NULL) {
7046 err = got_error(GOT_ERR_TAG_EXISTS);
7047 goto done;
7048 } else if (err->code != GOT_ERR_NOT_REF)
7049 goto done;
7051 if (tagmsg_arg == NULL) {
7052 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7053 tag_name, got_repo_get_path(repo));
7054 if (err) {
7055 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7056 tagmsg_path != NULL)
7057 preserve_tagmsg = 1;
7058 goto done;
7062 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7063 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
7064 if (err) {
7065 if (tagmsg_path)
7066 preserve_tagmsg = 1;
7067 goto done;
7070 err = got_ref_alloc(&ref, refname, tag_id);
7071 if (err) {
7072 if (tagmsg_path)
7073 preserve_tagmsg = 1;
7074 goto done;
7077 err = got_ref_write(ref, repo);
7078 if (err) {
7079 if (tagmsg_path)
7080 preserve_tagmsg = 1;
7081 goto done;
7084 err = got_object_id_str(&tag_id_str, tag_id);
7085 if (err) {
7086 if (tagmsg_path)
7087 preserve_tagmsg = 1;
7088 goto done;
7090 printf("Created tag %s\n", tag_id_str);
7091 done:
7092 if (preserve_tagmsg) {
7093 fprintf(stderr, "%s: tag message preserved in %s\n",
7094 getprogname(), tagmsg_path);
7095 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7096 err = got_error_from_errno2("unlink", tagmsg_path);
7097 free(tag_id_str);
7098 if (ref)
7099 got_ref_close(ref);
7100 free(commit_id);
7101 free(commit_id_str);
7102 free(refname);
7103 free(tagmsg);
7104 free(tagmsg_path);
7105 got_ref_list_free(&refs);
7106 return err;
7109 static const struct got_error *
7110 cmd_tag(int argc, char *argv[])
7112 const struct got_error *error = NULL;
7113 struct got_repository *repo = NULL;
7114 struct got_worktree *worktree = NULL;
7115 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7116 char *gitconfig_path = NULL, *tagger = NULL;
7117 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
7118 int ch, do_list = 0;
7119 int *pack_fds = NULL;
7121 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
7122 switch (ch) {
7123 case 'c':
7124 commit_id_arg = optarg;
7125 break;
7126 case 'm':
7127 tagmsg = optarg;
7128 break;
7129 case 'r':
7130 repo_path = realpath(optarg, NULL);
7131 if (repo_path == NULL)
7132 return got_error_from_errno2("realpath",
7133 optarg);
7134 got_path_strip_trailing_slashes(repo_path);
7135 break;
7136 case 'l':
7137 do_list = 1;
7138 break;
7139 default:
7140 usage_tag();
7141 /* NOTREACHED */
7145 argc -= optind;
7146 argv += optind;
7148 if (do_list) {
7149 if (commit_id_arg != NULL)
7150 errx(1,
7151 "-c option can only be used when creating a tag");
7152 if (tagmsg)
7153 option_conflict('l', 'm');
7154 if (argc > 0)
7155 usage_tag();
7156 } else if (argc != 1)
7157 usage_tag();
7159 tag_name = argv[0];
7161 #ifndef PROFILE
7162 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7163 "sendfd unveil", NULL) == -1)
7164 err(1, "pledge");
7165 #endif
7166 cwd = getcwd(NULL, 0);
7167 if (cwd == NULL) {
7168 error = got_error_from_errno("getcwd");
7169 goto done;
7172 error = got_repo_pack_fds_open(&pack_fds);
7173 if (error != NULL)
7174 goto done;
7176 if (repo_path == NULL) {
7177 error = got_worktree_open(&worktree, cwd);
7178 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7179 goto done;
7180 else
7181 error = NULL;
7182 if (worktree) {
7183 repo_path =
7184 strdup(got_worktree_get_repo_path(worktree));
7185 if (repo_path == NULL)
7186 error = got_error_from_errno("strdup");
7187 if (error)
7188 goto done;
7189 } else {
7190 repo_path = strdup(cwd);
7191 if (repo_path == NULL) {
7192 error = got_error_from_errno("strdup");
7193 goto done;
7198 if (do_list) {
7199 if (worktree) {
7200 /* Release work tree lock. */
7201 got_worktree_close(worktree);
7202 worktree = NULL;
7204 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7205 if (error != NULL)
7206 goto done;
7208 #ifndef PROFILE
7209 /* Remove "cpath" promise. */
7210 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7211 NULL) == -1)
7212 err(1, "pledge");
7213 #endif
7214 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7215 if (error)
7216 goto done;
7217 error = list_tags(repo);
7218 } else {
7219 error = get_gitconfig_path(&gitconfig_path);
7220 if (error)
7221 goto done;
7222 error = got_repo_open(&repo, repo_path, gitconfig_path,
7223 pack_fds);
7224 if (error != NULL)
7225 goto done;
7227 error = get_author(&tagger, repo, worktree);
7228 if (error)
7229 goto done;
7230 if (worktree) {
7231 /* Release work tree lock. */
7232 got_worktree_close(worktree);
7233 worktree = NULL;
7236 if (tagmsg) {
7237 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7238 if (error)
7239 goto done;
7242 if (commit_id_arg == NULL) {
7243 struct got_reference *head_ref;
7244 struct got_object_id *commit_id;
7245 error = got_ref_open(&head_ref, repo,
7246 worktree ? got_worktree_get_head_ref_name(worktree)
7247 : GOT_REF_HEAD, 0);
7248 if (error)
7249 goto done;
7250 error = got_ref_resolve(&commit_id, repo, head_ref);
7251 got_ref_close(head_ref);
7252 if (error)
7253 goto done;
7254 error = got_object_id_str(&commit_id_str, commit_id);
7255 free(commit_id);
7256 if (error)
7257 goto done;
7260 error = add_tag(repo, tagger, tag_name,
7261 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
7263 done:
7264 if (repo) {
7265 const struct got_error *close_err = got_repo_close(repo);
7266 if (error == NULL)
7267 error = close_err;
7269 if (worktree)
7270 got_worktree_close(worktree);
7271 if (pack_fds) {
7272 const struct got_error *pack_err =
7273 got_repo_pack_fds_close(pack_fds);
7274 if (error == NULL)
7275 error = pack_err;
7277 free(cwd);
7278 free(repo_path);
7279 free(gitconfig_path);
7280 free(commit_id_str);
7281 free(tagger);
7282 return error;
7285 __dead static void
7286 usage_add(void)
7288 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
7289 getprogname());
7290 exit(1);
7293 static const struct got_error *
7294 add_progress(void *arg, unsigned char status, const char *path)
7296 while (path[0] == '/')
7297 path++;
7298 printf("%c %s\n", status, path);
7299 return NULL;
7302 static const struct got_error *
7303 cmd_add(int argc, char *argv[])
7305 const struct got_error *error = NULL;
7306 struct got_repository *repo = NULL;
7307 struct got_worktree *worktree = NULL;
7308 char *cwd = NULL;
7309 struct got_pathlist_head paths;
7310 struct got_pathlist_entry *pe;
7311 int ch, can_recurse = 0, no_ignores = 0;
7312 int *pack_fds = NULL;
7314 TAILQ_INIT(&paths);
7316 while ((ch = getopt(argc, argv, "IR")) != -1) {
7317 switch (ch) {
7318 case 'I':
7319 no_ignores = 1;
7320 break;
7321 case 'R':
7322 can_recurse = 1;
7323 break;
7324 default:
7325 usage_add();
7326 /* NOTREACHED */
7330 argc -= optind;
7331 argv += optind;
7333 #ifndef PROFILE
7334 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7335 NULL) == -1)
7336 err(1, "pledge");
7337 #endif
7338 if (argc < 1)
7339 usage_add();
7341 cwd = getcwd(NULL, 0);
7342 if (cwd == NULL) {
7343 error = got_error_from_errno("getcwd");
7344 goto done;
7347 error = got_repo_pack_fds_open(&pack_fds);
7348 if (error != NULL)
7349 goto done;
7351 error = got_worktree_open(&worktree, cwd);
7352 if (error) {
7353 if (error->code == GOT_ERR_NOT_WORKTREE)
7354 error = wrap_not_worktree_error(error, "add", cwd);
7355 goto done;
7358 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7359 NULL, pack_fds);
7360 if (error != NULL)
7361 goto done;
7363 error = apply_unveil(got_repo_get_path(repo), 1,
7364 got_worktree_get_root_path(worktree));
7365 if (error)
7366 goto done;
7368 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7369 if (error)
7370 goto done;
7372 if (!can_recurse) {
7373 char *ondisk_path;
7374 struct stat sb;
7375 TAILQ_FOREACH(pe, &paths, entry) {
7376 if (asprintf(&ondisk_path, "%s/%s",
7377 got_worktree_get_root_path(worktree),
7378 pe->path) == -1) {
7379 error = got_error_from_errno("asprintf");
7380 goto done;
7382 if (lstat(ondisk_path, &sb) == -1) {
7383 if (errno == ENOENT) {
7384 free(ondisk_path);
7385 continue;
7387 error = got_error_from_errno2("lstat",
7388 ondisk_path);
7389 free(ondisk_path);
7390 goto done;
7392 free(ondisk_path);
7393 if (S_ISDIR(sb.st_mode)) {
7394 error = got_error_msg(GOT_ERR_BAD_PATH,
7395 "adding directories requires -R option");
7396 goto done;
7401 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7402 NULL, repo, no_ignores);
7403 done:
7404 if (repo) {
7405 const struct got_error *close_err = got_repo_close(repo);
7406 if (error == NULL)
7407 error = close_err;
7409 if (worktree)
7410 got_worktree_close(worktree);
7411 if (pack_fds) {
7412 const struct got_error *pack_err =
7413 got_repo_pack_fds_close(pack_fds);
7414 if (error == NULL)
7415 error = pack_err;
7417 TAILQ_FOREACH(pe, &paths, entry)
7418 free((char *)pe->path);
7419 got_pathlist_free(&paths);
7420 free(cwd);
7421 return error;
7424 __dead static void
7425 usage_remove(void)
7427 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
7428 "path ...\n", getprogname());
7429 exit(1);
7432 static const struct got_error *
7433 print_remove_status(void *arg, unsigned char status,
7434 unsigned char staged_status, const char *path)
7436 while (path[0] == '/')
7437 path++;
7438 if (status == GOT_STATUS_NONEXISTENT)
7439 return NULL;
7440 if (status == staged_status && (status == GOT_STATUS_DELETE))
7441 status = GOT_STATUS_NO_CHANGE;
7442 printf("%c%c %s\n", status, staged_status, path);
7443 return NULL;
7446 static const struct got_error *
7447 cmd_remove(int argc, char *argv[])
7449 const struct got_error *error = NULL;
7450 struct got_worktree *worktree = NULL;
7451 struct got_repository *repo = NULL;
7452 const char *status_codes = NULL;
7453 char *cwd = NULL;
7454 struct got_pathlist_head paths;
7455 struct got_pathlist_entry *pe;
7456 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7457 int ignore_missing_paths = 0;
7458 int *pack_fds = NULL;
7460 TAILQ_INIT(&paths);
7462 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7463 switch (ch) {
7464 case 'f':
7465 delete_local_mods = 1;
7466 ignore_missing_paths = 1;
7467 break;
7468 case 'k':
7469 keep_on_disk = 1;
7470 break;
7471 case 'R':
7472 can_recurse = 1;
7473 break;
7474 case 's':
7475 for (i = 0; i < strlen(optarg); i++) {
7476 switch (optarg[i]) {
7477 case GOT_STATUS_MODIFY:
7478 delete_local_mods = 1;
7479 break;
7480 case GOT_STATUS_MISSING:
7481 ignore_missing_paths = 1;
7482 break;
7483 default:
7484 errx(1, "invalid status code '%c'",
7485 optarg[i]);
7488 status_codes = optarg;
7489 break;
7490 default:
7491 usage_remove();
7492 /* NOTREACHED */
7496 argc -= optind;
7497 argv += optind;
7499 #ifndef PROFILE
7500 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7501 NULL) == -1)
7502 err(1, "pledge");
7503 #endif
7504 if (argc < 1)
7505 usage_remove();
7507 cwd = getcwd(NULL, 0);
7508 if (cwd == NULL) {
7509 error = got_error_from_errno("getcwd");
7510 goto done;
7513 error = got_repo_pack_fds_open(&pack_fds);
7514 if (error != NULL)
7515 goto done;
7517 error = got_worktree_open(&worktree, cwd);
7518 if (error) {
7519 if (error->code == GOT_ERR_NOT_WORKTREE)
7520 error = wrap_not_worktree_error(error, "remove", cwd);
7521 goto done;
7524 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7525 NULL, pack_fds);
7526 if (error)
7527 goto done;
7529 error = apply_unveil(got_repo_get_path(repo), 1,
7530 got_worktree_get_root_path(worktree));
7531 if (error)
7532 goto done;
7534 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7535 if (error)
7536 goto done;
7538 if (!can_recurse) {
7539 char *ondisk_path;
7540 struct stat sb;
7541 TAILQ_FOREACH(pe, &paths, entry) {
7542 if (asprintf(&ondisk_path, "%s/%s",
7543 got_worktree_get_root_path(worktree),
7544 pe->path) == -1) {
7545 error = got_error_from_errno("asprintf");
7546 goto done;
7548 if (lstat(ondisk_path, &sb) == -1) {
7549 if (errno == ENOENT) {
7550 free(ondisk_path);
7551 continue;
7553 error = got_error_from_errno2("lstat",
7554 ondisk_path);
7555 free(ondisk_path);
7556 goto done;
7558 free(ondisk_path);
7559 if (S_ISDIR(sb.st_mode)) {
7560 error = got_error_msg(GOT_ERR_BAD_PATH,
7561 "removing directories requires -R option");
7562 goto done;
7567 error = got_worktree_schedule_delete(worktree, &paths,
7568 delete_local_mods, status_codes, print_remove_status, NULL,
7569 repo, keep_on_disk, ignore_missing_paths);
7570 done:
7571 if (repo) {
7572 const struct got_error *close_err = got_repo_close(repo);
7573 if (error == NULL)
7574 error = close_err;
7576 if (worktree)
7577 got_worktree_close(worktree);
7578 if (pack_fds) {
7579 const struct got_error *pack_err =
7580 got_repo_pack_fds_close(pack_fds);
7581 if (error == NULL)
7582 error = pack_err;
7584 TAILQ_FOREACH(pe, &paths, entry)
7585 free((char *)pe->path);
7586 got_pathlist_free(&paths);
7587 free(cwd);
7588 return error;
7591 __dead static void
7592 usage_patch(void)
7594 fprintf(stderr, "usage: %s patch [-n] [-p strip-count] "
7595 "[-R] [patchfile]\n", getprogname());
7596 exit(1);
7599 static const struct got_error *
7600 patch_from_stdin(int *patchfd)
7602 const struct got_error *err = NULL;
7603 ssize_t r;
7604 char *path, buf[BUFSIZ];
7605 sig_t sighup, sigint, sigquit;
7607 err = got_opentemp_named_fd(&path, patchfd,
7608 GOT_TMPDIR_STR "/got-patch");
7609 if (err)
7610 return err;
7611 unlink(path);
7612 free(path);
7614 sighup = signal(SIGHUP, SIG_DFL);
7615 sigint = signal(SIGINT, SIG_DFL);
7616 sigquit = signal(SIGQUIT, SIG_DFL);
7618 for (;;) {
7619 r = read(0, buf, sizeof(buf));
7620 if (r == -1) {
7621 err = got_error_from_errno("read");
7622 break;
7624 if (r == 0)
7625 break;
7626 if (write(*patchfd, buf, r) == -1) {
7627 err = got_error_from_errno("write");
7628 break;
7632 signal(SIGHUP, sighup);
7633 signal(SIGINT, sigint);
7634 signal(SIGQUIT, sigquit);
7636 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
7637 err = got_error_from_errno("lseek");
7639 if (err != NULL) {
7640 close(*patchfd);
7641 *patchfd = -1;
7644 return err;
7647 static const struct got_error *
7648 patch_progress(void *arg, const char *old, const char *new,
7649 unsigned char status, const struct got_error *error, long old_from,
7650 long old_lines, long new_from, long new_lines, long offset,
7651 const struct got_error *hunk_err)
7653 const char *path = new == NULL ? old : new;
7655 while (*path == '/')
7656 path++;
7658 if (status != 0)
7659 printf("%c %s\n", status, path);
7661 if (error != NULL)
7662 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
7664 if (offset != 0 || hunk_err != NULL) {
7665 printf("@@ -%ld,%ld +%ld,%ld @@ ", old_from,
7666 old_lines, new_from, new_lines);
7667 if (hunk_err != NULL)
7668 printf("%s\n", hunk_err->msg);
7669 else
7670 printf("applied with offset %ld\n", offset);
7673 return NULL;
7676 static const struct got_error *
7677 cmd_patch(int argc, char *argv[])
7679 const struct got_error *error = NULL, *close_error = NULL;
7680 struct got_worktree *worktree = NULL;
7681 struct got_repository *repo = NULL;
7682 const char *errstr;
7683 char *cwd = NULL;
7684 int ch, nop = 0, strip = -1, reverse = 0;
7685 int patchfd;
7686 int *pack_fds = NULL;
7688 while ((ch = getopt(argc, argv, "np:R")) != -1) {
7689 switch (ch) {
7690 case 'n':
7691 nop = 1;
7692 break;
7693 case 'p':
7694 strip = strtonum(optarg, 0, INT_MAX, &errstr);
7695 if (errstr != NULL)
7696 errx(1, "pathname strip count is %s: %s",
7697 errstr, optarg);
7698 break;
7699 case 'R':
7700 reverse = 1;
7701 break;
7702 default:
7703 usage_patch();
7704 /* NOTREACHED */
7708 argc -= optind;
7709 argv += optind;
7711 if (argc == 0) {
7712 error = patch_from_stdin(&patchfd);
7713 if (error)
7714 return error;
7715 } else if (argc == 1) {
7716 patchfd = open(argv[0], O_RDONLY);
7717 if (patchfd == -1) {
7718 error = got_error_from_errno2("open", argv[0]);
7719 return error;
7721 } else
7722 usage_patch();
7724 if ((cwd = getcwd(NULL, 0)) == NULL) {
7725 error = got_error_from_errno("getcwd");
7726 goto done;
7729 error = got_repo_pack_fds_open(&pack_fds);
7730 if (error != NULL)
7731 goto done;
7733 error = got_worktree_open(&worktree, cwd);
7734 if (error != NULL)
7735 goto done;
7737 const char *repo_path = got_worktree_get_repo_path(worktree);
7738 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7739 if (error != NULL)
7740 goto done;
7742 error = apply_unveil(got_repo_get_path(repo), 0,
7743 got_worktree_get_root_path(worktree));
7744 if (error != NULL)
7745 goto done;
7747 #ifndef PROFILE
7748 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock",
7749 NULL) == -1)
7750 err(1, "pledge");
7751 #endif
7753 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
7754 &patch_progress, NULL, check_cancelled, NULL);
7756 done:
7757 if (repo) {
7758 close_error = got_repo_close(repo);
7759 if (error == NULL)
7760 error = close_error;
7762 if (worktree != NULL) {
7763 close_error = got_worktree_close(worktree);
7764 if (error == NULL)
7765 error = close_error;
7767 if (pack_fds) {
7768 const struct got_error *pack_err =
7769 got_repo_pack_fds_close(pack_fds);
7770 if (error == NULL)
7771 error = pack_err;
7773 free(cwd);
7774 return error;
7777 __dead static void
7778 usage_revert(void)
7780 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
7781 "path ...\n", getprogname());
7782 exit(1);
7785 static const struct got_error *
7786 revert_progress(void *arg, unsigned char status, const char *path)
7788 if (status == GOT_STATUS_UNVERSIONED)
7789 return NULL;
7791 while (path[0] == '/')
7792 path++;
7793 printf("%c %s\n", status, path);
7794 return NULL;
7797 struct choose_patch_arg {
7798 FILE *patch_script_file;
7799 const char *action;
7802 static const struct got_error *
7803 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
7804 int nchanges, const char *action)
7806 const struct got_error *err;
7807 char *line = NULL;
7808 size_t linesize = 0;
7809 ssize_t linelen;
7811 switch (status) {
7812 case GOT_STATUS_ADD:
7813 printf("A %s\n%s this addition? [y/n] ", path, action);
7814 break;
7815 case GOT_STATUS_DELETE:
7816 printf("D %s\n%s this deletion? [y/n] ", path, action);
7817 break;
7818 case GOT_STATUS_MODIFY:
7819 if (fseek(patch_file, 0L, SEEK_SET) == -1)
7820 return got_error_from_errno("fseek");
7821 printf(GOT_COMMIT_SEP_STR);
7822 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
7823 printf("%s", line);
7824 if (linelen == -1 && ferror(patch_file)) {
7825 err = got_error_from_errno("getline");
7826 free(line);
7827 return err;
7829 free(line);
7830 printf(GOT_COMMIT_SEP_STR);
7831 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
7832 path, n, nchanges, action);
7833 break;
7834 default:
7835 return got_error_path(path, GOT_ERR_FILE_STATUS);
7838 return NULL;
7841 static const struct got_error *
7842 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
7843 FILE *patch_file, int n, int nchanges)
7845 const struct got_error *err = NULL;
7846 char *line = NULL;
7847 size_t linesize = 0;
7848 ssize_t linelen;
7849 int resp = ' ';
7850 struct choose_patch_arg *a = arg;
7852 *choice = GOT_PATCH_CHOICE_NONE;
7854 if (a->patch_script_file) {
7855 char *nl;
7856 err = show_change(status, path, patch_file, n, nchanges,
7857 a->action);
7858 if (err)
7859 return err;
7860 linelen = getline(&line, &linesize, a->patch_script_file);
7861 if (linelen == -1) {
7862 if (ferror(a->patch_script_file))
7863 return got_error_from_errno("getline");
7864 return NULL;
7866 nl = strchr(line, '\n');
7867 if (nl)
7868 *nl = '\0';
7869 if (strcmp(line, "y") == 0) {
7870 *choice = GOT_PATCH_CHOICE_YES;
7871 printf("y\n");
7872 } else if (strcmp(line, "n") == 0) {
7873 *choice = GOT_PATCH_CHOICE_NO;
7874 printf("n\n");
7875 } else if (strcmp(line, "q") == 0 &&
7876 status == GOT_STATUS_MODIFY) {
7877 *choice = GOT_PATCH_CHOICE_QUIT;
7878 printf("q\n");
7879 } else
7880 printf("invalid response '%s'\n", line);
7881 free(line);
7882 return NULL;
7885 while (resp != 'y' && resp != 'n' && resp != 'q') {
7886 err = show_change(status, path, patch_file, n, nchanges,
7887 a->action);
7888 if (err)
7889 return err;
7890 resp = getchar();
7891 if (resp == '\n')
7892 resp = getchar();
7893 if (status == GOT_STATUS_MODIFY) {
7894 if (resp != 'y' && resp != 'n' && resp != 'q') {
7895 printf("invalid response '%c'\n", resp);
7896 resp = ' ';
7898 } else if (resp != 'y' && resp != 'n') {
7899 printf("invalid response '%c'\n", resp);
7900 resp = ' ';
7904 if (resp == 'y')
7905 *choice = GOT_PATCH_CHOICE_YES;
7906 else if (resp == 'n')
7907 *choice = GOT_PATCH_CHOICE_NO;
7908 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
7909 *choice = GOT_PATCH_CHOICE_QUIT;
7911 return NULL;
7914 static const struct got_error *
7915 cmd_revert(int argc, char *argv[])
7917 const struct got_error *error = NULL;
7918 struct got_worktree *worktree = NULL;
7919 struct got_repository *repo = NULL;
7920 char *cwd = NULL, *path = NULL;
7921 struct got_pathlist_head paths;
7922 struct got_pathlist_entry *pe;
7923 int ch, can_recurse = 0, pflag = 0;
7924 FILE *patch_script_file = NULL;
7925 const char *patch_script_path = NULL;
7926 struct choose_patch_arg cpa;
7927 int *pack_fds = NULL;
7929 TAILQ_INIT(&paths);
7931 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
7932 switch (ch) {
7933 case 'p':
7934 pflag = 1;
7935 break;
7936 case 'F':
7937 patch_script_path = optarg;
7938 break;
7939 case 'R':
7940 can_recurse = 1;
7941 break;
7942 default:
7943 usage_revert();
7944 /* NOTREACHED */
7948 argc -= optind;
7949 argv += optind;
7951 #ifndef PROFILE
7952 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7953 "unveil", NULL) == -1)
7954 err(1, "pledge");
7955 #endif
7956 if (argc < 1)
7957 usage_revert();
7958 if (patch_script_path && !pflag)
7959 errx(1, "-F option can only be used together with -p option");
7961 cwd = getcwd(NULL, 0);
7962 if (cwd == NULL) {
7963 error = got_error_from_errno("getcwd");
7964 goto done;
7967 error = got_repo_pack_fds_open(&pack_fds);
7968 if (error != NULL)
7969 goto done;
7971 error = got_worktree_open(&worktree, cwd);
7972 if (error) {
7973 if (error->code == GOT_ERR_NOT_WORKTREE)
7974 error = wrap_not_worktree_error(error, "revert", cwd);
7975 goto done;
7978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7979 NULL, pack_fds);
7980 if (error != NULL)
7981 goto done;
7983 if (patch_script_path) {
7984 patch_script_file = fopen(patch_script_path, "re");
7985 if (patch_script_file == NULL) {
7986 error = got_error_from_errno2("fopen",
7987 patch_script_path);
7988 goto done;
7991 error = apply_unveil(got_repo_get_path(repo), 1,
7992 got_worktree_get_root_path(worktree));
7993 if (error)
7994 goto done;
7996 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7997 if (error)
7998 goto done;
8000 if (!can_recurse) {
8001 char *ondisk_path;
8002 struct stat sb;
8003 TAILQ_FOREACH(pe, &paths, entry) {
8004 if (asprintf(&ondisk_path, "%s/%s",
8005 got_worktree_get_root_path(worktree),
8006 pe->path) == -1) {
8007 error = got_error_from_errno("asprintf");
8008 goto done;
8010 if (lstat(ondisk_path, &sb) == -1) {
8011 if (errno == ENOENT) {
8012 free(ondisk_path);
8013 continue;
8015 error = got_error_from_errno2("lstat",
8016 ondisk_path);
8017 free(ondisk_path);
8018 goto done;
8020 free(ondisk_path);
8021 if (S_ISDIR(sb.st_mode)) {
8022 error = got_error_msg(GOT_ERR_BAD_PATH,
8023 "reverting directories requires -R option");
8024 goto done;
8029 cpa.patch_script_file = patch_script_file;
8030 cpa.action = "revert";
8031 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8032 pflag ? choose_patch : NULL, &cpa, repo);
8033 done:
8034 if (patch_script_file && fclose(patch_script_file) == EOF &&
8035 error == NULL)
8036 error = got_error_from_errno2("fclose", patch_script_path);
8037 if (repo) {
8038 const struct got_error *close_err = got_repo_close(repo);
8039 if (error == NULL)
8040 error = close_err;
8042 if (worktree)
8043 got_worktree_close(worktree);
8044 if (pack_fds) {
8045 const struct got_error *pack_err =
8046 got_repo_pack_fds_close(pack_fds);
8047 if (error == NULL)
8048 error = pack_err;
8050 free(path);
8051 free(cwd);
8052 return error;
8055 __dead static void
8056 usage_commit(void)
8058 fprintf(stderr, "usage: %s commit [-F path] [-m msg] [-N] [-S] "
8059 "[path ...]\n", getprogname());
8060 exit(1);
8063 struct collect_commit_logmsg_arg {
8064 const char *cmdline_log;
8065 const char *prepared_log;
8066 int non_interactive;
8067 const char *editor;
8068 const char *worktree_path;
8069 const char *branch_name;
8070 const char *repo_path;
8071 char *logmsg_path;
8075 static const struct got_error *
8076 read_prepared_logmsg(char **logmsg, const char *path)
8078 const struct got_error *err = NULL;
8079 FILE *f = NULL;
8080 struct stat sb;
8081 size_t r;
8083 *logmsg = NULL;
8084 memset(&sb, 0, sizeof(sb));
8086 f = fopen(path, "re");
8087 if (f == NULL)
8088 return got_error_from_errno2("fopen", path);
8090 if (fstat(fileno(f), &sb) == -1) {
8091 err = got_error_from_errno2("fstat", path);
8092 goto done;
8094 if (sb.st_size == 0) {
8095 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8096 goto done;
8099 *logmsg = malloc(sb.st_size + 1);
8100 if (*logmsg == NULL) {
8101 err = got_error_from_errno("malloc");
8102 goto done;
8105 r = fread(*logmsg, 1, sb.st_size, f);
8106 if (r != sb.st_size) {
8107 if (ferror(f))
8108 err = got_error_from_errno2("fread", path);
8109 else
8110 err = got_error(GOT_ERR_IO);
8111 goto done;
8113 (*logmsg)[sb.st_size] = '\0';
8114 done:
8115 if (fclose(f) == EOF && err == NULL)
8116 err = got_error_from_errno2("fclose", path);
8117 if (err) {
8118 free(*logmsg);
8119 *logmsg = NULL;
8121 return err;
8125 static const struct got_error *
8126 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
8127 void *arg)
8129 char *initial_content = NULL;
8130 struct got_pathlist_entry *pe;
8131 const struct got_error *err = NULL;
8132 char *template = NULL;
8133 struct collect_commit_logmsg_arg *a = arg;
8134 int initial_content_len;
8135 int fd = -1;
8136 size_t len;
8138 /* if a message was specified on the command line, just use it */
8139 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8140 len = strlen(a->cmdline_log) + 1;
8141 *logmsg = malloc(len + 1);
8142 if (*logmsg == NULL)
8143 return got_error_from_errno("malloc");
8144 strlcpy(*logmsg, a->cmdline_log, len);
8145 return NULL;
8146 } else if (a->prepared_log != NULL && a->non_interactive)
8147 return read_prepared_logmsg(logmsg, a->prepared_log);
8149 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8150 return got_error_from_errno("asprintf");
8152 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
8153 if (err)
8154 goto done;
8156 if (a->prepared_log) {
8157 char *msg;
8158 err = read_prepared_logmsg(&msg, a->prepared_log);
8159 if (err)
8160 goto done;
8161 if (write(fd, msg, strlen(msg)) == -1) {
8162 err = got_error_from_errno2("write", a->logmsg_path);
8163 free(msg);
8164 goto done;
8166 free(msg);
8169 initial_content_len = asprintf(&initial_content,
8170 "\n# changes to be committed on branch %s:\n",
8171 a->branch_name);
8172 if (initial_content_len == -1) {
8173 err = got_error_from_errno("asprintf");
8174 goto done;
8177 if (write(fd, initial_content, initial_content_len) == -1) {
8178 err = got_error_from_errno2("write", a->logmsg_path);
8179 goto done;
8182 TAILQ_FOREACH(pe, commitable_paths, entry) {
8183 struct got_commitable *ct = pe->data;
8184 dprintf(fd, "# %c %s\n",
8185 got_commitable_get_status(ct),
8186 got_commitable_get_path(ct));
8189 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8190 initial_content_len, a->prepared_log ? 0 : 1);
8191 done:
8192 free(initial_content);
8193 free(template);
8195 if (fd != -1 && close(fd) == -1 && err == NULL)
8196 err = got_error_from_errno2("close", a->logmsg_path);
8198 /* Editor is done; we can now apply unveil(2) */
8199 if (err == NULL)
8200 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8201 if (err) {
8202 free(*logmsg);
8203 *logmsg = NULL;
8205 return err;
8208 static const struct got_error *
8209 cmd_commit(int argc, char *argv[])
8211 const struct got_error *error = NULL;
8212 struct got_worktree *worktree = NULL;
8213 struct got_repository *repo = NULL;
8214 char *cwd = NULL, *id_str = NULL;
8215 struct got_object_id *id = NULL;
8216 const char *logmsg = NULL;
8217 char *prepared_logmsg = NULL;
8218 struct collect_commit_logmsg_arg cl_arg;
8219 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
8220 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
8221 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
8222 struct got_pathlist_head paths;
8223 int *pack_fds = NULL;
8225 TAILQ_INIT(&paths);
8226 cl_arg.logmsg_path = NULL;
8228 while ((ch = getopt(argc, argv, "F:m:NS")) != -1) {
8229 switch (ch) {
8230 case 'F':
8231 if (logmsg != NULL)
8232 option_conflict('F', 'm');
8233 prepared_logmsg = realpath(optarg, NULL);
8234 if (prepared_logmsg == NULL)
8235 return got_error_from_errno2("realpath",
8236 optarg);
8237 break;
8238 case 'm':
8239 if (prepared_logmsg)
8240 option_conflict('m', 'F');
8241 logmsg = optarg;
8242 break;
8243 case 'N':
8244 non_interactive = 1;
8245 break;
8246 case 'S':
8247 allow_bad_symlinks = 1;
8248 break;
8249 default:
8250 usage_commit();
8251 /* NOTREACHED */
8255 argc -= optind;
8256 argv += optind;
8258 #ifndef PROFILE
8259 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8260 "unveil", NULL) == -1)
8261 err(1, "pledge");
8262 #endif
8263 cwd = getcwd(NULL, 0);
8264 if (cwd == NULL) {
8265 error = got_error_from_errno("getcwd");
8266 goto done;
8269 error = got_repo_pack_fds_open(&pack_fds);
8270 if (error != NULL)
8271 goto done;
8273 error = got_worktree_open(&worktree, cwd);
8274 if (error) {
8275 if (error->code == GOT_ERR_NOT_WORKTREE)
8276 error = wrap_not_worktree_error(error, "commit", cwd);
8277 goto done;
8280 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8281 if (error)
8282 goto done;
8283 if (rebase_in_progress) {
8284 error = got_error(GOT_ERR_REBASING);
8285 goto done;
8288 error = got_worktree_histedit_in_progress(&histedit_in_progress,
8289 worktree);
8290 if (error)
8291 goto done;
8293 error = get_gitconfig_path(&gitconfig_path);
8294 if (error)
8295 goto done;
8296 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8297 gitconfig_path, pack_fds);
8298 if (error != NULL)
8299 goto done;
8301 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
8302 if (error)
8303 goto done;
8304 if (merge_in_progress) {
8305 error = got_error(GOT_ERR_MERGE_BUSY);
8306 goto done;
8309 error = get_author(&author, repo, worktree);
8310 if (error)
8311 return error;
8314 * unveil(2) traverses exec(2); if an editor is used we have
8315 * to apply unveil after the log message has been written.
8317 if (logmsg == NULL || strlen(logmsg) == 0)
8318 error = get_editor(&editor);
8319 else
8320 error = apply_unveil(got_repo_get_path(repo), 0,
8321 got_worktree_get_root_path(worktree));
8322 if (error)
8323 goto done;
8325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8326 if (error)
8327 goto done;
8329 cl_arg.editor = editor;
8330 cl_arg.cmdline_log = logmsg;
8331 cl_arg.prepared_log = prepared_logmsg;
8332 cl_arg.non_interactive = non_interactive;
8333 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
8334 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
8335 if (!histedit_in_progress) {
8336 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
8337 error = got_error(GOT_ERR_COMMIT_BRANCH);
8338 goto done;
8340 cl_arg.branch_name += 11;
8342 cl_arg.repo_path = got_repo_get_path(repo);
8343 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
8344 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
8345 print_status, NULL, repo);
8346 if (error) {
8347 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
8348 cl_arg.logmsg_path != NULL)
8349 preserve_logmsg = 1;
8350 goto done;
8353 error = got_object_id_str(&id_str, id);
8354 if (error)
8355 goto done;
8356 printf("Created commit %s\n", id_str);
8357 done:
8358 if (preserve_logmsg) {
8359 fprintf(stderr, "%s: log message preserved in %s\n",
8360 getprogname(), cl_arg.logmsg_path);
8361 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
8362 error == NULL)
8363 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
8364 free(cl_arg.logmsg_path);
8365 if (repo) {
8366 const struct got_error *close_err = got_repo_close(repo);
8367 if (error == NULL)
8368 error = close_err;
8370 if (worktree)
8371 got_worktree_close(worktree);
8372 if (pack_fds) {
8373 const struct got_error *pack_err =
8374 got_repo_pack_fds_close(pack_fds);
8375 if (error == NULL)
8376 error = pack_err;
8378 free(cwd);
8379 free(id_str);
8380 free(gitconfig_path);
8381 free(editor);
8382 free(author);
8383 free(prepared_logmsg);
8384 return error;
8387 __dead static void
8388 usage_send(void)
8390 fprintf(stderr, "usage: %s send [-a] [-b branch] [-d branch] [-f] "
8391 "[-r repository-path] [-t tag] [-T] [-q] [-v] "
8392 "[remote-repository]\n", getprogname());
8393 exit(1);
8396 static void
8397 print_load_info(int print_colored, int print_found, int print_trees,
8398 int ncolored, int nfound, int ntrees)
8400 if (print_colored) {
8401 printf("%d commit%s colored", ncolored,
8402 ncolored == 1 ? "" : "s");
8404 if (print_found) {
8405 printf("%s%d object%s found",
8406 ncolored > 0 ? "; " : "",
8407 nfound, nfound == 1 ? "" : "s");
8409 if (print_trees) {
8410 printf("; %d tree%s scanned", ntrees,
8411 ntrees == 1 ? "" : "s");
8415 struct got_send_progress_arg {
8416 char last_scaled_packsize[FMT_SCALED_STRSIZE];
8417 int verbosity;
8418 int last_ncolored;
8419 int last_nfound;
8420 int last_ntrees;
8421 int loading_done;
8422 int last_ncommits;
8423 int last_nobj_total;
8424 int last_p_deltify;
8425 int last_p_written;
8426 int last_p_sent;
8427 int printed_something;
8428 int sent_something;
8429 struct got_pathlist_head *delete_branches;
8432 static const struct got_error *
8433 send_progress(void *arg, int ncolored, int nfound, int ntrees,
8434 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
8435 int nobj_written, off_t bytes_sent, const char *refname, int success)
8437 struct got_send_progress_arg *a = arg;
8438 char scaled_packsize[FMT_SCALED_STRSIZE];
8439 char scaled_sent[FMT_SCALED_STRSIZE];
8440 int p_deltify = 0, p_written = 0, p_sent = 0;
8441 int print_colored = 0, print_found = 0, print_trees = 0;
8442 int print_searching = 0, print_total = 0;
8443 int print_deltify = 0, print_written = 0, print_sent = 0;
8445 if (a->verbosity < 0)
8446 return NULL;
8448 if (refname) {
8449 const char *status = success ? "accepted" : "rejected";
8451 if (success) {
8452 struct got_pathlist_entry *pe;
8453 TAILQ_FOREACH(pe, a->delete_branches, entry) {
8454 const char *branchname = pe->path;
8455 if (got_path_cmp(branchname, refname,
8456 strlen(branchname), strlen(refname)) == 0) {
8457 status = "deleted";
8458 a->sent_something = 1;
8459 break;
8464 if (a->printed_something)
8465 putchar('\n');
8466 printf("Server has %s %s", status, refname);
8467 a->printed_something = 1;
8468 return NULL;
8471 if (a->last_ncolored != ncolored) {
8472 print_colored = 1;
8473 a->last_ncolored = ncolored;
8476 if (a->last_nfound != nfound) {
8477 print_colored = 1;
8478 print_found = 1;
8479 a->last_nfound = nfound;
8482 if (a->last_ntrees != ntrees) {
8483 print_colored = 1;
8484 print_found = 1;
8485 print_trees = 1;
8486 a->last_ntrees = ntrees;
8489 if ((print_colored || print_found || print_trees) &&
8490 !a->loading_done) {
8491 printf("\r");
8492 print_load_info(print_colored, print_found, print_trees,
8493 ncolored, nfound, ntrees);
8494 a->printed_something = 1;
8495 fflush(stdout);
8496 return NULL;
8497 } else if (!a->loading_done) {
8498 printf("\r");
8499 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
8500 printf("\n");
8501 a->loading_done = 1;
8504 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
8505 return got_error_from_errno("fmt_scaled");
8506 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
8507 return got_error_from_errno("fmt_scaled");
8509 if (a->last_ncommits != ncommits) {
8510 print_searching = 1;
8511 a->last_ncommits = ncommits;
8514 if (a->last_nobj_total != nobj_total) {
8515 print_searching = 1;
8516 print_total = 1;
8517 a->last_nobj_total = nobj_total;
8520 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
8521 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
8522 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
8523 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
8524 return got_error(GOT_ERR_NO_SPACE);
8527 if (nobj_deltify > 0 || nobj_written > 0) {
8528 if (nobj_deltify > 0) {
8529 p_deltify = (nobj_deltify * 100) / nobj_total;
8530 if (p_deltify != a->last_p_deltify) {
8531 a->last_p_deltify = p_deltify;
8532 print_searching = 1;
8533 print_total = 1;
8534 print_deltify = 1;
8537 if (nobj_written > 0) {
8538 p_written = (nobj_written * 100) / nobj_total;
8539 if (p_written != a->last_p_written) {
8540 a->last_p_written = p_written;
8541 print_searching = 1;
8542 print_total = 1;
8543 print_deltify = 1;
8544 print_written = 1;
8549 if (bytes_sent > 0) {
8550 p_sent = (bytes_sent * 100) / packfile_size;
8551 if (p_sent != a->last_p_sent) {
8552 a->last_p_sent = p_sent;
8553 print_searching = 1;
8554 print_total = 1;
8555 print_deltify = 1;
8556 print_written = 1;
8557 print_sent = 1;
8559 a->sent_something = 1;
8562 if (print_searching || print_total || print_deltify || print_written ||
8563 print_sent)
8564 printf("\r");
8565 if (print_searching)
8566 printf("packing %d reference%s", ncommits,
8567 ncommits == 1 ? "" : "s");
8568 if (print_total)
8569 printf("; %d object%s", nobj_total,
8570 nobj_total == 1 ? "" : "s");
8571 if (print_deltify)
8572 printf("; deltify: %d%%", p_deltify);
8573 if (print_sent)
8574 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8575 scaled_packsize, p_sent);
8576 else if (print_written)
8577 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
8578 scaled_packsize, p_written);
8579 if (print_searching || print_total || print_deltify ||
8580 print_written || print_sent) {
8581 a->printed_something = 1;
8582 fflush(stdout);
8584 return NULL;
8587 static const struct got_error *
8588 cmd_send(int argc, char *argv[])
8590 const struct got_error *error = NULL;
8591 char *cwd = NULL, *repo_path = NULL;
8592 const char *remote_name;
8593 char *proto = NULL, *host = NULL, *port = NULL;
8594 char *repo_name = NULL, *server_path = NULL;
8595 const struct got_remote_repo *remotes, *remote = NULL;
8596 int nremotes, nbranches = 0, ntags = 0, ndelete_branches = 0;
8597 struct got_repository *repo = NULL;
8598 struct got_worktree *worktree = NULL;
8599 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
8600 struct got_pathlist_head branches;
8601 struct got_pathlist_head tags;
8602 struct got_reflist_head all_branches;
8603 struct got_reflist_head all_tags;
8604 struct got_pathlist_head delete_args;
8605 struct got_pathlist_head delete_branches;
8606 struct got_reflist_entry *re;
8607 struct got_pathlist_entry *pe;
8608 int i, ch, sendfd = -1, sendstatus;
8609 pid_t sendpid = -1;
8610 struct got_send_progress_arg spa;
8611 int verbosity = 0, overwrite_refs = 0;
8612 int send_all_branches = 0, send_all_tags = 0;
8613 struct got_reference *ref = NULL;
8614 int *pack_fds = NULL;
8616 TAILQ_INIT(&branches);
8617 TAILQ_INIT(&tags);
8618 TAILQ_INIT(&all_branches);
8619 TAILQ_INIT(&all_tags);
8620 TAILQ_INIT(&delete_args);
8621 TAILQ_INIT(&delete_branches);
8623 while ((ch = getopt(argc, argv, "ab:d:fr:t:Tvq")) != -1) {
8624 switch (ch) {
8625 case 'a':
8626 send_all_branches = 1;
8627 break;
8628 case 'b':
8629 error = got_pathlist_append(&branches, optarg, NULL);
8630 if (error)
8631 return error;
8632 nbranches++;
8633 break;
8634 case 'd':
8635 error = got_pathlist_append(&delete_args, optarg, NULL);
8636 if (error)
8637 return error;
8638 break;
8639 case 'f':
8640 overwrite_refs = 1;
8641 break;
8642 case 'r':
8643 repo_path = realpath(optarg, NULL);
8644 if (repo_path == NULL)
8645 return got_error_from_errno2("realpath",
8646 optarg);
8647 got_path_strip_trailing_slashes(repo_path);
8648 break;
8649 case 't':
8650 error = got_pathlist_append(&tags, optarg, NULL);
8651 if (error)
8652 return error;
8653 ntags++;
8654 break;
8655 case 'T':
8656 send_all_tags = 1;
8657 break;
8658 case 'v':
8659 if (verbosity < 0)
8660 verbosity = 0;
8661 else if (verbosity < 3)
8662 verbosity++;
8663 break;
8664 case 'q':
8665 verbosity = -1;
8666 break;
8667 default:
8668 usage_send();
8669 /* NOTREACHED */
8672 argc -= optind;
8673 argv += optind;
8675 if (send_all_branches && !TAILQ_EMPTY(&branches))
8676 option_conflict('a', 'b');
8677 if (send_all_tags && !TAILQ_EMPTY(&tags))
8678 option_conflict('T', 't');
8681 if (argc == 0)
8682 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
8683 else if (argc == 1)
8684 remote_name = argv[0];
8685 else
8686 usage_send();
8688 cwd = getcwd(NULL, 0);
8689 if (cwd == NULL) {
8690 error = got_error_from_errno("getcwd");
8691 goto done;
8694 error = got_repo_pack_fds_open(&pack_fds);
8695 if (error != NULL)
8696 goto done;
8698 if (repo_path == NULL) {
8699 error = got_worktree_open(&worktree, cwd);
8700 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8701 goto done;
8702 else
8703 error = NULL;
8704 if (worktree) {
8705 repo_path =
8706 strdup(got_worktree_get_repo_path(worktree));
8707 if (repo_path == NULL)
8708 error = got_error_from_errno("strdup");
8709 if (error)
8710 goto done;
8711 } else {
8712 repo_path = strdup(cwd);
8713 if (repo_path == NULL) {
8714 error = got_error_from_errno("strdup");
8715 goto done;
8720 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8721 if (error)
8722 goto done;
8724 if (worktree) {
8725 worktree_conf = got_worktree_get_gotconfig(worktree);
8726 if (worktree_conf) {
8727 got_gotconfig_get_remotes(&nremotes, &remotes,
8728 worktree_conf);
8729 for (i = 0; i < nremotes; i++) {
8730 if (strcmp(remotes[i].name, remote_name) == 0) {
8731 remote = &remotes[i];
8732 break;
8737 if (remote == NULL) {
8738 repo_conf = got_repo_get_gotconfig(repo);
8739 if (repo_conf) {
8740 got_gotconfig_get_remotes(&nremotes, &remotes,
8741 repo_conf);
8742 for (i = 0; i < nremotes; i++) {
8743 if (strcmp(remotes[i].name, remote_name) == 0) {
8744 remote = &remotes[i];
8745 break;
8750 if (remote == NULL) {
8751 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
8752 for (i = 0; i < nremotes; i++) {
8753 if (strcmp(remotes[i].name, remote_name) == 0) {
8754 remote = &remotes[i];
8755 break;
8759 if (remote == NULL) {
8760 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
8761 goto done;
8764 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
8765 &repo_name, remote->send_url);
8766 if (error)
8767 goto done;
8769 if (strcmp(proto, "git") == 0) {
8770 #ifndef PROFILE
8771 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8772 "sendfd dns inet unveil", NULL) == -1)
8773 err(1, "pledge");
8774 #endif
8775 } else if (strcmp(proto, "git+ssh") == 0 ||
8776 strcmp(proto, "ssh") == 0) {
8777 #ifndef PROFILE
8778 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
8779 "sendfd unveil", NULL) == -1)
8780 err(1, "pledge");
8781 #endif
8782 } else if (strcmp(proto, "http") == 0 ||
8783 strcmp(proto, "git+http") == 0) {
8784 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
8785 goto done;
8786 } else {
8787 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
8788 goto done;
8791 error = got_dial_apply_unveil(proto);
8792 if (error)
8793 goto done;
8795 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
8796 if (error)
8797 goto done;
8799 if (send_all_branches) {
8800 error = got_ref_list(&all_branches, repo, "refs/heads",
8801 got_ref_cmp_by_name, NULL);
8802 if (error)
8803 goto done;
8804 TAILQ_FOREACH(re, &all_branches, entry) {
8805 const char *branchname = got_ref_get_name(re->ref);
8806 error = got_pathlist_append(&branches,
8807 branchname, NULL);
8808 if (error)
8809 goto done;
8810 nbranches++;
8812 } else if (nbranches == 0) {
8813 for (i = 0; i < remote->nsend_branches; i++) {
8814 got_pathlist_append(&branches,
8815 remote->send_branches[i], NULL);
8819 if (send_all_tags) {
8820 error = got_ref_list(&all_tags, repo, "refs/tags",
8821 got_ref_cmp_by_name, NULL);
8822 if (error)
8823 goto done;
8824 TAILQ_FOREACH(re, &all_tags, entry) {
8825 const char *tagname = got_ref_get_name(re->ref);
8826 error = got_pathlist_append(&tags,
8827 tagname, NULL);
8828 if (error)
8829 goto done;
8830 ntags++;
8835 * To prevent accidents only branches in refs/heads/ can be deleted
8836 * with 'got send -d'.
8837 * Deleting anything else requires local repository access or Git.
8839 TAILQ_FOREACH(pe, &delete_args, entry) {
8840 const char *branchname = pe->path;
8841 char *s;
8842 struct got_pathlist_entry *new;
8843 if (strncmp(branchname, "refs/heads/", 11) == 0) {
8844 s = strdup(branchname);
8845 if (s == NULL) {
8846 error = got_error_from_errno("strdup");
8847 goto done;
8849 } else {
8850 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
8851 error = got_error_from_errno("asprintf");
8852 goto done;
8855 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
8856 if (error || new == NULL /* duplicate */)
8857 free(s);
8858 if (error)
8859 goto done;
8860 ndelete_branches++;
8863 if (nbranches == 0 && ndelete_branches == 0) {
8864 struct got_reference *head_ref;
8865 if (worktree)
8866 error = got_ref_open(&head_ref, repo,
8867 got_worktree_get_head_ref_name(worktree), 0);
8868 else
8869 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
8870 if (error)
8871 goto done;
8872 if (got_ref_is_symbolic(head_ref)) {
8873 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
8874 got_ref_close(head_ref);
8875 if (error)
8876 goto done;
8877 } else
8878 ref = head_ref;
8879 error = got_pathlist_append(&branches, got_ref_get_name(ref),
8880 NULL);
8881 if (error)
8882 goto done;
8883 nbranches++;
8886 if (verbosity >= 0)
8887 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
8888 port ? ":" : "", port ? port : "");
8890 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
8891 server_path, verbosity);
8892 if (error)
8893 goto done;
8895 memset(&spa, 0, sizeof(spa));
8896 spa.last_scaled_packsize[0] = '\0';
8897 spa.last_p_deltify = -1;
8898 spa.last_p_written = -1;
8899 spa.verbosity = verbosity;
8900 spa.delete_branches = &delete_branches;
8901 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
8902 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
8903 check_cancelled, NULL);
8904 if (spa.printed_something)
8905 putchar('\n');
8906 if (error)
8907 goto done;
8908 if (!spa.sent_something && verbosity >= 0)
8909 printf("Already up-to-date\n");
8910 done:
8911 if (sendpid > 0) {
8912 if (kill(sendpid, SIGTERM) == -1)
8913 error = got_error_from_errno("kill");
8914 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
8915 error = got_error_from_errno("waitpid");
8917 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
8918 error = got_error_from_errno("close");
8919 if (repo) {
8920 const struct got_error *close_err = got_repo_close(repo);
8921 if (error == NULL)
8922 error = close_err;
8924 if (worktree)
8925 got_worktree_close(worktree);
8926 if (pack_fds) {
8927 const struct got_error *pack_err =
8928 got_repo_pack_fds_close(pack_fds);
8929 if (error == NULL)
8930 error = pack_err;
8932 if (ref)
8933 got_ref_close(ref);
8934 got_pathlist_free(&branches);
8935 got_pathlist_free(&tags);
8936 got_ref_list_free(&all_branches);
8937 got_ref_list_free(&all_tags);
8938 got_pathlist_free(&delete_args);
8939 TAILQ_FOREACH(pe, &delete_branches, entry)
8940 free((char *)pe->path);
8941 got_pathlist_free(&delete_branches);
8942 free(cwd);
8943 free(repo_path);
8944 free(proto);
8945 free(host);
8946 free(port);
8947 free(server_path);
8948 free(repo_name);
8949 return error;
8952 __dead static void
8953 usage_cherrypick(void)
8955 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
8956 exit(1);
8959 static const struct got_error *
8960 cmd_cherrypick(int argc, char *argv[])
8962 const struct got_error *error = NULL;
8963 struct got_worktree *worktree = NULL;
8964 struct got_repository *repo = NULL;
8965 char *cwd = NULL, *commit_id_str = NULL;
8966 struct got_object_id *commit_id = NULL;
8967 struct got_commit_object *commit = NULL;
8968 struct got_object_qid *pid;
8969 int ch;
8970 struct got_update_progress_arg upa;
8971 int *pack_fds = NULL;
8973 while ((ch = getopt(argc, argv, "")) != -1) {
8974 switch (ch) {
8975 default:
8976 usage_cherrypick();
8977 /* NOTREACHED */
8981 argc -= optind;
8982 argv += optind;
8984 #ifndef PROFILE
8985 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8986 "unveil", NULL) == -1)
8987 err(1, "pledge");
8988 #endif
8989 if (argc != 1)
8990 usage_cherrypick();
8992 cwd = getcwd(NULL, 0);
8993 if (cwd == NULL) {
8994 error = got_error_from_errno("getcwd");
8995 goto done;
8998 error = got_repo_pack_fds_open(&pack_fds);
8999 if (error != NULL)
9000 goto done;
9002 error = got_worktree_open(&worktree, cwd);
9003 if (error) {
9004 if (error->code == GOT_ERR_NOT_WORKTREE)
9005 error = wrap_not_worktree_error(error, "cherrypick",
9006 cwd);
9007 goto done;
9010 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9011 NULL, pack_fds);
9012 if (error != NULL)
9013 goto done;
9015 error = apply_unveil(got_repo_get_path(repo), 0,
9016 got_worktree_get_root_path(worktree));
9017 if (error)
9018 goto done;
9020 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9021 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9022 if (error)
9023 goto done;
9024 error = got_object_id_str(&commit_id_str, commit_id);
9025 if (error)
9026 goto done;
9028 error = got_object_open_as_commit(&commit, repo, commit_id);
9029 if (error)
9030 goto done;
9031 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9032 memset(&upa, 0, sizeof(upa));
9033 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
9034 commit_id, repo, update_progress, &upa, check_cancelled,
9035 NULL);
9036 if (error != NULL)
9037 goto done;
9039 if (upa.did_something)
9040 printf("Merged commit %s\n", commit_id_str);
9041 print_merge_progress_stats(&upa);
9042 done:
9043 if (commit)
9044 got_object_commit_close(commit);
9045 free(commit_id_str);
9046 if (worktree)
9047 got_worktree_close(worktree);
9048 if (repo) {
9049 const struct got_error *close_err = got_repo_close(repo);
9050 if (error == NULL)
9051 error = close_err;
9053 if (pack_fds) {
9054 const struct got_error *pack_err =
9055 got_repo_pack_fds_close(pack_fds);
9056 if (error == NULL)
9057 error = pack_err;
9060 return error;
9063 __dead static void
9064 usage_backout(void)
9066 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
9067 exit(1);
9070 static const struct got_error *
9071 cmd_backout(int argc, char *argv[])
9073 const struct got_error *error = NULL;
9074 struct got_worktree *worktree = NULL;
9075 struct got_repository *repo = NULL;
9076 char *cwd = NULL, *commit_id_str = NULL;
9077 struct got_object_id *commit_id = NULL;
9078 struct got_commit_object *commit = NULL;
9079 struct got_object_qid *pid;
9080 int ch;
9081 struct got_update_progress_arg upa;
9082 int *pack_fds = NULL;
9084 while ((ch = getopt(argc, argv, "")) != -1) {
9085 switch (ch) {
9086 default:
9087 usage_backout();
9088 /* NOTREACHED */
9092 argc -= optind;
9093 argv += optind;
9095 #ifndef PROFILE
9096 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9097 "unveil", NULL) == -1)
9098 err(1, "pledge");
9099 #endif
9100 if (argc != 1)
9101 usage_backout();
9103 cwd = getcwd(NULL, 0);
9104 if (cwd == NULL) {
9105 error = got_error_from_errno("getcwd");
9106 goto done;
9109 error = got_repo_pack_fds_open(&pack_fds);
9110 if (error != NULL)
9111 goto done;
9113 error = got_worktree_open(&worktree, cwd);
9114 if (error) {
9115 if (error->code == GOT_ERR_NOT_WORKTREE)
9116 error = wrap_not_worktree_error(error, "backout", cwd);
9117 goto done;
9120 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9121 NULL, pack_fds);
9122 if (error != NULL)
9123 goto done;
9125 error = apply_unveil(got_repo_get_path(repo), 0,
9126 got_worktree_get_root_path(worktree));
9127 if (error)
9128 goto done;
9130 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
9131 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9132 if (error)
9133 goto done;
9134 error = got_object_id_str(&commit_id_str, commit_id);
9135 if (error)
9136 goto done;
9138 error = got_object_open_as_commit(&commit, repo, commit_id);
9139 if (error)
9140 goto done;
9141 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
9142 if (pid == NULL) {
9143 error = got_error(GOT_ERR_ROOT_COMMIT);
9144 goto done;
9147 memset(&upa, 0, sizeof(upa));
9148 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
9149 repo, update_progress, &upa, check_cancelled, NULL);
9150 if (error != NULL)
9151 goto done;
9153 if (upa.did_something)
9154 printf("Backed out commit %s\n", commit_id_str);
9155 print_merge_progress_stats(&upa);
9156 done:
9157 if (commit)
9158 got_object_commit_close(commit);
9159 free(commit_id_str);
9160 if (worktree)
9161 got_worktree_close(worktree);
9162 if (repo) {
9163 const struct got_error *close_err = got_repo_close(repo);
9164 if (error == NULL)
9165 error = close_err;
9167 if (pack_fds) {
9168 const struct got_error *pack_err =
9169 got_repo_pack_fds_close(pack_fds);
9170 if (error == NULL)
9171 error = pack_err;
9173 return error;
9176 __dead static void
9177 usage_rebase(void)
9179 fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
9180 getprogname());
9181 exit(1);
9184 static void
9185 trim_logmsg(char *logmsg, int limit)
9187 char *nl;
9188 size_t len;
9190 len = strlen(logmsg);
9191 if (len > limit)
9192 len = limit;
9193 logmsg[len] = '\0';
9194 nl = strchr(logmsg, '\n');
9195 if (nl)
9196 *nl = '\0';
9199 static const struct got_error *
9200 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
9202 const struct got_error *err;
9203 char *logmsg0 = NULL;
9204 const char *s;
9206 err = got_object_commit_get_logmsg(&logmsg0, commit);
9207 if (err)
9208 return err;
9210 s = logmsg0;
9211 while (isspace((unsigned char)s[0]))
9212 s++;
9214 *logmsg = strdup(s);
9215 if (*logmsg == NULL) {
9216 err = got_error_from_errno("strdup");
9217 goto done;
9220 trim_logmsg(*logmsg, limit);
9221 done:
9222 free(logmsg0);
9223 return err;
9226 static const struct got_error *
9227 show_rebase_merge_conflict(struct got_object_id *id,
9228 struct got_repository *repo)
9230 const struct got_error *err;
9231 struct got_commit_object *commit = NULL;
9232 char *id_str = NULL, *logmsg = NULL;
9234 err = got_object_open_as_commit(&commit, repo, id);
9235 if (err)
9236 return err;
9238 err = got_object_id_str(&id_str, id);
9239 if (err)
9240 goto done;
9242 id_str[12] = '\0';
9244 err = get_short_logmsg(&logmsg, 42, commit);
9245 if (err)
9246 goto done;
9248 printf("%s -> merge conflict: %s\n", id_str, logmsg);
9249 done:
9250 free(id_str);
9251 got_object_commit_close(commit);
9252 free(logmsg);
9253 return err;
9256 static const struct got_error *
9257 show_rebase_progress(struct got_commit_object *commit,
9258 struct got_object_id *old_id, struct got_object_id *new_id)
9260 const struct got_error *err;
9261 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
9263 err = got_object_id_str(&old_id_str, old_id);
9264 if (err)
9265 goto done;
9267 if (new_id) {
9268 err = got_object_id_str(&new_id_str, new_id);
9269 if (err)
9270 goto done;
9273 old_id_str[12] = '\0';
9274 if (new_id_str)
9275 new_id_str[12] = '\0';
9277 err = get_short_logmsg(&logmsg, 42, commit);
9278 if (err)
9279 goto done;
9281 printf("%s -> %s: %s\n", old_id_str,
9282 new_id_str ? new_id_str : "no-op change", logmsg);
9283 done:
9284 free(old_id_str);
9285 free(new_id_str);
9286 free(logmsg);
9287 return err;
9290 static const struct got_error *
9291 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
9292 struct got_reference *branch, struct got_reference *new_base_branch,
9293 struct got_reference *tmp_branch, struct got_repository *repo,
9294 int create_backup)
9296 printf("Switching work tree to %s\n", got_ref_get_name(branch));
9297 return got_worktree_rebase_complete(worktree, fileindex,
9298 new_base_branch, tmp_branch, branch, repo, create_backup);
9301 static const struct got_error *
9302 rebase_commit(struct got_pathlist_head *merged_paths,
9303 struct got_worktree *worktree, struct got_fileindex *fileindex,
9304 struct got_reference *tmp_branch,
9305 struct got_object_id *commit_id, struct got_repository *repo)
9307 const struct got_error *error;
9308 struct got_commit_object *commit;
9309 struct got_object_id *new_commit_id;
9311 error = got_object_open_as_commit(&commit, repo, commit_id);
9312 if (error)
9313 return error;
9315 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
9316 worktree, fileindex, tmp_branch, commit, commit_id, repo);
9317 if (error) {
9318 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
9319 goto done;
9320 error = show_rebase_progress(commit, commit_id, NULL);
9321 } else {
9322 error = show_rebase_progress(commit, commit_id, new_commit_id);
9323 free(new_commit_id);
9325 done:
9326 got_object_commit_close(commit);
9327 return error;
9330 struct check_path_prefix_arg {
9331 const char *path_prefix;
9332 size_t len;
9333 int errcode;
9336 static const struct got_error *
9337 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
9338 struct got_blob_object *blob2, FILE *f1, FILE *f2,
9339 struct got_object_id *id1, struct got_object_id *id2,
9340 const char *path1, const char *path2,
9341 mode_t mode1, mode_t mode2, struct got_repository *repo)
9343 struct check_path_prefix_arg *a = arg;
9345 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
9346 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
9347 return got_error(a->errcode);
9349 return NULL;
9352 static const struct got_error *
9353 check_path_prefix(struct got_object_id *parent_id,
9354 struct got_object_id *commit_id, const char *path_prefix,
9355 int errcode, struct got_repository *repo)
9357 const struct got_error *err;
9358 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
9359 struct got_commit_object *commit = NULL, *parent_commit = NULL;
9360 struct check_path_prefix_arg cpp_arg;
9362 if (got_path_is_root_dir(path_prefix))
9363 return NULL;
9365 err = got_object_open_as_commit(&commit, repo, commit_id);
9366 if (err)
9367 goto done;
9369 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
9370 if (err)
9371 goto done;
9373 err = got_object_open_as_tree(&tree1, repo,
9374 got_object_commit_get_tree_id(parent_commit));
9375 if (err)
9376 goto done;
9378 err = got_object_open_as_tree(&tree2, repo,
9379 got_object_commit_get_tree_id(commit));
9380 if (err)
9381 goto done;
9383 cpp_arg.path_prefix = path_prefix;
9384 while (cpp_arg.path_prefix[0] == '/')
9385 cpp_arg.path_prefix++;
9386 cpp_arg.len = strlen(cpp_arg.path_prefix);
9387 cpp_arg.errcode = errcode;
9388 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
9389 check_path_prefix_in_diff, &cpp_arg, 0);
9390 done:
9391 if (tree1)
9392 got_object_tree_close(tree1);
9393 if (tree2)
9394 got_object_tree_close(tree2);
9395 if (commit)
9396 got_object_commit_close(commit);
9397 if (parent_commit)
9398 got_object_commit_close(parent_commit);
9399 return err;
9402 static const struct got_error *
9403 collect_commits(struct got_object_id_queue *commits,
9404 struct got_object_id *initial_commit_id,
9405 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
9406 const char *path_prefix, int path_prefix_errcode,
9407 struct got_repository *repo)
9409 const struct got_error *err = NULL;
9410 struct got_commit_graph *graph = NULL;
9411 struct got_object_id *parent_id = NULL;
9412 struct got_object_qid *qid;
9413 struct got_object_id *commit_id = initial_commit_id;
9415 err = got_commit_graph_open(&graph, "/", 1);
9416 if (err)
9417 return err;
9419 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
9420 check_cancelled, NULL);
9421 if (err)
9422 goto done;
9423 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
9424 err = got_commit_graph_iter_next(&parent_id, graph, repo,
9425 check_cancelled, NULL);
9426 if (err) {
9427 if (err->code == GOT_ERR_ITER_COMPLETED) {
9428 err = got_error_msg(GOT_ERR_ANCESTRY,
9429 "ran out of commits to rebase before "
9430 "youngest common ancestor commit has "
9431 "been reached?!?");
9433 goto done;
9434 } else {
9435 err = check_path_prefix(parent_id, commit_id,
9436 path_prefix, path_prefix_errcode, repo);
9437 if (err)
9438 goto done;
9440 err = got_object_qid_alloc(&qid, commit_id);
9441 if (err)
9442 goto done;
9443 STAILQ_INSERT_HEAD(commits, qid, entry);
9444 commit_id = parent_id;
9447 done:
9448 got_commit_graph_close(graph);
9449 return err;
9452 static const struct got_error *
9453 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
9455 const struct got_error *err = NULL;
9456 time_t committer_time;
9457 struct tm tm;
9458 char datebuf[11]; /* YYYY-MM-DD + NUL */
9459 char *author0 = NULL, *author, *smallerthan;
9460 char *logmsg0 = NULL, *logmsg, *newline;
9462 committer_time = got_object_commit_get_committer_time(commit);
9463 if (gmtime_r(&committer_time, &tm) == NULL)
9464 return got_error_from_errno("gmtime_r");
9465 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
9466 return got_error(GOT_ERR_NO_SPACE);
9468 author0 = strdup(got_object_commit_get_author(commit));
9469 if (author0 == NULL)
9470 return got_error_from_errno("strdup");
9471 author = author0;
9472 smallerthan = strchr(author, '<');
9473 if (smallerthan && smallerthan[1] != '\0')
9474 author = smallerthan + 1;
9475 author[strcspn(author, "@>")] = '\0';
9477 err = got_object_commit_get_logmsg(&logmsg0, commit);
9478 if (err)
9479 goto done;
9480 logmsg = logmsg0;
9481 while (*logmsg == '\n')
9482 logmsg++;
9483 newline = strchr(logmsg, '\n');
9484 if (newline)
9485 *newline = '\0';
9487 if (asprintf(brief_str, "%s %s %s",
9488 datebuf, author, logmsg) == -1)
9489 err = got_error_from_errno("asprintf");
9490 done:
9491 free(author0);
9492 free(logmsg0);
9493 return err;
9496 static const struct got_error *
9497 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
9498 struct got_repository *repo)
9500 const struct got_error *err;
9501 char *id_str;
9503 err = got_object_id_str(&id_str, id);
9504 if (err)
9505 return err;
9507 err = got_ref_delete(ref, repo);
9508 if (err)
9509 goto done;
9511 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
9512 done:
9513 free(id_str);
9514 return err;
9517 static const struct got_error *
9518 print_backup_ref(const char *branch_name, const char *new_id_str,
9519 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
9520 struct got_reflist_object_id_map *refs_idmap,
9521 struct got_repository *repo)
9523 const struct got_error *err = NULL;
9524 struct got_reflist_head *refs;
9525 char *refs_str = NULL;
9526 struct got_object_id *new_commit_id = NULL;
9527 struct got_commit_object *new_commit = NULL;
9528 char *new_commit_brief_str = NULL;
9529 struct got_object_id *yca_id = NULL;
9530 struct got_commit_object *yca_commit = NULL;
9531 char *yca_id_str = NULL, *yca_brief_str = NULL;
9532 char *custom_refs_str;
9534 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
9535 return got_error_from_errno("asprintf");
9537 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL,
9538 0, 0, refs_idmap, custom_refs_str);
9539 if (err)
9540 goto done;
9542 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
9543 if (err)
9544 goto done;
9546 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
9547 if (refs) {
9548 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
9549 if (err)
9550 goto done;
9553 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
9554 if (err)
9555 goto done;
9557 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
9558 if (err)
9559 goto done;
9561 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9562 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
9563 if (err)
9564 goto done;
9566 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
9567 refs_str ? " (" : "", refs_str ? refs_str : "",
9568 refs_str ? ")" : "", new_commit_brief_str);
9569 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
9570 got_object_id_cmp(yca_id, old_commit_id) != 0) {
9571 free(refs_str);
9572 refs_str = NULL;
9574 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
9575 if (err)
9576 goto done;
9578 err = get_commit_brief_str(&yca_brief_str, yca_commit);
9579 if (err)
9580 goto done;
9582 err = got_object_id_str(&yca_id_str, yca_id);
9583 if (err)
9584 goto done;
9586 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
9587 if (refs) {
9588 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
9589 if (err)
9590 goto done;
9592 printf("history forked at %s%s%s%s\n %s\n",
9593 yca_id_str,
9594 refs_str ? " (" : "", refs_str ? refs_str : "",
9595 refs_str ? ")" : "", yca_brief_str);
9597 done:
9598 free(custom_refs_str);
9599 free(new_commit_id);
9600 free(refs_str);
9601 free(yca_id);
9602 free(yca_id_str);
9603 free(yca_brief_str);
9604 if (new_commit)
9605 got_object_commit_close(new_commit);
9606 if (yca_commit)
9607 got_object_commit_close(yca_commit);
9609 return NULL;
9612 static const struct got_error *
9613 process_backup_refs(const char *backup_ref_prefix,
9614 const char *wanted_branch_name,
9615 int delete, struct got_repository *repo)
9617 const struct got_error *err;
9618 struct got_reflist_head refs, backup_refs;
9619 struct got_reflist_entry *re;
9620 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
9621 struct got_object_id *old_commit_id = NULL;
9622 char *branch_name = NULL;
9623 struct got_commit_object *old_commit = NULL;
9624 struct got_reflist_object_id_map *refs_idmap = NULL;
9625 int wanted_branch_found = 0;
9627 TAILQ_INIT(&refs);
9628 TAILQ_INIT(&backup_refs);
9630 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
9631 if (err)
9632 return err;
9634 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9635 if (err)
9636 goto done;
9638 if (wanted_branch_name) {
9639 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
9640 wanted_branch_name += 11;
9643 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
9644 got_ref_cmp_by_commit_timestamp_descending, repo);
9645 if (err)
9646 goto done;
9648 TAILQ_FOREACH(re, &backup_refs, entry) {
9649 const char *refname = got_ref_get_name(re->ref);
9650 char *slash;
9652 err = check_cancelled(NULL);
9653 if (err)
9654 break;
9656 err = got_ref_resolve(&old_commit_id, repo, re->ref);
9657 if (err)
9658 break;
9660 err = got_object_open_as_commit(&old_commit, repo,
9661 old_commit_id);
9662 if (err)
9663 break;
9665 if (strncmp(backup_ref_prefix, refname,
9666 backup_ref_prefix_len) == 0)
9667 refname += backup_ref_prefix_len;
9669 while (refname[0] == '/')
9670 refname++;
9672 branch_name = strdup(refname);
9673 if (branch_name == NULL) {
9674 err = got_error_from_errno("strdup");
9675 break;
9677 slash = strrchr(branch_name, '/');
9678 if (slash) {
9679 *slash = '\0';
9680 refname += strlen(branch_name) + 1;
9683 if (wanted_branch_name == NULL ||
9684 strcmp(wanted_branch_name, branch_name) == 0) {
9685 wanted_branch_found = 1;
9686 if (delete) {
9687 err = delete_backup_ref(re->ref,
9688 old_commit_id, repo);
9689 } else {
9690 err = print_backup_ref(branch_name, refname,
9691 old_commit_id, old_commit, refs_idmap,
9692 repo);
9694 if (err)
9695 break;
9698 free(old_commit_id);
9699 old_commit_id = NULL;
9700 free(branch_name);
9701 branch_name = NULL;
9702 got_object_commit_close(old_commit);
9703 old_commit = NULL;
9706 if (wanted_branch_name && !wanted_branch_found) {
9707 err = got_error_fmt(GOT_ERR_NOT_REF,
9708 "%s/%s/", backup_ref_prefix, wanted_branch_name);
9710 done:
9711 if (refs_idmap)
9712 got_reflist_object_id_map_free(refs_idmap);
9713 got_ref_list_free(&refs);
9714 got_ref_list_free(&backup_refs);
9715 free(old_commit_id);
9716 free(branch_name);
9717 if (old_commit)
9718 got_object_commit_close(old_commit);
9719 return err;
9722 static const struct got_error *
9723 abort_progress(void *arg, unsigned char status, const char *path)
9726 * Unversioned files should not clutter progress output when
9727 * an operation is aborted.
9729 if (status == GOT_STATUS_UNVERSIONED)
9730 return NULL;
9732 return update_progress(arg, status, path);
9735 static const struct got_error *
9736 cmd_rebase(int argc, char *argv[])
9738 const struct got_error *error = NULL;
9739 struct got_worktree *worktree = NULL;
9740 struct got_repository *repo = NULL;
9741 struct got_fileindex *fileindex = NULL;
9742 char *cwd = NULL;
9743 struct got_reference *branch = NULL;
9744 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
9745 struct got_object_id *commit_id = NULL, *parent_id = NULL;
9746 struct got_object_id *resume_commit_id = NULL;
9747 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
9748 struct got_commit_object *commit = NULL;
9749 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
9750 int histedit_in_progress = 0, merge_in_progress = 0;
9751 int create_backup = 1, list_backups = 0, delete_backups = 0;
9752 struct got_object_id_queue commits;
9753 struct got_pathlist_head merged_paths;
9754 const struct got_object_id_queue *parent_ids;
9755 struct got_object_qid *qid, *pid;
9756 struct got_update_progress_arg upa;
9757 int *pack_fds = NULL;
9759 STAILQ_INIT(&commits);
9760 TAILQ_INIT(&merged_paths);
9761 memset(&upa, 0, sizeof(upa));
9763 while ((ch = getopt(argc, argv, "aclX")) != -1) {
9764 switch (ch) {
9765 case 'a':
9766 abort_rebase = 1;
9767 break;
9768 case 'c':
9769 continue_rebase = 1;
9770 break;
9771 case 'l':
9772 list_backups = 1;
9773 break;
9774 case 'X':
9775 delete_backups = 1;
9776 break;
9777 default:
9778 usage_rebase();
9779 /* NOTREACHED */
9783 argc -= optind;
9784 argv += optind;
9786 #ifndef PROFILE
9787 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9788 "unveil", NULL) == -1)
9789 err(1, "pledge");
9790 #endif
9791 if (list_backups) {
9792 if (abort_rebase)
9793 option_conflict('l', 'a');
9794 if (continue_rebase)
9795 option_conflict('l', 'c');
9796 if (delete_backups)
9797 option_conflict('l', 'X');
9798 if (argc != 0 && argc != 1)
9799 usage_rebase();
9800 } else if (delete_backups) {
9801 if (abort_rebase)
9802 option_conflict('X', 'a');
9803 if (continue_rebase)
9804 option_conflict('X', 'c');
9805 if (list_backups)
9806 option_conflict('l', 'X');
9807 if (argc != 0 && argc != 1)
9808 usage_rebase();
9809 } else {
9810 if (abort_rebase && continue_rebase)
9811 usage_rebase();
9812 else if (abort_rebase || continue_rebase) {
9813 if (argc != 0)
9814 usage_rebase();
9815 } else if (argc != 1)
9816 usage_rebase();
9819 cwd = getcwd(NULL, 0);
9820 if (cwd == NULL) {
9821 error = got_error_from_errno("getcwd");
9822 goto done;
9825 error = got_repo_pack_fds_open(&pack_fds);
9826 if (error != NULL)
9827 goto done;
9829 error = got_worktree_open(&worktree, cwd);
9830 if (error) {
9831 if (list_backups || delete_backups) {
9832 if (error->code != GOT_ERR_NOT_WORKTREE)
9833 goto done;
9834 } else {
9835 if (error->code == GOT_ERR_NOT_WORKTREE)
9836 error = wrap_not_worktree_error(error,
9837 "rebase", cwd);
9838 goto done;
9842 error = got_repo_open(&repo,
9843 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
9844 pack_fds);
9845 if (error != NULL)
9846 goto done;
9848 error = apply_unveil(got_repo_get_path(repo), 0,
9849 worktree ? got_worktree_get_root_path(worktree) : NULL);
9850 if (error)
9851 goto done;
9853 if (list_backups || delete_backups) {
9854 error = process_backup_refs(
9855 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
9856 argc == 1 ? argv[0] : NULL, delete_backups, repo);
9857 goto done; /* nothing else to do */
9860 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9861 worktree);
9862 if (error)
9863 goto done;
9864 if (histedit_in_progress) {
9865 error = got_error(GOT_ERR_HISTEDIT_BUSY);
9866 goto done;
9869 error = got_worktree_merge_in_progress(&merge_in_progress,
9870 worktree, repo);
9871 if (error)
9872 goto done;
9873 if (merge_in_progress) {
9874 error = got_error(GOT_ERR_MERGE_BUSY);
9875 goto done;
9878 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9879 if (error)
9880 goto done;
9882 if (abort_rebase) {
9883 if (!rebase_in_progress) {
9884 error = got_error(GOT_ERR_NOT_REBASING);
9885 goto done;
9887 error = got_worktree_rebase_continue(&resume_commit_id,
9888 &new_base_branch, &tmp_branch, &branch, &fileindex,
9889 worktree, repo);
9890 if (error)
9891 goto done;
9892 printf("Switching work tree to %s\n",
9893 got_ref_get_symref_target(new_base_branch));
9894 error = got_worktree_rebase_abort(worktree, fileindex, repo,
9895 new_base_branch, abort_progress, &upa);
9896 if (error)
9897 goto done;
9898 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
9899 print_merge_progress_stats(&upa);
9900 goto done; /* nothing else to do */
9903 if (continue_rebase) {
9904 if (!rebase_in_progress) {
9905 error = got_error(GOT_ERR_NOT_REBASING);
9906 goto done;
9908 error = got_worktree_rebase_continue(&resume_commit_id,
9909 &new_base_branch, &tmp_branch, &branch, &fileindex,
9910 worktree, repo);
9911 if (error)
9912 goto done;
9914 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
9915 resume_commit_id, repo);
9916 if (error)
9917 goto done;
9919 yca_id = got_object_id_dup(resume_commit_id);
9920 if (yca_id == NULL) {
9921 error = got_error_from_errno("got_object_id_dup");
9922 goto done;
9924 } else {
9925 error = got_ref_open(&branch, repo, argv[0], 0);
9926 if (error != NULL)
9927 goto done;
9930 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
9931 if (error)
9932 goto done;
9934 if (!continue_rebase) {
9935 struct got_object_id *base_commit_id;
9937 base_commit_id = got_worktree_get_base_commit_id(worktree);
9938 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
9939 base_commit_id, branch_head_commit_id, 1, repo,
9940 check_cancelled, NULL);
9941 if (error)
9942 goto done;
9943 if (yca_id == NULL) {
9944 error = got_error_msg(GOT_ERR_ANCESTRY,
9945 "specified branch shares no common ancestry "
9946 "with work tree's branch");
9947 goto done;
9950 error = check_same_branch(base_commit_id, branch, yca_id, repo);
9951 if (error) {
9952 if (error->code != GOT_ERR_ANCESTRY)
9953 goto done;
9954 error = NULL;
9955 } else {
9956 struct got_pathlist_head paths;
9957 printf("%s is already based on %s\n",
9958 got_ref_get_name(branch),
9959 got_worktree_get_head_ref_name(worktree));
9960 error = switch_head_ref(branch, branch_head_commit_id,
9961 worktree, repo);
9962 if (error)
9963 goto done;
9964 error = got_worktree_set_base_commit_id(worktree, repo,
9965 branch_head_commit_id);
9966 if (error)
9967 goto done;
9968 TAILQ_INIT(&paths);
9969 error = got_pathlist_append(&paths, "", NULL);
9970 if (error)
9971 goto done;
9972 error = got_worktree_checkout_files(worktree,
9973 &paths, repo, update_progress, &upa,
9974 check_cancelled, NULL);
9975 got_pathlist_free(&paths);
9976 if (error)
9977 goto done;
9978 if (upa.did_something) {
9979 char *id_str;
9980 error = got_object_id_str(&id_str,
9981 branch_head_commit_id);
9982 if (error)
9983 goto done;
9984 printf("Updated to %s: %s\n",
9985 got_worktree_get_head_ref_name(worktree),
9986 id_str);
9987 free(id_str);
9988 } else
9989 printf("Already up-to-date\n");
9990 print_update_progress_stats(&upa);
9991 goto done;
9995 commit_id = branch_head_commit_id;
9996 error = got_object_open_as_commit(&commit, repo, commit_id);
9997 if (error)
9998 goto done;
10000 parent_ids = got_object_commit_get_parent_ids(commit);
10001 pid = STAILQ_FIRST(parent_ids);
10002 if (pid == NULL) {
10003 error = got_error(GOT_ERR_EMPTY_REBASE);
10004 goto done;
10006 error = collect_commits(&commits, commit_id, &pid->id,
10007 yca_id, got_worktree_get_path_prefix(worktree),
10008 GOT_ERR_REBASE_PATH, repo);
10009 got_object_commit_close(commit);
10010 commit = NULL;
10011 if (error)
10012 goto done;
10014 if (!continue_rebase) {
10015 error = got_worktree_rebase_prepare(&new_base_branch,
10016 &tmp_branch, &fileindex, worktree, branch, repo);
10017 if (error)
10018 goto done;
10021 if (STAILQ_EMPTY(&commits)) {
10022 if (continue_rebase) {
10023 error = rebase_complete(worktree, fileindex,
10024 branch, new_base_branch, tmp_branch, repo,
10025 create_backup);
10026 goto done;
10027 } else {
10028 /* Fast-forward the reference of the branch. */
10029 struct got_object_id *new_head_commit_id;
10030 char *id_str;
10031 error = got_ref_resolve(&new_head_commit_id, repo,
10032 new_base_branch);
10033 if (error)
10034 goto done;
10035 error = got_object_id_str(&id_str, new_head_commit_id);
10036 printf("Forwarding %s to commit %s\n",
10037 got_ref_get_name(branch), id_str);
10038 free(id_str);
10039 error = got_ref_change_ref(branch,
10040 new_head_commit_id);
10041 if (error)
10042 goto done;
10043 /* No backup needed since objects did not change. */
10044 create_backup = 0;
10048 pid = NULL;
10049 STAILQ_FOREACH(qid, &commits, entry) {
10051 commit_id = &qid->id;
10052 parent_id = pid ? &pid->id : yca_id;
10053 pid = qid;
10055 memset(&upa, 0, sizeof(upa));
10056 error = got_worktree_rebase_merge_files(&merged_paths,
10057 worktree, fileindex, parent_id, commit_id, repo,
10058 update_progress, &upa, check_cancelled, NULL);
10059 if (error)
10060 goto done;
10062 print_merge_progress_stats(&upa);
10063 if (upa.conflicts > 0 || upa.missing > 0 ||
10064 upa.not_deleted > 0 || upa.unversioned > 0) {
10065 if (upa.conflicts > 0) {
10066 error = show_rebase_merge_conflict(&qid->id,
10067 repo);
10068 if (error)
10069 goto done;
10071 got_worktree_rebase_pathlist_free(&merged_paths);
10072 break;
10075 error = rebase_commit(&merged_paths, worktree, fileindex,
10076 tmp_branch, commit_id, repo);
10077 got_worktree_rebase_pathlist_free(&merged_paths);
10078 if (error)
10079 goto done;
10082 if (upa.conflicts > 0 || upa.missing > 0 ||
10083 upa.not_deleted > 0 || upa.unversioned > 0) {
10084 error = got_worktree_rebase_postpone(worktree, fileindex);
10085 if (error)
10086 goto done;
10087 if (upa.conflicts > 0 && upa.missing == 0 &&
10088 upa.not_deleted == 0 && upa.unversioned == 0) {
10089 error = got_error_msg(GOT_ERR_CONFLICTS,
10090 "conflicts must be resolved before rebasing "
10091 "can continue");
10092 } else if (upa.conflicts > 0) {
10093 error = got_error_msg(GOT_ERR_CONFLICTS,
10094 "conflicts must be resolved before rebasing "
10095 "can continue; changes destined for some "
10096 "files were not yet merged and should be "
10097 "merged manually if required before the "
10098 "rebase operation is continued");
10099 } else {
10100 error = got_error_msg(GOT_ERR_CONFLICTS,
10101 "changes destined for some files were not "
10102 "yet merged and should be merged manually "
10103 "if required before the rebase operation "
10104 "is continued");
10106 } else
10107 error = rebase_complete(worktree, fileindex, branch,
10108 new_base_branch, tmp_branch, repo, create_backup);
10109 done:
10110 got_object_id_queue_free(&commits);
10111 free(branch_head_commit_id);
10112 free(resume_commit_id);
10113 free(yca_id);
10114 if (commit)
10115 got_object_commit_close(commit);
10116 if (branch)
10117 got_ref_close(branch);
10118 if (new_base_branch)
10119 got_ref_close(new_base_branch);
10120 if (tmp_branch)
10121 got_ref_close(tmp_branch);
10122 if (worktree)
10123 got_worktree_close(worktree);
10124 if (repo) {
10125 const struct got_error *close_err = got_repo_close(repo);
10126 if (error == NULL)
10127 error = close_err;
10129 if (pack_fds) {
10130 const struct got_error *pack_err =
10131 got_repo_pack_fds_close(pack_fds);
10132 if (error == NULL)
10133 error = pack_err;
10135 return error;
10138 __dead static void
10139 usage_histedit(void)
10141 fprintf(stderr, "usage: %s histedit [-a] [-c] [-e] [-f] "
10142 "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
10143 getprogname());
10144 exit(1);
10147 #define GOT_HISTEDIT_PICK 'p'
10148 #define GOT_HISTEDIT_EDIT 'e'
10149 #define GOT_HISTEDIT_FOLD 'f'
10150 #define GOT_HISTEDIT_DROP 'd'
10151 #define GOT_HISTEDIT_MESG 'm'
10153 static const struct got_histedit_cmd {
10154 unsigned char code;
10155 const char *name;
10156 const char *desc;
10157 } got_histedit_cmds[] = {
10158 { GOT_HISTEDIT_PICK, "pick", "use commit" },
10159 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
10160 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
10161 "be used" },
10162 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
10163 { GOT_HISTEDIT_MESG, "mesg",
10164 "single-line log message for commit above (open editor if empty)" },
10167 struct got_histedit_list_entry {
10168 TAILQ_ENTRY(got_histedit_list_entry) entry;
10169 struct got_object_id *commit_id;
10170 const struct got_histedit_cmd *cmd;
10171 char *logmsg;
10173 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
10175 static const struct got_error *
10176 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
10177 FILE *f, struct got_repository *repo)
10179 const struct got_error *err = NULL;
10180 char *logmsg = NULL, *id_str = NULL;
10181 struct got_commit_object *commit = NULL;
10182 int n;
10184 err = got_object_open_as_commit(&commit, repo, commit_id);
10185 if (err)
10186 goto done;
10188 err = get_short_logmsg(&logmsg, 34, commit);
10189 if (err)
10190 goto done;
10192 err = got_object_id_str(&id_str, commit_id);
10193 if (err)
10194 goto done;
10196 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
10197 if (n < 0)
10198 err = got_ferror(f, GOT_ERR_IO);
10199 done:
10200 if (commit)
10201 got_object_commit_close(commit);
10202 free(id_str);
10203 free(logmsg);
10204 return err;
10207 static const struct got_error *
10208 histedit_write_commit_list(struct got_object_id_queue *commits,
10209 FILE *f, int edit_logmsg_only, int fold_only, int edit_only,
10210 struct got_repository *repo)
10212 const struct got_error *err = NULL;
10213 struct got_object_qid *qid;
10214 const char *histedit_cmd = NULL;
10216 if (STAILQ_EMPTY(commits))
10217 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10219 STAILQ_FOREACH(qid, commits, entry) {
10220 histedit_cmd = got_histedit_cmds[0].name;
10221 if (edit_only)
10222 histedit_cmd = "edit";
10223 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
10224 histedit_cmd = "fold";
10225 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
10226 if (err)
10227 break;
10228 if (edit_logmsg_only) {
10229 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
10230 if (n < 0) {
10231 err = got_ferror(f, GOT_ERR_IO);
10232 break;
10237 return err;
10240 static const struct got_error *
10241 write_cmd_list(FILE *f, const char *branch_name,
10242 struct got_object_id_queue *commits)
10244 const struct got_error *err = NULL;
10245 size_t i;
10246 int n;
10247 char *id_str;
10248 struct got_object_qid *qid;
10250 qid = STAILQ_FIRST(commits);
10251 err = got_object_id_str(&id_str, &qid->id);
10252 if (err)
10253 return err;
10255 n = fprintf(f,
10256 "# Editing the history of branch '%s' starting at\n"
10257 "# commit %s\n"
10258 "# Commits will be processed in order from top to "
10259 "bottom of this file.\n", branch_name, id_str);
10260 if (n < 0) {
10261 err = got_ferror(f, GOT_ERR_IO);
10262 goto done;
10265 n = fprintf(f, "# Available histedit commands:\n");
10266 if (n < 0) {
10267 err = got_ferror(f, GOT_ERR_IO);
10268 goto done;
10271 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10272 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
10273 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
10274 cmd->desc);
10275 if (n < 0) {
10276 err = got_ferror(f, GOT_ERR_IO);
10277 break;
10280 done:
10281 free(id_str);
10282 return err;
10285 static const struct got_error *
10286 histedit_syntax_error(int lineno)
10288 static char msg[42];
10289 int ret;
10291 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
10292 lineno);
10293 if (ret == -1 || ret >= sizeof(msg))
10294 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
10296 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
10299 static const struct got_error *
10300 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
10301 char *logmsg, struct got_repository *repo)
10303 const struct got_error *err;
10304 struct got_commit_object *folded_commit = NULL;
10305 char *id_str, *folded_logmsg = NULL;
10307 err = got_object_id_str(&id_str, hle->commit_id);
10308 if (err)
10309 return err;
10311 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
10312 if (err)
10313 goto done;
10315 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
10316 if (err)
10317 goto done;
10318 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
10319 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
10320 folded_logmsg) == -1) {
10321 err = got_error_from_errno("asprintf");
10323 done:
10324 if (folded_commit)
10325 got_object_commit_close(folded_commit);
10326 free(id_str);
10327 free(folded_logmsg);
10328 return err;
10331 static struct got_histedit_list_entry *
10332 get_folded_commits(struct got_histedit_list_entry *hle)
10334 struct got_histedit_list_entry *prev, *folded = NULL;
10336 prev = TAILQ_PREV(hle, got_histedit_list, entry);
10337 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
10338 prev->cmd->code == GOT_HISTEDIT_DROP)) {
10339 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
10340 folded = prev;
10341 prev = TAILQ_PREV(prev, got_histedit_list, entry);
10344 return folded;
10347 static const struct got_error *
10348 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
10349 struct got_repository *repo)
10351 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
10352 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
10353 const struct got_error *err = NULL;
10354 struct got_commit_object *commit = NULL;
10355 int logmsg_len;
10356 int fd;
10357 struct got_histedit_list_entry *folded = NULL;
10359 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10360 if (err)
10361 return err;
10363 folded = get_folded_commits(hle);
10364 if (folded) {
10365 while (folded != hle) {
10366 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
10367 folded = TAILQ_NEXT(folded, entry);
10368 continue;
10370 err = append_folded_commit_msg(&new_msg, folded,
10371 logmsg, repo);
10372 if (err)
10373 goto done;
10374 free(logmsg);
10375 logmsg = new_msg;
10376 folded = TAILQ_NEXT(folded, entry);
10380 err = got_object_id_str(&id_str, hle->commit_id);
10381 if (err)
10382 goto done;
10383 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
10384 if (err)
10385 goto done;
10386 logmsg_len = asprintf(&new_msg,
10387 "%s\n# original log message of commit %s: %s",
10388 logmsg ? logmsg : "", id_str, orig_logmsg);
10389 if (logmsg_len == -1) {
10390 err = got_error_from_errno("asprintf");
10391 goto done;
10393 free(logmsg);
10394 logmsg = new_msg;
10396 err = got_object_id_str(&id_str, hle->commit_id);
10397 if (err)
10398 goto done;
10400 err = got_opentemp_named_fd(&logmsg_path, &fd,
10401 GOT_TMPDIR_STR "/got-logmsg");
10402 if (err)
10403 goto done;
10405 write(fd, logmsg, logmsg_len);
10406 close(fd);
10408 err = get_editor(&editor);
10409 if (err)
10410 goto done;
10412 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
10413 logmsg_len, 0);
10414 if (err) {
10415 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
10416 goto done;
10417 err = NULL;
10418 hle->logmsg = strdup(new_msg);
10419 if (hle->logmsg == NULL)
10420 err = got_error_from_errno("strdup");
10422 done:
10423 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
10424 err = got_error_from_errno2("unlink", logmsg_path);
10425 free(logmsg_path);
10426 free(logmsg);
10427 free(orig_logmsg);
10428 free(editor);
10429 if (commit)
10430 got_object_commit_close(commit);
10431 return err;
10434 static const struct got_error *
10435 histedit_parse_list(struct got_histedit_list *histedit_cmds,
10436 FILE *f, struct got_repository *repo)
10438 const struct got_error *err = NULL;
10439 char *line = NULL, *p, *end;
10440 size_t i, size;
10441 ssize_t len;
10442 int lineno = 0;
10443 const struct got_histedit_cmd *cmd;
10444 struct got_object_id *commit_id = NULL;
10445 struct got_histedit_list_entry *hle = NULL;
10447 for (;;) {
10448 len = getline(&line, &size, f);
10449 if (len == -1) {
10450 const struct got_error *getline_err;
10451 if (feof(f))
10452 break;
10453 getline_err = got_error_from_errno("getline");
10454 err = got_ferror(f, getline_err->code);
10455 break;
10457 lineno++;
10458 p = line;
10459 while (isspace((unsigned char)p[0]))
10460 p++;
10461 if (p[0] == '#' || p[0] == '\0') {
10462 free(line);
10463 line = NULL;
10464 continue;
10466 cmd = NULL;
10467 for (i = 0; i < nitems(got_histedit_cmds); i++) {
10468 cmd = &got_histedit_cmds[i];
10469 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
10470 isspace((unsigned char)p[strlen(cmd->name)])) {
10471 p += strlen(cmd->name);
10472 break;
10474 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
10475 p++;
10476 break;
10479 if (i == nitems(got_histedit_cmds)) {
10480 err = histedit_syntax_error(lineno);
10481 break;
10483 while (isspace((unsigned char)p[0]))
10484 p++;
10485 if (cmd->code == GOT_HISTEDIT_MESG) {
10486 if (hle == NULL || hle->logmsg != NULL) {
10487 err = got_error(GOT_ERR_HISTEDIT_CMD);
10488 break;
10490 if (p[0] == '\0') {
10491 err = histedit_edit_logmsg(hle, repo);
10492 if (err)
10493 break;
10494 } else {
10495 hle->logmsg = strdup(p);
10496 if (hle->logmsg == NULL) {
10497 err = got_error_from_errno("strdup");
10498 break;
10501 free(line);
10502 line = NULL;
10503 continue;
10504 } else {
10505 end = p;
10506 while (end[0] && !isspace((unsigned char)end[0]))
10507 end++;
10508 *end = '\0';
10510 err = got_object_resolve_id_str(&commit_id, repo, p);
10511 if (err) {
10512 /* override error code */
10513 err = histedit_syntax_error(lineno);
10514 break;
10517 hle = malloc(sizeof(*hle));
10518 if (hle == NULL) {
10519 err = got_error_from_errno("malloc");
10520 break;
10522 hle->cmd = cmd;
10523 hle->commit_id = commit_id;
10524 hle->logmsg = NULL;
10525 commit_id = NULL;
10526 free(line);
10527 line = NULL;
10528 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
10531 free(line);
10532 free(commit_id);
10533 return err;
10536 static const struct got_error *
10537 histedit_check_script(struct got_histedit_list *histedit_cmds,
10538 struct got_object_id_queue *commits, struct got_repository *repo)
10540 const struct got_error *err = NULL;
10541 struct got_object_qid *qid;
10542 struct got_histedit_list_entry *hle;
10543 static char msg[92];
10544 char *id_str;
10546 if (TAILQ_EMPTY(histedit_cmds))
10547 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
10548 "histedit script contains no commands");
10549 if (STAILQ_EMPTY(commits))
10550 return got_error(GOT_ERR_EMPTY_HISTEDIT);
10552 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10553 struct got_histedit_list_entry *hle2;
10554 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
10555 if (hle == hle2)
10556 continue;
10557 if (got_object_id_cmp(hle->commit_id,
10558 hle2->commit_id) != 0)
10559 continue;
10560 err = got_object_id_str(&id_str, hle->commit_id);
10561 if (err)
10562 return err;
10563 snprintf(msg, sizeof(msg), "commit %s is listed "
10564 "more than once in histedit script", id_str);
10565 free(id_str);
10566 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10570 STAILQ_FOREACH(qid, commits, entry) {
10571 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10572 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
10573 break;
10575 if (hle == NULL) {
10576 err = got_object_id_str(&id_str, &qid->id);
10577 if (err)
10578 return err;
10579 snprintf(msg, sizeof(msg),
10580 "commit %s missing from histedit script", id_str);
10581 free(id_str);
10582 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
10586 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
10587 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
10588 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
10589 "last commit in histedit script cannot be folded");
10591 return NULL;
10594 static const struct got_error *
10595 histedit_run_editor(struct got_histedit_list *histedit_cmds,
10596 const char *path, struct got_object_id_queue *commits,
10597 struct got_repository *repo)
10599 const struct got_error *err = NULL;
10600 char *editor;
10601 FILE *f = NULL;
10603 err = get_editor(&editor);
10604 if (err)
10605 return err;
10607 if (spawn_editor(editor, path) == -1) {
10608 err = got_error_from_errno("failed spawning editor");
10609 goto done;
10612 f = fopen(path, "re");
10613 if (f == NULL) {
10614 err = got_error_from_errno("fopen");
10615 goto done;
10617 err = histedit_parse_list(histedit_cmds, f, repo);
10618 if (err)
10619 goto done;
10621 err = histedit_check_script(histedit_cmds, commits, repo);
10622 done:
10623 if (f && fclose(f) == EOF && err == NULL)
10624 err = got_error_from_errno("fclose");
10625 free(editor);
10626 return err;
10629 static const struct got_error *
10630 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
10631 struct got_object_id_queue *, const char *, const char *,
10632 struct got_repository *);
10634 static const struct got_error *
10635 histedit_edit_script(struct got_histedit_list *histedit_cmds,
10636 struct got_object_id_queue *commits, const char *branch_name,
10637 int edit_logmsg_only, int fold_only, int edit_only,
10638 struct got_repository *repo)
10640 const struct got_error *err;
10641 FILE *f = NULL;
10642 char *path = NULL;
10644 err = got_opentemp_named(&path, &f, "got-histedit");
10645 if (err)
10646 return err;
10648 err = write_cmd_list(f, branch_name, commits);
10649 if (err)
10650 goto done;
10652 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
10653 fold_only, edit_only, repo);
10654 if (err)
10655 goto done;
10657 if (edit_logmsg_only || fold_only || edit_only) {
10658 rewind(f);
10659 err = histedit_parse_list(histedit_cmds, f, repo);
10660 } else {
10661 if (fclose(f) == EOF) {
10662 err = got_error_from_errno("fclose");
10663 goto done;
10665 f = NULL;
10666 err = histedit_run_editor(histedit_cmds, path, commits, repo);
10667 if (err) {
10668 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10669 err->code != GOT_ERR_HISTEDIT_CMD)
10670 goto done;
10671 err = histedit_edit_list_retry(histedit_cmds, err,
10672 commits, path, branch_name, repo);
10675 done:
10676 if (f && fclose(f) == EOF && err == NULL)
10677 err = got_error_from_errno("fclose");
10678 if (path && unlink(path) != 0 && err == NULL)
10679 err = got_error_from_errno2("unlink", path);
10680 free(path);
10681 return err;
10684 static const struct got_error *
10685 histedit_save_list(struct got_histedit_list *histedit_cmds,
10686 struct got_worktree *worktree, struct got_repository *repo)
10688 const struct got_error *err = NULL;
10689 char *path = NULL;
10690 FILE *f = NULL;
10691 struct got_histedit_list_entry *hle;
10692 struct got_commit_object *commit = NULL;
10694 err = got_worktree_get_histedit_script_path(&path, worktree);
10695 if (err)
10696 return err;
10698 f = fopen(path, "we");
10699 if (f == NULL) {
10700 err = got_error_from_errno2("fopen", path);
10701 goto done;
10703 TAILQ_FOREACH(hle, histedit_cmds, entry) {
10704 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
10705 repo);
10706 if (err)
10707 break;
10709 if (hle->logmsg) {
10710 int n = fprintf(f, "%c %s\n",
10711 GOT_HISTEDIT_MESG, hle->logmsg);
10712 if (n < 0) {
10713 err = got_ferror(f, GOT_ERR_IO);
10714 break;
10718 done:
10719 if (f && fclose(f) == EOF && err == NULL)
10720 err = got_error_from_errno("fclose");
10721 free(path);
10722 if (commit)
10723 got_object_commit_close(commit);
10724 return err;
10727 static void
10728 histedit_free_list(struct got_histedit_list *histedit_cmds)
10730 struct got_histedit_list_entry *hle;
10732 while ((hle = TAILQ_FIRST(histedit_cmds))) {
10733 TAILQ_REMOVE(histedit_cmds, hle, entry);
10734 free(hle);
10738 static const struct got_error *
10739 histedit_load_list(struct got_histedit_list *histedit_cmds,
10740 const char *path, struct got_repository *repo)
10742 const struct got_error *err = NULL;
10743 FILE *f = NULL;
10745 f = fopen(path, "re");
10746 if (f == NULL) {
10747 err = got_error_from_errno2("fopen", path);
10748 goto done;
10751 err = histedit_parse_list(histedit_cmds, f, repo);
10752 done:
10753 if (f && fclose(f) == EOF && err == NULL)
10754 err = got_error_from_errno("fclose");
10755 return err;
10758 static const struct got_error *
10759 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
10760 const struct got_error *edit_err, struct got_object_id_queue *commits,
10761 const char *path, const char *branch_name, struct got_repository *repo)
10763 const struct got_error *err = NULL, *prev_err = edit_err;
10764 int resp = ' ';
10766 while (resp != 'c' && resp != 'r' && resp != 'a') {
10767 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
10768 "or (a)bort: ", getprogname(), prev_err->msg);
10769 resp = getchar();
10770 if (resp == '\n')
10771 resp = getchar();
10772 if (resp == 'c') {
10773 histedit_free_list(histedit_cmds);
10774 err = histedit_run_editor(histedit_cmds, path, commits,
10775 repo);
10776 if (err) {
10777 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10778 err->code != GOT_ERR_HISTEDIT_CMD)
10779 break;
10780 prev_err = err;
10781 resp = ' ';
10782 continue;
10784 break;
10785 } else if (resp == 'r') {
10786 histedit_free_list(histedit_cmds);
10787 err = histedit_edit_script(histedit_cmds,
10788 commits, branch_name, 0, 0, 0, repo);
10789 if (err) {
10790 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
10791 err->code != GOT_ERR_HISTEDIT_CMD)
10792 break;
10793 prev_err = err;
10794 resp = ' ';
10795 continue;
10797 break;
10798 } else if (resp == 'a') {
10799 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
10800 break;
10801 } else
10802 printf("invalid response '%c'\n", resp);
10805 return err;
10808 static const struct got_error *
10809 histedit_complete(struct got_worktree *worktree,
10810 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
10811 struct got_reference *branch, struct got_repository *repo)
10813 printf("Switching work tree to %s\n",
10814 got_ref_get_symref_target(branch));
10815 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
10816 branch, repo);
10819 static const struct got_error *
10820 show_histedit_progress(struct got_commit_object *commit,
10821 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
10823 const struct got_error *err;
10824 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10826 err = got_object_id_str(&old_id_str, hle->commit_id);
10827 if (err)
10828 goto done;
10830 if (new_id) {
10831 err = got_object_id_str(&new_id_str, new_id);
10832 if (err)
10833 goto done;
10836 old_id_str[12] = '\0';
10837 if (new_id_str)
10838 new_id_str[12] = '\0';
10840 if (hle->logmsg) {
10841 logmsg = strdup(hle->logmsg);
10842 if (logmsg == NULL) {
10843 err = got_error_from_errno("strdup");
10844 goto done;
10846 trim_logmsg(logmsg, 42);
10847 } else {
10848 err = get_short_logmsg(&logmsg, 42, commit);
10849 if (err)
10850 goto done;
10853 switch (hle->cmd->code) {
10854 case GOT_HISTEDIT_PICK:
10855 case GOT_HISTEDIT_EDIT:
10856 printf("%s -> %s: %s\n", old_id_str,
10857 new_id_str ? new_id_str : "no-op change", logmsg);
10858 break;
10859 case GOT_HISTEDIT_DROP:
10860 case GOT_HISTEDIT_FOLD:
10861 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
10862 logmsg);
10863 break;
10864 default:
10865 break;
10867 done:
10868 free(old_id_str);
10869 free(new_id_str);
10870 return err;
10873 static const struct got_error *
10874 histedit_commit(struct got_pathlist_head *merged_paths,
10875 struct got_worktree *worktree, struct got_fileindex *fileindex,
10876 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
10877 struct got_repository *repo)
10879 const struct got_error *err;
10880 struct got_commit_object *commit;
10881 struct got_object_id *new_commit_id;
10883 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
10884 && hle->logmsg == NULL) {
10885 err = histedit_edit_logmsg(hle, repo);
10886 if (err)
10887 return err;
10890 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
10891 if (err)
10892 return err;
10894 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
10895 worktree, fileindex, tmp_branch, commit, hle->commit_id,
10896 hle->logmsg, repo);
10897 if (err) {
10898 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
10899 goto done;
10900 err = show_histedit_progress(commit, hle, NULL);
10901 } else {
10902 err = show_histedit_progress(commit, hle, new_commit_id);
10903 free(new_commit_id);
10905 done:
10906 got_object_commit_close(commit);
10907 return err;
10910 static const struct got_error *
10911 histedit_skip_commit(struct got_histedit_list_entry *hle,
10912 struct got_worktree *worktree, struct got_repository *repo)
10914 const struct got_error *error;
10915 struct got_commit_object *commit;
10917 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
10918 repo);
10919 if (error)
10920 return error;
10922 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
10923 if (error)
10924 return error;
10926 error = show_histedit_progress(commit, hle, NULL);
10927 got_object_commit_close(commit);
10928 return error;
10931 static const struct got_error *
10932 check_local_changes(void *arg, unsigned char status,
10933 unsigned char staged_status, const char *path,
10934 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
10935 struct got_object_id *commit_id, int dirfd, const char *de_name)
10937 int *have_local_changes = arg;
10939 switch (status) {
10940 case GOT_STATUS_ADD:
10941 case GOT_STATUS_DELETE:
10942 case GOT_STATUS_MODIFY:
10943 case GOT_STATUS_CONFLICT:
10944 *have_local_changes = 1;
10945 return got_error(GOT_ERR_CANCELLED);
10946 default:
10947 break;
10950 switch (staged_status) {
10951 case GOT_STATUS_ADD:
10952 case GOT_STATUS_DELETE:
10953 case GOT_STATUS_MODIFY:
10954 *have_local_changes = 1;
10955 return got_error(GOT_ERR_CANCELLED);
10956 default:
10957 break;
10960 return NULL;
10963 static const struct got_error *
10964 cmd_histedit(int argc, char *argv[])
10966 const struct got_error *error = NULL;
10967 struct got_worktree *worktree = NULL;
10968 struct got_fileindex *fileindex = NULL;
10969 struct got_repository *repo = NULL;
10970 char *cwd = NULL;
10971 struct got_reference *branch = NULL;
10972 struct got_reference *tmp_branch = NULL;
10973 struct got_object_id *resume_commit_id = NULL;
10974 struct got_object_id *base_commit_id = NULL;
10975 struct got_object_id *head_commit_id = NULL;
10976 struct got_commit_object *commit = NULL;
10977 int ch, rebase_in_progress = 0, merge_in_progress = 0;
10978 struct got_update_progress_arg upa;
10979 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
10980 int edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
10981 int list_backups = 0, delete_backups = 0;
10982 const char *edit_script_path = NULL;
10983 struct got_object_id_queue commits;
10984 struct got_pathlist_head merged_paths;
10985 const struct got_object_id_queue *parent_ids;
10986 struct got_object_qid *pid;
10987 struct got_histedit_list histedit_cmds;
10988 struct got_histedit_list_entry *hle;
10989 int *pack_fds = NULL;
10991 STAILQ_INIT(&commits);
10992 TAILQ_INIT(&histedit_cmds);
10993 TAILQ_INIT(&merged_paths);
10994 memset(&upa, 0, sizeof(upa));
10996 while ((ch = getopt(argc, argv, "acefF:mlX")) != -1) {
10997 switch (ch) {
10998 case 'a':
10999 abort_edit = 1;
11000 break;
11001 case 'c':
11002 continue_edit = 1;
11003 break;
11004 case 'e':
11005 edit_only = 1;
11006 break;
11007 case 'f':
11008 fold_only = 1;
11009 break;
11010 case 'F':
11011 edit_script_path = optarg;
11012 break;
11013 case 'm':
11014 edit_logmsg_only = 1;
11015 break;
11016 case 'l':
11017 list_backups = 1;
11018 break;
11019 case 'X':
11020 delete_backups = 1;
11021 break;
11022 default:
11023 usage_histedit();
11024 /* NOTREACHED */
11028 argc -= optind;
11029 argv += optind;
11031 #ifndef PROFILE
11032 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11033 "unveil", NULL) == -1)
11034 err(1, "pledge");
11035 #endif
11036 if (abort_edit && continue_edit)
11037 option_conflict('a', 'c');
11038 if (edit_script_path && edit_logmsg_only)
11039 option_conflict('F', 'm');
11040 if (abort_edit && edit_logmsg_only)
11041 option_conflict('a', 'm');
11042 if (continue_edit && edit_logmsg_only)
11043 option_conflict('c', 'm');
11044 if (abort_edit && fold_only)
11045 option_conflict('a', 'f');
11046 if (continue_edit && fold_only)
11047 option_conflict('c', 'f');
11048 if (fold_only && edit_logmsg_only)
11049 option_conflict('f', 'm');
11050 if (edit_script_path && fold_only)
11051 option_conflict('F', 'f');
11052 if (abort_edit && edit_only)
11053 option_conflict('a', 'e');
11054 if (continue_edit && edit_only)
11055 option_conflict('c', 'e');
11056 if (edit_only && edit_logmsg_only)
11057 option_conflict('e', 'm');
11058 if (edit_script_path && edit_only)
11059 option_conflict('F', 'e');
11060 if (list_backups) {
11061 if (abort_edit)
11062 option_conflict('l', 'a');
11063 if (continue_edit)
11064 option_conflict('l', 'c');
11065 if (edit_script_path)
11066 option_conflict('l', 'F');
11067 if (edit_logmsg_only)
11068 option_conflict('l', 'm');
11069 if (fold_only)
11070 option_conflict('l', 'f');
11071 if (edit_only)
11072 option_conflict('l', 'e');
11073 if (delete_backups)
11074 option_conflict('l', 'X');
11075 if (argc != 0 && argc != 1)
11076 usage_histedit();
11077 } else if (delete_backups) {
11078 if (abort_edit)
11079 option_conflict('X', 'a');
11080 if (continue_edit)
11081 option_conflict('X', 'c');
11082 if (edit_script_path)
11083 option_conflict('X', 'F');
11084 if (edit_logmsg_only)
11085 option_conflict('X', 'm');
11086 if (fold_only)
11087 option_conflict('X', 'f');
11088 if (edit_only)
11089 option_conflict('X', 'e');
11090 if (list_backups)
11091 option_conflict('X', 'l');
11092 if (argc != 0 && argc != 1)
11093 usage_histedit();
11094 } else if (argc != 0)
11095 usage_histedit();
11098 * This command cannot apply unveil(2) in all cases because the
11099 * user may choose to run an editor to edit the histedit script
11100 * and to edit individual commit log messages.
11101 * unveil(2) traverses exec(2); if an editor is used we have to
11102 * apply unveil after edit script and log messages have been written.
11103 * XXX TODO: Make use of unveil(2) where possible.
11106 cwd = getcwd(NULL, 0);
11107 if (cwd == NULL) {
11108 error = got_error_from_errno("getcwd");
11109 goto done;
11112 error = got_repo_pack_fds_open(&pack_fds);
11113 if (error != NULL)
11114 goto done;
11116 error = got_worktree_open(&worktree, cwd);
11117 if (error) {
11118 if (list_backups || delete_backups) {
11119 if (error->code != GOT_ERR_NOT_WORKTREE)
11120 goto done;
11121 } else {
11122 if (error->code == GOT_ERR_NOT_WORKTREE)
11123 error = wrap_not_worktree_error(error,
11124 "histedit", cwd);
11125 goto done;
11129 if (list_backups || delete_backups) {
11130 error = got_repo_open(&repo,
11131 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11132 NULL, pack_fds);
11133 if (error != NULL)
11134 goto done;
11135 error = apply_unveil(got_repo_get_path(repo), 0,
11136 worktree ? got_worktree_get_root_path(worktree) : NULL);
11137 if (error)
11138 goto done;
11139 error = process_backup_refs(
11140 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
11141 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11142 goto done; /* nothing else to do */
11145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11146 NULL, pack_fds);
11147 if (error != NULL)
11148 goto done;
11150 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11151 if (error)
11152 goto done;
11153 if (rebase_in_progress) {
11154 error = got_error(GOT_ERR_REBASING);
11155 goto done;
11158 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11159 repo);
11160 if (error)
11161 goto done;
11162 if (merge_in_progress) {
11163 error = got_error(GOT_ERR_MERGE_BUSY);
11164 goto done;
11167 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
11168 if (error)
11169 goto done;
11171 if (edit_in_progress && edit_logmsg_only) {
11172 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11173 "histedit operation is in progress in this "
11174 "work tree and must be continued or aborted "
11175 "before the -m option can be used");
11176 goto done;
11178 if (edit_in_progress && fold_only) {
11179 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11180 "histedit operation is in progress in this "
11181 "work tree and must be continued or aborted "
11182 "before the -f option can be used");
11183 goto done;
11185 if (edit_in_progress && edit_only) {
11186 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
11187 "histedit operation is in progress in this "
11188 "work tree and must be continued or aborted "
11189 "before the -e option can be used");
11190 goto done;
11193 if (edit_in_progress && abort_edit) {
11194 error = got_worktree_histedit_continue(&resume_commit_id,
11195 &tmp_branch, &branch, &base_commit_id, &fileindex,
11196 worktree, repo);
11197 if (error)
11198 goto done;
11199 printf("Switching work tree to %s\n",
11200 got_ref_get_symref_target(branch));
11201 error = got_worktree_histedit_abort(worktree, fileindex, repo,
11202 branch, base_commit_id, abort_progress, &upa);
11203 if (error)
11204 goto done;
11205 printf("Histedit of %s aborted\n",
11206 got_ref_get_symref_target(branch));
11207 print_merge_progress_stats(&upa);
11208 goto done; /* nothing else to do */
11209 } else if (abort_edit) {
11210 error = got_error(GOT_ERR_NOT_HISTEDIT);
11211 goto done;
11214 if (continue_edit) {
11215 char *path;
11217 if (!edit_in_progress) {
11218 error = got_error(GOT_ERR_NOT_HISTEDIT);
11219 goto done;
11222 error = got_worktree_get_histedit_script_path(&path, worktree);
11223 if (error)
11224 goto done;
11226 error = histedit_load_list(&histedit_cmds, path, repo);
11227 free(path);
11228 if (error)
11229 goto done;
11231 error = got_worktree_histedit_continue(&resume_commit_id,
11232 &tmp_branch, &branch, &base_commit_id, &fileindex,
11233 worktree, repo);
11234 if (error)
11235 goto done;
11237 error = got_ref_resolve(&head_commit_id, repo, branch);
11238 if (error)
11239 goto done;
11241 error = got_object_open_as_commit(&commit, repo,
11242 head_commit_id);
11243 if (error)
11244 goto done;
11245 parent_ids = got_object_commit_get_parent_ids(commit);
11246 pid = STAILQ_FIRST(parent_ids);
11247 if (pid == NULL) {
11248 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11249 goto done;
11251 error = collect_commits(&commits, head_commit_id, &pid->id,
11252 base_commit_id, got_worktree_get_path_prefix(worktree),
11253 GOT_ERR_HISTEDIT_PATH, repo);
11254 got_object_commit_close(commit);
11255 commit = NULL;
11256 if (error)
11257 goto done;
11258 } else {
11259 if (edit_in_progress) {
11260 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11261 goto done;
11264 error = got_ref_open(&branch, repo,
11265 got_worktree_get_head_ref_name(worktree), 0);
11266 if (error != NULL)
11267 goto done;
11269 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11270 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11271 "will not edit commit history of a branch outside "
11272 "the \"refs/heads/\" reference namespace");
11273 goto done;
11276 error = got_ref_resolve(&head_commit_id, repo, branch);
11277 got_ref_close(branch);
11278 branch = NULL;
11279 if (error)
11280 goto done;
11282 error = got_object_open_as_commit(&commit, repo,
11283 head_commit_id);
11284 if (error)
11285 goto done;
11286 parent_ids = got_object_commit_get_parent_ids(commit);
11287 pid = STAILQ_FIRST(parent_ids);
11288 if (pid == NULL) {
11289 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11290 goto done;
11292 error = collect_commits(&commits, head_commit_id, &pid->id,
11293 got_worktree_get_base_commit_id(worktree),
11294 got_worktree_get_path_prefix(worktree),
11295 GOT_ERR_HISTEDIT_PATH, repo);
11296 got_object_commit_close(commit);
11297 commit = NULL;
11298 if (error)
11299 goto done;
11301 if (STAILQ_EMPTY(&commits)) {
11302 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
11303 goto done;
11306 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
11307 &base_commit_id, &fileindex, worktree, repo);
11308 if (error)
11309 goto done;
11311 if (edit_script_path) {
11312 error = histedit_load_list(&histedit_cmds,
11313 edit_script_path, repo);
11314 if (error) {
11315 got_worktree_histedit_abort(worktree, fileindex,
11316 repo, branch, base_commit_id,
11317 abort_progress, &upa);
11318 print_merge_progress_stats(&upa);
11319 goto done;
11321 } else {
11322 const char *branch_name;
11323 branch_name = got_ref_get_symref_target(branch);
11324 if (strncmp(branch_name, "refs/heads/", 11) == 0)
11325 branch_name += 11;
11326 error = histedit_edit_script(&histedit_cmds, &commits,
11327 branch_name, edit_logmsg_only, fold_only,
11328 edit_only, repo);
11329 if (error) {
11330 got_worktree_histedit_abort(worktree, fileindex,
11331 repo, branch, base_commit_id,
11332 abort_progress, &upa);
11333 print_merge_progress_stats(&upa);
11334 goto done;
11339 error = histedit_save_list(&histedit_cmds, worktree,
11340 repo);
11341 if (error) {
11342 got_worktree_histedit_abort(worktree, fileindex,
11343 repo, branch, base_commit_id,
11344 abort_progress, &upa);
11345 print_merge_progress_stats(&upa);
11346 goto done;
11351 error = histedit_check_script(&histedit_cmds, &commits, repo);
11352 if (error)
11353 goto done;
11355 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
11356 if (resume_commit_id) {
11357 if (got_object_id_cmp(hle->commit_id,
11358 resume_commit_id) != 0)
11359 continue;
11361 resume_commit_id = NULL;
11362 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
11363 hle->cmd->code == GOT_HISTEDIT_FOLD) {
11364 error = histedit_skip_commit(hle, worktree,
11365 repo);
11366 if (error)
11367 goto done;
11368 } else {
11369 struct got_pathlist_head paths;
11370 int have_changes = 0;
11372 TAILQ_INIT(&paths);
11373 error = got_pathlist_append(&paths, "", NULL);
11374 if (error)
11375 goto done;
11376 error = got_worktree_status(worktree, &paths,
11377 repo, 0, check_local_changes, &have_changes,
11378 check_cancelled, NULL);
11379 got_pathlist_free(&paths);
11380 if (error) {
11381 if (error->code != GOT_ERR_CANCELLED)
11382 goto done;
11383 if (sigint_received || sigpipe_received)
11384 goto done;
11386 if (have_changes) {
11387 error = histedit_commit(NULL, worktree,
11388 fileindex, tmp_branch, hle, repo);
11389 if (error)
11390 goto done;
11391 } else {
11392 error = got_object_open_as_commit(
11393 &commit, repo, hle->commit_id);
11394 if (error)
11395 goto done;
11396 error = show_histedit_progress(commit,
11397 hle, NULL);
11398 got_object_commit_close(commit);
11399 commit = NULL;
11400 if (error)
11401 goto done;
11404 continue;
11407 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
11408 error = histedit_skip_commit(hle, worktree, repo);
11409 if (error)
11410 goto done;
11411 continue;
11414 error = got_object_open_as_commit(&commit, repo,
11415 hle->commit_id);
11416 if (error)
11417 goto done;
11418 parent_ids = got_object_commit_get_parent_ids(commit);
11419 pid = STAILQ_FIRST(parent_ids);
11421 error = got_worktree_histedit_merge_files(&merged_paths,
11422 worktree, fileindex, &pid->id, hle->commit_id, repo,
11423 update_progress, &upa, check_cancelled, NULL);
11424 if (error)
11425 goto done;
11426 got_object_commit_close(commit);
11427 commit = NULL;
11429 print_merge_progress_stats(&upa);
11430 if (upa.conflicts > 0 || upa.missing > 0 ||
11431 upa.not_deleted > 0 || upa.unversioned > 0) {
11432 if (upa.conflicts > 0) {
11433 error = show_rebase_merge_conflict(
11434 hle->commit_id, repo);
11435 if (error)
11436 goto done;
11438 got_worktree_rebase_pathlist_free(&merged_paths);
11439 break;
11442 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
11443 char *id_str;
11444 error = got_object_id_str(&id_str, hle->commit_id);
11445 if (error)
11446 goto done;
11447 printf("Stopping histedit for amending commit %s\n",
11448 id_str);
11449 free(id_str);
11450 got_worktree_rebase_pathlist_free(&merged_paths);
11451 error = got_worktree_histedit_postpone(worktree,
11452 fileindex);
11453 goto done;
11456 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
11457 error = histedit_skip_commit(hle, worktree, repo);
11458 if (error)
11459 goto done;
11460 continue;
11463 error = histedit_commit(&merged_paths, worktree, fileindex,
11464 tmp_branch, hle, repo);
11465 got_worktree_rebase_pathlist_free(&merged_paths);
11466 if (error)
11467 goto done;
11470 if (upa.conflicts > 0 || upa.missing > 0 ||
11471 upa.not_deleted > 0 || upa.unversioned > 0) {
11472 error = got_worktree_histedit_postpone(worktree, fileindex);
11473 if (error)
11474 goto done;
11475 if (upa.conflicts > 0 && upa.missing == 0 &&
11476 upa.not_deleted == 0 && upa.unversioned == 0) {
11477 error = got_error_msg(GOT_ERR_CONFLICTS,
11478 "conflicts must be resolved before histedit "
11479 "can continue");
11480 } else if (upa.conflicts > 0) {
11481 error = got_error_msg(GOT_ERR_CONFLICTS,
11482 "conflicts must be resolved before histedit "
11483 "can continue; changes destined for some "
11484 "files were not yet merged and should be "
11485 "merged manually if required before the "
11486 "histedit operation is continued");
11487 } else {
11488 error = got_error_msg(GOT_ERR_CONFLICTS,
11489 "changes destined for some files were not "
11490 "yet merged and should be merged manually "
11491 "if required before the histedit operation "
11492 "is continued");
11494 } else
11495 error = histedit_complete(worktree, fileindex, tmp_branch,
11496 branch, repo);
11497 done:
11498 got_object_id_queue_free(&commits);
11499 histedit_free_list(&histedit_cmds);
11500 free(head_commit_id);
11501 free(base_commit_id);
11502 free(resume_commit_id);
11503 if (commit)
11504 got_object_commit_close(commit);
11505 if (branch)
11506 got_ref_close(branch);
11507 if (tmp_branch)
11508 got_ref_close(tmp_branch);
11509 if (worktree)
11510 got_worktree_close(worktree);
11511 if (repo) {
11512 const struct got_error *close_err = got_repo_close(repo);
11513 if (error == NULL)
11514 error = close_err;
11516 if (pack_fds) {
11517 const struct got_error *pack_err =
11518 got_repo_pack_fds_close(pack_fds);
11519 if (error == NULL)
11520 error = pack_err;
11522 return error;
11525 __dead static void
11526 usage_integrate(void)
11528 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
11529 exit(1);
11532 static const struct got_error *
11533 cmd_integrate(int argc, char *argv[])
11535 const struct got_error *error = NULL;
11536 struct got_repository *repo = NULL;
11537 struct got_worktree *worktree = NULL;
11538 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
11539 const char *branch_arg = NULL;
11540 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
11541 struct got_fileindex *fileindex = NULL;
11542 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
11543 int ch;
11544 struct got_update_progress_arg upa;
11545 int *pack_fds = NULL;
11547 while ((ch = getopt(argc, argv, "")) != -1) {
11548 switch (ch) {
11549 default:
11550 usage_integrate();
11551 /* NOTREACHED */
11555 argc -= optind;
11556 argv += optind;
11558 if (argc != 1)
11559 usage_integrate();
11560 branch_arg = argv[0];
11561 #ifndef PROFILE
11562 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11563 "unveil", NULL) == -1)
11564 err(1, "pledge");
11565 #endif
11566 cwd = getcwd(NULL, 0);
11567 if (cwd == NULL) {
11568 error = got_error_from_errno("getcwd");
11569 goto done;
11572 error = got_repo_pack_fds_open(&pack_fds);
11573 if (error != NULL)
11574 goto done;
11576 error = got_worktree_open(&worktree, cwd);
11577 if (error) {
11578 if (error->code == GOT_ERR_NOT_WORKTREE)
11579 error = wrap_not_worktree_error(error, "integrate",
11580 cwd);
11581 goto done;
11584 error = check_rebase_or_histedit_in_progress(worktree);
11585 if (error)
11586 goto done;
11588 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
11589 NULL, pack_fds);
11590 if (error != NULL)
11591 goto done;
11593 error = apply_unveil(got_repo_get_path(repo), 0,
11594 got_worktree_get_root_path(worktree));
11595 if (error)
11596 goto done;
11598 error = check_merge_in_progress(worktree, repo);
11599 if (error)
11600 goto done;
11602 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
11603 error = got_error_from_errno("asprintf");
11604 goto done;
11607 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
11608 &base_branch_ref, worktree, refname, repo);
11609 if (error)
11610 goto done;
11612 refname = strdup(got_ref_get_name(branch_ref));
11613 if (refname == NULL) {
11614 error = got_error_from_errno("strdup");
11615 got_worktree_integrate_abort(worktree, fileindex, repo,
11616 branch_ref, base_branch_ref);
11617 goto done;
11619 base_refname = strdup(got_ref_get_name(base_branch_ref));
11620 if (base_refname == NULL) {
11621 error = got_error_from_errno("strdup");
11622 got_worktree_integrate_abort(worktree, fileindex, repo,
11623 branch_ref, base_branch_ref);
11624 goto done;
11627 error = got_ref_resolve(&commit_id, repo, branch_ref);
11628 if (error)
11629 goto done;
11631 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
11632 if (error)
11633 goto done;
11635 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
11636 error = got_error_msg(GOT_ERR_SAME_BRANCH,
11637 "specified branch has already been integrated");
11638 got_worktree_integrate_abort(worktree, fileindex, repo,
11639 branch_ref, base_branch_ref);
11640 goto done;
11643 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
11644 if (error) {
11645 if (error->code == GOT_ERR_ANCESTRY)
11646 error = got_error(GOT_ERR_REBASE_REQUIRED);
11647 got_worktree_integrate_abort(worktree, fileindex, repo,
11648 branch_ref, base_branch_ref);
11649 goto done;
11652 memset(&upa, 0, sizeof(upa));
11653 error = got_worktree_integrate_continue(worktree, fileindex, repo,
11654 branch_ref, base_branch_ref, update_progress, &upa,
11655 check_cancelled, NULL);
11656 if (error)
11657 goto done;
11659 printf("Integrated %s into %s\n", refname, base_refname);
11660 print_update_progress_stats(&upa);
11661 done:
11662 if (repo) {
11663 const struct got_error *close_err = got_repo_close(repo);
11664 if (error == NULL)
11665 error = close_err;
11667 if (worktree)
11668 got_worktree_close(worktree);
11669 if (pack_fds) {
11670 const struct got_error *pack_err =
11671 got_repo_pack_fds_close(pack_fds);
11672 if (error == NULL)
11673 error = pack_err;
11675 free(cwd);
11676 free(base_commit_id);
11677 free(commit_id);
11678 free(refname);
11679 free(base_refname);
11680 return error;
11683 __dead static void
11684 usage_merge(void)
11686 fprintf(stderr, "usage: %s merge [-a] [-c] [-n] [branch]\n",
11687 getprogname());
11688 exit(1);
11691 static const struct got_error *
11692 cmd_merge(int argc, char *argv[])
11694 const struct got_error *error = NULL;
11695 struct got_worktree *worktree = NULL;
11696 struct got_repository *repo = NULL;
11697 struct got_fileindex *fileindex = NULL;
11698 char *cwd = NULL, *id_str = NULL, *author = NULL;
11699 struct got_reference *branch = NULL, *wt_branch = NULL;
11700 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
11701 struct got_object_id *wt_branch_tip = NULL;
11702 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
11703 int interrupt_merge = 0;
11704 struct got_update_progress_arg upa;
11705 struct got_object_id *merge_commit_id = NULL;
11706 char *branch_name = NULL;
11707 int *pack_fds = NULL;
11709 memset(&upa, 0, sizeof(upa));
11711 while ((ch = getopt(argc, argv, "acn")) != -1) {
11712 switch (ch) {
11713 case 'a':
11714 abort_merge = 1;
11715 break;
11716 case 'c':
11717 continue_merge = 1;
11718 break;
11719 case 'n':
11720 interrupt_merge = 1;
11721 break;
11722 default:
11723 usage_rebase();
11724 /* NOTREACHED */
11728 argc -= optind;
11729 argv += optind;
11731 #ifndef PROFILE
11732 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11733 "unveil", NULL) == -1)
11734 err(1, "pledge");
11735 #endif
11737 if (abort_merge && continue_merge)
11738 option_conflict('a', 'c');
11739 if (abort_merge || continue_merge) {
11740 if (argc != 0)
11741 usage_merge();
11742 } else if (argc != 1)
11743 usage_merge();
11745 cwd = getcwd(NULL, 0);
11746 if (cwd == NULL) {
11747 error = got_error_from_errno("getcwd");
11748 goto done;
11751 error = got_repo_pack_fds_open(&pack_fds);
11752 if (error != NULL)
11753 goto done;
11755 error = got_worktree_open(&worktree, cwd);
11756 if (error) {
11757 if (error->code == GOT_ERR_NOT_WORKTREE)
11758 error = wrap_not_worktree_error(error,
11759 "merge", cwd);
11760 goto done;
11763 error = got_repo_open(&repo,
11764 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
11765 pack_fds);
11766 if (error != NULL)
11767 goto done;
11769 error = apply_unveil(got_repo_get_path(repo), 0,
11770 worktree ? got_worktree_get_root_path(worktree) : NULL);
11771 if (error)
11772 goto done;
11774 error = check_rebase_or_histedit_in_progress(worktree);
11775 if (error)
11776 goto done;
11778 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
11779 repo);
11780 if (error)
11781 goto done;
11783 if (abort_merge) {
11784 if (!merge_in_progress) {
11785 error = got_error(GOT_ERR_NOT_MERGING);
11786 goto done;
11788 error = got_worktree_merge_continue(&branch_name,
11789 &branch_tip, &fileindex, worktree, repo);
11790 if (error)
11791 goto done;
11792 error = got_worktree_merge_abort(worktree, fileindex, repo,
11793 abort_progress, &upa);
11794 if (error)
11795 goto done;
11796 printf("Merge of %s aborted\n", branch_name);
11797 goto done; /* nothing else to do */
11800 error = get_author(&author, repo, worktree);
11801 if (error)
11802 goto done;
11804 if (continue_merge) {
11805 if (!merge_in_progress) {
11806 error = got_error(GOT_ERR_NOT_MERGING);
11807 goto done;
11809 error = got_worktree_merge_continue(&branch_name,
11810 &branch_tip, &fileindex, worktree, repo);
11811 if (error)
11812 goto done;
11813 } else {
11814 error = got_ref_open(&branch, repo, argv[0], 0);
11815 if (error != NULL)
11816 goto done;
11817 branch_name = strdup(got_ref_get_name(branch));
11818 if (branch_name == NULL) {
11819 error = got_error_from_errno("strdup");
11820 goto done;
11822 error = got_ref_resolve(&branch_tip, repo, branch);
11823 if (error)
11824 goto done;
11827 error = got_ref_open(&wt_branch, repo,
11828 got_worktree_get_head_ref_name(worktree), 0);
11829 if (error)
11830 goto done;
11831 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
11832 if (error)
11833 goto done;
11834 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11835 wt_branch_tip, branch_tip, 0, repo,
11836 check_cancelled, NULL);
11837 if (error && error->code != GOT_ERR_ANCESTRY)
11838 goto done;
11840 if (!continue_merge) {
11841 error = check_path_prefix(wt_branch_tip, branch_tip,
11842 got_worktree_get_path_prefix(worktree),
11843 GOT_ERR_MERGE_PATH, repo);
11844 if (error)
11845 goto done;
11846 if (yca_id) {
11847 error = check_same_branch(wt_branch_tip, branch,
11848 yca_id, repo);
11849 if (error) {
11850 if (error->code != GOT_ERR_ANCESTRY)
11851 goto done;
11852 error = NULL;
11853 } else {
11854 static char msg[512];
11855 snprintf(msg, sizeof(msg),
11856 "cannot create a merge commit because "
11857 "%s is based on %s; %s can be integrated "
11858 "with 'got integrate' instead", branch_name,
11859 got_worktree_get_head_ref_name(worktree),
11860 branch_name);
11861 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
11862 goto done;
11865 error = got_worktree_merge_prepare(&fileindex, worktree,
11866 branch, repo);
11867 if (error)
11868 goto done;
11870 error = got_worktree_merge_branch(worktree, fileindex,
11871 yca_id, branch_tip, repo, update_progress, &upa,
11872 check_cancelled, NULL);
11873 if (error)
11874 goto done;
11875 print_merge_progress_stats(&upa);
11876 if (!upa.did_something) {
11877 error = got_worktree_merge_abort(worktree, fileindex,
11878 repo, abort_progress, &upa);
11879 if (error)
11880 goto done;
11881 printf("Already up-to-date\n");
11882 goto done;
11886 if (interrupt_merge) {
11887 error = got_worktree_merge_postpone(worktree, fileindex);
11888 if (error)
11889 goto done;
11890 printf("Merge of %s interrupted on request\n", branch_name);
11891 } else if (upa.conflicts > 0 || upa.missing > 0 ||
11892 upa.not_deleted > 0 || upa.unversioned > 0) {
11893 error = got_worktree_merge_postpone(worktree, fileindex);
11894 if (error)
11895 goto done;
11896 if (upa.conflicts > 0 && upa.missing == 0 &&
11897 upa.not_deleted == 0 && upa.unversioned == 0) {
11898 error = got_error_msg(GOT_ERR_CONFLICTS,
11899 "conflicts must be resolved before merging "
11900 "can continue");
11901 } else if (upa.conflicts > 0) {
11902 error = got_error_msg(GOT_ERR_CONFLICTS,
11903 "conflicts must be resolved before merging "
11904 "can continue; changes destined for some "
11905 "files were not yet merged and "
11906 "should be merged manually if required before the "
11907 "merge operation is continued");
11908 } else {
11909 error = got_error_msg(GOT_ERR_CONFLICTS,
11910 "changes destined for some "
11911 "files were not yet merged and should be "
11912 "merged manually if required before the "
11913 "merge operation is continued");
11915 goto done;
11916 } else {
11917 error = got_worktree_merge_commit(&merge_commit_id, worktree,
11918 fileindex, author, NULL, 1, branch_tip, branch_name,
11919 repo, continue_merge ? print_status : NULL, NULL);
11920 if (error)
11921 goto done;
11922 error = got_worktree_merge_complete(worktree, fileindex, repo);
11923 if (error)
11924 goto done;
11925 error = got_object_id_str(&id_str, merge_commit_id);
11926 if (error)
11927 goto done;
11928 printf("Merged %s into %s: %s\n", branch_name,
11929 got_worktree_get_head_ref_name(worktree),
11930 id_str);
11933 done:
11934 free(id_str);
11935 free(merge_commit_id);
11936 free(author);
11937 free(branch_tip);
11938 free(branch_name);
11939 free(yca_id);
11940 if (branch)
11941 got_ref_close(branch);
11942 if (wt_branch)
11943 got_ref_close(wt_branch);
11944 if (worktree)
11945 got_worktree_close(worktree);
11946 if (repo) {
11947 const struct got_error *close_err = got_repo_close(repo);
11948 if (error == NULL)
11949 error = close_err;
11951 if (pack_fds) {
11952 const struct got_error *pack_err =
11953 got_repo_pack_fds_close(pack_fds);
11954 if (error == NULL)
11955 error = pack_err;
11957 return error;
11960 __dead static void
11961 usage_stage(void)
11963 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
11964 "[-S] [file-path ...]\n",
11965 getprogname());
11966 exit(1);
11969 static const struct got_error *
11970 print_stage(void *arg, unsigned char status, unsigned char staged_status,
11971 const char *path, struct got_object_id *blob_id,
11972 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
11973 int dirfd, const char *de_name)
11975 const struct got_error *err = NULL;
11976 char *id_str = NULL;
11978 if (staged_status != GOT_STATUS_ADD &&
11979 staged_status != GOT_STATUS_MODIFY &&
11980 staged_status != GOT_STATUS_DELETE)
11981 return NULL;
11983 if (staged_status == GOT_STATUS_ADD ||
11984 staged_status == GOT_STATUS_MODIFY)
11985 err = got_object_id_str(&id_str, staged_blob_id);
11986 else
11987 err = got_object_id_str(&id_str, blob_id);
11988 if (err)
11989 return err;
11991 printf("%s %c %s\n", id_str, staged_status, path);
11992 free(id_str);
11993 return NULL;
11996 static const struct got_error *
11997 cmd_stage(int argc, char *argv[])
11999 const struct got_error *error = NULL;
12000 struct got_repository *repo = NULL;
12001 struct got_worktree *worktree = NULL;
12002 char *cwd = NULL;
12003 struct got_pathlist_head paths;
12004 struct got_pathlist_entry *pe;
12005 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
12006 FILE *patch_script_file = NULL;
12007 const char *patch_script_path = NULL;
12008 struct choose_patch_arg cpa;
12009 int *pack_fds = NULL;
12011 TAILQ_INIT(&paths);
12013 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
12014 switch (ch) {
12015 case 'l':
12016 list_stage = 1;
12017 break;
12018 case 'p':
12019 pflag = 1;
12020 break;
12021 case 'F':
12022 patch_script_path = optarg;
12023 break;
12024 case 'S':
12025 allow_bad_symlinks = 1;
12026 break;
12027 default:
12028 usage_stage();
12029 /* NOTREACHED */
12033 argc -= optind;
12034 argv += optind;
12036 #ifndef PROFILE
12037 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12038 "unveil", NULL) == -1)
12039 err(1, "pledge");
12040 #endif
12041 if (list_stage && (pflag || patch_script_path))
12042 errx(1, "-l option cannot be used with other options");
12043 if (patch_script_path && !pflag)
12044 errx(1, "-F option can only be used together with -p option");
12046 cwd = getcwd(NULL, 0);
12047 if (cwd == NULL) {
12048 error = got_error_from_errno("getcwd");
12049 goto done;
12052 error = got_repo_pack_fds_open(&pack_fds);
12053 if (error != NULL)
12054 goto done;
12056 error = got_worktree_open(&worktree, cwd);
12057 if (error) {
12058 if (error->code == GOT_ERR_NOT_WORKTREE)
12059 error = wrap_not_worktree_error(error, "stage", cwd);
12060 goto done;
12063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12064 NULL, pack_fds);
12065 if (error != NULL)
12066 goto done;
12068 if (patch_script_path) {
12069 patch_script_file = fopen(patch_script_path, "re");
12070 if (patch_script_file == NULL) {
12071 error = got_error_from_errno2("fopen",
12072 patch_script_path);
12073 goto done;
12076 error = apply_unveil(got_repo_get_path(repo), 0,
12077 got_worktree_get_root_path(worktree));
12078 if (error)
12079 goto done;
12081 error = check_merge_in_progress(worktree, repo);
12082 if (error)
12083 goto done;
12085 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12086 if (error)
12087 goto done;
12089 if (list_stage)
12090 error = got_worktree_status(worktree, &paths, repo, 0,
12091 print_stage, NULL, check_cancelled, NULL);
12092 else {
12093 cpa.patch_script_file = patch_script_file;
12094 cpa.action = "stage";
12095 error = got_worktree_stage(worktree, &paths,
12096 pflag ? NULL : print_status, NULL,
12097 pflag ? choose_patch : NULL, &cpa,
12098 allow_bad_symlinks, repo);
12100 done:
12101 if (patch_script_file && fclose(patch_script_file) == EOF &&
12102 error == NULL)
12103 error = got_error_from_errno2("fclose", patch_script_path);
12104 if (repo) {
12105 const struct got_error *close_err = got_repo_close(repo);
12106 if (error == NULL)
12107 error = close_err;
12109 if (worktree)
12110 got_worktree_close(worktree);
12111 if (pack_fds) {
12112 const struct got_error *pack_err =
12113 got_repo_pack_fds_close(pack_fds);
12114 if (error == NULL)
12115 error = pack_err;
12117 TAILQ_FOREACH(pe, &paths, entry)
12118 free((char *)pe->path);
12119 got_pathlist_free(&paths);
12120 free(cwd);
12121 return error;
12124 __dead static void
12125 usage_unstage(void)
12127 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
12128 "[file-path ...]\n",
12129 getprogname());
12130 exit(1);
12134 static const struct got_error *
12135 cmd_unstage(int argc, char *argv[])
12137 const struct got_error *error = NULL;
12138 struct got_repository *repo = NULL;
12139 struct got_worktree *worktree = NULL;
12140 char *cwd = NULL;
12141 struct got_pathlist_head paths;
12142 struct got_pathlist_entry *pe;
12143 int ch, pflag = 0;
12144 struct got_update_progress_arg upa;
12145 FILE *patch_script_file = NULL;
12146 const char *patch_script_path = NULL;
12147 struct choose_patch_arg cpa;
12148 int *pack_fds = NULL;
12150 TAILQ_INIT(&paths);
12152 while ((ch = getopt(argc, argv, "pF:")) != -1) {
12153 switch (ch) {
12154 case 'p':
12155 pflag = 1;
12156 break;
12157 case 'F':
12158 patch_script_path = optarg;
12159 break;
12160 default:
12161 usage_unstage();
12162 /* NOTREACHED */
12166 argc -= optind;
12167 argv += optind;
12169 #ifndef PROFILE
12170 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12171 "unveil", NULL) == -1)
12172 err(1, "pledge");
12173 #endif
12174 if (patch_script_path && !pflag)
12175 errx(1, "-F option can only be used together with -p option");
12177 cwd = getcwd(NULL, 0);
12178 if (cwd == NULL) {
12179 error = got_error_from_errno("getcwd");
12180 goto done;
12183 error = got_repo_pack_fds_open(&pack_fds);
12184 if (error != NULL)
12185 goto done;
12187 error = got_worktree_open(&worktree, cwd);
12188 if (error) {
12189 if (error->code == GOT_ERR_NOT_WORKTREE)
12190 error = wrap_not_worktree_error(error, "unstage", cwd);
12191 goto done;
12194 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12195 NULL, pack_fds);
12196 if (error != NULL)
12197 goto done;
12199 if (patch_script_path) {
12200 patch_script_file = fopen(patch_script_path, "re");
12201 if (patch_script_file == NULL) {
12202 error = got_error_from_errno2("fopen",
12203 patch_script_path);
12204 goto done;
12208 error = apply_unveil(got_repo_get_path(repo), 0,
12209 got_worktree_get_root_path(worktree));
12210 if (error)
12211 goto done;
12213 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
12214 if (error)
12215 goto done;
12217 cpa.patch_script_file = patch_script_file;
12218 cpa.action = "unstage";
12219 memset(&upa, 0, sizeof(upa));
12220 error = got_worktree_unstage(worktree, &paths, update_progress,
12221 &upa, pflag ? choose_patch : NULL, &cpa, repo);
12222 if (!error)
12223 print_merge_progress_stats(&upa);
12224 done:
12225 if (patch_script_file && fclose(patch_script_file) == EOF &&
12226 error == NULL)
12227 error = got_error_from_errno2("fclose", patch_script_path);
12228 if (repo) {
12229 const struct got_error *close_err = got_repo_close(repo);
12230 if (error == NULL)
12231 error = close_err;
12233 if (worktree)
12234 got_worktree_close(worktree);
12235 if (pack_fds) {
12236 const struct got_error *pack_err =
12237 got_repo_pack_fds_close(pack_fds);
12238 if (error == NULL)
12239 error = pack_err;
12241 TAILQ_FOREACH(pe, &paths, entry)
12242 free((char *)pe->path);
12243 got_pathlist_free(&paths);
12244 free(cwd);
12245 return error;
12248 __dead static void
12249 usage_cat(void)
12251 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
12252 "arg1 [arg2 ...]\n", getprogname());
12253 exit(1);
12256 static const struct got_error *
12257 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12259 const struct got_error *err;
12260 struct got_blob_object *blob;
12261 int fd = -1;
12263 fd = got_opentempfd();
12264 if (fd == -1)
12265 return got_error_from_errno("got_opentempfd");
12267 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
12268 if (err)
12269 goto done;
12271 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
12272 done:
12273 if (fd != -1 && close(fd) == -1 && err == NULL)
12274 err = got_error_from_errno("close");
12275 if (blob)
12276 got_object_blob_close(blob);
12277 return err;
12280 static const struct got_error *
12281 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12283 const struct got_error *err;
12284 struct got_tree_object *tree;
12285 int nentries, i;
12287 err = got_object_open_as_tree(&tree, repo, id);
12288 if (err)
12289 return err;
12291 nentries = got_object_tree_get_nentries(tree);
12292 for (i = 0; i < nentries; i++) {
12293 struct got_tree_entry *te;
12294 char *id_str;
12295 if (sigint_received || sigpipe_received)
12296 break;
12297 te = got_object_tree_get_entry(tree, i);
12298 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
12299 if (err)
12300 break;
12301 fprintf(outfile, "%s %.7o %s\n", id_str,
12302 got_tree_entry_get_mode(te),
12303 got_tree_entry_get_name(te));
12304 free(id_str);
12307 got_object_tree_close(tree);
12308 return err;
12311 static void
12312 format_gmtoff(char *buf, size_t sz, time_t gmtoff)
12314 long long h, m;
12315 char sign = '+';
12317 if (gmtoff < 0) {
12318 sign = '-';
12319 gmtoff = -gmtoff;
12322 h = (long long)gmtoff / 3600;
12323 m = ((long long)gmtoff - h*3600) / 60;
12324 snprintf(buf, sz, "%c%02lld%02lld", sign, h, m);
12327 static const struct got_error *
12328 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12330 const struct got_error *err;
12331 struct got_commit_object *commit;
12332 const struct got_object_id_queue *parent_ids;
12333 struct got_object_qid *pid;
12334 char *id_str = NULL;
12335 const char *logmsg = NULL;
12336 char gmtoff[6];
12338 err = got_object_open_as_commit(&commit, repo, id);
12339 if (err)
12340 return err;
12342 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
12343 if (err)
12344 goto done;
12346 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
12347 parent_ids = got_object_commit_get_parent_ids(commit);
12348 fprintf(outfile, "numparents %d\n",
12349 got_object_commit_get_nparents(commit));
12350 STAILQ_FOREACH(pid, parent_ids, entry) {
12351 char *pid_str;
12352 err = got_object_id_str(&pid_str, &pid->id);
12353 if (err)
12354 goto done;
12355 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
12356 free(pid_str);
12358 format_gmtoff(gmtoff, sizeof(gmtoff),
12359 got_object_commit_get_author_gmtoff(commit));
12360 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
12361 got_object_commit_get_author(commit),
12362 (long long)got_object_commit_get_author_time(commit),
12363 gmtoff);
12365 format_gmtoff(gmtoff, sizeof(gmtoff),
12366 got_object_commit_get_committer_gmtoff(commit));
12367 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
12368 got_object_commit_get_author(commit),
12369 (long long)got_object_commit_get_committer_time(commit),
12370 gmtoff);
12372 logmsg = got_object_commit_get_logmsg_raw(commit);
12373 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
12374 fprintf(outfile, "%s", logmsg);
12375 done:
12376 free(id_str);
12377 got_object_commit_close(commit);
12378 return err;
12381 static const struct got_error *
12382 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
12384 const struct got_error *err;
12385 struct got_tag_object *tag;
12386 char *id_str = NULL;
12387 const char *tagmsg = NULL;
12388 char gmtoff[6];
12390 err = got_object_open_as_tag(&tag, repo, id);
12391 if (err)
12392 return err;
12394 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
12395 if (err)
12396 goto done;
12398 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
12400 switch (got_object_tag_get_object_type(tag)) {
12401 case GOT_OBJ_TYPE_BLOB:
12402 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12403 GOT_OBJ_LABEL_BLOB);
12404 break;
12405 case GOT_OBJ_TYPE_TREE:
12406 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12407 GOT_OBJ_LABEL_TREE);
12408 break;
12409 case GOT_OBJ_TYPE_COMMIT:
12410 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12411 GOT_OBJ_LABEL_COMMIT);
12412 break;
12413 case GOT_OBJ_TYPE_TAG:
12414 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
12415 GOT_OBJ_LABEL_TAG);
12416 break;
12417 default:
12418 break;
12421 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
12422 got_object_tag_get_name(tag));
12424 format_gmtoff(gmtoff, sizeof(gmtoff),
12425 got_object_tag_get_tagger_gmtoff(tag));
12426 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
12427 got_object_tag_get_tagger(tag),
12428 (long long)got_object_tag_get_tagger_time(tag),
12429 gmtoff);
12431 tagmsg = got_object_tag_get_message(tag);
12432 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
12433 fprintf(outfile, "%s", tagmsg);
12434 done:
12435 free(id_str);
12436 got_object_tag_close(tag);
12437 return err;
12440 static const struct got_error *
12441 cmd_cat(int argc, char *argv[])
12443 const struct got_error *error;
12444 struct got_repository *repo = NULL;
12445 struct got_worktree *worktree = NULL;
12446 char *cwd = NULL, *repo_path = NULL, *label = NULL;
12447 const char *commit_id_str = NULL;
12448 struct got_object_id *id = NULL, *commit_id = NULL;
12449 struct got_commit_object *commit = NULL;
12450 int ch, obj_type, i, force_path = 0;
12451 struct got_reflist_head refs;
12452 int *pack_fds = NULL;
12454 TAILQ_INIT(&refs);
12456 #ifndef PROFILE
12457 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12458 NULL) == -1)
12459 err(1, "pledge");
12460 #endif
12462 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
12463 switch (ch) {
12464 case 'c':
12465 commit_id_str = optarg;
12466 break;
12467 case 'r':
12468 repo_path = realpath(optarg, NULL);
12469 if (repo_path == NULL)
12470 return got_error_from_errno2("realpath",
12471 optarg);
12472 got_path_strip_trailing_slashes(repo_path);
12473 break;
12474 case 'P':
12475 force_path = 1;
12476 break;
12477 default:
12478 usage_cat();
12479 /* NOTREACHED */
12483 argc -= optind;
12484 argv += optind;
12486 cwd = getcwd(NULL, 0);
12487 if (cwd == NULL) {
12488 error = got_error_from_errno("getcwd");
12489 goto done;
12492 error = got_repo_pack_fds_open(&pack_fds);
12493 if (error != NULL)
12494 goto done;
12496 if (repo_path == NULL) {
12497 error = got_worktree_open(&worktree, cwd);
12498 if (error && error->code != GOT_ERR_NOT_WORKTREE)
12499 goto done;
12500 if (worktree) {
12501 repo_path = strdup(
12502 got_worktree_get_repo_path(worktree));
12503 if (repo_path == NULL) {
12504 error = got_error_from_errno("strdup");
12505 goto done;
12508 /* Release work tree lock. */
12509 got_worktree_close(worktree);
12510 worktree = NULL;
12514 if (repo_path == NULL) {
12515 repo_path = strdup(cwd);
12516 if (repo_path == NULL)
12517 return got_error_from_errno("strdup");
12520 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
12521 free(repo_path);
12522 if (error != NULL)
12523 goto done;
12525 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
12526 if (error)
12527 goto done;
12529 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
12530 if (error)
12531 goto done;
12533 if (commit_id_str == NULL)
12534 commit_id_str = GOT_REF_HEAD;
12535 error = got_repo_match_object_id(&commit_id, NULL,
12536 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
12537 if (error)
12538 goto done;
12540 error = got_object_open_as_commit(&commit, repo, commit_id);
12541 if (error)
12542 goto done;
12544 for (i = 0; i < argc; i++) {
12545 if (force_path) {
12546 error = got_object_id_by_path(&id, repo, commit,
12547 argv[i]);
12548 if (error)
12549 break;
12550 } else {
12551 error = got_repo_match_object_id(&id, &label, argv[i],
12552 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
12553 repo);
12554 if (error) {
12555 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
12556 error->code != GOT_ERR_NOT_REF)
12557 break;
12558 error = got_object_id_by_path(&id, repo,
12559 commit, argv[i]);
12560 if (error)
12561 break;
12565 error = got_object_get_type(&obj_type, repo, id);
12566 if (error)
12567 break;
12569 switch (obj_type) {
12570 case GOT_OBJ_TYPE_BLOB:
12571 error = cat_blob(id, repo, stdout);
12572 break;
12573 case GOT_OBJ_TYPE_TREE:
12574 error = cat_tree(id, repo, stdout);
12575 break;
12576 case GOT_OBJ_TYPE_COMMIT:
12577 error = cat_commit(id, repo, stdout);
12578 break;
12579 case GOT_OBJ_TYPE_TAG:
12580 error = cat_tag(id, repo, stdout);
12581 break;
12582 default:
12583 error = got_error(GOT_ERR_OBJ_TYPE);
12584 break;
12586 if (error)
12587 break;
12588 free(label);
12589 label = NULL;
12590 free(id);
12591 id = NULL;
12593 done:
12594 free(label);
12595 free(id);
12596 free(commit_id);
12597 if (commit)
12598 got_object_commit_close(commit);
12599 if (worktree)
12600 got_worktree_close(worktree);
12601 if (repo) {
12602 const struct got_error *close_err = got_repo_close(repo);
12603 if (error == NULL)
12604 error = close_err;
12606 if (pack_fds) {
12607 const struct got_error *pack_err =
12608 got_repo_pack_fds_close(pack_fds);
12609 if (error == NULL)
12610 error = pack_err;
12613 got_ref_list_free(&refs);
12614 return error;
12617 __dead static void
12618 usage_info(void)
12620 fprintf(stderr, "usage: %s info [path ...]\n",
12621 getprogname());
12622 exit(1);
12625 static const struct got_error *
12626 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
12627 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12628 struct got_object_id *commit_id)
12630 const struct got_error *err = NULL;
12631 char *id_str = NULL;
12632 char datebuf[128];
12633 struct tm mytm, *tm;
12634 struct got_pathlist_head *paths = arg;
12635 struct got_pathlist_entry *pe;
12638 * Clear error indication from any of the path arguments which
12639 * would cause this file index entry to be displayed.
12641 TAILQ_FOREACH(pe, paths, entry) {
12642 if (got_path_cmp(path, pe->path, strlen(path),
12643 pe->path_len) == 0 ||
12644 got_path_is_child(path, pe->path, pe->path_len))
12645 pe->data = NULL; /* no error */
12648 printf(GOT_COMMIT_SEP_STR);
12649 if (S_ISLNK(mode))
12650 printf("symlink: %s\n", path);
12651 else if (S_ISREG(mode)) {
12652 printf("file: %s\n", path);
12653 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
12654 } else if (S_ISDIR(mode))
12655 printf("directory: %s\n", path);
12656 else
12657 printf("something: %s\n", path);
12659 tm = localtime_r(&mtime, &mytm);
12660 if (tm == NULL)
12661 return NULL;
12662 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
12663 return got_error(GOT_ERR_NO_SPACE);
12664 printf("timestamp: %s\n", datebuf);
12666 if (blob_id) {
12667 err = got_object_id_str(&id_str, blob_id);
12668 if (err)
12669 return err;
12670 printf("based on blob: %s\n", id_str);
12671 free(id_str);
12674 if (staged_blob_id) {
12675 err = got_object_id_str(&id_str, staged_blob_id);
12676 if (err)
12677 return err;
12678 printf("based on staged blob: %s\n", id_str);
12679 free(id_str);
12682 if (commit_id) {
12683 err = got_object_id_str(&id_str, commit_id);
12684 if (err)
12685 return err;
12686 printf("based on commit: %s\n", id_str);
12687 free(id_str);
12690 return NULL;
12693 static const struct got_error *
12694 cmd_info(int argc, char *argv[])
12696 const struct got_error *error = NULL;
12697 struct got_worktree *worktree = NULL;
12698 char *cwd = NULL, *id_str = NULL;
12699 struct got_pathlist_head paths;
12700 struct got_pathlist_entry *pe;
12701 char *uuidstr = NULL;
12702 int ch, show_files = 0;
12703 int *pack_fds = NULL;
12705 TAILQ_INIT(&paths);
12707 while ((ch = getopt(argc, argv, "")) != -1) {
12708 switch (ch) {
12709 default:
12710 usage_info();
12711 /* NOTREACHED */
12715 argc -= optind;
12716 argv += optind;
12718 #ifndef PROFILE
12719 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
12720 NULL) == -1)
12721 err(1, "pledge");
12722 #endif
12723 cwd = getcwd(NULL, 0);
12724 if (cwd == NULL) {
12725 error = got_error_from_errno("getcwd");
12726 goto done;
12729 error = got_repo_pack_fds_open(&pack_fds);
12730 if (error != NULL)
12731 goto done;
12733 error = got_worktree_open(&worktree, cwd);
12734 if (error) {
12735 if (error->code == GOT_ERR_NOT_WORKTREE)
12736 error = wrap_not_worktree_error(error, "info", cwd);
12737 goto done;
12740 #ifndef PROFILE
12741 /* Remove "cpath" promise. */
12742 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
12743 NULL) == -1)
12744 err(1, "pledge");
12745 #endif
12746 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
12747 if (error)
12748 goto done;
12750 if (argc >= 1) {
12751 error = get_worktree_paths_from_argv(&paths, argc, argv,
12752 worktree);
12753 if (error)
12754 goto done;
12755 show_files = 1;
12758 error = got_object_id_str(&id_str,
12759 got_worktree_get_base_commit_id(worktree));
12760 if (error)
12761 goto done;
12763 error = got_worktree_get_uuid(&uuidstr, worktree);
12764 if (error)
12765 goto done;
12767 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
12768 printf("work tree base commit: %s\n", id_str);
12769 printf("work tree path prefix: %s\n",
12770 got_worktree_get_path_prefix(worktree));
12771 printf("work tree branch reference: %s\n",
12772 got_worktree_get_head_ref_name(worktree));
12773 printf("work tree UUID: %s\n", uuidstr);
12774 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
12776 if (show_files) {
12777 struct got_pathlist_entry *pe;
12778 TAILQ_FOREACH(pe, &paths, entry) {
12779 if (pe->path_len == 0)
12780 continue;
12782 * Assume this path will fail. This will be corrected
12783 * in print_path_info() in case the path does suceeed.
12785 pe->data = (void *)got_error_path(pe->path,
12786 GOT_ERR_BAD_PATH);
12788 error = got_worktree_path_info(worktree, &paths,
12789 print_path_info, &paths, check_cancelled, NULL);
12790 if (error)
12791 goto done;
12792 TAILQ_FOREACH(pe, &paths, entry) {
12793 if (pe->data != NULL) {
12794 error = pe->data; /* bad path */
12795 break;
12799 done:
12800 if (pack_fds) {
12801 const struct got_error *pack_err =
12802 got_repo_pack_fds_close(pack_fds);
12803 if (error == NULL)
12804 error = pack_err;
12806 TAILQ_FOREACH(pe, &paths, entry)
12807 free((char *)pe->path);
12808 got_pathlist_free(&paths);
12809 free(cwd);
12810 free(id_str);
12811 free(uuidstr);
12812 return error;