2 417a6e49 2020-09-26 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 417a6e49 2020-09-26 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 417a6e49 2020-09-26 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 417a6e49 2020-09-26 stsp * Permission to use, copy, modify, and distribute this software for any
7 417a6e49 2020-09-26 stsp * purpose with or without fee is hereby granted, provided that the above
8 417a6e49 2020-09-26 stsp * copyright notice and this permission notice appear in all copies.
10 417a6e49 2020-09-26 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 417a6e49 2020-09-26 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 417a6e49 2020-09-26 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 417a6e49 2020-09-26 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 417a6e49 2020-09-26 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 417a6e49 2020-09-26 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 417a6e49 2020-09-26 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 417a6e49 2020-09-26 stsp #include <sys/queue.h>
20 417a6e49 2020-09-26 stsp #include <sys/types.h>
21 417a6e49 2020-09-26 stsp #include <sys/stat.h>
22 417a6e49 2020-09-26 stsp #include <sys/param.h>
23 417a6e49 2020-09-26 stsp #include <sys/wait.h>
25 417a6e49 2020-09-26 stsp #include <err.h>
26 417a6e49 2020-09-26 stsp #include <errno.h>
27 417a6e49 2020-09-26 stsp #include <fcntl.h>
28 417a6e49 2020-09-26 stsp #include <limits.h>
29 417a6e49 2020-09-26 stsp #include <locale.h>
30 417a6e49 2020-09-26 stsp #include <ctype.h>
31 417a6e49 2020-09-26 stsp #include <signal.h>
32 417a6e49 2020-09-26 stsp #include <stdio.h>
33 417a6e49 2020-09-26 stsp #include <stdlib.h>
34 417a6e49 2020-09-26 stsp #include <string.h>
35 417a6e49 2020-09-26 stsp #include <unistd.h>
36 417a6e49 2020-09-26 stsp #include <libgen.h>
37 417a6e49 2020-09-26 stsp #include <time.h>
38 417a6e49 2020-09-26 stsp #include <paths.h>
39 417a6e49 2020-09-26 stsp #include <regex.h>
40 417a6e49 2020-09-26 stsp #include <getopt.h>
41 417a6e49 2020-09-26 stsp #include <util.h>
43 417a6e49 2020-09-26 stsp #include "got_version.h"
44 417a6e49 2020-09-26 stsp #include "got_error.h"
45 417a6e49 2020-09-26 stsp #include "got_object.h"
46 417a6e49 2020-09-26 stsp #include "got_reference.h"
47 417a6e49 2020-09-26 stsp #include "got_repository.h"
48 417a6e49 2020-09-26 stsp #include "got_path.h"
49 417a6e49 2020-09-26 stsp #include "got_cancel.h"
50 417a6e49 2020-09-26 stsp #include "got_worktree.h"
51 417a6e49 2020-09-26 stsp #include "got_diff.h"
52 417a6e49 2020-09-26 stsp #include "got_commit_graph.h"
53 417a6e49 2020-09-26 stsp #include "got_fetch.h"
54 417a6e49 2020-09-26 stsp #include "got_blame.h"
55 417a6e49 2020-09-26 stsp #include "got_privsep.h"
56 417a6e49 2020-09-26 stsp #include "got_opentemp.h"
57 417a6e49 2020-09-26 stsp #include "got_gotconfig.h"
59 417a6e49 2020-09-26 stsp #ifndef nitems
60 417a6e49 2020-09-26 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 417a6e49 2020-09-26 stsp static volatile sig_atomic_t sigint_received;
64 417a6e49 2020-09-26 stsp static volatile sig_atomic_t sigpipe_received;
67 417a6e49 2020-09-26 stsp catch_sigint(int signo)
69 417a6e49 2020-09-26 stsp sigint_received = 1;
73 417a6e49 2020-09-26 stsp catch_sigpipe(int signo)
75 417a6e49 2020-09-26 stsp sigpipe_received = 1;
79 417a6e49 2020-09-26 stsp struct got_cmd {
80 417a6e49 2020-09-26 stsp const char *cmd_name;
81 417a6e49 2020-09-26 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 417a6e49 2020-09-26 stsp void (*cmd_usage)(void);
83 417a6e49 2020-09-26 stsp const char *cmd_alias;
86 417a6e49 2020-09-26 stsp __dead static void usage(int);
87 417a6e49 2020-09-26 stsp __dead static void usage_init(void);
88 417a6e49 2020-09-26 stsp __dead static void usage_import(void);
89 417a6e49 2020-09-26 stsp __dead static void usage_clone(void);
90 417a6e49 2020-09-26 stsp __dead static void usage_fetch(void);
91 417a6e49 2020-09-26 stsp __dead static void usage_checkout(void);
92 417a6e49 2020-09-26 stsp __dead static void usage_update(void);
93 417a6e49 2020-09-26 stsp __dead static void usage_log(void);
94 417a6e49 2020-09-26 stsp __dead static void usage_diff(void);
95 417a6e49 2020-09-26 stsp __dead static void usage_blame(void);
96 417a6e49 2020-09-26 stsp __dead static void usage_tree(void);
97 417a6e49 2020-09-26 stsp __dead static void usage_status(void);
98 417a6e49 2020-09-26 stsp __dead static void usage_ref(void);
99 417a6e49 2020-09-26 stsp __dead static void usage_branch(void);
100 417a6e49 2020-09-26 stsp __dead static void usage_tag(void);
101 417a6e49 2020-09-26 stsp __dead static void usage_add(void);
102 417a6e49 2020-09-26 stsp __dead static void usage_remove(void);
103 417a6e49 2020-09-26 stsp __dead static void usage_revert(void);
104 417a6e49 2020-09-26 stsp __dead static void usage_commit(void);
105 417a6e49 2020-09-26 stsp __dead static void usage_cherrypick(void);
106 417a6e49 2020-09-26 stsp __dead static void usage_backout(void);
107 417a6e49 2020-09-26 stsp __dead static void usage_rebase(void);
108 417a6e49 2020-09-26 stsp __dead static void usage_histedit(void);
109 417a6e49 2020-09-26 stsp __dead static void usage_integrate(void);
110 417a6e49 2020-09-26 stsp __dead static void usage_stage(void);
111 417a6e49 2020-09-26 stsp __dead static void usage_unstage(void);
112 417a6e49 2020-09-26 stsp __dead static void usage_cat(void);
113 417a6e49 2020-09-26 stsp __dead static void usage_info(void);
115 417a6e49 2020-09-26 stsp static const struct got_error* cmd_init(int, char *[]);
116 417a6e49 2020-09-26 stsp static const struct got_error* cmd_import(int, char *[]);
117 417a6e49 2020-09-26 stsp static const struct got_error* cmd_clone(int, char *[]);
118 417a6e49 2020-09-26 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 417a6e49 2020-09-26 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 417a6e49 2020-09-26 stsp static const struct got_error* cmd_update(int, char *[]);
121 417a6e49 2020-09-26 stsp static const struct got_error* cmd_log(int, char *[]);
122 417a6e49 2020-09-26 stsp static const struct got_error* cmd_diff(int, char *[]);
123 417a6e49 2020-09-26 stsp static const struct got_error* cmd_blame(int, char *[]);
124 417a6e49 2020-09-26 stsp static const struct got_error* cmd_tree(int, char *[]);
125 417a6e49 2020-09-26 stsp static const struct got_error* cmd_status(int, char *[]);
126 417a6e49 2020-09-26 stsp static const struct got_error* cmd_ref(int, char *[]);
127 417a6e49 2020-09-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 417a6e49 2020-09-26 stsp static const struct got_error* cmd_tag(int, char *[]);
129 417a6e49 2020-09-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 417a6e49 2020-09-26 stsp static const struct got_error* cmd_remove(int, char *[]);
131 417a6e49 2020-09-26 stsp static const struct got_error* cmd_revert(int, char *[]);
132 417a6e49 2020-09-26 stsp static const struct got_error* cmd_commit(int, char *[]);
133 417a6e49 2020-09-26 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 417a6e49 2020-09-26 stsp static const struct got_error* cmd_backout(int, char *[]);
135 417a6e49 2020-09-26 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 417a6e49 2020-09-26 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 417a6e49 2020-09-26 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 417a6e49 2020-09-26 stsp static const struct got_error* cmd_stage(int, char *[]);
139 417a6e49 2020-09-26 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 417a6e49 2020-09-26 stsp static const struct got_error* cmd_cat(int, char *[]);
141 417a6e49 2020-09-26 stsp static const struct got_error* cmd_info(int, char *[]);
143 417a6e49 2020-09-26 stsp static struct got_cmd got_commands[] = {
144 417a6e49 2020-09-26 stsp { "init", cmd_init, usage_init, "" },
145 417a6e49 2020-09-26 stsp { "import", cmd_import, usage_import, "im" },
146 417a6e49 2020-09-26 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 417a6e49 2020-09-26 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 417a6e49 2020-09-26 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 417a6e49 2020-09-26 stsp { "update", cmd_update, usage_update, "up" },
150 417a6e49 2020-09-26 stsp { "log", cmd_log, usage_log, "" },
151 417a6e49 2020-09-26 stsp { "diff", cmd_diff, usage_diff, "di" },
152 417a6e49 2020-09-26 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 417a6e49 2020-09-26 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 417a6e49 2020-09-26 stsp { "status", cmd_status, usage_status, "st" },
155 417a6e49 2020-09-26 stsp { "ref", cmd_ref, usage_ref, "" },
156 417a6e49 2020-09-26 stsp { "branch", cmd_branch, usage_branch, "br" },
157 417a6e49 2020-09-26 stsp { "tag", cmd_tag, usage_tag, "" },
158 417a6e49 2020-09-26 stsp { "add", cmd_add, usage_add, "" },
159 417a6e49 2020-09-26 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 417a6e49 2020-09-26 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 417a6e49 2020-09-26 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 417a6e49 2020-09-26 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 417a6e49 2020-09-26 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 417a6e49 2020-09-26 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 417a6e49 2020-09-26 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 417a6e49 2020-09-26 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 417a6e49 2020-09-26 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 417a6e49 2020-09-26 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 417a6e49 2020-09-26 stsp { "cat", cmd_cat, usage_cat, "" },
170 417a6e49 2020-09-26 stsp { "info", cmd_info, usage_info, "" },
173 417a6e49 2020-09-26 stsp static void
174 417a6e49 2020-09-26 stsp list_commands(void)
178 417a6e49 2020-09-26 stsp fprintf(stderr, "commands:");
179 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_commands); i++) {
180 417a6e49 2020-09-26 stsp struct got_cmd *cmd = &got_commands[i];
181 417a6e49 2020-09-26 stsp fprintf(stderr, " %s", cmd->cmd_name);
183 417a6e49 2020-09-26 stsp fputc('\n', stderr);
187 417a6e49 2020-09-26 stsp main(int argc, char *argv[])
189 417a6e49 2020-09-26 stsp struct got_cmd *cmd;
190 417a6e49 2020-09-26 stsp unsigned int i;
192 417a6e49 2020-09-26 stsp int hflag = 0, Vflag = 0;
193 417a6e49 2020-09-26 stsp static struct option longopts[] = {
194 417a6e49 2020-09-26 stsp { "version", no_argument, NULL, 'V' },
195 417a6e49 2020-09-26 stsp { NULL, 0, NULL, 0}
198 417a6e49 2020-09-26 stsp setlocale(LC_CTYPE, "");
200 417a6e49 2020-09-26 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 417a6e49 2020-09-26 stsp switch (ch) {
209 417a6e49 2020-09-26 stsp usage(hflag);
210 417a6e49 2020-09-26 stsp /* NOTREACHED */
214 417a6e49 2020-09-26 stsp argc -= optind;
215 417a6e49 2020-09-26 stsp argv += optind;
216 417a6e49 2020-09-26 stsp optind = 0;
218 417a6e49 2020-09-26 stsp if (Vflag) {
219 417a6e49 2020-09-26 stsp got_version_print_str();
223 417a6e49 2020-09-26 stsp if (argc <= 0)
224 417a6e49 2020-09-26 stsp usage(hflag);
226 417a6e49 2020-09-26 stsp signal(SIGINT, catch_sigint);
227 417a6e49 2020-09-26 stsp signal(SIGPIPE, catch_sigpipe);
229 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_commands); i++) {
230 417a6e49 2020-09-26 stsp const struct got_error *error;
232 417a6e49 2020-09-26 stsp cmd = &got_commands[i];
234 417a6e49 2020-09-26 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 417a6e49 2020-09-26 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
239 417a6e49 2020-09-26 stsp got_commands[i].cmd_usage();
241 417a6e49 2020-09-26 stsp error = got_commands[i].cmd_main(argc, argv);
242 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 417a6e49 2020-09-26 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 417a6e49 2020-09-26 stsp !(sigpipe_received &&
245 417a6e49 2020-09-26 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 417a6e49 2020-09-26 stsp !(sigint_received &&
247 417a6e49 2020-09-26 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 417a6e49 2020-09-26 stsp list_commands();
260 417a6e49 2020-09-26 stsp __dead static void
261 417a6e49 2020-09-26 stsp usage(int hflag)
263 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 417a6e49 2020-09-26 stsp getprogname());
266 417a6e49 2020-09-26 stsp list_commands();
270 417a6e49 2020-09-26 stsp static const struct got_error *
271 417a6e49 2020-09-26 stsp get_editor(char **abspath)
273 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
274 417a6e49 2020-09-26 stsp const char *editor;
276 417a6e49 2020-09-26 stsp *abspath = NULL;
278 417a6e49 2020-09-26 stsp editor = getenv("VISUAL");
279 417a6e49 2020-09-26 stsp if (editor == NULL)
280 417a6e49 2020-09-26 stsp editor = getenv("EDITOR");
282 417a6e49 2020-09-26 stsp if (editor) {
283 417a6e49 2020-09-26 stsp err = got_path_find_prog(abspath, editor);
285 417a6e49 2020-09-26 stsp return err;
288 417a6e49 2020-09-26 stsp if (*abspath == NULL) {
289 417a6e49 2020-09-26 stsp *abspath = strdup("/bin/ed");
290 417a6e49 2020-09-26 stsp if (*abspath == NULL)
291 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
294 417a6e49 2020-09-26 stsp return NULL;
297 417a6e49 2020-09-26 stsp static const struct got_error *
298 417a6e49 2020-09-26 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 417a6e49 2020-09-26 stsp const char *worktree_path)
301 417a6e49 2020-09-26 stsp const struct got_error *err;
303 417a6e49 2020-09-26 stsp #ifdef PROFILE
304 417a6e49 2020-09-26 stsp if (unveil("gmon.out", "rwc") != 0)
305 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", "gmon.out");
307 417a6e49 2020-09-26 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", repo_path);
310 417a6e49 2020-09-26 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", worktree_path);
313 417a6e49 2020-09-26 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 417a6e49 2020-09-26 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 417a6e49 2020-09-26 stsp err = got_privsep_unveil_exec_helpers();
317 417a6e49 2020-09-26 stsp if (err != NULL)
318 417a6e49 2020-09-26 stsp return err;
320 417a6e49 2020-09-26 stsp if (unveil(NULL, NULL) != 0)
321 417a6e49 2020-09-26 stsp return got_error_from_errno("unveil");
323 417a6e49 2020-09-26 stsp return NULL;
326 417a6e49 2020-09-26 stsp __dead static void
327 417a6e49 2020-09-26 stsp usage_init(void)
329 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 417a6e49 2020-09-26 stsp static const struct got_error *
334 417a6e49 2020-09-26 stsp cmd_init(int argc, char *argv[])
336 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
337 417a6e49 2020-09-26 stsp char *repo_path = NULL;
340 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 417a6e49 2020-09-26 stsp switch (ch) {
343 417a6e49 2020-09-26 stsp usage_init();
344 417a6e49 2020-09-26 stsp /* NOTREACHED */
348 417a6e49 2020-09-26 stsp argc -= optind;
349 417a6e49 2020-09-26 stsp argv += optind;
351 417a6e49 2020-09-26 stsp #ifndef PROFILE
352 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 417a6e49 2020-09-26 stsp err(1, "pledge");
355 417a6e49 2020-09-26 stsp if (argc != 1)
356 417a6e49 2020-09-26 stsp usage_init();
358 417a6e49 2020-09-26 stsp repo_path = strdup(argv[0]);
359 417a6e49 2020-09-26 stsp if (repo_path == NULL)
360 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
362 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
364 417a6e49 2020-09-26 stsp error = got_path_mkdir(repo_path);
365 417a6e49 2020-09-26 stsp if (error &&
366 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 417a6e49 2020-09-26 stsp error = apply_unveil(repo_path, 0, NULL);
373 417a6e49 2020-09-26 stsp error = got_repo_init(repo_path);
375 417a6e49 2020-09-26 stsp free(repo_path);
376 417a6e49 2020-09-26 stsp return error;
379 417a6e49 2020-09-26 stsp __dead static void
380 417a6e49 2020-09-26 stsp usage_import(void)
382 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 417a6e49 2020-09-26 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
388 417a6e49 2020-09-26 stsp spawn_editor(const char *editor, const char *file)
391 417a6e49 2020-09-26 stsp sig_t sighup, sigint, sigquit;
392 417a6e49 2020-09-26 stsp int st = -1;
394 417a6e49 2020-09-26 stsp sighup = signal(SIGHUP, SIG_IGN);
395 417a6e49 2020-09-26 stsp sigint = signal(SIGINT, SIG_IGN);
396 417a6e49 2020-09-26 stsp sigquit = signal(SIGQUIT, SIG_IGN);
398 417a6e49 2020-09-26 stsp switch (pid = fork()) {
400 417a6e49 2020-09-26 stsp goto doneediting;
402 417a6e49 2020-09-26 stsp execl(editor, editor, file, (char *)NULL);
403 417a6e49 2020-09-26 stsp _exit(127);
406 417a6e49 2020-09-26 stsp while (waitpid(pid, &st, 0) == -1)
407 417a6e49 2020-09-26 stsp if (errno != EINTR)
410 417a6e49 2020-09-26 stsp doneediting:
411 417a6e49 2020-09-26 stsp (void)signal(SIGHUP, sighup);
412 417a6e49 2020-09-26 stsp (void)signal(SIGINT, sigint);
413 417a6e49 2020-09-26 stsp (void)signal(SIGQUIT, sigquit);
415 417a6e49 2020-09-26 stsp if (!WIFEXITED(st)) {
416 417a6e49 2020-09-26 stsp errno = EINTR;
420 417a6e49 2020-09-26 stsp return WEXITSTATUS(st);
423 417a6e49 2020-09-26 stsp static const struct got_error *
424 417a6e49 2020-09-26 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 417a6e49 2020-09-26 stsp const char *initial_content)
427 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
428 417a6e49 2020-09-26 stsp char buf[1024];
429 417a6e49 2020-09-26 stsp struct stat st, st2;
431 417a6e49 2020-09-26 stsp int content_changed = 0;
432 417a6e49 2020-09-26 stsp size_t len;
434 417a6e49 2020-09-26 stsp *logmsg = NULL;
436 417a6e49 2020-09-26 stsp if (stat(logmsg_path, &st) == -1)
437 417a6e49 2020-09-26 stsp return got_error_from_errno2("stat", logmsg_path);
439 417a6e49 2020-09-26 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 417a6e49 2020-09-26 stsp return got_error_from_errno("failed spawning editor");
442 417a6e49 2020-09-26 stsp if (stat(logmsg_path, &st2) == -1)
443 417a6e49 2020-09-26 stsp return got_error_from_errno("stat");
445 417a6e49 2020-09-26 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 417a6e49 2020-09-26 stsp "no changes made to commit message, aborting");
449 417a6e49 2020-09-26 stsp *logmsg = malloc(st2.st_size + 1);
450 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
451 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
452 417a6e49 2020-09-26 stsp (*logmsg)[0] = '\0';
455 417a6e49 2020-09-26 stsp fp = fopen(logmsg_path, "r");
456 417a6e49 2020-09-26 stsp if (fp == NULL) {
457 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
460 417a6e49 2020-09-26 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 417a6e49 2020-09-26 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 417a6e49 2020-09-26 stsp content_changed = 1;
463 417a6e49 2020-09-26 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 417a6e49 2020-09-26 stsp continue; /* remove comments and leading empty lines */
465 417a6e49 2020-09-26 stsp len = strlcat(*logmsg, buf, st2.st_size);
467 417a6e49 2020-09-26 stsp fclose(fp);
469 417a6e49 2020-09-26 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 417a6e49 2020-09-26 stsp (*logmsg)[len - 1] = '\0';
474 417a6e49 2020-09-26 stsp if (len == 0 || !content_changed)
475 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 417a6e49 2020-09-26 stsp "commit message cannot be empty, aborting");
479 417a6e49 2020-09-26 stsp free(*logmsg);
480 417a6e49 2020-09-26 stsp *logmsg = NULL;
482 417a6e49 2020-09-26 stsp return err;
485 417a6e49 2020-09-26 stsp static const struct got_error *
486 417a6e49 2020-09-26 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 417a6e49 2020-09-26 stsp const char *path_dir, const char *branch_name)
489 417a6e49 2020-09-26 stsp char *initial_content = NULL;
490 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
491 417a6e49 2020-09-26 stsp int initial_content_len;
492 417a6e49 2020-09-26 stsp int fd = -1;
494 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
495 417a6e49 2020-09-26 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 417a6e49 2020-09-26 stsp branch_name);
497 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
498 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
500 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-importmsg");
505 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *logmsg_path);
510 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *logmsg_path);
514 417a6e49 2020-09-26 stsp free(initial_content);
516 417a6e49 2020-09-26 stsp free(*logmsg_path);
517 417a6e49 2020-09-26 stsp *logmsg_path = NULL;
519 417a6e49 2020-09-26 stsp return err;
522 417a6e49 2020-09-26 stsp static const struct got_error *
523 417a6e49 2020-09-26 stsp import_progress(void *arg, const char *path)
525 417a6e49 2020-09-26 stsp printf("A %s\n", path);
526 417a6e49 2020-09-26 stsp return NULL;
529 417a6e49 2020-09-26 stsp static const struct got_error *
530 417a6e49 2020-09-26 stsp get_author(char **author, struct got_repository *repo,
531 417a6e49 2020-09-26 stsp struct got_worktree *worktree)
533 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
534 417a6e49 2020-09-26 stsp const char *got_author = NULL, *name, *email;
535 417a6e49 2020-09-26 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
537 417a6e49 2020-09-26 stsp *author = NULL;
539 417a6e49 2020-09-26 stsp if (worktree)
540 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
544 417a6e49 2020-09-26 stsp * Priority of potential author information sources, from most
545 417a6e49 2020-09-26 stsp * significant to least significant:
546 417a6e49 2020-09-26 stsp * 1) work tree's .got/got.conf file
547 417a6e49 2020-09-26 stsp * 2) repository's got.conf file
548 417a6e49 2020-09-26 stsp * 3) repository's git config file
549 417a6e49 2020-09-26 stsp * 4) environment variables
550 417a6e49 2020-09-26 stsp * 5) global git config files (in user's home directory or /etc)
553 417a6e49 2020-09-26 stsp if (worktree_conf)
554 417a6e49 2020-09-26 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 417a6e49 2020-09-26 stsp if (got_author == NULL)
556 417a6e49 2020-09-26 stsp got_author = got_gotconfig_get_author(repo_conf);
557 417a6e49 2020-09-26 stsp if (got_author == NULL) {
558 417a6e49 2020-09-26 stsp name = got_repo_get_gitconfig_author_name(repo);
559 417a6e49 2020-09-26 stsp email = got_repo_get_gitconfig_author_email(repo);
560 417a6e49 2020-09-26 stsp if (name && email) {
561 417a6e49 2020-09-26 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
563 417a6e49 2020-09-26 stsp return NULL;
566 417a6e49 2020-09-26 stsp got_author = getenv("GOT_AUTHOR");
567 417a6e49 2020-09-26 stsp if (got_author == NULL) {
568 417a6e49 2020-09-26 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 417a6e49 2020-09-26 stsp email = got_repo_get_global_gitconfig_author_email(
571 417a6e49 2020-09-26 stsp if (name && email) {
572 417a6e49 2020-09-26 stsp if (asprintf(author, "%s <%s>", name, email)
574 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
575 417a6e49 2020-09-26 stsp return NULL;
577 417a6e49 2020-09-26 stsp /* TODO: Look up user in password database? */
578 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
582 417a6e49 2020-09-26 stsp *author = strdup(got_author);
583 417a6e49 2020-09-26 stsp if (*author == NULL)
584 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
587 417a6e49 2020-09-26 stsp * Really dumb email address check; we're only doing this to
588 417a6e49 2020-09-26 stsp * avoid git's object parser breaking on commits we create.
590 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '<')
591 417a6e49 2020-09-26 stsp got_author++;
592 417a6e49 2020-09-26 stsp if (*got_author != '<') {
593 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
596 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '@')
597 417a6e49 2020-09-26 stsp got_author++;
598 417a6e49 2020-09-26 stsp if (*got_author != '@') {
599 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
602 417a6e49 2020-09-26 stsp while (*got_author && *got_author != '>')
603 417a6e49 2020-09-26 stsp got_author++;
604 417a6e49 2020-09-26 stsp if (*got_author != '>')
605 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
608 417a6e49 2020-09-26 stsp free(*author);
609 417a6e49 2020-09-26 stsp *author = NULL;
611 417a6e49 2020-09-26 stsp return err;
614 417a6e49 2020-09-26 stsp static const struct got_error *
615 417a6e49 2020-09-26 stsp get_gitconfig_path(char **gitconfig_path)
617 417a6e49 2020-09-26 stsp const char *homedir = getenv("HOME");
619 417a6e49 2020-09-26 stsp *gitconfig_path = NULL;
620 417a6e49 2020-09-26 stsp if (homedir) {
621 417a6e49 2020-09-26 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
625 417a6e49 2020-09-26 stsp return NULL;
628 417a6e49 2020-09-26 stsp static const struct got_error *
629 417a6e49 2020-09-26 stsp cmd_import(int argc, char *argv[])
631 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
632 417a6e49 2020-09-26 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 417a6e49 2020-09-26 stsp const char *branch_name = "main";
635 417a6e49 2020-09-26 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
637 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id = NULL;
640 417a6e49 2020-09-26 stsp struct got_pathlist_head ignores;
641 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
642 417a6e49 2020-09-26 stsp int preserve_logmsg = 0;
644 417a6e49 2020-09-26 stsp TAILQ_INIT(&ignores);
646 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 417a6e49 2020-09-26 stsp switch (ch) {
649 417a6e49 2020-09-26 stsp branch_name = optarg;
652 417a6e49 2020-09-26 stsp logmsg = strdup(optarg);
653 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
654 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
659 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
660 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
661 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
667 417a6e49 2020-09-26 stsp if (optarg[0] == '\0')
669 417a6e49 2020-09-26 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
675 417a6e49 2020-09-26 stsp usage_import();
676 417a6e49 2020-09-26 stsp /* NOTREACHED */
680 417a6e49 2020-09-26 stsp argc -= optind;
681 417a6e49 2020-09-26 stsp argv += optind;
683 417a6e49 2020-09-26 stsp #ifndef PROFILE
684 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
686 417a6e49 2020-09-26 stsp NULL) == -1)
687 417a6e49 2020-09-26 stsp err(1, "pledge");
689 417a6e49 2020-09-26 stsp if (argc != 1)
690 417a6e49 2020-09-26 stsp usage_import();
692 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
693 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
694 417a6e49 2020-09-26 stsp if (repo_path == NULL)
695 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
697 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
698 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
701 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
705 417a6e49 2020-09-26 stsp error = get_author(&author, repo, NULL);
707 417a6e49 2020-09-26 stsp return error;
710 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
711 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
712 417a6e49 2020-09-26 stsp * an unintended typo.
714 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
715 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
717 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
722 417a6e49 2020-09-26 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 417a6e49 2020-09-26 stsp if (error) {
724 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
727 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 417a6e49 2020-09-26 stsp "import target branch already exists");
732 417a6e49 2020-09-26 stsp path_dir = realpath(argv[0], NULL);
733 417a6e49 2020-09-26 stsp if (path_dir == NULL) {
734 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
737 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(path_dir);
740 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
743 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 417a6e49 2020-09-26 stsp error = get_editor(&editor);
747 417a6e49 2020-09-26 stsp free(logmsg);
748 417a6e49 2020-09-26 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 417a6e49 2020-09-26 stsp path_dir, refname);
750 417a6e49 2020-09-26 stsp if (error) {
751 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 417a6e49 2020-09-26 stsp logmsg_path != NULL)
753 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
758 417a6e49 2020-09-26 stsp if (unveil(path_dir, "r") != 0) {
759 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil", path_dir);
760 417a6e49 2020-09-26 stsp if (logmsg_path)
761 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
765 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 417a6e49 2020-09-26 stsp if (error) {
767 417a6e49 2020-09-26 stsp if (logmsg_path)
768 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
772 417a6e49 2020-09-26 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 417a6e49 2020-09-26 stsp author, &ignores, repo, import_progress, NULL);
774 417a6e49 2020-09-26 stsp if (error) {
775 417a6e49 2020-09-26 stsp if (logmsg_path)
776 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
780 417a6e49 2020-09-26 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 417a6e49 2020-09-26 stsp if (error) {
782 417a6e49 2020-09-26 stsp if (logmsg_path)
783 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
787 417a6e49 2020-09-26 stsp error = got_ref_write(branch_ref, repo);
788 417a6e49 2020-09-26 stsp if (error) {
789 417a6e49 2020-09-26 stsp if (logmsg_path)
790 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
794 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_commit_id);
795 417a6e49 2020-09-26 stsp if (error) {
796 417a6e49 2020-09-26 stsp if (logmsg_path)
797 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
801 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 417a6e49 2020-09-26 stsp if (error) {
803 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF) {
804 417a6e49 2020-09-26 stsp if (logmsg_path)
805 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
809 417a6e49 2020-09-26 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 417a6e49 2020-09-26 stsp branch_ref);
811 417a6e49 2020-09-26 stsp if (error) {
812 417a6e49 2020-09-26 stsp if (logmsg_path)
813 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
817 417a6e49 2020-09-26 stsp error = got_ref_write(head_ref, repo);
818 417a6e49 2020-09-26 stsp if (error) {
819 417a6e49 2020-09-26 stsp if (logmsg_path)
820 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
825 417a6e49 2020-09-26 stsp printf("Created branch %s with commit %s\n",
826 417a6e49 2020-09-26 stsp got_ref_get_name(branch_ref), id_str);
828 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
829 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 417a6e49 2020-09-26 stsp getprogname(), logmsg_path);
831 417a6e49 2020-09-26 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 417a6e49 2020-09-26 stsp free(logmsg);
834 417a6e49 2020-09-26 stsp free(logmsg_path);
835 417a6e49 2020-09-26 stsp free(repo_path);
836 417a6e49 2020-09-26 stsp free(editor);
837 417a6e49 2020-09-26 stsp free(refname);
838 417a6e49 2020-09-26 stsp free(new_commit_id);
839 417a6e49 2020-09-26 stsp free(id_str);
840 417a6e49 2020-09-26 stsp free(author);
841 417a6e49 2020-09-26 stsp free(gitconfig_path);
842 417a6e49 2020-09-26 stsp if (branch_ref)
843 417a6e49 2020-09-26 stsp got_ref_close(branch_ref);
844 417a6e49 2020-09-26 stsp if (head_ref)
845 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
846 417a6e49 2020-09-26 stsp return error;
849 417a6e49 2020-09-26 stsp __dead static void
850 417a6e49 2020-09-26 stsp usage_clone(void)
852 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 417a6e49 2020-09-26 stsp "[-R reference] repository-url [directory]\n", getprogname());
857 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg {
858 417a6e49 2020-09-26 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 417a6e49 2020-09-26 stsp int last_p_indexed;
860 417a6e49 2020-09-26 stsp int last_p_resolved;
861 417a6e49 2020-09-26 stsp int verbosity;
864 417a6e49 2020-09-26 stsp static const struct got_error *
865 417a6e49 2020-09-26 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
866 417a6e49 2020-09-26 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
868 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg *a = arg;
869 417a6e49 2020-09-26 stsp char scaled_size[FMT_SCALED_STRSIZE];
870 417a6e49 2020-09-26 stsp int p_indexed, p_resolved;
871 417a6e49 2020-09-26 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
873 417a6e49 2020-09-26 stsp if (a->verbosity < 0)
874 417a6e49 2020-09-26 stsp return NULL;
876 417a6e49 2020-09-26 stsp if (message && message[0] != '\0') {
877 417a6e49 2020-09-26 stsp printf("\rserver: %s", message);
878 417a6e49 2020-09-26 stsp fflush(stdout);
879 417a6e49 2020-09-26 stsp return NULL;
882 417a6e49 2020-09-26 stsp if (packfile_size > 0 || nobj_indexed > 0) {
883 417a6e49 2020-09-26 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 417a6e49 2020-09-26 stsp (a->last_scaled_size[0] == '\0' ||
885 417a6e49 2020-09-26 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
886 417a6e49 2020-09-26 stsp print_size = 1;
887 417a6e49 2020-09-26 stsp if (strlcpy(a->last_scaled_size, scaled_size,
888 417a6e49 2020-09-26 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
891 417a6e49 2020-09-26 stsp if (nobj_indexed > 0) {
892 417a6e49 2020-09-26 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
893 417a6e49 2020-09-26 stsp if (p_indexed != a->last_p_indexed) {
894 417a6e49 2020-09-26 stsp a->last_p_indexed = p_indexed;
895 417a6e49 2020-09-26 stsp print_indexed = 1;
896 417a6e49 2020-09-26 stsp print_size = 1;
899 417a6e49 2020-09-26 stsp if (nobj_resolved > 0) {
900 417a6e49 2020-09-26 stsp p_resolved = (nobj_resolved * 100) /
901 417a6e49 2020-09-26 stsp (nobj_total - nobj_loose);
902 417a6e49 2020-09-26 stsp if (p_resolved != a->last_p_resolved) {
903 417a6e49 2020-09-26 stsp a->last_p_resolved = p_resolved;
904 417a6e49 2020-09-26 stsp print_resolved = 1;
905 417a6e49 2020-09-26 stsp print_indexed = 1;
906 417a6e49 2020-09-26 stsp print_size = 1;
911 417a6e49 2020-09-26 stsp if (print_size || print_indexed || print_resolved)
912 417a6e49 2020-09-26 stsp printf("\r");
913 417a6e49 2020-09-26 stsp if (print_size)
914 417a6e49 2020-09-26 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
915 417a6e49 2020-09-26 stsp if (print_indexed)
916 417a6e49 2020-09-26 stsp printf("; indexing %d%%", p_indexed);
917 417a6e49 2020-09-26 stsp if (print_resolved)
918 417a6e49 2020-09-26 stsp printf("; resolving deltas %d%%", p_resolved);
919 417a6e49 2020-09-26 stsp if (print_size || print_indexed || print_resolved)
920 417a6e49 2020-09-26 stsp fflush(stdout);
922 417a6e49 2020-09-26 stsp return NULL;
925 417a6e49 2020-09-26 stsp static const struct got_error *
926 417a6e49 2020-09-26 stsp create_symref(const char *refname, struct got_reference *target_ref,
927 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
929 417a6e49 2020-09-26 stsp const struct got_error *err;
930 417a6e49 2020-09-26 stsp struct got_reference *head_symref;
932 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
934 417a6e49 2020-09-26 stsp return err;
936 417a6e49 2020-09-26 stsp err = got_ref_write(head_symref, repo);
937 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
938 417a6e49 2020-09-26 stsp if (err == NULL && verbosity > 0) {
939 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
940 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
942 417a6e49 2020-09-26 stsp return err;
945 417a6e49 2020-09-26 stsp static const struct got_error *
946 417a6e49 2020-09-26 stsp list_remote_refs(struct got_pathlist_head *symrefs,
947 417a6e49 2020-09-26 stsp struct got_pathlist_head *refs)
949 417a6e49 2020-09-26 stsp const struct got_error *err;
950 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
952 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, symrefs, entry) {
953 417a6e49 2020-09-26 stsp const char *refname = pe->path;
954 417a6e49 2020-09-26 stsp const char *targetref = pe->data;
956 417a6e49 2020-09-26 stsp printf("%s: %s\n", refname, targetref);
959 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, refs, entry) {
960 417a6e49 2020-09-26 stsp const char *refname = pe->path;
961 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
962 417a6e49 2020-09-26 stsp char *id_str;
964 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
966 417a6e49 2020-09-26 stsp return err;
967 417a6e49 2020-09-26 stsp printf("%s: %s\n", refname, id_str);
968 417a6e49 2020-09-26 stsp free(id_str);
971 417a6e49 2020-09-26 stsp return NULL;
974 417a6e49 2020-09-26 stsp static const struct got_error *
975 417a6e49 2020-09-26 stsp create_ref(const char *refname, struct got_object_id *id,
976 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
978 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
979 417a6e49 2020-09-26 stsp struct got_reference *ref;
980 417a6e49 2020-09-26 stsp char *id_str;
982 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
984 417a6e49 2020-09-26 stsp return err;
986 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
990 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
991 417a6e49 2020-09-26 stsp got_ref_close(ref);
993 417a6e49 2020-09-26 stsp if (err == NULL && verbosity >= 0)
994 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n", refname, id_str);
996 417a6e49 2020-09-26 stsp free(id_str);
997 417a6e49 2020-09-26 stsp return err;
1000 417a6e49 2020-09-26 stsp static int
1001 417a6e49 2020-09-26 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1003 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/", 5) != 0)
1005 417a6e49 2020-09-26 stsp refname += 5;
1008 417a6e49 2020-09-26 stsp * Prevent fetching of references that won't make any
1009 417a6e49 2020-09-26 stsp * sense outside of the remote repository's context.
1011 417a6e49 2020-09-26 stsp if (strncmp(refname, "got/", 4) == 0)
1013 417a6e49 2020-09-26 stsp if (strncmp(refname, "remotes/", 8) == 0)
1016 417a6e49 2020-09-26 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1017 417a6e49 2020-09-26 stsp wanted_ref += 5;
1019 417a6e49 2020-09-26 stsp /* Allow prefix match. */
1020 417a6e49 2020-09-26 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1023 417a6e49 2020-09-26 stsp /* Allow exact match. */
1024 417a6e49 2020-09-26 stsp return (strcmp(refname, wanted_ref) == 0);
1027 417a6e49 2020-09-26 stsp static int
1028 417a6e49 2020-09-26 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1030 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1032 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1033 417a6e49 2020-09-26 stsp if (match_wanted_ref(refname, pe->path))
1040 417a6e49 2020-09-26 stsp static const struct got_error *
1041 417a6e49 2020-09-26 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1042 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1044 417a6e49 2020-09-26 stsp const struct got_error *err;
1045 417a6e49 2020-09-26 stsp char *remote_refname;
1047 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1048 417a6e49 2020-09-26 stsp refname += 5;
1050 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1051 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1052 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1054 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1055 417a6e49 2020-09-26 stsp free(remote_refname);
1056 417a6e49 2020-09-26 stsp return err;
1059 417a6e49 2020-09-26 stsp static const struct got_error *
1060 417a6e49 2020-09-26 stsp cmd_clone(int argc, char *argv[])
1062 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
1063 417a6e49 2020-09-26 stsp const char *uri, *dirname;
1064 417a6e49 2020-09-26 stsp char *proto, *host, *port, *repo_name, *server_path;
1065 417a6e49 2020-09-26 stsp char *default_destdir = NULL, *id_str = NULL;
1066 417a6e49 2020-09-26 stsp const char *repo_path, *remote_repo_path;
1067 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1068 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1069 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1070 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1071 417a6e49 2020-09-26 stsp int ch, fetchfd = -1, fetchstatus;
1072 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1073 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1074 417a6e49 2020-09-26 stsp char *git_url = NULL;
1075 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *gotconfig_path = NULL;
1076 417a6e49 2020-09-26 stsp char *gitconfig = NULL, *gotconfig = NULL;
1077 417a6e49 2020-09-26 stsp FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1078 417a6e49 2020-09-26 stsp ssize_t n;
1079 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1080 417a6e49 2020-09-26 stsp int list_refs_only = 0;
1081 417a6e49 2020-09-26 stsp struct got_reference *head_symref = NULL;
1083 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1084 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1085 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1086 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1088 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1089 417a6e49 2020-09-26 stsp switch (ch) {
1091 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1094 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1095 417a6e49 2020-09-26 stsp optarg, NULL);
1096 417a6e49 2020-09-26 stsp if (error)
1097 417a6e49 2020-09-26 stsp return error;
1100 417a6e49 2020-09-26 stsp list_refs_only = 1;
1103 417a6e49 2020-09-26 stsp mirror_references = 1;
1106 417a6e49 2020-09-26 stsp if (verbosity < 0)
1107 417a6e49 2020-09-26 stsp verbosity = 0;
1108 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1109 417a6e49 2020-09-26 stsp verbosity++;
1112 417a6e49 2020-09-26 stsp verbosity = -1;
1115 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1116 417a6e49 2020-09-26 stsp optarg, NULL);
1117 417a6e49 2020-09-26 stsp if (error)
1118 417a6e49 2020-09-26 stsp return error;
1121 417a6e49 2020-09-26 stsp usage_clone();
1125 417a6e49 2020-09-26 stsp argc -= optind;
1126 417a6e49 2020-09-26 stsp argv += optind;
1128 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1129 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1130 417a6e49 2020-09-26 stsp if (list_refs_only) {
1131 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1132 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1133 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1134 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1135 417a6e49 2020-09-26 stsp if (mirror_references)
1136 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
1137 417a6e49 2020-09-26 stsp if (verbosity == -1)
1138 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1139 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_refs))
1140 417a6e49 2020-09-26 stsp errx(1, "-l and -R options are mutually exclusive");
1143 417a6e49 2020-09-26 stsp uri = argv[0];
1145 417a6e49 2020-09-26 stsp if (argc == 1)
1146 417a6e49 2020-09-26 stsp dirname = NULL;
1147 417a6e49 2020-09-26 stsp else if (argc == 2)
1148 417a6e49 2020-09-26 stsp dirname = argv[1];
1150 417a6e49 2020-09-26 stsp usage_clone();
1152 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1153 417a6e49 2020-09-26 stsp &repo_name, uri);
1154 417a6e49 2020-09-26 stsp if (error)
1155 417a6e49 2020-09-26 stsp goto done;
1157 417a6e49 2020-09-26 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1158 417a6e49 2020-09-26 stsp host, port ? ":" : "", port ? port : "",
1159 417a6e49 2020-09-26 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1160 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1161 417a6e49 2020-09-26 stsp goto done;
1164 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1165 417a6e49 2020-09-26 stsp #ifndef PROFILE
1166 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1167 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1168 417a6e49 2020-09-26 stsp err(1, "pledge");
1170 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1171 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1172 417a6e49 2020-09-26 stsp #ifndef PROFILE
1173 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1174 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1175 417a6e49 2020-09-26 stsp err(1, "pledge");
1177 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1178 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1179 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1180 417a6e49 2020-09-26 stsp goto done;
1182 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1183 417a6e49 2020-09-26 stsp goto done;
1185 417a6e49 2020-09-26 stsp if (dirname == NULL) {
1186 417a6e49 2020-09-26 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1187 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1188 417a6e49 2020-09-26 stsp goto done;
1190 417a6e49 2020-09-26 stsp repo_path = default_destdir;
1192 417a6e49 2020-09-26 stsp repo_path = dirname;
1194 417a6e49 2020-09-26 stsp if (!list_refs_only) {
1195 417a6e49 2020-09-26 stsp error = got_path_mkdir(repo_path);
1196 417a6e49 2020-09-26 stsp if (error)
1197 417a6e49 2020-09-26 stsp goto done;
1199 417a6e49 2020-09-26 stsp error = got_repo_init(repo_path);
1200 417a6e49 2020-09-26 stsp if (error)
1201 417a6e49 2020-09-26 stsp goto done;
1202 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1203 417a6e49 2020-09-26 stsp if (error)
1204 417a6e49 2020-09-26 stsp goto done;
1207 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1208 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1209 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
1210 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
1211 417a6e49 2020-09-26 stsp goto done;
1214 417a6e49 2020-09-26 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1215 417a6e49 2020-09-26 stsp if (error)
1216 417a6e49 2020-09-26 stsp goto done;
1218 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1219 417a6e49 2020-09-26 stsp printf("Connecting to %s%s%s\n", host,
1220 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
1222 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1223 417a6e49 2020-09-26 stsp server_path, verbosity);
1224 417a6e49 2020-09-26 stsp if (error)
1225 417a6e49 2020-09-26 stsp goto done;
1227 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
1228 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
1229 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
1230 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
1231 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1232 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1233 417a6e49 2020-09-26 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1234 417a6e49 2020-09-26 stsp list_refs_only, verbosity, fetchfd, repo,
1235 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
1236 417a6e49 2020-09-26 stsp if (error)
1237 417a6e49 2020-09-26 stsp goto done;
1239 417a6e49 2020-09-26 stsp if (list_refs_only) {
1240 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
1241 417a6e49 2020-09-26 stsp goto done;
1244 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
1245 417a6e49 2020-09-26 stsp if (error)
1246 417a6e49 2020-09-26 stsp goto done;
1247 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1248 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
1249 417a6e49 2020-09-26 stsp free(id_str);
1251 417a6e49 2020-09-26 stsp /* Set up references provided with the pack file. */
1252 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1253 417a6e49 2020-09-26 stsp const char *refname = pe->path;
1254 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
1255 417a6e49 2020-09-26 stsp char *remote_refname;
1257 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1258 417a6e49 2020-09-26 stsp !mirror_references) {
1259 417a6e49 2020-09-26 stsp error = create_wanted_ref(refname, id,
1260 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1261 417a6e49 2020-09-26 stsp verbosity - 1, repo);
1262 417a6e49 2020-09-26 stsp if (error)
1263 417a6e49 2020-09-26 stsp goto done;
1267 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity - 1, repo);
1268 417a6e49 2020-09-26 stsp if (error)
1269 417a6e49 2020-09-26 stsp goto done;
1271 417a6e49 2020-09-26 stsp if (mirror_references)
1274 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1277 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname,
1278 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1279 417a6e49 2020-09-26 stsp refname + 11) == -1) {
1280 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1281 417a6e49 2020-09-26 stsp goto done;
1283 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1284 417a6e49 2020-09-26 stsp free(remote_refname);
1285 417a6e49 2020-09-26 stsp if (error)
1286 417a6e49 2020-09-26 stsp goto done;
1289 417a6e49 2020-09-26 stsp /* Set the HEAD reference if the server provided one. */
1290 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1291 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
1292 417a6e49 2020-09-26 stsp const char *refname = pe->path;
1293 417a6e49 2020-09-26 stsp const char *target = pe->data;
1294 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
1296 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1299 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, target, 0);
1300 417a6e49 2020-09-26 stsp if (error) {
1301 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1302 417a6e49 2020-09-26 stsp error = NULL;
1305 417a6e49 2020-09-26 stsp goto done;
1308 417a6e49 2020-09-26 stsp error = create_symref(refname, target_ref, verbosity, repo);
1309 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1310 417a6e49 2020-09-26 stsp if (error)
1311 417a6e49 2020-09-26 stsp goto done;
1313 417a6e49 2020-09-26 stsp if (mirror_references)
1316 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
1319 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname,
1320 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1321 417a6e49 2020-09-26 stsp refname) == -1) {
1322 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1323 417a6e49 2020-09-26 stsp goto done;
1325 417a6e49 2020-09-26 stsp if (asprintf(&remote_target,
1326 417a6e49 2020-09-26 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1327 417a6e49 2020-09-26 stsp target + 11) == -1) {
1328 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1329 417a6e49 2020-09-26 stsp free(remote_refname);
1330 417a6e49 2020-09-26 stsp goto done;
1332 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1333 417a6e49 2020-09-26 stsp if (error) {
1334 417a6e49 2020-09-26 stsp free(remote_refname);
1335 417a6e49 2020-09-26 stsp free(remote_target);
1336 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1337 417a6e49 2020-09-26 stsp error = NULL;
1340 417a6e49 2020-09-26 stsp goto done;
1342 417a6e49 2020-09-26 stsp error = create_symref(remote_refname, target_ref,
1343 417a6e49 2020-09-26 stsp verbosity - 1, repo);
1344 417a6e49 2020-09-26 stsp free(remote_refname);
1345 417a6e49 2020-09-26 stsp free(remote_target);
1346 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1347 417a6e49 2020-09-26 stsp if (error)
1348 417a6e49 2020-09-26 stsp goto done;
1350 417a6e49 2020-09-26 stsp if (pe == NULL) {
1352 417a6e49 2020-09-26 stsp * We failed to set the HEAD reference. If we asked for
1353 417a6e49 2020-09-26 stsp * a set of wanted branches use the first of one of those
1354 417a6e49 2020-09-26 stsp * which could be fetched instead.
1356 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1357 417a6e49 2020-09-26 stsp const char *target = pe->path;
1358 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
1360 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, target, 0);
1361 417a6e49 2020-09-26 stsp if (error) {
1362 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
1363 417a6e49 2020-09-26 stsp error = NULL;
1366 417a6e49 2020-09-26 stsp goto done;
1369 417a6e49 2020-09-26 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1370 417a6e49 2020-09-26 stsp verbosity, repo);
1371 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
1372 417a6e49 2020-09-26 stsp if (error)
1373 417a6e49 2020-09-26 stsp goto done;
1378 417a6e49 2020-09-26 stsp /* Create got.conf(5). */
1379 417a6e49 2020-09-26 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1380 417a6e49 2020-09-26 stsp if (gotconfig_path == NULL) {
1381 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gotconfig");
1382 417a6e49 2020-09-26 stsp goto done;
1384 417a6e49 2020-09-26 stsp gotconfig_file = fopen(gotconfig_path, "a");
1385 417a6e49 2020-09-26 stsp if (gotconfig_file == NULL) {
1386 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gotconfig_path);
1387 417a6e49 2020-09-26 stsp goto done;
1389 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(server_path);
1390 417a6e49 2020-09-26 stsp remote_repo_path = server_path;
1391 417a6e49 2020-09-26 stsp while (remote_repo_path[0] == '/')
1392 417a6e49 2020-09-26 stsp remote_repo_path++;
1393 417a6e49 2020-09-26 stsp if (asprintf(&gotconfig,
1394 417a6e49 2020-09-26 stsp "remote \"%s\" {\n"
1395 417a6e49 2020-09-26 stsp "\tserver %s\n"
1396 417a6e49 2020-09-26 stsp "\tprotocol %s\n"
1398 417a6e49 2020-09-26 stsp "\trepository \"%s\"\n"
1401 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1402 417a6e49 2020-09-26 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1403 417a6e49 2020-09-26 stsp remote_repo_path,
1404 417a6e49 2020-09-26 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1405 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1406 417a6e49 2020-09-26 stsp goto done;
1408 417a6e49 2020-09-26 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1409 417a6e49 2020-09-26 stsp if (n != strlen(gotconfig)) {
1410 417a6e49 2020-09-26 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1411 417a6e49 2020-09-26 stsp goto done;
1414 417a6e49 2020-09-26 stsp /* Create a config file Git can understand. */
1415 417a6e49 2020-09-26 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1416 417a6e49 2020-09-26 stsp if (gitconfig_path == NULL) {
1417 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1418 417a6e49 2020-09-26 stsp goto done;
1420 417a6e49 2020-09-26 stsp gitconfig_file = fopen(gitconfig_path, "a");
1421 417a6e49 2020-09-26 stsp if (gitconfig_file == NULL) {
1422 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1423 417a6e49 2020-09-26 stsp goto done;
1425 417a6e49 2020-09-26 stsp if (mirror_references) {
1426 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1427 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1428 417a6e49 2020-09-26 stsp "\turl = %s\n"
1429 417a6e49 2020-09-26 stsp "\tfetch = +refs/*:refs/*\n"
1430 417a6e49 2020-09-26 stsp "\tmirror = true\n",
1431 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1432 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1433 417a6e49 2020-09-26 stsp goto done;
1435 417a6e49 2020-09-26 stsp } else if (fetch_all_branches) {
1436 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1437 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1438 417a6e49 2020-09-26 stsp "\turl = %s\n"
1439 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1440 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1441 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1442 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1443 417a6e49 2020-09-26 stsp goto done;
1446 417a6e49 2020-09-26 stsp const char *branchname;
1449 417a6e49 2020-09-26 stsp * If the server specified a default branch, use just that one.
1450 417a6e49 2020-09-26 stsp * Otherwise fall back to fetching all branches on next fetch.
1452 417a6e49 2020-09-26 stsp if (head_symref) {
1453 417a6e49 2020-09-26 stsp branchname = got_ref_get_symref_target(head_symref);
1454 417a6e49 2020-09-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1455 417a6e49 2020-09-26 stsp branchname += 11;
1457 417a6e49 2020-09-26 stsp branchname = "*"; /* fall back to all branches */
1458 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1459 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1460 417a6e49 2020-09-26 stsp "\turl = %s\n"
1461 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1462 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1463 417a6e49 2020-09-26 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1464 417a6e49 2020-09-26 stsp branchname) == -1) {
1465 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1466 417a6e49 2020-09-26 stsp goto done;
1469 417a6e49 2020-09-26 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1470 417a6e49 2020-09-26 stsp if (n != strlen(gitconfig)) {
1471 417a6e49 2020-09-26 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1472 417a6e49 2020-09-26 stsp goto done;
1475 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1476 417a6e49 2020-09-26 stsp printf("Created %s repository '%s'\n",
1477 417a6e49 2020-09-26 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1479 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
1480 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
1481 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
1482 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1483 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
1485 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1486 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
1487 417a6e49 2020-09-26 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1488 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1489 417a6e49 2020-09-26 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1490 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1492 417a6e49 2020-09-26 stsp got_repo_close(repo);
1493 417a6e49 2020-09-26 stsp if (head_symref)
1494 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
1495 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1496 417a6e49 2020-09-26 stsp free((void *)pe->path);
1497 417a6e49 2020-09-26 stsp free(pe->data);
1499 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
1500 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1501 417a6e49 2020-09-26 stsp free((void *)pe->path);
1502 417a6e49 2020-09-26 stsp free(pe->data);
1504 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
1505 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
1506 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
1507 417a6e49 2020-09-26 stsp free(pack_hash);
1508 417a6e49 2020-09-26 stsp free(proto);
1509 417a6e49 2020-09-26 stsp free(host);
1510 417a6e49 2020-09-26 stsp free(port);
1511 417a6e49 2020-09-26 stsp free(server_path);
1512 417a6e49 2020-09-26 stsp free(repo_name);
1513 417a6e49 2020-09-26 stsp free(default_destdir);
1514 417a6e49 2020-09-26 stsp free(gotconfig);
1515 417a6e49 2020-09-26 stsp free(gitconfig);
1516 417a6e49 2020-09-26 stsp free(gotconfig_path);
1517 417a6e49 2020-09-26 stsp free(gitconfig_path);
1518 417a6e49 2020-09-26 stsp free(git_url);
1519 417a6e49 2020-09-26 stsp return error;
1522 417a6e49 2020-09-26 stsp static const struct got_error *
1523 417a6e49 2020-09-26 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1524 417a6e49 2020-09-26 stsp int replace_tags, int verbosity, struct got_repository *repo)
1526 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1527 417a6e49 2020-09-26 stsp char *new_id_str = NULL;
1528 417a6e49 2020-09-26 stsp struct got_object_id *old_id = NULL;
1530 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
1532 417a6e49 2020-09-26 stsp goto done;
1534 417a6e49 2020-09-26 stsp if (!replace_tags &&
1535 417a6e49 2020-09-26 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1536 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1538 417a6e49 2020-09-26 stsp goto done;
1539 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1540 417a6e49 2020-09-26 stsp goto done;
1541 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1542 417a6e49 2020-09-26 stsp printf("Rejecting update of existing tag %s: %s\n",
1543 417a6e49 2020-09-26 stsp got_ref_get_name(ref), new_id_str);
1545 417a6e49 2020-09-26 stsp goto done;
1548 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1549 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1550 417a6e49 2020-09-26 stsp printf("Replacing reference %s: %s\n",
1551 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1552 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1554 417a6e49 2020-09-26 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1556 417a6e49 2020-09-26 stsp goto done;
1557 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1559 417a6e49 2020-09-26 stsp goto done;
1561 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1563 417a6e49 2020-09-26 stsp goto done;
1564 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1565 417a6e49 2020-09-26 stsp goto done;
1567 417a6e49 2020-09-26 stsp err = got_ref_change_ref(ref, new_id);
1569 417a6e49 2020-09-26 stsp goto done;
1570 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1572 417a6e49 2020-09-26 stsp goto done;
1575 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1576 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1577 417a6e49 2020-09-26 stsp new_id_str);
1579 417a6e49 2020-09-26 stsp free(old_id);
1580 417a6e49 2020-09-26 stsp free(new_id_str);
1581 417a6e49 2020-09-26 stsp return err;
1584 417a6e49 2020-09-26 stsp static const struct got_error *
1585 417a6e49 2020-09-26 stsp update_symref(const char *refname, struct got_reference *target_ref,
1586 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1588 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1589 417a6e49 2020-09-26 stsp struct got_reference *symref;
1590 417a6e49 2020-09-26 stsp int symref_is_locked = 0;
1592 417a6e49 2020-09-26 stsp err = got_ref_open(&symref, repo, refname, 1);
1593 417a6e49 2020-09-26 stsp if (err) {
1594 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1595 417a6e49 2020-09-26 stsp return err;
1596 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1598 417a6e49 2020-09-26 stsp goto done;
1600 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1602 417a6e49 2020-09-26 stsp goto done;
1604 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1605 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n",
1606 417a6e49 2020-09-26 stsp got_ref_get_name(symref),
1607 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1609 417a6e49 2020-09-26 stsp symref_is_locked = 1;
1611 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1612 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref)) == 0)
1613 417a6e49 2020-09-26 stsp goto done;
1615 417a6e49 2020-09-26 stsp err = got_ref_change_symref(symref,
1616 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
1618 417a6e49 2020-09-26 stsp goto done;
1620 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1622 417a6e49 2020-09-26 stsp goto done;
1624 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1625 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1626 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1630 417a6e49 2020-09-26 stsp if (symref_is_locked) {
1631 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(symref);
1632 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1633 417a6e49 2020-09-26 stsp err = unlock_err;
1635 417a6e49 2020-09-26 stsp got_ref_close(symref);
1636 417a6e49 2020-09-26 stsp return err;
1639 417a6e49 2020-09-26 stsp __dead static void
1640 417a6e49 2020-09-26 stsp usage_fetch(void)
1642 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1643 417a6e49 2020-09-26 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1644 417a6e49 2020-09-26 stsp "[remote-repository-name]\n",
1645 417a6e49 2020-09-26 stsp getprogname());
1649 417a6e49 2020-09-26 stsp static const struct got_error *
1650 417a6e49 2020-09-26 stsp delete_missing_ref(struct got_reference *ref,
1651 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1653 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1654 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
1655 417a6e49 2020-09-26 stsp char *id_str = NULL;
1657 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1658 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1660 417a6e49 2020-09-26 stsp return err;
1661 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1662 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1663 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1664 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1667 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
1669 417a6e49 2020-09-26 stsp return err;
1670 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
1672 417a6e49 2020-09-26 stsp goto done;
1674 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1676 417a6e49 2020-09-26 stsp goto done;
1677 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1678 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1679 417a6e49 2020-09-26 stsp got_ref_get_name(ref), id_str);
1684 417a6e49 2020-09-26 stsp free(id_str);
1685 417a6e49 2020-09-26 stsp return NULL;
1688 417a6e49 2020-09-26 stsp static const struct got_error *
1689 417a6e49 2020-09-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1690 417a6e49 2020-09-26 stsp struct got_pathlist_head *their_symrefs,
1691 417a6e49 2020-09-26 stsp const struct got_remote_repo *remote,
1692 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1694 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1695 417a6e49 2020-09-26 stsp struct got_reflist_head my_refs;
1696 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
1697 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1698 417a6e49 2020-09-26 stsp char *remote_namespace = NULL;
1699 417a6e49 2020-09-26 stsp char *local_refname = NULL;
1701 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&my_refs);
1703 417a6e49 2020-09-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1705 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1707 417a6e49 2020-09-26 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1709 417a6e49 2020-09-26 stsp goto done;
1711 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1712 417a6e49 2020-09-26 stsp const char *refname = got_ref_get_name(re->ref);
1714 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
1715 417a6e49 2020-09-26 stsp if (strncmp(refname, remote_namespace,
1716 417a6e49 2020-09-26 stsp strlen(remote_namespace)) == 0) {
1717 417a6e49 2020-09-26 stsp if (strcmp(refname + strlen(remote_namespace),
1718 417a6e49 2020-09-26 stsp GOT_REF_HEAD) == 0)
1720 417a6e49 2020-09-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1721 417a6e49 2020-09-26 stsp refname + strlen(remote_namespace)) == -1) {
1722 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
1723 417a6e49 2020-09-26 stsp goto done;
1725 417a6e49 2020-09-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1729 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1730 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1733 417a6e49 2020-09-26 stsp if (pe != NULL)
1736 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1737 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1740 417a6e49 2020-09-26 stsp if (pe != NULL)
1743 417a6e49 2020-09-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1747 417a6e49 2020-09-26 stsp if (local_refname) {
1748 417a6e49 2020-09-26 stsp struct got_reference *ref;
1749 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1750 417a6e49 2020-09-26 stsp if (err) {
1751 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1753 417a6e49 2020-09-26 stsp free(local_refname);
1754 417a6e49 2020-09-26 stsp local_refname = NULL;
1757 417a6e49 2020-09-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1760 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1761 417a6e49 2020-09-26 stsp got_ref_close(ref);
1762 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL) {
1763 417a6e49 2020-09-26 stsp err = unlock_err;
1767 417a6e49 2020-09-26 stsp free(local_refname);
1768 417a6e49 2020-09-26 stsp local_refname = NULL;
1772 417a6e49 2020-09-26 stsp free(remote_namespace);
1773 417a6e49 2020-09-26 stsp free(local_refname);
1774 417a6e49 2020-09-26 stsp return err;
1777 417a6e49 2020-09-26 stsp static const struct got_error *
1778 417a6e49 2020-09-26 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1779 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1781 417a6e49 2020-09-26 stsp const struct got_error *err, *unlock_err;
1782 417a6e49 2020-09-26 stsp char *remote_refname;
1783 417a6e49 2020-09-26 stsp struct got_reference *ref;
1785 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1786 417a6e49 2020-09-26 stsp refname += 5;
1788 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1789 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1790 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1792 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1793 417a6e49 2020-09-26 stsp if (err) {
1794 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1795 417a6e49 2020-09-26 stsp goto done;
1796 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1798 417a6e49 2020-09-26 stsp err = update_ref(ref, id, 0, verbosity, repo);
1799 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1800 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1801 417a6e49 2020-09-26 stsp err = unlock_err;
1802 417a6e49 2020-09-26 stsp got_ref_close(ref);
1805 417a6e49 2020-09-26 stsp free(remote_refname);
1806 417a6e49 2020-09-26 stsp return err;
1809 417a6e49 2020-09-26 stsp static const struct got_error *
1810 417a6e49 2020-09-26 stsp cmd_fetch(int argc, char *argv[])
1812 417a6e49 2020-09-26 stsp const struct got_error *error = NULL, *unlock_err;
1813 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
1814 417a6e49 2020-09-26 stsp const char *remote_name;
1815 417a6e49 2020-09-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
1816 417a6e49 2020-09-26 stsp char *repo_name = NULL, *server_path = NULL;
1817 417a6e49 2020-09-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
1818 417a6e49 2020-09-26 stsp int nremotes;
1819 417a6e49 2020-09-26 stsp char *id_str = NULL;
1820 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1821 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
1822 417a6e49 2020-09-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1823 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1824 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1825 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1826 417a6e49 2020-09-26 stsp int i, ch, fetchfd = -1, fetchstatus;
1827 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1828 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1829 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1830 417a6e49 2020-09-26 stsp int delete_refs = 0, replace_tags = 0;
1832 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1833 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1834 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1835 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1837 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1838 417a6e49 2020-09-26 stsp switch (ch) {
1840 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1843 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1844 417a6e49 2020-09-26 stsp optarg, NULL);
1845 417a6e49 2020-09-26 stsp if (error)
1846 417a6e49 2020-09-26 stsp return error;
1849 417a6e49 2020-09-26 stsp delete_refs = 1;
1852 417a6e49 2020-09-26 stsp list_refs_only = 1;
1855 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
1856 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1857 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
1859 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
1862 417a6e49 2020-09-26 stsp replace_tags = 1;
1865 417a6e49 2020-09-26 stsp if (verbosity < 0)
1866 417a6e49 2020-09-26 stsp verbosity = 0;
1867 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1868 417a6e49 2020-09-26 stsp verbosity++;
1871 417a6e49 2020-09-26 stsp verbosity = -1;
1874 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1875 417a6e49 2020-09-26 stsp optarg, NULL);
1876 417a6e49 2020-09-26 stsp if (error)
1877 417a6e49 2020-09-26 stsp return error;
1880 417a6e49 2020-09-26 stsp usage_fetch();
1884 417a6e49 2020-09-26 stsp argc -= optind;
1885 417a6e49 2020-09-26 stsp argv += optind;
1887 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1888 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1889 417a6e49 2020-09-26 stsp if (list_refs_only) {
1890 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1891 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1892 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1893 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1894 417a6e49 2020-09-26 stsp if (delete_refs)
1895 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
1896 417a6e49 2020-09-26 stsp if (verbosity == -1)
1897 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1900 417a6e49 2020-09-26 stsp if (argc == 0)
1901 417a6e49 2020-09-26 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1902 417a6e49 2020-09-26 stsp else if (argc == 1)
1903 417a6e49 2020-09-26 stsp remote_name = argv[0];
1905 417a6e49 2020-09-26 stsp usage_fetch();
1907 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
1908 417a6e49 2020-09-26 stsp if (cwd == NULL) {
1909 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
1910 417a6e49 2020-09-26 stsp goto done;
1913 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1914 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
1915 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1916 417a6e49 2020-09-26 stsp goto done;
1918 417a6e49 2020-09-26 stsp error = NULL;
1919 417a6e49 2020-09-26 stsp if (worktree) {
1920 417a6e49 2020-09-26 stsp repo_path =
1921 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
1922 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1923 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1924 417a6e49 2020-09-26 stsp if (error)
1925 417a6e49 2020-09-26 stsp goto done;
1927 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
1928 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1929 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1930 417a6e49 2020-09-26 stsp goto done;
1935 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1936 417a6e49 2020-09-26 stsp if (error)
1937 417a6e49 2020-09-26 stsp goto done;
1939 417a6e49 2020-09-26 stsp if (worktree) {
1940 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1941 417a6e49 2020-09-26 stsp if (worktree_conf) {
1942 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1943 417a6e49 2020-09-26 stsp worktree_conf);
1944 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1945 417a6e49 2020-09-26 stsp remote = &remotes[i];
1946 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1951 417a6e49 2020-09-26 stsp if (remote == NULL) {
1952 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
1953 417a6e49 2020-09-26 stsp if (repo_conf) {
1954 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1955 417a6e49 2020-09-26 stsp repo_conf);
1956 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1957 417a6e49 2020-09-26 stsp remote = &remotes[i];
1958 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1963 417a6e49 2020-09-26 stsp if (remote == NULL) {
1964 417a6e49 2020-09-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1965 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1966 417a6e49 2020-09-26 stsp remote = &remotes[i];
1967 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1971 417a6e49 2020-09-26 stsp if (remote == NULL) {
1972 417a6e49 2020-09-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1973 417a6e49 2020-09-26 stsp goto done;
1976 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1977 417a6e49 2020-09-26 stsp &repo_name, remote->url);
1978 417a6e49 2020-09-26 stsp if (error)
1979 417a6e49 2020-09-26 stsp goto done;
1981 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1982 417a6e49 2020-09-26 stsp #ifndef PROFILE
1983 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1984 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1985 417a6e49 2020-09-26 stsp err(1, "pledge");
1987 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1988 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1989 417a6e49 2020-09-26 stsp #ifndef PROFILE
1990 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1991 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1992 417a6e49 2020-09-26 stsp err(1, "pledge");
1994 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1995 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1996 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1997 417a6e49 2020-09-26 stsp goto done;
1999 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2000 417a6e49 2020-09-26 stsp goto done;
2003 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2004 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2005 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
2006 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
2007 417a6e49 2020-09-26 stsp goto done;
2010 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2011 417a6e49 2020-09-26 stsp if (error)
2012 417a6e49 2020-09-26 stsp goto done;
2014 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2015 417a6e49 2020-09-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2016 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
2018 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2019 417a6e49 2020-09-26 stsp server_path, verbosity);
2020 417a6e49 2020-09-26 stsp if (error)
2021 417a6e49 2020-09-26 stsp goto done;
2023 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
2024 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
2025 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
2026 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
2027 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2028 417a6e49 2020-09-26 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2029 417a6e49 2020-09-26 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2030 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
2031 417a6e49 2020-09-26 stsp if (error)
2032 417a6e49 2020-09-26 stsp goto done;
2034 417a6e49 2020-09-26 stsp if (list_refs_only) {
2035 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
2036 417a6e49 2020-09-26 stsp goto done;
2039 417a6e49 2020-09-26 stsp if (pack_hash == NULL) {
2040 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2041 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2042 417a6e49 2020-09-26 stsp } else if (verbosity >= 0) {
2043 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
2044 417a6e49 2020-09-26 stsp if (error)
2045 417a6e49 2020-09-26 stsp goto done;
2046 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
2047 417a6e49 2020-09-26 stsp free(id_str);
2048 417a6e49 2020-09-26 stsp id_str = NULL;
2051 417a6e49 2020-09-26 stsp /* Update references provided with the pack file. */
2052 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2053 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2054 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
2055 417a6e49 2020-09-26 stsp struct got_reference *ref;
2056 417a6e49 2020-09-26 stsp char *remote_refname;
2058 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2059 417a6e49 2020-09-26 stsp !remote->mirror_references) {
2060 417a6e49 2020-09-26 stsp error = update_wanted_ref(refname, id,
2061 417a6e49 2020-09-26 stsp remote->name, verbosity, repo);
2062 417a6e49 2020-09-26 stsp if (error)
2063 417a6e49 2020-09-26 stsp goto done;
2067 417a6e49 2020-09-26 stsp if (remote->mirror_references ||
2068 417a6e49 2020-09-26 stsp strncmp("refs/tags/", refname, 10) == 0) {
2069 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2070 417a6e49 2020-09-26 stsp if (error) {
2071 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2072 417a6e49 2020-09-26 stsp goto done;
2073 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2075 417a6e49 2020-09-26 stsp if (error)
2076 417a6e49 2020-09-26 stsp goto done;
2078 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2079 417a6e49 2020-09-26 stsp verbosity, repo);
2080 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2081 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2082 417a6e49 2020-09-26 stsp error = unlock_err;
2083 417a6e49 2020-09-26 stsp got_ref_close(ref);
2084 417a6e49 2020-09-26 stsp if (error)
2085 417a6e49 2020-09-26 stsp goto done;
2087 417a6e49 2020-09-26 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2088 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2089 417a6e49 2020-09-26 stsp remote_name, refname + 11) == -1) {
2090 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2091 417a6e49 2020-09-26 stsp goto done;
2094 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2095 417a6e49 2020-09-26 stsp if (error) {
2096 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2097 417a6e49 2020-09-26 stsp goto done;
2098 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id,
2099 417a6e49 2020-09-26 stsp verbosity, repo);
2100 417a6e49 2020-09-26 stsp if (error)
2101 417a6e49 2020-09-26 stsp goto done;
2103 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2104 417a6e49 2020-09-26 stsp verbosity, repo);
2105 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2106 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2107 417a6e49 2020-09-26 stsp error = unlock_err;
2108 417a6e49 2020-09-26 stsp got_ref_close(ref);
2109 417a6e49 2020-09-26 stsp if (error)
2110 417a6e49 2020-09-26 stsp goto done;
2113 417a6e49 2020-09-26 stsp /* Also create a local branch if none exists yet. */
2114 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2115 417a6e49 2020-09-26 stsp if (error) {
2116 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2117 417a6e49 2020-09-26 stsp goto done;
2118 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2120 417a6e49 2020-09-26 stsp if (error)
2121 417a6e49 2020-09-26 stsp goto done;
2123 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2124 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2125 417a6e49 2020-09-26 stsp error = unlock_err;
2126 417a6e49 2020-09-26 stsp got_ref_close(ref);
2130 417a6e49 2020-09-26 stsp if (delete_refs) {
2131 417a6e49 2020-09-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2132 417a6e49 2020-09-26 stsp verbosity, repo);
2133 417a6e49 2020-09-26 stsp if (error)
2134 417a6e49 2020-09-26 stsp goto done;
2137 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
2138 417a6e49 2020-09-26 stsp /* Update remote HEAD reference if the server provided one. */
2139 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2140 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
2141 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2142 417a6e49 2020-09-26 stsp const char *target = pe->data;
2143 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
2145 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2148 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
2151 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2152 417a6e49 2020-09-26 stsp remote->name, refname) == -1) {
2153 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2154 417a6e49 2020-09-26 stsp goto done;
2156 417a6e49 2020-09-26 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2157 417a6e49 2020-09-26 stsp remote->name, target + 11) == -1) {
2158 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2159 417a6e49 2020-09-26 stsp free(remote_refname);
2160 417a6e49 2020-09-26 stsp goto done;
2163 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target,
2165 417a6e49 2020-09-26 stsp if (error) {
2166 417a6e49 2020-09-26 stsp free(remote_refname);
2167 417a6e49 2020-09-26 stsp free(remote_target);
2168 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
2169 417a6e49 2020-09-26 stsp error = NULL;
2172 417a6e49 2020-09-26 stsp goto done;
2174 417a6e49 2020-09-26 stsp error = update_symref(remote_refname, target_ref,
2175 417a6e49 2020-09-26 stsp verbosity, repo);
2176 417a6e49 2020-09-26 stsp free(remote_refname);
2177 417a6e49 2020-09-26 stsp free(remote_target);
2178 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
2179 417a6e49 2020-09-26 stsp if (error)
2180 417a6e49 2020-09-26 stsp goto done;
2184 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
2185 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
2186 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
2187 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2188 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
2190 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2191 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
2193 417a6e49 2020-09-26 stsp got_repo_close(repo);
2194 417a6e49 2020-09-26 stsp if (worktree)
2195 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
2196 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2197 417a6e49 2020-09-26 stsp free((void *)pe->path);
2198 417a6e49 2020-09-26 stsp free(pe->data);
2200 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
2201 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2202 417a6e49 2020-09-26 stsp free((void *)pe->path);
2203 417a6e49 2020-09-26 stsp free(pe->data);
2205 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
2206 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
2207 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
2208 417a6e49 2020-09-26 stsp free(id_str);
2209 417a6e49 2020-09-26 stsp free(cwd);
2210 417a6e49 2020-09-26 stsp free(repo_path);
2211 417a6e49 2020-09-26 stsp free(pack_hash);
2212 417a6e49 2020-09-26 stsp free(proto);
2213 417a6e49 2020-09-26 stsp free(host);
2214 417a6e49 2020-09-26 stsp free(port);
2215 417a6e49 2020-09-26 stsp free(server_path);
2216 417a6e49 2020-09-26 stsp free(repo_name);
2217 417a6e49 2020-09-26 stsp return error;
2221 417a6e49 2020-09-26 stsp __dead static void
2222 417a6e49 2020-09-26 stsp usage_checkout(void)
2224 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2225 417a6e49 2020-09-26 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2229 417a6e49 2020-09-26 stsp static void
2230 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning(void)
2232 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: warning: could not create a reference "
2233 417a6e49 2020-09-26 stsp "to the work tree's base commit; the commit could be "
2234 417a6e49 2020-09-26 stsp "garbage-collected by Git; making the repository "
2235 417a6e49 2020-09-26 stsp "writable and running 'got update' will prevent this\n",
2236 417a6e49 2020-09-26 stsp getprogname());
2239 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg {
2240 417a6e49 2020-09-26 stsp const char *worktree_path;
2241 417a6e49 2020-09-26 stsp int had_base_commit_ref_error;
2244 417a6e49 2020-09-26 stsp static const struct got_error *
2245 417a6e49 2020-09-26 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2247 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg *a = arg;
2249 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2250 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2251 417a6e49 2020-09-26 stsp return NULL;
2253 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2254 417a6e49 2020-09-26 stsp a->had_base_commit_ref_error = 1;
2255 417a6e49 2020-09-26 stsp return NULL;
2258 417a6e49 2020-09-26 stsp while (path[0] == '/')
2261 417a6e49 2020-09-26 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2262 417a6e49 2020-09-26 stsp return NULL;
2265 417a6e49 2020-09-26 stsp static const struct got_error *
2266 417a6e49 2020-09-26 stsp check_cancelled(void *arg)
2268 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
2269 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
2270 417a6e49 2020-09-26 stsp return NULL;
2273 417a6e49 2020-09-26 stsp static const struct got_error *
2274 417a6e49 2020-09-26 stsp check_linear_ancestry(struct got_object_id *commit_id,
2275 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2276 417a6e49 2020-09-26 stsp struct got_repository *repo)
2278 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2279 417a6e49 2020-09-26 stsp struct got_object_id *yca_id;
2281 417a6e49 2020-09-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2282 417a6e49 2020-09-26 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2284 417a6e49 2020-09-26 stsp return err;
2286 417a6e49 2020-09-26 stsp if (yca_id == NULL)
2287 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2290 417a6e49 2020-09-26 stsp * Require a straight line of history between the target commit
2291 417a6e49 2020-09-26 stsp * and the work tree's base commit.
2293 417a6e49 2020-09-26 stsp * Non-linear situations such as this require a rebase:
2295 417a6e49 2020-09-26 stsp * (commit) D F (base_commit)
2303 417a6e49 2020-09-26 stsp * 'got update' only handles linear cases:
2304 417a6e49 2020-09-26 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2305 417a6e49 2020-09-26 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2307 417a6e49 2020-09-26 stsp if (allow_forwards_in_time_only) {
2308 417a6e49 2020-09-26 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2310 417a6e49 2020-09-26 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2311 417a6e49 2020-09-26 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2312 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2314 417a6e49 2020-09-26 stsp free(yca_id);
2315 417a6e49 2020-09-26 stsp return NULL;
2318 417a6e49 2020-09-26 stsp static const struct got_error *
2319 417a6e49 2020-09-26 stsp check_same_branch(struct got_object_id *commit_id,
2320 417a6e49 2020-09-26 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2321 417a6e49 2020-09-26 stsp struct got_repository *repo)
2323 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2324 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
2325 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
2326 417a6e49 2020-09-26 stsp int is_same_branch = 0;
2328 417a6e49 2020-09-26 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2330 417a6e49 2020-09-26 stsp goto done;
2332 417a6e49 2020-09-26 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2333 417a6e49 2020-09-26 stsp is_same_branch = 1;
2334 417a6e49 2020-09-26 stsp goto done;
2336 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2337 417a6e49 2020-09-26 stsp is_same_branch = 1;
2338 417a6e49 2020-09-26 stsp goto done;
2341 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
2343 417a6e49 2020-09-26 stsp goto done;
2345 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2346 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2348 417a6e49 2020-09-26 stsp goto done;
2350 417a6e49 2020-09-26 stsp for (;;) {
2351 417a6e49 2020-09-26 stsp struct got_object_id *id;
2352 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2353 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2354 417a6e49 2020-09-26 stsp if (err) {
2355 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2356 417a6e49 2020-09-26 stsp err = NULL;
2361 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2363 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2364 417a6e49 2020-09-26 stsp is_same_branch = 1;
2370 417a6e49 2020-09-26 stsp if (graph)
2371 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
2372 417a6e49 2020-09-26 stsp free(head_commit_id);
2373 417a6e49 2020-09-26 stsp if (!err && !is_same_branch)
2374 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_ANCESTRY);
2375 417a6e49 2020-09-26 stsp return err;
2378 417a6e49 2020-09-26 stsp static const struct got_error *
2379 417a6e49 2020-09-26 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2381 417a6e49 2020-09-26 stsp static char msg[512];
2382 417a6e49 2020-09-26 stsp const char *branch_name;
2384 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref))
2385 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(ref);
2387 417a6e49 2020-09-26 stsp branch_name = got_ref_get_name(ref);
2389 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2390 417a6e49 2020-09-26 stsp branch_name += 11;
2392 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2393 417a6e49 2020-09-26 stsp "target commit is not contained in branch '%s'; "
2394 417a6e49 2020-09-26 stsp "the branch to use must be specified with -b; "
2395 417a6e49 2020-09-26 stsp "if necessary a new branch can be created for "
2396 417a6e49 2020-09-26 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2397 417a6e49 2020-09-26 stsp branch_name, commit_id_str);
2399 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2402 417a6e49 2020-09-26 stsp static const struct got_error *
2403 417a6e49 2020-09-26 stsp cmd_checkout(int argc, char *argv[])
2405 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2406 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2407 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2408 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2409 417a6e49 2020-09-26 stsp char *repo_path = NULL;
2410 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2411 417a6e49 2020-09-26 stsp const char *path_prefix = "";
2412 417a6e49 2020-09-26 stsp const char *branch_name = GOT_REF_HEAD;
2413 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2414 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
2415 417a6e49 2020-09-26 stsp int ch, same_path_prefix, allow_nonempty = 0;
2416 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2417 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg cpa;
2419 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2421 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2422 417a6e49 2020-09-26 stsp switch (ch) {
2424 417a6e49 2020-09-26 stsp branch_name = optarg;
2427 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2428 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2429 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2432 417a6e49 2020-09-26 stsp allow_nonempty = 1;
2435 417a6e49 2020-09-26 stsp path_prefix = optarg;
2438 417a6e49 2020-09-26 stsp usage_checkout();
2439 417a6e49 2020-09-26 stsp /* NOTREACHED */
2443 417a6e49 2020-09-26 stsp argc -= optind;
2444 417a6e49 2020-09-26 stsp argv += optind;
2446 417a6e49 2020-09-26 stsp #ifndef PROFILE
2447 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2448 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2449 417a6e49 2020-09-26 stsp err(1, "pledge");
2451 417a6e49 2020-09-26 stsp if (argc == 1) {
2452 417a6e49 2020-09-26 stsp char *base, *dotgit;
2453 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2454 417a6e49 2020-09-26 stsp if (repo_path == NULL)
2455 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath", argv[0]);
2456 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
2457 417a6e49 2020-09-26 stsp if (cwd == NULL) {
2458 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2459 417a6e49 2020-09-26 stsp goto done;
2461 417a6e49 2020-09-26 stsp if (path_prefix[0])
2462 417a6e49 2020-09-26 stsp path = strdup(path_prefix);
2464 417a6e49 2020-09-26 stsp path = strdup(repo_path);
2465 417a6e49 2020-09-26 stsp if (path == NULL) {
2466 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2467 417a6e49 2020-09-26 stsp goto done;
2469 417a6e49 2020-09-26 stsp base = basename(path);
2470 417a6e49 2020-09-26 stsp if (base == NULL) {
2471 417a6e49 2020-09-26 stsp error = got_error_from_errno2("basename", path);
2472 417a6e49 2020-09-26 stsp goto done;
2474 417a6e49 2020-09-26 stsp dotgit = strstr(base, ".git");
2475 417a6e49 2020-09-26 stsp if (dotgit)
2476 417a6e49 2020-09-26 stsp *dotgit = '\0';
2477 417a6e49 2020-09-26 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2478 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2479 417a6e49 2020-09-26 stsp goto done;
2481 417a6e49 2020-09-26 stsp } else if (argc == 2) {
2482 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2483 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
2484 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
2485 417a6e49 2020-09-26 stsp goto done;
2487 417a6e49 2020-09-26 stsp worktree_path = realpath(argv[1], NULL);
2488 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2489 417a6e49 2020-09-26 stsp if (errno != ENOENT) {
2490 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
2492 417a6e49 2020-09-26 stsp goto done;
2494 417a6e49 2020-09-26 stsp worktree_path = strdup(argv[1]);
2495 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2496 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2497 417a6e49 2020-09-26 stsp goto done;
2501 417a6e49 2020-09-26 stsp usage_checkout();
2503 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
2504 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(worktree_path);
2506 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
2507 417a6e49 2020-09-26 stsp if (error != NULL)
2508 417a6e49 2020-09-26 stsp goto done;
2510 417a6e49 2020-09-26 stsp /* Pre-create work tree path for unveil(2) */
2511 417a6e49 2020-09-26 stsp error = got_path_mkdir(worktree_path);
2512 417a6e49 2020-09-26 stsp if (error) {
2513 417a6e49 2020-09-26 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2514 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2515 417a6e49 2020-09-26 stsp goto done;
2516 417a6e49 2020-09-26 stsp if (!allow_nonempty &&
2517 417a6e49 2020-09-26 stsp !got_path_dir_is_empty(worktree_path)) {
2518 417a6e49 2020-09-26 stsp error = got_error_path(worktree_path,
2519 417a6e49 2020-09-26 stsp GOT_ERR_DIR_NOT_EMPTY);
2520 417a6e49 2020-09-26 stsp goto done;
2524 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2525 417a6e49 2020-09-26 stsp if (error)
2526 417a6e49 2020-09-26 stsp goto done;
2528 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2529 417a6e49 2020-09-26 stsp if (error != NULL)
2530 417a6e49 2020-09-26 stsp goto done;
2532 417a6e49 2020-09-26 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2533 417a6e49 2020-09-26 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2534 417a6e49 2020-09-26 stsp goto done;
2536 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2537 417a6e49 2020-09-26 stsp if (error != NULL)
2538 417a6e49 2020-09-26 stsp goto done;
2540 417a6e49 2020-09-26 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2541 417a6e49 2020-09-26 stsp path_prefix);
2542 417a6e49 2020-09-26 stsp if (error != NULL)
2543 417a6e49 2020-09-26 stsp goto done;
2544 417a6e49 2020-09-26 stsp if (!same_path_prefix) {
2545 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2546 417a6e49 2020-09-26 stsp goto done;
2549 417a6e49 2020-09-26 stsp if (commit_id_str) {
2550 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
2551 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2552 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2553 417a6e49 2020-09-26 stsp if (error)
2554 417a6e49 2020-09-26 stsp goto done;
2555 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2556 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2557 417a6e49 2020-09-26 stsp if (error != NULL) {
2558 417a6e49 2020-09-26 stsp free(commit_id);
2559 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2560 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2561 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2563 417a6e49 2020-09-26 stsp goto done;
2565 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2566 417a6e49 2020-09-26 stsp if (error) {
2567 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2568 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2569 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2571 417a6e49 2020-09-26 stsp goto done;
2573 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2574 417a6e49 2020-09-26 stsp commit_id);
2575 417a6e49 2020-09-26 stsp free(commit_id);
2576 417a6e49 2020-09-26 stsp if (error)
2577 417a6e49 2020-09-26 stsp goto done;
2580 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
2581 417a6e49 2020-09-26 stsp if (error)
2582 417a6e49 2020-09-26 stsp goto done;
2583 417a6e49 2020-09-26 stsp cpa.worktree_path = worktree_path;
2584 417a6e49 2020-09-26 stsp cpa.had_base_commit_ref_error = 0;
2585 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2586 417a6e49 2020-09-26 stsp checkout_progress, &cpa, check_cancelled, NULL);
2587 417a6e49 2020-09-26 stsp if (error != NULL)
2588 417a6e49 2020-09-26 stsp goto done;
2590 417a6e49 2020-09-26 stsp printf("Now shut up and hack\n");
2591 417a6e49 2020-09-26 stsp if (cpa.had_base_commit_ref_error)
2592 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning();
2594 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2595 417a6e49 2020-09-26 stsp free(commit_id_str);
2596 417a6e49 2020-09-26 stsp free(repo_path);
2597 417a6e49 2020-09-26 stsp free(worktree_path);
2598 417a6e49 2020-09-26 stsp free(cwd);
2599 417a6e49 2020-09-26 stsp free(path);
2600 417a6e49 2020-09-26 stsp return error;
2603 417a6e49 2020-09-26 stsp struct got_update_progress_arg {
2604 417a6e49 2020-09-26 stsp int did_something;
2605 417a6e49 2020-09-26 stsp int conflicts;
2606 417a6e49 2020-09-26 stsp int obstructed;
2607 417a6e49 2020-09-26 stsp int not_updated;
2611 417a6e49 2020-09-26 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2613 417a6e49 2020-09-26 stsp if (!upa->did_something)
2616 417a6e49 2020-09-26 stsp if (upa->conflicts > 0)
2617 417a6e49 2020-09-26 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2618 417a6e49 2020-09-26 stsp if (upa->obstructed > 0)
2619 417a6e49 2020-09-26 stsp printf("File paths obstructed by a non-regular file: %d\n",
2620 417a6e49 2020-09-26 stsp upa->obstructed);
2621 417a6e49 2020-09-26 stsp if (upa->not_updated > 0)
2622 417a6e49 2020-09-26 stsp printf("Files not updated because of existing merge "
2623 417a6e49 2020-09-26 stsp "conflicts: %d\n", upa->not_updated);
2626 417a6e49 2020-09-26 stsp __dead static void
2627 417a6e49 2020-09-26 stsp usage_update(void)
2629 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2630 417a6e49 2020-09-26 stsp getprogname());
2634 417a6e49 2020-09-26 stsp static const struct got_error *
2635 417a6e49 2020-09-26 stsp update_progress(void *arg, unsigned char status, const char *path)
2637 417a6e49 2020-09-26 stsp struct got_update_progress_arg *upa = arg;
2639 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_EXISTS ||
2640 417a6e49 2020-09-26 stsp status == GOT_STATUS_BASE_REF_ERR)
2641 417a6e49 2020-09-26 stsp return NULL;
2643 417a6e49 2020-09-26 stsp upa->did_something = 1;
2645 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2646 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2647 417a6e49 2020-09-26 stsp return NULL;
2649 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CONFLICT)
2650 417a6e49 2020-09-26 stsp upa->conflicts++;
2651 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_OBSTRUCTED)
2652 417a6e49 2020-09-26 stsp upa->obstructed++;
2653 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2654 417a6e49 2020-09-26 stsp upa->not_updated++;
2656 417a6e49 2020-09-26 stsp while (path[0] == '/')
2658 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
2659 417a6e49 2020-09-26 stsp return NULL;
2662 417a6e49 2020-09-26 stsp static const struct got_error *
2663 417a6e49 2020-09-26 stsp switch_head_ref(struct got_reference *head_ref,
2664 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2665 417a6e49 2020-09-26 stsp struct got_repository *repo)
2667 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2668 417a6e49 2020-09-26 stsp char *base_id_str;
2669 417a6e49 2020-09-26 stsp int ref_has_moved = 0;
2671 417a6e49 2020-09-26 stsp /* Trivial case: switching between two different references. */
2672 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_name(head_ref),
2673 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2674 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n",
2675 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree),
2676 417a6e49 2020-09-26 stsp got_ref_get_name(head_ref));
2677 417a6e49 2020-09-26 stsp return got_worktree_set_head_ref(worktree, head_ref);
2680 417a6e49 2020-09-26 stsp err = check_linear_ancestry(commit_id,
2681 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2682 417a6e49 2020-09-26 stsp if (err) {
2683 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_ANCESTRY)
2684 417a6e49 2020-09-26 stsp return err;
2685 417a6e49 2020-09-26 stsp ref_has_moved = 1;
2687 417a6e49 2020-09-26 stsp if (!ref_has_moved)
2688 417a6e49 2020-09-26 stsp return NULL;
2690 417a6e49 2020-09-26 stsp /* Switching to a rebased branch with the same reference name. */
2691 417a6e49 2020-09-26 stsp err = got_object_id_str(&base_id_str,
2692 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
2694 417a6e49 2020-09-26 stsp return err;
2695 417a6e49 2020-09-26 stsp printf("Reference %s now points at a different branch\n",
2696 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2697 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2698 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2699 417a6e49 2020-09-26 stsp return NULL;
2702 417a6e49 2020-09-26 stsp static const struct got_error *
2703 417a6e49 2020-09-26 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2705 417a6e49 2020-09-26 stsp const struct got_error *err;
2706 417a6e49 2020-09-26 stsp int in_progress;
2708 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2710 417a6e49 2020-09-26 stsp return err;
2711 417a6e49 2020-09-26 stsp if (in_progress)
2712 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_REBASING);
2714 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2716 417a6e49 2020-09-26 stsp return err;
2717 417a6e49 2020-09-26 stsp if (in_progress)
2718 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2720 417a6e49 2020-09-26 stsp return NULL;
2723 417a6e49 2020-09-26 stsp static const struct got_error *
2724 417a6e49 2020-09-26 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2725 417a6e49 2020-09-26 stsp char *argv[], struct got_worktree *worktree)
2727 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2728 417a6e49 2020-09-26 stsp char *path;
2731 417a6e49 2020-09-26 stsp if (argc == 0) {
2732 417a6e49 2020-09-26 stsp path = strdup("");
2733 417a6e49 2020-09-26 stsp if (path == NULL)
2734 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2735 417a6e49 2020-09-26 stsp return got_pathlist_append(paths, path, NULL);
2738 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
2739 417a6e49 2020-09-26 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2742 417a6e49 2020-09-26 stsp err = got_pathlist_append(paths, path, NULL);
2743 417a6e49 2020-09-26 stsp if (err) {
2744 417a6e49 2020-09-26 stsp free(path);
2749 417a6e49 2020-09-26 stsp return err;
2752 417a6e49 2020-09-26 stsp static const struct got_error *
2753 417a6e49 2020-09-26 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2754 417a6e49 2020-09-26 stsp const char *cmdname, const char *path)
2756 417a6e49 2020-09-26 stsp const struct got_error *err;
2757 417a6e49 2020-09-26 stsp struct got_repository *repo;
2758 417a6e49 2020-09-26 stsp static char msg[512];
2760 417a6e49 2020-09-26 stsp err = got_repo_open(&repo, path, NULL);
2762 417a6e49 2020-09-26 stsp return orig_err;
2764 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2765 417a6e49 2020-09-26 stsp "'got %s' needs a work tree in addition to a git repository\n"
2766 417a6e49 2020-09-26 stsp "Work trees can be checked out from this Git repository with "
2767 417a6e49 2020-09-26 stsp "'got checkout'.\n"
2768 417a6e49 2020-09-26 stsp "The got(1) manual page contains more information.", cmdname);
2769 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2770 417a6e49 2020-09-26 stsp got_repo_close(repo);
2771 417a6e49 2020-09-26 stsp return err;
2774 417a6e49 2020-09-26 stsp static const struct got_error *
2775 417a6e49 2020-09-26 stsp cmd_update(int argc, char *argv[])
2777 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2778 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2779 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2780 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2781 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
2782 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2783 417a6e49 2020-09-26 stsp const char *branch_name = NULL;
2784 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2785 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2786 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
2788 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
2790 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2792 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2793 417a6e49 2020-09-26 stsp switch (ch) {
2795 417a6e49 2020-09-26 stsp branch_name = optarg;
2798 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2799 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2800 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2803 417a6e49 2020-09-26 stsp usage_update();
2804 417a6e49 2020-09-26 stsp /* NOTREACHED */
2808 417a6e49 2020-09-26 stsp argc -= optind;
2809 417a6e49 2020-09-26 stsp argv += optind;
2811 417a6e49 2020-09-26 stsp #ifndef PROFILE
2812 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2813 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2814 417a6e49 2020-09-26 stsp err(1, "pledge");
2816 417a6e49 2020-09-26 stsp worktree_path = getcwd(NULL, 0);
2817 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2818 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2819 417a6e49 2020-09-26 stsp goto done;
2821 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2822 417a6e49 2020-09-26 stsp if (error) {
2823 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2824 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "update",
2825 417a6e49 2020-09-26 stsp worktree_path);
2826 417a6e49 2020-09-26 stsp goto done;
2829 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
2830 417a6e49 2020-09-26 stsp if (error)
2831 417a6e49 2020-09-26 stsp goto done;
2833 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2835 417a6e49 2020-09-26 stsp if (error != NULL)
2836 417a6e49 2020-09-26 stsp goto done;
2838 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2839 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
2840 417a6e49 2020-09-26 stsp if (error)
2841 417a6e49 2020-09-26 stsp goto done;
2843 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2844 417a6e49 2020-09-26 stsp if (error)
2845 417a6e49 2020-09-26 stsp goto done;
2847 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2848 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
2849 417a6e49 2020-09-26 stsp if (error != NULL)
2850 417a6e49 2020-09-26 stsp goto done;
2851 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
2852 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2853 417a6e49 2020-09-26 stsp if (error != NULL)
2854 417a6e49 2020-09-26 stsp goto done;
2855 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2856 417a6e49 2020-09-26 stsp if (error != NULL)
2857 417a6e49 2020-09-26 stsp goto done;
2859 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2860 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2861 417a6e49 2020-09-26 stsp free(commit_id_str);
2862 417a6e49 2020-09-26 stsp commit_id_str = NULL;
2863 417a6e49 2020-09-26 stsp if (error)
2864 417a6e49 2020-09-26 stsp goto done;
2865 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2866 417a6e49 2020-09-26 stsp if (error)
2867 417a6e49 2020-09-26 stsp goto done;
2870 417a6e49 2020-09-26 stsp if (branch_name) {
2871 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id;
2872 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
2873 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
2875 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2876 417a6e49 2020-09-26 stsp "switching between branches requires that "
2877 417a6e49 2020-09-26 stsp "the entire work tree gets updated");
2878 417a6e49 2020-09-26 stsp goto done;
2880 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2881 417a6e49 2020-09-26 stsp if (error)
2882 417a6e49 2020-09-26 stsp goto done;
2883 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2885 417a6e49 2020-09-26 stsp free(head_commit_id);
2886 417a6e49 2020-09-26 stsp if (error != NULL)
2887 417a6e49 2020-09-26 stsp goto done;
2888 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2889 417a6e49 2020-09-26 stsp if (error)
2890 417a6e49 2020-09-26 stsp goto done;
2891 417a6e49 2020-09-26 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2892 417a6e49 2020-09-26 stsp if (error)
2893 417a6e49 2020-09-26 stsp goto done;
2895 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2896 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2897 417a6e49 2020-09-26 stsp if (error != NULL) {
2898 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
2899 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2900 417a6e49 2020-09-26 stsp goto done;
2902 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2903 417a6e49 2020-09-26 stsp if (error)
2904 417a6e49 2020-09-26 stsp goto done;
2907 417a6e49 2020-09-26 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2908 417a6e49 2020-09-26 stsp commit_id) != 0) {
2909 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2910 417a6e49 2020-09-26 stsp commit_id);
2911 417a6e49 2020-09-26 stsp if (error)
2912 417a6e49 2020-09-26 stsp goto done;
2915 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
2916 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2917 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
2918 417a6e49 2020-09-26 stsp if (error != NULL)
2919 417a6e49 2020-09-26 stsp goto done;
2921 417a6e49 2020-09-26 stsp if (upa.did_something)
2922 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
2924 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2925 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
2927 417a6e49 2020-09-26 stsp free(worktree_path);
2928 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
2929 417a6e49 2020-09-26 stsp free((char *)pe->path);
2930 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2931 417a6e49 2020-09-26 stsp free(commit_id);
2932 417a6e49 2020-09-26 stsp free(commit_id_str);
2933 417a6e49 2020-09-26 stsp return error;
2936 417a6e49 2020-09-26 stsp static const struct got_error *
2937 417a6e49 2020-09-26 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2938 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2939 417a6e49 2020-09-26 stsp struct got_repository *repo)
2941 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2942 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2944 417a6e49 2020-09-26 stsp if (blob_id1) {
2945 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2947 417a6e49 2020-09-26 stsp goto done;
2950 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2952 417a6e49 2020-09-26 stsp goto done;
2954 417a6e49 2020-09-26 stsp while (path[0] == '/')
2956 417a6e49 2020-09-26 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2957 417a6e49 2020-09-26 stsp ignore_whitespace, stdout);
2959 417a6e49 2020-09-26 stsp if (blob1)
2960 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
2961 417a6e49 2020-09-26 stsp got_object_blob_close(blob2);
2962 417a6e49 2020-09-26 stsp return err;
2965 417a6e49 2020-09-26 stsp static const struct got_error *
2966 417a6e49 2020-09-26 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2967 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2968 417a6e49 2020-09-26 stsp struct got_repository *repo)
2970 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2971 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2972 417a6e49 2020-09-26 stsp struct got_diff_blob_output_unidiff_arg arg;
2974 417a6e49 2020-09-26 stsp if (tree_id1) {
2975 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2977 417a6e49 2020-09-26 stsp goto done;
2980 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2982 417a6e49 2020-09-26 stsp goto done;
2984 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
2985 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
2986 417a6e49 2020-09-26 stsp arg.outfile = stdout;
2987 417a6e49 2020-09-26 stsp while (path[0] == '/')
2989 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2990 417a6e49 2020-09-26 stsp got_diff_blob_output_unidiff, &arg, 1);
2992 417a6e49 2020-09-26 stsp if (tree1)
2993 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
2994 417a6e49 2020-09-26 stsp if (tree2)
2995 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
2996 417a6e49 2020-09-26 stsp return err;
2999 417a6e49 2020-09-26 stsp static const struct got_error *
3000 417a6e49 2020-09-26 stsp get_changed_paths(struct got_pathlist_head *paths,
3001 417a6e49 2020-09-26 stsp struct got_commit_object *commit, struct got_repository *repo)
3003 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3004 417a6e49 2020-09-26 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3005 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3006 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3008 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3009 417a6e49 2020-09-26 stsp if (qid != NULL) {
3010 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit;
3011 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3014 417a6e49 2020-09-26 stsp return err;
3016 417a6e49 2020-09-26 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3017 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3021 417a6e49 2020-09-26 stsp if (tree_id1) {
3022 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3024 417a6e49 2020-09-26 stsp goto done;
3027 417a6e49 2020-09-26 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3028 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3030 417a6e49 2020-09-26 stsp goto done;
3032 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3033 417a6e49 2020-09-26 stsp got_diff_tree_collect_changed_paths, paths, 0);
3035 417a6e49 2020-09-26 stsp if (tree1)
3036 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
3037 417a6e49 2020-09-26 stsp if (tree2)
3038 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
3039 417a6e49 2020-09-26 stsp return err;
3042 417a6e49 2020-09-26 stsp static const struct got_error *
3043 417a6e49 2020-09-26 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3044 417a6e49 2020-09-26 stsp const char *path, int diff_context, struct got_repository *repo)
3046 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3047 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit = NULL;
3048 417a6e49 2020-09-26 stsp char *id_str1 = NULL, *id_str2 = NULL;
3049 417a6e49 2020-09-26 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3050 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3052 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3053 417a6e49 2020-09-26 stsp if (qid != NULL) {
3054 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3057 417a6e49 2020-09-26 stsp return err;
3060 417a6e49 2020-09-26 stsp if (path && path[0] != '\0') {
3061 417a6e49 2020-09-26 stsp int obj_type;
3062 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3064 417a6e49 2020-09-26 stsp goto done;
3065 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3066 417a6e49 2020-09-26 stsp if (err) {
3067 417a6e49 2020-09-26 stsp free(obj_id2);
3068 417a6e49 2020-09-26 stsp goto done;
3070 417a6e49 2020-09-26 stsp if (pcommit) {
3071 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id1, repo,
3072 417a6e49 2020-09-26 stsp qid->id, path);
3073 417a6e49 2020-09-26 stsp if (err) {
3074 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3075 417a6e49 2020-09-26 stsp free(obj_id2);
3076 417a6e49 2020-09-26 stsp goto done;
3079 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3080 417a6e49 2020-09-26 stsp if (err) {
3081 417a6e49 2020-09-26 stsp free(obj_id2);
3082 417a6e49 2020-09-26 stsp goto done;
3086 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3087 417a6e49 2020-09-26 stsp if (err) {
3088 417a6e49 2020-09-26 stsp free(obj_id2);
3089 417a6e49 2020-09-26 stsp goto done;
3091 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3092 417a6e49 2020-09-26 stsp switch (obj_type) {
3093 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
3094 417a6e49 2020-09-26 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3097 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
3098 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3102 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3105 417a6e49 2020-09-26 stsp free(obj_id1);
3106 417a6e49 2020-09-26 stsp free(obj_id2);
3108 417a6e49 2020-09-26 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3109 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3111 417a6e49 2020-09-26 stsp goto done;
3112 417a6e49 2020-09-26 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3113 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3115 417a6e49 2020-09-26 stsp goto done;
3116 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3117 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3120 417a6e49 2020-09-26 stsp free(id_str1);
3121 417a6e49 2020-09-26 stsp free(id_str2);
3122 417a6e49 2020-09-26 stsp if (pcommit)
3123 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3124 417a6e49 2020-09-26 stsp return err;
3127 417a6e49 2020-09-26 stsp static char *
3128 417a6e49 2020-09-26 stsp get_datestr(time_t *time, char *datebuf)
3130 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
3131 417a6e49 2020-09-26 stsp char *p, *s;
3133 417a6e49 2020-09-26 stsp tm = gmtime_r(time, &mytm);
3134 417a6e49 2020-09-26 stsp if (tm == NULL)
3135 417a6e49 2020-09-26 stsp return NULL;
3136 417a6e49 2020-09-26 stsp s = asctime_r(tm, datebuf);
3137 417a6e49 2020-09-26 stsp if (s == NULL)
3138 417a6e49 2020-09-26 stsp return NULL;
3139 417a6e49 2020-09-26 stsp p = strchr(s, '\n');
3141 417a6e49 2020-09-26 stsp *p = '\0';
3145 417a6e49 2020-09-26 stsp static const struct got_error *
3146 417a6e49 2020-09-26 stsp match_logmsg(int *have_match, struct got_object_id *id,
3147 417a6e49 2020-09-26 stsp struct got_commit_object *commit, regex_t *regex)
3149 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3150 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3151 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
3153 417a6e49 2020-09-26 stsp *have_match = 0;
3155 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3157 417a6e49 2020-09-26 stsp return err;
3159 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3161 417a6e49 2020-09-26 stsp goto done;
3163 417a6e49 2020-09-26 stsp if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3164 417a6e49 2020-09-26 stsp *have_match = 1;
3166 417a6e49 2020-09-26 stsp free(id_str);
3167 417a6e49 2020-09-26 stsp free(logmsg);
3168 417a6e49 2020-09-26 stsp return err;
3171 417a6e49 2020-09-26 stsp static void
3172 417a6e49 2020-09-26 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3173 417a6e49 2020-09-26 stsp regex_t *regex)
3175 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3176 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3178 417a6e49 2020-09-26 stsp *have_match = 0;
3180 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3181 417a6e49 2020-09-26 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3182 417a6e49 2020-09-26 stsp *have_match = 1;
3188 417a6e49 2020-09-26 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3190 417a6e49 2020-09-26 stsp static const struct got_error *
3191 417a6e49 2020-09-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3192 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path,
3193 417a6e49 2020-09-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3194 417a6e49 2020-09-26 stsp int diff_context, struct got_reflist_head *refs)
3196 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3197 417a6e49 2020-09-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3198 417a6e49 2020-09-26 stsp char datebuf[26];
3199 417a6e49 2020-09-26 stsp time_t committer_time;
3200 417a6e49 2020-09-26 stsp const char *author, *committer;
3201 417a6e49 2020-09-26 stsp char *refs_str = NULL;
3202 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
3204 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3206 417a6e49 2020-09-26 stsp const char *name;
3207 417a6e49 2020-09-26 stsp struct got_tag_object *tag = NULL;
3208 417a6e49 2020-09-26 stsp struct got_object_id *ref_id;
3211 417a6e49 2020-09-26 stsp name = got_ref_get_name(re->ref);
3212 417a6e49 2020-09-26 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3214 417a6e49 2020-09-26 stsp if (strncmp(name, "refs/", 5) == 0)
3215 417a6e49 2020-09-26 stsp name += 5;
3216 417a6e49 2020-09-26 stsp if (strncmp(name, "got/", 4) == 0)
3218 417a6e49 2020-09-26 stsp if (strncmp(name, "heads/", 6) == 0)
3219 417a6e49 2020-09-26 stsp name += 6;
3220 417a6e49 2020-09-26 stsp if (strncmp(name, "remotes/", 8) == 0) {
3221 417a6e49 2020-09-26 stsp name += 8;
3222 417a6e49 2020-09-26 stsp s = strstr(name, "/" GOT_REF_HEAD);
3223 417a6e49 2020-09-26 stsp if (s != NULL && s[strlen(s)] == '\0')
3226 417a6e49 2020-09-26 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3228 417a6e49 2020-09-26 stsp return err;
3229 417a6e49 2020-09-26 stsp if (strncmp(name, "tags/", 5) == 0) {
3230 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3231 417a6e49 2020-09-26 stsp if (err) {
3232 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3233 417a6e49 2020-09-26 stsp free(ref_id);
3234 417a6e49 2020-09-26 stsp return err;
3236 417a6e49 2020-09-26 stsp /* Ref points at something other than a tag. */
3237 417a6e49 2020-09-26 stsp err = NULL;
3238 417a6e49 2020-09-26 stsp tag = NULL;
3241 417a6e49 2020-09-26 stsp cmp = got_object_id_cmp(tag ?
3242 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3243 417a6e49 2020-09-26 stsp free(ref_id);
3245 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3246 417a6e49 2020-09-26 stsp if (cmp != 0)
3248 417a6e49 2020-09-26 stsp s = refs_str;
3249 417a6e49 2020-09-26 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3250 417a6e49 2020-09-26 stsp name) == -1) {
3251 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3253 417a6e49 2020-09-26 stsp return err;
3257 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3259 417a6e49 2020-09-26 stsp return err;
3261 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
3262 417a6e49 2020-09-26 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3263 417a6e49 2020-09-26 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3264 417a6e49 2020-09-26 stsp free(id_str);
3265 417a6e49 2020-09-26 stsp id_str = NULL;
3266 417a6e49 2020-09-26 stsp free(refs_str);
3267 417a6e49 2020-09-26 stsp refs_str = NULL;
3268 417a6e49 2020-09-26 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3269 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
3270 417a6e49 2020-09-26 stsp datestr = get_datestr(&committer_time, datebuf);
3271 417a6e49 2020-09-26 stsp if (datestr)
3272 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
3273 417a6e49 2020-09-26 stsp author = got_object_commit_get_author(commit);
3274 417a6e49 2020-09-26 stsp committer = got_object_commit_get_committer(commit);
3275 417a6e49 2020-09-26 stsp if (strcmp(author, committer) != 0)
3276 417a6e49 2020-09-26 stsp printf("via: %s\n", committer);
3277 417a6e49 2020-09-26 stsp if (got_object_commit_get_nparents(commit) > 1) {
3278 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
3279 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3280 417a6e49 2020-09-26 stsp int n = 1;
3281 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3282 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3283 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
3285 417a6e49 2020-09-26 stsp return err;
3286 417a6e49 2020-09-26 stsp printf("parent %d: %s\n", n++, id_str);
3287 417a6e49 2020-09-26 stsp free(id_str);
3291 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3293 417a6e49 2020-09-26 stsp return err;
3295 417a6e49 2020-09-26 stsp logmsg = logmsg0;
3297 417a6e49 2020-09-26 stsp line = strsep(&logmsg, "\n");
3299 417a6e49 2020-09-26 stsp printf(" %s\n", line);
3300 417a6e49 2020-09-26 stsp } while (line);
3301 417a6e49 2020-09-26 stsp free(logmsg0);
3303 417a6e49 2020-09-26 stsp if (changed_paths) {
3304 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3305 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3306 417a6e49 2020-09-26 stsp struct got_diff_changed_path *cp = pe->data;
3307 417a6e49 2020-09-26 stsp printf(" %c %s\n", cp->status, pe->path);
3309 417a6e49 2020-09-26 stsp printf("\n");
3311 417a6e49 2020-09-26 stsp if (show_patch) {
3312 417a6e49 2020-09-26 stsp err = print_patch(commit, id, path, diff_context, repo);
3313 417a6e49 2020-09-26 stsp if (err == 0)
3314 417a6e49 2020-09-26 stsp printf("\n");
3317 417a6e49 2020-09-26 stsp if (fflush(stdout) != 0 && err == NULL)
3318 417a6e49 2020-09-26 stsp err = got_error_from_errno("fflush");
3319 417a6e49 2020-09-26 stsp return err;
3322 417a6e49 2020-09-26 stsp static const struct got_error *
3323 417a6e49 2020-09-26 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3324 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3325 417a6e49 2020-09-26 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3326 417a6e49 2020-09-26 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3328 417a6e49 2020-09-26 stsp const struct got_error *err;
3329 417a6e49 2020-09-26 stsp struct got_commit_graph *graph;
3330 417a6e49 2020-09-26 stsp regex_t regex;
3331 417a6e49 2020-09-26 stsp int have_match;
3332 417a6e49 2020-09-26 stsp struct got_object_id_queue reversed_commits;
3333 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3334 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
3335 417a6e49 2020-09-26 stsp struct got_pathlist_head changed_paths;
3336 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3338 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&reversed_commits);
3339 417a6e49 2020-09-26 stsp TAILQ_INIT(&changed_paths);
3341 417a6e49 2020-09-26 stsp if (search_pattern && regcomp(®ex, search_pattern,
3342 417a6e49 2020-09-26 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3343 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_REGEX, search_pattern);
3345 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3347 417a6e49 2020-09-26 stsp return err;
3348 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3349 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3351 417a6e49 2020-09-26 stsp goto done;
3352 417a6e49 2020-09-26 stsp for (;;) {
3353 417a6e49 2020-09-26 stsp struct got_object_id *id;
3355 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
3358 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3359 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3360 417a6e49 2020-09-26 stsp if (err) {
3361 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3362 417a6e49 2020-09-26 stsp err = NULL;
3365 417a6e49 2020-09-26 stsp if (id == NULL)
3368 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
3372 417a6e49 2020-09-26 stsp if (show_changed_paths && !reverse_display_order) {
3373 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths, commit, repo);
3378 417a6e49 2020-09-26 stsp if (search_pattern) {
3379 417a6e49 2020-09-26 stsp err = match_logmsg(&have_match, id, commit, ®ex);
3380 417a6e49 2020-09-26 stsp if (err) {
3381 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3384 417a6e49 2020-09-26 stsp if (have_match == 0 && show_changed_paths)
3385 417a6e49 2020-09-26 stsp match_changed_paths(&have_match,
3386 417a6e49 2020-09-26 stsp &changed_paths, ®ex);
3387 417a6e49 2020-09-26 stsp if (have_match == 0) {
3388 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3389 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3390 417a6e49 2020-09-26 stsp free((char *)pe->path);
3391 417a6e49 2020-09-26 stsp free(pe->data);
3393 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3398 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3399 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, id);
3402 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3403 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3405 417a6e49 2020-09-26 stsp err = print_commit(commit, id, repo, path,
3406 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3407 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3408 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3412 417a6e49 2020-09-26 stsp if ((limit && --limit == 0) ||
3413 417a6e49 2020-09-26 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3416 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3417 417a6e49 2020-09-26 stsp free((char *)pe->path);
3418 417a6e49 2020-09-26 stsp free(pe->data);
3420 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3422 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3423 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3424 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3427 417a6e49 2020-09-26 stsp if (show_changed_paths) {
3428 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths,
3429 417a6e49 2020-09-26 stsp commit, repo);
3433 417a6e49 2020-09-26 stsp err = print_commit(commit, qid->id, repo, path,
3434 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3435 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3436 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3439 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3440 417a6e49 2020-09-26 stsp free((char *)pe->path);
3441 417a6e49 2020-09-26 stsp free(pe->data);
3443 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3447 417a6e49 2020-09-26 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3448 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3449 417a6e49 2020-09-26 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3450 417a6e49 2020-09-26 stsp got_object_qid_free(qid);
3452 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3453 417a6e49 2020-09-26 stsp free((char *)pe->path);
3454 417a6e49 2020-09-26 stsp free(pe->data);
3456 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3457 417a6e49 2020-09-26 stsp if (search_pattern)
3458 417a6e49 2020-09-26 stsp regfree(®ex);
3459 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
3460 417a6e49 2020-09-26 stsp return err;
3463 417a6e49 2020-09-26 stsp __dead static void
3464 417a6e49 2020-09-26 stsp usage_log(void)
3466 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3467 417a6e49 2020-09-26 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3468 417a6e49 2020-09-26 stsp "[-R] [path]\n", getprogname());
3472 417a6e49 2020-09-26 stsp static int
3473 417a6e49 2020-09-26 stsp get_default_log_limit(void)
3475 417a6e49 2020-09-26 stsp const char *got_default_log_limit;
3476 417a6e49 2020-09-26 stsp long long n;
3477 417a6e49 2020-09-26 stsp const char *errstr;
3479 417a6e49 2020-09-26 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3480 417a6e49 2020-09-26 stsp if (got_default_log_limit == NULL)
3482 417a6e49 2020-09-26 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3483 417a6e49 2020-09-26 stsp if (errstr != NULL)
3488 417a6e49 2020-09-26 stsp static const struct got_error *
3489 417a6e49 2020-09-26 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3490 417a6e49 2020-09-26 stsp struct got_repository *repo)
3492 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3493 417a6e49 2020-09-26 stsp struct got_reference *ref;
3495 417a6e49 2020-09-26 stsp *id = NULL;
3497 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3498 417a6e49 2020-09-26 stsp if (err == NULL) {
3499 417a6e49 2020-09-26 stsp int obj_type;
3500 417a6e49 2020-09-26 stsp err = got_ref_resolve(id, repo, ref);
3501 417a6e49 2020-09-26 stsp got_ref_close(ref);
3503 417a6e49 2020-09-26 stsp return err;
3504 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, *id);
3506 417a6e49 2020-09-26 stsp return err;
3507 417a6e49 2020-09-26 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3508 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
3509 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, *id);
3511 417a6e49 2020-09-26 stsp return err;
3512 417a6e49 2020-09-26 stsp if (got_object_tag_get_object_type(tag) !=
3513 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT) {
3514 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3515 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3517 417a6e49 2020-09-26 stsp free(*id);
3518 417a6e49 2020-09-26 stsp *id = got_object_id_dup(
3519 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
3520 417a6e49 2020-09-26 stsp if (*id == NULL)
3521 417a6e49 2020-09-26 stsp err = got_error_from_errno(
3522 417a6e49 2020-09-26 stsp "got_object_id_dup");
3523 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3525 417a6e49 2020-09-26 stsp return err;
3526 417a6e49 2020-09-26 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3527 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3529 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3530 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
3533 417a6e49 2020-09-26 stsp return err;
3536 417a6e49 2020-09-26 stsp static const struct got_error *
3537 417a6e49 2020-09-26 stsp cmd_log(int argc, char *argv[])
3539 417a6e49 2020-09-26 stsp const struct got_error *error;
3540 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3541 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3542 417a6e49 2020-09-26 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3543 417a6e49 2020-09-26 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3544 417a6e49 2020-09-26 stsp const char *start_commit = NULL, *end_commit = NULL;
3545 417a6e49 2020-09-26 stsp const char *search_pattern = NULL;
3546 417a6e49 2020-09-26 stsp int diff_context = -1, ch;
3547 417a6e49 2020-09-26 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3548 417a6e49 2020-09-26 stsp int reverse_display_order = 0;
3549 417a6e49 2020-09-26 stsp const char *errstr;
3550 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
3552 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
3554 417a6e49 2020-09-26 stsp #ifndef PROFILE
3555 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3558 417a6e49 2020-09-26 stsp err(1, "pledge");
3561 417a6e49 2020-09-26 stsp limit = get_default_log_limit();
3563 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3564 417a6e49 2020-09-26 stsp switch (ch) {
3566 417a6e49 2020-09-26 stsp show_patch = 1;
3569 417a6e49 2020-09-26 stsp show_changed_paths = 1;
3572 417a6e49 2020-09-26 stsp start_commit = optarg;
3575 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3577 417a6e49 2020-09-26 stsp if (errstr != NULL)
3578 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3581 417a6e49 2020-09-26 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3582 417a6e49 2020-09-26 stsp if (errstr != NULL)
3583 417a6e49 2020-09-26 stsp err(1, "-l option %s", errstr);
3586 417a6e49 2020-09-26 stsp log_branches = 1;
3589 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3590 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3591 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3593 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3596 417a6e49 2020-09-26 stsp reverse_display_order = 1;
3599 417a6e49 2020-09-26 stsp search_pattern = optarg;
3602 417a6e49 2020-09-26 stsp end_commit = optarg;
3605 417a6e49 2020-09-26 stsp usage_log();
3606 417a6e49 2020-09-26 stsp /* NOTREACHED */
3610 417a6e49 2020-09-26 stsp argc -= optind;
3611 417a6e49 2020-09-26 stsp argv += optind;
3613 417a6e49 2020-09-26 stsp if (diff_context == -1)
3614 417a6e49 2020-09-26 stsp diff_context = 3;
3615 417a6e49 2020-09-26 stsp else if (!show_patch)
3616 417a6e49 2020-09-26 stsp errx(1, "-C requires -p");
3618 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3619 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3620 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3621 417a6e49 2020-09-26 stsp goto done;
3624 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3625 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
3626 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3627 417a6e49 2020-09-26 stsp goto done;
3628 417a6e49 2020-09-26 stsp error = NULL;
3631 417a6e49 2020-09-26 stsp if (argc == 0) {
3632 417a6e49 2020-09-26 stsp path = strdup("");
3633 417a6e49 2020-09-26 stsp if (path == NULL) {
3634 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3635 417a6e49 2020-09-26 stsp goto done;
3637 417a6e49 2020-09-26 stsp } else if (argc == 1) {
3638 417a6e49 2020-09-26 stsp if (worktree) {
3639 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
3641 417a6e49 2020-09-26 stsp if (error)
3642 417a6e49 2020-09-26 stsp goto done;
3644 417a6e49 2020-09-26 stsp path = strdup(argv[0]);
3645 417a6e49 2020-09-26 stsp if (path == NULL) {
3646 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3647 417a6e49 2020-09-26 stsp goto done;
3651 417a6e49 2020-09-26 stsp usage_log();
3653 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3654 417a6e49 2020-09-26 stsp repo_path = worktree ?
3655 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3657 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3658 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3659 417a6e49 2020-09-26 stsp goto done;
3662 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
3663 417a6e49 2020-09-26 stsp if (error != NULL)
3664 417a6e49 2020-09-26 stsp goto done;
3666 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3667 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3668 417a6e49 2020-09-26 stsp if (error)
3669 417a6e49 2020-09-26 stsp goto done;
3671 417a6e49 2020-09-26 stsp if (start_commit == NULL) {
3672 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
3673 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
3674 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
3675 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3676 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
3677 417a6e49 2020-09-26 stsp if (error != NULL)
3678 417a6e49 2020-09-26 stsp goto done;
3679 417a6e49 2020-09-26 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3680 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
3681 417a6e49 2020-09-26 stsp if (error != NULL)
3682 417a6e49 2020-09-26 stsp goto done;
3683 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
3684 417a6e49 2020-09-26 stsp start_id);
3685 417a6e49 2020-09-26 stsp if (error != NULL)
3686 417a6e49 2020-09-26 stsp goto done;
3687 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3689 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3690 417a6e49 2020-09-26 stsp if (error != NULL)
3691 417a6e49 2020-09-26 stsp goto done;
3693 417a6e49 2020-09-26 stsp if (end_commit != NULL) {
3694 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3695 417a6e49 2020-09-26 stsp if (error != NULL)
3696 417a6e49 2020-09-26 stsp goto done;
3699 417a6e49 2020-09-26 stsp if (worktree) {
3700 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3702 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s", prefix,
3703 417a6e49 2020-09-26 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3704 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
3705 417a6e49 2020-09-26 stsp goto done;
3707 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3710 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3711 417a6e49 2020-09-26 stsp if (error != NULL)
3712 417a6e49 2020-09-26 stsp goto done;
3713 417a6e49 2020-09-26 stsp if (in_repo_path) {
3714 417a6e49 2020-09-26 stsp free(path);
3715 417a6e49 2020-09-26 stsp path = in_repo_path;
3718 417a6e49 2020-09-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3719 417a6e49 2020-09-26 stsp if (error)
3720 417a6e49 2020-09-26 stsp goto done;
3722 417a6e49 2020-09-26 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3723 417a6e49 2020-09-26 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3724 417a6e49 2020-09-26 stsp reverse_display_order, &refs);
3726 417a6e49 2020-09-26 stsp free(path);
3727 417a6e49 2020-09-26 stsp free(repo_path);
3728 417a6e49 2020-09-26 stsp free(cwd);
3729 417a6e49 2020-09-26 stsp if (worktree)
3730 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
3731 417a6e49 2020-09-26 stsp if (repo) {
3732 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
3733 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
3734 417a6e49 2020-09-26 stsp if (error == NULL)
3735 417a6e49 2020-09-26 stsp error = repo_error;
3737 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
3738 417a6e49 2020-09-26 stsp return error;
3741 417a6e49 2020-09-26 stsp __dead static void
3742 417a6e49 2020-09-26 stsp usage_diff(void)
3744 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3745 417a6e49 2020-09-26 stsp "[-w] [object1 object2 | path]\n", getprogname());
3749 417a6e49 2020-09-26 stsp struct print_diff_arg {
3750 417a6e49 2020-09-26 stsp struct got_repository *repo;
3751 417a6e49 2020-09-26 stsp struct got_worktree *worktree;
3752 417a6e49 2020-09-26 stsp int diff_context;
3753 417a6e49 2020-09-26 stsp const char *id_str;
3754 417a6e49 2020-09-26 stsp int header_shown;
3755 417a6e49 2020-09-26 stsp int diff_staged;
3756 417a6e49 2020-09-26 stsp int ignore_whitespace;
3760 417a6e49 2020-09-26 stsp * Create a file which contains the target path of a symlink so we can feed
3761 417a6e49 2020-09-26 stsp * it as content to the diff engine.
3763 417a6e49 2020-09-26 stsp static const struct got_error *
3764 417a6e49 2020-09-26 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3765 417a6e49 2020-09-26 stsp const char *abspath)
3767 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3768 417a6e49 2020-09-26 stsp char target_path[PATH_MAX];
3769 417a6e49 2020-09-26 stsp ssize_t target_len, outlen;
3773 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3774 417a6e49 2020-09-26 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3775 417a6e49 2020-09-26 stsp if (target_len == -1)
3776 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlinkat", abspath);
3778 417a6e49 2020-09-26 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3779 417a6e49 2020-09-26 stsp if (target_len == -1)
3780 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlink", abspath);
3783 417a6e49 2020-09-26 stsp *fd = got_opentempfd();
3784 417a6e49 2020-09-26 stsp if (*fd == -1)
3785 417a6e49 2020-09-26 stsp return got_error_from_errno("got_opentempfd");
3787 417a6e49 2020-09-26 stsp outlen = write(*fd, target_path, target_len);
3788 417a6e49 2020-09-26 stsp if (outlen == -1) {
3789 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_opentempfd");
3790 417a6e49 2020-09-26 stsp goto done;
3793 417a6e49 2020-09-26 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3794 417a6e49 2020-09-26 stsp err = got_error_from_errno2("lseek", abspath);
3795 417a6e49 2020-09-26 stsp goto done;
3798 417a6e49 2020-09-26 stsp if (err) {
3799 417a6e49 2020-09-26 stsp close(*fd);
3802 417a6e49 2020-09-26 stsp return err;
3805 417a6e49 2020-09-26 stsp static const struct got_error *
3806 417a6e49 2020-09-26 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3807 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
3808 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3809 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
3811 417a6e49 2020-09-26 stsp struct print_diff_arg *a = arg;
3812 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3813 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL;
3814 417a6e49 2020-09-26 stsp int fd = -1;
3815 417a6e49 2020-09-26 stsp FILE *f2 = NULL;
3816 417a6e49 2020-09-26 stsp char *abspath = NULL, *label1 = NULL;
3817 417a6e49 2020-09-26 stsp struct stat sb;
3819 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3820 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_MODIFY &&
3821 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_ADD &&
3822 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
3823 417a6e49 2020-09-26 stsp return NULL;
3825 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_DELETE)
3826 417a6e49 2020-09-26 stsp return NULL;
3827 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
3828 417a6e49 2020-09-26 stsp return got_error_set_errno(ENOENT, path);
3829 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_MODIFY &&
3830 417a6e49 2020-09-26 stsp status != GOT_STATUS_ADD &&
3831 417a6e49 2020-09-26 stsp status != GOT_STATUS_DELETE &&
3832 417a6e49 2020-09-26 stsp status != GOT_STATUS_CONFLICT)
3833 417a6e49 2020-09-26 stsp return NULL;
3836 417a6e49 2020-09-26 stsp if (!a->header_shown) {
3837 417a6e49 2020-09-26 stsp printf("diff %s %s%s\n", a->id_str,
3838 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree),
3839 417a6e49 2020-09-26 stsp a->diff_staged ? " (staged changes)" : "");
3840 417a6e49 2020-09-26 stsp a->header_shown = 1;
3843 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3844 417a6e49 2020-09-26 stsp const char *label1 = NULL, *label2 = NULL;
3845 417a6e49 2020-09-26 stsp switch (staged_status) {
3846 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
3847 417a6e49 2020-09-26 stsp label1 = path;
3848 417a6e49 2020-09-26 stsp label2 = path;
3850 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
3851 417a6e49 2020-09-26 stsp label2 = path;
3853 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
3854 417a6e49 2020-09-26 stsp label1 = path;
3857 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_FILE_STATUS);
3859 417a6e49 2020-09-26 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3860 417a6e49 2020-09-26 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3861 417a6e49 2020-09-26 stsp a->repo, stdout);
3864 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
3865 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY) {
3866 417a6e49 2020-09-26 stsp char *id_str;
3867 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3870 417a6e49 2020-09-26 stsp goto done;
3871 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
3873 417a6e49 2020-09-26 stsp goto done;
3874 417a6e49 2020-09-26 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3875 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3876 417a6e49 2020-09-26 stsp free(id_str);
3877 417a6e49 2020-09-26 stsp goto done;
3879 417a6e49 2020-09-26 stsp free(id_str);
3880 417a6e49 2020-09-26 stsp } else if (status != GOT_STATUS_ADD) {
3881 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3883 417a6e49 2020-09-26 stsp goto done;
3886 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_DELETE) {
3887 417a6e49 2020-09-26 stsp if (asprintf(&abspath, "%s/%s",
3888 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3889 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3890 417a6e49 2020-09-26 stsp goto done;
3893 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3894 417a6e49 2020-09-26 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3895 417a6e49 2020-09-26 stsp if (fd == -1) {
3896 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3897 417a6e49 2020-09-26 stsp err = got_error_from_errno2("openat",
3899 417a6e49 2020-09-26 stsp goto done;
3901 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3902 417a6e49 2020-09-26 stsp de_name, abspath);
3904 417a6e49 2020-09-26 stsp goto done;
3907 417a6e49 2020-09-26 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3908 417a6e49 2020-09-26 stsp if (fd == -1) {
3909 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3910 417a6e49 2020-09-26 stsp err = got_error_from_errno2("open",
3912 417a6e49 2020-09-26 stsp goto done;
3914 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3915 417a6e49 2020-09-26 stsp de_name, abspath);
3917 417a6e49 2020-09-26 stsp goto done;
3920 417a6e49 2020-09-26 stsp if (fstat(fd, &sb) == -1) {
3921 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fstat", abspath);
3922 417a6e49 2020-09-26 stsp goto done;
3924 417a6e49 2020-09-26 stsp f2 = fdopen(fd, "r");
3925 417a6e49 2020-09-26 stsp if (f2 == NULL) {
3926 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fdopen", abspath);
3927 417a6e49 2020-09-26 stsp goto done;
3931 417a6e49 2020-09-26 stsp sb.st_size = 0;
3933 417a6e49 2020-09-26 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3934 417a6e49 2020-09-26 stsp a->diff_context, a->ignore_whitespace, stdout);
3936 417a6e49 2020-09-26 stsp if (blob1)
3937 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
3938 417a6e49 2020-09-26 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3939 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
3940 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3941 417a6e49 2020-09-26 stsp err = got_error_from_errno("close");
3942 417a6e49 2020-09-26 stsp free(abspath);
3943 417a6e49 2020-09-26 stsp return err;
3946 417a6e49 2020-09-26 stsp static const struct got_error *
3947 417a6e49 2020-09-26 stsp cmd_diff(int argc, char *argv[])
3949 417a6e49 2020-09-26 stsp const struct got_error *error;
3950 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3951 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3952 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
3953 417a6e49 2020-09-26 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3954 417a6e49 2020-09-26 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3955 417a6e49 2020-09-26 stsp char *label1 = NULL, *label2 = NULL;
3956 417a6e49 2020-09-26 stsp int type1, type2;
3957 417a6e49 2020-09-26 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3958 417a6e49 2020-09-26 stsp const char *errstr;
3959 417a6e49 2020-09-26 stsp char *path = NULL;
3961 417a6e49 2020-09-26 stsp #ifndef PROFILE
3962 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3963 417a6e49 2020-09-26 stsp NULL) == -1)
3964 417a6e49 2020-09-26 stsp err(1, "pledge");
3967 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3968 417a6e49 2020-09-26 stsp switch (ch) {
3970 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3972 417a6e49 2020-09-26 stsp if (errstr != NULL)
3973 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3976 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3977 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3978 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3980 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3983 417a6e49 2020-09-26 stsp diff_staged = 1;
3986 417a6e49 2020-09-26 stsp ignore_whitespace = 1;
3989 417a6e49 2020-09-26 stsp usage_diff();
3990 417a6e49 2020-09-26 stsp /* NOTREACHED */
3994 417a6e49 2020-09-26 stsp argc -= optind;
3995 417a6e49 2020-09-26 stsp argv += optind;
3997 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3998 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3999 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4000 417a6e49 2020-09-26 stsp goto done;
4002 417a6e49 2020-09-26 stsp if (argc <= 1) {
4003 417a6e49 2020-09-26 stsp if (repo_path)
4005 417a6e49 2020-09-26 stsp "-r option can't be used when diffing a work tree");
4006 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4007 417a6e49 2020-09-26 stsp if (error) {
4008 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4009 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "diff",
4011 417a6e49 2020-09-26 stsp goto done;
4013 417a6e49 2020-09-26 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4014 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4015 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4016 417a6e49 2020-09-26 stsp goto done;
4018 417a6e49 2020-09-26 stsp if (argc == 1) {
4019 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
4021 417a6e49 2020-09-26 stsp if (error)
4022 417a6e49 2020-09-26 stsp goto done;
4024 417a6e49 2020-09-26 stsp path = strdup("");
4025 417a6e49 2020-09-26 stsp if (path == NULL) {
4026 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4027 417a6e49 2020-09-26 stsp goto done;
4030 417a6e49 2020-09-26 stsp } else if (argc == 2) {
4031 417a6e49 2020-09-26 stsp if (diff_staged)
4032 417a6e49 2020-09-26 stsp errx(1, "-s option can't be used when diffing "
4033 417a6e49 2020-09-26 stsp "objects in repository");
4034 417a6e49 2020-09-26 stsp id_str1 = argv[0];
4035 417a6e49 2020-09-26 stsp id_str2 = argv[1];
4036 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4037 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4038 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4039 417a6e49 2020-09-26 stsp goto done;
4040 417a6e49 2020-09-26 stsp if (worktree) {
4041 417a6e49 2020-09-26 stsp repo_path = strdup(
4042 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
4043 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4044 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4045 417a6e49 2020-09-26 stsp goto done;
4048 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4049 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4050 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4051 417a6e49 2020-09-26 stsp goto done;
4056 417a6e49 2020-09-26 stsp usage_diff();
4058 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4059 417a6e49 2020-09-26 stsp free(repo_path);
4060 417a6e49 2020-09-26 stsp if (error != NULL)
4061 417a6e49 2020-09-26 stsp goto done;
4063 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4064 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4065 417a6e49 2020-09-26 stsp if (error)
4066 417a6e49 2020-09-26 stsp goto done;
4068 417a6e49 2020-09-26 stsp if (argc <= 1) {
4069 417a6e49 2020-09-26 stsp struct print_diff_arg arg;
4070 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4071 417a6e49 2020-09-26 stsp char *id_str;
4073 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4075 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
4076 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
4077 417a6e49 2020-09-26 stsp if (error)
4078 417a6e49 2020-09-26 stsp goto done;
4079 417a6e49 2020-09-26 stsp arg.repo = repo;
4080 417a6e49 2020-09-26 stsp arg.worktree = worktree;
4081 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
4082 417a6e49 2020-09-26 stsp arg.id_str = id_str;
4083 417a6e49 2020-09-26 stsp arg.header_shown = 0;
4084 417a6e49 2020-09-26 stsp arg.diff_staged = diff_staged;
4085 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
4087 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, path, NULL);
4088 417a6e49 2020-09-26 stsp if (error)
4089 417a6e49 2020-09-26 stsp goto done;
4091 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4092 417a6e49 2020-09-26 stsp &arg, check_cancelled, NULL);
4093 417a6e49 2020-09-26 stsp free(id_str);
4094 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4095 417a6e49 2020-09-26 stsp goto done;
4098 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4099 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4100 417a6e49 2020-09-26 stsp if (error)
4101 417a6e49 2020-09-26 stsp goto done;
4103 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4104 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4105 417a6e49 2020-09-26 stsp if (error)
4106 417a6e49 2020-09-26 stsp goto done;
4108 417a6e49 2020-09-26 stsp error = got_object_get_type(&type1, repo, id1);
4109 417a6e49 2020-09-26 stsp if (error)
4110 417a6e49 2020-09-26 stsp goto done;
4112 417a6e49 2020-09-26 stsp error = got_object_get_type(&type2, repo, id2);
4113 417a6e49 2020-09-26 stsp if (error)
4114 417a6e49 2020-09-26 stsp goto done;
4116 417a6e49 2020-09-26 stsp if (type1 != type2) {
4117 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4118 417a6e49 2020-09-26 stsp goto done;
4121 417a6e49 2020-09-26 stsp switch (type1) {
4122 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
4123 417a6e49 2020-09-26 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4124 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4126 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
4127 417a6e49 2020-09-26 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4128 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4130 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
4131 417a6e49 2020-09-26 stsp printf("diff %s %s\n", label1, label2);
4132 417a6e49 2020-09-26 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4133 417a6e49 2020-09-26 stsp ignore_whitespace, repo, stdout);
4136 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4139 417a6e49 2020-09-26 stsp free(label1);
4140 417a6e49 2020-09-26 stsp free(label2);
4141 417a6e49 2020-09-26 stsp free(id1);
4142 417a6e49 2020-09-26 stsp free(id2);
4143 417a6e49 2020-09-26 stsp free(path);
4144 417a6e49 2020-09-26 stsp if (worktree)
4145 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4146 417a6e49 2020-09-26 stsp if (repo) {
4147 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4148 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4149 417a6e49 2020-09-26 stsp if (error == NULL)
4150 417a6e49 2020-09-26 stsp error = repo_error;
4152 417a6e49 2020-09-26 stsp return error;
4155 417a6e49 2020-09-26 stsp __dead static void
4156 417a6e49 2020-09-26 stsp usage_blame(void)
4158 417a6e49 2020-09-26 stsp fprintf(stderr,
4159 417a6e49 2020-09-26 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4160 417a6e49 2020-09-26 stsp getprogname());
4164 417a6e49 2020-09-26 stsp struct blame_line {
4165 417a6e49 2020-09-26 stsp int annotated;
4166 417a6e49 2020-09-26 stsp char *id_str;
4167 417a6e49 2020-09-26 stsp char *committer;
4168 417a6e49 2020-09-26 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4171 417a6e49 2020-09-26 stsp struct blame_cb_args {
4172 417a6e49 2020-09-26 stsp struct blame_line *lines;
4173 417a6e49 2020-09-26 stsp int nlines;
4174 417a6e49 2020-09-26 stsp int nlines_prec;
4175 417a6e49 2020-09-26 stsp int lineno_cur;
4176 417a6e49 2020-09-26 stsp off_t *line_offsets;
4178 417a6e49 2020-09-26 stsp struct got_repository *repo;
4181 417a6e49 2020-09-26 stsp static const struct got_error *
4182 417a6e49 2020-09-26 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4184 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4185 417a6e49 2020-09-26 stsp struct blame_cb_args *a = arg;
4186 417a6e49 2020-09-26 stsp struct blame_line *bline;
4187 417a6e49 2020-09-26 stsp char *line = NULL;
4188 417a6e49 2020-09-26 stsp size_t linesize = 0;
4189 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
4190 417a6e49 2020-09-26 stsp off_t offset;
4191 417a6e49 2020-09-26 stsp struct tm tm;
4192 417a6e49 2020-09-26 stsp time_t committer_time;
4194 417a6e49 2020-09-26 stsp if (nlines != a->nlines ||
4195 417a6e49 2020-09-26 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4196 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_RANGE);
4198 417a6e49 2020-09-26 stsp if (sigint_received)
4199 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4201 417a6e49 2020-09-26 stsp if (lineno == -1)
4202 417a6e49 2020-09-26 stsp return NULL; /* no change in this commit */
4204 417a6e49 2020-09-26 stsp /* Annotate this line. */
4205 417a6e49 2020-09-26 stsp bline = &a->lines[lineno - 1];
4206 417a6e49 2020-09-26 stsp if (bline->annotated)
4207 417a6e49 2020-09-26 stsp return NULL;
4208 417a6e49 2020-09-26 stsp err = got_object_id_str(&bline->id_str, id);
4210 417a6e49 2020-09-26 stsp return err;
4212 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4214 417a6e49 2020-09-26 stsp goto done;
4216 417a6e49 2020-09-26 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4217 417a6e49 2020-09-26 stsp if (bline->committer == NULL) {
4218 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
4219 417a6e49 2020-09-26 stsp goto done;
4222 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
4223 417a6e49 2020-09-26 stsp if (localtime_r(&committer_time, &tm) == NULL)
4224 417a6e49 2020-09-26 stsp return got_error_from_errno("localtime_r");
4225 417a6e49 2020-09-26 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4226 417a6e49 2020-09-26 stsp &tm) >= sizeof(bline->datebuf)) {
4227 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
4228 417a6e49 2020-09-26 stsp goto done;
4230 417a6e49 2020-09-26 stsp bline->annotated = 1;
4232 417a6e49 2020-09-26 stsp /* Print lines annotated so far. */
4233 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4234 417a6e49 2020-09-26 stsp if (!bline->annotated)
4235 417a6e49 2020-09-26 stsp goto done;
4237 417a6e49 2020-09-26 stsp offset = a->line_offsets[a->lineno_cur - 1];
4238 417a6e49 2020-09-26 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4239 417a6e49 2020-09-26 stsp err = got_error_from_errno("fseeko");
4240 417a6e49 2020-09-26 stsp goto done;
4243 417a6e49 2020-09-26 stsp while (bline->annotated) {
4244 417a6e49 2020-09-26 stsp char *smallerthan, *at, *nl, *committer;
4245 417a6e49 2020-09-26 stsp size_t len;
4247 417a6e49 2020-09-26 stsp if (getline(&line, &linesize, a->f) == -1) {
4248 417a6e49 2020-09-26 stsp if (ferror(a->f))
4249 417a6e49 2020-09-26 stsp err = got_error_from_errno("getline");
4253 417a6e49 2020-09-26 stsp committer = bline->committer;
4254 417a6e49 2020-09-26 stsp smallerthan = strchr(committer, '<');
4255 417a6e49 2020-09-26 stsp if (smallerthan && smallerthan[1] != '\0')
4256 417a6e49 2020-09-26 stsp committer = smallerthan + 1;
4257 417a6e49 2020-09-26 stsp at = strchr(committer, '@');
4259 417a6e49 2020-09-26 stsp *at = '\0';
4260 417a6e49 2020-09-26 stsp len = strlen(committer);
4261 417a6e49 2020-09-26 stsp if (len >= 9)
4262 417a6e49 2020-09-26 stsp committer[8] = '\0';
4264 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
4266 417a6e49 2020-09-26 stsp *nl = '\0';
4267 417a6e49 2020-09-26 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4268 417a6e49 2020-09-26 stsp bline->id_str, bline->datebuf, committer, line);
4270 417a6e49 2020-09-26 stsp a->lineno_cur++;
4271 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4274 417a6e49 2020-09-26 stsp if (commit)
4275 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
4276 417a6e49 2020-09-26 stsp free(line);
4277 417a6e49 2020-09-26 stsp return err;
4280 417a6e49 2020-09-26 stsp static const struct got_error *
4281 417a6e49 2020-09-26 stsp cmd_blame(int argc, char *argv[])
4283 417a6e49 2020-09-26 stsp const struct got_error *error;
4284 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4285 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4286 417a6e49 2020-09-26 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4287 417a6e49 2020-09-26 stsp char *link_target = NULL;
4288 417a6e49 2020-09-26 stsp struct got_object_id *obj_id = NULL;
4289 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4290 417a6e49 2020-09-26 stsp struct got_blob_object *blob = NULL;
4291 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4292 417a6e49 2020-09-26 stsp struct blame_cb_args bca;
4293 417a6e49 2020-09-26 stsp int ch, obj_type, i;
4294 417a6e49 2020-09-26 stsp size_t filesize;
4296 417a6e49 2020-09-26 stsp memset(&bca, 0, sizeof(bca));
4298 417a6e49 2020-09-26 stsp #ifndef PROFILE
4299 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4300 417a6e49 2020-09-26 stsp NULL) == -1)
4301 417a6e49 2020-09-26 stsp err(1, "pledge");
4304 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4305 417a6e49 2020-09-26 stsp switch (ch) {
4307 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4310 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4311 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4312 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4314 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4317 417a6e49 2020-09-26 stsp usage_blame();
4318 417a6e49 2020-09-26 stsp /* NOTREACHED */
4322 417a6e49 2020-09-26 stsp argc -= optind;
4323 417a6e49 2020-09-26 stsp argv += optind;
4325 417a6e49 2020-09-26 stsp if (argc == 1)
4326 417a6e49 2020-09-26 stsp path = argv[0];
4328 417a6e49 2020-09-26 stsp usage_blame();
4330 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4331 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4332 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4333 417a6e49 2020-09-26 stsp goto done;
4335 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4336 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4337 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4338 417a6e49 2020-09-26 stsp goto done;
4340 417a6e49 2020-09-26 stsp error = NULL;
4341 417a6e49 2020-09-26 stsp if (worktree) {
4342 417a6e49 2020-09-26 stsp repo_path =
4343 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4344 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4345 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4346 417a6e49 2020-09-26 stsp if (error)
4347 417a6e49 2020-09-26 stsp goto done;
4350 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4351 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4352 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4353 417a6e49 2020-09-26 stsp goto done;
4358 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4359 417a6e49 2020-09-26 stsp if (error != NULL)
4360 417a6e49 2020-09-26 stsp goto done;
4362 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4363 417a6e49 2020-09-26 stsp if (error)
4364 417a6e49 2020-09-26 stsp goto done;
4366 417a6e49 2020-09-26 stsp if (worktree) {
4367 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4368 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4369 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4370 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s%s%s",
4371 417a6e49 2020-09-26 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4372 417a6e49 2020-09-26 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4373 417a6e49 2020-09-26 stsp path) == -1) {
4374 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4375 417a6e49 2020-09-26 stsp goto done;
4377 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4380 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4382 417a6e49 2020-09-26 stsp if (error)
4383 417a6e49 2020-09-26 stsp goto done;
4385 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4386 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4387 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, worktree ?
4388 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4389 417a6e49 2020-09-26 stsp if (error != NULL)
4390 417a6e49 2020-09-26 stsp goto done;
4391 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4392 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4393 417a6e49 2020-09-26 stsp if (error != NULL)
4394 417a6e49 2020-09-26 stsp goto done;
4396 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4397 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4398 417a6e49 2020-09-26 stsp if (error)
4399 417a6e49 2020-09-26 stsp goto done;
4402 417a6e49 2020-09-26 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4403 417a6e49 2020-09-26 stsp commit_id, repo);
4404 417a6e49 2020-09-26 stsp if (error)
4405 417a6e49 2020-09-26 stsp goto done;
4407 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4408 417a6e49 2020-09-26 stsp link_target ? link_target : in_repo_path);
4409 417a6e49 2020-09-26 stsp if (error)
4410 417a6e49 2020-09-26 stsp goto done;
4412 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4413 417a6e49 2020-09-26 stsp if (error)
4414 417a6e49 2020-09-26 stsp goto done;
4416 417a6e49 2020-09-26 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4417 417a6e49 2020-09-26 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4418 417a6e49 2020-09-26 stsp GOT_ERR_OBJ_TYPE);
4419 417a6e49 2020-09-26 stsp goto done;
4422 417a6e49 2020-09-26 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4423 417a6e49 2020-09-26 stsp if (error)
4424 417a6e49 2020-09-26 stsp goto done;
4425 417a6e49 2020-09-26 stsp bca.f = got_opentemp();
4426 417a6e49 2020-09-26 stsp if (bca.f == NULL) {
4427 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_opentemp");
4428 417a6e49 2020-09-26 stsp goto done;
4430 417a6e49 2020-09-26 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4431 417a6e49 2020-09-26 stsp &bca.line_offsets, bca.f, blob);
4432 417a6e49 2020-09-26 stsp if (error || bca.nlines == 0)
4433 417a6e49 2020-09-26 stsp goto done;
4435 417a6e49 2020-09-26 stsp /* Don't include \n at EOF in the blame line count. */
4436 417a6e49 2020-09-26 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4437 417a6e49 2020-09-26 stsp bca.nlines--;
4439 417a6e49 2020-09-26 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4440 417a6e49 2020-09-26 stsp if (bca.lines == NULL) {
4441 417a6e49 2020-09-26 stsp error = got_error_from_errno("calloc");
4442 417a6e49 2020-09-26 stsp goto done;
4444 417a6e49 2020-09-26 stsp bca.lineno_cur = 1;
4445 417a6e49 2020-09-26 stsp bca.nlines_prec = 0;
4446 417a6e49 2020-09-26 stsp i = bca.nlines;
4447 417a6e49 2020-09-26 stsp while (i > 0) {
4449 417a6e49 2020-09-26 stsp bca.nlines_prec++;
4451 417a6e49 2020-09-26 stsp bca.repo = repo;
4453 417a6e49 2020-09-26 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4454 417a6e49 2020-09-26 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4456 417a6e49 2020-09-26 stsp free(in_repo_path);
4457 417a6e49 2020-09-26 stsp free(link_target);
4458 417a6e49 2020-09-26 stsp free(repo_path);
4459 417a6e49 2020-09-26 stsp free(cwd);
4460 417a6e49 2020-09-26 stsp free(commit_id);
4461 417a6e49 2020-09-26 stsp free(obj_id);
4463 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
4464 417a6e49 2020-09-26 stsp if (worktree)
4465 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4466 417a6e49 2020-09-26 stsp if (repo) {
4467 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4468 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4469 417a6e49 2020-09-26 stsp if (error == NULL)
4470 417a6e49 2020-09-26 stsp error = repo_error;
4472 417a6e49 2020-09-26 stsp if (bca.lines) {
4473 417a6e49 2020-09-26 stsp for (i = 0; i < bca.nlines; i++) {
4474 417a6e49 2020-09-26 stsp struct blame_line *bline = &bca.lines[i];
4475 417a6e49 2020-09-26 stsp free(bline->id_str);
4476 417a6e49 2020-09-26 stsp free(bline->committer);
4478 417a6e49 2020-09-26 stsp free(bca.lines);
4480 417a6e49 2020-09-26 stsp free(bca.line_offsets);
4481 417a6e49 2020-09-26 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4482 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
4483 417a6e49 2020-09-26 stsp return error;
4486 417a6e49 2020-09-26 stsp __dead static void
4487 417a6e49 2020-09-26 stsp usage_tree(void)
4489 417a6e49 2020-09-26 stsp fprintf(stderr,
4490 417a6e49 2020-09-26 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4491 417a6e49 2020-09-26 stsp getprogname());
4495 417a6e49 2020-09-26 stsp static const struct got_error *
4496 417a6e49 2020-09-26 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4497 417a6e49 2020-09-26 stsp const char *root_path, struct got_repository *repo)
4499 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4500 417a6e49 2020-09-26 stsp int is_root_path = (strcmp(path, root_path) == 0);
4501 417a6e49 2020-09-26 stsp const char *modestr = "";
4502 417a6e49 2020-09-26 stsp mode_t mode = got_tree_entry_get_mode(te);
4503 417a6e49 2020-09-26 stsp char *link_target = NULL;
4505 417a6e49 2020-09-26 stsp path += strlen(root_path);
4506 417a6e49 2020-09-26 stsp while (path[0] == '/')
4509 417a6e49 2020-09-26 stsp if (got_object_tree_entry_is_submodule(te))
4510 417a6e49 2020-09-26 stsp modestr = "$";
4511 417a6e49 2020-09-26 stsp else if (S_ISLNK(mode)) {
4514 417a6e49 2020-09-26 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4516 417a6e49 2020-09-26 stsp return err;
4517 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(link_target); i++) {
4518 417a6e49 2020-09-26 stsp if (!isprint((unsigned char)link_target[i]))
4519 417a6e49 2020-09-26 stsp link_target[i] = '?';
4522 417a6e49 2020-09-26 stsp modestr = "@";
4524 417a6e49 2020-09-26 stsp else if (S_ISDIR(mode))
4525 417a6e49 2020-09-26 stsp modestr = "/";
4526 417a6e49 2020-09-26 stsp else if (mode & S_IXUSR)
4527 417a6e49 2020-09-26 stsp modestr = "*";
4529 417a6e49 2020-09-26 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4530 417a6e49 2020-09-26 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4531 417a6e49 2020-09-26 stsp link_target ? " -> ": "", link_target ? link_target : "");
4533 417a6e49 2020-09-26 stsp free(link_target);
4534 417a6e49 2020-09-26 stsp return NULL;
4537 417a6e49 2020-09-26 stsp static const struct got_error *
4538 417a6e49 2020-09-26 stsp print_tree(const char *path, struct got_object_id *commit_id,
4539 417a6e49 2020-09-26 stsp int show_ids, int recurse, const char *root_path,
4540 417a6e49 2020-09-26 stsp struct got_repository *repo)
4542 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4543 417a6e49 2020-09-26 stsp struct got_object_id *tree_id = NULL;
4544 417a6e49 2020-09-26 stsp struct got_tree_object *tree = NULL;
4545 417a6e49 2020-09-26 stsp int nentries, i;
4547 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4549 417a6e49 2020-09-26 stsp goto done;
4551 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4553 417a6e49 2020-09-26 stsp goto done;
4554 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
4555 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
4556 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
4557 417a6e49 2020-09-26 stsp char *id = NULL;
4559 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
4562 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
4563 417a6e49 2020-09-26 stsp if (show_ids) {
4564 417a6e49 2020-09-26 stsp char *id_str;
4565 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
4566 417a6e49 2020-09-26 stsp got_tree_entry_get_id(te));
4568 417a6e49 2020-09-26 stsp goto done;
4569 417a6e49 2020-09-26 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4570 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4571 417a6e49 2020-09-26 stsp free(id_str);
4572 417a6e49 2020-09-26 stsp goto done;
4574 417a6e49 2020-09-26 stsp free(id_str);
4576 417a6e49 2020-09-26 stsp err = print_entry(te, id, path, root_path, repo);
4579 417a6e49 2020-09-26 stsp goto done;
4581 417a6e49 2020-09-26 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4582 417a6e49 2020-09-26 stsp char *child_path;
4583 417a6e49 2020-09-26 stsp if (asprintf(&child_path, "%s%s%s", path,
4584 417a6e49 2020-09-26 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4585 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te)) == -1) {
4586 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4587 417a6e49 2020-09-26 stsp goto done;
4589 417a6e49 2020-09-26 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4590 417a6e49 2020-09-26 stsp root_path, repo);
4591 417a6e49 2020-09-26 stsp free(child_path);
4593 417a6e49 2020-09-26 stsp goto done;
4598 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
4599 417a6e49 2020-09-26 stsp free(tree_id);
4600 417a6e49 2020-09-26 stsp return err;
4603 417a6e49 2020-09-26 stsp static const struct got_error *
4604 417a6e49 2020-09-26 stsp cmd_tree(int argc, char *argv[])
4606 417a6e49 2020-09-26 stsp const struct got_error *error;
4607 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4608 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4609 417a6e49 2020-09-26 stsp const char *path, *refname = NULL;
4610 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4611 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4612 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4613 417a6e49 2020-09-26 stsp int show_ids = 0, recurse = 0;
4616 417a6e49 2020-09-26 stsp #ifndef PROFILE
4617 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4618 417a6e49 2020-09-26 stsp NULL) == -1)
4619 417a6e49 2020-09-26 stsp err(1, "pledge");
4622 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4623 417a6e49 2020-09-26 stsp switch (ch) {
4625 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4628 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4629 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4630 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4632 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4635 417a6e49 2020-09-26 stsp show_ids = 1;
4638 417a6e49 2020-09-26 stsp recurse = 1;
4641 417a6e49 2020-09-26 stsp usage_tree();
4642 417a6e49 2020-09-26 stsp /* NOTREACHED */
4646 417a6e49 2020-09-26 stsp argc -= optind;
4647 417a6e49 2020-09-26 stsp argv += optind;
4649 417a6e49 2020-09-26 stsp if (argc == 1)
4650 417a6e49 2020-09-26 stsp path = argv[0];
4651 417a6e49 2020-09-26 stsp else if (argc > 1)
4652 417a6e49 2020-09-26 stsp usage_tree();
4654 417a6e49 2020-09-26 stsp path = NULL;
4656 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4657 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4658 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4659 417a6e49 2020-09-26 stsp goto done;
4661 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4662 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4663 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4664 417a6e49 2020-09-26 stsp goto done;
4666 417a6e49 2020-09-26 stsp error = NULL;
4667 417a6e49 2020-09-26 stsp if (worktree) {
4668 417a6e49 2020-09-26 stsp repo_path =
4669 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4670 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4671 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4672 417a6e49 2020-09-26 stsp if (error)
4673 417a6e49 2020-09-26 stsp goto done;
4675 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4676 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4677 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4678 417a6e49 2020-09-26 stsp goto done;
4683 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4684 417a6e49 2020-09-26 stsp if (error != NULL)
4685 417a6e49 2020-09-26 stsp goto done;
4687 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4688 417a6e49 2020-09-26 stsp if (error)
4689 417a6e49 2020-09-26 stsp goto done;
4691 417a6e49 2020-09-26 stsp if (path == NULL) {
4692 417a6e49 2020-09-26 stsp if (worktree) {
4693 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4694 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4695 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s/%s",
4696 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
4697 417a6e49 2020-09-26 stsp worktree_subdir) == -1) {
4698 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4699 417a6e49 2020-09-26 stsp goto done;
4701 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4703 417a6e49 2020-09-26 stsp if (error)
4704 417a6e49 2020-09-26 stsp goto done;
4706 417a6e49 2020-09-26 stsp path = "/";
4708 417a6e49 2020-09-26 stsp if (in_repo_path == NULL) {
4709 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4710 417a6e49 2020-09-26 stsp if (error != NULL)
4711 417a6e49 2020-09-26 stsp goto done;
4714 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4715 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4716 417a6e49 2020-09-26 stsp if (worktree)
4717 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
4719 417a6e49 2020-09-26 stsp refname = GOT_REF_HEAD;
4720 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, refname, 0);
4721 417a6e49 2020-09-26 stsp if (error != NULL)
4722 417a6e49 2020-09-26 stsp goto done;
4723 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4724 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4725 417a6e49 2020-09-26 stsp if (error != NULL)
4726 417a6e49 2020-09-26 stsp goto done;
4728 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4729 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4730 417a6e49 2020-09-26 stsp if (error)
4731 417a6e49 2020-09-26 stsp goto done;
4734 417a6e49 2020-09-26 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4735 417a6e49 2020-09-26 stsp in_repo_path, repo);
4737 417a6e49 2020-09-26 stsp free(in_repo_path);
4738 417a6e49 2020-09-26 stsp free(repo_path);
4739 417a6e49 2020-09-26 stsp free(cwd);
4740 417a6e49 2020-09-26 stsp free(commit_id);
4741 417a6e49 2020-09-26 stsp if (worktree)
4742 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4743 417a6e49 2020-09-26 stsp if (repo) {
4744 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4745 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4746 417a6e49 2020-09-26 stsp if (error == NULL)
4747 417a6e49 2020-09-26 stsp error = repo_error;
4749 417a6e49 2020-09-26 stsp return error;
4752 417a6e49 2020-09-26 stsp __dead static void
4753 417a6e49 2020-09-26 stsp usage_status(void)
4755 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4756 417a6e49 2020-09-26 stsp getprogname());
4760 417a6e49 2020-09-26 stsp static const struct got_error *
4761 417a6e49 2020-09-26 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4762 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
4763 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4764 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
4766 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4767 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
4768 417a6e49 2020-09-26 stsp if (arg) {
4769 417a6e49 2020-09-26 stsp char *status_codes = arg;
4770 417a6e49 2020-09-26 stsp size_t ncodes = strlen(status_codes);
4772 417a6e49 2020-09-26 stsp for (i = 0; i < ncodes ; i++) {
4773 417a6e49 2020-09-26 stsp if (status == status_codes[i] ||
4774 417a6e49 2020-09-26 stsp staged_status == status_codes[i])
4777 417a6e49 2020-09-26 stsp if (i == ncodes)
4778 417a6e49 2020-09-26 stsp return NULL;
4780 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
4781 417a6e49 2020-09-26 stsp return NULL;
4784 417a6e49 2020-09-26 stsp static const struct got_error *
4785 417a6e49 2020-09-26 stsp cmd_status(int argc, char *argv[])
4787 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4788 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4789 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4790 417a6e49 2020-09-26 stsp char *cwd = NULL, *status_codes = NULL;;
4791 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4792 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
4793 417a6e49 2020-09-26 stsp int ch, i;
4795 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4797 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4798 417a6e49 2020-09-26 stsp switch (ch) {
4800 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
4801 417a6e49 2020-09-26 stsp switch (optarg[i]) {
4802 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
4803 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
4804 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
4805 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
4806 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
4807 417a6e49 2020-09-26 stsp case GOT_STATUS_OBSTRUCTED:
4808 417a6e49 2020-09-26 stsp case GOT_STATUS_UNVERSIONED:
4809 417a6e49 2020-09-26 stsp case GOT_STATUS_MODE_CHANGE:
4810 417a6e49 2020-09-26 stsp case GOT_STATUS_NONEXISTENT:
4813 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
4814 417a6e49 2020-09-26 stsp optarg[i]);
4817 417a6e49 2020-09-26 stsp status_codes = optarg;
4820 417a6e49 2020-09-26 stsp usage_status();
4821 417a6e49 2020-09-26 stsp /* NOTREACHED */
4825 417a6e49 2020-09-26 stsp argc -= optind;
4826 417a6e49 2020-09-26 stsp argv += optind;
4828 417a6e49 2020-09-26 stsp #ifndef PROFILE
4829 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4830 417a6e49 2020-09-26 stsp NULL) == -1)
4831 417a6e49 2020-09-26 stsp err(1, "pledge");
4833 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4834 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4835 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4836 417a6e49 2020-09-26 stsp goto done;
4839 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4840 417a6e49 2020-09-26 stsp if (error) {
4841 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4842 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
4843 417a6e49 2020-09-26 stsp goto done;
4846 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4848 417a6e49 2020-09-26 stsp if (error != NULL)
4849 417a6e49 2020-09-26 stsp goto done;
4851 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4852 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
4853 417a6e49 2020-09-26 stsp if (error)
4854 417a6e49 2020-09-26 stsp goto done;
4856 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4857 417a6e49 2020-09-26 stsp if (error)
4858 417a6e49 2020-09-26 stsp goto done;
4860 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4861 417a6e49 2020-09-26 stsp status_codes, check_cancelled, NULL);
4863 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
4864 417a6e49 2020-09-26 stsp free((char *)pe->path);
4865 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4866 417a6e49 2020-09-26 stsp free(cwd);
4867 417a6e49 2020-09-26 stsp return error;
4870 417a6e49 2020-09-26 stsp __dead static void
4871 417a6e49 2020-09-26 stsp usage_ref(void)
4873 417a6e49 2020-09-26 stsp fprintf(stderr,
4874 417a6e49 2020-09-26 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4875 417a6e49 2020-09-26 stsp "[-d] [name]\n",
4876 417a6e49 2020-09-26 stsp getprogname());
4880 417a6e49 2020-09-26 stsp static const struct got_error *
4881 417a6e49 2020-09-26 stsp list_refs(struct got_repository *repo, const char *refname)
4883 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
4884 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
4885 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
4887 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
4888 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4890 417a6e49 2020-09-26 stsp return err;
4892 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4893 417a6e49 2020-09-26 stsp char *refstr;
4894 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
4895 417a6e49 2020-09-26 stsp if (refstr == NULL)
4896 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
4897 417a6e49 2020-09-26 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4898 417a6e49 2020-09-26 stsp free(refstr);
4901 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
4902 417a6e49 2020-09-26 stsp return NULL;
4905 417a6e49 2020-09-26 stsp static const struct got_error *
4906 417a6e49 2020-09-26 stsp delete_ref(struct got_repository *repo, const char *refname)
4908 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4909 417a6e49 2020-09-26 stsp struct got_reference *ref;
4911 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4913 417a6e49 2020-09-26 stsp return err;
4915 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
4916 417a6e49 2020-09-26 stsp got_ref_close(ref);
4917 417a6e49 2020-09-26 stsp return err;
4920 417a6e49 2020-09-26 stsp static const struct got_error *
4921 417a6e49 2020-09-26 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4923 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4924 417a6e49 2020-09-26 stsp struct got_object_id *id;
4925 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4928 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4929 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4930 417a6e49 2020-09-26 stsp * an unintended typo.
4932 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4933 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4935 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4937 417a6e49 2020-09-26 stsp if (err) {
4938 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
4940 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4941 417a6e49 2020-09-26 stsp return err;
4942 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4944 417a6e49 2020-09-26 stsp return err;
4945 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, target_ref);
4946 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4948 417a6e49 2020-09-26 stsp return err;
4951 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
4953 417a6e49 2020-09-26 stsp goto done;
4955 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4958 417a6e49 2020-09-26 stsp got_ref_close(ref);
4960 417a6e49 2020-09-26 stsp return err;
4963 417a6e49 2020-09-26 stsp static const struct got_error *
4964 417a6e49 2020-09-26 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4966 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4967 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4968 417a6e49 2020-09-26 stsp struct got_reference *target_ref = NULL;
4971 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4972 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4973 417a6e49 2020-09-26 stsp * an unintended typo.
4975 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4976 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4978 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4980 417a6e49 2020-09-26 stsp return err;
4982 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4984 417a6e49 2020-09-26 stsp goto done;
4986 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4988 417a6e49 2020-09-26 stsp if (target_ref)
4989 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4991 417a6e49 2020-09-26 stsp got_ref_close(ref);
4992 417a6e49 2020-09-26 stsp return err;
4995 417a6e49 2020-09-26 stsp static const struct got_error *
4996 417a6e49 2020-09-26 stsp cmd_ref(int argc, char *argv[])
4998 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4999 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5000 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5001 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5002 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_delete = 0;
5003 417a6e49 2020-09-26 stsp const char *obj_arg = NULL, *symref_target= NULL;
5004 417a6e49 2020-09-26 stsp char *refname = NULL;
5006 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5007 417a6e49 2020-09-26 stsp switch (ch) {
5009 417a6e49 2020-09-26 stsp obj_arg = optarg;
5012 417a6e49 2020-09-26 stsp do_delete = 1;
5015 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5016 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5017 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5019 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5022 417a6e49 2020-09-26 stsp do_list = 1;
5025 417a6e49 2020-09-26 stsp symref_target = optarg;
5028 417a6e49 2020-09-26 stsp usage_ref();
5029 417a6e49 2020-09-26 stsp /* NOTREACHED */
5033 417a6e49 2020-09-26 stsp if (obj_arg && do_list)
5034 417a6e49 2020-09-26 stsp errx(1, "-c and -l options are mutually exclusive");
5035 417a6e49 2020-09-26 stsp if (obj_arg && do_delete)
5036 417a6e49 2020-09-26 stsp errx(1, "-c and -d options are mutually exclusive");
5037 417a6e49 2020-09-26 stsp if (obj_arg && symref_target)
5038 417a6e49 2020-09-26 stsp errx(1, "-c and -s options are mutually exclusive");
5039 417a6e49 2020-09-26 stsp if (symref_target && do_delete)
5040 417a6e49 2020-09-26 stsp errx(1, "-s and -d options are mutually exclusive");
5041 417a6e49 2020-09-26 stsp if (symref_target && do_list)
5042 417a6e49 2020-09-26 stsp errx(1, "-s and -l options are mutually exclusive");
5043 417a6e49 2020-09-26 stsp if (do_delete && do_list)
5044 417a6e49 2020-09-26 stsp errx(1, "-d and -l options are mutually exclusive");
5046 417a6e49 2020-09-26 stsp argc -= optind;
5047 417a6e49 2020-09-26 stsp argv += optind;
5049 417a6e49 2020-09-26 stsp if (do_list) {
5050 417a6e49 2020-09-26 stsp if (argc != 0 && argc != 1)
5051 417a6e49 2020-09-26 stsp usage_ref();
5052 417a6e49 2020-09-26 stsp if (argc == 1) {
5053 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5054 417a6e49 2020-09-26 stsp if (refname == NULL) {
5055 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5056 417a6e49 2020-09-26 stsp goto done;
5060 417a6e49 2020-09-26 stsp if (argc != 1)
5061 417a6e49 2020-09-26 stsp usage_ref();
5062 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5063 417a6e49 2020-09-26 stsp if (refname == NULL) {
5064 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5065 417a6e49 2020-09-26 stsp goto done;
5069 417a6e49 2020-09-26 stsp if (refname)
5070 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(refname);
5072 417a6e49 2020-09-26 stsp #ifndef PROFILE
5073 417a6e49 2020-09-26 stsp if (do_list) {
5074 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5075 417a6e49 2020-09-26 stsp NULL) == -1)
5076 417a6e49 2020-09-26 stsp err(1, "pledge");
5078 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5079 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5080 417a6e49 2020-09-26 stsp err(1, "pledge");
5083 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5084 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5085 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5086 417a6e49 2020-09-26 stsp goto done;
5089 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5090 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5091 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5092 417a6e49 2020-09-26 stsp goto done;
5094 417a6e49 2020-09-26 stsp error = NULL;
5095 417a6e49 2020-09-26 stsp if (worktree) {
5096 417a6e49 2020-09-26 stsp repo_path =
5097 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5098 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5099 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5100 417a6e49 2020-09-26 stsp if (error)
5101 417a6e49 2020-09-26 stsp goto done;
5103 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5104 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5105 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5106 417a6e49 2020-09-26 stsp goto done;
5111 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5112 417a6e49 2020-09-26 stsp if (error != NULL)
5113 417a6e49 2020-09-26 stsp goto done;
5115 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5116 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5117 417a6e49 2020-09-26 stsp if (error)
5118 417a6e49 2020-09-26 stsp goto done;
5120 417a6e49 2020-09-26 stsp if (do_list)
5121 417a6e49 2020-09-26 stsp error = list_refs(repo, refname);
5122 417a6e49 2020-09-26 stsp else if (do_delete)
5123 417a6e49 2020-09-26 stsp error = delete_ref(repo, refname);
5124 417a6e49 2020-09-26 stsp else if (symref_target)
5125 417a6e49 2020-09-26 stsp error = add_symref(repo, refname, symref_target);
5127 417a6e49 2020-09-26 stsp if (obj_arg == NULL)
5128 417a6e49 2020-09-26 stsp usage_ref();
5129 417a6e49 2020-09-26 stsp error = add_ref(repo, refname, obj_arg);
5132 417a6e49 2020-09-26 stsp free(refname);
5134 417a6e49 2020-09-26 stsp got_repo_close(repo);
5135 417a6e49 2020-09-26 stsp if (worktree)
5136 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5137 417a6e49 2020-09-26 stsp free(cwd);
5138 417a6e49 2020-09-26 stsp free(repo_path);
5139 417a6e49 2020-09-26 stsp return error;
5142 417a6e49 2020-09-26 stsp __dead static void
5143 417a6e49 2020-09-26 stsp usage_branch(void)
5145 417a6e49 2020-09-26 stsp fprintf(stderr,
5146 417a6e49 2020-09-26 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5147 417a6e49 2020-09-26 stsp "[name]\n", getprogname());
5151 417a6e49 2020-09-26 stsp static const struct got_error *
5152 417a6e49 2020-09-26 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5153 417a6e49 2020-09-26 stsp struct got_reference *ref)
5155 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5156 417a6e49 2020-09-26 stsp const char *refname, *marker = " ";
5157 417a6e49 2020-09-26 stsp char *refstr;
5159 417a6e49 2020-09-26 stsp refname = got_ref_get_name(ref);
5160 417a6e49 2020-09-26 stsp if (worktree && strcmp(refname,
5161 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5162 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
5164 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
5166 417a6e49 2020-09-26 stsp return err;
5167 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id,
5168 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5169 417a6e49 2020-09-26 stsp marker = "* ";
5171 417a6e49 2020-09-26 stsp marker = "~ ";
5175 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5176 417a6e49 2020-09-26 stsp refname += 11;
5177 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5178 417a6e49 2020-09-26 stsp refname += 18;
5180 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(ref);
5181 417a6e49 2020-09-26 stsp if (refstr == NULL)
5182 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
5184 417a6e49 2020-09-26 stsp printf("%s%s: %s\n", marker, refname, refstr);
5185 417a6e49 2020-09-26 stsp free(refstr);
5186 417a6e49 2020-09-26 stsp return NULL;
5189 417a6e49 2020-09-26 stsp static const struct got_error *
5190 417a6e49 2020-09-26 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5192 417a6e49 2020-09-26 stsp const char *refname;
5194 417a6e49 2020-09-26 stsp if (worktree == NULL)
5195 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5197 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
5199 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5200 417a6e49 2020-09-26 stsp refname += 11;
5201 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5202 417a6e49 2020-09-26 stsp refname += 18;
5204 417a6e49 2020-09-26 stsp printf("%s\n", refname);
5206 417a6e49 2020-09-26 stsp return NULL;
5209 417a6e49 2020-09-26 stsp static const struct got_error *
5210 417a6e49 2020-09-26 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5212 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5213 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5214 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5215 417a6e49 2020-09-26 stsp struct got_reference *temp_ref = NULL;
5216 417a6e49 2020-09-26 stsp int rebase_in_progress, histedit_in_progress;
5218 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5220 417a6e49 2020-09-26 stsp if (worktree) {
5221 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5222 417a6e49 2020-09-26 stsp worktree);
5224 417a6e49 2020-09-26 stsp return err;
5226 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5227 417a6e49 2020-09-26 stsp worktree);
5229 417a6e49 2020-09-26 stsp return err;
5231 417a6e49 2020-09-26 stsp if (rebase_in_progress || histedit_in_progress) {
5232 417a6e49 2020-09-26 stsp err = got_ref_open(&temp_ref, repo,
5233 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
5235 417a6e49 2020-09-26 stsp return err;
5236 417a6e49 2020-09-26 stsp list_branch(repo, worktree, temp_ref);
5237 417a6e49 2020-09-26 stsp got_ref_close(temp_ref);
5241 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/heads",
5242 417a6e49 2020-09-26 stsp got_ref_cmp_by_name, NULL);
5244 417a6e49 2020-09-26 stsp return err;
5246 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5247 417a6e49 2020-09-26 stsp list_branch(repo, worktree, re->ref);
5249 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5250 417a6e49 2020-09-26 stsp return NULL;
5253 417a6e49 2020-09-26 stsp static const struct got_error *
5254 417a6e49 2020-09-26 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5255 417a6e49 2020-09-26 stsp const char *branch_name)
5257 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5258 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5259 417a6e49 2020-09-26 stsp char *refname;
5261 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5262 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
5264 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5266 417a6e49 2020-09-26 stsp goto done;
5268 417a6e49 2020-09-26 stsp if (worktree &&
5269 417a6e49 2020-09-26 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5270 417a6e49 2020-09-26 stsp got_ref_get_name(ref)) == 0) {
5271 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5272 417a6e49 2020-09-26 stsp "will not delete this work tree's current branch");
5273 417a6e49 2020-09-26 stsp goto done;
5276 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
5279 417a6e49 2020-09-26 stsp got_ref_close(ref);
5280 417a6e49 2020-09-26 stsp free(refname);
5281 417a6e49 2020-09-26 stsp return err;
5284 417a6e49 2020-09-26 stsp static const struct got_error *
5285 417a6e49 2020-09-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5286 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id)
5288 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5289 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5290 417a6e49 2020-09-26 stsp char *base_refname = NULL, *refname = NULL;
5293 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
5294 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5295 417a6e49 2020-09-26 stsp * an unintended typo.
5297 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
5298 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5300 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5301 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5302 417a6e49 2020-09-26 stsp goto done;
5305 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5306 417a6e49 2020-09-26 stsp if (err == NULL) {
5307 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5308 417a6e49 2020-09-26 stsp goto done;
5309 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5310 417a6e49 2020-09-26 stsp goto done;
5312 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5314 417a6e49 2020-09-26 stsp goto done;
5316 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5319 417a6e49 2020-09-26 stsp got_ref_close(ref);
5320 417a6e49 2020-09-26 stsp free(base_refname);
5321 417a6e49 2020-09-26 stsp free(refname);
5322 417a6e49 2020-09-26 stsp return err;
5325 417a6e49 2020-09-26 stsp static const struct got_error *
5326 417a6e49 2020-09-26 stsp cmd_branch(int argc, char *argv[])
5328 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5329 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5330 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5331 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5332 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5333 417a6e49 2020-09-26 stsp const char *delref = NULL, *commit_id_arg = NULL;
5334 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5335 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
5336 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
5337 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
5338 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
5340 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
5342 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5343 417a6e49 2020-09-26 stsp switch (ch) {
5345 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5348 417a6e49 2020-09-26 stsp delref = optarg;
5351 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5352 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5353 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5355 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5358 417a6e49 2020-09-26 stsp do_list = 1;
5361 417a6e49 2020-09-26 stsp do_update = 0;
5364 417a6e49 2020-09-26 stsp usage_branch();
5365 417a6e49 2020-09-26 stsp /* NOTREACHED */
5369 417a6e49 2020-09-26 stsp if (do_list && delref)
5370 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
5372 417a6e49 2020-09-26 stsp argc -= optind;
5373 417a6e49 2020-09-26 stsp argv += optind;
5375 417a6e49 2020-09-26 stsp if (!do_list && !delref && argc == 0)
5376 417a6e49 2020-09-26 stsp do_show = 1;
5378 417a6e49 2020-09-26 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5379 417a6e49 2020-09-26 stsp errx(1, "-c option can only be used when creating a branch");
5381 417a6e49 2020-09-26 stsp if (do_list || delref) {
5382 417a6e49 2020-09-26 stsp if (argc > 0)
5383 417a6e49 2020-09-26 stsp usage_branch();
5384 417a6e49 2020-09-26 stsp } else if (!do_show && argc != 1)
5385 417a6e49 2020-09-26 stsp usage_branch();
5387 417a6e49 2020-09-26 stsp #ifndef PROFILE
5388 417a6e49 2020-09-26 stsp if (do_list || do_show) {
5389 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5390 417a6e49 2020-09-26 stsp NULL) == -1)
5391 417a6e49 2020-09-26 stsp err(1, "pledge");
5393 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5394 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5395 417a6e49 2020-09-26 stsp err(1, "pledge");
5398 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5399 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5400 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5401 417a6e49 2020-09-26 stsp goto done;
5404 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5405 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5406 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5407 417a6e49 2020-09-26 stsp goto done;
5409 417a6e49 2020-09-26 stsp error = NULL;
5410 417a6e49 2020-09-26 stsp if (worktree) {
5411 417a6e49 2020-09-26 stsp repo_path =
5412 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5413 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5414 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5415 417a6e49 2020-09-26 stsp if (error)
5416 417a6e49 2020-09-26 stsp goto done;
5418 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5419 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5420 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5421 417a6e49 2020-09-26 stsp goto done;
5426 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5427 417a6e49 2020-09-26 stsp if (error != NULL)
5428 417a6e49 2020-09-26 stsp goto done;
5430 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5431 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5432 417a6e49 2020-09-26 stsp if (error)
5433 417a6e49 2020-09-26 stsp goto done;
5435 417a6e49 2020-09-26 stsp if (do_show)
5436 417a6e49 2020-09-26 stsp error = show_current_branch(repo, worktree);
5437 417a6e49 2020-09-26 stsp else if (do_list)
5438 417a6e49 2020-09-26 stsp error = list_branches(repo, worktree);
5439 417a6e49 2020-09-26 stsp else if (delref)
5440 417a6e49 2020-09-26 stsp error = delete_branch(repo, worktree, delref);
5442 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL)
5443 417a6e49 2020-09-26 stsp commit_id_arg = worktree ?
5444 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) :
5445 417a6e49 2020-09-26 stsp GOT_REF_HEAD;
5446 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
5447 417a6e49 2020-09-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5448 417a6e49 2020-09-26 stsp if (error)
5449 417a6e49 2020-09-26 stsp goto done;
5450 417a6e49 2020-09-26 stsp error = add_branch(repo, argv[0], commit_id);
5451 417a6e49 2020-09-26 stsp if (error)
5452 417a6e49 2020-09-26 stsp goto done;
5453 417a6e49 2020-09-26 stsp if (worktree && do_update) {
5454 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
5455 417a6e49 2020-09-26 stsp char *branch_refname = NULL;
5457 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5458 417a6e49 2020-09-26 stsp if (error)
5459 417a6e49 2020-09-26 stsp goto done;
5460 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5461 417a6e49 2020-09-26 stsp worktree);
5462 417a6e49 2020-09-26 stsp if (error)
5463 417a6e49 2020-09-26 stsp goto done;
5464 417a6e49 2020-09-26 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5466 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
5467 417a6e49 2020-09-26 stsp goto done;
5469 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5470 417a6e49 2020-09-26 stsp free(branch_refname);
5471 417a6e49 2020-09-26 stsp if (error)
5472 417a6e49 2020-09-26 stsp goto done;
5473 417a6e49 2020-09-26 stsp error = switch_head_ref(ref, commit_id, worktree,
5475 417a6e49 2020-09-26 stsp if (error)
5476 417a6e49 2020-09-26 stsp goto done;
5477 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5478 417a6e49 2020-09-26 stsp commit_id);
5479 417a6e49 2020-09-26 stsp if (error)
5480 417a6e49 2020-09-26 stsp goto done;
5481 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
5482 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths,
5483 417a6e49 2020-09-26 stsp repo, update_progress, &upa, check_cancelled,
5485 417a6e49 2020-09-26 stsp if (error)
5486 417a6e49 2020-09-26 stsp goto done;
5487 417a6e49 2020-09-26 stsp if (upa.did_something)
5488 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
5489 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
5494 417a6e49 2020-09-26 stsp got_ref_close(ref);
5496 417a6e49 2020-09-26 stsp got_repo_close(repo);
5497 417a6e49 2020-09-26 stsp if (worktree)
5498 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5499 417a6e49 2020-09-26 stsp free(cwd);
5500 417a6e49 2020-09-26 stsp free(repo_path);
5501 417a6e49 2020-09-26 stsp free(commit_id);
5502 417a6e49 2020-09-26 stsp free(commit_id_str);
5503 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
5504 417a6e49 2020-09-26 stsp free((char *)pe->path);
5505 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
5506 417a6e49 2020-09-26 stsp return error;
5510 417a6e49 2020-09-26 stsp __dead static void
5511 417a6e49 2020-09-26 stsp usage_tag(void)
5513 417a6e49 2020-09-26 stsp fprintf(stderr,
5514 417a6e49 2020-09-26 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5515 417a6e49 2020-09-26 stsp "[-m message] name\n", getprogname());
5520 417a6e49 2020-09-26 stsp static const struct got_error *
5521 417a6e49 2020-09-26 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5524 417a6e49 2020-09-26 stsp struct got_reflist_entry *re, *se, *new;
5525 417a6e49 2020-09-26 stsp struct got_object_id *re_id, *se_id;
5526 417a6e49 2020-09-26 stsp struct got_tag_object *re_tag, *se_tag;
5527 417a6e49 2020-09-26 stsp time_t re_time, se_time;
5529 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5530 417a6e49 2020-09-26 stsp se = SIMPLEQ_FIRST(sorted);
5531 417a6e49 2020-09-26 stsp if (se == NULL) {
5532 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5534 417a6e49 2020-09-26 stsp return err;
5535 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5538 417a6e49 2020-09-26 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5541 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5542 417a6e49 2020-09-26 stsp free(re_id);
5545 417a6e49 2020-09-26 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5546 417a6e49 2020-09-26 stsp got_object_tag_close(re_tag);
5549 417a6e49 2020-09-26 stsp while (se) {
5550 417a6e49 2020-09-26 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5553 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5554 417a6e49 2020-09-26 stsp free(se_id);
5557 417a6e49 2020-09-26 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5558 417a6e49 2020-09-26 stsp got_object_tag_close(se_tag);
5560 417a6e49 2020-09-26 stsp if (se_time > re_time) {
5561 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5563 417a6e49 2020-09-26 stsp return err;
5564 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5567 417a6e49 2020-09-26 stsp se = SIMPLEQ_NEXT(se, entry);
5572 417a6e49 2020-09-26 stsp return err;
5576 417a6e49 2020-09-26 stsp static const struct got_error *
5577 417a6e49 2020-09-26 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5579 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5580 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5581 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5583 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5585 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5587 417a6e49 2020-09-26 stsp return err;
5589 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5590 417a6e49 2020-09-26 stsp const char *refname;
5591 417a6e49 2020-09-26 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5592 417a6e49 2020-09-26 stsp char datebuf[26];
5593 417a6e49 2020-09-26 stsp const char *tagger;
5594 417a6e49 2020-09-26 stsp time_t tagger_time;
5595 417a6e49 2020-09-26 stsp struct got_object_id *id;
5596 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
5597 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
5599 417a6e49 2020-09-26 stsp refname = got_ref_get_name(re->ref);
5600 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5602 417a6e49 2020-09-26 stsp refname += 10;
5603 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
5604 417a6e49 2020-09-26 stsp if (refstr == NULL) {
5605 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_ref_to_str");
5608 417a6e49 2020-09-26 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5609 417a6e49 2020-09-26 stsp free(refstr);
5611 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, re->ref);
5614 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
5615 417a6e49 2020-09-26 stsp if (err) {
5616 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5620 417a6e49 2020-09-26 stsp /* "lightweight" tag */
5621 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
5622 417a6e49 2020-09-26 stsp if (err) {
5626 417a6e49 2020-09-26 stsp tagger = got_object_commit_get_committer(commit);
5627 417a6e49 2020-09-26 stsp tagger_time =
5628 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit);
5629 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
5635 417a6e49 2020-09-26 stsp tagger = got_object_tag_get_tagger(tag);
5636 417a6e49 2020-09-26 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5637 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
5638 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
5642 417a6e49 2020-09-26 stsp printf("from: %s\n", tagger);
5643 417a6e49 2020-09-26 stsp datestr = get_datestr(&tagger_time, datebuf);
5644 417a6e49 2020-09-26 stsp if (datestr)
5645 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
5646 417a6e49 2020-09-26 stsp if (commit)
5647 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5649 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
5650 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
5651 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5654 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
5655 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5658 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
5659 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5662 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
5663 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5670 417a6e49 2020-09-26 stsp free(id_str);
5671 417a6e49 2020-09-26 stsp if (commit) {
5672 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5675 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
5677 417a6e49 2020-09-26 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5678 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
5679 417a6e49 2020-09-26 stsp if (tagmsg0 == NULL) {
5680 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5685 417a6e49 2020-09-26 stsp tagmsg = tagmsg0;
5687 417a6e49 2020-09-26 stsp line = strsep(&tagmsg, "\n");
5689 417a6e49 2020-09-26 stsp printf(" %s\n", line);
5690 417a6e49 2020-09-26 stsp } while (line);
5691 417a6e49 2020-09-26 stsp free(tagmsg0);
5694 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5695 417a6e49 2020-09-26 stsp return NULL;
5698 417a6e49 2020-09-26 stsp static const struct got_error *
5699 417a6e49 2020-09-26 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5700 417a6e49 2020-09-26 stsp const char *tag_name, const char *repo_path)
5702 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5703 417a6e49 2020-09-26 stsp char *template = NULL, *initial_content = NULL;
5704 417a6e49 2020-09-26 stsp char *editor = NULL;
5705 417a6e49 2020-09-26 stsp int initial_content_len;
5706 417a6e49 2020-09-26 stsp int fd = -1;
5708 417a6e49 2020-09-26 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5709 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5710 417a6e49 2020-09-26 stsp goto done;
5713 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
5714 417a6e49 2020-09-26 stsp "\n# tagging commit %s as %s\n",
5715 417a6e49 2020-09-26 stsp commit_id_str, tag_name);
5716 417a6e49 2020-09-26 stsp if (initial_content_len == -1) {
5717 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5718 417a6e49 2020-09-26 stsp goto done;
5721 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5723 417a6e49 2020-09-26 stsp goto done;
5725 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5726 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *tagmsg_path);
5727 417a6e49 2020-09-26 stsp goto done;
5730 417a6e49 2020-09-26 stsp err = get_editor(&editor);
5732 417a6e49 2020-09-26 stsp goto done;
5733 417a6e49 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5735 417a6e49 2020-09-26 stsp free(initial_content);
5736 417a6e49 2020-09-26 stsp free(template);
5737 417a6e49 2020-09-26 stsp free(editor);
5739 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5740 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *tagmsg_path);
5742 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
5743 417a6e49 2020-09-26 stsp if (err == NULL)
5744 417a6e49 2020-09-26 stsp err = apply_unveil(repo_path, 0, NULL);
5745 417a6e49 2020-09-26 stsp if (err) {
5746 417a6e49 2020-09-26 stsp free(*tagmsg);
5747 417a6e49 2020-09-26 stsp *tagmsg = NULL;
5749 417a6e49 2020-09-26 stsp return err;
5752 417a6e49 2020-09-26 stsp static const struct got_error *
5753 417a6e49 2020-09-26 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5754 417a6e49 2020-09-26 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5756 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5757 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5758 417a6e49 2020-09-26 stsp char *label = NULL, *commit_id_str = NULL;
5759 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5760 417a6e49 2020-09-26 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5761 417a6e49 2020-09-26 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5762 417a6e49 2020-09-26 stsp int preserve_tagmsg = 0;
5765 417a6e49 2020-09-26 stsp * Don't let the user create a tag name with a leading '-'.
5766 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5767 417a6e49 2020-09-26 stsp * an unintended typo.
5769 417a6e49 2020-09-26 stsp if (tag_name[0] == '-')
5770 417a6e49 2020-09-26 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5772 417a6e49 2020-09-26 stsp err = get_author(&tagger, repo, worktree);
5774 417a6e49 2020-09-26 stsp return err;
5776 417a6e49 2020-09-26 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5777 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5779 417a6e49 2020-09-26 stsp goto done;
5781 417a6e49 2020-09-26 stsp err = got_object_id_str(&commit_id_str, commit_id);
5783 417a6e49 2020-09-26 stsp goto done;
5785 417a6e49 2020-09-26 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5786 417a6e49 2020-09-26 stsp refname = strdup(tag_name);
5787 417a6e49 2020-09-26 stsp if (refname == NULL) {
5788 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5789 417a6e49 2020-09-26 stsp goto done;
5791 417a6e49 2020-09-26 stsp tag_name += 10;
5792 417a6e49 2020-09-26 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5793 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5794 417a6e49 2020-09-26 stsp goto done;
5797 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5798 417a6e49 2020-09-26 stsp if (err == NULL) {
5799 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5800 417a6e49 2020-09-26 stsp goto done;
5801 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5802 417a6e49 2020-09-26 stsp goto done;
5804 417a6e49 2020-09-26 stsp if (tagmsg_arg == NULL) {
5805 417a6e49 2020-09-26 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5806 417a6e49 2020-09-26 stsp tag_name, got_repo_get_path(repo));
5807 417a6e49 2020-09-26 stsp if (err) {
5808 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5809 417a6e49 2020-09-26 stsp tagmsg_path != NULL)
5810 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5811 417a6e49 2020-09-26 stsp goto done;
5815 417a6e49 2020-09-26 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5816 417a6e49 2020-09-26 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5817 417a6e49 2020-09-26 stsp if (err) {
5818 417a6e49 2020-09-26 stsp if (tagmsg_path)
5819 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5820 417a6e49 2020-09-26 stsp goto done;
5823 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, tag_id);
5824 417a6e49 2020-09-26 stsp if (err) {
5825 417a6e49 2020-09-26 stsp if (tagmsg_path)
5826 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5827 417a6e49 2020-09-26 stsp goto done;
5830 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5831 417a6e49 2020-09-26 stsp if (err) {
5832 417a6e49 2020-09-26 stsp if (tagmsg_path)
5833 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5834 417a6e49 2020-09-26 stsp goto done;
5837 417a6e49 2020-09-26 stsp err = got_object_id_str(&tag_id_str, tag_id);
5838 417a6e49 2020-09-26 stsp if (err) {
5839 417a6e49 2020-09-26 stsp if (tagmsg_path)
5840 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5841 417a6e49 2020-09-26 stsp goto done;
5843 417a6e49 2020-09-26 stsp printf("Created tag %s\n", tag_id_str);
5845 417a6e49 2020-09-26 stsp if (preserve_tagmsg) {
5846 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5847 417a6e49 2020-09-26 stsp getprogname(), tagmsg_path);
5848 417a6e49 2020-09-26 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5849 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5850 417a6e49 2020-09-26 stsp free(tag_id_str);
5852 417a6e49 2020-09-26 stsp got_ref_close(ref);
5853 417a6e49 2020-09-26 stsp free(commit_id);
5854 417a6e49 2020-09-26 stsp free(commit_id_str);
5855 417a6e49 2020-09-26 stsp free(refname);
5856 417a6e49 2020-09-26 stsp free(tagmsg);
5857 417a6e49 2020-09-26 stsp free(tagmsg_path);
5858 417a6e49 2020-09-26 stsp free(tagger);
5859 417a6e49 2020-09-26 stsp return err;
5862 417a6e49 2020-09-26 stsp static const struct got_error *
5863 417a6e49 2020-09-26 stsp cmd_tag(int argc, char *argv[])
5865 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5866 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5867 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5868 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5869 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL;
5870 417a6e49 2020-09-26 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5871 417a6e49 2020-09-26 stsp int ch, do_list = 0;
5873 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5874 417a6e49 2020-09-26 stsp switch (ch) {
5876 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5879 417a6e49 2020-09-26 stsp tagmsg = optarg;
5882 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5883 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5884 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5886 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5889 417a6e49 2020-09-26 stsp do_list = 1;
5892 417a6e49 2020-09-26 stsp usage_tag();
5893 417a6e49 2020-09-26 stsp /* NOTREACHED */
5897 417a6e49 2020-09-26 stsp argc -= optind;
5898 417a6e49 2020-09-26 stsp argv += optind;
5900 417a6e49 2020-09-26 stsp if (do_list) {
5901 417a6e49 2020-09-26 stsp if (commit_id_arg != NULL)
5903 417a6e49 2020-09-26 stsp "-c option can only be used when creating a tag");
5904 417a6e49 2020-09-26 stsp if (tagmsg)
5905 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
5906 417a6e49 2020-09-26 stsp if (argc > 0)
5907 417a6e49 2020-09-26 stsp usage_tag();
5908 417a6e49 2020-09-26 stsp } else if (argc != 1)
5909 417a6e49 2020-09-26 stsp usage_tag();
5911 417a6e49 2020-09-26 stsp tag_name = argv[0];
5913 417a6e49 2020-09-26 stsp #ifndef PROFILE
5914 417a6e49 2020-09-26 stsp if (do_list) {
5915 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5916 417a6e49 2020-09-26 stsp NULL) == -1)
5917 417a6e49 2020-09-26 stsp err(1, "pledge");
5919 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5920 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5921 417a6e49 2020-09-26 stsp err(1, "pledge");
5924 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5925 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5926 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5927 417a6e49 2020-09-26 stsp goto done;
5930 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5931 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5932 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5933 417a6e49 2020-09-26 stsp goto done;
5935 417a6e49 2020-09-26 stsp error = NULL;
5936 417a6e49 2020-09-26 stsp if (worktree) {
5937 417a6e49 2020-09-26 stsp repo_path =
5938 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5939 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5940 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5941 417a6e49 2020-09-26 stsp if (error)
5942 417a6e49 2020-09-26 stsp goto done;
5944 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5945 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5946 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5947 417a6e49 2020-09-26 stsp goto done;
5952 417a6e49 2020-09-26 stsp if (do_list) {
5953 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5954 417a6e49 2020-09-26 stsp if (error != NULL)
5955 417a6e49 2020-09-26 stsp goto done;
5956 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5957 417a6e49 2020-09-26 stsp if (error)
5958 417a6e49 2020-09-26 stsp goto done;
5959 417a6e49 2020-09-26 stsp error = list_tags(repo, worktree);
5961 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
5962 417a6e49 2020-09-26 stsp if (error)
5963 417a6e49 2020-09-26 stsp goto done;
5964 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5965 417a6e49 2020-09-26 stsp if (error != NULL)
5966 417a6e49 2020-09-26 stsp goto done;
5968 417a6e49 2020-09-26 stsp if (tagmsg) {
5969 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5970 417a6e49 2020-09-26 stsp if (error)
5971 417a6e49 2020-09-26 stsp goto done;
5974 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL) {
5975 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
5976 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
5977 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
5978 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5979 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
5980 417a6e49 2020-09-26 stsp if (error)
5981 417a6e49 2020-09-26 stsp goto done;
5982 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5983 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
5984 417a6e49 2020-09-26 stsp if (error)
5985 417a6e49 2020-09-26 stsp goto done;
5986 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5987 417a6e49 2020-09-26 stsp free(commit_id);
5988 417a6e49 2020-09-26 stsp if (error)
5989 417a6e49 2020-09-26 stsp goto done;
5992 417a6e49 2020-09-26 stsp error = add_tag(repo, worktree, tag_name,
5993 417a6e49 2020-09-26 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5997 417a6e49 2020-09-26 stsp got_repo_close(repo);
5998 417a6e49 2020-09-26 stsp if (worktree)
5999 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6000 417a6e49 2020-09-26 stsp free(cwd);
6001 417a6e49 2020-09-26 stsp free(repo_path);
6002 417a6e49 2020-09-26 stsp free(gitconfig_path);
6003 417a6e49 2020-09-26 stsp free(commit_id_str);
6004 417a6e49 2020-09-26 stsp return error;
6007 417a6e49 2020-09-26 stsp __dead static void
6008 417a6e49 2020-09-26 stsp usage_add(void)
6010 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6011 417a6e49 2020-09-26 stsp getprogname());
6015 417a6e49 2020-09-26 stsp static const struct got_error *
6016 417a6e49 2020-09-26 stsp add_progress(void *arg, unsigned char status, const char *path)
6018 417a6e49 2020-09-26 stsp while (path[0] == '/')
6020 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6021 417a6e49 2020-09-26 stsp return NULL;
6024 417a6e49 2020-09-26 stsp static const struct got_error *
6025 417a6e49 2020-09-26 stsp cmd_add(int argc, char *argv[])
6027 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6028 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6029 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6030 417a6e49 2020-09-26 stsp char *cwd = NULL;
6031 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6032 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6033 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, no_ignores = 0;
6035 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6037 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "IR")) != -1) {
6038 417a6e49 2020-09-26 stsp switch (ch) {
6040 417a6e49 2020-09-26 stsp no_ignores = 1;
6043 417a6e49 2020-09-26 stsp can_recurse = 1;
6046 417a6e49 2020-09-26 stsp usage_add();
6047 417a6e49 2020-09-26 stsp /* NOTREACHED */
6051 417a6e49 2020-09-26 stsp argc -= optind;
6052 417a6e49 2020-09-26 stsp argv += optind;
6054 417a6e49 2020-09-26 stsp #ifndef PROFILE
6055 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6056 417a6e49 2020-09-26 stsp NULL) == -1)
6057 417a6e49 2020-09-26 stsp err(1, "pledge");
6059 417a6e49 2020-09-26 stsp if (argc < 1)
6060 417a6e49 2020-09-26 stsp usage_add();
6062 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6063 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6064 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6065 417a6e49 2020-09-26 stsp goto done;
6068 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6069 417a6e49 2020-09-26 stsp if (error) {
6070 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6071 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "add", cwd);
6072 417a6e49 2020-09-26 stsp goto done;
6075 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6077 417a6e49 2020-09-26 stsp if (error != NULL)
6078 417a6e49 2020-09-26 stsp goto done;
6080 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6081 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6082 417a6e49 2020-09-26 stsp if (error)
6083 417a6e49 2020-09-26 stsp goto done;
6085 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6086 417a6e49 2020-09-26 stsp if (error)
6087 417a6e49 2020-09-26 stsp goto done;
6089 417a6e49 2020-09-26 stsp if (!can_recurse && no_ignores) {
6090 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6091 417a6e49 2020-09-26 stsp "disregarding ignores requires -R option");
6092 417a6e49 2020-09-26 stsp goto done;
6096 417a6e49 2020-09-26 stsp if (!can_recurse) {
6097 417a6e49 2020-09-26 stsp char *ondisk_path;
6098 417a6e49 2020-09-26 stsp struct stat sb;
6099 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6100 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6101 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6102 417a6e49 2020-09-26 stsp pe->path) == -1) {
6103 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6104 417a6e49 2020-09-26 stsp goto done;
6106 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6107 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6108 417a6e49 2020-09-26 stsp free(ondisk_path);
6111 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6112 417a6e49 2020-09-26 stsp ondisk_path);
6113 417a6e49 2020-09-26 stsp free(ondisk_path);
6114 417a6e49 2020-09-26 stsp goto done;
6116 417a6e49 2020-09-26 stsp free(ondisk_path);
6117 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6118 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6119 417a6e49 2020-09-26 stsp "adding directories requires -R option");
6120 417a6e49 2020-09-26 stsp goto done;
6125 417a6e49 2020-09-26 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6126 417a6e49 2020-09-26 stsp NULL, repo, no_ignores);
6129 417a6e49 2020-09-26 stsp got_repo_close(repo);
6130 417a6e49 2020-09-26 stsp if (worktree)
6131 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6132 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6133 417a6e49 2020-09-26 stsp free((char *)pe->path);
6134 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6135 417a6e49 2020-09-26 stsp free(cwd);
6136 417a6e49 2020-09-26 stsp return error;
6139 417a6e49 2020-09-26 stsp __dead static void
6140 417a6e49 2020-09-26 stsp usage_remove(void)
6142 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6143 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6147 417a6e49 2020-09-26 stsp static const struct got_error *
6148 417a6e49 2020-09-26 stsp print_remove_status(void *arg, unsigned char status,
6149 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path)
6151 417a6e49 2020-09-26 stsp while (path[0] == '/')
6153 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
6154 417a6e49 2020-09-26 stsp return NULL;
6155 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6156 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
6157 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
6158 417a6e49 2020-09-26 stsp return NULL;
6161 417a6e49 2020-09-26 stsp static const struct got_error *
6162 417a6e49 2020-09-26 stsp cmd_remove(int argc, char *argv[])
6164 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6165 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6166 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6167 417a6e49 2020-09-26 stsp const char *status_codes = NULL;
6168 417a6e49 2020-09-26 stsp char *cwd = NULL;
6169 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6170 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6171 417a6e49 2020-09-26 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6173 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6175 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6176 417a6e49 2020-09-26 stsp switch (ch) {
6178 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6181 417a6e49 2020-09-26 stsp keep_on_disk = 1;
6184 417a6e49 2020-09-26 stsp can_recurse = 1;
6187 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
6188 417a6e49 2020-09-26 stsp switch (optarg[i]) {
6189 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6190 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6192 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
6195 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
6196 417a6e49 2020-09-26 stsp optarg[i]);
6199 417a6e49 2020-09-26 stsp status_codes = optarg;
6202 417a6e49 2020-09-26 stsp usage_remove();
6203 417a6e49 2020-09-26 stsp /* NOTREACHED */
6207 417a6e49 2020-09-26 stsp argc -= optind;
6208 417a6e49 2020-09-26 stsp argv += optind;
6210 417a6e49 2020-09-26 stsp #ifndef PROFILE
6211 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6212 417a6e49 2020-09-26 stsp NULL) == -1)
6213 417a6e49 2020-09-26 stsp err(1, "pledge");
6215 417a6e49 2020-09-26 stsp if (argc < 1)
6216 417a6e49 2020-09-26 stsp usage_remove();
6218 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6219 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6220 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6221 417a6e49 2020-09-26 stsp goto done;
6223 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6224 417a6e49 2020-09-26 stsp if (error) {
6225 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6226 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6227 417a6e49 2020-09-26 stsp goto done;
6230 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6232 417a6e49 2020-09-26 stsp if (error)
6233 417a6e49 2020-09-26 stsp goto done;
6235 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6236 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6237 417a6e49 2020-09-26 stsp if (error)
6238 417a6e49 2020-09-26 stsp goto done;
6240 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6241 417a6e49 2020-09-26 stsp if (error)
6242 417a6e49 2020-09-26 stsp goto done;
6244 417a6e49 2020-09-26 stsp if (!can_recurse) {
6245 417a6e49 2020-09-26 stsp char *ondisk_path;
6246 417a6e49 2020-09-26 stsp struct stat sb;
6247 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6248 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6249 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6250 417a6e49 2020-09-26 stsp pe->path) == -1) {
6251 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6252 417a6e49 2020-09-26 stsp goto done;
6254 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6255 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6256 417a6e49 2020-09-26 stsp free(ondisk_path);
6259 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6260 417a6e49 2020-09-26 stsp ondisk_path);
6261 417a6e49 2020-09-26 stsp free(ondisk_path);
6262 417a6e49 2020-09-26 stsp goto done;
6264 417a6e49 2020-09-26 stsp free(ondisk_path);
6265 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6266 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6267 417a6e49 2020-09-26 stsp "removing directories requires -R option");
6268 417a6e49 2020-09-26 stsp goto done;
6273 417a6e49 2020-09-26 stsp error = got_worktree_schedule_delete(worktree, &paths,
6274 417a6e49 2020-09-26 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6275 417a6e49 2020-09-26 stsp repo, keep_on_disk);
6278 417a6e49 2020-09-26 stsp got_repo_close(repo);
6279 417a6e49 2020-09-26 stsp if (worktree)
6280 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6281 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6282 417a6e49 2020-09-26 stsp free((char *)pe->path);
6283 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6284 417a6e49 2020-09-26 stsp free(cwd);
6285 417a6e49 2020-09-26 stsp return error;
6288 417a6e49 2020-09-26 stsp __dead static void
6289 417a6e49 2020-09-26 stsp usage_revert(void)
6291 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6292 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6296 417a6e49 2020-09-26 stsp static const struct got_error *
6297 417a6e49 2020-09-26 stsp revert_progress(void *arg, unsigned char status, const char *path)
6299 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_UNVERSIONED)
6300 417a6e49 2020-09-26 stsp return NULL;
6302 417a6e49 2020-09-26 stsp while (path[0] == '/')
6304 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6305 417a6e49 2020-09-26 stsp return NULL;
6308 417a6e49 2020-09-26 stsp struct choose_patch_arg {
6309 417a6e49 2020-09-26 stsp FILE *patch_script_file;
6310 417a6e49 2020-09-26 stsp const char *action;
6313 417a6e49 2020-09-26 stsp static const struct got_error *
6314 417a6e49 2020-09-26 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6315 417a6e49 2020-09-26 stsp int nchanges, const char *action)
6317 417a6e49 2020-09-26 stsp char *line = NULL;
6318 417a6e49 2020-09-26 stsp size_t linesize = 0;
6319 417a6e49 2020-09-26 stsp ssize_t linelen;
6321 417a6e49 2020-09-26 stsp switch (status) {
6322 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
6323 417a6e49 2020-09-26 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6325 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
6326 417a6e49 2020-09-26 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6328 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6329 417a6e49 2020-09-26 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6330 417a6e49 2020-09-26 stsp return got_error_from_errno("fseek");
6331 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6332 417a6e49 2020-09-26 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6333 417a6e49 2020-09-26 stsp printf("%s", line);
6334 417a6e49 2020-09-26 stsp if (ferror(patch_file))
6335 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6336 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6337 417a6e49 2020-09-26 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6338 417a6e49 2020-09-26 stsp path, n, nchanges, action);
6341 417a6e49 2020-09-26 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6344 417a6e49 2020-09-26 stsp return NULL;
6347 417a6e49 2020-09-26 stsp static const struct got_error *
6348 417a6e49 2020-09-26 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6349 417a6e49 2020-09-26 stsp FILE *patch_file, int n, int nchanges)
6351 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6352 417a6e49 2020-09-26 stsp char *line = NULL;
6353 417a6e49 2020-09-26 stsp size_t linesize = 0;
6354 417a6e49 2020-09-26 stsp ssize_t linelen;
6355 417a6e49 2020-09-26 stsp int resp = ' ';
6356 417a6e49 2020-09-26 stsp struct choose_patch_arg *a = arg;
6358 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NONE;
6360 417a6e49 2020-09-26 stsp if (a->patch_script_file) {
6362 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6363 417a6e49 2020-09-26 stsp a->action);
6365 417a6e49 2020-09-26 stsp return err;
6366 417a6e49 2020-09-26 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6367 417a6e49 2020-09-26 stsp if (linelen == -1) {
6368 417a6e49 2020-09-26 stsp if (ferror(a->patch_script_file))
6369 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6370 417a6e49 2020-09-26 stsp return NULL;
6372 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
6374 417a6e49 2020-09-26 stsp *nl = '\0';
6375 417a6e49 2020-09-26 stsp if (strcmp(line, "y") == 0) {
6376 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6377 417a6e49 2020-09-26 stsp printf("y\n");
6378 417a6e49 2020-09-26 stsp } else if (strcmp(line, "n") == 0) {
6379 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6380 417a6e49 2020-09-26 stsp printf("n\n");
6381 417a6e49 2020-09-26 stsp } else if (strcmp(line, "q") == 0 &&
6382 417a6e49 2020-09-26 stsp status == GOT_STATUS_MODIFY) {
6383 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6384 417a6e49 2020-09-26 stsp printf("q\n");
6386 417a6e49 2020-09-26 stsp printf("invalid response '%s'\n", line);
6387 417a6e49 2020-09-26 stsp free(line);
6388 417a6e49 2020-09-26 stsp return NULL;
6391 417a6e49 2020-09-26 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6392 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6393 417a6e49 2020-09-26 stsp a->action);
6395 417a6e49 2020-09-26 stsp return err;
6396 417a6e49 2020-09-26 stsp resp = getchar();
6397 417a6e49 2020-09-26 stsp if (resp == '\n')
6398 417a6e49 2020-09-26 stsp resp = getchar();
6399 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_MODIFY) {
6400 417a6e49 2020-09-26 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6401 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6402 417a6e49 2020-09-26 stsp resp = ' ';
6404 417a6e49 2020-09-26 stsp } else if (resp != 'y' && resp != 'n') {
6405 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6406 417a6e49 2020-09-26 stsp resp = ' ';
6410 417a6e49 2020-09-26 stsp if (resp == 'y')
6411 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6412 417a6e49 2020-09-26 stsp else if (resp == 'n')
6413 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6414 417a6e49 2020-09-26 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6415 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6417 417a6e49 2020-09-26 stsp return NULL;
6421 417a6e49 2020-09-26 stsp static const struct got_error *
6422 417a6e49 2020-09-26 stsp cmd_revert(int argc, char *argv[])
6424 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6425 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6426 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6427 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
6428 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6429 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6430 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, pflag = 0;
6431 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
6432 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
6433 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
6435 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6437 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6438 417a6e49 2020-09-26 stsp switch (ch) {
6440 417a6e49 2020-09-26 stsp pflag = 1;
6443 417a6e49 2020-09-26 stsp patch_script_path = optarg;
6446 417a6e49 2020-09-26 stsp can_recurse = 1;
6449 417a6e49 2020-09-26 stsp usage_revert();
6450 417a6e49 2020-09-26 stsp /* NOTREACHED */
6454 417a6e49 2020-09-26 stsp argc -= optind;
6455 417a6e49 2020-09-26 stsp argv += optind;
6457 417a6e49 2020-09-26 stsp #ifndef PROFILE
6458 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6459 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6460 417a6e49 2020-09-26 stsp err(1, "pledge");
6462 417a6e49 2020-09-26 stsp if (argc < 1)
6463 417a6e49 2020-09-26 stsp usage_revert();
6464 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
6465 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
6467 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6468 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6469 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6470 417a6e49 2020-09-26 stsp goto done;
6472 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6473 417a6e49 2020-09-26 stsp if (error) {
6474 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6475 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6476 417a6e49 2020-09-26 stsp goto done;
6479 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6481 417a6e49 2020-09-26 stsp if (error != NULL)
6482 417a6e49 2020-09-26 stsp goto done;
6484 417a6e49 2020-09-26 stsp if (patch_script_path) {
6485 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
6486 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
6487 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
6488 417a6e49 2020-09-26 stsp patch_script_path);
6489 417a6e49 2020-09-26 stsp goto done;
6492 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6493 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6494 417a6e49 2020-09-26 stsp if (error)
6495 417a6e49 2020-09-26 stsp goto done;
6497 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6498 417a6e49 2020-09-26 stsp if (error)
6499 417a6e49 2020-09-26 stsp goto done;
6501 417a6e49 2020-09-26 stsp if (!can_recurse) {
6502 417a6e49 2020-09-26 stsp char *ondisk_path;
6503 417a6e49 2020-09-26 stsp struct stat sb;
6504 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6505 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6506 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6507 417a6e49 2020-09-26 stsp pe->path) == -1) {
6508 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6509 417a6e49 2020-09-26 stsp goto done;
6511 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6512 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6513 417a6e49 2020-09-26 stsp free(ondisk_path);
6516 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6517 417a6e49 2020-09-26 stsp ondisk_path);
6518 417a6e49 2020-09-26 stsp free(ondisk_path);
6519 417a6e49 2020-09-26 stsp goto done;
6521 417a6e49 2020-09-26 stsp free(ondisk_path);
6522 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6523 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6524 417a6e49 2020-09-26 stsp "reverting directories requires -R option");
6525 417a6e49 2020-09-26 stsp goto done;
6530 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
6531 417a6e49 2020-09-26 stsp cpa.action = "revert";
6532 417a6e49 2020-09-26 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6533 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa, repo);
6535 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6536 417a6e49 2020-09-26 stsp error == NULL)
6537 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
6539 417a6e49 2020-09-26 stsp got_repo_close(repo);
6540 417a6e49 2020-09-26 stsp if (worktree)
6541 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6542 417a6e49 2020-09-26 stsp free(path);
6543 417a6e49 2020-09-26 stsp free(cwd);
6544 417a6e49 2020-09-26 stsp return error;
6547 417a6e49 2020-09-26 stsp __dead static void
6548 417a6e49 2020-09-26 stsp usage_commit(void)
6550 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6551 417a6e49 2020-09-26 stsp getprogname());
6555 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg {
6556 417a6e49 2020-09-26 stsp const char *cmdline_log;
6557 417a6e49 2020-09-26 stsp const char *editor;
6558 417a6e49 2020-09-26 stsp const char *worktree_path;
6559 417a6e49 2020-09-26 stsp const char *branch_name;
6560 417a6e49 2020-09-26 stsp const char *repo_path;
6561 417a6e49 2020-09-26 stsp char *logmsg_path;
6565 417a6e49 2020-09-26 stsp static const struct got_error *
6566 417a6e49 2020-09-26 stsp collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6567 417a6e49 2020-09-26 stsp void *arg)
6569 417a6e49 2020-09-26 stsp char *initial_content = NULL;
6570 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6571 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6572 417a6e49 2020-09-26 stsp char *template = NULL;
6573 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg *a = arg;
6574 417a6e49 2020-09-26 stsp int initial_content_len;
6575 417a6e49 2020-09-26 stsp int fd = -1;
6576 417a6e49 2020-09-26 stsp size_t len;
6578 417a6e49 2020-09-26 stsp /* if a message was specified on the command line, just use it */
6579 417a6e49 2020-09-26 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6580 417a6e49 2020-09-26 stsp len = strlen(a->cmdline_log) + 1;
6581 417a6e49 2020-09-26 stsp *logmsg = malloc(len + 1);
6582 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
6583 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
6584 417a6e49 2020-09-26 stsp strlcpy(*logmsg, a->cmdline_log, len);
6585 417a6e49 2020-09-26 stsp return NULL;
6588 417a6e49 2020-09-26 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6589 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6591 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
6592 417a6e49 2020-09-26 stsp "\n# changes to be committed on branch %s:\n",
6593 417a6e49 2020-09-26 stsp a->branch_name);
6594 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
6595 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6597 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6599 417a6e49 2020-09-26 stsp goto done;
6601 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6602 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", a->logmsg_path);
6603 417a6e49 2020-09-26 stsp goto done;
6606 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6607 417a6e49 2020-09-26 stsp struct got_commitable *ct = pe->data;
6608 417a6e49 2020-09-26 stsp dprintf(fd, "# %c %s\n",
6609 417a6e49 2020-09-26 stsp got_commitable_get_status(ct),
6610 417a6e49 2020-09-26 stsp got_commitable_get_path(ct));
6613 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6615 417a6e49 2020-09-26 stsp free(initial_content);
6616 417a6e49 2020-09-26 stsp free(template);
6618 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6619 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", a->logmsg_path);
6621 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
6622 417a6e49 2020-09-26 stsp if (err == NULL)
6623 417a6e49 2020-09-26 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6624 417a6e49 2020-09-26 stsp if (err) {
6625 417a6e49 2020-09-26 stsp free(*logmsg);
6626 417a6e49 2020-09-26 stsp *logmsg = NULL;
6628 417a6e49 2020-09-26 stsp return err;
6631 417a6e49 2020-09-26 stsp static const struct got_error *
6632 417a6e49 2020-09-26 stsp cmd_commit(int argc, char *argv[])
6634 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6635 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6636 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6637 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
6638 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
6639 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
6640 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg cl_arg;
6641 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6642 417a6e49 2020-09-26 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6643 417a6e49 2020-09-26 stsp int allow_bad_symlinks = 0;
6644 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6646 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6647 417a6e49 2020-09-26 stsp cl_arg.logmsg_path = NULL;
6649 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6650 417a6e49 2020-09-26 stsp switch (ch) {
6652 417a6e49 2020-09-26 stsp logmsg = optarg;
6655 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
6658 417a6e49 2020-09-26 stsp usage_commit();
6659 417a6e49 2020-09-26 stsp /* NOTREACHED */
6663 417a6e49 2020-09-26 stsp argc -= optind;
6664 417a6e49 2020-09-26 stsp argv += optind;
6666 417a6e49 2020-09-26 stsp #ifndef PROFILE
6667 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6668 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6669 417a6e49 2020-09-26 stsp err(1, "pledge");
6671 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6672 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6673 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6674 417a6e49 2020-09-26 stsp goto done;
6676 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6677 417a6e49 2020-09-26 stsp if (error) {
6678 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6679 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6680 417a6e49 2020-09-26 stsp goto done;
6683 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6684 417a6e49 2020-09-26 stsp if (error)
6685 417a6e49 2020-09-26 stsp goto done;
6686 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
6687 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
6688 417a6e49 2020-09-26 stsp goto done;
6691 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6692 417a6e49 2020-09-26 stsp worktree);
6693 417a6e49 2020-09-26 stsp if (error)
6694 417a6e49 2020-09-26 stsp goto done;
6696 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
6697 417a6e49 2020-09-26 stsp if (error)
6698 417a6e49 2020-09-26 stsp goto done;
6699 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6700 417a6e49 2020-09-26 stsp gitconfig_path);
6701 417a6e49 2020-09-26 stsp if (error != NULL)
6702 417a6e49 2020-09-26 stsp goto done;
6704 417a6e49 2020-09-26 stsp error = get_author(&author, repo, worktree);
6705 417a6e49 2020-09-26 stsp if (error)
6706 417a6e49 2020-09-26 stsp return error;
6709 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
6710 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
6712 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6713 417a6e49 2020-09-26 stsp error = get_editor(&editor);
6715 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6716 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6717 417a6e49 2020-09-26 stsp if (error)
6718 417a6e49 2020-09-26 stsp goto done;
6720 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6721 417a6e49 2020-09-26 stsp if (error)
6722 417a6e49 2020-09-26 stsp goto done;
6724 417a6e49 2020-09-26 stsp cl_arg.editor = editor;
6725 417a6e49 2020-09-26 stsp cl_arg.cmdline_log = logmsg;
6726 417a6e49 2020-09-26 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6727 417a6e49 2020-09-26 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6728 417a6e49 2020-09-26 stsp if (!histedit_in_progress) {
6729 417a6e49 2020-09-26 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6730 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6731 417a6e49 2020-09-26 stsp goto done;
6733 417a6e49 2020-09-26 stsp cl_arg.branch_name += 11;
6735 417a6e49 2020-09-26 stsp cl_arg.repo_path = got_repo_get_path(repo);
6736 417a6e49 2020-09-26 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6737 417a6e49 2020-09-26 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6738 417a6e49 2020-09-26 stsp print_status, NULL, repo);
6739 417a6e49 2020-09-26 stsp if (error) {
6740 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6741 417a6e49 2020-09-26 stsp cl_arg.logmsg_path != NULL)
6742 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
6743 417a6e49 2020-09-26 stsp goto done;
6746 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, id);
6747 417a6e49 2020-09-26 stsp if (error)
6748 417a6e49 2020-09-26 stsp goto done;
6749 417a6e49 2020-09-26 stsp printf("Created commit %s\n", id_str);
6751 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
6752 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6753 417a6e49 2020-09-26 stsp getprogname(), cl_arg.logmsg_path);
6754 417a6e49 2020-09-26 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6755 417a6e49 2020-09-26 stsp error == NULL)
6756 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6757 417a6e49 2020-09-26 stsp free(cl_arg.logmsg_path);
6759 417a6e49 2020-09-26 stsp got_repo_close(repo);
6760 417a6e49 2020-09-26 stsp if (worktree)
6761 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6762 417a6e49 2020-09-26 stsp free(cwd);
6763 417a6e49 2020-09-26 stsp free(id_str);
6764 417a6e49 2020-09-26 stsp free(gitconfig_path);
6765 417a6e49 2020-09-26 stsp free(editor);
6766 417a6e49 2020-09-26 stsp free(author);
6767 417a6e49 2020-09-26 stsp return error;
6770 417a6e49 2020-09-26 stsp __dead static void
6771 417a6e49 2020-09-26 stsp usage_cherrypick(void)
6773 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6777 417a6e49 2020-09-26 stsp static const struct got_error *
6778 417a6e49 2020-09-26 stsp cmd_cherrypick(int argc, char *argv[])
6780 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6781 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6782 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6783 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6784 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6785 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6786 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6787 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6789 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6791 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6792 417a6e49 2020-09-26 stsp switch (ch) {
6794 417a6e49 2020-09-26 stsp usage_cherrypick();
6795 417a6e49 2020-09-26 stsp /* NOTREACHED */
6799 417a6e49 2020-09-26 stsp argc -= optind;
6800 417a6e49 2020-09-26 stsp argv += optind;
6802 417a6e49 2020-09-26 stsp #ifndef PROFILE
6803 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6804 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6805 417a6e49 2020-09-26 stsp err(1, "pledge");
6807 417a6e49 2020-09-26 stsp if (argc != 1)
6808 417a6e49 2020-09-26 stsp usage_cherrypick();
6810 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6811 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6812 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6813 417a6e49 2020-09-26 stsp goto done;
6815 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6816 417a6e49 2020-09-26 stsp if (error) {
6817 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6818 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "cherrypick",
6820 417a6e49 2020-09-26 stsp goto done;
6823 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6825 417a6e49 2020-09-26 stsp if (error != NULL)
6826 417a6e49 2020-09-26 stsp goto done;
6828 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6829 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6830 417a6e49 2020-09-26 stsp if (error)
6831 417a6e49 2020-09-26 stsp goto done;
6833 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6834 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6835 417a6e49 2020-09-26 stsp if (error != NULL) {
6836 417a6e49 2020-09-26 stsp struct got_reference *ref;
6837 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6838 417a6e49 2020-09-26 stsp goto done;
6839 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6840 417a6e49 2020-09-26 stsp if (error != NULL)
6841 417a6e49 2020-09-26 stsp goto done;
6842 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6843 417a6e49 2020-09-26 stsp got_ref_close(ref);
6844 417a6e49 2020-09-26 stsp if (error != NULL)
6845 417a6e49 2020-09-26 stsp goto done;
6847 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6848 417a6e49 2020-09-26 stsp if (error)
6849 417a6e49 2020-09-26 stsp goto done;
6851 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6852 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6853 417a6e49 2020-09-26 stsp if (error != NULL)
6854 417a6e49 2020-09-26 stsp goto done;
6856 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6857 417a6e49 2020-09-26 stsp if (error) {
6858 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
6859 417a6e49 2020-09-26 stsp goto done;
6860 417a6e49 2020-09-26 stsp error = NULL;
6862 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6863 417a6e49 2020-09-26 stsp goto done;
6866 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6867 417a6e49 2020-09-26 stsp if (error)
6868 417a6e49 2020-09-26 stsp goto done;
6869 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6870 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6871 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6872 417a6e49 2020-09-26 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6874 417a6e49 2020-09-26 stsp if (error != NULL)
6875 417a6e49 2020-09-26 stsp goto done;
6877 417a6e49 2020-09-26 stsp if (upa.did_something)
6878 417a6e49 2020-09-26 stsp printf("Merged commit %s\n", commit_id_str);
6879 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6881 417a6e49 2020-09-26 stsp if (commit)
6882 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
6883 417a6e49 2020-09-26 stsp free(commit_id_str);
6884 417a6e49 2020-09-26 stsp if (head_ref)
6885 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
6886 417a6e49 2020-09-26 stsp if (worktree)
6887 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6889 417a6e49 2020-09-26 stsp got_repo_close(repo);
6890 417a6e49 2020-09-26 stsp return error;
6893 417a6e49 2020-09-26 stsp __dead static void
6894 417a6e49 2020-09-26 stsp usage_backout(void)
6896 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6900 417a6e49 2020-09-26 stsp static const struct got_error *
6901 417a6e49 2020-09-26 stsp cmd_backout(int argc, char *argv[])
6903 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6904 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6905 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6906 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6907 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6908 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6909 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6910 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6912 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6914 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6915 417a6e49 2020-09-26 stsp switch (ch) {
6917 417a6e49 2020-09-26 stsp usage_backout();
6918 417a6e49 2020-09-26 stsp /* NOTREACHED */
6922 417a6e49 2020-09-26 stsp argc -= optind;
6923 417a6e49 2020-09-26 stsp argv += optind;
6925 417a6e49 2020-09-26 stsp #ifndef PROFILE
6926 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6927 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6928 417a6e49 2020-09-26 stsp err(1, "pledge");
6930 417a6e49 2020-09-26 stsp if (argc != 1)
6931 417a6e49 2020-09-26 stsp usage_backout();
6933 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6934 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6935 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6936 417a6e49 2020-09-26 stsp goto done;
6938 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6939 417a6e49 2020-09-26 stsp if (error) {
6940 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6941 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6942 417a6e49 2020-09-26 stsp goto done;
6945 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6947 417a6e49 2020-09-26 stsp if (error != NULL)
6948 417a6e49 2020-09-26 stsp goto done;
6950 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6951 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6952 417a6e49 2020-09-26 stsp if (error)
6953 417a6e49 2020-09-26 stsp goto done;
6955 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6956 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6957 417a6e49 2020-09-26 stsp if (error != NULL) {
6958 417a6e49 2020-09-26 stsp struct got_reference *ref;
6959 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6960 417a6e49 2020-09-26 stsp goto done;
6961 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6962 417a6e49 2020-09-26 stsp if (error != NULL)
6963 417a6e49 2020-09-26 stsp goto done;
6964 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6965 417a6e49 2020-09-26 stsp got_ref_close(ref);
6966 417a6e49 2020-09-26 stsp if (error != NULL)
6967 417a6e49 2020-09-26 stsp goto done;
6969 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6970 417a6e49 2020-09-26 stsp if (error)
6971 417a6e49 2020-09-26 stsp goto done;
6973 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6974 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6975 417a6e49 2020-09-26 stsp if (error != NULL)
6976 417a6e49 2020-09-26 stsp goto done;
6978 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6979 417a6e49 2020-09-26 stsp if (error)
6980 417a6e49 2020-09-26 stsp goto done;
6982 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6983 417a6e49 2020-09-26 stsp if (error)
6984 417a6e49 2020-09-26 stsp goto done;
6985 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6986 417a6e49 2020-09-26 stsp if (pid == NULL) {
6987 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6988 417a6e49 2020-09-26 stsp goto done;
6991 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6992 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6993 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
6994 417a6e49 2020-09-26 stsp if (error != NULL)
6995 417a6e49 2020-09-26 stsp goto done;
6997 417a6e49 2020-09-26 stsp if (upa.did_something)
6998 417a6e49 2020-09-26 stsp printf("Backed out commit %s\n", commit_id_str);
6999 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7001 417a6e49 2020-09-26 stsp if (commit)
7002 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7003 417a6e49 2020-09-26 stsp free(commit_id_str);
7004 417a6e49 2020-09-26 stsp if (head_ref)
7005 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
7006 417a6e49 2020-09-26 stsp if (worktree)
7007 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7009 417a6e49 2020-09-26 stsp got_repo_close(repo);
7010 417a6e49 2020-09-26 stsp return error;
7013 417a6e49 2020-09-26 stsp __dead static void
7014 417a6e49 2020-09-26 stsp usage_rebase(void)
7016 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7017 417a6e49 2020-09-26 stsp getprogname());
7022 417a6e49 2020-09-26 stsp trim_logmsg(char *logmsg, int limit)
7025 417a6e49 2020-09-26 stsp size_t len;
7027 417a6e49 2020-09-26 stsp len = strlen(logmsg);
7028 417a6e49 2020-09-26 stsp if (len > limit)
7029 417a6e49 2020-09-26 stsp len = limit;
7030 417a6e49 2020-09-26 stsp logmsg[len] = '\0';
7031 417a6e49 2020-09-26 stsp nl = strchr(logmsg, '\n');
7033 417a6e49 2020-09-26 stsp *nl = '\0';
7036 417a6e49 2020-09-26 stsp static const struct got_error *
7037 417a6e49 2020-09-26 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7039 417a6e49 2020-09-26 stsp const struct got_error *err;
7040 417a6e49 2020-09-26 stsp char *logmsg0 = NULL;
7041 417a6e49 2020-09-26 stsp const char *s;
7043 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7045 417a6e49 2020-09-26 stsp return err;
7047 417a6e49 2020-09-26 stsp s = logmsg0;
7048 417a6e49 2020-09-26 stsp while (isspace((unsigned char)s[0]))
7051 417a6e49 2020-09-26 stsp *logmsg = strdup(s);
7052 417a6e49 2020-09-26 stsp if (*logmsg == NULL) {
7053 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7054 417a6e49 2020-09-26 stsp goto done;
7057 417a6e49 2020-09-26 stsp trim_logmsg(*logmsg, limit);
7059 417a6e49 2020-09-26 stsp free(logmsg0);
7060 417a6e49 2020-09-26 stsp return err;
7063 417a6e49 2020-09-26 stsp static const struct got_error *
7064 417a6e49 2020-09-26 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7066 417a6e49 2020-09-26 stsp const struct got_error *err;
7067 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7068 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
7070 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
7072 417a6e49 2020-09-26 stsp return err;
7074 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
7076 417a6e49 2020-09-26 stsp goto done;
7078 417a6e49 2020-09-26 stsp id_str[12] = '\0';
7080 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7082 417a6e49 2020-09-26 stsp goto done;
7084 417a6e49 2020-09-26 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7086 417a6e49 2020-09-26 stsp free(id_str);
7087 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7088 417a6e49 2020-09-26 stsp free(logmsg);
7089 417a6e49 2020-09-26 stsp return err;
7092 417a6e49 2020-09-26 stsp static const struct got_error *
7093 417a6e49 2020-09-26 stsp show_rebase_progress(struct got_commit_object *commit,
7094 417a6e49 2020-09-26 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7096 417a6e49 2020-09-26 stsp const struct got_error *err;
7097 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7099 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, old_id);
7101 417a6e49 2020-09-26 stsp goto done;
7103 417a6e49 2020-09-26 stsp if (new_id) {
7104 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
7106 417a6e49 2020-09-26 stsp goto done;
7109 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
7110 417a6e49 2020-09-26 stsp if (new_id_str)
7111 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
7113 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7115 417a6e49 2020-09-26 stsp goto done;
7117 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
7118 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7120 417a6e49 2020-09-26 stsp free(old_id_str);
7121 417a6e49 2020-09-26 stsp free(new_id_str);
7122 417a6e49 2020-09-26 stsp free(logmsg);
7123 417a6e49 2020-09-26 stsp return err;
7126 417a6e49 2020-09-26 stsp static const struct got_error *
7127 417a6e49 2020-09-26 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7128 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7129 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7131 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7132 417a6e49 2020-09-26 stsp return got_worktree_rebase_complete(worktree, fileindex,
7133 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, branch, repo);
7136 417a6e49 2020-09-26 stsp static const struct got_error *
7137 417a6e49 2020-09-26 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7138 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7139 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch,
7140 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_repository *repo)
7142 417a6e49 2020-09-26 stsp const struct got_error *error;
7143 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
7144 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
7146 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7147 417a6e49 2020-09-26 stsp if (error)
7148 417a6e49 2020-09-26 stsp return error;
7150 417a6e49 2020-09-26 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7151 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7152 417a6e49 2020-09-26 stsp if (error) {
7153 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7154 417a6e49 2020-09-26 stsp goto done;
7155 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, NULL);
7157 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7158 417a6e49 2020-09-26 stsp free(new_commit_id);
7161 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7162 417a6e49 2020-09-26 stsp return error;
7165 417a6e49 2020-09-26 stsp struct check_path_prefix_arg {
7166 417a6e49 2020-09-26 stsp const char *path_prefix;
7167 417a6e49 2020-09-26 stsp size_t len;
7168 417a6e49 2020-09-26 stsp int errcode;
7171 417a6e49 2020-09-26 stsp static const struct got_error *
7172 417a6e49 2020-09-26 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7173 417a6e49 2020-09-26 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7174 417a6e49 2020-09-26 stsp struct got_object_id *id2, const char *path1, const char *path2,
7175 417a6e49 2020-09-26 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7177 417a6e49 2020-09-26 stsp struct check_path_prefix_arg *a = arg;
7179 417a6e49 2020-09-26 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7180 417a6e49 2020-09-26 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7181 417a6e49 2020-09-26 stsp return got_error(a->errcode);
7183 417a6e49 2020-09-26 stsp return NULL;
7186 417a6e49 2020-09-26 stsp static const struct got_error *
7187 417a6e49 2020-09-26 stsp check_path_prefix(struct got_object_id *parent_id,
7188 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, const char *path_prefix,
7189 417a6e49 2020-09-26 stsp int errcode, struct got_repository *repo)
7191 417a6e49 2020-09-26 stsp const struct got_error *err;
7192 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7193 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7194 417a6e49 2020-09-26 stsp struct check_path_prefix_arg cpp_arg;
7196 417a6e49 2020-09-26 stsp if (got_path_is_root_dir(path_prefix))
7197 417a6e49 2020-09-26 stsp return NULL;
7199 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7201 417a6e49 2020-09-26 stsp goto done;
7203 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7205 417a6e49 2020-09-26 stsp goto done;
7207 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo,
7208 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(parent_commit));
7210 417a6e49 2020-09-26 stsp goto done;
7212 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo,
7213 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(commit));
7215 417a6e49 2020-09-26 stsp goto done;
7217 417a6e49 2020-09-26 stsp cpp_arg.path_prefix = path_prefix;
7218 417a6e49 2020-09-26 stsp while (cpp_arg.path_prefix[0] == '/')
7219 417a6e49 2020-09-26 stsp cpp_arg.path_prefix++;
7220 417a6e49 2020-09-26 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7221 417a6e49 2020-09-26 stsp cpp_arg.errcode = errcode;
7222 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7223 417a6e49 2020-09-26 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7225 417a6e49 2020-09-26 stsp if (tree1)
7226 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
7227 417a6e49 2020-09-26 stsp if (tree2)
7228 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
7229 417a6e49 2020-09-26 stsp if (commit)
7230 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7231 417a6e49 2020-09-26 stsp if (parent_commit)
7232 417a6e49 2020-09-26 stsp got_object_commit_close(parent_commit);
7233 417a6e49 2020-09-26 stsp return err;
7236 417a6e49 2020-09-26 stsp static const struct got_error *
7237 417a6e49 2020-09-26 stsp collect_commits(struct got_object_id_queue *commits,
7238 417a6e49 2020-09-26 stsp struct got_object_id *initial_commit_id,
7239 417a6e49 2020-09-26 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7240 417a6e49 2020-09-26 stsp const char *path_prefix, int path_prefix_errcode,
7241 417a6e49 2020-09-26 stsp struct got_repository *repo)
7243 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7244 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
7245 417a6e49 2020-09-26 stsp struct got_object_id *parent_id = NULL;
7246 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7247 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = initial_commit_id;
7249 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
7251 417a6e49 2020-09-26 stsp return err;
7253 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7254 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7256 417a6e49 2020-09-26 stsp goto done;
7257 417a6e49 2020-09-26 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7258 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7259 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7260 417a6e49 2020-09-26 stsp if (err) {
7261 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7262 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7263 417a6e49 2020-09-26 stsp "ran out of commits to rebase before "
7264 417a6e49 2020-09-26 stsp "youngest common ancestor commit has "
7265 417a6e49 2020-09-26 stsp "been reached?!?");
7267 417a6e49 2020-09-26 stsp goto done;
7269 417a6e49 2020-09-26 stsp err = check_path_prefix(parent_id, commit_id,
7270 417a6e49 2020-09-26 stsp path_prefix, path_prefix_errcode, repo);
7272 417a6e49 2020-09-26 stsp goto done;
7274 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, commit_id);
7276 417a6e49 2020-09-26 stsp goto done;
7277 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7278 417a6e49 2020-09-26 stsp commit_id = parent_id;
7282 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
7283 417a6e49 2020-09-26 stsp return err;
7286 417a6e49 2020-09-26 stsp static const struct got_error *
7287 417a6e49 2020-09-26 stsp cmd_rebase(int argc, char *argv[])
7289 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
7290 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
7291 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
7292 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
7293 417a6e49 2020-09-26 stsp char *cwd = NULL;
7294 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
7295 417a6e49 2020-09-26 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7296 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7297 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
7298 417a6e49 2020-09-26 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7299 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7300 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7301 417a6e49 2020-09-26 stsp int histedit_in_progress = 0;
7302 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7303 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
7304 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
7305 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
7306 417a6e49 2020-09-26 stsp struct got_object_qid *qid, *pid;
7308 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
7309 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
7311 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7312 417a6e49 2020-09-26 stsp switch (ch) {
7314 417a6e49 2020-09-26 stsp abort_rebase = 1;
7317 417a6e49 2020-09-26 stsp continue_rebase = 1;
7320 417a6e49 2020-09-26 stsp usage_rebase();
7321 417a6e49 2020-09-26 stsp /* NOTREACHED */
7325 417a6e49 2020-09-26 stsp argc -= optind;
7326 417a6e49 2020-09-26 stsp argv += optind;
7328 417a6e49 2020-09-26 stsp #ifndef PROFILE
7329 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7330 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
7331 417a6e49 2020-09-26 stsp err(1, "pledge");
7333 417a6e49 2020-09-26 stsp if (abort_rebase && continue_rebase)
7334 417a6e49 2020-09-26 stsp usage_rebase();
7335 417a6e49 2020-09-26 stsp else if (abort_rebase || continue_rebase) {
7336 417a6e49 2020-09-26 stsp if (argc != 0)
7337 417a6e49 2020-09-26 stsp usage_rebase();
7338 417a6e49 2020-09-26 stsp } else if (argc != 1)
7339 417a6e49 2020-09-26 stsp usage_rebase();
7341 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
7342 417a6e49 2020-09-26 stsp if (cwd == NULL) {
7343 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
7344 417a6e49 2020-09-26 stsp goto done;
7346 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
7347 417a6e49 2020-09-26 stsp if (error) {
7348 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7349 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7350 417a6e49 2020-09-26 stsp goto done;
7353 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7355 417a6e49 2020-09-26 stsp if (error != NULL)
7356 417a6e49 2020-09-26 stsp goto done;
7358 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7359 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
7360 417a6e49 2020-09-26 stsp if (error)
7361 417a6e49 2020-09-26 stsp goto done;
7363 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7364 417a6e49 2020-09-26 stsp worktree);
7365 417a6e49 2020-09-26 stsp if (error)
7366 417a6e49 2020-09-26 stsp goto done;
7367 417a6e49 2020-09-26 stsp if (histedit_in_progress) {
7368 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7369 417a6e49 2020-09-26 stsp goto done;
7372 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7373 417a6e49 2020-09-26 stsp if (error)
7374 417a6e49 2020-09-26 stsp goto done;
7376 417a6e49 2020-09-26 stsp if (abort_rebase) {
7377 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7378 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7379 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7380 417a6e49 2020-09-26 stsp goto done;
7382 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7383 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7384 417a6e49 2020-09-26 stsp worktree, repo);
7385 417a6e49 2020-09-26 stsp if (error)
7386 417a6e49 2020-09-26 stsp goto done;
7387 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
7388 417a6e49 2020-09-26 stsp got_ref_get_symref_target(new_base_branch));
7389 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7390 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7391 417a6e49 2020-09-26 stsp new_base_branch, update_progress, &upa);
7392 417a6e49 2020-09-26 stsp if (error)
7393 417a6e49 2020-09-26 stsp goto done;
7394 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7395 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7396 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
7399 417a6e49 2020-09-26 stsp if (continue_rebase) {
7400 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7401 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7402 417a6e49 2020-09-26 stsp goto done;
7404 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7405 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7406 417a6e49 2020-09-26 stsp worktree, repo);
7407 417a6e49 2020-09-26 stsp if (error)
7408 417a6e49 2020-09-26 stsp goto done;
7410 417a6e49 2020-09-26 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7411 417a6e49 2020-09-26 stsp resume_commit_id, repo);
7412 417a6e49 2020-09-26 stsp if (error)
7413 417a6e49 2020-09-26 stsp goto done;
7415 417a6e49 2020-09-26 stsp yca_id = got_object_id_dup(resume_commit_id);
7416 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7417 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_object_id_dup");
7418 417a6e49 2020-09-26 stsp goto done;
7421 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7422 417a6e49 2020-09-26 stsp if (error != NULL)
7423 417a6e49 2020-09-26 stsp goto done;
7426 417a6e49 2020-09-26 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7427 417a6e49 2020-09-26 stsp if (error)
7428 417a6e49 2020-09-26 stsp goto done;
7430 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7431 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id;
7433 417a6e49 2020-09-26 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7434 417a6e49 2020-09-26 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7435 417a6e49 2020-09-26 stsp base_commit_id, branch_head_commit_id, repo,
7436 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7437 417a6e49 2020-09-26 stsp if (error)
7438 417a6e49 2020-09-26 stsp goto done;
7439 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7440 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7441 417a6e49 2020-09-26 stsp "specified branch shares no common ancestry "
7442 417a6e49 2020-09-26 stsp "with work tree's branch");
7443 417a6e49 2020-09-26 stsp goto done;
7446 417a6e49 2020-09-26 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7447 417a6e49 2020-09-26 stsp if (error) {
7448 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
7449 417a6e49 2020-09-26 stsp goto done;
7450 417a6e49 2020-09-26 stsp error = NULL;
7452 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7453 417a6e49 2020-09-26 stsp "specified branch resolves to a commit which "
7454 417a6e49 2020-09-26 stsp "is already contained in work tree's branch");
7455 417a6e49 2020-09-26 stsp goto done;
7457 417a6e49 2020-09-26 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7458 417a6e49 2020-09-26 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7459 417a6e49 2020-09-26 stsp if (error)
7460 417a6e49 2020-09-26 stsp goto done;
7463 417a6e49 2020-09-26 stsp commit_id = branch_head_commit_id;
7464 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7465 417a6e49 2020-09-26 stsp if (error)
7466 417a6e49 2020-09-26 stsp goto done;
7468 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7469 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
7470 417a6e49 2020-09-26 stsp if (pid == NULL) {
7471 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7472 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7473 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7474 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7475 417a6e49 2020-09-26 stsp repo, new_base_branch, update_progress, &upa);
7476 417a6e49 2020-09-26 stsp if (error)
7477 417a6e49 2020-09-26 stsp goto done;
7478 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n",
7479 417a6e49 2020-09-26 stsp got_ref_get_name(branch));
7480 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7483 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7484 417a6e49 2020-09-26 stsp goto done;
7486 417a6e49 2020-09-26 stsp error = collect_commits(&commits, commit_id, pid->id,
7487 417a6e49 2020-09-26 stsp yca_id, got_worktree_get_path_prefix(worktree),
7488 417a6e49 2020-09-26 stsp GOT_ERR_REBASE_PATH, repo);
7489 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7490 417a6e49 2020-09-26 stsp commit = NULL;
7491 417a6e49 2020-09-26 stsp if (error)
7492 417a6e49 2020-09-26 stsp goto done;
7494 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
7495 417a6e49 2020-09-26 stsp if (continue_rebase) {
7496 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex,
7497 417a6e49 2020-09-26 stsp branch, new_base_branch, tmp_branch, repo);
7498 417a6e49 2020-09-26 stsp goto done;
7500 417a6e49 2020-09-26 stsp /* Fast-forward the reference of the branch. */
7501 417a6e49 2020-09-26 stsp struct got_object_id *new_head_commit_id;
7502 417a6e49 2020-09-26 stsp char *id_str;
7503 417a6e49 2020-09-26 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7504 417a6e49 2020-09-26 stsp new_base_branch);
7505 417a6e49 2020-09-26 stsp if (error)
7506 417a6e49 2020-09-26 stsp goto done;
7507 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7508 417a6e49 2020-09-26 stsp printf("Forwarding %s to commit %s\n",
7509 417a6e49 2020-09-26 stsp got_ref_get_name(branch), id_str);
7510 417a6e49 2020-09-26 stsp free(id_str);
7511 417a6e49 2020-09-26 stsp error = got_ref_change_ref(branch,
7512 417a6e49 2020-09-26 stsp new_head_commit_id);
7513 417a6e49 2020-09-26 stsp if (error)
7514 417a6e49 2020-09-26 stsp goto done;
7518 417a6e49 2020-09-26 stsp pid = NULL;
7519 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7520 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7522 417a6e49 2020-09-26 stsp commit_id = qid->id;
7523 417a6e49 2020-09-26 stsp parent_id = pid ? pid->id : yca_id;
7524 417a6e49 2020-09-26 stsp pid = qid;
7526 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7527 417a6e49 2020-09-26 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7528 417a6e49 2020-09-26 stsp worktree, fileindex, parent_id, commit_id, repo,
7529 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
7530 417a6e49 2020-09-26 stsp if (error)
7531 417a6e49 2020-09-26 stsp goto done;
7533 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7534 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
7535 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
7537 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7538 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(qid->id, repo);
7539 417a6e49 2020-09-26 stsp if (error)
7540 417a6e49 2020-09-26 stsp goto done;
7541 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7545 417a6e49 2020-09-26 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7546 417a6e49 2020-09-26 stsp tmp_branch, commit_id, repo);
7547 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7548 417a6e49 2020-09-26 stsp if (error)
7549 417a6e49 2020-09-26 stsp goto done;
7552 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7553 417a6e49 2020-09-26 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7554 417a6e49 2020-09-26 stsp if (error)
7555 417a6e49 2020-09-26 stsp goto done;
7556 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7557 417a6e49 2020-09-26 stsp "conflicts must be resolved before rebasing can continue");
7559 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex, branch,
7560 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, repo);
7562 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
7563 417a6e49 2020-09-26 stsp free(branch_head_commit_id);
7564 417a6e49 2020-09-26 stsp free(resume_commit_id);
7565 417a6e49 2020-09-26 stsp free(yca_id);
7566 417a6e49 2020-09-26 stsp if (commit)
7567 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7568 417a6e49 2020-09-26 stsp if (branch)
7569 417a6e49 2020-09-26 stsp got_ref_close(branch);
7570 417a6e49 2020-09-26 stsp if (new_base_branch)
7571 417a6e49 2020-09-26 stsp got_ref_close(new_base_branch);
7572 417a6e49 2020-09-26 stsp if (tmp_branch)
7573 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
7574 417a6e49 2020-09-26 stsp if (worktree)
7575 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7577 417a6e49 2020-09-26 stsp got_repo_close(repo);
7578 417a6e49 2020-09-26 stsp return error;
7581 417a6e49 2020-09-26 stsp __dead static void
7582 417a6e49 2020-09-26 stsp usage_histedit(void)
7584 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7585 417a6e49 2020-09-26 stsp getprogname());
7589 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_PICK 'p'
7590 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_EDIT 'e'
7591 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_FOLD 'f'
7592 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_DROP 'd'
7593 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_MESG 'm'
7595 417a6e49 2020-09-26 stsp static struct got_histedit_cmd {
7596 417a6e49 2020-09-26 stsp unsigned char code;
7597 417a6e49 2020-09-26 stsp const char *name;
7598 417a6e49 2020-09-26 stsp const char *desc;
7599 417a6e49 2020-09-26 stsp } got_histedit_cmds[] = {
7600 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7601 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7602 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7603 417a6e49 2020-09-26 stsp "be used" },
7604 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7605 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_MESG, "mesg",
7606 417a6e49 2020-09-26 stsp "single-line log message for commit above (open editor if empty)" },
7609 417a6e49 2020-09-26 stsp struct got_histedit_list_entry {
7610 417a6e49 2020-09-26 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7611 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
7612 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7613 417a6e49 2020-09-26 stsp char *logmsg;
7615 417a6e49 2020-09-26 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7617 417a6e49 2020-09-26 stsp static const struct got_error *
7618 417a6e49 2020-09-26 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7619 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7621 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7622 417a6e49 2020-09-26 stsp char *logmsg = NULL, *id_str = NULL;
7623 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7626 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7628 417a6e49 2020-09-26 stsp goto done;
7630 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 34, commit);
7632 417a6e49 2020-09-26 stsp goto done;
7634 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
7636 417a6e49 2020-09-26 stsp goto done;
7638 417a6e49 2020-09-26 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7639 417a6e49 2020-09-26 stsp if (n < 0)
7640 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7642 417a6e49 2020-09-26 stsp if (commit)
7643 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7644 417a6e49 2020-09-26 stsp free(id_str);
7645 417a6e49 2020-09-26 stsp free(logmsg);
7646 417a6e49 2020-09-26 stsp return err;
7649 417a6e49 2020-09-26 stsp static const struct got_error *
7650 417a6e49 2020-09-26 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7651 417a6e49 2020-09-26 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7653 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7654 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7656 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7657 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7659 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7660 417a6e49 2020-09-26 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7664 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
7665 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7666 417a6e49 2020-09-26 stsp if (n < 0) {
7667 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7673 417a6e49 2020-09-26 stsp return err;
7676 417a6e49 2020-09-26 stsp static const struct got_error *
7677 417a6e49 2020-09-26 stsp write_cmd_list(FILE *f, const char *branch_name,
7678 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits)
7680 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7682 417a6e49 2020-09-26 stsp char *id_str;
7683 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7685 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(commits);
7686 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
7688 417a6e49 2020-09-26 stsp return err;
7690 417a6e49 2020-09-26 stsp n = fprintf(f,
7691 417a6e49 2020-09-26 stsp "# Editing the history of branch '%s' starting at\n"
7692 417a6e49 2020-09-26 stsp "# commit %s\n"
7693 417a6e49 2020-09-26 stsp "# Commits will be processed in order from top to "
7694 417a6e49 2020-09-26 stsp "bottom of this file.\n", branch_name, id_str);
7695 417a6e49 2020-09-26 stsp if (n < 0) {
7696 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7697 417a6e49 2020-09-26 stsp goto done;
7700 417a6e49 2020-09-26 stsp n = fprintf(f, "# Available histedit commands:\n");
7701 417a6e49 2020-09-26 stsp if (n < 0) {
7702 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7703 417a6e49 2020-09-26 stsp goto done;
7706 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7707 417a6e49 2020-09-26 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7708 417a6e49 2020-09-26 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7709 417a6e49 2020-09-26 stsp cmd->desc);
7710 417a6e49 2020-09-26 stsp if (n < 0) {
7711 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7716 417a6e49 2020-09-26 stsp free(id_str);
7717 417a6e49 2020-09-26 stsp return err;
7720 417a6e49 2020-09-26 stsp static const struct got_error *
7721 417a6e49 2020-09-26 stsp histedit_syntax_error(int lineno)
7723 417a6e49 2020-09-26 stsp static char msg[42];
7726 417a6e49 2020-09-26 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7728 417a6e49 2020-09-26 stsp if (ret == -1 || ret >= sizeof(msg))
7729 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7731 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7734 417a6e49 2020-09-26 stsp static const struct got_error *
7735 417a6e49 2020-09-26 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7736 417a6e49 2020-09-26 stsp char *logmsg, struct got_repository *repo)
7738 417a6e49 2020-09-26 stsp const struct got_error *err;
7739 417a6e49 2020-09-26 stsp struct got_commit_object *folded_commit = NULL;
7740 417a6e49 2020-09-26 stsp char *id_str, *folded_logmsg = NULL;
7742 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7744 417a6e49 2020-09-26 stsp return err;
7746 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7748 417a6e49 2020-09-26 stsp goto done;
7750 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7752 417a6e49 2020-09-26 stsp goto done;
7753 417a6e49 2020-09-26 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7754 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7755 417a6e49 2020-09-26 stsp folded_logmsg) == -1) {
7756 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7759 417a6e49 2020-09-26 stsp if (folded_commit)
7760 417a6e49 2020-09-26 stsp got_object_commit_close(folded_commit);
7761 417a6e49 2020-09-26 stsp free(id_str);
7762 417a6e49 2020-09-26 stsp free(folded_logmsg);
7763 417a6e49 2020-09-26 stsp return err;
7766 417a6e49 2020-09-26 stsp static struct got_histedit_list_entry *
7767 417a6e49 2020-09-26 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7769 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7771 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7772 417a6e49 2020-09-26 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7773 417a6e49 2020-09-26 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7774 417a6e49 2020-09-26 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7775 417a6e49 2020-09-26 stsp folded = prev;
7776 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7779 417a6e49 2020-09-26 stsp return folded;
7782 417a6e49 2020-09-26 stsp static const struct got_error *
7783 417a6e49 2020-09-26 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7784 417a6e49 2020-09-26 stsp struct got_repository *repo)
7786 417a6e49 2020-09-26 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7787 417a6e49 2020-09-26 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7788 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7789 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7790 417a6e49 2020-09-26 stsp int logmsg_len;
7792 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *folded = NULL;
7794 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7796 417a6e49 2020-09-26 stsp return err;
7798 417a6e49 2020-09-26 stsp folded = get_folded_commits(hle);
7799 417a6e49 2020-09-26 stsp if (folded) {
7800 417a6e49 2020-09-26 stsp while (folded != hle) {
7801 417a6e49 2020-09-26 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7802 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7805 417a6e49 2020-09-26 stsp err = append_folded_commit_msg(&new_msg, folded,
7806 417a6e49 2020-09-26 stsp logmsg, repo);
7808 417a6e49 2020-09-26 stsp goto done;
7809 417a6e49 2020-09-26 stsp free(logmsg);
7810 417a6e49 2020-09-26 stsp logmsg = new_msg;
7811 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7815 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7817 417a6e49 2020-09-26 stsp goto done;
7818 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7820 417a6e49 2020-09-26 stsp goto done;
7821 417a6e49 2020-09-26 stsp logmsg_len = asprintf(&new_msg,
7822 417a6e49 2020-09-26 stsp "%s\n# original log message of commit %s: %s",
7823 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", id_str, orig_logmsg);
7824 417a6e49 2020-09-26 stsp if (logmsg_len == -1) {
7825 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7826 417a6e49 2020-09-26 stsp goto done;
7828 417a6e49 2020-09-26 stsp free(logmsg);
7829 417a6e49 2020-09-26 stsp logmsg = new_msg;
7831 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7833 417a6e49 2020-09-26 stsp goto done;
7835 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7836 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-logmsg");
7838 417a6e49 2020-09-26 stsp goto done;
7840 417a6e49 2020-09-26 stsp write(fd, logmsg, logmsg_len);
7841 417a6e49 2020-09-26 stsp close(fd);
7843 417a6e49 2020-09-26 stsp err = get_editor(&editor);
7845 417a6e49 2020-09-26 stsp goto done;
7847 417a6e49 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7848 417a6e49 2020-09-26 stsp if (err) {
7849 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7850 417a6e49 2020-09-26 stsp goto done;
7851 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7854 417a6e49 2020-09-26 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7855 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", logmsg_path);
7856 417a6e49 2020-09-26 stsp free(logmsg_path);
7857 417a6e49 2020-09-26 stsp free(logmsg);
7858 417a6e49 2020-09-26 stsp free(orig_logmsg);
7859 417a6e49 2020-09-26 stsp free(editor);
7860 417a6e49 2020-09-26 stsp if (commit)
7861 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7862 417a6e49 2020-09-26 stsp return err;
7865 417a6e49 2020-09-26 stsp static const struct got_error *
7866 417a6e49 2020-09-26 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7867 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7869 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7870 417a6e49 2020-09-26 stsp char *line = NULL, *p, *end;
7871 417a6e49 2020-09-26 stsp size_t size;
7872 417a6e49 2020-09-26 stsp ssize_t len;
7873 417a6e49 2020-09-26 stsp int lineno = 0, i;
7874 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7875 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
7876 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle = NULL;
7878 417a6e49 2020-09-26 stsp for (;;) {
7879 417a6e49 2020-09-26 stsp len = getline(&line, &size, f);
7880 417a6e49 2020-09-26 stsp if (len == -1) {
7881 417a6e49 2020-09-26 stsp const struct got_error *getline_err;
7882 417a6e49 2020-09-26 stsp if (feof(f))
7884 417a6e49 2020-09-26 stsp getline_err = got_error_from_errno("getline");
7885 417a6e49 2020-09-26 stsp err = got_ferror(f, getline_err->code);
7890 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7892 417a6e49 2020-09-26 stsp if (p[0] == '#' || p[0] == '\0') {
7893 417a6e49 2020-09-26 stsp free(line);
7894 417a6e49 2020-09-26 stsp line = NULL;
7897 417a6e49 2020-09-26 stsp cmd = NULL;
7898 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7899 417a6e49 2020-09-26 stsp cmd = &got_histedit_cmds[i];
7900 417a6e49 2020-09-26 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7901 417a6e49 2020-09-26 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7902 417a6e49 2020-09-26 stsp p += strlen(cmd->name);
7905 417a6e49 2020-09-26 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7910 417a6e49 2020-09-26 stsp if (i == nitems(got_histedit_cmds)) {
7911 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7914 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7916 417a6e49 2020-09-26 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7917 417a6e49 2020-09-26 stsp if (hle == NULL || hle->logmsg != NULL) {
7918 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7921 417a6e49 2020-09-26 stsp if (p[0] == '\0') {
7922 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
7926 417a6e49 2020-09-26 stsp hle->logmsg = strdup(p);
7927 417a6e49 2020-09-26 stsp if (hle->logmsg == NULL) {
7928 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7932 417a6e49 2020-09-26 stsp free(line);
7933 417a6e49 2020-09-26 stsp line = NULL;
7937 417a6e49 2020-09-26 stsp while (end[0] && !isspace((unsigned char)end[0]))
7939 417a6e49 2020-09-26 stsp *end = '\0';
7941 417a6e49 2020-09-26 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7942 417a6e49 2020-09-26 stsp if (err) {
7943 417a6e49 2020-09-26 stsp /* override error code */
7944 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7948 417a6e49 2020-09-26 stsp hle = malloc(sizeof(*hle));
7949 417a6e49 2020-09-26 stsp if (hle == NULL) {
7950 417a6e49 2020-09-26 stsp err = got_error_from_errno("malloc");
7953 417a6e49 2020-09-26 stsp hle->cmd = cmd;
7954 417a6e49 2020-09-26 stsp hle->commit_id = commit_id;
7955 417a6e49 2020-09-26 stsp hle->logmsg = NULL;
7956 417a6e49 2020-09-26 stsp commit_id = NULL;
7957 417a6e49 2020-09-26 stsp free(line);
7958 417a6e49 2020-09-26 stsp line = NULL;
7959 417a6e49 2020-09-26 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7962 417a6e49 2020-09-26 stsp free(line);
7963 417a6e49 2020-09-26 stsp free(commit_id);
7964 417a6e49 2020-09-26 stsp return err;
7967 417a6e49 2020-09-26 stsp static const struct got_error *
7968 417a6e49 2020-09-26 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7969 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7971 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7972 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7973 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
7974 417a6e49 2020-09-26 stsp static char msg[92];
7975 417a6e49 2020-09-26 stsp char *id_str;
7977 417a6e49 2020-09-26 stsp if (TAILQ_EMPTY(histedit_cmds))
7978 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7979 417a6e49 2020-09-26 stsp "histedit script contains no commands");
7980 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7981 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7983 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7984 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle2;
7985 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7986 417a6e49 2020-09-26 stsp if (hle == hle2)
7988 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
7989 417a6e49 2020-09-26 stsp hle2->commit_id) != 0)
7991 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7993 417a6e49 2020-09-26 stsp return err;
7994 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7995 417a6e49 2020-09-26 stsp "more than once in histedit script", id_str);
7996 417a6e49 2020-09-26 stsp free(id_str);
7997 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8001 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8002 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8003 417a6e49 2020-09-26 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8006 417a6e49 2020-09-26 stsp if (hle == NULL) {
8007 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
8009 417a6e49 2020-09-26 stsp return err;
8010 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
8011 417a6e49 2020-09-26 stsp "commit %s missing from histedit script", id_str);
8012 417a6e49 2020-09-26 stsp free(id_str);
8013 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8017 417a6e49 2020-09-26 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8018 417a6e49 2020-09-26 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8019 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8020 417a6e49 2020-09-26 stsp "last commit in histedit script cannot be folded");
8022 417a6e49 2020-09-26 stsp return NULL;
8025 417a6e49 2020-09-26 stsp static const struct got_error *
8026 417a6e49 2020-09-26 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8027 417a6e49 2020-09-26 stsp const char *path, struct got_object_id_queue *commits,
8028 417a6e49 2020-09-26 stsp struct got_repository *repo)
8030 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8031 417a6e49 2020-09-26 stsp char *editor;
8032 417a6e49 2020-09-26 stsp FILE *f = NULL;
8034 417a6e49 2020-09-26 stsp err = get_editor(&editor);
8036 417a6e49 2020-09-26 stsp return err;
8038 417a6e49 2020-09-26 stsp if (spawn_editor(editor, path) == -1) {
8039 417a6e49 2020-09-26 stsp err = got_error_from_errno("failed spawning editor");
8040 417a6e49 2020-09-26 stsp goto done;
8043 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8044 417a6e49 2020-09-26 stsp if (f == NULL) {
8045 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
8046 417a6e49 2020-09-26 stsp goto done;
8048 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8050 417a6e49 2020-09-26 stsp goto done;
8052 417a6e49 2020-09-26 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8054 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8055 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8056 417a6e49 2020-09-26 stsp free(editor);
8057 417a6e49 2020-09-26 stsp return err;
8060 417a6e49 2020-09-26 stsp static const struct got_error *
8061 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8062 417a6e49 2020-09-26 stsp struct got_object_id_queue *, const char *, const char *,
8063 417a6e49 2020-09-26 stsp struct got_repository *);
8065 417a6e49 2020-09-26 stsp static const struct got_error *
8066 417a6e49 2020-09-26 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8067 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, const char *branch_name,
8068 417a6e49 2020-09-26 stsp int edit_logmsg_only, struct got_repository *repo)
8070 417a6e49 2020-09-26 stsp const struct got_error *err;
8071 417a6e49 2020-09-26 stsp FILE *f = NULL;
8072 417a6e49 2020-09-26 stsp char *path = NULL;
8074 417a6e49 2020-09-26 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8076 417a6e49 2020-09-26 stsp return err;
8078 417a6e49 2020-09-26 stsp err = write_cmd_list(f, branch_name, commits);
8080 417a6e49 2020-09-26 stsp goto done;
8082 417a6e49 2020-09-26 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8084 417a6e49 2020-09-26 stsp goto done;
8086 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
8087 417a6e49 2020-09-26 stsp rewind(f);
8088 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8090 417a6e49 2020-09-26 stsp if (fclose(f) != 0) {
8091 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8092 417a6e49 2020-09-26 stsp goto done;
8095 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8096 417a6e49 2020-09-26 stsp if (err) {
8097 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8098 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8099 417a6e49 2020-09-26 stsp goto done;
8100 417a6e49 2020-09-26 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8101 417a6e49 2020-09-26 stsp commits, path, branch_name, repo);
8105 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8106 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8107 417a6e49 2020-09-26 stsp if (path && unlink(path) != 0 && err == NULL)
8108 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", path);
8109 417a6e49 2020-09-26 stsp free(path);
8110 417a6e49 2020-09-26 stsp return err;
8113 417a6e49 2020-09-26 stsp static const struct got_error *
8114 417a6e49 2020-09-26 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8115 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8117 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8118 417a6e49 2020-09-26 stsp char *path = NULL;
8119 417a6e49 2020-09-26 stsp FILE *f = NULL;
8120 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8121 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8123 417a6e49 2020-09-26 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8125 417a6e49 2020-09-26 stsp return err;
8127 417a6e49 2020-09-26 stsp f = fopen(path, "w");
8128 417a6e49 2020-09-26 stsp if (f == NULL) {
8129 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8130 417a6e49 2020-09-26 stsp goto done;
8132 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8133 417a6e49 2020-09-26 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8138 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8139 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c %s\n",
8140 417a6e49 2020-09-26 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8141 417a6e49 2020-09-26 stsp if (n < 0) {
8142 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
8148 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8149 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8150 417a6e49 2020-09-26 stsp free(path);
8151 417a6e49 2020-09-26 stsp if (commit)
8152 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8153 417a6e49 2020-09-26 stsp return err;
8157 417a6e49 2020-09-26 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8159 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8161 417a6e49 2020-09-26 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8162 417a6e49 2020-09-26 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8163 417a6e49 2020-09-26 stsp free(hle);
8167 417a6e49 2020-09-26 stsp static const struct got_error *
8168 417a6e49 2020-09-26 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8169 417a6e49 2020-09-26 stsp const char *path, struct got_repository *repo)
8171 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8172 417a6e49 2020-09-26 stsp FILE *f = NULL;
8174 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8175 417a6e49 2020-09-26 stsp if (f == NULL) {
8176 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8177 417a6e49 2020-09-26 stsp goto done;
8180 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8182 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8183 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8184 417a6e49 2020-09-26 stsp return err;
8187 417a6e49 2020-09-26 stsp static const struct got_error *
8188 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8189 417a6e49 2020-09-26 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8190 417a6e49 2020-09-26 stsp const char *path, const char *branch_name, struct got_repository *repo)
8192 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8193 417a6e49 2020-09-26 stsp int resp = ' ';
8195 417a6e49 2020-09-26 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8196 417a6e49 2020-09-26 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8197 417a6e49 2020-09-26 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8198 417a6e49 2020-09-26 stsp resp = getchar();
8199 417a6e49 2020-09-26 stsp if (resp == '\n')
8200 417a6e49 2020-09-26 stsp resp = getchar();
8201 417a6e49 2020-09-26 stsp if (resp == 'c') {
8202 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8203 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8205 417a6e49 2020-09-26 stsp if (err) {
8206 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8207 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8209 417a6e49 2020-09-26 stsp prev_err = err;
8210 417a6e49 2020-09-26 stsp resp = ' ';
8214 417a6e49 2020-09-26 stsp } else if (resp == 'r') {
8215 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8216 417a6e49 2020-09-26 stsp err = histedit_edit_script(histedit_cmds,
8217 417a6e49 2020-09-26 stsp commits, branch_name, 0, repo);
8218 417a6e49 2020-09-26 stsp if (err) {
8219 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8220 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8222 417a6e49 2020-09-26 stsp prev_err = err;
8223 417a6e49 2020-09-26 stsp resp = ' ';
8227 417a6e49 2020-09-26 stsp } else if (resp == 'a') {
8228 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8231 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
8234 417a6e49 2020-09-26 stsp return err;
8237 417a6e49 2020-09-26 stsp static const struct got_error *
8238 417a6e49 2020-09-26 stsp histedit_complete(struct got_worktree *worktree,
8239 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8240 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_repository *repo)
8242 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8243 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8244 417a6e49 2020-09-26 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8245 417a6e49 2020-09-26 stsp branch, repo);
8248 417a6e49 2020-09-26 stsp static const struct got_error *
8249 417a6e49 2020-09-26 stsp show_histedit_progress(struct got_commit_object *commit,
8250 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8252 417a6e49 2020-09-26 stsp const struct got_error *err;
8253 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8255 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8257 417a6e49 2020-09-26 stsp goto done;
8259 417a6e49 2020-09-26 stsp if (new_id) {
8260 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
8262 417a6e49 2020-09-26 stsp goto done;
8265 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
8266 417a6e49 2020-09-26 stsp if (new_id_str)
8267 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
8269 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8270 417a6e49 2020-09-26 stsp logmsg = strdup(hle->logmsg);
8271 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
8272 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
8273 417a6e49 2020-09-26 stsp goto done;
8275 417a6e49 2020-09-26 stsp trim_logmsg(logmsg, 42);
8277 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
8279 417a6e49 2020-09-26 stsp goto done;
8282 417a6e49 2020-09-26 stsp switch (hle->cmd->code) {
8283 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_PICK:
8284 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_EDIT:
8285 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
8286 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8288 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_DROP:
8289 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_FOLD:
8290 417a6e49 2020-09-26 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8297 417a6e49 2020-09-26 stsp free(old_id_str);
8298 417a6e49 2020-09-26 stsp free(new_id_str);
8299 417a6e49 2020-09-26 stsp return err;
8302 417a6e49 2020-09-26 stsp static const struct got_error *
8303 417a6e49 2020-09-26 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8304 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8305 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8306 417a6e49 2020-09-26 stsp struct got_repository *repo)
8308 417a6e49 2020-09-26 stsp const struct got_error *err;
8309 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8310 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
8312 417a6e49 2020-09-26 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8313 417a6e49 2020-09-26 stsp && hle->logmsg == NULL) {
8314 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
8316 417a6e49 2020-09-26 stsp return err;
8319 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8321 417a6e49 2020-09-26 stsp return err;
8323 417a6e49 2020-09-26 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8324 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8325 417a6e49 2020-09-26 stsp hle->logmsg, repo);
8326 417a6e49 2020-09-26 stsp if (err) {
8327 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8328 417a6e49 2020-09-26 stsp goto done;
8329 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, NULL);
8331 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8332 417a6e49 2020-09-26 stsp free(new_commit_id);
8335 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8336 417a6e49 2020-09-26 stsp return err;
8339 417a6e49 2020-09-26 stsp static const struct got_error *
8340 417a6e49 2020-09-26 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8341 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8343 417a6e49 2020-09-26 stsp const struct got_error *error;
8344 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8346 417a6e49 2020-09-26 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8348 417a6e49 2020-09-26 stsp if (error)
8349 417a6e49 2020-09-26 stsp return error;
8351 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8352 417a6e49 2020-09-26 stsp if (error)
8353 417a6e49 2020-09-26 stsp return error;
8355 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit, hle, NULL);
8356 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8357 417a6e49 2020-09-26 stsp return error;
8360 417a6e49 2020-09-26 stsp static const struct got_error *
8361 417a6e49 2020-09-26 stsp check_local_changes(void *arg, unsigned char status,
8362 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path,
8363 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8364 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8366 417a6e49 2020-09-26 stsp int *have_local_changes = arg;
8368 417a6e49 2020-09-26 stsp switch (status) {
8369 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8370 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8371 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8372 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
8373 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8374 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8379 417a6e49 2020-09-26 stsp switch (staged_status) {
8380 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8381 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8382 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8383 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8384 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8389 417a6e49 2020-09-26 stsp return NULL;
8392 417a6e49 2020-09-26 stsp static const struct got_error *
8393 417a6e49 2020-09-26 stsp cmd_histedit(int argc, char *argv[])
8395 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8396 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8397 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8398 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8399 417a6e49 2020-09-26 stsp char *cwd = NULL;
8400 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
8401 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch = NULL;
8402 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
8403 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id = NULL;
8404 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
8405 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8406 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0;
8407 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8408 417a6e49 2020-09-26 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8409 417a6e49 2020-09-26 stsp int edit_logmsg_only = 0;
8410 417a6e49 2020-09-26 stsp const char *edit_script_path = NULL;
8411 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8412 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
8413 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
8414 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
8415 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
8416 417a6e49 2020-09-26 stsp struct got_histedit_list histedit_cmds;
8417 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8419 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
8420 417a6e49 2020-09-26 stsp TAILQ_INIT(&histedit_cmds);
8421 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
8422 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8424 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8425 417a6e49 2020-09-26 stsp switch (ch) {
8427 417a6e49 2020-09-26 stsp abort_edit = 1;
8430 417a6e49 2020-09-26 stsp continue_edit = 1;
8433 417a6e49 2020-09-26 stsp edit_script_path = optarg;
8436 417a6e49 2020-09-26 stsp edit_logmsg_only = 1;
8439 417a6e49 2020-09-26 stsp usage_histedit();
8440 417a6e49 2020-09-26 stsp /* NOTREACHED */
8444 417a6e49 2020-09-26 stsp argc -= optind;
8445 417a6e49 2020-09-26 stsp argv += optind;
8447 417a6e49 2020-09-26 stsp #ifndef PROFILE
8448 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8449 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8450 417a6e49 2020-09-26 stsp err(1, "pledge");
8452 417a6e49 2020-09-26 stsp if (abort_edit && continue_edit)
8453 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8454 417a6e49 2020-09-26 stsp if (edit_script_path && edit_logmsg_only)
8455 417a6e49 2020-09-26 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8456 417a6e49 2020-09-26 stsp if (abort_edit && edit_logmsg_only)
8457 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8458 417a6e49 2020-09-26 stsp if (continue_edit && edit_logmsg_only)
8459 417a6e49 2020-09-26 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8460 417a6e49 2020-09-26 stsp if (argc != 0)
8461 417a6e49 2020-09-26 stsp usage_histedit();
8464 417a6e49 2020-09-26 stsp * This command cannot apply unveil(2) in all cases because the
8465 417a6e49 2020-09-26 stsp * user may choose to run an editor to edit the histedit script
8466 417a6e49 2020-09-26 stsp * and to edit individual commit log messages.
8467 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8468 417a6e49 2020-09-26 stsp * apply unveil after edit script and log messages have been written.
8469 417a6e49 2020-09-26 stsp * XXX TODO: Make use of unveil(2) where possible.
8472 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8473 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8474 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8475 417a6e49 2020-09-26 stsp goto done;
8477 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8478 417a6e49 2020-09-26 stsp if (error) {
8479 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8480 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8481 417a6e49 2020-09-26 stsp goto done;
8484 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8486 417a6e49 2020-09-26 stsp if (error != NULL)
8487 417a6e49 2020-09-26 stsp goto done;
8489 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8490 417a6e49 2020-09-26 stsp if (error)
8491 417a6e49 2020-09-26 stsp goto done;
8492 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
8493 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
8494 417a6e49 2020-09-26 stsp goto done;
8497 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8498 417a6e49 2020-09-26 stsp if (error)
8499 417a6e49 2020-09-26 stsp goto done;
8501 417a6e49 2020-09-26 stsp if (edit_in_progress && edit_logmsg_only) {
8502 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8503 417a6e49 2020-09-26 stsp "histedit operation is in progress in this "
8504 417a6e49 2020-09-26 stsp "work tree and must be continued or aborted "
8505 417a6e49 2020-09-26 stsp "before the -m option can be used");
8506 417a6e49 2020-09-26 stsp goto done;
8509 417a6e49 2020-09-26 stsp if (edit_in_progress && abort_edit) {
8510 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8511 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8512 417a6e49 2020-09-26 stsp worktree, repo);
8513 417a6e49 2020-09-26 stsp if (error)
8514 417a6e49 2020-09-26 stsp goto done;
8515 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8516 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8517 417a6e49 2020-09-26 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8518 417a6e49 2020-09-26 stsp branch, base_commit_id, update_progress, &upa);
8519 417a6e49 2020-09-26 stsp if (error)
8520 417a6e49 2020-09-26 stsp goto done;
8521 417a6e49 2020-09-26 stsp printf("Histedit of %s aborted\n",
8522 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8523 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8524 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
8525 417a6e49 2020-09-26 stsp } else if (abort_edit) {
8526 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8527 417a6e49 2020-09-26 stsp goto done;
8530 417a6e49 2020-09-26 stsp if (continue_edit) {
8531 417a6e49 2020-09-26 stsp char *path;
8533 417a6e49 2020-09-26 stsp if (!edit_in_progress) {
8534 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8535 417a6e49 2020-09-26 stsp goto done;
8538 417a6e49 2020-09-26 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8539 417a6e49 2020-09-26 stsp if (error)
8540 417a6e49 2020-09-26 stsp goto done;
8542 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8543 417a6e49 2020-09-26 stsp free(path);
8544 417a6e49 2020-09-26 stsp if (error)
8545 417a6e49 2020-09-26 stsp goto done;
8547 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8548 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8549 417a6e49 2020-09-26 stsp worktree, repo);
8550 417a6e49 2020-09-26 stsp if (error)
8551 417a6e49 2020-09-26 stsp goto done;
8553 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8554 417a6e49 2020-09-26 stsp if (error)
8555 417a6e49 2020-09-26 stsp goto done;
8557 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8558 417a6e49 2020-09-26 stsp head_commit_id);
8559 417a6e49 2020-09-26 stsp if (error)
8560 417a6e49 2020-09-26 stsp goto done;
8561 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8562 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8563 417a6e49 2020-09-26 stsp if (pid == NULL) {
8564 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8565 417a6e49 2020-09-26 stsp goto done;
8567 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8568 417a6e49 2020-09-26 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8569 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8570 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8571 417a6e49 2020-09-26 stsp commit = NULL;
8572 417a6e49 2020-09-26 stsp if (error)
8573 417a6e49 2020-09-26 stsp goto done;
8575 417a6e49 2020-09-26 stsp if (edit_in_progress) {
8576 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8577 417a6e49 2020-09-26 stsp goto done;
8580 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo,
8581 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8582 417a6e49 2020-09-26 stsp if (error != NULL)
8583 417a6e49 2020-09-26 stsp goto done;
8585 417a6e49 2020-09-26 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8586 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8587 417a6e49 2020-09-26 stsp "will not edit commit history of a branch outside "
8588 417a6e49 2020-09-26 stsp "the \"refs/heads/\" reference namespace");
8589 417a6e49 2020-09-26 stsp goto done;
8592 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8593 417a6e49 2020-09-26 stsp got_ref_close(branch);
8594 417a6e49 2020-09-26 stsp branch = NULL;
8595 417a6e49 2020-09-26 stsp if (error)
8596 417a6e49 2020-09-26 stsp goto done;
8598 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8599 417a6e49 2020-09-26 stsp head_commit_id);
8600 417a6e49 2020-09-26 stsp if (error)
8601 417a6e49 2020-09-26 stsp goto done;
8602 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8603 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8604 417a6e49 2020-09-26 stsp if (pid == NULL) {
8605 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8606 417a6e49 2020-09-26 stsp goto done;
8608 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8609 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree),
8610 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
8611 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8612 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8613 417a6e49 2020-09-26 stsp commit = NULL;
8614 417a6e49 2020-09-26 stsp if (error)
8615 417a6e49 2020-09-26 stsp goto done;
8617 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
8618 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8619 417a6e49 2020-09-26 stsp goto done;
8622 417a6e49 2020-09-26 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8623 417a6e49 2020-09-26 stsp &base_commit_id, &fileindex, worktree, repo);
8624 417a6e49 2020-09-26 stsp if (error)
8625 417a6e49 2020-09-26 stsp goto done;
8627 417a6e49 2020-09-26 stsp if (edit_script_path) {
8628 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds,
8629 417a6e49 2020-09-26 stsp edit_script_path, repo);
8630 417a6e49 2020-09-26 stsp if (error) {
8631 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8632 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8633 417a6e49 2020-09-26 stsp update_progress, &upa);
8634 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8635 417a6e49 2020-09-26 stsp goto done;
8638 417a6e49 2020-09-26 stsp const char *branch_name;
8639 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(branch);
8640 417a6e49 2020-09-26 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8641 417a6e49 2020-09-26 stsp branch_name += 11;
8642 417a6e49 2020-09-26 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8643 417a6e49 2020-09-26 stsp branch_name, edit_logmsg_only, repo);
8644 417a6e49 2020-09-26 stsp if (error) {
8645 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8646 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8647 417a6e49 2020-09-26 stsp update_progress, &upa);
8648 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8649 417a6e49 2020-09-26 stsp goto done;
8654 417a6e49 2020-09-26 stsp error = histedit_save_list(&histedit_cmds, worktree,
8656 417a6e49 2020-09-26 stsp if (error) {
8657 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8658 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8659 417a6e49 2020-09-26 stsp update_progress, &upa);
8660 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8661 417a6e49 2020-09-26 stsp goto done;
8666 417a6e49 2020-09-26 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
8667 417a6e49 2020-09-26 stsp if (error)
8668 417a6e49 2020-09-26 stsp goto done;
8670 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8671 417a6e49 2020-09-26 stsp if (resume_commit_id) {
8672 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
8673 417a6e49 2020-09-26 stsp resume_commit_id) != 0)
8676 417a6e49 2020-09-26 stsp resume_commit_id = NULL;
8677 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8678 417a6e49 2020-09-26 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8679 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree,
8681 417a6e49 2020-09-26 stsp if (error)
8682 417a6e49 2020-09-26 stsp goto done;
8684 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8685 417a6e49 2020-09-26 stsp int have_changes = 0;
8687 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
8688 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
8689 417a6e49 2020-09-26 stsp if (error)
8690 417a6e49 2020-09-26 stsp goto done;
8691 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths,
8692 417a6e49 2020-09-26 stsp repo, check_local_changes, &have_changes,
8693 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8694 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
8695 417a6e49 2020-09-26 stsp if (error) {
8696 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_CANCELLED)
8697 417a6e49 2020-09-26 stsp goto done;
8698 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
8699 417a6e49 2020-09-26 stsp goto done;
8701 417a6e49 2020-09-26 stsp if (have_changes) {
8702 417a6e49 2020-09-26 stsp error = histedit_commit(NULL, worktree,
8703 417a6e49 2020-09-26 stsp fileindex, tmp_branch, hle, repo);
8704 417a6e49 2020-09-26 stsp if (error)
8705 417a6e49 2020-09-26 stsp goto done;
8707 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(
8708 417a6e49 2020-09-26 stsp &commit, repo, hle->commit_id);
8709 417a6e49 2020-09-26 stsp if (error)
8710 417a6e49 2020-09-26 stsp goto done;
8711 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit,
8712 417a6e49 2020-09-26 stsp hle, NULL);
8713 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8714 417a6e49 2020-09-26 stsp commit = NULL;
8715 417a6e49 2020-09-26 stsp if (error)
8716 417a6e49 2020-09-26 stsp goto done;
8722 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8723 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8724 417a6e49 2020-09-26 stsp if (error)
8725 417a6e49 2020-09-26 stsp goto done;
8729 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8730 417a6e49 2020-09-26 stsp hle->commit_id);
8731 417a6e49 2020-09-26 stsp if (error)
8732 417a6e49 2020-09-26 stsp goto done;
8733 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8734 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8736 417a6e49 2020-09-26 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8737 417a6e49 2020-09-26 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8738 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
8739 417a6e49 2020-09-26 stsp if (error)
8740 417a6e49 2020-09-26 stsp goto done;
8741 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8742 417a6e49 2020-09-26 stsp commit = NULL;
8744 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8745 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
8746 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
8748 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8749 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(hle->commit_id,
8751 417a6e49 2020-09-26 stsp if (error)
8752 417a6e49 2020-09-26 stsp goto done;
8753 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8757 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8758 417a6e49 2020-09-26 stsp char *id_str;
8759 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, hle->commit_id);
8760 417a6e49 2020-09-26 stsp if (error)
8761 417a6e49 2020-09-26 stsp goto done;
8762 417a6e49 2020-09-26 stsp printf("Stopping histedit for amending commit %s\n",
8764 417a6e49 2020-09-26 stsp free(id_str);
8765 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8766 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree,
8767 417a6e49 2020-09-26 stsp fileindex);
8768 417a6e49 2020-09-26 stsp goto done;
8771 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8772 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8773 417a6e49 2020-09-26 stsp if (error)
8774 417a6e49 2020-09-26 stsp goto done;
8778 417a6e49 2020-09-26 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8779 417a6e49 2020-09-26 stsp tmp_branch, hle, repo);
8780 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8781 417a6e49 2020-09-26 stsp if (error)
8782 417a6e49 2020-09-26 stsp goto done;
8785 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8786 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8787 417a6e49 2020-09-26 stsp if (error)
8788 417a6e49 2020-09-26 stsp goto done;
8789 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8790 417a6e49 2020-09-26 stsp "conflicts must be resolved before histedit can continue");
8792 417a6e49 2020-09-26 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8793 417a6e49 2020-09-26 stsp branch, repo);
8795 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
8796 417a6e49 2020-09-26 stsp histedit_free_list(&histedit_cmds);
8797 417a6e49 2020-09-26 stsp free(head_commit_id);
8798 417a6e49 2020-09-26 stsp free(base_commit_id);
8799 417a6e49 2020-09-26 stsp free(resume_commit_id);
8800 417a6e49 2020-09-26 stsp if (commit)
8801 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8802 417a6e49 2020-09-26 stsp if (branch)
8803 417a6e49 2020-09-26 stsp got_ref_close(branch);
8804 417a6e49 2020-09-26 stsp if (tmp_branch)
8805 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
8806 417a6e49 2020-09-26 stsp if (worktree)
8807 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8809 417a6e49 2020-09-26 stsp got_repo_close(repo);
8810 417a6e49 2020-09-26 stsp return error;
8813 417a6e49 2020-09-26 stsp __dead static void
8814 417a6e49 2020-09-26 stsp usage_integrate(void)
8816 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8820 417a6e49 2020-09-26 stsp static const struct got_error *
8821 417a6e49 2020-09-26 stsp cmd_integrate(int argc, char *argv[])
8823 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8824 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8825 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8826 417a6e49 2020-09-26 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8827 417a6e49 2020-09-26 stsp const char *branch_arg = NULL;
8828 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8829 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8830 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8832 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8834 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8835 417a6e49 2020-09-26 stsp switch (ch) {
8837 417a6e49 2020-09-26 stsp usage_integrate();
8838 417a6e49 2020-09-26 stsp /* NOTREACHED */
8842 417a6e49 2020-09-26 stsp argc -= optind;
8843 417a6e49 2020-09-26 stsp argv += optind;
8845 417a6e49 2020-09-26 stsp if (argc != 1)
8846 417a6e49 2020-09-26 stsp usage_integrate();
8847 417a6e49 2020-09-26 stsp branch_arg = argv[0];
8849 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8850 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8851 417a6e49 2020-09-26 stsp err(1, "pledge");
8853 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8854 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8855 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8856 417a6e49 2020-09-26 stsp goto done;
8859 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8860 417a6e49 2020-09-26 stsp if (error) {
8861 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8862 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "integrate",
8864 417a6e49 2020-09-26 stsp goto done;
8867 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
8868 417a6e49 2020-09-26 stsp if (error)
8869 417a6e49 2020-09-26 stsp goto done;
8871 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8873 417a6e49 2020-09-26 stsp if (error != NULL)
8874 417a6e49 2020-09-26 stsp goto done;
8876 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8877 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
8878 417a6e49 2020-09-26 stsp if (error)
8879 417a6e49 2020-09-26 stsp goto done;
8881 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8882 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
8883 417a6e49 2020-09-26 stsp goto done;
8886 417a6e49 2020-09-26 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8887 417a6e49 2020-09-26 stsp &base_branch_ref, worktree, refname, repo);
8888 417a6e49 2020-09-26 stsp if (error)
8889 417a6e49 2020-09-26 stsp goto done;
8891 417a6e49 2020-09-26 stsp refname = strdup(got_ref_get_name(branch_ref));
8892 417a6e49 2020-09-26 stsp if (refname == NULL) {
8893 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8894 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8895 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8896 417a6e49 2020-09-26 stsp goto done;
8898 417a6e49 2020-09-26 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8899 417a6e49 2020-09-26 stsp if (base_refname == NULL) {
8900 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8901 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8902 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8903 417a6e49 2020-09-26 stsp goto done;
8906 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8907 417a6e49 2020-09-26 stsp if (error)
8908 417a6e49 2020-09-26 stsp goto done;
8910 417a6e49 2020-09-26 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8911 417a6e49 2020-09-26 stsp if (error)
8912 417a6e49 2020-09-26 stsp goto done;
8914 417a6e49 2020-09-26 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8915 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8916 417a6e49 2020-09-26 stsp "specified branch has already been integrated");
8917 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8918 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8919 417a6e49 2020-09-26 stsp goto done;
8922 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8923 417a6e49 2020-09-26 stsp if (error) {
8924 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
8925 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8926 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8927 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8928 417a6e49 2020-09-26 stsp goto done;
8931 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8932 417a6e49 2020-09-26 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8933 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref, update_progress, &upa,
8934 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8935 417a6e49 2020-09-26 stsp if (error)
8936 417a6e49 2020-09-26 stsp goto done;
8938 417a6e49 2020-09-26 stsp printf("Integrated %s into %s\n", refname, base_refname);
8939 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8942 417a6e49 2020-09-26 stsp got_repo_close(repo);
8943 417a6e49 2020-09-26 stsp if (worktree)
8944 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8945 417a6e49 2020-09-26 stsp free(cwd);
8946 417a6e49 2020-09-26 stsp free(base_commit_id);
8947 417a6e49 2020-09-26 stsp free(commit_id);
8948 417a6e49 2020-09-26 stsp free(refname);
8949 417a6e49 2020-09-26 stsp free(base_refname);
8950 417a6e49 2020-09-26 stsp return error;
8953 417a6e49 2020-09-26 stsp __dead static void
8954 417a6e49 2020-09-26 stsp usage_stage(void)
8956 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8957 417a6e49 2020-09-26 stsp "[-S] [file-path ...]\n",
8958 417a6e49 2020-09-26 stsp getprogname());
8962 417a6e49 2020-09-26 stsp static const struct got_error *
8963 417a6e49 2020-09-26 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8964 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
8965 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8966 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
8968 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8969 417a6e49 2020-09-26 stsp char *id_str = NULL;
8971 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_ADD &&
8972 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_MODIFY &&
8973 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
8974 417a6e49 2020-09-26 stsp return NULL;
8976 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
8977 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY)
8978 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
8980 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
8982 417a6e49 2020-09-26 stsp return err;
8984 417a6e49 2020-09-26 stsp printf("%s %c %s\n", id_str, staged_status, path);
8985 417a6e49 2020-09-26 stsp free(id_str);
8986 417a6e49 2020-09-26 stsp return NULL;
8989 417a6e49 2020-09-26 stsp static const struct got_error *
8990 417a6e49 2020-09-26 stsp cmd_stage(int argc, char *argv[])
8992 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8993 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8994 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8995 417a6e49 2020-09-26 stsp char *cwd = NULL;
8996 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8997 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
8998 417a6e49 2020-09-26 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8999 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9000 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9001 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9003 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9005 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9006 417a6e49 2020-09-26 stsp switch (ch) {
9008 417a6e49 2020-09-26 stsp list_stage = 1;
9011 417a6e49 2020-09-26 stsp pflag = 1;
9014 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9017 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
9020 417a6e49 2020-09-26 stsp usage_stage();
9021 417a6e49 2020-09-26 stsp /* NOTREACHED */
9025 417a6e49 2020-09-26 stsp argc -= optind;
9026 417a6e49 2020-09-26 stsp argv += optind;
9028 417a6e49 2020-09-26 stsp #ifndef PROFILE
9029 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9030 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9031 417a6e49 2020-09-26 stsp err(1, "pledge");
9033 417a6e49 2020-09-26 stsp if (list_stage && (pflag || patch_script_path))
9034 417a6e49 2020-09-26 stsp errx(1, "-l option cannot be used with other options");
9035 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9036 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9038 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9039 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9040 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9041 417a6e49 2020-09-26 stsp goto done;
9044 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9045 417a6e49 2020-09-26 stsp if (error) {
9046 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9047 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9048 417a6e49 2020-09-26 stsp goto done;
9051 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9053 417a6e49 2020-09-26 stsp if (error != NULL)
9054 417a6e49 2020-09-26 stsp goto done;
9056 417a6e49 2020-09-26 stsp if (patch_script_path) {
9057 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9058 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9059 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9060 417a6e49 2020-09-26 stsp patch_script_path);
9061 417a6e49 2020-09-26 stsp goto done;
9064 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9065 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9066 417a6e49 2020-09-26 stsp if (error)
9067 417a6e49 2020-09-26 stsp goto done;
9069 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9070 417a6e49 2020-09-26 stsp if (error)
9071 417a6e49 2020-09-26 stsp goto done;
9073 417a6e49 2020-09-26 stsp if (list_stage)
9074 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo,
9075 417a6e49 2020-09-26 stsp print_stage, NULL, check_cancelled, NULL);
9077 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9078 417a6e49 2020-09-26 stsp cpa.action = "stage";
9079 417a6e49 2020-09-26 stsp error = got_worktree_stage(worktree, &paths,
9080 417a6e49 2020-09-26 stsp pflag ? NULL : print_status, NULL,
9081 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa,
9082 417a6e49 2020-09-26 stsp allow_bad_symlinks, repo);
9085 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9086 417a6e49 2020-09-26 stsp error == NULL)
9087 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9089 417a6e49 2020-09-26 stsp got_repo_close(repo);
9090 417a6e49 2020-09-26 stsp if (worktree)
9091 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9092 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9093 417a6e49 2020-09-26 stsp free((char *)pe->path);
9094 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9095 417a6e49 2020-09-26 stsp free(cwd);
9096 417a6e49 2020-09-26 stsp return error;
9099 417a6e49 2020-09-26 stsp __dead static void
9100 417a6e49 2020-09-26 stsp usage_unstage(void)
9102 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9103 417a6e49 2020-09-26 stsp "[file-path ...]\n",
9104 417a6e49 2020-09-26 stsp getprogname());
9109 417a6e49 2020-09-26 stsp static const struct got_error *
9110 417a6e49 2020-09-26 stsp cmd_unstage(int argc, char *argv[])
9112 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9113 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9114 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9115 417a6e49 2020-09-26 stsp char *cwd = NULL;
9116 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9117 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9118 417a6e49 2020-09-26 stsp int ch, pflag = 0;
9119 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
9120 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9121 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9122 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9124 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9126 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9127 417a6e49 2020-09-26 stsp switch (ch) {
9129 417a6e49 2020-09-26 stsp pflag = 1;
9132 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9135 417a6e49 2020-09-26 stsp usage_unstage();
9136 417a6e49 2020-09-26 stsp /* NOTREACHED */
9140 417a6e49 2020-09-26 stsp argc -= optind;
9141 417a6e49 2020-09-26 stsp argv += optind;
9143 417a6e49 2020-09-26 stsp #ifndef PROFILE
9144 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9145 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9146 417a6e49 2020-09-26 stsp err(1, "pledge");
9148 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9149 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9151 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9152 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9153 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9154 417a6e49 2020-09-26 stsp goto done;
9157 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9158 417a6e49 2020-09-26 stsp if (error) {
9159 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9160 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9161 417a6e49 2020-09-26 stsp goto done;
9164 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9166 417a6e49 2020-09-26 stsp if (error != NULL)
9167 417a6e49 2020-09-26 stsp goto done;
9169 417a6e49 2020-09-26 stsp if (patch_script_path) {
9170 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9171 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9172 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9173 417a6e49 2020-09-26 stsp patch_script_path);
9174 417a6e49 2020-09-26 stsp goto done;
9178 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9179 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9180 417a6e49 2020-09-26 stsp if (error)
9181 417a6e49 2020-09-26 stsp goto done;
9183 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9184 417a6e49 2020-09-26 stsp if (error)
9185 417a6e49 2020-09-26 stsp goto done;
9187 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9188 417a6e49 2020-09-26 stsp cpa.action = "unstage";
9189 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
9190 417a6e49 2020-09-26 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9191 417a6e49 2020-09-26 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9192 417a6e49 2020-09-26 stsp if (!error)
9193 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
9195 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9196 417a6e49 2020-09-26 stsp error == NULL)
9197 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9199 417a6e49 2020-09-26 stsp got_repo_close(repo);
9200 417a6e49 2020-09-26 stsp if (worktree)
9201 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9202 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9203 417a6e49 2020-09-26 stsp free((char *)pe->path);
9204 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9205 417a6e49 2020-09-26 stsp free(cwd);
9206 417a6e49 2020-09-26 stsp return error;
9209 417a6e49 2020-09-26 stsp __dead static void
9210 417a6e49 2020-09-26 stsp usage_cat(void)
9212 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9213 417a6e49 2020-09-26 stsp "arg1 [arg2 ...]\n", getprogname());
9217 417a6e49 2020-09-26 stsp static const struct got_error *
9218 417a6e49 2020-09-26 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9220 417a6e49 2020-09-26 stsp const struct got_error *err;
9221 417a6e49 2020-09-26 stsp struct got_blob_object *blob;
9223 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9225 417a6e49 2020-09-26 stsp return err;
9227 417a6e49 2020-09-26 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9228 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
9229 417a6e49 2020-09-26 stsp return err;
9232 417a6e49 2020-09-26 stsp static const struct got_error *
9233 417a6e49 2020-09-26 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9235 417a6e49 2020-09-26 stsp const struct got_error *err;
9236 417a6e49 2020-09-26 stsp struct got_tree_object *tree;
9237 417a6e49 2020-09-26 stsp int nentries, i;
9239 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, id);
9241 417a6e49 2020-09-26 stsp return err;
9243 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
9244 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
9245 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
9246 417a6e49 2020-09-26 stsp char *id_str;
9247 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
9249 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
9250 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9253 417a6e49 2020-09-26 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9254 417a6e49 2020-09-26 stsp got_tree_entry_get_mode(te),
9255 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te));
9256 417a6e49 2020-09-26 stsp free(id_str);
9259 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
9260 417a6e49 2020-09-26 stsp return err;
9263 417a6e49 2020-09-26 stsp static const struct got_error *
9264 417a6e49 2020-09-26 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9266 417a6e49 2020-09-26 stsp const struct got_error *err;
9267 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
9268 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
9269 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
9270 417a6e49 2020-09-26 stsp char *id_str = NULL;
9271 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
9273 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
9275 417a6e49 2020-09-26 stsp return err;
9277 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9279 417a6e49 2020-09-26 stsp goto done;
9281 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9282 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9283 417a6e49 2020-09-26 stsp fprintf(outfile, "numparents %d\n",
9284 417a6e49 2020-09-26 stsp got_object_commit_get_nparents(commit));
9285 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9286 417a6e49 2020-09-26 stsp char *pid_str;
9287 417a6e49 2020-09-26 stsp err = got_object_id_str(&pid_str, pid->id);
9289 417a6e49 2020-09-26 stsp goto done;
9290 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9291 417a6e49 2020-09-26 stsp free(pid_str);
9293 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9294 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9295 417a6e49 2020-09-26 stsp got_object_commit_get_author_time(commit));
9297 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9298 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9299 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit));
9301 417a6e49 2020-09-26 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9302 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9303 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", logmsg);
9305 417a6e49 2020-09-26 stsp free(id_str);
9306 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
9307 417a6e49 2020-09-26 stsp return err;
9310 417a6e49 2020-09-26 stsp static const struct got_error *
9311 417a6e49 2020-09-26 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9313 417a6e49 2020-09-26 stsp const struct got_error *err;
9314 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
9315 417a6e49 2020-09-26 stsp char *id_str = NULL;
9316 417a6e49 2020-09-26 stsp const char *tagmsg = NULL;
9318 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
9320 417a6e49 2020-09-26 stsp return err;
9322 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9324 417a6e49 2020-09-26 stsp goto done;
9326 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9328 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
9329 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9330 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9331 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_BLOB);
9333 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9334 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9335 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TREE);
9337 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9338 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9339 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_COMMIT);
9341 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9342 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9343 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TAG);
9349 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9350 417a6e49 2020-09-26 stsp got_object_tag_get_name(tag));
9352 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9353 417a6e49 2020-09-26 stsp got_object_tag_get_tagger(tag),
9354 417a6e49 2020-09-26 stsp got_object_tag_get_tagger_time(tag));
9356 417a6e49 2020-09-26 stsp tagmsg = got_object_tag_get_message(tag);
9357 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9358 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", tagmsg);
9360 417a6e49 2020-09-26 stsp free(id_str);
9361 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
9362 417a6e49 2020-09-26 stsp return err;
9365 417a6e49 2020-09-26 stsp static const struct got_error *
9366 417a6e49 2020-09-26 stsp cmd_cat(int argc, char *argv[])
9368 417a6e49 2020-09-26 stsp const struct got_error *error;
9369 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9370 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9371 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9372 417a6e49 2020-09-26 stsp const char *commit_id_str = NULL;
9373 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9374 417a6e49 2020-09-26 stsp int ch, obj_type, i, force_path = 0;
9376 417a6e49 2020-09-26 stsp #ifndef PROFILE
9377 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9378 417a6e49 2020-09-26 stsp NULL) == -1)
9379 417a6e49 2020-09-26 stsp err(1, "pledge");
9382 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9383 417a6e49 2020-09-26 stsp switch (ch) {
9385 417a6e49 2020-09-26 stsp commit_id_str = optarg;
9388 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
9389 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9390 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
9392 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
9395 417a6e49 2020-09-26 stsp force_path = 1;
9398 417a6e49 2020-09-26 stsp usage_cat();
9399 417a6e49 2020-09-26 stsp /* NOTREACHED */
9403 417a6e49 2020-09-26 stsp argc -= optind;
9404 417a6e49 2020-09-26 stsp argv += optind;
9406 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9407 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9408 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9409 417a6e49 2020-09-26 stsp goto done;
9411 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9412 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9413 417a6e49 2020-09-26 stsp goto done;
9414 417a6e49 2020-09-26 stsp if (worktree) {
9415 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9416 417a6e49 2020-09-26 stsp repo_path = strdup(
9417 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
9418 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9419 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
9420 417a6e49 2020-09-26 stsp goto done;
9425 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9426 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
9427 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9428 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
9431 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
9432 417a6e49 2020-09-26 stsp free(repo_path);
9433 417a6e49 2020-09-26 stsp if (error != NULL)
9434 417a6e49 2020-09-26 stsp goto done;
9436 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9437 417a6e49 2020-09-26 stsp if (error)
9438 417a6e49 2020-09-26 stsp goto done;
9440 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
9441 417a6e49 2020-09-26 stsp commit_id_str = GOT_REF_HEAD;
9442 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
9443 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9444 417a6e49 2020-09-26 stsp if (error)
9445 417a6e49 2020-09-26 stsp goto done;
9447 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
9448 417a6e49 2020-09-26 stsp if (force_path) {
9449 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9451 417a6e49 2020-09-26 stsp if (error)
9454 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9455 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9456 417a6e49 2020-09-26 stsp if (error) {
9457 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9458 417a6e49 2020-09-26 stsp error->code != GOT_ERR_NOT_REF)
9460 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo,
9461 417a6e49 2020-09-26 stsp commit_id, argv[i]);
9462 417a6e49 2020-09-26 stsp if (error)
9467 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, id);
9468 417a6e49 2020-09-26 stsp if (error)
9471 417a6e49 2020-09-26 stsp switch (obj_type) {
9472 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9473 417a6e49 2020-09-26 stsp error = cat_blob(id, repo, stdout);
9475 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9476 417a6e49 2020-09-26 stsp error = cat_tree(id, repo, stdout);
9478 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9479 417a6e49 2020-09-26 stsp error = cat_commit(id, repo, stdout);
9481 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9482 417a6e49 2020-09-26 stsp error = cat_tag(id, repo, stdout);
9485 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9488 417a6e49 2020-09-26 stsp if (error)
9490 417a6e49 2020-09-26 stsp free(label);
9491 417a6e49 2020-09-26 stsp label = NULL;
9493 417a6e49 2020-09-26 stsp id = NULL;
9496 417a6e49 2020-09-26 stsp free(label);
9498 417a6e49 2020-09-26 stsp free(commit_id);
9499 417a6e49 2020-09-26 stsp if (worktree)
9500 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9501 417a6e49 2020-09-26 stsp if (repo) {
9502 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
9503 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
9504 417a6e49 2020-09-26 stsp if (error == NULL)
9505 417a6e49 2020-09-26 stsp error = repo_error;
9507 417a6e49 2020-09-26 stsp return error;
9510 417a6e49 2020-09-26 stsp __dead static void
9511 417a6e49 2020-09-26 stsp usage_info(void)
9513 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9514 417a6e49 2020-09-26 stsp getprogname());
9518 417a6e49 2020-09-26 stsp static const struct got_error *
9519 417a6e49 2020-09-26 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9520 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9521 417a6e49 2020-09-26 stsp struct got_object_id *commit_id)
9523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
9524 417a6e49 2020-09-26 stsp char *id_str = NULL;
9525 417a6e49 2020-09-26 stsp char datebuf[128];
9526 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
9527 417a6e49 2020-09-26 stsp struct got_pathlist_head *paths = arg;
9528 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9531 417a6e49 2020-09-26 stsp * Clear error indication from any of the path arguments which
9532 417a6e49 2020-09-26 stsp * would cause this file index entry to be displayed.
9534 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, paths, entry) {
9535 417a6e49 2020-09-26 stsp if (got_path_cmp(path, pe->path, strlen(path),
9536 417a6e49 2020-09-26 stsp pe->path_len) == 0 ||
9537 417a6e49 2020-09-26 stsp got_path_is_child(path, pe->path, pe->path_len))
9538 417a6e49 2020-09-26 stsp pe->data = NULL; /* no error */
9541 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
9542 417a6e49 2020-09-26 stsp if (S_ISLNK(mode))
9543 417a6e49 2020-09-26 stsp printf("symlink: %s\n", path);
9544 417a6e49 2020-09-26 stsp else if (S_ISREG(mode)) {
9545 417a6e49 2020-09-26 stsp printf("file: %s\n", path);
9546 417a6e49 2020-09-26 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9547 417a6e49 2020-09-26 stsp } else if (S_ISDIR(mode))
9548 417a6e49 2020-09-26 stsp printf("directory: %s\n", path);
9550 417a6e49 2020-09-26 stsp printf("something: %s\n", path);
9552 417a6e49 2020-09-26 stsp tm = localtime_r(&mtime, &mytm);
9553 417a6e49 2020-09-26 stsp if (tm == NULL)
9554 417a6e49 2020-09-26 stsp return NULL;
9555 417a6e49 2020-09-26 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9556 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
9557 417a6e49 2020-09-26 stsp printf("timestamp: %s\n", datebuf);
9559 417a6e49 2020-09-26 stsp if (blob_id) {
9560 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
9562 417a6e49 2020-09-26 stsp return err;
9563 417a6e49 2020-09-26 stsp printf("based on blob: %s\n", id_str);
9564 417a6e49 2020-09-26 stsp free(id_str);
9567 417a6e49 2020-09-26 stsp if (staged_blob_id) {
9568 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
9570 417a6e49 2020-09-26 stsp return err;
9571 417a6e49 2020-09-26 stsp printf("based on staged blob: %s\n", id_str);
9572 417a6e49 2020-09-26 stsp free(id_str);
9575 417a6e49 2020-09-26 stsp if (commit_id) {
9576 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
9578 417a6e49 2020-09-26 stsp return err;
9579 417a6e49 2020-09-26 stsp printf("based on commit: %s\n", id_str);
9580 417a6e49 2020-09-26 stsp free(id_str);
9583 417a6e49 2020-09-26 stsp return NULL;
9586 417a6e49 2020-09-26 stsp static const struct got_error *
9587 417a6e49 2020-09-26 stsp cmd_info(int argc, char *argv[])
9589 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9590 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9591 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
9592 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9593 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9594 417a6e49 2020-09-26 stsp char *uuidstr = NULL;
9595 417a6e49 2020-09-26 stsp int ch, show_files = 0;
9597 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9599 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9600 417a6e49 2020-09-26 stsp switch (ch) {
9602 417a6e49 2020-09-26 stsp usage_info();
9603 417a6e49 2020-09-26 stsp /* NOTREACHED */
9607 417a6e49 2020-09-26 stsp argc -= optind;
9608 417a6e49 2020-09-26 stsp argv += optind;
9610 417a6e49 2020-09-26 stsp #ifndef PROFILE
9611 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9612 417a6e49 2020-09-26 stsp NULL) == -1)
9613 417a6e49 2020-09-26 stsp err(1, "pledge");
9615 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9616 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9617 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9618 417a6e49 2020-09-26 stsp goto done;
9621 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9622 417a6e49 2020-09-26 stsp if (error) {
9623 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9624 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
9625 417a6e49 2020-09-26 stsp goto done;
9628 417a6e49 2020-09-26 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9629 417a6e49 2020-09-26 stsp if (error)
9630 417a6e49 2020-09-26 stsp goto done;
9632 417a6e49 2020-09-26 stsp if (argc >= 1) {
9633 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9634 417a6e49 2020-09-26 stsp worktree);
9635 417a6e49 2020-09-26 stsp if (error)
9636 417a6e49 2020-09-26 stsp goto done;
9637 417a6e49 2020-09-26 stsp show_files = 1;
9640 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
9641 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
9642 417a6e49 2020-09-26 stsp if (error)
9643 417a6e49 2020-09-26 stsp goto done;
9645 417a6e49 2020-09-26 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9646 417a6e49 2020-09-26 stsp if (error)
9647 417a6e49 2020-09-26 stsp goto done;
9649 417a6e49 2020-09-26 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9650 417a6e49 2020-09-26 stsp printf("work tree base commit: %s\n", id_str);
9651 417a6e49 2020-09-26 stsp printf("work tree path prefix: %s\n",
9652 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree));
9653 417a6e49 2020-09-26 stsp printf("work tree branch reference: %s\n",
9654 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
9655 417a6e49 2020-09-26 stsp printf("work tree UUID: %s\n", uuidstr);
9656 417a6e49 2020-09-26 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9658 417a6e49 2020-09-26 stsp if (show_files) {
9659 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9660 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9661 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
9664 417a6e49 2020-09-26 stsp * Assume this path will fail. This will be corrected
9665 417a6e49 2020-09-26 stsp * in print_path_info() in case the path does suceeed.
9667 417a6e49 2020-09-26 stsp pe->data = (void *)got_error_path(pe->path,
9668 417a6e49 2020-09-26 stsp GOT_ERR_BAD_PATH);
9670 417a6e49 2020-09-26 stsp error = got_worktree_path_info(worktree, &paths,
9671 417a6e49 2020-09-26 stsp print_path_info, &paths, check_cancelled, NULL);
9672 417a6e49 2020-09-26 stsp if (error)
9673 417a6e49 2020-09-26 stsp goto done;
9674 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9675 417a6e49 2020-09-26 stsp if (pe->data != NULL) {
9676 417a6e49 2020-09-26 stsp error = pe->data; /* bad path */
9682 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9683 417a6e49 2020-09-26 stsp free((char *)pe->path);
9684 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9685 417a6e49 2020-09-26 stsp free(cwd);
9686 417a6e49 2020-09-26 stsp free(id_str);
9687 417a6e49 2020-09-26 stsp free(uuidstr);
9688 417a6e49 2020-09-26 stsp return error;