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;
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 if (asprintf(&gotconfig,
1391 417a6e49 2020-09-26 stsp "remote \"%s\" {\n"
1392 417a6e49 2020-09-26 stsp "\tserver %s\n"
1393 417a6e49 2020-09-26 stsp "\tprotocol %s\n"
1395 417a6e49 2020-09-26 stsp "\trepository \"%s\"\n"
1398 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1399 417a6e49 2020-09-26 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1400 417a6e49 2020-09-26 stsp server_path,
1401 417a6e49 2020-09-26 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1402 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1403 417a6e49 2020-09-26 stsp goto done;
1405 417a6e49 2020-09-26 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1406 417a6e49 2020-09-26 stsp if (n != strlen(gotconfig)) {
1407 417a6e49 2020-09-26 stsp error = got_ferror(gotconfig_file, GOT_ERR_IO);
1408 417a6e49 2020-09-26 stsp goto done;
1411 417a6e49 2020-09-26 stsp /* Create a config file Git can understand. */
1412 417a6e49 2020-09-26 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1413 417a6e49 2020-09-26 stsp if (gitconfig_path == NULL) {
1414 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1415 417a6e49 2020-09-26 stsp goto done;
1417 417a6e49 2020-09-26 stsp gitconfig_file = fopen(gitconfig_path, "a");
1418 417a6e49 2020-09-26 stsp if (gitconfig_file == NULL) {
1419 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1420 417a6e49 2020-09-26 stsp goto done;
1422 417a6e49 2020-09-26 stsp if (mirror_references) {
1423 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1424 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1425 417a6e49 2020-09-26 stsp "\turl = %s\n"
1426 417a6e49 2020-09-26 stsp "\tfetch = +refs/*:refs/*\n"
1427 417a6e49 2020-09-26 stsp "\tmirror = true\n",
1428 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1429 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1430 417a6e49 2020-09-26 stsp goto done;
1432 417a6e49 2020-09-26 stsp } else if (fetch_all_branches) {
1433 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1434 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1435 417a6e49 2020-09-26 stsp "\turl = %s\n"
1436 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1437 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1438 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1439 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1440 417a6e49 2020-09-26 stsp goto done;
1443 417a6e49 2020-09-26 stsp const char *branchname;
1446 417a6e49 2020-09-26 stsp * If the server specified a default branch, use just that one.
1447 417a6e49 2020-09-26 stsp * Otherwise fall back to fetching all branches on next fetch.
1449 417a6e49 2020-09-26 stsp if (head_symref) {
1450 417a6e49 2020-09-26 stsp branchname = got_ref_get_symref_target(head_symref);
1451 417a6e49 2020-09-26 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1452 417a6e49 2020-09-26 stsp branchname += 11;
1454 417a6e49 2020-09-26 stsp branchname = "*"; /* fall back to all branches */
1455 417a6e49 2020-09-26 stsp if (asprintf(&gitconfig,
1456 417a6e49 2020-09-26 stsp "[remote \"%s\"]\n"
1457 417a6e49 2020-09-26 stsp "\turl = %s\n"
1458 417a6e49 2020-09-26 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1459 417a6e49 2020-09-26 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1460 417a6e49 2020-09-26 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1461 417a6e49 2020-09-26 stsp branchname) == -1) {
1462 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
1463 417a6e49 2020-09-26 stsp goto done;
1466 417a6e49 2020-09-26 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1467 417a6e49 2020-09-26 stsp if (n != strlen(gitconfig)) {
1468 417a6e49 2020-09-26 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1469 417a6e49 2020-09-26 stsp goto done;
1472 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1473 417a6e49 2020-09-26 stsp printf("Created %s repository '%s'\n",
1474 417a6e49 2020-09-26 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1476 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
1477 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
1478 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
1479 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1480 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
1482 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1483 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
1484 417a6e49 2020-09-26 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1485 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1486 417a6e49 2020-09-26 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1487 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
1489 417a6e49 2020-09-26 stsp got_repo_close(repo);
1490 417a6e49 2020-09-26 stsp if (head_symref)
1491 417a6e49 2020-09-26 stsp got_ref_close(head_symref);
1492 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
1493 417a6e49 2020-09-26 stsp free((void *)pe->path);
1494 417a6e49 2020-09-26 stsp free(pe->data);
1496 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
1497 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1498 417a6e49 2020-09-26 stsp free((void *)pe->path);
1499 417a6e49 2020-09-26 stsp free(pe->data);
1501 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
1502 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
1503 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
1504 417a6e49 2020-09-26 stsp free(pack_hash);
1505 417a6e49 2020-09-26 stsp free(proto);
1506 417a6e49 2020-09-26 stsp free(host);
1507 417a6e49 2020-09-26 stsp free(port);
1508 417a6e49 2020-09-26 stsp free(server_path);
1509 417a6e49 2020-09-26 stsp free(repo_name);
1510 417a6e49 2020-09-26 stsp free(default_destdir);
1511 417a6e49 2020-09-26 stsp free(gotconfig);
1512 417a6e49 2020-09-26 stsp free(gitconfig);
1513 417a6e49 2020-09-26 stsp free(gotconfig_path);
1514 417a6e49 2020-09-26 stsp free(gitconfig_path);
1515 417a6e49 2020-09-26 stsp free(git_url);
1516 417a6e49 2020-09-26 stsp return error;
1519 417a6e49 2020-09-26 stsp static const struct got_error *
1520 417a6e49 2020-09-26 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1521 417a6e49 2020-09-26 stsp int replace_tags, int verbosity, struct got_repository *repo)
1523 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1524 417a6e49 2020-09-26 stsp char *new_id_str = NULL;
1525 417a6e49 2020-09-26 stsp struct got_object_id *old_id = NULL;
1527 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
1529 417a6e49 2020-09-26 stsp goto done;
1531 417a6e49 2020-09-26 stsp if (!replace_tags &&
1532 417a6e49 2020-09-26 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1533 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1535 417a6e49 2020-09-26 stsp goto done;
1536 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1537 417a6e49 2020-09-26 stsp goto done;
1538 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1539 417a6e49 2020-09-26 stsp printf("Rejecting update of existing tag %s: %s\n",
1540 417a6e49 2020-09-26 stsp got_ref_get_name(ref), new_id_str);
1542 417a6e49 2020-09-26 stsp goto done;
1545 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1546 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1547 417a6e49 2020-09-26 stsp printf("Replacing reference %s: %s\n",
1548 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1549 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1551 417a6e49 2020-09-26 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1553 417a6e49 2020-09-26 stsp goto done;
1554 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1556 417a6e49 2020-09-26 stsp goto done;
1558 417a6e49 2020-09-26 stsp err = got_ref_resolve(&old_id, repo, ref);
1560 417a6e49 2020-09-26 stsp goto done;
1561 417a6e49 2020-09-26 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1562 417a6e49 2020-09-26 stsp goto done;
1564 417a6e49 2020-09-26 stsp err = got_ref_change_ref(ref, new_id);
1566 417a6e49 2020-09-26 stsp goto done;
1567 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
1569 417a6e49 2020-09-26 stsp goto done;
1572 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1573 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1574 417a6e49 2020-09-26 stsp new_id_str);
1576 417a6e49 2020-09-26 stsp free(old_id);
1577 417a6e49 2020-09-26 stsp free(new_id_str);
1578 417a6e49 2020-09-26 stsp return err;
1581 417a6e49 2020-09-26 stsp static const struct got_error *
1582 417a6e49 2020-09-26 stsp update_symref(const char *refname, struct got_reference *target_ref,
1583 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1585 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1586 417a6e49 2020-09-26 stsp struct got_reference *symref;
1587 417a6e49 2020-09-26 stsp int symref_is_locked = 0;
1589 417a6e49 2020-09-26 stsp err = got_ref_open(&symref, repo, refname, 1);
1590 417a6e49 2020-09-26 stsp if (err) {
1591 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1592 417a6e49 2020-09-26 stsp return err;
1593 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1595 417a6e49 2020-09-26 stsp goto done;
1597 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1599 417a6e49 2020-09-26 stsp goto done;
1601 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1602 417a6e49 2020-09-26 stsp printf("Created reference %s: %s\n",
1603 417a6e49 2020-09-26 stsp got_ref_get_name(symref),
1604 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1606 417a6e49 2020-09-26 stsp symref_is_locked = 1;
1608 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1609 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref)) == 0)
1610 417a6e49 2020-09-26 stsp goto done;
1612 417a6e49 2020-09-26 stsp err = got_ref_change_symref(symref,
1613 417a6e49 2020-09-26 stsp got_ref_get_name(target_ref));
1615 417a6e49 2020-09-26 stsp goto done;
1617 417a6e49 2020-09-26 stsp err = got_ref_write(symref, repo);
1619 417a6e49 2020-09-26 stsp goto done;
1621 417a6e49 2020-09-26 stsp if (verbosity >= 0)
1622 417a6e49 2020-09-26 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1623 417a6e49 2020-09-26 stsp got_ref_get_symref_target(symref));
1627 417a6e49 2020-09-26 stsp if (symref_is_locked) {
1628 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(symref);
1629 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1630 417a6e49 2020-09-26 stsp err = unlock_err;
1632 417a6e49 2020-09-26 stsp got_ref_close(symref);
1633 417a6e49 2020-09-26 stsp return err;
1636 417a6e49 2020-09-26 stsp __dead static void
1637 417a6e49 2020-09-26 stsp usage_fetch(void)
1639 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1640 417a6e49 2020-09-26 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1641 417a6e49 2020-09-26 stsp "[remote-repository-name]\n",
1642 417a6e49 2020-09-26 stsp getprogname());
1646 417a6e49 2020-09-26 stsp static const struct got_error *
1647 417a6e49 2020-09-26 stsp delete_missing_ref(struct got_reference *ref,
1648 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1650 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
1651 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
1652 417a6e49 2020-09-26 stsp char *id_str = NULL;
1654 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref)) {
1655 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1657 417a6e49 2020-09-26 stsp return err;
1658 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1659 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1660 417a6e49 2020-09-26 stsp got_ref_get_name(ref),
1661 417a6e49 2020-09-26 stsp got_ref_get_symref_target(ref));
1664 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
1666 417a6e49 2020-09-26 stsp return err;
1667 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
1669 417a6e49 2020-09-26 stsp goto done;
1671 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
1673 417a6e49 2020-09-26 stsp goto done;
1674 417a6e49 2020-09-26 stsp if (verbosity >= 0) {
1675 417a6e49 2020-09-26 stsp printf("Deleted reference %s: %s\n",
1676 417a6e49 2020-09-26 stsp got_ref_get_name(ref), id_str);
1681 417a6e49 2020-09-26 stsp free(id_str);
1682 417a6e49 2020-09-26 stsp return NULL;
1685 417a6e49 2020-09-26 stsp static const struct got_error *
1686 417a6e49 2020-09-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1687 417a6e49 2020-09-26 stsp struct got_pathlist_head *their_symrefs,
1688 417a6e49 2020-09-26 stsp const struct got_remote_repo *remote,
1689 417a6e49 2020-09-26 stsp int verbosity, struct got_repository *repo)
1691 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *unlock_err;
1692 417a6e49 2020-09-26 stsp struct got_reflist_head my_refs;
1693 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
1694 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1695 417a6e49 2020-09-26 stsp char *remote_namespace = NULL;
1696 417a6e49 2020-09-26 stsp char *local_refname = NULL;
1698 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&my_refs);
1700 417a6e49 2020-09-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1702 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1704 417a6e49 2020-09-26 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1706 417a6e49 2020-09-26 stsp goto done;
1708 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1709 417a6e49 2020-09-26 stsp const char *refname = got_ref_get_name(re->ref);
1711 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
1712 417a6e49 2020-09-26 stsp if (strncmp(refname, remote_namespace,
1713 417a6e49 2020-09-26 stsp strlen(remote_namespace)) == 0) {
1714 417a6e49 2020-09-26 stsp if (strcmp(refname + strlen(remote_namespace),
1715 417a6e49 2020-09-26 stsp GOT_REF_HEAD) == 0)
1717 417a6e49 2020-09-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1718 417a6e49 2020-09-26 stsp refname + strlen(remote_namespace)) == -1) {
1719 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
1720 417a6e49 2020-09-26 stsp goto done;
1722 417a6e49 2020-09-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1726 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1727 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1730 417a6e49 2020-09-26 stsp if (pe != NULL)
1733 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1734 417a6e49 2020-09-26 stsp if (strcmp(local_refname, pe->path) == 0)
1737 417a6e49 2020-09-26 stsp if (pe != NULL)
1740 417a6e49 2020-09-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1744 417a6e49 2020-09-26 stsp if (local_refname) {
1745 417a6e49 2020-09-26 stsp struct got_reference *ref;
1746 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1747 417a6e49 2020-09-26 stsp if (err) {
1748 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1750 417a6e49 2020-09-26 stsp free(local_refname);
1751 417a6e49 2020-09-26 stsp local_refname = NULL;
1754 417a6e49 2020-09-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1757 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1758 417a6e49 2020-09-26 stsp got_ref_close(ref);
1759 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL) {
1760 417a6e49 2020-09-26 stsp err = unlock_err;
1764 417a6e49 2020-09-26 stsp free(local_refname);
1765 417a6e49 2020-09-26 stsp local_refname = NULL;
1769 417a6e49 2020-09-26 stsp free(remote_namespace);
1770 417a6e49 2020-09-26 stsp free(local_refname);
1771 417a6e49 2020-09-26 stsp return err;
1774 417a6e49 2020-09-26 stsp static const struct got_error *
1775 417a6e49 2020-09-26 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1776 417a6e49 2020-09-26 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1778 417a6e49 2020-09-26 stsp const struct got_error *err, *unlock_err;
1779 417a6e49 2020-09-26 stsp char *remote_refname;
1780 417a6e49 2020-09-26 stsp struct got_reference *ref;
1782 417a6e49 2020-09-26 stsp if (strncmp("refs/", refname, 5) == 0)
1783 417a6e49 2020-09-26 stsp refname += 5;
1785 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1786 417a6e49 2020-09-26 stsp remote_repo_name, refname) == -1)
1787 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
1789 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1790 417a6e49 2020-09-26 stsp if (err) {
1791 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NOT_REF)
1792 417a6e49 2020-09-26 stsp goto done;
1793 417a6e49 2020-09-26 stsp err = create_ref(remote_refname, id, verbosity, repo);
1795 417a6e49 2020-09-26 stsp err = update_ref(ref, id, 0, verbosity, repo);
1796 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
1797 417a6e49 2020-09-26 stsp if (unlock_err && err == NULL)
1798 417a6e49 2020-09-26 stsp err = unlock_err;
1799 417a6e49 2020-09-26 stsp got_ref_close(ref);
1802 417a6e49 2020-09-26 stsp free(remote_refname);
1803 417a6e49 2020-09-26 stsp return err;
1806 417a6e49 2020-09-26 stsp static const struct got_error *
1807 417a6e49 2020-09-26 stsp cmd_fetch(int argc, char *argv[])
1809 417a6e49 2020-09-26 stsp const struct got_error *error = NULL, *unlock_err;
1810 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
1811 417a6e49 2020-09-26 stsp const char *remote_name;
1812 417a6e49 2020-09-26 stsp char *proto = NULL, *host = NULL, *port = NULL;
1813 417a6e49 2020-09-26 stsp char *repo_name = NULL, *server_path = NULL;
1814 417a6e49 2020-09-26 stsp const struct got_remote_repo *remotes, *remote = NULL;
1815 417a6e49 2020-09-26 stsp int nremotes;
1816 417a6e49 2020-09-26 stsp char *id_str = NULL;
1817 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
1818 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
1819 417a6e49 2020-09-26 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1820 417a6e49 2020-09-26 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1821 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
1822 417a6e49 2020-09-26 stsp struct got_object_id *pack_hash = NULL;
1823 417a6e49 2020-09-26 stsp int i, ch, fetchfd = -1, fetchstatus;
1824 417a6e49 2020-09-26 stsp pid_t fetchpid = -1;
1825 417a6e49 2020-09-26 stsp struct got_fetch_progress_arg fpa;
1826 417a6e49 2020-09-26 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1827 417a6e49 2020-09-26 stsp int delete_refs = 0, replace_tags = 0;
1829 417a6e49 2020-09-26 stsp TAILQ_INIT(&refs);
1830 417a6e49 2020-09-26 stsp TAILQ_INIT(&symrefs);
1831 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_branches);
1832 417a6e49 2020-09-26 stsp TAILQ_INIT(&wanted_refs);
1834 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1835 417a6e49 2020-09-26 stsp switch (ch) {
1837 417a6e49 2020-09-26 stsp fetch_all_branches = 1;
1840 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_branches,
1841 417a6e49 2020-09-26 stsp optarg, NULL);
1842 417a6e49 2020-09-26 stsp if (error)
1843 417a6e49 2020-09-26 stsp return error;
1846 417a6e49 2020-09-26 stsp delete_refs = 1;
1849 417a6e49 2020-09-26 stsp list_refs_only = 1;
1852 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
1853 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1854 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
1856 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
1859 417a6e49 2020-09-26 stsp replace_tags = 1;
1862 417a6e49 2020-09-26 stsp if (verbosity < 0)
1863 417a6e49 2020-09-26 stsp verbosity = 0;
1864 417a6e49 2020-09-26 stsp else if (verbosity < 3)
1865 417a6e49 2020-09-26 stsp verbosity++;
1868 417a6e49 2020-09-26 stsp verbosity = -1;
1871 417a6e49 2020-09-26 stsp error = got_pathlist_append(&wanted_refs,
1872 417a6e49 2020-09-26 stsp optarg, NULL);
1873 417a6e49 2020-09-26 stsp if (error)
1874 417a6e49 2020-09-26 stsp return error;
1877 417a6e49 2020-09-26 stsp usage_fetch();
1881 417a6e49 2020-09-26 stsp argc -= optind;
1882 417a6e49 2020-09-26 stsp argv += optind;
1884 417a6e49 2020-09-26 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1885 417a6e49 2020-09-26 stsp errx(1, "-a and -b options are mutually exclusive");
1886 417a6e49 2020-09-26 stsp if (list_refs_only) {
1887 417a6e49 2020-09-26 stsp if (!TAILQ_EMPTY(&wanted_branches))
1888 417a6e49 2020-09-26 stsp errx(1, "-l and -b options are mutually exclusive");
1889 417a6e49 2020-09-26 stsp if (fetch_all_branches)
1890 417a6e49 2020-09-26 stsp errx(1, "-l and -a options are mutually exclusive");
1891 417a6e49 2020-09-26 stsp if (delete_refs)
1892 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
1893 417a6e49 2020-09-26 stsp if (verbosity == -1)
1894 417a6e49 2020-09-26 stsp errx(1, "-l and -q options are mutually exclusive");
1897 417a6e49 2020-09-26 stsp if (argc == 0)
1898 417a6e49 2020-09-26 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1899 417a6e49 2020-09-26 stsp else if (argc == 1)
1900 417a6e49 2020-09-26 stsp remote_name = argv[0];
1902 417a6e49 2020-09-26 stsp usage_fetch();
1904 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
1905 417a6e49 2020-09-26 stsp if (cwd == NULL) {
1906 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
1907 417a6e49 2020-09-26 stsp goto done;
1910 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1911 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
1912 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1913 417a6e49 2020-09-26 stsp goto done;
1915 417a6e49 2020-09-26 stsp error = NULL;
1916 417a6e49 2020-09-26 stsp if (worktree) {
1917 417a6e49 2020-09-26 stsp repo_path =
1918 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
1919 417a6e49 2020-09-26 stsp if (repo_path == NULL)
1920 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1921 417a6e49 2020-09-26 stsp if (error)
1922 417a6e49 2020-09-26 stsp goto done;
1924 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
1925 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
1926 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
1927 417a6e49 2020-09-26 stsp goto done;
1932 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
1933 417a6e49 2020-09-26 stsp if (error)
1934 417a6e49 2020-09-26 stsp goto done;
1936 417a6e49 2020-09-26 stsp if (worktree) {
1937 417a6e49 2020-09-26 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
1938 417a6e49 2020-09-26 stsp if (worktree_conf) {
1939 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1940 417a6e49 2020-09-26 stsp worktree_conf);
1941 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1942 417a6e49 2020-09-26 stsp remote = &remotes[i];
1943 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1948 417a6e49 2020-09-26 stsp if (remote == NULL) {
1949 417a6e49 2020-09-26 stsp repo_conf = got_repo_get_gotconfig(repo);
1950 417a6e49 2020-09-26 stsp if (repo_conf) {
1951 417a6e49 2020-09-26 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
1952 417a6e49 2020-09-26 stsp repo_conf);
1953 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1954 417a6e49 2020-09-26 stsp remote = &remotes[i];
1955 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1960 417a6e49 2020-09-26 stsp if (remote == NULL) {
1961 417a6e49 2020-09-26 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1962 417a6e49 2020-09-26 stsp for (i = 0; i < nremotes; i++) {
1963 417a6e49 2020-09-26 stsp remote = &remotes[i];
1964 417a6e49 2020-09-26 stsp if (strcmp(remote->name, remote_name) == 0)
1968 417a6e49 2020-09-26 stsp if (remote == NULL) {
1969 417a6e49 2020-09-26 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1970 417a6e49 2020-09-26 stsp goto done;
1973 417a6e49 2020-09-26 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1974 417a6e49 2020-09-26 stsp &repo_name, remote->url);
1975 417a6e49 2020-09-26 stsp if (error)
1976 417a6e49 2020-09-26 stsp goto done;
1978 417a6e49 2020-09-26 stsp if (strcmp(proto, "git") == 0) {
1979 417a6e49 2020-09-26 stsp #ifndef PROFILE
1980 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1981 417a6e49 2020-09-26 stsp "sendfd dns inet unveil", NULL) == -1)
1982 417a6e49 2020-09-26 stsp err(1, "pledge");
1984 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1985 417a6e49 2020-09-26 stsp strcmp(proto, "ssh") == 0) {
1986 417a6e49 2020-09-26 stsp #ifndef PROFILE
1987 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1988 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
1989 417a6e49 2020-09-26 stsp err(1, "pledge");
1991 417a6e49 2020-09-26 stsp } else if (strcmp(proto, "http") == 0 ||
1992 417a6e49 2020-09-26 stsp strcmp(proto, "git+http") == 0) {
1993 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1994 417a6e49 2020-09-26 stsp goto done;
1996 417a6e49 2020-09-26 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1997 417a6e49 2020-09-26 stsp goto done;
2000 417a6e49 2020-09-26 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2001 417a6e49 2020-09-26 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2002 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unveil",
2003 417a6e49 2020-09-26 stsp GOT_FETCH_PATH_SSH);
2004 417a6e49 2020-09-26 stsp goto done;
2007 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2008 417a6e49 2020-09-26 stsp if (error)
2009 417a6e49 2020-09-26 stsp goto done;
2011 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2012 417a6e49 2020-09-26 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2013 417a6e49 2020-09-26 stsp port ? ":" : "", port ? port : "");
2015 417a6e49 2020-09-26 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2016 417a6e49 2020-09-26 stsp server_path, verbosity);
2017 417a6e49 2020-09-26 stsp if (error)
2018 417a6e49 2020-09-26 stsp goto done;
2020 417a6e49 2020-09-26 stsp fpa.last_scaled_size[0] = '\0';
2021 417a6e49 2020-09-26 stsp fpa.last_p_indexed = -1;
2022 417a6e49 2020-09-26 stsp fpa.last_p_resolved = -1;
2023 417a6e49 2020-09-26 stsp fpa.verbosity = verbosity;
2024 417a6e49 2020-09-26 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2025 417a6e49 2020-09-26 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2026 417a6e49 2020-09-26 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2027 417a6e49 2020-09-26 stsp fetch_progress, &fpa);
2028 417a6e49 2020-09-26 stsp if (error)
2029 417a6e49 2020-09-26 stsp goto done;
2031 417a6e49 2020-09-26 stsp if (list_refs_only) {
2032 417a6e49 2020-09-26 stsp error = list_remote_refs(&symrefs, &refs);
2033 417a6e49 2020-09-26 stsp goto done;
2036 417a6e49 2020-09-26 stsp if (pack_hash == NULL) {
2037 417a6e49 2020-09-26 stsp if (verbosity >= 0)
2038 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2039 417a6e49 2020-09-26 stsp } else if (verbosity >= 0) {
2040 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, pack_hash);
2041 417a6e49 2020-09-26 stsp if (error)
2042 417a6e49 2020-09-26 stsp goto done;
2043 417a6e49 2020-09-26 stsp printf("\nFetched %s.pack\n", id_str);
2044 417a6e49 2020-09-26 stsp free(id_str);
2045 417a6e49 2020-09-26 stsp id_str = NULL;
2048 417a6e49 2020-09-26 stsp /* Update references provided with the pack file. */
2049 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2050 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2051 417a6e49 2020-09-26 stsp struct got_object_id *id = pe->data;
2052 417a6e49 2020-09-26 stsp struct got_reference *ref;
2053 417a6e49 2020-09-26 stsp char *remote_refname;
2055 417a6e49 2020-09-26 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2056 417a6e49 2020-09-26 stsp !remote->mirror_references) {
2057 417a6e49 2020-09-26 stsp error = update_wanted_ref(refname, id,
2058 417a6e49 2020-09-26 stsp remote->name, verbosity, repo);
2059 417a6e49 2020-09-26 stsp if (error)
2060 417a6e49 2020-09-26 stsp goto done;
2064 417a6e49 2020-09-26 stsp if (remote->mirror_references ||
2065 417a6e49 2020-09-26 stsp strncmp("refs/tags/", refname, 10) == 0) {
2066 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2067 417a6e49 2020-09-26 stsp if (error) {
2068 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2069 417a6e49 2020-09-26 stsp goto done;
2070 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2072 417a6e49 2020-09-26 stsp if (error)
2073 417a6e49 2020-09-26 stsp goto done;
2075 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2076 417a6e49 2020-09-26 stsp verbosity, repo);
2077 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2078 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2079 417a6e49 2020-09-26 stsp error = unlock_err;
2080 417a6e49 2020-09-26 stsp got_ref_close(ref);
2081 417a6e49 2020-09-26 stsp if (error)
2082 417a6e49 2020-09-26 stsp goto done;
2084 417a6e49 2020-09-26 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2085 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2086 417a6e49 2020-09-26 stsp remote_name, refname + 11) == -1) {
2087 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2088 417a6e49 2020-09-26 stsp goto done;
2091 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2092 417a6e49 2020-09-26 stsp if (error) {
2093 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2094 417a6e49 2020-09-26 stsp goto done;
2095 417a6e49 2020-09-26 stsp error = create_ref(remote_refname, id,
2096 417a6e49 2020-09-26 stsp verbosity, repo);
2097 417a6e49 2020-09-26 stsp if (error)
2098 417a6e49 2020-09-26 stsp goto done;
2100 417a6e49 2020-09-26 stsp error = update_ref(ref, id, replace_tags,
2101 417a6e49 2020-09-26 stsp verbosity, repo);
2102 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2103 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2104 417a6e49 2020-09-26 stsp error = unlock_err;
2105 417a6e49 2020-09-26 stsp got_ref_close(ref);
2106 417a6e49 2020-09-26 stsp if (error)
2107 417a6e49 2020-09-26 stsp goto done;
2110 417a6e49 2020-09-26 stsp /* Also create a local branch if none exists yet. */
2111 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, refname, 1);
2112 417a6e49 2020-09-26 stsp if (error) {
2113 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_NOT_REF)
2114 417a6e49 2020-09-26 stsp goto done;
2115 417a6e49 2020-09-26 stsp error = create_ref(refname, id, verbosity,
2117 417a6e49 2020-09-26 stsp if (error)
2118 417a6e49 2020-09-26 stsp goto done;
2120 417a6e49 2020-09-26 stsp unlock_err = got_ref_unlock(ref);
2121 417a6e49 2020-09-26 stsp if (unlock_err && error == NULL)
2122 417a6e49 2020-09-26 stsp error = unlock_err;
2123 417a6e49 2020-09-26 stsp got_ref_close(ref);
2127 417a6e49 2020-09-26 stsp if (delete_refs) {
2128 417a6e49 2020-09-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2129 417a6e49 2020-09-26 stsp verbosity, repo);
2130 417a6e49 2020-09-26 stsp if (error)
2131 417a6e49 2020-09-26 stsp goto done;
2134 417a6e49 2020-09-26 stsp if (!remote->mirror_references) {
2135 417a6e49 2020-09-26 stsp /* Update remote HEAD reference if the server provided one. */
2136 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2137 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
2138 417a6e49 2020-09-26 stsp const char *refname = pe->path;
2139 417a6e49 2020-09-26 stsp const char *target = pe->data;
2140 417a6e49 2020-09-26 stsp char *remote_refname = NULL, *remote_target = NULL;
2142 417a6e49 2020-09-26 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2145 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", target, 11) != 0)
2148 417a6e49 2020-09-26 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2149 417a6e49 2020-09-26 stsp remote->name, refname) == -1) {
2150 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2151 417a6e49 2020-09-26 stsp goto done;
2153 417a6e49 2020-09-26 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2154 417a6e49 2020-09-26 stsp remote->name, target + 11) == -1) {
2155 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2156 417a6e49 2020-09-26 stsp free(remote_refname);
2157 417a6e49 2020-09-26 stsp goto done;
2160 417a6e49 2020-09-26 stsp error = got_ref_open(&target_ref, repo, remote_target,
2162 417a6e49 2020-09-26 stsp if (error) {
2163 417a6e49 2020-09-26 stsp free(remote_refname);
2164 417a6e49 2020-09-26 stsp free(remote_target);
2165 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_REF) {
2166 417a6e49 2020-09-26 stsp error = NULL;
2169 417a6e49 2020-09-26 stsp goto done;
2171 417a6e49 2020-09-26 stsp error = update_symref(remote_refname, target_ref,
2172 417a6e49 2020-09-26 stsp verbosity, repo);
2173 417a6e49 2020-09-26 stsp free(remote_refname);
2174 417a6e49 2020-09-26 stsp free(remote_target);
2175 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
2176 417a6e49 2020-09-26 stsp if (error)
2177 417a6e49 2020-09-26 stsp goto done;
2181 417a6e49 2020-09-26 stsp if (fetchpid > 0) {
2182 417a6e49 2020-09-26 stsp if (kill(fetchpid, SIGTERM) == -1)
2183 417a6e49 2020-09-26 stsp error = got_error_from_errno("kill");
2184 417a6e49 2020-09-26 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2185 417a6e49 2020-09-26 stsp error = got_error_from_errno("waitpid");
2187 417a6e49 2020-09-26 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2188 417a6e49 2020-09-26 stsp error = got_error_from_errno("close");
2190 417a6e49 2020-09-26 stsp got_repo_close(repo);
2191 417a6e49 2020-09-26 stsp if (worktree)
2192 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
2193 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &refs, entry) {
2194 417a6e49 2020-09-26 stsp free((void *)pe->path);
2195 417a6e49 2020-09-26 stsp free(pe->data);
2197 417a6e49 2020-09-26 stsp got_pathlist_free(&refs);
2198 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2199 417a6e49 2020-09-26 stsp free((void *)pe->path);
2200 417a6e49 2020-09-26 stsp free(pe->data);
2202 417a6e49 2020-09-26 stsp got_pathlist_free(&symrefs);
2203 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_branches);
2204 417a6e49 2020-09-26 stsp got_pathlist_free(&wanted_refs);
2205 417a6e49 2020-09-26 stsp free(id_str);
2206 417a6e49 2020-09-26 stsp free(cwd);
2207 417a6e49 2020-09-26 stsp free(repo_path);
2208 417a6e49 2020-09-26 stsp free(pack_hash);
2209 417a6e49 2020-09-26 stsp free(proto);
2210 417a6e49 2020-09-26 stsp free(host);
2211 417a6e49 2020-09-26 stsp free(port);
2212 417a6e49 2020-09-26 stsp free(server_path);
2213 417a6e49 2020-09-26 stsp free(repo_name);
2214 417a6e49 2020-09-26 stsp return error;
2218 417a6e49 2020-09-26 stsp __dead static void
2219 417a6e49 2020-09-26 stsp usage_checkout(void)
2221 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2222 417a6e49 2020-09-26 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2226 417a6e49 2020-09-26 stsp static void
2227 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning(void)
2229 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: warning: could not create a reference "
2230 417a6e49 2020-09-26 stsp "to the work tree's base commit; the commit could be "
2231 417a6e49 2020-09-26 stsp "garbage-collected by Git; making the repository "
2232 417a6e49 2020-09-26 stsp "writable and running 'got update' will prevent this\n",
2233 417a6e49 2020-09-26 stsp getprogname());
2236 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg {
2237 417a6e49 2020-09-26 stsp const char *worktree_path;
2238 417a6e49 2020-09-26 stsp int had_base_commit_ref_error;
2241 417a6e49 2020-09-26 stsp static const struct got_error *
2242 417a6e49 2020-09-26 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2244 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg *a = arg;
2246 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2247 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2248 417a6e49 2020-09-26 stsp return NULL;
2250 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2251 417a6e49 2020-09-26 stsp a->had_base_commit_ref_error = 1;
2252 417a6e49 2020-09-26 stsp return NULL;
2255 417a6e49 2020-09-26 stsp while (path[0] == '/')
2258 417a6e49 2020-09-26 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2259 417a6e49 2020-09-26 stsp return NULL;
2262 417a6e49 2020-09-26 stsp static const struct got_error *
2263 417a6e49 2020-09-26 stsp check_cancelled(void *arg)
2265 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
2266 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
2267 417a6e49 2020-09-26 stsp return NULL;
2270 417a6e49 2020-09-26 stsp static const struct got_error *
2271 417a6e49 2020-09-26 stsp check_linear_ancestry(struct got_object_id *commit_id,
2272 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2273 417a6e49 2020-09-26 stsp struct got_repository *repo)
2275 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2276 417a6e49 2020-09-26 stsp struct got_object_id *yca_id;
2278 417a6e49 2020-09-26 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2279 417a6e49 2020-09-26 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2281 417a6e49 2020-09-26 stsp return err;
2283 417a6e49 2020-09-26 stsp if (yca_id == NULL)
2284 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2287 417a6e49 2020-09-26 stsp * Require a straight line of history between the target commit
2288 417a6e49 2020-09-26 stsp * and the work tree's base commit.
2290 417a6e49 2020-09-26 stsp * Non-linear situations such as this require a rebase:
2292 417a6e49 2020-09-26 stsp * (commit) D F (base_commit)
2300 417a6e49 2020-09-26 stsp * 'got update' only handles linear cases:
2301 417a6e49 2020-09-26 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2302 417a6e49 2020-09-26 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2304 417a6e49 2020-09-26 stsp if (allow_forwards_in_time_only) {
2305 417a6e49 2020-09-26 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2306 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2307 417a6e49 2020-09-26 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2308 417a6e49 2020-09-26 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2309 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ANCESTRY);
2311 417a6e49 2020-09-26 stsp free(yca_id);
2312 417a6e49 2020-09-26 stsp return NULL;
2315 417a6e49 2020-09-26 stsp static const struct got_error *
2316 417a6e49 2020-09-26 stsp check_same_branch(struct got_object_id *commit_id,
2317 417a6e49 2020-09-26 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2318 417a6e49 2020-09-26 stsp struct got_repository *repo)
2320 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2321 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
2322 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
2323 417a6e49 2020-09-26 stsp int is_same_branch = 0;
2325 417a6e49 2020-09-26 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2327 417a6e49 2020-09-26 stsp goto done;
2329 417a6e49 2020-09-26 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2330 417a6e49 2020-09-26 stsp is_same_branch = 1;
2331 417a6e49 2020-09-26 stsp goto done;
2333 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2334 417a6e49 2020-09-26 stsp is_same_branch = 1;
2335 417a6e49 2020-09-26 stsp goto done;
2338 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
2340 417a6e49 2020-09-26 stsp goto done;
2342 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2343 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2345 417a6e49 2020-09-26 stsp goto done;
2347 417a6e49 2020-09-26 stsp for (;;) {
2348 417a6e49 2020-09-26 stsp struct got_object_id *id;
2349 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2350 417a6e49 2020-09-26 stsp check_cancelled, NULL);
2351 417a6e49 2020-09-26 stsp if (err) {
2352 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2353 417a6e49 2020-09-26 stsp err = NULL;
2358 417a6e49 2020-09-26 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2360 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2361 417a6e49 2020-09-26 stsp is_same_branch = 1;
2367 417a6e49 2020-09-26 stsp if (graph)
2368 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
2369 417a6e49 2020-09-26 stsp free(head_commit_id);
2370 417a6e49 2020-09-26 stsp if (!err && !is_same_branch)
2371 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_ANCESTRY);
2372 417a6e49 2020-09-26 stsp return err;
2375 417a6e49 2020-09-26 stsp static const struct got_error *
2376 417a6e49 2020-09-26 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2378 417a6e49 2020-09-26 stsp static char msg[512];
2379 417a6e49 2020-09-26 stsp const char *branch_name;
2381 417a6e49 2020-09-26 stsp if (got_ref_is_symbolic(ref))
2382 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(ref);
2384 417a6e49 2020-09-26 stsp branch_name = got_ref_get_name(ref);
2386 417a6e49 2020-09-26 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2387 417a6e49 2020-09-26 stsp branch_name += 11;
2389 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2390 417a6e49 2020-09-26 stsp "target commit is not contained in branch '%s'; "
2391 417a6e49 2020-09-26 stsp "the branch to use must be specified with -b; "
2392 417a6e49 2020-09-26 stsp "if necessary a new branch can be created for "
2393 417a6e49 2020-09-26 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2394 417a6e49 2020-09-26 stsp branch_name, commit_id_str);
2396 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2399 417a6e49 2020-09-26 stsp static const struct got_error *
2400 417a6e49 2020-09-26 stsp cmd_checkout(int argc, char *argv[])
2402 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2403 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2404 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2405 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2406 417a6e49 2020-09-26 stsp char *repo_path = NULL;
2407 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2408 417a6e49 2020-09-26 stsp const char *path_prefix = "";
2409 417a6e49 2020-09-26 stsp const char *branch_name = GOT_REF_HEAD;
2410 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2411 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
2412 417a6e49 2020-09-26 stsp int ch, same_path_prefix, allow_nonempty = 0;
2413 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2414 417a6e49 2020-09-26 stsp struct got_checkout_progress_arg cpa;
2416 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2418 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2419 417a6e49 2020-09-26 stsp switch (ch) {
2421 417a6e49 2020-09-26 stsp branch_name = optarg;
2424 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2425 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2426 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2429 417a6e49 2020-09-26 stsp allow_nonempty = 1;
2432 417a6e49 2020-09-26 stsp path_prefix = optarg;
2435 417a6e49 2020-09-26 stsp usage_checkout();
2436 417a6e49 2020-09-26 stsp /* NOTREACHED */
2440 417a6e49 2020-09-26 stsp argc -= optind;
2441 417a6e49 2020-09-26 stsp argv += optind;
2443 417a6e49 2020-09-26 stsp #ifndef PROFILE
2444 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2445 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2446 417a6e49 2020-09-26 stsp err(1, "pledge");
2448 417a6e49 2020-09-26 stsp if (argc == 1) {
2449 417a6e49 2020-09-26 stsp char *base, *dotgit;
2450 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2451 417a6e49 2020-09-26 stsp if (repo_path == NULL)
2452 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath", argv[0]);
2453 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
2454 417a6e49 2020-09-26 stsp if (cwd == NULL) {
2455 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2456 417a6e49 2020-09-26 stsp goto done;
2458 417a6e49 2020-09-26 stsp if (path_prefix[0])
2459 417a6e49 2020-09-26 stsp path = strdup(path_prefix);
2461 417a6e49 2020-09-26 stsp path = strdup(repo_path);
2462 417a6e49 2020-09-26 stsp if (path == NULL) {
2463 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2464 417a6e49 2020-09-26 stsp goto done;
2466 417a6e49 2020-09-26 stsp base = basename(path);
2467 417a6e49 2020-09-26 stsp if (base == NULL) {
2468 417a6e49 2020-09-26 stsp error = got_error_from_errno2("basename", path);
2469 417a6e49 2020-09-26 stsp goto done;
2471 417a6e49 2020-09-26 stsp dotgit = strstr(base, ".git");
2472 417a6e49 2020-09-26 stsp if (dotgit)
2473 417a6e49 2020-09-26 stsp *dotgit = '\0';
2474 417a6e49 2020-09-26 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2475 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
2476 417a6e49 2020-09-26 stsp goto done;
2478 417a6e49 2020-09-26 stsp } else if (argc == 2) {
2479 417a6e49 2020-09-26 stsp repo_path = realpath(argv[0], NULL);
2480 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
2481 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath", argv[0]);
2482 417a6e49 2020-09-26 stsp goto done;
2484 417a6e49 2020-09-26 stsp worktree_path = realpath(argv[1], NULL);
2485 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2486 417a6e49 2020-09-26 stsp if (errno != ENOENT) {
2487 417a6e49 2020-09-26 stsp error = got_error_from_errno2("realpath",
2489 417a6e49 2020-09-26 stsp goto done;
2491 417a6e49 2020-09-26 stsp worktree_path = strdup(argv[1]);
2492 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2493 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
2494 417a6e49 2020-09-26 stsp goto done;
2498 417a6e49 2020-09-26 stsp usage_checkout();
2500 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
2501 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(worktree_path);
2503 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
2504 417a6e49 2020-09-26 stsp if (error != NULL)
2505 417a6e49 2020-09-26 stsp goto done;
2507 417a6e49 2020-09-26 stsp /* Pre-create work tree path for unveil(2) */
2508 417a6e49 2020-09-26 stsp error = got_path_mkdir(worktree_path);
2509 417a6e49 2020-09-26 stsp if (error) {
2510 417a6e49 2020-09-26 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2511 417a6e49 2020-09-26 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2512 417a6e49 2020-09-26 stsp goto done;
2513 417a6e49 2020-09-26 stsp if (!allow_nonempty &&
2514 417a6e49 2020-09-26 stsp !got_path_dir_is_empty(worktree_path)) {
2515 417a6e49 2020-09-26 stsp error = got_error_path(worktree_path,
2516 417a6e49 2020-09-26 stsp GOT_ERR_DIR_NOT_EMPTY);
2517 417a6e49 2020-09-26 stsp goto done;
2521 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2522 417a6e49 2020-09-26 stsp if (error)
2523 417a6e49 2020-09-26 stsp goto done;
2525 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2526 417a6e49 2020-09-26 stsp if (error != NULL)
2527 417a6e49 2020-09-26 stsp goto done;
2529 417a6e49 2020-09-26 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2530 417a6e49 2020-09-26 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2531 417a6e49 2020-09-26 stsp goto done;
2533 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2534 417a6e49 2020-09-26 stsp if (error != NULL)
2535 417a6e49 2020-09-26 stsp goto done;
2537 417a6e49 2020-09-26 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2538 417a6e49 2020-09-26 stsp path_prefix);
2539 417a6e49 2020-09-26 stsp if (error != NULL)
2540 417a6e49 2020-09-26 stsp goto done;
2541 417a6e49 2020-09-26 stsp if (!same_path_prefix) {
2542 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2543 417a6e49 2020-09-26 stsp goto done;
2546 417a6e49 2020-09-26 stsp if (commit_id_str) {
2547 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
2548 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2549 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2550 417a6e49 2020-09-26 stsp if (error)
2551 417a6e49 2020-09-26 stsp goto done;
2552 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2553 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2554 417a6e49 2020-09-26 stsp if (error != NULL) {
2555 417a6e49 2020-09-26 stsp free(commit_id);
2556 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2557 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2558 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2560 417a6e49 2020-09-26 stsp goto done;
2562 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2563 417a6e49 2020-09-26 stsp if (error) {
2564 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY) {
2565 417a6e49 2020-09-26 stsp error = checkout_ancestry_error(
2566 417a6e49 2020-09-26 stsp head_ref, commit_id_str);
2568 417a6e49 2020-09-26 stsp goto done;
2570 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2571 417a6e49 2020-09-26 stsp commit_id);
2572 417a6e49 2020-09-26 stsp free(commit_id);
2573 417a6e49 2020-09-26 stsp if (error)
2574 417a6e49 2020-09-26 stsp goto done;
2577 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
2578 417a6e49 2020-09-26 stsp if (error)
2579 417a6e49 2020-09-26 stsp goto done;
2580 417a6e49 2020-09-26 stsp cpa.worktree_path = worktree_path;
2581 417a6e49 2020-09-26 stsp cpa.had_base_commit_ref_error = 0;
2582 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2583 417a6e49 2020-09-26 stsp checkout_progress, &cpa, check_cancelled, NULL);
2584 417a6e49 2020-09-26 stsp if (error != NULL)
2585 417a6e49 2020-09-26 stsp goto done;
2587 417a6e49 2020-09-26 stsp printf("Now shut up and hack\n");
2588 417a6e49 2020-09-26 stsp if (cpa.had_base_commit_ref_error)
2589 417a6e49 2020-09-26 stsp show_worktree_base_ref_warning();
2591 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2592 417a6e49 2020-09-26 stsp free(commit_id_str);
2593 417a6e49 2020-09-26 stsp free(repo_path);
2594 417a6e49 2020-09-26 stsp free(worktree_path);
2595 417a6e49 2020-09-26 stsp free(cwd);
2596 417a6e49 2020-09-26 stsp free(path);
2597 417a6e49 2020-09-26 stsp return error;
2600 417a6e49 2020-09-26 stsp struct got_update_progress_arg {
2601 417a6e49 2020-09-26 stsp int did_something;
2602 417a6e49 2020-09-26 stsp int conflicts;
2603 417a6e49 2020-09-26 stsp int obstructed;
2604 417a6e49 2020-09-26 stsp int not_updated;
2608 417a6e49 2020-09-26 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2610 417a6e49 2020-09-26 stsp if (!upa->did_something)
2613 417a6e49 2020-09-26 stsp if (upa->conflicts > 0)
2614 417a6e49 2020-09-26 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2615 417a6e49 2020-09-26 stsp if (upa->obstructed > 0)
2616 417a6e49 2020-09-26 stsp printf("File paths obstructed by a non-regular file: %d\n",
2617 417a6e49 2020-09-26 stsp upa->obstructed);
2618 417a6e49 2020-09-26 stsp if (upa->not_updated > 0)
2619 417a6e49 2020-09-26 stsp printf("Files not updated because of existing merge "
2620 417a6e49 2020-09-26 stsp "conflicts: %d\n", upa->not_updated);
2623 417a6e49 2020-09-26 stsp __dead static void
2624 417a6e49 2020-09-26 stsp usage_update(void)
2626 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2627 417a6e49 2020-09-26 stsp getprogname());
2631 417a6e49 2020-09-26 stsp static const struct got_error *
2632 417a6e49 2020-09-26 stsp update_progress(void *arg, unsigned char status, const char *path)
2634 417a6e49 2020-09-26 stsp struct got_update_progress_arg *upa = arg;
2636 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_EXISTS ||
2637 417a6e49 2020-09-26 stsp status == GOT_STATUS_BASE_REF_ERR)
2638 417a6e49 2020-09-26 stsp return NULL;
2640 417a6e49 2020-09-26 stsp upa->did_something = 1;
2642 417a6e49 2020-09-26 stsp /* Base commit bump happens silently. */
2643 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_BUMP_BASE)
2644 417a6e49 2020-09-26 stsp return NULL;
2646 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CONFLICT)
2647 417a6e49 2020-09-26 stsp upa->conflicts++;
2648 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_OBSTRUCTED)
2649 417a6e49 2020-09-26 stsp upa->obstructed++;
2650 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2651 417a6e49 2020-09-26 stsp upa->not_updated++;
2653 417a6e49 2020-09-26 stsp while (path[0] == '/')
2655 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
2656 417a6e49 2020-09-26 stsp return NULL;
2659 417a6e49 2020-09-26 stsp static const struct got_error *
2660 417a6e49 2020-09-26 stsp switch_head_ref(struct got_reference *head_ref,
2661 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2662 417a6e49 2020-09-26 stsp struct got_repository *repo)
2664 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2665 417a6e49 2020-09-26 stsp char *base_id_str;
2666 417a6e49 2020-09-26 stsp int ref_has_moved = 0;
2668 417a6e49 2020-09-26 stsp /* Trivial case: switching between two different references. */
2669 417a6e49 2020-09-26 stsp if (strcmp(got_ref_get_name(head_ref),
2670 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2671 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n",
2672 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree),
2673 417a6e49 2020-09-26 stsp got_ref_get_name(head_ref));
2674 417a6e49 2020-09-26 stsp return got_worktree_set_head_ref(worktree, head_ref);
2677 417a6e49 2020-09-26 stsp err = check_linear_ancestry(commit_id,
2678 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2679 417a6e49 2020-09-26 stsp if (err) {
2680 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_ANCESTRY)
2681 417a6e49 2020-09-26 stsp return err;
2682 417a6e49 2020-09-26 stsp ref_has_moved = 1;
2684 417a6e49 2020-09-26 stsp if (!ref_has_moved)
2685 417a6e49 2020-09-26 stsp return NULL;
2687 417a6e49 2020-09-26 stsp /* Switching to a rebased branch with the same reference name. */
2688 417a6e49 2020-09-26 stsp err = got_object_id_str(&base_id_str,
2689 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
2691 417a6e49 2020-09-26 stsp return err;
2692 417a6e49 2020-09-26 stsp printf("Reference %s now points at a different branch\n",
2693 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2694 417a6e49 2020-09-26 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2695 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
2696 417a6e49 2020-09-26 stsp return NULL;
2699 417a6e49 2020-09-26 stsp static const struct got_error *
2700 417a6e49 2020-09-26 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2702 417a6e49 2020-09-26 stsp const struct got_error *err;
2703 417a6e49 2020-09-26 stsp int in_progress;
2705 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2707 417a6e49 2020-09-26 stsp return err;
2708 417a6e49 2020-09-26 stsp if (in_progress)
2709 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_REBASING);
2711 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2713 417a6e49 2020-09-26 stsp return err;
2714 417a6e49 2020-09-26 stsp if (in_progress)
2715 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2717 417a6e49 2020-09-26 stsp return NULL;
2720 417a6e49 2020-09-26 stsp static const struct got_error *
2721 417a6e49 2020-09-26 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2722 417a6e49 2020-09-26 stsp char *argv[], struct got_worktree *worktree)
2724 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2725 417a6e49 2020-09-26 stsp char *path;
2728 417a6e49 2020-09-26 stsp if (argc == 0) {
2729 417a6e49 2020-09-26 stsp path = strdup("");
2730 417a6e49 2020-09-26 stsp if (path == NULL)
2731 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2732 417a6e49 2020-09-26 stsp return got_pathlist_append(paths, path, NULL);
2735 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
2736 417a6e49 2020-09-26 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2739 417a6e49 2020-09-26 stsp err = got_pathlist_append(paths, path, NULL);
2740 417a6e49 2020-09-26 stsp if (err) {
2741 417a6e49 2020-09-26 stsp free(path);
2746 417a6e49 2020-09-26 stsp return err;
2749 417a6e49 2020-09-26 stsp static const struct got_error *
2750 417a6e49 2020-09-26 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2751 417a6e49 2020-09-26 stsp const char *cmdname, const char *path)
2753 417a6e49 2020-09-26 stsp const struct got_error *err;
2754 417a6e49 2020-09-26 stsp struct got_repository *repo;
2755 417a6e49 2020-09-26 stsp static char msg[512];
2757 417a6e49 2020-09-26 stsp err = got_repo_open(&repo, path, NULL);
2759 417a6e49 2020-09-26 stsp return orig_err;
2761 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
2762 417a6e49 2020-09-26 stsp "'got %s' needs a work tree in addition to a git repository\n"
2763 417a6e49 2020-09-26 stsp "Work trees can be checked out from this Git repository with "
2764 417a6e49 2020-09-26 stsp "'got checkout'.\n"
2765 417a6e49 2020-09-26 stsp "The got(1) manual page contains more information.", cmdname);
2766 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2767 417a6e49 2020-09-26 stsp got_repo_close(repo);
2768 417a6e49 2020-09-26 stsp return err;
2771 417a6e49 2020-09-26 stsp static const struct got_error *
2772 417a6e49 2020-09-26 stsp cmd_update(int argc, char *argv[])
2774 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
2775 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
2776 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
2777 417a6e49 2020-09-26 stsp char *worktree_path = NULL;
2778 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
2779 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
2780 417a6e49 2020-09-26 stsp const char *branch_name = NULL;
2781 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
2782 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
2783 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
2785 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
2787 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
2789 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2790 417a6e49 2020-09-26 stsp switch (ch) {
2792 417a6e49 2020-09-26 stsp branch_name = optarg;
2795 417a6e49 2020-09-26 stsp commit_id_str = strdup(optarg);
2796 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
2797 417a6e49 2020-09-26 stsp return got_error_from_errno("strdup");
2800 417a6e49 2020-09-26 stsp usage_update();
2801 417a6e49 2020-09-26 stsp /* NOTREACHED */
2805 417a6e49 2020-09-26 stsp argc -= optind;
2806 417a6e49 2020-09-26 stsp argv += optind;
2808 417a6e49 2020-09-26 stsp #ifndef PROFILE
2809 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2810 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
2811 417a6e49 2020-09-26 stsp err(1, "pledge");
2813 417a6e49 2020-09-26 stsp worktree_path = getcwd(NULL, 0);
2814 417a6e49 2020-09-26 stsp if (worktree_path == NULL) {
2815 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
2816 417a6e49 2020-09-26 stsp goto done;
2818 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, worktree_path);
2819 417a6e49 2020-09-26 stsp if (error) {
2820 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2821 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "update",
2822 417a6e49 2020-09-26 stsp worktree_path);
2823 417a6e49 2020-09-26 stsp goto done;
2826 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
2827 417a6e49 2020-09-26 stsp if (error)
2828 417a6e49 2020-09-26 stsp goto done;
2830 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2832 417a6e49 2020-09-26 stsp if (error != NULL)
2833 417a6e49 2020-09-26 stsp goto done;
2835 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2836 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
2837 417a6e49 2020-09-26 stsp if (error)
2838 417a6e49 2020-09-26 stsp goto done;
2840 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2841 417a6e49 2020-09-26 stsp if (error)
2842 417a6e49 2020-09-26 stsp goto done;
2844 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2845 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
2846 417a6e49 2020-09-26 stsp if (error != NULL)
2847 417a6e49 2020-09-26 stsp goto done;
2848 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
2849 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2850 417a6e49 2020-09-26 stsp if (error != NULL)
2851 417a6e49 2020-09-26 stsp goto done;
2852 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2853 417a6e49 2020-09-26 stsp if (error != NULL)
2854 417a6e49 2020-09-26 stsp goto done;
2856 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
2857 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2858 417a6e49 2020-09-26 stsp free(commit_id_str);
2859 417a6e49 2020-09-26 stsp commit_id_str = NULL;
2860 417a6e49 2020-09-26 stsp if (error)
2861 417a6e49 2020-09-26 stsp goto done;
2862 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
2863 417a6e49 2020-09-26 stsp if (error)
2864 417a6e49 2020-09-26 stsp goto done;
2867 417a6e49 2020-09-26 stsp if (branch_name) {
2868 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id;
2869 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
2870 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
2872 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2873 417a6e49 2020-09-26 stsp "switching between branches requires that "
2874 417a6e49 2020-09-26 stsp "the entire work tree gets updated");
2875 417a6e49 2020-09-26 stsp goto done;
2877 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2878 417a6e49 2020-09-26 stsp if (error)
2879 417a6e49 2020-09-26 stsp goto done;
2880 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2882 417a6e49 2020-09-26 stsp free(head_commit_id);
2883 417a6e49 2020-09-26 stsp if (error != NULL)
2884 417a6e49 2020-09-26 stsp goto done;
2885 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2886 417a6e49 2020-09-26 stsp if (error)
2887 417a6e49 2020-09-26 stsp goto done;
2888 417a6e49 2020-09-26 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2889 417a6e49 2020-09-26 stsp if (error)
2890 417a6e49 2020-09-26 stsp goto done;
2892 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id,
2893 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2894 417a6e49 2020-09-26 stsp if (error != NULL) {
2895 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
2896 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2897 417a6e49 2020-09-26 stsp goto done;
2899 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2900 417a6e49 2020-09-26 stsp if (error)
2901 417a6e49 2020-09-26 stsp goto done;
2904 417a6e49 2020-09-26 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2905 417a6e49 2020-09-26 stsp commit_id) != 0) {
2906 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2907 417a6e49 2020-09-26 stsp commit_id);
2908 417a6e49 2020-09-26 stsp if (error)
2909 417a6e49 2020-09-26 stsp goto done;
2912 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
2913 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2914 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
2915 417a6e49 2020-09-26 stsp if (error != NULL)
2916 417a6e49 2020-09-26 stsp goto done;
2918 417a6e49 2020-09-26 stsp if (upa.did_something)
2919 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
2921 417a6e49 2020-09-26 stsp printf("Already up-to-date\n");
2922 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
2924 417a6e49 2020-09-26 stsp free(worktree_path);
2925 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
2926 417a6e49 2020-09-26 stsp free((char *)pe->path);
2927 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
2928 417a6e49 2020-09-26 stsp free(commit_id);
2929 417a6e49 2020-09-26 stsp free(commit_id_str);
2930 417a6e49 2020-09-26 stsp return error;
2933 417a6e49 2020-09-26 stsp static const struct got_error *
2934 417a6e49 2020-09-26 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2935 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2936 417a6e49 2020-09-26 stsp struct got_repository *repo)
2938 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2939 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2941 417a6e49 2020-09-26 stsp if (blob_id1) {
2942 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2944 417a6e49 2020-09-26 stsp goto done;
2947 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2949 417a6e49 2020-09-26 stsp goto done;
2951 417a6e49 2020-09-26 stsp while (path[0] == '/')
2953 417a6e49 2020-09-26 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2954 417a6e49 2020-09-26 stsp ignore_whitespace, stdout);
2956 417a6e49 2020-09-26 stsp if (blob1)
2957 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
2958 417a6e49 2020-09-26 stsp got_object_blob_close(blob2);
2959 417a6e49 2020-09-26 stsp return err;
2962 417a6e49 2020-09-26 stsp static const struct got_error *
2963 417a6e49 2020-09-26 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2964 417a6e49 2020-09-26 stsp const char *path, int diff_context, int ignore_whitespace,
2965 417a6e49 2020-09-26 stsp struct got_repository *repo)
2967 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
2968 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2969 417a6e49 2020-09-26 stsp struct got_diff_blob_output_unidiff_arg arg;
2971 417a6e49 2020-09-26 stsp if (tree_id1) {
2972 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2974 417a6e49 2020-09-26 stsp goto done;
2977 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2979 417a6e49 2020-09-26 stsp goto done;
2981 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
2982 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
2983 417a6e49 2020-09-26 stsp arg.outfile = stdout;
2984 417a6e49 2020-09-26 stsp while (path[0] == '/')
2986 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2987 417a6e49 2020-09-26 stsp got_diff_blob_output_unidiff, &arg, 1);
2989 417a6e49 2020-09-26 stsp if (tree1)
2990 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
2991 417a6e49 2020-09-26 stsp if (tree2)
2992 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
2993 417a6e49 2020-09-26 stsp return err;
2996 417a6e49 2020-09-26 stsp static const struct got_error *
2997 417a6e49 2020-09-26 stsp get_changed_paths(struct got_pathlist_head *paths,
2998 417a6e49 2020-09-26 stsp struct got_commit_object *commit, struct got_repository *repo)
3000 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3001 417a6e49 2020-09-26 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3002 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3003 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3005 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3006 417a6e49 2020-09-26 stsp if (qid != NULL) {
3007 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit;
3008 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3011 417a6e49 2020-09-26 stsp return err;
3013 417a6e49 2020-09-26 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3014 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3018 417a6e49 2020-09-26 stsp if (tree_id1) {
3019 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3021 417a6e49 2020-09-26 stsp goto done;
3024 417a6e49 2020-09-26 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3025 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3027 417a6e49 2020-09-26 stsp goto done;
3029 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3030 417a6e49 2020-09-26 stsp got_diff_tree_collect_changed_paths, paths, 0);
3032 417a6e49 2020-09-26 stsp if (tree1)
3033 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
3034 417a6e49 2020-09-26 stsp if (tree2)
3035 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
3036 417a6e49 2020-09-26 stsp return err;
3039 417a6e49 2020-09-26 stsp static const struct got_error *
3040 417a6e49 2020-09-26 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3041 417a6e49 2020-09-26 stsp const char *path, int diff_context, struct got_repository *repo)
3043 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3044 417a6e49 2020-09-26 stsp struct got_commit_object *pcommit = NULL;
3045 417a6e49 2020-09-26 stsp char *id_str1 = NULL, *id_str2 = NULL;
3046 417a6e49 2020-09-26 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3047 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3049 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3050 417a6e49 2020-09-26 stsp if (qid != NULL) {
3051 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&pcommit, repo,
3054 417a6e49 2020-09-26 stsp return err;
3057 417a6e49 2020-09-26 stsp if (path && path[0] != '\0') {
3058 417a6e49 2020-09-26 stsp int obj_type;
3059 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3061 417a6e49 2020-09-26 stsp goto done;
3062 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3063 417a6e49 2020-09-26 stsp if (err) {
3064 417a6e49 2020-09-26 stsp free(obj_id2);
3065 417a6e49 2020-09-26 stsp goto done;
3067 417a6e49 2020-09-26 stsp if (pcommit) {
3068 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&obj_id1, repo,
3069 417a6e49 2020-09-26 stsp qid->id, path);
3070 417a6e49 2020-09-26 stsp if (err) {
3071 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3072 417a6e49 2020-09-26 stsp free(obj_id2);
3073 417a6e49 2020-09-26 stsp goto done;
3076 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3077 417a6e49 2020-09-26 stsp if (err) {
3078 417a6e49 2020-09-26 stsp free(obj_id2);
3079 417a6e49 2020-09-26 stsp goto done;
3083 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3084 417a6e49 2020-09-26 stsp if (err) {
3085 417a6e49 2020-09-26 stsp free(obj_id2);
3086 417a6e49 2020-09-26 stsp goto done;
3088 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3089 417a6e49 2020-09-26 stsp switch (obj_type) {
3090 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
3091 417a6e49 2020-09-26 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3094 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
3095 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3099 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3102 417a6e49 2020-09-26 stsp free(obj_id1);
3103 417a6e49 2020-09-26 stsp free(obj_id2);
3105 417a6e49 2020-09-26 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3106 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str2, obj_id2);
3108 417a6e49 2020-09-26 stsp goto done;
3109 417a6e49 2020-09-26 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3110 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str1, obj_id1);
3112 417a6e49 2020-09-26 stsp goto done;
3113 417a6e49 2020-09-26 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3114 417a6e49 2020-09-26 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3117 417a6e49 2020-09-26 stsp free(id_str1);
3118 417a6e49 2020-09-26 stsp free(id_str2);
3119 417a6e49 2020-09-26 stsp if (pcommit)
3120 417a6e49 2020-09-26 stsp got_object_commit_close(pcommit);
3121 417a6e49 2020-09-26 stsp return err;
3124 417a6e49 2020-09-26 stsp static char *
3125 417a6e49 2020-09-26 stsp get_datestr(time_t *time, char *datebuf)
3127 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
3128 417a6e49 2020-09-26 stsp char *p, *s;
3130 417a6e49 2020-09-26 stsp tm = gmtime_r(time, &mytm);
3131 417a6e49 2020-09-26 stsp if (tm == NULL)
3132 417a6e49 2020-09-26 stsp return NULL;
3133 417a6e49 2020-09-26 stsp s = asctime_r(tm, datebuf);
3134 417a6e49 2020-09-26 stsp if (s == NULL)
3135 417a6e49 2020-09-26 stsp return NULL;
3136 417a6e49 2020-09-26 stsp p = strchr(s, '\n');
3138 417a6e49 2020-09-26 stsp *p = '\0';
3142 417a6e49 2020-09-26 stsp static const struct got_error *
3143 417a6e49 2020-09-26 stsp match_logmsg(int *have_match, struct got_object_id *id,
3144 417a6e49 2020-09-26 stsp struct got_commit_object *commit, regex_t *regex)
3146 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3147 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3148 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
3150 417a6e49 2020-09-26 stsp *have_match = 0;
3152 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3154 417a6e49 2020-09-26 stsp return err;
3156 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg, commit);
3158 417a6e49 2020-09-26 stsp goto done;
3160 417a6e49 2020-09-26 stsp if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3161 417a6e49 2020-09-26 stsp *have_match = 1;
3163 417a6e49 2020-09-26 stsp free(id_str);
3164 417a6e49 2020-09-26 stsp free(logmsg);
3165 417a6e49 2020-09-26 stsp return err;
3168 417a6e49 2020-09-26 stsp static void
3169 417a6e49 2020-09-26 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3170 417a6e49 2020-09-26 stsp regex_t *regex)
3172 417a6e49 2020-09-26 stsp regmatch_t regmatch;
3173 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3175 417a6e49 2020-09-26 stsp *have_match = 0;
3177 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3178 417a6e49 2020-09-26 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3179 417a6e49 2020-09-26 stsp *have_match = 1;
3185 417a6e49 2020-09-26 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3187 417a6e49 2020-09-26 stsp static const struct got_error *
3188 417a6e49 2020-09-26 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3189 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path,
3190 417a6e49 2020-09-26 stsp struct got_pathlist_head *changed_paths, int show_patch,
3191 417a6e49 2020-09-26 stsp int diff_context, struct got_reflist_head *refs)
3193 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3194 417a6e49 2020-09-26 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3195 417a6e49 2020-09-26 stsp char datebuf[26];
3196 417a6e49 2020-09-26 stsp time_t committer_time;
3197 417a6e49 2020-09-26 stsp const char *author, *committer;
3198 417a6e49 2020-09-26 stsp char *refs_str = NULL;
3199 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
3201 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3203 417a6e49 2020-09-26 stsp const char *name;
3204 417a6e49 2020-09-26 stsp struct got_tag_object *tag = NULL;
3205 417a6e49 2020-09-26 stsp struct got_object_id *ref_id;
3208 417a6e49 2020-09-26 stsp name = got_ref_get_name(re->ref);
3209 417a6e49 2020-09-26 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3211 417a6e49 2020-09-26 stsp if (strncmp(name, "refs/", 5) == 0)
3212 417a6e49 2020-09-26 stsp name += 5;
3213 417a6e49 2020-09-26 stsp if (strncmp(name, "got/", 4) == 0)
3215 417a6e49 2020-09-26 stsp if (strncmp(name, "heads/", 6) == 0)
3216 417a6e49 2020-09-26 stsp name += 6;
3217 417a6e49 2020-09-26 stsp if (strncmp(name, "remotes/", 8) == 0) {
3218 417a6e49 2020-09-26 stsp name += 8;
3219 417a6e49 2020-09-26 stsp s = strstr(name, "/" GOT_REF_HEAD);
3220 417a6e49 2020-09-26 stsp if (s != NULL && s[strlen(s)] == '\0')
3223 417a6e49 2020-09-26 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3225 417a6e49 2020-09-26 stsp return err;
3226 417a6e49 2020-09-26 stsp if (strncmp(name, "tags/", 5) == 0) {
3227 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3228 417a6e49 2020-09-26 stsp if (err) {
3229 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3230 417a6e49 2020-09-26 stsp free(ref_id);
3231 417a6e49 2020-09-26 stsp return err;
3233 417a6e49 2020-09-26 stsp /* Ref points at something other than a tag. */
3234 417a6e49 2020-09-26 stsp err = NULL;
3235 417a6e49 2020-09-26 stsp tag = NULL;
3238 417a6e49 2020-09-26 stsp cmp = got_object_id_cmp(tag ?
3239 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3240 417a6e49 2020-09-26 stsp free(ref_id);
3242 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3243 417a6e49 2020-09-26 stsp if (cmp != 0)
3245 417a6e49 2020-09-26 stsp s = refs_str;
3246 417a6e49 2020-09-26 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3247 417a6e49 2020-09-26 stsp name) == -1) {
3248 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3250 417a6e49 2020-09-26 stsp return err;
3254 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
3256 417a6e49 2020-09-26 stsp return err;
3258 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
3259 417a6e49 2020-09-26 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3260 417a6e49 2020-09-26 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3261 417a6e49 2020-09-26 stsp free(id_str);
3262 417a6e49 2020-09-26 stsp id_str = NULL;
3263 417a6e49 2020-09-26 stsp free(refs_str);
3264 417a6e49 2020-09-26 stsp refs_str = NULL;
3265 417a6e49 2020-09-26 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3266 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
3267 417a6e49 2020-09-26 stsp datestr = get_datestr(&committer_time, datebuf);
3268 417a6e49 2020-09-26 stsp if (datestr)
3269 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
3270 417a6e49 2020-09-26 stsp author = got_object_commit_get_author(commit);
3271 417a6e49 2020-09-26 stsp committer = got_object_commit_get_committer(commit);
3272 417a6e49 2020-09-26 stsp if (strcmp(author, committer) != 0)
3273 417a6e49 2020-09-26 stsp printf("via: %s\n", committer);
3274 417a6e49 2020-09-26 stsp if (got_object_commit_get_nparents(commit) > 1) {
3275 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
3276 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3277 417a6e49 2020-09-26 stsp int n = 1;
3278 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3279 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3280 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
3282 417a6e49 2020-09-26 stsp return err;
3283 417a6e49 2020-09-26 stsp printf("parent %d: %s\n", n++, id_str);
3284 417a6e49 2020-09-26 stsp free(id_str);
3288 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3290 417a6e49 2020-09-26 stsp return err;
3292 417a6e49 2020-09-26 stsp logmsg = logmsg0;
3294 417a6e49 2020-09-26 stsp line = strsep(&logmsg, "\n");
3296 417a6e49 2020-09-26 stsp printf(" %s\n", line);
3297 417a6e49 2020-09-26 stsp } while (line);
3298 417a6e49 2020-09-26 stsp free(logmsg0);
3300 417a6e49 2020-09-26 stsp if (changed_paths) {
3301 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3302 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3303 417a6e49 2020-09-26 stsp struct got_diff_changed_path *cp = pe->data;
3304 417a6e49 2020-09-26 stsp printf(" %c %s\n", cp->status, pe->path);
3306 417a6e49 2020-09-26 stsp printf("\n");
3308 417a6e49 2020-09-26 stsp if (show_patch) {
3309 417a6e49 2020-09-26 stsp err = print_patch(commit, id, path, diff_context, repo);
3310 417a6e49 2020-09-26 stsp if (err == 0)
3311 417a6e49 2020-09-26 stsp printf("\n");
3314 417a6e49 2020-09-26 stsp if (fflush(stdout) != 0 && err == NULL)
3315 417a6e49 2020-09-26 stsp err = got_error_from_errno("fflush");
3316 417a6e49 2020-09-26 stsp return err;
3319 417a6e49 2020-09-26 stsp static const struct got_error *
3320 417a6e49 2020-09-26 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3321 417a6e49 2020-09-26 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3322 417a6e49 2020-09-26 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3323 417a6e49 2020-09-26 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3325 417a6e49 2020-09-26 stsp const struct got_error *err;
3326 417a6e49 2020-09-26 stsp struct got_commit_graph *graph;
3327 417a6e49 2020-09-26 stsp regex_t regex;
3328 417a6e49 2020-09-26 stsp int have_match;
3329 417a6e49 2020-09-26 stsp struct got_object_id_queue reversed_commits;
3330 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
3331 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
3332 417a6e49 2020-09-26 stsp struct got_pathlist_head changed_paths;
3333 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
3335 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&reversed_commits);
3336 417a6e49 2020-09-26 stsp TAILQ_INIT(&changed_paths);
3338 417a6e49 2020-09-26 stsp if (search_pattern && regcomp(®ex, search_pattern,
3339 417a6e49 2020-09-26 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3340 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_REGEX, search_pattern);
3342 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3344 417a6e49 2020-09-26 stsp return err;
3345 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3346 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3348 417a6e49 2020-09-26 stsp goto done;
3349 417a6e49 2020-09-26 stsp for (;;) {
3350 417a6e49 2020-09-26 stsp struct got_object_id *id;
3352 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
3355 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3356 417a6e49 2020-09-26 stsp check_cancelled, NULL);
3357 417a6e49 2020-09-26 stsp if (err) {
3358 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3359 417a6e49 2020-09-26 stsp err = NULL;
3362 417a6e49 2020-09-26 stsp if (id == NULL)
3365 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
3369 417a6e49 2020-09-26 stsp if (show_changed_paths && !reverse_display_order) {
3370 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths, commit, repo);
3375 417a6e49 2020-09-26 stsp if (search_pattern) {
3376 417a6e49 2020-09-26 stsp err = match_logmsg(&have_match, id, commit, ®ex);
3377 417a6e49 2020-09-26 stsp if (err) {
3378 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3381 417a6e49 2020-09-26 stsp if (have_match == 0 && show_changed_paths)
3382 417a6e49 2020-09-26 stsp match_changed_paths(&have_match,
3383 417a6e49 2020-09-26 stsp &changed_paths, ®ex);
3384 417a6e49 2020-09-26 stsp if (have_match == 0) {
3385 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3386 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3387 417a6e49 2020-09-26 stsp free((char *)pe->path);
3388 417a6e49 2020-09-26 stsp free(pe->data);
3390 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3395 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3396 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, id);
3399 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3400 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3402 417a6e49 2020-09-26 stsp err = print_commit(commit, id, repo, path,
3403 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3404 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3405 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3409 417a6e49 2020-09-26 stsp if ((limit && --limit == 0) ||
3410 417a6e49 2020-09-26 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3413 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3414 417a6e49 2020-09-26 stsp free((char *)pe->path);
3415 417a6e49 2020-09-26 stsp free(pe->data);
3417 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3419 417a6e49 2020-09-26 stsp if (reverse_display_order) {
3420 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3421 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3424 417a6e49 2020-09-26 stsp if (show_changed_paths) {
3425 417a6e49 2020-09-26 stsp err = get_changed_paths(&changed_paths,
3426 417a6e49 2020-09-26 stsp commit, repo);
3430 417a6e49 2020-09-26 stsp err = print_commit(commit, qid->id, repo, path,
3431 417a6e49 2020-09-26 stsp show_changed_paths ? &changed_paths : NULL,
3432 417a6e49 2020-09-26 stsp show_patch, diff_context, refs);
3433 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3436 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3437 417a6e49 2020-09-26 stsp free((char *)pe->path);
3438 417a6e49 2020-09-26 stsp free(pe->data);
3440 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3444 417a6e49 2020-09-26 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3445 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3446 417a6e49 2020-09-26 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3447 417a6e49 2020-09-26 stsp got_object_qid_free(qid);
3449 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3450 417a6e49 2020-09-26 stsp free((char *)pe->path);
3451 417a6e49 2020-09-26 stsp free(pe->data);
3453 417a6e49 2020-09-26 stsp got_pathlist_free(&changed_paths);
3454 417a6e49 2020-09-26 stsp if (search_pattern)
3455 417a6e49 2020-09-26 stsp regfree(®ex);
3456 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
3457 417a6e49 2020-09-26 stsp return err;
3460 417a6e49 2020-09-26 stsp __dead static void
3461 417a6e49 2020-09-26 stsp usage_log(void)
3463 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3464 417a6e49 2020-09-26 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3465 417a6e49 2020-09-26 stsp "[-R] [path]\n", getprogname());
3469 417a6e49 2020-09-26 stsp static int
3470 417a6e49 2020-09-26 stsp get_default_log_limit(void)
3472 417a6e49 2020-09-26 stsp const char *got_default_log_limit;
3473 417a6e49 2020-09-26 stsp long long n;
3474 417a6e49 2020-09-26 stsp const char *errstr;
3476 417a6e49 2020-09-26 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3477 417a6e49 2020-09-26 stsp if (got_default_log_limit == NULL)
3479 417a6e49 2020-09-26 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3480 417a6e49 2020-09-26 stsp if (errstr != NULL)
3485 417a6e49 2020-09-26 stsp static const struct got_error *
3486 417a6e49 2020-09-26 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3487 417a6e49 2020-09-26 stsp struct got_repository *repo)
3489 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3490 417a6e49 2020-09-26 stsp struct got_reference *ref;
3492 417a6e49 2020-09-26 stsp *id = NULL;
3494 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3495 417a6e49 2020-09-26 stsp if (err == NULL) {
3496 417a6e49 2020-09-26 stsp int obj_type;
3497 417a6e49 2020-09-26 stsp err = got_ref_resolve(id, repo, ref);
3498 417a6e49 2020-09-26 stsp got_ref_close(ref);
3500 417a6e49 2020-09-26 stsp return err;
3501 417a6e49 2020-09-26 stsp err = got_object_get_type(&obj_type, repo, *id);
3503 417a6e49 2020-09-26 stsp return err;
3504 417a6e49 2020-09-26 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3505 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
3506 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, *id);
3508 417a6e49 2020-09-26 stsp return err;
3509 417a6e49 2020-09-26 stsp if (got_object_tag_get_object_type(tag) !=
3510 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT) {
3511 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3512 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3514 417a6e49 2020-09-26 stsp free(*id);
3515 417a6e49 2020-09-26 stsp *id = got_object_id_dup(
3516 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
3517 417a6e49 2020-09-26 stsp if (*id == NULL)
3518 417a6e49 2020-09-26 stsp err = got_error_from_errno(
3519 417a6e49 2020-09-26 stsp "got_object_id_dup");
3520 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
3522 417a6e49 2020-09-26 stsp return err;
3523 417a6e49 2020-09-26 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3524 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
3526 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3527 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
3530 417a6e49 2020-09-26 stsp return err;
3533 417a6e49 2020-09-26 stsp static const struct got_error *
3534 417a6e49 2020-09-26 stsp cmd_log(int argc, char *argv[])
3536 417a6e49 2020-09-26 stsp const struct got_error *error;
3537 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3538 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3539 417a6e49 2020-09-26 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3540 417a6e49 2020-09-26 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3541 417a6e49 2020-09-26 stsp const char *start_commit = NULL, *end_commit = NULL;
3542 417a6e49 2020-09-26 stsp const char *search_pattern = NULL;
3543 417a6e49 2020-09-26 stsp int diff_context = -1, ch;
3544 417a6e49 2020-09-26 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3545 417a6e49 2020-09-26 stsp int reverse_display_order = 0;
3546 417a6e49 2020-09-26 stsp const char *errstr;
3547 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
3549 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
3551 417a6e49 2020-09-26 stsp #ifndef PROFILE
3552 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3555 417a6e49 2020-09-26 stsp err(1, "pledge");
3558 417a6e49 2020-09-26 stsp limit = get_default_log_limit();
3560 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3561 417a6e49 2020-09-26 stsp switch (ch) {
3563 417a6e49 2020-09-26 stsp show_patch = 1;
3566 417a6e49 2020-09-26 stsp show_changed_paths = 1;
3569 417a6e49 2020-09-26 stsp start_commit = optarg;
3572 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3574 417a6e49 2020-09-26 stsp if (errstr != NULL)
3575 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3578 417a6e49 2020-09-26 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3579 417a6e49 2020-09-26 stsp if (errstr != NULL)
3580 417a6e49 2020-09-26 stsp err(1, "-l option %s", errstr);
3583 417a6e49 2020-09-26 stsp log_branches = 1;
3586 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3587 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3588 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3590 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3593 417a6e49 2020-09-26 stsp reverse_display_order = 1;
3596 417a6e49 2020-09-26 stsp search_pattern = optarg;
3599 417a6e49 2020-09-26 stsp end_commit = optarg;
3602 417a6e49 2020-09-26 stsp usage_log();
3603 417a6e49 2020-09-26 stsp /* NOTREACHED */
3607 417a6e49 2020-09-26 stsp argc -= optind;
3608 417a6e49 2020-09-26 stsp argv += optind;
3610 417a6e49 2020-09-26 stsp if (diff_context == -1)
3611 417a6e49 2020-09-26 stsp diff_context = 3;
3612 417a6e49 2020-09-26 stsp else if (!show_patch)
3613 417a6e49 2020-09-26 stsp errx(1, "-C requires -p");
3615 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3616 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3617 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3618 417a6e49 2020-09-26 stsp goto done;
3621 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3622 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
3623 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3624 417a6e49 2020-09-26 stsp goto done;
3625 417a6e49 2020-09-26 stsp error = NULL;
3628 417a6e49 2020-09-26 stsp if (argc == 0) {
3629 417a6e49 2020-09-26 stsp path = strdup("");
3630 417a6e49 2020-09-26 stsp if (path == NULL) {
3631 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3632 417a6e49 2020-09-26 stsp goto done;
3634 417a6e49 2020-09-26 stsp } else if (argc == 1) {
3635 417a6e49 2020-09-26 stsp if (worktree) {
3636 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
3638 417a6e49 2020-09-26 stsp if (error)
3639 417a6e49 2020-09-26 stsp goto done;
3641 417a6e49 2020-09-26 stsp path = strdup(argv[0]);
3642 417a6e49 2020-09-26 stsp if (path == NULL) {
3643 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3644 417a6e49 2020-09-26 stsp goto done;
3648 417a6e49 2020-09-26 stsp usage_log();
3650 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3651 417a6e49 2020-09-26 stsp repo_path = worktree ?
3652 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3654 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
3655 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
3656 417a6e49 2020-09-26 stsp goto done;
3659 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
3660 417a6e49 2020-09-26 stsp if (error != NULL)
3661 417a6e49 2020-09-26 stsp goto done;
3663 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3664 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3665 417a6e49 2020-09-26 stsp if (error)
3666 417a6e49 2020-09-26 stsp goto done;
3668 417a6e49 2020-09-26 stsp if (start_commit == NULL) {
3669 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
3670 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
3671 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
3672 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3673 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
3674 417a6e49 2020-09-26 stsp if (error != NULL)
3675 417a6e49 2020-09-26 stsp goto done;
3676 417a6e49 2020-09-26 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3677 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
3678 417a6e49 2020-09-26 stsp if (error != NULL)
3679 417a6e49 2020-09-26 stsp goto done;
3680 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
3681 417a6e49 2020-09-26 stsp start_id);
3682 417a6e49 2020-09-26 stsp if (error != NULL)
3683 417a6e49 2020-09-26 stsp goto done;
3684 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
3686 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3687 417a6e49 2020-09-26 stsp if (error != NULL)
3688 417a6e49 2020-09-26 stsp goto done;
3690 417a6e49 2020-09-26 stsp if (end_commit != NULL) {
3691 417a6e49 2020-09-26 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3692 417a6e49 2020-09-26 stsp if (error != NULL)
3693 417a6e49 2020-09-26 stsp goto done;
3696 417a6e49 2020-09-26 stsp if (worktree) {
3697 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3699 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s", prefix,
3700 417a6e49 2020-09-26 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3701 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
3702 417a6e49 2020-09-26 stsp goto done;
3704 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3707 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3708 417a6e49 2020-09-26 stsp if (error != NULL)
3709 417a6e49 2020-09-26 stsp goto done;
3710 417a6e49 2020-09-26 stsp if (in_repo_path) {
3711 417a6e49 2020-09-26 stsp free(path);
3712 417a6e49 2020-09-26 stsp path = in_repo_path;
3715 417a6e49 2020-09-26 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3716 417a6e49 2020-09-26 stsp if (error)
3717 417a6e49 2020-09-26 stsp goto done;
3719 417a6e49 2020-09-26 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3720 417a6e49 2020-09-26 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3721 417a6e49 2020-09-26 stsp reverse_display_order, &refs);
3723 417a6e49 2020-09-26 stsp free(path);
3724 417a6e49 2020-09-26 stsp free(repo_path);
3725 417a6e49 2020-09-26 stsp free(cwd);
3726 417a6e49 2020-09-26 stsp if (worktree)
3727 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
3728 417a6e49 2020-09-26 stsp if (repo) {
3729 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
3730 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
3731 417a6e49 2020-09-26 stsp if (error == NULL)
3732 417a6e49 2020-09-26 stsp error = repo_error;
3734 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
3735 417a6e49 2020-09-26 stsp return error;
3738 417a6e49 2020-09-26 stsp __dead static void
3739 417a6e49 2020-09-26 stsp usage_diff(void)
3741 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3742 417a6e49 2020-09-26 stsp "[-w] [object1 object2 | path]\n", getprogname());
3746 417a6e49 2020-09-26 stsp struct print_diff_arg {
3747 417a6e49 2020-09-26 stsp struct got_repository *repo;
3748 417a6e49 2020-09-26 stsp struct got_worktree *worktree;
3749 417a6e49 2020-09-26 stsp int diff_context;
3750 417a6e49 2020-09-26 stsp const char *id_str;
3751 417a6e49 2020-09-26 stsp int header_shown;
3752 417a6e49 2020-09-26 stsp int diff_staged;
3753 417a6e49 2020-09-26 stsp int ignore_whitespace;
3757 417a6e49 2020-09-26 stsp * Create a file which contains the target path of a symlink so we can feed
3758 417a6e49 2020-09-26 stsp * it as content to the diff engine.
3760 417a6e49 2020-09-26 stsp static const struct got_error *
3761 417a6e49 2020-09-26 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3762 417a6e49 2020-09-26 stsp const char *abspath)
3764 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3765 417a6e49 2020-09-26 stsp char target_path[PATH_MAX];
3766 417a6e49 2020-09-26 stsp ssize_t target_len, outlen;
3770 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3771 417a6e49 2020-09-26 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3772 417a6e49 2020-09-26 stsp if (target_len == -1)
3773 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlinkat", abspath);
3775 417a6e49 2020-09-26 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3776 417a6e49 2020-09-26 stsp if (target_len == -1)
3777 417a6e49 2020-09-26 stsp return got_error_from_errno2("readlink", abspath);
3780 417a6e49 2020-09-26 stsp *fd = got_opentempfd();
3781 417a6e49 2020-09-26 stsp if (*fd == -1)
3782 417a6e49 2020-09-26 stsp return got_error_from_errno("got_opentempfd");
3784 417a6e49 2020-09-26 stsp outlen = write(*fd, target_path, target_len);
3785 417a6e49 2020-09-26 stsp if (outlen == -1) {
3786 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_opentempfd");
3787 417a6e49 2020-09-26 stsp goto done;
3790 417a6e49 2020-09-26 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3791 417a6e49 2020-09-26 stsp err = got_error_from_errno2("lseek", abspath);
3792 417a6e49 2020-09-26 stsp goto done;
3795 417a6e49 2020-09-26 stsp if (err) {
3796 417a6e49 2020-09-26 stsp close(*fd);
3799 417a6e49 2020-09-26 stsp return err;
3802 417a6e49 2020-09-26 stsp static const struct got_error *
3803 417a6e49 2020-09-26 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3804 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
3805 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3806 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
3808 417a6e49 2020-09-26 stsp struct print_diff_arg *a = arg;
3809 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
3810 417a6e49 2020-09-26 stsp struct got_blob_object *blob1 = NULL;
3811 417a6e49 2020-09-26 stsp int fd = -1;
3812 417a6e49 2020-09-26 stsp FILE *f2 = NULL;
3813 417a6e49 2020-09-26 stsp char *abspath = NULL, *label1 = NULL;
3814 417a6e49 2020-09-26 stsp struct stat sb;
3816 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3817 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_MODIFY &&
3818 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_ADD &&
3819 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
3820 417a6e49 2020-09-26 stsp return NULL;
3822 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_DELETE)
3823 417a6e49 2020-09-26 stsp return NULL;
3824 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
3825 417a6e49 2020-09-26 stsp return got_error_set_errno(ENOENT, path);
3826 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_MODIFY &&
3827 417a6e49 2020-09-26 stsp status != GOT_STATUS_ADD &&
3828 417a6e49 2020-09-26 stsp status != GOT_STATUS_DELETE &&
3829 417a6e49 2020-09-26 stsp status != GOT_STATUS_CONFLICT)
3830 417a6e49 2020-09-26 stsp return NULL;
3833 417a6e49 2020-09-26 stsp if (!a->header_shown) {
3834 417a6e49 2020-09-26 stsp printf("diff %s %s%s\n", a->id_str,
3835 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree),
3836 417a6e49 2020-09-26 stsp a->diff_staged ? " (staged changes)" : "");
3837 417a6e49 2020-09-26 stsp a->header_shown = 1;
3840 417a6e49 2020-09-26 stsp if (a->diff_staged) {
3841 417a6e49 2020-09-26 stsp const char *label1 = NULL, *label2 = NULL;
3842 417a6e49 2020-09-26 stsp switch (staged_status) {
3843 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
3844 417a6e49 2020-09-26 stsp label1 = path;
3845 417a6e49 2020-09-26 stsp label2 = path;
3847 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
3848 417a6e49 2020-09-26 stsp label2 = path;
3850 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
3851 417a6e49 2020-09-26 stsp label1 = path;
3854 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_FILE_STATUS);
3856 417a6e49 2020-09-26 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3857 417a6e49 2020-09-26 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3858 417a6e49 2020-09-26 stsp a->repo, stdout);
3861 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
3862 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY) {
3863 417a6e49 2020-09-26 stsp char *id_str;
3864 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3867 417a6e49 2020-09-26 stsp goto done;
3868 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
3870 417a6e49 2020-09-26 stsp goto done;
3871 417a6e49 2020-09-26 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3872 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3873 417a6e49 2020-09-26 stsp free(id_str);
3874 417a6e49 2020-09-26 stsp goto done;
3876 417a6e49 2020-09-26 stsp free(id_str);
3877 417a6e49 2020-09-26 stsp } else if (status != GOT_STATUS_ADD) {
3878 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3880 417a6e49 2020-09-26 stsp goto done;
3883 417a6e49 2020-09-26 stsp if (status != GOT_STATUS_DELETE) {
3884 417a6e49 2020-09-26 stsp if (asprintf(&abspath, "%s/%s",
3885 417a6e49 2020-09-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3886 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
3887 417a6e49 2020-09-26 stsp goto done;
3890 417a6e49 2020-09-26 stsp if (dirfd != -1) {
3891 417a6e49 2020-09-26 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3892 417a6e49 2020-09-26 stsp if (fd == -1) {
3893 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3894 417a6e49 2020-09-26 stsp err = got_error_from_errno2("openat",
3896 417a6e49 2020-09-26 stsp goto done;
3898 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3899 417a6e49 2020-09-26 stsp de_name, abspath);
3901 417a6e49 2020-09-26 stsp goto done;
3904 417a6e49 2020-09-26 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3905 417a6e49 2020-09-26 stsp if (fd == -1) {
3906 417a6e49 2020-09-26 stsp if (errno != ELOOP) {
3907 417a6e49 2020-09-26 stsp err = got_error_from_errno2("open",
3909 417a6e49 2020-09-26 stsp goto done;
3911 417a6e49 2020-09-26 stsp err = get_symlink_target_file(&fd, dirfd,
3912 417a6e49 2020-09-26 stsp de_name, abspath);
3914 417a6e49 2020-09-26 stsp goto done;
3917 417a6e49 2020-09-26 stsp if (fstat(fd, &sb) == -1) {
3918 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fstat", abspath);
3919 417a6e49 2020-09-26 stsp goto done;
3921 417a6e49 2020-09-26 stsp f2 = fdopen(fd, "r");
3922 417a6e49 2020-09-26 stsp if (f2 == NULL) {
3923 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fdopen", abspath);
3924 417a6e49 2020-09-26 stsp goto done;
3928 417a6e49 2020-09-26 stsp sb.st_size = 0;
3930 417a6e49 2020-09-26 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3931 417a6e49 2020-09-26 stsp a->diff_context, a->ignore_whitespace, stdout);
3933 417a6e49 2020-09-26 stsp if (blob1)
3934 417a6e49 2020-09-26 stsp got_object_blob_close(blob1);
3935 417a6e49 2020-09-26 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3936 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
3937 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3938 417a6e49 2020-09-26 stsp err = got_error_from_errno("close");
3939 417a6e49 2020-09-26 stsp free(abspath);
3940 417a6e49 2020-09-26 stsp return err;
3943 417a6e49 2020-09-26 stsp static const struct got_error *
3944 417a6e49 2020-09-26 stsp cmd_diff(int argc, char *argv[])
3946 417a6e49 2020-09-26 stsp const struct got_error *error;
3947 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
3948 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
3949 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
3950 417a6e49 2020-09-26 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3951 417a6e49 2020-09-26 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3952 417a6e49 2020-09-26 stsp char *label1 = NULL, *label2 = NULL;
3953 417a6e49 2020-09-26 stsp int type1, type2;
3954 417a6e49 2020-09-26 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3955 417a6e49 2020-09-26 stsp const char *errstr;
3956 417a6e49 2020-09-26 stsp char *path = NULL;
3958 417a6e49 2020-09-26 stsp #ifndef PROFILE
3959 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3960 417a6e49 2020-09-26 stsp NULL) == -1)
3961 417a6e49 2020-09-26 stsp err(1, "pledge");
3964 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3965 417a6e49 2020-09-26 stsp switch (ch) {
3967 417a6e49 2020-09-26 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3969 417a6e49 2020-09-26 stsp if (errstr != NULL)
3970 417a6e49 2020-09-26 stsp err(1, "-C option %s", errstr);
3973 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
3974 417a6e49 2020-09-26 stsp if (repo_path == NULL)
3975 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
3977 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
3980 417a6e49 2020-09-26 stsp diff_staged = 1;
3983 417a6e49 2020-09-26 stsp ignore_whitespace = 1;
3986 417a6e49 2020-09-26 stsp usage_diff();
3987 417a6e49 2020-09-26 stsp /* NOTREACHED */
3991 417a6e49 2020-09-26 stsp argc -= optind;
3992 417a6e49 2020-09-26 stsp argv += optind;
3994 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
3995 417a6e49 2020-09-26 stsp if (cwd == NULL) {
3996 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
3997 417a6e49 2020-09-26 stsp goto done;
3999 417a6e49 2020-09-26 stsp if (argc <= 1) {
4000 417a6e49 2020-09-26 stsp if (repo_path)
4002 417a6e49 2020-09-26 stsp "-r option can't be used when diffing a work tree");
4003 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4004 417a6e49 2020-09-26 stsp if (error) {
4005 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4006 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "diff",
4008 417a6e49 2020-09-26 stsp goto done;
4010 417a6e49 2020-09-26 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4011 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4012 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4013 417a6e49 2020-09-26 stsp goto done;
4015 417a6e49 2020-09-26 stsp if (argc == 1) {
4016 417a6e49 2020-09-26 stsp error = got_worktree_resolve_path(&path, worktree,
4018 417a6e49 2020-09-26 stsp if (error)
4019 417a6e49 2020-09-26 stsp goto done;
4021 417a6e49 2020-09-26 stsp path = strdup("");
4022 417a6e49 2020-09-26 stsp if (path == NULL) {
4023 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4024 417a6e49 2020-09-26 stsp goto done;
4027 417a6e49 2020-09-26 stsp } else if (argc == 2) {
4028 417a6e49 2020-09-26 stsp if (diff_staged)
4029 417a6e49 2020-09-26 stsp errx(1, "-s option can't be used when diffing "
4030 417a6e49 2020-09-26 stsp "objects in repository");
4031 417a6e49 2020-09-26 stsp id_str1 = argv[0];
4032 417a6e49 2020-09-26 stsp id_str2 = argv[1];
4033 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4034 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4035 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4036 417a6e49 2020-09-26 stsp goto done;
4037 417a6e49 2020-09-26 stsp if (worktree) {
4038 417a6e49 2020-09-26 stsp repo_path = strdup(
4039 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
4040 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4041 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4042 417a6e49 2020-09-26 stsp goto done;
4045 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4046 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4047 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4048 417a6e49 2020-09-26 stsp goto done;
4053 417a6e49 2020-09-26 stsp usage_diff();
4055 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4056 417a6e49 2020-09-26 stsp free(repo_path);
4057 417a6e49 2020-09-26 stsp if (error != NULL)
4058 417a6e49 2020-09-26 stsp goto done;
4060 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4061 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4062 417a6e49 2020-09-26 stsp if (error)
4063 417a6e49 2020-09-26 stsp goto done;
4065 417a6e49 2020-09-26 stsp if (argc <= 1) {
4066 417a6e49 2020-09-26 stsp struct print_diff_arg arg;
4067 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4068 417a6e49 2020-09-26 stsp char *id_str;
4070 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4072 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
4073 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
4074 417a6e49 2020-09-26 stsp if (error)
4075 417a6e49 2020-09-26 stsp goto done;
4076 417a6e49 2020-09-26 stsp arg.repo = repo;
4077 417a6e49 2020-09-26 stsp arg.worktree = worktree;
4078 417a6e49 2020-09-26 stsp arg.diff_context = diff_context;
4079 417a6e49 2020-09-26 stsp arg.id_str = id_str;
4080 417a6e49 2020-09-26 stsp arg.header_shown = 0;
4081 417a6e49 2020-09-26 stsp arg.diff_staged = diff_staged;
4082 417a6e49 2020-09-26 stsp arg.ignore_whitespace = ignore_whitespace;
4084 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, path, NULL);
4085 417a6e49 2020-09-26 stsp if (error)
4086 417a6e49 2020-09-26 stsp goto done;
4088 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4089 417a6e49 2020-09-26 stsp &arg, check_cancelled, NULL);
4090 417a6e49 2020-09-26 stsp free(id_str);
4091 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4092 417a6e49 2020-09-26 stsp goto done;
4095 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4096 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4097 417a6e49 2020-09-26 stsp if (error)
4098 417a6e49 2020-09-26 stsp goto done;
4100 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4101 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4102 417a6e49 2020-09-26 stsp if (error)
4103 417a6e49 2020-09-26 stsp goto done;
4105 417a6e49 2020-09-26 stsp error = got_object_get_type(&type1, repo, id1);
4106 417a6e49 2020-09-26 stsp if (error)
4107 417a6e49 2020-09-26 stsp goto done;
4109 417a6e49 2020-09-26 stsp error = got_object_get_type(&type2, repo, id2);
4110 417a6e49 2020-09-26 stsp if (error)
4111 417a6e49 2020-09-26 stsp goto done;
4113 417a6e49 2020-09-26 stsp if (type1 != type2) {
4114 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4115 417a6e49 2020-09-26 stsp goto done;
4118 417a6e49 2020-09-26 stsp switch (type1) {
4119 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
4120 417a6e49 2020-09-26 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4121 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4123 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
4124 417a6e49 2020-09-26 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4125 417a6e49 2020-09-26 stsp diff_context, ignore_whitespace, repo, stdout);
4127 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
4128 417a6e49 2020-09-26 stsp printf("diff %s %s\n", label1, label2);
4129 417a6e49 2020-09-26 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4130 417a6e49 2020-09-26 stsp ignore_whitespace, repo, stdout);
4133 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4136 417a6e49 2020-09-26 stsp free(label1);
4137 417a6e49 2020-09-26 stsp free(label2);
4138 417a6e49 2020-09-26 stsp free(id1);
4139 417a6e49 2020-09-26 stsp free(id2);
4140 417a6e49 2020-09-26 stsp free(path);
4141 417a6e49 2020-09-26 stsp if (worktree)
4142 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4143 417a6e49 2020-09-26 stsp if (repo) {
4144 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4145 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4146 417a6e49 2020-09-26 stsp if (error == NULL)
4147 417a6e49 2020-09-26 stsp error = repo_error;
4149 417a6e49 2020-09-26 stsp return error;
4152 417a6e49 2020-09-26 stsp __dead static void
4153 417a6e49 2020-09-26 stsp usage_blame(void)
4155 417a6e49 2020-09-26 stsp fprintf(stderr,
4156 417a6e49 2020-09-26 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4157 417a6e49 2020-09-26 stsp getprogname());
4161 417a6e49 2020-09-26 stsp struct blame_line {
4162 417a6e49 2020-09-26 stsp int annotated;
4163 417a6e49 2020-09-26 stsp char *id_str;
4164 417a6e49 2020-09-26 stsp char *committer;
4165 417a6e49 2020-09-26 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4168 417a6e49 2020-09-26 stsp struct blame_cb_args {
4169 417a6e49 2020-09-26 stsp struct blame_line *lines;
4170 417a6e49 2020-09-26 stsp int nlines;
4171 417a6e49 2020-09-26 stsp int nlines_prec;
4172 417a6e49 2020-09-26 stsp int lineno_cur;
4173 417a6e49 2020-09-26 stsp off_t *line_offsets;
4175 417a6e49 2020-09-26 stsp struct got_repository *repo;
4178 417a6e49 2020-09-26 stsp static const struct got_error *
4179 417a6e49 2020-09-26 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4181 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4182 417a6e49 2020-09-26 stsp struct blame_cb_args *a = arg;
4183 417a6e49 2020-09-26 stsp struct blame_line *bline;
4184 417a6e49 2020-09-26 stsp char *line = NULL;
4185 417a6e49 2020-09-26 stsp size_t linesize = 0;
4186 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
4187 417a6e49 2020-09-26 stsp off_t offset;
4188 417a6e49 2020-09-26 stsp struct tm tm;
4189 417a6e49 2020-09-26 stsp time_t committer_time;
4191 417a6e49 2020-09-26 stsp if (nlines != a->nlines ||
4192 417a6e49 2020-09-26 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4193 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_RANGE);
4195 417a6e49 2020-09-26 stsp if (sigint_received)
4196 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4198 417a6e49 2020-09-26 stsp if (lineno == -1)
4199 417a6e49 2020-09-26 stsp return NULL; /* no change in this commit */
4201 417a6e49 2020-09-26 stsp /* Annotate this line. */
4202 417a6e49 2020-09-26 stsp bline = &a->lines[lineno - 1];
4203 417a6e49 2020-09-26 stsp if (bline->annotated)
4204 417a6e49 2020-09-26 stsp return NULL;
4205 417a6e49 2020-09-26 stsp err = got_object_id_str(&bline->id_str, id);
4207 417a6e49 2020-09-26 stsp return err;
4209 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4211 417a6e49 2020-09-26 stsp goto done;
4213 417a6e49 2020-09-26 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4214 417a6e49 2020-09-26 stsp if (bline->committer == NULL) {
4215 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
4216 417a6e49 2020-09-26 stsp goto done;
4219 417a6e49 2020-09-26 stsp committer_time = got_object_commit_get_committer_time(commit);
4220 417a6e49 2020-09-26 stsp if (localtime_r(&committer_time, &tm) == NULL)
4221 417a6e49 2020-09-26 stsp return got_error_from_errno("localtime_r");
4222 417a6e49 2020-09-26 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4223 417a6e49 2020-09-26 stsp &tm) >= sizeof(bline->datebuf)) {
4224 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_NO_SPACE);
4225 417a6e49 2020-09-26 stsp goto done;
4227 417a6e49 2020-09-26 stsp bline->annotated = 1;
4229 417a6e49 2020-09-26 stsp /* Print lines annotated so far. */
4230 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4231 417a6e49 2020-09-26 stsp if (!bline->annotated)
4232 417a6e49 2020-09-26 stsp goto done;
4234 417a6e49 2020-09-26 stsp offset = a->line_offsets[a->lineno_cur - 1];
4235 417a6e49 2020-09-26 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4236 417a6e49 2020-09-26 stsp err = got_error_from_errno("fseeko");
4237 417a6e49 2020-09-26 stsp goto done;
4240 417a6e49 2020-09-26 stsp while (bline->annotated) {
4241 417a6e49 2020-09-26 stsp char *smallerthan, *at, *nl, *committer;
4242 417a6e49 2020-09-26 stsp size_t len;
4244 417a6e49 2020-09-26 stsp if (getline(&line, &linesize, a->f) == -1) {
4245 417a6e49 2020-09-26 stsp if (ferror(a->f))
4246 417a6e49 2020-09-26 stsp err = got_error_from_errno("getline");
4250 417a6e49 2020-09-26 stsp committer = bline->committer;
4251 417a6e49 2020-09-26 stsp smallerthan = strchr(committer, '<');
4252 417a6e49 2020-09-26 stsp if (smallerthan && smallerthan[1] != '\0')
4253 417a6e49 2020-09-26 stsp committer = smallerthan + 1;
4254 417a6e49 2020-09-26 stsp at = strchr(committer, '@');
4256 417a6e49 2020-09-26 stsp *at = '\0';
4257 417a6e49 2020-09-26 stsp len = strlen(committer);
4258 417a6e49 2020-09-26 stsp if (len >= 9)
4259 417a6e49 2020-09-26 stsp committer[8] = '\0';
4261 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
4263 417a6e49 2020-09-26 stsp *nl = '\0';
4264 417a6e49 2020-09-26 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4265 417a6e49 2020-09-26 stsp bline->id_str, bline->datebuf, committer, line);
4267 417a6e49 2020-09-26 stsp a->lineno_cur++;
4268 417a6e49 2020-09-26 stsp bline = &a->lines[a->lineno_cur - 1];
4271 417a6e49 2020-09-26 stsp if (commit)
4272 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
4273 417a6e49 2020-09-26 stsp free(line);
4274 417a6e49 2020-09-26 stsp return err;
4277 417a6e49 2020-09-26 stsp static const struct got_error *
4278 417a6e49 2020-09-26 stsp cmd_blame(int argc, char *argv[])
4280 417a6e49 2020-09-26 stsp const struct got_error *error;
4281 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4282 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4283 417a6e49 2020-09-26 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4284 417a6e49 2020-09-26 stsp char *link_target = NULL;
4285 417a6e49 2020-09-26 stsp struct got_object_id *obj_id = NULL;
4286 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4287 417a6e49 2020-09-26 stsp struct got_blob_object *blob = NULL;
4288 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4289 417a6e49 2020-09-26 stsp struct blame_cb_args bca;
4290 417a6e49 2020-09-26 stsp int ch, obj_type, i;
4291 417a6e49 2020-09-26 stsp size_t filesize;
4293 417a6e49 2020-09-26 stsp memset(&bca, 0, sizeof(bca));
4295 417a6e49 2020-09-26 stsp #ifndef PROFILE
4296 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4297 417a6e49 2020-09-26 stsp NULL) == -1)
4298 417a6e49 2020-09-26 stsp err(1, "pledge");
4301 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4302 417a6e49 2020-09-26 stsp switch (ch) {
4304 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4307 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4308 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4309 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4311 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4314 417a6e49 2020-09-26 stsp usage_blame();
4315 417a6e49 2020-09-26 stsp /* NOTREACHED */
4319 417a6e49 2020-09-26 stsp argc -= optind;
4320 417a6e49 2020-09-26 stsp argv += optind;
4322 417a6e49 2020-09-26 stsp if (argc == 1)
4323 417a6e49 2020-09-26 stsp path = argv[0];
4325 417a6e49 2020-09-26 stsp usage_blame();
4327 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4328 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4329 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4330 417a6e49 2020-09-26 stsp goto done;
4332 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4333 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4334 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4335 417a6e49 2020-09-26 stsp goto done;
4337 417a6e49 2020-09-26 stsp error = NULL;
4338 417a6e49 2020-09-26 stsp if (worktree) {
4339 417a6e49 2020-09-26 stsp repo_path =
4340 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4341 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4342 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4343 417a6e49 2020-09-26 stsp if (error)
4344 417a6e49 2020-09-26 stsp goto done;
4347 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4348 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4349 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4350 417a6e49 2020-09-26 stsp goto done;
4355 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4356 417a6e49 2020-09-26 stsp if (error != NULL)
4357 417a6e49 2020-09-26 stsp goto done;
4359 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4360 417a6e49 2020-09-26 stsp if (error)
4361 417a6e49 2020-09-26 stsp goto done;
4363 417a6e49 2020-09-26 stsp if (worktree) {
4364 417a6e49 2020-09-26 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4365 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4366 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4367 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s%s%s%s%s",
4368 417a6e49 2020-09-26 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4369 417a6e49 2020-09-26 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4370 417a6e49 2020-09-26 stsp path) == -1) {
4371 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4372 417a6e49 2020-09-26 stsp goto done;
4374 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4377 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4379 417a6e49 2020-09-26 stsp if (error)
4380 417a6e49 2020-09-26 stsp goto done;
4382 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4383 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4384 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, worktree ?
4385 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4386 417a6e49 2020-09-26 stsp if (error != NULL)
4387 417a6e49 2020-09-26 stsp goto done;
4388 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4389 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4390 417a6e49 2020-09-26 stsp if (error != NULL)
4391 417a6e49 2020-09-26 stsp goto done;
4393 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4394 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4395 417a6e49 2020-09-26 stsp if (error)
4396 417a6e49 2020-09-26 stsp goto done;
4399 417a6e49 2020-09-26 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4400 417a6e49 2020-09-26 stsp commit_id, repo);
4401 417a6e49 2020-09-26 stsp if (error)
4402 417a6e49 2020-09-26 stsp goto done;
4404 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4405 417a6e49 2020-09-26 stsp link_target ? link_target : in_repo_path);
4406 417a6e49 2020-09-26 stsp if (error)
4407 417a6e49 2020-09-26 stsp goto done;
4409 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4410 417a6e49 2020-09-26 stsp if (error)
4411 417a6e49 2020-09-26 stsp goto done;
4413 417a6e49 2020-09-26 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4414 417a6e49 2020-09-26 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4415 417a6e49 2020-09-26 stsp GOT_ERR_OBJ_TYPE);
4416 417a6e49 2020-09-26 stsp goto done;
4419 417a6e49 2020-09-26 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4420 417a6e49 2020-09-26 stsp if (error)
4421 417a6e49 2020-09-26 stsp goto done;
4422 417a6e49 2020-09-26 stsp bca.f = got_opentemp();
4423 417a6e49 2020-09-26 stsp if (bca.f == NULL) {
4424 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_opentemp");
4425 417a6e49 2020-09-26 stsp goto done;
4427 417a6e49 2020-09-26 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4428 417a6e49 2020-09-26 stsp &bca.line_offsets, bca.f, blob);
4429 417a6e49 2020-09-26 stsp if (error || bca.nlines == 0)
4430 417a6e49 2020-09-26 stsp goto done;
4432 417a6e49 2020-09-26 stsp /* Don't include \n at EOF in the blame line count. */
4433 417a6e49 2020-09-26 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4434 417a6e49 2020-09-26 stsp bca.nlines--;
4436 417a6e49 2020-09-26 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4437 417a6e49 2020-09-26 stsp if (bca.lines == NULL) {
4438 417a6e49 2020-09-26 stsp error = got_error_from_errno("calloc");
4439 417a6e49 2020-09-26 stsp goto done;
4441 417a6e49 2020-09-26 stsp bca.lineno_cur = 1;
4442 417a6e49 2020-09-26 stsp bca.nlines_prec = 0;
4443 417a6e49 2020-09-26 stsp i = bca.nlines;
4444 417a6e49 2020-09-26 stsp while (i > 0) {
4446 417a6e49 2020-09-26 stsp bca.nlines_prec++;
4448 417a6e49 2020-09-26 stsp bca.repo = repo;
4450 417a6e49 2020-09-26 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4451 417a6e49 2020-09-26 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4453 417a6e49 2020-09-26 stsp free(in_repo_path);
4454 417a6e49 2020-09-26 stsp free(link_target);
4455 417a6e49 2020-09-26 stsp free(repo_path);
4456 417a6e49 2020-09-26 stsp free(cwd);
4457 417a6e49 2020-09-26 stsp free(commit_id);
4458 417a6e49 2020-09-26 stsp free(obj_id);
4460 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
4461 417a6e49 2020-09-26 stsp if (worktree)
4462 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4463 417a6e49 2020-09-26 stsp if (repo) {
4464 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4465 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4466 417a6e49 2020-09-26 stsp if (error == NULL)
4467 417a6e49 2020-09-26 stsp error = repo_error;
4469 417a6e49 2020-09-26 stsp if (bca.lines) {
4470 417a6e49 2020-09-26 stsp for (i = 0; i < bca.nlines; i++) {
4471 417a6e49 2020-09-26 stsp struct blame_line *bline = &bca.lines[i];
4472 417a6e49 2020-09-26 stsp free(bline->id_str);
4473 417a6e49 2020-09-26 stsp free(bline->committer);
4475 417a6e49 2020-09-26 stsp free(bca.lines);
4477 417a6e49 2020-09-26 stsp free(bca.line_offsets);
4478 417a6e49 2020-09-26 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4479 417a6e49 2020-09-26 stsp error = got_error_from_errno("fclose");
4480 417a6e49 2020-09-26 stsp return error;
4483 417a6e49 2020-09-26 stsp __dead static void
4484 417a6e49 2020-09-26 stsp usage_tree(void)
4486 417a6e49 2020-09-26 stsp fprintf(stderr,
4487 417a6e49 2020-09-26 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4488 417a6e49 2020-09-26 stsp getprogname());
4492 417a6e49 2020-09-26 stsp static const struct got_error *
4493 417a6e49 2020-09-26 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4494 417a6e49 2020-09-26 stsp const char *root_path, struct got_repository *repo)
4496 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4497 417a6e49 2020-09-26 stsp int is_root_path = (strcmp(path, root_path) == 0);
4498 417a6e49 2020-09-26 stsp const char *modestr = "";
4499 417a6e49 2020-09-26 stsp mode_t mode = got_tree_entry_get_mode(te);
4500 417a6e49 2020-09-26 stsp char *link_target = NULL;
4502 417a6e49 2020-09-26 stsp path += strlen(root_path);
4503 417a6e49 2020-09-26 stsp while (path[0] == '/')
4506 417a6e49 2020-09-26 stsp if (got_object_tree_entry_is_submodule(te))
4507 417a6e49 2020-09-26 stsp modestr = "$";
4508 417a6e49 2020-09-26 stsp else if (S_ISLNK(mode)) {
4511 417a6e49 2020-09-26 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4513 417a6e49 2020-09-26 stsp return err;
4514 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(link_target); i++) {
4515 417a6e49 2020-09-26 stsp if (!isprint((unsigned char)link_target[i]))
4516 417a6e49 2020-09-26 stsp link_target[i] = '?';
4519 417a6e49 2020-09-26 stsp modestr = "@";
4521 417a6e49 2020-09-26 stsp else if (S_ISDIR(mode))
4522 417a6e49 2020-09-26 stsp modestr = "/";
4523 417a6e49 2020-09-26 stsp else if (mode & S_IXUSR)
4524 417a6e49 2020-09-26 stsp modestr = "*";
4526 417a6e49 2020-09-26 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4527 417a6e49 2020-09-26 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4528 417a6e49 2020-09-26 stsp link_target ? " -> ": "", link_target ? link_target : "");
4530 417a6e49 2020-09-26 stsp free(link_target);
4531 417a6e49 2020-09-26 stsp return NULL;
4534 417a6e49 2020-09-26 stsp static const struct got_error *
4535 417a6e49 2020-09-26 stsp print_tree(const char *path, struct got_object_id *commit_id,
4536 417a6e49 2020-09-26 stsp int show_ids, int recurse, const char *root_path,
4537 417a6e49 2020-09-26 stsp struct got_repository *repo)
4539 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4540 417a6e49 2020-09-26 stsp struct got_object_id *tree_id = NULL;
4541 417a6e49 2020-09-26 stsp struct got_tree_object *tree = NULL;
4542 417a6e49 2020-09-26 stsp int nentries, i;
4544 417a6e49 2020-09-26 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4546 417a6e49 2020-09-26 stsp goto done;
4548 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4550 417a6e49 2020-09-26 stsp goto done;
4551 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
4552 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
4553 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
4554 417a6e49 2020-09-26 stsp char *id = NULL;
4556 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
4559 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
4560 417a6e49 2020-09-26 stsp if (show_ids) {
4561 417a6e49 2020-09-26 stsp char *id_str;
4562 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
4563 417a6e49 2020-09-26 stsp got_tree_entry_get_id(te));
4565 417a6e49 2020-09-26 stsp goto done;
4566 417a6e49 2020-09-26 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4567 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4568 417a6e49 2020-09-26 stsp free(id_str);
4569 417a6e49 2020-09-26 stsp goto done;
4571 417a6e49 2020-09-26 stsp free(id_str);
4573 417a6e49 2020-09-26 stsp err = print_entry(te, id, path, root_path, repo);
4576 417a6e49 2020-09-26 stsp goto done;
4578 417a6e49 2020-09-26 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4579 417a6e49 2020-09-26 stsp char *child_path;
4580 417a6e49 2020-09-26 stsp if (asprintf(&child_path, "%s%s%s", path,
4581 417a6e49 2020-09-26 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4582 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te)) == -1) {
4583 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
4584 417a6e49 2020-09-26 stsp goto done;
4586 417a6e49 2020-09-26 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4587 417a6e49 2020-09-26 stsp root_path, repo);
4588 417a6e49 2020-09-26 stsp free(child_path);
4590 417a6e49 2020-09-26 stsp goto done;
4595 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
4596 417a6e49 2020-09-26 stsp free(tree_id);
4597 417a6e49 2020-09-26 stsp return err;
4600 417a6e49 2020-09-26 stsp static const struct got_error *
4601 417a6e49 2020-09-26 stsp cmd_tree(int argc, char *argv[])
4603 417a6e49 2020-09-26 stsp const struct got_error *error;
4604 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4605 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4606 417a6e49 2020-09-26 stsp const char *path, *refname = NULL;
4607 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4608 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
4609 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
4610 417a6e49 2020-09-26 stsp int show_ids = 0, recurse = 0;
4613 417a6e49 2020-09-26 stsp #ifndef PROFILE
4614 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4615 417a6e49 2020-09-26 stsp NULL) == -1)
4616 417a6e49 2020-09-26 stsp err(1, "pledge");
4619 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4620 417a6e49 2020-09-26 stsp switch (ch) {
4622 417a6e49 2020-09-26 stsp commit_id_str = optarg;
4625 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
4626 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4627 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
4629 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
4632 417a6e49 2020-09-26 stsp show_ids = 1;
4635 417a6e49 2020-09-26 stsp recurse = 1;
4638 417a6e49 2020-09-26 stsp usage_tree();
4639 417a6e49 2020-09-26 stsp /* NOTREACHED */
4643 417a6e49 2020-09-26 stsp argc -= optind;
4644 417a6e49 2020-09-26 stsp argv += optind;
4646 417a6e49 2020-09-26 stsp if (argc == 1)
4647 417a6e49 2020-09-26 stsp path = argv[0];
4648 417a6e49 2020-09-26 stsp else if (argc > 1)
4649 417a6e49 2020-09-26 stsp usage_tree();
4651 417a6e49 2020-09-26 stsp path = NULL;
4653 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4654 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4655 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4656 417a6e49 2020-09-26 stsp goto done;
4658 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4659 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4660 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4661 417a6e49 2020-09-26 stsp goto done;
4663 417a6e49 2020-09-26 stsp error = NULL;
4664 417a6e49 2020-09-26 stsp if (worktree) {
4665 417a6e49 2020-09-26 stsp repo_path =
4666 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
4667 417a6e49 2020-09-26 stsp if (repo_path == NULL)
4668 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4669 417a6e49 2020-09-26 stsp if (error)
4670 417a6e49 2020-09-26 stsp goto done;
4672 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
4673 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
4674 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
4675 417a6e49 2020-09-26 stsp goto done;
4680 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
4681 417a6e49 2020-09-26 stsp if (error != NULL)
4682 417a6e49 2020-09-26 stsp goto done;
4684 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4685 417a6e49 2020-09-26 stsp if (error)
4686 417a6e49 2020-09-26 stsp goto done;
4688 417a6e49 2020-09-26 stsp if (path == NULL) {
4689 417a6e49 2020-09-26 stsp if (worktree) {
4690 417a6e49 2020-09-26 stsp char *p, *worktree_subdir = cwd +
4691 417a6e49 2020-09-26 stsp strlen(got_worktree_get_root_path(worktree));
4692 417a6e49 2020-09-26 stsp if (asprintf(&p, "%s/%s",
4693 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
4694 417a6e49 2020-09-26 stsp worktree_subdir) == -1) {
4695 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
4696 417a6e49 2020-09-26 stsp goto done;
4698 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4700 417a6e49 2020-09-26 stsp if (error)
4701 417a6e49 2020-09-26 stsp goto done;
4703 417a6e49 2020-09-26 stsp path = "/";
4705 417a6e49 2020-09-26 stsp if (in_repo_path == NULL) {
4706 417a6e49 2020-09-26 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4707 417a6e49 2020-09-26 stsp if (error != NULL)
4708 417a6e49 2020-09-26 stsp goto done;
4711 417a6e49 2020-09-26 stsp if (commit_id_str == NULL) {
4712 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
4713 417a6e49 2020-09-26 stsp if (worktree)
4714 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
4716 417a6e49 2020-09-26 stsp refname = GOT_REF_HEAD;
4717 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo, refname, 0);
4718 417a6e49 2020-09-26 stsp if (error != NULL)
4719 417a6e49 2020-09-26 stsp goto done;
4720 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4721 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
4722 417a6e49 2020-09-26 stsp if (error != NULL)
4723 417a6e49 2020-09-26 stsp goto done;
4725 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
4726 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4727 417a6e49 2020-09-26 stsp if (error)
4728 417a6e49 2020-09-26 stsp goto done;
4731 417a6e49 2020-09-26 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4732 417a6e49 2020-09-26 stsp in_repo_path, repo);
4734 417a6e49 2020-09-26 stsp free(in_repo_path);
4735 417a6e49 2020-09-26 stsp free(repo_path);
4736 417a6e49 2020-09-26 stsp free(cwd);
4737 417a6e49 2020-09-26 stsp free(commit_id);
4738 417a6e49 2020-09-26 stsp if (worktree)
4739 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
4740 417a6e49 2020-09-26 stsp if (repo) {
4741 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
4742 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
4743 417a6e49 2020-09-26 stsp if (error == NULL)
4744 417a6e49 2020-09-26 stsp error = repo_error;
4746 417a6e49 2020-09-26 stsp return error;
4749 417a6e49 2020-09-26 stsp __dead static void
4750 417a6e49 2020-09-26 stsp usage_status(void)
4752 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4753 417a6e49 2020-09-26 stsp getprogname());
4757 417a6e49 2020-09-26 stsp static const struct got_error *
4758 417a6e49 2020-09-26 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4759 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
4760 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4761 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
4763 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4764 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
4765 417a6e49 2020-09-26 stsp if (arg) {
4766 417a6e49 2020-09-26 stsp char *status_codes = arg;
4767 417a6e49 2020-09-26 stsp size_t ncodes = strlen(status_codes);
4769 417a6e49 2020-09-26 stsp for (i = 0; i < ncodes ; i++) {
4770 417a6e49 2020-09-26 stsp if (status == status_codes[i] ||
4771 417a6e49 2020-09-26 stsp staged_status == status_codes[i])
4774 417a6e49 2020-09-26 stsp if (i == ncodes)
4775 417a6e49 2020-09-26 stsp return NULL;
4777 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
4778 417a6e49 2020-09-26 stsp return NULL;
4781 417a6e49 2020-09-26 stsp static const struct got_error *
4782 417a6e49 2020-09-26 stsp cmd_status(int argc, char *argv[])
4784 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4785 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4786 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4787 417a6e49 2020-09-26 stsp char *cwd = NULL, *status_codes = NULL;;
4788 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
4789 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
4790 417a6e49 2020-09-26 stsp int ch, i;
4792 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
4794 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4795 417a6e49 2020-09-26 stsp switch (ch) {
4797 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
4798 417a6e49 2020-09-26 stsp switch (optarg[i]) {
4799 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
4800 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
4801 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
4802 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
4803 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
4804 417a6e49 2020-09-26 stsp case GOT_STATUS_OBSTRUCTED:
4805 417a6e49 2020-09-26 stsp case GOT_STATUS_UNVERSIONED:
4806 417a6e49 2020-09-26 stsp case GOT_STATUS_MODE_CHANGE:
4807 417a6e49 2020-09-26 stsp case GOT_STATUS_NONEXISTENT:
4810 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
4811 417a6e49 2020-09-26 stsp optarg[i]);
4814 417a6e49 2020-09-26 stsp status_codes = optarg;
4817 417a6e49 2020-09-26 stsp usage_status();
4818 417a6e49 2020-09-26 stsp /* NOTREACHED */
4822 417a6e49 2020-09-26 stsp argc -= optind;
4823 417a6e49 2020-09-26 stsp argv += optind;
4825 417a6e49 2020-09-26 stsp #ifndef PROFILE
4826 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4827 417a6e49 2020-09-26 stsp NULL) == -1)
4828 417a6e49 2020-09-26 stsp err(1, "pledge");
4830 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
4831 417a6e49 2020-09-26 stsp if (cwd == NULL) {
4832 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
4833 417a6e49 2020-09-26 stsp goto done;
4836 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
4837 417a6e49 2020-09-26 stsp if (error) {
4838 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4839 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
4840 417a6e49 2020-09-26 stsp goto done;
4843 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4845 417a6e49 2020-09-26 stsp if (error != NULL)
4846 417a6e49 2020-09-26 stsp goto done;
4848 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4849 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
4850 417a6e49 2020-09-26 stsp if (error)
4851 417a6e49 2020-09-26 stsp goto done;
4853 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4854 417a6e49 2020-09-26 stsp if (error)
4855 417a6e49 2020-09-26 stsp goto done;
4857 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4858 417a6e49 2020-09-26 stsp status_codes, check_cancelled, NULL);
4860 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
4861 417a6e49 2020-09-26 stsp free((char *)pe->path);
4862 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
4863 417a6e49 2020-09-26 stsp free(cwd);
4864 417a6e49 2020-09-26 stsp return error;
4867 417a6e49 2020-09-26 stsp __dead static void
4868 417a6e49 2020-09-26 stsp usage_ref(void)
4870 417a6e49 2020-09-26 stsp fprintf(stderr,
4871 417a6e49 2020-09-26 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4872 417a6e49 2020-09-26 stsp "[-d] [name]\n",
4873 417a6e49 2020-09-26 stsp getprogname());
4877 417a6e49 2020-09-26 stsp static const struct got_error *
4878 417a6e49 2020-09-26 stsp list_refs(struct got_repository *repo, const char *refname)
4880 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
4881 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
4882 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
4884 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
4885 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4887 417a6e49 2020-09-26 stsp return err;
4889 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4890 417a6e49 2020-09-26 stsp char *refstr;
4891 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
4892 417a6e49 2020-09-26 stsp if (refstr == NULL)
4893 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
4894 417a6e49 2020-09-26 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4895 417a6e49 2020-09-26 stsp free(refstr);
4898 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
4899 417a6e49 2020-09-26 stsp return NULL;
4902 417a6e49 2020-09-26 stsp static const struct got_error *
4903 417a6e49 2020-09-26 stsp delete_ref(struct got_repository *repo, const char *refname)
4905 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4906 417a6e49 2020-09-26 stsp struct got_reference *ref;
4908 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4910 417a6e49 2020-09-26 stsp return err;
4912 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
4913 417a6e49 2020-09-26 stsp got_ref_close(ref);
4914 417a6e49 2020-09-26 stsp return err;
4917 417a6e49 2020-09-26 stsp static const struct got_error *
4918 417a6e49 2020-09-26 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4920 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4921 417a6e49 2020-09-26 stsp struct got_object_id *id;
4922 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4925 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4926 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4927 417a6e49 2020-09-26 stsp * an unintended typo.
4929 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4930 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4932 417a6e49 2020-09-26 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4934 417a6e49 2020-09-26 stsp if (err) {
4935 417a6e49 2020-09-26 stsp struct got_reference *target_ref;
4937 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4938 417a6e49 2020-09-26 stsp return err;
4939 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4941 417a6e49 2020-09-26 stsp return err;
4942 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, target_ref);
4943 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4945 417a6e49 2020-09-26 stsp return err;
4948 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, id);
4950 417a6e49 2020-09-26 stsp goto done;
4952 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4955 417a6e49 2020-09-26 stsp got_ref_close(ref);
4957 417a6e49 2020-09-26 stsp return err;
4960 417a6e49 2020-09-26 stsp static const struct got_error *
4961 417a6e49 2020-09-26 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4963 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
4964 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
4965 417a6e49 2020-09-26 stsp struct got_reference *target_ref = NULL;
4968 417a6e49 2020-09-26 stsp * Don't let the user create a reference name with a leading '-'.
4969 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
4970 417a6e49 2020-09-26 stsp * an unintended typo.
4972 417a6e49 2020-09-26 stsp if (refname[0] == '-')
4973 417a6e49 2020-09-26 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4975 417a6e49 2020-09-26 stsp err = got_ref_open(&target_ref, repo, target, 0);
4977 417a6e49 2020-09-26 stsp return err;
4979 417a6e49 2020-09-26 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4981 417a6e49 2020-09-26 stsp goto done;
4983 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
4985 417a6e49 2020-09-26 stsp if (target_ref)
4986 417a6e49 2020-09-26 stsp got_ref_close(target_ref);
4988 417a6e49 2020-09-26 stsp got_ref_close(ref);
4989 417a6e49 2020-09-26 stsp return err;
4992 417a6e49 2020-09-26 stsp static const struct got_error *
4993 417a6e49 2020-09-26 stsp cmd_ref(int argc, char *argv[])
4995 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
4996 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
4997 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
4998 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
4999 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_delete = 0;
5000 417a6e49 2020-09-26 stsp const char *obj_arg = NULL, *symref_target= NULL;
5001 417a6e49 2020-09-26 stsp char *refname = NULL;
5003 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5004 417a6e49 2020-09-26 stsp switch (ch) {
5006 417a6e49 2020-09-26 stsp obj_arg = optarg;
5009 417a6e49 2020-09-26 stsp do_delete = 1;
5012 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5013 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5014 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5016 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5019 417a6e49 2020-09-26 stsp do_list = 1;
5022 417a6e49 2020-09-26 stsp symref_target = optarg;
5025 417a6e49 2020-09-26 stsp usage_ref();
5026 417a6e49 2020-09-26 stsp /* NOTREACHED */
5030 417a6e49 2020-09-26 stsp if (obj_arg && do_list)
5031 417a6e49 2020-09-26 stsp errx(1, "-c and -l options are mutually exclusive");
5032 417a6e49 2020-09-26 stsp if (obj_arg && do_delete)
5033 417a6e49 2020-09-26 stsp errx(1, "-c and -d options are mutually exclusive");
5034 417a6e49 2020-09-26 stsp if (obj_arg && symref_target)
5035 417a6e49 2020-09-26 stsp errx(1, "-c and -s options are mutually exclusive");
5036 417a6e49 2020-09-26 stsp if (symref_target && do_delete)
5037 417a6e49 2020-09-26 stsp errx(1, "-s and -d options are mutually exclusive");
5038 417a6e49 2020-09-26 stsp if (symref_target && do_list)
5039 417a6e49 2020-09-26 stsp errx(1, "-s and -l options are mutually exclusive");
5040 417a6e49 2020-09-26 stsp if (do_delete && do_list)
5041 417a6e49 2020-09-26 stsp errx(1, "-d and -l options are mutually exclusive");
5043 417a6e49 2020-09-26 stsp argc -= optind;
5044 417a6e49 2020-09-26 stsp argv += optind;
5046 417a6e49 2020-09-26 stsp if (do_list) {
5047 417a6e49 2020-09-26 stsp if (argc != 0 && argc != 1)
5048 417a6e49 2020-09-26 stsp usage_ref();
5049 417a6e49 2020-09-26 stsp if (argc == 1) {
5050 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5051 417a6e49 2020-09-26 stsp if (refname == NULL) {
5052 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5053 417a6e49 2020-09-26 stsp goto done;
5057 417a6e49 2020-09-26 stsp if (argc != 1)
5058 417a6e49 2020-09-26 stsp usage_ref();
5059 417a6e49 2020-09-26 stsp refname = strdup(argv[0]);
5060 417a6e49 2020-09-26 stsp if (refname == NULL) {
5061 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5062 417a6e49 2020-09-26 stsp goto done;
5066 417a6e49 2020-09-26 stsp if (refname)
5067 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(refname);
5069 417a6e49 2020-09-26 stsp #ifndef PROFILE
5070 417a6e49 2020-09-26 stsp if (do_list) {
5071 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5072 417a6e49 2020-09-26 stsp NULL) == -1)
5073 417a6e49 2020-09-26 stsp err(1, "pledge");
5075 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5076 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5077 417a6e49 2020-09-26 stsp err(1, "pledge");
5080 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5081 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5082 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5083 417a6e49 2020-09-26 stsp goto done;
5086 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5087 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5088 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5089 417a6e49 2020-09-26 stsp goto done;
5091 417a6e49 2020-09-26 stsp error = NULL;
5092 417a6e49 2020-09-26 stsp if (worktree) {
5093 417a6e49 2020-09-26 stsp repo_path =
5094 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5095 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5096 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5097 417a6e49 2020-09-26 stsp if (error)
5098 417a6e49 2020-09-26 stsp goto done;
5100 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5101 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5102 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5103 417a6e49 2020-09-26 stsp goto done;
5108 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5109 417a6e49 2020-09-26 stsp if (error != NULL)
5110 417a6e49 2020-09-26 stsp goto done;
5112 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5113 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5114 417a6e49 2020-09-26 stsp if (error)
5115 417a6e49 2020-09-26 stsp goto done;
5117 417a6e49 2020-09-26 stsp if (do_list)
5118 417a6e49 2020-09-26 stsp error = list_refs(repo, refname);
5119 417a6e49 2020-09-26 stsp else if (do_delete)
5120 417a6e49 2020-09-26 stsp error = delete_ref(repo, refname);
5121 417a6e49 2020-09-26 stsp else if (symref_target)
5122 417a6e49 2020-09-26 stsp error = add_symref(repo, refname, symref_target);
5124 417a6e49 2020-09-26 stsp if (obj_arg == NULL)
5125 417a6e49 2020-09-26 stsp usage_ref();
5126 417a6e49 2020-09-26 stsp error = add_ref(repo, refname, obj_arg);
5129 417a6e49 2020-09-26 stsp free(refname);
5131 417a6e49 2020-09-26 stsp got_repo_close(repo);
5132 417a6e49 2020-09-26 stsp if (worktree)
5133 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5134 417a6e49 2020-09-26 stsp free(cwd);
5135 417a6e49 2020-09-26 stsp free(repo_path);
5136 417a6e49 2020-09-26 stsp return error;
5139 417a6e49 2020-09-26 stsp __dead static void
5140 417a6e49 2020-09-26 stsp usage_branch(void)
5142 417a6e49 2020-09-26 stsp fprintf(stderr,
5143 417a6e49 2020-09-26 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5144 417a6e49 2020-09-26 stsp "[name]\n", getprogname());
5148 417a6e49 2020-09-26 stsp static const struct got_error *
5149 417a6e49 2020-09-26 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5150 417a6e49 2020-09-26 stsp struct got_reference *ref)
5152 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5153 417a6e49 2020-09-26 stsp const char *refname, *marker = " ";
5154 417a6e49 2020-09-26 stsp char *refstr;
5156 417a6e49 2020-09-26 stsp refname = got_ref_get_name(ref);
5157 417a6e49 2020-09-26 stsp if (worktree && strcmp(refname,
5158 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5159 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
5161 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, ref);
5163 417a6e49 2020-09-26 stsp return err;
5164 417a6e49 2020-09-26 stsp if (got_object_id_cmp(id,
5165 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5166 417a6e49 2020-09-26 stsp marker = "* ";
5168 417a6e49 2020-09-26 stsp marker = "~ ";
5172 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5173 417a6e49 2020-09-26 stsp refname += 11;
5174 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5175 417a6e49 2020-09-26 stsp refname += 18;
5177 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(ref);
5178 417a6e49 2020-09-26 stsp if (refstr == NULL)
5179 417a6e49 2020-09-26 stsp return got_error_from_errno("got_ref_to_str");
5181 417a6e49 2020-09-26 stsp printf("%s%s: %s\n", marker, refname, refstr);
5182 417a6e49 2020-09-26 stsp free(refstr);
5183 417a6e49 2020-09-26 stsp return NULL;
5186 417a6e49 2020-09-26 stsp static const struct got_error *
5187 417a6e49 2020-09-26 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5189 417a6e49 2020-09-26 stsp const char *refname;
5191 417a6e49 2020-09-26 stsp if (worktree == NULL)
5192 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5194 417a6e49 2020-09-26 stsp refname = got_worktree_get_head_ref_name(worktree);
5196 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5197 417a6e49 2020-09-26 stsp refname += 11;
5198 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5199 417a6e49 2020-09-26 stsp refname += 18;
5201 417a6e49 2020-09-26 stsp printf("%s\n", refname);
5203 417a6e49 2020-09-26 stsp return NULL;
5206 417a6e49 2020-09-26 stsp static const struct got_error *
5207 417a6e49 2020-09-26 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5209 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5210 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5211 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5212 417a6e49 2020-09-26 stsp struct got_reference *temp_ref = NULL;
5213 417a6e49 2020-09-26 stsp int rebase_in_progress, histedit_in_progress;
5215 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5217 417a6e49 2020-09-26 stsp if (worktree) {
5218 417a6e49 2020-09-26 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5219 417a6e49 2020-09-26 stsp worktree);
5221 417a6e49 2020-09-26 stsp return err;
5223 417a6e49 2020-09-26 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5224 417a6e49 2020-09-26 stsp worktree);
5226 417a6e49 2020-09-26 stsp return err;
5228 417a6e49 2020-09-26 stsp if (rebase_in_progress || histedit_in_progress) {
5229 417a6e49 2020-09-26 stsp err = got_ref_open(&temp_ref, repo,
5230 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
5232 417a6e49 2020-09-26 stsp return err;
5233 417a6e49 2020-09-26 stsp list_branch(repo, worktree, temp_ref);
5234 417a6e49 2020-09-26 stsp got_ref_close(temp_ref);
5238 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/heads",
5239 417a6e49 2020-09-26 stsp got_ref_cmp_by_name, NULL);
5241 417a6e49 2020-09-26 stsp return err;
5243 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5244 417a6e49 2020-09-26 stsp list_branch(repo, worktree, re->ref);
5246 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5247 417a6e49 2020-09-26 stsp return NULL;
5250 417a6e49 2020-09-26 stsp static const struct got_error *
5251 417a6e49 2020-09-26 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5252 417a6e49 2020-09-26 stsp const char *branch_name)
5254 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5255 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5256 417a6e49 2020-09-26 stsp char *refname;
5258 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5259 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
5261 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5263 417a6e49 2020-09-26 stsp goto done;
5265 417a6e49 2020-09-26 stsp if (worktree &&
5266 417a6e49 2020-09-26 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5267 417a6e49 2020-09-26 stsp got_ref_get_name(ref)) == 0) {
5268 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5269 417a6e49 2020-09-26 stsp "will not delete this work tree's current branch");
5270 417a6e49 2020-09-26 stsp goto done;
5273 417a6e49 2020-09-26 stsp err = got_ref_delete(ref, repo);
5276 417a6e49 2020-09-26 stsp got_ref_close(ref);
5277 417a6e49 2020-09-26 stsp free(refname);
5278 417a6e49 2020-09-26 stsp return err;
5281 417a6e49 2020-09-26 stsp static const struct got_error *
5282 417a6e49 2020-09-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5283 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id)
5285 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5286 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5287 417a6e49 2020-09-26 stsp char *base_refname = NULL, *refname = NULL;
5290 417a6e49 2020-09-26 stsp * Don't let the user create a branch name with a leading '-'.
5291 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5292 417a6e49 2020-09-26 stsp * an unintended typo.
5294 417a6e49 2020-09-26 stsp if (branch_name[0] == '-')
5295 417a6e49 2020-09-26 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5297 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5298 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5299 417a6e49 2020-09-26 stsp goto done;
5302 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5303 417a6e49 2020-09-26 stsp if (err == NULL) {
5304 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5305 417a6e49 2020-09-26 stsp goto done;
5306 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5307 417a6e49 2020-09-26 stsp goto done;
5309 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5311 417a6e49 2020-09-26 stsp goto done;
5313 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5316 417a6e49 2020-09-26 stsp got_ref_close(ref);
5317 417a6e49 2020-09-26 stsp free(base_refname);
5318 417a6e49 2020-09-26 stsp free(refname);
5319 417a6e49 2020-09-26 stsp return err;
5322 417a6e49 2020-09-26 stsp static const struct got_error *
5323 417a6e49 2020-09-26 stsp cmd_branch(int argc, char *argv[])
5325 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5326 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5327 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5328 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL;
5329 417a6e49 2020-09-26 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5330 417a6e49 2020-09-26 stsp const char *delref = NULL, *commit_id_arg = NULL;
5331 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5332 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
5333 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
5334 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
5335 417a6e49 2020-09-26 stsp char *commit_id_str = NULL;
5337 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
5339 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5340 417a6e49 2020-09-26 stsp switch (ch) {
5342 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5345 417a6e49 2020-09-26 stsp delref = optarg;
5348 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5349 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5350 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5352 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5355 417a6e49 2020-09-26 stsp do_list = 1;
5358 417a6e49 2020-09-26 stsp do_update = 0;
5361 417a6e49 2020-09-26 stsp usage_branch();
5362 417a6e49 2020-09-26 stsp /* NOTREACHED */
5366 417a6e49 2020-09-26 stsp if (do_list && delref)
5367 417a6e49 2020-09-26 stsp errx(1, "-l and -d options are mutually exclusive");
5369 417a6e49 2020-09-26 stsp argc -= optind;
5370 417a6e49 2020-09-26 stsp argv += optind;
5372 417a6e49 2020-09-26 stsp if (!do_list && !delref && argc == 0)
5373 417a6e49 2020-09-26 stsp do_show = 1;
5375 417a6e49 2020-09-26 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5376 417a6e49 2020-09-26 stsp errx(1, "-c option can only be used when creating a branch");
5378 417a6e49 2020-09-26 stsp if (do_list || delref) {
5379 417a6e49 2020-09-26 stsp if (argc > 0)
5380 417a6e49 2020-09-26 stsp usage_branch();
5381 417a6e49 2020-09-26 stsp } else if (!do_show && argc != 1)
5382 417a6e49 2020-09-26 stsp usage_branch();
5384 417a6e49 2020-09-26 stsp #ifndef PROFILE
5385 417a6e49 2020-09-26 stsp if (do_list || do_show) {
5386 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5387 417a6e49 2020-09-26 stsp NULL) == -1)
5388 417a6e49 2020-09-26 stsp err(1, "pledge");
5390 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5391 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5392 417a6e49 2020-09-26 stsp err(1, "pledge");
5395 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5396 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5397 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5398 417a6e49 2020-09-26 stsp goto done;
5401 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5402 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5403 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5404 417a6e49 2020-09-26 stsp goto done;
5406 417a6e49 2020-09-26 stsp error = NULL;
5407 417a6e49 2020-09-26 stsp if (worktree) {
5408 417a6e49 2020-09-26 stsp repo_path =
5409 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5410 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5411 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5412 417a6e49 2020-09-26 stsp if (error)
5413 417a6e49 2020-09-26 stsp goto done;
5415 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5416 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5417 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5418 417a6e49 2020-09-26 stsp goto done;
5423 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5424 417a6e49 2020-09-26 stsp if (error != NULL)
5425 417a6e49 2020-09-26 stsp goto done;
5427 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5428 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5429 417a6e49 2020-09-26 stsp if (error)
5430 417a6e49 2020-09-26 stsp goto done;
5432 417a6e49 2020-09-26 stsp if (do_show)
5433 417a6e49 2020-09-26 stsp error = show_current_branch(repo, worktree);
5434 417a6e49 2020-09-26 stsp else if (do_list)
5435 417a6e49 2020-09-26 stsp error = list_branches(repo, worktree);
5436 417a6e49 2020-09-26 stsp else if (delref)
5437 417a6e49 2020-09-26 stsp error = delete_branch(repo, worktree, delref);
5439 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL)
5440 417a6e49 2020-09-26 stsp commit_id_arg = worktree ?
5441 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree) :
5442 417a6e49 2020-09-26 stsp GOT_REF_HEAD;
5443 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
5444 417a6e49 2020-09-26 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5445 417a6e49 2020-09-26 stsp if (error)
5446 417a6e49 2020-09-26 stsp goto done;
5447 417a6e49 2020-09-26 stsp error = add_branch(repo, argv[0], commit_id);
5448 417a6e49 2020-09-26 stsp if (error)
5449 417a6e49 2020-09-26 stsp goto done;
5450 417a6e49 2020-09-26 stsp if (worktree && do_update) {
5451 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
5452 417a6e49 2020-09-26 stsp char *branch_refname = NULL;
5454 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5455 417a6e49 2020-09-26 stsp if (error)
5456 417a6e49 2020-09-26 stsp goto done;
5457 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5458 417a6e49 2020-09-26 stsp worktree);
5459 417a6e49 2020-09-26 stsp if (error)
5460 417a6e49 2020-09-26 stsp goto done;
5461 417a6e49 2020-09-26 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5463 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
5464 417a6e49 2020-09-26 stsp goto done;
5466 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5467 417a6e49 2020-09-26 stsp free(branch_refname);
5468 417a6e49 2020-09-26 stsp if (error)
5469 417a6e49 2020-09-26 stsp goto done;
5470 417a6e49 2020-09-26 stsp error = switch_head_ref(ref, commit_id, worktree,
5472 417a6e49 2020-09-26 stsp if (error)
5473 417a6e49 2020-09-26 stsp goto done;
5474 417a6e49 2020-09-26 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5475 417a6e49 2020-09-26 stsp commit_id);
5476 417a6e49 2020-09-26 stsp if (error)
5477 417a6e49 2020-09-26 stsp goto done;
5478 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
5479 417a6e49 2020-09-26 stsp error = got_worktree_checkout_files(worktree, &paths,
5480 417a6e49 2020-09-26 stsp repo, update_progress, &upa, check_cancelled,
5482 417a6e49 2020-09-26 stsp if (error)
5483 417a6e49 2020-09-26 stsp goto done;
5484 417a6e49 2020-09-26 stsp if (upa.did_something)
5485 417a6e49 2020-09-26 stsp printf("Updated to commit %s\n", commit_id_str);
5486 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
5491 417a6e49 2020-09-26 stsp got_ref_close(ref);
5493 417a6e49 2020-09-26 stsp got_repo_close(repo);
5494 417a6e49 2020-09-26 stsp if (worktree)
5495 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5496 417a6e49 2020-09-26 stsp free(cwd);
5497 417a6e49 2020-09-26 stsp free(repo_path);
5498 417a6e49 2020-09-26 stsp free(commit_id);
5499 417a6e49 2020-09-26 stsp free(commit_id_str);
5500 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
5501 417a6e49 2020-09-26 stsp free((char *)pe->path);
5502 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
5503 417a6e49 2020-09-26 stsp return error;
5507 417a6e49 2020-09-26 stsp __dead static void
5508 417a6e49 2020-09-26 stsp usage_tag(void)
5510 417a6e49 2020-09-26 stsp fprintf(stderr,
5511 417a6e49 2020-09-26 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5512 417a6e49 2020-09-26 stsp "[-m message] name\n", getprogname());
5517 417a6e49 2020-09-26 stsp static const struct got_error *
5518 417a6e49 2020-09-26 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5520 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5521 417a6e49 2020-09-26 stsp struct got_reflist_entry *re, *se, *new;
5522 417a6e49 2020-09-26 stsp struct got_object_id *re_id, *se_id;
5523 417a6e49 2020-09-26 stsp struct got_tag_object *re_tag, *se_tag;
5524 417a6e49 2020-09-26 stsp time_t re_time, se_time;
5526 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5527 417a6e49 2020-09-26 stsp se = SIMPLEQ_FIRST(sorted);
5528 417a6e49 2020-09-26 stsp if (se == NULL) {
5529 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5531 417a6e49 2020-09-26 stsp return err;
5532 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5535 417a6e49 2020-09-26 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5538 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5539 417a6e49 2020-09-26 stsp free(re_id);
5542 417a6e49 2020-09-26 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5543 417a6e49 2020-09-26 stsp got_object_tag_close(re_tag);
5546 417a6e49 2020-09-26 stsp while (se) {
5547 417a6e49 2020-09-26 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5550 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5551 417a6e49 2020-09-26 stsp free(se_id);
5554 417a6e49 2020-09-26 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5555 417a6e49 2020-09-26 stsp got_object_tag_close(se_tag);
5557 417a6e49 2020-09-26 stsp if (se_time > re_time) {
5558 417a6e49 2020-09-26 stsp err = got_reflist_entry_dup(&new, re);
5560 417a6e49 2020-09-26 stsp return err;
5561 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5564 417a6e49 2020-09-26 stsp se = SIMPLEQ_NEXT(se, entry);
5569 417a6e49 2020-09-26 stsp return err;
5573 417a6e49 2020-09-26 stsp static const struct got_error *
5574 417a6e49 2020-09-26 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5576 417a6e49 2020-09-26 stsp static const struct got_error *err = NULL;
5577 417a6e49 2020-09-26 stsp struct got_reflist_head refs;
5578 417a6e49 2020-09-26 stsp struct got_reflist_entry *re;
5580 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&refs);
5582 417a6e49 2020-09-26 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5584 417a6e49 2020-09-26 stsp return err;
5586 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5587 417a6e49 2020-09-26 stsp const char *refname;
5588 417a6e49 2020-09-26 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5589 417a6e49 2020-09-26 stsp char datebuf[26];
5590 417a6e49 2020-09-26 stsp const char *tagger;
5591 417a6e49 2020-09-26 stsp time_t tagger_time;
5592 417a6e49 2020-09-26 stsp struct got_object_id *id;
5593 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
5594 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
5596 417a6e49 2020-09-26 stsp refname = got_ref_get_name(re->ref);
5597 417a6e49 2020-09-26 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5599 417a6e49 2020-09-26 stsp refname += 10;
5600 417a6e49 2020-09-26 stsp refstr = got_ref_to_str(re->ref);
5601 417a6e49 2020-09-26 stsp if (refstr == NULL) {
5602 417a6e49 2020-09-26 stsp err = got_error_from_errno("got_ref_to_str");
5605 417a6e49 2020-09-26 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5606 417a6e49 2020-09-26 stsp free(refstr);
5608 417a6e49 2020-09-26 stsp err = got_ref_resolve(&id, repo, re->ref);
5611 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
5612 417a6e49 2020-09-26 stsp if (err) {
5613 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5617 417a6e49 2020-09-26 stsp /* "lightweight" tag */
5618 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
5619 417a6e49 2020-09-26 stsp if (err) {
5623 417a6e49 2020-09-26 stsp tagger = got_object_commit_get_committer(commit);
5624 417a6e49 2020-09-26 stsp tagger_time =
5625 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit);
5626 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
5632 417a6e49 2020-09-26 stsp tagger = got_object_tag_get_tagger(tag);
5633 417a6e49 2020-09-26 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5634 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str,
5635 417a6e49 2020-09-26 stsp got_object_tag_get_object_id(tag));
5639 417a6e49 2020-09-26 stsp printf("from: %s\n", tagger);
5640 417a6e49 2020-09-26 stsp datestr = get_datestr(&tagger_time, datebuf);
5641 417a6e49 2020-09-26 stsp if (datestr)
5642 417a6e49 2020-09-26 stsp printf("date: %s UTC\n", datestr);
5643 417a6e49 2020-09-26 stsp if (commit)
5644 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5646 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
5647 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
5648 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5651 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
5652 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5655 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
5656 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5659 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
5660 417a6e49 2020-09-26 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5667 417a6e49 2020-09-26 stsp free(id_str);
5668 417a6e49 2020-09-26 stsp if (commit) {
5669 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5672 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
5674 417a6e49 2020-09-26 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5675 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
5676 417a6e49 2020-09-26 stsp if (tagmsg0 == NULL) {
5677 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5682 417a6e49 2020-09-26 stsp tagmsg = tagmsg0;
5684 417a6e49 2020-09-26 stsp line = strsep(&tagmsg, "\n");
5686 417a6e49 2020-09-26 stsp printf(" %s\n", line);
5687 417a6e49 2020-09-26 stsp } while (line);
5688 417a6e49 2020-09-26 stsp free(tagmsg0);
5691 417a6e49 2020-09-26 stsp got_ref_list_free(&refs);
5692 417a6e49 2020-09-26 stsp return NULL;
5695 417a6e49 2020-09-26 stsp static const struct got_error *
5696 417a6e49 2020-09-26 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5697 417a6e49 2020-09-26 stsp const char *tag_name, const char *repo_path)
5699 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5700 417a6e49 2020-09-26 stsp char *template = NULL, *initial_content = NULL;
5701 417a6e49 2020-09-26 stsp char *editor = NULL;
5702 417a6e49 2020-09-26 stsp int initial_content_len;
5703 417a6e49 2020-09-26 stsp int fd = -1;
5705 417a6e49 2020-09-26 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5706 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5707 417a6e49 2020-09-26 stsp goto done;
5710 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
5711 417a6e49 2020-09-26 stsp "\n# tagging commit %s as %s\n",
5712 417a6e49 2020-09-26 stsp commit_id_str, tag_name);
5713 417a6e49 2020-09-26 stsp if (initial_content_len == -1) {
5714 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5715 417a6e49 2020-09-26 stsp goto done;
5718 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5720 417a6e49 2020-09-26 stsp goto done;
5722 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5723 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", *tagmsg_path);
5724 417a6e49 2020-09-26 stsp goto done;
5727 417a6e49 2020-09-26 stsp err = get_editor(&editor);
5729 417a6e49 2020-09-26 stsp goto done;
5730 417a6e49 2020-09-26 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5732 417a6e49 2020-09-26 stsp free(initial_content);
5733 417a6e49 2020-09-26 stsp free(template);
5734 417a6e49 2020-09-26 stsp free(editor);
5736 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5737 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", *tagmsg_path);
5739 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
5740 417a6e49 2020-09-26 stsp if (err == NULL)
5741 417a6e49 2020-09-26 stsp err = apply_unveil(repo_path, 0, NULL);
5742 417a6e49 2020-09-26 stsp if (err) {
5743 417a6e49 2020-09-26 stsp free(*tagmsg);
5744 417a6e49 2020-09-26 stsp *tagmsg = NULL;
5746 417a6e49 2020-09-26 stsp return err;
5749 417a6e49 2020-09-26 stsp static const struct got_error *
5750 417a6e49 2020-09-26 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5751 417a6e49 2020-09-26 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5753 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
5754 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5755 417a6e49 2020-09-26 stsp char *label = NULL, *commit_id_str = NULL;
5756 417a6e49 2020-09-26 stsp struct got_reference *ref = NULL;
5757 417a6e49 2020-09-26 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5758 417a6e49 2020-09-26 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5759 417a6e49 2020-09-26 stsp int preserve_tagmsg = 0;
5762 417a6e49 2020-09-26 stsp * Don't let the user create a tag name with a leading '-'.
5763 417a6e49 2020-09-26 stsp * While technically a valid reference name, this case is usually
5764 417a6e49 2020-09-26 stsp * an unintended typo.
5766 417a6e49 2020-09-26 stsp if (tag_name[0] == '-')
5767 417a6e49 2020-09-26 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5769 417a6e49 2020-09-26 stsp err = get_author(&tagger, repo, worktree);
5771 417a6e49 2020-09-26 stsp return err;
5773 417a6e49 2020-09-26 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5774 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5776 417a6e49 2020-09-26 stsp goto done;
5778 417a6e49 2020-09-26 stsp err = got_object_id_str(&commit_id_str, commit_id);
5780 417a6e49 2020-09-26 stsp goto done;
5782 417a6e49 2020-09-26 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5783 417a6e49 2020-09-26 stsp refname = strdup(tag_name);
5784 417a6e49 2020-09-26 stsp if (refname == NULL) {
5785 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
5786 417a6e49 2020-09-26 stsp goto done;
5788 417a6e49 2020-09-26 stsp tag_name += 10;
5789 417a6e49 2020-09-26 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5790 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
5791 417a6e49 2020-09-26 stsp goto done;
5794 417a6e49 2020-09-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5795 417a6e49 2020-09-26 stsp if (err == NULL) {
5796 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5797 417a6e49 2020-09-26 stsp goto done;
5798 417a6e49 2020-09-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5799 417a6e49 2020-09-26 stsp goto done;
5801 417a6e49 2020-09-26 stsp if (tagmsg_arg == NULL) {
5802 417a6e49 2020-09-26 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5803 417a6e49 2020-09-26 stsp tag_name, got_repo_get_path(repo));
5804 417a6e49 2020-09-26 stsp if (err) {
5805 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5806 417a6e49 2020-09-26 stsp tagmsg_path != NULL)
5807 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5808 417a6e49 2020-09-26 stsp goto done;
5812 417a6e49 2020-09-26 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5813 417a6e49 2020-09-26 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5814 417a6e49 2020-09-26 stsp if (err) {
5815 417a6e49 2020-09-26 stsp if (tagmsg_path)
5816 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5817 417a6e49 2020-09-26 stsp goto done;
5820 417a6e49 2020-09-26 stsp err = got_ref_alloc(&ref, refname, tag_id);
5821 417a6e49 2020-09-26 stsp if (err) {
5822 417a6e49 2020-09-26 stsp if (tagmsg_path)
5823 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5824 417a6e49 2020-09-26 stsp goto done;
5827 417a6e49 2020-09-26 stsp err = got_ref_write(ref, repo);
5828 417a6e49 2020-09-26 stsp if (err) {
5829 417a6e49 2020-09-26 stsp if (tagmsg_path)
5830 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5831 417a6e49 2020-09-26 stsp goto done;
5834 417a6e49 2020-09-26 stsp err = got_object_id_str(&tag_id_str, tag_id);
5835 417a6e49 2020-09-26 stsp if (err) {
5836 417a6e49 2020-09-26 stsp if (tagmsg_path)
5837 417a6e49 2020-09-26 stsp preserve_tagmsg = 1;
5838 417a6e49 2020-09-26 stsp goto done;
5840 417a6e49 2020-09-26 stsp printf("Created tag %s\n", tag_id_str);
5842 417a6e49 2020-09-26 stsp if (preserve_tagmsg) {
5843 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5844 417a6e49 2020-09-26 stsp getprogname(), tagmsg_path);
5845 417a6e49 2020-09-26 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5846 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5847 417a6e49 2020-09-26 stsp free(tag_id_str);
5849 417a6e49 2020-09-26 stsp got_ref_close(ref);
5850 417a6e49 2020-09-26 stsp free(commit_id);
5851 417a6e49 2020-09-26 stsp free(commit_id_str);
5852 417a6e49 2020-09-26 stsp free(refname);
5853 417a6e49 2020-09-26 stsp free(tagmsg);
5854 417a6e49 2020-09-26 stsp free(tagmsg_path);
5855 417a6e49 2020-09-26 stsp free(tagger);
5856 417a6e49 2020-09-26 stsp return err;
5859 417a6e49 2020-09-26 stsp static const struct got_error *
5860 417a6e49 2020-09-26 stsp cmd_tag(int argc, char *argv[])
5862 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
5863 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
5864 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
5865 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5866 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL;
5867 417a6e49 2020-09-26 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5868 417a6e49 2020-09-26 stsp int ch, do_list = 0;
5870 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5871 417a6e49 2020-09-26 stsp switch (ch) {
5873 417a6e49 2020-09-26 stsp commit_id_arg = optarg;
5876 417a6e49 2020-09-26 stsp tagmsg = optarg;
5879 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
5880 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5881 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
5883 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
5886 417a6e49 2020-09-26 stsp do_list = 1;
5889 417a6e49 2020-09-26 stsp usage_tag();
5890 417a6e49 2020-09-26 stsp /* NOTREACHED */
5894 417a6e49 2020-09-26 stsp argc -= optind;
5895 417a6e49 2020-09-26 stsp argv += optind;
5897 417a6e49 2020-09-26 stsp if (do_list) {
5898 417a6e49 2020-09-26 stsp if (commit_id_arg != NULL)
5900 417a6e49 2020-09-26 stsp "-c option can only be used when creating a tag");
5901 417a6e49 2020-09-26 stsp if (tagmsg)
5902 417a6e49 2020-09-26 stsp errx(1, "-l and -m options are mutually exclusive");
5903 417a6e49 2020-09-26 stsp if (argc > 0)
5904 417a6e49 2020-09-26 stsp usage_tag();
5905 417a6e49 2020-09-26 stsp } else if (argc != 1)
5906 417a6e49 2020-09-26 stsp usage_tag();
5908 417a6e49 2020-09-26 stsp tag_name = argv[0];
5910 417a6e49 2020-09-26 stsp #ifndef PROFILE
5911 417a6e49 2020-09-26 stsp if (do_list) {
5912 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5913 417a6e49 2020-09-26 stsp NULL) == -1)
5914 417a6e49 2020-09-26 stsp err(1, "pledge");
5916 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5917 417a6e49 2020-09-26 stsp "sendfd unveil", NULL) == -1)
5918 417a6e49 2020-09-26 stsp err(1, "pledge");
5921 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
5922 417a6e49 2020-09-26 stsp if (cwd == NULL) {
5923 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
5924 417a6e49 2020-09-26 stsp goto done;
5927 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5928 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
5929 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5930 417a6e49 2020-09-26 stsp goto done;
5932 417a6e49 2020-09-26 stsp error = NULL;
5933 417a6e49 2020-09-26 stsp if (worktree) {
5934 417a6e49 2020-09-26 stsp repo_path =
5935 417a6e49 2020-09-26 stsp strdup(got_worktree_get_repo_path(worktree));
5936 417a6e49 2020-09-26 stsp if (repo_path == NULL)
5937 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5938 417a6e49 2020-09-26 stsp if (error)
5939 417a6e49 2020-09-26 stsp goto done;
5941 417a6e49 2020-09-26 stsp repo_path = strdup(cwd);
5942 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
5943 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
5944 417a6e49 2020-09-26 stsp goto done;
5949 417a6e49 2020-09-26 stsp if (do_list) {
5950 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
5951 417a6e49 2020-09-26 stsp if (error != NULL)
5952 417a6e49 2020-09-26 stsp goto done;
5953 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5954 417a6e49 2020-09-26 stsp if (error)
5955 417a6e49 2020-09-26 stsp goto done;
5956 417a6e49 2020-09-26 stsp error = list_tags(repo, worktree);
5958 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
5959 417a6e49 2020-09-26 stsp if (error)
5960 417a6e49 2020-09-26 stsp goto done;
5961 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5962 417a6e49 2020-09-26 stsp if (error != NULL)
5963 417a6e49 2020-09-26 stsp goto done;
5965 417a6e49 2020-09-26 stsp if (tagmsg) {
5966 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5967 417a6e49 2020-09-26 stsp if (error)
5968 417a6e49 2020-09-26 stsp goto done;
5971 417a6e49 2020-09-26 stsp if (commit_id_arg == NULL) {
5972 417a6e49 2020-09-26 stsp struct got_reference *head_ref;
5973 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
5974 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
5975 417a6e49 2020-09-26 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5976 417a6e49 2020-09-26 stsp : GOT_REF_HEAD, 0);
5977 417a6e49 2020-09-26 stsp if (error)
5978 417a6e49 2020-09-26 stsp goto done;
5979 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5980 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
5981 417a6e49 2020-09-26 stsp if (error)
5982 417a6e49 2020-09-26 stsp goto done;
5983 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
5984 417a6e49 2020-09-26 stsp free(commit_id);
5985 417a6e49 2020-09-26 stsp if (error)
5986 417a6e49 2020-09-26 stsp goto done;
5989 417a6e49 2020-09-26 stsp error = add_tag(repo, worktree, tag_name,
5990 417a6e49 2020-09-26 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5994 417a6e49 2020-09-26 stsp got_repo_close(repo);
5995 417a6e49 2020-09-26 stsp if (worktree)
5996 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
5997 417a6e49 2020-09-26 stsp free(cwd);
5998 417a6e49 2020-09-26 stsp free(repo_path);
5999 417a6e49 2020-09-26 stsp free(gitconfig_path);
6000 417a6e49 2020-09-26 stsp free(commit_id_str);
6001 417a6e49 2020-09-26 stsp return error;
6004 417a6e49 2020-09-26 stsp __dead static void
6005 417a6e49 2020-09-26 stsp usage_add(void)
6007 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6008 417a6e49 2020-09-26 stsp getprogname());
6012 417a6e49 2020-09-26 stsp static const struct got_error *
6013 417a6e49 2020-09-26 stsp add_progress(void *arg, unsigned char status, const char *path)
6015 417a6e49 2020-09-26 stsp while (path[0] == '/')
6017 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6018 417a6e49 2020-09-26 stsp return NULL;
6021 417a6e49 2020-09-26 stsp static const struct got_error *
6022 417a6e49 2020-09-26 stsp cmd_add(int argc, char *argv[])
6024 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6025 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6026 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6027 417a6e49 2020-09-26 stsp char *cwd = NULL;
6028 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6029 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6030 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, no_ignores = 0;
6032 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6034 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "IR")) != -1) {
6035 417a6e49 2020-09-26 stsp switch (ch) {
6037 417a6e49 2020-09-26 stsp no_ignores = 1;
6040 417a6e49 2020-09-26 stsp can_recurse = 1;
6043 417a6e49 2020-09-26 stsp usage_add();
6044 417a6e49 2020-09-26 stsp /* NOTREACHED */
6048 417a6e49 2020-09-26 stsp argc -= optind;
6049 417a6e49 2020-09-26 stsp argv += optind;
6051 417a6e49 2020-09-26 stsp #ifndef PROFILE
6052 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6053 417a6e49 2020-09-26 stsp NULL) == -1)
6054 417a6e49 2020-09-26 stsp err(1, "pledge");
6056 417a6e49 2020-09-26 stsp if (argc < 1)
6057 417a6e49 2020-09-26 stsp usage_add();
6059 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6060 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6061 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6062 417a6e49 2020-09-26 stsp goto done;
6065 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6066 417a6e49 2020-09-26 stsp if (error) {
6067 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6068 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "add", cwd);
6069 417a6e49 2020-09-26 stsp goto done;
6072 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6074 417a6e49 2020-09-26 stsp if (error != NULL)
6075 417a6e49 2020-09-26 stsp goto done;
6077 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6078 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6079 417a6e49 2020-09-26 stsp if (error)
6080 417a6e49 2020-09-26 stsp goto done;
6082 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6083 417a6e49 2020-09-26 stsp if (error)
6084 417a6e49 2020-09-26 stsp goto done;
6086 417a6e49 2020-09-26 stsp if (!can_recurse && no_ignores) {
6087 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6088 417a6e49 2020-09-26 stsp "disregarding ignores requires -R option");
6089 417a6e49 2020-09-26 stsp goto done;
6093 417a6e49 2020-09-26 stsp if (!can_recurse) {
6094 417a6e49 2020-09-26 stsp char *ondisk_path;
6095 417a6e49 2020-09-26 stsp struct stat sb;
6096 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6097 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6098 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6099 417a6e49 2020-09-26 stsp pe->path) == -1) {
6100 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6101 417a6e49 2020-09-26 stsp goto done;
6103 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6104 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6105 417a6e49 2020-09-26 stsp free(ondisk_path);
6108 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6109 417a6e49 2020-09-26 stsp ondisk_path);
6110 417a6e49 2020-09-26 stsp free(ondisk_path);
6111 417a6e49 2020-09-26 stsp goto done;
6113 417a6e49 2020-09-26 stsp free(ondisk_path);
6114 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6115 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6116 417a6e49 2020-09-26 stsp "adding directories requires -R option");
6117 417a6e49 2020-09-26 stsp goto done;
6122 417a6e49 2020-09-26 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6123 417a6e49 2020-09-26 stsp NULL, repo, no_ignores);
6126 417a6e49 2020-09-26 stsp got_repo_close(repo);
6127 417a6e49 2020-09-26 stsp if (worktree)
6128 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6129 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6130 417a6e49 2020-09-26 stsp free((char *)pe->path);
6131 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6132 417a6e49 2020-09-26 stsp free(cwd);
6133 417a6e49 2020-09-26 stsp return error;
6136 417a6e49 2020-09-26 stsp __dead static void
6137 417a6e49 2020-09-26 stsp usage_remove(void)
6139 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6140 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6144 417a6e49 2020-09-26 stsp static const struct got_error *
6145 417a6e49 2020-09-26 stsp print_remove_status(void *arg, unsigned char status,
6146 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path)
6148 417a6e49 2020-09-26 stsp while (path[0] == '/')
6150 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_NONEXISTENT)
6151 417a6e49 2020-09-26 stsp return NULL;
6152 417a6e49 2020-09-26 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6153 417a6e49 2020-09-26 stsp status = GOT_STATUS_NO_CHANGE;
6154 417a6e49 2020-09-26 stsp printf("%c%c %s\n", status, staged_status, path);
6155 417a6e49 2020-09-26 stsp return NULL;
6158 417a6e49 2020-09-26 stsp static const struct got_error *
6159 417a6e49 2020-09-26 stsp cmd_remove(int argc, char *argv[])
6161 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6162 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6163 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6164 417a6e49 2020-09-26 stsp const char *status_codes = NULL;
6165 417a6e49 2020-09-26 stsp char *cwd = NULL;
6166 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6167 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6168 417a6e49 2020-09-26 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6170 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6172 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6173 417a6e49 2020-09-26 stsp switch (ch) {
6175 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6178 417a6e49 2020-09-26 stsp keep_on_disk = 1;
6181 417a6e49 2020-09-26 stsp can_recurse = 1;
6184 417a6e49 2020-09-26 stsp for (i = 0; i < strlen(optarg); i++) {
6185 417a6e49 2020-09-26 stsp switch (optarg[i]) {
6186 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6187 417a6e49 2020-09-26 stsp delete_local_mods = 1;
6189 417a6e49 2020-09-26 stsp case GOT_STATUS_MISSING:
6192 417a6e49 2020-09-26 stsp errx(1, "invalid status code '%c'",
6193 417a6e49 2020-09-26 stsp optarg[i]);
6196 417a6e49 2020-09-26 stsp status_codes = optarg;
6199 417a6e49 2020-09-26 stsp usage_remove();
6200 417a6e49 2020-09-26 stsp /* NOTREACHED */
6204 417a6e49 2020-09-26 stsp argc -= optind;
6205 417a6e49 2020-09-26 stsp argv += optind;
6207 417a6e49 2020-09-26 stsp #ifndef PROFILE
6208 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6209 417a6e49 2020-09-26 stsp NULL) == -1)
6210 417a6e49 2020-09-26 stsp err(1, "pledge");
6212 417a6e49 2020-09-26 stsp if (argc < 1)
6213 417a6e49 2020-09-26 stsp usage_remove();
6215 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6216 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6217 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6218 417a6e49 2020-09-26 stsp goto done;
6220 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6221 417a6e49 2020-09-26 stsp if (error) {
6222 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6223 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6224 417a6e49 2020-09-26 stsp goto done;
6227 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6229 417a6e49 2020-09-26 stsp if (error)
6230 417a6e49 2020-09-26 stsp goto done;
6232 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6233 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6234 417a6e49 2020-09-26 stsp if (error)
6235 417a6e49 2020-09-26 stsp goto done;
6237 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6238 417a6e49 2020-09-26 stsp if (error)
6239 417a6e49 2020-09-26 stsp goto done;
6241 417a6e49 2020-09-26 stsp if (!can_recurse) {
6242 417a6e49 2020-09-26 stsp char *ondisk_path;
6243 417a6e49 2020-09-26 stsp struct stat sb;
6244 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6245 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6246 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6247 417a6e49 2020-09-26 stsp pe->path) == -1) {
6248 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6249 417a6e49 2020-09-26 stsp goto done;
6251 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6252 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6253 417a6e49 2020-09-26 stsp free(ondisk_path);
6256 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6257 417a6e49 2020-09-26 stsp ondisk_path);
6258 417a6e49 2020-09-26 stsp free(ondisk_path);
6259 417a6e49 2020-09-26 stsp goto done;
6261 417a6e49 2020-09-26 stsp free(ondisk_path);
6262 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6263 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6264 417a6e49 2020-09-26 stsp "removing directories requires -R option");
6265 417a6e49 2020-09-26 stsp goto done;
6270 417a6e49 2020-09-26 stsp error = got_worktree_schedule_delete(worktree, &paths,
6271 417a6e49 2020-09-26 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6272 417a6e49 2020-09-26 stsp repo, keep_on_disk);
6275 417a6e49 2020-09-26 stsp got_repo_close(repo);
6276 417a6e49 2020-09-26 stsp if (worktree)
6277 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6278 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
6279 417a6e49 2020-09-26 stsp free((char *)pe->path);
6280 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
6281 417a6e49 2020-09-26 stsp free(cwd);
6282 417a6e49 2020-09-26 stsp return error;
6285 417a6e49 2020-09-26 stsp __dead static void
6286 417a6e49 2020-09-26 stsp usage_revert(void)
6288 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6289 417a6e49 2020-09-26 stsp "path ...\n", getprogname());
6293 417a6e49 2020-09-26 stsp static const struct got_error *
6294 417a6e49 2020-09-26 stsp revert_progress(void *arg, unsigned char status, const char *path)
6296 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_UNVERSIONED)
6297 417a6e49 2020-09-26 stsp return NULL;
6299 417a6e49 2020-09-26 stsp while (path[0] == '/')
6301 417a6e49 2020-09-26 stsp printf("%c %s\n", status, path);
6302 417a6e49 2020-09-26 stsp return NULL;
6305 417a6e49 2020-09-26 stsp struct choose_patch_arg {
6306 417a6e49 2020-09-26 stsp FILE *patch_script_file;
6307 417a6e49 2020-09-26 stsp const char *action;
6310 417a6e49 2020-09-26 stsp static const struct got_error *
6311 417a6e49 2020-09-26 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6312 417a6e49 2020-09-26 stsp int nchanges, const char *action)
6314 417a6e49 2020-09-26 stsp char *line = NULL;
6315 417a6e49 2020-09-26 stsp size_t linesize = 0;
6316 417a6e49 2020-09-26 stsp ssize_t linelen;
6318 417a6e49 2020-09-26 stsp switch (status) {
6319 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
6320 417a6e49 2020-09-26 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6322 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
6323 417a6e49 2020-09-26 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6325 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
6326 417a6e49 2020-09-26 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6327 417a6e49 2020-09-26 stsp return got_error_from_errno("fseek");
6328 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6329 417a6e49 2020-09-26 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6330 417a6e49 2020-09-26 stsp printf("%s", line);
6331 417a6e49 2020-09-26 stsp if (ferror(patch_file))
6332 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6333 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
6334 417a6e49 2020-09-26 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6335 417a6e49 2020-09-26 stsp path, n, nchanges, action);
6338 417a6e49 2020-09-26 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6341 417a6e49 2020-09-26 stsp return NULL;
6344 417a6e49 2020-09-26 stsp static const struct got_error *
6345 417a6e49 2020-09-26 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6346 417a6e49 2020-09-26 stsp FILE *patch_file, int n, int nchanges)
6348 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6349 417a6e49 2020-09-26 stsp char *line = NULL;
6350 417a6e49 2020-09-26 stsp size_t linesize = 0;
6351 417a6e49 2020-09-26 stsp ssize_t linelen;
6352 417a6e49 2020-09-26 stsp int resp = ' ';
6353 417a6e49 2020-09-26 stsp struct choose_patch_arg *a = arg;
6355 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NONE;
6357 417a6e49 2020-09-26 stsp if (a->patch_script_file) {
6359 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6360 417a6e49 2020-09-26 stsp a->action);
6362 417a6e49 2020-09-26 stsp return err;
6363 417a6e49 2020-09-26 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6364 417a6e49 2020-09-26 stsp if (linelen == -1) {
6365 417a6e49 2020-09-26 stsp if (ferror(a->patch_script_file))
6366 417a6e49 2020-09-26 stsp return got_error_from_errno("getline");
6367 417a6e49 2020-09-26 stsp return NULL;
6369 417a6e49 2020-09-26 stsp nl = strchr(line, '\n');
6371 417a6e49 2020-09-26 stsp *nl = '\0';
6372 417a6e49 2020-09-26 stsp if (strcmp(line, "y") == 0) {
6373 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6374 417a6e49 2020-09-26 stsp printf("y\n");
6375 417a6e49 2020-09-26 stsp } else if (strcmp(line, "n") == 0) {
6376 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6377 417a6e49 2020-09-26 stsp printf("n\n");
6378 417a6e49 2020-09-26 stsp } else if (strcmp(line, "q") == 0 &&
6379 417a6e49 2020-09-26 stsp status == GOT_STATUS_MODIFY) {
6380 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6381 417a6e49 2020-09-26 stsp printf("q\n");
6383 417a6e49 2020-09-26 stsp printf("invalid response '%s'\n", line);
6384 417a6e49 2020-09-26 stsp free(line);
6385 417a6e49 2020-09-26 stsp return NULL;
6388 417a6e49 2020-09-26 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6389 417a6e49 2020-09-26 stsp err = show_change(status, path, patch_file, n, nchanges,
6390 417a6e49 2020-09-26 stsp a->action);
6392 417a6e49 2020-09-26 stsp return err;
6393 417a6e49 2020-09-26 stsp resp = getchar();
6394 417a6e49 2020-09-26 stsp if (resp == '\n')
6395 417a6e49 2020-09-26 stsp resp = getchar();
6396 417a6e49 2020-09-26 stsp if (status == GOT_STATUS_MODIFY) {
6397 417a6e49 2020-09-26 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6398 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6399 417a6e49 2020-09-26 stsp resp = ' ';
6401 417a6e49 2020-09-26 stsp } else if (resp != 'y' && resp != 'n') {
6402 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
6403 417a6e49 2020-09-26 stsp resp = ' ';
6407 417a6e49 2020-09-26 stsp if (resp == 'y')
6408 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_YES;
6409 417a6e49 2020-09-26 stsp else if (resp == 'n')
6410 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_NO;
6411 417a6e49 2020-09-26 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6412 417a6e49 2020-09-26 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6414 417a6e49 2020-09-26 stsp return NULL;
6418 417a6e49 2020-09-26 stsp static const struct got_error *
6419 417a6e49 2020-09-26 stsp cmd_revert(int argc, char *argv[])
6421 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6422 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6423 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6424 417a6e49 2020-09-26 stsp char *cwd = NULL, *path = NULL;
6425 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6426 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6427 417a6e49 2020-09-26 stsp int ch, can_recurse = 0, pflag = 0;
6428 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
6429 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
6430 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
6432 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6434 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6435 417a6e49 2020-09-26 stsp switch (ch) {
6437 417a6e49 2020-09-26 stsp pflag = 1;
6440 417a6e49 2020-09-26 stsp patch_script_path = optarg;
6443 417a6e49 2020-09-26 stsp can_recurse = 1;
6446 417a6e49 2020-09-26 stsp usage_revert();
6447 417a6e49 2020-09-26 stsp /* NOTREACHED */
6451 417a6e49 2020-09-26 stsp argc -= optind;
6452 417a6e49 2020-09-26 stsp argv += optind;
6454 417a6e49 2020-09-26 stsp #ifndef PROFILE
6455 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6456 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6457 417a6e49 2020-09-26 stsp err(1, "pledge");
6459 417a6e49 2020-09-26 stsp if (argc < 1)
6460 417a6e49 2020-09-26 stsp usage_revert();
6461 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
6462 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
6464 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6465 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6466 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6467 417a6e49 2020-09-26 stsp goto done;
6469 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6470 417a6e49 2020-09-26 stsp if (error) {
6471 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6472 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6473 417a6e49 2020-09-26 stsp goto done;
6476 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6478 417a6e49 2020-09-26 stsp if (error != NULL)
6479 417a6e49 2020-09-26 stsp goto done;
6481 417a6e49 2020-09-26 stsp if (patch_script_path) {
6482 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
6483 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
6484 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
6485 417a6e49 2020-09-26 stsp patch_script_path);
6486 417a6e49 2020-09-26 stsp goto done;
6489 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6490 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6491 417a6e49 2020-09-26 stsp if (error)
6492 417a6e49 2020-09-26 stsp goto done;
6494 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6495 417a6e49 2020-09-26 stsp if (error)
6496 417a6e49 2020-09-26 stsp goto done;
6498 417a6e49 2020-09-26 stsp if (!can_recurse) {
6499 417a6e49 2020-09-26 stsp char *ondisk_path;
6500 417a6e49 2020-09-26 stsp struct stat sb;
6501 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
6502 417a6e49 2020-09-26 stsp if (asprintf(&ondisk_path, "%s/%s",
6503 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree),
6504 417a6e49 2020-09-26 stsp pe->path) == -1) {
6505 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
6506 417a6e49 2020-09-26 stsp goto done;
6508 417a6e49 2020-09-26 stsp if (lstat(ondisk_path, &sb) == -1) {
6509 417a6e49 2020-09-26 stsp if (errno == ENOENT) {
6510 417a6e49 2020-09-26 stsp free(ondisk_path);
6513 417a6e49 2020-09-26 stsp error = got_error_from_errno2("lstat",
6514 417a6e49 2020-09-26 stsp ondisk_path);
6515 417a6e49 2020-09-26 stsp free(ondisk_path);
6516 417a6e49 2020-09-26 stsp goto done;
6518 417a6e49 2020-09-26 stsp free(ondisk_path);
6519 417a6e49 2020-09-26 stsp if (S_ISDIR(sb.st_mode)) {
6520 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6521 417a6e49 2020-09-26 stsp "reverting directories requires -R option");
6522 417a6e49 2020-09-26 stsp goto done;
6527 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
6528 417a6e49 2020-09-26 stsp cpa.action = "revert";
6529 417a6e49 2020-09-26 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6530 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa, repo);
6532 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6533 417a6e49 2020-09-26 stsp error == NULL)
6534 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
6536 417a6e49 2020-09-26 stsp got_repo_close(repo);
6537 417a6e49 2020-09-26 stsp if (worktree)
6538 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6539 417a6e49 2020-09-26 stsp free(path);
6540 417a6e49 2020-09-26 stsp free(cwd);
6541 417a6e49 2020-09-26 stsp return error;
6544 417a6e49 2020-09-26 stsp __dead static void
6545 417a6e49 2020-09-26 stsp usage_commit(void)
6547 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6548 417a6e49 2020-09-26 stsp getprogname());
6552 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg {
6553 417a6e49 2020-09-26 stsp const char *cmdline_log;
6554 417a6e49 2020-09-26 stsp const char *editor;
6555 417a6e49 2020-09-26 stsp const char *worktree_path;
6556 417a6e49 2020-09-26 stsp const char *branch_name;
6557 417a6e49 2020-09-26 stsp const char *repo_path;
6558 417a6e49 2020-09-26 stsp char *logmsg_path;
6562 417a6e49 2020-09-26 stsp static const struct got_error *
6563 417a6e49 2020-09-26 stsp collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6564 417a6e49 2020-09-26 stsp void *arg)
6566 417a6e49 2020-09-26 stsp char *initial_content = NULL;
6567 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
6568 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
6569 417a6e49 2020-09-26 stsp char *template = NULL;
6570 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg *a = arg;
6571 417a6e49 2020-09-26 stsp int initial_content_len;
6572 417a6e49 2020-09-26 stsp int fd = -1;
6573 417a6e49 2020-09-26 stsp size_t len;
6575 417a6e49 2020-09-26 stsp /* if a message was specified on the command line, just use it */
6576 417a6e49 2020-09-26 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6577 417a6e49 2020-09-26 stsp len = strlen(a->cmdline_log) + 1;
6578 417a6e49 2020-09-26 stsp *logmsg = malloc(len + 1);
6579 417a6e49 2020-09-26 stsp if (*logmsg == NULL)
6580 417a6e49 2020-09-26 stsp return got_error_from_errno("malloc");
6581 417a6e49 2020-09-26 stsp strlcpy(*logmsg, a->cmdline_log, len);
6582 417a6e49 2020-09-26 stsp return NULL;
6585 417a6e49 2020-09-26 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6586 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6588 417a6e49 2020-09-26 stsp initial_content_len = asprintf(&initial_content,
6589 417a6e49 2020-09-26 stsp "\n# changes to be committed on branch %s:\n",
6590 417a6e49 2020-09-26 stsp a->branch_name);
6591 417a6e49 2020-09-26 stsp if (initial_content_len == -1)
6592 417a6e49 2020-09-26 stsp return got_error_from_errno("asprintf");
6594 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6596 417a6e49 2020-09-26 stsp goto done;
6598 417a6e49 2020-09-26 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6599 417a6e49 2020-09-26 stsp err = got_error_from_errno2("write", a->logmsg_path);
6600 417a6e49 2020-09-26 stsp goto done;
6603 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, commitable_paths, entry) {
6604 417a6e49 2020-09-26 stsp struct got_commitable *ct = pe->data;
6605 417a6e49 2020-09-26 stsp dprintf(fd, "# %c %s\n",
6606 417a6e49 2020-09-26 stsp got_commitable_get_status(ct),
6607 417a6e49 2020-09-26 stsp got_commitable_get_path(ct));
6610 417a6e49 2020-09-26 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6612 417a6e49 2020-09-26 stsp free(initial_content);
6613 417a6e49 2020-09-26 stsp free(template);
6615 417a6e49 2020-09-26 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6616 417a6e49 2020-09-26 stsp err = got_error_from_errno2("close", a->logmsg_path);
6618 417a6e49 2020-09-26 stsp /* Editor is done; we can now apply unveil(2) */
6619 417a6e49 2020-09-26 stsp if (err == NULL)
6620 417a6e49 2020-09-26 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6621 417a6e49 2020-09-26 stsp if (err) {
6622 417a6e49 2020-09-26 stsp free(*logmsg);
6623 417a6e49 2020-09-26 stsp *logmsg = NULL;
6625 417a6e49 2020-09-26 stsp return err;
6628 417a6e49 2020-09-26 stsp static const struct got_error *
6629 417a6e49 2020-09-26 stsp cmd_commit(int argc, char *argv[])
6631 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6632 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6633 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6634 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
6635 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL;
6636 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
6637 417a6e49 2020-09-26 stsp struct collect_commit_logmsg_arg cl_arg;
6638 417a6e49 2020-09-26 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6639 417a6e49 2020-09-26 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6640 417a6e49 2020-09-26 stsp int allow_bad_symlinks = 0;
6641 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
6643 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
6644 417a6e49 2020-09-26 stsp cl_arg.logmsg_path = NULL;
6646 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6647 417a6e49 2020-09-26 stsp switch (ch) {
6649 417a6e49 2020-09-26 stsp logmsg = optarg;
6652 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
6655 417a6e49 2020-09-26 stsp usage_commit();
6656 417a6e49 2020-09-26 stsp /* NOTREACHED */
6660 417a6e49 2020-09-26 stsp argc -= optind;
6661 417a6e49 2020-09-26 stsp argv += optind;
6663 417a6e49 2020-09-26 stsp #ifndef PROFILE
6664 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6665 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6666 417a6e49 2020-09-26 stsp err(1, "pledge");
6668 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6669 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6670 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6671 417a6e49 2020-09-26 stsp goto done;
6673 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6674 417a6e49 2020-09-26 stsp if (error) {
6675 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6676 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6677 417a6e49 2020-09-26 stsp goto done;
6680 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6681 417a6e49 2020-09-26 stsp if (error)
6682 417a6e49 2020-09-26 stsp goto done;
6683 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
6684 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
6685 417a6e49 2020-09-26 stsp goto done;
6688 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6689 417a6e49 2020-09-26 stsp worktree);
6690 417a6e49 2020-09-26 stsp if (error)
6691 417a6e49 2020-09-26 stsp goto done;
6693 417a6e49 2020-09-26 stsp error = get_gitconfig_path(&gitconfig_path);
6694 417a6e49 2020-09-26 stsp if (error)
6695 417a6e49 2020-09-26 stsp goto done;
6696 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6697 417a6e49 2020-09-26 stsp gitconfig_path);
6698 417a6e49 2020-09-26 stsp if (error != NULL)
6699 417a6e49 2020-09-26 stsp goto done;
6701 417a6e49 2020-09-26 stsp error = get_author(&author, repo, worktree);
6702 417a6e49 2020-09-26 stsp if (error)
6703 417a6e49 2020-09-26 stsp return error;
6706 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have
6707 417a6e49 2020-09-26 stsp * to apply unveil after the log message has been written.
6709 417a6e49 2020-09-26 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6710 417a6e49 2020-09-26 stsp error = get_editor(&editor);
6712 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6713 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6714 417a6e49 2020-09-26 stsp if (error)
6715 417a6e49 2020-09-26 stsp goto done;
6717 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6718 417a6e49 2020-09-26 stsp if (error)
6719 417a6e49 2020-09-26 stsp goto done;
6721 417a6e49 2020-09-26 stsp cl_arg.editor = editor;
6722 417a6e49 2020-09-26 stsp cl_arg.cmdline_log = logmsg;
6723 417a6e49 2020-09-26 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6724 417a6e49 2020-09-26 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6725 417a6e49 2020-09-26 stsp if (!histedit_in_progress) {
6726 417a6e49 2020-09-26 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6727 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6728 417a6e49 2020-09-26 stsp goto done;
6730 417a6e49 2020-09-26 stsp cl_arg.branch_name += 11;
6732 417a6e49 2020-09-26 stsp cl_arg.repo_path = got_repo_get_path(repo);
6733 417a6e49 2020-09-26 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6734 417a6e49 2020-09-26 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6735 417a6e49 2020-09-26 stsp print_status, NULL, repo);
6736 417a6e49 2020-09-26 stsp if (error) {
6737 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6738 417a6e49 2020-09-26 stsp cl_arg.logmsg_path != NULL)
6739 417a6e49 2020-09-26 stsp preserve_logmsg = 1;
6740 417a6e49 2020-09-26 stsp goto done;
6743 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, id);
6744 417a6e49 2020-09-26 stsp if (error)
6745 417a6e49 2020-09-26 stsp goto done;
6746 417a6e49 2020-09-26 stsp printf("Created commit %s\n", id_str);
6748 417a6e49 2020-09-26 stsp if (preserve_logmsg) {
6749 417a6e49 2020-09-26 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6750 417a6e49 2020-09-26 stsp getprogname(), cl_arg.logmsg_path);
6751 417a6e49 2020-09-26 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6752 417a6e49 2020-09-26 stsp error == NULL)
6753 417a6e49 2020-09-26 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6754 417a6e49 2020-09-26 stsp free(cl_arg.logmsg_path);
6756 417a6e49 2020-09-26 stsp got_repo_close(repo);
6757 417a6e49 2020-09-26 stsp if (worktree)
6758 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6759 417a6e49 2020-09-26 stsp free(cwd);
6760 417a6e49 2020-09-26 stsp free(id_str);
6761 417a6e49 2020-09-26 stsp free(gitconfig_path);
6762 417a6e49 2020-09-26 stsp free(editor);
6763 417a6e49 2020-09-26 stsp free(author);
6764 417a6e49 2020-09-26 stsp return error;
6767 417a6e49 2020-09-26 stsp __dead static void
6768 417a6e49 2020-09-26 stsp usage_cherrypick(void)
6770 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6774 417a6e49 2020-09-26 stsp static const struct got_error *
6775 417a6e49 2020-09-26 stsp cmd_cherrypick(int argc, char *argv[])
6777 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6778 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6779 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6780 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6781 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6782 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6783 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6784 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6786 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6788 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6789 417a6e49 2020-09-26 stsp switch (ch) {
6791 417a6e49 2020-09-26 stsp usage_cherrypick();
6792 417a6e49 2020-09-26 stsp /* NOTREACHED */
6796 417a6e49 2020-09-26 stsp argc -= optind;
6797 417a6e49 2020-09-26 stsp argv += optind;
6799 417a6e49 2020-09-26 stsp #ifndef PROFILE
6800 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6801 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6802 417a6e49 2020-09-26 stsp err(1, "pledge");
6804 417a6e49 2020-09-26 stsp if (argc != 1)
6805 417a6e49 2020-09-26 stsp usage_cherrypick();
6807 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6808 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6809 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6810 417a6e49 2020-09-26 stsp goto done;
6812 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6813 417a6e49 2020-09-26 stsp if (error) {
6814 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6815 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "cherrypick",
6817 417a6e49 2020-09-26 stsp goto done;
6820 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6822 417a6e49 2020-09-26 stsp if (error != NULL)
6823 417a6e49 2020-09-26 stsp goto done;
6825 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6826 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6827 417a6e49 2020-09-26 stsp if (error)
6828 417a6e49 2020-09-26 stsp goto done;
6830 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6831 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6832 417a6e49 2020-09-26 stsp if (error != NULL) {
6833 417a6e49 2020-09-26 stsp struct got_reference *ref;
6834 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6835 417a6e49 2020-09-26 stsp goto done;
6836 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6837 417a6e49 2020-09-26 stsp if (error != NULL)
6838 417a6e49 2020-09-26 stsp goto done;
6839 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6840 417a6e49 2020-09-26 stsp got_ref_close(ref);
6841 417a6e49 2020-09-26 stsp if (error != NULL)
6842 417a6e49 2020-09-26 stsp goto done;
6844 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6845 417a6e49 2020-09-26 stsp if (error)
6846 417a6e49 2020-09-26 stsp goto done;
6848 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6849 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6850 417a6e49 2020-09-26 stsp if (error != NULL)
6851 417a6e49 2020-09-26 stsp goto done;
6853 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6854 417a6e49 2020-09-26 stsp if (error) {
6855 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
6856 417a6e49 2020-09-26 stsp goto done;
6857 417a6e49 2020-09-26 stsp error = NULL;
6859 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6860 417a6e49 2020-09-26 stsp goto done;
6863 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6864 417a6e49 2020-09-26 stsp if (error)
6865 417a6e49 2020-09-26 stsp goto done;
6866 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6867 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6868 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6869 417a6e49 2020-09-26 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6871 417a6e49 2020-09-26 stsp if (error != NULL)
6872 417a6e49 2020-09-26 stsp goto done;
6874 417a6e49 2020-09-26 stsp if (upa.did_something)
6875 417a6e49 2020-09-26 stsp printf("Merged commit %s\n", commit_id_str);
6876 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6878 417a6e49 2020-09-26 stsp if (commit)
6879 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
6880 417a6e49 2020-09-26 stsp free(commit_id_str);
6881 417a6e49 2020-09-26 stsp if (head_ref)
6882 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
6883 417a6e49 2020-09-26 stsp if (worktree)
6884 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
6886 417a6e49 2020-09-26 stsp got_repo_close(repo);
6887 417a6e49 2020-09-26 stsp return error;
6890 417a6e49 2020-09-26 stsp __dead static void
6891 417a6e49 2020-09-26 stsp usage_backout(void)
6893 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6897 417a6e49 2020-09-26 stsp static const struct got_error *
6898 417a6e49 2020-09-26 stsp cmd_backout(int argc, char *argv[])
6900 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
6901 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
6902 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
6903 417a6e49 2020-09-26 stsp char *cwd = NULL, *commit_id_str = NULL;
6904 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
6905 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
6906 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
6907 417a6e49 2020-09-26 stsp struct got_reference *head_ref = NULL;
6909 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
6911 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6912 417a6e49 2020-09-26 stsp switch (ch) {
6914 417a6e49 2020-09-26 stsp usage_backout();
6915 417a6e49 2020-09-26 stsp /* NOTREACHED */
6919 417a6e49 2020-09-26 stsp argc -= optind;
6920 417a6e49 2020-09-26 stsp argv += optind;
6922 417a6e49 2020-09-26 stsp #ifndef PROFILE
6923 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6924 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
6925 417a6e49 2020-09-26 stsp err(1, "pledge");
6927 417a6e49 2020-09-26 stsp if (argc != 1)
6928 417a6e49 2020-09-26 stsp usage_backout();
6930 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
6931 417a6e49 2020-09-26 stsp if (cwd == NULL) {
6932 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
6933 417a6e49 2020-09-26 stsp goto done;
6935 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
6936 417a6e49 2020-09-26 stsp if (error) {
6937 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6938 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6939 417a6e49 2020-09-26 stsp goto done;
6942 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6944 417a6e49 2020-09-26 stsp if (error != NULL)
6945 417a6e49 2020-09-26 stsp goto done;
6947 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6948 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
6949 417a6e49 2020-09-26 stsp if (error)
6950 417a6e49 2020-09-26 stsp goto done;
6952 417a6e49 2020-09-26 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6953 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_COMMIT, repo);
6954 417a6e49 2020-09-26 stsp if (error != NULL) {
6955 417a6e49 2020-09-26 stsp struct got_reference *ref;
6956 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6957 417a6e49 2020-09-26 stsp goto done;
6958 417a6e49 2020-09-26 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6959 417a6e49 2020-09-26 stsp if (error != NULL)
6960 417a6e49 2020-09-26 stsp goto done;
6961 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, ref);
6962 417a6e49 2020-09-26 stsp got_ref_close(ref);
6963 417a6e49 2020-09-26 stsp if (error != NULL)
6964 417a6e49 2020-09-26 stsp goto done;
6966 417a6e49 2020-09-26 stsp error = got_object_id_str(&commit_id_str, commit_id);
6967 417a6e49 2020-09-26 stsp if (error)
6968 417a6e49 2020-09-26 stsp goto done;
6970 417a6e49 2020-09-26 stsp error = got_ref_open(&head_ref, repo,
6971 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
6972 417a6e49 2020-09-26 stsp if (error != NULL)
6973 417a6e49 2020-09-26 stsp goto done;
6975 417a6e49 2020-09-26 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6976 417a6e49 2020-09-26 stsp if (error)
6977 417a6e49 2020-09-26 stsp goto done;
6979 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6980 417a6e49 2020-09-26 stsp if (error)
6981 417a6e49 2020-09-26 stsp goto done;
6982 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6983 417a6e49 2020-09-26 stsp if (pid == NULL) {
6984 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6985 417a6e49 2020-09-26 stsp goto done;
6988 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
6989 417a6e49 2020-09-26 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6990 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
6991 417a6e49 2020-09-26 stsp if (error != NULL)
6992 417a6e49 2020-09-26 stsp goto done;
6994 417a6e49 2020-09-26 stsp if (upa.did_something)
6995 417a6e49 2020-09-26 stsp printf("Backed out commit %s\n", commit_id_str);
6996 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
6998 417a6e49 2020-09-26 stsp if (commit)
6999 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7000 417a6e49 2020-09-26 stsp free(commit_id_str);
7001 417a6e49 2020-09-26 stsp if (head_ref)
7002 417a6e49 2020-09-26 stsp got_ref_close(head_ref);
7003 417a6e49 2020-09-26 stsp if (worktree)
7004 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7006 417a6e49 2020-09-26 stsp got_repo_close(repo);
7007 417a6e49 2020-09-26 stsp return error;
7010 417a6e49 2020-09-26 stsp __dead static void
7011 417a6e49 2020-09-26 stsp usage_rebase(void)
7013 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7014 417a6e49 2020-09-26 stsp getprogname());
7019 417a6e49 2020-09-26 stsp trim_logmsg(char *logmsg, int limit)
7022 417a6e49 2020-09-26 stsp size_t len;
7024 417a6e49 2020-09-26 stsp len = strlen(logmsg);
7025 417a6e49 2020-09-26 stsp if (len > limit)
7026 417a6e49 2020-09-26 stsp len = limit;
7027 417a6e49 2020-09-26 stsp logmsg[len] = '\0';
7028 417a6e49 2020-09-26 stsp nl = strchr(logmsg, '\n');
7030 417a6e49 2020-09-26 stsp *nl = '\0';
7033 417a6e49 2020-09-26 stsp static const struct got_error *
7034 417a6e49 2020-09-26 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7036 417a6e49 2020-09-26 stsp const struct got_error *err;
7037 417a6e49 2020-09-26 stsp char *logmsg0 = NULL;
7038 417a6e49 2020-09-26 stsp const char *s;
7040 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7042 417a6e49 2020-09-26 stsp return err;
7044 417a6e49 2020-09-26 stsp s = logmsg0;
7045 417a6e49 2020-09-26 stsp while (isspace((unsigned char)s[0]))
7048 417a6e49 2020-09-26 stsp *logmsg = strdup(s);
7049 417a6e49 2020-09-26 stsp if (*logmsg == NULL) {
7050 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7051 417a6e49 2020-09-26 stsp goto done;
7054 417a6e49 2020-09-26 stsp trim_logmsg(*logmsg, limit);
7056 417a6e49 2020-09-26 stsp free(logmsg0);
7057 417a6e49 2020-09-26 stsp return err;
7060 417a6e49 2020-09-26 stsp static const struct got_error *
7061 417a6e49 2020-09-26 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7063 417a6e49 2020-09-26 stsp const struct got_error *err;
7064 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7065 417a6e49 2020-09-26 stsp char *id_str = NULL, *logmsg = NULL;
7067 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
7069 417a6e49 2020-09-26 stsp return err;
7071 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, id);
7073 417a6e49 2020-09-26 stsp goto done;
7075 417a6e49 2020-09-26 stsp id_str[12] = '\0';
7077 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7079 417a6e49 2020-09-26 stsp goto done;
7081 417a6e49 2020-09-26 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7083 417a6e49 2020-09-26 stsp free(id_str);
7084 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7085 417a6e49 2020-09-26 stsp free(logmsg);
7086 417a6e49 2020-09-26 stsp return err;
7089 417a6e49 2020-09-26 stsp static const struct got_error *
7090 417a6e49 2020-09-26 stsp show_rebase_progress(struct got_commit_object *commit,
7091 417a6e49 2020-09-26 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7093 417a6e49 2020-09-26 stsp const struct got_error *err;
7094 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7096 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, old_id);
7098 417a6e49 2020-09-26 stsp goto done;
7100 417a6e49 2020-09-26 stsp if (new_id) {
7101 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
7103 417a6e49 2020-09-26 stsp goto done;
7106 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
7107 417a6e49 2020-09-26 stsp if (new_id_str)
7108 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
7110 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
7112 417a6e49 2020-09-26 stsp goto done;
7114 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
7115 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7117 417a6e49 2020-09-26 stsp free(old_id_str);
7118 417a6e49 2020-09-26 stsp free(new_id_str);
7119 417a6e49 2020-09-26 stsp free(logmsg);
7120 417a6e49 2020-09-26 stsp return err;
7123 417a6e49 2020-09-26 stsp static const struct got_error *
7124 417a6e49 2020-09-26 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7125 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7126 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7128 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7129 417a6e49 2020-09-26 stsp return got_worktree_rebase_complete(worktree, fileindex,
7130 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, branch, repo);
7133 417a6e49 2020-09-26 stsp static const struct got_error *
7134 417a6e49 2020-09-26 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7135 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7136 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch,
7137 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, struct got_repository *repo)
7139 417a6e49 2020-09-26 stsp const struct got_error *error;
7140 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
7141 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
7143 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7144 417a6e49 2020-09-26 stsp if (error)
7145 417a6e49 2020-09-26 stsp return error;
7147 417a6e49 2020-09-26 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7148 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7149 417a6e49 2020-09-26 stsp if (error) {
7150 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7151 417a6e49 2020-09-26 stsp goto done;
7152 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, NULL);
7154 417a6e49 2020-09-26 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7155 417a6e49 2020-09-26 stsp free(new_commit_id);
7158 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7159 417a6e49 2020-09-26 stsp return error;
7162 417a6e49 2020-09-26 stsp struct check_path_prefix_arg {
7163 417a6e49 2020-09-26 stsp const char *path_prefix;
7164 417a6e49 2020-09-26 stsp size_t len;
7165 417a6e49 2020-09-26 stsp int errcode;
7168 417a6e49 2020-09-26 stsp static const struct got_error *
7169 417a6e49 2020-09-26 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7170 417a6e49 2020-09-26 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7171 417a6e49 2020-09-26 stsp struct got_object_id *id2, const char *path1, const char *path2,
7172 417a6e49 2020-09-26 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7174 417a6e49 2020-09-26 stsp struct check_path_prefix_arg *a = arg;
7176 417a6e49 2020-09-26 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7177 417a6e49 2020-09-26 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7178 417a6e49 2020-09-26 stsp return got_error(a->errcode);
7180 417a6e49 2020-09-26 stsp return NULL;
7183 417a6e49 2020-09-26 stsp static const struct got_error *
7184 417a6e49 2020-09-26 stsp check_path_prefix(struct got_object_id *parent_id,
7185 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, const char *path_prefix,
7186 417a6e49 2020-09-26 stsp int errcode, struct got_repository *repo)
7188 417a6e49 2020-09-26 stsp const struct got_error *err;
7189 417a6e49 2020-09-26 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7190 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7191 417a6e49 2020-09-26 stsp struct check_path_prefix_arg cpp_arg;
7193 417a6e49 2020-09-26 stsp if (got_path_is_root_dir(path_prefix))
7194 417a6e49 2020-09-26 stsp return NULL;
7196 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7198 417a6e49 2020-09-26 stsp goto done;
7200 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7202 417a6e49 2020-09-26 stsp goto done;
7204 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree1, repo,
7205 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(parent_commit));
7207 417a6e49 2020-09-26 stsp goto done;
7209 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree2, repo,
7210 417a6e49 2020-09-26 stsp got_object_commit_get_tree_id(commit));
7212 417a6e49 2020-09-26 stsp goto done;
7214 417a6e49 2020-09-26 stsp cpp_arg.path_prefix = path_prefix;
7215 417a6e49 2020-09-26 stsp while (cpp_arg.path_prefix[0] == '/')
7216 417a6e49 2020-09-26 stsp cpp_arg.path_prefix++;
7217 417a6e49 2020-09-26 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7218 417a6e49 2020-09-26 stsp cpp_arg.errcode = errcode;
7219 417a6e49 2020-09-26 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7220 417a6e49 2020-09-26 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7222 417a6e49 2020-09-26 stsp if (tree1)
7223 417a6e49 2020-09-26 stsp got_object_tree_close(tree1);
7224 417a6e49 2020-09-26 stsp if (tree2)
7225 417a6e49 2020-09-26 stsp got_object_tree_close(tree2);
7226 417a6e49 2020-09-26 stsp if (commit)
7227 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7228 417a6e49 2020-09-26 stsp if (parent_commit)
7229 417a6e49 2020-09-26 stsp got_object_commit_close(parent_commit);
7230 417a6e49 2020-09-26 stsp return err;
7233 417a6e49 2020-09-26 stsp static const struct got_error *
7234 417a6e49 2020-09-26 stsp collect_commits(struct got_object_id_queue *commits,
7235 417a6e49 2020-09-26 stsp struct got_object_id *initial_commit_id,
7236 417a6e49 2020-09-26 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7237 417a6e49 2020-09-26 stsp const char *path_prefix, int path_prefix_errcode,
7238 417a6e49 2020-09-26 stsp struct got_repository *repo)
7240 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7241 417a6e49 2020-09-26 stsp struct got_commit_graph *graph = NULL;
7242 417a6e49 2020-09-26 stsp struct got_object_id *parent_id = NULL;
7243 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7244 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = initial_commit_id;
7246 417a6e49 2020-09-26 stsp err = got_commit_graph_open(&graph, "/", 1);
7248 417a6e49 2020-09-26 stsp return err;
7250 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7251 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7253 417a6e49 2020-09-26 stsp goto done;
7254 417a6e49 2020-09-26 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7255 417a6e49 2020-09-26 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7256 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7257 417a6e49 2020-09-26 stsp if (err) {
7258 417a6e49 2020-09-26 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7259 417a6e49 2020-09-26 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7260 417a6e49 2020-09-26 stsp "ran out of commits to rebase before "
7261 417a6e49 2020-09-26 stsp "youngest common ancestor commit has "
7262 417a6e49 2020-09-26 stsp "been reached?!?");
7264 417a6e49 2020-09-26 stsp goto done;
7266 417a6e49 2020-09-26 stsp err = check_path_prefix(parent_id, commit_id,
7267 417a6e49 2020-09-26 stsp path_prefix, path_prefix_errcode, repo);
7269 417a6e49 2020-09-26 stsp goto done;
7271 417a6e49 2020-09-26 stsp err = got_object_qid_alloc(&qid, commit_id);
7273 417a6e49 2020-09-26 stsp goto done;
7274 417a6e49 2020-09-26 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7275 417a6e49 2020-09-26 stsp commit_id = parent_id;
7279 417a6e49 2020-09-26 stsp got_commit_graph_close(graph);
7280 417a6e49 2020-09-26 stsp return err;
7283 417a6e49 2020-09-26 stsp static const struct got_error *
7284 417a6e49 2020-09-26 stsp cmd_rebase(int argc, char *argv[])
7286 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
7287 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
7288 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
7289 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
7290 417a6e49 2020-09-26 stsp char *cwd = NULL;
7291 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
7292 417a6e49 2020-09-26 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7293 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7294 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
7295 417a6e49 2020-09-26 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7296 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7297 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7298 417a6e49 2020-09-26 stsp int histedit_in_progress = 0;
7299 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7300 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
7301 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
7302 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
7303 417a6e49 2020-09-26 stsp struct got_object_qid *qid, *pid;
7305 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
7306 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
7308 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7309 417a6e49 2020-09-26 stsp switch (ch) {
7311 417a6e49 2020-09-26 stsp abort_rebase = 1;
7314 417a6e49 2020-09-26 stsp continue_rebase = 1;
7317 417a6e49 2020-09-26 stsp usage_rebase();
7318 417a6e49 2020-09-26 stsp /* NOTREACHED */
7322 417a6e49 2020-09-26 stsp argc -= optind;
7323 417a6e49 2020-09-26 stsp argv += optind;
7325 417a6e49 2020-09-26 stsp #ifndef PROFILE
7326 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7327 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
7328 417a6e49 2020-09-26 stsp err(1, "pledge");
7330 417a6e49 2020-09-26 stsp if (abort_rebase && continue_rebase)
7331 417a6e49 2020-09-26 stsp usage_rebase();
7332 417a6e49 2020-09-26 stsp else if (abort_rebase || continue_rebase) {
7333 417a6e49 2020-09-26 stsp if (argc != 0)
7334 417a6e49 2020-09-26 stsp usage_rebase();
7335 417a6e49 2020-09-26 stsp } else if (argc != 1)
7336 417a6e49 2020-09-26 stsp usage_rebase();
7338 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
7339 417a6e49 2020-09-26 stsp if (cwd == NULL) {
7340 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
7341 417a6e49 2020-09-26 stsp goto done;
7343 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
7344 417a6e49 2020-09-26 stsp if (error) {
7345 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7346 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7347 417a6e49 2020-09-26 stsp goto done;
7350 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7352 417a6e49 2020-09-26 stsp if (error != NULL)
7353 417a6e49 2020-09-26 stsp goto done;
7355 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7356 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
7357 417a6e49 2020-09-26 stsp if (error)
7358 417a6e49 2020-09-26 stsp goto done;
7360 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7361 417a6e49 2020-09-26 stsp worktree);
7362 417a6e49 2020-09-26 stsp if (error)
7363 417a6e49 2020-09-26 stsp goto done;
7364 417a6e49 2020-09-26 stsp if (histedit_in_progress) {
7365 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7366 417a6e49 2020-09-26 stsp goto done;
7369 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7370 417a6e49 2020-09-26 stsp if (error)
7371 417a6e49 2020-09-26 stsp goto done;
7373 417a6e49 2020-09-26 stsp if (abort_rebase) {
7374 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7375 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7376 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7377 417a6e49 2020-09-26 stsp goto done;
7379 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7380 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7381 417a6e49 2020-09-26 stsp worktree, repo);
7382 417a6e49 2020-09-26 stsp if (error)
7383 417a6e49 2020-09-26 stsp goto done;
7384 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
7385 417a6e49 2020-09-26 stsp got_ref_get_symref_target(new_base_branch));
7386 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7387 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7388 417a6e49 2020-09-26 stsp new_base_branch, update_progress, &upa);
7389 417a6e49 2020-09-26 stsp if (error)
7390 417a6e49 2020-09-26 stsp goto done;
7391 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7392 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7393 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
7396 417a6e49 2020-09-26 stsp if (continue_rebase) {
7397 417a6e49 2020-09-26 stsp if (!rebase_in_progress) {
7398 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_REBASING);
7399 417a6e49 2020-09-26 stsp goto done;
7401 417a6e49 2020-09-26 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7402 417a6e49 2020-09-26 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7403 417a6e49 2020-09-26 stsp worktree, repo);
7404 417a6e49 2020-09-26 stsp if (error)
7405 417a6e49 2020-09-26 stsp goto done;
7407 417a6e49 2020-09-26 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7408 417a6e49 2020-09-26 stsp resume_commit_id, repo);
7409 417a6e49 2020-09-26 stsp if (error)
7410 417a6e49 2020-09-26 stsp goto done;
7412 417a6e49 2020-09-26 stsp yca_id = got_object_id_dup(resume_commit_id);
7413 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7414 417a6e49 2020-09-26 stsp error = got_error_from_errno("got_object_id_dup");
7415 417a6e49 2020-09-26 stsp goto done;
7418 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7419 417a6e49 2020-09-26 stsp if (error != NULL)
7420 417a6e49 2020-09-26 stsp goto done;
7423 417a6e49 2020-09-26 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7424 417a6e49 2020-09-26 stsp if (error)
7425 417a6e49 2020-09-26 stsp goto done;
7427 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7428 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id;
7430 417a6e49 2020-09-26 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7431 417a6e49 2020-09-26 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7432 417a6e49 2020-09-26 stsp base_commit_id, branch_head_commit_id, repo,
7433 417a6e49 2020-09-26 stsp check_cancelled, NULL);
7434 417a6e49 2020-09-26 stsp if (error)
7435 417a6e49 2020-09-26 stsp goto done;
7436 417a6e49 2020-09-26 stsp if (yca_id == NULL) {
7437 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7438 417a6e49 2020-09-26 stsp "specified branch shares no common ancestry "
7439 417a6e49 2020-09-26 stsp "with work tree's branch");
7440 417a6e49 2020-09-26 stsp goto done;
7443 417a6e49 2020-09-26 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7444 417a6e49 2020-09-26 stsp if (error) {
7445 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_ANCESTRY)
7446 417a6e49 2020-09-26 stsp goto done;
7447 417a6e49 2020-09-26 stsp error = NULL;
7449 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7450 417a6e49 2020-09-26 stsp "specified branch resolves to a commit which "
7451 417a6e49 2020-09-26 stsp "is already contained in work tree's branch");
7452 417a6e49 2020-09-26 stsp goto done;
7454 417a6e49 2020-09-26 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7455 417a6e49 2020-09-26 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7456 417a6e49 2020-09-26 stsp if (error)
7457 417a6e49 2020-09-26 stsp goto done;
7460 417a6e49 2020-09-26 stsp commit_id = branch_head_commit_id;
7461 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7462 417a6e49 2020-09-26 stsp if (error)
7463 417a6e49 2020-09-26 stsp goto done;
7465 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7466 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
7467 417a6e49 2020-09-26 stsp if (pid == NULL) {
7468 417a6e49 2020-09-26 stsp if (!continue_rebase) {
7469 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7470 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7471 417a6e49 2020-09-26 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7472 417a6e49 2020-09-26 stsp repo, new_base_branch, update_progress, &upa);
7473 417a6e49 2020-09-26 stsp if (error)
7474 417a6e49 2020-09-26 stsp goto done;
7475 417a6e49 2020-09-26 stsp printf("Rebase of %s aborted\n",
7476 417a6e49 2020-09-26 stsp got_ref_get_name(branch));
7477 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7480 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7481 417a6e49 2020-09-26 stsp goto done;
7483 417a6e49 2020-09-26 stsp error = collect_commits(&commits, commit_id, pid->id,
7484 417a6e49 2020-09-26 stsp yca_id, got_worktree_get_path_prefix(worktree),
7485 417a6e49 2020-09-26 stsp GOT_ERR_REBASE_PATH, repo);
7486 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7487 417a6e49 2020-09-26 stsp commit = NULL;
7488 417a6e49 2020-09-26 stsp if (error)
7489 417a6e49 2020-09-26 stsp goto done;
7491 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
7492 417a6e49 2020-09-26 stsp if (continue_rebase) {
7493 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex,
7494 417a6e49 2020-09-26 stsp branch, new_base_branch, tmp_branch, repo);
7495 417a6e49 2020-09-26 stsp goto done;
7497 417a6e49 2020-09-26 stsp /* Fast-forward the reference of the branch. */
7498 417a6e49 2020-09-26 stsp struct got_object_id *new_head_commit_id;
7499 417a6e49 2020-09-26 stsp char *id_str;
7500 417a6e49 2020-09-26 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7501 417a6e49 2020-09-26 stsp new_base_branch);
7502 417a6e49 2020-09-26 stsp if (error)
7503 417a6e49 2020-09-26 stsp goto done;
7504 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7505 417a6e49 2020-09-26 stsp printf("Forwarding %s to commit %s\n",
7506 417a6e49 2020-09-26 stsp got_ref_get_name(branch), id_str);
7507 417a6e49 2020-09-26 stsp free(id_str);
7508 417a6e49 2020-09-26 stsp error = got_ref_change_ref(branch,
7509 417a6e49 2020-09-26 stsp new_head_commit_id);
7510 417a6e49 2020-09-26 stsp if (error)
7511 417a6e49 2020-09-26 stsp goto done;
7515 417a6e49 2020-09-26 stsp pid = NULL;
7516 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7517 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
7519 417a6e49 2020-09-26 stsp commit_id = qid->id;
7520 417a6e49 2020-09-26 stsp parent_id = pid ? pid->id : yca_id;
7521 417a6e49 2020-09-26 stsp pid = qid;
7523 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
7524 417a6e49 2020-09-26 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7525 417a6e49 2020-09-26 stsp worktree, fileindex, parent_id, commit_id, repo,
7526 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
7527 417a6e49 2020-09-26 stsp if (error)
7528 417a6e49 2020-09-26 stsp goto done;
7530 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
7531 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
7532 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
7534 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7535 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(qid->id, repo);
7536 417a6e49 2020-09-26 stsp if (error)
7537 417a6e49 2020-09-26 stsp goto done;
7538 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7542 417a6e49 2020-09-26 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7543 417a6e49 2020-09-26 stsp tmp_branch, commit_id, repo);
7544 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7545 417a6e49 2020-09-26 stsp if (error)
7546 417a6e49 2020-09-26 stsp goto done;
7549 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7550 417a6e49 2020-09-26 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7551 417a6e49 2020-09-26 stsp if (error)
7552 417a6e49 2020-09-26 stsp goto done;
7553 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7554 417a6e49 2020-09-26 stsp "conflicts must be resolved before rebasing can continue");
7556 417a6e49 2020-09-26 stsp error = rebase_complete(worktree, fileindex, branch,
7557 417a6e49 2020-09-26 stsp new_base_branch, tmp_branch, repo);
7559 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
7560 417a6e49 2020-09-26 stsp free(branch_head_commit_id);
7561 417a6e49 2020-09-26 stsp free(resume_commit_id);
7562 417a6e49 2020-09-26 stsp free(yca_id);
7563 417a6e49 2020-09-26 stsp if (commit)
7564 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7565 417a6e49 2020-09-26 stsp if (branch)
7566 417a6e49 2020-09-26 stsp got_ref_close(branch);
7567 417a6e49 2020-09-26 stsp if (new_base_branch)
7568 417a6e49 2020-09-26 stsp got_ref_close(new_base_branch);
7569 417a6e49 2020-09-26 stsp if (tmp_branch)
7570 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
7571 417a6e49 2020-09-26 stsp if (worktree)
7572 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
7574 417a6e49 2020-09-26 stsp got_repo_close(repo);
7575 417a6e49 2020-09-26 stsp return error;
7578 417a6e49 2020-09-26 stsp __dead static void
7579 417a6e49 2020-09-26 stsp usage_histedit(void)
7581 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7582 417a6e49 2020-09-26 stsp getprogname());
7586 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_PICK 'p'
7587 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_EDIT 'e'
7588 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_FOLD 'f'
7589 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_DROP 'd'
7590 417a6e49 2020-09-26 stsp #define GOT_HISTEDIT_MESG 'm'
7592 417a6e49 2020-09-26 stsp static struct got_histedit_cmd {
7593 417a6e49 2020-09-26 stsp unsigned char code;
7594 417a6e49 2020-09-26 stsp const char *name;
7595 417a6e49 2020-09-26 stsp const char *desc;
7596 417a6e49 2020-09-26 stsp } got_histedit_cmds[] = {
7597 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7598 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7599 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7600 417a6e49 2020-09-26 stsp "be used" },
7601 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7602 417a6e49 2020-09-26 stsp { GOT_HISTEDIT_MESG, "mesg",
7603 417a6e49 2020-09-26 stsp "single-line log message for commit above (open editor if empty)" },
7606 417a6e49 2020-09-26 stsp struct got_histedit_list_entry {
7607 417a6e49 2020-09-26 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7608 417a6e49 2020-09-26 stsp struct got_object_id *commit_id;
7609 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7610 417a6e49 2020-09-26 stsp char *logmsg;
7612 417a6e49 2020-09-26 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7614 417a6e49 2020-09-26 stsp static const struct got_error *
7615 417a6e49 2020-09-26 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7616 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7618 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7619 417a6e49 2020-09-26 stsp char *logmsg = NULL, *id_str = NULL;
7620 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7623 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7625 417a6e49 2020-09-26 stsp goto done;
7627 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 34, commit);
7629 417a6e49 2020-09-26 stsp goto done;
7631 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
7633 417a6e49 2020-09-26 stsp goto done;
7635 417a6e49 2020-09-26 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7636 417a6e49 2020-09-26 stsp if (n < 0)
7637 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7639 417a6e49 2020-09-26 stsp if (commit)
7640 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7641 417a6e49 2020-09-26 stsp free(id_str);
7642 417a6e49 2020-09-26 stsp free(logmsg);
7643 417a6e49 2020-09-26 stsp return err;
7646 417a6e49 2020-09-26 stsp static const struct got_error *
7647 417a6e49 2020-09-26 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7648 417a6e49 2020-09-26 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7650 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7651 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7653 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7654 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7656 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7657 417a6e49 2020-09-26 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7661 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
7662 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7663 417a6e49 2020-09-26 stsp if (n < 0) {
7664 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7670 417a6e49 2020-09-26 stsp return err;
7673 417a6e49 2020-09-26 stsp static const struct got_error *
7674 417a6e49 2020-09-26 stsp write_cmd_list(FILE *f, const char *branch_name,
7675 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits)
7677 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7679 417a6e49 2020-09-26 stsp char *id_str;
7680 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7682 417a6e49 2020-09-26 stsp qid = SIMPLEQ_FIRST(commits);
7683 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
7685 417a6e49 2020-09-26 stsp return err;
7687 417a6e49 2020-09-26 stsp n = fprintf(f,
7688 417a6e49 2020-09-26 stsp "# Editing the history of branch '%s' starting at\n"
7689 417a6e49 2020-09-26 stsp "# commit %s\n"
7690 417a6e49 2020-09-26 stsp "# Commits will be processed in order from top to "
7691 417a6e49 2020-09-26 stsp "bottom of this file.\n", branch_name, id_str);
7692 417a6e49 2020-09-26 stsp if (n < 0) {
7693 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7694 417a6e49 2020-09-26 stsp goto done;
7697 417a6e49 2020-09-26 stsp n = fprintf(f, "# Available histedit commands:\n");
7698 417a6e49 2020-09-26 stsp if (n < 0) {
7699 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7700 417a6e49 2020-09-26 stsp goto done;
7703 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7704 417a6e49 2020-09-26 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7705 417a6e49 2020-09-26 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7706 417a6e49 2020-09-26 stsp cmd->desc);
7707 417a6e49 2020-09-26 stsp if (n < 0) {
7708 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
7713 417a6e49 2020-09-26 stsp free(id_str);
7714 417a6e49 2020-09-26 stsp return err;
7717 417a6e49 2020-09-26 stsp static const struct got_error *
7718 417a6e49 2020-09-26 stsp histedit_syntax_error(int lineno)
7720 417a6e49 2020-09-26 stsp static char msg[42];
7723 417a6e49 2020-09-26 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7725 417a6e49 2020-09-26 stsp if (ret == -1 || ret >= sizeof(msg))
7726 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7728 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7731 417a6e49 2020-09-26 stsp static const struct got_error *
7732 417a6e49 2020-09-26 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7733 417a6e49 2020-09-26 stsp char *logmsg, struct got_repository *repo)
7735 417a6e49 2020-09-26 stsp const struct got_error *err;
7736 417a6e49 2020-09-26 stsp struct got_commit_object *folded_commit = NULL;
7737 417a6e49 2020-09-26 stsp char *id_str, *folded_logmsg = NULL;
7739 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7741 417a6e49 2020-09-26 stsp return err;
7743 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7745 417a6e49 2020-09-26 stsp goto done;
7747 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7749 417a6e49 2020-09-26 stsp goto done;
7750 417a6e49 2020-09-26 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7751 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7752 417a6e49 2020-09-26 stsp folded_logmsg) == -1) {
7753 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7756 417a6e49 2020-09-26 stsp if (folded_commit)
7757 417a6e49 2020-09-26 stsp got_object_commit_close(folded_commit);
7758 417a6e49 2020-09-26 stsp free(id_str);
7759 417a6e49 2020-09-26 stsp free(folded_logmsg);
7760 417a6e49 2020-09-26 stsp return err;
7763 417a6e49 2020-09-26 stsp static struct got_histedit_list_entry *
7764 417a6e49 2020-09-26 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7766 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7768 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7769 417a6e49 2020-09-26 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7770 417a6e49 2020-09-26 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7771 417a6e49 2020-09-26 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7772 417a6e49 2020-09-26 stsp folded = prev;
7773 417a6e49 2020-09-26 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7776 417a6e49 2020-09-26 stsp return folded;
7779 417a6e49 2020-09-26 stsp static const struct got_error *
7780 417a6e49 2020-09-26 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7781 417a6e49 2020-09-26 stsp struct got_repository *repo)
7783 417a6e49 2020-09-26 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7784 417a6e49 2020-09-26 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7785 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7786 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
7787 417a6e49 2020-09-26 stsp int logmsg_len;
7789 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *folded = NULL;
7791 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7793 417a6e49 2020-09-26 stsp return err;
7795 417a6e49 2020-09-26 stsp folded = get_folded_commits(hle);
7796 417a6e49 2020-09-26 stsp if (folded) {
7797 417a6e49 2020-09-26 stsp while (folded != hle) {
7798 417a6e49 2020-09-26 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7799 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7802 417a6e49 2020-09-26 stsp err = append_folded_commit_msg(&new_msg, folded,
7803 417a6e49 2020-09-26 stsp logmsg, repo);
7805 417a6e49 2020-09-26 stsp goto done;
7806 417a6e49 2020-09-26 stsp free(logmsg);
7807 417a6e49 2020-09-26 stsp logmsg = new_msg;
7808 417a6e49 2020-09-26 stsp folded = TAILQ_NEXT(folded, entry);
7812 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7814 417a6e49 2020-09-26 stsp goto done;
7815 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7817 417a6e49 2020-09-26 stsp goto done;
7818 417a6e49 2020-09-26 stsp logmsg_len = asprintf(&new_msg,
7819 417a6e49 2020-09-26 stsp "%s\n# original log message of commit %s: %s",
7820 417a6e49 2020-09-26 stsp logmsg ? logmsg : "", id_str, orig_logmsg);
7821 417a6e49 2020-09-26 stsp if (logmsg_len == -1) {
7822 417a6e49 2020-09-26 stsp err = got_error_from_errno("asprintf");
7823 417a6e49 2020-09-26 stsp goto done;
7825 417a6e49 2020-09-26 stsp free(logmsg);
7826 417a6e49 2020-09-26 stsp logmsg = new_msg;
7828 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7830 417a6e49 2020-09-26 stsp goto done;
7832 417a6e49 2020-09-26 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7833 417a6e49 2020-09-26 stsp GOT_TMPDIR_STR "/got-logmsg");
7835 417a6e49 2020-09-26 stsp goto done;
7837 417a6e49 2020-09-26 stsp write(fd, logmsg, logmsg_len);
7838 417a6e49 2020-09-26 stsp close(fd);
7840 417a6e49 2020-09-26 stsp err = get_editor(&editor);
7842 417a6e49 2020-09-26 stsp goto done;
7844 417a6e49 2020-09-26 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7845 417a6e49 2020-09-26 stsp if (err) {
7846 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7847 417a6e49 2020-09-26 stsp goto done;
7848 417a6e49 2020-09-26 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7851 417a6e49 2020-09-26 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7852 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", logmsg_path);
7853 417a6e49 2020-09-26 stsp free(logmsg_path);
7854 417a6e49 2020-09-26 stsp free(logmsg);
7855 417a6e49 2020-09-26 stsp free(orig_logmsg);
7856 417a6e49 2020-09-26 stsp free(editor);
7857 417a6e49 2020-09-26 stsp if (commit)
7858 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
7859 417a6e49 2020-09-26 stsp return err;
7862 417a6e49 2020-09-26 stsp static const struct got_error *
7863 417a6e49 2020-09-26 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7864 417a6e49 2020-09-26 stsp FILE *f, struct got_repository *repo)
7866 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7867 417a6e49 2020-09-26 stsp char *line = NULL, *p, *end;
7868 417a6e49 2020-09-26 stsp size_t size;
7869 417a6e49 2020-09-26 stsp ssize_t len;
7870 417a6e49 2020-09-26 stsp int lineno = 0, i;
7871 417a6e49 2020-09-26 stsp const struct got_histedit_cmd *cmd;
7872 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL;
7873 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle = NULL;
7875 417a6e49 2020-09-26 stsp for (;;) {
7876 417a6e49 2020-09-26 stsp len = getline(&line, &size, f);
7877 417a6e49 2020-09-26 stsp if (len == -1) {
7878 417a6e49 2020-09-26 stsp const struct got_error *getline_err;
7879 417a6e49 2020-09-26 stsp if (feof(f))
7881 417a6e49 2020-09-26 stsp getline_err = got_error_from_errno("getline");
7882 417a6e49 2020-09-26 stsp err = got_ferror(f, getline_err->code);
7887 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7889 417a6e49 2020-09-26 stsp if (p[0] == '#' || p[0] == '\0') {
7890 417a6e49 2020-09-26 stsp free(line);
7891 417a6e49 2020-09-26 stsp line = NULL;
7894 417a6e49 2020-09-26 stsp cmd = NULL;
7895 417a6e49 2020-09-26 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7896 417a6e49 2020-09-26 stsp cmd = &got_histedit_cmds[i];
7897 417a6e49 2020-09-26 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7898 417a6e49 2020-09-26 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7899 417a6e49 2020-09-26 stsp p += strlen(cmd->name);
7902 417a6e49 2020-09-26 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7907 417a6e49 2020-09-26 stsp if (i == nitems(got_histedit_cmds)) {
7908 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7911 417a6e49 2020-09-26 stsp while (isspace((unsigned char)p[0]))
7913 417a6e49 2020-09-26 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7914 417a6e49 2020-09-26 stsp if (hle == NULL || hle->logmsg != NULL) {
7915 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7918 417a6e49 2020-09-26 stsp if (p[0] == '\0') {
7919 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
7923 417a6e49 2020-09-26 stsp hle->logmsg = strdup(p);
7924 417a6e49 2020-09-26 stsp if (hle->logmsg == NULL) {
7925 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
7929 417a6e49 2020-09-26 stsp free(line);
7930 417a6e49 2020-09-26 stsp line = NULL;
7934 417a6e49 2020-09-26 stsp while (end[0] && !isspace((unsigned char)end[0]))
7936 417a6e49 2020-09-26 stsp *end = '\0';
7938 417a6e49 2020-09-26 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7939 417a6e49 2020-09-26 stsp if (err) {
7940 417a6e49 2020-09-26 stsp /* override error code */
7941 417a6e49 2020-09-26 stsp err = histedit_syntax_error(lineno);
7945 417a6e49 2020-09-26 stsp hle = malloc(sizeof(*hle));
7946 417a6e49 2020-09-26 stsp if (hle == NULL) {
7947 417a6e49 2020-09-26 stsp err = got_error_from_errno("malloc");
7950 417a6e49 2020-09-26 stsp hle->cmd = cmd;
7951 417a6e49 2020-09-26 stsp hle->commit_id = commit_id;
7952 417a6e49 2020-09-26 stsp hle->logmsg = NULL;
7953 417a6e49 2020-09-26 stsp commit_id = NULL;
7954 417a6e49 2020-09-26 stsp free(line);
7955 417a6e49 2020-09-26 stsp line = NULL;
7956 417a6e49 2020-09-26 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7959 417a6e49 2020-09-26 stsp free(line);
7960 417a6e49 2020-09-26 stsp free(commit_id);
7961 417a6e49 2020-09-26 stsp return err;
7964 417a6e49 2020-09-26 stsp static const struct got_error *
7965 417a6e49 2020-09-26 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7966 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7968 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
7969 417a6e49 2020-09-26 stsp struct got_object_qid *qid;
7970 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
7971 417a6e49 2020-09-26 stsp static char msg[92];
7972 417a6e49 2020-09-26 stsp char *id_str;
7974 417a6e49 2020-09-26 stsp if (TAILQ_EMPTY(histedit_cmds))
7975 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7976 417a6e49 2020-09-26 stsp "histedit script contains no commands");
7977 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(commits))
7978 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7980 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7981 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle2;
7982 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7983 417a6e49 2020-09-26 stsp if (hle == hle2)
7985 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
7986 417a6e49 2020-09-26 stsp hle2->commit_id) != 0)
7988 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, hle->commit_id);
7990 417a6e49 2020-09-26 stsp return err;
7991 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7992 417a6e49 2020-09-26 stsp "more than once in histedit script", id_str);
7993 417a6e49 2020-09-26 stsp free(id_str);
7994 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7998 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7999 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8000 417a6e49 2020-09-26 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8003 417a6e49 2020-09-26 stsp if (hle == NULL) {
8004 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, qid->id);
8006 417a6e49 2020-09-26 stsp return err;
8007 417a6e49 2020-09-26 stsp snprintf(msg, sizeof(msg),
8008 417a6e49 2020-09-26 stsp "commit %s missing from histedit script", id_str);
8009 417a6e49 2020-09-26 stsp free(id_str);
8010 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8014 417a6e49 2020-09-26 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8015 417a6e49 2020-09-26 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8016 417a6e49 2020-09-26 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8017 417a6e49 2020-09-26 stsp "last commit in histedit script cannot be folded");
8019 417a6e49 2020-09-26 stsp return NULL;
8022 417a6e49 2020-09-26 stsp static const struct got_error *
8023 417a6e49 2020-09-26 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8024 417a6e49 2020-09-26 stsp const char *path, struct got_object_id_queue *commits,
8025 417a6e49 2020-09-26 stsp struct got_repository *repo)
8027 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8028 417a6e49 2020-09-26 stsp char *editor;
8029 417a6e49 2020-09-26 stsp FILE *f = NULL;
8031 417a6e49 2020-09-26 stsp err = get_editor(&editor);
8033 417a6e49 2020-09-26 stsp return err;
8035 417a6e49 2020-09-26 stsp if (spawn_editor(editor, path) == -1) {
8036 417a6e49 2020-09-26 stsp err = got_error_from_errno("failed spawning editor");
8037 417a6e49 2020-09-26 stsp goto done;
8040 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8041 417a6e49 2020-09-26 stsp if (f == NULL) {
8042 417a6e49 2020-09-26 stsp err = got_error_from_errno("fopen");
8043 417a6e49 2020-09-26 stsp goto done;
8045 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8047 417a6e49 2020-09-26 stsp goto done;
8049 417a6e49 2020-09-26 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8051 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8052 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8053 417a6e49 2020-09-26 stsp free(editor);
8054 417a6e49 2020-09-26 stsp return err;
8057 417a6e49 2020-09-26 stsp static const struct got_error *
8058 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8059 417a6e49 2020-09-26 stsp struct got_object_id_queue *, const char *, const char *,
8060 417a6e49 2020-09-26 stsp struct got_repository *);
8062 417a6e49 2020-09-26 stsp static const struct got_error *
8063 417a6e49 2020-09-26 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8064 417a6e49 2020-09-26 stsp struct got_object_id_queue *commits, const char *branch_name,
8065 417a6e49 2020-09-26 stsp int edit_logmsg_only, struct got_repository *repo)
8067 417a6e49 2020-09-26 stsp const struct got_error *err;
8068 417a6e49 2020-09-26 stsp FILE *f = NULL;
8069 417a6e49 2020-09-26 stsp char *path = NULL;
8071 417a6e49 2020-09-26 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8073 417a6e49 2020-09-26 stsp return err;
8075 417a6e49 2020-09-26 stsp err = write_cmd_list(f, branch_name, commits);
8077 417a6e49 2020-09-26 stsp goto done;
8079 417a6e49 2020-09-26 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8081 417a6e49 2020-09-26 stsp goto done;
8083 417a6e49 2020-09-26 stsp if (edit_logmsg_only) {
8084 417a6e49 2020-09-26 stsp rewind(f);
8085 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8087 417a6e49 2020-09-26 stsp if (fclose(f) != 0) {
8088 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8089 417a6e49 2020-09-26 stsp goto done;
8092 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8093 417a6e49 2020-09-26 stsp if (err) {
8094 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8095 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8096 417a6e49 2020-09-26 stsp goto done;
8097 417a6e49 2020-09-26 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8098 417a6e49 2020-09-26 stsp commits, path, branch_name, repo);
8102 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8103 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8104 417a6e49 2020-09-26 stsp if (path && unlink(path) != 0 && err == NULL)
8105 417a6e49 2020-09-26 stsp err = got_error_from_errno2("unlink", path);
8106 417a6e49 2020-09-26 stsp free(path);
8107 417a6e49 2020-09-26 stsp return err;
8110 417a6e49 2020-09-26 stsp static const struct got_error *
8111 417a6e49 2020-09-26 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8112 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8114 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8115 417a6e49 2020-09-26 stsp char *path = NULL;
8116 417a6e49 2020-09-26 stsp FILE *f = NULL;
8117 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8118 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8120 417a6e49 2020-09-26 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8122 417a6e49 2020-09-26 stsp return err;
8124 417a6e49 2020-09-26 stsp f = fopen(path, "w");
8125 417a6e49 2020-09-26 stsp if (f == NULL) {
8126 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8127 417a6e49 2020-09-26 stsp goto done;
8129 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8130 417a6e49 2020-09-26 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8135 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8136 417a6e49 2020-09-26 stsp int n = fprintf(f, "%c %s\n",
8137 417a6e49 2020-09-26 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8138 417a6e49 2020-09-26 stsp if (n < 0) {
8139 417a6e49 2020-09-26 stsp err = got_ferror(f, GOT_ERR_IO);
8145 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8146 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8147 417a6e49 2020-09-26 stsp free(path);
8148 417a6e49 2020-09-26 stsp if (commit)
8149 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8150 417a6e49 2020-09-26 stsp return err;
8154 417a6e49 2020-09-26 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8156 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8158 417a6e49 2020-09-26 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8159 417a6e49 2020-09-26 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8160 417a6e49 2020-09-26 stsp free(hle);
8164 417a6e49 2020-09-26 stsp static const struct got_error *
8165 417a6e49 2020-09-26 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8166 417a6e49 2020-09-26 stsp const char *path, struct got_repository *repo)
8168 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8169 417a6e49 2020-09-26 stsp FILE *f = NULL;
8171 417a6e49 2020-09-26 stsp f = fopen(path, "r");
8172 417a6e49 2020-09-26 stsp if (f == NULL) {
8173 417a6e49 2020-09-26 stsp err = got_error_from_errno2("fopen", path);
8174 417a6e49 2020-09-26 stsp goto done;
8177 417a6e49 2020-09-26 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8179 417a6e49 2020-09-26 stsp if (f && fclose(f) != 0 && err == NULL)
8180 417a6e49 2020-09-26 stsp err = got_error_from_errno("fclose");
8181 417a6e49 2020-09-26 stsp return err;
8184 417a6e49 2020-09-26 stsp static const struct got_error *
8185 417a6e49 2020-09-26 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8186 417a6e49 2020-09-26 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8187 417a6e49 2020-09-26 stsp const char *path, const char *branch_name, struct got_repository *repo)
8189 417a6e49 2020-09-26 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8190 417a6e49 2020-09-26 stsp int resp = ' ';
8192 417a6e49 2020-09-26 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8193 417a6e49 2020-09-26 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8194 417a6e49 2020-09-26 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8195 417a6e49 2020-09-26 stsp resp = getchar();
8196 417a6e49 2020-09-26 stsp if (resp == '\n')
8197 417a6e49 2020-09-26 stsp resp = getchar();
8198 417a6e49 2020-09-26 stsp if (resp == 'c') {
8199 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8200 417a6e49 2020-09-26 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8202 417a6e49 2020-09-26 stsp if (err) {
8203 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8204 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8206 417a6e49 2020-09-26 stsp prev_err = err;
8207 417a6e49 2020-09-26 stsp resp = ' ';
8211 417a6e49 2020-09-26 stsp } else if (resp == 'r') {
8212 417a6e49 2020-09-26 stsp histedit_free_list(histedit_cmds);
8213 417a6e49 2020-09-26 stsp err = histedit_edit_script(histedit_cmds,
8214 417a6e49 2020-09-26 stsp commits, branch_name, 0, repo);
8215 417a6e49 2020-09-26 stsp if (err) {
8216 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8217 417a6e49 2020-09-26 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8219 417a6e49 2020-09-26 stsp prev_err = err;
8220 417a6e49 2020-09-26 stsp resp = ' ';
8224 417a6e49 2020-09-26 stsp } else if (resp == 'a') {
8225 417a6e49 2020-09-26 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8228 417a6e49 2020-09-26 stsp printf("invalid response '%c'\n", resp);
8231 417a6e49 2020-09-26 stsp return err;
8234 417a6e49 2020-09-26 stsp static const struct got_error *
8235 417a6e49 2020-09-26 stsp histedit_complete(struct got_worktree *worktree,
8236 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8237 417a6e49 2020-09-26 stsp struct got_reference *branch, struct got_repository *repo)
8239 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8240 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8241 417a6e49 2020-09-26 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8242 417a6e49 2020-09-26 stsp branch, repo);
8245 417a6e49 2020-09-26 stsp static const struct got_error *
8246 417a6e49 2020-09-26 stsp show_histedit_progress(struct got_commit_object *commit,
8247 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8249 417a6e49 2020-09-26 stsp const struct got_error *err;
8250 417a6e49 2020-09-26 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8252 417a6e49 2020-09-26 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8254 417a6e49 2020-09-26 stsp goto done;
8256 417a6e49 2020-09-26 stsp if (new_id) {
8257 417a6e49 2020-09-26 stsp err = got_object_id_str(&new_id_str, new_id);
8259 417a6e49 2020-09-26 stsp goto done;
8262 417a6e49 2020-09-26 stsp old_id_str[12] = '\0';
8263 417a6e49 2020-09-26 stsp if (new_id_str)
8264 417a6e49 2020-09-26 stsp new_id_str[12] = '\0';
8266 417a6e49 2020-09-26 stsp if (hle->logmsg) {
8267 417a6e49 2020-09-26 stsp logmsg = strdup(hle->logmsg);
8268 417a6e49 2020-09-26 stsp if (logmsg == NULL) {
8269 417a6e49 2020-09-26 stsp err = got_error_from_errno("strdup");
8270 417a6e49 2020-09-26 stsp goto done;
8272 417a6e49 2020-09-26 stsp trim_logmsg(logmsg, 42);
8274 417a6e49 2020-09-26 stsp err = get_short_logmsg(&logmsg, 42, commit);
8276 417a6e49 2020-09-26 stsp goto done;
8279 417a6e49 2020-09-26 stsp switch (hle->cmd->code) {
8280 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_PICK:
8281 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_EDIT:
8282 417a6e49 2020-09-26 stsp printf("%s -> %s: %s\n", old_id_str,
8283 417a6e49 2020-09-26 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8285 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_DROP:
8286 417a6e49 2020-09-26 stsp case GOT_HISTEDIT_FOLD:
8287 417a6e49 2020-09-26 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8294 417a6e49 2020-09-26 stsp free(old_id_str);
8295 417a6e49 2020-09-26 stsp free(new_id_str);
8296 417a6e49 2020-09-26 stsp return err;
8299 417a6e49 2020-09-26 stsp static const struct got_error *
8300 417a6e49 2020-09-26 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8301 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8302 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8303 417a6e49 2020-09-26 stsp struct got_repository *repo)
8305 417a6e49 2020-09-26 stsp const struct got_error *err;
8306 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8307 417a6e49 2020-09-26 stsp struct got_object_id *new_commit_id;
8309 417a6e49 2020-09-26 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8310 417a6e49 2020-09-26 stsp && hle->logmsg == NULL) {
8311 417a6e49 2020-09-26 stsp err = histedit_edit_logmsg(hle, repo);
8313 417a6e49 2020-09-26 stsp return err;
8316 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8318 417a6e49 2020-09-26 stsp return err;
8320 417a6e49 2020-09-26 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8321 417a6e49 2020-09-26 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8322 417a6e49 2020-09-26 stsp hle->logmsg, repo);
8323 417a6e49 2020-09-26 stsp if (err) {
8324 417a6e49 2020-09-26 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8325 417a6e49 2020-09-26 stsp goto done;
8326 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, NULL);
8328 417a6e49 2020-09-26 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8329 417a6e49 2020-09-26 stsp free(new_commit_id);
8332 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8333 417a6e49 2020-09-26 stsp return err;
8336 417a6e49 2020-09-26 stsp static const struct got_error *
8337 417a6e49 2020-09-26 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8338 417a6e49 2020-09-26 stsp struct got_worktree *worktree, struct got_repository *repo)
8340 417a6e49 2020-09-26 stsp const struct got_error *error;
8341 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
8343 417a6e49 2020-09-26 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8345 417a6e49 2020-09-26 stsp if (error)
8346 417a6e49 2020-09-26 stsp return error;
8348 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8349 417a6e49 2020-09-26 stsp if (error)
8350 417a6e49 2020-09-26 stsp return error;
8352 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit, hle, NULL);
8353 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8354 417a6e49 2020-09-26 stsp return error;
8357 417a6e49 2020-09-26 stsp static const struct got_error *
8358 417a6e49 2020-09-26 stsp check_local_changes(void *arg, unsigned char status,
8359 417a6e49 2020-09-26 stsp unsigned char staged_status, const char *path,
8360 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8361 417a6e49 2020-09-26 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8363 417a6e49 2020-09-26 stsp int *have_local_changes = arg;
8365 417a6e49 2020-09-26 stsp switch (status) {
8366 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8367 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8368 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8369 417a6e49 2020-09-26 stsp case GOT_STATUS_CONFLICT:
8370 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8371 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8376 417a6e49 2020-09-26 stsp switch (staged_status) {
8377 417a6e49 2020-09-26 stsp case GOT_STATUS_ADD:
8378 417a6e49 2020-09-26 stsp case GOT_STATUS_DELETE:
8379 417a6e49 2020-09-26 stsp case GOT_STATUS_MODIFY:
8380 417a6e49 2020-09-26 stsp *have_local_changes = 1;
8381 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_CANCELLED);
8386 417a6e49 2020-09-26 stsp return NULL;
8389 417a6e49 2020-09-26 stsp static const struct got_error *
8390 417a6e49 2020-09-26 stsp cmd_histedit(int argc, char *argv[])
8392 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8393 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8394 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8395 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8396 417a6e49 2020-09-26 stsp char *cwd = NULL;
8397 417a6e49 2020-09-26 stsp struct got_reference *branch = NULL;
8398 417a6e49 2020-09-26 stsp struct got_reference *tmp_branch = NULL;
8399 417a6e49 2020-09-26 stsp struct got_object_id *resume_commit_id = NULL;
8400 417a6e49 2020-09-26 stsp struct got_object_id *base_commit_id = NULL;
8401 417a6e49 2020-09-26 stsp struct got_object_id *head_commit_id = NULL;
8402 417a6e49 2020-09-26 stsp struct got_commit_object *commit = NULL;
8403 417a6e49 2020-09-26 stsp int ch, rebase_in_progress = 0;
8404 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8405 417a6e49 2020-09-26 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8406 417a6e49 2020-09-26 stsp int edit_logmsg_only = 0;
8407 417a6e49 2020-09-26 stsp const char *edit_script_path = NULL;
8408 417a6e49 2020-09-26 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8409 417a6e49 2020-09-26 stsp struct got_object_id_queue commits;
8410 417a6e49 2020-09-26 stsp struct got_pathlist_head merged_paths;
8411 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
8412 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
8413 417a6e49 2020-09-26 stsp struct got_histedit_list histedit_cmds;
8414 417a6e49 2020-09-26 stsp struct got_histedit_list_entry *hle;
8416 417a6e49 2020-09-26 stsp SIMPLEQ_INIT(&commits);
8417 417a6e49 2020-09-26 stsp TAILQ_INIT(&histedit_cmds);
8418 417a6e49 2020-09-26 stsp TAILQ_INIT(&merged_paths);
8419 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8421 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8422 417a6e49 2020-09-26 stsp switch (ch) {
8424 417a6e49 2020-09-26 stsp abort_edit = 1;
8427 417a6e49 2020-09-26 stsp continue_edit = 1;
8430 417a6e49 2020-09-26 stsp edit_script_path = optarg;
8433 417a6e49 2020-09-26 stsp edit_logmsg_only = 1;
8436 417a6e49 2020-09-26 stsp usage_histedit();
8437 417a6e49 2020-09-26 stsp /* NOTREACHED */
8441 417a6e49 2020-09-26 stsp argc -= optind;
8442 417a6e49 2020-09-26 stsp argv += optind;
8444 417a6e49 2020-09-26 stsp #ifndef PROFILE
8445 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8446 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8447 417a6e49 2020-09-26 stsp err(1, "pledge");
8449 417a6e49 2020-09-26 stsp if (abort_edit && continue_edit)
8450 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8451 417a6e49 2020-09-26 stsp if (edit_script_path && edit_logmsg_only)
8452 417a6e49 2020-09-26 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8453 417a6e49 2020-09-26 stsp if (abort_edit && edit_logmsg_only)
8454 417a6e49 2020-09-26 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8455 417a6e49 2020-09-26 stsp if (continue_edit && edit_logmsg_only)
8456 417a6e49 2020-09-26 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8457 417a6e49 2020-09-26 stsp if (argc != 0)
8458 417a6e49 2020-09-26 stsp usage_histedit();
8461 417a6e49 2020-09-26 stsp * This command cannot apply unveil(2) in all cases because the
8462 417a6e49 2020-09-26 stsp * user may choose to run an editor to edit the histedit script
8463 417a6e49 2020-09-26 stsp * and to edit individual commit log messages.
8464 417a6e49 2020-09-26 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8465 417a6e49 2020-09-26 stsp * apply unveil after edit script and log messages have been written.
8466 417a6e49 2020-09-26 stsp * XXX TODO: Make use of unveil(2) where possible.
8469 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8470 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8471 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8472 417a6e49 2020-09-26 stsp goto done;
8474 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8475 417a6e49 2020-09-26 stsp if (error) {
8476 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8477 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8478 417a6e49 2020-09-26 stsp goto done;
8481 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8483 417a6e49 2020-09-26 stsp if (error != NULL)
8484 417a6e49 2020-09-26 stsp goto done;
8486 417a6e49 2020-09-26 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8487 417a6e49 2020-09-26 stsp if (error)
8488 417a6e49 2020-09-26 stsp goto done;
8489 417a6e49 2020-09-26 stsp if (rebase_in_progress) {
8490 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASING);
8491 417a6e49 2020-09-26 stsp goto done;
8494 417a6e49 2020-09-26 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8495 417a6e49 2020-09-26 stsp if (error)
8496 417a6e49 2020-09-26 stsp goto done;
8498 417a6e49 2020-09-26 stsp if (edit_in_progress && edit_logmsg_only) {
8499 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8500 417a6e49 2020-09-26 stsp "histedit operation is in progress in this "
8501 417a6e49 2020-09-26 stsp "work tree and must be continued or aborted "
8502 417a6e49 2020-09-26 stsp "before the -m option can be used");
8503 417a6e49 2020-09-26 stsp goto done;
8506 417a6e49 2020-09-26 stsp if (edit_in_progress && abort_edit) {
8507 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8508 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8509 417a6e49 2020-09-26 stsp worktree, repo);
8510 417a6e49 2020-09-26 stsp if (error)
8511 417a6e49 2020-09-26 stsp goto done;
8512 417a6e49 2020-09-26 stsp printf("Switching work tree to %s\n",
8513 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8514 417a6e49 2020-09-26 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8515 417a6e49 2020-09-26 stsp branch, base_commit_id, update_progress, &upa);
8516 417a6e49 2020-09-26 stsp if (error)
8517 417a6e49 2020-09-26 stsp goto done;
8518 417a6e49 2020-09-26 stsp printf("Histedit of %s aborted\n",
8519 417a6e49 2020-09-26 stsp got_ref_get_symref_target(branch));
8520 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8521 417a6e49 2020-09-26 stsp goto done; /* nothing else to do */
8522 417a6e49 2020-09-26 stsp } else if (abort_edit) {
8523 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8524 417a6e49 2020-09-26 stsp goto done;
8527 417a6e49 2020-09-26 stsp if (continue_edit) {
8528 417a6e49 2020-09-26 stsp char *path;
8530 417a6e49 2020-09-26 stsp if (!edit_in_progress) {
8531 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8532 417a6e49 2020-09-26 stsp goto done;
8535 417a6e49 2020-09-26 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8536 417a6e49 2020-09-26 stsp if (error)
8537 417a6e49 2020-09-26 stsp goto done;
8539 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8540 417a6e49 2020-09-26 stsp free(path);
8541 417a6e49 2020-09-26 stsp if (error)
8542 417a6e49 2020-09-26 stsp goto done;
8544 417a6e49 2020-09-26 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8545 417a6e49 2020-09-26 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8546 417a6e49 2020-09-26 stsp worktree, repo);
8547 417a6e49 2020-09-26 stsp if (error)
8548 417a6e49 2020-09-26 stsp goto done;
8550 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8551 417a6e49 2020-09-26 stsp if (error)
8552 417a6e49 2020-09-26 stsp goto done;
8554 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8555 417a6e49 2020-09-26 stsp head_commit_id);
8556 417a6e49 2020-09-26 stsp if (error)
8557 417a6e49 2020-09-26 stsp goto done;
8558 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8559 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8560 417a6e49 2020-09-26 stsp if (pid == NULL) {
8561 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8562 417a6e49 2020-09-26 stsp goto done;
8564 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8565 417a6e49 2020-09-26 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8566 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8567 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8568 417a6e49 2020-09-26 stsp commit = NULL;
8569 417a6e49 2020-09-26 stsp if (error)
8570 417a6e49 2020-09-26 stsp goto done;
8572 417a6e49 2020-09-26 stsp if (edit_in_progress) {
8573 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8574 417a6e49 2020-09-26 stsp goto done;
8577 417a6e49 2020-09-26 stsp error = got_ref_open(&branch, repo,
8578 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree), 0);
8579 417a6e49 2020-09-26 stsp if (error != NULL)
8580 417a6e49 2020-09-26 stsp goto done;
8582 417a6e49 2020-09-26 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8583 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8584 417a6e49 2020-09-26 stsp "will not edit commit history of a branch outside "
8585 417a6e49 2020-09-26 stsp "the \"refs/heads/\" reference namespace");
8586 417a6e49 2020-09-26 stsp goto done;
8589 417a6e49 2020-09-26 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8590 417a6e49 2020-09-26 stsp got_ref_close(branch);
8591 417a6e49 2020-09-26 stsp branch = NULL;
8592 417a6e49 2020-09-26 stsp if (error)
8593 417a6e49 2020-09-26 stsp goto done;
8595 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8596 417a6e49 2020-09-26 stsp head_commit_id);
8597 417a6e49 2020-09-26 stsp if (error)
8598 417a6e49 2020-09-26 stsp goto done;
8599 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8600 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8601 417a6e49 2020-09-26 stsp if (pid == NULL) {
8602 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8603 417a6e49 2020-09-26 stsp goto done;
8605 417a6e49 2020-09-26 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8606 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree),
8607 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree),
8608 417a6e49 2020-09-26 stsp GOT_ERR_HISTEDIT_PATH, repo);
8609 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8610 417a6e49 2020-09-26 stsp commit = NULL;
8611 417a6e49 2020-09-26 stsp if (error)
8612 417a6e49 2020-09-26 stsp goto done;
8614 417a6e49 2020-09-26 stsp if (SIMPLEQ_EMPTY(&commits)) {
8615 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8616 417a6e49 2020-09-26 stsp goto done;
8619 417a6e49 2020-09-26 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8620 417a6e49 2020-09-26 stsp &base_commit_id, &fileindex, worktree, repo);
8621 417a6e49 2020-09-26 stsp if (error)
8622 417a6e49 2020-09-26 stsp goto done;
8624 417a6e49 2020-09-26 stsp if (edit_script_path) {
8625 417a6e49 2020-09-26 stsp error = histedit_load_list(&histedit_cmds,
8626 417a6e49 2020-09-26 stsp edit_script_path, repo);
8627 417a6e49 2020-09-26 stsp if (error) {
8628 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8629 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8630 417a6e49 2020-09-26 stsp update_progress, &upa);
8631 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8632 417a6e49 2020-09-26 stsp goto done;
8635 417a6e49 2020-09-26 stsp const char *branch_name;
8636 417a6e49 2020-09-26 stsp branch_name = got_ref_get_symref_target(branch);
8637 417a6e49 2020-09-26 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8638 417a6e49 2020-09-26 stsp branch_name += 11;
8639 417a6e49 2020-09-26 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8640 417a6e49 2020-09-26 stsp branch_name, edit_logmsg_only, repo);
8641 417a6e49 2020-09-26 stsp if (error) {
8642 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8643 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8644 417a6e49 2020-09-26 stsp update_progress, &upa);
8645 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8646 417a6e49 2020-09-26 stsp goto done;
8651 417a6e49 2020-09-26 stsp error = histedit_save_list(&histedit_cmds, worktree,
8653 417a6e49 2020-09-26 stsp if (error) {
8654 417a6e49 2020-09-26 stsp got_worktree_histedit_abort(worktree, fileindex,
8655 417a6e49 2020-09-26 stsp repo, branch, base_commit_id,
8656 417a6e49 2020-09-26 stsp update_progress, &upa);
8657 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8658 417a6e49 2020-09-26 stsp goto done;
8663 417a6e49 2020-09-26 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
8664 417a6e49 2020-09-26 stsp if (error)
8665 417a6e49 2020-09-26 stsp goto done;
8667 417a6e49 2020-09-26 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8668 417a6e49 2020-09-26 stsp if (resume_commit_id) {
8669 417a6e49 2020-09-26 stsp if (got_object_id_cmp(hle->commit_id,
8670 417a6e49 2020-09-26 stsp resume_commit_id) != 0)
8673 417a6e49 2020-09-26 stsp resume_commit_id = NULL;
8674 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8675 417a6e49 2020-09-26 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8676 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree,
8678 417a6e49 2020-09-26 stsp if (error)
8679 417a6e49 2020-09-26 stsp goto done;
8681 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8682 417a6e49 2020-09-26 stsp int have_changes = 0;
8684 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
8685 417a6e49 2020-09-26 stsp error = got_pathlist_append(&paths, "", NULL);
8686 417a6e49 2020-09-26 stsp if (error)
8687 417a6e49 2020-09-26 stsp goto done;
8688 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths,
8689 417a6e49 2020-09-26 stsp repo, check_local_changes, &have_changes,
8690 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8691 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
8692 417a6e49 2020-09-26 stsp if (error) {
8693 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_CANCELLED)
8694 417a6e49 2020-09-26 stsp goto done;
8695 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
8696 417a6e49 2020-09-26 stsp goto done;
8698 417a6e49 2020-09-26 stsp if (have_changes) {
8699 417a6e49 2020-09-26 stsp error = histedit_commit(NULL, worktree,
8700 417a6e49 2020-09-26 stsp fileindex, tmp_branch, hle, repo);
8701 417a6e49 2020-09-26 stsp if (error)
8702 417a6e49 2020-09-26 stsp goto done;
8704 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(
8705 417a6e49 2020-09-26 stsp &commit, repo, hle->commit_id);
8706 417a6e49 2020-09-26 stsp if (error)
8707 417a6e49 2020-09-26 stsp goto done;
8708 417a6e49 2020-09-26 stsp error = show_histedit_progress(commit,
8709 417a6e49 2020-09-26 stsp hle, NULL);
8710 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8711 417a6e49 2020-09-26 stsp commit = NULL;
8712 417a6e49 2020-09-26 stsp if (error)
8713 417a6e49 2020-09-26 stsp goto done;
8719 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8720 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8721 417a6e49 2020-09-26 stsp if (error)
8722 417a6e49 2020-09-26 stsp goto done;
8726 417a6e49 2020-09-26 stsp error = got_object_open_as_commit(&commit, repo,
8727 417a6e49 2020-09-26 stsp hle->commit_id);
8728 417a6e49 2020-09-26 stsp if (error)
8729 417a6e49 2020-09-26 stsp goto done;
8730 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8731 417a6e49 2020-09-26 stsp pid = SIMPLEQ_FIRST(parent_ids);
8733 417a6e49 2020-09-26 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8734 417a6e49 2020-09-26 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8735 417a6e49 2020-09-26 stsp update_progress, &upa, check_cancelled, NULL);
8736 417a6e49 2020-09-26 stsp if (error)
8737 417a6e49 2020-09-26 stsp goto done;
8738 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8739 417a6e49 2020-09-26 stsp commit = NULL;
8741 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8742 417a6e49 2020-09-26 stsp if (upa.conflicts > 0)
8743 417a6e49 2020-09-26 stsp rebase_status = GOT_STATUS_CONFLICT;
8745 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8746 417a6e49 2020-09-26 stsp error = show_rebase_merge_conflict(hle->commit_id,
8748 417a6e49 2020-09-26 stsp if (error)
8749 417a6e49 2020-09-26 stsp goto done;
8750 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8754 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8755 417a6e49 2020-09-26 stsp char *id_str;
8756 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str, hle->commit_id);
8757 417a6e49 2020-09-26 stsp if (error)
8758 417a6e49 2020-09-26 stsp goto done;
8759 417a6e49 2020-09-26 stsp printf("Stopping histedit for amending commit %s\n",
8761 417a6e49 2020-09-26 stsp free(id_str);
8762 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8763 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree,
8764 417a6e49 2020-09-26 stsp fileindex);
8765 417a6e49 2020-09-26 stsp goto done;
8768 417a6e49 2020-09-26 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8769 417a6e49 2020-09-26 stsp error = histedit_skip_commit(hle, worktree, repo);
8770 417a6e49 2020-09-26 stsp if (error)
8771 417a6e49 2020-09-26 stsp goto done;
8775 417a6e49 2020-09-26 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8776 417a6e49 2020-09-26 stsp tmp_branch, hle, repo);
8777 417a6e49 2020-09-26 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8778 417a6e49 2020-09-26 stsp if (error)
8779 417a6e49 2020-09-26 stsp goto done;
8782 417a6e49 2020-09-26 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8783 417a6e49 2020-09-26 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8784 417a6e49 2020-09-26 stsp if (error)
8785 417a6e49 2020-09-26 stsp goto done;
8786 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8787 417a6e49 2020-09-26 stsp "conflicts must be resolved before histedit can continue");
8789 417a6e49 2020-09-26 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8790 417a6e49 2020-09-26 stsp branch, repo);
8792 417a6e49 2020-09-26 stsp got_object_id_queue_free(&commits);
8793 417a6e49 2020-09-26 stsp histedit_free_list(&histedit_cmds);
8794 417a6e49 2020-09-26 stsp free(head_commit_id);
8795 417a6e49 2020-09-26 stsp free(base_commit_id);
8796 417a6e49 2020-09-26 stsp free(resume_commit_id);
8797 417a6e49 2020-09-26 stsp if (commit)
8798 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
8799 417a6e49 2020-09-26 stsp if (branch)
8800 417a6e49 2020-09-26 stsp got_ref_close(branch);
8801 417a6e49 2020-09-26 stsp if (tmp_branch)
8802 417a6e49 2020-09-26 stsp got_ref_close(tmp_branch);
8803 417a6e49 2020-09-26 stsp if (worktree)
8804 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8806 417a6e49 2020-09-26 stsp got_repo_close(repo);
8807 417a6e49 2020-09-26 stsp return error;
8810 417a6e49 2020-09-26 stsp __dead static void
8811 417a6e49 2020-09-26 stsp usage_integrate(void)
8813 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8817 417a6e49 2020-09-26 stsp static const struct got_error *
8818 417a6e49 2020-09-26 stsp cmd_integrate(int argc, char *argv[])
8820 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8821 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8822 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8823 417a6e49 2020-09-26 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8824 417a6e49 2020-09-26 stsp const char *branch_arg = NULL;
8825 417a6e49 2020-09-26 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8826 417a6e49 2020-09-26 stsp struct got_fileindex *fileindex = NULL;
8827 417a6e49 2020-09-26 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8829 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
8831 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8832 417a6e49 2020-09-26 stsp switch (ch) {
8834 417a6e49 2020-09-26 stsp usage_integrate();
8835 417a6e49 2020-09-26 stsp /* NOTREACHED */
8839 417a6e49 2020-09-26 stsp argc -= optind;
8840 417a6e49 2020-09-26 stsp argv += optind;
8842 417a6e49 2020-09-26 stsp if (argc != 1)
8843 417a6e49 2020-09-26 stsp usage_integrate();
8844 417a6e49 2020-09-26 stsp branch_arg = argv[0];
8846 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8847 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
8848 417a6e49 2020-09-26 stsp err(1, "pledge");
8850 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
8851 417a6e49 2020-09-26 stsp if (cwd == NULL) {
8852 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
8853 417a6e49 2020-09-26 stsp goto done;
8856 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
8857 417a6e49 2020-09-26 stsp if (error) {
8858 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8859 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "integrate",
8861 417a6e49 2020-09-26 stsp goto done;
8864 417a6e49 2020-09-26 stsp error = check_rebase_or_histedit_in_progress(worktree);
8865 417a6e49 2020-09-26 stsp if (error)
8866 417a6e49 2020-09-26 stsp goto done;
8868 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8870 417a6e49 2020-09-26 stsp if (error != NULL)
8871 417a6e49 2020-09-26 stsp goto done;
8873 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8874 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
8875 417a6e49 2020-09-26 stsp if (error)
8876 417a6e49 2020-09-26 stsp goto done;
8878 417a6e49 2020-09-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8879 417a6e49 2020-09-26 stsp error = got_error_from_errno("asprintf");
8880 417a6e49 2020-09-26 stsp goto done;
8883 417a6e49 2020-09-26 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8884 417a6e49 2020-09-26 stsp &base_branch_ref, worktree, refname, repo);
8885 417a6e49 2020-09-26 stsp if (error)
8886 417a6e49 2020-09-26 stsp goto done;
8888 417a6e49 2020-09-26 stsp refname = strdup(got_ref_get_name(branch_ref));
8889 417a6e49 2020-09-26 stsp if (refname == NULL) {
8890 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8891 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8892 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8893 417a6e49 2020-09-26 stsp goto done;
8895 417a6e49 2020-09-26 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8896 417a6e49 2020-09-26 stsp if (base_refname == NULL) {
8897 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
8898 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8899 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8900 417a6e49 2020-09-26 stsp goto done;
8903 417a6e49 2020-09-26 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8904 417a6e49 2020-09-26 stsp if (error)
8905 417a6e49 2020-09-26 stsp goto done;
8907 417a6e49 2020-09-26 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8908 417a6e49 2020-09-26 stsp if (error)
8909 417a6e49 2020-09-26 stsp goto done;
8911 417a6e49 2020-09-26 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8912 417a6e49 2020-09-26 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8913 417a6e49 2020-09-26 stsp "specified branch has already been integrated");
8914 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8915 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8916 417a6e49 2020-09-26 stsp goto done;
8919 417a6e49 2020-09-26 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8920 417a6e49 2020-09-26 stsp if (error) {
8921 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_ANCESTRY)
8922 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8923 417a6e49 2020-09-26 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8924 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref);
8925 417a6e49 2020-09-26 stsp goto done;
8928 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
8929 417a6e49 2020-09-26 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8930 417a6e49 2020-09-26 stsp branch_ref, base_branch_ref, update_progress, &upa,
8931 417a6e49 2020-09-26 stsp check_cancelled, NULL);
8932 417a6e49 2020-09-26 stsp if (error)
8933 417a6e49 2020-09-26 stsp goto done;
8935 417a6e49 2020-09-26 stsp printf("Integrated %s into %s\n", refname, base_refname);
8936 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
8939 417a6e49 2020-09-26 stsp got_repo_close(repo);
8940 417a6e49 2020-09-26 stsp if (worktree)
8941 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
8942 417a6e49 2020-09-26 stsp free(cwd);
8943 417a6e49 2020-09-26 stsp free(base_commit_id);
8944 417a6e49 2020-09-26 stsp free(commit_id);
8945 417a6e49 2020-09-26 stsp free(refname);
8946 417a6e49 2020-09-26 stsp free(base_refname);
8947 417a6e49 2020-09-26 stsp return error;
8950 417a6e49 2020-09-26 stsp __dead static void
8951 417a6e49 2020-09-26 stsp usage_stage(void)
8953 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8954 417a6e49 2020-09-26 stsp "[-S] [file-path ...]\n",
8955 417a6e49 2020-09-26 stsp getprogname());
8959 417a6e49 2020-09-26 stsp static const struct got_error *
8960 417a6e49 2020-09-26 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8961 417a6e49 2020-09-26 stsp const char *path, struct got_object_id *blob_id,
8962 417a6e49 2020-09-26 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8963 417a6e49 2020-09-26 stsp int dirfd, const char *de_name)
8965 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
8966 417a6e49 2020-09-26 stsp char *id_str = NULL;
8968 417a6e49 2020-09-26 stsp if (staged_status != GOT_STATUS_ADD &&
8969 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_MODIFY &&
8970 417a6e49 2020-09-26 stsp staged_status != GOT_STATUS_DELETE)
8971 417a6e49 2020-09-26 stsp return NULL;
8973 417a6e49 2020-09-26 stsp if (staged_status == GOT_STATUS_ADD ||
8974 417a6e49 2020-09-26 stsp staged_status == GOT_STATUS_MODIFY)
8975 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
8977 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
8979 417a6e49 2020-09-26 stsp return err;
8981 417a6e49 2020-09-26 stsp printf("%s %c %s\n", id_str, staged_status, path);
8982 417a6e49 2020-09-26 stsp free(id_str);
8983 417a6e49 2020-09-26 stsp return NULL;
8986 417a6e49 2020-09-26 stsp static const struct got_error *
8987 417a6e49 2020-09-26 stsp cmd_stage(int argc, char *argv[])
8989 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
8990 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
8991 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
8992 417a6e49 2020-09-26 stsp char *cwd = NULL;
8993 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
8994 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
8995 417a6e49 2020-09-26 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8996 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
8997 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
8998 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9000 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9002 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9003 417a6e49 2020-09-26 stsp switch (ch) {
9005 417a6e49 2020-09-26 stsp list_stage = 1;
9008 417a6e49 2020-09-26 stsp pflag = 1;
9011 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9014 417a6e49 2020-09-26 stsp allow_bad_symlinks = 1;
9017 417a6e49 2020-09-26 stsp usage_stage();
9018 417a6e49 2020-09-26 stsp /* NOTREACHED */
9022 417a6e49 2020-09-26 stsp argc -= optind;
9023 417a6e49 2020-09-26 stsp argv += optind;
9025 417a6e49 2020-09-26 stsp #ifndef PROFILE
9026 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9027 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9028 417a6e49 2020-09-26 stsp err(1, "pledge");
9030 417a6e49 2020-09-26 stsp if (list_stage && (pflag || patch_script_path))
9031 417a6e49 2020-09-26 stsp errx(1, "-l option cannot be used with other options");
9032 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9033 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9035 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9036 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9037 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9038 417a6e49 2020-09-26 stsp goto done;
9041 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9042 417a6e49 2020-09-26 stsp if (error) {
9043 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9044 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9045 417a6e49 2020-09-26 stsp goto done;
9048 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9050 417a6e49 2020-09-26 stsp if (error != NULL)
9051 417a6e49 2020-09-26 stsp goto done;
9053 417a6e49 2020-09-26 stsp if (patch_script_path) {
9054 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9055 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9056 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9057 417a6e49 2020-09-26 stsp patch_script_path);
9058 417a6e49 2020-09-26 stsp goto done;
9061 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9062 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9063 417a6e49 2020-09-26 stsp if (error)
9064 417a6e49 2020-09-26 stsp goto done;
9066 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9067 417a6e49 2020-09-26 stsp if (error)
9068 417a6e49 2020-09-26 stsp goto done;
9070 417a6e49 2020-09-26 stsp if (list_stage)
9071 417a6e49 2020-09-26 stsp error = got_worktree_status(worktree, &paths, repo,
9072 417a6e49 2020-09-26 stsp print_stage, NULL, check_cancelled, NULL);
9074 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9075 417a6e49 2020-09-26 stsp cpa.action = "stage";
9076 417a6e49 2020-09-26 stsp error = got_worktree_stage(worktree, &paths,
9077 417a6e49 2020-09-26 stsp pflag ? NULL : print_status, NULL,
9078 417a6e49 2020-09-26 stsp pflag ? choose_patch : NULL, &cpa,
9079 417a6e49 2020-09-26 stsp allow_bad_symlinks, repo);
9082 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9083 417a6e49 2020-09-26 stsp error == NULL)
9084 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9086 417a6e49 2020-09-26 stsp got_repo_close(repo);
9087 417a6e49 2020-09-26 stsp if (worktree)
9088 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9089 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9090 417a6e49 2020-09-26 stsp free((char *)pe->path);
9091 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9092 417a6e49 2020-09-26 stsp free(cwd);
9093 417a6e49 2020-09-26 stsp return error;
9096 417a6e49 2020-09-26 stsp __dead static void
9097 417a6e49 2020-09-26 stsp usage_unstage(void)
9099 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9100 417a6e49 2020-09-26 stsp "[file-path ...]\n",
9101 417a6e49 2020-09-26 stsp getprogname());
9106 417a6e49 2020-09-26 stsp static const struct got_error *
9107 417a6e49 2020-09-26 stsp cmd_unstage(int argc, char *argv[])
9109 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9110 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9111 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9112 417a6e49 2020-09-26 stsp char *cwd = NULL;
9113 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9114 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9115 417a6e49 2020-09-26 stsp int ch, pflag = 0;
9116 417a6e49 2020-09-26 stsp struct got_update_progress_arg upa;
9117 417a6e49 2020-09-26 stsp FILE *patch_script_file = NULL;
9118 417a6e49 2020-09-26 stsp const char *patch_script_path = NULL;
9119 417a6e49 2020-09-26 stsp struct choose_patch_arg cpa;
9121 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9123 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9124 417a6e49 2020-09-26 stsp switch (ch) {
9126 417a6e49 2020-09-26 stsp pflag = 1;
9129 417a6e49 2020-09-26 stsp patch_script_path = optarg;
9132 417a6e49 2020-09-26 stsp usage_unstage();
9133 417a6e49 2020-09-26 stsp /* NOTREACHED */
9137 417a6e49 2020-09-26 stsp argc -= optind;
9138 417a6e49 2020-09-26 stsp argv += optind;
9140 417a6e49 2020-09-26 stsp #ifndef PROFILE
9141 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9142 417a6e49 2020-09-26 stsp "unveil", NULL) == -1)
9143 417a6e49 2020-09-26 stsp err(1, "pledge");
9145 417a6e49 2020-09-26 stsp if (patch_script_path && !pflag)
9146 417a6e49 2020-09-26 stsp errx(1, "-F option can only be used together with -p option");
9148 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9149 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9150 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9151 417a6e49 2020-09-26 stsp goto done;
9154 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9155 417a6e49 2020-09-26 stsp if (error) {
9156 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9157 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9158 417a6e49 2020-09-26 stsp goto done;
9161 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9163 417a6e49 2020-09-26 stsp if (error != NULL)
9164 417a6e49 2020-09-26 stsp goto done;
9166 417a6e49 2020-09-26 stsp if (patch_script_path) {
9167 417a6e49 2020-09-26 stsp patch_script_file = fopen(patch_script_path, "r");
9168 417a6e49 2020-09-26 stsp if (patch_script_file == NULL) {
9169 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fopen",
9170 417a6e49 2020-09-26 stsp patch_script_path);
9171 417a6e49 2020-09-26 stsp goto done;
9175 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9176 417a6e49 2020-09-26 stsp got_worktree_get_root_path(worktree));
9177 417a6e49 2020-09-26 stsp if (error)
9178 417a6e49 2020-09-26 stsp goto done;
9180 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9181 417a6e49 2020-09-26 stsp if (error)
9182 417a6e49 2020-09-26 stsp goto done;
9184 417a6e49 2020-09-26 stsp cpa.patch_script_file = patch_script_file;
9185 417a6e49 2020-09-26 stsp cpa.action = "unstage";
9186 417a6e49 2020-09-26 stsp memset(&upa, 0, sizeof(upa));
9187 417a6e49 2020-09-26 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9188 417a6e49 2020-09-26 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9189 417a6e49 2020-09-26 stsp if (!error)
9190 417a6e49 2020-09-26 stsp print_update_progress_stats(&upa);
9192 417a6e49 2020-09-26 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9193 417a6e49 2020-09-26 stsp error == NULL)
9194 417a6e49 2020-09-26 stsp error = got_error_from_errno2("fclose", patch_script_path);
9196 417a6e49 2020-09-26 stsp got_repo_close(repo);
9197 417a6e49 2020-09-26 stsp if (worktree)
9198 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9199 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9200 417a6e49 2020-09-26 stsp free((char *)pe->path);
9201 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9202 417a6e49 2020-09-26 stsp free(cwd);
9203 417a6e49 2020-09-26 stsp return error;
9206 417a6e49 2020-09-26 stsp __dead static void
9207 417a6e49 2020-09-26 stsp usage_cat(void)
9209 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9210 417a6e49 2020-09-26 stsp "arg1 [arg2 ...]\n", getprogname());
9214 417a6e49 2020-09-26 stsp static const struct got_error *
9215 417a6e49 2020-09-26 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9217 417a6e49 2020-09-26 stsp const struct got_error *err;
9218 417a6e49 2020-09-26 stsp struct got_blob_object *blob;
9220 417a6e49 2020-09-26 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9222 417a6e49 2020-09-26 stsp return err;
9224 417a6e49 2020-09-26 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9225 417a6e49 2020-09-26 stsp got_object_blob_close(blob);
9226 417a6e49 2020-09-26 stsp return err;
9229 417a6e49 2020-09-26 stsp static const struct got_error *
9230 417a6e49 2020-09-26 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9232 417a6e49 2020-09-26 stsp const struct got_error *err;
9233 417a6e49 2020-09-26 stsp struct got_tree_object *tree;
9234 417a6e49 2020-09-26 stsp int nentries, i;
9236 417a6e49 2020-09-26 stsp err = got_object_open_as_tree(&tree, repo, id);
9238 417a6e49 2020-09-26 stsp return err;
9240 417a6e49 2020-09-26 stsp nentries = got_object_tree_get_nentries(tree);
9241 417a6e49 2020-09-26 stsp for (i = 0; i < nentries; i++) {
9242 417a6e49 2020-09-26 stsp struct got_tree_entry *te;
9243 417a6e49 2020-09-26 stsp char *id_str;
9244 417a6e49 2020-09-26 stsp if (sigint_received || sigpipe_received)
9246 417a6e49 2020-09-26 stsp te = got_object_tree_get_entry(tree, i);
9247 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9250 417a6e49 2020-09-26 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9251 417a6e49 2020-09-26 stsp got_tree_entry_get_mode(te),
9252 417a6e49 2020-09-26 stsp got_tree_entry_get_name(te));
9253 417a6e49 2020-09-26 stsp free(id_str);
9256 417a6e49 2020-09-26 stsp got_object_tree_close(tree);
9257 417a6e49 2020-09-26 stsp return err;
9260 417a6e49 2020-09-26 stsp static const struct got_error *
9261 417a6e49 2020-09-26 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9263 417a6e49 2020-09-26 stsp const struct got_error *err;
9264 417a6e49 2020-09-26 stsp struct got_commit_object *commit;
9265 417a6e49 2020-09-26 stsp const struct got_object_id_queue *parent_ids;
9266 417a6e49 2020-09-26 stsp struct got_object_qid *pid;
9267 417a6e49 2020-09-26 stsp char *id_str = NULL;
9268 417a6e49 2020-09-26 stsp const char *logmsg = NULL;
9270 417a6e49 2020-09-26 stsp err = got_object_open_as_commit(&commit, repo, id);
9272 417a6e49 2020-09-26 stsp return err;
9274 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9276 417a6e49 2020-09-26 stsp goto done;
9278 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9279 417a6e49 2020-09-26 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9280 417a6e49 2020-09-26 stsp fprintf(outfile, "numparents %d\n",
9281 417a6e49 2020-09-26 stsp got_object_commit_get_nparents(commit));
9282 417a6e49 2020-09-26 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9283 417a6e49 2020-09-26 stsp char *pid_str;
9284 417a6e49 2020-09-26 stsp err = got_object_id_str(&pid_str, pid->id);
9286 417a6e49 2020-09-26 stsp goto done;
9287 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9288 417a6e49 2020-09-26 stsp free(pid_str);
9290 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9291 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9292 417a6e49 2020-09-26 stsp got_object_commit_get_author_time(commit));
9294 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9295 417a6e49 2020-09-26 stsp got_object_commit_get_author(commit),
9296 417a6e49 2020-09-26 stsp got_object_commit_get_committer_time(commit));
9298 417a6e49 2020-09-26 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9299 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9300 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", logmsg);
9302 417a6e49 2020-09-26 stsp free(id_str);
9303 417a6e49 2020-09-26 stsp got_object_commit_close(commit);
9304 417a6e49 2020-09-26 stsp return err;
9307 417a6e49 2020-09-26 stsp static const struct got_error *
9308 417a6e49 2020-09-26 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9310 417a6e49 2020-09-26 stsp const struct got_error *err;
9311 417a6e49 2020-09-26 stsp struct got_tag_object *tag;
9312 417a6e49 2020-09-26 stsp char *id_str = NULL;
9313 417a6e49 2020-09-26 stsp const char *tagmsg = NULL;
9315 417a6e49 2020-09-26 stsp err = got_object_open_as_tag(&tag, repo, id);
9317 417a6e49 2020-09-26 stsp return err;
9319 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9321 417a6e49 2020-09-26 stsp goto done;
9323 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9325 417a6e49 2020-09-26 stsp switch (got_object_tag_get_object_type(tag)) {
9326 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9327 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9328 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_BLOB);
9330 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9331 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9332 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TREE);
9334 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9335 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9336 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_COMMIT);
9338 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9339 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9340 417a6e49 2020-09-26 stsp GOT_OBJ_LABEL_TAG);
9346 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9347 417a6e49 2020-09-26 stsp got_object_tag_get_name(tag));
9349 417a6e49 2020-09-26 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9350 417a6e49 2020-09-26 stsp got_object_tag_get_tagger(tag),
9351 417a6e49 2020-09-26 stsp got_object_tag_get_tagger_time(tag));
9353 417a6e49 2020-09-26 stsp tagmsg = got_object_tag_get_message(tag);
9354 417a6e49 2020-09-26 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9355 417a6e49 2020-09-26 stsp fprintf(outfile, "%s", tagmsg);
9357 417a6e49 2020-09-26 stsp free(id_str);
9358 417a6e49 2020-09-26 stsp got_object_tag_close(tag);
9359 417a6e49 2020-09-26 stsp return err;
9362 417a6e49 2020-09-26 stsp static const struct got_error *
9363 417a6e49 2020-09-26 stsp cmd_cat(int argc, char *argv[])
9365 417a6e49 2020-09-26 stsp const struct got_error *error;
9366 417a6e49 2020-09-26 stsp struct got_repository *repo = NULL;
9367 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9368 417a6e49 2020-09-26 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9369 417a6e49 2020-09-26 stsp const char *commit_id_str = NULL;
9370 417a6e49 2020-09-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9371 417a6e49 2020-09-26 stsp int ch, obj_type, i, force_path = 0;
9373 417a6e49 2020-09-26 stsp #ifndef PROFILE
9374 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9375 417a6e49 2020-09-26 stsp NULL) == -1)
9376 417a6e49 2020-09-26 stsp err(1, "pledge");
9379 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9380 417a6e49 2020-09-26 stsp switch (ch) {
9382 417a6e49 2020-09-26 stsp commit_id_str = optarg;
9385 417a6e49 2020-09-26 stsp repo_path = realpath(optarg, NULL);
9386 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9387 417a6e49 2020-09-26 stsp return got_error_from_errno2("realpath",
9389 417a6e49 2020-09-26 stsp got_path_strip_trailing_slashes(repo_path);
9392 417a6e49 2020-09-26 stsp force_path = 1;
9395 417a6e49 2020-09-26 stsp usage_cat();
9396 417a6e49 2020-09-26 stsp /* NOTREACHED */
9400 417a6e49 2020-09-26 stsp argc -= optind;
9401 417a6e49 2020-09-26 stsp argv += optind;
9403 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9404 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9405 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9406 417a6e49 2020-09-26 stsp goto done;
9408 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9409 417a6e49 2020-09-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9410 417a6e49 2020-09-26 stsp goto done;
9411 417a6e49 2020-09-26 stsp if (worktree) {
9412 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9413 417a6e49 2020-09-26 stsp repo_path = strdup(
9414 417a6e49 2020-09-26 stsp got_worktree_get_repo_path(worktree));
9415 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9416 417a6e49 2020-09-26 stsp error = got_error_from_errno("strdup");
9417 417a6e49 2020-09-26 stsp goto done;
9422 417a6e49 2020-09-26 stsp if (repo_path == NULL) {
9423 417a6e49 2020-09-26 stsp repo_path = getcwd(NULL, 0);
9424 417a6e49 2020-09-26 stsp if (repo_path == NULL)
9425 417a6e49 2020-09-26 stsp return got_error_from_errno("getcwd");
9428 417a6e49 2020-09-26 stsp error = got_repo_open(&repo, repo_path, NULL);
9429 417a6e49 2020-09-26 stsp free(repo_path);
9430 417a6e49 2020-09-26 stsp if (error != NULL)
9431 417a6e49 2020-09-26 stsp goto done;
9433 417a6e49 2020-09-26 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9434 417a6e49 2020-09-26 stsp if (error)
9435 417a6e49 2020-09-26 stsp goto done;
9437 417a6e49 2020-09-26 stsp if (commit_id_str == NULL)
9438 417a6e49 2020-09-26 stsp commit_id_str = GOT_REF_HEAD;
9439 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&commit_id, NULL,
9440 417a6e49 2020-09-26 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9441 417a6e49 2020-09-26 stsp if (error)
9442 417a6e49 2020-09-26 stsp goto done;
9444 417a6e49 2020-09-26 stsp for (i = 0; i < argc; i++) {
9445 417a6e49 2020-09-26 stsp if (force_path) {
9446 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9448 417a6e49 2020-09-26 stsp if (error)
9451 417a6e49 2020-09-26 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9452 417a6e49 2020-09-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9453 417a6e49 2020-09-26 stsp if (error) {
9454 417a6e49 2020-09-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9455 417a6e49 2020-09-26 stsp error->code != GOT_ERR_NOT_REF)
9457 417a6e49 2020-09-26 stsp error = got_object_id_by_path(&id, repo,
9458 417a6e49 2020-09-26 stsp commit_id, argv[i]);
9459 417a6e49 2020-09-26 stsp if (error)
9464 417a6e49 2020-09-26 stsp error = got_object_get_type(&obj_type, repo, id);
9465 417a6e49 2020-09-26 stsp if (error)
9468 417a6e49 2020-09-26 stsp switch (obj_type) {
9469 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_BLOB:
9470 417a6e49 2020-09-26 stsp error = cat_blob(id, repo, stdout);
9472 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TREE:
9473 417a6e49 2020-09-26 stsp error = cat_tree(id, repo, stdout);
9475 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_COMMIT:
9476 417a6e49 2020-09-26 stsp error = cat_commit(id, repo, stdout);
9478 417a6e49 2020-09-26 stsp case GOT_OBJ_TYPE_TAG:
9479 417a6e49 2020-09-26 stsp error = cat_tag(id, repo, stdout);
9482 417a6e49 2020-09-26 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9485 417a6e49 2020-09-26 stsp if (error)
9487 417a6e49 2020-09-26 stsp free(label);
9488 417a6e49 2020-09-26 stsp label = NULL;
9490 417a6e49 2020-09-26 stsp id = NULL;
9493 417a6e49 2020-09-26 stsp free(label);
9495 417a6e49 2020-09-26 stsp free(commit_id);
9496 417a6e49 2020-09-26 stsp if (worktree)
9497 417a6e49 2020-09-26 stsp got_worktree_close(worktree);
9498 417a6e49 2020-09-26 stsp if (repo) {
9499 417a6e49 2020-09-26 stsp const struct got_error *repo_error;
9500 417a6e49 2020-09-26 stsp repo_error = got_repo_close(repo);
9501 417a6e49 2020-09-26 stsp if (error == NULL)
9502 417a6e49 2020-09-26 stsp error = repo_error;
9504 417a6e49 2020-09-26 stsp return error;
9507 417a6e49 2020-09-26 stsp __dead static void
9508 417a6e49 2020-09-26 stsp usage_info(void)
9510 417a6e49 2020-09-26 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9511 417a6e49 2020-09-26 stsp getprogname());
9515 417a6e49 2020-09-26 stsp static const struct got_error *
9516 417a6e49 2020-09-26 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9517 417a6e49 2020-09-26 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9518 417a6e49 2020-09-26 stsp struct got_object_id *commit_id)
9520 417a6e49 2020-09-26 stsp const struct got_error *err = NULL;
9521 417a6e49 2020-09-26 stsp char *id_str = NULL;
9522 417a6e49 2020-09-26 stsp char datebuf[128];
9523 417a6e49 2020-09-26 stsp struct tm mytm, *tm;
9524 417a6e49 2020-09-26 stsp struct got_pathlist_head *paths = arg;
9525 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9528 417a6e49 2020-09-26 stsp * Clear error indication from any of the path arguments which
9529 417a6e49 2020-09-26 stsp * would cause this file index entry to be displayed.
9531 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, paths, entry) {
9532 417a6e49 2020-09-26 stsp if (got_path_cmp(path, pe->path, strlen(path),
9533 417a6e49 2020-09-26 stsp pe->path_len) == 0 ||
9534 417a6e49 2020-09-26 stsp got_path_is_child(path, pe->path, pe->path_len))
9535 417a6e49 2020-09-26 stsp pe->data = NULL; /* no error */
9538 417a6e49 2020-09-26 stsp printf(GOT_COMMIT_SEP_STR);
9539 417a6e49 2020-09-26 stsp if (S_ISLNK(mode))
9540 417a6e49 2020-09-26 stsp printf("symlink: %s\n", path);
9541 417a6e49 2020-09-26 stsp else if (S_ISREG(mode)) {
9542 417a6e49 2020-09-26 stsp printf("file: %s\n", path);
9543 417a6e49 2020-09-26 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9544 417a6e49 2020-09-26 stsp } else if (S_ISDIR(mode))
9545 417a6e49 2020-09-26 stsp printf("directory: %s\n", path);
9547 417a6e49 2020-09-26 stsp printf("something: %s\n", path);
9549 417a6e49 2020-09-26 stsp tm = localtime_r(&mtime, &mytm);
9550 417a6e49 2020-09-26 stsp if (tm == NULL)
9551 417a6e49 2020-09-26 stsp return NULL;
9552 417a6e49 2020-09-26 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9553 417a6e49 2020-09-26 stsp return got_error(GOT_ERR_NO_SPACE);
9554 417a6e49 2020-09-26 stsp printf("timestamp: %s\n", datebuf);
9556 417a6e49 2020-09-26 stsp if (blob_id) {
9557 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, blob_id);
9559 417a6e49 2020-09-26 stsp return err;
9560 417a6e49 2020-09-26 stsp printf("based on blob: %s\n", id_str);
9561 417a6e49 2020-09-26 stsp free(id_str);
9564 417a6e49 2020-09-26 stsp if (staged_blob_id) {
9565 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, staged_blob_id);
9567 417a6e49 2020-09-26 stsp return err;
9568 417a6e49 2020-09-26 stsp printf("based on staged blob: %s\n", id_str);
9569 417a6e49 2020-09-26 stsp free(id_str);
9572 417a6e49 2020-09-26 stsp if (commit_id) {
9573 417a6e49 2020-09-26 stsp err = got_object_id_str(&id_str, commit_id);
9575 417a6e49 2020-09-26 stsp return err;
9576 417a6e49 2020-09-26 stsp printf("based on commit: %s\n", id_str);
9577 417a6e49 2020-09-26 stsp free(id_str);
9580 417a6e49 2020-09-26 stsp return NULL;
9583 417a6e49 2020-09-26 stsp static const struct got_error *
9584 417a6e49 2020-09-26 stsp cmd_info(int argc, char *argv[])
9586 417a6e49 2020-09-26 stsp const struct got_error *error = NULL;
9587 417a6e49 2020-09-26 stsp struct got_worktree *worktree = NULL;
9588 417a6e49 2020-09-26 stsp char *cwd = NULL, *id_str = NULL;
9589 417a6e49 2020-09-26 stsp struct got_pathlist_head paths;
9590 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9591 417a6e49 2020-09-26 stsp char *uuidstr = NULL;
9592 417a6e49 2020-09-26 stsp int ch, show_files = 0;
9594 417a6e49 2020-09-26 stsp TAILQ_INIT(&paths);
9596 417a6e49 2020-09-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9597 417a6e49 2020-09-26 stsp switch (ch) {
9599 417a6e49 2020-09-26 stsp usage_info();
9600 417a6e49 2020-09-26 stsp /* NOTREACHED */
9604 417a6e49 2020-09-26 stsp argc -= optind;
9605 417a6e49 2020-09-26 stsp argv += optind;
9607 417a6e49 2020-09-26 stsp #ifndef PROFILE
9608 417a6e49 2020-09-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9609 417a6e49 2020-09-26 stsp NULL) == -1)
9610 417a6e49 2020-09-26 stsp err(1, "pledge");
9612 417a6e49 2020-09-26 stsp cwd = getcwd(NULL, 0);
9613 417a6e49 2020-09-26 stsp if (cwd == NULL) {
9614 417a6e49 2020-09-26 stsp error = got_error_from_errno("getcwd");
9615 417a6e49 2020-09-26 stsp goto done;
9618 417a6e49 2020-09-26 stsp error = got_worktree_open(&worktree, cwd);
9619 417a6e49 2020-09-26 stsp if (error) {
9620 417a6e49 2020-09-26 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9621 417a6e49 2020-09-26 stsp error = wrap_not_worktree_error(error, "status", cwd);
9622 417a6e49 2020-09-26 stsp goto done;
9625 417a6e49 2020-09-26 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9626 417a6e49 2020-09-26 stsp if (error)
9627 417a6e49 2020-09-26 stsp goto done;
9629 417a6e49 2020-09-26 stsp if (argc >= 1) {
9630 417a6e49 2020-09-26 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9631 417a6e49 2020-09-26 stsp worktree);
9632 417a6e49 2020-09-26 stsp if (error)
9633 417a6e49 2020-09-26 stsp goto done;
9634 417a6e49 2020-09-26 stsp show_files = 1;
9637 417a6e49 2020-09-26 stsp error = got_object_id_str(&id_str,
9638 417a6e49 2020-09-26 stsp got_worktree_get_base_commit_id(worktree));
9639 417a6e49 2020-09-26 stsp if (error)
9640 417a6e49 2020-09-26 stsp goto done;
9642 417a6e49 2020-09-26 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9643 417a6e49 2020-09-26 stsp if (error)
9644 417a6e49 2020-09-26 stsp goto done;
9646 417a6e49 2020-09-26 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9647 417a6e49 2020-09-26 stsp printf("work tree base commit: %s\n", id_str);
9648 417a6e49 2020-09-26 stsp printf("work tree path prefix: %s\n",
9649 417a6e49 2020-09-26 stsp got_worktree_get_path_prefix(worktree));
9650 417a6e49 2020-09-26 stsp printf("work tree branch reference: %s\n",
9651 417a6e49 2020-09-26 stsp got_worktree_get_head_ref_name(worktree));
9652 417a6e49 2020-09-26 stsp printf("work tree UUID: %s\n", uuidstr);
9653 417a6e49 2020-09-26 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9655 417a6e49 2020-09-26 stsp if (show_files) {
9656 417a6e49 2020-09-26 stsp struct got_pathlist_entry *pe;
9657 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9658 417a6e49 2020-09-26 stsp if (pe->path_len == 0)
9661 417a6e49 2020-09-26 stsp * Assume this path will fail. This will be corrected
9662 417a6e49 2020-09-26 stsp * in print_path_info() in case the path does suceeed.
9664 417a6e49 2020-09-26 stsp pe->data = (void *)got_error_path(pe->path,
9665 417a6e49 2020-09-26 stsp GOT_ERR_BAD_PATH);
9667 417a6e49 2020-09-26 stsp error = got_worktree_path_info(worktree, &paths,
9668 417a6e49 2020-09-26 stsp print_path_info, &paths, check_cancelled, NULL);
9669 417a6e49 2020-09-26 stsp if (error)
9670 417a6e49 2020-09-26 stsp goto done;
9671 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry) {
9672 417a6e49 2020-09-26 stsp if (pe->data != NULL) {
9673 417a6e49 2020-09-26 stsp error = pe->data; /* bad path */
9679 417a6e49 2020-09-26 stsp TAILQ_FOREACH(pe, &paths, entry)
9680 417a6e49 2020-09-26 stsp free((char *)pe->path);
9681 417a6e49 2020-09-26 stsp got_pathlist_free(&paths);
9682 417a6e49 2020-09-26 stsp free(cwd);
9683 417a6e49 2020-09-26 stsp free(id_str);
9684 417a6e49 2020-09-26 stsp free(uuidstr);
9685 417a6e49 2020-09-26 stsp return error;