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 4ba14133 2020-03-20 stsp "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 30718c93 2020-03-21 stsp create_head_ref(struct got_reference *target_ref, int verbosity,
889 30718c93 2020-03-21 stsp 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 4ba14133 2020-03-20 stsp err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, 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;
962 41b0de12 2020-03-21 stsp static const struct got_error *
963 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
965 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
966 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
967 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
968 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
969 bb64b798 2020-03-18 stsp const char *repo_path;
970 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
971 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
972 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
973 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
974 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
975 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
976 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
977 b46f3e71 2020-03-18 stsp char *git_url = NULL;
978 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
979 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
980 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
982 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
983 41b0de12 2020-03-21 stsp int list_refs_only = 0;
984 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
986 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
987 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
988 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
990 41b0de12 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
991 93658fb9 2020-03-18 stsp switch (ch) {
993 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
996 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
997 4ba14133 2020-03-20 stsp optarg, NULL);
999 4ba14133 2020-03-20 stsp return error;
1002 41b0de12 2020-03-21 stsp list_refs_only = 1;
1005 469dd726 2020-03-20 stsp mirror_references = 1;
1008 68999b92 2020-03-18 stsp if (verbosity < 0)
1009 68999b92 2020-03-18 stsp verbosity = 0;
1010 68999b92 2020-03-18 stsp else if (verbosity < 3)
1011 68999b92 2020-03-18 stsp verbosity++;
1014 68999b92 2020-03-18 stsp verbosity = -1;
1017 93658fb9 2020-03-18 stsp usage_clone();
1021 93658fb9 2020-03-18 stsp argc -= optind;
1022 93658fb9 2020-03-18 stsp argv += optind;
1024 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1025 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1026 41b0de12 2020-03-21 stsp if (list_refs_only) {
1027 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1028 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1029 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1030 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1031 41b0de12 2020-03-21 stsp if (mirror_references)
1032 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1033 41b0de12 2020-03-21 stsp if (verbosity == -1)
1034 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1037 93658fb9 2020-03-18 stsp uri = argv[0];
1039 9df6f38b 2020-03-18 stsp if (argc == 1)
1040 93658fb9 2020-03-18 stsp dirname = NULL;
1041 9df6f38b 2020-03-18 stsp else if (argc == 2)
1042 93658fb9 2020-03-18 stsp dirname = argv[1];
1044 93658fb9 2020-03-18 stsp usage_clone();
1046 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1047 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1048 39c64a6a 2020-03-18 stsp if (error)
1049 09f63084 2020-03-20 stsp goto done;
1051 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1052 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1053 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1054 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1055 09838ffc 2020-03-18 stsp goto done;
1058 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1059 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1060 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1061 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1062 39c64a6a 2020-03-18 stsp err(1, "pledge");
1064 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1065 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1066 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1067 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1068 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1069 39c64a6a 2020-03-18 stsp err(1, "pledge");
1071 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1072 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1073 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1074 39c64a6a 2020-03-18 stsp goto done;
1076 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1077 39c64a6a 2020-03-18 stsp goto done;
1079 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1080 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1081 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1082 bb64b798 2020-03-18 stsp goto done;
1084 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1086 bb64b798 2020-03-18 stsp repo_path = dirname;
1088 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1089 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1090 41b0de12 2020-03-21 stsp if (error)
1091 41b0de12 2020-03-21 stsp goto done;
1093 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1094 41b0de12 2020-03-21 stsp if (error)
1095 41b0de12 2020-03-21 stsp goto done;
1096 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1097 41b0de12 2020-03-21 stsp if (error)
1098 41b0de12 2020-03-21 stsp goto done;
1101 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1102 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1103 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1104 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1105 ee448f5f 2020-03-18 stsp goto done;
1108 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1109 ee448f5f 2020-03-18 stsp if (error)
1110 ee448f5f 2020-03-18 stsp goto done;
1112 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1113 9c52365f 2020-03-21 stsp server_path, verbosity);
1114 39c64a6a 2020-03-18 stsp if (error)
1115 bb64b798 2020-03-18 stsp goto done;
1117 68999b92 2020-03-18 stsp if (verbosity >= 0)
1118 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1119 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1121 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1122 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1123 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1124 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1125 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1126 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1127 41b0de12 2020-03-21 stsp fetch_all_branches, &wanted_branches, list_refs_only,
1128 2690194b 2020-03-21 stsp verbosity, fetchfd, repo, fetch_progress, &fpa);
1129 39c64a6a 2020-03-18 stsp if (error)
1130 d9b4d0c0 2020-03-18 stsp goto done;
1132 41b0de12 2020-03-21 stsp if (list_refs_only) {
1133 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1134 41b0de12 2020-03-21 stsp goto done;
1137 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1138 39c64a6a 2020-03-18 stsp if (error)
1139 d9b4d0c0 2020-03-18 stsp goto done;
1140 68999b92 2020-03-18 stsp if (verbosity >= 0)
1141 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1142 d9b4d0c0 2020-03-18 stsp free(id_str);
1144 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1145 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1146 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1147 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1148 7ebc0570 2020-03-18 stsp char *remote_refname;
1150 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1151 39c64a6a 2020-03-18 stsp if (error)
1152 d9b4d0c0 2020-03-18 stsp goto done;
1154 469dd726 2020-03-20 stsp if (mirror_references)
1157 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1160 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1161 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1162 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1163 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1164 7ebc0570 2020-03-18 stsp goto done;
1166 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1167 39c64a6a 2020-03-18 stsp if (error)
1168 d9b4d0c0 2020-03-18 stsp goto done;
1171 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1172 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1173 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1174 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1175 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1177 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1180 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1181 39c64a6a 2020-03-18 stsp if (error) {
1182 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1183 55330abe 2020-03-20 stsp error = NULL;
1186 d9b4d0c0 2020-03-18 stsp goto done;
1189 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1190 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1191 39c64a6a 2020-03-18 stsp if (error)
1192 d9b4d0c0 2020-03-18 stsp goto done;
1194 4ba14133 2020-03-20 stsp if (pe == NULL) {
1196 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1197 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1198 4ba14133 2020-03-20 stsp * which could be fetched instead.
1200 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1201 4ba14133 2020-03-20 stsp const char *target = pe->path;
1202 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1204 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1205 4ba14133 2020-03-20 stsp if (error) {
1206 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1207 4ba14133 2020-03-20 stsp error = NULL;
1210 4ba14133 2020-03-20 stsp goto done;
1213 30718c93 2020-03-21 stsp error = create_head_ref(target_ref, verbosity, repo);
1214 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1215 4ba14133 2020-03-20 stsp if (error)
1216 4ba14133 2020-03-20 stsp goto done;
1221 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1222 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1223 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1224 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1225 659e7fbd 2020-03-20 stsp goto done;
1227 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1228 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1229 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1230 659e7fbd 2020-03-20 stsp goto done;
1232 659e7fbd 2020-03-20 stsp if (mirror_references) {
1233 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1234 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1235 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1236 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1237 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1238 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1239 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1240 659e7fbd 2020-03-20 stsp goto done;
1242 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1243 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1244 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1245 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1246 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1247 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1248 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1249 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1250 659e7fbd 2020-03-20 stsp goto done;
1253 659e7fbd 2020-03-20 stsp const char *branchname;
1256 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1257 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1259 659e7fbd 2020-03-20 stsp if (head_symref) {
1260 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1261 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1262 659e7fbd 2020-03-20 stsp branchname += 11;
1264 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1265 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1266 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1267 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1268 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1269 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1270 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1271 659e7fbd 2020-03-20 stsp branchname) == -1) {
1272 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1273 659e7fbd 2020-03-20 stsp goto done;
1276 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1277 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1278 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1279 659e7fbd 2020-03-20 stsp goto done;
1282 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1283 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1284 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1286 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1287 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1288 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1289 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1290 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1292 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1293 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1294 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1295 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1297 bb64b798 2020-03-18 stsp got_repo_close(repo);
1298 659e7fbd 2020-03-20 stsp if (head_symref)
1299 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1300 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1301 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1302 d9b4d0c0 2020-03-18 stsp free(pe->data);
1304 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1305 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1306 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1307 d9b4d0c0 2020-03-18 stsp free(pe->data);
1309 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1310 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1311 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1312 09838ffc 2020-03-18 stsp free(proto);
1313 09838ffc 2020-03-18 stsp free(host);
1314 09838ffc 2020-03-18 stsp free(port);
1315 09838ffc 2020-03-18 stsp free(server_path);
1316 09838ffc 2020-03-18 stsp free(repo_name);
1317 bb64b798 2020-03-18 stsp free(default_destdir);
1318 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1319 b46f3e71 2020-03-18 stsp free(git_url);
1320 39c64a6a 2020-03-18 stsp return error;
1323 7848a0e1 2020-03-19 stsp static const struct got_error *
1324 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1325 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1327 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1328 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1329 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1331 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1333 7848a0e1 2020-03-19 stsp goto done;
1335 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1336 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1337 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1338 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1339 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1341 db6d8ad8 2020-03-21 stsp goto done;
1344 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1345 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1346 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1348 7848a0e1 2020-03-19 stsp goto done;
1349 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1351 7848a0e1 2020-03-19 stsp goto done;
1352 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1353 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1354 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1355 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1357 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1359 7848a0e1 2020-03-19 stsp goto done;
1361 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1363 7848a0e1 2020-03-19 stsp goto done;
1364 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1365 6338a6a1 2020-03-21 stsp goto done;
1367 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1369 6338a6a1 2020-03-21 stsp goto done;
1370 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1372 6338a6a1 2020-03-21 stsp goto done;
1375 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1376 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1377 6338a6a1 2020-03-21 stsp new_id_str);
1379 7848a0e1 2020-03-19 stsp free(old_id);
1380 7848a0e1 2020-03-19 stsp free(new_id_str);
1381 7848a0e1 2020-03-19 stsp return err;
1384 2ab43947 2020-03-18 stsp __dead static void
1385 7848a0e1 2020-03-19 stsp usage_fetch(void)
1387 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1388 db6d8ad8 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [remote-repository-name]\n",
1389 13f12b09 2020-03-21 stsp getprogname());
1393 7848a0e1 2020-03-19 stsp static const struct got_error *
1394 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1395 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1397 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1398 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1399 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1400 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1401 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1402 f21ec2f0 2020-03-21 stsp char *id_str;
1404 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1406 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1408 f21ec2f0 2020-03-21 stsp return err;
1410 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1411 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1413 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1414 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1417 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1418 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1421 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1424 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1427 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1432 f21ec2f0 2020-03-21 stsp free(id_str);
1433 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1436 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1437 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1438 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1442 f21ec2f0 2020-03-21 stsp return err;
1445 f21ec2f0 2020-03-21 stsp static const struct got_error *
1446 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1448 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1449 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1450 7848a0e1 2020-03-19 stsp const char *remote_name;
1451 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1452 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1453 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1454 7848a0e1 2020-03-19 stsp int nremotes;
1455 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1456 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1457 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1458 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
1459 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1460 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1461 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1462 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1463 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1464 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1465 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1467 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1468 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1469 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1471 db6d8ad8 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvq")) != -1) {
1472 7848a0e1 2020-03-19 stsp switch (ch) {
1474 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1477 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1478 4ba14133 2020-03-20 stsp optarg, NULL);
1479 4ba14133 2020-03-20 stsp if (error)
1480 4ba14133 2020-03-20 stsp return error;
1483 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1486 41b0de12 2020-03-21 stsp list_refs_only = 1;
1489 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1490 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1491 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1493 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1496 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1499 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1500 7848a0e1 2020-03-19 stsp verbosity = 0;
1501 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1502 7848a0e1 2020-03-19 stsp verbosity++;
1505 7848a0e1 2020-03-19 stsp verbosity = -1;
1508 7848a0e1 2020-03-19 stsp usage_fetch();
1512 7848a0e1 2020-03-19 stsp argc -= optind;
1513 7848a0e1 2020-03-19 stsp argv += optind;
1515 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1516 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1517 41b0de12 2020-03-21 stsp if (list_refs_only) {
1518 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1519 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1520 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1521 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1522 f21ec2f0 2020-03-21 stsp if (delete_refs)
1523 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1524 41b0de12 2020-03-21 stsp if (verbosity == -1)
1525 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1528 7848a0e1 2020-03-19 stsp if (argc == 0)
1529 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1530 7848a0e1 2020-03-19 stsp else if (argc == 1)
1531 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1533 7848a0e1 2020-03-19 stsp usage_fetch();
1535 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1536 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1537 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1538 7848a0e1 2020-03-19 stsp goto done;
1541 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1542 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1543 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1544 7848a0e1 2020-03-19 stsp goto done;
1546 7848a0e1 2020-03-19 stsp error = NULL;
1547 7848a0e1 2020-03-19 stsp if (worktree) {
1548 7848a0e1 2020-03-19 stsp repo_path =
1549 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1550 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1551 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1552 7848a0e1 2020-03-19 stsp if (error)
1553 7848a0e1 2020-03-19 stsp goto done;
1555 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1556 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1557 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1558 7848a0e1 2020-03-19 stsp goto done;
1563 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1564 7848a0e1 2020-03-19 stsp if (error)
1565 7848a0e1 2020-03-19 stsp goto done;
1567 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1568 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1569 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1570 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1573 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1574 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1575 7848a0e1 2020-03-19 stsp goto done;
1578 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1579 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1580 7848a0e1 2020-03-19 stsp if (error)
1581 7848a0e1 2020-03-19 stsp goto done;
1583 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1584 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1585 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1586 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1587 7848a0e1 2020-03-19 stsp err(1, "pledge");
1589 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1590 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1591 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1592 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1593 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1594 7848a0e1 2020-03-19 stsp err(1, "pledge");
1596 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1597 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1598 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1599 7848a0e1 2020-03-19 stsp goto done;
1601 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1602 7848a0e1 2020-03-19 stsp goto done;
1605 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1606 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1607 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1608 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1609 7848a0e1 2020-03-19 stsp goto done;
1612 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1613 7848a0e1 2020-03-19 stsp if (error)
1614 7848a0e1 2020-03-19 stsp goto done;
1616 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1617 9c52365f 2020-03-21 stsp server_path, verbosity);
1618 7848a0e1 2020-03-19 stsp if (error)
1619 7848a0e1 2020-03-19 stsp goto done;
1621 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1622 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1623 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1625 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1626 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1627 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1628 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1629 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1630 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1631 2690194b 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo, fetch_progress, &fpa);
1632 7848a0e1 2020-03-19 stsp if (error)
1633 7848a0e1 2020-03-19 stsp goto done;
1635 41b0de12 2020-03-21 stsp if (list_refs_only) {
1636 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1637 41b0de12 2020-03-21 stsp goto done;
1640 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1641 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1642 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1643 f21ec2f0 2020-03-21 stsp if (delete_refs)
1644 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1645 7848a0e1 2020-03-19 stsp goto done;
1648 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1649 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1650 984065c8 2020-03-19 stsp if (error)
1651 984065c8 2020-03-19 stsp goto done;
1652 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1653 984065c8 2020-03-19 stsp free(id_str);
1654 984065c8 2020-03-19 stsp id_str = NULL;
1657 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1658 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1659 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1660 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1661 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1662 7848a0e1 2020-03-19 stsp char *remote_refname;
1664 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1665 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1666 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1667 7848a0e1 2020-03-19 stsp if (error) {
1668 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1669 7848a0e1 2020-03-19 stsp goto done;
1670 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1672 7848a0e1 2020-03-19 stsp if (error)
1673 7848a0e1 2020-03-19 stsp goto done;
1675 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1676 db6d8ad8 2020-03-21 stsp verbosity, repo);
1677 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1678 7848a0e1 2020-03-19 stsp if (error)
1679 7848a0e1 2020-03-19 stsp goto done;
1681 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1682 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1683 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1684 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1685 7848a0e1 2020-03-19 stsp goto done;
1688 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1689 7848a0e1 2020-03-19 stsp if (error) {
1690 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1691 7848a0e1 2020-03-19 stsp goto done;
1692 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1693 688f11b3 2020-03-21 stsp verbosity, repo);
1694 7848a0e1 2020-03-19 stsp if (error)
1695 7848a0e1 2020-03-19 stsp goto done;
1697 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1698 db6d8ad8 2020-03-21 stsp verbosity, repo);
1699 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1700 7848a0e1 2020-03-19 stsp if (error)
1701 7848a0e1 2020-03-19 stsp goto done;
1704 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1705 2ec30c80 2020-03-20 stsp error = got_ref_open(&ref, repo, refname, 0);
1706 2ec30c80 2020-03-20 stsp if (error) {
1707 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1708 2ec30c80 2020-03-20 stsp goto done;
1709 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1711 2ec30c80 2020-03-20 stsp if (error)
1712 2ec30c80 2020-03-20 stsp goto done;
1714 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1717 f21ec2f0 2020-03-21 stsp if (delete_refs)
1718 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1720 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1721 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1722 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1723 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1724 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1726 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1727 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1729 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1730 7848a0e1 2020-03-19 stsp if (worktree)
1731 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1732 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1733 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1734 7848a0e1 2020-03-19 stsp free(pe->data);
1736 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1737 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1738 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1739 7848a0e1 2020-03-19 stsp free(pe->data);
1741 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1742 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1743 7848a0e1 2020-03-19 stsp free(id_str);
1744 7848a0e1 2020-03-19 stsp free(cwd);
1745 7848a0e1 2020-03-19 stsp free(repo_path);
1746 7848a0e1 2020-03-19 stsp free(pack_hash);
1747 7848a0e1 2020-03-19 stsp free(proto);
1748 7848a0e1 2020-03-19 stsp free(host);
1749 7848a0e1 2020-03-19 stsp free(port);
1750 7848a0e1 2020-03-19 stsp free(server_path);
1751 7848a0e1 2020-03-19 stsp free(repo_name);
1752 7848a0e1 2020-03-19 stsp return error;
1756 7848a0e1 2020-03-19 stsp __dead static void
1757 2ab43947 2020-03-18 stsp usage_checkout(void)
1759 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1760 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1764 2ab43947 2020-03-18 stsp static void
1765 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1767 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1768 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1769 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1770 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1771 2ab43947 2020-03-18 stsp getprogname());
1774 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1775 2ab43947 2020-03-18 stsp const char *worktree_path;
1776 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1779 2ab43947 2020-03-18 stsp static const struct got_error *
1780 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1782 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1784 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1785 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1786 2ab43947 2020-03-18 stsp return NULL;
1788 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1789 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1790 2ab43947 2020-03-18 stsp return NULL;
1793 2ab43947 2020-03-18 stsp while (path[0] == '/')
1796 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1797 2ab43947 2020-03-18 stsp return NULL;
1800 2ab43947 2020-03-18 stsp static const struct got_error *
1801 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1803 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1804 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1805 2ab43947 2020-03-18 stsp return NULL;
1808 2ab43947 2020-03-18 stsp static const struct got_error *
1809 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1810 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1811 2ab43947 2020-03-18 stsp struct got_repository *repo)
1813 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1814 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1816 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1817 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1819 2ab43947 2020-03-18 stsp return err;
1821 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1822 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1825 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1826 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1828 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1830 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1838 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1839 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1840 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1842 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1843 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1844 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1845 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1846 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1847 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1849 2ab43947 2020-03-18 stsp free(yca_id);
1850 2ab43947 2020-03-18 stsp return NULL;
1853 2ab43947 2020-03-18 stsp static const struct got_error *
1854 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1855 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1856 2ab43947 2020-03-18 stsp struct got_repository *repo)
1858 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1859 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1860 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1861 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1863 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1865 2ab43947 2020-03-18 stsp goto done;
1867 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1868 2ab43947 2020-03-18 stsp is_same_branch = 1;
1869 2ab43947 2020-03-18 stsp goto done;
1871 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1872 2ab43947 2020-03-18 stsp is_same_branch = 1;
1873 2ab43947 2020-03-18 stsp goto done;
1876 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1878 2ab43947 2020-03-18 stsp goto done;
1880 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1881 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1883 2ab43947 2020-03-18 stsp goto done;
1885 2ab43947 2020-03-18 stsp for (;;) {
1886 2ab43947 2020-03-18 stsp struct got_object_id *id;
1887 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1888 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1889 2ab43947 2020-03-18 stsp if (err) {
1890 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1891 2ab43947 2020-03-18 stsp err = NULL;
1896 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1898 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1899 2ab43947 2020-03-18 stsp is_same_branch = 1;
1905 2ab43947 2020-03-18 stsp if (graph)
1906 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1907 2ab43947 2020-03-18 stsp free(head_commit_id);
1908 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1909 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1910 2ab43947 2020-03-18 stsp return err;
1913 4ed7e80c 2018-05-20 stsp static const struct got_error *
1914 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1916 4b6c9460 2020-03-05 stsp static char msg[512];
1917 4b6c9460 2020-03-05 stsp const char *branch_name;
1919 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1920 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1922 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1924 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1925 4b6c9460 2020-03-05 stsp branch_name += 11;
1927 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1928 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1929 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1930 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1931 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1932 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1934 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1937 4b6c9460 2020-03-05 stsp static const struct got_error *
1938 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1940 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1941 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1942 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1943 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1944 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1945 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1946 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1947 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1948 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1949 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1950 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1951 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1953 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1955 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1956 0bb8a95e 2018-03-12 stsp switch (ch) {
1958 08573d5b 2019-05-14 stsp branch_name = optarg;
1961 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1962 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1963 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1966 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1969 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1972 2deda0b9 2019-03-07 stsp usage_checkout();
1973 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1977 0bb8a95e 2018-03-12 stsp argc -= optind;
1978 0bb8a95e 2018-03-12 stsp argv += optind;
1980 6715a751 2018-03-16 stsp #ifndef PROFILE
1981 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1982 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1983 c09a553d 2018-03-12 stsp err(1, "pledge");
1985 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1986 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1987 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1988 76089277 2018-04-01 stsp if (repo_path == NULL)
1989 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1990 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1991 76089277 2018-04-01 stsp if (cwd == NULL) {
1992 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1993 76089277 2018-04-01 stsp goto done;
1995 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1996 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1997 230a42bd 2019-05-11 jcs if (base == NULL) {
1998 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1999 230a42bd 2019-05-11 jcs path_prefix);
2003 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2004 230a42bd 2019-05-11 jcs if (base == NULL) {
2005 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2006 230a42bd 2019-05-11 jcs repo_path);
2010 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2011 c09a553d 2018-03-12 stsp if (dotgit)
2012 c09a553d 2018-03-12 stsp *dotgit = '\0';
2013 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2014 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2015 c09a553d 2018-03-12 stsp free(cwd);
2016 76089277 2018-04-01 stsp goto done;
2018 c09a553d 2018-03-12 stsp free(cwd);
2019 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2020 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2021 76089277 2018-04-01 stsp if (repo_path == NULL) {
2022 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2023 76089277 2018-04-01 stsp goto done;
2025 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2026 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2027 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2028 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2030 b4b3a7dd 2019-07-22 stsp goto done;
2032 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2033 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2034 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2035 b4b3a7dd 2019-07-22 stsp goto done;
2039 c09a553d 2018-03-12 stsp usage_checkout();
2041 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2042 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2044 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2045 c09a553d 2018-03-12 stsp if (error != NULL)
2046 c02c541e 2019-03-29 stsp goto done;
2048 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2049 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2050 c530dc23 2019-07-23 stsp if (error) {
2051 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2052 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2053 c530dc23 2019-07-23 stsp goto done;
2054 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2055 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2056 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2057 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2058 c530dc23 2019-07-23 stsp goto done;
2062 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2063 c02c541e 2019-03-29 stsp if (error)
2064 c09a553d 2018-03-12 stsp goto done;
2066 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2067 c09a553d 2018-03-12 stsp if (error != NULL)
2068 c09a553d 2018-03-12 stsp goto done;
2070 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2071 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2072 c09a553d 2018-03-12 stsp goto done;
2074 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2075 c09a553d 2018-03-12 stsp if (error != NULL)
2076 c09a553d 2018-03-12 stsp goto done;
2078 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2079 e5dc7198 2018-12-29 stsp path_prefix);
2080 e5dc7198 2018-12-29 stsp if (error != NULL)
2081 e5dc7198 2018-12-29 stsp goto done;
2082 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2083 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2084 49520a32 2018-12-29 stsp goto done;
2087 8069f636 2019-01-12 stsp if (commit_id_str) {
2088 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2089 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2090 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2091 30837e32 2019-07-25 stsp if (error)
2092 8069f636 2019-01-12 stsp goto done;
2093 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2094 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2095 8069f636 2019-01-12 stsp if (error != NULL) {
2096 8069f636 2019-01-12 stsp free(commit_id);
2097 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2098 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2099 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2101 8069f636 2019-01-12 stsp goto done;
2103 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2104 4b6c9460 2020-03-05 stsp if (error) {
2105 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2106 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2107 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2109 45d344f6 2019-05-14 stsp goto done;
2111 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2112 8069f636 2019-01-12 stsp commit_id);
2113 8069f636 2019-01-12 stsp free(commit_id);
2114 8069f636 2019-01-12 stsp if (error)
2115 8069f636 2019-01-12 stsp goto done;
2118 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2119 f2ea84fa 2019-07-27 stsp if (error)
2120 f2ea84fa 2019-07-27 stsp goto done;
2121 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2122 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2123 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2124 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2125 c09a553d 2018-03-12 stsp if (error != NULL)
2126 c09a553d 2018-03-12 stsp goto done;
2128 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2129 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2130 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2132 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2133 8069f636 2019-01-12 stsp free(commit_id_str);
2134 76089277 2018-04-01 stsp free(repo_path);
2135 507dc3bb 2018-12-29 stsp free(worktree_path);
2136 507dc3bb 2018-12-29 stsp return error;
2139 507dc3bb 2018-12-29 stsp __dead static void
2140 507dc3bb 2018-12-29 stsp usage_update(void)
2142 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2143 507dc3bb 2018-12-29 stsp getprogname());
2147 1ee397ad 2019-07-12 stsp static const struct got_error *
2148 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2150 784955db 2019-01-12 stsp int *did_something = arg;
2152 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2153 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2154 1ee397ad 2019-07-12 stsp return NULL;
2156 1545c615 2019-02-10 stsp *did_something = 1;
2158 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2159 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2160 1ee397ad 2019-07-12 stsp return NULL;
2162 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2164 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2165 1ee397ad 2019-07-12 stsp return NULL;
2168 be7061eb 2018-12-30 stsp static const struct got_error *
2169 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2170 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2171 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2173 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2174 a1fb16d8 2019-05-24 stsp char *base_id_str;
2175 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2177 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2178 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2179 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2180 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2181 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2182 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2183 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2186 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2187 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2188 a1fb16d8 2019-05-24 stsp if (err) {
2189 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2190 a1fb16d8 2019-05-24 stsp return err;
2191 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2193 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2194 a1fb16d8 2019-05-24 stsp return NULL;
2196 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2197 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2198 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2200 a1fb16d8 2019-05-24 stsp return err;
2201 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2202 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2203 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2204 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2205 0ebf8283 2019-07-24 stsp return NULL;
2208 0ebf8283 2019-07-24 stsp static const struct got_error *
2209 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2211 0ebf8283 2019-07-24 stsp const struct got_error *err;
2212 0ebf8283 2019-07-24 stsp int in_progress;
2214 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2216 0ebf8283 2019-07-24 stsp return err;
2217 0ebf8283 2019-07-24 stsp if (in_progress)
2218 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2220 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2222 0ebf8283 2019-07-24 stsp return err;
2223 0ebf8283 2019-07-24 stsp if (in_progress)
2224 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2226 a1fb16d8 2019-05-24 stsp return NULL;
2229 a5edda0a 2019-07-27 stsp static const struct got_error *
2230 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2231 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2233 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2234 a5edda0a 2019-07-27 stsp char *path;
2237 a5edda0a 2019-07-27 stsp if (argc == 0) {
2238 a5edda0a 2019-07-27 stsp path = strdup("");
2239 a5edda0a 2019-07-27 stsp if (path == NULL)
2240 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2241 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2244 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2245 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2248 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2249 a5edda0a 2019-07-27 stsp if (err) {
2250 a5edda0a 2019-07-27 stsp free(path);
2255 a5edda0a 2019-07-27 stsp return err;
2258 a1fb16d8 2019-05-24 stsp static const struct got_error *
2259 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2261 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2262 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2263 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2264 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2265 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2266 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2267 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2268 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2269 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2270 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2271 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2273 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2275 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2276 507dc3bb 2018-12-29 stsp switch (ch) {
2278 024e9686 2019-05-14 stsp branch_name = optarg;
2281 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2282 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2283 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2286 2deda0b9 2019-03-07 stsp usage_update();
2287 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2291 507dc3bb 2018-12-29 stsp argc -= optind;
2292 507dc3bb 2018-12-29 stsp argv += optind;
2294 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2295 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2296 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2297 507dc3bb 2018-12-29 stsp err(1, "pledge");
2299 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2300 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2301 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2302 c4cdcb68 2019-04-03 stsp goto done;
2304 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2305 7d5807f4 2019-07-11 stsp if (error)
2306 7d5807f4 2019-07-11 stsp goto done;
2308 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2309 f2ea84fa 2019-07-27 stsp if (error)
2310 f2ea84fa 2019-07-27 stsp goto done;
2312 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2314 507dc3bb 2018-12-29 stsp if (error != NULL)
2315 507dc3bb 2018-12-29 stsp goto done;
2317 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2318 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2319 087fb88c 2019-08-04 stsp if (error)
2320 087fb88c 2019-08-04 stsp goto done;
2322 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2323 0266afb7 2019-01-04 stsp if (error)
2324 0266afb7 2019-01-04 stsp goto done;
2326 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2327 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2328 024e9686 2019-05-14 stsp if (error != NULL)
2329 024e9686 2019-05-14 stsp goto done;
2330 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2331 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2332 9c4b8182 2019-01-02 stsp if (error != NULL)
2333 9c4b8182 2019-01-02 stsp goto done;
2334 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2335 507dc3bb 2018-12-29 stsp if (error != NULL)
2336 507dc3bb 2018-12-29 stsp goto done;
2338 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2339 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2340 04f57cb3 2019-07-25 stsp free(commit_id_str);
2341 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2342 30837e32 2019-07-25 stsp if (error)
2343 a297e751 2019-07-11 stsp goto done;
2344 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2345 a297e751 2019-07-11 stsp if (error)
2346 507dc3bb 2018-12-29 stsp goto done;
2349 a1fb16d8 2019-05-24 stsp if (branch_name) {
2350 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2351 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2352 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2354 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2355 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2356 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2357 024e9686 2019-05-14 stsp goto done;
2359 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2360 024e9686 2019-05-14 stsp if (error)
2361 024e9686 2019-05-14 stsp goto done;
2362 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2364 a367ff0f 2019-05-14 stsp free(head_commit_id);
2365 024e9686 2019-05-14 stsp if (error != NULL)
2366 024e9686 2019-05-14 stsp goto done;
2367 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2368 a367ff0f 2019-05-14 stsp if (error)
2369 a367ff0f 2019-05-14 stsp goto done;
2370 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2371 024e9686 2019-05-14 stsp if (error)
2372 024e9686 2019-05-14 stsp goto done;
2374 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2375 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2376 a367ff0f 2019-05-14 stsp if (error != NULL) {
2377 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2378 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2379 024e9686 2019-05-14 stsp goto done;
2381 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2382 a367ff0f 2019-05-14 stsp if (error)
2383 a367ff0f 2019-05-14 stsp goto done;
2386 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2387 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2388 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2389 507dc3bb 2018-12-29 stsp commit_id);
2390 507dc3bb 2018-12-29 stsp if (error)
2391 507dc3bb 2018-12-29 stsp goto done;
2394 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2395 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2396 507dc3bb 2018-12-29 stsp if (error != NULL)
2397 507dc3bb 2018-12-29 stsp goto done;
2399 784955db 2019-01-12 stsp if (did_something)
2400 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2402 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2404 c09a553d 2018-03-12 stsp free(worktree_path);
2405 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2406 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2407 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2408 507dc3bb 2018-12-29 stsp free(commit_id);
2409 9c4b8182 2019-01-02 stsp free(commit_id_str);
2410 c09a553d 2018-03-12 stsp return error;
2413 f42b1b34 2018-03-12 stsp static const struct got_error *
2414 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2415 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2416 63035f9f 2019-10-06 stsp struct got_repository *repo)
2418 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2419 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2421 44392932 2019-08-25 stsp if (blob_id1) {
2422 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2424 44392932 2019-08-25 stsp goto done;
2427 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2429 44392932 2019-08-25 stsp goto done;
2431 44392932 2019-08-25 stsp while (path[0] == '/')
2433 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2434 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2436 44392932 2019-08-25 stsp if (blob1)
2437 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2438 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2439 44392932 2019-08-25 stsp return err;
2442 44392932 2019-08-25 stsp static const struct got_error *
2443 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2444 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2445 63035f9f 2019-10-06 stsp struct got_repository *repo)
2447 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2448 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2449 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2451 44392932 2019-08-25 stsp if (tree_id1) {
2452 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2454 44392932 2019-08-25 stsp goto done;
2457 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2459 0f2b3dca 2018-12-22 stsp goto done;
2461 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2462 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2463 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2464 44392932 2019-08-25 stsp while (path[0] == '/')
2466 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2467 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2469 79109fed 2018-03-27 stsp if (tree1)
2470 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2471 366e0a5f 2019-10-10 stsp if (tree2)
2472 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2473 44392932 2019-08-25 stsp return err;
2476 44392932 2019-08-25 stsp static const struct got_error *
2477 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2478 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2480 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2481 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2482 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2483 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2484 44392932 2019-08-25 stsp struct got_object_qid *qid;
2486 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2487 44392932 2019-08-25 stsp if (qid != NULL) {
2488 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2491 44392932 2019-08-25 stsp return err;
2494 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2495 44392932 2019-08-25 stsp int obj_type;
2496 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2498 44392932 2019-08-25 stsp goto done;
2499 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2500 44392932 2019-08-25 stsp if (err) {
2501 44392932 2019-08-25 stsp free(obj_id2);
2502 44392932 2019-08-25 stsp goto done;
2504 44392932 2019-08-25 stsp if (pcommit) {
2505 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2506 44392932 2019-08-25 stsp qid->id, path);
2507 44392932 2019-08-25 stsp if (err) {
2508 44392932 2019-08-25 stsp free(obj_id2);
2509 44392932 2019-08-25 stsp goto done;
2511 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2512 44392932 2019-08-25 stsp if (err) {
2513 44392932 2019-08-25 stsp free(obj_id2);
2514 44392932 2019-08-25 stsp goto done;
2517 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2518 44392932 2019-08-25 stsp if (err) {
2519 44392932 2019-08-25 stsp free(obj_id2);
2520 44392932 2019-08-25 stsp goto done;
2522 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2523 44392932 2019-08-25 stsp switch (obj_type) {
2524 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2525 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2528 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2529 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2533 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2536 44392932 2019-08-25 stsp free(obj_id1);
2537 44392932 2019-08-25 stsp free(obj_id2);
2539 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2540 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2542 44392932 2019-08-25 stsp goto done;
2543 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2544 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2546 44392932 2019-08-25 stsp goto done;
2547 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2548 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2551 0f2b3dca 2018-12-22 stsp free(id_str1);
2552 0f2b3dca 2018-12-22 stsp free(id_str2);
2553 44392932 2019-08-25 stsp if (pcommit)
2554 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2555 79109fed 2018-03-27 stsp return err;
2558 4bb494d5 2018-06-16 stsp static char *
2559 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2561 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2562 09867e48 2019-08-13 stsp char *p, *s;
2564 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2565 09867e48 2019-08-13 stsp if (tm == NULL)
2566 09867e48 2019-08-13 stsp return NULL;
2567 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2568 09867e48 2019-08-13 stsp if (s == NULL)
2569 09867e48 2019-08-13 stsp return NULL;
2570 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2572 6c281f94 2018-06-11 stsp *p = '\0';
2576 6841bf13 2019-11-29 kn static const struct got_error *
2577 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2578 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2580 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2581 6841bf13 2019-11-29 kn regmatch_t regmatch;
2582 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2584 6841bf13 2019-11-29 kn *have_match = 0;
2586 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2590 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2594 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2595 6841bf13 2019-11-29 kn *have_match = 1;
2597 6841bf13 2019-11-29 kn free(id_str);
2598 6841bf13 2019-11-29 kn free(logmsg);
2602 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2604 79109fed 2018-03-27 stsp static const struct got_error *
2605 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2606 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2607 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2609 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2610 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2611 6c281f94 2018-06-11 stsp char datebuf[26];
2612 45d799e2 2018-12-23 stsp time_t committer_time;
2613 45d799e2 2018-12-23 stsp const char *author, *committer;
2614 199a4027 2019-02-02 stsp char *refs_str = NULL;
2615 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2617 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2619 199a4027 2019-02-02 stsp const char *name;
2620 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2623 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2624 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2626 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2627 199a4027 2019-02-02 stsp name += 5;
2628 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2630 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2631 e34f9ed6 2019-02-02 stsp name += 6;
2632 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2633 141c2bff 2019-02-04 stsp name += 8;
2634 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2635 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2636 5d844a1e 2019-08-13 stsp if (err) {
2637 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2638 5d844a1e 2019-08-13 stsp return err;
2639 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2640 5d844a1e 2019-08-13 stsp err = NULL;
2641 5d844a1e 2019-08-13 stsp tag = NULL;
2644 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2645 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2647 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2648 a436ad14 2019-08-13 stsp if (cmp != 0)
2650 199a4027 2019-02-02 stsp s = refs_str;
2651 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2652 199a4027 2019-02-02 stsp name) == -1) {
2653 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2655 34ca4898 2019-08-13 stsp return err;
2659 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2661 8bf5b3c9 2018-03-17 stsp return err;
2663 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2664 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2665 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2666 832c249c 2018-06-10 stsp free(id_str);
2667 d3d493d7 2019-02-21 stsp id_str = NULL;
2668 d3d493d7 2019-02-21 stsp free(refs_str);
2669 d3d493d7 2019-02-21 stsp refs_str = NULL;
2670 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2671 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2672 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2673 09867e48 2019-08-13 stsp if (datestr)
2674 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2675 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2676 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2677 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2678 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2679 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2680 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2681 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2682 3fe1abad 2018-06-10 stsp int n = 1;
2683 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2684 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2685 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2687 a0603db2 2018-06-10 stsp return err;
2688 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2689 a0603db2 2018-06-10 stsp free(id_str);
2693 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2695 5943eee2 2019-08-13 stsp return err;
2697 621015ac 2018-07-23 stsp logmsg = logmsg0;
2699 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2701 832c249c 2018-06-10 stsp printf(" %s\n", line);
2702 832c249c 2018-06-10 stsp } while (line);
2703 621015ac 2018-07-23 stsp free(logmsg0);
2705 971751ac 2018-03-27 stsp if (show_patch) {
2706 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2707 971751ac 2018-03-27 stsp if (err == 0)
2708 971751ac 2018-03-27 stsp printf("\n");
2711 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2712 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2713 07862c20 2018-09-15 stsp return err;
2716 f42b1b34 2018-03-12 stsp static const struct got_error *
2717 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2718 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2719 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2720 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2722 f42b1b34 2018-03-12 stsp const struct got_error *err;
2723 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2724 6841bf13 2019-11-29 kn regex_t regex;
2725 6841bf13 2019-11-29 kn int have_match;
2727 6841bf13 2019-11-29 kn if (search_pattern &&
2728 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2729 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2731 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2733 f42b1b34 2018-03-12 stsp return err;
2734 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2735 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2737 fcc85cad 2018-07-22 stsp goto done;
2739 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2740 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2742 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2745 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2746 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2747 372ccdbb 2018-06-10 stsp if (err) {
2748 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2749 9ba79e04 2018-06-11 stsp err = NULL;
2752 b43fbaa0 2018-06-11 stsp if (id == NULL)
2755 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2759 6841bf13 2019-11-29 kn if (search_pattern) {
2760 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
2762 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2765 6841bf13 2019-11-29 kn if (have_match == 0) {
2766 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2771 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2772 44392932 2019-08-25 stsp diff_context, refs);
2773 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2774 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2778 6841bf13 2019-11-29 kn if (search_pattern)
2779 6841bf13 2019-11-29 kn regfree(®ex);
2780 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2781 f42b1b34 2018-03-12 stsp return err;
2784 4ed7e80c 2018-05-20 stsp __dead static void
2785 6f3d1eb0 2018-03-12 stsp usage_log(void)
2787 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2788 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2792 b1ebc001 2019-08-13 stsp static int
2793 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2795 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2796 b1ebc001 2019-08-13 stsp long long n;
2797 b1ebc001 2019-08-13 stsp const char *errstr;
2799 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2800 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2802 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2803 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2808 4ed7e80c 2018-05-20 stsp static const struct got_error *
2809 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2811 f42b1b34 2018-03-12 stsp const struct got_error *error;
2812 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2813 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2814 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2815 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2816 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2817 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2818 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2819 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2820 64a96a6d 2018-04-01 stsp const char *errstr;
2821 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2823 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2825 6715a751 2018-03-16 stsp #ifndef PROFILE
2826 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2829 f42b1b34 2018-03-12 stsp err(1, "pledge");
2832 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2834 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2835 79109fed 2018-03-27 stsp switch (ch) {
2837 79109fed 2018-03-27 stsp show_patch = 1;
2840 d142fc45 2018-04-01 stsp start_commit = optarg;
2843 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2845 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2846 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2849 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2850 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2851 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2854 48c8c60d 2020-01-27 stsp log_branches = 1;
2857 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2858 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2859 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2861 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2864 6841bf13 2019-11-29 kn search_pattern = optarg;
2867 2deda0b9 2019-03-07 stsp usage_log();
2868 79109fed 2018-03-27 stsp /* NOTREACHED */
2872 79109fed 2018-03-27 stsp argc -= optind;
2873 79109fed 2018-03-27 stsp argv += optind;
2875 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2876 dc1edbfa 2019-11-29 kn diff_context = 3;
2877 dc1edbfa 2019-11-29 kn else if (!show_patch)
2878 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2880 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2881 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2882 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2883 04ca23f4 2018-07-16 stsp goto done;
2886 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2887 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2888 cffc0aa4 2019-02-05 stsp goto done;
2889 cffc0aa4 2019-02-05 stsp error = NULL;
2891 e7301579 2019-03-18 stsp if (argc == 0) {
2892 cbd1af7a 2019-03-18 stsp path = strdup("");
2893 e7301579 2019-03-18 stsp if (path == NULL) {
2894 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2895 cbd1af7a 2019-03-18 stsp goto done;
2897 e7301579 2019-03-18 stsp } else if (argc == 1) {
2898 e7301579 2019-03-18 stsp if (worktree) {
2899 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2901 e7301579 2019-03-18 stsp if (error)
2902 e7301579 2019-03-18 stsp goto done;
2904 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2905 e7301579 2019-03-18 stsp if (path == NULL) {
2906 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2907 e7301579 2019-03-18 stsp goto done;
2911 cbd1af7a 2019-03-18 stsp usage_log();
2913 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2914 5486daa2 2019-05-11 stsp repo_path = worktree ?
2915 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2917 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2918 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2919 cffc0aa4 2019-02-05 stsp goto done;
2922 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2923 d7d4f210 2018-03-12 stsp if (error != NULL)
2924 04ca23f4 2018-07-16 stsp goto done;
2926 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2927 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2928 c02c541e 2019-03-29 stsp if (error)
2929 c02c541e 2019-03-29 stsp goto done;
2931 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2932 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2933 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2934 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2935 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2936 3235492e 2018-04-01 stsp if (error != NULL)
2937 3235492e 2018-04-01 stsp return error;
2938 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2939 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2940 3235492e 2018-04-01 stsp if (error != NULL)
2941 3235492e 2018-04-01 stsp return error;
2942 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2944 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2945 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2946 3235492e 2018-04-01 stsp if (error == NULL) {
2947 1caad61b 2019-02-01 stsp int obj_type;
2948 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2949 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2950 d1f2edc9 2018-06-13 stsp if (error != NULL)
2951 1caad61b 2019-02-01 stsp goto done;
2952 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2953 1caad61b 2019-02-01 stsp if (error != NULL)
2954 1caad61b 2019-02-01 stsp goto done;
2955 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2956 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2957 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2958 1caad61b 2019-02-01 stsp if (error != NULL)
2959 1caad61b 2019-02-01 stsp goto done;
2960 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2961 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2962 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2963 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2964 1caad61b 2019-02-01 stsp goto done;
2967 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2968 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2969 1caad61b 2019-02-01 stsp if (id == NULL)
2970 638f9024 2019-05-13 stsp error = got_error_from_errno(
2971 230a42bd 2019-05-11 jcs "got_object_id_dup");
2972 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2973 1caad61b 2019-02-01 stsp if (error)
2974 1caad61b 2019-02-01 stsp goto done;
2975 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2976 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2977 1caad61b 2019-02-01 stsp goto done;
2979 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2980 d1f2edc9 2018-06-13 stsp if (error != NULL)
2981 1caad61b 2019-02-01 stsp goto done;
2983 15a94983 2018-12-23 stsp if (commit == NULL) {
2984 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2985 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2986 d1f2edc9 2018-06-13 stsp if (error != NULL)
2987 d1f2edc9 2018-06-13 stsp return error;
2990 d7d4f210 2018-03-12 stsp if (error != NULL)
2991 04ca23f4 2018-07-16 stsp goto done;
2993 deeabeae 2019-08-27 stsp if (worktree) {
2994 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2996 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2997 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2998 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2999 deeabeae 2019-08-27 stsp goto done;
3001 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3004 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3005 04ca23f4 2018-07-16 stsp if (error != NULL)
3006 04ca23f4 2018-07-16 stsp goto done;
3007 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3008 04ca23f4 2018-07-16 stsp free(path);
3009 04ca23f4 2018-07-16 stsp path = in_repo_path;
3012 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3013 199a4027 2019-02-02 stsp if (error)
3014 199a4027 2019-02-02 stsp goto done;
3016 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3017 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3019 04ca23f4 2018-07-16 stsp free(path);
3020 04ca23f4 2018-07-16 stsp free(repo_path);
3021 04ca23f4 2018-07-16 stsp free(cwd);
3023 cffc0aa4 2019-02-05 stsp if (worktree)
3024 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3025 ad242220 2018-09-08 stsp if (repo) {
3026 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3027 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3028 ad242220 2018-09-08 stsp if (error == NULL)
3029 ad242220 2018-09-08 stsp error = repo_error;
3031 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3032 8bf5b3c9 2018-03-17 stsp return error;
3035 4ed7e80c 2018-05-20 stsp __dead static void
3036 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3038 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3039 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3043 b72f483a 2019-02-05 stsp struct print_diff_arg {
3044 b72f483a 2019-02-05 stsp struct got_repository *repo;
3045 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3046 b72f483a 2019-02-05 stsp int diff_context;
3047 3fc0c068 2019-02-10 stsp const char *id_str;
3048 3fc0c068 2019-02-10 stsp int header_shown;
3049 98eaaa12 2019-08-03 stsp int diff_staged;
3050 63035f9f 2019-10-06 stsp int ignore_whitespace;
3053 4ed7e80c 2018-05-20 stsp static const struct got_error *
3054 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3055 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3056 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3057 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3059 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3060 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3061 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3062 12463d8b 2019-12-13 stsp int fd = -1;
3063 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3064 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3065 b72f483a 2019-02-05 stsp struct stat sb;
3067 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3068 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3069 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3070 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3071 98eaaa12 2019-08-03 stsp return NULL;
3073 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3074 98eaaa12 2019-08-03 stsp return NULL;
3075 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3076 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3077 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3078 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3079 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3080 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3081 98eaaa12 2019-08-03 stsp return NULL;
3084 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3085 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3086 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3087 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3088 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3091 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3092 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3093 98eaaa12 2019-08-03 stsp switch (staged_status) {
3094 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3095 98eaaa12 2019-08-03 stsp label1 = path;
3096 98eaaa12 2019-08-03 stsp label2 = path;
3098 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3099 98eaaa12 2019-08-03 stsp label2 = path;
3101 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3102 98eaaa12 2019-08-03 stsp label1 = path;
3105 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3107 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3108 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3109 63035f9f 2019-10-06 stsp a->repo, stdout);
3112 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3113 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3114 4ce46740 2019-08-08 stsp char *id_str;
3115 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3118 4ce46740 2019-08-08 stsp goto done;
3119 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3121 4ce46740 2019-08-08 stsp goto done;
3122 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3123 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3124 4ce46740 2019-08-08 stsp free(id_str);
3125 4ce46740 2019-08-08 stsp goto done;
3127 4ce46740 2019-08-08 stsp free(id_str);
3128 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3129 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3131 4ce46740 2019-08-08 stsp goto done;
3134 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3135 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3136 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3137 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3138 2ec1f75b 2019-03-26 stsp goto done;
3141 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3142 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3143 12463d8b 2019-12-13 stsp if (fd == -1) {
3144 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3145 12463d8b 2019-12-13 stsp goto done;
3148 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3149 12463d8b 2019-12-13 stsp if (fd == -1) {
3150 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3151 12463d8b 2019-12-13 stsp goto done;
3154 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3155 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3156 2ec1f75b 2019-03-26 stsp goto done;
3158 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3159 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3160 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3161 2ec1f75b 2019-03-26 stsp goto done;
3165 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3167 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3168 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3170 b72f483a 2019-02-05 stsp if (blob1)
3171 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3172 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3173 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3174 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3175 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3176 b72f483a 2019-02-05 stsp free(abspath);
3177 927df6b7 2019-02-10 stsp return err;
3180 927df6b7 2019-02-10 stsp static const struct got_error *
3181 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3183 b00d56cd 2018-04-01 stsp const struct got_error *error;
3184 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3185 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3186 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3187 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3188 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3189 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3190 15a94983 2018-12-23 stsp int type1, type2;
3191 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3192 c0cc5c62 2018-10-18 stsp const char *errstr;
3193 927df6b7 2019-02-10 stsp char *path = NULL;
3195 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3196 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3197 25eccc22 2019-01-04 stsp NULL) == -1)
3198 b00d56cd 2018-04-01 stsp err(1, "pledge");
3201 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3202 b00d56cd 2018-04-01 stsp switch (ch) {
3204 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3206 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3207 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3210 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3211 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3212 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3214 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3217 98eaaa12 2019-08-03 stsp diff_staged = 1;
3220 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3223 2deda0b9 2019-03-07 stsp usage_diff();
3224 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3228 b00d56cd 2018-04-01 stsp argc -= optind;
3229 b00d56cd 2018-04-01 stsp argv += optind;
3231 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3232 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3233 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3234 b72f483a 2019-02-05 stsp goto done;
3236 927df6b7 2019-02-10 stsp if (argc <= 1) {
3237 b72f483a 2019-02-05 stsp if (repo_path)
3239 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3240 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3241 1f03b8da 2020-03-20 stsp if (error)
3242 1f03b8da 2020-03-20 stsp goto done;
3243 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3244 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3245 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3246 927df6b7 2019-02-10 stsp goto done;
3248 927df6b7 2019-02-10 stsp if (argc == 1) {
3249 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3251 927df6b7 2019-02-10 stsp if (error)
3252 927df6b7 2019-02-10 stsp goto done;
3254 927df6b7 2019-02-10 stsp path = strdup("");
3255 927df6b7 2019-02-10 stsp if (path == NULL) {
3256 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3257 927df6b7 2019-02-10 stsp goto done;
3260 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3261 98eaaa12 2019-08-03 stsp if (diff_staged)
3262 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3263 98eaaa12 2019-08-03 stsp "objects in repository");
3264 15a94983 2018-12-23 stsp id_str1 = argv[0];
3265 15a94983 2018-12-23 stsp id_str2 = argv[1];
3266 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3267 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3268 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3269 30db809c 2019-06-05 stsp goto done;
3270 1f03b8da 2020-03-20 stsp if (worktree) {
3271 1f03b8da 2020-03-20 stsp repo_path = strdup(
3272 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3273 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3274 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3275 1f03b8da 2020-03-20 stsp goto done;
3278 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3279 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3280 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3281 1f03b8da 2020-03-20 stsp goto done;
3286 b00d56cd 2018-04-01 stsp usage_diff();
3288 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3289 76089277 2018-04-01 stsp free(repo_path);
3290 b00d56cd 2018-04-01 stsp if (error != NULL)
3291 b00d56cd 2018-04-01 stsp goto done;
3293 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3294 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3295 c02c541e 2019-03-29 stsp if (error)
3296 c02c541e 2019-03-29 stsp goto done;
3298 30db809c 2019-06-05 stsp if (argc <= 1) {
3299 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3300 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3301 b72f483a 2019-02-05 stsp char *id_str;
3303 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3305 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3306 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3307 b72f483a 2019-02-05 stsp if (error)
3308 b72f483a 2019-02-05 stsp goto done;
3309 b72f483a 2019-02-05 stsp arg.repo = repo;
3310 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3311 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3312 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3313 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3314 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3315 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3317 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3318 72ea6654 2019-07-27 stsp if (error)
3319 72ea6654 2019-07-27 stsp goto done;
3321 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3322 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3323 b72f483a 2019-02-05 stsp free(id_str);
3324 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3325 b72f483a 2019-02-05 stsp goto done;
3328 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3329 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3330 0adb7196 2019-08-11 stsp if (error)
3331 0adb7196 2019-08-11 stsp goto done;
3333 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3334 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3335 0adb7196 2019-08-11 stsp if (error)
3336 0adb7196 2019-08-11 stsp goto done;
3338 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3339 15a94983 2018-12-23 stsp if (error)
3340 15a94983 2018-12-23 stsp goto done;
3342 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3343 15a94983 2018-12-23 stsp if (error)
3344 15a94983 2018-12-23 stsp goto done;
3346 15a94983 2018-12-23 stsp if (type1 != type2) {
3347 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3348 b00d56cd 2018-04-01 stsp goto done;
3351 15a94983 2018-12-23 stsp switch (type1) {
3352 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3353 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3354 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3356 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3357 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3358 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3360 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3361 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3362 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3363 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3366 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3369 5e70831e 2019-05-28 stsp free(label1);
3370 5e70831e 2019-05-28 stsp free(label2);
3371 15a94983 2018-12-23 stsp free(id1);
3372 15a94983 2018-12-23 stsp free(id2);
3373 927df6b7 2019-02-10 stsp free(path);
3374 b72f483a 2019-02-05 stsp if (worktree)
3375 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3376 ad242220 2018-09-08 stsp if (repo) {
3377 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3378 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3379 ad242220 2018-09-08 stsp if (error == NULL)
3380 ad242220 2018-09-08 stsp error = repo_error;
3382 b00d56cd 2018-04-01 stsp return error;
3385 404c43c4 2018-06-21 stsp __dead static void
3386 404c43c4 2018-06-21 stsp usage_blame(void)
3388 9270e621 2019-02-05 stsp fprintf(stderr,
3389 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3390 404c43c4 2018-06-21 stsp getprogname());
3394 28315671 2019-08-14 stsp struct blame_line {
3395 28315671 2019-08-14 stsp int annotated;
3396 28315671 2019-08-14 stsp char *id_str;
3397 82f6abb8 2019-08-14 stsp char *committer;
3398 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3401 28315671 2019-08-14 stsp struct blame_cb_args {
3402 28315671 2019-08-14 stsp struct blame_line *lines;
3403 28315671 2019-08-14 stsp int nlines;
3404 7ef28ff8 2019-08-14 stsp int nlines_prec;
3405 28315671 2019-08-14 stsp int lineno_cur;
3406 28315671 2019-08-14 stsp off_t *line_offsets;
3408 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3411 404c43c4 2018-06-21 stsp static const struct got_error *
3412 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3414 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3415 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3416 28315671 2019-08-14 stsp struct blame_line *bline;
3417 82f6abb8 2019-08-14 stsp char *line = NULL;
3418 28315671 2019-08-14 stsp size_t linesize = 0;
3419 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3420 28315671 2019-08-14 stsp off_t offset;
3421 bcb49d15 2019-08-14 stsp struct tm tm;
3422 bcb49d15 2019-08-14 stsp time_t committer_time;
3424 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3425 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3426 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3428 28315671 2019-08-14 stsp if (sigint_received)
3429 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3431 2839f8b9 2019-08-18 stsp if (lineno == -1)
3432 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3434 28315671 2019-08-14 stsp /* Annotate this line. */
3435 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3436 28315671 2019-08-14 stsp if (bline->annotated)
3437 28315671 2019-08-14 stsp return NULL;
3438 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3440 28315671 2019-08-14 stsp return err;
3442 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3444 82f6abb8 2019-08-14 stsp goto done;
3446 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3447 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3448 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3449 bcb49d15 2019-08-14 stsp goto done;
3452 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3453 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3454 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3455 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3456 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3457 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3458 82f6abb8 2019-08-14 stsp goto done;
3460 28315671 2019-08-14 stsp bline->annotated = 1;
3462 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3463 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3464 28315671 2019-08-14 stsp if (!bline->annotated)
3465 82f6abb8 2019-08-14 stsp goto done;
3467 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3468 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3469 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3470 82f6abb8 2019-08-14 stsp goto done;
3473 28315671 2019-08-14 stsp while (bline->annotated) {
3474 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3475 82f6abb8 2019-08-14 stsp size_t len;
3477 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3478 28315671 2019-08-14 stsp if (ferror(a->f))
3479 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3483 82f6abb8 2019-08-14 stsp committer = bline->committer;
3484 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3485 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3486 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3487 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3489 82f6abb8 2019-08-14 stsp *at = '\0';
3490 82f6abb8 2019-08-14 stsp len = strlen(committer);
3491 82f6abb8 2019-08-14 stsp if (len >= 9)
3492 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3494 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3496 28315671 2019-08-14 stsp *nl = '\0';
3497 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3498 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3500 28315671 2019-08-14 stsp a->lineno_cur++;
3501 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3504 82f6abb8 2019-08-14 stsp if (commit)
3505 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3506 28315671 2019-08-14 stsp free(line);
3507 28315671 2019-08-14 stsp return err;
3510 28315671 2019-08-14 stsp static const struct got_error *
3511 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3513 404c43c4 2018-06-21 stsp const struct got_error *error;
3514 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3515 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3516 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3517 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3518 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3519 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3520 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3521 28315671 2019-08-14 stsp struct blame_cb_args bca;
3522 28315671 2019-08-14 stsp int ch, obj_type, i;
3523 b02560ec 2019-08-19 stsp size_t filesize;
3525 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3527 404c43c4 2018-06-21 stsp #ifndef PROFILE
3528 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3529 36e2fb66 2019-01-04 stsp NULL) == -1)
3530 404c43c4 2018-06-21 stsp err(1, "pledge");
3533 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3534 404c43c4 2018-06-21 stsp switch (ch) {
3536 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3539 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3540 66bea077 2018-08-02 stsp if (repo_path == NULL)
3541 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3543 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3546 2deda0b9 2019-03-07 stsp usage_blame();
3547 404c43c4 2018-06-21 stsp /* NOTREACHED */
3551 404c43c4 2018-06-21 stsp argc -= optind;
3552 404c43c4 2018-06-21 stsp argv += optind;
3554 a39318fd 2018-08-02 stsp if (argc == 1)
3555 404c43c4 2018-06-21 stsp path = argv[0];
3557 404c43c4 2018-06-21 stsp usage_blame();
3559 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3560 66bea077 2018-08-02 stsp if (cwd == NULL) {
3561 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3562 66bea077 2018-08-02 stsp goto done;
3564 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3565 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3566 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3567 66bea077 2018-08-02 stsp goto done;
3569 0c06baac 2019-02-05 stsp error = NULL;
3570 0c06baac 2019-02-05 stsp if (worktree) {
3571 0c06baac 2019-02-05 stsp repo_path =
3572 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3573 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3574 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3575 1f03b8da 2020-03-20 stsp if (error)
3576 1f03b8da 2020-03-20 stsp goto done;
3579 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3580 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3581 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3582 0c06baac 2019-02-05 stsp goto done;
3587 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3588 c02c541e 2019-03-29 stsp if (error != NULL)
3589 36e2fb66 2019-01-04 stsp goto done;
3591 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3592 c02c541e 2019-03-29 stsp if (error)
3593 404c43c4 2018-06-21 stsp goto done;
3595 0c06baac 2019-02-05 stsp if (worktree) {
3596 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3597 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3598 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3599 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3600 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3601 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3602 6efaaa2d 2019-02-05 stsp path) == -1) {
3603 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3604 0c06baac 2019-02-05 stsp goto done;
3606 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3609 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3611 0c06baac 2019-02-05 stsp if (error)
3612 66bea077 2018-08-02 stsp goto done;
3614 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3615 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3616 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3617 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3618 404c43c4 2018-06-21 stsp if (error != NULL)
3619 66bea077 2018-08-02 stsp goto done;
3620 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3621 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3622 404c43c4 2018-06-21 stsp if (error != NULL)
3623 66bea077 2018-08-02 stsp goto done;
3625 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3626 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3627 30837e32 2019-07-25 stsp if (error)
3628 66bea077 2018-08-02 stsp goto done;
3631 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3632 28315671 2019-08-14 stsp if (error)
3633 28315671 2019-08-14 stsp goto done;
3635 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3636 28315671 2019-08-14 stsp if (error)
3637 28315671 2019-08-14 stsp goto done;
3639 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3640 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3641 28315671 2019-08-14 stsp goto done;
3644 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3645 28315671 2019-08-14 stsp if (error)
3646 28315671 2019-08-14 stsp goto done;
3647 28315671 2019-08-14 stsp bca.f = got_opentemp();
3648 28315671 2019-08-14 stsp if (bca.f == NULL) {
3649 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3650 28315671 2019-08-14 stsp goto done;
3652 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3653 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3654 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3655 28315671 2019-08-14 stsp goto done;
3657 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3658 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3659 b02560ec 2019-08-19 stsp bca.nlines--;
3661 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3662 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3663 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3664 28315671 2019-08-14 stsp goto done;
3666 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3667 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3668 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3669 7ef28ff8 2019-08-14 stsp while (i > 0) {
3671 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3673 82f6abb8 2019-08-14 stsp bca.repo = repo;
3675 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3676 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3678 66bea077 2018-08-02 stsp free(in_repo_path);
3679 66bea077 2018-08-02 stsp free(repo_path);
3680 66bea077 2018-08-02 stsp free(cwd);
3681 404c43c4 2018-06-21 stsp free(commit_id);
3682 28315671 2019-08-14 stsp free(obj_id);
3684 28315671 2019-08-14 stsp got_object_blob_close(blob);
3685 0c06baac 2019-02-05 stsp if (worktree)
3686 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3687 ad242220 2018-09-08 stsp if (repo) {
3688 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3689 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3690 ad242220 2018-09-08 stsp if (error == NULL)
3691 ad242220 2018-09-08 stsp error = repo_error;
3693 3affba96 2019-09-22 stsp if (bca.lines) {
3694 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3695 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3696 3affba96 2019-09-22 stsp free(bline->id_str);
3697 3affba96 2019-09-22 stsp free(bline->committer);
3699 3affba96 2019-09-22 stsp free(bca.lines);
3701 28315671 2019-08-14 stsp free(bca.line_offsets);
3702 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3703 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3704 404c43c4 2018-06-21 stsp return error;
3707 5de5890b 2018-10-18 stsp __dead static void
3708 5de5890b 2018-10-18 stsp usage_tree(void)
3710 c1669e2e 2019-01-09 stsp fprintf(stderr,
3711 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3712 5de5890b 2018-10-18 stsp getprogname());
3716 c1669e2e 2019-01-09 stsp static void
3717 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3718 c1669e2e 2019-01-09 stsp const char *root_path)
3720 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3721 848d6979 2019-08-12 stsp const char *modestr = "";
3722 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3724 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3725 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3728 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3729 63c5ca5d 2019-08-24 stsp modestr = "$";
3730 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3731 848d6979 2019-08-12 stsp modestr = "@";
3732 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3733 848d6979 2019-08-12 stsp modestr = "/";
3734 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3735 848d6979 2019-08-12 stsp modestr = "*";
3737 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3738 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3741 5de5890b 2018-10-18 stsp static const struct got_error *
3742 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3743 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3744 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3746 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3747 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3748 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3749 56e0773d 2019-11-28 stsp int nentries, i;
3751 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3753 5de5890b 2018-10-18 stsp goto done;
3755 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3757 5de5890b 2018-10-18 stsp goto done;
3758 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3759 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3760 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3761 5de5890b 2018-10-18 stsp char *id = NULL;
3763 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3766 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3767 5de5890b 2018-10-18 stsp if (show_ids) {
3768 5de5890b 2018-10-18 stsp char *id_str;
3769 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3770 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3772 5de5890b 2018-10-18 stsp goto done;
3773 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3774 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3775 5de5890b 2018-10-18 stsp free(id_str);
3776 5de5890b 2018-10-18 stsp goto done;
3778 5de5890b 2018-10-18 stsp free(id_str);
3780 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3783 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3784 c1669e2e 2019-01-09 stsp char *child_path;
3785 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3786 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3787 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3788 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3789 c1669e2e 2019-01-09 stsp goto done;
3791 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3792 c1669e2e 2019-01-09 stsp root_path, repo);
3793 c1669e2e 2019-01-09 stsp free(child_path);
3795 c1669e2e 2019-01-09 stsp goto done;
3800 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3801 5de5890b 2018-10-18 stsp free(tree_id);
3802 5de5890b 2018-10-18 stsp return err;
3805 5de5890b 2018-10-18 stsp static const struct got_error *
3806 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3808 5de5890b 2018-10-18 stsp const struct got_error *error;
3809 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3810 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3811 7a2c19d6 2019-02-05 stsp const char *path;
3812 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3813 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3814 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3815 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3818 5de5890b 2018-10-18 stsp #ifndef PROFILE
3819 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3820 0f8d269b 2019-01-04 stsp NULL) == -1)
3821 5de5890b 2018-10-18 stsp err(1, "pledge");
3824 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3825 5de5890b 2018-10-18 stsp switch (ch) {
3827 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3830 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3831 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3832 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3834 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3837 5de5890b 2018-10-18 stsp show_ids = 1;
3840 c1669e2e 2019-01-09 stsp recurse = 1;
3843 2deda0b9 2019-03-07 stsp usage_tree();
3844 5de5890b 2018-10-18 stsp /* NOTREACHED */
3848 5de5890b 2018-10-18 stsp argc -= optind;
3849 5de5890b 2018-10-18 stsp argv += optind;
3851 5de5890b 2018-10-18 stsp if (argc == 1)
3852 5de5890b 2018-10-18 stsp path = argv[0];
3853 5de5890b 2018-10-18 stsp else if (argc > 1)
3854 5de5890b 2018-10-18 stsp usage_tree();
3856 7a2c19d6 2019-02-05 stsp path = NULL;
3858 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3859 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3860 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3861 9bf7a39b 2019-02-05 stsp goto done;
3863 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3864 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3865 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3866 7a2c19d6 2019-02-05 stsp goto done;
3868 7a2c19d6 2019-02-05 stsp error = NULL;
3869 7a2c19d6 2019-02-05 stsp if (worktree) {
3870 7a2c19d6 2019-02-05 stsp repo_path =
3871 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3872 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3873 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3874 7a2c19d6 2019-02-05 stsp if (error)
3875 7a2c19d6 2019-02-05 stsp goto done;
3877 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3878 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3879 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3880 7a2c19d6 2019-02-05 stsp goto done;
3885 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3886 5de5890b 2018-10-18 stsp if (error != NULL)
3887 c02c541e 2019-03-29 stsp goto done;
3889 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3890 c02c541e 2019-03-29 stsp if (error)
3891 5de5890b 2018-10-18 stsp goto done;
3893 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3894 9bf7a39b 2019-02-05 stsp if (worktree) {
3895 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3896 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3897 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3898 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3899 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3900 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3901 9bf7a39b 2019-02-05 stsp goto done;
3903 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3905 9bf7a39b 2019-02-05 stsp if (error)
3906 9bf7a39b 2019-02-05 stsp goto done;
3908 9bf7a39b 2019-02-05 stsp path = "/";
3910 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3911 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3912 9bf7a39b 2019-02-05 stsp if (error != NULL)
3913 9bf7a39b 2019-02-05 stsp goto done;
3916 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3917 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3918 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3919 5de5890b 2018-10-18 stsp if (error != NULL)
3920 5de5890b 2018-10-18 stsp goto done;
3921 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3922 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3923 5de5890b 2018-10-18 stsp if (error != NULL)
3924 5de5890b 2018-10-18 stsp goto done;
3926 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3927 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3928 30837e32 2019-07-25 stsp if (error)
3929 5de5890b 2018-10-18 stsp goto done;
3932 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3933 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3935 5de5890b 2018-10-18 stsp free(in_repo_path);
3936 5de5890b 2018-10-18 stsp free(repo_path);
3937 5de5890b 2018-10-18 stsp free(cwd);
3938 5de5890b 2018-10-18 stsp free(commit_id);
3939 7a2c19d6 2019-02-05 stsp if (worktree)
3940 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3941 5de5890b 2018-10-18 stsp if (repo) {
3942 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3943 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3944 5de5890b 2018-10-18 stsp if (error == NULL)
3945 5de5890b 2018-10-18 stsp error = repo_error;
3947 5de5890b 2018-10-18 stsp return error;
3950 6bad629b 2019-02-04 stsp __dead static void
3951 6bad629b 2019-02-04 stsp usage_status(void)
3953 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3957 b72f483a 2019-02-05 stsp static const struct got_error *
3958 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3959 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3960 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3961 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3963 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3964 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3965 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3966 b72f483a 2019-02-05 stsp return NULL;
3969 6bad629b 2019-02-04 stsp static const struct got_error *
3970 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3972 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3973 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3974 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3975 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3976 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3977 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3980 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3982 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3983 6bad629b 2019-02-04 stsp switch (ch) {
3985 2deda0b9 2019-03-07 stsp usage_status();
3986 6bad629b 2019-02-04 stsp /* NOTREACHED */
3990 6bad629b 2019-02-04 stsp argc -= optind;
3991 6bad629b 2019-02-04 stsp argv += optind;
3993 6bad629b 2019-02-04 stsp #ifndef PROFILE
3994 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3995 6bad629b 2019-02-04 stsp NULL) == -1)
3996 6bad629b 2019-02-04 stsp err(1, "pledge");
3998 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3999 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4000 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4001 927df6b7 2019-02-10 stsp goto done;
4004 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4005 927df6b7 2019-02-10 stsp if (error != NULL)
4006 a5edda0a 2019-07-27 stsp goto done;
4008 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4010 6bad629b 2019-02-04 stsp if (error != NULL)
4011 6bad629b 2019-02-04 stsp goto done;
4013 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4014 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4015 087fb88c 2019-08-04 stsp if (error)
4016 087fb88c 2019-08-04 stsp goto done;
4018 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4019 6bad629b 2019-02-04 stsp if (error)
4020 6bad629b 2019-02-04 stsp goto done;
4022 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4023 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4025 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4026 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4027 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4028 927df6b7 2019-02-10 stsp free(cwd);
4029 d0eebce4 2019-03-11 stsp return error;
4032 d0eebce4 2019-03-11 stsp __dead static void
4033 d0eebce4 2019-03-11 stsp usage_ref(void)
4035 d0eebce4 2019-03-11 stsp fprintf(stderr,
4036 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4037 d0eebce4 2019-03-11 stsp getprogname());
4041 d0eebce4 2019-03-11 stsp static const struct got_error *
4042 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
4044 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4045 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4046 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4048 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4049 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4051 d0eebce4 2019-03-11 stsp return err;
4053 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4054 d0eebce4 2019-03-11 stsp char *refstr;
4055 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4056 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4057 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4058 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4059 d0eebce4 2019-03-11 stsp free(refstr);
4062 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4063 d0eebce4 2019-03-11 stsp return NULL;
4066 d0eebce4 2019-03-11 stsp static const struct got_error *
4067 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4069 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4070 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4072 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4074 d0eebce4 2019-03-11 stsp return err;
4076 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4077 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4078 d0eebce4 2019-03-11 stsp return err;
4081 d0eebce4 2019-03-11 stsp static const struct got_error *
4082 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4084 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4085 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4086 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4089 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4090 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4091 d1644381 2019-07-14 stsp * an unintended typo.
4093 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4094 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4096 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4098 d83d9d5c 2019-05-13 stsp if (err) {
4099 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4101 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4102 d83d9d5c 2019-05-13 stsp return err;
4103 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4105 d83d9d5c 2019-05-13 stsp return err;
4106 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4107 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4109 d83d9d5c 2019-05-13 stsp return err;
4112 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4114 d0eebce4 2019-03-11 stsp goto done;
4116 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4119 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4121 d1c1ae5f 2019-08-12 stsp return err;
4124 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4125 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4127 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4128 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4129 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4132 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4133 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4134 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4136 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4137 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4139 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4141 d1c1ae5f 2019-08-12 stsp return err;
4143 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4145 d1c1ae5f 2019-08-12 stsp goto done;
4147 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4149 d1c1ae5f 2019-08-12 stsp if (target_ref)
4150 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4152 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4153 d0eebce4 2019-03-11 stsp return err;
4156 d0eebce4 2019-03-11 stsp static const struct got_error *
4157 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4159 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4160 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4161 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4162 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4163 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
4164 d0eebce4 2019-03-11 stsp const char *delref = NULL;
4166 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
4167 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4168 d0eebce4 2019-03-11 stsp switch (ch) {
4170 d0eebce4 2019-03-11 stsp delref = optarg;
4173 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4174 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4175 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4177 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4180 d0eebce4 2019-03-11 stsp do_list = 1;
4183 d1c1ae5f 2019-08-12 stsp create_symref = 1;
4186 d0eebce4 2019-03-11 stsp usage_ref();
4187 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4191 d0eebce4 2019-03-11 stsp if (do_list && delref)
4192 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
4194 d0eebce4 2019-03-11 stsp argc -= optind;
4195 d0eebce4 2019-03-11 stsp argv += optind;
4197 d0eebce4 2019-03-11 stsp if (do_list || delref) {
4198 d1c1ae5f 2019-08-12 stsp if (create_symref)
4199 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
4200 d1c1ae5f 2019-08-12 stsp "-l or -d options");
4201 d0eebce4 2019-03-11 stsp if (argc > 0)
4202 d0eebce4 2019-03-11 stsp usage_ref();
4203 d0eebce4 2019-03-11 stsp } else if (argc != 2)
4204 d0eebce4 2019-03-11 stsp usage_ref();
4206 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4207 e0b57350 2019-03-12 stsp if (do_list) {
4208 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4209 e0b57350 2019-03-12 stsp NULL) == -1)
4210 e0b57350 2019-03-12 stsp err(1, "pledge");
4212 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4213 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4214 e0b57350 2019-03-12 stsp err(1, "pledge");
4217 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4218 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4219 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4220 d0eebce4 2019-03-11 stsp goto done;
4223 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4224 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4225 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4226 d0eebce4 2019-03-11 stsp goto done;
4228 d0eebce4 2019-03-11 stsp error = NULL;
4229 d0eebce4 2019-03-11 stsp if (worktree) {
4230 d0eebce4 2019-03-11 stsp repo_path =
4231 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4232 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4233 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4234 d0eebce4 2019-03-11 stsp if (error)
4235 d0eebce4 2019-03-11 stsp goto done;
4237 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4238 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4239 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4240 d0eebce4 2019-03-11 stsp goto done;
4245 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4246 d0eebce4 2019-03-11 stsp if (error != NULL)
4247 d0eebce4 2019-03-11 stsp goto done;
4249 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4250 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4251 c02c541e 2019-03-29 stsp if (error)
4252 c02c541e 2019-03-29 stsp goto done;
4254 d0eebce4 2019-03-11 stsp if (do_list)
4255 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4256 d0eebce4 2019-03-11 stsp else if (delref)
4257 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4258 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4259 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4261 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4264 4e759de4 2019-06-26 stsp got_repo_close(repo);
4265 4e759de4 2019-06-26 stsp if (worktree)
4266 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4267 4e759de4 2019-06-26 stsp free(cwd);
4268 4e759de4 2019-06-26 stsp free(repo_path);
4269 4e759de4 2019-06-26 stsp return error;
4272 4e759de4 2019-06-26 stsp __dead static void
4273 4e759de4 2019-06-26 stsp usage_branch(void)
4275 4e759de4 2019-06-26 stsp fprintf(stderr,
4276 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4277 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4281 4e759de4 2019-06-26 stsp static const struct got_error *
4282 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4283 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4285 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4286 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4287 4e99b47e 2019-10-04 stsp char *refstr;
4289 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4290 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4291 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4292 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4294 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4296 4e99b47e 2019-10-04 stsp return err;
4297 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4298 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4299 4e99b47e 2019-10-04 stsp marker = "* ";
4301 4e99b47e 2019-10-04 stsp marker = "~ ";
4305 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4306 4e99b47e 2019-10-04 stsp refname += 11;
4307 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4308 4e99b47e 2019-10-04 stsp refname += 18;
4310 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4311 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4312 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4314 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4315 4e99b47e 2019-10-04 stsp free(refstr);
4316 4e99b47e 2019-10-04 stsp return NULL;
4319 4e99b47e 2019-10-04 stsp static const struct got_error *
4320 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4322 ad89fa31 2019-10-04 stsp const char *refname;
4324 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4325 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4327 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4329 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4330 ad89fa31 2019-10-04 stsp refname += 11;
4331 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4332 ad89fa31 2019-10-04 stsp refname += 18;
4334 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4336 ad89fa31 2019-10-04 stsp return NULL;
4339 ad89fa31 2019-10-04 stsp static const struct got_error *
4340 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4342 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4343 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4344 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4345 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4346 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4348 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4350 4e99b47e 2019-10-04 stsp if (worktree) {
4351 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4352 4e99b47e 2019-10-04 stsp worktree);
4354 4e99b47e 2019-10-04 stsp return err;
4356 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4357 4e99b47e 2019-10-04 stsp worktree);
4359 4e99b47e 2019-10-04 stsp return err;
4361 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4362 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4363 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4365 4e99b47e 2019-10-04 stsp return err;
4366 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4367 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4371 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4372 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4374 4e759de4 2019-06-26 stsp return err;
4376 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4377 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4379 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4380 4e759de4 2019-06-26 stsp return NULL;
4383 4e759de4 2019-06-26 stsp static const struct got_error *
4384 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4385 45cd4e47 2019-08-25 stsp const char *branch_name)
4387 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4388 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4389 4e759de4 2019-06-26 stsp char *refname;
4391 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4392 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4394 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4396 4e759de4 2019-06-26 stsp goto done;
4398 45cd4e47 2019-08-25 stsp if (worktree &&
4399 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4400 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4401 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4402 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4403 45cd4e47 2019-08-25 stsp goto done;
4406 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4409 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4410 4e759de4 2019-06-26 stsp free(refname);
4411 4e759de4 2019-06-26 stsp return err;
4414 4e759de4 2019-06-26 stsp static const struct got_error *
4415 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4416 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4418 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4419 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4420 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4423 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4424 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4425 d3f84d51 2019-07-11 stsp * an unintended typo.
4427 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4428 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4430 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4431 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4432 4e759de4 2019-06-26 stsp goto done;
4435 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4436 4e759de4 2019-06-26 stsp if (err == NULL) {
4437 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4438 4e759de4 2019-06-26 stsp goto done;
4439 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4440 4e759de4 2019-06-26 stsp goto done;
4442 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4444 4e759de4 2019-06-26 stsp goto done;
4446 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4449 4e759de4 2019-06-26 stsp got_ref_close(ref);
4450 4e759de4 2019-06-26 stsp free(base_refname);
4451 4e759de4 2019-06-26 stsp free(refname);
4452 4e759de4 2019-06-26 stsp return err;
4455 4e759de4 2019-06-26 stsp static const struct got_error *
4456 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4458 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4459 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4460 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4461 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4462 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4463 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4464 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4465 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4466 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4467 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4468 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4470 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4472 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4473 4e759de4 2019-06-26 stsp switch (ch) {
4475 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4478 4e759de4 2019-06-26 stsp delref = optarg;
4481 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4482 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4483 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4485 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4488 4e759de4 2019-06-26 stsp do_list = 1;
4491 da76fce2 2020-02-24 stsp do_update = 0;
4494 4e759de4 2019-06-26 stsp usage_branch();
4495 4e759de4 2019-06-26 stsp /* NOTREACHED */
4499 4e759de4 2019-06-26 stsp if (do_list && delref)
4500 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4502 4e759de4 2019-06-26 stsp argc -= optind;
4503 4e759de4 2019-06-26 stsp argv += optind;
4505 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4506 ad89fa31 2019-10-04 stsp do_show = 1;
4508 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4509 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4511 4e759de4 2019-06-26 stsp if (do_list || delref) {
4512 4e759de4 2019-06-26 stsp if (argc > 0)
4513 4e759de4 2019-06-26 stsp usage_branch();
4514 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4515 4e759de4 2019-06-26 stsp usage_branch();
4517 4e759de4 2019-06-26 stsp #ifndef PROFILE
4518 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4519 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4520 4e759de4 2019-06-26 stsp NULL) == -1)
4521 4e759de4 2019-06-26 stsp err(1, "pledge");
4523 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4524 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4525 4e759de4 2019-06-26 stsp err(1, "pledge");
4528 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4529 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4530 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4531 4e759de4 2019-06-26 stsp goto done;
4534 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4535 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4536 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4537 4e759de4 2019-06-26 stsp goto done;
4539 4e759de4 2019-06-26 stsp error = NULL;
4540 4e759de4 2019-06-26 stsp if (worktree) {
4541 4e759de4 2019-06-26 stsp repo_path =
4542 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4543 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4544 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4545 4e759de4 2019-06-26 stsp if (error)
4546 4e759de4 2019-06-26 stsp goto done;
4548 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4549 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4550 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4551 4e759de4 2019-06-26 stsp goto done;
4556 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4557 4e759de4 2019-06-26 stsp if (error != NULL)
4558 4e759de4 2019-06-26 stsp goto done;
4560 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4561 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4562 4e759de4 2019-06-26 stsp if (error)
4563 4e759de4 2019-06-26 stsp goto done;
4565 ad89fa31 2019-10-04 stsp if (do_show)
4566 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4567 ad89fa31 2019-10-04 stsp else if (do_list)
4568 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4569 4e759de4 2019-06-26 stsp else if (delref)
4570 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4572 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4573 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4574 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4575 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4576 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4577 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4578 a74f7e83 2019-11-10 stsp if (error)
4579 a74f7e83 2019-11-10 stsp goto done;
4580 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4581 da76fce2 2020-02-24 stsp if (error)
4582 da76fce2 2020-02-24 stsp goto done;
4583 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4584 da76fce2 2020-02-24 stsp int did_something = 0;
4585 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4587 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4588 da76fce2 2020-02-24 stsp if (error)
4589 da76fce2 2020-02-24 stsp goto done;
4590 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4591 da76fce2 2020-02-24 stsp worktree);
4592 da76fce2 2020-02-24 stsp if (error)
4593 da76fce2 2020-02-24 stsp goto done;
4594 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4596 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4597 da76fce2 2020-02-24 stsp goto done;
4599 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4600 da76fce2 2020-02-24 stsp free(branch_refname);
4601 da76fce2 2020-02-24 stsp if (error)
4602 da76fce2 2020-02-24 stsp goto done;
4603 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4605 da76fce2 2020-02-24 stsp if (error)
4606 da76fce2 2020-02-24 stsp goto done;
4607 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4608 da76fce2 2020-02-24 stsp commit_id);
4609 da76fce2 2020-02-24 stsp if (error)
4610 da76fce2 2020-02-24 stsp goto done;
4611 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4612 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4613 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4614 da76fce2 2020-02-24 stsp if (error)
4615 da76fce2 2020-02-24 stsp goto done;
4616 da76fce2 2020-02-24 stsp if (did_something)
4617 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4622 da76fce2 2020-02-24 stsp got_ref_close(ref);
4624 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4625 d0eebce4 2019-03-11 stsp if (worktree)
4626 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4627 d0eebce4 2019-03-11 stsp free(cwd);
4628 d0eebce4 2019-03-11 stsp free(repo_path);
4629 da76fce2 2020-02-24 stsp free(commit_id);
4630 da76fce2 2020-02-24 stsp free(commit_id_str);
4631 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4632 da76fce2 2020-02-24 stsp free((char *)pe->path);
4633 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4634 d00136be 2019-03-26 stsp return error;
4638 d00136be 2019-03-26 stsp __dead static void
4639 8e7bd50a 2019-08-22 stsp usage_tag(void)
4641 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4642 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4643 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4648 b8bad2ba 2019-08-23 stsp static const struct got_error *
4649 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4651 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4652 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4653 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4654 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4655 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4657 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4658 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4659 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4660 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4662 b8bad2ba 2019-08-23 stsp return err;
4663 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4666 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4669 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4670 b8bad2ba 2019-08-23 stsp free(re_id);
4673 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4674 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4677 b8bad2ba 2019-08-23 stsp while (se) {
4678 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4681 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4682 b8bad2ba 2019-08-23 stsp free(se_id);
4685 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4686 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4688 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4689 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4691 b8bad2ba 2019-08-23 stsp return err;
4692 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4695 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4700 b8bad2ba 2019-08-23 stsp return err;
4704 b8bad2ba 2019-08-23 stsp static const struct got_error *
4705 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4707 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4708 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4709 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4711 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4713 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4715 8e7bd50a 2019-08-22 stsp return err;
4717 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4718 8e7bd50a 2019-08-22 stsp const char *refname;
4719 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4720 8e7bd50a 2019-08-22 stsp char datebuf[26];
4721 d4efa91b 2020-01-14 stsp const char *tagger;
4722 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4723 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4724 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4725 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4727 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4728 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4730 8e7bd50a 2019-08-22 stsp refname += 10;
4731 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4732 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4733 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4736 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4737 8e7bd50a 2019-08-22 stsp free(refstr);
4739 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4742 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4743 d4efa91b 2020-01-14 stsp if (err) {
4744 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4748 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4749 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4750 d4efa91b 2020-01-14 stsp if (err) {
4754 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4755 d4efa91b 2020-01-14 stsp tagger_time =
4756 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4757 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4763 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4764 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4765 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4766 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4770 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4771 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4772 2417344c 2019-08-23 stsp if (datestr)
4773 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4774 d4efa91b 2020-01-14 stsp if (commit)
4775 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4777 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4778 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4779 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4782 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4783 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4786 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4787 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4790 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4791 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4798 8e7bd50a 2019-08-22 stsp free(id_str);
4799 d4efa91b 2020-01-14 stsp if (commit) {
4800 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4803 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4805 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4806 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4807 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4808 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4813 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4815 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4817 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4818 8e7bd50a 2019-08-22 stsp } while (line);
4819 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4822 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4823 8e7bd50a 2019-08-22 stsp return NULL;
4826 8e7bd50a 2019-08-22 stsp static const struct got_error *
4827 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4828 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4830 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4831 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4832 f372d5cd 2019-10-21 stsp char *editor = NULL;
4833 8e7bd50a 2019-08-22 stsp int fd = -1;
4835 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4836 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4837 8e7bd50a 2019-08-22 stsp goto done;
4840 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4841 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4842 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4843 8e7bd50a 2019-08-22 stsp goto done;
4846 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4848 8e7bd50a 2019-08-22 stsp goto done;
4850 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4851 8e7bd50a 2019-08-22 stsp close(fd);
4853 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4855 8e7bd50a 2019-08-22 stsp goto done;
4856 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4858 8e7bd50a 2019-08-22 stsp free(initial_content);
4859 8e7bd50a 2019-08-22 stsp free(template);
4860 8e7bd50a 2019-08-22 stsp free(editor);
4862 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4863 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4864 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4865 8e7bd50a 2019-08-22 stsp if (err) {
4866 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4867 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4870 8e7bd50a 2019-08-22 stsp return err;
4873 8e7bd50a 2019-08-22 stsp static const struct got_error *
4874 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4875 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4877 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4878 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4879 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4880 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4881 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4882 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4883 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4886 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4887 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4888 8e7bd50a 2019-08-22 stsp * an unintended typo.
4890 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4891 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4893 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4895 8e7bd50a 2019-08-22 stsp return err;
4897 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4898 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4900 8e7bd50a 2019-08-22 stsp goto done;
4902 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4904 8e7bd50a 2019-08-22 stsp goto done;
4906 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4907 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4908 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4909 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4910 8e7bd50a 2019-08-22 stsp goto done;
4912 8e7bd50a 2019-08-22 stsp tag_name += 10;
4913 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4914 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4915 8e7bd50a 2019-08-22 stsp goto done;
4918 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4919 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4920 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4921 8e7bd50a 2019-08-22 stsp goto done;
4922 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4923 8e7bd50a 2019-08-22 stsp goto done;
4925 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4926 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4927 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4928 f372d5cd 2019-10-21 stsp if (err) {
4929 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4930 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4931 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4932 8e7bd50a 2019-08-22 stsp goto done;
4936 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4937 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4938 f372d5cd 2019-10-21 stsp if (err) {
4939 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4940 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4941 8e7bd50a 2019-08-22 stsp goto done;
4944 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4945 f372d5cd 2019-10-21 stsp if (err) {
4946 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4947 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4948 8e7bd50a 2019-08-22 stsp goto done;
4951 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4952 f372d5cd 2019-10-21 stsp if (err) {
4953 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4954 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4955 f372d5cd 2019-10-21 stsp goto done;
4958 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4959 f372d5cd 2019-10-21 stsp if (err) {
4960 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4961 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4962 f372d5cd 2019-10-21 stsp goto done;
4964 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4966 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4967 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4968 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4969 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4970 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4971 f372d5cd 2019-10-21 stsp free(tag_id_str);
4973 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4974 8e7bd50a 2019-08-22 stsp free(commit_id);
4975 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4976 8e7bd50a 2019-08-22 stsp free(refname);
4977 8e7bd50a 2019-08-22 stsp free(tagmsg);
4978 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4979 aba9c984 2019-09-08 stsp free(tagger);
4980 8e7bd50a 2019-08-22 stsp return err;
4983 8e7bd50a 2019-08-22 stsp static const struct got_error *
4984 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4986 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4987 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4988 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4989 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4990 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4991 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4992 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4994 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4995 8e7bd50a 2019-08-22 stsp switch (ch) {
4997 80106605 2020-02-24 stsp commit_id_arg = optarg;
5000 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5003 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5004 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5005 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5007 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5010 8e7bd50a 2019-08-22 stsp do_list = 1;
5013 8e7bd50a 2019-08-22 stsp usage_tag();
5014 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5018 8e7bd50a 2019-08-22 stsp argc -= optind;
5019 8e7bd50a 2019-08-22 stsp argv += optind;
5021 8e7bd50a 2019-08-22 stsp if (do_list) {
5022 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5023 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
5024 8e7bd50a 2019-08-22 stsp if (tagmsg)
5025 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5026 8e7bd50a 2019-08-22 stsp if (argc > 0)
5027 8e7bd50a 2019-08-22 stsp usage_tag();
5028 80106605 2020-02-24 stsp } else if (argc != 1)
5029 8e7bd50a 2019-08-22 stsp usage_tag();
5031 a2887370 2019-08-23 stsp tag_name = argv[0];
5033 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5034 8e7bd50a 2019-08-22 stsp if (do_list) {
5035 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5036 8e7bd50a 2019-08-22 stsp NULL) == -1)
5037 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5039 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5040 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5041 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5044 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5045 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5046 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5047 8e7bd50a 2019-08-22 stsp goto done;
5050 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5051 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5052 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5053 8e7bd50a 2019-08-22 stsp goto done;
5055 8e7bd50a 2019-08-22 stsp error = NULL;
5056 8e7bd50a 2019-08-22 stsp if (worktree) {
5057 8e7bd50a 2019-08-22 stsp repo_path =
5058 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5059 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5060 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5061 8e7bd50a 2019-08-22 stsp if (error)
5062 8e7bd50a 2019-08-22 stsp goto done;
5064 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5065 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5066 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5067 8e7bd50a 2019-08-22 stsp goto done;
5072 8e7bd50a 2019-08-22 stsp if (do_list) {
5073 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5074 c9956ddf 2019-09-08 stsp if (error != NULL)
5075 c9956ddf 2019-09-08 stsp goto done;
5076 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5077 8e7bd50a 2019-08-22 stsp if (error)
5078 8e7bd50a 2019-08-22 stsp goto done;
5079 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5081 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5082 c9956ddf 2019-09-08 stsp if (error)
5083 c9956ddf 2019-09-08 stsp goto done;
5084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5085 c9956ddf 2019-09-08 stsp if (error != NULL)
5086 c9956ddf 2019-09-08 stsp goto done;
5088 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5089 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5090 8e7bd50a 2019-08-22 stsp if (error)
5091 8e7bd50a 2019-08-22 stsp goto done;
5094 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5095 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5096 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5097 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5098 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5099 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5100 8e7bd50a 2019-08-22 stsp if (error)
5101 8e7bd50a 2019-08-22 stsp goto done;
5102 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5103 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5104 8e7bd50a 2019-08-22 stsp if (error)
5105 8e7bd50a 2019-08-22 stsp goto done;
5106 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5107 8e7bd50a 2019-08-22 stsp free(commit_id);
5108 8e7bd50a 2019-08-22 stsp if (error)
5109 8e7bd50a 2019-08-22 stsp goto done;
5112 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5113 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5117 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5118 8e7bd50a 2019-08-22 stsp if (worktree)
5119 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5120 8e7bd50a 2019-08-22 stsp free(cwd);
5121 8e7bd50a 2019-08-22 stsp free(repo_path);
5122 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5123 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5124 8e7bd50a 2019-08-22 stsp return error;
5127 8e7bd50a 2019-08-22 stsp __dead static void
5128 d00136be 2019-03-26 stsp usage_add(void)
5130 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5131 022fae89 2019-12-06 tracey getprogname());
5135 d00136be 2019-03-26 stsp static const struct got_error *
5136 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5138 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5140 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5141 4e68cba3 2019-11-23 stsp return NULL;
5144 4e68cba3 2019-11-23 stsp static const struct got_error *
5145 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5147 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5148 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5149 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5150 1dd54920 2019-05-11 stsp char *cwd = NULL;
5151 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5152 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5153 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5155 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5157 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5158 d00136be 2019-03-26 stsp switch (ch) {
5159 022fae89 2019-12-06 tracey case 'I':
5160 022fae89 2019-12-06 tracey no_ignores = 1;
5163 4e68cba3 2019-11-23 stsp can_recurse = 1;
5166 d00136be 2019-03-26 stsp usage_add();
5167 d00136be 2019-03-26 stsp /* NOTREACHED */
5171 d00136be 2019-03-26 stsp argc -= optind;
5172 d00136be 2019-03-26 stsp argv += optind;
5174 43012d58 2019-07-14 stsp #ifndef PROFILE
5175 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5176 43012d58 2019-07-14 stsp NULL) == -1)
5177 43012d58 2019-07-14 stsp err(1, "pledge");
5179 723c305c 2019-05-11 jcs if (argc < 1)
5180 d00136be 2019-03-26 stsp usage_add();
5182 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5183 d00136be 2019-03-26 stsp if (cwd == NULL) {
5184 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5185 d00136be 2019-03-26 stsp goto done;
5188 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5189 d00136be 2019-03-26 stsp if (error)
5190 d00136be 2019-03-26 stsp goto done;
5192 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5194 031a5338 2019-03-26 stsp if (error != NULL)
5195 031a5338 2019-03-26 stsp goto done;
5197 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5198 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5199 d00136be 2019-03-26 stsp if (error)
5200 d00136be 2019-03-26 stsp goto done;
5202 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5203 6d022e97 2019-08-04 stsp if (error)
5204 022fae89 2019-12-06 tracey goto done;
5206 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5207 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5208 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5209 6d022e97 2019-08-04 stsp goto done;
5213 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5214 4e68cba3 2019-11-23 stsp char *ondisk_path;
5215 4e68cba3 2019-11-23 stsp struct stat sb;
5216 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5217 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5218 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5219 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5220 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5221 4e68cba3 2019-11-23 stsp goto done;
5223 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5224 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5225 4e68cba3 2019-11-23 stsp free(ondisk_path);
5228 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5229 4e68cba3 2019-11-23 stsp ondisk_path);
5230 4e68cba3 2019-11-23 stsp free(ondisk_path);
5231 4e68cba3 2019-11-23 stsp goto done;
5233 4e68cba3 2019-11-23 stsp free(ondisk_path);
5234 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5235 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5236 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5237 4e68cba3 2019-11-23 stsp goto done;
5242 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5243 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5246 031a5338 2019-03-26 stsp got_repo_close(repo);
5247 d00136be 2019-03-26 stsp if (worktree)
5248 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5249 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5250 1dd54920 2019-05-11 stsp free((char *)pe->path);
5251 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5252 2ec1f75b 2019-03-26 stsp free(cwd);
5253 2ec1f75b 2019-03-26 stsp return error;
5256 2ec1f75b 2019-03-26 stsp __dead static void
5257 648e4ef7 2019-07-09 stsp usage_remove(void)
5259 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5260 f2a9dc41 2019-12-13 tracey getprogname());
5264 2a06fe5f 2019-08-24 stsp static const struct got_error *
5265 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5266 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5268 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5270 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5271 2a06fe5f 2019-08-24 stsp return NULL;
5272 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5273 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5274 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5275 2a06fe5f 2019-08-24 stsp return NULL;
5278 2ec1f75b 2019-03-26 stsp static const struct got_error *
5279 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5281 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5282 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5283 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5284 17ed4618 2019-06-02 stsp char *cwd = NULL;
5285 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5286 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5287 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5289 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5291 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5292 2ec1f75b 2019-03-26 stsp switch (ch) {
5294 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5296 70e3e7f5 2019-12-13 tracey case 'k':
5297 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5299 f2a9dc41 2019-12-13 tracey case 'R':
5300 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5303 f2a9dc41 2019-12-13 tracey usage_remove();
5304 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5308 2ec1f75b 2019-03-26 stsp argc -= optind;
5309 2ec1f75b 2019-03-26 stsp argv += optind;
5311 43012d58 2019-07-14 stsp #ifndef PROFILE
5312 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5313 43012d58 2019-07-14 stsp NULL) == -1)
5314 43012d58 2019-07-14 stsp err(1, "pledge");
5316 17ed4618 2019-06-02 stsp if (argc < 1)
5317 648e4ef7 2019-07-09 stsp usage_remove();
5319 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5320 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5321 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5322 2ec1f75b 2019-03-26 stsp goto done;
5324 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5325 2ec1f75b 2019-03-26 stsp if (error)
5326 2ec1f75b 2019-03-26 stsp goto done;
5328 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5331 2ec1f75b 2019-03-26 stsp goto done;
5333 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5334 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5335 2ec1f75b 2019-03-26 stsp if (error)
5336 2ec1f75b 2019-03-26 stsp goto done;
5338 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5339 6d022e97 2019-08-04 stsp if (error)
5340 6d022e97 2019-08-04 stsp goto done;
5342 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5343 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5344 f2a9dc41 2019-12-13 tracey struct stat sb;
5345 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5346 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5347 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5348 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5349 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5350 f2a9dc41 2019-12-13 tracey goto done;
5352 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5353 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5354 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5355 f2a9dc41 2019-12-13 tracey continue;
5357 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5358 f2a9dc41 2019-12-13 tracey ondisk_path);
5359 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5360 f2a9dc41 2019-12-13 tracey goto done;
5362 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5363 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5364 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5365 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5366 f2a9dc41 2019-12-13 tracey goto done;
5371 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5372 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5375 a129376b 2019-03-28 stsp got_repo_close(repo);
5376 a129376b 2019-03-28 stsp if (worktree)
5377 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5378 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5379 17ed4618 2019-06-02 stsp free((char *)pe->path);
5380 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5381 a129376b 2019-03-28 stsp free(cwd);
5382 a129376b 2019-03-28 stsp return error;
5385 a129376b 2019-03-28 stsp __dead static void
5386 a129376b 2019-03-28 stsp usage_revert(void)
5388 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5389 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5393 1ee397ad 2019-07-12 stsp static const struct got_error *
5394 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5396 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5397 fb9704af 2020-01-27 stsp return NULL;
5399 a129376b 2019-03-28 stsp while (path[0] == '/')
5401 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5402 33aa809d 2019-08-08 stsp return NULL;
5405 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5406 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5407 33aa809d 2019-08-08 stsp const char *action;
5410 33aa809d 2019-08-08 stsp static const struct got_error *
5411 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5412 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5414 33aa809d 2019-08-08 stsp char *line = NULL;
5415 33aa809d 2019-08-08 stsp size_t linesize = 0;
5416 33aa809d 2019-08-08 stsp ssize_t linelen;
5418 33aa809d 2019-08-08 stsp switch (status) {
5419 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5420 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5422 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5423 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5425 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5426 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5427 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5428 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5429 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5430 33aa809d 2019-08-08 stsp printf("%s", line);
5431 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5432 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5433 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5434 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5435 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5438 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5441 33aa809d 2019-08-08 stsp return NULL;
5444 33aa809d 2019-08-08 stsp static const struct got_error *
5445 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5446 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5448 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5449 33aa809d 2019-08-08 stsp char *line = NULL;
5450 33aa809d 2019-08-08 stsp size_t linesize = 0;
5451 33aa809d 2019-08-08 stsp ssize_t linelen;
5452 33aa809d 2019-08-08 stsp int resp = ' ';
5453 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5455 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5457 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5459 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5460 33aa809d 2019-08-08 stsp a->action);
5462 33aa809d 2019-08-08 stsp return err;
5463 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5464 33aa809d 2019-08-08 stsp if (linelen == -1) {
5465 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5466 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5467 33aa809d 2019-08-08 stsp return NULL;
5469 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5471 33aa809d 2019-08-08 stsp *nl = '\0';
5472 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5473 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5474 33aa809d 2019-08-08 stsp printf("y\n");
5475 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5476 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5477 33aa809d 2019-08-08 stsp printf("n\n");
5478 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5479 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5480 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5481 33aa809d 2019-08-08 stsp printf("q\n");
5483 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5484 33aa809d 2019-08-08 stsp free(line);
5485 33aa809d 2019-08-08 stsp return NULL;
5488 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5489 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5490 33aa809d 2019-08-08 stsp a->action);
5492 33aa809d 2019-08-08 stsp return err;
5493 33aa809d 2019-08-08 stsp resp = getchar();
5494 33aa809d 2019-08-08 stsp if (resp == '\n')
5495 33aa809d 2019-08-08 stsp resp = getchar();
5496 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5497 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5498 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5499 33aa809d 2019-08-08 stsp resp = ' ';
5501 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5502 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5503 33aa809d 2019-08-08 stsp resp = ' ';
5507 33aa809d 2019-08-08 stsp if (resp == 'y')
5508 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5509 33aa809d 2019-08-08 stsp else if (resp == 'n')
5510 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5511 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5512 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5514 1ee397ad 2019-07-12 stsp return NULL;
5518 a129376b 2019-03-28 stsp static const struct got_error *
5519 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5521 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5522 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5523 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5524 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5525 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5526 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5527 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5528 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5529 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5530 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5532 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5534 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5535 a129376b 2019-03-28 stsp switch (ch) {
5537 33aa809d 2019-08-08 stsp pflag = 1;
5540 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5543 0f6d7415 2019-08-08 stsp can_recurse = 1;
5546 a129376b 2019-03-28 stsp usage_revert();
5547 a129376b 2019-03-28 stsp /* NOTREACHED */
5551 a129376b 2019-03-28 stsp argc -= optind;
5552 a129376b 2019-03-28 stsp argv += optind;
5554 43012d58 2019-07-14 stsp #ifndef PROFILE
5555 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5556 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5557 43012d58 2019-07-14 stsp err(1, "pledge");
5559 e20a8b6f 2019-06-04 stsp if (argc < 1)
5560 a129376b 2019-03-28 stsp usage_revert();
5561 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5562 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5564 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5565 a129376b 2019-03-28 stsp if (cwd == NULL) {
5566 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5567 a129376b 2019-03-28 stsp goto done;
5569 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5570 2ec1f75b 2019-03-26 stsp if (error)
5571 2ec1f75b 2019-03-26 stsp goto done;
5573 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5575 a129376b 2019-03-28 stsp if (error != NULL)
5576 a129376b 2019-03-28 stsp goto done;
5578 33aa809d 2019-08-08 stsp if (patch_script_path) {
5579 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5580 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5581 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5582 33aa809d 2019-08-08 stsp patch_script_path);
5583 33aa809d 2019-08-08 stsp goto done;
5586 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5587 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5588 a129376b 2019-03-28 stsp if (error)
5589 a129376b 2019-03-28 stsp goto done;
5591 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5592 6d022e97 2019-08-04 stsp if (error)
5593 6d022e97 2019-08-04 stsp goto done;
5595 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5596 0f6d7415 2019-08-08 stsp char *ondisk_path;
5597 0f6d7415 2019-08-08 stsp struct stat sb;
5598 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5599 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5600 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5601 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5602 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5603 0f6d7415 2019-08-08 stsp goto done;
5605 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5606 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5607 0f6d7415 2019-08-08 stsp free(ondisk_path);
5610 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5611 0f6d7415 2019-08-08 stsp ondisk_path);
5612 0f6d7415 2019-08-08 stsp free(ondisk_path);
5613 0f6d7415 2019-08-08 stsp goto done;
5615 0f6d7415 2019-08-08 stsp free(ondisk_path);
5616 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5617 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5618 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5619 0f6d7415 2019-08-08 stsp goto done;
5624 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5625 33aa809d 2019-08-08 stsp cpa.action = "revert";
5626 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5627 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5629 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5630 33aa809d 2019-08-08 stsp error == NULL)
5631 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5633 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5634 2ec1f75b 2019-03-26 stsp if (worktree)
5635 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5636 2ec1f75b 2019-03-26 stsp free(path);
5637 d00136be 2019-03-26 stsp free(cwd);
5638 6bad629b 2019-02-04 stsp return error;
5641 c4296144 2019-05-09 stsp __dead static void
5642 c4296144 2019-05-09 stsp usage_commit(void)
5644 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5645 5c1e53bc 2019-07-28 stsp getprogname());
5649 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5650 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5651 e2ba3d07 2019-05-13 stsp const char *editor;
5652 e0870e44 2019-05-13 stsp const char *worktree_path;
5653 76d98825 2019-06-03 stsp const char *branch_name;
5654 314a6357 2019-05-13 stsp const char *repo_path;
5655 e0870e44 2019-05-13 stsp char *logmsg_path;
5659 33ad4cbe 2019-05-12 jcs static const struct got_error *
5660 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5663 76d98825 2019-06-03 stsp char *initial_content = NULL;
5664 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5665 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5666 e0870e44 2019-05-13 stsp char *template = NULL;
5667 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5669 33ad4cbe 2019-05-12 jcs size_t len;
5671 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5672 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5673 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5674 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5675 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5676 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5677 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5678 33ad4cbe 2019-05-12 jcs return NULL;
5681 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5682 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5684 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5685 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5686 76d98825 2019-06-03 stsp a->branch_name) == -1)
5687 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5689 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5691 e0870e44 2019-05-13 stsp goto done;
5693 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5695 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5696 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5697 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5698 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5699 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5703 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5705 76d98825 2019-06-03 stsp free(initial_content);
5706 e0870e44 2019-05-13 stsp free(template);
5708 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5709 3ce1b845 2019-07-15 stsp if (err == NULL) {
5710 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5711 3ce1b845 2019-07-15 stsp if (err) {
5712 3ce1b845 2019-07-15 stsp free(*logmsg);
5713 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5716 33ad4cbe 2019-05-12 jcs return err;
5719 c4296144 2019-05-09 stsp static const struct got_error *
5720 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5722 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5723 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5724 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5725 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5726 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5727 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5728 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5729 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5730 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5731 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5733 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5734 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5736 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5737 c4296144 2019-05-09 stsp switch (ch) {
5739 c4296144 2019-05-09 stsp logmsg = optarg;
5742 c4296144 2019-05-09 stsp usage_commit();
5743 c4296144 2019-05-09 stsp /* NOTREACHED */
5747 c4296144 2019-05-09 stsp argc -= optind;
5748 c4296144 2019-05-09 stsp argv += optind;
5750 43012d58 2019-07-14 stsp #ifndef PROFILE
5751 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5752 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5753 43012d58 2019-07-14 stsp err(1, "pledge");
5755 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5756 c4296144 2019-05-09 stsp if (cwd == NULL) {
5757 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5758 c4296144 2019-05-09 stsp goto done;
5760 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5761 7d5807f4 2019-07-11 stsp if (error)
5762 7d5807f4 2019-07-11 stsp goto done;
5764 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5765 c4296144 2019-05-09 stsp if (error)
5766 a698f62e 2019-07-25 stsp goto done;
5767 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5768 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5769 c4296144 2019-05-09 stsp goto done;
5772 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5773 916f288c 2019-07-30 stsp worktree);
5774 5c1e53bc 2019-07-28 stsp if (error)
5775 5c1e53bc 2019-07-28 stsp goto done;
5777 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5778 c9956ddf 2019-09-08 stsp if (error)
5779 c9956ddf 2019-09-08 stsp goto done;
5780 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5781 c9956ddf 2019-09-08 stsp gitconfig_path);
5782 c4296144 2019-05-09 stsp if (error != NULL)
5783 c4296144 2019-05-09 stsp goto done;
5785 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5786 aba9c984 2019-09-08 stsp if (error)
5787 aba9c984 2019-09-08 stsp return error;
5790 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5791 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5793 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5794 314a6357 2019-05-13 stsp error = get_editor(&editor);
5796 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5797 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5798 c4296144 2019-05-09 stsp if (error)
5799 c4296144 2019-05-09 stsp goto done;
5801 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5802 087fb88c 2019-08-04 stsp if (error)
5803 087fb88c 2019-08-04 stsp goto done;
5805 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5806 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5807 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5808 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5809 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5810 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5811 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5812 916f288c 2019-07-30 stsp goto done;
5814 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5816 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5817 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5818 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5819 e0870e44 2019-05-13 stsp if (error) {
5820 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5821 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5822 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5823 c4296144 2019-05-09 stsp goto done;
5826 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5827 c4296144 2019-05-09 stsp if (error)
5828 c4296144 2019-05-09 stsp goto done;
5829 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5831 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5832 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5833 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5834 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5835 7266f21f 2019-10-21 stsp error == NULL)
5836 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5837 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5839 c4296144 2019-05-09 stsp got_repo_close(repo);
5840 c4296144 2019-05-09 stsp if (worktree)
5841 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5842 c4296144 2019-05-09 stsp free(cwd);
5843 c4296144 2019-05-09 stsp free(id_str);
5844 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5845 e2ba3d07 2019-05-13 stsp free(editor);
5846 aba9c984 2019-09-08 stsp free(author);
5847 234035bc 2019-06-01 stsp return error;
5850 234035bc 2019-06-01 stsp __dead static void
5851 234035bc 2019-06-01 stsp usage_cherrypick(void)
5853 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5857 234035bc 2019-06-01 stsp static const struct got_error *
5858 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5860 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5861 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5862 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5863 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5864 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5865 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5866 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5867 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5868 234035bc 2019-06-01 stsp int ch, did_something = 0;
5870 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5871 234035bc 2019-06-01 stsp switch (ch) {
5873 234035bc 2019-06-01 stsp usage_cherrypick();
5874 234035bc 2019-06-01 stsp /* NOTREACHED */
5878 234035bc 2019-06-01 stsp argc -= optind;
5879 234035bc 2019-06-01 stsp argv += optind;
5881 43012d58 2019-07-14 stsp #ifndef PROFILE
5882 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5883 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5884 43012d58 2019-07-14 stsp err(1, "pledge");
5886 234035bc 2019-06-01 stsp if (argc != 1)
5887 234035bc 2019-06-01 stsp usage_cherrypick();
5889 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5890 234035bc 2019-06-01 stsp if (cwd == NULL) {
5891 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5892 234035bc 2019-06-01 stsp goto done;
5894 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5895 234035bc 2019-06-01 stsp if (error)
5896 234035bc 2019-06-01 stsp goto done;
5898 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5900 234035bc 2019-06-01 stsp if (error != NULL)
5901 234035bc 2019-06-01 stsp goto done;
5903 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5904 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5905 234035bc 2019-06-01 stsp if (error)
5906 234035bc 2019-06-01 stsp goto done;
5908 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5909 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5910 234035bc 2019-06-01 stsp if (error != NULL) {
5911 234035bc 2019-06-01 stsp struct got_reference *ref;
5912 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5913 234035bc 2019-06-01 stsp goto done;
5914 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5915 234035bc 2019-06-01 stsp if (error != NULL)
5916 234035bc 2019-06-01 stsp goto done;
5917 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5918 234035bc 2019-06-01 stsp got_ref_close(ref);
5919 234035bc 2019-06-01 stsp if (error != NULL)
5920 234035bc 2019-06-01 stsp goto done;
5922 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5923 234035bc 2019-06-01 stsp if (error)
5924 234035bc 2019-06-01 stsp goto done;
5926 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5927 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5928 234035bc 2019-06-01 stsp if (error != NULL)
5929 234035bc 2019-06-01 stsp goto done;
5931 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5932 234035bc 2019-06-01 stsp if (error) {
5933 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5934 234035bc 2019-06-01 stsp goto done;
5935 234035bc 2019-06-01 stsp error = NULL;
5937 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5938 234035bc 2019-06-01 stsp goto done;
5941 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5942 234035bc 2019-06-01 stsp if (error)
5943 234035bc 2019-06-01 stsp goto done;
5944 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5945 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5946 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5948 234035bc 2019-06-01 stsp if (error != NULL)
5949 234035bc 2019-06-01 stsp goto done;
5951 234035bc 2019-06-01 stsp if (did_something)
5952 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5954 234035bc 2019-06-01 stsp if (commit)
5955 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5956 234035bc 2019-06-01 stsp free(commit_id_str);
5957 234035bc 2019-06-01 stsp if (head_ref)
5958 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5959 234035bc 2019-06-01 stsp if (worktree)
5960 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5962 234035bc 2019-06-01 stsp got_repo_close(repo);
5963 c4296144 2019-05-09 stsp return error;
5966 5ef14e63 2019-06-02 stsp __dead static void
5967 5ef14e63 2019-06-02 stsp usage_backout(void)
5969 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5973 5ef14e63 2019-06-02 stsp static const struct got_error *
5974 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5976 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5977 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5978 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5979 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5980 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5981 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5982 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5983 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5984 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5986 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5987 5ef14e63 2019-06-02 stsp switch (ch) {
5989 5ef14e63 2019-06-02 stsp usage_backout();
5990 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5994 5ef14e63 2019-06-02 stsp argc -= optind;
5995 5ef14e63 2019-06-02 stsp argv += optind;
5997 43012d58 2019-07-14 stsp #ifndef PROFILE
5998 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5999 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6000 43012d58 2019-07-14 stsp err(1, "pledge");
6002 5ef14e63 2019-06-02 stsp if (argc != 1)
6003 5ef14e63 2019-06-02 stsp usage_backout();
6005 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6006 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6007 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6008 5ef14e63 2019-06-02 stsp goto done;
6010 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6011 5ef14e63 2019-06-02 stsp if (error)
6012 5ef14e63 2019-06-02 stsp goto done;
6014 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6016 5ef14e63 2019-06-02 stsp if (error != NULL)
6017 5ef14e63 2019-06-02 stsp goto done;
6019 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6020 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6021 5ef14e63 2019-06-02 stsp if (error)
6022 5ef14e63 2019-06-02 stsp goto done;
6024 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6025 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6026 5ef14e63 2019-06-02 stsp if (error != NULL) {
6027 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6028 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6029 5ef14e63 2019-06-02 stsp goto done;
6030 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6031 5ef14e63 2019-06-02 stsp if (error != NULL)
6032 5ef14e63 2019-06-02 stsp goto done;
6033 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6034 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6035 5ef14e63 2019-06-02 stsp if (error != NULL)
6036 5ef14e63 2019-06-02 stsp goto done;
6038 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6039 5ef14e63 2019-06-02 stsp if (error)
6040 5ef14e63 2019-06-02 stsp goto done;
6042 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6043 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6044 5ef14e63 2019-06-02 stsp if (error != NULL)
6045 5ef14e63 2019-06-02 stsp goto done;
6047 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6048 5ef14e63 2019-06-02 stsp if (error)
6049 5ef14e63 2019-06-02 stsp goto done;
6051 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6052 5ef14e63 2019-06-02 stsp if (error)
6053 5ef14e63 2019-06-02 stsp goto done;
6054 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6055 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6056 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6057 5ef14e63 2019-06-02 stsp goto done;
6060 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6061 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6062 5ef14e63 2019-06-02 stsp if (error != NULL)
6063 5ef14e63 2019-06-02 stsp goto done;
6065 5ef14e63 2019-06-02 stsp if (did_something)
6066 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6068 5ef14e63 2019-06-02 stsp if (commit)
6069 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6070 5ef14e63 2019-06-02 stsp free(commit_id_str);
6071 5ef14e63 2019-06-02 stsp if (head_ref)
6072 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6073 818c7501 2019-07-11 stsp if (worktree)
6074 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6076 818c7501 2019-07-11 stsp got_repo_close(repo);
6077 818c7501 2019-07-11 stsp return error;
6080 818c7501 2019-07-11 stsp __dead static void
6081 818c7501 2019-07-11 stsp usage_rebase(void)
6083 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6084 af54c8f8 2019-07-11 stsp getprogname());
6089 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6092 0ebf8283 2019-07-24 stsp size_t len;
6094 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6095 0ebf8283 2019-07-24 stsp if (len > limit)
6096 0ebf8283 2019-07-24 stsp len = limit;
6097 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6098 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6100 0ebf8283 2019-07-24 stsp *nl = '\0';
6103 0ebf8283 2019-07-24 stsp static const struct got_error *
6104 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6106 5943eee2 2019-08-13 stsp const struct got_error *err;
6107 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6108 5943eee2 2019-08-13 stsp const char *s;
6110 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6112 5943eee2 2019-08-13 stsp return err;
6114 5943eee2 2019-08-13 stsp s = logmsg0;
6115 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6118 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6119 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6120 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6121 5943eee2 2019-08-13 stsp goto done;
6124 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6126 5943eee2 2019-08-13 stsp free(logmsg0);
6127 a0ea4fc0 2020-02-28 stsp return err;
6130 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6131 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6133 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6134 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6135 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6137 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6139 a0ea4fc0 2020-02-28 stsp return err;
6141 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6143 a0ea4fc0 2020-02-28 stsp goto done;
6145 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6147 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6149 a0ea4fc0 2020-02-28 stsp goto done;
6151 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6153 a0ea4fc0 2020-02-28 stsp free(id_str);
6154 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6155 a0ea4fc0 2020-02-28 stsp free(logmsg);
6156 5943eee2 2019-08-13 stsp return err;
6159 818c7501 2019-07-11 stsp static const struct got_error *
6160 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6161 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6163 818c7501 2019-07-11 stsp const struct got_error *err;
6164 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6166 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6168 818c7501 2019-07-11 stsp goto done;
6170 ff0d2220 2019-07-11 stsp if (new_id) {
6171 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6173 ff0d2220 2019-07-11 stsp goto done;
6176 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6177 ff0d2220 2019-07-11 stsp if (new_id_str)
6178 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6180 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6182 0ebf8283 2019-07-24 stsp goto done;
6184 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6185 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6187 818c7501 2019-07-11 stsp free(old_id_str);
6188 818c7501 2019-07-11 stsp free(new_id_str);
6189 272a1371 2020-02-28 stsp free(logmsg);
6190 818c7501 2019-07-11 stsp return err;
6193 1ee397ad 2019-07-12 stsp static const struct got_error *
6194 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6196 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6198 818c7501 2019-07-11 stsp while (path[0] == '/')
6200 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6202 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6203 1ee397ad 2019-07-12 stsp return NULL;
6204 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6205 818c7501 2019-07-11 stsp *rebase_status = status;
6206 1ee397ad 2019-07-12 stsp return NULL;
6209 818c7501 2019-07-11 stsp static const struct got_error *
6210 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6211 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6212 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6214 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6215 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6216 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6219 818c7501 2019-07-11 stsp static const struct got_error *
6220 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6221 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6222 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6223 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6225 ff0d2220 2019-07-11 stsp const struct got_error *error;
6226 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6227 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6229 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6230 ff0d2220 2019-07-11 stsp if (error)
6231 ff0d2220 2019-07-11 stsp return error;
6233 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6234 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6235 ff0d2220 2019-07-11 stsp if (error) {
6236 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6237 ff0d2220 2019-07-11 stsp goto done;
6238 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6240 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6241 ff0d2220 2019-07-11 stsp free(new_commit_id);
6244 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6245 ff0d2220 2019-07-11 stsp return error;
6248 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6249 64c6d990 2019-07-11 stsp const char *path_prefix;
6250 64c6d990 2019-07-11 stsp size_t len;
6251 8ca9bd68 2019-07-25 stsp int errcode;
6254 64c6d990 2019-07-11 stsp static const struct got_error *
6255 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6256 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6257 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6258 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6260 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6262 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6263 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6264 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6266 64c6d990 2019-07-11 stsp return NULL;
6269 64c6d990 2019-07-11 stsp static const struct got_error *
6270 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6271 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6272 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6274 64c6d990 2019-07-11 stsp const struct got_error *err;
6275 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6276 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6277 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6279 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6280 64c6d990 2019-07-11 stsp return NULL;
6282 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6284 64c6d990 2019-07-11 stsp goto done;
6286 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6288 64c6d990 2019-07-11 stsp goto done;
6290 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6291 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6293 64c6d990 2019-07-11 stsp goto done;
6295 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6296 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6298 64c6d990 2019-07-11 stsp goto done;
6300 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6301 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6302 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6303 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6304 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6305 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6306 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6308 64c6d990 2019-07-11 stsp if (tree1)
6309 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6310 64c6d990 2019-07-11 stsp if (tree2)
6311 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6312 64c6d990 2019-07-11 stsp if (commit)
6313 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6314 64c6d990 2019-07-11 stsp if (parent_commit)
6315 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6316 64c6d990 2019-07-11 stsp return err;
6319 ff0d2220 2019-07-11 stsp static const struct got_error *
6320 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6321 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6322 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6323 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6324 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6326 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6327 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6328 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6329 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6330 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6332 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6334 af61c510 2019-07-19 stsp return err;
6336 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6337 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6339 af61c510 2019-07-19 stsp goto done;
6340 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6341 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6342 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6343 af61c510 2019-07-19 stsp if (err) {
6344 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6345 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6346 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6347 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6348 af61c510 2019-07-19 stsp "been reached?!?");
6350 ee780d5c 2020-01-04 stsp goto done;
6352 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6353 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6355 af61c510 2019-07-19 stsp goto done;
6357 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6359 af61c510 2019-07-19 stsp goto done;
6360 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6361 af61c510 2019-07-19 stsp commit_id = parent_id;
6365 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6366 af61c510 2019-07-19 stsp return err;
6369 af61c510 2019-07-19 stsp static const struct got_error *
6370 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6372 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6373 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6374 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6375 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6376 818c7501 2019-07-11 stsp char *cwd = NULL;
6377 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6378 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6379 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6380 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6381 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6382 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6383 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6384 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6385 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6386 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6387 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6388 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6389 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6391 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6392 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6394 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6395 818c7501 2019-07-11 stsp switch (ch) {
6397 818c7501 2019-07-11 stsp abort_rebase = 1;
6400 818c7501 2019-07-11 stsp continue_rebase = 1;
6403 818c7501 2019-07-11 stsp usage_rebase();
6404 818c7501 2019-07-11 stsp /* NOTREACHED */
6408 818c7501 2019-07-11 stsp argc -= optind;
6409 818c7501 2019-07-11 stsp argv += optind;
6411 43012d58 2019-07-14 stsp #ifndef PROFILE
6412 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6413 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6414 43012d58 2019-07-14 stsp err(1, "pledge");
6416 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6417 818c7501 2019-07-11 stsp usage_rebase();
6418 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6419 818c7501 2019-07-11 stsp if (argc != 0)
6420 818c7501 2019-07-11 stsp usage_rebase();
6421 818c7501 2019-07-11 stsp } else if (argc != 1)
6422 818c7501 2019-07-11 stsp usage_rebase();
6424 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6425 818c7501 2019-07-11 stsp if (cwd == NULL) {
6426 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6427 818c7501 2019-07-11 stsp goto done;
6429 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6430 818c7501 2019-07-11 stsp if (error)
6431 818c7501 2019-07-11 stsp goto done;
6433 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6435 818c7501 2019-07-11 stsp if (error != NULL)
6436 818c7501 2019-07-11 stsp goto done;
6438 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6439 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6440 7ef62c4e 2020-02-24 stsp if (error)
6441 7ef62c4e 2020-02-24 stsp goto done;
6443 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6444 7ef62c4e 2020-02-24 stsp worktree);
6445 818c7501 2019-07-11 stsp if (error)
6446 7ef62c4e 2020-02-24 stsp goto done;
6447 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6448 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6449 818c7501 2019-07-11 stsp goto done;
6452 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6453 818c7501 2019-07-11 stsp if (error)
6454 818c7501 2019-07-11 stsp goto done;
6456 f6794adc 2019-07-23 stsp if (abort_rebase) {
6457 818c7501 2019-07-11 stsp int did_something;
6458 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6459 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6460 f6794adc 2019-07-23 stsp goto done;
6462 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6463 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6464 3e3a69f1 2019-07-25 stsp worktree, repo);
6465 818c7501 2019-07-11 stsp if (error)
6466 818c7501 2019-07-11 stsp goto done;
6467 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6468 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6469 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6470 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6471 818c7501 2019-07-11 stsp if (error)
6472 818c7501 2019-07-11 stsp goto done;
6473 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6474 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6477 818c7501 2019-07-11 stsp if (continue_rebase) {
6478 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6479 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6480 f6794adc 2019-07-23 stsp goto done;
6482 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6483 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6484 3e3a69f1 2019-07-25 stsp worktree, repo);
6485 818c7501 2019-07-11 stsp if (error)
6486 818c7501 2019-07-11 stsp goto done;
6488 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6489 01757395 2019-07-12 stsp resume_commit_id, repo);
6490 818c7501 2019-07-11 stsp if (error)
6491 818c7501 2019-07-11 stsp goto done;
6493 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6494 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6495 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6496 818c7501 2019-07-11 stsp goto done;
6499 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6500 818c7501 2019-07-11 stsp if (error != NULL)
6501 818c7501 2019-07-11 stsp goto done;
6504 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6505 ff0d2220 2019-07-11 stsp if (error)
6506 ff0d2220 2019-07-11 stsp goto done;
6508 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6509 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6511 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6512 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6513 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6514 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6515 818c7501 2019-07-11 stsp if (error)
6516 818c7501 2019-07-11 stsp goto done;
6517 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6518 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6519 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6520 818c7501 2019-07-11 stsp "with work tree's branch");
6521 818c7501 2019-07-11 stsp goto done;
6524 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6525 a51a74b3 2019-07-27 stsp if (error) {
6526 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6527 a51a74b3 2019-07-27 stsp goto done;
6528 a51a74b3 2019-07-27 stsp error = NULL;
6530 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6531 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6532 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6533 a51a74b3 2019-07-27 stsp goto done;
6535 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6536 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6537 818c7501 2019-07-11 stsp if (error)
6538 818c7501 2019-07-11 stsp goto done;
6541 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6542 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6543 818c7501 2019-07-11 stsp if (error)
6544 818c7501 2019-07-11 stsp goto done;
6546 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6547 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6548 fc66b545 2019-08-12 stsp if (pid == NULL) {
6549 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6550 fc66b545 2019-08-12 stsp int did_something;
6551 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6552 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6553 fc66b545 2019-08-12 stsp &did_something);
6554 fc66b545 2019-08-12 stsp if (error)
6555 fc66b545 2019-08-12 stsp goto done;
6556 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6557 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6559 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6560 fc66b545 2019-08-12 stsp goto done;
6562 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6563 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6564 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6565 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6566 818c7501 2019-07-11 stsp commit = NULL;
6567 818c7501 2019-07-11 stsp if (error)
6568 818c7501 2019-07-11 stsp goto done;
6570 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6571 38b0338b 2019-11-29 stsp if (continue_rebase) {
6572 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6573 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6574 38b0338b 2019-11-29 stsp goto done;
6576 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6577 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6578 38b0338b 2019-11-29 stsp char *id_str;
6579 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6580 38b0338b 2019-11-29 stsp new_base_branch);
6581 38b0338b 2019-11-29 stsp if (error)
6582 38b0338b 2019-11-29 stsp goto done;
6583 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6584 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6585 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6586 38b0338b 2019-11-29 stsp free(id_str);
6587 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6588 38b0338b 2019-11-29 stsp new_head_commit_id);
6589 38b0338b 2019-11-29 stsp if (error)
6590 38b0338b 2019-11-29 stsp goto done;
6594 818c7501 2019-07-11 stsp pid = NULL;
6595 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6596 818c7501 2019-07-11 stsp commit_id = qid->id;
6597 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6598 818c7501 2019-07-11 stsp pid = qid;
6600 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6601 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6602 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6603 818c7501 2019-07-11 stsp if (error)
6604 818c7501 2019-07-11 stsp goto done;
6606 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6607 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6608 a0ea4fc0 2020-02-28 stsp if (error)
6609 a0ea4fc0 2020-02-28 stsp goto done;
6610 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6614 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6615 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6616 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6617 818c7501 2019-07-11 stsp if (error)
6618 818c7501 2019-07-11 stsp goto done;
6621 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6622 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6623 818c7501 2019-07-11 stsp if (error)
6624 818c7501 2019-07-11 stsp goto done;
6625 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6626 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6628 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6629 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6631 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6632 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6633 818c7501 2019-07-11 stsp free(resume_commit_id);
6634 818c7501 2019-07-11 stsp free(yca_id);
6635 818c7501 2019-07-11 stsp if (commit)
6636 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6637 818c7501 2019-07-11 stsp if (branch)
6638 818c7501 2019-07-11 stsp got_ref_close(branch);
6639 818c7501 2019-07-11 stsp if (new_base_branch)
6640 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6641 818c7501 2019-07-11 stsp if (tmp_branch)
6642 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6643 5ef14e63 2019-06-02 stsp if (worktree)
6644 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6646 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6647 5ef14e63 2019-06-02 stsp return error;
6650 0ebf8283 2019-07-24 stsp __dead static void
6651 0ebf8283 2019-07-24 stsp usage_histedit(void)
6653 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6654 0ebf8283 2019-07-24 stsp getprogname());
6658 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6659 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6660 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6661 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6662 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6664 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6665 0ebf8283 2019-07-24 stsp unsigned char code;
6666 0ebf8283 2019-07-24 stsp const char *name;
6667 0ebf8283 2019-07-24 stsp const char *desc;
6668 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6669 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6670 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6671 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6672 82997472 2020-01-29 stsp "be used" },
6673 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6674 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6675 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6678 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6679 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6680 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6681 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6682 0ebf8283 2019-07-24 stsp char *logmsg;
6684 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6686 0ebf8283 2019-07-24 stsp static const struct got_error *
6687 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6688 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6690 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6691 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6692 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6695 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6697 0ebf8283 2019-07-24 stsp goto done;
6699 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6701 0ebf8283 2019-07-24 stsp goto done;
6703 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6705 0ebf8283 2019-07-24 stsp goto done;
6707 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6708 0ebf8283 2019-07-24 stsp if (n < 0)
6709 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6711 0ebf8283 2019-07-24 stsp if (commit)
6712 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6713 0ebf8283 2019-07-24 stsp free(id_str);
6714 0ebf8283 2019-07-24 stsp free(logmsg);
6715 0ebf8283 2019-07-24 stsp return err;
6718 0ebf8283 2019-07-24 stsp static const struct got_error *
6719 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6720 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6722 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6723 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6725 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6726 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6728 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6729 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6733 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6734 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6735 083957f4 2020-02-24 stsp if (n < 0) {
6736 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6742 0ebf8283 2019-07-24 stsp return err;
6745 0ebf8283 2019-07-24 stsp static const struct got_error *
6746 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6747 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6749 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6751 514f2ffe 2020-01-29 stsp char *id_str;
6752 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6754 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6755 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6757 514f2ffe 2020-01-29 stsp return err;
6759 514f2ffe 2020-01-29 stsp n = fprintf(f,
6760 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6761 514f2ffe 2020-01-29 stsp "# commit %s\n"
6762 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6763 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6764 514f2ffe 2020-01-29 stsp if (n < 0) {
6765 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6766 514f2ffe 2020-01-29 stsp goto done;
6769 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6770 514f2ffe 2020-01-29 stsp if (n < 0) {
6771 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6772 514f2ffe 2020-01-29 stsp goto done;
6775 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6776 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6777 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6778 0ebf8283 2019-07-24 stsp cmd->desc);
6779 0ebf8283 2019-07-24 stsp if (n < 0) {
6780 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6785 514f2ffe 2020-01-29 stsp free(id_str);
6786 0ebf8283 2019-07-24 stsp return err;
6789 0ebf8283 2019-07-24 stsp static const struct got_error *
6790 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6792 0ebf8283 2019-07-24 stsp static char msg[42];
6795 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6797 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6798 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6800 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6803 0ebf8283 2019-07-24 stsp static const struct got_error *
6804 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6805 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6807 0ebf8283 2019-07-24 stsp const struct got_error *err;
6808 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6809 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6811 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6813 0ebf8283 2019-07-24 stsp return err;
6815 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6817 0ebf8283 2019-07-24 stsp goto done;
6819 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6821 5943eee2 2019-08-13 stsp goto done;
6822 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6823 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6824 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6825 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6828 0ebf8283 2019-07-24 stsp if (folded_commit)
6829 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6830 0ebf8283 2019-07-24 stsp free(id_str);
6831 5943eee2 2019-08-13 stsp free(folded_logmsg);
6832 0ebf8283 2019-07-24 stsp return err;
6835 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6836 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6838 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6840 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6841 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6842 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6843 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6844 3f9de99f 2019-07-24 stsp folded = prev;
6845 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6848 0ebf8283 2019-07-24 stsp return folded;
6851 0ebf8283 2019-07-24 stsp static const struct got_error *
6852 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6853 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6855 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6856 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6857 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6858 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6860 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6862 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6864 0ebf8283 2019-07-24 stsp return err;
6866 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6867 0ebf8283 2019-07-24 stsp if (folded) {
6868 0ebf8283 2019-07-24 stsp while (folded != hle) {
6869 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6870 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6873 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6874 0ebf8283 2019-07-24 stsp logmsg, repo);
6876 0ebf8283 2019-07-24 stsp goto done;
6877 0ebf8283 2019-07-24 stsp free(logmsg);
6878 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6879 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6883 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6885 0ebf8283 2019-07-24 stsp goto done;
6886 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6888 5943eee2 2019-08-13 stsp goto done;
6889 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6890 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6891 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6892 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6893 0ebf8283 2019-07-24 stsp goto done;
6895 0ebf8283 2019-07-24 stsp free(logmsg);
6896 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6898 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6900 0ebf8283 2019-07-24 stsp goto done;
6902 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6903 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6905 0ebf8283 2019-07-24 stsp goto done;
6907 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6908 0ebf8283 2019-07-24 stsp close(fd);
6910 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6912 0ebf8283 2019-07-24 stsp goto done;
6914 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6915 0ebf8283 2019-07-24 stsp if (err) {
6916 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6917 0ebf8283 2019-07-24 stsp goto done;
6918 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6921 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6922 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6923 0ebf8283 2019-07-24 stsp free(logmsg_path);
6924 0ebf8283 2019-07-24 stsp free(logmsg);
6925 5943eee2 2019-08-13 stsp free(orig_logmsg);
6926 0ebf8283 2019-07-24 stsp free(editor);
6927 0ebf8283 2019-07-24 stsp if (commit)
6928 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6929 0ebf8283 2019-07-24 stsp return err;
6932 0ebf8283 2019-07-24 stsp static const struct got_error *
6933 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6934 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6936 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6937 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6938 0ebf8283 2019-07-24 stsp size_t size;
6939 0ebf8283 2019-07-24 stsp ssize_t len;
6940 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6941 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6942 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6943 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6945 0ebf8283 2019-07-24 stsp for (;;) {
6946 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6947 0ebf8283 2019-07-24 stsp if (len == -1) {
6948 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6949 0ebf8283 2019-07-24 stsp if (feof(f))
6951 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6952 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6957 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6959 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6960 0ebf8283 2019-07-24 stsp free(line);
6961 0ebf8283 2019-07-24 stsp line = NULL;
6964 0ebf8283 2019-07-24 stsp cmd = NULL;
6965 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6966 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6967 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6968 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6969 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6972 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6977 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6978 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6981 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6983 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6984 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6985 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6988 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6989 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6993 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6994 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6995 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6999 0ebf8283 2019-07-24 stsp free(line);
7000 0ebf8283 2019-07-24 stsp line = NULL;
7004 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7006 0ebf8283 2019-07-24 stsp *end = '\0';
7008 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7009 0ebf8283 2019-07-24 stsp if (err) {
7010 0ebf8283 2019-07-24 stsp /* override error code */
7011 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7015 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7016 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7017 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7020 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7021 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7022 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7023 0ebf8283 2019-07-24 stsp commit_id = NULL;
7024 0ebf8283 2019-07-24 stsp free(line);
7025 0ebf8283 2019-07-24 stsp line = NULL;
7026 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7029 0ebf8283 2019-07-24 stsp free(line);
7030 0ebf8283 2019-07-24 stsp free(commit_id);
7031 0ebf8283 2019-07-24 stsp return err;
7034 0ebf8283 2019-07-24 stsp static const struct got_error *
7035 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7036 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7038 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7039 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7040 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7041 5b87815e 2020-03-05 stsp static char msg[92];
7042 bfce7f83 2019-07-27 stsp char *id_str;
7044 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7045 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7046 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7047 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7048 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7050 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7051 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7052 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7053 5b87815e 2020-03-05 stsp if (hle == hle2)
7055 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7056 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7058 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7060 5b87815e 2020-03-05 stsp return err;
7061 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7062 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7063 5b87815e 2020-03-05 stsp free(id_str);
7064 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7068 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7069 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7070 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7073 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7074 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7076 bfce7f83 2019-07-27 stsp return err;
7077 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7078 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7079 bfce7f83 2019-07-27 stsp free(id_str);
7080 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7084 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7085 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7086 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7087 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7089 bfce7f83 2019-07-27 stsp return NULL;
7092 bfce7f83 2019-07-27 stsp static const struct got_error *
7093 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7094 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7095 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7097 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7098 0ebf8283 2019-07-24 stsp char *editor;
7099 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7101 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7103 0ebf8283 2019-07-24 stsp return err;
7105 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7106 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7107 0ebf8283 2019-07-24 stsp goto done;
7110 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7111 0ebf8283 2019-07-24 stsp if (f == NULL) {
7112 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7113 0ebf8283 2019-07-24 stsp goto done;
7115 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7117 bfce7f83 2019-07-27 stsp goto done;
7119 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7121 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7122 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7123 0ebf8283 2019-07-24 stsp free(editor);
7124 0ebf8283 2019-07-24 stsp return err;
7127 0ebf8283 2019-07-24 stsp static const struct got_error *
7128 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7129 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7130 514f2ffe 2020-01-29 stsp struct got_repository *);
7132 0ebf8283 2019-07-24 stsp static const struct got_error *
7133 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7134 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7135 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7137 0ebf8283 2019-07-24 stsp const struct got_error *err;
7138 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7139 0ebf8283 2019-07-24 stsp char *path = NULL;
7141 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7143 0ebf8283 2019-07-24 stsp return err;
7145 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7147 0ebf8283 2019-07-24 stsp goto done;
7149 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7151 0ebf8283 2019-07-24 stsp goto done;
7153 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7154 083957f4 2020-02-24 stsp rewind(f);
7155 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7157 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7158 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7159 0ebf8283 2019-07-24 stsp goto done;
7162 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7163 083957f4 2020-02-24 stsp if (err) {
7164 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7165 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7166 083957f4 2020-02-24 stsp goto done;
7167 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7168 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7172 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7173 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7174 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7175 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7176 0ebf8283 2019-07-24 stsp free(path);
7177 0ebf8283 2019-07-24 stsp return err;
7180 0ebf8283 2019-07-24 stsp static const struct got_error *
7181 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7182 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7184 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7185 0ebf8283 2019-07-24 stsp char *path = NULL;
7186 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7187 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7188 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7190 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7192 0ebf8283 2019-07-24 stsp return err;
7194 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7195 0ebf8283 2019-07-24 stsp if (f == NULL) {
7196 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7197 0ebf8283 2019-07-24 stsp goto done;
7199 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7200 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7205 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7206 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7207 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7208 0ebf8283 2019-07-24 stsp if (n < 0) {
7209 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7215 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7216 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7217 0ebf8283 2019-07-24 stsp free(path);
7218 0ebf8283 2019-07-24 stsp if (commit)
7219 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7220 0ebf8283 2019-07-24 stsp return err;
7224 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7226 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7228 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7229 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7230 bfce7f83 2019-07-27 stsp free(hle);
7234 0ebf8283 2019-07-24 stsp static const struct got_error *
7235 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7236 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7238 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7239 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7241 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7242 0ebf8283 2019-07-24 stsp if (f == NULL) {
7243 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7244 0ebf8283 2019-07-24 stsp goto done;
7247 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7249 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7250 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7251 0ebf8283 2019-07-24 stsp return err;
7254 0ebf8283 2019-07-24 stsp static const struct got_error *
7255 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7256 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7257 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7259 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7260 0ebf8283 2019-07-24 stsp int resp = ' ';
7262 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7263 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7264 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7265 0ebf8283 2019-07-24 stsp resp = getchar();
7266 a61a4414 2019-08-07 stsp if (resp == '\n')
7267 a61a4414 2019-08-07 stsp resp = getchar();
7268 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7269 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7270 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7272 426ebf2e 2019-07-25 stsp if (err) {
7273 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7274 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7276 bfce7f83 2019-07-27 stsp prev_err = err;
7277 426ebf2e 2019-07-25 stsp resp = ' ';
7281 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7282 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7283 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7284 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7285 426ebf2e 2019-07-25 stsp if (err) {
7286 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7287 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7289 bfce7f83 2019-07-27 stsp prev_err = err;
7290 426ebf2e 2019-07-25 stsp resp = ' ';
7294 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7295 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7298 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7301 0ebf8283 2019-07-24 stsp return err;
7304 0ebf8283 2019-07-24 stsp static const struct got_error *
7305 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7306 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7307 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7309 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7310 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7311 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7312 3e3a69f1 2019-07-25 stsp branch, repo);
7315 0ebf8283 2019-07-24 stsp static const struct got_error *
7316 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7317 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7319 0ebf8283 2019-07-24 stsp const struct got_error *err;
7320 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7322 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7324 0ebf8283 2019-07-24 stsp goto done;
7326 0ebf8283 2019-07-24 stsp if (new_id) {
7327 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7329 0ebf8283 2019-07-24 stsp goto done;
7332 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7333 0ebf8283 2019-07-24 stsp if (new_id_str)
7334 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7336 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7337 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7338 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7339 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7340 0ebf8283 2019-07-24 stsp goto done;
7342 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7344 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7346 0ebf8283 2019-07-24 stsp goto done;
7349 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7350 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7351 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7352 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7353 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7355 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7356 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7357 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7364 0ebf8283 2019-07-24 stsp free(old_id_str);
7365 0ebf8283 2019-07-24 stsp free(new_id_str);
7366 0ebf8283 2019-07-24 stsp return err;
7369 0ebf8283 2019-07-24 stsp static const struct got_error *
7370 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7371 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7372 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7373 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7375 0ebf8283 2019-07-24 stsp const struct got_error *err;
7376 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7377 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7379 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7380 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7381 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7383 0ebf8283 2019-07-24 stsp return err;
7386 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7388 0ebf8283 2019-07-24 stsp return err;
7390 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7391 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7392 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7393 0ebf8283 2019-07-24 stsp if (err) {
7394 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7395 0ebf8283 2019-07-24 stsp goto done;
7396 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7398 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7399 0ebf8283 2019-07-24 stsp free(new_commit_id);
7402 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7403 0ebf8283 2019-07-24 stsp return err;
7406 0ebf8283 2019-07-24 stsp static const struct got_error *
7407 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7408 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7410 0ebf8283 2019-07-24 stsp const struct got_error *error;
7411 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7413 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7415 0ebf8283 2019-07-24 stsp if (error)
7416 0ebf8283 2019-07-24 stsp return error;
7418 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7419 0ebf8283 2019-07-24 stsp if (error)
7420 0ebf8283 2019-07-24 stsp return error;
7422 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7423 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7424 0ebf8283 2019-07-24 stsp return error;
7427 ab20a43a 2020-01-29 stsp static const struct got_error *
7428 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7429 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7430 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7431 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7433 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7435 ab20a43a 2020-01-29 stsp switch (status) {
7436 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7437 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7438 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7439 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7440 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7441 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7446 ab20a43a 2020-01-29 stsp switch (staged_status) {
7447 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7448 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7449 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7450 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7451 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7456 ab20a43a 2020-01-29 stsp return NULL;
7459 0ebf8283 2019-07-24 stsp static const struct got_error *
7460 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7462 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7463 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7464 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7465 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7466 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7467 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7468 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7469 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7470 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7471 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7472 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7473 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7474 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7475 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7476 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7477 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7478 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7479 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7480 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7481 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7482 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7483 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7485 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7486 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7487 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7489 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7490 0ebf8283 2019-07-24 stsp switch (ch) {
7492 0ebf8283 2019-07-24 stsp abort_edit = 1;
7495 0ebf8283 2019-07-24 stsp continue_edit = 1;
7498 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7501 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7504 0ebf8283 2019-07-24 stsp usage_histedit();
7505 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7509 0ebf8283 2019-07-24 stsp argc -= optind;
7510 0ebf8283 2019-07-24 stsp argv += optind;
7512 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7513 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7514 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7515 0ebf8283 2019-07-24 stsp err(1, "pledge");
7517 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7518 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7519 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7520 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7521 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7522 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7523 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7524 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7525 0ebf8283 2019-07-24 stsp if (argc != 0)
7526 0ebf8283 2019-07-24 stsp usage_histedit();
7529 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7530 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7531 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7532 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7533 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7534 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7537 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7538 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7539 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7540 0ebf8283 2019-07-24 stsp goto done;
7542 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7543 0ebf8283 2019-07-24 stsp if (error)
7544 0ebf8283 2019-07-24 stsp goto done;
7546 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7548 0ebf8283 2019-07-24 stsp if (error != NULL)
7549 0ebf8283 2019-07-24 stsp goto done;
7551 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7552 0ebf8283 2019-07-24 stsp if (error)
7553 0ebf8283 2019-07-24 stsp goto done;
7554 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7555 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7556 0ebf8283 2019-07-24 stsp goto done;
7559 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7560 0ebf8283 2019-07-24 stsp if (error)
7561 0ebf8283 2019-07-24 stsp goto done;
7563 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7564 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7565 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7566 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7567 083957f4 2020-02-24 stsp "before the -m option can be used");
7568 083957f4 2020-02-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7572 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7573 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7574 3e3a69f1 2019-07-25 stsp worktree, repo);
7575 0ebf8283 2019-07-24 stsp if (error)
7576 0ebf8283 2019-07-24 stsp goto done;
7577 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7578 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7579 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7580 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7581 0ebf8283 2019-07-24 stsp if (error)
7582 0ebf8283 2019-07-24 stsp goto done;
7583 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7584 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7585 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7586 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7587 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7588 0ebf8283 2019-07-24 stsp goto done;
7591 0ebf8283 2019-07-24 stsp if (continue_edit) {
7592 0ebf8283 2019-07-24 stsp char *path;
7594 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7595 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7596 0ebf8283 2019-07-24 stsp goto done;
7599 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7600 0ebf8283 2019-07-24 stsp if (error)
7601 0ebf8283 2019-07-24 stsp goto done;
7603 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7604 0ebf8283 2019-07-24 stsp free(path);
7605 0ebf8283 2019-07-24 stsp if (error)
7606 0ebf8283 2019-07-24 stsp goto done;
7608 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7609 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7610 3e3a69f1 2019-07-25 stsp worktree, repo);
7611 0ebf8283 2019-07-24 stsp if (error)
7612 0ebf8283 2019-07-24 stsp goto done;
7614 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7615 0ebf8283 2019-07-24 stsp if (error)
7616 0ebf8283 2019-07-24 stsp goto done;
7618 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7619 0ebf8283 2019-07-24 stsp head_commit_id);
7620 0ebf8283 2019-07-24 stsp if (error)
7621 0ebf8283 2019-07-24 stsp goto done;
7622 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7623 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7624 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7625 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7626 3aac7cf7 2019-07-25 stsp goto done;
7628 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7629 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7630 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7631 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7632 0ebf8283 2019-07-24 stsp commit = NULL;
7633 0ebf8283 2019-07-24 stsp if (error)
7634 0ebf8283 2019-07-24 stsp goto done;
7636 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7637 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7638 0ebf8283 2019-07-24 stsp goto done;
7641 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7642 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7643 0ebf8283 2019-07-24 stsp if (error != NULL)
7644 0ebf8283 2019-07-24 stsp goto done;
7646 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7647 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7648 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7649 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7650 c7d20a3f 2019-07-30 stsp goto done;
7653 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7654 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7655 bfce7f83 2019-07-27 stsp branch = NULL;
7656 0ebf8283 2019-07-24 stsp if (error)
7657 0ebf8283 2019-07-24 stsp goto done;
7659 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7660 0ebf8283 2019-07-24 stsp head_commit_id);
7661 0ebf8283 2019-07-24 stsp if (error)
7662 0ebf8283 2019-07-24 stsp goto done;
7663 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7664 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7665 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7666 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7667 3aac7cf7 2019-07-25 stsp goto done;
7669 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7670 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7671 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7672 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7673 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7674 0ebf8283 2019-07-24 stsp commit = NULL;
7675 0ebf8283 2019-07-24 stsp if (error)
7676 0ebf8283 2019-07-24 stsp goto done;
7678 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7679 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7680 c5996fff 2020-01-29 stsp goto done;
7683 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7684 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7685 bfce7f83 2019-07-27 stsp if (error)
7686 bfce7f83 2019-07-27 stsp goto done;
7688 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7689 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7690 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7691 6ba2bea2 2019-07-27 stsp if (error) {
7692 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7693 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7694 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7695 0ebf8283 2019-07-24 stsp goto done;
7698 514f2ffe 2020-01-29 stsp const char *branch_name;
7699 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7700 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7701 514f2ffe 2020-01-29 stsp branch_name += 11;
7702 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7703 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7704 6ba2bea2 2019-07-27 stsp if (error) {
7705 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7706 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7707 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7708 0ebf8283 2019-07-24 stsp goto done;
7713 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7715 6ba2bea2 2019-07-27 stsp if (error) {
7716 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7717 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7718 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7719 0ebf8283 2019-07-24 stsp goto done;
7724 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7725 4ec14e60 2019-08-28 hiltjo if (error)
7726 0ebf8283 2019-07-24 stsp goto done;
7728 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7729 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7730 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7731 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7734 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7735 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7736 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7737 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7739 ab20a43a 2020-01-29 stsp if (error)
7740 ab20a43a 2020-01-29 stsp goto done;
7742 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7743 ab20a43a 2020-01-29 stsp int have_changes = 0;
7745 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7746 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7747 ab20a43a 2020-01-29 stsp if (error)
7748 ab20a43a 2020-01-29 stsp goto done;
7749 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7750 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7751 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7752 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7753 ab20a43a 2020-01-29 stsp if (error) {
7754 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7755 ab20a43a 2020-01-29 stsp goto done;
7756 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7757 ab20a43a 2020-01-29 stsp goto done;
7759 ab20a43a 2020-01-29 stsp if (have_changes) {
7760 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7761 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7762 ab20a43a 2020-01-29 stsp if (error)
7763 ab20a43a 2020-01-29 stsp goto done;
7765 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7766 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7767 ab20a43a 2020-01-29 stsp if (error)
7768 ab20a43a 2020-01-29 stsp goto done;
7769 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7770 ab20a43a 2020-01-29 stsp hle, NULL);
7771 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7772 ab20a43a 2020-01-29 stsp commit = NULL;
7773 ab20a43a 2020-01-29 stsp if (error)
7774 ab20a43a 2020-01-29 stsp goto done;
7780 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7781 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7782 0ebf8283 2019-07-24 stsp if (error)
7783 0ebf8283 2019-07-24 stsp goto done;
7787 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7788 0ebf8283 2019-07-24 stsp hle->commit_id);
7789 0ebf8283 2019-07-24 stsp if (error)
7790 0ebf8283 2019-07-24 stsp goto done;
7791 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7792 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7794 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7795 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7796 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7797 0ebf8283 2019-07-24 stsp if (error)
7798 0ebf8283 2019-07-24 stsp goto done;
7799 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7800 0ebf8283 2019-07-24 stsp commit = NULL;
7802 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7803 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7805 a0ea4fc0 2020-02-28 stsp if (error)
7806 a0ea4fc0 2020-02-28 stsp goto done;
7807 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7811 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7812 0ebf8283 2019-07-24 stsp char *id_str;
7813 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7814 0ebf8283 2019-07-24 stsp if (error)
7815 0ebf8283 2019-07-24 stsp goto done;
7816 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7818 0ebf8283 2019-07-24 stsp free(id_str);
7819 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7820 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7821 3e3a69f1 2019-07-25 stsp fileindex);
7822 0ebf8283 2019-07-24 stsp goto done;
7825 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7826 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7827 0ebf8283 2019-07-24 stsp if (error)
7828 0ebf8283 2019-07-24 stsp goto done;
7832 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7833 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7834 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7835 0ebf8283 2019-07-24 stsp if (error)
7836 0ebf8283 2019-07-24 stsp goto done;
7839 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7840 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7841 0ebf8283 2019-07-24 stsp if (error)
7842 0ebf8283 2019-07-24 stsp goto done;
7843 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7844 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7846 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7847 3e3a69f1 2019-07-25 stsp branch, repo);
7849 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7850 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7851 0ebf8283 2019-07-24 stsp free(head_commit_id);
7852 0ebf8283 2019-07-24 stsp free(base_commit_id);
7853 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7854 0ebf8283 2019-07-24 stsp if (commit)
7855 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7856 0ebf8283 2019-07-24 stsp if (branch)
7857 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7858 0ebf8283 2019-07-24 stsp if (tmp_branch)
7859 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7860 0ebf8283 2019-07-24 stsp if (worktree)
7861 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7863 2822a352 2019-10-15 stsp got_repo_close(repo);
7864 2822a352 2019-10-15 stsp return error;
7867 2822a352 2019-10-15 stsp __dead static void
7868 2822a352 2019-10-15 stsp usage_integrate(void)
7870 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7874 2822a352 2019-10-15 stsp static const struct got_error *
7875 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7877 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7878 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7879 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7880 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7881 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7882 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7883 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7884 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7885 2822a352 2019-10-15 stsp int ch, did_something = 0;
7887 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7888 2822a352 2019-10-15 stsp switch (ch) {
7890 2822a352 2019-10-15 stsp usage_integrate();
7891 2822a352 2019-10-15 stsp /* NOTREACHED */
7895 2822a352 2019-10-15 stsp argc -= optind;
7896 2822a352 2019-10-15 stsp argv += optind;
7898 2822a352 2019-10-15 stsp if (argc != 1)
7899 2822a352 2019-10-15 stsp usage_integrate();
7900 2822a352 2019-10-15 stsp branch_arg = argv[0];
7902 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7903 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7904 2822a352 2019-10-15 stsp err(1, "pledge");
7906 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7907 2822a352 2019-10-15 stsp if (cwd == NULL) {
7908 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7909 2822a352 2019-10-15 stsp goto done;
7912 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7913 2822a352 2019-10-15 stsp if (error)
7914 2822a352 2019-10-15 stsp goto done;
7916 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7917 2822a352 2019-10-15 stsp if (error)
7918 2822a352 2019-10-15 stsp goto done;
7920 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7922 2822a352 2019-10-15 stsp if (error != NULL)
7923 2822a352 2019-10-15 stsp goto done;
7925 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7926 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7927 2822a352 2019-10-15 stsp if (error)
7928 2822a352 2019-10-15 stsp goto done;
7930 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7931 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7932 2822a352 2019-10-15 stsp goto done;
7935 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7936 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7937 2822a352 2019-10-15 stsp if (error)
7938 2822a352 2019-10-15 stsp goto done;
7940 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7941 2822a352 2019-10-15 stsp if (refname == NULL) {
7942 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7943 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7944 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7945 2822a352 2019-10-15 stsp goto done;
7947 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7948 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7949 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7950 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7951 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7952 2822a352 2019-10-15 stsp goto done;
7955 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7956 2822a352 2019-10-15 stsp if (error)
7957 2822a352 2019-10-15 stsp goto done;
7959 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7960 2822a352 2019-10-15 stsp if (error)
7961 2822a352 2019-10-15 stsp goto done;
7963 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7964 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7965 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7966 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7967 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7968 2822a352 2019-10-15 stsp goto done;
7971 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7972 2822a352 2019-10-15 stsp if (error) {
7973 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7974 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7975 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7976 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7977 2822a352 2019-10-15 stsp goto done;
7980 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7981 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7982 2822a352 2019-10-15 stsp check_cancelled, NULL);
7983 2822a352 2019-10-15 stsp if (error)
7984 2822a352 2019-10-15 stsp goto done;
7986 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7989 715dc77e 2019-08-03 stsp got_repo_close(repo);
7990 2822a352 2019-10-15 stsp if (worktree)
7991 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7992 2822a352 2019-10-15 stsp free(cwd);
7993 2822a352 2019-10-15 stsp free(base_commit_id);
7994 2822a352 2019-10-15 stsp free(commit_id);
7995 2822a352 2019-10-15 stsp free(refname);
7996 2822a352 2019-10-15 stsp free(base_refname);
7997 715dc77e 2019-08-03 stsp return error;
8000 715dc77e 2019-08-03 stsp __dead static void
8001 715dc77e 2019-08-03 stsp usage_stage(void)
8003 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8004 f3055044 2019-08-07 stsp "[file-path ...]\n",
8005 715dc77e 2019-08-03 stsp getprogname());
8009 715dc77e 2019-08-03 stsp static const struct got_error *
8010 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8011 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8012 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8013 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8015 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8016 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8018 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8019 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8020 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8021 408b4ebc 2019-08-03 stsp return NULL;
8023 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8024 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8025 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8027 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8029 408b4ebc 2019-08-03 stsp return err;
8031 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8032 408b4ebc 2019-08-03 stsp free(id_str);
8033 dc424a06 2019-08-07 stsp return NULL;
8036 dc424a06 2019-08-07 stsp static const struct got_error *
8037 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8039 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8040 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8041 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8042 715dc77e 2019-08-03 stsp char *cwd = NULL;
8043 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8044 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8045 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8046 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8047 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8048 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8050 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8052 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8053 715dc77e 2019-08-03 stsp switch (ch) {
8055 408b4ebc 2019-08-03 stsp list_stage = 1;
8058 dc424a06 2019-08-07 stsp pflag = 1;
8061 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8064 715dc77e 2019-08-03 stsp usage_stage();
8065 715dc77e 2019-08-03 stsp /* NOTREACHED */
8069 715dc77e 2019-08-03 stsp argc -= optind;
8070 715dc77e 2019-08-03 stsp argv += optind;
8072 715dc77e 2019-08-03 stsp #ifndef PROFILE
8073 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8074 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8075 715dc77e 2019-08-03 stsp err(1, "pledge");
8077 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8078 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8079 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8080 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8082 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8083 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8084 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8085 715dc77e 2019-08-03 stsp goto done;
8088 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8089 715dc77e 2019-08-03 stsp if (error)
8090 715dc77e 2019-08-03 stsp goto done;
8092 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8094 715dc77e 2019-08-03 stsp if (error != NULL)
8095 715dc77e 2019-08-03 stsp goto done;
8097 dc424a06 2019-08-07 stsp if (patch_script_path) {
8098 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8099 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8100 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8101 dc424a06 2019-08-07 stsp patch_script_path);
8102 dc424a06 2019-08-07 stsp goto done;
8105 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8106 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8107 715dc77e 2019-08-03 stsp if (error)
8108 715dc77e 2019-08-03 stsp goto done;
8110 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8111 6d022e97 2019-08-04 stsp if (error)
8112 6d022e97 2019-08-04 stsp goto done;
8114 6d022e97 2019-08-04 stsp if (list_stage)
8115 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8116 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8118 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8119 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8120 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8121 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8122 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8125 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8126 2e1f37b0 2019-08-08 stsp error == NULL)
8127 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8129 ad493afc 2019-08-03 stsp got_repo_close(repo);
8130 ad493afc 2019-08-03 stsp if (worktree)
8131 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8132 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8133 ad493afc 2019-08-03 stsp free((char *)pe->path);
8134 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8135 ad493afc 2019-08-03 stsp free(cwd);
8136 ad493afc 2019-08-03 stsp return error;
8139 ad493afc 2019-08-03 stsp __dead static void
8140 ad493afc 2019-08-03 stsp usage_unstage(void)
8142 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8143 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8144 ad493afc 2019-08-03 stsp getprogname());
8149 ad493afc 2019-08-03 stsp static const struct got_error *
8150 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8152 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8153 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8154 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8155 ad493afc 2019-08-03 stsp char *cwd = NULL;
8156 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8157 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8158 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8159 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8160 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8161 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8163 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8165 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8166 ad493afc 2019-08-03 stsp switch (ch) {
8168 2e1f37b0 2019-08-08 stsp pflag = 1;
8171 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8174 ad493afc 2019-08-03 stsp usage_unstage();
8175 ad493afc 2019-08-03 stsp /* NOTREACHED */
8179 ad493afc 2019-08-03 stsp argc -= optind;
8180 ad493afc 2019-08-03 stsp argv += optind;
8182 ad493afc 2019-08-03 stsp #ifndef PROFILE
8183 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8184 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8185 ad493afc 2019-08-03 stsp err(1, "pledge");
8187 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8188 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8190 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8191 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8192 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8193 ad493afc 2019-08-03 stsp goto done;
8196 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8197 ad493afc 2019-08-03 stsp if (error)
8198 ad493afc 2019-08-03 stsp goto done;
8200 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8202 ad493afc 2019-08-03 stsp if (error != NULL)
8203 ad493afc 2019-08-03 stsp goto done;
8205 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8206 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8207 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8208 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8209 2e1f37b0 2019-08-08 stsp patch_script_path);
8210 2e1f37b0 2019-08-08 stsp goto done;
8214 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8215 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8216 ad493afc 2019-08-03 stsp if (error)
8217 ad493afc 2019-08-03 stsp goto done;
8219 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8220 ad493afc 2019-08-03 stsp if (error)
8221 ad493afc 2019-08-03 stsp goto done;
8223 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8224 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8225 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8226 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8228 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8229 2e1f37b0 2019-08-08 stsp error == NULL)
8230 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8232 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8233 715dc77e 2019-08-03 stsp if (worktree)
8234 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8235 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8236 715dc77e 2019-08-03 stsp free((char *)pe->path);
8237 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8238 715dc77e 2019-08-03 stsp free(cwd);
8239 01073a5d 2019-08-22 stsp return error;
8242 01073a5d 2019-08-22 stsp __dead static void
8243 01073a5d 2019-08-22 stsp usage_cat(void)
8245 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8246 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8250 01073a5d 2019-08-22 stsp static const struct got_error *
8251 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8253 01073a5d 2019-08-22 stsp const struct got_error *err;
8254 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8256 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8258 01073a5d 2019-08-22 stsp return err;
8260 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8261 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8262 01073a5d 2019-08-22 stsp return err;
8265 01073a5d 2019-08-22 stsp static const struct got_error *
8266 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8268 01073a5d 2019-08-22 stsp const struct got_error *err;
8269 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8270 56e0773d 2019-11-28 stsp int nentries, i;
8272 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8274 01073a5d 2019-08-22 stsp return err;
8276 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8277 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8278 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8279 01073a5d 2019-08-22 stsp char *id_str;
8280 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8282 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8283 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8286 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8287 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8288 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8289 01073a5d 2019-08-22 stsp free(id_str);
8292 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8293 01073a5d 2019-08-22 stsp return err;
8296 01073a5d 2019-08-22 stsp static const struct got_error *
8297 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8299 01073a5d 2019-08-22 stsp const struct got_error *err;
8300 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8301 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8302 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8303 01073a5d 2019-08-22 stsp char *id_str = NULL;
8304 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8306 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8308 01073a5d 2019-08-22 stsp return err;
8310 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8312 01073a5d 2019-08-22 stsp goto done;
8314 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8315 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8316 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8317 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8318 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8319 01073a5d 2019-08-22 stsp char *pid_str;
8320 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8322 01073a5d 2019-08-22 stsp goto done;
8323 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8324 01073a5d 2019-08-22 stsp free(pid_str);
8326 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8327 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8328 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8330 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8331 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8332 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8334 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8335 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8336 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8338 01073a5d 2019-08-22 stsp free(id_str);
8339 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8340 01073a5d 2019-08-22 stsp return err;
8343 01073a5d 2019-08-22 stsp static const struct got_error *
8344 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8346 01073a5d 2019-08-22 stsp const struct got_error *err;
8347 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8348 01073a5d 2019-08-22 stsp char *id_str = NULL;
8349 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8351 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8353 01073a5d 2019-08-22 stsp return err;
8355 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8357 01073a5d 2019-08-22 stsp goto done;
8359 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8361 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8362 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8363 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8364 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8366 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8367 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8368 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8370 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8371 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8372 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8374 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8375 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8376 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8382 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8383 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8385 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8386 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8387 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8389 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8390 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8391 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8393 01073a5d 2019-08-22 stsp free(id_str);
8394 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8395 01073a5d 2019-08-22 stsp return err;
8398 01073a5d 2019-08-22 stsp static const struct got_error *
8399 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8401 01073a5d 2019-08-22 stsp const struct got_error *error;
8402 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8403 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8404 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8405 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8406 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8407 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8409 01073a5d 2019-08-22 stsp #ifndef PROFILE
8410 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8411 01073a5d 2019-08-22 stsp NULL) == -1)
8412 01073a5d 2019-08-22 stsp err(1, "pledge");
8415 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8416 01073a5d 2019-08-22 stsp switch (ch) {
8418 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8421 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8422 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8423 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8425 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8428 896e9b6f 2019-08-26 stsp force_path = 1;
8431 01073a5d 2019-08-22 stsp usage_cat();
8432 01073a5d 2019-08-22 stsp /* NOTREACHED */
8436 01073a5d 2019-08-22 stsp argc -= optind;
8437 01073a5d 2019-08-22 stsp argv += optind;
8439 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8440 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8441 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8442 01073a5d 2019-08-22 stsp goto done;
8444 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8445 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8446 01073a5d 2019-08-22 stsp goto done;
8447 01073a5d 2019-08-22 stsp if (worktree) {
8448 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8449 01073a5d 2019-08-22 stsp repo_path = strdup(
8450 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8451 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8452 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8453 01073a5d 2019-08-22 stsp goto done;
8458 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8459 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8460 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8461 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8464 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8465 01073a5d 2019-08-22 stsp free(repo_path);
8466 01073a5d 2019-08-22 stsp if (error != NULL)
8467 01073a5d 2019-08-22 stsp goto done;
8469 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8470 896e9b6f 2019-08-26 stsp if (error)
8471 896e9b6f 2019-08-26 stsp goto done;
8473 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8474 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8475 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8476 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8477 01073a5d 2019-08-22 stsp if (error)
8478 01073a5d 2019-08-22 stsp goto done;
8480 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8481 896e9b6f 2019-08-26 stsp if (force_path) {
8482 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8484 896e9b6f 2019-08-26 stsp if (error)
8487 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8488 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8489 896e9b6f 2019-08-26 stsp if (error) {
8490 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8491 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8493 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8494 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8495 896e9b6f 2019-08-26 stsp if (error)
8500 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8501 01073a5d 2019-08-22 stsp if (error)
8504 01073a5d 2019-08-22 stsp switch (obj_type) {
8505 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8506 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8508 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8509 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8511 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8512 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8514 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8515 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8518 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8521 01073a5d 2019-08-22 stsp if (error)
8523 01073a5d 2019-08-22 stsp free(label);
8524 01073a5d 2019-08-22 stsp label = NULL;
8526 01073a5d 2019-08-22 stsp id = NULL;
8529 01073a5d 2019-08-22 stsp free(label);
8531 896e9b6f 2019-08-26 stsp free(commit_id);
8532 01073a5d 2019-08-22 stsp if (worktree)
8533 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8534 01073a5d 2019-08-22 stsp if (repo) {
8535 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8536 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8537 01073a5d 2019-08-22 stsp if (error == NULL)
8538 01073a5d 2019-08-22 stsp error = repo_error;
8540 0ebf8283 2019-07-24 stsp return error;