2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
66 99437157 2018-11-11 stsp catch_sigint(int signo)
68 99437157 2018-11-11 stsp sigint_received = 1;
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
74 99437157 2018-11-11 stsp sigpipe_received = 1;
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
262 ce5b7c56 2019-07-09 stsp list_commands();
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
272 8920fa04 2019-08-18 stsp *abspath = NULL;
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
281 0ee7065d 2019-05-13 stsp return err;
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
290 e2ba3d07 2019-05-13 stsp return NULL;
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
297 163ce85a 2019-05-13 stsp const struct got_error *err;
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
319 0266afb7 2019-01-04 stsp return NULL;
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
396 3ce1b845 2019-07-15 stsp goto doneediting;
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
463 3ce1b845 2019-07-15 stsp fclose(fp);
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
478 3ce1b845 2019-07-15 stsp return err;
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
521 84792843 2019-08-09 stsp *author = NULL;
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
573 aba9c984 2019-09-08 stsp return err;
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
587 c9956ddf 2019-09-08 stsp return NULL;
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
669 aba9c984 2019-09-08 stsp return error;
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
884 531c3985 2020-03-18 stsp return NULL;
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
896 4ba14133 2020-03-20 stsp return err;
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
904 4ba14133 2020-03-20 stsp return err;
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
933 41b0de12 2020-03-21 stsp return NULL;
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
946 6338a6a1 2020-03-21 stsp return err;
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
967 0e4002ca 2020-03-21 stsp refname += 5;
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1074 68999b92 2020-03-18 stsp verbosity = -1;
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1083 93658fb9 2020-03-18 stsp usage_clone();
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1112 93658fb9 2020-03-18 stsp usage_clone();
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1180 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 9c52365f 2020-03-21 stsp server_path, verbosity);
1182 39c64a6a 2020-03-18 stsp if (error)
1183 bb64b798 2020-03-18 stsp goto done;
1185 68999b92 2020-03-18 stsp if (verbosity >= 0)
1186 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1187 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1233 469dd726 2020-03-20 stsp if (mirror_references)
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1267 d9b4d0c0 2020-03-18 stsp goto done;
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1302 f298ae0f 2020-03-25 stsp goto done;
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1328 4ba14133 2020-03-20 stsp goto done;
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1453 7848a0e1 2020-03-19 stsp goto done;
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1466 db6d8ad8 2020-03-21 stsp goto done;
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1480 e8a967e0 2020-03-21 stsp goto done;
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1493 6338a6a1 2020-03-21 stsp goto done;
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1511 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1513 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1514 bcf34b0e 2020-03-26 stsp if (err) {
1515 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1516 bcf34b0e 2020-03-26 stsp return err;
1517 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1519 bcf34b0e 2020-03-26 stsp goto done;
1521 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1523 bcf34b0e 2020-03-26 stsp goto done;
1525 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1526 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1527 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1528 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1530 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1532 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1533 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1534 bcf34b0e 2020-03-26 stsp goto done;
1536 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1537 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1539 bcf34b0e 2020-03-26 stsp goto done;
1541 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1543 bcf34b0e 2020-03-26 stsp goto done;
1545 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1546 bcf34b0e 2020-03-26 stsp printf("Updated reference %s: %s\n",
1547 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1548 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1552 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1553 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1554 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1555 bcf34b0e 2020-03-26 stsp err = unlock_err;
1557 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1558 f1bcca34 2020-03-25 stsp return err;
1561 2ab43947 2020-03-18 stsp __dead static void
1562 7848a0e1 2020-03-19 stsp usage_fetch(void)
1564 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1565 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1566 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1567 13f12b09 2020-03-21 stsp getprogname());
1571 7848a0e1 2020-03-19 stsp static const struct got_error *
1572 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1573 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1575 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1576 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1577 3789fd73 2020-03-26 stsp char *id_str = NULL;
1579 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1580 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1582 3789fd73 2020-03-26 stsp return err;
1583 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1584 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1585 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1586 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1589 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1591 3789fd73 2020-03-26 stsp return err;
1592 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1594 3789fd73 2020-03-26 stsp goto done;
1596 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1598 3789fd73 2020-03-26 stsp goto done;
1599 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1600 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1601 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1606 3789fd73 2020-03-26 stsp free(id_str);
1607 3789fd73 2020-03-26 stsp return NULL;
1610 3789fd73 2020-03-26 stsp static const struct got_error *
1611 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1612 3789fd73 2020-03-26 stsp struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1613 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1615 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1616 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1617 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1618 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1619 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1620 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1622 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1624 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1626 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1628 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1630 3789fd73 2020-03-26 stsp goto done;
1632 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1633 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1635 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1636 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1637 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1638 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1639 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1641 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1642 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1643 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1644 3789fd73 2020-03-26 stsp goto done;
1646 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1650 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1651 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1654 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1657 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1658 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1661 3789fd73 2020-03-26 stsp if (pe != NULL)
1664 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1668 3789fd73 2020-03-26 stsp if (local_refname) {
1669 3789fd73 2020-03-26 stsp struct got_reference *ref;
1670 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1671 3789fd73 2020-03-26 stsp if (err) {
1672 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1674 3789fd73 2020-03-26 stsp free(local_refname);
1675 3789fd73 2020-03-26 stsp local_refname = NULL;
1678 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1681 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1682 3789fd73 2020-03-26 stsp got_ref_close(ref);
1683 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1684 3789fd73 2020-03-26 stsp err = unlock_err;
1688 3789fd73 2020-03-26 stsp free(local_refname);
1689 3789fd73 2020-03-26 stsp local_refname = NULL;
1693 3789fd73 2020-03-26 stsp free(remote_namespace);
1694 3789fd73 2020-03-26 stsp free(local_refname);
1695 0e4002ca 2020-03-21 stsp return err;
1698 0e4002ca 2020-03-21 stsp static const struct got_error *
1699 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1700 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1702 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1703 0e4002ca 2020-03-21 stsp char *remote_refname;
1704 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1706 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1707 0e4002ca 2020-03-21 stsp refname += 5;
1709 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1710 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1711 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1713 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1714 0e4002ca 2020-03-21 stsp if (err) {
1715 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1716 0e4002ca 2020-03-21 stsp goto done;
1717 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1719 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1720 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1721 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1722 9f142382 2020-03-21 stsp err = unlock_err;
1723 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1726 0e4002ca 2020-03-21 stsp free(remote_refname);
1727 f21ec2f0 2020-03-21 stsp return err;
1730 f21ec2f0 2020-03-21 stsp static const struct got_error *
1731 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1733 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1734 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1735 7848a0e1 2020-03-19 stsp const char *remote_name;
1736 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1737 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1738 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1739 7848a0e1 2020-03-19 stsp int nremotes;
1740 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1741 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1742 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1743 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1744 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1745 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1746 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1747 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1748 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1749 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1750 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1752 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1753 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1754 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1755 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1757 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1758 7848a0e1 2020-03-19 stsp switch (ch) {
1760 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1763 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1764 4ba14133 2020-03-20 stsp optarg, NULL);
1765 4ba14133 2020-03-20 stsp if (error)
1766 4ba14133 2020-03-20 stsp return error;
1769 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1772 41b0de12 2020-03-21 stsp list_refs_only = 1;
1775 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1776 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1777 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1779 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1782 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1785 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1786 7848a0e1 2020-03-19 stsp verbosity = 0;
1787 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1788 7848a0e1 2020-03-19 stsp verbosity++;
1791 7848a0e1 2020-03-19 stsp verbosity = -1;
1794 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1795 0e4002ca 2020-03-21 stsp optarg, NULL);
1796 0e4002ca 2020-03-21 stsp if (error)
1797 0e4002ca 2020-03-21 stsp return error;
1800 7848a0e1 2020-03-19 stsp usage_fetch();
1804 7848a0e1 2020-03-19 stsp argc -= optind;
1805 7848a0e1 2020-03-19 stsp argv += optind;
1807 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1808 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1809 41b0de12 2020-03-21 stsp if (list_refs_only) {
1810 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1811 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1812 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1813 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1814 f21ec2f0 2020-03-21 stsp if (delete_refs)
1815 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1816 41b0de12 2020-03-21 stsp if (verbosity == -1)
1817 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1820 7848a0e1 2020-03-19 stsp if (argc == 0)
1821 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1822 7848a0e1 2020-03-19 stsp else if (argc == 1)
1823 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1825 7848a0e1 2020-03-19 stsp usage_fetch();
1827 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1828 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1829 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1830 7848a0e1 2020-03-19 stsp goto done;
1833 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1834 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1835 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1836 7848a0e1 2020-03-19 stsp goto done;
1838 7848a0e1 2020-03-19 stsp error = NULL;
1839 7848a0e1 2020-03-19 stsp if (worktree) {
1840 7848a0e1 2020-03-19 stsp repo_path =
1841 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1842 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1843 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1844 7848a0e1 2020-03-19 stsp if (error)
1845 7848a0e1 2020-03-19 stsp goto done;
1847 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1848 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1849 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1850 7848a0e1 2020-03-19 stsp goto done;
1855 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1856 7848a0e1 2020-03-19 stsp if (error)
1857 7848a0e1 2020-03-19 stsp goto done;
1859 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1860 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1861 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1862 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1865 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1866 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1867 7848a0e1 2020-03-19 stsp goto done;
1870 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1871 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1872 7848a0e1 2020-03-19 stsp if (error)
1873 7848a0e1 2020-03-19 stsp goto done;
1875 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1876 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1877 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1878 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1879 7848a0e1 2020-03-19 stsp err(1, "pledge");
1881 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1882 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1883 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1884 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1885 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1886 7848a0e1 2020-03-19 stsp err(1, "pledge");
1888 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1889 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1890 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1891 7848a0e1 2020-03-19 stsp goto done;
1893 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1894 7848a0e1 2020-03-19 stsp goto done;
1897 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1898 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1899 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1900 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1901 7848a0e1 2020-03-19 stsp goto done;
1904 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1905 7848a0e1 2020-03-19 stsp if (error)
1906 7848a0e1 2020-03-19 stsp goto done;
1908 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1909 9c52365f 2020-03-21 stsp server_path, verbosity);
1910 7848a0e1 2020-03-19 stsp if (error)
1911 7848a0e1 2020-03-19 stsp goto done;
1913 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1914 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1915 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1917 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1918 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1919 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1920 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1921 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1922 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1923 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1924 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1925 7848a0e1 2020-03-19 stsp if (error)
1926 7848a0e1 2020-03-19 stsp goto done;
1928 41b0de12 2020-03-21 stsp if (list_refs_only) {
1929 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1930 41b0de12 2020-03-21 stsp goto done;
1933 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1934 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1935 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1936 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
1937 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1938 984065c8 2020-03-19 stsp if (error)
1939 984065c8 2020-03-19 stsp goto done;
1940 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1941 984065c8 2020-03-19 stsp free(id_str);
1942 984065c8 2020-03-19 stsp id_str = NULL;
1945 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1946 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1947 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1949 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1950 7848a0e1 2020-03-19 stsp char *remote_refname;
1952 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1953 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1954 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1955 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1956 0e4002ca 2020-03-21 stsp if (error)
1957 0e4002ca 2020-03-21 stsp goto done;
1961 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1962 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1963 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1964 7848a0e1 2020-03-19 stsp if (error) {
1965 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1966 7848a0e1 2020-03-19 stsp goto done;
1967 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1969 7848a0e1 2020-03-19 stsp if (error)
1970 7848a0e1 2020-03-19 stsp goto done;
1972 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1973 db6d8ad8 2020-03-21 stsp verbosity, repo);
1974 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1975 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1976 9f142382 2020-03-21 stsp error = unlock_err;
1977 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1978 7848a0e1 2020-03-19 stsp if (error)
1979 7848a0e1 2020-03-19 stsp goto done;
1981 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1982 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1983 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1984 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1985 7848a0e1 2020-03-19 stsp goto done;
1988 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1989 7848a0e1 2020-03-19 stsp if (error) {
1990 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1991 7848a0e1 2020-03-19 stsp goto done;
1992 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1993 688f11b3 2020-03-21 stsp verbosity, repo);
1994 7848a0e1 2020-03-19 stsp if (error)
1995 7848a0e1 2020-03-19 stsp goto done;
1997 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1998 db6d8ad8 2020-03-21 stsp verbosity, repo);
1999 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2000 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2001 9f142382 2020-03-21 stsp error = unlock_err;
2002 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2003 7848a0e1 2020-03-19 stsp if (error)
2004 7848a0e1 2020-03-19 stsp goto done;
2007 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2008 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2009 2ec30c80 2020-03-20 stsp if (error) {
2010 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2011 2ec30c80 2020-03-20 stsp goto done;
2012 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2014 2ec30c80 2020-03-20 stsp if (error)
2015 2ec30c80 2020-03-20 stsp goto done;
2017 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2018 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2019 9f142382 2020-03-21 stsp error = unlock_err;
2020 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2024 f1bcca34 2020-03-25 stsp if (delete_refs) {
2025 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2026 3789fd73 2020-03-26 stsp verbosity, repo);
2027 f1bcca34 2020-03-25 stsp if (error)
2028 f1bcca34 2020-03-25 stsp goto done;
2031 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2032 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2033 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2034 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2035 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2036 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2037 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2039 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2042 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2045 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2046 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2047 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2048 f1bcca34 2020-03-25 stsp goto done;
2050 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2051 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2052 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2053 f1bcca34 2020-03-25 stsp free(remote_refname);
2054 f1bcca34 2020-03-25 stsp goto done;
2057 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2059 f1bcca34 2020-03-25 stsp if (error) {
2060 f1bcca34 2020-03-25 stsp free(remote_refname);
2061 f1bcca34 2020-03-25 stsp free(remote_target);
2062 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2063 f1bcca34 2020-03-25 stsp error = NULL;
2066 f1bcca34 2020-03-25 stsp goto done;
2068 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2069 f1bcca34 2020-03-25 stsp verbosity, repo);
2070 f1bcca34 2020-03-25 stsp free(remote_refname);
2071 f1bcca34 2020-03-25 stsp free(remote_target);
2072 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2073 f1bcca34 2020-03-25 stsp if (error)
2074 f1bcca34 2020-03-25 stsp goto done;
2078 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2079 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2080 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2081 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2082 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2084 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2085 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2087 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2088 7848a0e1 2020-03-19 stsp if (worktree)
2089 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2090 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2091 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2092 7848a0e1 2020-03-19 stsp free(pe->data);
2094 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2095 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2096 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2097 7848a0e1 2020-03-19 stsp free(pe->data);
2099 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2100 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2101 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2102 7848a0e1 2020-03-19 stsp free(id_str);
2103 7848a0e1 2020-03-19 stsp free(cwd);
2104 7848a0e1 2020-03-19 stsp free(repo_path);
2105 7848a0e1 2020-03-19 stsp free(pack_hash);
2106 7848a0e1 2020-03-19 stsp free(proto);
2107 7848a0e1 2020-03-19 stsp free(host);
2108 7848a0e1 2020-03-19 stsp free(port);
2109 7848a0e1 2020-03-19 stsp free(server_path);
2110 7848a0e1 2020-03-19 stsp free(repo_name);
2111 7848a0e1 2020-03-19 stsp return error;
2115 7848a0e1 2020-03-19 stsp __dead static void
2116 2ab43947 2020-03-18 stsp usage_checkout(void)
2118 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2119 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2123 2ab43947 2020-03-18 stsp static void
2124 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2126 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2127 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2128 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2129 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2130 2ab43947 2020-03-18 stsp getprogname());
2133 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2134 2ab43947 2020-03-18 stsp const char *worktree_path;
2135 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2138 2ab43947 2020-03-18 stsp static const struct got_error *
2139 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2141 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2143 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2144 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2145 2ab43947 2020-03-18 stsp return NULL;
2147 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2148 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2149 2ab43947 2020-03-18 stsp return NULL;
2152 2ab43947 2020-03-18 stsp while (path[0] == '/')
2155 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2156 2ab43947 2020-03-18 stsp return NULL;
2159 2ab43947 2020-03-18 stsp static const struct got_error *
2160 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2162 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2163 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2164 2ab43947 2020-03-18 stsp return NULL;
2167 2ab43947 2020-03-18 stsp static const struct got_error *
2168 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2169 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2170 2ab43947 2020-03-18 stsp struct got_repository *repo)
2172 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2173 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2175 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2176 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2178 2ab43947 2020-03-18 stsp return err;
2180 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2181 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2184 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2185 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2187 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2189 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2197 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2198 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2199 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2201 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2202 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2203 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2204 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2205 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2206 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2208 2ab43947 2020-03-18 stsp free(yca_id);
2209 2ab43947 2020-03-18 stsp return NULL;
2212 2ab43947 2020-03-18 stsp static const struct got_error *
2213 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2214 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2215 2ab43947 2020-03-18 stsp struct got_repository *repo)
2217 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2218 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2219 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2220 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2222 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2224 2ab43947 2020-03-18 stsp goto done;
2226 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2227 2ab43947 2020-03-18 stsp is_same_branch = 1;
2228 2ab43947 2020-03-18 stsp goto done;
2230 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2231 2ab43947 2020-03-18 stsp is_same_branch = 1;
2232 2ab43947 2020-03-18 stsp goto done;
2235 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2237 2ab43947 2020-03-18 stsp goto done;
2239 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2240 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2242 2ab43947 2020-03-18 stsp goto done;
2244 2ab43947 2020-03-18 stsp for (;;) {
2245 2ab43947 2020-03-18 stsp struct got_object_id *id;
2246 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2247 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2248 2ab43947 2020-03-18 stsp if (err) {
2249 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2250 2ab43947 2020-03-18 stsp err = NULL;
2255 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2257 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2258 2ab43947 2020-03-18 stsp is_same_branch = 1;
2264 2ab43947 2020-03-18 stsp if (graph)
2265 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2266 2ab43947 2020-03-18 stsp free(head_commit_id);
2267 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2268 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2269 2ab43947 2020-03-18 stsp return err;
2272 4ed7e80c 2018-05-20 stsp static const struct got_error *
2273 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2275 4b6c9460 2020-03-05 stsp static char msg[512];
2276 4b6c9460 2020-03-05 stsp const char *branch_name;
2278 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2279 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2281 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2283 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2284 4b6c9460 2020-03-05 stsp branch_name += 11;
2286 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2287 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2288 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2289 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2290 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2291 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2293 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2296 4b6c9460 2020-03-05 stsp static const struct got_error *
2297 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2299 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2300 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2301 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2302 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2303 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2304 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2305 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2306 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2307 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2308 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2309 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2310 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2312 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2314 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2315 0bb8a95e 2018-03-12 stsp switch (ch) {
2317 08573d5b 2019-05-14 stsp branch_name = optarg;
2320 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2321 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2322 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2325 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2328 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2331 2deda0b9 2019-03-07 stsp usage_checkout();
2332 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2336 0bb8a95e 2018-03-12 stsp argc -= optind;
2337 0bb8a95e 2018-03-12 stsp argv += optind;
2339 6715a751 2018-03-16 stsp #ifndef PROFILE
2340 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2341 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2342 c09a553d 2018-03-12 stsp err(1, "pledge");
2344 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2345 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2346 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2347 76089277 2018-04-01 stsp if (repo_path == NULL)
2348 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2349 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2350 76089277 2018-04-01 stsp if (cwd == NULL) {
2351 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2352 76089277 2018-04-01 stsp goto done;
2354 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2355 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2356 230a42bd 2019-05-11 jcs if (base == NULL) {
2357 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2358 230a42bd 2019-05-11 jcs path_prefix);
2362 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2363 230a42bd 2019-05-11 jcs if (base == NULL) {
2364 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2365 230a42bd 2019-05-11 jcs repo_path);
2369 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2370 c09a553d 2018-03-12 stsp if (dotgit)
2371 c09a553d 2018-03-12 stsp *dotgit = '\0';
2372 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2373 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2374 c09a553d 2018-03-12 stsp free(cwd);
2375 76089277 2018-04-01 stsp goto done;
2377 c09a553d 2018-03-12 stsp free(cwd);
2378 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2379 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2380 76089277 2018-04-01 stsp if (repo_path == NULL) {
2381 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2382 76089277 2018-04-01 stsp goto done;
2384 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2385 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2386 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2387 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2389 b4b3a7dd 2019-07-22 stsp goto done;
2391 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2392 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2393 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2394 b4b3a7dd 2019-07-22 stsp goto done;
2398 c09a553d 2018-03-12 stsp usage_checkout();
2400 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2401 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2403 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2404 c09a553d 2018-03-12 stsp if (error != NULL)
2405 c02c541e 2019-03-29 stsp goto done;
2407 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2408 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2409 c530dc23 2019-07-23 stsp if (error) {
2410 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2411 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2412 c530dc23 2019-07-23 stsp goto done;
2413 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2414 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2415 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2416 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2417 c530dc23 2019-07-23 stsp goto done;
2421 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2422 c02c541e 2019-03-29 stsp if (error)
2423 c09a553d 2018-03-12 stsp goto done;
2425 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2426 c09a553d 2018-03-12 stsp if (error != NULL)
2427 c09a553d 2018-03-12 stsp goto done;
2429 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2430 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2431 c09a553d 2018-03-12 stsp goto done;
2433 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2434 c09a553d 2018-03-12 stsp if (error != NULL)
2435 c09a553d 2018-03-12 stsp goto done;
2437 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2438 e5dc7198 2018-12-29 stsp path_prefix);
2439 e5dc7198 2018-12-29 stsp if (error != NULL)
2440 e5dc7198 2018-12-29 stsp goto done;
2441 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2442 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2443 49520a32 2018-12-29 stsp goto done;
2446 8069f636 2019-01-12 stsp if (commit_id_str) {
2447 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2448 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2449 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2450 30837e32 2019-07-25 stsp if (error)
2451 8069f636 2019-01-12 stsp goto done;
2452 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2453 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2454 8069f636 2019-01-12 stsp if (error != NULL) {
2455 8069f636 2019-01-12 stsp free(commit_id);
2456 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2457 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2458 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2460 8069f636 2019-01-12 stsp goto done;
2462 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2463 4b6c9460 2020-03-05 stsp if (error) {
2464 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2465 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2466 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2468 45d344f6 2019-05-14 stsp goto done;
2470 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2471 8069f636 2019-01-12 stsp commit_id);
2472 8069f636 2019-01-12 stsp free(commit_id);
2473 8069f636 2019-01-12 stsp if (error)
2474 8069f636 2019-01-12 stsp goto done;
2477 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2478 f2ea84fa 2019-07-27 stsp if (error)
2479 f2ea84fa 2019-07-27 stsp goto done;
2480 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2481 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2482 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2483 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2484 c09a553d 2018-03-12 stsp if (error != NULL)
2485 c09a553d 2018-03-12 stsp goto done;
2487 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2488 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2489 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2491 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2492 8069f636 2019-01-12 stsp free(commit_id_str);
2493 76089277 2018-04-01 stsp free(repo_path);
2494 507dc3bb 2018-12-29 stsp free(worktree_path);
2495 507dc3bb 2018-12-29 stsp return error;
2498 507dc3bb 2018-12-29 stsp __dead static void
2499 507dc3bb 2018-12-29 stsp usage_update(void)
2501 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2502 507dc3bb 2018-12-29 stsp getprogname());
2506 1ee397ad 2019-07-12 stsp static const struct got_error *
2507 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2509 784955db 2019-01-12 stsp int *did_something = arg;
2511 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2512 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2513 1ee397ad 2019-07-12 stsp return NULL;
2515 1545c615 2019-02-10 stsp *did_something = 1;
2517 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2518 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2519 1ee397ad 2019-07-12 stsp return NULL;
2521 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2523 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2524 1ee397ad 2019-07-12 stsp return NULL;
2527 be7061eb 2018-12-30 stsp static const struct got_error *
2528 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2529 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2530 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2532 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2533 a1fb16d8 2019-05-24 stsp char *base_id_str;
2534 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2536 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2537 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2538 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2539 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2540 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2541 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2542 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2545 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2546 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2547 a1fb16d8 2019-05-24 stsp if (err) {
2548 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2549 a1fb16d8 2019-05-24 stsp return err;
2550 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2552 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2553 a1fb16d8 2019-05-24 stsp return NULL;
2555 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2556 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2557 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2559 a1fb16d8 2019-05-24 stsp return err;
2560 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2561 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2562 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2563 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2564 0ebf8283 2019-07-24 stsp return NULL;
2567 0ebf8283 2019-07-24 stsp static const struct got_error *
2568 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2570 0ebf8283 2019-07-24 stsp const struct got_error *err;
2571 0ebf8283 2019-07-24 stsp int in_progress;
2573 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2575 0ebf8283 2019-07-24 stsp return err;
2576 0ebf8283 2019-07-24 stsp if (in_progress)
2577 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2579 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2581 0ebf8283 2019-07-24 stsp return err;
2582 0ebf8283 2019-07-24 stsp if (in_progress)
2583 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2585 a1fb16d8 2019-05-24 stsp return NULL;
2588 a5edda0a 2019-07-27 stsp static const struct got_error *
2589 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2590 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2592 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2593 a5edda0a 2019-07-27 stsp char *path;
2596 a5edda0a 2019-07-27 stsp if (argc == 0) {
2597 a5edda0a 2019-07-27 stsp path = strdup("");
2598 a5edda0a 2019-07-27 stsp if (path == NULL)
2599 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2600 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2603 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2604 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2607 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2608 a5edda0a 2019-07-27 stsp if (err) {
2609 a5edda0a 2019-07-27 stsp free(path);
2614 a5edda0a 2019-07-27 stsp return err;
2617 a1fb16d8 2019-05-24 stsp static const struct got_error *
2618 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2619 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2621 fa51e947 2020-03-27 stsp const struct got_error *err;
2622 fa51e947 2020-03-27 stsp struct got_repository *repo;
2623 fa51e947 2020-03-27 stsp static char msg[512];
2625 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2627 fa51e947 2020-03-27 stsp return orig_err;
2629 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2630 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2631 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2632 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2633 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2634 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2635 fa51e947 2020-03-27 stsp got_repo_close(repo);
2636 fa51e947 2020-03-27 stsp return err;
2639 fa51e947 2020-03-27 stsp static const struct got_error *
2640 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2642 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2643 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2644 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2645 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2646 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2647 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2648 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2649 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2650 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2651 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2652 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2654 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2656 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2657 507dc3bb 2018-12-29 stsp switch (ch) {
2659 024e9686 2019-05-14 stsp branch_name = optarg;
2662 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2663 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2664 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2667 2deda0b9 2019-03-07 stsp usage_update();
2668 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2672 507dc3bb 2018-12-29 stsp argc -= optind;
2673 507dc3bb 2018-12-29 stsp argv += optind;
2675 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2676 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2677 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2678 507dc3bb 2018-12-29 stsp err(1, "pledge");
2680 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2681 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2682 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2683 c4cdcb68 2019-04-03 stsp goto done;
2685 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2686 fa51e947 2020-03-27 stsp if (error) {
2687 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2688 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2689 fa51e947 2020-03-27 stsp worktree_path);
2690 7d5807f4 2019-07-11 stsp goto done;
2693 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2694 f2ea84fa 2019-07-27 stsp if (error)
2695 f2ea84fa 2019-07-27 stsp goto done;
2697 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2699 507dc3bb 2018-12-29 stsp if (error != NULL)
2700 507dc3bb 2018-12-29 stsp goto done;
2702 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2703 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2704 087fb88c 2019-08-04 stsp if (error)
2705 087fb88c 2019-08-04 stsp goto done;
2707 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2708 0266afb7 2019-01-04 stsp if (error)
2709 0266afb7 2019-01-04 stsp goto done;
2711 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2712 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2713 024e9686 2019-05-14 stsp if (error != NULL)
2714 024e9686 2019-05-14 stsp goto done;
2715 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2716 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2717 9c4b8182 2019-01-02 stsp if (error != NULL)
2718 9c4b8182 2019-01-02 stsp goto done;
2719 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2720 507dc3bb 2018-12-29 stsp if (error != NULL)
2721 507dc3bb 2018-12-29 stsp goto done;
2723 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2724 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2725 04f57cb3 2019-07-25 stsp free(commit_id_str);
2726 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2727 30837e32 2019-07-25 stsp if (error)
2728 a297e751 2019-07-11 stsp goto done;
2729 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2730 a297e751 2019-07-11 stsp if (error)
2731 507dc3bb 2018-12-29 stsp goto done;
2734 a1fb16d8 2019-05-24 stsp if (branch_name) {
2735 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2736 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2737 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2739 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2740 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2741 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2742 024e9686 2019-05-14 stsp goto done;
2744 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2745 024e9686 2019-05-14 stsp if (error)
2746 024e9686 2019-05-14 stsp goto done;
2747 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2749 a367ff0f 2019-05-14 stsp free(head_commit_id);
2750 024e9686 2019-05-14 stsp if (error != NULL)
2751 024e9686 2019-05-14 stsp goto done;
2752 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2753 a367ff0f 2019-05-14 stsp if (error)
2754 a367ff0f 2019-05-14 stsp goto done;
2755 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2756 024e9686 2019-05-14 stsp if (error)
2757 024e9686 2019-05-14 stsp goto done;
2759 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2760 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2761 a367ff0f 2019-05-14 stsp if (error != NULL) {
2762 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2763 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2764 024e9686 2019-05-14 stsp goto done;
2766 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2767 a367ff0f 2019-05-14 stsp if (error)
2768 a367ff0f 2019-05-14 stsp goto done;
2771 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2772 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2773 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2774 507dc3bb 2018-12-29 stsp commit_id);
2775 507dc3bb 2018-12-29 stsp if (error)
2776 507dc3bb 2018-12-29 stsp goto done;
2779 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2780 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2781 507dc3bb 2018-12-29 stsp if (error != NULL)
2782 507dc3bb 2018-12-29 stsp goto done;
2784 784955db 2019-01-12 stsp if (did_something)
2785 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2787 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2789 c09a553d 2018-03-12 stsp free(worktree_path);
2790 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2791 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2792 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2793 507dc3bb 2018-12-29 stsp free(commit_id);
2794 9c4b8182 2019-01-02 stsp free(commit_id_str);
2795 c09a553d 2018-03-12 stsp return error;
2798 f42b1b34 2018-03-12 stsp static const struct got_error *
2799 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2800 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2801 63035f9f 2019-10-06 stsp struct got_repository *repo)
2803 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2804 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2806 44392932 2019-08-25 stsp if (blob_id1) {
2807 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2809 44392932 2019-08-25 stsp goto done;
2812 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2814 44392932 2019-08-25 stsp goto done;
2816 44392932 2019-08-25 stsp while (path[0] == '/')
2818 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2819 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2821 44392932 2019-08-25 stsp if (blob1)
2822 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2823 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2824 44392932 2019-08-25 stsp return err;
2827 44392932 2019-08-25 stsp static const struct got_error *
2828 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2829 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2830 63035f9f 2019-10-06 stsp struct got_repository *repo)
2832 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2833 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2834 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2836 44392932 2019-08-25 stsp if (tree_id1) {
2837 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2839 44392932 2019-08-25 stsp goto done;
2842 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2844 0f2b3dca 2018-12-22 stsp goto done;
2846 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2847 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2848 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2849 44392932 2019-08-25 stsp while (path[0] == '/')
2851 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2852 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2854 79109fed 2018-03-27 stsp if (tree1)
2855 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2856 366e0a5f 2019-10-10 stsp if (tree2)
2857 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2858 44392932 2019-08-25 stsp return err;
2861 44392932 2019-08-25 stsp static const struct got_error *
2862 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2863 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2865 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2866 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2867 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2868 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2869 44392932 2019-08-25 stsp struct got_object_qid *qid;
2871 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2872 44392932 2019-08-25 stsp if (qid != NULL) {
2873 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2876 44392932 2019-08-25 stsp return err;
2879 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2880 44392932 2019-08-25 stsp int obj_type;
2881 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2883 44392932 2019-08-25 stsp goto done;
2884 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2885 44392932 2019-08-25 stsp if (err) {
2886 44392932 2019-08-25 stsp free(obj_id2);
2887 44392932 2019-08-25 stsp goto done;
2889 44392932 2019-08-25 stsp if (pcommit) {
2890 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2891 44392932 2019-08-25 stsp qid->id, path);
2892 44392932 2019-08-25 stsp if (err) {
2893 44392932 2019-08-25 stsp free(obj_id2);
2894 44392932 2019-08-25 stsp goto done;
2896 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2897 44392932 2019-08-25 stsp if (err) {
2898 44392932 2019-08-25 stsp free(obj_id2);
2899 44392932 2019-08-25 stsp goto done;
2902 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2903 44392932 2019-08-25 stsp if (err) {
2904 44392932 2019-08-25 stsp free(obj_id2);
2905 44392932 2019-08-25 stsp goto done;
2907 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2908 44392932 2019-08-25 stsp switch (obj_type) {
2909 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2910 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2913 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2914 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2918 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2921 44392932 2019-08-25 stsp free(obj_id1);
2922 44392932 2019-08-25 stsp free(obj_id2);
2924 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2925 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2927 44392932 2019-08-25 stsp goto done;
2928 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2929 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2931 44392932 2019-08-25 stsp goto done;
2932 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2933 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2936 0f2b3dca 2018-12-22 stsp free(id_str1);
2937 0f2b3dca 2018-12-22 stsp free(id_str2);
2938 44392932 2019-08-25 stsp if (pcommit)
2939 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2940 79109fed 2018-03-27 stsp return err;
2943 4bb494d5 2018-06-16 stsp static char *
2944 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2946 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2947 09867e48 2019-08-13 stsp char *p, *s;
2949 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2950 09867e48 2019-08-13 stsp if (tm == NULL)
2951 09867e48 2019-08-13 stsp return NULL;
2952 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2953 09867e48 2019-08-13 stsp if (s == NULL)
2954 09867e48 2019-08-13 stsp return NULL;
2955 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2957 6c281f94 2018-06-11 stsp *p = '\0';
2961 6841bf13 2019-11-29 kn static const struct got_error *
2962 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2963 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2965 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2966 6841bf13 2019-11-29 kn regmatch_t regmatch;
2967 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2969 6841bf13 2019-11-29 kn *have_match = 0;
2971 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2975 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2979 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2980 6841bf13 2019-11-29 kn *have_match = 1;
2982 6841bf13 2019-11-29 kn free(id_str);
2983 6841bf13 2019-11-29 kn free(logmsg);
2987 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2989 79109fed 2018-03-27 stsp static const struct got_error *
2990 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2991 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2992 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2994 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2995 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2996 6c281f94 2018-06-11 stsp char datebuf[26];
2997 45d799e2 2018-12-23 stsp time_t committer_time;
2998 45d799e2 2018-12-23 stsp const char *author, *committer;
2999 199a4027 2019-02-02 stsp char *refs_str = NULL;
3000 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3002 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3004 199a4027 2019-02-02 stsp const char *name;
3005 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3008 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3009 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3011 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3012 199a4027 2019-02-02 stsp name += 5;
3013 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3015 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3016 e34f9ed6 2019-02-02 stsp name += 6;
3017 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
3018 141c2bff 2019-02-04 stsp name += 8;
3019 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3020 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3021 5d844a1e 2019-08-13 stsp if (err) {
3022 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3023 5d844a1e 2019-08-13 stsp return err;
3024 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3025 5d844a1e 2019-08-13 stsp err = NULL;
3026 5d844a1e 2019-08-13 stsp tag = NULL;
3029 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3030 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3032 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3033 a436ad14 2019-08-13 stsp if (cmp != 0)
3035 199a4027 2019-02-02 stsp s = refs_str;
3036 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3037 199a4027 2019-02-02 stsp name) == -1) {
3038 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3040 34ca4898 2019-08-13 stsp return err;
3044 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3046 8bf5b3c9 2018-03-17 stsp return err;
3048 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3049 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3050 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3051 832c249c 2018-06-10 stsp free(id_str);
3052 d3d493d7 2019-02-21 stsp id_str = NULL;
3053 d3d493d7 2019-02-21 stsp free(refs_str);
3054 d3d493d7 2019-02-21 stsp refs_str = NULL;
3055 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3056 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3057 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3058 09867e48 2019-08-13 stsp if (datestr)
3059 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3060 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3061 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3062 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3063 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3064 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3065 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3066 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3067 3fe1abad 2018-06-10 stsp int n = 1;
3068 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3069 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3070 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3072 a0603db2 2018-06-10 stsp return err;
3073 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3074 a0603db2 2018-06-10 stsp free(id_str);
3078 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3080 5943eee2 2019-08-13 stsp return err;
3082 621015ac 2018-07-23 stsp logmsg = logmsg0;
3084 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3086 832c249c 2018-06-10 stsp printf(" %s\n", line);
3087 832c249c 2018-06-10 stsp } while (line);
3088 621015ac 2018-07-23 stsp free(logmsg0);
3090 971751ac 2018-03-27 stsp if (show_patch) {
3091 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3092 971751ac 2018-03-27 stsp if (err == 0)
3093 971751ac 2018-03-27 stsp printf("\n");
3096 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3097 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3098 07862c20 2018-09-15 stsp return err;
3101 f42b1b34 2018-03-12 stsp static const struct got_error *
3102 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3103 d1fe46f9 2020-04-18 stsp struct got_repository *repo, const char *path, int show_patch,
3104 d1fe46f9 2020-04-18 stsp const char *search_pattern, int diff_context, int limit, int log_branches,
3105 463a997a 2019-12-08 kn struct got_reflist_head *refs)
3107 f42b1b34 2018-03-12 stsp const struct got_error *err;
3108 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3109 6841bf13 2019-11-29 kn regex_t regex;
3110 6841bf13 2019-11-29 kn int have_match;
3112 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3113 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3114 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3116 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3118 f42b1b34 2018-03-12 stsp return err;
3119 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3120 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3122 fcc85cad 2018-07-22 stsp goto done;
3124 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
3125 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3127 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3130 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3131 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3132 372ccdbb 2018-06-10 stsp if (err) {
3133 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3134 9ba79e04 2018-06-11 stsp err = NULL;
3137 b43fbaa0 2018-06-11 stsp if (id == NULL)
3140 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3144 6841bf13 2019-11-29 kn if (search_pattern) {
3145 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3147 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3150 6841bf13 2019-11-29 kn if (have_match == 0) {
3151 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3156 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
3157 44392932 2019-08-25 stsp diff_context, refs);
3158 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
3159 d1fe46f9 2020-04-18 stsp if (err || (limit && --limit == 0) ||
3160 d1fe46f9 2020-04-18 stsp (end_id != NULL && got_object_id_cmp(id, end_id) == 0))
3164 6841bf13 2019-11-29 kn if (search_pattern)
3165 6841bf13 2019-11-29 kn regfree(®ex);
3166 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3167 f42b1b34 2018-03-12 stsp return err;
3170 4ed7e80c 2018-05-20 stsp __dead static void
3171 6f3d1eb0 2018-03-12 stsp usage_log(void)
3173 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3174 d1fe46f9 2020-04-18 stsp "[-p] [-x commit] [-s search-pattern] [-r repository-path] "
3175 d1fe46f9 2020-04-18 stsp "[path]\n", getprogname());
3179 b1ebc001 2019-08-13 stsp static int
3180 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3182 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3183 b1ebc001 2019-08-13 stsp long long n;
3184 b1ebc001 2019-08-13 stsp const char *errstr;
3186 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3187 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3189 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3190 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3195 d1fe46f9 2020-04-18 stsp static const struct got_error *
3196 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3197 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3199 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3200 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3202 d1fe46f9 2020-04-18 stsp *id = NULL;
3204 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3205 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3206 d1fe46f9 2020-04-18 stsp int obj_type;
3207 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3208 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3210 d1fe46f9 2020-04-18 stsp return err;
3211 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3213 d1fe46f9 2020-04-18 stsp return err;
3214 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3215 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3216 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3218 d1fe46f9 2020-04-18 stsp return err;
3219 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3220 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3221 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3222 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3224 d1fe46f9 2020-04-18 stsp free(*id);
3225 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3226 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3227 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3228 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3229 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3230 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3232 d1fe46f9 2020-04-18 stsp return err;
3233 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3234 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3236 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3237 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3240 d1fe46f9 2020-04-18 stsp return err;
3243 4ed7e80c 2018-05-20 stsp static const struct got_error *
3244 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3246 f42b1b34 2018-03-12 stsp const struct got_error *error;
3247 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3248 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3249 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3250 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3251 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3252 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3253 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3254 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3255 64a96a6d 2018-04-01 stsp const char *errstr;
3256 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3258 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3260 6715a751 2018-03-16 stsp #ifndef PROFILE
3261 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3264 f42b1b34 2018-03-12 stsp err(1, "pledge");
3267 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3269 d1fe46f9 2020-04-18 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:x:")) != -1) {
3270 79109fed 2018-03-27 stsp switch (ch) {
3272 79109fed 2018-03-27 stsp show_patch = 1;
3275 d142fc45 2018-04-01 stsp start_commit = optarg;
3278 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3280 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3281 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3284 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3285 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3286 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3289 48c8c60d 2020-01-27 stsp log_branches = 1;
3292 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3293 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3294 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3296 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3299 6841bf13 2019-11-29 kn search_pattern = optarg;
3302 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3305 2deda0b9 2019-03-07 stsp usage_log();
3306 79109fed 2018-03-27 stsp /* NOTREACHED */
3310 79109fed 2018-03-27 stsp argc -= optind;
3311 79109fed 2018-03-27 stsp argv += optind;
3313 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3314 dc1edbfa 2019-11-29 kn diff_context = 3;
3315 dc1edbfa 2019-11-29 kn else if (!show_patch)
3316 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3318 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3319 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3320 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3321 04ca23f4 2018-07-16 stsp goto done;
3324 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3325 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3326 cffc0aa4 2019-02-05 stsp goto done;
3327 cffc0aa4 2019-02-05 stsp error = NULL;
3329 e7301579 2019-03-18 stsp if (argc == 0) {
3330 cbd1af7a 2019-03-18 stsp path = strdup("");
3331 e7301579 2019-03-18 stsp if (path == NULL) {
3332 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3333 cbd1af7a 2019-03-18 stsp goto done;
3335 e7301579 2019-03-18 stsp } else if (argc == 1) {
3336 e7301579 2019-03-18 stsp if (worktree) {
3337 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3339 e7301579 2019-03-18 stsp if (error)
3340 e7301579 2019-03-18 stsp goto done;
3342 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3343 e7301579 2019-03-18 stsp if (path == NULL) {
3344 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3345 e7301579 2019-03-18 stsp goto done;
3349 cbd1af7a 2019-03-18 stsp usage_log();
3351 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3352 5486daa2 2019-05-11 stsp repo_path = worktree ?
3353 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3355 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3356 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3357 cffc0aa4 2019-02-05 stsp goto done;
3360 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3361 d7d4f210 2018-03-12 stsp if (error != NULL)
3362 04ca23f4 2018-07-16 stsp goto done;
3364 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3365 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3366 c02c541e 2019-03-29 stsp if (error)
3367 c02c541e 2019-03-29 stsp goto done;
3369 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3370 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3371 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3372 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3373 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3374 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3375 3235492e 2018-04-01 stsp if (error != NULL)
3376 d1fe46f9 2020-04-18 stsp goto done;
3377 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3378 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3379 3235492e 2018-04-01 stsp if (error != NULL)
3380 d1fe46f9 2020-04-18 stsp goto done;
3381 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3382 d1fe46f9 2020-04-18 stsp start_id);
3383 d1fe46f9 2020-04-18 stsp if (error != NULL)
3384 d1fe46f9 2020-04-18 stsp goto done;
3385 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3387 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3388 d1fe46f9 2020-04-18 stsp if (error != NULL)
3389 d1fe46f9 2020-04-18 stsp goto done;
3391 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3392 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3393 d1fe46f9 2020-04-18 stsp if (error != NULL)
3394 d1fe46f9 2020-04-18 stsp goto done;
3397 deeabeae 2019-08-27 stsp if (worktree) {
3398 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3400 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3401 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3402 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3403 deeabeae 2019-08-27 stsp goto done;
3405 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3408 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3409 04ca23f4 2018-07-16 stsp if (error != NULL)
3410 04ca23f4 2018-07-16 stsp goto done;
3411 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3412 04ca23f4 2018-07-16 stsp free(path);
3413 04ca23f4 2018-07-16 stsp path = in_repo_path;
3416 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3417 199a4027 2019-02-02 stsp if (error)
3418 199a4027 2019-02-02 stsp goto done;
3420 d1fe46f9 2020-04-18 stsp error = print_commits(start_id, end_id, repo, path, show_patch,
3421 d1fe46f9 2020-04-18 stsp search_pattern, diff_context, limit, log_branches, &refs);
3423 04ca23f4 2018-07-16 stsp free(path);
3424 04ca23f4 2018-07-16 stsp free(repo_path);
3425 04ca23f4 2018-07-16 stsp free(cwd);
3426 cffc0aa4 2019-02-05 stsp if (worktree)
3427 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3428 ad242220 2018-09-08 stsp if (repo) {
3429 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3430 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3431 ad242220 2018-09-08 stsp if (error == NULL)
3432 ad242220 2018-09-08 stsp error = repo_error;
3434 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3435 8bf5b3c9 2018-03-17 stsp return error;
3438 4ed7e80c 2018-05-20 stsp __dead static void
3439 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3441 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3442 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3446 b72f483a 2019-02-05 stsp struct print_diff_arg {
3447 b72f483a 2019-02-05 stsp struct got_repository *repo;
3448 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3449 b72f483a 2019-02-05 stsp int diff_context;
3450 3fc0c068 2019-02-10 stsp const char *id_str;
3451 3fc0c068 2019-02-10 stsp int header_shown;
3452 98eaaa12 2019-08-03 stsp int diff_staged;
3453 63035f9f 2019-10-06 stsp int ignore_whitespace;
3456 4ed7e80c 2018-05-20 stsp static const struct got_error *
3457 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3458 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3459 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3460 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3462 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3463 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3464 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3465 12463d8b 2019-12-13 stsp int fd = -1;
3466 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3467 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3468 b72f483a 2019-02-05 stsp struct stat sb;
3470 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3471 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3472 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3473 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3474 98eaaa12 2019-08-03 stsp return NULL;
3476 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3477 98eaaa12 2019-08-03 stsp return NULL;
3478 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3479 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3480 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3481 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3482 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3483 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3484 98eaaa12 2019-08-03 stsp return NULL;
3487 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3488 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3489 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3490 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3491 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3494 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3495 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3496 98eaaa12 2019-08-03 stsp switch (staged_status) {
3497 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3498 98eaaa12 2019-08-03 stsp label1 = path;
3499 98eaaa12 2019-08-03 stsp label2 = path;
3501 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3502 98eaaa12 2019-08-03 stsp label2 = path;
3504 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3505 98eaaa12 2019-08-03 stsp label1 = path;
3508 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3510 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3511 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3512 63035f9f 2019-10-06 stsp a->repo, stdout);
3515 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3516 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3517 4ce46740 2019-08-08 stsp char *id_str;
3518 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3521 4ce46740 2019-08-08 stsp goto done;
3522 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3524 4ce46740 2019-08-08 stsp goto done;
3525 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3526 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3527 4ce46740 2019-08-08 stsp free(id_str);
3528 4ce46740 2019-08-08 stsp goto done;
3530 4ce46740 2019-08-08 stsp free(id_str);
3531 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3532 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3534 4ce46740 2019-08-08 stsp goto done;
3537 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3538 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3539 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3540 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3541 2ec1f75b 2019-03-26 stsp goto done;
3544 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3545 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3546 12463d8b 2019-12-13 stsp if (fd == -1) {
3547 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3548 12463d8b 2019-12-13 stsp goto done;
3551 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3552 12463d8b 2019-12-13 stsp if (fd == -1) {
3553 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3554 12463d8b 2019-12-13 stsp goto done;
3557 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3558 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3559 2ec1f75b 2019-03-26 stsp goto done;
3561 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3562 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3563 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3564 2ec1f75b 2019-03-26 stsp goto done;
3568 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3570 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3571 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3573 b72f483a 2019-02-05 stsp if (blob1)
3574 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3575 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3576 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3577 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3578 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3579 b72f483a 2019-02-05 stsp free(abspath);
3580 927df6b7 2019-02-10 stsp return err;
3583 927df6b7 2019-02-10 stsp static const struct got_error *
3584 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3586 b00d56cd 2018-04-01 stsp const struct got_error *error;
3587 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3588 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3589 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3590 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3591 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3592 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3593 15a94983 2018-12-23 stsp int type1, type2;
3594 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3595 c0cc5c62 2018-10-18 stsp const char *errstr;
3596 927df6b7 2019-02-10 stsp char *path = NULL;
3598 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3599 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3600 25eccc22 2019-01-04 stsp NULL) == -1)
3601 b00d56cd 2018-04-01 stsp err(1, "pledge");
3604 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3605 b00d56cd 2018-04-01 stsp switch (ch) {
3607 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3609 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3610 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3613 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3614 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3615 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3617 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3620 98eaaa12 2019-08-03 stsp diff_staged = 1;
3623 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3626 2deda0b9 2019-03-07 stsp usage_diff();
3627 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3631 b00d56cd 2018-04-01 stsp argc -= optind;
3632 b00d56cd 2018-04-01 stsp argv += optind;
3634 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3635 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3636 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3637 b72f483a 2019-02-05 stsp goto done;
3639 927df6b7 2019-02-10 stsp if (argc <= 1) {
3640 b72f483a 2019-02-05 stsp if (repo_path)
3642 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3643 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3644 fa51e947 2020-03-27 stsp if (error) {
3645 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3646 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
3648 1f03b8da 2020-03-20 stsp goto done;
3650 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3651 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3652 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3653 927df6b7 2019-02-10 stsp goto done;
3655 927df6b7 2019-02-10 stsp if (argc == 1) {
3656 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3658 927df6b7 2019-02-10 stsp if (error)
3659 927df6b7 2019-02-10 stsp goto done;
3661 927df6b7 2019-02-10 stsp path = strdup("");
3662 927df6b7 2019-02-10 stsp if (path == NULL) {
3663 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3664 927df6b7 2019-02-10 stsp goto done;
3667 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3668 98eaaa12 2019-08-03 stsp if (diff_staged)
3669 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3670 98eaaa12 2019-08-03 stsp "objects in repository");
3671 15a94983 2018-12-23 stsp id_str1 = argv[0];
3672 15a94983 2018-12-23 stsp id_str2 = argv[1];
3673 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3674 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3675 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3676 30db809c 2019-06-05 stsp goto done;
3677 1f03b8da 2020-03-20 stsp if (worktree) {
3678 1f03b8da 2020-03-20 stsp repo_path = strdup(
3679 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3680 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3681 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3682 1f03b8da 2020-03-20 stsp goto done;
3685 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3686 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3687 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3688 1f03b8da 2020-03-20 stsp goto done;
3693 b00d56cd 2018-04-01 stsp usage_diff();
3695 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3696 76089277 2018-04-01 stsp free(repo_path);
3697 b00d56cd 2018-04-01 stsp if (error != NULL)
3698 b00d56cd 2018-04-01 stsp goto done;
3700 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3701 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3702 c02c541e 2019-03-29 stsp if (error)
3703 c02c541e 2019-03-29 stsp goto done;
3705 30db809c 2019-06-05 stsp if (argc <= 1) {
3706 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3707 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3708 b72f483a 2019-02-05 stsp char *id_str;
3710 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3712 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3713 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3714 b72f483a 2019-02-05 stsp if (error)
3715 b72f483a 2019-02-05 stsp goto done;
3716 b72f483a 2019-02-05 stsp arg.repo = repo;
3717 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3718 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3719 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3720 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3721 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3722 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3724 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3725 72ea6654 2019-07-27 stsp if (error)
3726 72ea6654 2019-07-27 stsp goto done;
3728 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3729 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3730 b72f483a 2019-02-05 stsp free(id_str);
3731 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3732 b72f483a 2019-02-05 stsp goto done;
3735 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3736 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3737 0adb7196 2019-08-11 stsp if (error)
3738 0adb7196 2019-08-11 stsp goto done;
3740 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3741 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3742 0adb7196 2019-08-11 stsp if (error)
3743 0adb7196 2019-08-11 stsp goto done;
3745 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3746 15a94983 2018-12-23 stsp if (error)
3747 15a94983 2018-12-23 stsp goto done;
3749 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3750 15a94983 2018-12-23 stsp if (error)
3751 15a94983 2018-12-23 stsp goto done;
3753 15a94983 2018-12-23 stsp if (type1 != type2) {
3754 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3755 b00d56cd 2018-04-01 stsp goto done;
3758 15a94983 2018-12-23 stsp switch (type1) {
3759 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3760 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3761 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3763 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3764 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3765 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3767 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3768 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3769 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3770 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3773 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3776 5e70831e 2019-05-28 stsp free(label1);
3777 5e70831e 2019-05-28 stsp free(label2);
3778 15a94983 2018-12-23 stsp free(id1);
3779 15a94983 2018-12-23 stsp free(id2);
3780 927df6b7 2019-02-10 stsp free(path);
3781 b72f483a 2019-02-05 stsp if (worktree)
3782 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3783 ad242220 2018-09-08 stsp if (repo) {
3784 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3785 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3786 ad242220 2018-09-08 stsp if (error == NULL)
3787 ad242220 2018-09-08 stsp error = repo_error;
3789 b00d56cd 2018-04-01 stsp return error;
3792 404c43c4 2018-06-21 stsp __dead static void
3793 404c43c4 2018-06-21 stsp usage_blame(void)
3795 9270e621 2019-02-05 stsp fprintf(stderr,
3796 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3797 404c43c4 2018-06-21 stsp getprogname());
3801 28315671 2019-08-14 stsp struct blame_line {
3802 28315671 2019-08-14 stsp int annotated;
3803 28315671 2019-08-14 stsp char *id_str;
3804 82f6abb8 2019-08-14 stsp char *committer;
3805 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3808 28315671 2019-08-14 stsp struct blame_cb_args {
3809 28315671 2019-08-14 stsp struct blame_line *lines;
3810 28315671 2019-08-14 stsp int nlines;
3811 7ef28ff8 2019-08-14 stsp int nlines_prec;
3812 28315671 2019-08-14 stsp int lineno_cur;
3813 28315671 2019-08-14 stsp off_t *line_offsets;
3815 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3818 404c43c4 2018-06-21 stsp static const struct got_error *
3819 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3821 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3822 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3823 28315671 2019-08-14 stsp struct blame_line *bline;
3824 82f6abb8 2019-08-14 stsp char *line = NULL;
3825 28315671 2019-08-14 stsp size_t linesize = 0;
3826 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3827 28315671 2019-08-14 stsp off_t offset;
3828 bcb49d15 2019-08-14 stsp struct tm tm;
3829 bcb49d15 2019-08-14 stsp time_t committer_time;
3831 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3832 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3833 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3835 28315671 2019-08-14 stsp if (sigint_received)
3836 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3838 2839f8b9 2019-08-18 stsp if (lineno == -1)
3839 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3841 28315671 2019-08-14 stsp /* Annotate this line. */
3842 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3843 28315671 2019-08-14 stsp if (bline->annotated)
3844 28315671 2019-08-14 stsp return NULL;
3845 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3847 28315671 2019-08-14 stsp return err;
3849 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3851 82f6abb8 2019-08-14 stsp goto done;
3853 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3854 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3855 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3856 bcb49d15 2019-08-14 stsp goto done;
3859 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3860 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3861 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3862 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3863 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3864 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3865 82f6abb8 2019-08-14 stsp goto done;
3867 28315671 2019-08-14 stsp bline->annotated = 1;
3869 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3870 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3871 28315671 2019-08-14 stsp if (!bline->annotated)
3872 82f6abb8 2019-08-14 stsp goto done;
3874 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3875 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3876 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3877 82f6abb8 2019-08-14 stsp goto done;
3880 28315671 2019-08-14 stsp while (bline->annotated) {
3881 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3882 82f6abb8 2019-08-14 stsp size_t len;
3884 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3885 28315671 2019-08-14 stsp if (ferror(a->f))
3886 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3890 82f6abb8 2019-08-14 stsp committer = bline->committer;
3891 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3892 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3893 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3894 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3896 82f6abb8 2019-08-14 stsp *at = '\0';
3897 82f6abb8 2019-08-14 stsp len = strlen(committer);
3898 82f6abb8 2019-08-14 stsp if (len >= 9)
3899 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3901 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3903 28315671 2019-08-14 stsp *nl = '\0';
3904 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3905 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3907 28315671 2019-08-14 stsp a->lineno_cur++;
3908 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3911 82f6abb8 2019-08-14 stsp if (commit)
3912 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3913 28315671 2019-08-14 stsp free(line);
3914 28315671 2019-08-14 stsp return err;
3917 28315671 2019-08-14 stsp static const struct got_error *
3918 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3920 404c43c4 2018-06-21 stsp const struct got_error *error;
3921 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3922 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3923 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3924 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3925 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3926 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3927 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3928 28315671 2019-08-14 stsp struct blame_cb_args bca;
3929 28315671 2019-08-14 stsp int ch, obj_type, i;
3930 b02560ec 2019-08-19 stsp size_t filesize;
3932 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3934 404c43c4 2018-06-21 stsp #ifndef PROFILE
3935 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3936 36e2fb66 2019-01-04 stsp NULL) == -1)
3937 404c43c4 2018-06-21 stsp err(1, "pledge");
3940 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3941 404c43c4 2018-06-21 stsp switch (ch) {
3943 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3946 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3947 66bea077 2018-08-02 stsp if (repo_path == NULL)
3948 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3950 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3953 2deda0b9 2019-03-07 stsp usage_blame();
3954 404c43c4 2018-06-21 stsp /* NOTREACHED */
3958 404c43c4 2018-06-21 stsp argc -= optind;
3959 404c43c4 2018-06-21 stsp argv += optind;
3961 a39318fd 2018-08-02 stsp if (argc == 1)
3962 404c43c4 2018-06-21 stsp path = argv[0];
3964 404c43c4 2018-06-21 stsp usage_blame();
3966 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3967 66bea077 2018-08-02 stsp if (cwd == NULL) {
3968 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3969 66bea077 2018-08-02 stsp goto done;
3971 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3972 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3973 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3974 66bea077 2018-08-02 stsp goto done;
3976 0c06baac 2019-02-05 stsp error = NULL;
3977 0c06baac 2019-02-05 stsp if (worktree) {
3978 0c06baac 2019-02-05 stsp repo_path =
3979 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3980 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3981 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3982 1f03b8da 2020-03-20 stsp if (error)
3983 1f03b8da 2020-03-20 stsp goto done;
3986 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3987 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3988 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3989 0c06baac 2019-02-05 stsp goto done;
3994 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3995 c02c541e 2019-03-29 stsp if (error != NULL)
3996 36e2fb66 2019-01-04 stsp goto done;
3998 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3999 c02c541e 2019-03-29 stsp if (error)
4000 404c43c4 2018-06-21 stsp goto done;
4002 0c06baac 2019-02-05 stsp if (worktree) {
4003 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4004 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4005 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4006 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4007 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4008 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4009 6efaaa2d 2019-02-05 stsp path) == -1) {
4010 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4011 0c06baac 2019-02-05 stsp goto done;
4013 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4016 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4018 0c06baac 2019-02-05 stsp if (error)
4019 66bea077 2018-08-02 stsp goto done;
4021 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4022 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4023 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4024 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4025 404c43c4 2018-06-21 stsp if (error != NULL)
4026 66bea077 2018-08-02 stsp goto done;
4027 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4028 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4029 404c43c4 2018-06-21 stsp if (error != NULL)
4030 66bea077 2018-08-02 stsp goto done;
4032 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4033 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4034 30837e32 2019-07-25 stsp if (error)
4035 66bea077 2018-08-02 stsp goto done;
4038 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
4039 28315671 2019-08-14 stsp if (error)
4040 28315671 2019-08-14 stsp goto done;
4042 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4043 28315671 2019-08-14 stsp if (error)
4044 28315671 2019-08-14 stsp goto done;
4046 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4047 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4048 28315671 2019-08-14 stsp goto done;
4051 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4052 28315671 2019-08-14 stsp if (error)
4053 28315671 2019-08-14 stsp goto done;
4054 28315671 2019-08-14 stsp bca.f = got_opentemp();
4055 28315671 2019-08-14 stsp if (bca.f == NULL) {
4056 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4057 28315671 2019-08-14 stsp goto done;
4059 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4060 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4061 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4062 28315671 2019-08-14 stsp goto done;
4064 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4065 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4066 b02560ec 2019-08-19 stsp bca.nlines--;
4068 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4069 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4070 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4071 28315671 2019-08-14 stsp goto done;
4073 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4074 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4075 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4076 7ef28ff8 2019-08-14 stsp while (i > 0) {
4078 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4080 82f6abb8 2019-08-14 stsp bca.repo = repo;
4082 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
4083 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
4085 66bea077 2018-08-02 stsp free(in_repo_path);
4086 66bea077 2018-08-02 stsp free(repo_path);
4087 66bea077 2018-08-02 stsp free(cwd);
4088 404c43c4 2018-06-21 stsp free(commit_id);
4089 28315671 2019-08-14 stsp free(obj_id);
4091 28315671 2019-08-14 stsp got_object_blob_close(blob);
4092 0c06baac 2019-02-05 stsp if (worktree)
4093 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4094 ad242220 2018-09-08 stsp if (repo) {
4095 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4096 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4097 ad242220 2018-09-08 stsp if (error == NULL)
4098 ad242220 2018-09-08 stsp error = repo_error;
4100 3affba96 2019-09-22 stsp if (bca.lines) {
4101 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4102 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4103 3affba96 2019-09-22 stsp free(bline->id_str);
4104 3affba96 2019-09-22 stsp free(bline->committer);
4106 3affba96 2019-09-22 stsp free(bca.lines);
4108 28315671 2019-08-14 stsp free(bca.line_offsets);
4109 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4110 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4111 404c43c4 2018-06-21 stsp return error;
4114 5de5890b 2018-10-18 stsp __dead static void
4115 5de5890b 2018-10-18 stsp usage_tree(void)
4117 c1669e2e 2019-01-09 stsp fprintf(stderr,
4118 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
4119 5de5890b 2018-10-18 stsp getprogname());
4123 c1669e2e 2019-01-09 stsp static void
4124 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4125 c1669e2e 2019-01-09 stsp const char *root_path)
4127 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4128 848d6979 2019-08-12 stsp const char *modestr = "";
4129 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4131 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4132 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4135 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4136 63c5ca5d 2019-08-24 stsp modestr = "$";
4137 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
4138 848d6979 2019-08-12 stsp modestr = "@";
4139 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4140 848d6979 2019-08-12 stsp modestr = "/";
4141 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4142 848d6979 2019-08-12 stsp modestr = "*";
4144 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
4145 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4148 5de5890b 2018-10-18 stsp static const struct got_error *
4149 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4150 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4151 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4153 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4154 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4155 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4156 56e0773d 2019-11-28 stsp int nentries, i;
4158 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4160 5de5890b 2018-10-18 stsp goto done;
4162 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4164 5de5890b 2018-10-18 stsp goto done;
4165 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4166 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4167 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4168 5de5890b 2018-10-18 stsp char *id = NULL;
4170 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4173 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4174 5de5890b 2018-10-18 stsp if (show_ids) {
4175 5de5890b 2018-10-18 stsp char *id_str;
4176 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4177 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4179 5de5890b 2018-10-18 stsp goto done;
4180 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4181 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4182 5de5890b 2018-10-18 stsp free(id_str);
4183 5de5890b 2018-10-18 stsp goto done;
4185 5de5890b 2018-10-18 stsp free(id_str);
4187 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
4190 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4191 c1669e2e 2019-01-09 stsp char *child_path;
4192 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4193 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4194 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4195 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4196 c1669e2e 2019-01-09 stsp goto done;
4198 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4199 c1669e2e 2019-01-09 stsp root_path, repo);
4200 c1669e2e 2019-01-09 stsp free(child_path);
4202 c1669e2e 2019-01-09 stsp goto done;
4207 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4208 5de5890b 2018-10-18 stsp free(tree_id);
4209 5de5890b 2018-10-18 stsp return err;
4212 5de5890b 2018-10-18 stsp static const struct got_error *
4213 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4215 5de5890b 2018-10-18 stsp const struct got_error *error;
4216 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4217 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4218 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4219 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4220 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4221 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4222 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4225 5de5890b 2018-10-18 stsp #ifndef PROFILE
4226 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4227 0f8d269b 2019-01-04 stsp NULL) == -1)
4228 5de5890b 2018-10-18 stsp err(1, "pledge");
4231 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4232 5de5890b 2018-10-18 stsp switch (ch) {
4234 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4237 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4238 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4239 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4241 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4244 5de5890b 2018-10-18 stsp show_ids = 1;
4247 c1669e2e 2019-01-09 stsp recurse = 1;
4250 2deda0b9 2019-03-07 stsp usage_tree();
4251 5de5890b 2018-10-18 stsp /* NOTREACHED */
4255 5de5890b 2018-10-18 stsp argc -= optind;
4256 5de5890b 2018-10-18 stsp argv += optind;
4258 5de5890b 2018-10-18 stsp if (argc == 1)
4259 5de5890b 2018-10-18 stsp path = argv[0];
4260 5de5890b 2018-10-18 stsp else if (argc > 1)
4261 5de5890b 2018-10-18 stsp usage_tree();
4263 7a2c19d6 2019-02-05 stsp path = NULL;
4265 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4266 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4267 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4268 9bf7a39b 2019-02-05 stsp goto done;
4270 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4271 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4272 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4273 7a2c19d6 2019-02-05 stsp goto done;
4275 7a2c19d6 2019-02-05 stsp error = NULL;
4276 7a2c19d6 2019-02-05 stsp if (worktree) {
4277 7a2c19d6 2019-02-05 stsp repo_path =
4278 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4279 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4280 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4281 7a2c19d6 2019-02-05 stsp if (error)
4282 7a2c19d6 2019-02-05 stsp goto done;
4284 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4285 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4286 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4287 7a2c19d6 2019-02-05 stsp goto done;
4292 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4293 5de5890b 2018-10-18 stsp if (error != NULL)
4294 c02c541e 2019-03-29 stsp goto done;
4296 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4297 c02c541e 2019-03-29 stsp if (error)
4298 5de5890b 2018-10-18 stsp goto done;
4300 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4301 9bf7a39b 2019-02-05 stsp if (worktree) {
4302 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4303 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4304 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4305 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4306 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4307 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4308 9bf7a39b 2019-02-05 stsp goto done;
4310 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4312 9bf7a39b 2019-02-05 stsp if (error)
4313 9bf7a39b 2019-02-05 stsp goto done;
4315 9bf7a39b 2019-02-05 stsp path = "/";
4317 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4318 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4319 9bf7a39b 2019-02-05 stsp if (error != NULL)
4320 9bf7a39b 2019-02-05 stsp goto done;
4323 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4324 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4325 4e0a20a4 2020-03-23 tracey if (worktree)
4326 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4328 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4329 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4330 5de5890b 2018-10-18 stsp if (error != NULL)
4331 5de5890b 2018-10-18 stsp goto done;
4332 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4333 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4334 5de5890b 2018-10-18 stsp if (error != NULL)
4335 5de5890b 2018-10-18 stsp goto done;
4337 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4338 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4339 30837e32 2019-07-25 stsp if (error)
4340 5de5890b 2018-10-18 stsp goto done;
4343 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4344 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4346 5de5890b 2018-10-18 stsp free(in_repo_path);
4347 5de5890b 2018-10-18 stsp free(repo_path);
4348 5de5890b 2018-10-18 stsp free(cwd);
4349 5de5890b 2018-10-18 stsp free(commit_id);
4350 7a2c19d6 2019-02-05 stsp if (worktree)
4351 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4352 5de5890b 2018-10-18 stsp if (repo) {
4353 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4354 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4355 5de5890b 2018-10-18 stsp if (error == NULL)
4356 5de5890b 2018-10-18 stsp error = repo_error;
4358 5de5890b 2018-10-18 stsp return error;
4361 6bad629b 2019-02-04 stsp __dead static void
4362 6bad629b 2019-02-04 stsp usage_status(void)
4364 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4368 b72f483a 2019-02-05 stsp static const struct got_error *
4369 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4370 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4371 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4372 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4374 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4375 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4376 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4377 b72f483a 2019-02-05 stsp return NULL;
4380 6bad629b 2019-02-04 stsp static const struct got_error *
4381 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4383 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4384 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4385 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4386 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4387 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4388 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4391 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4393 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4394 6bad629b 2019-02-04 stsp switch (ch) {
4396 2deda0b9 2019-03-07 stsp usage_status();
4397 6bad629b 2019-02-04 stsp /* NOTREACHED */
4401 6bad629b 2019-02-04 stsp argc -= optind;
4402 6bad629b 2019-02-04 stsp argv += optind;
4404 6bad629b 2019-02-04 stsp #ifndef PROFILE
4405 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4406 6bad629b 2019-02-04 stsp NULL) == -1)
4407 6bad629b 2019-02-04 stsp err(1, "pledge");
4409 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4410 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4411 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4412 927df6b7 2019-02-10 stsp goto done;
4415 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4416 fa51e947 2020-03-27 stsp if (error) {
4417 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4418 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4419 a5edda0a 2019-07-27 stsp goto done;
4422 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4424 6bad629b 2019-02-04 stsp if (error != NULL)
4425 6bad629b 2019-02-04 stsp goto done;
4427 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4428 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4429 087fb88c 2019-08-04 stsp if (error)
4430 087fb88c 2019-08-04 stsp goto done;
4432 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4433 6bad629b 2019-02-04 stsp if (error)
4434 6bad629b 2019-02-04 stsp goto done;
4436 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4437 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4439 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4440 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4441 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4442 927df6b7 2019-02-10 stsp free(cwd);
4443 d0eebce4 2019-03-11 stsp return error;
4446 d0eebce4 2019-03-11 stsp __dead static void
4447 d0eebce4 2019-03-11 stsp usage_ref(void)
4449 d0eebce4 2019-03-11 stsp fprintf(stderr,
4450 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4451 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4452 d0eebce4 2019-03-11 stsp getprogname());
4456 d0eebce4 2019-03-11 stsp static const struct got_error *
4457 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4459 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4460 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4461 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4463 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4464 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4466 d0eebce4 2019-03-11 stsp return err;
4468 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4469 d0eebce4 2019-03-11 stsp char *refstr;
4470 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4471 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4472 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4473 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4474 d0eebce4 2019-03-11 stsp free(refstr);
4477 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4478 d0eebce4 2019-03-11 stsp return NULL;
4481 d0eebce4 2019-03-11 stsp static const struct got_error *
4482 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4484 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4485 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4487 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4489 d0eebce4 2019-03-11 stsp return err;
4491 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4492 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4493 d0eebce4 2019-03-11 stsp return err;
4496 d0eebce4 2019-03-11 stsp static const struct got_error *
4497 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4499 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4500 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4501 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4504 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4505 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4506 d1644381 2019-07-14 stsp * an unintended typo.
4508 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4509 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4511 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4513 d83d9d5c 2019-05-13 stsp if (err) {
4514 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4516 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4517 d83d9d5c 2019-05-13 stsp return err;
4518 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4520 d83d9d5c 2019-05-13 stsp return err;
4521 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4522 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4524 d83d9d5c 2019-05-13 stsp return err;
4527 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4529 d0eebce4 2019-03-11 stsp goto done;
4531 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4534 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4536 d1c1ae5f 2019-08-12 stsp return err;
4539 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4540 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4542 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4543 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4544 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4547 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4548 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4549 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4551 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4552 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4554 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4556 d1c1ae5f 2019-08-12 stsp return err;
4558 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4560 d1c1ae5f 2019-08-12 stsp goto done;
4562 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4564 d1c1ae5f 2019-08-12 stsp if (target_ref)
4565 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4567 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4568 d0eebce4 2019-03-11 stsp return err;
4571 d0eebce4 2019-03-11 stsp static const struct got_error *
4572 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4574 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4575 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4576 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4577 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4578 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4579 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4580 b2070a3f 2020-03-22 stsp char *refname = NULL;
4582 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4583 d0eebce4 2019-03-11 stsp switch (ch) {
4585 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4588 e31abbf2 2020-03-22 stsp do_delete = 1;
4591 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4592 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4593 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4595 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4598 d0eebce4 2019-03-11 stsp do_list = 1;
4601 e31abbf2 2020-03-22 stsp symref_target = optarg;
4604 d0eebce4 2019-03-11 stsp usage_ref();
4605 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4609 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4610 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4611 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4612 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4613 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4614 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4615 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4616 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4617 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4618 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4619 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4620 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4622 d0eebce4 2019-03-11 stsp argc -= optind;
4623 d0eebce4 2019-03-11 stsp argv += optind;
4625 e31abbf2 2020-03-22 stsp if (do_list) {
4626 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4627 d0eebce4 2019-03-11 stsp usage_ref();
4628 b2070a3f 2020-03-22 stsp if (argc == 1) {
4629 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4630 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4631 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4632 b2070a3f 2020-03-22 stsp goto done;
4636 e31abbf2 2020-03-22 stsp if (argc != 1)
4637 e31abbf2 2020-03-22 stsp usage_ref();
4638 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4639 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4640 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4641 b2070a3f 2020-03-22 stsp goto done;
4645 b2070a3f 2020-03-22 stsp if (refname)
4646 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4648 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4649 e0b57350 2019-03-12 stsp if (do_list) {
4650 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4651 e0b57350 2019-03-12 stsp NULL) == -1)
4652 e0b57350 2019-03-12 stsp err(1, "pledge");
4654 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4655 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4656 e0b57350 2019-03-12 stsp err(1, "pledge");
4659 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4660 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4661 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4662 d0eebce4 2019-03-11 stsp goto done;
4665 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4666 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4667 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4668 d0eebce4 2019-03-11 stsp goto done;
4670 d0eebce4 2019-03-11 stsp error = NULL;
4671 d0eebce4 2019-03-11 stsp if (worktree) {
4672 d0eebce4 2019-03-11 stsp repo_path =
4673 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4674 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4675 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4676 d0eebce4 2019-03-11 stsp if (error)
4677 d0eebce4 2019-03-11 stsp goto done;
4679 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4680 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4681 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4682 d0eebce4 2019-03-11 stsp goto done;
4687 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4688 d0eebce4 2019-03-11 stsp if (error != NULL)
4689 d0eebce4 2019-03-11 stsp goto done;
4691 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4692 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4693 c02c541e 2019-03-29 stsp if (error)
4694 c02c541e 2019-03-29 stsp goto done;
4696 d0eebce4 2019-03-11 stsp if (do_list)
4697 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4698 e31abbf2 2020-03-22 stsp else if (do_delete)
4699 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4700 e31abbf2 2020-03-22 stsp else if (symref_target)
4701 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4703 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4704 e31abbf2 2020-03-22 stsp usage_ref();
4705 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4708 b2070a3f 2020-03-22 stsp free(refname);
4710 4e759de4 2019-06-26 stsp got_repo_close(repo);
4711 4e759de4 2019-06-26 stsp if (worktree)
4712 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4713 4e759de4 2019-06-26 stsp free(cwd);
4714 4e759de4 2019-06-26 stsp free(repo_path);
4715 4e759de4 2019-06-26 stsp return error;
4718 4e759de4 2019-06-26 stsp __dead static void
4719 4e759de4 2019-06-26 stsp usage_branch(void)
4721 4e759de4 2019-06-26 stsp fprintf(stderr,
4722 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4723 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4727 4e759de4 2019-06-26 stsp static const struct got_error *
4728 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4729 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4731 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4732 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4733 4e99b47e 2019-10-04 stsp char *refstr;
4735 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4736 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4737 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4738 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4740 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4742 4e99b47e 2019-10-04 stsp return err;
4743 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4744 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4745 4e99b47e 2019-10-04 stsp marker = "* ";
4747 4e99b47e 2019-10-04 stsp marker = "~ ";
4751 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4752 4e99b47e 2019-10-04 stsp refname += 11;
4753 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4754 4e99b47e 2019-10-04 stsp refname += 18;
4756 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4757 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4758 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4760 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4761 4e99b47e 2019-10-04 stsp free(refstr);
4762 4e99b47e 2019-10-04 stsp return NULL;
4765 4e99b47e 2019-10-04 stsp static const struct got_error *
4766 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4768 ad89fa31 2019-10-04 stsp const char *refname;
4770 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4771 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4773 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4775 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4776 ad89fa31 2019-10-04 stsp refname += 11;
4777 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4778 ad89fa31 2019-10-04 stsp refname += 18;
4780 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4782 ad89fa31 2019-10-04 stsp return NULL;
4785 ad89fa31 2019-10-04 stsp static const struct got_error *
4786 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4788 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4789 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4790 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4791 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4792 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4794 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4796 4e99b47e 2019-10-04 stsp if (worktree) {
4797 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4798 4e99b47e 2019-10-04 stsp worktree);
4800 4e99b47e 2019-10-04 stsp return err;
4802 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4803 4e99b47e 2019-10-04 stsp worktree);
4805 4e99b47e 2019-10-04 stsp return err;
4807 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4808 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4809 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4811 4e99b47e 2019-10-04 stsp return err;
4812 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4813 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4817 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4818 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4820 4e759de4 2019-06-26 stsp return err;
4822 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4823 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4825 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4826 4e759de4 2019-06-26 stsp return NULL;
4829 4e759de4 2019-06-26 stsp static const struct got_error *
4830 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4831 45cd4e47 2019-08-25 stsp const char *branch_name)
4833 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4834 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4835 4e759de4 2019-06-26 stsp char *refname;
4837 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4838 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4840 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4842 4e759de4 2019-06-26 stsp goto done;
4844 45cd4e47 2019-08-25 stsp if (worktree &&
4845 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4846 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4847 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4848 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4849 45cd4e47 2019-08-25 stsp goto done;
4852 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4855 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4856 4e759de4 2019-06-26 stsp free(refname);
4857 4e759de4 2019-06-26 stsp return err;
4860 4e759de4 2019-06-26 stsp static const struct got_error *
4861 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4862 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4864 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4865 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4866 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4869 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4870 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4871 d3f84d51 2019-07-11 stsp * an unintended typo.
4873 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4874 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4876 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4877 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4878 4e759de4 2019-06-26 stsp goto done;
4881 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4882 4e759de4 2019-06-26 stsp if (err == NULL) {
4883 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4884 4e759de4 2019-06-26 stsp goto done;
4885 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4886 4e759de4 2019-06-26 stsp goto done;
4888 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4890 4e759de4 2019-06-26 stsp goto done;
4892 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4895 4e759de4 2019-06-26 stsp got_ref_close(ref);
4896 4e759de4 2019-06-26 stsp free(base_refname);
4897 4e759de4 2019-06-26 stsp free(refname);
4898 4e759de4 2019-06-26 stsp return err;
4901 4e759de4 2019-06-26 stsp static const struct got_error *
4902 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4904 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4905 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4906 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4907 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4908 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4909 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4910 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4911 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4912 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4913 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4914 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4916 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4918 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4919 4e759de4 2019-06-26 stsp switch (ch) {
4921 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4924 4e759de4 2019-06-26 stsp delref = optarg;
4927 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4928 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4929 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4931 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4934 4e759de4 2019-06-26 stsp do_list = 1;
4937 da76fce2 2020-02-24 stsp do_update = 0;
4940 4e759de4 2019-06-26 stsp usage_branch();
4941 4e759de4 2019-06-26 stsp /* NOTREACHED */
4945 4e759de4 2019-06-26 stsp if (do_list && delref)
4946 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4948 4e759de4 2019-06-26 stsp argc -= optind;
4949 4e759de4 2019-06-26 stsp argv += optind;
4951 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4952 ad89fa31 2019-10-04 stsp do_show = 1;
4954 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4955 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4957 4e759de4 2019-06-26 stsp if (do_list || delref) {
4958 4e759de4 2019-06-26 stsp if (argc > 0)
4959 4e759de4 2019-06-26 stsp usage_branch();
4960 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4961 4e759de4 2019-06-26 stsp usage_branch();
4963 4e759de4 2019-06-26 stsp #ifndef PROFILE
4964 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4965 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4966 4e759de4 2019-06-26 stsp NULL) == -1)
4967 4e759de4 2019-06-26 stsp err(1, "pledge");
4969 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4970 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4971 4e759de4 2019-06-26 stsp err(1, "pledge");
4974 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4975 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4976 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4977 4e759de4 2019-06-26 stsp goto done;
4980 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4981 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4982 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4983 4e759de4 2019-06-26 stsp goto done;
4985 4e759de4 2019-06-26 stsp error = NULL;
4986 4e759de4 2019-06-26 stsp if (worktree) {
4987 4e759de4 2019-06-26 stsp repo_path =
4988 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4989 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4990 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4991 4e759de4 2019-06-26 stsp if (error)
4992 4e759de4 2019-06-26 stsp goto done;
4994 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4995 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4996 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4997 4e759de4 2019-06-26 stsp goto done;
5002 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5003 4e759de4 2019-06-26 stsp if (error != NULL)
5004 4e759de4 2019-06-26 stsp goto done;
5006 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5007 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5008 4e759de4 2019-06-26 stsp if (error)
5009 4e759de4 2019-06-26 stsp goto done;
5011 ad89fa31 2019-10-04 stsp if (do_show)
5012 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5013 ad89fa31 2019-10-04 stsp else if (do_list)
5014 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5015 4e759de4 2019-06-26 stsp else if (delref)
5016 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5018 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5019 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5020 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5021 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5022 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5023 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5024 a74f7e83 2019-11-10 stsp if (error)
5025 a74f7e83 2019-11-10 stsp goto done;
5026 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5027 da76fce2 2020-02-24 stsp if (error)
5028 da76fce2 2020-02-24 stsp goto done;
5029 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5030 da76fce2 2020-02-24 stsp int did_something = 0;
5031 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5033 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5034 da76fce2 2020-02-24 stsp if (error)
5035 da76fce2 2020-02-24 stsp goto done;
5036 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5037 da76fce2 2020-02-24 stsp worktree);
5038 da76fce2 2020-02-24 stsp if (error)
5039 da76fce2 2020-02-24 stsp goto done;
5040 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5042 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5043 da76fce2 2020-02-24 stsp goto done;
5045 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5046 da76fce2 2020-02-24 stsp free(branch_refname);
5047 da76fce2 2020-02-24 stsp if (error)
5048 da76fce2 2020-02-24 stsp goto done;
5049 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5051 da76fce2 2020-02-24 stsp if (error)
5052 da76fce2 2020-02-24 stsp goto done;
5053 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5054 da76fce2 2020-02-24 stsp commit_id);
5055 da76fce2 2020-02-24 stsp if (error)
5056 da76fce2 2020-02-24 stsp goto done;
5057 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5058 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
5059 da76fce2 2020-02-24 stsp check_cancelled, NULL);
5060 da76fce2 2020-02-24 stsp if (error)
5061 da76fce2 2020-02-24 stsp goto done;
5062 da76fce2 2020-02-24 stsp if (did_something)
5063 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5068 da76fce2 2020-02-24 stsp got_ref_close(ref);
5070 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5071 d0eebce4 2019-03-11 stsp if (worktree)
5072 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5073 d0eebce4 2019-03-11 stsp free(cwd);
5074 d0eebce4 2019-03-11 stsp free(repo_path);
5075 da76fce2 2020-02-24 stsp free(commit_id);
5076 da76fce2 2020-02-24 stsp free(commit_id_str);
5077 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5078 da76fce2 2020-02-24 stsp free((char *)pe->path);
5079 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5080 d00136be 2019-03-26 stsp return error;
5084 d00136be 2019-03-26 stsp __dead static void
5085 8e7bd50a 2019-08-22 stsp usage_tag(void)
5087 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5088 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5089 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5094 b8bad2ba 2019-08-23 stsp static const struct got_error *
5095 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5097 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5098 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5099 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5100 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5101 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5103 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5104 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5105 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5106 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5108 b8bad2ba 2019-08-23 stsp return err;
5109 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5112 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5115 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5116 b8bad2ba 2019-08-23 stsp free(re_id);
5119 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5120 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5123 b8bad2ba 2019-08-23 stsp while (se) {
5124 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5127 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5128 b8bad2ba 2019-08-23 stsp free(se_id);
5131 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5132 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5134 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5135 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5137 b8bad2ba 2019-08-23 stsp return err;
5138 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5141 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5146 b8bad2ba 2019-08-23 stsp return err;
5150 b8bad2ba 2019-08-23 stsp static const struct got_error *
5151 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5153 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5154 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5155 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5157 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5159 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5161 8e7bd50a 2019-08-22 stsp return err;
5163 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5164 8e7bd50a 2019-08-22 stsp const char *refname;
5165 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5166 8e7bd50a 2019-08-22 stsp char datebuf[26];
5167 d4efa91b 2020-01-14 stsp const char *tagger;
5168 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5169 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5170 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5171 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5173 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5174 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5176 8e7bd50a 2019-08-22 stsp refname += 10;
5177 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5178 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5179 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5182 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5183 8e7bd50a 2019-08-22 stsp free(refstr);
5185 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5188 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5189 d4efa91b 2020-01-14 stsp if (err) {
5190 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5194 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5195 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5196 d4efa91b 2020-01-14 stsp if (err) {
5200 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5201 d4efa91b 2020-01-14 stsp tagger_time =
5202 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5203 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5209 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5210 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5211 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5212 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5216 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5217 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5218 2417344c 2019-08-23 stsp if (datestr)
5219 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5220 d4efa91b 2020-01-14 stsp if (commit)
5221 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5223 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5224 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5225 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5228 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5229 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5232 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5233 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5236 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5237 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5244 8e7bd50a 2019-08-22 stsp free(id_str);
5245 d4efa91b 2020-01-14 stsp if (commit) {
5246 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5249 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5251 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5252 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5253 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5254 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5259 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5261 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5263 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5264 8e7bd50a 2019-08-22 stsp } while (line);
5265 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5268 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5269 8e7bd50a 2019-08-22 stsp return NULL;
5272 8e7bd50a 2019-08-22 stsp static const struct got_error *
5273 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5274 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5276 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5277 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5278 f372d5cd 2019-10-21 stsp char *editor = NULL;
5279 8e7bd50a 2019-08-22 stsp int fd = -1;
5281 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5282 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5283 8e7bd50a 2019-08-22 stsp goto done;
5286 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5287 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5288 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5289 8e7bd50a 2019-08-22 stsp goto done;
5292 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5294 8e7bd50a 2019-08-22 stsp goto done;
5296 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5297 8e7bd50a 2019-08-22 stsp close(fd);
5299 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5301 8e7bd50a 2019-08-22 stsp goto done;
5302 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5304 8e7bd50a 2019-08-22 stsp free(initial_content);
5305 8e7bd50a 2019-08-22 stsp free(template);
5306 8e7bd50a 2019-08-22 stsp free(editor);
5308 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5309 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5310 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5311 8e7bd50a 2019-08-22 stsp if (err) {
5312 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5313 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5316 8e7bd50a 2019-08-22 stsp return err;
5319 8e7bd50a 2019-08-22 stsp static const struct got_error *
5320 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5321 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5323 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5324 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5325 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5326 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5327 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5328 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5329 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5332 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5333 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5334 8e7bd50a 2019-08-22 stsp * an unintended typo.
5336 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5337 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5339 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5341 8e7bd50a 2019-08-22 stsp return err;
5343 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5344 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5346 8e7bd50a 2019-08-22 stsp goto done;
5348 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5350 8e7bd50a 2019-08-22 stsp goto done;
5352 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5353 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5354 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5355 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5356 8e7bd50a 2019-08-22 stsp goto done;
5358 8e7bd50a 2019-08-22 stsp tag_name += 10;
5359 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5360 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5361 8e7bd50a 2019-08-22 stsp goto done;
5364 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5365 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5366 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5367 8e7bd50a 2019-08-22 stsp goto done;
5368 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5369 8e7bd50a 2019-08-22 stsp goto done;
5371 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5372 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5373 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5374 f372d5cd 2019-10-21 stsp if (err) {
5375 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5376 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5377 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5378 8e7bd50a 2019-08-22 stsp goto done;
5382 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5383 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5384 f372d5cd 2019-10-21 stsp if (err) {
5385 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5386 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5387 8e7bd50a 2019-08-22 stsp goto done;
5390 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5391 f372d5cd 2019-10-21 stsp if (err) {
5392 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5393 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5394 8e7bd50a 2019-08-22 stsp goto done;
5397 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5398 f372d5cd 2019-10-21 stsp if (err) {
5399 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5400 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5401 f372d5cd 2019-10-21 stsp goto done;
5404 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5405 f372d5cd 2019-10-21 stsp if (err) {
5406 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5407 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5408 f372d5cd 2019-10-21 stsp goto done;
5410 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5412 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5413 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5414 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5415 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5416 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5417 f372d5cd 2019-10-21 stsp free(tag_id_str);
5419 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5420 8e7bd50a 2019-08-22 stsp free(commit_id);
5421 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5422 8e7bd50a 2019-08-22 stsp free(refname);
5423 8e7bd50a 2019-08-22 stsp free(tagmsg);
5424 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5425 aba9c984 2019-09-08 stsp free(tagger);
5426 8e7bd50a 2019-08-22 stsp return err;
5429 8e7bd50a 2019-08-22 stsp static const struct got_error *
5430 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5432 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5433 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5434 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5435 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5436 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5437 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5438 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5440 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5441 8e7bd50a 2019-08-22 stsp switch (ch) {
5443 80106605 2020-02-24 stsp commit_id_arg = optarg;
5446 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5449 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5450 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5451 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5453 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5456 8e7bd50a 2019-08-22 stsp do_list = 1;
5459 8e7bd50a 2019-08-22 stsp usage_tag();
5460 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5464 8e7bd50a 2019-08-22 stsp argc -= optind;
5465 8e7bd50a 2019-08-22 stsp argv += optind;
5467 8e7bd50a 2019-08-22 stsp if (do_list) {
5468 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5470 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5471 8e7bd50a 2019-08-22 stsp if (tagmsg)
5472 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5473 8e7bd50a 2019-08-22 stsp if (argc > 0)
5474 8e7bd50a 2019-08-22 stsp usage_tag();
5475 80106605 2020-02-24 stsp } else if (argc != 1)
5476 8e7bd50a 2019-08-22 stsp usage_tag();
5478 a2887370 2019-08-23 stsp tag_name = argv[0];
5480 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5481 8e7bd50a 2019-08-22 stsp if (do_list) {
5482 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5483 8e7bd50a 2019-08-22 stsp NULL) == -1)
5484 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5486 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5487 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5488 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5491 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5492 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5493 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5494 8e7bd50a 2019-08-22 stsp goto done;
5497 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5498 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5499 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5500 8e7bd50a 2019-08-22 stsp goto done;
5502 8e7bd50a 2019-08-22 stsp error = NULL;
5503 8e7bd50a 2019-08-22 stsp if (worktree) {
5504 8e7bd50a 2019-08-22 stsp repo_path =
5505 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5506 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5507 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5508 8e7bd50a 2019-08-22 stsp if (error)
5509 8e7bd50a 2019-08-22 stsp goto done;
5511 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5512 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5513 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5514 8e7bd50a 2019-08-22 stsp goto done;
5519 8e7bd50a 2019-08-22 stsp if (do_list) {
5520 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5521 c9956ddf 2019-09-08 stsp if (error != NULL)
5522 c9956ddf 2019-09-08 stsp goto done;
5523 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5524 8e7bd50a 2019-08-22 stsp if (error)
5525 8e7bd50a 2019-08-22 stsp goto done;
5526 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5528 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5529 c9956ddf 2019-09-08 stsp if (error)
5530 c9956ddf 2019-09-08 stsp goto done;
5531 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5532 c9956ddf 2019-09-08 stsp if (error != NULL)
5533 c9956ddf 2019-09-08 stsp goto done;
5535 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5536 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5537 8e7bd50a 2019-08-22 stsp if (error)
5538 8e7bd50a 2019-08-22 stsp goto done;
5541 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5542 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5543 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5544 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5545 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5546 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5547 8e7bd50a 2019-08-22 stsp if (error)
5548 8e7bd50a 2019-08-22 stsp goto done;
5549 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5550 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5551 8e7bd50a 2019-08-22 stsp if (error)
5552 8e7bd50a 2019-08-22 stsp goto done;
5553 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5554 8e7bd50a 2019-08-22 stsp free(commit_id);
5555 8e7bd50a 2019-08-22 stsp if (error)
5556 8e7bd50a 2019-08-22 stsp goto done;
5559 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5560 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5564 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5565 8e7bd50a 2019-08-22 stsp if (worktree)
5566 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5567 8e7bd50a 2019-08-22 stsp free(cwd);
5568 8e7bd50a 2019-08-22 stsp free(repo_path);
5569 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5570 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5571 8e7bd50a 2019-08-22 stsp return error;
5574 8e7bd50a 2019-08-22 stsp __dead static void
5575 d00136be 2019-03-26 stsp usage_add(void)
5577 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5578 022fae89 2019-12-06 tracey getprogname());
5582 d00136be 2019-03-26 stsp static const struct got_error *
5583 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5585 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5587 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5588 4e68cba3 2019-11-23 stsp return NULL;
5591 4e68cba3 2019-11-23 stsp static const struct got_error *
5592 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5594 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5595 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5596 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5597 1dd54920 2019-05-11 stsp char *cwd = NULL;
5598 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5599 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5600 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5602 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5604 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5605 d00136be 2019-03-26 stsp switch (ch) {
5606 022fae89 2019-12-06 tracey case 'I':
5607 022fae89 2019-12-06 tracey no_ignores = 1;
5610 4e68cba3 2019-11-23 stsp can_recurse = 1;
5613 d00136be 2019-03-26 stsp usage_add();
5614 d00136be 2019-03-26 stsp /* NOTREACHED */
5618 d00136be 2019-03-26 stsp argc -= optind;
5619 d00136be 2019-03-26 stsp argv += optind;
5621 43012d58 2019-07-14 stsp #ifndef PROFILE
5622 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5623 43012d58 2019-07-14 stsp NULL) == -1)
5624 43012d58 2019-07-14 stsp err(1, "pledge");
5626 723c305c 2019-05-11 jcs if (argc < 1)
5627 d00136be 2019-03-26 stsp usage_add();
5629 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5630 d00136be 2019-03-26 stsp if (cwd == NULL) {
5631 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5632 d00136be 2019-03-26 stsp goto done;
5635 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5636 fa51e947 2020-03-27 stsp if (error) {
5637 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5638 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
5639 d00136be 2019-03-26 stsp goto done;
5642 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5644 031a5338 2019-03-26 stsp if (error != NULL)
5645 031a5338 2019-03-26 stsp goto done;
5647 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5648 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5649 d00136be 2019-03-26 stsp if (error)
5650 d00136be 2019-03-26 stsp goto done;
5652 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5653 6d022e97 2019-08-04 stsp if (error)
5654 022fae89 2019-12-06 tracey goto done;
5656 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5657 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5658 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5659 6d022e97 2019-08-04 stsp goto done;
5663 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5664 4e68cba3 2019-11-23 stsp char *ondisk_path;
5665 4e68cba3 2019-11-23 stsp struct stat sb;
5666 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5667 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5668 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5669 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5670 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5671 4e68cba3 2019-11-23 stsp goto done;
5673 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5674 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5675 4e68cba3 2019-11-23 stsp free(ondisk_path);
5678 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5679 4e68cba3 2019-11-23 stsp ondisk_path);
5680 4e68cba3 2019-11-23 stsp free(ondisk_path);
5681 4e68cba3 2019-11-23 stsp goto done;
5683 4e68cba3 2019-11-23 stsp free(ondisk_path);
5684 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5685 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5686 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5687 4e68cba3 2019-11-23 stsp goto done;
5692 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5693 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5696 031a5338 2019-03-26 stsp got_repo_close(repo);
5697 d00136be 2019-03-26 stsp if (worktree)
5698 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5699 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5700 1dd54920 2019-05-11 stsp free((char *)pe->path);
5701 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5702 2ec1f75b 2019-03-26 stsp free(cwd);
5703 2ec1f75b 2019-03-26 stsp return error;
5706 2ec1f75b 2019-03-26 stsp __dead static void
5707 648e4ef7 2019-07-09 stsp usage_remove(void)
5709 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5710 f2a9dc41 2019-12-13 tracey getprogname());
5714 2a06fe5f 2019-08-24 stsp static const struct got_error *
5715 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5716 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5718 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5720 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5721 2a06fe5f 2019-08-24 stsp return NULL;
5722 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5723 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5724 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5725 2a06fe5f 2019-08-24 stsp return NULL;
5728 2ec1f75b 2019-03-26 stsp static const struct got_error *
5729 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5731 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5732 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5733 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5734 17ed4618 2019-06-02 stsp char *cwd = NULL;
5735 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5736 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5737 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5739 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5741 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5742 2ec1f75b 2019-03-26 stsp switch (ch) {
5744 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5746 70e3e7f5 2019-12-13 tracey case 'k':
5747 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5749 f2a9dc41 2019-12-13 tracey case 'R':
5750 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5753 f2a9dc41 2019-12-13 tracey usage_remove();
5754 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5758 2ec1f75b 2019-03-26 stsp argc -= optind;
5759 2ec1f75b 2019-03-26 stsp argv += optind;
5761 43012d58 2019-07-14 stsp #ifndef PROFILE
5762 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5763 43012d58 2019-07-14 stsp NULL) == -1)
5764 43012d58 2019-07-14 stsp err(1, "pledge");
5766 17ed4618 2019-06-02 stsp if (argc < 1)
5767 648e4ef7 2019-07-09 stsp usage_remove();
5769 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5770 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5771 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5772 2ec1f75b 2019-03-26 stsp goto done;
5774 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5775 fa51e947 2020-03-27 stsp if (error) {
5776 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5777 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
5778 2ec1f75b 2019-03-26 stsp goto done;
5781 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5784 2ec1f75b 2019-03-26 stsp goto done;
5786 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5787 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5788 2ec1f75b 2019-03-26 stsp if (error)
5789 2ec1f75b 2019-03-26 stsp goto done;
5791 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5792 6d022e97 2019-08-04 stsp if (error)
5793 6d022e97 2019-08-04 stsp goto done;
5795 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5796 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5797 f2a9dc41 2019-12-13 tracey struct stat sb;
5798 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5799 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5800 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5801 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5802 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5803 f2a9dc41 2019-12-13 tracey goto done;
5805 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5806 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5807 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5808 f2a9dc41 2019-12-13 tracey continue;
5810 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5811 f2a9dc41 2019-12-13 tracey ondisk_path);
5812 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5813 f2a9dc41 2019-12-13 tracey goto done;
5815 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5816 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5817 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5818 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5819 f2a9dc41 2019-12-13 tracey goto done;
5824 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5825 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5828 a129376b 2019-03-28 stsp got_repo_close(repo);
5829 a129376b 2019-03-28 stsp if (worktree)
5830 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5831 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5832 17ed4618 2019-06-02 stsp free((char *)pe->path);
5833 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5834 a129376b 2019-03-28 stsp free(cwd);
5835 a129376b 2019-03-28 stsp return error;
5838 a129376b 2019-03-28 stsp __dead static void
5839 a129376b 2019-03-28 stsp usage_revert(void)
5841 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5842 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5846 1ee397ad 2019-07-12 stsp static const struct got_error *
5847 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5849 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5850 fb9704af 2020-01-27 stsp return NULL;
5852 a129376b 2019-03-28 stsp while (path[0] == '/')
5854 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5855 33aa809d 2019-08-08 stsp return NULL;
5858 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5859 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5860 33aa809d 2019-08-08 stsp const char *action;
5863 33aa809d 2019-08-08 stsp static const struct got_error *
5864 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5865 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5867 33aa809d 2019-08-08 stsp char *line = NULL;
5868 33aa809d 2019-08-08 stsp size_t linesize = 0;
5869 33aa809d 2019-08-08 stsp ssize_t linelen;
5871 33aa809d 2019-08-08 stsp switch (status) {
5872 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5873 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5875 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5876 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5878 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5879 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5880 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5881 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5882 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5883 33aa809d 2019-08-08 stsp printf("%s", line);
5884 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5885 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5886 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5887 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5888 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5891 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5894 33aa809d 2019-08-08 stsp return NULL;
5897 33aa809d 2019-08-08 stsp static const struct got_error *
5898 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5899 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5901 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5902 33aa809d 2019-08-08 stsp char *line = NULL;
5903 33aa809d 2019-08-08 stsp size_t linesize = 0;
5904 33aa809d 2019-08-08 stsp ssize_t linelen;
5905 33aa809d 2019-08-08 stsp int resp = ' ';
5906 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5908 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5910 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5912 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5913 33aa809d 2019-08-08 stsp a->action);
5915 33aa809d 2019-08-08 stsp return err;
5916 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5917 33aa809d 2019-08-08 stsp if (linelen == -1) {
5918 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5919 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5920 33aa809d 2019-08-08 stsp return NULL;
5922 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5924 33aa809d 2019-08-08 stsp *nl = '\0';
5925 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5926 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5927 33aa809d 2019-08-08 stsp printf("y\n");
5928 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5929 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5930 33aa809d 2019-08-08 stsp printf("n\n");
5931 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5932 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5933 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5934 33aa809d 2019-08-08 stsp printf("q\n");
5936 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5937 33aa809d 2019-08-08 stsp free(line);
5938 33aa809d 2019-08-08 stsp return NULL;
5941 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5942 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5943 33aa809d 2019-08-08 stsp a->action);
5945 33aa809d 2019-08-08 stsp return err;
5946 33aa809d 2019-08-08 stsp resp = getchar();
5947 33aa809d 2019-08-08 stsp if (resp == '\n')
5948 33aa809d 2019-08-08 stsp resp = getchar();
5949 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5950 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5951 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5952 33aa809d 2019-08-08 stsp resp = ' ';
5954 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5955 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5956 33aa809d 2019-08-08 stsp resp = ' ';
5960 33aa809d 2019-08-08 stsp if (resp == 'y')
5961 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5962 33aa809d 2019-08-08 stsp else if (resp == 'n')
5963 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5964 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5965 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5967 1ee397ad 2019-07-12 stsp return NULL;
5971 a129376b 2019-03-28 stsp static const struct got_error *
5972 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5974 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5975 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5976 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5977 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5978 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5979 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5980 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5981 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5982 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5983 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5985 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5987 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5988 a129376b 2019-03-28 stsp switch (ch) {
5990 33aa809d 2019-08-08 stsp pflag = 1;
5993 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5996 0f6d7415 2019-08-08 stsp can_recurse = 1;
5999 a129376b 2019-03-28 stsp usage_revert();
6000 a129376b 2019-03-28 stsp /* NOTREACHED */
6004 a129376b 2019-03-28 stsp argc -= optind;
6005 a129376b 2019-03-28 stsp argv += optind;
6007 43012d58 2019-07-14 stsp #ifndef PROFILE
6008 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6009 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6010 43012d58 2019-07-14 stsp err(1, "pledge");
6012 e20a8b6f 2019-06-04 stsp if (argc < 1)
6013 a129376b 2019-03-28 stsp usage_revert();
6014 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6015 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6017 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6018 a129376b 2019-03-28 stsp if (cwd == NULL) {
6019 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6020 a129376b 2019-03-28 stsp goto done;
6022 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6023 fa51e947 2020-03-27 stsp if (error) {
6024 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6025 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6026 2ec1f75b 2019-03-26 stsp goto done;
6029 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6031 a129376b 2019-03-28 stsp if (error != NULL)
6032 a129376b 2019-03-28 stsp goto done;
6034 33aa809d 2019-08-08 stsp if (patch_script_path) {
6035 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6036 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6037 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6038 33aa809d 2019-08-08 stsp patch_script_path);
6039 33aa809d 2019-08-08 stsp goto done;
6042 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6043 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6044 a129376b 2019-03-28 stsp if (error)
6045 a129376b 2019-03-28 stsp goto done;
6047 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6048 6d022e97 2019-08-04 stsp if (error)
6049 6d022e97 2019-08-04 stsp goto done;
6051 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6052 0f6d7415 2019-08-08 stsp char *ondisk_path;
6053 0f6d7415 2019-08-08 stsp struct stat sb;
6054 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6055 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6056 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6057 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6058 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6059 0f6d7415 2019-08-08 stsp goto done;
6061 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6062 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6063 0f6d7415 2019-08-08 stsp free(ondisk_path);
6066 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6067 0f6d7415 2019-08-08 stsp ondisk_path);
6068 0f6d7415 2019-08-08 stsp free(ondisk_path);
6069 0f6d7415 2019-08-08 stsp goto done;
6071 0f6d7415 2019-08-08 stsp free(ondisk_path);
6072 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6073 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6074 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6075 0f6d7415 2019-08-08 stsp goto done;
6080 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6081 33aa809d 2019-08-08 stsp cpa.action = "revert";
6082 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6083 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6085 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6086 33aa809d 2019-08-08 stsp error == NULL)
6087 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6089 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6090 2ec1f75b 2019-03-26 stsp if (worktree)
6091 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6092 2ec1f75b 2019-03-26 stsp free(path);
6093 d00136be 2019-03-26 stsp free(cwd);
6094 6bad629b 2019-02-04 stsp return error;
6097 c4296144 2019-05-09 stsp __dead static void
6098 c4296144 2019-05-09 stsp usage_commit(void)
6100 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
6101 5c1e53bc 2019-07-28 stsp getprogname());
6105 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6106 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6107 e2ba3d07 2019-05-13 stsp const char *editor;
6108 e0870e44 2019-05-13 stsp const char *worktree_path;
6109 76d98825 2019-06-03 stsp const char *branch_name;
6110 314a6357 2019-05-13 stsp const char *repo_path;
6111 e0870e44 2019-05-13 stsp char *logmsg_path;
6115 33ad4cbe 2019-05-12 jcs static const struct got_error *
6116 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6119 76d98825 2019-06-03 stsp char *initial_content = NULL;
6120 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6121 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6122 e0870e44 2019-05-13 stsp char *template = NULL;
6123 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6125 33ad4cbe 2019-05-12 jcs size_t len;
6127 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6128 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6129 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6130 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6131 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6132 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6133 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6134 33ad4cbe 2019-05-12 jcs return NULL;
6137 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6138 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6140 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
6141 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6142 76d98825 2019-06-03 stsp a->branch_name) == -1)
6143 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6145 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6147 e0870e44 2019-05-13 stsp goto done;
6149 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
6151 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6152 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6153 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6154 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6155 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6159 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6161 76d98825 2019-06-03 stsp free(initial_content);
6162 e0870e44 2019-05-13 stsp free(template);
6164 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6165 3ce1b845 2019-07-15 stsp if (err == NULL) {
6166 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6167 3ce1b845 2019-07-15 stsp if (err) {
6168 3ce1b845 2019-07-15 stsp free(*logmsg);
6169 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6172 33ad4cbe 2019-05-12 jcs return err;
6175 c4296144 2019-05-09 stsp static const struct got_error *
6176 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6178 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6179 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6180 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6181 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6182 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6183 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6184 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6185 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6186 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6187 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6189 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6190 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6192 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6193 c4296144 2019-05-09 stsp switch (ch) {
6195 c4296144 2019-05-09 stsp logmsg = optarg;
6198 c4296144 2019-05-09 stsp usage_commit();
6199 c4296144 2019-05-09 stsp /* NOTREACHED */
6203 c4296144 2019-05-09 stsp argc -= optind;
6204 c4296144 2019-05-09 stsp argv += optind;
6206 43012d58 2019-07-14 stsp #ifndef PROFILE
6207 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6208 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6209 43012d58 2019-07-14 stsp err(1, "pledge");
6211 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6212 c4296144 2019-05-09 stsp if (cwd == NULL) {
6213 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6214 c4296144 2019-05-09 stsp goto done;
6216 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6217 fa51e947 2020-03-27 stsp if (error) {
6218 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6219 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6220 7d5807f4 2019-07-11 stsp goto done;
6223 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6224 c4296144 2019-05-09 stsp if (error)
6225 a698f62e 2019-07-25 stsp goto done;
6226 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6227 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6228 c4296144 2019-05-09 stsp goto done;
6231 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6232 916f288c 2019-07-30 stsp worktree);
6233 5c1e53bc 2019-07-28 stsp if (error)
6234 5c1e53bc 2019-07-28 stsp goto done;
6236 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6237 c9956ddf 2019-09-08 stsp if (error)
6238 c9956ddf 2019-09-08 stsp goto done;
6239 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6240 c9956ddf 2019-09-08 stsp gitconfig_path);
6241 c4296144 2019-05-09 stsp if (error != NULL)
6242 c4296144 2019-05-09 stsp goto done;
6244 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6245 aba9c984 2019-09-08 stsp if (error)
6246 aba9c984 2019-09-08 stsp return error;
6249 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6250 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6252 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6253 314a6357 2019-05-13 stsp error = get_editor(&editor);
6255 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6256 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6257 c4296144 2019-05-09 stsp if (error)
6258 c4296144 2019-05-09 stsp goto done;
6260 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6261 087fb88c 2019-08-04 stsp if (error)
6262 087fb88c 2019-08-04 stsp goto done;
6264 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6265 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6266 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6267 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6268 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6269 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6270 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6271 916f288c 2019-07-30 stsp goto done;
6273 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6275 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6276 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6277 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6278 e0870e44 2019-05-13 stsp if (error) {
6279 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6280 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6281 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6282 c4296144 2019-05-09 stsp goto done;
6285 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6286 c4296144 2019-05-09 stsp if (error)
6287 c4296144 2019-05-09 stsp goto done;
6288 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6290 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6291 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6292 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6293 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6294 7266f21f 2019-10-21 stsp error == NULL)
6295 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6296 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6298 c4296144 2019-05-09 stsp got_repo_close(repo);
6299 c4296144 2019-05-09 stsp if (worktree)
6300 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6301 c4296144 2019-05-09 stsp free(cwd);
6302 c4296144 2019-05-09 stsp free(id_str);
6303 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6304 e2ba3d07 2019-05-13 stsp free(editor);
6305 aba9c984 2019-09-08 stsp free(author);
6306 234035bc 2019-06-01 stsp return error;
6309 234035bc 2019-06-01 stsp __dead static void
6310 234035bc 2019-06-01 stsp usage_cherrypick(void)
6312 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6316 234035bc 2019-06-01 stsp static const struct got_error *
6317 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6319 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6320 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6321 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6322 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6323 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6324 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6325 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6326 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6327 234035bc 2019-06-01 stsp int ch, did_something = 0;
6329 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6330 234035bc 2019-06-01 stsp switch (ch) {
6332 234035bc 2019-06-01 stsp usage_cherrypick();
6333 234035bc 2019-06-01 stsp /* NOTREACHED */
6337 234035bc 2019-06-01 stsp argc -= optind;
6338 234035bc 2019-06-01 stsp argv += optind;
6340 43012d58 2019-07-14 stsp #ifndef PROFILE
6341 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6342 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6343 43012d58 2019-07-14 stsp err(1, "pledge");
6345 234035bc 2019-06-01 stsp if (argc != 1)
6346 234035bc 2019-06-01 stsp usage_cherrypick();
6348 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6349 234035bc 2019-06-01 stsp if (cwd == NULL) {
6350 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6351 234035bc 2019-06-01 stsp goto done;
6353 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6354 fa51e947 2020-03-27 stsp if (error) {
6355 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6356 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6358 234035bc 2019-06-01 stsp goto done;
6361 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6363 234035bc 2019-06-01 stsp if (error != NULL)
6364 234035bc 2019-06-01 stsp goto done;
6366 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6367 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6368 234035bc 2019-06-01 stsp if (error)
6369 234035bc 2019-06-01 stsp goto done;
6371 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6372 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6373 234035bc 2019-06-01 stsp if (error != NULL) {
6374 234035bc 2019-06-01 stsp struct got_reference *ref;
6375 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6376 234035bc 2019-06-01 stsp goto done;
6377 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6378 234035bc 2019-06-01 stsp if (error != NULL)
6379 234035bc 2019-06-01 stsp goto done;
6380 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6381 234035bc 2019-06-01 stsp got_ref_close(ref);
6382 234035bc 2019-06-01 stsp if (error != NULL)
6383 234035bc 2019-06-01 stsp goto done;
6385 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6386 234035bc 2019-06-01 stsp if (error)
6387 234035bc 2019-06-01 stsp goto done;
6389 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6390 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6391 234035bc 2019-06-01 stsp if (error != NULL)
6392 234035bc 2019-06-01 stsp goto done;
6394 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6395 234035bc 2019-06-01 stsp if (error) {
6396 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6397 234035bc 2019-06-01 stsp goto done;
6398 234035bc 2019-06-01 stsp error = NULL;
6400 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6401 234035bc 2019-06-01 stsp goto done;
6404 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6405 234035bc 2019-06-01 stsp if (error)
6406 234035bc 2019-06-01 stsp goto done;
6407 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6408 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6409 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6411 234035bc 2019-06-01 stsp if (error != NULL)
6412 234035bc 2019-06-01 stsp goto done;
6414 234035bc 2019-06-01 stsp if (did_something)
6415 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6417 234035bc 2019-06-01 stsp if (commit)
6418 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6419 234035bc 2019-06-01 stsp free(commit_id_str);
6420 234035bc 2019-06-01 stsp if (head_ref)
6421 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6422 234035bc 2019-06-01 stsp if (worktree)
6423 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6425 234035bc 2019-06-01 stsp got_repo_close(repo);
6426 c4296144 2019-05-09 stsp return error;
6429 5ef14e63 2019-06-02 stsp __dead static void
6430 5ef14e63 2019-06-02 stsp usage_backout(void)
6432 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6436 5ef14e63 2019-06-02 stsp static const struct got_error *
6437 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6439 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6440 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6441 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6442 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6443 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6444 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6445 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6446 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6447 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6449 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6450 5ef14e63 2019-06-02 stsp switch (ch) {
6452 5ef14e63 2019-06-02 stsp usage_backout();
6453 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6457 5ef14e63 2019-06-02 stsp argc -= optind;
6458 5ef14e63 2019-06-02 stsp argv += optind;
6460 43012d58 2019-07-14 stsp #ifndef PROFILE
6461 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6462 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6463 43012d58 2019-07-14 stsp err(1, "pledge");
6465 5ef14e63 2019-06-02 stsp if (argc != 1)
6466 5ef14e63 2019-06-02 stsp usage_backout();
6468 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6469 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6470 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6471 5ef14e63 2019-06-02 stsp goto done;
6473 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6474 fa51e947 2020-03-27 stsp if (error) {
6475 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6476 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6477 5ef14e63 2019-06-02 stsp goto done;
6480 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6482 5ef14e63 2019-06-02 stsp if (error != NULL)
6483 5ef14e63 2019-06-02 stsp goto done;
6485 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6486 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6487 5ef14e63 2019-06-02 stsp if (error)
6488 5ef14e63 2019-06-02 stsp goto done;
6490 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6491 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6492 5ef14e63 2019-06-02 stsp if (error != NULL) {
6493 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6494 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6495 5ef14e63 2019-06-02 stsp goto done;
6496 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6497 5ef14e63 2019-06-02 stsp if (error != NULL)
6498 5ef14e63 2019-06-02 stsp goto done;
6499 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6500 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6501 5ef14e63 2019-06-02 stsp if (error != NULL)
6502 5ef14e63 2019-06-02 stsp goto done;
6504 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6505 5ef14e63 2019-06-02 stsp if (error)
6506 5ef14e63 2019-06-02 stsp goto done;
6508 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6509 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6510 5ef14e63 2019-06-02 stsp if (error != NULL)
6511 5ef14e63 2019-06-02 stsp goto done;
6513 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6514 5ef14e63 2019-06-02 stsp if (error)
6515 5ef14e63 2019-06-02 stsp goto done;
6517 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6518 5ef14e63 2019-06-02 stsp if (error)
6519 5ef14e63 2019-06-02 stsp goto done;
6520 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6521 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6522 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6523 5ef14e63 2019-06-02 stsp goto done;
6526 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6527 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6528 5ef14e63 2019-06-02 stsp if (error != NULL)
6529 5ef14e63 2019-06-02 stsp goto done;
6531 5ef14e63 2019-06-02 stsp if (did_something)
6532 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6534 5ef14e63 2019-06-02 stsp if (commit)
6535 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6536 5ef14e63 2019-06-02 stsp free(commit_id_str);
6537 5ef14e63 2019-06-02 stsp if (head_ref)
6538 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6539 818c7501 2019-07-11 stsp if (worktree)
6540 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6542 818c7501 2019-07-11 stsp got_repo_close(repo);
6543 818c7501 2019-07-11 stsp return error;
6546 818c7501 2019-07-11 stsp __dead static void
6547 818c7501 2019-07-11 stsp usage_rebase(void)
6549 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6550 af54c8f8 2019-07-11 stsp getprogname());
6555 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6558 0ebf8283 2019-07-24 stsp size_t len;
6560 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6561 0ebf8283 2019-07-24 stsp if (len > limit)
6562 0ebf8283 2019-07-24 stsp len = limit;
6563 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6564 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6566 0ebf8283 2019-07-24 stsp *nl = '\0';
6569 0ebf8283 2019-07-24 stsp static const struct got_error *
6570 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6572 5943eee2 2019-08-13 stsp const struct got_error *err;
6573 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6574 5943eee2 2019-08-13 stsp const char *s;
6576 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6578 5943eee2 2019-08-13 stsp return err;
6580 5943eee2 2019-08-13 stsp s = logmsg0;
6581 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6584 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6585 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6586 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6587 5943eee2 2019-08-13 stsp goto done;
6590 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6592 5943eee2 2019-08-13 stsp free(logmsg0);
6593 a0ea4fc0 2020-02-28 stsp return err;
6596 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6597 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6599 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6600 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6601 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6603 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6605 a0ea4fc0 2020-02-28 stsp return err;
6607 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6609 a0ea4fc0 2020-02-28 stsp goto done;
6611 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6613 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6615 a0ea4fc0 2020-02-28 stsp goto done;
6617 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6619 a0ea4fc0 2020-02-28 stsp free(id_str);
6620 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6621 a0ea4fc0 2020-02-28 stsp free(logmsg);
6622 5943eee2 2019-08-13 stsp return err;
6625 818c7501 2019-07-11 stsp static const struct got_error *
6626 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6627 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6629 818c7501 2019-07-11 stsp const struct got_error *err;
6630 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6632 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6634 818c7501 2019-07-11 stsp goto done;
6636 ff0d2220 2019-07-11 stsp if (new_id) {
6637 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6639 ff0d2220 2019-07-11 stsp goto done;
6642 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6643 ff0d2220 2019-07-11 stsp if (new_id_str)
6644 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6646 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6648 0ebf8283 2019-07-24 stsp goto done;
6650 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6651 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6653 818c7501 2019-07-11 stsp free(old_id_str);
6654 818c7501 2019-07-11 stsp free(new_id_str);
6655 272a1371 2020-02-28 stsp free(logmsg);
6656 818c7501 2019-07-11 stsp return err;
6659 1ee397ad 2019-07-12 stsp static const struct got_error *
6660 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6662 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6664 818c7501 2019-07-11 stsp while (path[0] == '/')
6666 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6668 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6669 1ee397ad 2019-07-12 stsp return NULL;
6670 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6671 818c7501 2019-07-11 stsp *rebase_status = status;
6672 1ee397ad 2019-07-12 stsp return NULL;
6675 818c7501 2019-07-11 stsp static const struct got_error *
6676 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6677 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6678 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6680 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6681 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6682 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6685 818c7501 2019-07-11 stsp static const struct got_error *
6686 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6687 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6688 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6689 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6691 ff0d2220 2019-07-11 stsp const struct got_error *error;
6692 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6693 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6695 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6696 ff0d2220 2019-07-11 stsp if (error)
6697 ff0d2220 2019-07-11 stsp return error;
6699 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6700 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6701 ff0d2220 2019-07-11 stsp if (error) {
6702 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6703 ff0d2220 2019-07-11 stsp goto done;
6704 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6706 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6707 ff0d2220 2019-07-11 stsp free(new_commit_id);
6710 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6711 ff0d2220 2019-07-11 stsp return error;
6714 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6715 64c6d990 2019-07-11 stsp const char *path_prefix;
6716 64c6d990 2019-07-11 stsp size_t len;
6717 8ca9bd68 2019-07-25 stsp int errcode;
6720 64c6d990 2019-07-11 stsp static const struct got_error *
6721 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6722 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6723 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6724 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6726 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6728 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6729 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6730 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6732 64c6d990 2019-07-11 stsp return NULL;
6735 64c6d990 2019-07-11 stsp static const struct got_error *
6736 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6737 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6738 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6740 64c6d990 2019-07-11 stsp const struct got_error *err;
6741 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6742 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6743 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6745 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6746 64c6d990 2019-07-11 stsp return NULL;
6748 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6750 64c6d990 2019-07-11 stsp goto done;
6752 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6754 64c6d990 2019-07-11 stsp goto done;
6756 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6757 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6759 64c6d990 2019-07-11 stsp goto done;
6761 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6762 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6764 64c6d990 2019-07-11 stsp goto done;
6766 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6767 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6768 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6769 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6770 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6771 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6772 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6774 64c6d990 2019-07-11 stsp if (tree1)
6775 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6776 64c6d990 2019-07-11 stsp if (tree2)
6777 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6778 64c6d990 2019-07-11 stsp if (commit)
6779 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6780 64c6d990 2019-07-11 stsp if (parent_commit)
6781 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6782 64c6d990 2019-07-11 stsp return err;
6785 ff0d2220 2019-07-11 stsp static const struct got_error *
6786 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6787 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6788 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6789 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6790 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6792 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6793 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6794 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6795 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6796 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6798 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6800 af61c510 2019-07-19 stsp return err;
6802 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6803 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6805 af61c510 2019-07-19 stsp goto done;
6806 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6807 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6808 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6809 af61c510 2019-07-19 stsp if (err) {
6810 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6811 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6812 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6813 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6814 af61c510 2019-07-19 stsp "been reached?!?");
6816 ee780d5c 2020-01-04 stsp goto done;
6818 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6819 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6821 af61c510 2019-07-19 stsp goto done;
6823 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6825 af61c510 2019-07-19 stsp goto done;
6826 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6827 af61c510 2019-07-19 stsp commit_id = parent_id;
6831 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6832 af61c510 2019-07-19 stsp return err;
6835 af61c510 2019-07-19 stsp static const struct got_error *
6836 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6838 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6839 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6840 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6841 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6842 818c7501 2019-07-11 stsp char *cwd = NULL;
6843 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6844 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6845 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6846 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6847 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6848 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6849 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6850 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6851 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6852 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6853 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6854 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6855 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6857 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6858 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6860 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6861 818c7501 2019-07-11 stsp switch (ch) {
6863 818c7501 2019-07-11 stsp abort_rebase = 1;
6866 818c7501 2019-07-11 stsp continue_rebase = 1;
6869 818c7501 2019-07-11 stsp usage_rebase();
6870 818c7501 2019-07-11 stsp /* NOTREACHED */
6874 818c7501 2019-07-11 stsp argc -= optind;
6875 818c7501 2019-07-11 stsp argv += optind;
6877 43012d58 2019-07-14 stsp #ifndef PROFILE
6878 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6879 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6880 43012d58 2019-07-14 stsp err(1, "pledge");
6882 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6883 818c7501 2019-07-11 stsp usage_rebase();
6884 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6885 818c7501 2019-07-11 stsp if (argc != 0)
6886 818c7501 2019-07-11 stsp usage_rebase();
6887 818c7501 2019-07-11 stsp } else if (argc != 1)
6888 818c7501 2019-07-11 stsp usage_rebase();
6890 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6891 818c7501 2019-07-11 stsp if (cwd == NULL) {
6892 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6893 818c7501 2019-07-11 stsp goto done;
6895 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6896 fa51e947 2020-03-27 stsp if (error) {
6897 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6898 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
6899 818c7501 2019-07-11 stsp goto done;
6902 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6904 818c7501 2019-07-11 stsp if (error != NULL)
6905 818c7501 2019-07-11 stsp goto done;
6907 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6908 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6909 7ef62c4e 2020-02-24 stsp if (error)
6910 7ef62c4e 2020-02-24 stsp goto done;
6912 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6913 7ef62c4e 2020-02-24 stsp worktree);
6914 818c7501 2019-07-11 stsp if (error)
6915 7ef62c4e 2020-02-24 stsp goto done;
6916 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6917 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6918 818c7501 2019-07-11 stsp goto done;
6921 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6922 818c7501 2019-07-11 stsp if (error)
6923 818c7501 2019-07-11 stsp goto done;
6925 f6794adc 2019-07-23 stsp if (abort_rebase) {
6926 818c7501 2019-07-11 stsp int did_something;
6927 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6928 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6929 f6794adc 2019-07-23 stsp goto done;
6931 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6932 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6933 3e3a69f1 2019-07-25 stsp worktree, repo);
6934 818c7501 2019-07-11 stsp if (error)
6935 818c7501 2019-07-11 stsp goto done;
6936 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6937 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6938 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6939 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6940 818c7501 2019-07-11 stsp if (error)
6941 818c7501 2019-07-11 stsp goto done;
6942 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6943 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6946 818c7501 2019-07-11 stsp if (continue_rebase) {
6947 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6948 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6949 f6794adc 2019-07-23 stsp goto done;
6951 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6952 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6953 3e3a69f1 2019-07-25 stsp worktree, repo);
6954 818c7501 2019-07-11 stsp if (error)
6955 818c7501 2019-07-11 stsp goto done;
6957 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6958 01757395 2019-07-12 stsp resume_commit_id, repo);
6959 818c7501 2019-07-11 stsp if (error)
6960 818c7501 2019-07-11 stsp goto done;
6962 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6963 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6964 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6965 818c7501 2019-07-11 stsp goto done;
6968 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6969 818c7501 2019-07-11 stsp if (error != NULL)
6970 818c7501 2019-07-11 stsp goto done;
6973 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6974 ff0d2220 2019-07-11 stsp if (error)
6975 ff0d2220 2019-07-11 stsp goto done;
6977 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6978 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6980 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6981 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6982 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6983 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6984 818c7501 2019-07-11 stsp if (error)
6985 818c7501 2019-07-11 stsp goto done;
6986 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6987 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6988 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6989 818c7501 2019-07-11 stsp "with work tree's branch");
6990 818c7501 2019-07-11 stsp goto done;
6993 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6994 a51a74b3 2019-07-27 stsp if (error) {
6995 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6996 a51a74b3 2019-07-27 stsp goto done;
6997 a51a74b3 2019-07-27 stsp error = NULL;
6999 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7000 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7001 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7002 a51a74b3 2019-07-27 stsp goto done;
7004 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7005 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7006 818c7501 2019-07-11 stsp if (error)
7007 818c7501 2019-07-11 stsp goto done;
7010 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7011 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7012 818c7501 2019-07-11 stsp if (error)
7013 818c7501 2019-07-11 stsp goto done;
7015 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7016 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7017 fc66b545 2019-08-12 stsp if (pid == NULL) {
7018 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7019 fc66b545 2019-08-12 stsp int did_something;
7020 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7021 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
7022 fc66b545 2019-08-12 stsp &did_something);
7023 fc66b545 2019-08-12 stsp if (error)
7024 fc66b545 2019-08-12 stsp goto done;
7025 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7026 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7028 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7029 fc66b545 2019-08-12 stsp goto done;
7031 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7032 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7033 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7034 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7035 818c7501 2019-07-11 stsp commit = NULL;
7036 818c7501 2019-07-11 stsp if (error)
7037 818c7501 2019-07-11 stsp goto done;
7039 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7040 38b0338b 2019-11-29 stsp if (continue_rebase) {
7041 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7042 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7043 38b0338b 2019-11-29 stsp goto done;
7045 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7046 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7047 38b0338b 2019-11-29 stsp char *id_str;
7048 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7049 38b0338b 2019-11-29 stsp new_base_branch);
7050 38b0338b 2019-11-29 stsp if (error)
7051 38b0338b 2019-11-29 stsp goto done;
7052 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7053 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7054 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7055 38b0338b 2019-11-29 stsp free(id_str);
7056 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7057 38b0338b 2019-11-29 stsp new_head_commit_id);
7058 38b0338b 2019-11-29 stsp if (error)
7059 38b0338b 2019-11-29 stsp goto done;
7063 818c7501 2019-07-11 stsp pid = NULL;
7064 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7065 818c7501 2019-07-11 stsp commit_id = qid->id;
7066 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7067 818c7501 2019-07-11 stsp pid = qid;
7069 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7070 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7071 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7072 818c7501 2019-07-11 stsp if (error)
7073 818c7501 2019-07-11 stsp goto done;
7075 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7076 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7077 a0ea4fc0 2020-02-28 stsp if (error)
7078 a0ea4fc0 2020-02-28 stsp goto done;
7079 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7083 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7084 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7085 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7086 818c7501 2019-07-11 stsp if (error)
7087 818c7501 2019-07-11 stsp goto done;
7090 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7091 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7092 818c7501 2019-07-11 stsp if (error)
7093 818c7501 2019-07-11 stsp goto done;
7094 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7095 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7097 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7098 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7100 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7101 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7102 818c7501 2019-07-11 stsp free(resume_commit_id);
7103 818c7501 2019-07-11 stsp free(yca_id);
7104 818c7501 2019-07-11 stsp if (commit)
7105 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7106 818c7501 2019-07-11 stsp if (branch)
7107 818c7501 2019-07-11 stsp got_ref_close(branch);
7108 818c7501 2019-07-11 stsp if (new_base_branch)
7109 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7110 818c7501 2019-07-11 stsp if (tmp_branch)
7111 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7112 5ef14e63 2019-06-02 stsp if (worktree)
7113 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7115 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7116 5ef14e63 2019-06-02 stsp return error;
7119 0ebf8283 2019-07-24 stsp __dead static void
7120 0ebf8283 2019-07-24 stsp usage_histedit(void)
7122 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7123 0ebf8283 2019-07-24 stsp getprogname());
7127 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7128 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7129 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7130 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7131 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7133 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7134 0ebf8283 2019-07-24 stsp unsigned char code;
7135 0ebf8283 2019-07-24 stsp const char *name;
7136 0ebf8283 2019-07-24 stsp const char *desc;
7137 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7138 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7139 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7140 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7141 82997472 2020-01-29 stsp "be used" },
7142 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7143 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7144 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7147 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7148 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7149 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7150 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7151 0ebf8283 2019-07-24 stsp char *logmsg;
7153 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7155 0ebf8283 2019-07-24 stsp static const struct got_error *
7156 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7157 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7159 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7160 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7161 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7164 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7166 0ebf8283 2019-07-24 stsp goto done;
7168 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7170 0ebf8283 2019-07-24 stsp goto done;
7172 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7174 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7177 0ebf8283 2019-07-24 stsp if (n < 0)
7178 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7180 0ebf8283 2019-07-24 stsp if (commit)
7181 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7182 0ebf8283 2019-07-24 stsp free(id_str);
7183 0ebf8283 2019-07-24 stsp free(logmsg);
7184 0ebf8283 2019-07-24 stsp return err;
7187 0ebf8283 2019-07-24 stsp static const struct got_error *
7188 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7189 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7191 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7192 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7194 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7195 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7197 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7198 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7202 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7203 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7204 083957f4 2020-02-24 stsp if (n < 0) {
7205 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7211 0ebf8283 2019-07-24 stsp return err;
7214 0ebf8283 2019-07-24 stsp static const struct got_error *
7215 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7216 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7218 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7220 514f2ffe 2020-01-29 stsp char *id_str;
7221 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7223 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7224 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7226 514f2ffe 2020-01-29 stsp return err;
7228 514f2ffe 2020-01-29 stsp n = fprintf(f,
7229 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7230 514f2ffe 2020-01-29 stsp "# commit %s\n"
7231 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7232 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7233 514f2ffe 2020-01-29 stsp if (n < 0) {
7234 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7235 514f2ffe 2020-01-29 stsp goto done;
7238 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7239 514f2ffe 2020-01-29 stsp if (n < 0) {
7240 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7241 514f2ffe 2020-01-29 stsp goto done;
7244 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7245 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7246 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7247 0ebf8283 2019-07-24 stsp cmd->desc);
7248 0ebf8283 2019-07-24 stsp if (n < 0) {
7249 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7254 514f2ffe 2020-01-29 stsp free(id_str);
7255 0ebf8283 2019-07-24 stsp return err;
7258 0ebf8283 2019-07-24 stsp static const struct got_error *
7259 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7261 0ebf8283 2019-07-24 stsp static char msg[42];
7264 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7266 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7267 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7269 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7272 0ebf8283 2019-07-24 stsp static const struct got_error *
7273 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7274 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7276 0ebf8283 2019-07-24 stsp const struct got_error *err;
7277 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7278 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7280 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7282 0ebf8283 2019-07-24 stsp return err;
7284 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7286 0ebf8283 2019-07-24 stsp goto done;
7288 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7290 5943eee2 2019-08-13 stsp goto done;
7291 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7292 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7293 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7294 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7297 0ebf8283 2019-07-24 stsp if (folded_commit)
7298 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7299 0ebf8283 2019-07-24 stsp free(id_str);
7300 5943eee2 2019-08-13 stsp free(folded_logmsg);
7301 0ebf8283 2019-07-24 stsp return err;
7304 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7305 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7307 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7309 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7310 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7311 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7312 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7313 3f9de99f 2019-07-24 stsp folded = prev;
7314 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7317 0ebf8283 2019-07-24 stsp return folded;
7320 0ebf8283 2019-07-24 stsp static const struct got_error *
7321 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7322 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7324 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7325 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7326 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7327 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7329 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7331 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7333 0ebf8283 2019-07-24 stsp return err;
7335 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7336 0ebf8283 2019-07-24 stsp if (folded) {
7337 0ebf8283 2019-07-24 stsp while (folded != hle) {
7338 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7339 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7342 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7343 0ebf8283 2019-07-24 stsp logmsg, repo);
7345 0ebf8283 2019-07-24 stsp goto done;
7346 0ebf8283 2019-07-24 stsp free(logmsg);
7347 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7348 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7352 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7354 0ebf8283 2019-07-24 stsp goto done;
7355 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7357 5943eee2 2019-08-13 stsp goto done;
7358 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7359 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7360 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7361 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7362 0ebf8283 2019-07-24 stsp goto done;
7364 0ebf8283 2019-07-24 stsp free(logmsg);
7365 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7367 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7369 0ebf8283 2019-07-24 stsp goto done;
7371 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7372 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7374 0ebf8283 2019-07-24 stsp goto done;
7376 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7377 0ebf8283 2019-07-24 stsp close(fd);
7379 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7381 0ebf8283 2019-07-24 stsp goto done;
7383 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7384 0ebf8283 2019-07-24 stsp if (err) {
7385 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7386 0ebf8283 2019-07-24 stsp goto done;
7387 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7390 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7391 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7392 0ebf8283 2019-07-24 stsp free(logmsg_path);
7393 0ebf8283 2019-07-24 stsp free(logmsg);
7394 5943eee2 2019-08-13 stsp free(orig_logmsg);
7395 0ebf8283 2019-07-24 stsp free(editor);
7396 0ebf8283 2019-07-24 stsp if (commit)
7397 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7398 0ebf8283 2019-07-24 stsp return err;
7401 0ebf8283 2019-07-24 stsp static const struct got_error *
7402 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7403 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7405 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7406 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7407 0ebf8283 2019-07-24 stsp size_t size;
7408 0ebf8283 2019-07-24 stsp ssize_t len;
7409 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7410 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7411 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7412 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7414 0ebf8283 2019-07-24 stsp for (;;) {
7415 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7416 0ebf8283 2019-07-24 stsp if (len == -1) {
7417 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7418 0ebf8283 2019-07-24 stsp if (feof(f))
7420 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7421 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7426 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7428 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7429 0ebf8283 2019-07-24 stsp free(line);
7430 0ebf8283 2019-07-24 stsp line = NULL;
7433 0ebf8283 2019-07-24 stsp cmd = NULL;
7434 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7435 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7436 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7437 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7438 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7441 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7446 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7447 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7450 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7452 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7453 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7454 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7457 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7458 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7462 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7463 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7464 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7468 0ebf8283 2019-07-24 stsp free(line);
7469 0ebf8283 2019-07-24 stsp line = NULL;
7473 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7475 0ebf8283 2019-07-24 stsp *end = '\0';
7477 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7478 0ebf8283 2019-07-24 stsp if (err) {
7479 0ebf8283 2019-07-24 stsp /* override error code */
7480 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7484 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7485 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7486 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7489 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7490 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7491 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7492 0ebf8283 2019-07-24 stsp commit_id = NULL;
7493 0ebf8283 2019-07-24 stsp free(line);
7494 0ebf8283 2019-07-24 stsp line = NULL;
7495 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7498 0ebf8283 2019-07-24 stsp free(line);
7499 0ebf8283 2019-07-24 stsp free(commit_id);
7500 0ebf8283 2019-07-24 stsp return err;
7503 0ebf8283 2019-07-24 stsp static const struct got_error *
7504 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7505 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7507 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7508 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7509 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7510 5b87815e 2020-03-05 stsp static char msg[92];
7511 bfce7f83 2019-07-27 stsp char *id_str;
7513 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7514 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7515 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7516 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7517 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7519 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7520 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7521 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7522 5b87815e 2020-03-05 stsp if (hle == hle2)
7524 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7525 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7527 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7529 5b87815e 2020-03-05 stsp return err;
7530 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7531 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7532 5b87815e 2020-03-05 stsp free(id_str);
7533 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7537 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7538 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7539 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7542 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7543 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7545 bfce7f83 2019-07-27 stsp return err;
7546 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7547 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7548 bfce7f83 2019-07-27 stsp free(id_str);
7549 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7553 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7554 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7555 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7556 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7558 bfce7f83 2019-07-27 stsp return NULL;
7561 bfce7f83 2019-07-27 stsp static const struct got_error *
7562 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7563 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7564 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7566 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7567 0ebf8283 2019-07-24 stsp char *editor;
7568 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7570 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7572 0ebf8283 2019-07-24 stsp return err;
7574 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7575 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7576 0ebf8283 2019-07-24 stsp goto done;
7579 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7580 0ebf8283 2019-07-24 stsp if (f == NULL) {
7581 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7582 0ebf8283 2019-07-24 stsp goto done;
7584 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7586 bfce7f83 2019-07-27 stsp goto done;
7588 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7590 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7591 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7592 0ebf8283 2019-07-24 stsp free(editor);
7593 0ebf8283 2019-07-24 stsp return err;
7596 0ebf8283 2019-07-24 stsp static const struct got_error *
7597 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7598 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7599 514f2ffe 2020-01-29 stsp struct got_repository *);
7601 0ebf8283 2019-07-24 stsp static const struct got_error *
7602 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7603 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7604 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7606 0ebf8283 2019-07-24 stsp const struct got_error *err;
7607 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7608 0ebf8283 2019-07-24 stsp char *path = NULL;
7610 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7612 0ebf8283 2019-07-24 stsp return err;
7614 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7616 0ebf8283 2019-07-24 stsp goto done;
7618 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7620 0ebf8283 2019-07-24 stsp goto done;
7622 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7623 083957f4 2020-02-24 stsp rewind(f);
7624 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7626 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7627 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7628 0ebf8283 2019-07-24 stsp goto done;
7631 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7632 083957f4 2020-02-24 stsp if (err) {
7633 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7634 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7635 083957f4 2020-02-24 stsp goto done;
7636 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7637 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7641 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7642 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7643 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7644 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7645 0ebf8283 2019-07-24 stsp free(path);
7646 0ebf8283 2019-07-24 stsp return err;
7649 0ebf8283 2019-07-24 stsp static const struct got_error *
7650 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7651 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7653 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7654 0ebf8283 2019-07-24 stsp char *path = NULL;
7655 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7656 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7657 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7659 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7661 0ebf8283 2019-07-24 stsp return err;
7663 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7664 0ebf8283 2019-07-24 stsp if (f == NULL) {
7665 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7666 0ebf8283 2019-07-24 stsp goto done;
7668 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7669 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7674 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7675 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7676 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7677 0ebf8283 2019-07-24 stsp if (n < 0) {
7678 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7684 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7685 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7686 0ebf8283 2019-07-24 stsp free(path);
7687 0ebf8283 2019-07-24 stsp if (commit)
7688 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7689 0ebf8283 2019-07-24 stsp return err;
7693 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7695 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7697 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7698 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7699 bfce7f83 2019-07-27 stsp free(hle);
7703 0ebf8283 2019-07-24 stsp static const struct got_error *
7704 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7705 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7707 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7708 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7710 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7711 0ebf8283 2019-07-24 stsp if (f == NULL) {
7712 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7713 0ebf8283 2019-07-24 stsp goto done;
7716 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7718 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7719 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7720 0ebf8283 2019-07-24 stsp return err;
7723 0ebf8283 2019-07-24 stsp static const struct got_error *
7724 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7725 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7726 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7728 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7729 0ebf8283 2019-07-24 stsp int resp = ' ';
7731 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7732 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7733 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7734 0ebf8283 2019-07-24 stsp resp = getchar();
7735 a61a4414 2019-08-07 stsp if (resp == '\n')
7736 a61a4414 2019-08-07 stsp resp = getchar();
7737 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7738 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7739 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7741 426ebf2e 2019-07-25 stsp if (err) {
7742 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7743 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7745 bfce7f83 2019-07-27 stsp prev_err = err;
7746 426ebf2e 2019-07-25 stsp resp = ' ';
7750 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7751 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7752 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7753 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7754 426ebf2e 2019-07-25 stsp if (err) {
7755 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7756 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7758 bfce7f83 2019-07-27 stsp prev_err = err;
7759 426ebf2e 2019-07-25 stsp resp = ' ';
7763 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7764 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7767 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7770 0ebf8283 2019-07-24 stsp return err;
7773 0ebf8283 2019-07-24 stsp static const struct got_error *
7774 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7775 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7776 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7778 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7779 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7780 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7781 3e3a69f1 2019-07-25 stsp branch, repo);
7784 0ebf8283 2019-07-24 stsp static const struct got_error *
7785 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7786 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7788 0ebf8283 2019-07-24 stsp const struct got_error *err;
7789 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7791 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7793 0ebf8283 2019-07-24 stsp goto done;
7795 0ebf8283 2019-07-24 stsp if (new_id) {
7796 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7798 0ebf8283 2019-07-24 stsp goto done;
7801 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7802 0ebf8283 2019-07-24 stsp if (new_id_str)
7803 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7805 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7806 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7807 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7808 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7809 0ebf8283 2019-07-24 stsp goto done;
7811 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7813 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7815 0ebf8283 2019-07-24 stsp goto done;
7818 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7819 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7820 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7821 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7822 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7824 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7825 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7826 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7833 0ebf8283 2019-07-24 stsp free(old_id_str);
7834 0ebf8283 2019-07-24 stsp free(new_id_str);
7835 0ebf8283 2019-07-24 stsp return err;
7838 0ebf8283 2019-07-24 stsp static const struct got_error *
7839 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7840 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7841 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7842 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7844 0ebf8283 2019-07-24 stsp const struct got_error *err;
7845 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7846 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7848 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7849 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7850 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7852 0ebf8283 2019-07-24 stsp return err;
7855 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7857 0ebf8283 2019-07-24 stsp return err;
7859 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7860 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7861 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7862 0ebf8283 2019-07-24 stsp if (err) {
7863 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7864 0ebf8283 2019-07-24 stsp goto done;
7865 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7867 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7868 0ebf8283 2019-07-24 stsp free(new_commit_id);
7871 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7872 0ebf8283 2019-07-24 stsp return err;
7875 0ebf8283 2019-07-24 stsp static const struct got_error *
7876 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7877 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7879 0ebf8283 2019-07-24 stsp const struct got_error *error;
7880 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7882 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7884 0ebf8283 2019-07-24 stsp if (error)
7885 0ebf8283 2019-07-24 stsp return error;
7887 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7888 0ebf8283 2019-07-24 stsp if (error)
7889 0ebf8283 2019-07-24 stsp return error;
7891 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7892 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7893 0ebf8283 2019-07-24 stsp return error;
7896 ab20a43a 2020-01-29 stsp static const struct got_error *
7897 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7898 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7899 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7900 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7902 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7904 ab20a43a 2020-01-29 stsp switch (status) {
7905 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7906 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7907 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7908 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7909 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7910 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7915 ab20a43a 2020-01-29 stsp switch (staged_status) {
7916 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7917 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7918 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7919 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7920 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7925 ab20a43a 2020-01-29 stsp return NULL;
7928 0ebf8283 2019-07-24 stsp static const struct got_error *
7929 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7931 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7932 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7933 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7934 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7935 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7936 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7937 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7938 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7939 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7940 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7941 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7942 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7943 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7944 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7945 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7946 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7947 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7948 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7949 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7950 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7951 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7952 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7954 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7955 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7956 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7958 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7959 0ebf8283 2019-07-24 stsp switch (ch) {
7961 0ebf8283 2019-07-24 stsp abort_edit = 1;
7964 0ebf8283 2019-07-24 stsp continue_edit = 1;
7967 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7970 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7973 0ebf8283 2019-07-24 stsp usage_histedit();
7974 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7978 0ebf8283 2019-07-24 stsp argc -= optind;
7979 0ebf8283 2019-07-24 stsp argv += optind;
7981 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7982 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7983 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7984 0ebf8283 2019-07-24 stsp err(1, "pledge");
7986 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7987 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7988 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7989 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7990 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7991 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7992 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7993 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7994 0ebf8283 2019-07-24 stsp if (argc != 0)
7995 0ebf8283 2019-07-24 stsp usage_histedit();
7998 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7999 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8000 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8001 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8002 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8003 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8006 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8007 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8008 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8009 0ebf8283 2019-07-24 stsp goto done;
8011 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8012 fa51e947 2020-03-27 stsp if (error) {
8013 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8014 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8015 0ebf8283 2019-07-24 stsp goto done;
8018 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8020 0ebf8283 2019-07-24 stsp if (error != NULL)
8021 0ebf8283 2019-07-24 stsp goto done;
8023 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8024 0ebf8283 2019-07-24 stsp if (error)
8025 0ebf8283 2019-07-24 stsp goto done;
8026 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8027 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8028 0ebf8283 2019-07-24 stsp goto done;
8031 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8032 0ebf8283 2019-07-24 stsp if (error)
8033 0ebf8283 2019-07-24 stsp goto done;
8035 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8036 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8037 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8038 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8039 083957f4 2020-02-24 stsp "before the -m option can be used");
8040 083957f4 2020-02-24 stsp goto done;
8043 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8044 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8045 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8046 3e3a69f1 2019-07-25 stsp worktree, repo);
8047 0ebf8283 2019-07-24 stsp if (error)
8048 0ebf8283 2019-07-24 stsp goto done;
8049 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8050 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8051 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8052 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
8053 0ebf8283 2019-07-24 stsp if (error)
8054 0ebf8283 2019-07-24 stsp goto done;
8055 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8056 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8057 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8058 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8059 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8060 0ebf8283 2019-07-24 stsp goto done;
8063 0ebf8283 2019-07-24 stsp if (continue_edit) {
8064 0ebf8283 2019-07-24 stsp char *path;
8066 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8067 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8068 0ebf8283 2019-07-24 stsp goto done;
8071 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8072 0ebf8283 2019-07-24 stsp if (error)
8073 0ebf8283 2019-07-24 stsp goto done;
8075 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8076 0ebf8283 2019-07-24 stsp free(path);
8077 0ebf8283 2019-07-24 stsp if (error)
8078 0ebf8283 2019-07-24 stsp goto done;
8080 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8081 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8082 3e3a69f1 2019-07-25 stsp worktree, repo);
8083 0ebf8283 2019-07-24 stsp if (error)
8084 0ebf8283 2019-07-24 stsp goto done;
8086 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8087 0ebf8283 2019-07-24 stsp if (error)
8088 0ebf8283 2019-07-24 stsp goto done;
8090 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8091 0ebf8283 2019-07-24 stsp head_commit_id);
8092 0ebf8283 2019-07-24 stsp if (error)
8093 0ebf8283 2019-07-24 stsp goto done;
8094 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8095 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8096 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8097 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8098 3aac7cf7 2019-07-25 stsp goto done;
8100 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8101 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8102 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8103 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8104 0ebf8283 2019-07-24 stsp commit = NULL;
8105 0ebf8283 2019-07-24 stsp if (error)
8106 0ebf8283 2019-07-24 stsp goto done;
8108 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8109 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8110 0ebf8283 2019-07-24 stsp goto done;
8113 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8114 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8115 0ebf8283 2019-07-24 stsp if (error != NULL)
8116 0ebf8283 2019-07-24 stsp goto done;
8118 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8119 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8120 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8121 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8122 c7d20a3f 2019-07-30 stsp goto done;
8125 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8126 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8127 bfce7f83 2019-07-27 stsp branch = NULL;
8128 0ebf8283 2019-07-24 stsp if (error)
8129 0ebf8283 2019-07-24 stsp goto done;
8131 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8132 0ebf8283 2019-07-24 stsp head_commit_id);
8133 0ebf8283 2019-07-24 stsp if (error)
8134 0ebf8283 2019-07-24 stsp goto done;
8135 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8136 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8137 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8138 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8139 3aac7cf7 2019-07-25 stsp goto done;
8141 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8142 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8143 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8144 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8145 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8146 0ebf8283 2019-07-24 stsp commit = NULL;
8147 0ebf8283 2019-07-24 stsp if (error)
8148 0ebf8283 2019-07-24 stsp goto done;
8150 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8151 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8152 c5996fff 2020-01-29 stsp goto done;
8155 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8156 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8157 bfce7f83 2019-07-27 stsp if (error)
8158 bfce7f83 2019-07-27 stsp goto done;
8160 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8161 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8162 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8163 6ba2bea2 2019-07-27 stsp if (error) {
8164 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8165 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8166 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8167 0ebf8283 2019-07-24 stsp goto done;
8170 514f2ffe 2020-01-29 stsp const char *branch_name;
8171 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8172 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8173 514f2ffe 2020-01-29 stsp branch_name += 11;
8174 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8175 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8176 6ba2bea2 2019-07-27 stsp if (error) {
8177 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8178 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8179 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8180 0ebf8283 2019-07-24 stsp goto done;
8185 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8187 6ba2bea2 2019-07-27 stsp if (error) {
8188 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8189 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8190 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8191 0ebf8283 2019-07-24 stsp goto done;
8196 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8197 4ec14e60 2019-08-28 hiltjo if (error)
8198 0ebf8283 2019-07-24 stsp goto done;
8200 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8201 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8202 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8203 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8206 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8207 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8208 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8209 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8211 ab20a43a 2020-01-29 stsp if (error)
8212 ab20a43a 2020-01-29 stsp goto done;
8214 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8215 ab20a43a 2020-01-29 stsp int have_changes = 0;
8217 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8218 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8219 ab20a43a 2020-01-29 stsp if (error)
8220 ab20a43a 2020-01-29 stsp goto done;
8221 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8222 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8223 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8224 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8225 ab20a43a 2020-01-29 stsp if (error) {
8226 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8227 ab20a43a 2020-01-29 stsp goto done;
8228 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8229 ab20a43a 2020-01-29 stsp goto done;
8231 ab20a43a 2020-01-29 stsp if (have_changes) {
8232 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8233 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8234 ab20a43a 2020-01-29 stsp if (error)
8235 ab20a43a 2020-01-29 stsp goto done;
8237 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8238 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8239 ab20a43a 2020-01-29 stsp if (error)
8240 ab20a43a 2020-01-29 stsp goto done;
8241 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8242 ab20a43a 2020-01-29 stsp hle, NULL);
8243 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8244 ab20a43a 2020-01-29 stsp commit = NULL;
8245 ab20a43a 2020-01-29 stsp if (error)
8246 ab20a43a 2020-01-29 stsp goto done;
8252 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8253 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8254 0ebf8283 2019-07-24 stsp if (error)
8255 0ebf8283 2019-07-24 stsp goto done;
8259 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8260 0ebf8283 2019-07-24 stsp hle->commit_id);
8261 0ebf8283 2019-07-24 stsp if (error)
8262 0ebf8283 2019-07-24 stsp goto done;
8263 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8264 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8266 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8267 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8268 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
8269 0ebf8283 2019-07-24 stsp if (error)
8270 0ebf8283 2019-07-24 stsp goto done;
8271 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8272 0ebf8283 2019-07-24 stsp commit = NULL;
8274 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8275 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8277 a0ea4fc0 2020-02-28 stsp if (error)
8278 a0ea4fc0 2020-02-28 stsp goto done;
8279 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8283 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8284 0ebf8283 2019-07-24 stsp char *id_str;
8285 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8286 0ebf8283 2019-07-24 stsp if (error)
8287 0ebf8283 2019-07-24 stsp goto done;
8288 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8290 0ebf8283 2019-07-24 stsp free(id_str);
8291 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8292 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8293 3e3a69f1 2019-07-25 stsp fileindex);
8294 0ebf8283 2019-07-24 stsp goto done;
8297 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8298 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8299 0ebf8283 2019-07-24 stsp if (error)
8300 0ebf8283 2019-07-24 stsp goto done;
8304 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8305 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8306 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8307 0ebf8283 2019-07-24 stsp if (error)
8308 0ebf8283 2019-07-24 stsp goto done;
8311 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8312 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8313 0ebf8283 2019-07-24 stsp if (error)
8314 0ebf8283 2019-07-24 stsp goto done;
8315 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8316 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8318 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8319 3e3a69f1 2019-07-25 stsp branch, repo);
8321 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8322 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8323 0ebf8283 2019-07-24 stsp free(head_commit_id);
8324 0ebf8283 2019-07-24 stsp free(base_commit_id);
8325 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8326 0ebf8283 2019-07-24 stsp if (commit)
8327 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8328 0ebf8283 2019-07-24 stsp if (branch)
8329 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8330 0ebf8283 2019-07-24 stsp if (tmp_branch)
8331 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8332 0ebf8283 2019-07-24 stsp if (worktree)
8333 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8335 2822a352 2019-10-15 stsp got_repo_close(repo);
8336 2822a352 2019-10-15 stsp return error;
8339 2822a352 2019-10-15 stsp __dead static void
8340 2822a352 2019-10-15 stsp usage_integrate(void)
8342 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8346 2822a352 2019-10-15 stsp static const struct got_error *
8347 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8349 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8350 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8351 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8352 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8353 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8354 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8355 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8356 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8357 2822a352 2019-10-15 stsp int ch, did_something = 0;
8359 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8360 2822a352 2019-10-15 stsp switch (ch) {
8362 2822a352 2019-10-15 stsp usage_integrate();
8363 2822a352 2019-10-15 stsp /* NOTREACHED */
8367 2822a352 2019-10-15 stsp argc -= optind;
8368 2822a352 2019-10-15 stsp argv += optind;
8370 2822a352 2019-10-15 stsp if (argc != 1)
8371 2822a352 2019-10-15 stsp usage_integrate();
8372 2822a352 2019-10-15 stsp branch_arg = argv[0];
8374 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8375 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8376 2822a352 2019-10-15 stsp err(1, "pledge");
8378 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8379 2822a352 2019-10-15 stsp if (cwd == NULL) {
8380 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8381 2822a352 2019-10-15 stsp goto done;
8384 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8385 fa51e947 2020-03-27 stsp if (error) {
8386 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8387 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8389 2822a352 2019-10-15 stsp goto done;
8392 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8393 2822a352 2019-10-15 stsp if (error)
8394 2822a352 2019-10-15 stsp goto done;
8396 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8398 2822a352 2019-10-15 stsp if (error != NULL)
8399 2822a352 2019-10-15 stsp goto done;
8401 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8402 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8403 2822a352 2019-10-15 stsp if (error)
8404 2822a352 2019-10-15 stsp goto done;
8406 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8407 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8408 2822a352 2019-10-15 stsp goto done;
8411 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8412 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8413 2822a352 2019-10-15 stsp if (error)
8414 2822a352 2019-10-15 stsp goto done;
8416 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8417 2822a352 2019-10-15 stsp if (refname == NULL) {
8418 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8419 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8420 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8421 2822a352 2019-10-15 stsp goto done;
8423 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8424 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8425 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8426 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8427 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8428 2822a352 2019-10-15 stsp goto done;
8431 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8432 2822a352 2019-10-15 stsp if (error)
8433 2822a352 2019-10-15 stsp goto done;
8435 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8436 2822a352 2019-10-15 stsp if (error)
8437 2822a352 2019-10-15 stsp goto done;
8439 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8440 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8441 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8442 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8443 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8444 2822a352 2019-10-15 stsp goto done;
8447 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8448 2822a352 2019-10-15 stsp if (error) {
8449 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8450 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8451 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8452 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8453 2822a352 2019-10-15 stsp goto done;
8456 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8457 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8458 2822a352 2019-10-15 stsp check_cancelled, NULL);
8459 2822a352 2019-10-15 stsp if (error)
8460 2822a352 2019-10-15 stsp goto done;
8462 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8465 715dc77e 2019-08-03 stsp got_repo_close(repo);
8466 2822a352 2019-10-15 stsp if (worktree)
8467 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8468 2822a352 2019-10-15 stsp free(cwd);
8469 2822a352 2019-10-15 stsp free(base_commit_id);
8470 2822a352 2019-10-15 stsp free(commit_id);
8471 2822a352 2019-10-15 stsp free(refname);
8472 2822a352 2019-10-15 stsp free(base_refname);
8473 715dc77e 2019-08-03 stsp return error;
8476 715dc77e 2019-08-03 stsp __dead static void
8477 715dc77e 2019-08-03 stsp usage_stage(void)
8479 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8480 f3055044 2019-08-07 stsp "[file-path ...]\n",
8481 715dc77e 2019-08-03 stsp getprogname());
8485 715dc77e 2019-08-03 stsp static const struct got_error *
8486 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8487 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8488 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8489 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8491 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8492 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8494 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8495 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8496 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8497 408b4ebc 2019-08-03 stsp return NULL;
8499 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8500 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8501 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8503 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8505 408b4ebc 2019-08-03 stsp return err;
8507 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8508 408b4ebc 2019-08-03 stsp free(id_str);
8509 dc424a06 2019-08-07 stsp return NULL;
8512 dc424a06 2019-08-07 stsp static const struct got_error *
8513 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8515 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8516 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8517 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8518 715dc77e 2019-08-03 stsp char *cwd = NULL;
8519 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8520 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8521 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8522 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8523 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8524 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8526 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8528 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8529 715dc77e 2019-08-03 stsp switch (ch) {
8531 408b4ebc 2019-08-03 stsp list_stage = 1;
8534 dc424a06 2019-08-07 stsp pflag = 1;
8537 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8540 715dc77e 2019-08-03 stsp usage_stage();
8541 715dc77e 2019-08-03 stsp /* NOTREACHED */
8545 715dc77e 2019-08-03 stsp argc -= optind;
8546 715dc77e 2019-08-03 stsp argv += optind;
8548 715dc77e 2019-08-03 stsp #ifndef PROFILE
8549 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8550 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8551 715dc77e 2019-08-03 stsp err(1, "pledge");
8553 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8554 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8555 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8556 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8558 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8559 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8560 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8561 715dc77e 2019-08-03 stsp goto done;
8564 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8565 fa51e947 2020-03-27 stsp if (error) {
8566 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8567 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
8568 715dc77e 2019-08-03 stsp goto done;
8571 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8573 715dc77e 2019-08-03 stsp if (error != NULL)
8574 715dc77e 2019-08-03 stsp goto done;
8576 dc424a06 2019-08-07 stsp if (patch_script_path) {
8577 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8578 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8579 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8580 dc424a06 2019-08-07 stsp patch_script_path);
8581 dc424a06 2019-08-07 stsp goto done;
8584 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8585 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8586 715dc77e 2019-08-03 stsp if (error)
8587 715dc77e 2019-08-03 stsp goto done;
8589 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8590 6d022e97 2019-08-04 stsp if (error)
8591 6d022e97 2019-08-04 stsp goto done;
8593 6d022e97 2019-08-04 stsp if (list_stage)
8594 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8595 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8597 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8598 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8599 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8600 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8601 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8604 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8605 2e1f37b0 2019-08-08 stsp error == NULL)
8606 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8608 ad493afc 2019-08-03 stsp got_repo_close(repo);
8609 ad493afc 2019-08-03 stsp if (worktree)
8610 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8611 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8612 ad493afc 2019-08-03 stsp free((char *)pe->path);
8613 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8614 ad493afc 2019-08-03 stsp free(cwd);
8615 ad493afc 2019-08-03 stsp return error;
8618 ad493afc 2019-08-03 stsp __dead static void
8619 ad493afc 2019-08-03 stsp usage_unstage(void)
8621 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8622 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8623 ad493afc 2019-08-03 stsp getprogname());
8628 ad493afc 2019-08-03 stsp static const struct got_error *
8629 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8631 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8632 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8633 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8634 ad493afc 2019-08-03 stsp char *cwd = NULL;
8635 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8636 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8637 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8638 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8639 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8640 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8642 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8644 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8645 ad493afc 2019-08-03 stsp switch (ch) {
8647 2e1f37b0 2019-08-08 stsp pflag = 1;
8650 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8653 ad493afc 2019-08-03 stsp usage_unstage();
8654 ad493afc 2019-08-03 stsp /* NOTREACHED */
8658 ad493afc 2019-08-03 stsp argc -= optind;
8659 ad493afc 2019-08-03 stsp argv += optind;
8661 ad493afc 2019-08-03 stsp #ifndef PROFILE
8662 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8663 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8664 ad493afc 2019-08-03 stsp err(1, "pledge");
8666 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8667 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8669 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8670 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8671 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8672 ad493afc 2019-08-03 stsp goto done;
8675 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8676 fa51e947 2020-03-27 stsp if (error) {
8677 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8678 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
8679 ad493afc 2019-08-03 stsp goto done;
8682 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8684 ad493afc 2019-08-03 stsp if (error != NULL)
8685 ad493afc 2019-08-03 stsp goto done;
8687 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8688 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8689 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8690 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8691 2e1f37b0 2019-08-08 stsp patch_script_path);
8692 2e1f37b0 2019-08-08 stsp goto done;
8696 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8697 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8698 ad493afc 2019-08-03 stsp if (error)
8699 ad493afc 2019-08-03 stsp goto done;
8701 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8702 ad493afc 2019-08-03 stsp if (error)
8703 ad493afc 2019-08-03 stsp goto done;
8705 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8706 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8707 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8708 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8710 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8711 2e1f37b0 2019-08-08 stsp error == NULL)
8712 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8714 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8715 715dc77e 2019-08-03 stsp if (worktree)
8716 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8717 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8718 715dc77e 2019-08-03 stsp free((char *)pe->path);
8719 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8720 715dc77e 2019-08-03 stsp free(cwd);
8721 01073a5d 2019-08-22 stsp return error;
8724 01073a5d 2019-08-22 stsp __dead static void
8725 01073a5d 2019-08-22 stsp usage_cat(void)
8727 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8728 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8732 01073a5d 2019-08-22 stsp static const struct got_error *
8733 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8735 01073a5d 2019-08-22 stsp const struct got_error *err;
8736 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8738 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8740 01073a5d 2019-08-22 stsp return err;
8742 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8743 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8744 01073a5d 2019-08-22 stsp return err;
8747 01073a5d 2019-08-22 stsp static const struct got_error *
8748 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8750 01073a5d 2019-08-22 stsp const struct got_error *err;
8751 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8752 56e0773d 2019-11-28 stsp int nentries, i;
8754 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8756 01073a5d 2019-08-22 stsp return err;
8758 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8759 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8760 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8761 01073a5d 2019-08-22 stsp char *id_str;
8762 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8764 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8765 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8768 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8769 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8770 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8771 01073a5d 2019-08-22 stsp free(id_str);
8774 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8775 01073a5d 2019-08-22 stsp return err;
8778 01073a5d 2019-08-22 stsp static const struct got_error *
8779 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8781 01073a5d 2019-08-22 stsp const struct got_error *err;
8782 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8783 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8784 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8785 01073a5d 2019-08-22 stsp char *id_str = NULL;
8786 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8788 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8790 01073a5d 2019-08-22 stsp return err;
8792 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8794 01073a5d 2019-08-22 stsp goto done;
8796 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8797 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8798 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8799 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8800 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8801 01073a5d 2019-08-22 stsp char *pid_str;
8802 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8804 01073a5d 2019-08-22 stsp goto done;
8805 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8806 01073a5d 2019-08-22 stsp free(pid_str);
8808 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8809 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8810 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8812 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8813 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8814 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8816 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8817 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8818 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8820 01073a5d 2019-08-22 stsp free(id_str);
8821 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8822 01073a5d 2019-08-22 stsp return err;
8825 01073a5d 2019-08-22 stsp static const struct got_error *
8826 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8828 01073a5d 2019-08-22 stsp const struct got_error *err;
8829 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8830 01073a5d 2019-08-22 stsp char *id_str = NULL;
8831 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8833 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8835 01073a5d 2019-08-22 stsp return err;
8837 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8839 01073a5d 2019-08-22 stsp goto done;
8841 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8843 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8844 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8845 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8846 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8848 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8849 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8850 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8852 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8853 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8854 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8856 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8857 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8858 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8864 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8865 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8867 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8868 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8869 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8871 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8872 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8873 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8875 01073a5d 2019-08-22 stsp free(id_str);
8876 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8877 01073a5d 2019-08-22 stsp return err;
8880 01073a5d 2019-08-22 stsp static const struct got_error *
8881 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8883 01073a5d 2019-08-22 stsp const struct got_error *error;
8884 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8885 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8886 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8887 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8888 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8889 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8891 01073a5d 2019-08-22 stsp #ifndef PROFILE
8892 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8893 01073a5d 2019-08-22 stsp NULL) == -1)
8894 01073a5d 2019-08-22 stsp err(1, "pledge");
8897 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8898 01073a5d 2019-08-22 stsp switch (ch) {
8900 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8903 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8904 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8905 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8907 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8910 896e9b6f 2019-08-26 stsp force_path = 1;
8913 01073a5d 2019-08-22 stsp usage_cat();
8914 01073a5d 2019-08-22 stsp /* NOTREACHED */
8918 01073a5d 2019-08-22 stsp argc -= optind;
8919 01073a5d 2019-08-22 stsp argv += optind;
8921 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8922 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8923 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8924 01073a5d 2019-08-22 stsp goto done;
8926 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8927 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8928 01073a5d 2019-08-22 stsp goto done;
8929 01073a5d 2019-08-22 stsp if (worktree) {
8930 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8931 01073a5d 2019-08-22 stsp repo_path = strdup(
8932 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8933 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8934 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8935 01073a5d 2019-08-22 stsp goto done;
8940 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8941 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8942 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8943 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8946 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8947 01073a5d 2019-08-22 stsp free(repo_path);
8948 01073a5d 2019-08-22 stsp if (error != NULL)
8949 01073a5d 2019-08-22 stsp goto done;
8951 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8952 896e9b6f 2019-08-26 stsp if (error)
8953 896e9b6f 2019-08-26 stsp goto done;
8955 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8956 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8957 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8958 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8959 01073a5d 2019-08-22 stsp if (error)
8960 01073a5d 2019-08-22 stsp goto done;
8962 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8963 896e9b6f 2019-08-26 stsp if (force_path) {
8964 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8966 896e9b6f 2019-08-26 stsp if (error)
8969 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8970 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8971 896e9b6f 2019-08-26 stsp if (error) {
8972 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8973 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8975 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8976 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8977 896e9b6f 2019-08-26 stsp if (error)
8982 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8983 01073a5d 2019-08-22 stsp if (error)
8986 01073a5d 2019-08-22 stsp switch (obj_type) {
8987 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8988 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8990 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8991 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8993 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8994 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8996 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8997 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9000 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9003 01073a5d 2019-08-22 stsp if (error)
9005 01073a5d 2019-08-22 stsp free(label);
9006 01073a5d 2019-08-22 stsp label = NULL;
9008 01073a5d 2019-08-22 stsp id = NULL;
9011 01073a5d 2019-08-22 stsp free(label);
9013 896e9b6f 2019-08-26 stsp free(commit_id);
9014 01073a5d 2019-08-22 stsp if (worktree)
9015 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9016 01073a5d 2019-08-22 stsp if (repo) {
9017 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9018 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9019 01073a5d 2019-08-22 stsp if (error == NULL)
9020 01073a5d 2019-08-22 stsp error = repo_error;
9022 0ebf8283 2019-07-24 stsp return error;