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 659e7fbd 2020-03-20 stsp fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 8a8b05ce 2020-03-19 stsp "[target-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 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
890 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
891 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
892 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
893 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
894 bb64b798 2020-03-18 stsp const char *repo_path;
895 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
896 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
897 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
898 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
899 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
900 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
901 b46f3e71 2020-03-18 stsp char *git_url = NULL;
902 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
903 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
904 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
906 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
909 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
910 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
912 659e7fbd 2020-03-20 stsp while ((ch = getopt(argc, argv, "amvq")) != -1) {
913 93658fb9 2020-03-18 stsp switch (ch) {
915 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
918 469dd726 2020-03-20 stsp mirror_references = 1;
921 68999b92 2020-03-18 stsp if (verbosity < 0)
922 68999b92 2020-03-18 stsp verbosity = 0;
923 68999b92 2020-03-18 stsp else if (verbosity < 3)
924 68999b92 2020-03-18 stsp verbosity++;
927 68999b92 2020-03-18 stsp verbosity = -1;
930 93658fb9 2020-03-18 stsp usage_clone();
934 93658fb9 2020-03-18 stsp argc -= optind;
935 93658fb9 2020-03-18 stsp argv += optind;
937 93658fb9 2020-03-18 stsp uri = argv[0];
939 9df6f38b 2020-03-18 stsp if (argc == 1)
940 93658fb9 2020-03-18 stsp dirname = NULL;
941 9df6f38b 2020-03-18 stsp else if (argc == 2)
942 93658fb9 2020-03-18 stsp dirname = argv[1];
944 93658fb9 2020-03-18 stsp usage_clone();
946 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
951 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
953 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
958 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
959 b46f3e71 2020-03-18 stsp #ifndef PROFILE
960 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
962 39c64a6a 2020-03-18 stsp err(1, "pledge");
964 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
965 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
966 b46f3e71 2020-03-18 stsp #ifndef PROFILE
967 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
969 39c64a6a 2020-03-18 stsp err(1, "pledge");
971 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
972 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
973 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 bb64b798 2020-03-18 stsp if (dirname == NULL) {
980 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
984 bb64b798 2020-03-18 stsp repo_path = default_destdir;
986 bb64b798 2020-03-18 stsp repo_path = dirname;
988 39c64a6a 2020-03-18 stsp error = got_path_mkdir(repo_path);
992 39c64a6a 2020-03-18 stsp error = got_repo_init(repo_path);
996 39c64a6a 2020-03-18 stsp error = got_repo_open(&repo, repo_path, NULL);
1000 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1003 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1004 ee448f5f 2020-03-18 stsp goto done;
1007 ee448f5f 2020-03-18 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1008 ee448f5f 2020-03-18 stsp if (error)
1009 ee448f5f 2020-03-18 stsp goto done;
1011 68999b92 2020-03-18 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 68999b92 2020-03-18 stsp verbosity);
1013 39c64a6a 2020-03-18 stsp if (error)
1014 bb64b798 2020-03-18 stsp goto done;
1016 68999b92 2020-03-18 stsp if (verbosity >= 0)
1017 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1018 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1020 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1021 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1022 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1023 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1024 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 659e7fbd 2020-03-20 stsp fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1027 39c64a6a 2020-03-18 stsp if (error)
1028 d9b4d0c0 2020-03-18 stsp goto done;
1030 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1031 39c64a6a 2020-03-18 stsp if (error)
1032 d9b4d0c0 2020-03-18 stsp goto done;
1033 68999b92 2020-03-18 stsp if (verbosity >= 0)
1034 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1035 d9b4d0c0 2020-03-18 stsp free(id_str);
1037 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1038 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1039 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1040 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1041 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1042 7ebc0570 2020-03-18 stsp char *remote_refname;
1044 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1045 39c64a6a 2020-03-18 stsp if (error)
1046 d9b4d0c0 2020-03-18 stsp goto done;
1047 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1048 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1049 7ebc0570 2020-03-18 stsp if (error)
1050 7ebc0570 2020-03-18 stsp goto done;
1052 469dd726 2020-03-20 stsp if (mirror_references)
1055 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1058 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1059 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1061 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1062 7ebc0570 2020-03-18 stsp goto done;
1064 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1065 39c64a6a 2020-03-18 stsp if (error)
1066 d9b4d0c0 2020-03-18 stsp goto done;
1067 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1068 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1069 39c64a6a 2020-03-18 stsp if (error)
1070 d9b4d0c0 2020-03-18 stsp goto done;
1073 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1074 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1075 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1076 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1077 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1079 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1083 39c64a6a 2020-03-18 stsp if (error) {
1084 39c64a6a 2020-03-18 stsp if (error->code == GOT_ERR_NOT_REF)
1086 d9b4d0c0 2020-03-18 stsp goto done;
1089 659e7fbd 2020-03-20 stsp error = got_ref_alloc_symref(&head_symref,
1090 659e7fbd 2020-03-20 stsp GOT_REF_HEAD, target_ref);
1091 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1092 39c64a6a 2020-03-18 stsp if (error)
1093 d9b4d0c0 2020-03-18 stsp goto done;
1095 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1096 68999b92 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 659e7fbd 2020-03-20 stsp got_ref_get_symref_target(head_symref));
1099 659e7fbd 2020-03-20 stsp error = got_ref_write(head_symref, repo);
1103 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1104 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1106 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1107 659e7fbd 2020-03-20 stsp goto done;
1109 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1110 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1111 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1112 659e7fbd 2020-03-20 stsp goto done;
1114 659e7fbd 2020-03-20 stsp if (mirror_references) {
1115 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1116 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1117 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1118 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1119 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1120 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1121 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1122 659e7fbd 2020-03-20 stsp goto done;
1124 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1125 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1126 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1127 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1128 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1129 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1130 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1131 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1132 659e7fbd 2020-03-20 stsp goto done;
1135 659e7fbd 2020-03-20 stsp const char *branchname;
1138 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1139 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1141 659e7fbd 2020-03-20 stsp if (head_symref) {
1142 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1143 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1144 659e7fbd 2020-03-20 stsp branchname += 11;
1146 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1147 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1148 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1149 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1150 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1151 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1152 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1153 659e7fbd 2020-03-20 stsp branchname) == -1) {
1154 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1155 659e7fbd 2020-03-20 stsp goto done;
1158 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1159 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1160 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1161 659e7fbd 2020-03-20 stsp goto done;
1165 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1166 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1167 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1169 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1170 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1171 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1172 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1174 bb64b798 2020-03-18 stsp got_repo_close(repo);
1175 659e7fbd 2020-03-20 stsp if (head_symref)
1176 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1177 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1178 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1179 d9b4d0c0 2020-03-18 stsp free(pe->data);
1181 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1182 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1183 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1184 d9b4d0c0 2020-03-18 stsp free(pe->data);
1186 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1187 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1188 09838ffc 2020-03-18 stsp free(proto);
1189 09838ffc 2020-03-18 stsp free(host);
1190 09838ffc 2020-03-18 stsp free(port);
1191 09838ffc 2020-03-18 stsp free(server_path);
1192 09838ffc 2020-03-18 stsp free(repo_name);
1193 bb64b798 2020-03-18 stsp free(default_destdir);
1194 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1195 b46f3e71 2020-03-18 stsp free(git_url);
1196 39c64a6a 2020-03-18 stsp return error;
1199 7848a0e1 2020-03-19 stsp static const struct got_error *
1200 7848a0e1 2020-03-19 stsp create_ref(const char *refname, struct got_object_id *id,
1201 7848a0e1 2020-03-19 stsp const char *id_str, struct got_repository *repo)
1203 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1204 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1206 7848a0e1 2020-03-19 stsp printf("Creating %s: %s\n", refname, id_str);
1208 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&ref, refname, id);
1210 7848a0e1 2020-03-19 stsp return err;
1212 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1213 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1214 7848a0e1 2020-03-19 stsp return err;
1217 7848a0e1 2020-03-19 stsp static const struct got_error *
1218 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1219 7848a0e1 2020-03-19 stsp struct got_repository *repo)
1221 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1222 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1223 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1225 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1227 7848a0e1 2020-03-19 stsp goto done;
1229 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1230 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1231 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1233 7848a0e1 2020-03-19 stsp goto done;
1234 7848a0e1 2020-03-19 stsp printf("Deleting symbolic reference %s -> %s\n",
1235 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), got_ref_get_symref_target(ref));
1236 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1238 7848a0e1 2020-03-19 stsp goto done;
1239 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n", got_ref_get_name(ref),
1240 7848a0e1 2020-03-19 stsp new_id_str);
1241 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1243 7848a0e1 2020-03-19 stsp goto done;
1245 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1247 7848a0e1 2020-03-19 stsp goto done;
1248 7848a0e1 2020-03-19 stsp if (got_object_id_cmp(old_id, new_id) != 0) {
1249 7848a0e1 2020-03-19 stsp printf("Setting %s to %s\n",
1250 7848a0e1 2020-03-19 stsp got_ref_get_name(ref), new_id_str);
1251 7848a0e1 2020-03-19 stsp err = got_ref_change_ref(ref, new_id);
1253 7848a0e1 2020-03-19 stsp goto done;
1254 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1256 7848a0e1 2020-03-19 stsp goto done;
1260 7848a0e1 2020-03-19 stsp free(old_id);
1261 7848a0e1 2020-03-19 stsp free(new_id_str);
1262 7848a0e1 2020-03-19 stsp return err;
1265 2ab43947 2020-03-18 stsp __dead static void
1266 7848a0e1 2020-03-19 stsp usage_fetch(void)
1268 659e7fbd 2020-03-20 stsp fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1269 7848a0e1 2020-03-19 stsp "[remote-repository-name]\n", getprogname());
1273 7848a0e1 2020-03-19 stsp static const struct got_error *
1274 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1276 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1277 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1278 7848a0e1 2020-03-19 stsp const char *remote_name;
1279 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1280 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1281 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1282 7848a0e1 2020-03-19 stsp int nremotes;
1283 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1284 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1285 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1286 7848a0e1 2020-03-19 stsp struct got_pathlist_head refs, symrefs;
1287 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1288 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1289 7848a0e1 2020-03-19 stsp int i, ch, fetchfd = -1;
1290 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1291 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0;
1293 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1294 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1296 659e7fbd 2020-03-20 stsp while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1297 7848a0e1 2020-03-19 stsp switch (ch) {
1299 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1302 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1303 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1304 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1306 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1309 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1310 7848a0e1 2020-03-19 stsp verbosity = 0;
1311 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1312 7848a0e1 2020-03-19 stsp verbosity++;
1315 7848a0e1 2020-03-19 stsp verbosity = -1;
1318 7848a0e1 2020-03-19 stsp usage_fetch();
1322 7848a0e1 2020-03-19 stsp argc -= optind;
1323 7848a0e1 2020-03-19 stsp argv += optind;
1325 7848a0e1 2020-03-19 stsp if (argc == 0)
1326 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1327 7848a0e1 2020-03-19 stsp else if (argc == 1)
1328 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1330 7848a0e1 2020-03-19 stsp usage_fetch();
1332 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1333 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1334 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1335 7848a0e1 2020-03-19 stsp goto done;
1338 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1339 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1340 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1341 7848a0e1 2020-03-19 stsp goto done;
1343 7848a0e1 2020-03-19 stsp error = NULL;
1344 7848a0e1 2020-03-19 stsp if (worktree) {
1345 7848a0e1 2020-03-19 stsp repo_path =
1346 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1347 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1348 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1349 7848a0e1 2020-03-19 stsp if (error)
1350 7848a0e1 2020-03-19 stsp goto done;
1352 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1353 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1354 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1355 7848a0e1 2020-03-19 stsp goto done;
1360 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1361 7848a0e1 2020-03-19 stsp if (error)
1362 7848a0e1 2020-03-19 stsp goto done;
1364 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1365 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1366 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1367 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1370 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1371 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1372 7848a0e1 2020-03-19 stsp goto done;
1375 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1376 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1377 7848a0e1 2020-03-19 stsp if (error)
1378 7848a0e1 2020-03-19 stsp goto done;
1380 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1381 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1382 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1384 7848a0e1 2020-03-19 stsp err(1, "pledge");
1386 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1387 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1388 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1389 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1390 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1391 7848a0e1 2020-03-19 stsp err(1, "pledge");
1393 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1394 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1395 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1396 7848a0e1 2020-03-19 stsp goto done;
1398 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1399 7848a0e1 2020-03-19 stsp goto done;
1402 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1403 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1404 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1405 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1406 7848a0e1 2020-03-19 stsp goto done;
1409 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1410 7848a0e1 2020-03-19 stsp if (error)
1411 7848a0e1 2020-03-19 stsp goto done;
1413 7848a0e1 2020-03-19 stsp error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1414 7848a0e1 2020-03-19 stsp verbosity);
1415 7848a0e1 2020-03-19 stsp if (error)
1416 7848a0e1 2020-03-19 stsp goto done;
1418 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1419 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1420 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1422 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1423 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1424 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1425 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1426 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1427 659e7fbd 2020-03-20 stsp remote->mirror_references, fetch_all_branches, fetchfd, repo,
1428 659e7fbd 2020-03-20 stsp fetch_progress, &fpa);
1429 7848a0e1 2020-03-19 stsp if (error)
1430 7848a0e1 2020-03-19 stsp goto done;
1432 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1433 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1434 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1435 7848a0e1 2020-03-19 stsp goto done;
1438 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1439 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1440 984065c8 2020-03-19 stsp if (error)
1441 984065c8 2020-03-19 stsp goto done;
1442 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1443 984065c8 2020-03-19 stsp free(id_str);
1444 984065c8 2020-03-19 stsp id_str = NULL;
1447 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1448 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1449 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1450 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1451 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1452 7848a0e1 2020-03-19 stsp char *remote_refname;
1454 fed0b873 2020-03-20 stsp if (remote->mirror_references) {
1455 fed0b873 2020-03-20 stsp error = got_ref_alloc(&ref, refname, id);
1456 fed0b873 2020-03-20 stsp if (error)
1457 fed0b873 2020-03-20 stsp goto done;
1458 fed0b873 2020-03-20 stsp error = got_ref_write(ref, repo);
1459 fed0b873 2020-03-20 stsp got_ref_close(ref);
1460 fed0b873 2020-03-20 stsp if (error)
1461 fed0b873 2020-03-20 stsp goto done;
1465 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, id);
1466 7848a0e1 2020-03-19 stsp if (error)
1467 7848a0e1 2020-03-19 stsp goto done;
1469 7848a0e1 2020-03-19 stsp if (strncmp("refs/tags/", refname, 10) == 0) {
1470 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1471 7848a0e1 2020-03-19 stsp if (error) {
1472 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1473 7848a0e1 2020-03-19 stsp goto done;
1474 7848a0e1 2020-03-19 stsp error = create_ref(refname, id, id_str, repo);
1475 7848a0e1 2020-03-19 stsp if (error)
1476 7848a0e1 2020-03-19 stsp goto done;
1478 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1479 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1480 7848a0e1 2020-03-19 stsp if (error)
1481 7848a0e1 2020-03-19 stsp goto done;
1483 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1484 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1485 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1486 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1487 7848a0e1 2020-03-19 stsp goto done;
1490 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1491 7848a0e1 2020-03-19 stsp if (error) {
1492 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1493 7848a0e1 2020-03-19 stsp goto done;
1494 e5083482 2020-03-20 stsp error = create_ref(remote_refname, id, id_str,
1496 7848a0e1 2020-03-19 stsp if (error)
1497 7848a0e1 2020-03-19 stsp goto done;
1499 7848a0e1 2020-03-19 stsp error = update_ref(ref, id, repo);
1500 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1501 7848a0e1 2020-03-19 stsp if (error)
1502 7848a0e1 2020-03-19 stsp goto done;
1505 7848a0e1 2020-03-19 stsp free(id_str);
1506 7848a0e1 2020-03-19 stsp id_str = NULL;
1509 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1510 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1512 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1513 7848a0e1 2020-03-19 stsp if (worktree)
1514 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1515 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1516 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1517 7848a0e1 2020-03-19 stsp free(pe->data);
1519 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1520 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1521 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1522 7848a0e1 2020-03-19 stsp free(pe->data);
1524 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1525 7848a0e1 2020-03-19 stsp free(id_str);
1526 7848a0e1 2020-03-19 stsp free(cwd);
1527 7848a0e1 2020-03-19 stsp free(repo_path);
1528 7848a0e1 2020-03-19 stsp free(pack_hash);
1529 7848a0e1 2020-03-19 stsp free(proto);
1530 7848a0e1 2020-03-19 stsp free(host);
1531 7848a0e1 2020-03-19 stsp free(port);
1532 7848a0e1 2020-03-19 stsp free(server_path);
1533 7848a0e1 2020-03-19 stsp free(repo_name);
1534 7848a0e1 2020-03-19 stsp return error;
1538 7848a0e1 2020-03-19 stsp __dead static void
1539 2ab43947 2020-03-18 stsp usage_checkout(void)
1541 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1542 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1546 2ab43947 2020-03-18 stsp static void
1547 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1549 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1550 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1551 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1552 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1553 2ab43947 2020-03-18 stsp getprogname());
1556 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1557 2ab43947 2020-03-18 stsp const char *worktree_path;
1558 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1561 2ab43947 2020-03-18 stsp static const struct got_error *
1562 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1564 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1566 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1567 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1568 2ab43947 2020-03-18 stsp return NULL;
1570 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1571 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1572 2ab43947 2020-03-18 stsp return NULL;
1575 2ab43947 2020-03-18 stsp while (path[0] == '/')
1578 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1579 2ab43947 2020-03-18 stsp return NULL;
1582 2ab43947 2020-03-18 stsp static const struct got_error *
1583 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1585 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1586 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1587 2ab43947 2020-03-18 stsp return NULL;
1590 2ab43947 2020-03-18 stsp static const struct got_error *
1591 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1592 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1593 2ab43947 2020-03-18 stsp struct got_repository *repo)
1595 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1596 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1598 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1599 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1601 2ab43947 2020-03-18 stsp return err;
1603 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1604 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1607 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1608 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1610 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1612 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1620 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1621 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1622 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1624 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1625 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1626 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1627 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1628 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1629 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1631 2ab43947 2020-03-18 stsp free(yca_id);
1632 2ab43947 2020-03-18 stsp return NULL;
1635 2ab43947 2020-03-18 stsp static const struct got_error *
1636 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1637 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1638 2ab43947 2020-03-18 stsp struct got_repository *repo)
1640 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1641 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1642 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1643 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1645 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1647 2ab43947 2020-03-18 stsp goto done;
1649 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1650 2ab43947 2020-03-18 stsp is_same_branch = 1;
1651 2ab43947 2020-03-18 stsp goto done;
1653 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1654 2ab43947 2020-03-18 stsp is_same_branch = 1;
1655 2ab43947 2020-03-18 stsp goto done;
1658 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1660 2ab43947 2020-03-18 stsp goto done;
1662 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1663 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1665 2ab43947 2020-03-18 stsp goto done;
1667 2ab43947 2020-03-18 stsp for (;;) {
1668 2ab43947 2020-03-18 stsp struct got_object_id *id;
1669 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1670 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1671 2ab43947 2020-03-18 stsp if (err) {
1672 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1673 2ab43947 2020-03-18 stsp err = NULL;
1678 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1680 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1681 2ab43947 2020-03-18 stsp is_same_branch = 1;
1687 2ab43947 2020-03-18 stsp if (graph)
1688 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1689 2ab43947 2020-03-18 stsp free(head_commit_id);
1690 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1691 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1692 2ab43947 2020-03-18 stsp return err;
1695 4ed7e80c 2018-05-20 stsp static const struct got_error *
1696 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1698 4b6c9460 2020-03-05 stsp static char msg[512];
1699 4b6c9460 2020-03-05 stsp const char *branch_name;
1701 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1702 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1704 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1706 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1707 4b6c9460 2020-03-05 stsp branch_name += 11;
1709 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1710 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1711 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1712 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1713 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1714 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1716 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1719 4b6c9460 2020-03-05 stsp static const struct got_error *
1720 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1722 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1723 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1724 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1725 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1726 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1727 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1728 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1729 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1730 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1731 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1732 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1733 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1735 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1737 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1738 0bb8a95e 2018-03-12 stsp switch (ch) {
1740 08573d5b 2019-05-14 stsp branch_name = optarg;
1743 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1744 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1745 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1748 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1751 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1754 2deda0b9 2019-03-07 stsp usage_checkout();
1755 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1759 0bb8a95e 2018-03-12 stsp argc -= optind;
1760 0bb8a95e 2018-03-12 stsp argv += optind;
1762 6715a751 2018-03-16 stsp #ifndef PROFILE
1763 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1764 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1765 c09a553d 2018-03-12 stsp err(1, "pledge");
1767 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1768 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1769 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1770 76089277 2018-04-01 stsp if (repo_path == NULL)
1771 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1772 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1773 76089277 2018-04-01 stsp if (cwd == NULL) {
1774 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1775 76089277 2018-04-01 stsp goto done;
1777 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1778 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1779 230a42bd 2019-05-11 jcs if (base == NULL) {
1780 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1781 230a42bd 2019-05-11 jcs path_prefix);
1785 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1786 230a42bd 2019-05-11 jcs if (base == NULL) {
1787 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1788 230a42bd 2019-05-11 jcs repo_path);
1792 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1793 c09a553d 2018-03-12 stsp if (dotgit)
1794 c09a553d 2018-03-12 stsp *dotgit = '\0';
1795 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1796 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1797 c09a553d 2018-03-12 stsp free(cwd);
1798 76089277 2018-04-01 stsp goto done;
1800 c09a553d 2018-03-12 stsp free(cwd);
1801 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1802 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1803 76089277 2018-04-01 stsp if (repo_path == NULL) {
1804 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1805 76089277 2018-04-01 stsp goto done;
1807 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1808 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1809 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1810 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1812 b4b3a7dd 2019-07-22 stsp goto done;
1814 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1815 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1816 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1817 b4b3a7dd 2019-07-22 stsp goto done;
1821 c09a553d 2018-03-12 stsp usage_checkout();
1823 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1824 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1827 c09a553d 2018-03-12 stsp if (error != NULL)
1828 c02c541e 2019-03-29 stsp goto done;
1830 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1831 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1832 c530dc23 2019-07-23 stsp if (error) {
1833 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1834 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1835 c530dc23 2019-07-23 stsp goto done;
1836 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1837 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1838 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1839 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1840 c530dc23 2019-07-23 stsp goto done;
1844 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1845 c02c541e 2019-03-29 stsp if (error)
1846 c09a553d 2018-03-12 stsp goto done;
1848 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1849 c09a553d 2018-03-12 stsp if (error != NULL)
1850 c09a553d 2018-03-12 stsp goto done;
1852 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1853 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1854 c09a553d 2018-03-12 stsp goto done;
1856 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1857 c09a553d 2018-03-12 stsp if (error != NULL)
1858 c09a553d 2018-03-12 stsp goto done;
1860 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1861 e5dc7198 2018-12-29 stsp path_prefix);
1862 e5dc7198 2018-12-29 stsp if (error != NULL)
1863 e5dc7198 2018-12-29 stsp goto done;
1864 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1865 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1866 49520a32 2018-12-29 stsp goto done;
1869 8069f636 2019-01-12 stsp if (commit_id_str) {
1870 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1871 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1872 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1873 30837e32 2019-07-25 stsp if (error)
1874 8069f636 2019-01-12 stsp goto done;
1875 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1876 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1877 8069f636 2019-01-12 stsp if (error != NULL) {
1878 8069f636 2019-01-12 stsp free(commit_id);
1879 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1880 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1881 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1883 8069f636 2019-01-12 stsp goto done;
1885 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1886 4b6c9460 2020-03-05 stsp if (error) {
1887 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1888 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1889 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1891 45d344f6 2019-05-14 stsp goto done;
1893 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1894 8069f636 2019-01-12 stsp commit_id);
1895 8069f636 2019-01-12 stsp free(commit_id);
1896 8069f636 2019-01-12 stsp if (error)
1897 8069f636 2019-01-12 stsp goto done;
1900 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1901 f2ea84fa 2019-07-27 stsp if (error)
1902 f2ea84fa 2019-07-27 stsp goto done;
1903 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1904 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1905 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1906 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1907 c09a553d 2018-03-12 stsp if (error != NULL)
1908 c09a553d 2018-03-12 stsp goto done;
1910 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1911 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1912 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1914 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1915 8069f636 2019-01-12 stsp free(commit_id_str);
1916 76089277 2018-04-01 stsp free(repo_path);
1917 507dc3bb 2018-12-29 stsp free(worktree_path);
1918 507dc3bb 2018-12-29 stsp return error;
1921 507dc3bb 2018-12-29 stsp __dead static void
1922 507dc3bb 2018-12-29 stsp usage_update(void)
1924 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1925 507dc3bb 2018-12-29 stsp getprogname());
1929 1ee397ad 2019-07-12 stsp static const struct got_error *
1930 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1932 784955db 2019-01-12 stsp int *did_something = arg;
1934 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1935 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1936 1ee397ad 2019-07-12 stsp return NULL;
1938 1545c615 2019-02-10 stsp *did_something = 1;
1940 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1941 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1942 1ee397ad 2019-07-12 stsp return NULL;
1944 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1946 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1947 1ee397ad 2019-07-12 stsp return NULL;
1950 be7061eb 2018-12-30 stsp static const struct got_error *
1951 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1952 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1953 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1955 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1956 a1fb16d8 2019-05-24 stsp char *base_id_str;
1957 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1959 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1960 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1961 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1962 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1963 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1964 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1965 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1968 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1969 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1970 a1fb16d8 2019-05-24 stsp if (err) {
1971 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1972 a1fb16d8 2019-05-24 stsp return err;
1973 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1975 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1976 a1fb16d8 2019-05-24 stsp return NULL;
1978 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1979 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1980 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1982 a1fb16d8 2019-05-24 stsp return err;
1983 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1984 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1985 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1986 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1987 0ebf8283 2019-07-24 stsp return NULL;
1990 0ebf8283 2019-07-24 stsp static const struct got_error *
1991 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1993 0ebf8283 2019-07-24 stsp const struct got_error *err;
1994 0ebf8283 2019-07-24 stsp int in_progress;
1996 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1998 0ebf8283 2019-07-24 stsp return err;
1999 0ebf8283 2019-07-24 stsp if (in_progress)
2000 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2002 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2004 0ebf8283 2019-07-24 stsp return err;
2005 0ebf8283 2019-07-24 stsp if (in_progress)
2006 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2008 a1fb16d8 2019-05-24 stsp return NULL;
2011 a5edda0a 2019-07-27 stsp static const struct got_error *
2012 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2013 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2015 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2016 a5edda0a 2019-07-27 stsp char *path;
2019 a5edda0a 2019-07-27 stsp if (argc == 0) {
2020 a5edda0a 2019-07-27 stsp path = strdup("");
2021 a5edda0a 2019-07-27 stsp if (path == NULL)
2022 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2023 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2026 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2027 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2030 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2031 a5edda0a 2019-07-27 stsp if (err) {
2032 a5edda0a 2019-07-27 stsp free(path);
2037 a5edda0a 2019-07-27 stsp return err;
2040 a1fb16d8 2019-05-24 stsp static const struct got_error *
2041 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2043 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2044 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2045 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2046 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2047 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2048 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2049 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2050 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2051 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2052 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2053 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2055 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2057 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2058 507dc3bb 2018-12-29 stsp switch (ch) {
2060 024e9686 2019-05-14 stsp branch_name = optarg;
2063 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2064 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2065 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2068 2deda0b9 2019-03-07 stsp usage_update();
2069 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2073 507dc3bb 2018-12-29 stsp argc -= optind;
2074 507dc3bb 2018-12-29 stsp argv += optind;
2076 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2077 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2078 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2079 507dc3bb 2018-12-29 stsp err(1, "pledge");
2081 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2082 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2083 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2084 c4cdcb68 2019-04-03 stsp goto done;
2086 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2087 7d5807f4 2019-07-11 stsp if (error)
2088 7d5807f4 2019-07-11 stsp goto done;
2090 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2091 f2ea84fa 2019-07-27 stsp if (error)
2092 f2ea84fa 2019-07-27 stsp goto done;
2094 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2096 507dc3bb 2018-12-29 stsp if (error != NULL)
2097 507dc3bb 2018-12-29 stsp goto done;
2099 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2100 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2101 087fb88c 2019-08-04 stsp if (error)
2102 087fb88c 2019-08-04 stsp goto done;
2104 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2105 0266afb7 2019-01-04 stsp if (error)
2106 0266afb7 2019-01-04 stsp goto done;
2108 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2109 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2110 024e9686 2019-05-14 stsp if (error != NULL)
2111 024e9686 2019-05-14 stsp goto done;
2112 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2113 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2114 9c4b8182 2019-01-02 stsp if (error != NULL)
2115 9c4b8182 2019-01-02 stsp goto done;
2116 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2117 507dc3bb 2018-12-29 stsp if (error != NULL)
2118 507dc3bb 2018-12-29 stsp goto done;
2120 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2121 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2122 04f57cb3 2019-07-25 stsp free(commit_id_str);
2123 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2124 30837e32 2019-07-25 stsp if (error)
2125 a297e751 2019-07-11 stsp goto done;
2126 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2127 a297e751 2019-07-11 stsp if (error)
2128 507dc3bb 2018-12-29 stsp goto done;
2131 a1fb16d8 2019-05-24 stsp if (branch_name) {
2132 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2133 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2134 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2136 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2137 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2138 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2139 024e9686 2019-05-14 stsp goto done;
2141 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2142 024e9686 2019-05-14 stsp if (error)
2143 024e9686 2019-05-14 stsp goto done;
2144 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2146 a367ff0f 2019-05-14 stsp free(head_commit_id);
2147 024e9686 2019-05-14 stsp if (error != NULL)
2148 024e9686 2019-05-14 stsp goto done;
2149 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2150 a367ff0f 2019-05-14 stsp if (error)
2151 a367ff0f 2019-05-14 stsp goto done;
2152 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2153 024e9686 2019-05-14 stsp if (error)
2154 024e9686 2019-05-14 stsp goto done;
2156 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2157 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2158 a367ff0f 2019-05-14 stsp if (error != NULL) {
2159 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2160 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2161 024e9686 2019-05-14 stsp goto done;
2163 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2164 a367ff0f 2019-05-14 stsp if (error)
2165 a367ff0f 2019-05-14 stsp goto done;
2168 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2169 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2170 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2171 507dc3bb 2018-12-29 stsp commit_id);
2172 507dc3bb 2018-12-29 stsp if (error)
2173 507dc3bb 2018-12-29 stsp goto done;
2176 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2177 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2178 507dc3bb 2018-12-29 stsp if (error != NULL)
2179 507dc3bb 2018-12-29 stsp goto done;
2181 784955db 2019-01-12 stsp if (did_something)
2182 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2184 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2186 c09a553d 2018-03-12 stsp free(worktree_path);
2187 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2188 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2189 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2190 507dc3bb 2018-12-29 stsp free(commit_id);
2191 9c4b8182 2019-01-02 stsp free(commit_id_str);
2192 c09a553d 2018-03-12 stsp return error;
2195 f42b1b34 2018-03-12 stsp static const struct got_error *
2196 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2197 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2198 63035f9f 2019-10-06 stsp struct got_repository *repo)
2200 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2201 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2203 44392932 2019-08-25 stsp if (blob_id1) {
2204 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2206 44392932 2019-08-25 stsp goto done;
2209 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2211 44392932 2019-08-25 stsp goto done;
2213 44392932 2019-08-25 stsp while (path[0] == '/')
2215 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2216 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2218 44392932 2019-08-25 stsp if (blob1)
2219 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2220 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2221 44392932 2019-08-25 stsp return err;
2224 44392932 2019-08-25 stsp static const struct got_error *
2225 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2226 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2227 63035f9f 2019-10-06 stsp struct got_repository *repo)
2229 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2230 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2231 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2233 44392932 2019-08-25 stsp if (tree_id1) {
2234 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2236 44392932 2019-08-25 stsp goto done;
2239 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2241 0f2b3dca 2018-12-22 stsp goto done;
2243 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2244 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2245 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2246 44392932 2019-08-25 stsp while (path[0] == '/')
2248 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2249 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2251 79109fed 2018-03-27 stsp if (tree1)
2252 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2253 366e0a5f 2019-10-10 stsp if (tree2)
2254 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2255 44392932 2019-08-25 stsp return err;
2258 44392932 2019-08-25 stsp static const struct got_error *
2259 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2260 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2262 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2263 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2264 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2265 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2266 44392932 2019-08-25 stsp struct got_object_qid *qid;
2268 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2269 44392932 2019-08-25 stsp if (qid != NULL) {
2270 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2273 44392932 2019-08-25 stsp return err;
2276 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2277 44392932 2019-08-25 stsp int obj_type;
2278 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2280 44392932 2019-08-25 stsp goto done;
2281 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2282 44392932 2019-08-25 stsp if (err) {
2283 44392932 2019-08-25 stsp free(obj_id2);
2284 44392932 2019-08-25 stsp goto done;
2286 44392932 2019-08-25 stsp if (pcommit) {
2287 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2288 44392932 2019-08-25 stsp qid->id, path);
2289 44392932 2019-08-25 stsp if (err) {
2290 44392932 2019-08-25 stsp free(obj_id2);
2291 44392932 2019-08-25 stsp goto done;
2293 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2294 44392932 2019-08-25 stsp if (err) {
2295 44392932 2019-08-25 stsp free(obj_id2);
2296 44392932 2019-08-25 stsp goto done;
2299 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2300 44392932 2019-08-25 stsp if (err) {
2301 44392932 2019-08-25 stsp free(obj_id2);
2302 44392932 2019-08-25 stsp goto done;
2304 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2305 44392932 2019-08-25 stsp switch (obj_type) {
2306 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2307 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2310 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2311 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2315 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2318 44392932 2019-08-25 stsp free(obj_id1);
2319 44392932 2019-08-25 stsp free(obj_id2);
2321 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2322 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2324 44392932 2019-08-25 stsp goto done;
2325 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2326 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2328 44392932 2019-08-25 stsp goto done;
2329 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2330 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2333 0f2b3dca 2018-12-22 stsp free(id_str1);
2334 0f2b3dca 2018-12-22 stsp free(id_str2);
2335 44392932 2019-08-25 stsp if (pcommit)
2336 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2337 79109fed 2018-03-27 stsp return err;
2340 4bb494d5 2018-06-16 stsp static char *
2341 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2343 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2344 09867e48 2019-08-13 stsp char *p, *s;
2346 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2347 09867e48 2019-08-13 stsp if (tm == NULL)
2348 09867e48 2019-08-13 stsp return NULL;
2349 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2350 09867e48 2019-08-13 stsp if (s == NULL)
2351 09867e48 2019-08-13 stsp return NULL;
2352 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2354 6c281f94 2018-06-11 stsp *p = '\0';
2358 6841bf13 2019-11-29 kn static const struct got_error *
2359 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2360 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2362 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2363 6841bf13 2019-11-29 kn regmatch_t regmatch;
2364 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2366 6841bf13 2019-11-29 kn *have_match = 0;
2368 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2372 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2376 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2377 6841bf13 2019-11-29 kn *have_match = 1;
2379 6841bf13 2019-11-29 kn free(id_str);
2380 6841bf13 2019-11-29 kn free(logmsg);
2384 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2386 79109fed 2018-03-27 stsp static const struct got_error *
2387 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2388 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2389 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2391 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2392 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2393 6c281f94 2018-06-11 stsp char datebuf[26];
2394 45d799e2 2018-12-23 stsp time_t committer_time;
2395 45d799e2 2018-12-23 stsp const char *author, *committer;
2396 199a4027 2019-02-02 stsp char *refs_str = NULL;
2397 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2399 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2401 199a4027 2019-02-02 stsp const char *name;
2402 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2405 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2406 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2408 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2409 199a4027 2019-02-02 stsp name += 5;
2410 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2412 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2413 e34f9ed6 2019-02-02 stsp name += 6;
2414 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2415 141c2bff 2019-02-04 stsp name += 8;
2416 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2417 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2418 5d844a1e 2019-08-13 stsp if (err) {
2419 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2420 5d844a1e 2019-08-13 stsp return err;
2421 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2422 5d844a1e 2019-08-13 stsp err = NULL;
2423 5d844a1e 2019-08-13 stsp tag = NULL;
2426 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2427 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2429 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2430 a436ad14 2019-08-13 stsp if (cmp != 0)
2432 199a4027 2019-02-02 stsp s = refs_str;
2433 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2434 199a4027 2019-02-02 stsp name) == -1) {
2435 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2437 34ca4898 2019-08-13 stsp return err;
2441 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2443 8bf5b3c9 2018-03-17 stsp return err;
2445 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2446 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2447 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2448 832c249c 2018-06-10 stsp free(id_str);
2449 d3d493d7 2019-02-21 stsp id_str = NULL;
2450 d3d493d7 2019-02-21 stsp free(refs_str);
2451 d3d493d7 2019-02-21 stsp refs_str = NULL;
2452 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2453 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2454 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2455 09867e48 2019-08-13 stsp if (datestr)
2456 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2457 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2458 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2459 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2460 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2461 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2462 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2463 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2464 3fe1abad 2018-06-10 stsp int n = 1;
2465 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2466 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2467 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2469 a0603db2 2018-06-10 stsp return err;
2470 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2471 a0603db2 2018-06-10 stsp free(id_str);
2475 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2477 5943eee2 2019-08-13 stsp return err;
2479 621015ac 2018-07-23 stsp logmsg = logmsg0;
2481 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2483 832c249c 2018-06-10 stsp printf(" %s\n", line);
2484 832c249c 2018-06-10 stsp } while (line);
2485 621015ac 2018-07-23 stsp free(logmsg0);
2487 971751ac 2018-03-27 stsp if (show_patch) {
2488 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2489 971751ac 2018-03-27 stsp if (err == 0)
2490 971751ac 2018-03-27 stsp printf("\n");
2493 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2494 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2495 07862c20 2018-09-15 stsp return err;
2498 f42b1b34 2018-03-12 stsp static const struct got_error *
2499 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2500 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2501 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2502 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2504 f42b1b34 2018-03-12 stsp const struct got_error *err;
2505 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2506 6841bf13 2019-11-29 kn regex_t regex;
2507 6841bf13 2019-11-29 kn int have_match;
2509 6841bf13 2019-11-29 kn if (search_pattern &&
2510 6841bf13 2019-11-29 kn regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2511 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2513 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2515 f42b1b34 2018-03-12 stsp return err;
2516 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2517 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2519 fcc85cad 2018-07-22 stsp goto done;
2521 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2522 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2524 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2527 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2528 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2529 372ccdbb 2018-06-10 stsp if (err) {
2530 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2531 9ba79e04 2018-06-11 stsp err = NULL;
2534 b43fbaa0 2018-06-11 stsp if (id == NULL)
2537 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2541 6841bf13 2019-11-29 kn if (search_pattern) {
2542 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
2544 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2547 6841bf13 2019-11-29 kn if (have_match == 0) {
2548 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2553 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2554 44392932 2019-08-25 stsp diff_context, refs);
2555 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2556 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2560 6841bf13 2019-11-29 kn if (search_pattern)
2561 6841bf13 2019-11-29 kn regfree(®ex);
2562 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2563 f42b1b34 2018-03-12 stsp return err;
2566 4ed7e80c 2018-05-20 stsp __dead static void
2567 6f3d1eb0 2018-03-12 stsp usage_log(void)
2569 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2570 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2574 b1ebc001 2019-08-13 stsp static int
2575 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2577 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2578 b1ebc001 2019-08-13 stsp long long n;
2579 b1ebc001 2019-08-13 stsp const char *errstr;
2581 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2582 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2584 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2585 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2590 4ed7e80c 2018-05-20 stsp static const struct got_error *
2591 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2593 f42b1b34 2018-03-12 stsp const struct got_error *error;
2594 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2595 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2596 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2597 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2598 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2599 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2600 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2601 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2602 64a96a6d 2018-04-01 stsp const char *errstr;
2603 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2605 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2607 6715a751 2018-03-16 stsp #ifndef PROFILE
2608 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2611 f42b1b34 2018-03-12 stsp err(1, "pledge");
2614 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2616 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2617 79109fed 2018-03-27 stsp switch (ch) {
2619 79109fed 2018-03-27 stsp show_patch = 1;
2622 d142fc45 2018-04-01 stsp start_commit = optarg;
2625 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2627 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2628 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2631 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2632 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2633 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2636 48c8c60d 2020-01-27 stsp log_branches = 1;
2639 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2640 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2641 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2643 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2646 6841bf13 2019-11-29 kn search_pattern = optarg;
2649 2deda0b9 2019-03-07 stsp usage_log();
2650 79109fed 2018-03-27 stsp /* NOTREACHED */
2654 79109fed 2018-03-27 stsp argc -= optind;
2655 79109fed 2018-03-27 stsp argv += optind;
2657 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2658 dc1edbfa 2019-11-29 kn diff_context = 3;
2659 dc1edbfa 2019-11-29 kn else if (!show_patch)
2660 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2662 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2663 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2664 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2665 04ca23f4 2018-07-16 stsp goto done;
2668 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2669 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2670 cffc0aa4 2019-02-05 stsp goto done;
2671 cffc0aa4 2019-02-05 stsp error = NULL;
2673 e7301579 2019-03-18 stsp if (argc == 0) {
2674 cbd1af7a 2019-03-18 stsp path = strdup("");
2675 e7301579 2019-03-18 stsp if (path == NULL) {
2676 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2677 cbd1af7a 2019-03-18 stsp goto done;
2679 e7301579 2019-03-18 stsp } else if (argc == 1) {
2680 e7301579 2019-03-18 stsp if (worktree) {
2681 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2683 e7301579 2019-03-18 stsp if (error)
2684 e7301579 2019-03-18 stsp goto done;
2686 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2687 e7301579 2019-03-18 stsp if (path == NULL) {
2688 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2689 e7301579 2019-03-18 stsp goto done;
2693 cbd1af7a 2019-03-18 stsp usage_log();
2695 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2696 5486daa2 2019-05-11 stsp repo_path = worktree ?
2697 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2699 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2700 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2701 cffc0aa4 2019-02-05 stsp goto done;
2704 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2705 d7d4f210 2018-03-12 stsp if (error != NULL)
2706 04ca23f4 2018-07-16 stsp goto done;
2708 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2709 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2710 c02c541e 2019-03-29 stsp if (error)
2711 c02c541e 2019-03-29 stsp goto done;
2713 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2714 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2715 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2716 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2717 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2718 3235492e 2018-04-01 stsp if (error != NULL)
2719 3235492e 2018-04-01 stsp return error;
2720 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2721 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2722 3235492e 2018-04-01 stsp if (error != NULL)
2723 3235492e 2018-04-01 stsp return error;
2724 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2726 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2727 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2728 3235492e 2018-04-01 stsp if (error == NULL) {
2729 1caad61b 2019-02-01 stsp int obj_type;
2730 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2731 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2732 d1f2edc9 2018-06-13 stsp if (error != NULL)
2733 1caad61b 2019-02-01 stsp goto done;
2734 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2735 1caad61b 2019-02-01 stsp if (error != NULL)
2736 1caad61b 2019-02-01 stsp goto done;
2737 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2738 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2739 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2740 1caad61b 2019-02-01 stsp if (error != NULL)
2741 1caad61b 2019-02-01 stsp goto done;
2742 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2743 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2744 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2745 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2746 1caad61b 2019-02-01 stsp goto done;
2749 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2750 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2751 1caad61b 2019-02-01 stsp if (id == NULL)
2752 638f9024 2019-05-13 stsp error = got_error_from_errno(
2753 230a42bd 2019-05-11 jcs "got_object_id_dup");
2754 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2755 1caad61b 2019-02-01 stsp if (error)
2756 1caad61b 2019-02-01 stsp goto done;
2757 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2758 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2759 1caad61b 2019-02-01 stsp goto done;
2761 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2762 d1f2edc9 2018-06-13 stsp if (error != NULL)
2763 1caad61b 2019-02-01 stsp goto done;
2765 15a94983 2018-12-23 stsp if (commit == NULL) {
2766 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2767 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2768 d1f2edc9 2018-06-13 stsp if (error != NULL)
2769 d1f2edc9 2018-06-13 stsp return error;
2772 d7d4f210 2018-03-12 stsp if (error != NULL)
2773 04ca23f4 2018-07-16 stsp goto done;
2775 deeabeae 2019-08-27 stsp if (worktree) {
2776 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2778 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2779 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2780 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2781 deeabeae 2019-08-27 stsp goto done;
2783 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2786 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2787 04ca23f4 2018-07-16 stsp if (error != NULL)
2788 04ca23f4 2018-07-16 stsp goto done;
2789 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2790 04ca23f4 2018-07-16 stsp free(path);
2791 04ca23f4 2018-07-16 stsp path = in_repo_path;
2794 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2795 199a4027 2019-02-02 stsp if (error)
2796 199a4027 2019-02-02 stsp goto done;
2798 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2799 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2801 04ca23f4 2018-07-16 stsp free(path);
2802 04ca23f4 2018-07-16 stsp free(repo_path);
2803 04ca23f4 2018-07-16 stsp free(cwd);
2805 cffc0aa4 2019-02-05 stsp if (worktree)
2806 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2807 ad242220 2018-09-08 stsp if (repo) {
2808 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2809 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2810 ad242220 2018-09-08 stsp if (error == NULL)
2811 ad242220 2018-09-08 stsp error = repo_error;
2813 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2814 8bf5b3c9 2018-03-17 stsp return error;
2817 4ed7e80c 2018-05-20 stsp __dead static void
2818 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2820 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2821 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2825 b72f483a 2019-02-05 stsp struct print_diff_arg {
2826 b72f483a 2019-02-05 stsp struct got_repository *repo;
2827 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2828 b72f483a 2019-02-05 stsp int diff_context;
2829 3fc0c068 2019-02-10 stsp const char *id_str;
2830 3fc0c068 2019-02-10 stsp int header_shown;
2831 98eaaa12 2019-08-03 stsp int diff_staged;
2832 63035f9f 2019-10-06 stsp int ignore_whitespace;
2835 4ed7e80c 2018-05-20 stsp static const struct got_error *
2836 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2837 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2838 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2839 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2841 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2842 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2843 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2844 12463d8b 2019-12-13 stsp int fd = -1;
2845 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2846 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2847 b72f483a 2019-02-05 stsp struct stat sb;
2849 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2850 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2851 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2852 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2853 98eaaa12 2019-08-03 stsp return NULL;
2855 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2856 98eaaa12 2019-08-03 stsp return NULL;
2857 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2858 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2859 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2860 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2861 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2862 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2863 98eaaa12 2019-08-03 stsp return NULL;
2866 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2867 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2868 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2869 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2870 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2873 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2874 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2875 98eaaa12 2019-08-03 stsp switch (staged_status) {
2876 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2877 98eaaa12 2019-08-03 stsp label1 = path;
2878 98eaaa12 2019-08-03 stsp label2 = path;
2880 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2881 98eaaa12 2019-08-03 stsp label2 = path;
2883 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2884 98eaaa12 2019-08-03 stsp label1 = path;
2887 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2889 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2890 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2891 63035f9f 2019-10-06 stsp a->repo, stdout);
2894 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2895 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2896 4ce46740 2019-08-08 stsp char *id_str;
2897 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2900 4ce46740 2019-08-08 stsp goto done;
2901 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2903 4ce46740 2019-08-08 stsp goto done;
2904 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2905 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2906 4ce46740 2019-08-08 stsp free(id_str);
2907 4ce46740 2019-08-08 stsp goto done;
2909 4ce46740 2019-08-08 stsp free(id_str);
2910 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2911 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2913 4ce46740 2019-08-08 stsp goto done;
2916 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2917 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2918 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2919 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2920 2ec1f75b 2019-03-26 stsp goto done;
2923 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2924 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2925 12463d8b 2019-12-13 stsp if (fd == -1) {
2926 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2927 12463d8b 2019-12-13 stsp goto done;
2930 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2931 12463d8b 2019-12-13 stsp if (fd == -1) {
2932 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2933 12463d8b 2019-12-13 stsp goto done;
2936 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2937 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2938 2ec1f75b 2019-03-26 stsp goto done;
2940 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2941 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2942 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2943 2ec1f75b 2019-03-26 stsp goto done;
2947 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2949 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2950 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2952 b72f483a 2019-02-05 stsp if (blob1)
2953 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2954 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2955 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2956 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2957 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2958 b72f483a 2019-02-05 stsp free(abspath);
2959 927df6b7 2019-02-10 stsp return err;
2962 927df6b7 2019-02-10 stsp static const struct got_error *
2963 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2965 b00d56cd 2018-04-01 stsp const struct got_error *error;
2966 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2967 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2968 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2969 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2970 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2971 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2972 15a94983 2018-12-23 stsp int type1, type2;
2973 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2974 c0cc5c62 2018-10-18 stsp const char *errstr;
2975 927df6b7 2019-02-10 stsp char *path = NULL;
2977 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2978 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2979 25eccc22 2019-01-04 stsp NULL) == -1)
2980 b00d56cd 2018-04-01 stsp err(1, "pledge");
2983 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2984 b00d56cd 2018-04-01 stsp switch (ch) {
2986 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2988 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2989 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2992 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2993 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2994 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2996 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2999 98eaaa12 2019-08-03 stsp diff_staged = 1;
3002 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3005 2deda0b9 2019-03-07 stsp usage_diff();
3006 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3010 b00d56cd 2018-04-01 stsp argc -= optind;
3011 b00d56cd 2018-04-01 stsp argv += optind;
3013 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3014 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3015 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3016 b72f483a 2019-02-05 stsp goto done;
3018 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3019 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3020 927df6b7 2019-02-10 stsp goto done;
3021 927df6b7 2019-02-10 stsp if (argc <= 1) {
3022 927df6b7 2019-02-10 stsp if (worktree == NULL) {
3023 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
3024 927df6b7 2019-02-10 stsp goto done;
3026 b72f483a 2019-02-05 stsp if (repo_path)
3028 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3029 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3030 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3031 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3032 927df6b7 2019-02-10 stsp goto done;
3034 927df6b7 2019-02-10 stsp if (argc == 1) {
3035 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3037 927df6b7 2019-02-10 stsp if (error)
3038 927df6b7 2019-02-10 stsp goto done;
3040 927df6b7 2019-02-10 stsp path = strdup("");
3041 927df6b7 2019-02-10 stsp if (path == NULL) {
3042 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3043 927df6b7 2019-02-10 stsp goto done;
3046 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3047 98eaaa12 2019-08-03 stsp if (diff_staged)
3048 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3049 98eaaa12 2019-08-03 stsp "objects in repository");
3050 15a94983 2018-12-23 stsp id_str1 = argv[0];
3051 15a94983 2018-12-23 stsp id_str2 = argv[1];
3052 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
3053 30db809c 2019-06-05 stsp repo_path =
3054 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
3055 30db809c 2019-06-05 stsp if (repo_path == NULL) {
3056 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
3057 30db809c 2019-06-05 stsp goto done;
3061 b00d56cd 2018-04-01 stsp usage_diff();
3063 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
3064 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
3065 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3066 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
3069 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3070 76089277 2018-04-01 stsp free(repo_path);
3071 b00d56cd 2018-04-01 stsp if (error != NULL)
3072 b00d56cd 2018-04-01 stsp goto done;
3074 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3075 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3076 c02c541e 2019-03-29 stsp if (error)
3077 c02c541e 2019-03-29 stsp goto done;
3079 30db809c 2019-06-05 stsp if (argc <= 1) {
3080 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3081 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3082 b72f483a 2019-02-05 stsp char *id_str;
3084 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3086 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3087 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3088 b72f483a 2019-02-05 stsp if (error)
3089 b72f483a 2019-02-05 stsp goto done;
3090 b72f483a 2019-02-05 stsp arg.repo = repo;
3091 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3092 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3093 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3094 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3095 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3096 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3098 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3099 72ea6654 2019-07-27 stsp if (error)
3100 72ea6654 2019-07-27 stsp goto done;
3102 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3103 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3104 b72f483a 2019-02-05 stsp free(id_str);
3105 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3106 b72f483a 2019-02-05 stsp goto done;
3109 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3110 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3111 0adb7196 2019-08-11 stsp if (error)
3112 0adb7196 2019-08-11 stsp goto done;
3114 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3115 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3116 0adb7196 2019-08-11 stsp if (error)
3117 0adb7196 2019-08-11 stsp goto done;
3119 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3120 15a94983 2018-12-23 stsp if (error)
3121 15a94983 2018-12-23 stsp goto done;
3123 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3124 15a94983 2018-12-23 stsp if (error)
3125 15a94983 2018-12-23 stsp goto done;
3127 15a94983 2018-12-23 stsp if (type1 != type2) {
3128 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3129 b00d56cd 2018-04-01 stsp goto done;
3132 15a94983 2018-12-23 stsp switch (type1) {
3133 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3134 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3135 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3137 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3138 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3139 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3141 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3142 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3143 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3144 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3147 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3150 5e70831e 2019-05-28 stsp free(label1);
3151 5e70831e 2019-05-28 stsp free(label2);
3152 15a94983 2018-12-23 stsp free(id1);
3153 15a94983 2018-12-23 stsp free(id2);
3154 927df6b7 2019-02-10 stsp free(path);
3155 b72f483a 2019-02-05 stsp if (worktree)
3156 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3157 ad242220 2018-09-08 stsp if (repo) {
3158 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3159 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3160 ad242220 2018-09-08 stsp if (error == NULL)
3161 ad242220 2018-09-08 stsp error = repo_error;
3163 b00d56cd 2018-04-01 stsp return error;
3166 404c43c4 2018-06-21 stsp __dead static void
3167 404c43c4 2018-06-21 stsp usage_blame(void)
3169 9270e621 2019-02-05 stsp fprintf(stderr,
3170 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3171 404c43c4 2018-06-21 stsp getprogname());
3175 28315671 2019-08-14 stsp struct blame_line {
3176 28315671 2019-08-14 stsp int annotated;
3177 28315671 2019-08-14 stsp char *id_str;
3178 82f6abb8 2019-08-14 stsp char *committer;
3179 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3182 28315671 2019-08-14 stsp struct blame_cb_args {
3183 28315671 2019-08-14 stsp struct blame_line *lines;
3184 28315671 2019-08-14 stsp int nlines;
3185 7ef28ff8 2019-08-14 stsp int nlines_prec;
3186 28315671 2019-08-14 stsp int lineno_cur;
3187 28315671 2019-08-14 stsp off_t *line_offsets;
3189 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3192 404c43c4 2018-06-21 stsp static const struct got_error *
3193 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3195 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3196 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3197 28315671 2019-08-14 stsp struct blame_line *bline;
3198 82f6abb8 2019-08-14 stsp char *line = NULL;
3199 28315671 2019-08-14 stsp size_t linesize = 0;
3200 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3201 28315671 2019-08-14 stsp off_t offset;
3202 bcb49d15 2019-08-14 stsp struct tm tm;
3203 bcb49d15 2019-08-14 stsp time_t committer_time;
3205 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3206 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3207 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3209 28315671 2019-08-14 stsp if (sigint_received)
3210 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3212 2839f8b9 2019-08-18 stsp if (lineno == -1)
3213 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3215 28315671 2019-08-14 stsp /* Annotate this line. */
3216 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3217 28315671 2019-08-14 stsp if (bline->annotated)
3218 28315671 2019-08-14 stsp return NULL;
3219 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3221 28315671 2019-08-14 stsp return err;
3223 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3225 82f6abb8 2019-08-14 stsp goto done;
3227 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3228 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3229 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3230 bcb49d15 2019-08-14 stsp goto done;
3233 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3234 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3235 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3236 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3237 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3238 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3239 82f6abb8 2019-08-14 stsp goto done;
3241 28315671 2019-08-14 stsp bline->annotated = 1;
3243 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3244 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3245 28315671 2019-08-14 stsp if (!bline->annotated)
3246 82f6abb8 2019-08-14 stsp goto done;
3248 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3249 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3250 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3251 82f6abb8 2019-08-14 stsp goto done;
3254 28315671 2019-08-14 stsp while (bline->annotated) {
3255 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3256 82f6abb8 2019-08-14 stsp size_t len;
3258 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3259 28315671 2019-08-14 stsp if (ferror(a->f))
3260 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3264 82f6abb8 2019-08-14 stsp committer = bline->committer;
3265 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3266 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3267 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3268 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3270 82f6abb8 2019-08-14 stsp *at = '\0';
3271 82f6abb8 2019-08-14 stsp len = strlen(committer);
3272 82f6abb8 2019-08-14 stsp if (len >= 9)
3273 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3275 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3277 28315671 2019-08-14 stsp *nl = '\0';
3278 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3279 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3281 28315671 2019-08-14 stsp a->lineno_cur++;
3282 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3285 82f6abb8 2019-08-14 stsp if (commit)
3286 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3287 28315671 2019-08-14 stsp free(line);
3288 28315671 2019-08-14 stsp return err;
3291 28315671 2019-08-14 stsp static const struct got_error *
3292 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3294 404c43c4 2018-06-21 stsp const struct got_error *error;
3295 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3296 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3297 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3298 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3299 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3300 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3301 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3302 28315671 2019-08-14 stsp struct blame_cb_args bca;
3303 28315671 2019-08-14 stsp int ch, obj_type, i;
3304 b02560ec 2019-08-19 stsp size_t filesize;
3306 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3308 404c43c4 2018-06-21 stsp #ifndef PROFILE
3309 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3310 36e2fb66 2019-01-04 stsp NULL) == -1)
3311 404c43c4 2018-06-21 stsp err(1, "pledge");
3314 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3315 404c43c4 2018-06-21 stsp switch (ch) {
3317 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3320 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3321 66bea077 2018-08-02 stsp if (repo_path == NULL)
3322 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3324 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3327 2deda0b9 2019-03-07 stsp usage_blame();
3328 404c43c4 2018-06-21 stsp /* NOTREACHED */
3332 404c43c4 2018-06-21 stsp argc -= optind;
3333 404c43c4 2018-06-21 stsp argv += optind;
3335 a39318fd 2018-08-02 stsp if (argc == 1)
3336 404c43c4 2018-06-21 stsp path = argv[0];
3338 404c43c4 2018-06-21 stsp usage_blame();
3340 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3341 66bea077 2018-08-02 stsp if (cwd == NULL) {
3342 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3343 66bea077 2018-08-02 stsp goto done;
3345 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3346 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3347 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3348 66bea077 2018-08-02 stsp goto done;
3350 0c06baac 2019-02-05 stsp error = NULL;
3351 0c06baac 2019-02-05 stsp if (worktree) {
3352 0c06baac 2019-02-05 stsp repo_path =
3353 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3354 0c06baac 2019-02-05 stsp if (repo_path == NULL)
3355 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3356 0c06baac 2019-02-05 stsp if (error)
3357 0c06baac 2019-02-05 stsp goto done;
3359 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3360 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3361 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3362 0c06baac 2019-02-05 stsp goto done;
3367 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3368 c02c541e 2019-03-29 stsp if (error != NULL)
3369 36e2fb66 2019-01-04 stsp goto done;
3371 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3372 c02c541e 2019-03-29 stsp if (error)
3373 404c43c4 2018-06-21 stsp goto done;
3375 0c06baac 2019-02-05 stsp if (worktree) {
3376 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3377 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3378 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3379 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3380 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3381 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3382 6efaaa2d 2019-02-05 stsp path) == -1) {
3383 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3384 0c06baac 2019-02-05 stsp goto done;
3386 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3389 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3391 0c06baac 2019-02-05 stsp if (error)
3392 66bea077 2018-08-02 stsp goto done;
3394 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3395 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3396 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3397 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3398 404c43c4 2018-06-21 stsp if (error != NULL)
3399 66bea077 2018-08-02 stsp goto done;
3400 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3401 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3402 404c43c4 2018-06-21 stsp if (error != NULL)
3403 66bea077 2018-08-02 stsp goto done;
3405 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3406 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3407 30837e32 2019-07-25 stsp if (error)
3408 66bea077 2018-08-02 stsp goto done;
3411 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3412 28315671 2019-08-14 stsp if (error)
3413 28315671 2019-08-14 stsp goto done;
3415 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3416 28315671 2019-08-14 stsp if (error)
3417 28315671 2019-08-14 stsp goto done;
3419 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3420 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3421 28315671 2019-08-14 stsp goto done;
3424 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3425 28315671 2019-08-14 stsp if (error)
3426 28315671 2019-08-14 stsp goto done;
3427 28315671 2019-08-14 stsp bca.f = got_opentemp();
3428 28315671 2019-08-14 stsp if (bca.f == NULL) {
3429 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3430 28315671 2019-08-14 stsp goto done;
3432 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3433 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3434 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3435 28315671 2019-08-14 stsp goto done;
3437 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3438 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3439 b02560ec 2019-08-19 stsp bca.nlines--;
3441 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3442 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3443 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3444 28315671 2019-08-14 stsp goto done;
3446 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3447 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3448 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3449 7ef28ff8 2019-08-14 stsp while (i > 0) {
3451 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3453 82f6abb8 2019-08-14 stsp bca.repo = repo;
3455 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3456 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3458 66bea077 2018-08-02 stsp free(in_repo_path);
3459 66bea077 2018-08-02 stsp free(repo_path);
3460 66bea077 2018-08-02 stsp free(cwd);
3461 404c43c4 2018-06-21 stsp free(commit_id);
3462 28315671 2019-08-14 stsp free(obj_id);
3464 28315671 2019-08-14 stsp got_object_blob_close(blob);
3465 0c06baac 2019-02-05 stsp if (worktree)
3466 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3467 ad242220 2018-09-08 stsp if (repo) {
3468 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3469 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3470 ad242220 2018-09-08 stsp if (error == NULL)
3471 ad242220 2018-09-08 stsp error = repo_error;
3473 3affba96 2019-09-22 stsp if (bca.lines) {
3474 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3475 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3476 3affba96 2019-09-22 stsp free(bline->id_str);
3477 3affba96 2019-09-22 stsp free(bline->committer);
3479 3affba96 2019-09-22 stsp free(bca.lines);
3481 28315671 2019-08-14 stsp free(bca.line_offsets);
3482 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3483 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3484 404c43c4 2018-06-21 stsp return error;
3487 5de5890b 2018-10-18 stsp __dead static void
3488 5de5890b 2018-10-18 stsp usage_tree(void)
3490 c1669e2e 2019-01-09 stsp fprintf(stderr,
3491 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3492 5de5890b 2018-10-18 stsp getprogname());
3496 c1669e2e 2019-01-09 stsp static void
3497 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3498 c1669e2e 2019-01-09 stsp const char *root_path)
3500 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3501 848d6979 2019-08-12 stsp const char *modestr = "";
3502 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3504 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3505 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3508 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3509 63c5ca5d 2019-08-24 stsp modestr = "$";
3510 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3511 848d6979 2019-08-12 stsp modestr = "@";
3512 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3513 848d6979 2019-08-12 stsp modestr = "/";
3514 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3515 848d6979 2019-08-12 stsp modestr = "*";
3517 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3518 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3521 5de5890b 2018-10-18 stsp static const struct got_error *
3522 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3523 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3524 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3526 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3527 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3528 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3529 56e0773d 2019-11-28 stsp int nentries, i;
3531 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3533 5de5890b 2018-10-18 stsp goto done;
3535 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3537 5de5890b 2018-10-18 stsp goto done;
3538 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3539 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3540 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3541 5de5890b 2018-10-18 stsp char *id = NULL;
3543 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3546 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3547 5de5890b 2018-10-18 stsp if (show_ids) {
3548 5de5890b 2018-10-18 stsp char *id_str;
3549 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3550 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3552 5de5890b 2018-10-18 stsp goto done;
3553 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3554 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3555 5de5890b 2018-10-18 stsp free(id_str);
3556 5de5890b 2018-10-18 stsp goto done;
3558 5de5890b 2018-10-18 stsp free(id_str);
3560 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3563 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3564 c1669e2e 2019-01-09 stsp char *child_path;
3565 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3566 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3567 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3568 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3569 c1669e2e 2019-01-09 stsp goto done;
3571 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3572 c1669e2e 2019-01-09 stsp root_path, repo);
3573 c1669e2e 2019-01-09 stsp free(child_path);
3575 c1669e2e 2019-01-09 stsp goto done;
3580 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3581 5de5890b 2018-10-18 stsp free(tree_id);
3582 5de5890b 2018-10-18 stsp return err;
3585 5de5890b 2018-10-18 stsp static const struct got_error *
3586 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3588 5de5890b 2018-10-18 stsp const struct got_error *error;
3589 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3590 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3591 7a2c19d6 2019-02-05 stsp const char *path;
3592 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3593 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3594 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3595 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3598 5de5890b 2018-10-18 stsp #ifndef PROFILE
3599 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3600 0f8d269b 2019-01-04 stsp NULL) == -1)
3601 5de5890b 2018-10-18 stsp err(1, "pledge");
3604 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3605 5de5890b 2018-10-18 stsp switch (ch) {
3607 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3610 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3611 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3612 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3614 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3617 5de5890b 2018-10-18 stsp show_ids = 1;
3620 c1669e2e 2019-01-09 stsp recurse = 1;
3623 2deda0b9 2019-03-07 stsp usage_tree();
3624 5de5890b 2018-10-18 stsp /* NOTREACHED */
3628 5de5890b 2018-10-18 stsp argc -= optind;
3629 5de5890b 2018-10-18 stsp argv += optind;
3631 5de5890b 2018-10-18 stsp if (argc == 1)
3632 5de5890b 2018-10-18 stsp path = argv[0];
3633 5de5890b 2018-10-18 stsp else if (argc > 1)
3634 5de5890b 2018-10-18 stsp usage_tree();
3636 7a2c19d6 2019-02-05 stsp path = NULL;
3638 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3639 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3640 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3641 9bf7a39b 2019-02-05 stsp goto done;
3643 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3644 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3645 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3646 7a2c19d6 2019-02-05 stsp goto done;
3648 7a2c19d6 2019-02-05 stsp error = NULL;
3649 7a2c19d6 2019-02-05 stsp if (worktree) {
3650 7a2c19d6 2019-02-05 stsp repo_path =
3651 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3652 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3653 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3654 7a2c19d6 2019-02-05 stsp if (error)
3655 7a2c19d6 2019-02-05 stsp goto done;
3657 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3658 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3659 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3660 7a2c19d6 2019-02-05 stsp goto done;
3665 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3666 5de5890b 2018-10-18 stsp if (error != NULL)
3667 c02c541e 2019-03-29 stsp goto done;
3669 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3670 c02c541e 2019-03-29 stsp if (error)
3671 5de5890b 2018-10-18 stsp goto done;
3673 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3674 9bf7a39b 2019-02-05 stsp if (worktree) {
3675 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3676 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3677 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3678 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3679 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3680 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3681 9bf7a39b 2019-02-05 stsp goto done;
3683 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3685 9bf7a39b 2019-02-05 stsp if (error)
3686 9bf7a39b 2019-02-05 stsp goto done;
3688 9bf7a39b 2019-02-05 stsp path = "/";
3690 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3691 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3692 9bf7a39b 2019-02-05 stsp if (error != NULL)
3693 9bf7a39b 2019-02-05 stsp goto done;
3696 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3697 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3698 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3699 5de5890b 2018-10-18 stsp if (error != NULL)
3700 5de5890b 2018-10-18 stsp goto done;
3701 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3702 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3703 5de5890b 2018-10-18 stsp if (error != NULL)
3704 5de5890b 2018-10-18 stsp goto done;
3706 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3707 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3708 30837e32 2019-07-25 stsp if (error)
3709 5de5890b 2018-10-18 stsp goto done;
3712 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3713 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3715 5de5890b 2018-10-18 stsp free(in_repo_path);
3716 5de5890b 2018-10-18 stsp free(repo_path);
3717 5de5890b 2018-10-18 stsp free(cwd);
3718 5de5890b 2018-10-18 stsp free(commit_id);
3719 7a2c19d6 2019-02-05 stsp if (worktree)
3720 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3721 5de5890b 2018-10-18 stsp if (repo) {
3722 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3723 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3724 5de5890b 2018-10-18 stsp if (error == NULL)
3725 5de5890b 2018-10-18 stsp error = repo_error;
3727 5de5890b 2018-10-18 stsp return error;
3730 6bad629b 2019-02-04 stsp __dead static void
3731 6bad629b 2019-02-04 stsp usage_status(void)
3733 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3737 b72f483a 2019-02-05 stsp static const struct got_error *
3738 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3739 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3740 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3743 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3744 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3745 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3746 b72f483a 2019-02-05 stsp return NULL;
3749 6bad629b 2019-02-04 stsp static const struct got_error *
3750 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3752 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3753 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3754 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3755 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3756 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3757 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3760 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3762 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3763 6bad629b 2019-02-04 stsp switch (ch) {
3765 2deda0b9 2019-03-07 stsp usage_status();
3766 6bad629b 2019-02-04 stsp /* NOTREACHED */
3770 6bad629b 2019-02-04 stsp argc -= optind;
3771 6bad629b 2019-02-04 stsp argv += optind;
3773 6bad629b 2019-02-04 stsp #ifndef PROFILE
3774 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3775 6bad629b 2019-02-04 stsp NULL) == -1)
3776 6bad629b 2019-02-04 stsp err(1, "pledge");
3778 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3779 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3780 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3781 927df6b7 2019-02-10 stsp goto done;
3784 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3785 927df6b7 2019-02-10 stsp if (error != NULL)
3786 a5edda0a 2019-07-27 stsp goto done;
3788 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3790 6bad629b 2019-02-04 stsp if (error != NULL)
3791 6bad629b 2019-02-04 stsp goto done;
3793 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3794 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3795 087fb88c 2019-08-04 stsp if (error)
3796 087fb88c 2019-08-04 stsp goto done;
3798 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3799 6bad629b 2019-02-04 stsp if (error)
3800 6bad629b 2019-02-04 stsp goto done;
3802 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3803 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3805 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3806 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3807 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3808 927df6b7 2019-02-10 stsp free(cwd);
3809 d0eebce4 2019-03-11 stsp return error;
3812 d0eebce4 2019-03-11 stsp __dead static void
3813 d0eebce4 2019-03-11 stsp usage_ref(void)
3815 d0eebce4 2019-03-11 stsp fprintf(stderr,
3816 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3817 d0eebce4 2019-03-11 stsp getprogname());
3821 d0eebce4 2019-03-11 stsp static const struct got_error *
3822 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3824 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3825 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3826 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3828 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3829 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3831 d0eebce4 2019-03-11 stsp return err;
3833 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3834 d0eebce4 2019-03-11 stsp char *refstr;
3835 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3836 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3837 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3838 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3839 d0eebce4 2019-03-11 stsp free(refstr);
3842 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3843 d0eebce4 2019-03-11 stsp return NULL;
3846 d0eebce4 2019-03-11 stsp static const struct got_error *
3847 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3849 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3850 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3852 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3854 d0eebce4 2019-03-11 stsp return err;
3856 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3857 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3858 d0eebce4 2019-03-11 stsp return err;
3861 d0eebce4 2019-03-11 stsp static const struct got_error *
3862 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3864 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3865 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3866 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3869 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3870 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3871 d1644381 2019-07-14 stsp * an unintended typo.
3873 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3874 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3876 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3878 d83d9d5c 2019-05-13 stsp if (err) {
3879 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3881 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3882 d83d9d5c 2019-05-13 stsp return err;
3883 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3885 d83d9d5c 2019-05-13 stsp return err;
3886 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3887 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3889 d83d9d5c 2019-05-13 stsp return err;
3892 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3894 d0eebce4 2019-03-11 stsp goto done;
3896 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3899 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3901 d1c1ae5f 2019-08-12 stsp return err;
3904 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3905 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3907 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3908 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3909 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3912 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3913 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3914 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3916 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3917 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3919 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3921 d1c1ae5f 2019-08-12 stsp return err;
3923 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3925 d1c1ae5f 2019-08-12 stsp goto done;
3927 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3929 d1c1ae5f 2019-08-12 stsp if (target_ref)
3930 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3932 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3933 d0eebce4 2019-03-11 stsp return err;
3936 d0eebce4 2019-03-11 stsp static const struct got_error *
3937 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3939 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3940 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3941 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3942 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3943 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3944 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3946 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3947 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3948 d0eebce4 2019-03-11 stsp switch (ch) {
3950 d0eebce4 2019-03-11 stsp delref = optarg;
3953 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3954 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3955 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3957 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3960 d0eebce4 2019-03-11 stsp do_list = 1;
3963 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3966 d0eebce4 2019-03-11 stsp usage_ref();
3967 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3971 d0eebce4 2019-03-11 stsp if (do_list && delref)
3972 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3974 d0eebce4 2019-03-11 stsp argc -= optind;
3975 d0eebce4 2019-03-11 stsp argv += optind;
3977 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3978 d1c1ae5f 2019-08-12 stsp if (create_symref)
3979 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3980 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3981 d0eebce4 2019-03-11 stsp if (argc > 0)
3982 d0eebce4 2019-03-11 stsp usage_ref();
3983 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3984 d0eebce4 2019-03-11 stsp usage_ref();
3986 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3987 e0b57350 2019-03-12 stsp if (do_list) {
3988 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3989 e0b57350 2019-03-12 stsp NULL) == -1)
3990 e0b57350 2019-03-12 stsp err(1, "pledge");
3992 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3993 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3994 e0b57350 2019-03-12 stsp err(1, "pledge");
3997 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3998 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3999 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4000 d0eebce4 2019-03-11 stsp goto done;
4003 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4004 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4005 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4006 d0eebce4 2019-03-11 stsp goto done;
4008 d0eebce4 2019-03-11 stsp error = NULL;
4009 d0eebce4 2019-03-11 stsp if (worktree) {
4010 d0eebce4 2019-03-11 stsp repo_path =
4011 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4012 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4013 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4014 d0eebce4 2019-03-11 stsp if (error)
4015 d0eebce4 2019-03-11 stsp goto done;
4017 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4018 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4019 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4020 d0eebce4 2019-03-11 stsp goto done;
4025 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4026 d0eebce4 2019-03-11 stsp if (error != NULL)
4027 d0eebce4 2019-03-11 stsp goto done;
4029 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4030 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4031 c02c541e 2019-03-29 stsp if (error)
4032 c02c541e 2019-03-29 stsp goto done;
4034 d0eebce4 2019-03-11 stsp if (do_list)
4035 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4036 d0eebce4 2019-03-11 stsp else if (delref)
4037 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4038 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4039 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4041 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4044 4e759de4 2019-06-26 stsp got_repo_close(repo);
4045 4e759de4 2019-06-26 stsp if (worktree)
4046 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4047 4e759de4 2019-06-26 stsp free(cwd);
4048 4e759de4 2019-06-26 stsp free(repo_path);
4049 4e759de4 2019-06-26 stsp return error;
4052 4e759de4 2019-06-26 stsp __dead static void
4053 4e759de4 2019-06-26 stsp usage_branch(void)
4055 4e759de4 2019-06-26 stsp fprintf(stderr,
4056 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4057 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4061 4e759de4 2019-06-26 stsp static const struct got_error *
4062 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4063 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4065 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4066 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4067 4e99b47e 2019-10-04 stsp char *refstr;
4069 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4070 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4071 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4072 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4074 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4076 4e99b47e 2019-10-04 stsp return err;
4077 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4078 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4079 4e99b47e 2019-10-04 stsp marker = "* ";
4081 4e99b47e 2019-10-04 stsp marker = "~ ";
4085 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4086 4e99b47e 2019-10-04 stsp refname += 11;
4087 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4088 4e99b47e 2019-10-04 stsp refname += 18;
4090 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4091 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4092 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4094 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4095 4e99b47e 2019-10-04 stsp free(refstr);
4096 4e99b47e 2019-10-04 stsp return NULL;
4099 4e99b47e 2019-10-04 stsp static const struct got_error *
4100 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4102 ad89fa31 2019-10-04 stsp const char *refname;
4104 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4105 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4107 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4109 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4110 ad89fa31 2019-10-04 stsp refname += 11;
4111 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4112 ad89fa31 2019-10-04 stsp refname += 18;
4114 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4116 ad89fa31 2019-10-04 stsp return NULL;
4119 ad89fa31 2019-10-04 stsp static const struct got_error *
4120 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4122 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4123 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4124 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4125 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4126 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4128 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4130 4e99b47e 2019-10-04 stsp if (worktree) {
4131 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4132 4e99b47e 2019-10-04 stsp worktree);
4134 4e99b47e 2019-10-04 stsp return err;
4136 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4137 4e99b47e 2019-10-04 stsp worktree);
4139 4e99b47e 2019-10-04 stsp return err;
4141 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4142 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4143 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4145 4e99b47e 2019-10-04 stsp return err;
4146 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4147 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4151 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4152 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4154 4e759de4 2019-06-26 stsp return err;
4156 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4157 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4159 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4160 4e759de4 2019-06-26 stsp return NULL;
4163 4e759de4 2019-06-26 stsp static const struct got_error *
4164 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4165 45cd4e47 2019-08-25 stsp const char *branch_name)
4167 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4168 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4169 4e759de4 2019-06-26 stsp char *refname;
4171 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4172 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4174 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4176 4e759de4 2019-06-26 stsp goto done;
4178 45cd4e47 2019-08-25 stsp if (worktree &&
4179 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4180 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4181 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4182 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4183 45cd4e47 2019-08-25 stsp goto done;
4186 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4189 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4190 4e759de4 2019-06-26 stsp free(refname);
4191 4e759de4 2019-06-26 stsp return err;
4194 4e759de4 2019-06-26 stsp static const struct got_error *
4195 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4196 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4198 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4199 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4200 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4203 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4204 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4205 d3f84d51 2019-07-11 stsp * an unintended typo.
4207 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4208 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4210 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4211 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4212 4e759de4 2019-06-26 stsp goto done;
4215 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4216 4e759de4 2019-06-26 stsp if (err == NULL) {
4217 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4218 4e759de4 2019-06-26 stsp goto done;
4219 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4220 4e759de4 2019-06-26 stsp goto done;
4222 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4224 4e759de4 2019-06-26 stsp goto done;
4226 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4229 4e759de4 2019-06-26 stsp got_ref_close(ref);
4230 4e759de4 2019-06-26 stsp free(base_refname);
4231 4e759de4 2019-06-26 stsp free(refname);
4232 4e759de4 2019-06-26 stsp return err;
4235 4e759de4 2019-06-26 stsp static const struct got_error *
4236 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4238 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4239 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4240 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4241 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4242 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4243 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4244 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4245 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4246 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4247 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4248 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4250 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4252 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4253 4e759de4 2019-06-26 stsp switch (ch) {
4255 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4258 4e759de4 2019-06-26 stsp delref = optarg;
4261 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4262 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4263 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4265 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4268 4e759de4 2019-06-26 stsp do_list = 1;
4271 da76fce2 2020-02-24 stsp do_update = 0;
4274 4e759de4 2019-06-26 stsp usage_branch();
4275 4e759de4 2019-06-26 stsp /* NOTREACHED */
4279 4e759de4 2019-06-26 stsp if (do_list && delref)
4280 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4282 4e759de4 2019-06-26 stsp argc -= optind;
4283 4e759de4 2019-06-26 stsp argv += optind;
4285 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4286 ad89fa31 2019-10-04 stsp do_show = 1;
4288 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4289 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4291 4e759de4 2019-06-26 stsp if (do_list || delref) {
4292 4e759de4 2019-06-26 stsp if (argc > 0)
4293 4e759de4 2019-06-26 stsp usage_branch();
4294 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4295 4e759de4 2019-06-26 stsp usage_branch();
4297 4e759de4 2019-06-26 stsp #ifndef PROFILE
4298 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4299 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4300 4e759de4 2019-06-26 stsp NULL) == -1)
4301 4e759de4 2019-06-26 stsp err(1, "pledge");
4303 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4304 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4305 4e759de4 2019-06-26 stsp err(1, "pledge");
4308 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4309 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4310 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4311 4e759de4 2019-06-26 stsp goto done;
4314 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4315 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4316 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4317 4e759de4 2019-06-26 stsp goto done;
4319 4e759de4 2019-06-26 stsp error = NULL;
4320 4e759de4 2019-06-26 stsp if (worktree) {
4321 4e759de4 2019-06-26 stsp repo_path =
4322 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4323 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4324 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4325 4e759de4 2019-06-26 stsp if (error)
4326 4e759de4 2019-06-26 stsp goto done;
4328 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4329 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4330 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4331 4e759de4 2019-06-26 stsp goto done;
4336 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4337 4e759de4 2019-06-26 stsp if (error != NULL)
4338 4e759de4 2019-06-26 stsp goto done;
4340 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4341 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4342 4e759de4 2019-06-26 stsp if (error)
4343 4e759de4 2019-06-26 stsp goto done;
4345 ad89fa31 2019-10-04 stsp if (do_show)
4346 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4347 ad89fa31 2019-10-04 stsp else if (do_list)
4348 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4349 4e759de4 2019-06-26 stsp else if (delref)
4350 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4352 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4353 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4354 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4355 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4356 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4357 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4358 a74f7e83 2019-11-10 stsp if (error)
4359 a74f7e83 2019-11-10 stsp goto done;
4360 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4361 da76fce2 2020-02-24 stsp if (error)
4362 da76fce2 2020-02-24 stsp goto done;
4363 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4364 da76fce2 2020-02-24 stsp int did_something = 0;
4365 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4367 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4368 da76fce2 2020-02-24 stsp if (error)
4369 da76fce2 2020-02-24 stsp goto done;
4370 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4371 da76fce2 2020-02-24 stsp worktree);
4372 da76fce2 2020-02-24 stsp if (error)
4373 da76fce2 2020-02-24 stsp goto done;
4374 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4376 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4377 da76fce2 2020-02-24 stsp goto done;
4379 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4380 da76fce2 2020-02-24 stsp free(branch_refname);
4381 da76fce2 2020-02-24 stsp if (error)
4382 da76fce2 2020-02-24 stsp goto done;
4383 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4385 da76fce2 2020-02-24 stsp if (error)
4386 da76fce2 2020-02-24 stsp goto done;
4387 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4388 da76fce2 2020-02-24 stsp commit_id);
4389 da76fce2 2020-02-24 stsp if (error)
4390 da76fce2 2020-02-24 stsp goto done;
4391 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4392 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4393 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4394 da76fce2 2020-02-24 stsp if (error)
4395 da76fce2 2020-02-24 stsp goto done;
4396 da76fce2 2020-02-24 stsp if (did_something)
4397 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4402 da76fce2 2020-02-24 stsp got_ref_close(ref);
4404 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4405 d0eebce4 2019-03-11 stsp if (worktree)
4406 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4407 d0eebce4 2019-03-11 stsp free(cwd);
4408 d0eebce4 2019-03-11 stsp free(repo_path);
4409 da76fce2 2020-02-24 stsp free(commit_id);
4410 da76fce2 2020-02-24 stsp free(commit_id_str);
4411 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4412 da76fce2 2020-02-24 stsp free((char *)pe->path);
4413 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4414 d00136be 2019-03-26 stsp return error;
4418 d00136be 2019-03-26 stsp __dead static void
4419 8e7bd50a 2019-08-22 stsp usage_tag(void)
4421 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4422 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4423 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4428 b8bad2ba 2019-08-23 stsp static const struct got_error *
4429 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4431 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4432 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4433 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4434 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4435 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4437 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4438 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4439 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4440 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4442 b8bad2ba 2019-08-23 stsp return err;
4443 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4446 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4449 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4450 b8bad2ba 2019-08-23 stsp free(re_id);
4453 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4454 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4457 b8bad2ba 2019-08-23 stsp while (se) {
4458 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4461 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4462 b8bad2ba 2019-08-23 stsp free(se_id);
4465 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4466 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4468 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4469 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4471 b8bad2ba 2019-08-23 stsp return err;
4472 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4475 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4480 b8bad2ba 2019-08-23 stsp return err;
4484 b8bad2ba 2019-08-23 stsp static const struct got_error *
4485 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4487 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4488 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4489 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4491 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4493 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4495 8e7bd50a 2019-08-22 stsp return err;
4497 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4498 8e7bd50a 2019-08-22 stsp const char *refname;
4499 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4500 8e7bd50a 2019-08-22 stsp char datebuf[26];
4501 d4efa91b 2020-01-14 stsp const char *tagger;
4502 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4503 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4504 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4505 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4507 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4508 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4510 8e7bd50a 2019-08-22 stsp refname += 10;
4511 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4512 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4513 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4516 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4517 8e7bd50a 2019-08-22 stsp free(refstr);
4519 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4522 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4523 d4efa91b 2020-01-14 stsp if (err) {
4524 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4528 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4529 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4530 d4efa91b 2020-01-14 stsp if (err) {
4534 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4535 d4efa91b 2020-01-14 stsp tagger_time =
4536 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4537 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4543 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4544 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4545 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4546 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4550 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4551 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4552 2417344c 2019-08-23 stsp if (datestr)
4553 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4554 d4efa91b 2020-01-14 stsp if (commit)
4555 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4557 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4558 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4559 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4562 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4563 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4566 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4567 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4570 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4571 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4578 8e7bd50a 2019-08-22 stsp free(id_str);
4579 d4efa91b 2020-01-14 stsp if (commit) {
4580 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4583 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4585 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4586 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4587 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4588 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4593 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4595 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4597 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4598 8e7bd50a 2019-08-22 stsp } while (line);
4599 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4602 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4603 8e7bd50a 2019-08-22 stsp return NULL;
4606 8e7bd50a 2019-08-22 stsp static const struct got_error *
4607 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4608 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4610 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4611 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4612 f372d5cd 2019-10-21 stsp char *editor = NULL;
4613 8e7bd50a 2019-08-22 stsp int fd = -1;
4615 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4616 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4617 8e7bd50a 2019-08-22 stsp goto done;
4620 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4621 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4622 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4623 8e7bd50a 2019-08-22 stsp goto done;
4626 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4628 8e7bd50a 2019-08-22 stsp goto done;
4630 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4631 8e7bd50a 2019-08-22 stsp close(fd);
4633 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4635 8e7bd50a 2019-08-22 stsp goto done;
4636 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4638 8e7bd50a 2019-08-22 stsp free(initial_content);
4639 8e7bd50a 2019-08-22 stsp free(template);
4640 8e7bd50a 2019-08-22 stsp free(editor);
4642 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4643 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4644 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4645 8e7bd50a 2019-08-22 stsp if (err) {
4646 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4647 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4650 8e7bd50a 2019-08-22 stsp return err;
4653 8e7bd50a 2019-08-22 stsp static const struct got_error *
4654 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4655 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4657 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4658 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4659 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4660 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4661 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4662 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4663 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4666 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4667 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4668 8e7bd50a 2019-08-22 stsp * an unintended typo.
4670 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4671 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4673 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4675 8e7bd50a 2019-08-22 stsp return err;
4677 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4678 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4680 8e7bd50a 2019-08-22 stsp goto done;
4682 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4684 8e7bd50a 2019-08-22 stsp goto done;
4686 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4687 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4688 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4689 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4690 8e7bd50a 2019-08-22 stsp goto done;
4692 8e7bd50a 2019-08-22 stsp tag_name += 10;
4693 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4694 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4695 8e7bd50a 2019-08-22 stsp goto done;
4698 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4699 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4700 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4701 8e7bd50a 2019-08-22 stsp goto done;
4702 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4703 8e7bd50a 2019-08-22 stsp goto done;
4705 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4706 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4707 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4708 f372d5cd 2019-10-21 stsp if (err) {
4709 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4710 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4711 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4712 8e7bd50a 2019-08-22 stsp goto done;
4716 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4717 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4718 f372d5cd 2019-10-21 stsp if (err) {
4719 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4720 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4721 8e7bd50a 2019-08-22 stsp goto done;
4724 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4725 f372d5cd 2019-10-21 stsp if (err) {
4726 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4727 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4728 8e7bd50a 2019-08-22 stsp goto done;
4731 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4732 f372d5cd 2019-10-21 stsp if (err) {
4733 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4734 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4735 f372d5cd 2019-10-21 stsp goto done;
4738 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4739 f372d5cd 2019-10-21 stsp if (err) {
4740 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4741 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4742 f372d5cd 2019-10-21 stsp goto done;
4744 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4746 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4747 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4748 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4749 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4750 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4751 f372d5cd 2019-10-21 stsp free(tag_id_str);
4753 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4754 8e7bd50a 2019-08-22 stsp free(commit_id);
4755 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4756 8e7bd50a 2019-08-22 stsp free(refname);
4757 8e7bd50a 2019-08-22 stsp free(tagmsg);
4758 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4759 aba9c984 2019-09-08 stsp free(tagger);
4760 8e7bd50a 2019-08-22 stsp return err;
4763 8e7bd50a 2019-08-22 stsp static const struct got_error *
4764 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4766 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4767 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4768 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4769 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4770 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4771 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4772 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4774 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4775 8e7bd50a 2019-08-22 stsp switch (ch) {
4777 80106605 2020-02-24 stsp commit_id_arg = optarg;
4780 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4783 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4784 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4785 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4787 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4790 8e7bd50a 2019-08-22 stsp do_list = 1;
4793 8e7bd50a 2019-08-22 stsp usage_tag();
4794 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4798 8e7bd50a 2019-08-22 stsp argc -= optind;
4799 8e7bd50a 2019-08-22 stsp argv += optind;
4801 8e7bd50a 2019-08-22 stsp if (do_list) {
4802 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4803 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4804 8e7bd50a 2019-08-22 stsp if (tagmsg)
4805 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4806 8e7bd50a 2019-08-22 stsp if (argc > 0)
4807 8e7bd50a 2019-08-22 stsp usage_tag();
4808 80106605 2020-02-24 stsp } else if (argc != 1)
4809 8e7bd50a 2019-08-22 stsp usage_tag();
4811 a2887370 2019-08-23 stsp tag_name = argv[0];
4813 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4814 8e7bd50a 2019-08-22 stsp if (do_list) {
4815 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4816 8e7bd50a 2019-08-22 stsp NULL) == -1)
4817 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4819 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4820 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4821 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4824 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4825 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4826 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4827 8e7bd50a 2019-08-22 stsp goto done;
4830 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4831 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4832 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4833 8e7bd50a 2019-08-22 stsp goto done;
4835 8e7bd50a 2019-08-22 stsp error = NULL;
4836 8e7bd50a 2019-08-22 stsp if (worktree) {
4837 8e7bd50a 2019-08-22 stsp repo_path =
4838 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4839 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4840 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4841 8e7bd50a 2019-08-22 stsp if (error)
4842 8e7bd50a 2019-08-22 stsp goto done;
4844 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4845 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4846 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4847 8e7bd50a 2019-08-22 stsp goto done;
4852 8e7bd50a 2019-08-22 stsp if (do_list) {
4853 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4854 c9956ddf 2019-09-08 stsp if (error != NULL)
4855 c9956ddf 2019-09-08 stsp goto done;
4856 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4857 8e7bd50a 2019-08-22 stsp if (error)
4858 8e7bd50a 2019-08-22 stsp goto done;
4859 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4861 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4862 c9956ddf 2019-09-08 stsp if (error)
4863 c9956ddf 2019-09-08 stsp goto done;
4864 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4865 c9956ddf 2019-09-08 stsp if (error != NULL)
4866 c9956ddf 2019-09-08 stsp goto done;
4868 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4869 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4870 8e7bd50a 2019-08-22 stsp if (error)
4871 8e7bd50a 2019-08-22 stsp goto done;
4874 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4875 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4876 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4877 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4878 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4879 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4880 8e7bd50a 2019-08-22 stsp if (error)
4881 8e7bd50a 2019-08-22 stsp goto done;
4882 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4883 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4884 8e7bd50a 2019-08-22 stsp if (error)
4885 8e7bd50a 2019-08-22 stsp goto done;
4886 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4887 8e7bd50a 2019-08-22 stsp free(commit_id);
4888 8e7bd50a 2019-08-22 stsp if (error)
4889 8e7bd50a 2019-08-22 stsp goto done;
4892 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4893 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4897 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4898 8e7bd50a 2019-08-22 stsp if (worktree)
4899 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4900 8e7bd50a 2019-08-22 stsp free(cwd);
4901 8e7bd50a 2019-08-22 stsp free(repo_path);
4902 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4903 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4904 8e7bd50a 2019-08-22 stsp return error;
4907 8e7bd50a 2019-08-22 stsp __dead static void
4908 d00136be 2019-03-26 stsp usage_add(void)
4910 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4911 022fae89 2019-12-06 tracey getprogname());
4915 d00136be 2019-03-26 stsp static const struct got_error *
4916 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4918 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4920 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4921 4e68cba3 2019-11-23 stsp return NULL;
4924 4e68cba3 2019-11-23 stsp static const struct got_error *
4925 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4927 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4928 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4929 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4930 1dd54920 2019-05-11 stsp char *cwd = NULL;
4931 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4932 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4933 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4935 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4937 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4938 d00136be 2019-03-26 stsp switch (ch) {
4939 022fae89 2019-12-06 tracey case 'I':
4940 022fae89 2019-12-06 tracey no_ignores = 1;
4943 4e68cba3 2019-11-23 stsp can_recurse = 1;
4946 d00136be 2019-03-26 stsp usage_add();
4947 d00136be 2019-03-26 stsp /* NOTREACHED */
4951 d00136be 2019-03-26 stsp argc -= optind;
4952 d00136be 2019-03-26 stsp argv += optind;
4954 43012d58 2019-07-14 stsp #ifndef PROFILE
4955 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4956 43012d58 2019-07-14 stsp NULL) == -1)
4957 43012d58 2019-07-14 stsp err(1, "pledge");
4959 723c305c 2019-05-11 jcs if (argc < 1)
4960 d00136be 2019-03-26 stsp usage_add();
4962 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4963 d00136be 2019-03-26 stsp if (cwd == NULL) {
4964 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4965 d00136be 2019-03-26 stsp goto done;
4968 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4969 d00136be 2019-03-26 stsp if (error)
4970 d00136be 2019-03-26 stsp goto done;
4972 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4974 031a5338 2019-03-26 stsp if (error != NULL)
4975 031a5338 2019-03-26 stsp goto done;
4977 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4978 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4979 d00136be 2019-03-26 stsp if (error)
4980 d00136be 2019-03-26 stsp goto done;
4982 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4983 6d022e97 2019-08-04 stsp if (error)
4984 022fae89 2019-12-06 tracey goto done;
4986 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4987 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4988 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4989 6d022e97 2019-08-04 stsp goto done;
4993 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4994 4e68cba3 2019-11-23 stsp char *ondisk_path;
4995 4e68cba3 2019-11-23 stsp struct stat sb;
4996 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4997 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4998 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4999 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5000 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5001 4e68cba3 2019-11-23 stsp goto done;
5003 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5004 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5005 4e68cba3 2019-11-23 stsp free(ondisk_path);
5008 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5009 4e68cba3 2019-11-23 stsp ondisk_path);
5010 4e68cba3 2019-11-23 stsp free(ondisk_path);
5011 4e68cba3 2019-11-23 stsp goto done;
5013 4e68cba3 2019-11-23 stsp free(ondisk_path);
5014 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5015 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5016 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5017 4e68cba3 2019-11-23 stsp goto done;
5022 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5023 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5026 031a5338 2019-03-26 stsp got_repo_close(repo);
5027 d00136be 2019-03-26 stsp if (worktree)
5028 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5029 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5030 1dd54920 2019-05-11 stsp free((char *)pe->path);
5031 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5032 2ec1f75b 2019-03-26 stsp free(cwd);
5033 2ec1f75b 2019-03-26 stsp return error;
5036 2ec1f75b 2019-03-26 stsp __dead static void
5037 648e4ef7 2019-07-09 stsp usage_remove(void)
5039 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5040 f2a9dc41 2019-12-13 tracey getprogname());
5044 2a06fe5f 2019-08-24 stsp static const struct got_error *
5045 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5046 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5048 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5050 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5051 2a06fe5f 2019-08-24 stsp return NULL;
5052 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5053 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5054 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5055 2a06fe5f 2019-08-24 stsp return NULL;
5058 2ec1f75b 2019-03-26 stsp static const struct got_error *
5059 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5061 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5062 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5063 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5064 17ed4618 2019-06-02 stsp char *cwd = NULL;
5065 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5066 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5067 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5069 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5071 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5072 2ec1f75b 2019-03-26 stsp switch (ch) {
5074 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5076 70e3e7f5 2019-12-13 tracey case 'k':
5077 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5079 f2a9dc41 2019-12-13 tracey case 'R':
5080 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5083 f2a9dc41 2019-12-13 tracey usage_remove();
5084 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5088 2ec1f75b 2019-03-26 stsp argc -= optind;
5089 2ec1f75b 2019-03-26 stsp argv += optind;
5091 43012d58 2019-07-14 stsp #ifndef PROFILE
5092 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5093 43012d58 2019-07-14 stsp NULL) == -1)
5094 43012d58 2019-07-14 stsp err(1, "pledge");
5096 17ed4618 2019-06-02 stsp if (argc < 1)
5097 648e4ef7 2019-07-09 stsp usage_remove();
5099 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5100 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5101 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5102 2ec1f75b 2019-03-26 stsp goto done;
5104 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5105 2ec1f75b 2019-03-26 stsp if (error)
5106 2ec1f75b 2019-03-26 stsp goto done;
5108 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5111 2ec1f75b 2019-03-26 stsp goto done;
5113 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5114 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5115 2ec1f75b 2019-03-26 stsp if (error)
5116 2ec1f75b 2019-03-26 stsp goto done;
5118 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5119 6d022e97 2019-08-04 stsp if (error)
5120 6d022e97 2019-08-04 stsp goto done;
5122 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5123 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5124 f2a9dc41 2019-12-13 tracey struct stat sb;
5125 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5126 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5127 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5128 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5129 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5130 f2a9dc41 2019-12-13 tracey goto done;
5132 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5133 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5134 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5135 f2a9dc41 2019-12-13 tracey continue;
5137 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5138 f2a9dc41 2019-12-13 tracey ondisk_path);
5139 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5140 f2a9dc41 2019-12-13 tracey goto done;
5142 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5143 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5144 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5145 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5146 f2a9dc41 2019-12-13 tracey goto done;
5151 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5152 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5155 a129376b 2019-03-28 stsp got_repo_close(repo);
5156 a129376b 2019-03-28 stsp if (worktree)
5157 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5158 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5159 17ed4618 2019-06-02 stsp free((char *)pe->path);
5160 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5161 a129376b 2019-03-28 stsp free(cwd);
5162 a129376b 2019-03-28 stsp return error;
5165 a129376b 2019-03-28 stsp __dead static void
5166 a129376b 2019-03-28 stsp usage_revert(void)
5168 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5169 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5173 1ee397ad 2019-07-12 stsp static const struct got_error *
5174 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5176 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5177 fb9704af 2020-01-27 stsp return NULL;
5179 a129376b 2019-03-28 stsp while (path[0] == '/')
5181 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5182 33aa809d 2019-08-08 stsp return NULL;
5185 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5186 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5187 33aa809d 2019-08-08 stsp const char *action;
5190 33aa809d 2019-08-08 stsp static const struct got_error *
5191 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5192 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5194 33aa809d 2019-08-08 stsp char *line = NULL;
5195 33aa809d 2019-08-08 stsp size_t linesize = 0;
5196 33aa809d 2019-08-08 stsp ssize_t linelen;
5198 33aa809d 2019-08-08 stsp switch (status) {
5199 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5200 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5202 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5203 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5205 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5206 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5207 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5208 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5209 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5210 33aa809d 2019-08-08 stsp printf("%s", line);
5211 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5212 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5213 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5214 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5215 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5218 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5221 33aa809d 2019-08-08 stsp return NULL;
5224 33aa809d 2019-08-08 stsp static const struct got_error *
5225 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5226 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5228 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5229 33aa809d 2019-08-08 stsp char *line = NULL;
5230 33aa809d 2019-08-08 stsp size_t linesize = 0;
5231 33aa809d 2019-08-08 stsp ssize_t linelen;
5232 33aa809d 2019-08-08 stsp int resp = ' ';
5233 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5235 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5237 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5239 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5240 33aa809d 2019-08-08 stsp a->action);
5242 33aa809d 2019-08-08 stsp return err;
5243 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5244 33aa809d 2019-08-08 stsp if (linelen == -1) {
5245 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5246 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5247 33aa809d 2019-08-08 stsp return NULL;
5249 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5251 33aa809d 2019-08-08 stsp *nl = '\0';
5252 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5253 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5254 33aa809d 2019-08-08 stsp printf("y\n");
5255 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5256 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5257 33aa809d 2019-08-08 stsp printf("n\n");
5258 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5259 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5260 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5261 33aa809d 2019-08-08 stsp printf("q\n");
5263 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5264 33aa809d 2019-08-08 stsp free(line);
5265 33aa809d 2019-08-08 stsp return NULL;
5268 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5269 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5270 33aa809d 2019-08-08 stsp a->action);
5272 33aa809d 2019-08-08 stsp return err;
5273 33aa809d 2019-08-08 stsp resp = getchar();
5274 33aa809d 2019-08-08 stsp if (resp == '\n')
5275 33aa809d 2019-08-08 stsp resp = getchar();
5276 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5277 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5278 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5279 33aa809d 2019-08-08 stsp resp = ' ';
5281 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5282 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5283 33aa809d 2019-08-08 stsp resp = ' ';
5287 33aa809d 2019-08-08 stsp if (resp == 'y')
5288 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5289 33aa809d 2019-08-08 stsp else if (resp == 'n')
5290 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5291 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5292 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5294 1ee397ad 2019-07-12 stsp return NULL;
5298 a129376b 2019-03-28 stsp static const struct got_error *
5299 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5301 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5302 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5303 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5304 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5305 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5306 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5307 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5308 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5309 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5310 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5312 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5314 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5315 a129376b 2019-03-28 stsp switch (ch) {
5317 33aa809d 2019-08-08 stsp pflag = 1;
5320 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5323 0f6d7415 2019-08-08 stsp can_recurse = 1;
5326 a129376b 2019-03-28 stsp usage_revert();
5327 a129376b 2019-03-28 stsp /* NOTREACHED */
5331 a129376b 2019-03-28 stsp argc -= optind;
5332 a129376b 2019-03-28 stsp argv += optind;
5334 43012d58 2019-07-14 stsp #ifndef PROFILE
5335 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5336 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5337 43012d58 2019-07-14 stsp err(1, "pledge");
5339 e20a8b6f 2019-06-04 stsp if (argc < 1)
5340 a129376b 2019-03-28 stsp usage_revert();
5341 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5342 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5344 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5345 a129376b 2019-03-28 stsp if (cwd == NULL) {
5346 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5347 a129376b 2019-03-28 stsp goto done;
5349 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5350 2ec1f75b 2019-03-26 stsp if (error)
5351 2ec1f75b 2019-03-26 stsp goto done;
5353 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5355 a129376b 2019-03-28 stsp if (error != NULL)
5356 a129376b 2019-03-28 stsp goto done;
5358 33aa809d 2019-08-08 stsp if (patch_script_path) {
5359 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5360 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5361 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5362 33aa809d 2019-08-08 stsp patch_script_path);
5363 33aa809d 2019-08-08 stsp goto done;
5366 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5367 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5368 a129376b 2019-03-28 stsp if (error)
5369 a129376b 2019-03-28 stsp goto done;
5371 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5372 6d022e97 2019-08-04 stsp if (error)
5373 6d022e97 2019-08-04 stsp goto done;
5375 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5376 0f6d7415 2019-08-08 stsp char *ondisk_path;
5377 0f6d7415 2019-08-08 stsp struct stat sb;
5378 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5379 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5380 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5381 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5382 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5383 0f6d7415 2019-08-08 stsp goto done;
5385 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5386 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5387 0f6d7415 2019-08-08 stsp free(ondisk_path);
5390 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5391 0f6d7415 2019-08-08 stsp ondisk_path);
5392 0f6d7415 2019-08-08 stsp free(ondisk_path);
5393 0f6d7415 2019-08-08 stsp goto done;
5395 0f6d7415 2019-08-08 stsp free(ondisk_path);
5396 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5397 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5398 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5399 0f6d7415 2019-08-08 stsp goto done;
5404 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5405 33aa809d 2019-08-08 stsp cpa.action = "revert";
5406 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5407 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5409 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5410 33aa809d 2019-08-08 stsp error == NULL)
5411 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5413 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5414 2ec1f75b 2019-03-26 stsp if (worktree)
5415 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5416 2ec1f75b 2019-03-26 stsp free(path);
5417 d00136be 2019-03-26 stsp free(cwd);
5418 6bad629b 2019-02-04 stsp return error;
5421 c4296144 2019-05-09 stsp __dead static void
5422 c4296144 2019-05-09 stsp usage_commit(void)
5424 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5425 5c1e53bc 2019-07-28 stsp getprogname());
5429 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5430 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5431 e2ba3d07 2019-05-13 stsp const char *editor;
5432 e0870e44 2019-05-13 stsp const char *worktree_path;
5433 76d98825 2019-06-03 stsp const char *branch_name;
5434 314a6357 2019-05-13 stsp const char *repo_path;
5435 e0870e44 2019-05-13 stsp char *logmsg_path;
5439 33ad4cbe 2019-05-12 jcs static const struct got_error *
5440 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5443 76d98825 2019-06-03 stsp char *initial_content = NULL;
5444 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5445 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5446 e0870e44 2019-05-13 stsp char *template = NULL;
5447 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5449 33ad4cbe 2019-05-12 jcs size_t len;
5451 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5452 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5453 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5454 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5455 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5456 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5457 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5458 33ad4cbe 2019-05-12 jcs return NULL;
5461 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5462 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5464 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5465 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5466 76d98825 2019-06-03 stsp a->branch_name) == -1)
5467 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5469 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5471 e0870e44 2019-05-13 stsp goto done;
5473 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5475 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5476 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5477 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5478 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5479 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5483 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5485 76d98825 2019-06-03 stsp free(initial_content);
5486 e0870e44 2019-05-13 stsp free(template);
5488 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5489 3ce1b845 2019-07-15 stsp if (err == NULL) {
5490 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5491 3ce1b845 2019-07-15 stsp if (err) {
5492 3ce1b845 2019-07-15 stsp free(*logmsg);
5493 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5496 33ad4cbe 2019-05-12 jcs return err;
5499 c4296144 2019-05-09 stsp static const struct got_error *
5500 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5502 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5503 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5504 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5505 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5506 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5507 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5508 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5509 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5510 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5511 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5513 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5514 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5516 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5517 c4296144 2019-05-09 stsp switch (ch) {
5519 c4296144 2019-05-09 stsp logmsg = optarg;
5522 c4296144 2019-05-09 stsp usage_commit();
5523 c4296144 2019-05-09 stsp /* NOTREACHED */
5527 c4296144 2019-05-09 stsp argc -= optind;
5528 c4296144 2019-05-09 stsp argv += optind;
5530 43012d58 2019-07-14 stsp #ifndef PROFILE
5531 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5532 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5533 43012d58 2019-07-14 stsp err(1, "pledge");
5535 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5536 c4296144 2019-05-09 stsp if (cwd == NULL) {
5537 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5538 c4296144 2019-05-09 stsp goto done;
5540 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5541 7d5807f4 2019-07-11 stsp if (error)
5542 7d5807f4 2019-07-11 stsp goto done;
5544 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5545 c4296144 2019-05-09 stsp if (error)
5546 a698f62e 2019-07-25 stsp goto done;
5547 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5548 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5549 c4296144 2019-05-09 stsp goto done;
5552 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5553 916f288c 2019-07-30 stsp worktree);
5554 5c1e53bc 2019-07-28 stsp if (error)
5555 5c1e53bc 2019-07-28 stsp goto done;
5557 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5558 c9956ddf 2019-09-08 stsp if (error)
5559 c9956ddf 2019-09-08 stsp goto done;
5560 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5561 c9956ddf 2019-09-08 stsp gitconfig_path);
5562 c4296144 2019-05-09 stsp if (error != NULL)
5563 c4296144 2019-05-09 stsp goto done;
5565 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5566 aba9c984 2019-09-08 stsp if (error)
5567 aba9c984 2019-09-08 stsp return error;
5570 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5571 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5573 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5574 314a6357 2019-05-13 stsp error = get_editor(&editor);
5576 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5577 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5578 c4296144 2019-05-09 stsp if (error)
5579 c4296144 2019-05-09 stsp goto done;
5581 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5582 087fb88c 2019-08-04 stsp if (error)
5583 087fb88c 2019-08-04 stsp goto done;
5585 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5586 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5587 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5588 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5589 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5590 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5591 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5592 916f288c 2019-07-30 stsp goto done;
5594 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5596 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5597 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5598 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5599 e0870e44 2019-05-13 stsp if (error) {
5600 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5601 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5602 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5603 c4296144 2019-05-09 stsp goto done;
5606 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5607 c4296144 2019-05-09 stsp if (error)
5608 c4296144 2019-05-09 stsp goto done;
5609 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5611 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5612 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5613 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5614 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5615 7266f21f 2019-10-21 stsp error == NULL)
5616 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5617 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5619 c4296144 2019-05-09 stsp got_repo_close(repo);
5620 c4296144 2019-05-09 stsp if (worktree)
5621 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5622 c4296144 2019-05-09 stsp free(cwd);
5623 c4296144 2019-05-09 stsp free(id_str);
5624 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5625 e2ba3d07 2019-05-13 stsp free(editor);
5626 aba9c984 2019-09-08 stsp free(author);
5627 234035bc 2019-06-01 stsp return error;
5630 234035bc 2019-06-01 stsp __dead static void
5631 234035bc 2019-06-01 stsp usage_cherrypick(void)
5633 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5637 234035bc 2019-06-01 stsp static const struct got_error *
5638 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5640 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5641 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5642 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5643 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5644 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5645 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5646 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5647 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5648 234035bc 2019-06-01 stsp int ch, did_something = 0;
5650 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5651 234035bc 2019-06-01 stsp switch (ch) {
5653 234035bc 2019-06-01 stsp usage_cherrypick();
5654 234035bc 2019-06-01 stsp /* NOTREACHED */
5658 234035bc 2019-06-01 stsp argc -= optind;
5659 234035bc 2019-06-01 stsp argv += optind;
5661 43012d58 2019-07-14 stsp #ifndef PROFILE
5662 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5664 43012d58 2019-07-14 stsp err(1, "pledge");
5666 234035bc 2019-06-01 stsp if (argc != 1)
5667 234035bc 2019-06-01 stsp usage_cherrypick();
5669 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5670 234035bc 2019-06-01 stsp if (cwd == NULL) {
5671 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5672 234035bc 2019-06-01 stsp goto done;
5674 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5675 234035bc 2019-06-01 stsp if (error)
5676 234035bc 2019-06-01 stsp goto done;
5678 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5680 234035bc 2019-06-01 stsp if (error != NULL)
5681 234035bc 2019-06-01 stsp goto done;
5683 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5684 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5685 234035bc 2019-06-01 stsp if (error)
5686 234035bc 2019-06-01 stsp goto done;
5688 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5689 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5690 234035bc 2019-06-01 stsp if (error != NULL) {
5691 234035bc 2019-06-01 stsp struct got_reference *ref;
5692 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5693 234035bc 2019-06-01 stsp goto done;
5694 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5695 234035bc 2019-06-01 stsp if (error != NULL)
5696 234035bc 2019-06-01 stsp goto done;
5697 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5698 234035bc 2019-06-01 stsp got_ref_close(ref);
5699 234035bc 2019-06-01 stsp if (error != NULL)
5700 234035bc 2019-06-01 stsp goto done;
5702 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5703 234035bc 2019-06-01 stsp if (error)
5704 234035bc 2019-06-01 stsp goto done;
5706 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5707 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5708 234035bc 2019-06-01 stsp if (error != NULL)
5709 234035bc 2019-06-01 stsp goto done;
5711 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5712 234035bc 2019-06-01 stsp if (error) {
5713 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5714 234035bc 2019-06-01 stsp goto done;
5715 234035bc 2019-06-01 stsp error = NULL;
5717 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5718 234035bc 2019-06-01 stsp goto done;
5721 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5722 234035bc 2019-06-01 stsp if (error)
5723 234035bc 2019-06-01 stsp goto done;
5724 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5725 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5726 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5728 234035bc 2019-06-01 stsp if (error != NULL)
5729 234035bc 2019-06-01 stsp goto done;
5731 234035bc 2019-06-01 stsp if (did_something)
5732 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5734 234035bc 2019-06-01 stsp if (commit)
5735 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5736 234035bc 2019-06-01 stsp free(commit_id_str);
5737 234035bc 2019-06-01 stsp if (head_ref)
5738 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5739 234035bc 2019-06-01 stsp if (worktree)
5740 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5742 234035bc 2019-06-01 stsp got_repo_close(repo);
5743 c4296144 2019-05-09 stsp return error;
5746 5ef14e63 2019-06-02 stsp __dead static void
5747 5ef14e63 2019-06-02 stsp usage_backout(void)
5749 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5753 5ef14e63 2019-06-02 stsp static const struct got_error *
5754 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5756 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5757 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5758 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5759 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5760 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5761 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5762 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5763 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5764 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5766 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5767 5ef14e63 2019-06-02 stsp switch (ch) {
5769 5ef14e63 2019-06-02 stsp usage_backout();
5770 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5774 5ef14e63 2019-06-02 stsp argc -= optind;
5775 5ef14e63 2019-06-02 stsp argv += optind;
5777 43012d58 2019-07-14 stsp #ifndef PROFILE
5778 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5780 43012d58 2019-07-14 stsp err(1, "pledge");
5782 5ef14e63 2019-06-02 stsp if (argc != 1)
5783 5ef14e63 2019-06-02 stsp usage_backout();
5785 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5786 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5787 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5788 5ef14e63 2019-06-02 stsp goto done;
5790 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5791 5ef14e63 2019-06-02 stsp if (error)
5792 5ef14e63 2019-06-02 stsp goto done;
5794 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5796 5ef14e63 2019-06-02 stsp if (error != NULL)
5797 5ef14e63 2019-06-02 stsp goto done;
5799 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5800 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5801 5ef14e63 2019-06-02 stsp if (error)
5802 5ef14e63 2019-06-02 stsp goto done;
5804 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5805 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5806 5ef14e63 2019-06-02 stsp if (error != NULL) {
5807 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5808 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5809 5ef14e63 2019-06-02 stsp goto done;
5810 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5811 5ef14e63 2019-06-02 stsp if (error != NULL)
5812 5ef14e63 2019-06-02 stsp goto done;
5813 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5814 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5815 5ef14e63 2019-06-02 stsp if (error != NULL)
5816 5ef14e63 2019-06-02 stsp goto done;
5818 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5819 5ef14e63 2019-06-02 stsp if (error)
5820 5ef14e63 2019-06-02 stsp goto done;
5822 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5823 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5824 5ef14e63 2019-06-02 stsp if (error != NULL)
5825 5ef14e63 2019-06-02 stsp goto done;
5827 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5828 5ef14e63 2019-06-02 stsp if (error)
5829 5ef14e63 2019-06-02 stsp goto done;
5831 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5832 5ef14e63 2019-06-02 stsp if (error)
5833 5ef14e63 2019-06-02 stsp goto done;
5834 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5835 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5836 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5837 5ef14e63 2019-06-02 stsp goto done;
5840 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5841 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5842 5ef14e63 2019-06-02 stsp if (error != NULL)
5843 5ef14e63 2019-06-02 stsp goto done;
5845 5ef14e63 2019-06-02 stsp if (did_something)
5846 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5848 5ef14e63 2019-06-02 stsp if (commit)
5849 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5850 5ef14e63 2019-06-02 stsp free(commit_id_str);
5851 5ef14e63 2019-06-02 stsp if (head_ref)
5852 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5853 818c7501 2019-07-11 stsp if (worktree)
5854 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5856 818c7501 2019-07-11 stsp got_repo_close(repo);
5857 818c7501 2019-07-11 stsp return error;
5860 818c7501 2019-07-11 stsp __dead static void
5861 818c7501 2019-07-11 stsp usage_rebase(void)
5863 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5864 af54c8f8 2019-07-11 stsp getprogname());
5869 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5872 0ebf8283 2019-07-24 stsp size_t len;
5874 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5875 0ebf8283 2019-07-24 stsp if (len > limit)
5876 0ebf8283 2019-07-24 stsp len = limit;
5877 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5878 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5880 0ebf8283 2019-07-24 stsp *nl = '\0';
5883 0ebf8283 2019-07-24 stsp static const struct got_error *
5884 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5886 5943eee2 2019-08-13 stsp const struct got_error *err;
5887 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5888 5943eee2 2019-08-13 stsp const char *s;
5890 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5892 5943eee2 2019-08-13 stsp return err;
5894 5943eee2 2019-08-13 stsp s = logmsg0;
5895 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5898 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5899 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5900 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5901 5943eee2 2019-08-13 stsp goto done;
5904 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5906 5943eee2 2019-08-13 stsp free(logmsg0);
5907 a0ea4fc0 2020-02-28 stsp return err;
5910 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5911 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5913 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5914 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5915 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5917 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5919 a0ea4fc0 2020-02-28 stsp return err;
5921 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5923 a0ea4fc0 2020-02-28 stsp goto done;
5925 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5927 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5929 a0ea4fc0 2020-02-28 stsp goto done;
5931 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5933 a0ea4fc0 2020-02-28 stsp free(id_str);
5934 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5935 a0ea4fc0 2020-02-28 stsp free(logmsg);
5936 5943eee2 2019-08-13 stsp return err;
5939 818c7501 2019-07-11 stsp static const struct got_error *
5940 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5941 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5943 818c7501 2019-07-11 stsp const struct got_error *err;
5944 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5946 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5948 818c7501 2019-07-11 stsp goto done;
5950 ff0d2220 2019-07-11 stsp if (new_id) {
5951 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5953 ff0d2220 2019-07-11 stsp goto done;
5956 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5957 ff0d2220 2019-07-11 stsp if (new_id_str)
5958 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5960 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5962 0ebf8283 2019-07-24 stsp goto done;
5964 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5965 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5967 818c7501 2019-07-11 stsp free(old_id_str);
5968 818c7501 2019-07-11 stsp free(new_id_str);
5969 272a1371 2020-02-28 stsp free(logmsg);
5970 818c7501 2019-07-11 stsp return err;
5973 1ee397ad 2019-07-12 stsp static const struct got_error *
5974 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5976 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5978 818c7501 2019-07-11 stsp while (path[0] == '/')
5980 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5982 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5983 1ee397ad 2019-07-12 stsp return NULL;
5984 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5985 818c7501 2019-07-11 stsp *rebase_status = status;
5986 1ee397ad 2019-07-12 stsp return NULL;
5989 818c7501 2019-07-11 stsp static const struct got_error *
5990 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5991 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5992 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5994 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5995 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5996 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5999 818c7501 2019-07-11 stsp static const struct got_error *
6000 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6001 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6002 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6003 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6005 ff0d2220 2019-07-11 stsp const struct got_error *error;
6006 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6007 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6009 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6010 ff0d2220 2019-07-11 stsp if (error)
6011 ff0d2220 2019-07-11 stsp return error;
6013 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6014 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6015 ff0d2220 2019-07-11 stsp if (error) {
6016 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6017 ff0d2220 2019-07-11 stsp goto done;
6018 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6020 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6021 ff0d2220 2019-07-11 stsp free(new_commit_id);
6024 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6025 ff0d2220 2019-07-11 stsp return error;
6028 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6029 64c6d990 2019-07-11 stsp const char *path_prefix;
6030 64c6d990 2019-07-11 stsp size_t len;
6031 8ca9bd68 2019-07-25 stsp int errcode;
6034 64c6d990 2019-07-11 stsp static const struct got_error *
6035 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6036 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6037 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6038 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6040 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6042 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6043 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6044 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6046 64c6d990 2019-07-11 stsp return NULL;
6049 64c6d990 2019-07-11 stsp static const struct got_error *
6050 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6051 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6052 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6054 64c6d990 2019-07-11 stsp const struct got_error *err;
6055 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6056 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6057 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6059 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6060 64c6d990 2019-07-11 stsp return NULL;
6062 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6064 64c6d990 2019-07-11 stsp goto done;
6066 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6068 64c6d990 2019-07-11 stsp goto done;
6070 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6071 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6073 64c6d990 2019-07-11 stsp goto done;
6075 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6076 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6078 64c6d990 2019-07-11 stsp goto done;
6080 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6081 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6082 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6083 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6084 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6085 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6086 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6088 64c6d990 2019-07-11 stsp if (tree1)
6089 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6090 64c6d990 2019-07-11 stsp if (tree2)
6091 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6092 64c6d990 2019-07-11 stsp if (commit)
6093 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6094 64c6d990 2019-07-11 stsp if (parent_commit)
6095 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6096 64c6d990 2019-07-11 stsp return err;
6099 ff0d2220 2019-07-11 stsp static const struct got_error *
6100 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6101 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6102 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6103 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6104 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6106 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6107 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6108 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6109 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6110 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6112 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6114 af61c510 2019-07-19 stsp return err;
6116 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6117 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6119 af61c510 2019-07-19 stsp goto done;
6120 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6121 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6122 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6123 af61c510 2019-07-19 stsp if (err) {
6124 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6125 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6126 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6127 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6128 af61c510 2019-07-19 stsp "been reached?!?");
6130 ee780d5c 2020-01-04 stsp goto done;
6132 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6133 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6135 af61c510 2019-07-19 stsp goto done;
6137 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6139 af61c510 2019-07-19 stsp goto done;
6140 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6141 af61c510 2019-07-19 stsp commit_id = parent_id;
6145 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6146 af61c510 2019-07-19 stsp return err;
6149 af61c510 2019-07-19 stsp static const struct got_error *
6150 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6152 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6153 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6154 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6155 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6156 818c7501 2019-07-11 stsp char *cwd = NULL;
6157 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6158 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6159 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6160 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6161 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6162 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6163 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6164 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6165 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6166 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6167 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6168 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6169 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6171 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6172 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6174 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6175 818c7501 2019-07-11 stsp switch (ch) {
6177 818c7501 2019-07-11 stsp abort_rebase = 1;
6180 818c7501 2019-07-11 stsp continue_rebase = 1;
6183 818c7501 2019-07-11 stsp usage_rebase();
6184 818c7501 2019-07-11 stsp /* NOTREACHED */
6188 818c7501 2019-07-11 stsp argc -= optind;
6189 818c7501 2019-07-11 stsp argv += optind;
6191 43012d58 2019-07-14 stsp #ifndef PROFILE
6192 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6193 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6194 43012d58 2019-07-14 stsp err(1, "pledge");
6196 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6197 818c7501 2019-07-11 stsp usage_rebase();
6198 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6199 818c7501 2019-07-11 stsp if (argc != 0)
6200 818c7501 2019-07-11 stsp usage_rebase();
6201 818c7501 2019-07-11 stsp } else if (argc != 1)
6202 818c7501 2019-07-11 stsp usage_rebase();
6204 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6205 818c7501 2019-07-11 stsp if (cwd == NULL) {
6206 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6207 818c7501 2019-07-11 stsp goto done;
6209 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6210 818c7501 2019-07-11 stsp if (error)
6211 818c7501 2019-07-11 stsp goto done;
6213 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6215 818c7501 2019-07-11 stsp if (error != NULL)
6216 818c7501 2019-07-11 stsp goto done;
6218 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6219 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6220 7ef62c4e 2020-02-24 stsp if (error)
6221 7ef62c4e 2020-02-24 stsp goto done;
6223 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6224 7ef62c4e 2020-02-24 stsp worktree);
6225 818c7501 2019-07-11 stsp if (error)
6226 7ef62c4e 2020-02-24 stsp goto done;
6227 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6228 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6229 818c7501 2019-07-11 stsp goto done;
6232 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6233 818c7501 2019-07-11 stsp if (error)
6234 818c7501 2019-07-11 stsp goto done;
6236 f6794adc 2019-07-23 stsp if (abort_rebase) {
6237 818c7501 2019-07-11 stsp int did_something;
6238 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6239 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6240 f6794adc 2019-07-23 stsp goto done;
6242 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6243 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6244 3e3a69f1 2019-07-25 stsp worktree, repo);
6245 818c7501 2019-07-11 stsp if (error)
6246 818c7501 2019-07-11 stsp goto done;
6247 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6248 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6249 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6250 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6251 818c7501 2019-07-11 stsp if (error)
6252 818c7501 2019-07-11 stsp goto done;
6253 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6254 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6257 818c7501 2019-07-11 stsp if (continue_rebase) {
6258 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6259 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6260 f6794adc 2019-07-23 stsp goto done;
6262 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6263 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6264 3e3a69f1 2019-07-25 stsp worktree, repo);
6265 818c7501 2019-07-11 stsp if (error)
6266 818c7501 2019-07-11 stsp goto done;
6268 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6269 01757395 2019-07-12 stsp resume_commit_id, repo);
6270 818c7501 2019-07-11 stsp if (error)
6271 818c7501 2019-07-11 stsp goto done;
6273 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6274 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6275 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6276 818c7501 2019-07-11 stsp goto done;
6279 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6280 818c7501 2019-07-11 stsp if (error != NULL)
6281 818c7501 2019-07-11 stsp goto done;
6284 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6285 ff0d2220 2019-07-11 stsp if (error)
6286 ff0d2220 2019-07-11 stsp goto done;
6288 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6289 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6291 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6292 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6293 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6294 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6295 818c7501 2019-07-11 stsp if (error)
6296 818c7501 2019-07-11 stsp goto done;
6297 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6298 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6299 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6300 818c7501 2019-07-11 stsp "with work tree's branch");
6301 818c7501 2019-07-11 stsp goto done;
6304 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6305 a51a74b3 2019-07-27 stsp if (error) {
6306 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6307 a51a74b3 2019-07-27 stsp goto done;
6308 a51a74b3 2019-07-27 stsp error = NULL;
6310 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6311 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6312 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6313 a51a74b3 2019-07-27 stsp goto done;
6315 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6316 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6317 818c7501 2019-07-11 stsp if (error)
6318 818c7501 2019-07-11 stsp goto done;
6321 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6322 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6323 818c7501 2019-07-11 stsp if (error)
6324 818c7501 2019-07-11 stsp goto done;
6326 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6327 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6328 fc66b545 2019-08-12 stsp if (pid == NULL) {
6329 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6330 fc66b545 2019-08-12 stsp int did_something;
6331 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6332 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6333 fc66b545 2019-08-12 stsp &did_something);
6334 fc66b545 2019-08-12 stsp if (error)
6335 fc66b545 2019-08-12 stsp goto done;
6336 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6337 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6339 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6340 fc66b545 2019-08-12 stsp goto done;
6342 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6343 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6344 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6345 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6346 818c7501 2019-07-11 stsp commit = NULL;
6347 818c7501 2019-07-11 stsp if (error)
6348 818c7501 2019-07-11 stsp goto done;
6350 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6351 38b0338b 2019-11-29 stsp if (continue_rebase) {
6352 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6353 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6354 38b0338b 2019-11-29 stsp goto done;
6356 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6357 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6358 38b0338b 2019-11-29 stsp char *id_str;
6359 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6360 38b0338b 2019-11-29 stsp new_base_branch);
6361 38b0338b 2019-11-29 stsp if (error)
6362 38b0338b 2019-11-29 stsp goto done;
6363 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6364 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6365 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6366 38b0338b 2019-11-29 stsp free(id_str);
6367 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6368 38b0338b 2019-11-29 stsp new_head_commit_id);
6369 38b0338b 2019-11-29 stsp if (error)
6370 38b0338b 2019-11-29 stsp goto done;
6374 818c7501 2019-07-11 stsp pid = NULL;
6375 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6376 818c7501 2019-07-11 stsp commit_id = qid->id;
6377 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6378 818c7501 2019-07-11 stsp pid = qid;
6380 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6381 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6382 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6383 818c7501 2019-07-11 stsp if (error)
6384 818c7501 2019-07-11 stsp goto done;
6386 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6387 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6388 a0ea4fc0 2020-02-28 stsp if (error)
6389 a0ea4fc0 2020-02-28 stsp goto done;
6390 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6394 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6395 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6396 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6397 818c7501 2019-07-11 stsp if (error)
6398 818c7501 2019-07-11 stsp goto done;
6401 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6402 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6403 818c7501 2019-07-11 stsp if (error)
6404 818c7501 2019-07-11 stsp goto done;
6405 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6406 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6408 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6409 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6411 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6412 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6413 818c7501 2019-07-11 stsp free(resume_commit_id);
6414 818c7501 2019-07-11 stsp free(yca_id);
6415 818c7501 2019-07-11 stsp if (commit)
6416 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6417 818c7501 2019-07-11 stsp if (branch)
6418 818c7501 2019-07-11 stsp got_ref_close(branch);
6419 818c7501 2019-07-11 stsp if (new_base_branch)
6420 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6421 818c7501 2019-07-11 stsp if (tmp_branch)
6422 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6423 5ef14e63 2019-06-02 stsp if (worktree)
6424 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6426 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6427 5ef14e63 2019-06-02 stsp return error;
6430 0ebf8283 2019-07-24 stsp __dead static void
6431 0ebf8283 2019-07-24 stsp usage_histedit(void)
6433 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6434 0ebf8283 2019-07-24 stsp getprogname());
6438 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6439 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6440 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6441 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6442 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6444 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6445 0ebf8283 2019-07-24 stsp unsigned char code;
6446 0ebf8283 2019-07-24 stsp const char *name;
6447 0ebf8283 2019-07-24 stsp const char *desc;
6448 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6449 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6450 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6451 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6452 82997472 2020-01-29 stsp "be used" },
6453 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6454 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6455 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6458 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6459 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6460 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6461 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6462 0ebf8283 2019-07-24 stsp char *logmsg;
6464 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6466 0ebf8283 2019-07-24 stsp static const struct got_error *
6467 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6468 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6470 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6471 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6472 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6475 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6477 0ebf8283 2019-07-24 stsp goto done;
6479 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6481 0ebf8283 2019-07-24 stsp goto done;
6483 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6485 0ebf8283 2019-07-24 stsp goto done;
6487 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6488 0ebf8283 2019-07-24 stsp if (n < 0)
6489 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6491 0ebf8283 2019-07-24 stsp if (commit)
6492 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6493 0ebf8283 2019-07-24 stsp free(id_str);
6494 0ebf8283 2019-07-24 stsp free(logmsg);
6495 0ebf8283 2019-07-24 stsp return err;
6498 0ebf8283 2019-07-24 stsp static const struct got_error *
6499 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6500 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6502 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6503 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6505 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6506 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6508 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6509 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6513 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6514 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6515 083957f4 2020-02-24 stsp if (n < 0) {
6516 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6522 0ebf8283 2019-07-24 stsp return err;
6525 0ebf8283 2019-07-24 stsp static const struct got_error *
6526 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6527 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6529 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6531 514f2ffe 2020-01-29 stsp char *id_str;
6532 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6534 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6535 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6537 514f2ffe 2020-01-29 stsp return err;
6539 514f2ffe 2020-01-29 stsp n = fprintf(f,
6540 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6541 514f2ffe 2020-01-29 stsp "# commit %s\n"
6542 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6543 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6544 514f2ffe 2020-01-29 stsp if (n < 0) {
6545 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6546 514f2ffe 2020-01-29 stsp goto done;
6549 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6550 514f2ffe 2020-01-29 stsp if (n < 0) {
6551 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6552 514f2ffe 2020-01-29 stsp goto done;
6555 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6556 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6557 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6558 0ebf8283 2019-07-24 stsp cmd->desc);
6559 0ebf8283 2019-07-24 stsp if (n < 0) {
6560 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6565 514f2ffe 2020-01-29 stsp free(id_str);
6566 0ebf8283 2019-07-24 stsp return err;
6569 0ebf8283 2019-07-24 stsp static const struct got_error *
6570 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6572 0ebf8283 2019-07-24 stsp static char msg[42];
6575 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6577 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6578 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6580 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6583 0ebf8283 2019-07-24 stsp static const struct got_error *
6584 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6585 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6587 0ebf8283 2019-07-24 stsp const struct got_error *err;
6588 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6589 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6591 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6593 0ebf8283 2019-07-24 stsp return err;
6595 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6597 0ebf8283 2019-07-24 stsp goto done;
6599 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6601 5943eee2 2019-08-13 stsp goto done;
6602 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6603 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6604 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6605 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6608 0ebf8283 2019-07-24 stsp if (folded_commit)
6609 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6610 0ebf8283 2019-07-24 stsp free(id_str);
6611 5943eee2 2019-08-13 stsp free(folded_logmsg);
6612 0ebf8283 2019-07-24 stsp return err;
6615 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6616 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6618 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6620 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6621 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6622 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6623 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6624 3f9de99f 2019-07-24 stsp folded = prev;
6625 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6628 0ebf8283 2019-07-24 stsp return folded;
6631 0ebf8283 2019-07-24 stsp static const struct got_error *
6632 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6633 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6635 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6636 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6637 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6638 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6640 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6642 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6644 0ebf8283 2019-07-24 stsp return err;
6646 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6647 0ebf8283 2019-07-24 stsp if (folded) {
6648 0ebf8283 2019-07-24 stsp while (folded != hle) {
6649 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6650 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6653 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6654 0ebf8283 2019-07-24 stsp logmsg, repo);
6656 0ebf8283 2019-07-24 stsp goto done;
6657 0ebf8283 2019-07-24 stsp free(logmsg);
6658 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6659 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6663 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6665 0ebf8283 2019-07-24 stsp goto done;
6666 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6668 5943eee2 2019-08-13 stsp goto done;
6669 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6670 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6671 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6672 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6673 0ebf8283 2019-07-24 stsp goto done;
6675 0ebf8283 2019-07-24 stsp free(logmsg);
6676 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6678 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6680 0ebf8283 2019-07-24 stsp goto done;
6682 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6683 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6685 0ebf8283 2019-07-24 stsp goto done;
6687 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6688 0ebf8283 2019-07-24 stsp close(fd);
6690 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6692 0ebf8283 2019-07-24 stsp goto done;
6694 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6695 0ebf8283 2019-07-24 stsp if (err) {
6696 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6697 0ebf8283 2019-07-24 stsp goto done;
6698 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6701 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6702 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6703 0ebf8283 2019-07-24 stsp free(logmsg_path);
6704 0ebf8283 2019-07-24 stsp free(logmsg);
6705 5943eee2 2019-08-13 stsp free(orig_logmsg);
6706 0ebf8283 2019-07-24 stsp free(editor);
6707 0ebf8283 2019-07-24 stsp if (commit)
6708 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6709 0ebf8283 2019-07-24 stsp return err;
6712 0ebf8283 2019-07-24 stsp static const struct got_error *
6713 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6714 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6716 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6717 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6718 0ebf8283 2019-07-24 stsp size_t size;
6719 0ebf8283 2019-07-24 stsp ssize_t len;
6720 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6721 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6722 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6723 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6725 0ebf8283 2019-07-24 stsp for (;;) {
6726 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6727 0ebf8283 2019-07-24 stsp if (len == -1) {
6728 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6729 0ebf8283 2019-07-24 stsp if (feof(f))
6731 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6732 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6737 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6739 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6740 0ebf8283 2019-07-24 stsp free(line);
6741 0ebf8283 2019-07-24 stsp line = NULL;
6744 0ebf8283 2019-07-24 stsp cmd = NULL;
6745 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6746 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6747 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6748 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6749 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6752 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6757 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6758 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6761 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6763 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6764 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6765 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6768 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6769 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6773 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6774 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6775 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6779 0ebf8283 2019-07-24 stsp free(line);
6780 0ebf8283 2019-07-24 stsp line = NULL;
6784 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6786 0ebf8283 2019-07-24 stsp *end = '\0';
6788 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6789 0ebf8283 2019-07-24 stsp if (err) {
6790 0ebf8283 2019-07-24 stsp /* override error code */
6791 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6795 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6796 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6797 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6800 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6801 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6802 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6803 0ebf8283 2019-07-24 stsp commit_id = NULL;
6804 0ebf8283 2019-07-24 stsp free(line);
6805 0ebf8283 2019-07-24 stsp line = NULL;
6806 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6809 0ebf8283 2019-07-24 stsp free(line);
6810 0ebf8283 2019-07-24 stsp free(commit_id);
6811 0ebf8283 2019-07-24 stsp return err;
6814 0ebf8283 2019-07-24 stsp static const struct got_error *
6815 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6816 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6818 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6819 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6820 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6821 5b87815e 2020-03-05 stsp static char msg[92];
6822 bfce7f83 2019-07-27 stsp char *id_str;
6824 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6825 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6826 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6827 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6828 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6830 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6831 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6832 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6833 5b87815e 2020-03-05 stsp if (hle == hle2)
6835 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6836 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6838 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6840 5b87815e 2020-03-05 stsp return err;
6841 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6842 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6843 5b87815e 2020-03-05 stsp free(id_str);
6844 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6848 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6849 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6850 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6853 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6854 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6856 bfce7f83 2019-07-27 stsp return err;
6857 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6858 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6859 bfce7f83 2019-07-27 stsp free(id_str);
6860 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6864 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6865 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6866 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6867 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6869 bfce7f83 2019-07-27 stsp return NULL;
6872 bfce7f83 2019-07-27 stsp static const struct got_error *
6873 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6874 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6875 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6877 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6878 0ebf8283 2019-07-24 stsp char *editor;
6879 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6881 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6883 0ebf8283 2019-07-24 stsp return err;
6885 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6886 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6887 0ebf8283 2019-07-24 stsp goto done;
6890 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6891 0ebf8283 2019-07-24 stsp if (f == NULL) {
6892 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6893 0ebf8283 2019-07-24 stsp goto done;
6895 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6897 bfce7f83 2019-07-27 stsp goto done;
6899 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6901 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6902 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6903 0ebf8283 2019-07-24 stsp free(editor);
6904 0ebf8283 2019-07-24 stsp return err;
6907 0ebf8283 2019-07-24 stsp static const struct got_error *
6908 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6909 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6910 514f2ffe 2020-01-29 stsp struct got_repository *);
6912 0ebf8283 2019-07-24 stsp static const struct got_error *
6913 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6914 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6915 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6917 0ebf8283 2019-07-24 stsp const struct got_error *err;
6918 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6919 0ebf8283 2019-07-24 stsp char *path = NULL;
6921 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6923 0ebf8283 2019-07-24 stsp return err;
6925 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6927 0ebf8283 2019-07-24 stsp goto done;
6929 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6931 0ebf8283 2019-07-24 stsp goto done;
6933 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6934 083957f4 2020-02-24 stsp rewind(f);
6935 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6937 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6938 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6939 0ebf8283 2019-07-24 stsp goto done;
6942 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6943 083957f4 2020-02-24 stsp if (err) {
6944 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6945 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6946 083957f4 2020-02-24 stsp goto done;
6947 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6948 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6952 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6953 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6954 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6955 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6956 0ebf8283 2019-07-24 stsp free(path);
6957 0ebf8283 2019-07-24 stsp return err;
6960 0ebf8283 2019-07-24 stsp static const struct got_error *
6961 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6962 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6964 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6965 0ebf8283 2019-07-24 stsp char *path = NULL;
6966 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6967 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6968 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6970 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6972 0ebf8283 2019-07-24 stsp return err;
6974 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6975 0ebf8283 2019-07-24 stsp if (f == NULL) {
6976 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6977 0ebf8283 2019-07-24 stsp goto done;
6979 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6980 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6985 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6986 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6987 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6988 0ebf8283 2019-07-24 stsp if (n < 0) {
6989 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6995 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6996 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6997 0ebf8283 2019-07-24 stsp free(path);
6998 0ebf8283 2019-07-24 stsp if (commit)
6999 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7000 0ebf8283 2019-07-24 stsp return err;
7004 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7006 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7008 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7009 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7010 bfce7f83 2019-07-27 stsp free(hle);
7014 0ebf8283 2019-07-24 stsp static const struct got_error *
7015 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7016 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7018 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7019 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7021 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7022 0ebf8283 2019-07-24 stsp if (f == NULL) {
7023 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7024 0ebf8283 2019-07-24 stsp goto done;
7027 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7029 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7030 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7031 0ebf8283 2019-07-24 stsp return err;
7034 0ebf8283 2019-07-24 stsp static const struct got_error *
7035 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7036 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7037 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7039 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7040 0ebf8283 2019-07-24 stsp int resp = ' ';
7042 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7043 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7044 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7045 0ebf8283 2019-07-24 stsp resp = getchar();
7046 a61a4414 2019-08-07 stsp if (resp == '\n')
7047 a61a4414 2019-08-07 stsp resp = getchar();
7048 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7049 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7050 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7052 426ebf2e 2019-07-25 stsp if (err) {
7053 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7054 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7056 bfce7f83 2019-07-27 stsp prev_err = err;
7057 426ebf2e 2019-07-25 stsp resp = ' ';
7061 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7062 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7063 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7064 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7065 426ebf2e 2019-07-25 stsp if (err) {
7066 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7067 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7069 bfce7f83 2019-07-27 stsp prev_err = err;
7070 426ebf2e 2019-07-25 stsp resp = ' ';
7074 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7075 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7078 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7081 0ebf8283 2019-07-24 stsp return err;
7084 0ebf8283 2019-07-24 stsp static const struct got_error *
7085 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7086 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7087 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7089 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7090 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7091 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7092 3e3a69f1 2019-07-25 stsp branch, repo);
7095 0ebf8283 2019-07-24 stsp static const struct got_error *
7096 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7097 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7099 0ebf8283 2019-07-24 stsp const struct got_error *err;
7100 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7102 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7104 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp if (new_id) {
7107 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7109 0ebf8283 2019-07-24 stsp goto done;
7112 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7113 0ebf8283 2019-07-24 stsp if (new_id_str)
7114 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7116 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7117 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7118 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7119 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7120 0ebf8283 2019-07-24 stsp goto done;
7122 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7124 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7126 0ebf8283 2019-07-24 stsp goto done;
7129 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7130 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7131 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7132 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7133 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7135 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7136 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7137 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7144 0ebf8283 2019-07-24 stsp free(old_id_str);
7145 0ebf8283 2019-07-24 stsp free(new_id_str);
7146 0ebf8283 2019-07-24 stsp return err;
7149 0ebf8283 2019-07-24 stsp static const struct got_error *
7150 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7151 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7152 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7153 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7155 0ebf8283 2019-07-24 stsp const struct got_error *err;
7156 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7157 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7159 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7160 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7161 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7163 0ebf8283 2019-07-24 stsp return err;
7166 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7168 0ebf8283 2019-07-24 stsp return err;
7170 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7171 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7172 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7173 0ebf8283 2019-07-24 stsp if (err) {
7174 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7175 0ebf8283 2019-07-24 stsp goto done;
7176 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7178 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7179 0ebf8283 2019-07-24 stsp free(new_commit_id);
7182 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7183 0ebf8283 2019-07-24 stsp return err;
7186 0ebf8283 2019-07-24 stsp static const struct got_error *
7187 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7188 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7190 0ebf8283 2019-07-24 stsp const struct got_error *error;
7191 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7193 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7195 0ebf8283 2019-07-24 stsp if (error)
7196 0ebf8283 2019-07-24 stsp return error;
7198 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7199 0ebf8283 2019-07-24 stsp if (error)
7200 0ebf8283 2019-07-24 stsp return error;
7202 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7203 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7204 0ebf8283 2019-07-24 stsp return error;
7207 ab20a43a 2020-01-29 stsp static const struct got_error *
7208 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7209 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7210 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7211 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7213 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7215 ab20a43a 2020-01-29 stsp switch (status) {
7216 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7217 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7218 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7219 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7220 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7221 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7226 ab20a43a 2020-01-29 stsp switch (staged_status) {
7227 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7228 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7229 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7230 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7231 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7236 ab20a43a 2020-01-29 stsp return NULL;
7239 0ebf8283 2019-07-24 stsp static const struct got_error *
7240 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7242 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7243 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7244 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7245 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7246 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7247 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7248 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7249 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7250 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7251 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7252 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7253 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7254 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7255 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7256 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7257 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7258 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7259 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7260 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7261 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7262 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7263 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7265 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7266 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7267 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7269 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7270 0ebf8283 2019-07-24 stsp switch (ch) {
7272 0ebf8283 2019-07-24 stsp abort_edit = 1;
7275 0ebf8283 2019-07-24 stsp continue_edit = 1;
7278 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7281 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7284 0ebf8283 2019-07-24 stsp usage_histedit();
7285 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7289 0ebf8283 2019-07-24 stsp argc -= optind;
7290 0ebf8283 2019-07-24 stsp argv += optind;
7292 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7293 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7294 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7295 0ebf8283 2019-07-24 stsp err(1, "pledge");
7297 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7298 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7299 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7300 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7301 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7302 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7303 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7304 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7305 0ebf8283 2019-07-24 stsp if (argc != 0)
7306 0ebf8283 2019-07-24 stsp usage_histedit();
7309 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7310 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7311 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7312 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7313 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7314 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7317 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7318 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7319 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7320 0ebf8283 2019-07-24 stsp goto done;
7322 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7323 0ebf8283 2019-07-24 stsp if (error)
7324 0ebf8283 2019-07-24 stsp goto done;
7326 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7328 0ebf8283 2019-07-24 stsp if (error != NULL)
7329 0ebf8283 2019-07-24 stsp goto done;
7331 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7332 0ebf8283 2019-07-24 stsp if (error)
7333 0ebf8283 2019-07-24 stsp goto done;
7334 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7335 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7336 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7340 0ebf8283 2019-07-24 stsp if (error)
7341 0ebf8283 2019-07-24 stsp goto done;
7343 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7344 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7345 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7346 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7347 083957f4 2020-02-24 stsp "before the -m option can be used");
7348 083957f4 2020-02-24 stsp goto done;
7351 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7352 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7353 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7354 3e3a69f1 2019-07-25 stsp worktree, repo);
7355 0ebf8283 2019-07-24 stsp if (error)
7356 0ebf8283 2019-07-24 stsp goto done;
7357 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7358 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7359 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7360 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7361 0ebf8283 2019-07-24 stsp if (error)
7362 0ebf8283 2019-07-24 stsp goto done;
7363 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7364 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7365 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7366 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7367 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7368 0ebf8283 2019-07-24 stsp goto done;
7371 0ebf8283 2019-07-24 stsp if (continue_edit) {
7372 0ebf8283 2019-07-24 stsp char *path;
7374 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7375 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7376 0ebf8283 2019-07-24 stsp goto done;
7379 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7380 0ebf8283 2019-07-24 stsp if (error)
7381 0ebf8283 2019-07-24 stsp goto done;
7383 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7384 0ebf8283 2019-07-24 stsp free(path);
7385 0ebf8283 2019-07-24 stsp if (error)
7386 0ebf8283 2019-07-24 stsp goto done;
7388 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7389 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7390 3e3a69f1 2019-07-25 stsp worktree, repo);
7391 0ebf8283 2019-07-24 stsp if (error)
7392 0ebf8283 2019-07-24 stsp goto done;
7394 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7395 0ebf8283 2019-07-24 stsp if (error)
7396 0ebf8283 2019-07-24 stsp goto done;
7398 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7399 0ebf8283 2019-07-24 stsp head_commit_id);
7400 0ebf8283 2019-07-24 stsp if (error)
7401 0ebf8283 2019-07-24 stsp goto done;
7402 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7403 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7404 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7405 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7406 3aac7cf7 2019-07-25 stsp goto done;
7408 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7409 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7410 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7411 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7412 0ebf8283 2019-07-24 stsp commit = NULL;
7413 0ebf8283 2019-07-24 stsp if (error)
7414 0ebf8283 2019-07-24 stsp goto done;
7416 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7417 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7418 0ebf8283 2019-07-24 stsp goto done;
7421 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7422 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7423 0ebf8283 2019-07-24 stsp if (error != NULL)
7424 0ebf8283 2019-07-24 stsp goto done;
7426 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7427 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7428 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7429 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7430 c7d20a3f 2019-07-30 stsp goto done;
7433 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7434 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7435 bfce7f83 2019-07-27 stsp branch = NULL;
7436 0ebf8283 2019-07-24 stsp if (error)
7437 0ebf8283 2019-07-24 stsp goto done;
7439 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7440 0ebf8283 2019-07-24 stsp head_commit_id);
7441 0ebf8283 2019-07-24 stsp if (error)
7442 0ebf8283 2019-07-24 stsp goto done;
7443 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7444 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7445 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7446 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7447 3aac7cf7 2019-07-25 stsp goto done;
7449 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7450 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7451 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7452 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7453 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7454 0ebf8283 2019-07-24 stsp commit = NULL;
7455 0ebf8283 2019-07-24 stsp if (error)
7456 0ebf8283 2019-07-24 stsp goto done;
7458 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7459 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7460 c5996fff 2020-01-29 stsp goto done;
7463 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7464 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7465 bfce7f83 2019-07-27 stsp if (error)
7466 bfce7f83 2019-07-27 stsp goto done;
7468 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7469 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7470 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7471 6ba2bea2 2019-07-27 stsp if (error) {
7472 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7473 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7474 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7475 0ebf8283 2019-07-24 stsp goto done;
7478 514f2ffe 2020-01-29 stsp const char *branch_name;
7479 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7480 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7481 514f2ffe 2020-01-29 stsp branch_name += 11;
7482 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7483 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7484 6ba2bea2 2019-07-27 stsp if (error) {
7485 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7486 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7487 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7488 0ebf8283 2019-07-24 stsp goto done;
7493 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7495 6ba2bea2 2019-07-27 stsp if (error) {
7496 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7497 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7498 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7499 0ebf8283 2019-07-24 stsp goto done;
7504 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7505 4ec14e60 2019-08-28 hiltjo if (error)
7506 0ebf8283 2019-07-24 stsp goto done;
7508 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7509 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7510 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7511 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7514 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7515 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7516 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7517 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7519 ab20a43a 2020-01-29 stsp if (error)
7520 ab20a43a 2020-01-29 stsp goto done;
7522 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7523 ab20a43a 2020-01-29 stsp int have_changes = 0;
7525 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7526 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7527 ab20a43a 2020-01-29 stsp if (error)
7528 ab20a43a 2020-01-29 stsp goto done;
7529 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7530 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7531 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7532 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7533 ab20a43a 2020-01-29 stsp if (error) {
7534 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7535 ab20a43a 2020-01-29 stsp goto done;
7536 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7537 ab20a43a 2020-01-29 stsp goto done;
7539 ab20a43a 2020-01-29 stsp if (have_changes) {
7540 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7541 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7542 ab20a43a 2020-01-29 stsp if (error)
7543 ab20a43a 2020-01-29 stsp goto done;
7545 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7546 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7547 ab20a43a 2020-01-29 stsp if (error)
7548 ab20a43a 2020-01-29 stsp goto done;
7549 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7550 ab20a43a 2020-01-29 stsp hle, NULL);
7551 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7552 ab20a43a 2020-01-29 stsp commit = NULL;
7553 ab20a43a 2020-01-29 stsp if (error)
7554 ab20a43a 2020-01-29 stsp goto done;
7560 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7561 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7562 0ebf8283 2019-07-24 stsp if (error)
7563 0ebf8283 2019-07-24 stsp goto done;
7567 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7568 0ebf8283 2019-07-24 stsp hle->commit_id);
7569 0ebf8283 2019-07-24 stsp if (error)
7570 0ebf8283 2019-07-24 stsp goto done;
7571 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7572 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7574 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7575 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7576 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7577 0ebf8283 2019-07-24 stsp if (error)
7578 0ebf8283 2019-07-24 stsp goto done;
7579 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7580 0ebf8283 2019-07-24 stsp commit = NULL;
7582 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7583 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7585 a0ea4fc0 2020-02-28 stsp if (error)
7586 a0ea4fc0 2020-02-28 stsp goto done;
7587 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7591 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7592 0ebf8283 2019-07-24 stsp char *id_str;
7593 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7594 0ebf8283 2019-07-24 stsp if (error)
7595 0ebf8283 2019-07-24 stsp goto done;
7596 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7598 0ebf8283 2019-07-24 stsp free(id_str);
7599 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7600 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7601 3e3a69f1 2019-07-25 stsp fileindex);
7602 0ebf8283 2019-07-24 stsp goto done;
7605 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7606 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7607 0ebf8283 2019-07-24 stsp if (error)
7608 0ebf8283 2019-07-24 stsp goto done;
7612 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7613 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7614 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7615 0ebf8283 2019-07-24 stsp if (error)
7616 0ebf8283 2019-07-24 stsp goto done;
7619 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7620 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7621 0ebf8283 2019-07-24 stsp if (error)
7622 0ebf8283 2019-07-24 stsp goto done;
7623 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7624 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7626 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7627 3e3a69f1 2019-07-25 stsp branch, repo);
7629 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7630 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7631 0ebf8283 2019-07-24 stsp free(head_commit_id);
7632 0ebf8283 2019-07-24 stsp free(base_commit_id);
7633 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7634 0ebf8283 2019-07-24 stsp if (commit)
7635 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7636 0ebf8283 2019-07-24 stsp if (branch)
7637 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7638 0ebf8283 2019-07-24 stsp if (tmp_branch)
7639 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7640 0ebf8283 2019-07-24 stsp if (worktree)
7641 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7643 2822a352 2019-10-15 stsp got_repo_close(repo);
7644 2822a352 2019-10-15 stsp return error;
7647 2822a352 2019-10-15 stsp __dead static void
7648 2822a352 2019-10-15 stsp usage_integrate(void)
7650 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7654 2822a352 2019-10-15 stsp static const struct got_error *
7655 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7657 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7658 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7659 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7660 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7661 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7662 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7663 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7664 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7665 2822a352 2019-10-15 stsp int ch, did_something = 0;
7667 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7668 2822a352 2019-10-15 stsp switch (ch) {
7670 2822a352 2019-10-15 stsp usage_integrate();
7671 2822a352 2019-10-15 stsp /* NOTREACHED */
7675 2822a352 2019-10-15 stsp argc -= optind;
7676 2822a352 2019-10-15 stsp argv += optind;
7678 2822a352 2019-10-15 stsp if (argc != 1)
7679 2822a352 2019-10-15 stsp usage_integrate();
7680 2822a352 2019-10-15 stsp branch_arg = argv[0];
7682 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7683 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7684 2822a352 2019-10-15 stsp err(1, "pledge");
7686 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7687 2822a352 2019-10-15 stsp if (cwd == NULL) {
7688 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7689 2822a352 2019-10-15 stsp goto done;
7692 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7693 2822a352 2019-10-15 stsp if (error)
7694 2822a352 2019-10-15 stsp goto done;
7696 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7697 2822a352 2019-10-15 stsp if (error)
7698 2822a352 2019-10-15 stsp goto done;
7700 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7702 2822a352 2019-10-15 stsp if (error != NULL)
7703 2822a352 2019-10-15 stsp goto done;
7705 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7706 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7707 2822a352 2019-10-15 stsp if (error)
7708 2822a352 2019-10-15 stsp goto done;
7710 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7711 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7712 2822a352 2019-10-15 stsp goto done;
7715 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7716 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7717 2822a352 2019-10-15 stsp if (error)
7718 2822a352 2019-10-15 stsp goto done;
7720 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7721 2822a352 2019-10-15 stsp if (refname == NULL) {
7722 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7723 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7724 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7725 2822a352 2019-10-15 stsp goto done;
7727 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7728 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7729 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7730 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7731 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7732 2822a352 2019-10-15 stsp goto done;
7735 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7736 2822a352 2019-10-15 stsp if (error)
7737 2822a352 2019-10-15 stsp goto done;
7739 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7740 2822a352 2019-10-15 stsp if (error)
7741 2822a352 2019-10-15 stsp goto done;
7743 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7744 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7745 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7746 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7747 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7748 2822a352 2019-10-15 stsp goto done;
7751 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7752 2822a352 2019-10-15 stsp if (error) {
7753 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7754 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7755 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7756 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7757 2822a352 2019-10-15 stsp goto done;
7760 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7761 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7762 2822a352 2019-10-15 stsp check_cancelled, NULL);
7763 2822a352 2019-10-15 stsp if (error)
7764 2822a352 2019-10-15 stsp goto done;
7766 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7769 715dc77e 2019-08-03 stsp got_repo_close(repo);
7770 2822a352 2019-10-15 stsp if (worktree)
7771 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7772 2822a352 2019-10-15 stsp free(cwd);
7773 2822a352 2019-10-15 stsp free(base_commit_id);
7774 2822a352 2019-10-15 stsp free(commit_id);
7775 2822a352 2019-10-15 stsp free(refname);
7776 2822a352 2019-10-15 stsp free(base_refname);
7777 715dc77e 2019-08-03 stsp return error;
7780 715dc77e 2019-08-03 stsp __dead static void
7781 715dc77e 2019-08-03 stsp usage_stage(void)
7783 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7784 f3055044 2019-08-07 stsp "[file-path ...]\n",
7785 715dc77e 2019-08-03 stsp getprogname());
7789 715dc77e 2019-08-03 stsp static const struct got_error *
7790 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7791 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7792 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7793 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7795 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7796 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7798 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7799 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7800 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7801 408b4ebc 2019-08-03 stsp return NULL;
7803 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7804 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7805 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7807 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7809 408b4ebc 2019-08-03 stsp return err;
7811 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7812 408b4ebc 2019-08-03 stsp free(id_str);
7813 dc424a06 2019-08-07 stsp return NULL;
7816 dc424a06 2019-08-07 stsp static const struct got_error *
7817 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7819 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7820 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7821 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7822 715dc77e 2019-08-03 stsp char *cwd = NULL;
7823 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7824 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7825 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7826 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7827 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7828 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7830 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7832 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7833 715dc77e 2019-08-03 stsp switch (ch) {
7835 408b4ebc 2019-08-03 stsp list_stage = 1;
7838 dc424a06 2019-08-07 stsp pflag = 1;
7841 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7844 715dc77e 2019-08-03 stsp usage_stage();
7845 715dc77e 2019-08-03 stsp /* NOTREACHED */
7849 715dc77e 2019-08-03 stsp argc -= optind;
7850 715dc77e 2019-08-03 stsp argv += optind;
7852 715dc77e 2019-08-03 stsp #ifndef PROFILE
7853 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7854 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7855 715dc77e 2019-08-03 stsp err(1, "pledge");
7857 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7858 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7859 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7860 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7862 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7863 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7864 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7865 715dc77e 2019-08-03 stsp goto done;
7868 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7869 715dc77e 2019-08-03 stsp if (error)
7870 715dc77e 2019-08-03 stsp goto done;
7872 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7874 715dc77e 2019-08-03 stsp if (error != NULL)
7875 715dc77e 2019-08-03 stsp goto done;
7877 dc424a06 2019-08-07 stsp if (patch_script_path) {
7878 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7879 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7880 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7881 dc424a06 2019-08-07 stsp patch_script_path);
7882 dc424a06 2019-08-07 stsp goto done;
7885 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7886 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7887 715dc77e 2019-08-03 stsp if (error)
7888 715dc77e 2019-08-03 stsp goto done;
7890 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7891 6d022e97 2019-08-04 stsp if (error)
7892 6d022e97 2019-08-04 stsp goto done;
7894 6d022e97 2019-08-04 stsp if (list_stage)
7895 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7896 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7898 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7899 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7900 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7901 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7902 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7905 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7906 2e1f37b0 2019-08-08 stsp error == NULL)
7907 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7909 ad493afc 2019-08-03 stsp got_repo_close(repo);
7910 ad493afc 2019-08-03 stsp if (worktree)
7911 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7912 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7913 ad493afc 2019-08-03 stsp free((char *)pe->path);
7914 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7915 ad493afc 2019-08-03 stsp free(cwd);
7916 ad493afc 2019-08-03 stsp return error;
7919 ad493afc 2019-08-03 stsp __dead static void
7920 ad493afc 2019-08-03 stsp usage_unstage(void)
7922 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7923 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7924 ad493afc 2019-08-03 stsp getprogname());
7929 ad493afc 2019-08-03 stsp static const struct got_error *
7930 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7932 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7933 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7934 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7935 ad493afc 2019-08-03 stsp char *cwd = NULL;
7936 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7937 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7938 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7939 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7940 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7941 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7943 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7945 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7946 ad493afc 2019-08-03 stsp switch (ch) {
7948 2e1f37b0 2019-08-08 stsp pflag = 1;
7951 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7954 ad493afc 2019-08-03 stsp usage_unstage();
7955 ad493afc 2019-08-03 stsp /* NOTREACHED */
7959 ad493afc 2019-08-03 stsp argc -= optind;
7960 ad493afc 2019-08-03 stsp argv += optind;
7962 ad493afc 2019-08-03 stsp #ifndef PROFILE
7963 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7964 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7965 ad493afc 2019-08-03 stsp err(1, "pledge");
7967 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7968 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7970 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7971 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7972 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7973 ad493afc 2019-08-03 stsp goto done;
7976 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7977 ad493afc 2019-08-03 stsp if (error)
7978 ad493afc 2019-08-03 stsp goto done;
7980 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7982 ad493afc 2019-08-03 stsp if (error != NULL)
7983 ad493afc 2019-08-03 stsp goto done;
7985 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7986 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7987 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7988 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7989 2e1f37b0 2019-08-08 stsp patch_script_path);
7990 2e1f37b0 2019-08-08 stsp goto done;
7994 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7995 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7996 ad493afc 2019-08-03 stsp if (error)
7997 ad493afc 2019-08-03 stsp goto done;
7999 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8000 ad493afc 2019-08-03 stsp if (error)
8001 ad493afc 2019-08-03 stsp goto done;
8003 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8004 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8005 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8006 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8008 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8009 2e1f37b0 2019-08-08 stsp error == NULL)
8010 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8012 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8013 715dc77e 2019-08-03 stsp if (worktree)
8014 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8015 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8016 715dc77e 2019-08-03 stsp free((char *)pe->path);
8017 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8018 715dc77e 2019-08-03 stsp free(cwd);
8019 01073a5d 2019-08-22 stsp return error;
8022 01073a5d 2019-08-22 stsp __dead static void
8023 01073a5d 2019-08-22 stsp usage_cat(void)
8025 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8026 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8030 01073a5d 2019-08-22 stsp static const struct got_error *
8031 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8033 01073a5d 2019-08-22 stsp const struct got_error *err;
8034 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8036 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8038 01073a5d 2019-08-22 stsp return err;
8040 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8041 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8042 01073a5d 2019-08-22 stsp return err;
8045 01073a5d 2019-08-22 stsp static const struct got_error *
8046 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8048 01073a5d 2019-08-22 stsp const struct got_error *err;
8049 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8050 56e0773d 2019-11-28 stsp int nentries, i;
8052 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8054 01073a5d 2019-08-22 stsp return err;
8056 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8057 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8058 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8059 01073a5d 2019-08-22 stsp char *id_str;
8060 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8062 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8063 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8066 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8067 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8068 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8069 01073a5d 2019-08-22 stsp free(id_str);
8072 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8073 01073a5d 2019-08-22 stsp return err;
8076 01073a5d 2019-08-22 stsp static const struct got_error *
8077 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8079 01073a5d 2019-08-22 stsp const struct got_error *err;
8080 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8081 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8082 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8083 01073a5d 2019-08-22 stsp char *id_str = NULL;
8084 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8086 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8088 01073a5d 2019-08-22 stsp return err;
8090 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8092 01073a5d 2019-08-22 stsp goto done;
8094 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8095 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8096 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8097 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8098 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8099 01073a5d 2019-08-22 stsp char *pid_str;
8100 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8102 01073a5d 2019-08-22 stsp goto done;
8103 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8104 01073a5d 2019-08-22 stsp free(pid_str);
8106 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8107 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8108 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8110 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8111 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8112 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8114 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8115 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8116 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8118 01073a5d 2019-08-22 stsp free(id_str);
8119 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8120 01073a5d 2019-08-22 stsp return err;
8123 01073a5d 2019-08-22 stsp static const struct got_error *
8124 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8126 01073a5d 2019-08-22 stsp const struct got_error *err;
8127 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8128 01073a5d 2019-08-22 stsp char *id_str = NULL;
8129 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8131 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8133 01073a5d 2019-08-22 stsp return err;
8135 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8137 01073a5d 2019-08-22 stsp goto done;
8139 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8141 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8142 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8143 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8144 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8146 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8147 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8150 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8151 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8154 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8155 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8156 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8162 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8163 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8165 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8166 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8167 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8169 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8170 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8171 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8173 01073a5d 2019-08-22 stsp free(id_str);
8174 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8175 01073a5d 2019-08-22 stsp return err;
8178 01073a5d 2019-08-22 stsp static const struct got_error *
8179 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8181 01073a5d 2019-08-22 stsp const struct got_error *error;
8182 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8183 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8184 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8185 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8186 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8187 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8189 01073a5d 2019-08-22 stsp #ifndef PROFILE
8190 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8191 01073a5d 2019-08-22 stsp NULL) == -1)
8192 01073a5d 2019-08-22 stsp err(1, "pledge");
8195 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8196 01073a5d 2019-08-22 stsp switch (ch) {
8198 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8201 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8202 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8203 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8205 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8208 896e9b6f 2019-08-26 stsp force_path = 1;
8211 01073a5d 2019-08-22 stsp usage_cat();
8212 01073a5d 2019-08-22 stsp /* NOTREACHED */
8216 01073a5d 2019-08-22 stsp argc -= optind;
8217 01073a5d 2019-08-22 stsp argv += optind;
8219 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8220 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8221 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8222 01073a5d 2019-08-22 stsp goto done;
8224 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8225 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8226 01073a5d 2019-08-22 stsp goto done;
8227 01073a5d 2019-08-22 stsp if (worktree) {
8228 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8229 01073a5d 2019-08-22 stsp repo_path = strdup(
8230 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8231 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8232 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8233 01073a5d 2019-08-22 stsp goto done;
8238 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8239 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8240 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8241 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8244 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8245 01073a5d 2019-08-22 stsp free(repo_path);
8246 01073a5d 2019-08-22 stsp if (error != NULL)
8247 01073a5d 2019-08-22 stsp goto done;
8249 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8250 896e9b6f 2019-08-26 stsp if (error)
8251 896e9b6f 2019-08-26 stsp goto done;
8253 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8254 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8255 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8256 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8257 01073a5d 2019-08-22 stsp if (error)
8258 01073a5d 2019-08-22 stsp goto done;
8260 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8261 896e9b6f 2019-08-26 stsp if (force_path) {
8262 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8264 896e9b6f 2019-08-26 stsp if (error)
8267 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8268 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8269 896e9b6f 2019-08-26 stsp if (error) {
8270 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8271 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8273 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8274 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8275 896e9b6f 2019-08-26 stsp if (error)
8280 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8281 01073a5d 2019-08-22 stsp if (error)
8284 01073a5d 2019-08-22 stsp switch (obj_type) {
8285 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8286 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8288 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8289 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8291 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8292 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8294 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8295 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8298 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8301 01073a5d 2019-08-22 stsp if (error)
8303 01073a5d 2019-08-22 stsp free(label);
8304 01073a5d 2019-08-22 stsp label = NULL;
8306 01073a5d 2019-08-22 stsp id = NULL;
8309 01073a5d 2019-08-22 stsp free(label);
8311 896e9b6f 2019-08-26 stsp free(commit_id);
8312 01073a5d 2019-08-22 stsp if (worktree)
8313 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8314 01073a5d 2019-08-22 stsp if (repo) {
8315 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8316 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8317 01073a5d 2019-08-22 stsp if (error == NULL)
8318 01073a5d 2019-08-22 stsp error = repo_error;
8320 0ebf8283 2019-07-24 stsp return error;