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"
57 50b0790e 2020-09-11 stsp #include "got_gotconfig.h"
59 5c860e29 2018-03-12 stsp #ifndef nitems
60 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
64 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
67 99437157 2018-11-11 stsp catch_sigint(int signo)
69 99437157 2018-11-11 stsp sigint_received = 1;
73 99437157 2018-11-11 stsp catch_sigpipe(int signo)
75 99437157 2018-11-11 stsp sigpipe_received = 1;
79 8cfb4057 2019-07-09 stsp struct got_cmd {
80 5c860e29 2018-03-12 stsp const char *cmd_name;
81 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
82 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
83 97b3a7be 2019-07-09 stsp const char *cmd_alias;
86 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
87 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
88 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
89 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
90 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
91 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
92 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
94 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
95 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
96 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
97 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
98 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
99 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
100 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
101 d00136be 2019-03-26 stsp __dead static void usage_add(void);
102 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
103 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
104 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
105 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
106 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
107 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
108 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
109 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
110 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
111 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
112 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
113 b2118c49 2020-07-28 stsp __dead static void usage_info(void);
115 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
116 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
117 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
118 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
120 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
121 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
122 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
123 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
124 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
125 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
126 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
127 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
128 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
129 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
130 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
131 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
132 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
133 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
134 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
135 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
136 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
137 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
138 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
139 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
140 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
141 b2118c49 2020-07-28 stsp static const struct got_error* cmd_info(int, char *[]);
143 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
144 b2118c49 2020-07-28 stsp { "init", cmd_init, usage_init, "" },
145 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
146 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
147 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
148 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
149 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
150 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
151 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
152 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
153 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
154 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
155 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
156 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
157 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
158 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
159 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
160 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
161 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
162 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
164 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
165 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
166 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
167 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
168 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
169 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
170 b2118c49 2020-07-28 stsp { "info", cmd_info, usage_info, "" },
173 ce5b7c56 2019-07-09 stsp static void
174 ce5b7c56 2019-07-09 stsp list_commands(void)
178 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
179 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
180 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
181 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
183 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
187 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
189 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
190 5c860e29 2018-03-12 stsp unsigned int i;
192 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
193 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
194 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
195 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
198 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
200 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
201 5c860e29 2018-03-12 stsp switch (ch) {
209 ce5b7c56 2019-07-09 stsp usage(hflag);
210 5c860e29 2018-03-12 stsp /* NOTREACHED */
214 5c860e29 2018-03-12 stsp argc -= optind;
215 5c860e29 2018-03-12 stsp argv += optind;
216 1e70621d 2018-03-27 stsp optind = 0;
218 53ccebc2 2019-07-30 stsp if (Vflag) {
219 53ccebc2 2019-07-30 stsp got_version_print_str();
223 5c860e29 2018-03-12 stsp if (argc <= 0)
224 ce5b7c56 2019-07-09 stsp usage(hflag);
226 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
227 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
229 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
230 d7d4f210 2018-03-12 stsp const struct got_error *error;
232 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
234 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
239 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
241 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
242 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
243 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
244 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
245 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 70015d7a 2019-11-08 stsp !(sigint_received &&
247 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 ce5b7c56 2019-07-09 stsp list_commands();
260 4ed7e80c 2018-05-20 stsp __dead static void
261 ce5b7c56 2019-07-09 stsp usage(int hflag)
263 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
264 53ccebc2 2019-07-30 stsp getprogname());
266 ce5b7c56 2019-07-09 stsp list_commands();
270 0266afb7 2019-01-04 stsp static const struct got_error *
271 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
273 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
274 e2ba3d07 2019-05-13 stsp const char *editor;
276 8920fa04 2019-08-18 stsp *abspath = NULL;
278 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
279 e2ba3d07 2019-05-13 stsp if (editor == NULL)
280 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
282 0ee7065d 2019-05-13 stsp if (editor) {
283 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
285 0ee7065d 2019-05-13 stsp return err;
288 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
289 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
290 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
291 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
294 e2ba3d07 2019-05-13 stsp return NULL;
297 e2ba3d07 2019-05-13 stsp static const struct got_error *
298 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
299 c530dc23 2019-07-23 stsp const char *worktree_path)
301 163ce85a 2019-05-13 stsp const struct got_error *err;
303 37c06ea4 2019-07-15 stsp #ifdef PROFILE
304 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
305 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
307 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
310 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
313 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
317 163ce85a 2019-05-13 stsp if (err != NULL)
318 163ce85a 2019-05-13 stsp return err;
320 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
321 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
323 0266afb7 2019-01-04 stsp return NULL;
326 2c7829a4 2019-06-17 stsp __dead static void
327 2c7829a4 2019-06-17 stsp usage_init(void)
329 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 2c7829a4 2019-06-17 stsp static const struct got_error *
334 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
336 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
337 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
340 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
341 2c7829a4 2019-06-17 stsp switch (ch) {
343 2c7829a4 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp /* NOTREACHED */
348 2c7829a4 2019-06-17 stsp argc -= optind;
349 2c7829a4 2019-06-17 stsp argv += optind;
351 2c7829a4 2019-06-17 stsp #ifndef PROFILE
352 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
353 2c7829a4 2019-06-17 stsp err(1, "pledge");
355 2c7829a4 2019-06-17 stsp if (argc != 1)
356 bc20e173 2019-06-17 stsp usage_init();
358 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
359 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
360 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
362 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
364 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
365 2c7829a4 2019-06-17 stsp if (error &&
366 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
373 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
375 3ce1b845 2019-07-15 stsp free(repo_path);
376 3ce1b845 2019-07-15 stsp return error;
379 3ce1b845 2019-07-15 stsp __dead static void
380 3ce1b845 2019-07-15 stsp usage_import(void)
382 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
388 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
391 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
392 3ce1b845 2019-07-15 stsp int st = -1;
394 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
395 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
396 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
398 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
400 3ce1b845 2019-07-15 stsp goto doneediting;
402 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
403 3ce1b845 2019-07-15 stsp _exit(127);
406 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
407 3ce1b845 2019-07-15 stsp if (errno != EINTR)
410 3ce1b845 2019-07-15 stsp doneediting:
411 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
412 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
413 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
415 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
416 3ce1b845 2019-07-15 stsp errno = EINTR;
420 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
423 3ce1b845 2019-07-15 stsp static const struct got_error *
424 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 3ce1b845 2019-07-15 stsp const char *initial_content)
427 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
428 3ce1b845 2019-07-15 stsp char buf[1024];
429 3ce1b845 2019-07-15 stsp struct stat st, st2;
431 3ce1b845 2019-07-15 stsp int content_changed = 0;
432 3ce1b845 2019-07-15 stsp size_t len;
434 3ce1b845 2019-07-15 stsp *logmsg = NULL;
436 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
437 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
439 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
440 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
442 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
445 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
449 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
450 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
451 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
452 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
455 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
456 3ce1b845 2019-07-15 stsp if (fp == NULL) {
457 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
460 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
461 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
462 3ce1b845 2019-07-15 stsp content_changed = 1;
463 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
465 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
467 3ce1b845 2019-07-15 stsp fclose(fp);
469 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
474 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
475 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
479 3ce1b845 2019-07-15 stsp free(*logmsg);
480 3ce1b845 2019-07-15 stsp *logmsg = NULL;
482 3ce1b845 2019-07-15 stsp return err;
485 3ce1b845 2019-07-15 stsp static const struct got_error *
486 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
489 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
490 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
491 1601cb9f 2020-09-11 naddy int initial_content_len;
492 97972933 2020-09-11 stsp int fd = -1;
494 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
495 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
496 1601cb9f 2020-09-11 naddy branch_name);
497 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
498 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
500 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
501 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
505 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
506 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *logmsg_path);
510 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
513 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *logmsg_path);
514 3ce1b845 2019-07-15 stsp free(initial_content);
516 59f86c76 2020-09-11 stsp free(*logmsg_path);
517 59f86c76 2020-09-11 stsp *logmsg_path = NULL;
519 3ce1b845 2019-07-15 stsp return err;
522 3ce1b845 2019-07-15 stsp static const struct got_error *
523 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
525 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
526 3ce1b845 2019-07-15 stsp return NULL;
529 3ce1b845 2019-07-15 stsp static const struct got_error *
530 50b0790e 2020-09-11 stsp get_author(char **author, struct got_repository *repo,
531 50b0790e 2020-09-11 stsp struct got_worktree *worktree)
533 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
534 50b0790e 2020-09-11 stsp const char *got_author = NULL, *name, *email;
535 50b0790e 2020-09-11 stsp const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
537 84792843 2019-08-09 stsp *author = NULL;
539 50b0790e 2020-09-11 stsp if (worktree)
540 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
541 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
544 50b0790e 2020-09-11 stsp * Priority of potential author information sources, from most
545 50b0790e 2020-09-11 stsp * significant to least significant:
546 50b0790e 2020-09-11 stsp * 1) work tree's .got/got.conf file
547 50b0790e 2020-09-11 stsp * 2) repository's got.conf file
548 50b0790e 2020-09-11 stsp * 3) repository's git config file
549 50b0790e 2020-09-11 stsp * 4) environment variables
550 50b0790e 2020-09-11 stsp * 5) global git config files (in user's home directory or /etc)
553 50b0790e 2020-09-11 stsp if (worktree_conf)
554 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(worktree_conf);
555 50b0790e 2020-09-11 stsp if (got_author == NULL)
556 50b0790e 2020-09-11 stsp got_author = got_gotconfig_get_author(repo_conf);
557 84792843 2019-08-09 stsp if (got_author == NULL) {
558 257add31 2020-09-09 stsp name = got_repo_get_gitconfig_author_name(repo);
559 257add31 2020-09-09 stsp email = got_repo_get_gitconfig_author_email(repo);
560 c9956ddf 2019-09-08 stsp if (name && email) {
561 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
562 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
563 c9956ddf 2019-09-08 stsp return NULL;
566 257add31 2020-09-09 stsp got_author = getenv("GOT_AUTHOR");
567 257add31 2020-09-09 stsp if (got_author == NULL) {
568 257add31 2020-09-09 stsp name = got_repo_get_global_gitconfig_author_name(repo);
569 257add31 2020-09-09 stsp email = got_repo_get_global_gitconfig_author_email(
571 257add31 2020-09-09 stsp if (name && email) {
572 257add31 2020-09-09 stsp if (asprintf(author, "%s <%s>", name, email)
574 257add31 2020-09-09 stsp return got_error_from_errno("asprintf");
575 257add31 2020-09-09 stsp return NULL;
577 257add31 2020-09-09 stsp /* TODO: Look up user in password database? */
578 257add31 2020-09-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
582 aba9c984 2019-09-08 stsp *author = strdup(got_author);
583 aba9c984 2019-09-08 stsp if (*author == NULL)
584 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
587 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
588 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
590 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
591 84792843 2019-08-09 stsp got_author++;
592 aba9c984 2019-09-08 stsp if (*got_author != '<') {
593 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
596 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
597 84792843 2019-08-09 stsp got_author++;
598 aba9c984 2019-09-08 stsp if (*got_author != '@') {
599 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
602 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
603 84792843 2019-08-09 stsp got_author++;
604 84792843 2019-08-09 stsp if (*got_author != '>')
605 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
608 aba9c984 2019-09-08 stsp free(*author);
609 aba9c984 2019-09-08 stsp *author = NULL;
611 aba9c984 2019-09-08 stsp return err;
614 c9956ddf 2019-09-08 stsp static const struct got_error *
615 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
617 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
619 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
620 c9956ddf 2019-09-08 stsp if (homedir) {
621 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
625 c9956ddf 2019-09-08 stsp return NULL;
628 84792843 2019-08-09 stsp static const struct got_error *
629 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
631 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
632 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
635 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
637 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
640 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
641 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
642 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
644 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
646 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
647 3ce1b845 2019-07-15 stsp switch (ch) {
649 3ce1b845 2019-07-15 stsp branch_name = optarg;
652 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
653 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
654 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
659 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
660 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
661 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
667 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
669 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
675 b2b65d18 2019-08-22 stsp usage_import();
676 3ce1b845 2019-07-15 stsp /* NOTREACHED */
680 3ce1b845 2019-07-15 stsp argc -= optind;
681 3ce1b845 2019-07-15 stsp argv += optind;
683 3ce1b845 2019-07-15 stsp #ifndef PROFILE
684 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
686 3ce1b845 2019-07-15 stsp NULL) == -1)
687 3ce1b845 2019-07-15 stsp err(1, "pledge");
689 3ce1b845 2019-07-15 stsp if (argc != 1)
690 3ce1b845 2019-07-15 stsp usage_import();
692 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
693 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
694 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
695 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
697 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
698 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
701 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
705 50b0790e 2020-09-11 stsp error = get_author(&author, repo, NULL);
707 aba9c984 2019-09-08 stsp return error;
710 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
711 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
712 e560b7e0 2019-11-28 stsp * an unintended typo.
714 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
715 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
717 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
722 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
723 3ce1b845 2019-07-15 stsp if (error) {
724 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
727 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 3ce1b845 2019-07-15 stsp "import target branch already exists");
732 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
733 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
734 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
737 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
740 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
741 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
743 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
744 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
747 8e158b01 2019-09-22 stsp free(logmsg);
748 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
749 ef293bdd 2019-10-21 stsp path_dir, refname);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
752 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
758 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
759 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
760 ef293bdd 2019-10-21 stsp if (logmsg_path)
761 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
765 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
766 ef293bdd 2019-10-21 stsp if (error) {
767 ef293bdd 2019-10-21 stsp if (logmsg_path)
768 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
772 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
774 ef293bdd 2019-10-21 stsp if (error) {
775 ef293bdd 2019-10-21 stsp if (logmsg_path)
776 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
780 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
781 ef293bdd 2019-10-21 stsp if (error) {
782 ef293bdd 2019-10-21 stsp if (logmsg_path)
783 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
787 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
788 ef293bdd 2019-10-21 stsp if (error) {
789 ef293bdd 2019-10-21 stsp if (logmsg_path)
790 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
794 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
795 ef293bdd 2019-10-21 stsp if (error) {
796 ef293bdd 2019-10-21 stsp if (logmsg_path)
797 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
801 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
802 3ce1b845 2019-07-15 stsp if (error) {
803 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
804 ef293bdd 2019-10-21 stsp if (logmsg_path)
805 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
809 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
810 3ce1b845 2019-07-15 stsp branch_ref);
811 ef293bdd 2019-10-21 stsp if (error) {
812 ef293bdd 2019-10-21 stsp if (logmsg_path)
813 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
817 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
818 ef293bdd 2019-10-21 stsp if (error) {
819 ef293bdd 2019-10-21 stsp if (logmsg_path)
820 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
825 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
826 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
828 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
829 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
830 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
831 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
833 8e158b01 2019-09-22 stsp free(logmsg);
834 ef293bdd 2019-10-21 stsp free(logmsg_path);
835 2c7829a4 2019-06-17 stsp free(repo_path);
836 3ce1b845 2019-07-15 stsp free(editor);
837 3ce1b845 2019-07-15 stsp free(refname);
838 3ce1b845 2019-07-15 stsp free(new_commit_id);
839 3ce1b845 2019-07-15 stsp free(id_str);
840 aba9c984 2019-09-08 stsp free(author);
841 c9956ddf 2019-09-08 stsp free(gitconfig_path);
842 3ce1b845 2019-07-15 stsp if (branch_ref)
843 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
844 3ce1b845 2019-07-15 stsp if (head_ref)
845 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
846 2c7829a4 2019-06-17 stsp return error;
849 93658fb9 2020-03-18 stsp __dead static void
850 93658fb9 2020-03-18 stsp usage_clone(void)
852 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
857 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
858 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
859 892ac3b6 2020-03-18 stsp int last_p_indexed;
860 892ac3b6 2020-03-18 stsp int last_p_resolved;
861 68999b92 2020-03-18 stsp int verbosity;
863 04d9a9ec 2020-09-24 stsp struct got_repository *repo;
865 04d9a9ec 2020-09-24 stsp int create_configs;
866 04d9a9ec 2020-09-24 stsp int configs_created;
868 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *symrefs;
869 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches;
870 04d9a9ec 2020-09-24 stsp const char *proto;
871 04d9a9ec 2020-09-24 stsp const char *host;
872 04d9a9ec 2020-09-24 stsp const char *port;
873 04d9a9ec 2020-09-24 stsp const char *remote_repo_path;
874 04d9a9ec 2020-09-24 stsp const char *git_url;
875 04d9a9ec 2020-09-24 stsp int fetch_all_branches;
876 04d9a9ec 2020-09-24 stsp int mirror_references;
877 04d9a9ec 2020-09-24 stsp } config_info;
880 04d9a9ec 2020-09-24 stsp /* XXX forward declaration */
881 93658fb9 2020-03-18 stsp static const struct got_error *
882 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
883 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
884 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
885 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo);
887 04d9a9ec 2020-09-24 stsp static const struct got_error *
888 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
889 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
891 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
892 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
893 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
894 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
895 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
898 04d9a9ec 2020-09-24 stsp * In order to allow a failed clone to be resumed with 'got fetch'
899 04d9a9ec 2020-09-24 stsp * we try to create configuration files as soon as possible.
900 04d9a9ec 2020-09-24 stsp * Once the server has sent information about its default branch
901 04d9a9ec 2020-09-24 stsp * we have all required information.
903 04d9a9ec 2020-09-24 stsp if (a->create_configs && !a->configs_created &&
904 04d9a9ec 2020-09-24 stsp !TAILQ_EMPTY(a->config_info.symrefs)) {
905 04d9a9ec 2020-09-24 stsp err = create_config_files(a->config_info.proto,
906 04d9a9ec 2020-09-24 stsp a->config_info.host, a->config_info.port,
907 04d9a9ec 2020-09-24 stsp a->config_info.remote_repo_path,
908 04d9a9ec 2020-09-24 stsp a->config_info.git_url,
909 04d9a9ec 2020-09-24 stsp a->config_info.fetch_all_branches,
910 04d9a9ec 2020-09-24 stsp a->config_info.mirror_references,
911 04d9a9ec 2020-09-24 stsp a->config_info.symrefs,
912 04d9a9ec 2020-09-24 stsp a->config_info.wanted_branches, a->repo);
914 04d9a9ec 2020-09-24 stsp return err;
915 04d9a9ec 2020-09-24 stsp a->configs_created = 1;
918 68999b92 2020-03-18 stsp if (a->verbosity < 0)
919 68999b92 2020-03-18 stsp return NULL;
921 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
922 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
923 892ac3b6 2020-03-18 stsp fflush(stdout);
924 12d1281e 2020-03-19 stsp return NULL;
927 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
928 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
929 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
930 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
931 892ac3b6 2020-03-18 stsp print_size = 1;
932 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
933 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
934 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
936 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
937 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
938 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
939 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
940 892ac3b6 2020-03-18 stsp print_indexed = 1;
941 892ac3b6 2020-03-18 stsp print_size = 1;
944 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
945 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
946 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
947 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
948 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
949 892ac3b6 2020-03-18 stsp print_resolved = 1;
950 892ac3b6 2020-03-18 stsp print_indexed = 1;
951 892ac3b6 2020-03-18 stsp print_size = 1;
956 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
957 892ac3b6 2020-03-18 stsp printf("\r");
958 892ac3b6 2020-03-18 stsp if (print_size)
959 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
960 d715f13e 2020-03-19 stsp if (print_indexed)
961 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
962 d715f13e 2020-03-19 stsp if (print_resolved)
963 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
964 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
965 892ac3b6 2020-03-18 stsp fflush(stdout);
967 531c3985 2020-03-18 stsp return NULL;
970 531c3985 2020-03-18 stsp static const struct got_error *
971 04d9a9ec 2020-09-24 stsp create_symref(const char *refname, struct got_reference *target_ref,
972 04d9a9ec 2020-09-24 stsp int verbosity, struct got_repository *repo)
974 4ba14133 2020-03-20 stsp const struct got_error *err;
975 04d9a9ec 2020-09-24 stsp struct got_reference *head_symref;
977 04d9a9ec 2020-09-24 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
979 4ba14133 2020-03-20 stsp return err;
981 04d9a9ec 2020-09-24 stsp err = got_ref_write(head_symref, repo);
982 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
983 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
984 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
986 04d9a9ec 2020-09-24 stsp got_ref_close(head_symref);
987 4ba14133 2020-03-20 stsp return err;
990 4ba14133 2020-03-20 stsp static const struct got_error *
991 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
992 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
994 41b0de12 2020-03-21 stsp const struct got_error *err;
995 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
997 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
998 41b0de12 2020-03-21 stsp const char *refname = pe->path;
999 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
1001 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
1004 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
1005 41b0de12 2020-03-21 stsp const char *refname = pe->path;
1006 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
1007 41b0de12 2020-03-21 stsp char *id_str;
1009 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1011 41b0de12 2020-03-21 stsp return err;
1012 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
1013 41b0de12 2020-03-21 stsp free(id_str);
1016 41b0de12 2020-03-21 stsp return NULL;
1019 6338a6a1 2020-03-21 stsp static const struct got_error *
1020 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
1021 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
1023 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
1024 6338a6a1 2020-03-21 stsp struct got_reference *ref;
1025 6338a6a1 2020-03-21 stsp char *id_str;
1027 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1029 6338a6a1 2020-03-21 stsp return err;
1031 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
1033 6338a6a1 2020-03-21 stsp goto done;
1035 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1036 6338a6a1 2020-03-21 stsp got_ref_close(ref);
1038 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
1039 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
1041 6338a6a1 2020-03-21 stsp free(id_str);
1042 6338a6a1 2020-03-21 stsp return err;
1045 0e4002ca 2020-03-21 stsp static int
1046 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
1048 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
1050 0e4002ca 2020-03-21 stsp refname += 5;
1053 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
1054 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
1056 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
1058 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
1061 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
1062 0e4002ca 2020-03-21 stsp wanted_ref += 5;
1064 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
1065 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1068 0e4002ca 2020-03-21 stsp /* Allow exact match. */
1069 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
1072 0e4002ca 2020-03-21 stsp static int
1073 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1075 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
1077 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
1078 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
1085 41b0de12 2020-03-21 stsp static const struct got_error *
1086 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1087 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1089 0e4002ca 2020-03-21 stsp const struct got_error *err;
1090 0e4002ca 2020-03-21 stsp char *remote_refname;
1092 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1093 0e4002ca 2020-03-21 stsp refname += 5;
1095 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1096 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1097 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1099 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1100 0e4002ca 2020-03-21 stsp free(remote_refname);
1101 7c0b7f42 2020-09-24 stsp return err;
1104 7c0b7f42 2020-09-24 stsp static const struct got_error *
1105 7c0b7f42 2020-09-24 stsp create_gotconfig(const char *proto, const char *host, const char *port,
1106 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, int fetch_all_branches, int mirror_references,
1107 7c0b7f42 2020-09-24 stsp struct got_repository *repo)
1109 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1110 7c0b7f42 2020-09-24 stsp char *gotconfig_path = NULL;
1111 7c0b7f42 2020-09-24 stsp char *gotconfig = NULL;
1112 7c0b7f42 2020-09-24 stsp FILE *gotconfig_file = NULL;
1113 7c0b7f42 2020-09-24 stsp ssize_t n;
1115 7c0b7f42 2020-09-24 stsp /* Create got.conf(5). */
1116 7c0b7f42 2020-09-24 stsp gotconfig_path = got_repo_get_path_gotconfig(repo);
1117 7c0b7f42 2020-09-24 stsp if (gotconfig_path == NULL) {
1118 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gotconfig");
1119 7c0b7f42 2020-09-24 stsp goto done;
1121 7c0b7f42 2020-09-24 stsp gotconfig_file = fopen(gotconfig_path, "a");
1122 7c0b7f42 2020-09-24 stsp if (gotconfig_file == NULL) {
1123 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gotconfig_path);
1124 7c0b7f42 2020-09-24 stsp goto done;
1126 7c0b7f42 2020-09-24 stsp if (asprintf(&gotconfig,
1127 7c0b7f42 2020-09-24 stsp "remote \"%s\" {\n"
1128 7c0b7f42 2020-09-24 stsp "\tserver %s\n"
1129 7c0b7f42 2020-09-24 stsp "\tprotocol %s\n"
1131 7c0b7f42 2020-09-24 stsp "\trepository \"%s\"\n"
1134 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1135 7c0b7f42 2020-09-24 stsp port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1136 7c0b7f42 2020-09-24 stsp remote_repo_path,
1137 7c0b7f42 2020-09-24 stsp mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1138 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1139 7c0b7f42 2020-09-24 stsp goto done;
1141 7c0b7f42 2020-09-24 stsp n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1142 7c0b7f42 2020-09-24 stsp if (n != strlen(gotconfig)) {
1143 7c0b7f42 2020-09-24 stsp err = got_ferror(gotconfig_file, GOT_ERR_IO);
1144 7c0b7f42 2020-09-24 stsp goto done;
1148 7c0b7f42 2020-09-24 stsp if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1149 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gotconfig_path);
1150 7c0b7f42 2020-09-24 stsp free(gotconfig_path);
1151 7c0b7f42 2020-09-24 stsp return err;
1154 7c0b7f42 2020-09-24 stsp static const struct got_error *
1155 04d9a9ec 2020-09-24 stsp create_gitconfig(const char *git_url, const char *default_branch,
1156 7c0b7f42 2020-09-24 stsp int fetch_all_branches, int mirror_references, struct got_repository *repo)
1158 7c0b7f42 2020-09-24 stsp const struct got_error *err = NULL;
1159 7c0b7f42 2020-09-24 stsp char *gitconfig_path = NULL;
1160 7c0b7f42 2020-09-24 stsp char *gitconfig = NULL;
1161 7c0b7f42 2020-09-24 stsp FILE *gitconfig_file = NULL;
1162 7c0b7f42 2020-09-24 stsp ssize_t n;
1164 7c0b7f42 2020-09-24 stsp /* Create a config file Git can understand. */
1165 7c0b7f42 2020-09-24 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1166 7c0b7f42 2020-09-24 stsp if (gitconfig_path == NULL) {
1167 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("got_repo_get_path_gitconfig");
1168 7c0b7f42 2020-09-24 stsp goto done;
1170 7c0b7f42 2020-09-24 stsp gitconfig_file = fopen(gitconfig_path, "a");
1171 7c0b7f42 2020-09-24 stsp if (gitconfig_file == NULL) {
1172 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fopen", gitconfig_path);
1173 7c0b7f42 2020-09-24 stsp goto done;
1175 7c0b7f42 2020-09-24 stsp if (mirror_references) {
1176 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1177 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1178 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1179 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/*:refs/*\n"
1180 7c0b7f42 2020-09-24 stsp "\tmirror = true\n",
1181 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1182 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1183 7c0b7f42 2020-09-24 stsp goto done;
1185 7c0b7f42 2020-09-24 stsp } else if (fetch_all_branches) {
1186 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1187 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1188 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1189 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1190 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1191 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1192 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1193 7c0b7f42 2020-09-24 stsp goto done;
1196 7c0b7f42 2020-09-24 stsp const char *branchname;
1199 7c0b7f42 2020-09-24 stsp * If the server specified a default branch, use just that one.
1200 7c0b7f42 2020-09-24 stsp * Otherwise fall back to fetching all branches on next fetch.
1202 04d9a9ec 2020-09-24 stsp if (default_branch) {
1203 04d9a9ec 2020-09-24 stsp branchname = default_branch;
1204 7c0b7f42 2020-09-24 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1205 7c0b7f42 2020-09-24 stsp branchname += 11;
1207 7c0b7f42 2020-09-24 stsp branchname = "*"; /* fall back to all branches */
1208 7c0b7f42 2020-09-24 stsp if (asprintf(&gitconfig,
1209 7c0b7f42 2020-09-24 stsp "[remote \"%s\"]\n"
1210 7c0b7f42 2020-09-24 stsp "\turl = %s\n"
1211 7c0b7f42 2020-09-24 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1212 7c0b7f42 2020-09-24 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1213 7c0b7f42 2020-09-24 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1214 7c0b7f42 2020-09-24 stsp branchname) == -1) {
1215 7c0b7f42 2020-09-24 stsp err = got_error_from_errno("asprintf");
1216 7c0b7f42 2020-09-24 stsp goto done;
1219 7c0b7f42 2020-09-24 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1220 7c0b7f42 2020-09-24 stsp if (n != strlen(gitconfig)) {
1221 7c0b7f42 2020-09-24 stsp err = got_ferror(gitconfig_file, GOT_ERR_IO);
1222 7c0b7f42 2020-09-24 stsp goto done;
1225 7c0b7f42 2020-09-24 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1226 7c0b7f42 2020-09-24 stsp err = got_error_from_errno2("fclose", gitconfig_path);
1227 7c0b7f42 2020-09-24 stsp free(gitconfig_path);
1228 0e4002ca 2020-03-21 stsp return err;
1231 0e4002ca 2020-03-21 stsp static const struct got_error *
1232 04d9a9ec 2020-09-24 stsp create_config_files(const char *proto, const char *host, const char *port,
1233 04d9a9ec 2020-09-24 stsp const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1234 04d9a9ec 2020-09-24 stsp int mirror_references, struct got_pathlist_head *symrefs,
1235 04d9a9ec 2020-09-24 stsp struct got_pathlist_head *wanted_branches, struct got_repository *repo)
1237 04d9a9ec 2020-09-24 stsp const struct got_error *err = NULL;
1238 04d9a9ec 2020-09-24 stsp const char *default_branch = NULL;
1239 04d9a9ec 2020-09-24 stsp struct got_pathlist_entry *pe;
1242 04d9a9ec 2020-09-24 stsp * If we asked for a set of wanted branches then use the first
1243 04d9a9ec 2020-09-24 stsp * one of those.
1245 04d9a9ec 2020-09-24 stsp if (!TAILQ_EMPTY(wanted_branches)) {
1246 04d9a9ec 2020-09-24 stsp pe = TAILQ_FIRST(wanted_branches);
1247 04d9a9ec 2020-09-24 stsp default_branch = pe->path;
1249 04d9a9ec 2020-09-24 stsp /* First HEAD ref listed by server is the default branch. */
1250 04d9a9ec 2020-09-24 stsp TAILQ_FOREACH(pe, symrefs, entry) {
1251 04d9a9ec 2020-09-24 stsp const char *refname = pe->path;
1252 04d9a9ec 2020-09-24 stsp const char *target = pe->data;
1254 04d9a9ec 2020-09-24 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1257 04d9a9ec 2020-09-24 stsp default_branch = target;
1262 04d9a9ec 2020-09-24 stsp /* Create got.conf(5). */
1263 04d9a9ec 2020-09-24 stsp err = create_gotconfig(proto, host, port, remote_repo_path,
1264 04d9a9ec 2020-09-24 stsp fetch_all_branches, mirror_references, repo);
1266 04d9a9ec 2020-09-24 stsp return err;
1268 04d9a9ec 2020-09-24 stsp /* Create a config file Git can understand. */
1269 04d9a9ec 2020-09-24 stsp return create_gitconfig(git_url, default_branch, fetch_all_branches,
1270 04d9a9ec 2020-09-24 stsp mirror_references, repo);
1273 04d9a9ec 2020-09-24 stsp static const struct got_error *
1274 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1276 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1277 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1278 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1279 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1280 a9c2d4c2 2020-09-24 stsp const char *repo_path;
1281 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1282 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1283 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1284 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1285 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1286 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1287 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1288 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1289 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1290 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1292 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1293 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1294 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1295 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1297 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1298 93658fb9 2020-03-18 stsp switch (ch) {
1300 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1303 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1304 4ba14133 2020-03-20 stsp optarg, NULL);
1305 4ba14133 2020-03-20 stsp if (error)
1306 4ba14133 2020-03-20 stsp return error;
1309 41b0de12 2020-03-21 stsp list_refs_only = 1;
1312 469dd726 2020-03-20 stsp mirror_references = 1;
1315 68999b92 2020-03-18 stsp if (verbosity < 0)
1316 68999b92 2020-03-18 stsp verbosity = 0;
1317 68999b92 2020-03-18 stsp else if (verbosity < 3)
1318 68999b92 2020-03-18 stsp verbosity++;
1321 68999b92 2020-03-18 stsp verbosity = -1;
1324 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1325 0e4002ca 2020-03-21 stsp optarg, NULL);
1326 0e4002ca 2020-03-21 stsp if (error)
1327 0e4002ca 2020-03-21 stsp return error;
1330 93658fb9 2020-03-18 stsp usage_clone();
1334 93658fb9 2020-03-18 stsp argc -= optind;
1335 93658fb9 2020-03-18 stsp argv += optind;
1337 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1338 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1339 41b0de12 2020-03-21 stsp if (list_refs_only) {
1340 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1341 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1342 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1343 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1344 41b0de12 2020-03-21 stsp if (mirror_references)
1345 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1346 41b0de12 2020-03-21 stsp if (verbosity == -1)
1347 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1348 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1349 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1352 93658fb9 2020-03-18 stsp uri = argv[0];
1354 9df6f38b 2020-03-18 stsp if (argc == 1)
1355 93658fb9 2020-03-18 stsp dirname = NULL;
1356 9df6f38b 2020-03-18 stsp else if (argc == 2)
1357 93658fb9 2020-03-18 stsp dirname = argv[1];
1359 93658fb9 2020-03-18 stsp usage_clone();
1361 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1362 4dbec0a8 2020-08-27 stsp &repo_name, uri);
1363 39c64a6a 2020-03-18 stsp if (error)
1364 09f63084 2020-03-20 stsp goto done;
1366 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1367 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1368 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1369 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1370 09838ffc 2020-03-18 stsp goto done;
1373 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1374 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1375 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1376 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1377 39c64a6a 2020-03-18 stsp err(1, "pledge");
1379 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1380 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1381 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1382 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1384 39c64a6a 2020-03-18 stsp err(1, "pledge");
1386 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1387 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1388 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1389 39c64a6a 2020-03-18 stsp goto done;
1391 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1392 39c64a6a 2020-03-18 stsp goto done;
1394 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1395 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1396 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1397 bb64b798 2020-03-18 stsp goto done;
1399 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1401 bb64b798 2020-03-18 stsp repo_path = dirname;
1403 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1404 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1405 2751fe64 2020-09-24 stsp if (error &&
1406 2751fe64 2020-09-24 stsp (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1407 2751fe64 2020-09-24 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1408 41b0de12 2020-03-21 stsp goto done;
1409 2751fe64 2020-09-24 stsp if (!got_path_dir_is_empty(repo_path)) {
1410 2751fe64 2020-09-24 stsp error = got_error_path(repo_path,
1411 2751fe64 2020-09-24 stsp GOT_ERR_DIR_NOT_EMPTY);
1412 41b0de12 2020-03-21 stsp goto done;
1416 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1417 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1418 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1419 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1420 ee448f5f 2020-03-18 stsp goto done;
1423 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1424 ee448f5f 2020-03-18 stsp if (error)
1425 ee448f5f 2020-03-18 stsp goto done;
1427 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1428 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1429 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1431 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1432 9c52365f 2020-03-21 stsp server_path, verbosity);
1433 39c64a6a 2020-03-18 stsp if (error)
1434 bb64b798 2020-03-18 stsp goto done;
1436 2751fe64 2020-09-24 stsp if (!list_refs_only) {
1437 2751fe64 2020-09-24 stsp error = got_repo_init(repo_path);
1438 2751fe64 2020-09-24 stsp if (error)
1439 2751fe64 2020-09-24 stsp goto done;
1440 2751fe64 2020-09-24 stsp error = got_repo_open(&repo, repo_path, NULL);
1441 2751fe64 2020-09-24 stsp if (error)
1442 2751fe64 2020-09-24 stsp goto done;
1445 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1446 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1447 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1448 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1449 04d9a9ec 2020-09-24 stsp fpa.create_configs = 1;
1450 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
1451 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
1452 04d9a9ec 2020-09-24 stsp fpa.config_info.symrefs = &symrefs;
1453 04d9a9ec 2020-09-24 stsp fpa.config_info.wanted_branches = &wanted_branches;
1454 04d9a9ec 2020-09-24 stsp fpa.config_info.proto = proto;
1455 04d9a9ec 2020-09-24 stsp fpa.config_info.host = host;
1456 04d9a9ec 2020-09-24 stsp fpa.config_info.port = port;
1457 04d9a9ec 2020-09-24 stsp fpa.config_info.remote_repo_path = server_path;
1458 04d9a9ec 2020-09-24 stsp fpa.config_info.git_url = git_url;
1459 04d9a9ec 2020-09-24 stsp fpa.config_info.fetch_all_branches = fetch_all_branches;
1460 04d9a9ec 2020-09-24 stsp fpa.config_info.mirror_references = mirror_references;
1461 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1462 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1463 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1464 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1465 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1466 39c64a6a 2020-03-18 stsp if (error)
1467 d9b4d0c0 2020-03-18 stsp goto done;
1469 41b0de12 2020-03-21 stsp if (list_refs_only) {
1470 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1471 41b0de12 2020-03-21 stsp goto done;
1474 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1475 39c64a6a 2020-03-18 stsp if (error)
1476 d9b4d0c0 2020-03-18 stsp goto done;
1477 68999b92 2020-03-18 stsp if (verbosity >= 0)
1478 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1479 d9b4d0c0 2020-03-18 stsp free(id_str);
1481 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1482 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1483 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1484 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1485 7ebc0570 2020-03-18 stsp char *remote_refname;
1487 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1488 0e4002ca 2020-03-21 stsp !mirror_references) {
1489 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1490 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1491 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1492 0e4002ca 2020-03-21 stsp if (error)
1493 0e4002ca 2020-03-21 stsp goto done;
1497 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1498 39c64a6a 2020-03-18 stsp if (error)
1499 d9b4d0c0 2020-03-18 stsp goto done;
1501 469dd726 2020-03-20 stsp if (mirror_references)
1504 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1507 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1508 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1509 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1510 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1511 7ebc0570 2020-03-18 stsp goto done;
1513 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1514 f298ae0f 2020-03-25 stsp free(remote_refname);
1515 39c64a6a 2020-03-18 stsp if (error)
1516 d9b4d0c0 2020-03-18 stsp goto done;
1519 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1520 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1521 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1522 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1523 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1524 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1526 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1529 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1530 39c64a6a 2020-03-18 stsp if (error) {
1531 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1532 55330abe 2020-03-20 stsp error = NULL;
1535 d9b4d0c0 2020-03-18 stsp goto done;
1538 04d9a9ec 2020-09-24 stsp error = create_symref(refname, target_ref, verbosity, repo);
1539 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1540 f298ae0f 2020-03-25 stsp if (error)
1541 f298ae0f 2020-03-25 stsp goto done;
1543 f298ae0f 2020-03-25 stsp if (mirror_references)
1546 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1549 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1550 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1551 f298ae0f 2020-03-25 stsp refname) == -1) {
1552 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1553 f298ae0f 2020-03-25 stsp goto done;
1555 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1556 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1557 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1558 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1559 f298ae0f 2020-03-25 stsp free(remote_refname);
1560 f298ae0f 2020-03-25 stsp goto done;
1562 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1563 f298ae0f 2020-03-25 stsp if (error) {
1564 f298ae0f 2020-03-25 stsp free(remote_refname);
1565 f298ae0f 2020-03-25 stsp free(remote_target);
1566 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1567 f298ae0f 2020-03-25 stsp error = NULL;
1570 f298ae0f 2020-03-25 stsp goto done;
1572 04d9a9ec 2020-09-24 stsp error = create_symref(remote_refname, target_ref,
1573 04d9a9ec 2020-09-24 stsp verbosity - 1, repo);
1574 f298ae0f 2020-03-25 stsp free(remote_refname);
1575 f298ae0f 2020-03-25 stsp free(remote_target);
1576 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1577 39c64a6a 2020-03-18 stsp if (error)
1578 d9b4d0c0 2020-03-18 stsp goto done;
1580 4ba14133 2020-03-20 stsp if (pe == NULL) {
1582 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1583 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1584 4ba14133 2020-03-20 stsp * which could be fetched instead.
1586 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1587 4ba14133 2020-03-20 stsp const char *target = pe->path;
1588 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1590 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1591 4ba14133 2020-03-20 stsp if (error) {
1592 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1593 4ba14133 2020-03-20 stsp error = NULL;
1596 4ba14133 2020-03-20 stsp goto done;
1599 04d9a9ec 2020-09-24 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1600 04d9a9ec 2020-09-24 stsp verbosity, repo);
1601 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1602 4ba14133 2020-03-20 stsp if (error)
1603 4ba14133 2020-03-20 stsp goto done;
1608 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1609 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1610 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1612 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1613 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1614 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1615 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1616 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1618 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1619 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1621 bb64b798 2020-03-18 stsp got_repo_close(repo);
1622 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1623 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1624 d9b4d0c0 2020-03-18 stsp free(pe->data);
1626 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1627 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1628 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1629 d9b4d0c0 2020-03-18 stsp free(pe->data);
1631 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1632 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1633 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1634 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1635 09838ffc 2020-03-18 stsp free(proto);
1636 09838ffc 2020-03-18 stsp free(host);
1637 09838ffc 2020-03-18 stsp free(port);
1638 09838ffc 2020-03-18 stsp free(server_path);
1639 09838ffc 2020-03-18 stsp free(repo_name);
1640 bb64b798 2020-03-18 stsp free(default_destdir);
1641 b46f3e71 2020-03-18 stsp free(git_url);
1642 39c64a6a 2020-03-18 stsp return error;
1645 7848a0e1 2020-03-19 stsp static const struct got_error *
1646 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1647 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1649 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1650 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1651 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1653 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1655 7848a0e1 2020-03-19 stsp goto done;
1657 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1658 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1659 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1661 88609724 2020-03-21 stsp goto done;
1662 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1663 88609724 2020-03-21 stsp goto done;
1664 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1665 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1666 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1668 db6d8ad8 2020-03-21 stsp goto done;
1671 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1672 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1673 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1674 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1675 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1677 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1679 7848a0e1 2020-03-19 stsp goto done;
1680 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1682 e8a967e0 2020-03-21 stsp goto done;
1684 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1686 7848a0e1 2020-03-19 stsp goto done;
1687 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1688 6338a6a1 2020-03-21 stsp goto done;
1690 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1692 6338a6a1 2020-03-21 stsp goto done;
1693 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1695 6338a6a1 2020-03-21 stsp goto done;
1698 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1699 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1700 6338a6a1 2020-03-21 stsp new_id_str);
1702 7848a0e1 2020-03-19 stsp free(old_id);
1703 7848a0e1 2020-03-19 stsp free(new_id_str);
1704 7848a0e1 2020-03-19 stsp return err;
1707 f1bcca34 2020-03-25 stsp static const struct got_error *
1708 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1709 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1711 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1712 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1713 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1715 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1716 bcf34b0e 2020-03-26 stsp if (err) {
1717 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1718 bcf34b0e 2020-03-26 stsp return err;
1719 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1721 bcf34b0e 2020-03-26 stsp goto done;
1723 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1725 bcf34b0e 2020-03-26 stsp goto done;
1727 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1728 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1729 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1730 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1732 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1734 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1735 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1736 bcf34b0e 2020-03-26 stsp goto done;
1738 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1739 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1741 bcf34b0e 2020-03-26 stsp goto done;
1743 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1745 bcf34b0e 2020-03-26 stsp goto done;
1747 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1748 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1749 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1753 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1754 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1755 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1756 bcf34b0e 2020-03-26 stsp err = unlock_err;
1758 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1759 f1bcca34 2020-03-25 stsp return err;
1762 2ab43947 2020-03-18 stsp __dead static void
1763 7848a0e1 2020-03-19 stsp usage_fetch(void)
1765 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1766 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1767 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1768 13f12b09 2020-03-21 stsp getprogname());
1772 7848a0e1 2020-03-19 stsp static const struct got_error *
1773 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1774 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1776 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1777 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1778 3789fd73 2020-03-26 stsp char *id_str = NULL;
1780 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1781 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1783 3789fd73 2020-03-26 stsp return err;
1784 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1785 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1786 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1787 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1790 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1792 3789fd73 2020-03-26 stsp return err;
1793 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1795 3789fd73 2020-03-26 stsp goto done;
1797 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1799 3789fd73 2020-03-26 stsp goto done;
1800 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1801 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1802 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1807 3789fd73 2020-03-26 stsp free(id_str);
1808 3789fd73 2020-03-26 stsp return NULL;
1811 3789fd73 2020-03-26 stsp static const struct got_error *
1812 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1813 50b0790e 2020-09-11 stsp struct got_pathlist_head *their_symrefs,
1814 50b0790e 2020-09-11 stsp const struct got_remote_repo *remote,
1815 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1817 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1818 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1819 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1820 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1821 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1822 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1824 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1826 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1828 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1830 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1832 3789fd73 2020-03-26 stsp goto done;
1834 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1835 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1837 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1838 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1839 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1840 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1841 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1843 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1844 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1845 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1846 3789fd73 2020-03-26 stsp goto done;
1848 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1852 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1853 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1856 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1859 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1860 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1863 3789fd73 2020-03-26 stsp if (pe != NULL)
1866 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1870 3789fd73 2020-03-26 stsp if (local_refname) {
1871 3789fd73 2020-03-26 stsp struct got_reference *ref;
1872 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1873 3789fd73 2020-03-26 stsp if (err) {
1874 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1876 3789fd73 2020-03-26 stsp free(local_refname);
1877 3789fd73 2020-03-26 stsp local_refname = NULL;
1880 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1883 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1884 3789fd73 2020-03-26 stsp got_ref_close(ref);
1885 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1886 3789fd73 2020-03-26 stsp err = unlock_err;
1890 3789fd73 2020-03-26 stsp free(local_refname);
1891 3789fd73 2020-03-26 stsp local_refname = NULL;
1895 3789fd73 2020-03-26 stsp free(remote_namespace);
1896 3789fd73 2020-03-26 stsp free(local_refname);
1897 0e4002ca 2020-03-21 stsp return err;
1900 0e4002ca 2020-03-21 stsp static const struct got_error *
1901 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1902 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1904 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1905 0e4002ca 2020-03-21 stsp char *remote_refname;
1906 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1908 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1909 0e4002ca 2020-03-21 stsp refname += 5;
1911 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1912 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1913 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1915 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1916 0e4002ca 2020-03-21 stsp if (err) {
1917 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1918 0e4002ca 2020-03-21 stsp goto done;
1919 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1921 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1922 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1923 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1924 9f142382 2020-03-21 stsp err = unlock_err;
1925 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1928 0e4002ca 2020-03-21 stsp free(remote_refname);
1929 f21ec2f0 2020-03-21 stsp return err;
1932 f21ec2f0 2020-03-21 stsp static const struct got_error *
1933 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1935 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1936 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1937 7848a0e1 2020-03-19 stsp const char *remote_name;
1938 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1939 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1940 50b0790e 2020-09-11 stsp const struct got_remote_repo *remotes, *remote = NULL;
1941 7848a0e1 2020-03-19 stsp int nremotes;
1942 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1943 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1944 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1945 50b0790e 2020-09-11 stsp const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1946 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1947 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1948 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1949 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1950 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1951 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1952 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1953 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1955 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1956 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1957 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1958 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1960 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1961 7848a0e1 2020-03-19 stsp switch (ch) {
1963 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1966 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1967 4ba14133 2020-03-20 stsp optarg, NULL);
1968 4ba14133 2020-03-20 stsp if (error)
1969 4ba14133 2020-03-20 stsp return error;
1972 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1975 41b0de12 2020-03-21 stsp list_refs_only = 1;
1978 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1979 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1980 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1982 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1985 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1988 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1989 7848a0e1 2020-03-19 stsp verbosity = 0;
1990 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1991 7848a0e1 2020-03-19 stsp verbosity++;
1994 7848a0e1 2020-03-19 stsp verbosity = -1;
1997 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1998 0e4002ca 2020-03-21 stsp optarg, NULL);
1999 0e4002ca 2020-03-21 stsp if (error)
2000 0e4002ca 2020-03-21 stsp return error;
2003 7848a0e1 2020-03-19 stsp usage_fetch();
2007 7848a0e1 2020-03-19 stsp argc -= optind;
2008 7848a0e1 2020-03-19 stsp argv += optind;
2010 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2011 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
2012 41b0de12 2020-03-21 stsp if (list_refs_only) {
2013 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
2014 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
2015 41b0de12 2020-03-21 stsp if (fetch_all_branches)
2016 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
2017 f21ec2f0 2020-03-21 stsp if (delete_refs)
2018 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
2019 41b0de12 2020-03-21 stsp if (verbosity == -1)
2020 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
2023 7848a0e1 2020-03-19 stsp if (argc == 0)
2024 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2025 7848a0e1 2020-03-19 stsp else if (argc == 1)
2026 7848a0e1 2020-03-19 stsp remote_name = argv[0];
2028 7848a0e1 2020-03-19 stsp usage_fetch();
2030 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
2031 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
2032 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
2033 7848a0e1 2020-03-19 stsp goto done;
2036 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2037 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
2038 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2039 7848a0e1 2020-03-19 stsp goto done;
2041 7848a0e1 2020-03-19 stsp error = NULL;
2042 7848a0e1 2020-03-19 stsp if (worktree) {
2043 7848a0e1 2020-03-19 stsp repo_path =
2044 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
2045 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
2046 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2047 7848a0e1 2020-03-19 stsp if (error)
2048 7848a0e1 2020-03-19 stsp goto done;
2050 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
2051 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
2052 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
2053 7848a0e1 2020-03-19 stsp goto done;
2058 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
2059 7848a0e1 2020-03-19 stsp if (error)
2060 7848a0e1 2020-03-19 stsp goto done;
2062 50b0790e 2020-09-11 stsp if (worktree) {
2063 50b0790e 2020-09-11 stsp worktree_conf = got_worktree_get_gotconfig(worktree);
2064 50b0790e 2020-09-11 stsp if (worktree_conf) {
2065 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2066 50b0790e 2020-09-11 stsp worktree_conf);
2067 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2068 50b0790e 2020-09-11 stsp remote = &remotes[i];
2069 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2074 50b0790e 2020-09-11 stsp if (remote == NULL) {
2075 50b0790e 2020-09-11 stsp repo_conf = got_repo_get_gotconfig(repo);
2076 50b0790e 2020-09-11 stsp if (repo_conf) {
2077 50b0790e 2020-09-11 stsp got_gotconfig_get_remotes(&nremotes, &remotes,
2078 50b0790e 2020-09-11 stsp repo_conf);
2079 50b0790e 2020-09-11 stsp for (i = 0; i < nremotes; i++) {
2080 50b0790e 2020-09-11 stsp remote = &remotes[i];
2081 50b0790e 2020-09-11 stsp if (strcmp(remote->name, remote_name) == 0)
2086 50b0790e 2020-09-11 stsp if (remote == NULL) {
2087 257add31 2020-09-09 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2088 257add31 2020-09-09 stsp for (i = 0; i < nremotes; i++) {
2089 257add31 2020-09-09 stsp remote = &remotes[i];
2090 257add31 2020-09-09 stsp if (strcmp(remote->name, remote_name) == 0)
2094 50b0790e 2020-09-11 stsp if (remote == NULL) {
2095 50b0790e 2020-09-11 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2096 50b0790e 2020-09-11 stsp goto done;
2099 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2100 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
2101 7848a0e1 2020-03-19 stsp if (error)
2102 7848a0e1 2020-03-19 stsp goto done;
2104 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
2105 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2106 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2107 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
2108 7848a0e1 2020-03-19 stsp err(1, "pledge");
2110 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
2111 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
2112 7848a0e1 2020-03-19 stsp #ifndef PROFILE
2113 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2114 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
2115 7848a0e1 2020-03-19 stsp err(1, "pledge");
2117 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
2118 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
2119 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2120 7848a0e1 2020-03-19 stsp goto done;
2122 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2123 7848a0e1 2020-03-19 stsp goto done;
2126 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2127 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2128 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
2129 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
2130 7848a0e1 2020-03-19 stsp goto done;
2133 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2134 7848a0e1 2020-03-19 stsp if (error)
2135 7848a0e1 2020-03-19 stsp goto done;
2137 f79e6490 2020-04-19 stsp if (verbosity >= 0)
2138 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2139 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
2141 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2142 9c52365f 2020-03-21 stsp server_path, verbosity);
2143 7848a0e1 2020-03-19 stsp if (error)
2144 7848a0e1 2020-03-19 stsp goto done;
2146 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
2147 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
2148 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
2149 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
2150 04d9a9ec 2020-09-24 stsp fpa.repo = repo;
2151 04d9a9ec 2020-09-24 stsp fpa.create_configs = 0;
2152 04d9a9ec 2020-09-24 stsp fpa.configs_created = 0;
2153 04d9a9ec 2020-09-24 stsp memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2154 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2155 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
2156 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2157 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
2158 7848a0e1 2020-03-19 stsp if (error)
2159 7848a0e1 2020-03-19 stsp goto done;
2161 41b0de12 2020-03-21 stsp if (list_refs_only) {
2162 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
2163 41b0de12 2020-03-21 stsp goto done;
2166 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
2167 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
2168 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
2169 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
2170 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
2171 984065c8 2020-03-19 stsp if (error)
2172 984065c8 2020-03-19 stsp goto done;
2173 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
2174 984065c8 2020-03-19 stsp free(id_str);
2175 984065c8 2020-03-19 stsp id_str = NULL;
2178 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
2179 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2180 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
2181 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
2182 7848a0e1 2020-03-19 stsp struct got_reference *ref;
2183 7848a0e1 2020-03-19 stsp char *remote_refname;
2185 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
2186 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
2187 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
2188 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
2189 0e4002ca 2020-03-21 stsp if (error)
2190 0e4002ca 2020-03-21 stsp goto done;
2194 1510c839 2020-03-20 stsp if (remote->mirror_references ||
2195 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
2196 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2197 7848a0e1 2020-03-19 stsp if (error) {
2198 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2199 7848a0e1 2020-03-19 stsp goto done;
2200 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2202 7848a0e1 2020-03-19 stsp if (error)
2203 7848a0e1 2020-03-19 stsp goto done;
2205 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2206 db6d8ad8 2020-03-21 stsp verbosity, repo);
2207 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2208 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2209 9f142382 2020-03-21 stsp error = unlock_err;
2210 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2211 7848a0e1 2020-03-19 stsp if (error)
2212 7848a0e1 2020-03-19 stsp goto done;
2214 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
2215 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2216 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
2217 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
2218 7848a0e1 2020-03-19 stsp goto done;
2221 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
2222 7848a0e1 2020-03-19 stsp if (error) {
2223 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
2224 7848a0e1 2020-03-19 stsp goto done;
2225 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
2226 688f11b3 2020-03-21 stsp verbosity, repo);
2227 7848a0e1 2020-03-19 stsp if (error)
2228 7848a0e1 2020-03-19 stsp goto done;
2230 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
2231 db6d8ad8 2020-03-21 stsp verbosity, repo);
2232 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2233 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2234 9f142382 2020-03-21 stsp error = unlock_err;
2235 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2236 7848a0e1 2020-03-19 stsp if (error)
2237 7848a0e1 2020-03-19 stsp goto done;
2240 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2241 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2242 2ec30c80 2020-03-20 stsp if (error) {
2243 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2244 2ec30c80 2020-03-20 stsp goto done;
2245 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2247 2ec30c80 2020-03-20 stsp if (error)
2248 2ec30c80 2020-03-20 stsp goto done;
2250 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2251 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2252 9f142382 2020-03-21 stsp error = unlock_err;
2253 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2257 f1bcca34 2020-03-25 stsp if (delete_refs) {
2258 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2259 3789fd73 2020-03-26 stsp verbosity, repo);
2260 f1bcca34 2020-03-25 stsp if (error)
2261 f1bcca34 2020-03-25 stsp goto done;
2264 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2265 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2266 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2267 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2268 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2269 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2270 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2272 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2275 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2278 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2279 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2280 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2281 f1bcca34 2020-03-25 stsp goto done;
2283 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2284 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2285 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2286 f1bcca34 2020-03-25 stsp free(remote_refname);
2287 f1bcca34 2020-03-25 stsp goto done;
2290 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2292 f1bcca34 2020-03-25 stsp if (error) {
2293 f1bcca34 2020-03-25 stsp free(remote_refname);
2294 f1bcca34 2020-03-25 stsp free(remote_target);
2295 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2296 f1bcca34 2020-03-25 stsp error = NULL;
2299 f1bcca34 2020-03-25 stsp goto done;
2301 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2302 f1bcca34 2020-03-25 stsp verbosity, repo);
2303 f1bcca34 2020-03-25 stsp free(remote_refname);
2304 f1bcca34 2020-03-25 stsp free(remote_target);
2305 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2306 f1bcca34 2020-03-25 stsp if (error)
2307 f1bcca34 2020-03-25 stsp goto done;
2311 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2312 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2313 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2314 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2315 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2317 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2318 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2320 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2321 7848a0e1 2020-03-19 stsp if (worktree)
2322 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2323 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2324 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2325 7848a0e1 2020-03-19 stsp free(pe->data);
2327 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2328 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2329 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2330 7848a0e1 2020-03-19 stsp free(pe->data);
2332 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2333 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2334 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2335 7848a0e1 2020-03-19 stsp free(id_str);
2336 7848a0e1 2020-03-19 stsp free(cwd);
2337 7848a0e1 2020-03-19 stsp free(repo_path);
2338 7848a0e1 2020-03-19 stsp free(pack_hash);
2339 7848a0e1 2020-03-19 stsp free(proto);
2340 7848a0e1 2020-03-19 stsp free(host);
2341 7848a0e1 2020-03-19 stsp free(port);
2342 7848a0e1 2020-03-19 stsp free(server_path);
2343 7848a0e1 2020-03-19 stsp free(repo_name);
2344 7848a0e1 2020-03-19 stsp return error;
2348 7848a0e1 2020-03-19 stsp __dead static void
2349 2ab43947 2020-03-18 stsp usage_checkout(void)
2351 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2352 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2356 2ab43947 2020-03-18 stsp static void
2357 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2359 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2360 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2361 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2362 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2363 2ab43947 2020-03-18 stsp getprogname());
2366 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2367 2ab43947 2020-03-18 stsp const char *worktree_path;
2368 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2371 2ab43947 2020-03-18 stsp static const struct got_error *
2372 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2374 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2376 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2377 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2378 2ab43947 2020-03-18 stsp return NULL;
2380 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2381 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2382 2ab43947 2020-03-18 stsp return NULL;
2385 2ab43947 2020-03-18 stsp while (path[0] == '/')
2388 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2389 2ab43947 2020-03-18 stsp return NULL;
2392 2ab43947 2020-03-18 stsp static const struct got_error *
2393 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2395 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2396 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2397 2ab43947 2020-03-18 stsp return NULL;
2400 2ab43947 2020-03-18 stsp static const struct got_error *
2401 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2402 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2403 2ab43947 2020-03-18 stsp struct got_repository *repo)
2405 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2406 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2408 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2409 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2411 2ab43947 2020-03-18 stsp return err;
2413 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2414 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2417 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2418 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2420 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2422 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2430 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2431 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2432 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2434 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2435 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2436 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2437 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2438 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2439 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2441 2ab43947 2020-03-18 stsp free(yca_id);
2442 2ab43947 2020-03-18 stsp return NULL;
2445 2ab43947 2020-03-18 stsp static const struct got_error *
2446 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2447 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2448 2ab43947 2020-03-18 stsp struct got_repository *repo)
2450 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2451 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2452 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2453 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2455 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2457 2ab43947 2020-03-18 stsp goto done;
2459 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2460 2ab43947 2020-03-18 stsp is_same_branch = 1;
2461 2ab43947 2020-03-18 stsp goto done;
2463 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2464 2ab43947 2020-03-18 stsp is_same_branch = 1;
2465 2ab43947 2020-03-18 stsp goto done;
2468 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2470 2ab43947 2020-03-18 stsp goto done;
2472 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2473 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2475 2ab43947 2020-03-18 stsp goto done;
2477 2ab43947 2020-03-18 stsp for (;;) {
2478 2ab43947 2020-03-18 stsp struct got_object_id *id;
2479 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2480 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2481 2ab43947 2020-03-18 stsp if (err) {
2482 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2483 2ab43947 2020-03-18 stsp err = NULL;
2488 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2490 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2491 2ab43947 2020-03-18 stsp is_same_branch = 1;
2497 2ab43947 2020-03-18 stsp if (graph)
2498 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2499 2ab43947 2020-03-18 stsp free(head_commit_id);
2500 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2501 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2502 2ab43947 2020-03-18 stsp return err;
2505 4ed7e80c 2018-05-20 stsp static const struct got_error *
2506 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2508 4b6c9460 2020-03-05 stsp static char msg[512];
2509 4b6c9460 2020-03-05 stsp const char *branch_name;
2511 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2512 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2514 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2516 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2517 4b6c9460 2020-03-05 stsp branch_name += 11;
2519 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2520 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2521 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2522 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2523 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2524 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2526 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2529 4b6c9460 2020-03-05 stsp static const struct got_error *
2530 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2532 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2533 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2534 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2535 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2536 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2537 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2538 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2539 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2540 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2541 c47340da 2020-09-20 stsp char *cwd = NULL, *path = NULL;
2542 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2543 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2544 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2546 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2548 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2549 0bb8a95e 2018-03-12 stsp switch (ch) {
2551 08573d5b 2019-05-14 stsp branch_name = optarg;
2554 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2555 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2556 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2559 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2562 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2565 2deda0b9 2019-03-07 stsp usage_checkout();
2566 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2570 0bb8a95e 2018-03-12 stsp argc -= optind;
2571 0bb8a95e 2018-03-12 stsp argv += optind;
2573 6715a751 2018-03-16 stsp #ifndef PROFILE
2574 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2575 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2576 c09a553d 2018-03-12 stsp err(1, "pledge");
2578 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2579 c47340da 2020-09-20 stsp char *base, *dotgit;
2580 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2581 76089277 2018-04-01 stsp if (repo_path == NULL)
2582 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2583 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2584 76089277 2018-04-01 stsp if (cwd == NULL) {
2585 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2586 76089277 2018-04-01 stsp goto done;
2588 c47340da 2020-09-20 stsp if (path_prefix[0])
2589 c47340da 2020-09-20 stsp path = strdup(path_prefix);
2591 c47340da 2020-09-20 stsp path = strdup(repo_path);
2592 c47340da 2020-09-20 stsp if (path == NULL) {
2593 c47340da 2020-09-20 stsp error = got_error_from_errno("strdup");
2594 c47340da 2020-09-20 stsp goto done;
2596 c47340da 2020-09-20 stsp base = basename(path);
2597 c47340da 2020-09-20 stsp if (base == NULL) {
2598 c47340da 2020-09-20 stsp error = got_error_from_errno2("basename", path);
2599 c47340da 2020-09-20 stsp goto done;
2601 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2602 c09a553d 2018-03-12 stsp if (dotgit)
2603 c09a553d 2018-03-12 stsp *dotgit = '\0';
2604 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2605 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2606 76089277 2018-04-01 stsp goto done;
2608 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2609 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2610 76089277 2018-04-01 stsp if (repo_path == NULL) {
2611 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2612 76089277 2018-04-01 stsp goto done;
2614 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2615 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2616 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2617 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2619 b4b3a7dd 2019-07-22 stsp goto done;
2621 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2622 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2623 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2624 b4b3a7dd 2019-07-22 stsp goto done;
2628 c09a553d 2018-03-12 stsp usage_checkout();
2630 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2631 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2633 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2634 c09a553d 2018-03-12 stsp if (error != NULL)
2635 c02c541e 2019-03-29 stsp goto done;
2637 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2638 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2639 c530dc23 2019-07-23 stsp if (error) {
2640 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2641 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2642 c530dc23 2019-07-23 stsp goto done;
2643 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2644 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2645 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2646 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2647 c530dc23 2019-07-23 stsp goto done;
2651 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2652 c02c541e 2019-03-29 stsp if (error)
2653 c09a553d 2018-03-12 stsp goto done;
2655 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2656 c09a553d 2018-03-12 stsp if (error != NULL)
2657 c09a553d 2018-03-12 stsp goto done;
2659 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2660 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2661 c09a553d 2018-03-12 stsp goto done;
2663 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2664 c09a553d 2018-03-12 stsp if (error != NULL)
2665 c09a553d 2018-03-12 stsp goto done;
2667 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2668 e5dc7198 2018-12-29 stsp path_prefix);
2669 e5dc7198 2018-12-29 stsp if (error != NULL)
2670 e5dc7198 2018-12-29 stsp goto done;
2671 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2672 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2673 49520a32 2018-12-29 stsp goto done;
2676 8069f636 2019-01-12 stsp if (commit_id_str) {
2677 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2678 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2679 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2680 30837e32 2019-07-25 stsp if (error)
2681 8069f636 2019-01-12 stsp goto done;
2682 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2683 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2684 8069f636 2019-01-12 stsp if (error != NULL) {
2685 8069f636 2019-01-12 stsp free(commit_id);
2686 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2687 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2688 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2690 8069f636 2019-01-12 stsp goto done;
2692 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2693 4b6c9460 2020-03-05 stsp if (error) {
2694 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2695 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2696 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2698 45d344f6 2019-05-14 stsp goto done;
2700 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2701 8069f636 2019-01-12 stsp commit_id);
2702 8069f636 2019-01-12 stsp free(commit_id);
2703 8069f636 2019-01-12 stsp if (error)
2704 8069f636 2019-01-12 stsp goto done;
2707 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2708 f2ea84fa 2019-07-27 stsp if (error)
2709 f2ea84fa 2019-07-27 stsp goto done;
2710 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2711 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2712 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2713 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2714 c09a553d 2018-03-12 stsp if (error != NULL)
2715 c09a553d 2018-03-12 stsp goto done;
2717 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2718 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2719 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2721 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2722 8069f636 2019-01-12 stsp free(commit_id_str);
2723 76089277 2018-04-01 stsp free(repo_path);
2724 507dc3bb 2018-12-29 stsp free(worktree_path);
2725 c47340da 2020-09-20 stsp free(cwd);
2726 c47340da 2020-09-20 stsp free(path);
2727 507dc3bb 2018-12-29 stsp return error;
2730 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2731 9627c110 2020-04-18 stsp int did_something;
2732 9627c110 2020-04-18 stsp int conflicts;
2733 9627c110 2020-04-18 stsp int obstructed;
2734 9627c110 2020-04-18 stsp int not_updated;
2738 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2740 9627c110 2020-04-18 stsp if (!upa->did_something)
2743 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2744 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2745 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2746 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2747 9627c110 2020-04-18 stsp upa->obstructed);
2748 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2749 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2750 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2753 507dc3bb 2018-12-29 stsp __dead static void
2754 507dc3bb 2018-12-29 stsp usage_update(void)
2756 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2757 507dc3bb 2018-12-29 stsp getprogname());
2761 1ee397ad 2019-07-12 stsp static const struct got_error *
2762 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2764 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2766 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2767 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2768 1ee397ad 2019-07-12 stsp return NULL;
2770 9627c110 2020-04-18 stsp upa->did_something = 1;
2772 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2773 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2774 1ee397ad 2019-07-12 stsp return NULL;
2776 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2777 9627c110 2020-04-18 stsp upa->conflicts++;
2778 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2779 9627c110 2020-04-18 stsp upa->obstructed++;
2780 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2781 9627c110 2020-04-18 stsp upa->not_updated++;
2783 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2785 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2786 1ee397ad 2019-07-12 stsp return NULL;
2789 be7061eb 2018-12-30 stsp static const struct got_error *
2790 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2791 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2792 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2794 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2795 a1fb16d8 2019-05-24 stsp char *base_id_str;
2796 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2798 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2799 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2800 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2801 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2802 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2803 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2804 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2807 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2808 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2809 a1fb16d8 2019-05-24 stsp if (err) {
2810 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2811 a1fb16d8 2019-05-24 stsp return err;
2812 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2814 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2815 a1fb16d8 2019-05-24 stsp return NULL;
2817 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2818 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2819 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2821 a1fb16d8 2019-05-24 stsp return err;
2822 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2823 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2824 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2825 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2826 0ebf8283 2019-07-24 stsp return NULL;
2829 0ebf8283 2019-07-24 stsp static const struct got_error *
2830 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2832 0ebf8283 2019-07-24 stsp const struct got_error *err;
2833 0ebf8283 2019-07-24 stsp int in_progress;
2835 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2837 0ebf8283 2019-07-24 stsp return err;
2838 0ebf8283 2019-07-24 stsp if (in_progress)
2839 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2841 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2843 0ebf8283 2019-07-24 stsp return err;
2844 0ebf8283 2019-07-24 stsp if (in_progress)
2845 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2847 a1fb16d8 2019-05-24 stsp return NULL;
2850 a5edda0a 2019-07-27 stsp static const struct got_error *
2851 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2852 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2854 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2855 a5edda0a 2019-07-27 stsp char *path;
2858 a5edda0a 2019-07-27 stsp if (argc == 0) {
2859 a5edda0a 2019-07-27 stsp path = strdup("");
2860 a5edda0a 2019-07-27 stsp if (path == NULL)
2861 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2862 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2865 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2866 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2869 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2870 a5edda0a 2019-07-27 stsp if (err) {
2871 a5edda0a 2019-07-27 stsp free(path);
2876 a5edda0a 2019-07-27 stsp return err;
2879 a1fb16d8 2019-05-24 stsp static const struct got_error *
2880 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2881 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2883 fa51e947 2020-03-27 stsp const struct got_error *err;
2884 fa51e947 2020-03-27 stsp struct got_repository *repo;
2885 fa51e947 2020-03-27 stsp static char msg[512];
2887 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2889 fa51e947 2020-03-27 stsp return orig_err;
2891 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2892 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2893 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2894 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2895 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2896 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2897 fa51e947 2020-03-27 stsp got_repo_close(repo);
2898 fa51e947 2020-03-27 stsp return err;
2901 fa51e947 2020-03-27 stsp static const struct got_error *
2902 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2904 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2905 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2906 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2907 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2908 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2909 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2910 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2911 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2912 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2913 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2915 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2917 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2919 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2920 507dc3bb 2018-12-29 stsp switch (ch) {
2922 024e9686 2019-05-14 stsp branch_name = optarg;
2925 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2926 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2927 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2930 2deda0b9 2019-03-07 stsp usage_update();
2931 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2935 507dc3bb 2018-12-29 stsp argc -= optind;
2936 507dc3bb 2018-12-29 stsp argv += optind;
2938 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2939 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2940 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2941 507dc3bb 2018-12-29 stsp err(1, "pledge");
2943 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2944 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2945 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2946 c4cdcb68 2019-04-03 stsp goto done;
2948 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2949 fa51e947 2020-03-27 stsp if (error) {
2950 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2951 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2952 fa51e947 2020-03-27 stsp worktree_path);
2953 7d5807f4 2019-07-11 stsp goto done;
2956 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2957 f2ea84fa 2019-07-27 stsp if (error)
2958 f2ea84fa 2019-07-27 stsp goto done;
2960 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2962 507dc3bb 2018-12-29 stsp if (error != NULL)
2963 507dc3bb 2018-12-29 stsp goto done;
2965 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2966 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2967 087fb88c 2019-08-04 stsp if (error)
2968 087fb88c 2019-08-04 stsp goto done;
2970 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2971 0266afb7 2019-01-04 stsp if (error)
2972 0266afb7 2019-01-04 stsp goto done;
2974 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2975 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2976 024e9686 2019-05-14 stsp if (error != NULL)
2977 024e9686 2019-05-14 stsp goto done;
2978 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2979 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2980 9c4b8182 2019-01-02 stsp if (error != NULL)
2981 9c4b8182 2019-01-02 stsp goto done;
2982 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2983 507dc3bb 2018-12-29 stsp if (error != NULL)
2984 507dc3bb 2018-12-29 stsp goto done;
2986 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2987 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2988 04f57cb3 2019-07-25 stsp free(commit_id_str);
2989 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2990 30837e32 2019-07-25 stsp if (error)
2991 a297e751 2019-07-11 stsp goto done;
2992 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2993 a297e751 2019-07-11 stsp if (error)
2994 507dc3bb 2018-12-29 stsp goto done;
2997 a1fb16d8 2019-05-24 stsp if (branch_name) {
2998 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2999 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
3000 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
3002 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
3003 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
3004 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
3005 024e9686 2019-05-14 stsp goto done;
3007 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
3008 024e9686 2019-05-14 stsp if (error)
3009 024e9686 2019-05-14 stsp goto done;
3010 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
3012 a367ff0f 2019-05-14 stsp free(head_commit_id);
3013 024e9686 2019-05-14 stsp if (error != NULL)
3014 024e9686 2019-05-14 stsp goto done;
3015 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3016 a367ff0f 2019-05-14 stsp if (error)
3017 a367ff0f 2019-05-14 stsp goto done;
3018 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
3019 024e9686 2019-05-14 stsp if (error)
3020 024e9686 2019-05-14 stsp goto done;
3022 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
3023 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
3024 a367ff0f 2019-05-14 stsp if (error != NULL) {
3025 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
3026 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
3027 024e9686 2019-05-14 stsp goto done;
3029 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3030 a367ff0f 2019-05-14 stsp if (error)
3031 a367ff0f 2019-05-14 stsp goto done;
3034 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3035 507dc3bb 2018-12-29 stsp commit_id) != 0) {
3036 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3037 507dc3bb 2018-12-29 stsp commit_id);
3038 507dc3bb 2018-12-29 stsp if (error)
3039 507dc3bb 2018-12-29 stsp goto done;
3042 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
3043 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
3044 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
3045 507dc3bb 2018-12-29 stsp if (error != NULL)
3046 507dc3bb 2018-12-29 stsp goto done;
3048 9627c110 2020-04-18 stsp if (upa.did_something)
3049 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
3051 784955db 2019-01-12 stsp printf("Already up-to-date\n");
3052 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
3054 c09a553d 2018-03-12 stsp free(worktree_path);
3055 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3056 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
3057 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
3058 507dc3bb 2018-12-29 stsp free(commit_id);
3059 9c4b8182 2019-01-02 stsp free(commit_id_str);
3060 c09a553d 2018-03-12 stsp return error;
3063 f42b1b34 2018-03-12 stsp static const struct got_error *
3064 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3065 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3066 63035f9f 2019-10-06 stsp struct got_repository *repo)
3068 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
3069 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3071 44392932 2019-08-25 stsp if (blob_id1) {
3072 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
3074 44392932 2019-08-25 stsp goto done;
3077 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3079 44392932 2019-08-25 stsp goto done;
3081 44392932 2019-08-25 stsp while (path[0] == '/')
3083 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
3084 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
3086 44392932 2019-08-25 stsp if (blob1)
3087 44392932 2019-08-25 stsp got_object_blob_close(blob1);
3088 44392932 2019-08-25 stsp got_object_blob_close(blob2);
3089 44392932 2019-08-25 stsp return err;
3092 44392932 2019-08-25 stsp static const struct got_error *
3093 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3094 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
3095 63035f9f 2019-10-06 stsp struct got_repository *repo)
3097 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3098 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3099 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
3101 44392932 2019-08-25 stsp if (tree_id1) {
3102 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3104 44392932 2019-08-25 stsp goto done;
3107 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3109 0f2b3dca 2018-12-22 stsp goto done;
3111 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
3112 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3113 aaa13589 2019-06-01 stsp arg.outfile = stdout;
3114 44392932 2019-08-25 stsp while (path[0] == '/')
3116 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
3117 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
3119 79109fed 2018-03-27 stsp if (tree1)
3120 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
3121 366e0a5f 2019-10-10 stsp if (tree2)
3122 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
3123 44392932 2019-08-25 stsp return err;
3126 44392932 2019-08-25 stsp static const struct got_error *
3127 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
3128 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
3130 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
3131 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3132 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3133 0208f208 2020-05-05 stsp struct got_object_qid *qid;
3135 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3136 0208f208 2020-05-05 stsp if (qid != NULL) {
3137 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
3138 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
3141 0208f208 2020-05-05 stsp return err;
3143 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
3144 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
3148 0208f208 2020-05-05 stsp if (tree_id1) {
3149 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
3151 0208f208 2020-05-05 stsp goto done;
3154 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
3155 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
3157 0208f208 2020-05-05 stsp goto done;
3159 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3160 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
3162 0208f208 2020-05-05 stsp if (tree1)
3163 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
3164 0208f208 2020-05-05 stsp if (tree2)
3165 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
3166 0208f208 2020-05-05 stsp return err;
3169 0208f208 2020-05-05 stsp static const struct got_error *
3170 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
3171 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
3173 44392932 2019-08-25 stsp const struct got_error *err = NULL;
3174 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
3175 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
3176 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3177 44392932 2019-08-25 stsp struct got_object_qid *qid;
3179 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3180 44392932 2019-08-25 stsp if (qid != NULL) {
3181 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
3184 44392932 2019-08-25 stsp return err;
3187 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
3188 44392932 2019-08-25 stsp int obj_type;
3189 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
3191 44392932 2019-08-25 stsp goto done;
3192 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3193 44392932 2019-08-25 stsp if (err) {
3194 44392932 2019-08-25 stsp free(obj_id2);
3195 44392932 2019-08-25 stsp goto done;
3197 44392932 2019-08-25 stsp if (pcommit) {
3198 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
3199 44392932 2019-08-25 stsp qid->id, path);
3200 44392932 2019-08-25 stsp if (err) {
3201 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3202 2e8c69d1 2020-05-04 stsp free(obj_id2);
3203 2e8c69d1 2020-05-04 stsp goto done;
3206 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
3207 2e8c69d1 2020-05-04 stsp if (err) {
3208 2e8c69d1 2020-05-04 stsp free(obj_id2);
3209 2e8c69d1 2020-05-04 stsp goto done;
3213 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
3214 44392932 2019-08-25 stsp if (err) {
3215 44392932 2019-08-25 stsp free(obj_id2);
3216 44392932 2019-08-25 stsp goto done;
3218 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3219 44392932 2019-08-25 stsp switch (obj_type) {
3220 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
3221 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3224 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
3225 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
3229 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
3232 44392932 2019-08-25 stsp free(obj_id1);
3233 44392932 2019-08-25 stsp free(obj_id2);
3235 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3236 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3238 44392932 2019-08-25 stsp goto done;
3239 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3240 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3242 44392932 2019-08-25 stsp goto done;
3243 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3244 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3247 0f2b3dca 2018-12-22 stsp free(id_str1);
3248 0f2b3dca 2018-12-22 stsp free(id_str2);
3249 44392932 2019-08-25 stsp if (pcommit)
3250 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3251 79109fed 2018-03-27 stsp return err;
3254 4bb494d5 2018-06-16 stsp static char *
3255 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3257 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3258 09867e48 2019-08-13 stsp char *p, *s;
3260 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3261 09867e48 2019-08-13 stsp if (tm == NULL)
3262 09867e48 2019-08-13 stsp return NULL;
3263 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3264 09867e48 2019-08-13 stsp if (s == NULL)
3265 09867e48 2019-08-13 stsp return NULL;
3266 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3268 6c281f94 2018-06-11 stsp *p = '\0';
3272 6841bf13 2019-11-29 kn static const struct got_error *
3273 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3274 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3276 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3277 6841bf13 2019-11-29 kn regmatch_t regmatch;
3278 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3280 6841bf13 2019-11-29 kn *have_match = 0;
3282 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3286 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3290 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3291 6841bf13 2019-11-29 kn *have_match = 1;
3293 6841bf13 2019-11-29 kn free(id_str);
3294 6841bf13 2019-11-29 kn free(logmsg);
3298 0208f208 2020-05-05 stsp static void
3299 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3300 0208f208 2020-05-05 stsp regex_t *regex)
3302 0208f208 2020-05-05 stsp regmatch_t regmatch;
3303 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3305 0208f208 2020-05-05 stsp *have_match = 0;
3307 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3308 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3309 0208f208 2020-05-05 stsp *have_match = 1;
3315 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3317 79109fed 2018-03-27 stsp static const struct got_error *
3318 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3319 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3320 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3321 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3323 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3324 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3325 6c281f94 2018-06-11 stsp char datebuf[26];
3326 45d799e2 2018-12-23 stsp time_t committer_time;
3327 45d799e2 2018-12-23 stsp const char *author, *committer;
3328 199a4027 2019-02-02 stsp char *refs_str = NULL;
3329 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3331 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3333 199a4027 2019-02-02 stsp const char *name;
3334 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3335 48cae60d 2020-09-22 stsp struct got_object_id *ref_id;
3338 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3339 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3341 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3342 199a4027 2019-02-02 stsp name += 5;
3343 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3345 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3346 e34f9ed6 2019-02-02 stsp name += 6;
3347 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3348 141c2bff 2019-02-04 stsp name += 8;
3349 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3350 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3353 48cae60d 2020-09-22 stsp err = got_ref_resolve(&ref_id, repo, re->ref);
3355 48cae60d 2020-09-22 stsp return err;
3356 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3357 48cae60d 2020-09-22 stsp err = got_object_open_as_tag(&tag, repo, ref_id);
3358 5d844a1e 2019-08-13 stsp if (err) {
3359 48cae60d 2020-09-22 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3360 48cae60d 2020-09-22 stsp free(ref_id);
3361 5d844a1e 2019-08-13 stsp return err;
3363 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3364 5d844a1e 2019-08-13 stsp err = NULL;
3365 5d844a1e 2019-08-13 stsp tag = NULL;
3368 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3369 48cae60d 2020-09-22 stsp got_object_tag_get_object_id(tag) : ref_id, id);
3370 48cae60d 2020-09-22 stsp free(ref_id);
3372 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3373 a436ad14 2019-08-13 stsp if (cmp != 0)
3375 199a4027 2019-02-02 stsp s = refs_str;
3376 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3377 199a4027 2019-02-02 stsp name) == -1) {
3378 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3380 34ca4898 2019-08-13 stsp return err;
3384 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3386 8bf5b3c9 2018-03-17 stsp return err;
3388 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3389 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3390 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3391 832c249c 2018-06-10 stsp free(id_str);
3392 d3d493d7 2019-02-21 stsp id_str = NULL;
3393 d3d493d7 2019-02-21 stsp free(refs_str);
3394 d3d493d7 2019-02-21 stsp refs_str = NULL;
3395 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3396 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3397 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3398 09867e48 2019-08-13 stsp if (datestr)
3399 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3400 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3401 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3402 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3403 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3404 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3405 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3406 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3407 3fe1abad 2018-06-10 stsp int n = 1;
3408 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3409 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3410 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3412 a0603db2 2018-06-10 stsp return err;
3413 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3414 a0603db2 2018-06-10 stsp free(id_str);
3418 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3420 5943eee2 2019-08-13 stsp return err;
3422 621015ac 2018-07-23 stsp logmsg = logmsg0;
3424 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3426 832c249c 2018-06-10 stsp printf(" %s\n", line);
3427 832c249c 2018-06-10 stsp } while (line);
3428 621015ac 2018-07-23 stsp free(logmsg0);
3430 0208f208 2020-05-05 stsp if (changed_paths) {
3431 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3432 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3433 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3434 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3436 0208f208 2020-05-05 stsp printf("\n");
3438 971751ac 2018-03-27 stsp if (show_patch) {
3439 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3440 971751ac 2018-03-27 stsp if (err == 0)
3441 971751ac 2018-03-27 stsp printf("\n");
3444 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3445 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3446 07862c20 2018-09-15 stsp return err;
3449 f42b1b34 2018-03-12 stsp static const struct got_error *
3450 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3451 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3452 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3453 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3455 f42b1b34 2018-03-12 stsp const struct got_error *err;
3456 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3457 6841bf13 2019-11-29 kn regex_t regex;
3458 6841bf13 2019-11-29 kn int have_match;
3459 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3460 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3461 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3462 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3463 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3465 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3466 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3468 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(®ex, search_pattern,
3469 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3470 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3472 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3474 f42b1b34 2018-03-12 stsp return err;
3475 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3476 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3478 fcc85cad 2018-07-22 stsp goto done;
3480 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3482 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3485 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3486 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3487 372ccdbb 2018-06-10 stsp if (err) {
3488 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3489 9ba79e04 2018-06-11 stsp err = NULL;
3492 b43fbaa0 2018-06-11 stsp if (id == NULL)
3495 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3499 502b9684 2020-07-31 stsp if (show_changed_paths && !reverse_display_order) {
3500 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3505 6841bf13 2019-11-29 kn if (search_pattern) {
3506 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, ®ex);
3508 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3511 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3512 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3513 0208f208 2020-05-05 stsp &changed_paths, ®ex);
3514 6841bf13 2019-11-29 kn if (have_match == 0) {
3515 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3516 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3517 0208f208 2020-05-05 stsp free((char *)pe->path);
3518 0208f208 2020-05-05 stsp free(pe->data);
3520 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3525 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3526 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3529 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3530 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3532 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3533 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3534 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3535 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3539 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3540 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3543 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3544 0208f208 2020-05-05 stsp free((char *)pe->path);
3545 0208f208 2020-05-05 stsp free(pe->data);
3547 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3549 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3550 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3551 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3554 502b9684 2020-07-31 stsp if (show_changed_paths) {
3555 502b9684 2020-07-31 stsp err = get_changed_paths(&changed_paths,
3556 502b9684 2020-07-31 stsp commit, repo);
3560 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3561 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3562 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3563 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3566 502b9684 2020-07-31 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3567 502b9684 2020-07-31 stsp free((char *)pe->path);
3568 502b9684 2020-07-31 stsp free(pe->data);
3570 502b9684 2020-07-31 stsp got_pathlist_free(&changed_paths);
3574 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3575 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3576 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3577 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3579 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3580 0208f208 2020-05-05 stsp free((char *)pe->path);
3581 0208f208 2020-05-05 stsp free(pe->data);
3583 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3584 6841bf13 2019-11-29 kn if (search_pattern)
3585 6841bf13 2019-11-29 kn regfree(®ex);
3586 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3587 f42b1b34 2018-03-12 stsp return err;
3590 4ed7e80c 2018-05-20 stsp __dead static void
3591 6f3d1eb0 2018-03-12 stsp usage_log(void)
3593 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3594 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3595 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3599 b1ebc001 2019-08-13 stsp static int
3600 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3602 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3603 b1ebc001 2019-08-13 stsp long long n;
3604 b1ebc001 2019-08-13 stsp const char *errstr;
3606 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3607 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3609 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3610 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3615 d1fe46f9 2020-04-18 stsp static const struct got_error *
3616 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3617 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3619 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3620 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3622 d1fe46f9 2020-04-18 stsp *id = NULL;
3624 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3625 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3626 d1fe46f9 2020-04-18 stsp int obj_type;
3627 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3628 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3630 d1fe46f9 2020-04-18 stsp return err;
3631 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3633 d1fe46f9 2020-04-18 stsp return err;
3634 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3635 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3636 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3638 d1fe46f9 2020-04-18 stsp return err;
3639 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3640 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3641 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3642 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3644 d1fe46f9 2020-04-18 stsp free(*id);
3645 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3646 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3647 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3648 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3649 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3650 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3652 d1fe46f9 2020-04-18 stsp return err;
3653 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3654 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3656 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3657 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3660 d1fe46f9 2020-04-18 stsp return err;
3663 4ed7e80c 2018-05-20 stsp static const struct got_error *
3664 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3666 f42b1b34 2018-03-12 stsp const struct got_error *error;
3667 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3668 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3669 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3670 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3671 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3672 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3673 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3674 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3675 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3676 64a96a6d 2018-04-01 stsp const char *errstr;
3677 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3679 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3681 6715a751 2018-03-16 stsp #ifndef PROFILE
3682 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3685 f42b1b34 2018-03-12 stsp err(1, "pledge");
3688 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3690 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3691 79109fed 2018-03-27 stsp switch (ch) {
3693 79109fed 2018-03-27 stsp show_patch = 1;
3696 0208f208 2020-05-05 stsp show_changed_paths = 1;
3699 d142fc45 2018-04-01 stsp start_commit = optarg;
3702 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3704 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3705 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3708 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3709 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3710 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3713 48c8c60d 2020-01-27 stsp log_branches = 1;
3716 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3717 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3718 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3720 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3723 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3726 6841bf13 2019-11-29 kn search_pattern = optarg;
3729 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3732 2deda0b9 2019-03-07 stsp usage_log();
3733 79109fed 2018-03-27 stsp /* NOTREACHED */
3737 79109fed 2018-03-27 stsp argc -= optind;
3738 79109fed 2018-03-27 stsp argv += optind;
3740 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3741 dc1edbfa 2019-11-29 kn diff_context = 3;
3742 dc1edbfa 2019-11-29 kn else if (!show_patch)
3743 0429cd76 2020-09-15 naddy errx(1, "-C requires -p");
3745 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3746 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3747 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3748 04ca23f4 2018-07-16 stsp goto done;
3751 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3752 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3753 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3754 50f2fada 2020-04-24 stsp goto done;
3755 50f2fada 2020-04-24 stsp error = NULL;
3758 e7301579 2019-03-18 stsp if (argc == 0) {
3759 cbd1af7a 2019-03-18 stsp path = strdup("");
3760 e7301579 2019-03-18 stsp if (path == NULL) {
3761 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3762 cbd1af7a 2019-03-18 stsp goto done;
3764 e7301579 2019-03-18 stsp } else if (argc == 1) {
3765 e7301579 2019-03-18 stsp if (worktree) {
3766 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3768 e7301579 2019-03-18 stsp if (error)
3769 e7301579 2019-03-18 stsp goto done;
3771 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3772 e7301579 2019-03-18 stsp if (path == NULL) {
3773 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3774 e7301579 2019-03-18 stsp goto done;
3778 cbd1af7a 2019-03-18 stsp usage_log();
3780 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3781 5486daa2 2019-05-11 stsp repo_path = worktree ?
3782 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3784 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3785 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3786 cffc0aa4 2019-02-05 stsp goto done;
3789 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3790 d7d4f210 2018-03-12 stsp if (error != NULL)
3791 04ca23f4 2018-07-16 stsp goto done;
3793 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3794 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3795 c02c541e 2019-03-29 stsp if (error)
3796 c02c541e 2019-03-29 stsp goto done;
3798 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3799 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3800 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3801 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3802 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3803 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3804 3235492e 2018-04-01 stsp if (error != NULL)
3805 d1fe46f9 2020-04-18 stsp goto done;
3806 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3807 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3808 3235492e 2018-04-01 stsp if (error != NULL)
3809 d1fe46f9 2020-04-18 stsp goto done;
3810 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3811 d1fe46f9 2020-04-18 stsp start_id);
3812 d1fe46f9 2020-04-18 stsp if (error != NULL)
3813 d1fe46f9 2020-04-18 stsp goto done;
3814 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3816 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3817 d1fe46f9 2020-04-18 stsp if (error != NULL)
3818 d1fe46f9 2020-04-18 stsp goto done;
3820 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3821 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3822 d1fe46f9 2020-04-18 stsp if (error != NULL)
3823 d1fe46f9 2020-04-18 stsp goto done;
3826 deeabeae 2019-08-27 stsp if (worktree) {
3827 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3829 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3830 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3831 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3832 deeabeae 2019-08-27 stsp goto done;
3834 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3837 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3838 04ca23f4 2018-07-16 stsp if (error != NULL)
3839 04ca23f4 2018-07-16 stsp goto done;
3840 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3841 04ca23f4 2018-07-16 stsp free(path);
3842 04ca23f4 2018-07-16 stsp path = in_repo_path;
3845 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3846 199a4027 2019-02-02 stsp if (error)
3847 199a4027 2019-02-02 stsp goto done;
3849 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3850 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3851 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3853 04ca23f4 2018-07-16 stsp free(path);
3854 04ca23f4 2018-07-16 stsp free(repo_path);
3855 04ca23f4 2018-07-16 stsp free(cwd);
3856 cffc0aa4 2019-02-05 stsp if (worktree)
3857 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3858 ad242220 2018-09-08 stsp if (repo) {
3859 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3860 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3861 ad242220 2018-09-08 stsp if (error == NULL)
3862 ad242220 2018-09-08 stsp error = repo_error;
3864 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3865 8bf5b3c9 2018-03-17 stsp return error;
3868 4ed7e80c 2018-05-20 stsp __dead static void
3869 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3871 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3872 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3876 b72f483a 2019-02-05 stsp struct print_diff_arg {
3877 b72f483a 2019-02-05 stsp struct got_repository *repo;
3878 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3879 b72f483a 2019-02-05 stsp int diff_context;
3880 3fc0c068 2019-02-10 stsp const char *id_str;
3881 3fc0c068 2019-02-10 stsp int header_shown;
3882 98eaaa12 2019-08-03 stsp int diff_staged;
3883 63035f9f 2019-10-06 stsp int ignore_whitespace;
3887 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3888 39449a05 2020-07-23 stsp * it as content to the diff engine.
3890 39449a05 2020-07-23 stsp static const struct got_error *
3891 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3892 39449a05 2020-07-23 stsp const char *abspath)
3894 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3895 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3896 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3900 39449a05 2020-07-23 stsp if (dirfd != -1) {
3901 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3902 39449a05 2020-07-23 stsp if (target_len == -1)
3903 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3905 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3906 39449a05 2020-07-23 stsp if (target_len == -1)
3907 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3910 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3911 39449a05 2020-07-23 stsp if (*fd == -1)
3912 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3914 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3915 39449a05 2020-07-23 stsp if (outlen == -1) {
3916 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3917 39449a05 2020-07-23 stsp goto done;
3920 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3921 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3922 39449a05 2020-07-23 stsp goto done;
3925 39449a05 2020-07-23 stsp if (err) {
3926 39449a05 2020-07-23 stsp close(*fd);
3929 39449a05 2020-07-23 stsp return err;
3932 4ed7e80c 2018-05-20 stsp static const struct got_error *
3933 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3934 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3935 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3936 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3938 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3939 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3940 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3941 12463d8b 2019-12-13 stsp int fd = -1;
3942 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3943 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3944 b72f483a 2019-02-05 stsp struct stat sb;
3946 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3947 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3948 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3949 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3950 98eaaa12 2019-08-03 stsp return NULL;
3952 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3953 98eaaa12 2019-08-03 stsp return NULL;
3954 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3955 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3956 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3957 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3958 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3959 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3960 98eaaa12 2019-08-03 stsp return NULL;
3963 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3964 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3965 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3966 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3967 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3970 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3971 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3972 98eaaa12 2019-08-03 stsp switch (staged_status) {
3973 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3974 98eaaa12 2019-08-03 stsp label1 = path;
3975 98eaaa12 2019-08-03 stsp label2 = path;
3977 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3978 98eaaa12 2019-08-03 stsp label2 = path;
3980 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3981 98eaaa12 2019-08-03 stsp label1 = path;
3984 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3986 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3987 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3988 63035f9f 2019-10-06 stsp a->repo, stdout);
3991 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3992 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3993 4ce46740 2019-08-08 stsp char *id_str;
3994 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3997 4ce46740 2019-08-08 stsp goto done;
3998 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
4000 4ce46740 2019-08-08 stsp goto done;
4001 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4002 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
4003 4ce46740 2019-08-08 stsp free(id_str);
4004 4ce46740 2019-08-08 stsp goto done;
4006 4ce46740 2019-08-08 stsp free(id_str);
4007 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
4008 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
4010 4ce46740 2019-08-08 stsp goto done;
4013 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
4014 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
4015 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
4016 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4017 2ec1f75b 2019-03-26 stsp goto done;
4020 12463d8b 2019-12-13 stsp if (dirfd != -1) {
4021 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
4022 12463d8b 2019-12-13 stsp if (fd == -1) {
4023 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4024 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
4026 39449a05 2020-07-23 stsp goto done;
4028 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4029 39449a05 2020-07-23 stsp de_name, abspath);
4031 39449a05 2020-07-23 stsp goto done;
4034 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
4035 12463d8b 2019-12-13 stsp if (fd == -1) {
4036 39449a05 2020-07-23 stsp if (errno != ELOOP) {
4037 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
4039 39449a05 2020-07-23 stsp goto done;
4041 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
4042 39449a05 2020-07-23 stsp de_name, abspath);
4044 39449a05 2020-07-23 stsp goto done;
4047 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
4048 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
4049 2ec1f75b 2019-03-26 stsp goto done;
4051 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
4052 12463d8b 2019-12-13 stsp if (f2 == NULL) {
4053 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
4054 2ec1f75b 2019-03-26 stsp goto done;
4058 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
4060 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
4061 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
4063 b72f483a 2019-02-05 stsp if (blob1)
4064 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
4065 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
4066 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
4067 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
4068 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
4069 b72f483a 2019-02-05 stsp free(abspath);
4070 927df6b7 2019-02-10 stsp return err;
4073 927df6b7 2019-02-10 stsp static const struct got_error *
4074 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
4076 b00d56cd 2018-04-01 stsp const struct got_error *error;
4077 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
4078 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
4079 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
4080 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
4081 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
4082 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
4083 15a94983 2018-12-23 stsp int type1, type2;
4084 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4085 c0cc5c62 2018-10-18 stsp const char *errstr;
4086 927df6b7 2019-02-10 stsp char *path = NULL;
4088 b00d56cd 2018-04-01 stsp #ifndef PROFILE
4089 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4090 25eccc22 2019-01-04 stsp NULL) == -1)
4091 b00d56cd 2018-04-01 stsp err(1, "pledge");
4094 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4095 b00d56cd 2018-04-01 stsp switch (ch) {
4097 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4099 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
4100 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
4103 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
4104 b72f483a 2019-02-05 stsp if (repo_path == NULL)
4105 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4107 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4110 98eaaa12 2019-08-03 stsp diff_staged = 1;
4113 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
4116 2deda0b9 2019-03-07 stsp usage_diff();
4117 b00d56cd 2018-04-01 stsp /* NOTREACHED */
4121 b00d56cd 2018-04-01 stsp argc -= optind;
4122 b00d56cd 2018-04-01 stsp argv += optind;
4124 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
4125 b72f483a 2019-02-05 stsp if (cwd == NULL) {
4126 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4127 b72f483a 2019-02-05 stsp goto done;
4129 927df6b7 2019-02-10 stsp if (argc <= 1) {
4130 b72f483a 2019-02-05 stsp if (repo_path)
4132 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
4133 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4134 fa51e947 2020-03-27 stsp if (error) {
4135 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4136 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
4138 1f03b8da 2020-03-20 stsp goto done;
4140 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
4141 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
4142 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4143 927df6b7 2019-02-10 stsp goto done;
4145 927df6b7 2019-02-10 stsp if (argc == 1) {
4146 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
4148 927df6b7 2019-02-10 stsp if (error)
4149 927df6b7 2019-02-10 stsp goto done;
4151 927df6b7 2019-02-10 stsp path = strdup("");
4152 927df6b7 2019-02-10 stsp if (path == NULL) {
4153 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4154 927df6b7 2019-02-10 stsp goto done;
4157 b72f483a 2019-02-05 stsp } else if (argc == 2) {
4158 98eaaa12 2019-08-03 stsp if (diff_staged)
4159 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
4160 98eaaa12 2019-08-03 stsp "objects in repository");
4161 15a94983 2018-12-23 stsp id_str1 = argv[0];
4162 15a94983 2018-12-23 stsp id_str2 = argv[1];
4163 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4164 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
4165 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4166 30db809c 2019-06-05 stsp goto done;
4167 1f03b8da 2020-03-20 stsp if (worktree) {
4168 1f03b8da 2020-03-20 stsp repo_path = strdup(
4169 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
4170 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4171 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4172 1f03b8da 2020-03-20 stsp goto done;
4175 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
4176 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4177 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
4178 1f03b8da 2020-03-20 stsp goto done;
4183 b00d56cd 2018-04-01 stsp usage_diff();
4185 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4186 76089277 2018-04-01 stsp free(repo_path);
4187 b00d56cd 2018-04-01 stsp if (error != NULL)
4188 b00d56cd 2018-04-01 stsp goto done;
4190 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4191 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4192 c02c541e 2019-03-29 stsp if (error)
4193 c02c541e 2019-03-29 stsp goto done;
4195 30db809c 2019-06-05 stsp if (argc <= 1) {
4196 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
4197 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4198 b72f483a 2019-02-05 stsp char *id_str;
4200 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4202 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
4203 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
4204 b72f483a 2019-02-05 stsp if (error)
4205 b72f483a 2019-02-05 stsp goto done;
4206 b72f483a 2019-02-05 stsp arg.repo = repo;
4207 b72f483a 2019-02-05 stsp arg.worktree = worktree;
4208 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
4209 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
4210 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
4211 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
4212 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
4214 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
4215 72ea6654 2019-07-27 stsp if (error)
4216 72ea6654 2019-07-27 stsp goto done;
4218 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
4219 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
4220 b72f483a 2019-02-05 stsp free(id_str);
4221 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4222 b72f483a 2019-02-05 stsp goto done;
4225 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
4226 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4227 0adb7196 2019-08-11 stsp if (error)
4228 0adb7196 2019-08-11 stsp goto done;
4230 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
4231 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
4232 0adb7196 2019-08-11 stsp if (error)
4233 0adb7196 2019-08-11 stsp goto done;
4235 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
4236 15a94983 2018-12-23 stsp if (error)
4237 15a94983 2018-12-23 stsp goto done;
4239 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
4240 15a94983 2018-12-23 stsp if (error)
4241 15a94983 2018-12-23 stsp goto done;
4243 15a94983 2018-12-23 stsp if (type1 != type2) {
4244 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4245 b00d56cd 2018-04-01 stsp goto done;
4248 15a94983 2018-12-23 stsp switch (type1) {
4249 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
4250 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4251 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4253 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4254 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4255 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4257 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4258 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4259 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4260 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4263 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4266 5e70831e 2019-05-28 stsp free(label1);
4267 5e70831e 2019-05-28 stsp free(label2);
4268 15a94983 2018-12-23 stsp free(id1);
4269 15a94983 2018-12-23 stsp free(id2);
4270 927df6b7 2019-02-10 stsp free(path);
4271 b72f483a 2019-02-05 stsp if (worktree)
4272 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4273 ad242220 2018-09-08 stsp if (repo) {
4274 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4275 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4276 ad242220 2018-09-08 stsp if (error == NULL)
4277 ad242220 2018-09-08 stsp error = repo_error;
4279 b00d56cd 2018-04-01 stsp return error;
4282 404c43c4 2018-06-21 stsp __dead static void
4283 404c43c4 2018-06-21 stsp usage_blame(void)
4285 9270e621 2019-02-05 stsp fprintf(stderr,
4286 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4287 404c43c4 2018-06-21 stsp getprogname());
4291 28315671 2019-08-14 stsp struct blame_line {
4292 28315671 2019-08-14 stsp int annotated;
4293 28315671 2019-08-14 stsp char *id_str;
4294 82f6abb8 2019-08-14 stsp char *committer;
4295 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4298 28315671 2019-08-14 stsp struct blame_cb_args {
4299 28315671 2019-08-14 stsp struct blame_line *lines;
4300 28315671 2019-08-14 stsp int nlines;
4301 7ef28ff8 2019-08-14 stsp int nlines_prec;
4302 28315671 2019-08-14 stsp int lineno_cur;
4303 28315671 2019-08-14 stsp off_t *line_offsets;
4305 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4308 404c43c4 2018-06-21 stsp static const struct got_error *
4309 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4311 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4312 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4313 28315671 2019-08-14 stsp struct blame_line *bline;
4314 82f6abb8 2019-08-14 stsp char *line = NULL;
4315 28315671 2019-08-14 stsp size_t linesize = 0;
4316 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4317 28315671 2019-08-14 stsp off_t offset;
4318 bcb49d15 2019-08-14 stsp struct tm tm;
4319 bcb49d15 2019-08-14 stsp time_t committer_time;
4321 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4322 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4323 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4325 28315671 2019-08-14 stsp if (sigint_received)
4326 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4328 2839f8b9 2019-08-18 stsp if (lineno == -1)
4329 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4331 28315671 2019-08-14 stsp /* Annotate this line. */
4332 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4333 28315671 2019-08-14 stsp if (bline->annotated)
4334 28315671 2019-08-14 stsp return NULL;
4335 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4337 28315671 2019-08-14 stsp return err;
4339 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4341 82f6abb8 2019-08-14 stsp goto done;
4343 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4344 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4345 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4346 bcb49d15 2019-08-14 stsp goto done;
4349 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4350 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4351 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4352 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4353 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4354 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4355 82f6abb8 2019-08-14 stsp goto done;
4357 28315671 2019-08-14 stsp bline->annotated = 1;
4359 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4360 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4361 28315671 2019-08-14 stsp if (!bline->annotated)
4362 82f6abb8 2019-08-14 stsp goto done;
4364 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4365 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4366 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4367 82f6abb8 2019-08-14 stsp goto done;
4370 28315671 2019-08-14 stsp while (bline->annotated) {
4371 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4372 82f6abb8 2019-08-14 stsp size_t len;
4374 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4375 28315671 2019-08-14 stsp if (ferror(a->f))
4376 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4380 82f6abb8 2019-08-14 stsp committer = bline->committer;
4381 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4382 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4383 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4384 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4386 82f6abb8 2019-08-14 stsp *at = '\0';
4387 82f6abb8 2019-08-14 stsp len = strlen(committer);
4388 82f6abb8 2019-08-14 stsp if (len >= 9)
4389 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4391 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4393 28315671 2019-08-14 stsp *nl = '\0';
4394 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4395 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4397 28315671 2019-08-14 stsp a->lineno_cur++;
4398 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4401 82f6abb8 2019-08-14 stsp if (commit)
4402 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4403 28315671 2019-08-14 stsp free(line);
4404 28315671 2019-08-14 stsp return err;
4407 28315671 2019-08-14 stsp static const struct got_error *
4408 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4410 404c43c4 2018-06-21 stsp const struct got_error *error;
4411 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4412 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4413 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4414 0587e10c 2020-07-23 stsp char *link_target = NULL;
4415 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4416 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4417 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4418 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4419 28315671 2019-08-14 stsp struct blame_cb_args bca;
4420 28315671 2019-08-14 stsp int ch, obj_type, i;
4421 b02560ec 2019-08-19 stsp size_t filesize;
4423 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4425 404c43c4 2018-06-21 stsp #ifndef PROFILE
4426 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4427 36e2fb66 2019-01-04 stsp NULL) == -1)
4428 404c43c4 2018-06-21 stsp err(1, "pledge");
4431 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4432 404c43c4 2018-06-21 stsp switch (ch) {
4434 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4437 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4438 66bea077 2018-08-02 stsp if (repo_path == NULL)
4439 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4441 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4444 2deda0b9 2019-03-07 stsp usage_blame();
4445 404c43c4 2018-06-21 stsp /* NOTREACHED */
4449 404c43c4 2018-06-21 stsp argc -= optind;
4450 404c43c4 2018-06-21 stsp argv += optind;
4452 a39318fd 2018-08-02 stsp if (argc == 1)
4453 404c43c4 2018-06-21 stsp path = argv[0];
4455 404c43c4 2018-06-21 stsp usage_blame();
4457 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4458 66bea077 2018-08-02 stsp if (cwd == NULL) {
4459 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4460 66bea077 2018-08-02 stsp goto done;
4462 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4463 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4464 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4465 66bea077 2018-08-02 stsp goto done;
4467 0c06baac 2019-02-05 stsp error = NULL;
4468 0c06baac 2019-02-05 stsp if (worktree) {
4469 0c06baac 2019-02-05 stsp repo_path =
4470 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4471 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4473 1f03b8da 2020-03-20 stsp if (error)
4474 1f03b8da 2020-03-20 stsp goto done;
4477 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4478 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4479 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4480 0c06baac 2019-02-05 stsp goto done;
4485 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4486 c02c541e 2019-03-29 stsp if (error != NULL)
4487 36e2fb66 2019-01-04 stsp goto done;
4489 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4490 c02c541e 2019-03-29 stsp if (error)
4491 404c43c4 2018-06-21 stsp goto done;
4493 0c06baac 2019-02-05 stsp if (worktree) {
4494 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4495 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4496 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4497 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4498 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4499 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4500 6efaaa2d 2019-02-05 stsp path) == -1) {
4501 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4502 0c06baac 2019-02-05 stsp goto done;
4504 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4507 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4509 0c06baac 2019-02-05 stsp if (error)
4510 66bea077 2018-08-02 stsp goto done;
4512 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4513 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4514 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4515 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4516 404c43c4 2018-06-21 stsp if (error != NULL)
4517 66bea077 2018-08-02 stsp goto done;
4518 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4519 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4520 404c43c4 2018-06-21 stsp if (error != NULL)
4521 66bea077 2018-08-02 stsp goto done;
4523 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4524 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4525 30837e32 2019-07-25 stsp if (error)
4526 66bea077 2018-08-02 stsp goto done;
4529 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4530 0587e10c 2020-07-23 stsp commit_id, repo);
4531 0587e10c 2020-07-23 stsp if (error)
4532 0587e10c 2020-07-23 stsp goto done;
4534 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4535 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4536 28315671 2019-08-14 stsp if (error)
4537 28315671 2019-08-14 stsp goto done;
4539 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4540 28315671 2019-08-14 stsp if (error)
4541 28315671 2019-08-14 stsp goto done;
4543 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4544 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4545 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4546 28315671 2019-08-14 stsp goto done;
4549 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4550 28315671 2019-08-14 stsp if (error)
4551 28315671 2019-08-14 stsp goto done;
4552 28315671 2019-08-14 stsp bca.f = got_opentemp();
4553 28315671 2019-08-14 stsp if (bca.f == NULL) {
4554 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4555 28315671 2019-08-14 stsp goto done;
4557 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4558 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4559 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4560 28315671 2019-08-14 stsp goto done;
4562 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4563 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4564 b02560ec 2019-08-19 stsp bca.nlines--;
4566 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4567 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4568 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4569 28315671 2019-08-14 stsp goto done;
4571 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4572 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4573 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4574 7ef28ff8 2019-08-14 stsp while (i > 0) {
4576 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4578 82f6abb8 2019-08-14 stsp bca.repo = repo;
4580 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4581 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4583 66bea077 2018-08-02 stsp free(in_repo_path);
4584 0587e10c 2020-07-23 stsp free(link_target);
4585 66bea077 2018-08-02 stsp free(repo_path);
4586 66bea077 2018-08-02 stsp free(cwd);
4587 404c43c4 2018-06-21 stsp free(commit_id);
4588 28315671 2019-08-14 stsp free(obj_id);
4590 28315671 2019-08-14 stsp got_object_blob_close(blob);
4591 0c06baac 2019-02-05 stsp if (worktree)
4592 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4593 ad242220 2018-09-08 stsp if (repo) {
4594 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4595 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4596 ad242220 2018-09-08 stsp if (error == NULL)
4597 ad242220 2018-09-08 stsp error = repo_error;
4599 3affba96 2019-09-22 stsp if (bca.lines) {
4600 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4601 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4602 3affba96 2019-09-22 stsp free(bline->id_str);
4603 3affba96 2019-09-22 stsp free(bline->committer);
4605 3affba96 2019-09-22 stsp free(bca.lines);
4607 28315671 2019-08-14 stsp free(bca.line_offsets);
4608 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4609 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4610 404c43c4 2018-06-21 stsp return error;
4613 5de5890b 2018-10-18 stsp __dead static void
4614 5de5890b 2018-10-18 stsp usage_tree(void)
4616 c1669e2e 2019-01-09 stsp fprintf(stderr,
4617 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4618 5de5890b 2018-10-18 stsp getprogname());
4622 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4623 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4624 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4626 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4627 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4628 848d6979 2019-08-12 stsp const char *modestr = "";
4629 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4630 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4632 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4633 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4636 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4637 63c5ca5d 2019-08-24 stsp modestr = "$";
4638 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4641 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4643 0d6c6ee3 2020-05-20 stsp return err;
4644 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4645 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4646 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4649 848d6979 2019-08-12 stsp modestr = "@";
4651 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4652 848d6979 2019-08-12 stsp modestr = "/";
4653 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4654 848d6979 2019-08-12 stsp modestr = "*";
4656 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4657 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4658 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4660 0d6c6ee3 2020-05-20 stsp free(link_target);
4661 0d6c6ee3 2020-05-20 stsp return NULL;
4664 5de5890b 2018-10-18 stsp static const struct got_error *
4665 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4666 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4667 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4669 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4670 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4671 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4672 56e0773d 2019-11-28 stsp int nentries, i;
4674 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4676 5de5890b 2018-10-18 stsp goto done;
4678 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4680 5de5890b 2018-10-18 stsp goto done;
4681 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4682 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4683 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4684 5de5890b 2018-10-18 stsp char *id = NULL;
4686 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4689 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4690 5de5890b 2018-10-18 stsp if (show_ids) {
4691 5de5890b 2018-10-18 stsp char *id_str;
4692 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4693 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4695 5de5890b 2018-10-18 stsp goto done;
4696 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4697 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4698 5de5890b 2018-10-18 stsp free(id_str);
4699 5de5890b 2018-10-18 stsp goto done;
4701 5de5890b 2018-10-18 stsp free(id_str);
4703 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4706 0d6c6ee3 2020-05-20 stsp goto done;
4708 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4709 c1669e2e 2019-01-09 stsp char *child_path;
4710 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4711 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4712 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4713 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4714 c1669e2e 2019-01-09 stsp goto done;
4716 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4717 c1669e2e 2019-01-09 stsp root_path, repo);
4718 c1669e2e 2019-01-09 stsp free(child_path);
4720 c1669e2e 2019-01-09 stsp goto done;
4725 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4726 5de5890b 2018-10-18 stsp free(tree_id);
4727 5de5890b 2018-10-18 stsp return err;
4730 5de5890b 2018-10-18 stsp static const struct got_error *
4731 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4733 5de5890b 2018-10-18 stsp const struct got_error *error;
4734 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4735 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4736 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4737 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4738 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4739 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4740 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4743 5de5890b 2018-10-18 stsp #ifndef PROFILE
4744 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4745 0f8d269b 2019-01-04 stsp NULL) == -1)
4746 5de5890b 2018-10-18 stsp err(1, "pledge");
4749 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4750 5de5890b 2018-10-18 stsp switch (ch) {
4752 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4755 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4756 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4757 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4759 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4762 5de5890b 2018-10-18 stsp show_ids = 1;
4765 c1669e2e 2019-01-09 stsp recurse = 1;
4768 2deda0b9 2019-03-07 stsp usage_tree();
4769 5de5890b 2018-10-18 stsp /* NOTREACHED */
4773 5de5890b 2018-10-18 stsp argc -= optind;
4774 5de5890b 2018-10-18 stsp argv += optind;
4776 5de5890b 2018-10-18 stsp if (argc == 1)
4777 5de5890b 2018-10-18 stsp path = argv[0];
4778 5de5890b 2018-10-18 stsp else if (argc > 1)
4779 5de5890b 2018-10-18 stsp usage_tree();
4781 7a2c19d6 2019-02-05 stsp path = NULL;
4783 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4784 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4785 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4786 9bf7a39b 2019-02-05 stsp goto done;
4788 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4789 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4790 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4791 7a2c19d6 2019-02-05 stsp goto done;
4793 7a2c19d6 2019-02-05 stsp error = NULL;
4794 7a2c19d6 2019-02-05 stsp if (worktree) {
4795 7a2c19d6 2019-02-05 stsp repo_path =
4796 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4797 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4798 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4799 7a2c19d6 2019-02-05 stsp if (error)
4800 7a2c19d6 2019-02-05 stsp goto done;
4802 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4803 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4804 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4805 7a2c19d6 2019-02-05 stsp goto done;
4810 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4811 5de5890b 2018-10-18 stsp if (error != NULL)
4812 c02c541e 2019-03-29 stsp goto done;
4814 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4815 c02c541e 2019-03-29 stsp if (error)
4816 5de5890b 2018-10-18 stsp goto done;
4818 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4819 9bf7a39b 2019-02-05 stsp if (worktree) {
4820 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4821 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4822 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4823 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4824 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4825 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4826 9bf7a39b 2019-02-05 stsp goto done;
4828 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4830 9bf7a39b 2019-02-05 stsp if (error)
4831 9bf7a39b 2019-02-05 stsp goto done;
4833 9bf7a39b 2019-02-05 stsp path = "/";
4835 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4836 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4837 9bf7a39b 2019-02-05 stsp if (error != NULL)
4838 9bf7a39b 2019-02-05 stsp goto done;
4841 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4842 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4843 4e0a20a4 2020-03-23 tracey if (worktree)
4844 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4846 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4847 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4848 5de5890b 2018-10-18 stsp if (error != NULL)
4849 5de5890b 2018-10-18 stsp goto done;
4850 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4851 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4852 5de5890b 2018-10-18 stsp if (error != NULL)
4853 5de5890b 2018-10-18 stsp goto done;
4855 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4856 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4857 30837e32 2019-07-25 stsp if (error)
4858 5de5890b 2018-10-18 stsp goto done;
4861 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4862 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4864 5de5890b 2018-10-18 stsp free(in_repo_path);
4865 5de5890b 2018-10-18 stsp free(repo_path);
4866 5de5890b 2018-10-18 stsp free(cwd);
4867 5de5890b 2018-10-18 stsp free(commit_id);
4868 7a2c19d6 2019-02-05 stsp if (worktree)
4869 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4870 5de5890b 2018-10-18 stsp if (repo) {
4871 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4872 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4873 5de5890b 2018-10-18 stsp if (error == NULL)
4874 5de5890b 2018-10-18 stsp error = repo_error;
4876 5de5890b 2018-10-18 stsp return error;
4879 6bad629b 2019-02-04 stsp __dead static void
4880 6bad629b 2019-02-04 stsp usage_status(void)
4882 081470ac 2020-08-13 stsp fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4883 081470ac 2020-08-13 stsp getprogname());
4887 b72f483a 2019-02-05 stsp static const struct got_error *
4888 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4889 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4890 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4891 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4893 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4894 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4895 081470ac 2020-08-13 stsp if (arg) {
4896 081470ac 2020-08-13 stsp char *status_codes = arg;
4897 081470ac 2020-08-13 stsp size_t ncodes = strlen(status_codes);
4899 081470ac 2020-08-13 stsp for (i = 0; i < ncodes ; i++) {
4900 081470ac 2020-08-13 stsp if (status == status_codes[i] ||
4901 081470ac 2020-08-13 stsp staged_status == status_codes[i])
4904 081470ac 2020-08-13 stsp if (i == ncodes)
4905 081470ac 2020-08-13 stsp return NULL;
4907 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4908 b72f483a 2019-02-05 stsp return NULL;
4911 6bad629b 2019-02-04 stsp static const struct got_error *
4912 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4914 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4915 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4916 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4917 081470ac 2020-08-13 stsp char *cwd = NULL, *status_codes = NULL;;
4918 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4919 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4920 081470ac 2020-08-13 stsp int ch, i;
4922 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4924 081470ac 2020-08-13 stsp while ((ch = getopt(argc, argv, "s:")) != -1) {
4925 6bad629b 2019-02-04 stsp switch (ch) {
4927 081470ac 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
4928 081470ac 2020-08-13 stsp switch (optarg[i]) {
4929 081470ac 2020-08-13 stsp case GOT_STATUS_MODIFY:
4930 081470ac 2020-08-13 stsp case GOT_STATUS_ADD:
4931 081470ac 2020-08-13 stsp case GOT_STATUS_DELETE:
4932 081470ac 2020-08-13 stsp case GOT_STATUS_CONFLICT:
4933 081470ac 2020-08-13 stsp case GOT_STATUS_MISSING:
4934 081470ac 2020-08-13 stsp case GOT_STATUS_OBSTRUCTED:
4935 081470ac 2020-08-13 stsp case GOT_STATUS_UNVERSIONED:
4936 081470ac 2020-08-13 stsp case GOT_STATUS_MODE_CHANGE:
4937 081470ac 2020-08-13 stsp case GOT_STATUS_NONEXISTENT:
4940 081470ac 2020-08-13 stsp errx(1, "invalid status code '%c'",
4941 081470ac 2020-08-13 stsp optarg[i]);
4944 081470ac 2020-08-13 stsp status_codes = optarg;
4947 2deda0b9 2019-03-07 stsp usage_status();
4948 6bad629b 2019-02-04 stsp /* NOTREACHED */
4952 6bad629b 2019-02-04 stsp argc -= optind;
4953 6bad629b 2019-02-04 stsp argv += optind;
4955 6bad629b 2019-02-04 stsp #ifndef PROFILE
4956 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4957 6bad629b 2019-02-04 stsp NULL) == -1)
4958 6bad629b 2019-02-04 stsp err(1, "pledge");
4960 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4961 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4963 927df6b7 2019-02-10 stsp goto done;
4966 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4967 fa51e947 2020-03-27 stsp if (error) {
4968 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4969 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4970 a5edda0a 2019-07-27 stsp goto done;
4973 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4975 6bad629b 2019-02-04 stsp if (error != NULL)
4976 6bad629b 2019-02-04 stsp goto done;
4978 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4979 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4980 087fb88c 2019-08-04 stsp if (error)
4981 087fb88c 2019-08-04 stsp goto done;
4983 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4984 6bad629b 2019-02-04 stsp if (error)
4985 6bad629b 2019-02-04 stsp goto done;
4987 081470ac 2020-08-13 stsp error = got_worktree_status(worktree, &paths, repo, print_status,
4988 081470ac 2020-08-13 stsp status_codes, check_cancelled, NULL);
4990 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4991 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4992 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4993 927df6b7 2019-02-10 stsp free(cwd);
4994 d0eebce4 2019-03-11 stsp return error;
4997 d0eebce4 2019-03-11 stsp __dead static void
4998 d0eebce4 2019-03-11 stsp usage_ref(void)
5000 d0eebce4 2019-03-11 stsp fprintf(stderr,
5001 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
5002 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
5003 d0eebce4 2019-03-11 stsp getprogname());
5007 d0eebce4 2019-03-11 stsp static const struct got_error *
5008 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
5010 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
5011 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
5012 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
5014 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
5015 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
5017 d0eebce4 2019-03-11 stsp return err;
5019 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5020 d0eebce4 2019-03-11 stsp char *refstr;
5021 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
5022 d0eebce4 2019-03-11 stsp if (refstr == NULL)
5023 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
5024 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
5025 d0eebce4 2019-03-11 stsp free(refstr);
5028 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
5029 d0eebce4 2019-03-11 stsp return NULL;
5032 d0eebce4 2019-03-11 stsp static const struct got_error *
5033 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
5035 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5036 d0eebce4 2019-03-11 stsp struct got_reference *ref;
5038 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
5040 d0eebce4 2019-03-11 stsp return err;
5042 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
5043 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5044 d0eebce4 2019-03-11 stsp return err;
5047 d0eebce4 2019-03-11 stsp static const struct got_error *
5048 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
5050 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
5051 d0eebce4 2019-03-11 stsp struct got_object_id *id;
5052 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
5055 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5056 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
5057 d1644381 2019-07-14 stsp * an unintended typo.
5059 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5060 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5062 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
5064 d83d9d5c 2019-05-13 stsp if (err) {
5065 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
5067 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
5068 d83d9d5c 2019-05-13 stsp return err;
5069 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
5071 d83d9d5c 2019-05-13 stsp return err;
5072 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
5073 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
5075 d83d9d5c 2019-05-13 stsp return err;
5078 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
5080 d0eebce4 2019-03-11 stsp goto done;
5082 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
5085 d0eebce4 2019-03-11 stsp got_ref_close(ref);
5087 d1c1ae5f 2019-08-12 stsp return err;
5090 d1c1ae5f 2019-08-12 stsp static const struct got_error *
5091 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
5093 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
5094 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
5095 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
5098 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
5099 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
5100 d1c1ae5f 2019-08-12 stsp * an unintended typo.
5102 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
5103 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5105 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
5107 d1c1ae5f 2019-08-12 stsp return err;
5109 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
5111 d1c1ae5f 2019-08-12 stsp goto done;
5113 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
5115 d1c1ae5f 2019-08-12 stsp if (target_ref)
5116 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
5118 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
5119 d0eebce4 2019-03-11 stsp return err;
5122 d0eebce4 2019-03-11 stsp static const struct got_error *
5123 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
5125 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
5126 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
5127 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
5128 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
5129 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
5130 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
5131 b2070a3f 2020-03-22 stsp char *refname = NULL;
5133 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5134 d0eebce4 2019-03-11 stsp switch (ch) {
5136 e31abbf2 2020-03-22 stsp obj_arg = optarg;
5139 e31abbf2 2020-03-22 stsp do_delete = 1;
5142 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
5143 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5144 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5146 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
5149 d0eebce4 2019-03-11 stsp do_list = 1;
5152 e31abbf2 2020-03-22 stsp symref_target = optarg;
5155 d0eebce4 2019-03-11 stsp usage_ref();
5156 d0eebce4 2019-03-11 stsp /* NOTREACHED */
5160 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
5161 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
5162 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
5163 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
5164 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
5165 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
5166 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
5167 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
5168 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
5169 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
5170 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
5171 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
5173 d0eebce4 2019-03-11 stsp argc -= optind;
5174 d0eebce4 2019-03-11 stsp argv += optind;
5176 e31abbf2 2020-03-22 stsp if (do_list) {
5177 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
5178 d0eebce4 2019-03-11 stsp usage_ref();
5179 b2070a3f 2020-03-22 stsp if (argc == 1) {
5180 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5181 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5182 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5183 b2070a3f 2020-03-22 stsp goto done;
5187 e31abbf2 2020-03-22 stsp if (argc != 1)
5188 e31abbf2 2020-03-22 stsp usage_ref();
5189 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
5190 b2070a3f 2020-03-22 stsp if (refname == NULL) {
5191 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
5192 b2070a3f 2020-03-22 stsp goto done;
5196 b2070a3f 2020-03-22 stsp if (refname)
5197 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
5199 d0eebce4 2019-03-11 stsp #ifndef PROFILE
5200 e0b57350 2019-03-12 stsp if (do_list) {
5201 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5202 e0b57350 2019-03-12 stsp NULL) == -1)
5203 e0b57350 2019-03-12 stsp err(1, "pledge");
5205 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5206 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
5207 e0b57350 2019-03-12 stsp err(1, "pledge");
5210 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
5211 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
5212 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5213 d0eebce4 2019-03-11 stsp goto done;
5216 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5217 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
5218 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5219 d0eebce4 2019-03-11 stsp goto done;
5221 d0eebce4 2019-03-11 stsp error = NULL;
5222 d0eebce4 2019-03-11 stsp if (worktree) {
5223 d0eebce4 2019-03-11 stsp repo_path =
5224 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
5225 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
5226 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5227 d0eebce4 2019-03-11 stsp if (error)
5228 d0eebce4 2019-03-11 stsp goto done;
5230 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
5231 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
5232 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
5233 d0eebce4 2019-03-11 stsp goto done;
5238 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5239 d0eebce4 2019-03-11 stsp if (error != NULL)
5240 d0eebce4 2019-03-11 stsp goto done;
5242 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5243 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5244 c02c541e 2019-03-29 stsp if (error)
5245 c02c541e 2019-03-29 stsp goto done;
5247 d0eebce4 2019-03-11 stsp if (do_list)
5248 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
5249 e31abbf2 2020-03-22 stsp else if (do_delete)
5250 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
5251 e31abbf2 2020-03-22 stsp else if (symref_target)
5252 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
5254 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
5255 e31abbf2 2020-03-22 stsp usage_ref();
5256 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
5259 b2070a3f 2020-03-22 stsp free(refname);
5261 4e759de4 2019-06-26 stsp got_repo_close(repo);
5262 4e759de4 2019-06-26 stsp if (worktree)
5263 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
5264 4e759de4 2019-06-26 stsp free(cwd);
5265 4e759de4 2019-06-26 stsp free(repo_path);
5266 4e759de4 2019-06-26 stsp return error;
5269 4e759de4 2019-06-26 stsp __dead static void
5270 4e759de4 2019-06-26 stsp usage_branch(void)
5272 4e759de4 2019-06-26 stsp fprintf(stderr,
5273 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5274 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
5278 4e759de4 2019-06-26 stsp static const struct got_error *
5279 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
5280 4e99b47e 2019-10-04 stsp struct got_reference *ref)
5282 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
5283 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5284 4e99b47e 2019-10-04 stsp char *refstr;
5286 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5287 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5288 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5289 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5291 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5293 4e99b47e 2019-10-04 stsp return err;
5294 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5295 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5296 4e99b47e 2019-10-04 stsp marker = "* ";
5298 4e99b47e 2019-10-04 stsp marker = "~ ";
5302 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5303 4e99b47e 2019-10-04 stsp refname += 11;
5304 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5305 4e99b47e 2019-10-04 stsp refname += 18;
5307 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5308 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5309 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5311 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5312 4e99b47e 2019-10-04 stsp free(refstr);
5313 4e99b47e 2019-10-04 stsp return NULL;
5316 4e99b47e 2019-10-04 stsp static const struct got_error *
5317 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5319 ad89fa31 2019-10-04 stsp const char *refname;
5321 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5322 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5324 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5326 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5327 ad89fa31 2019-10-04 stsp refname += 11;
5328 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5329 ad89fa31 2019-10-04 stsp refname += 18;
5331 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5333 ad89fa31 2019-10-04 stsp return NULL;
5336 ad89fa31 2019-10-04 stsp static const struct got_error *
5337 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5339 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5340 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5341 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5342 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5343 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5345 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5347 4e99b47e 2019-10-04 stsp if (worktree) {
5348 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5349 4e99b47e 2019-10-04 stsp worktree);
5351 4e99b47e 2019-10-04 stsp return err;
5353 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5354 4e99b47e 2019-10-04 stsp worktree);
5356 4e99b47e 2019-10-04 stsp return err;
5358 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5359 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5360 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5362 4e99b47e 2019-10-04 stsp return err;
5363 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5364 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5368 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5369 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5371 4e759de4 2019-06-26 stsp return err;
5373 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5374 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5376 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5377 4e759de4 2019-06-26 stsp return NULL;
5380 4e759de4 2019-06-26 stsp static const struct got_error *
5381 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5382 45cd4e47 2019-08-25 stsp const char *branch_name)
5384 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5385 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5386 4e759de4 2019-06-26 stsp char *refname;
5388 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5389 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5391 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5393 4e759de4 2019-06-26 stsp goto done;
5395 45cd4e47 2019-08-25 stsp if (worktree &&
5396 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5397 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5398 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5399 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5400 45cd4e47 2019-08-25 stsp goto done;
5403 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5406 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5407 4e759de4 2019-06-26 stsp free(refname);
5408 4e759de4 2019-06-26 stsp return err;
5411 4e759de4 2019-06-26 stsp static const struct got_error *
5412 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5413 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5415 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5416 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5417 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5420 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5421 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5422 d3f84d51 2019-07-11 stsp * an unintended typo.
5424 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5425 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5427 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5428 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5429 4e759de4 2019-06-26 stsp goto done;
5432 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5433 4e759de4 2019-06-26 stsp if (err == NULL) {
5434 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5435 4e759de4 2019-06-26 stsp goto done;
5436 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5437 4e759de4 2019-06-26 stsp goto done;
5439 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5441 4e759de4 2019-06-26 stsp goto done;
5443 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5446 4e759de4 2019-06-26 stsp got_ref_close(ref);
5447 4e759de4 2019-06-26 stsp free(base_refname);
5448 4e759de4 2019-06-26 stsp free(refname);
5449 4e759de4 2019-06-26 stsp return err;
5452 4e759de4 2019-06-26 stsp static const struct got_error *
5453 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5455 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5456 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5457 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5458 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5459 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5460 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5461 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5462 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5463 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5464 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5465 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5467 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5469 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5470 4e759de4 2019-06-26 stsp switch (ch) {
5472 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5475 4e759de4 2019-06-26 stsp delref = optarg;
5478 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5479 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5480 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5482 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5485 4e759de4 2019-06-26 stsp do_list = 1;
5488 da76fce2 2020-02-24 stsp do_update = 0;
5491 4e759de4 2019-06-26 stsp usage_branch();
5492 4e759de4 2019-06-26 stsp /* NOTREACHED */
5496 4e759de4 2019-06-26 stsp if (do_list && delref)
5497 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5499 4e759de4 2019-06-26 stsp argc -= optind;
5500 4e759de4 2019-06-26 stsp argv += optind;
5502 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5503 ad89fa31 2019-10-04 stsp do_show = 1;
5505 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5506 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5508 4e759de4 2019-06-26 stsp if (do_list || delref) {
5509 4e759de4 2019-06-26 stsp if (argc > 0)
5510 4e759de4 2019-06-26 stsp usage_branch();
5511 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5512 4e759de4 2019-06-26 stsp usage_branch();
5514 4e759de4 2019-06-26 stsp #ifndef PROFILE
5515 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5516 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5517 4e759de4 2019-06-26 stsp NULL) == -1)
5518 4e759de4 2019-06-26 stsp err(1, "pledge");
5520 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5521 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5522 4e759de4 2019-06-26 stsp err(1, "pledge");
5525 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5526 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5527 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5528 4e759de4 2019-06-26 stsp goto done;
5531 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5532 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5533 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5534 4e759de4 2019-06-26 stsp goto done;
5536 4e759de4 2019-06-26 stsp error = NULL;
5537 4e759de4 2019-06-26 stsp if (worktree) {
5538 4e759de4 2019-06-26 stsp repo_path =
5539 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5540 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5541 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5542 4e759de4 2019-06-26 stsp if (error)
5543 4e759de4 2019-06-26 stsp goto done;
5545 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5546 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5547 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5548 4e759de4 2019-06-26 stsp goto done;
5553 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5554 4e759de4 2019-06-26 stsp if (error != NULL)
5555 4e759de4 2019-06-26 stsp goto done;
5557 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5558 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5559 4e759de4 2019-06-26 stsp if (error)
5560 4e759de4 2019-06-26 stsp goto done;
5562 ad89fa31 2019-10-04 stsp if (do_show)
5563 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5564 ad89fa31 2019-10-04 stsp else if (do_list)
5565 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5566 4e759de4 2019-06-26 stsp else if (delref)
5567 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5569 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5570 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5571 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5572 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5573 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5574 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5575 a74f7e83 2019-11-10 stsp if (error)
5576 a74f7e83 2019-11-10 stsp goto done;
5577 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5578 da76fce2 2020-02-24 stsp if (error)
5579 da76fce2 2020-02-24 stsp goto done;
5580 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5581 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5582 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5584 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5585 da76fce2 2020-02-24 stsp if (error)
5586 da76fce2 2020-02-24 stsp goto done;
5587 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5588 da76fce2 2020-02-24 stsp worktree);
5589 da76fce2 2020-02-24 stsp if (error)
5590 da76fce2 2020-02-24 stsp goto done;
5591 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5593 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5594 da76fce2 2020-02-24 stsp goto done;
5596 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5597 da76fce2 2020-02-24 stsp free(branch_refname);
5598 da76fce2 2020-02-24 stsp if (error)
5599 da76fce2 2020-02-24 stsp goto done;
5600 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5602 da76fce2 2020-02-24 stsp if (error)
5603 da76fce2 2020-02-24 stsp goto done;
5604 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5605 da76fce2 2020-02-24 stsp commit_id);
5606 da76fce2 2020-02-24 stsp if (error)
5607 da76fce2 2020-02-24 stsp goto done;
5608 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5609 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5610 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5612 da76fce2 2020-02-24 stsp if (error)
5613 da76fce2 2020-02-24 stsp goto done;
5614 9627c110 2020-04-18 stsp if (upa.did_something)
5615 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5616 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5621 da76fce2 2020-02-24 stsp got_ref_close(ref);
5623 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5624 d0eebce4 2019-03-11 stsp if (worktree)
5625 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5626 d0eebce4 2019-03-11 stsp free(cwd);
5627 d0eebce4 2019-03-11 stsp free(repo_path);
5628 da76fce2 2020-02-24 stsp free(commit_id);
5629 da76fce2 2020-02-24 stsp free(commit_id_str);
5630 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5631 da76fce2 2020-02-24 stsp free((char *)pe->path);
5632 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5633 d00136be 2019-03-26 stsp return error;
5637 d00136be 2019-03-26 stsp __dead static void
5638 8e7bd50a 2019-08-22 stsp usage_tag(void)
5640 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5641 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5642 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5647 b8bad2ba 2019-08-23 stsp static const struct got_error *
5648 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5650 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5651 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5652 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5653 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5654 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5656 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5657 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5658 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5659 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5661 b8bad2ba 2019-08-23 stsp return err;
5662 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5665 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5668 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5669 b8bad2ba 2019-08-23 stsp free(re_id);
5672 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5673 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5676 b8bad2ba 2019-08-23 stsp while (se) {
5677 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5680 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5681 b8bad2ba 2019-08-23 stsp free(se_id);
5684 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5685 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5687 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5688 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5690 b8bad2ba 2019-08-23 stsp return err;
5691 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5694 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5699 b8bad2ba 2019-08-23 stsp return err;
5703 b8bad2ba 2019-08-23 stsp static const struct got_error *
5704 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5706 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5707 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5708 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5710 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5712 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5714 8e7bd50a 2019-08-22 stsp return err;
5716 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5717 8e7bd50a 2019-08-22 stsp const char *refname;
5718 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5719 8e7bd50a 2019-08-22 stsp char datebuf[26];
5720 d4efa91b 2020-01-14 stsp const char *tagger;
5721 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5722 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5723 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5724 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5726 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5727 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5729 8e7bd50a 2019-08-22 stsp refname += 10;
5730 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5731 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5732 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5735 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5736 8e7bd50a 2019-08-22 stsp free(refstr);
5738 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5741 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5742 d4efa91b 2020-01-14 stsp if (err) {
5743 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5747 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5748 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5749 d4efa91b 2020-01-14 stsp if (err) {
5753 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5754 d4efa91b 2020-01-14 stsp tagger_time =
5755 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5756 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5762 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5763 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5764 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5765 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5769 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5770 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5771 2417344c 2019-08-23 stsp if (datestr)
5772 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5773 d4efa91b 2020-01-14 stsp if (commit)
5774 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5776 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5777 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5778 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5781 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5782 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5785 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5786 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5789 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5790 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5797 8e7bd50a 2019-08-22 stsp free(id_str);
5798 d4efa91b 2020-01-14 stsp if (commit) {
5799 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5802 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5804 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5805 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5806 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5807 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5812 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5814 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5816 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5817 8e7bd50a 2019-08-22 stsp } while (line);
5818 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5821 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5822 8e7bd50a 2019-08-22 stsp return NULL;
5825 8e7bd50a 2019-08-22 stsp static const struct got_error *
5826 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5827 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5829 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5830 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5831 f372d5cd 2019-10-21 stsp char *editor = NULL;
5832 1601cb9f 2020-09-11 naddy int initial_content_len;
5833 8e7bd50a 2019-08-22 stsp int fd = -1;
5835 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5836 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5837 8e7bd50a 2019-08-22 stsp goto done;
5840 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
5841 1601cb9f 2020-09-11 naddy "\n# tagging commit %s as %s\n",
5842 1601cb9f 2020-09-11 naddy commit_id_str, tag_name);
5843 1601cb9f 2020-09-11 naddy if (initial_content_len == -1) {
5844 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5845 8e7bd50a 2019-08-22 stsp goto done;
5848 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5850 8e7bd50a 2019-08-22 stsp goto done;
5852 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
5853 97972933 2020-09-11 stsp err = got_error_from_errno2("write", *tagmsg_path);
5854 97972933 2020-09-11 stsp goto done;
5857 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5859 8e7bd50a 2019-08-22 stsp goto done;
5860 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5862 8e7bd50a 2019-08-22 stsp free(initial_content);
5863 8e7bd50a 2019-08-22 stsp free(template);
5864 8e7bd50a 2019-08-22 stsp free(editor);
5866 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
5867 97972933 2020-09-11 stsp err = got_error_from_errno2("close", *tagmsg_path);
5869 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5870 59f86c76 2020-09-11 stsp if (err == NULL)
5871 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5872 59f86c76 2020-09-11 stsp if (err) {
5873 59f86c76 2020-09-11 stsp free(*tagmsg);
5874 59f86c76 2020-09-11 stsp *tagmsg = NULL;
5876 8e7bd50a 2019-08-22 stsp return err;
5879 8e7bd50a 2019-08-22 stsp static const struct got_error *
5880 50b0790e 2020-09-11 stsp add_tag(struct got_repository *repo, struct got_worktree *worktree,
5881 50b0790e 2020-09-11 stsp const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5883 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5884 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5885 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5886 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5887 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5888 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5889 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5892 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5893 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5894 8e7bd50a 2019-08-22 stsp * an unintended typo.
5896 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5897 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5899 50b0790e 2020-09-11 stsp err = get_author(&tagger, repo, worktree);
5901 8e7bd50a 2019-08-22 stsp return err;
5903 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5904 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5906 8e7bd50a 2019-08-22 stsp goto done;
5908 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5910 8e7bd50a 2019-08-22 stsp goto done;
5912 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5913 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5914 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5915 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5916 8e7bd50a 2019-08-22 stsp goto done;
5918 8e7bd50a 2019-08-22 stsp tag_name += 10;
5919 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5920 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5921 8e7bd50a 2019-08-22 stsp goto done;
5924 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5925 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5926 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5927 8e7bd50a 2019-08-22 stsp goto done;
5928 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5929 8e7bd50a 2019-08-22 stsp goto done;
5931 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5932 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5933 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5934 f372d5cd 2019-10-21 stsp if (err) {
5935 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5936 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5937 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5938 8e7bd50a 2019-08-22 stsp goto done;
5942 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5943 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5944 f372d5cd 2019-10-21 stsp if (err) {
5945 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5946 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5947 8e7bd50a 2019-08-22 stsp goto done;
5950 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5951 f372d5cd 2019-10-21 stsp if (err) {
5952 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5953 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5954 8e7bd50a 2019-08-22 stsp goto done;
5957 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5958 f372d5cd 2019-10-21 stsp if (err) {
5959 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5960 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5961 f372d5cd 2019-10-21 stsp goto done;
5964 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5965 f372d5cd 2019-10-21 stsp if (err) {
5966 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5967 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5968 f372d5cd 2019-10-21 stsp goto done;
5970 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5972 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5973 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5974 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5975 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5976 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5977 f372d5cd 2019-10-21 stsp free(tag_id_str);
5979 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5980 8e7bd50a 2019-08-22 stsp free(commit_id);
5981 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5982 8e7bd50a 2019-08-22 stsp free(refname);
5983 8e7bd50a 2019-08-22 stsp free(tagmsg);
5984 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5985 aba9c984 2019-09-08 stsp free(tagger);
5986 8e7bd50a 2019-08-22 stsp return err;
5989 8e7bd50a 2019-08-22 stsp static const struct got_error *
5990 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5992 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5993 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5994 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5995 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5996 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5997 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5998 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
6000 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
6001 8e7bd50a 2019-08-22 stsp switch (ch) {
6003 80106605 2020-02-24 stsp commit_id_arg = optarg;
6006 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
6009 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
6010 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6011 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
6013 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
6016 8e7bd50a 2019-08-22 stsp do_list = 1;
6019 8e7bd50a 2019-08-22 stsp usage_tag();
6020 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
6024 8e7bd50a 2019-08-22 stsp argc -= optind;
6025 8e7bd50a 2019-08-22 stsp argv += optind;
6027 8e7bd50a 2019-08-22 stsp if (do_list) {
6028 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
6030 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
6031 8e7bd50a 2019-08-22 stsp if (tagmsg)
6032 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
6033 8e7bd50a 2019-08-22 stsp if (argc > 0)
6034 8e7bd50a 2019-08-22 stsp usage_tag();
6035 80106605 2020-02-24 stsp } else if (argc != 1)
6036 8e7bd50a 2019-08-22 stsp usage_tag();
6038 a2887370 2019-08-23 stsp tag_name = argv[0];
6040 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
6041 8e7bd50a 2019-08-22 stsp if (do_list) {
6042 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6043 8e7bd50a 2019-08-22 stsp NULL) == -1)
6044 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6046 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6047 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
6048 8e7bd50a 2019-08-22 stsp err(1, "pledge");
6051 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
6052 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
6053 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
6054 8e7bd50a 2019-08-22 stsp goto done;
6057 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6058 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
6059 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
6060 8e7bd50a 2019-08-22 stsp goto done;
6062 8e7bd50a 2019-08-22 stsp error = NULL;
6063 8e7bd50a 2019-08-22 stsp if (worktree) {
6064 8e7bd50a 2019-08-22 stsp repo_path =
6065 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
6066 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
6067 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6068 8e7bd50a 2019-08-22 stsp if (error)
6069 8e7bd50a 2019-08-22 stsp goto done;
6071 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
6072 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
6073 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
6074 8e7bd50a 2019-08-22 stsp goto done;
6079 8e7bd50a 2019-08-22 stsp if (do_list) {
6080 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
6081 c9956ddf 2019-09-08 stsp if (error != NULL)
6082 c9956ddf 2019-09-08 stsp goto done;
6083 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6084 8e7bd50a 2019-08-22 stsp if (error)
6085 8e7bd50a 2019-08-22 stsp goto done;
6086 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
6088 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6089 c9956ddf 2019-09-08 stsp if (error)
6090 c9956ddf 2019-09-08 stsp goto done;
6091 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
6092 c9956ddf 2019-09-08 stsp if (error != NULL)
6093 c9956ddf 2019-09-08 stsp goto done;
6095 8e7bd50a 2019-08-22 stsp if (tagmsg) {
6096 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6097 8e7bd50a 2019-08-22 stsp if (error)
6098 8e7bd50a 2019-08-22 stsp goto done;
6101 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
6102 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
6103 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
6104 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
6105 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
6106 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
6107 8e7bd50a 2019-08-22 stsp if (error)
6108 8e7bd50a 2019-08-22 stsp goto done;
6109 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
6110 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
6111 8e7bd50a 2019-08-22 stsp if (error)
6112 8e7bd50a 2019-08-22 stsp goto done;
6113 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
6114 8e7bd50a 2019-08-22 stsp free(commit_id);
6115 8e7bd50a 2019-08-22 stsp if (error)
6116 8e7bd50a 2019-08-22 stsp goto done;
6119 50b0790e 2020-09-11 stsp error = add_tag(repo, worktree, tag_name,
6120 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6124 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
6125 8e7bd50a 2019-08-22 stsp if (worktree)
6126 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
6127 8e7bd50a 2019-08-22 stsp free(cwd);
6128 8e7bd50a 2019-08-22 stsp free(repo_path);
6129 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6130 8e7bd50a 2019-08-22 stsp free(commit_id_str);
6131 8e7bd50a 2019-08-22 stsp return error;
6134 8e7bd50a 2019-08-22 stsp __dead static void
6135 d00136be 2019-03-26 stsp usage_add(void)
6137 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6138 022fae89 2019-12-06 tracey getprogname());
6142 d00136be 2019-03-26 stsp static const struct got_error *
6143 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
6145 4e68cba3 2019-11-23 stsp while (path[0] == '/')
6147 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
6148 4e68cba3 2019-11-23 stsp return NULL;
6151 4e68cba3 2019-11-23 stsp static const struct got_error *
6152 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
6154 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
6155 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
6156 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
6157 1dd54920 2019-05-11 stsp char *cwd = NULL;
6158 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
6159 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
6160 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
6162 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
6164 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
6165 d00136be 2019-03-26 stsp switch (ch) {
6166 022fae89 2019-12-06 tracey case 'I':
6167 022fae89 2019-12-06 tracey no_ignores = 1;
6170 4e68cba3 2019-11-23 stsp can_recurse = 1;
6173 d00136be 2019-03-26 stsp usage_add();
6174 d00136be 2019-03-26 stsp /* NOTREACHED */
6178 d00136be 2019-03-26 stsp argc -= optind;
6179 d00136be 2019-03-26 stsp argv += optind;
6181 43012d58 2019-07-14 stsp #ifndef PROFILE
6182 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6183 43012d58 2019-07-14 stsp NULL) == -1)
6184 43012d58 2019-07-14 stsp err(1, "pledge");
6186 723c305c 2019-05-11 jcs if (argc < 1)
6187 d00136be 2019-03-26 stsp usage_add();
6189 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
6190 d00136be 2019-03-26 stsp if (cwd == NULL) {
6191 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6192 d00136be 2019-03-26 stsp goto done;
6195 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6196 fa51e947 2020-03-27 stsp if (error) {
6197 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6198 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
6199 d00136be 2019-03-26 stsp goto done;
6202 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6204 031a5338 2019-03-26 stsp if (error != NULL)
6205 031a5338 2019-03-26 stsp goto done;
6207 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6208 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6209 d00136be 2019-03-26 stsp if (error)
6210 d00136be 2019-03-26 stsp goto done;
6212 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6213 6d022e97 2019-08-04 stsp if (error)
6214 022fae89 2019-12-06 tracey goto done;
6216 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
6217 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6218 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
6219 6d022e97 2019-08-04 stsp goto done;
6223 4e68cba3 2019-11-23 stsp if (!can_recurse) {
6224 4e68cba3 2019-11-23 stsp char *ondisk_path;
6225 4e68cba3 2019-11-23 stsp struct stat sb;
6226 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
6227 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
6228 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
6229 4e68cba3 2019-11-23 stsp pe->path) == -1) {
6230 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
6231 4e68cba3 2019-11-23 stsp goto done;
6233 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
6234 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
6235 4e68cba3 2019-11-23 stsp free(ondisk_path);
6238 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
6239 4e68cba3 2019-11-23 stsp ondisk_path);
6240 4e68cba3 2019-11-23 stsp free(ondisk_path);
6241 4e68cba3 2019-11-23 stsp goto done;
6243 4e68cba3 2019-11-23 stsp free(ondisk_path);
6244 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
6245 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6246 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
6247 4e68cba3 2019-11-23 stsp goto done;
6252 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
6253 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
6256 031a5338 2019-03-26 stsp got_repo_close(repo);
6257 d00136be 2019-03-26 stsp if (worktree)
6258 d00136be 2019-03-26 stsp got_worktree_close(worktree);
6259 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
6260 1dd54920 2019-05-11 stsp free((char *)pe->path);
6261 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
6262 2ec1f75b 2019-03-26 stsp free(cwd);
6263 2ec1f75b 2019-03-26 stsp return error;
6266 2ec1f75b 2019-03-26 stsp __dead static void
6267 648e4ef7 2019-07-09 stsp usage_remove(void)
6269 766841c2 2020-08-13 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6270 766841c2 2020-08-13 stsp "path ...\n", getprogname());
6274 2a06fe5f 2019-08-24 stsp static const struct got_error *
6275 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
6276 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
6278 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
6280 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
6281 2a06fe5f 2019-08-24 stsp return NULL;
6282 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
6283 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
6284 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
6285 2a06fe5f 2019-08-24 stsp return NULL;
6288 2ec1f75b 2019-03-26 stsp static const struct got_error *
6289 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
6291 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6292 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6293 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6294 766841c2 2020-08-13 stsp const char *status_codes = NULL;
6295 17ed4618 2019-06-02 stsp char *cwd = NULL;
6296 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6297 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6298 766841c2 2020-08-13 stsp int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6300 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6302 766841c2 2020-08-13 stsp while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6303 2ec1f75b 2019-03-26 stsp switch (ch) {
6305 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6307 70e3e7f5 2019-12-13 tracey case 'k':
6308 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6310 f2a9dc41 2019-12-13 tracey case 'R':
6311 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6314 766841c2 2020-08-13 stsp for (i = 0; i < strlen(optarg); i++) {
6315 766841c2 2020-08-13 stsp switch (optarg[i]) {
6316 766841c2 2020-08-13 stsp case GOT_STATUS_MODIFY:
6317 766841c2 2020-08-13 stsp delete_local_mods = 1;
6319 766841c2 2020-08-13 stsp case GOT_STATUS_MISSING:
6322 766841c2 2020-08-13 stsp errx(1, "invalid status code '%c'",
6323 766841c2 2020-08-13 stsp optarg[i]);
6326 766841c2 2020-08-13 stsp status_codes = optarg;
6329 f2a9dc41 2019-12-13 tracey usage_remove();
6330 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6334 2ec1f75b 2019-03-26 stsp argc -= optind;
6335 2ec1f75b 2019-03-26 stsp argv += optind;
6337 43012d58 2019-07-14 stsp #ifndef PROFILE
6338 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6339 43012d58 2019-07-14 stsp NULL) == -1)
6340 43012d58 2019-07-14 stsp err(1, "pledge");
6342 17ed4618 2019-06-02 stsp if (argc < 1)
6343 648e4ef7 2019-07-09 stsp usage_remove();
6345 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6346 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6347 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6348 2ec1f75b 2019-03-26 stsp goto done;
6350 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6351 fa51e947 2020-03-27 stsp if (error) {
6352 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6353 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6354 2ec1f75b 2019-03-26 stsp goto done;
6357 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6360 2ec1f75b 2019-03-26 stsp goto done;
6362 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6363 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6364 2ec1f75b 2019-03-26 stsp if (error)
6365 2ec1f75b 2019-03-26 stsp goto done;
6367 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6368 6d022e97 2019-08-04 stsp if (error)
6369 6d022e97 2019-08-04 stsp goto done;
6371 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6372 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6373 f2a9dc41 2019-12-13 tracey struct stat sb;
6374 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6375 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6376 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6377 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6378 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6379 f2a9dc41 2019-12-13 tracey goto done;
6381 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6382 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6383 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6384 f2a9dc41 2019-12-13 tracey continue;
6386 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6387 f2a9dc41 2019-12-13 tracey ondisk_path);
6388 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6389 f2a9dc41 2019-12-13 tracey goto done;
6391 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6392 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6393 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6394 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6395 f2a9dc41 2019-12-13 tracey goto done;
6400 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6401 766841c2 2020-08-13 stsp delete_local_mods, status_codes, print_remove_status, NULL,
6402 766841c2 2020-08-13 stsp repo, keep_on_disk);
6405 a129376b 2019-03-28 stsp got_repo_close(repo);
6406 a129376b 2019-03-28 stsp if (worktree)
6407 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6408 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6409 17ed4618 2019-06-02 stsp free((char *)pe->path);
6410 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6411 a129376b 2019-03-28 stsp free(cwd);
6412 a129376b 2019-03-28 stsp return error;
6415 a129376b 2019-03-28 stsp __dead static void
6416 a129376b 2019-03-28 stsp usage_revert(void)
6418 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6419 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6423 1ee397ad 2019-07-12 stsp static const struct got_error *
6424 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6426 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6427 fb9704af 2020-01-27 stsp return NULL;
6429 a129376b 2019-03-28 stsp while (path[0] == '/')
6431 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6432 33aa809d 2019-08-08 stsp return NULL;
6435 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6436 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6437 33aa809d 2019-08-08 stsp const char *action;
6440 33aa809d 2019-08-08 stsp static const struct got_error *
6441 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6442 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6444 33aa809d 2019-08-08 stsp char *line = NULL;
6445 33aa809d 2019-08-08 stsp size_t linesize = 0;
6446 33aa809d 2019-08-08 stsp ssize_t linelen;
6448 33aa809d 2019-08-08 stsp switch (status) {
6449 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6450 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6452 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6453 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6455 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6456 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6457 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6458 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6459 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6460 33aa809d 2019-08-08 stsp printf("%s", line);
6461 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6462 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6463 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6464 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6465 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6468 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6471 33aa809d 2019-08-08 stsp return NULL;
6474 33aa809d 2019-08-08 stsp static const struct got_error *
6475 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6476 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6478 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6479 33aa809d 2019-08-08 stsp char *line = NULL;
6480 33aa809d 2019-08-08 stsp size_t linesize = 0;
6481 33aa809d 2019-08-08 stsp ssize_t linelen;
6482 33aa809d 2019-08-08 stsp int resp = ' ';
6483 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6485 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6487 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6489 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6490 33aa809d 2019-08-08 stsp a->action);
6492 33aa809d 2019-08-08 stsp return err;
6493 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6494 33aa809d 2019-08-08 stsp if (linelen == -1) {
6495 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6496 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6497 33aa809d 2019-08-08 stsp return NULL;
6499 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6501 33aa809d 2019-08-08 stsp *nl = '\0';
6502 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6503 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6504 33aa809d 2019-08-08 stsp printf("y\n");
6505 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6506 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6507 33aa809d 2019-08-08 stsp printf("n\n");
6508 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6509 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6510 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6511 33aa809d 2019-08-08 stsp printf("q\n");
6513 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6514 33aa809d 2019-08-08 stsp free(line);
6515 33aa809d 2019-08-08 stsp return NULL;
6518 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6519 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6520 33aa809d 2019-08-08 stsp a->action);
6522 33aa809d 2019-08-08 stsp return err;
6523 33aa809d 2019-08-08 stsp resp = getchar();
6524 33aa809d 2019-08-08 stsp if (resp == '\n')
6525 33aa809d 2019-08-08 stsp resp = getchar();
6526 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6527 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6528 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6529 33aa809d 2019-08-08 stsp resp = ' ';
6531 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6532 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6533 33aa809d 2019-08-08 stsp resp = ' ';
6537 33aa809d 2019-08-08 stsp if (resp == 'y')
6538 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6539 33aa809d 2019-08-08 stsp else if (resp == 'n')
6540 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6541 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6542 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6544 1ee397ad 2019-07-12 stsp return NULL;
6548 a129376b 2019-03-28 stsp static const struct got_error *
6549 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6551 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6552 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6553 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6554 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6555 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6556 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6557 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6558 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6559 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6560 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6562 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6564 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6565 a129376b 2019-03-28 stsp switch (ch) {
6567 33aa809d 2019-08-08 stsp pflag = 1;
6570 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6573 0f6d7415 2019-08-08 stsp can_recurse = 1;
6576 a129376b 2019-03-28 stsp usage_revert();
6577 a129376b 2019-03-28 stsp /* NOTREACHED */
6581 a129376b 2019-03-28 stsp argc -= optind;
6582 a129376b 2019-03-28 stsp argv += optind;
6584 43012d58 2019-07-14 stsp #ifndef PROFILE
6585 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6586 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6587 43012d58 2019-07-14 stsp err(1, "pledge");
6589 e20a8b6f 2019-06-04 stsp if (argc < 1)
6590 a129376b 2019-03-28 stsp usage_revert();
6591 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6592 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6594 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6595 a129376b 2019-03-28 stsp if (cwd == NULL) {
6596 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6597 a129376b 2019-03-28 stsp goto done;
6599 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6600 fa51e947 2020-03-27 stsp if (error) {
6601 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6602 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6603 2ec1f75b 2019-03-26 stsp goto done;
6606 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6608 a129376b 2019-03-28 stsp if (error != NULL)
6609 a129376b 2019-03-28 stsp goto done;
6611 33aa809d 2019-08-08 stsp if (patch_script_path) {
6612 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6613 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6614 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6615 33aa809d 2019-08-08 stsp patch_script_path);
6616 33aa809d 2019-08-08 stsp goto done;
6619 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6620 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6621 a129376b 2019-03-28 stsp if (error)
6622 a129376b 2019-03-28 stsp goto done;
6624 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6625 6d022e97 2019-08-04 stsp if (error)
6626 6d022e97 2019-08-04 stsp goto done;
6628 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6629 0f6d7415 2019-08-08 stsp char *ondisk_path;
6630 0f6d7415 2019-08-08 stsp struct stat sb;
6631 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6632 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6633 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6634 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6635 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6636 0f6d7415 2019-08-08 stsp goto done;
6638 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6639 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6640 0f6d7415 2019-08-08 stsp free(ondisk_path);
6643 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6644 0f6d7415 2019-08-08 stsp ondisk_path);
6645 0f6d7415 2019-08-08 stsp free(ondisk_path);
6646 0f6d7415 2019-08-08 stsp goto done;
6648 0f6d7415 2019-08-08 stsp free(ondisk_path);
6649 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6650 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6651 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6652 0f6d7415 2019-08-08 stsp goto done;
6657 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6658 33aa809d 2019-08-08 stsp cpa.action = "revert";
6659 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6660 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6662 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6663 33aa809d 2019-08-08 stsp error == NULL)
6664 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6666 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6667 2ec1f75b 2019-03-26 stsp if (worktree)
6668 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6669 2ec1f75b 2019-03-26 stsp free(path);
6670 d00136be 2019-03-26 stsp free(cwd);
6671 6bad629b 2019-02-04 stsp return error;
6674 c4296144 2019-05-09 stsp __dead static void
6675 c4296144 2019-05-09 stsp usage_commit(void)
6677 35213c7c 2020-07-23 stsp fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6678 5c1e53bc 2019-07-28 stsp getprogname());
6682 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6683 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6684 e2ba3d07 2019-05-13 stsp const char *editor;
6685 e0870e44 2019-05-13 stsp const char *worktree_path;
6686 76d98825 2019-06-03 stsp const char *branch_name;
6687 314a6357 2019-05-13 stsp const char *repo_path;
6688 e0870e44 2019-05-13 stsp char *logmsg_path;
6692 33ad4cbe 2019-05-12 jcs static const struct got_error *
6693 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6696 76d98825 2019-06-03 stsp char *initial_content = NULL;
6697 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6698 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6699 e0870e44 2019-05-13 stsp char *template = NULL;
6700 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6701 1601cb9f 2020-09-11 naddy int initial_content_len;
6702 97972933 2020-09-11 stsp int fd = -1;
6703 33ad4cbe 2019-05-12 jcs size_t len;
6705 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6706 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6707 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6708 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6709 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6710 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6711 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6712 33ad4cbe 2019-05-12 jcs return NULL;
6715 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6716 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6718 1601cb9f 2020-09-11 naddy initial_content_len = asprintf(&initial_content,
6719 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6720 1601cb9f 2020-09-11 naddy a->branch_name);
6721 1601cb9f 2020-09-11 naddy if (initial_content_len == -1)
6722 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6724 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6726 e0870e44 2019-05-13 stsp goto done;
6728 97972933 2020-09-11 stsp if (write(fd, initial_content, initial_content_len) == -1) {
6729 97972933 2020-09-11 stsp err = got_error_from_errno2("write", a->logmsg_path);
6730 97972933 2020-09-11 stsp goto done;
6733 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6734 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6735 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6736 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6737 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6740 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6742 76d98825 2019-06-03 stsp free(initial_content);
6743 e0870e44 2019-05-13 stsp free(template);
6745 97972933 2020-09-11 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
6746 97972933 2020-09-11 stsp err = got_error_from_errno2("close", a->logmsg_path);
6748 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6749 59f86c76 2020-09-11 stsp if (err == NULL)
6750 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6751 59f86c76 2020-09-11 stsp if (err) {
6752 59f86c76 2020-09-11 stsp free(*logmsg);
6753 59f86c76 2020-09-11 stsp *logmsg = NULL;
6755 33ad4cbe 2019-05-12 jcs return err;
6758 c4296144 2019-05-09 stsp static const struct got_error *
6759 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6761 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6762 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6763 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6764 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6765 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6766 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6767 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6768 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6769 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6770 35213c7c 2020-07-23 stsp int allow_bad_symlinks = 0;
6771 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6773 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6774 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6776 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "m:S")) != -1) {
6777 c4296144 2019-05-09 stsp switch (ch) {
6779 c4296144 2019-05-09 stsp logmsg = optarg;
6782 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
6785 c4296144 2019-05-09 stsp usage_commit();
6786 c4296144 2019-05-09 stsp /* NOTREACHED */
6790 c4296144 2019-05-09 stsp argc -= optind;
6791 c4296144 2019-05-09 stsp argv += optind;
6793 43012d58 2019-07-14 stsp #ifndef PROFILE
6794 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6795 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6796 43012d58 2019-07-14 stsp err(1, "pledge");
6798 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6799 c4296144 2019-05-09 stsp if (cwd == NULL) {
6800 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6801 c4296144 2019-05-09 stsp goto done;
6803 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6804 fa51e947 2020-03-27 stsp if (error) {
6805 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6806 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6807 7d5807f4 2019-07-11 stsp goto done;
6810 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6811 c4296144 2019-05-09 stsp if (error)
6812 a698f62e 2019-07-25 stsp goto done;
6813 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6814 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6815 c4296144 2019-05-09 stsp goto done;
6818 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6819 916f288c 2019-07-30 stsp worktree);
6820 5c1e53bc 2019-07-28 stsp if (error)
6821 5c1e53bc 2019-07-28 stsp goto done;
6823 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6824 c9956ddf 2019-09-08 stsp if (error)
6825 c9956ddf 2019-09-08 stsp goto done;
6826 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6827 c9956ddf 2019-09-08 stsp gitconfig_path);
6828 c4296144 2019-05-09 stsp if (error != NULL)
6829 c4296144 2019-05-09 stsp goto done;
6831 50b0790e 2020-09-11 stsp error = get_author(&author, repo, worktree);
6832 aba9c984 2019-09-08 stsp if (error)
6833 aba9c984 2019-09-08 stsp return error;
6836 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6837 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6839 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6840 314a6357 2019-05-13 stsp error = get_editor(&editor);
6842 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6843 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6844 c4296144 2019-05-09 stsp if (error)
6845 c4296144 2019-05-09 stsp goto done;
6847 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6848 087fb88c 2019-08-04 stsp if (error)
6849 087fb88c 2019-08-04 stsp goto done;
6851 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6852 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6853 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6854 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6855 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6856 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6857 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6858 916f288c 2019-07-30 stsp goto done;
6860 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6862 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6863 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6864 35213c7c 2020-07-23 stsp allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6865 35213c7c 2020-07-23 stsp print_status, NULL, repo);
6866 e0870e44 2019-05-13 stsp if (error) {
6867 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6868 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6869 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6870 c4296144 2019-05-09 stsp goto done;
6873 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6874 c4296144 2019-05-09 stsp if (error)
6875 c4296144 2019-05-09 stsp goto done;
6876 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6878 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6879 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6880 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6881 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6882 7266f21f 2019-10-21 stsp error == NULL)
6883 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6884 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6886 c4296144 2019-05-09 stsp got_repo_close(repo);
6887 c4296144 2019-05-09 stsp if (worktree)
6888 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6889 c4296144 2019-05-09 stsp free(cwd);
6890 c4296144 2019-05-09 stsp free(id_str);
6891 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6892 e2ba3d07 2019-05-13 stsp free(editor);
6893 aba9c984 2019-09-08 stsp free(author);
6894 234035bc 2019-06-01 stsp return error;
6897 234035bc 2019-06-01 stsp __dead static void
6898 234035bc 2019-06-01 stsp usage_cherrypick(void)
6900 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6904 234035bc 2019-06-01 stsp static const struct got_error *
6905 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6907 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6908 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6909 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6910 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6911 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6912 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6913 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6914 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6916 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6918 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6919 234035bc 2019-06-01 stsp switch (ch) {
6921 234035bc 2019-06-01 stsp usage_cherrypick();
6922 234035bc 2019-06-01 stsp /* NOTREACHED */
6926 234035bc 2019-06-01 stsp argc -= optind;
6927 234035bc 2019-06-01 stsp argv += optind;
6929 43012d58 2019-07-14 stsp #ifndef PROFILE
6930 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6931 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6932 43012d58 2019-07-14 stsp err(1, "pledge");
6934 234035bc 2019-06-01 stsp if (argc != 1)
6935 234035bc 2019-06-01 stsp usage_cherrypick();
6937 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6938 234035bc 2019-06-01 stsp if (cwd == NULL) {
6939 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6940 234035bc 2019-06-01 stsp goto done;
6942 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6943 fa51e947 2020-03-27 stsp if (error) {
6944 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6945 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6947 234035bc 2019-06-01 stsp goto done;
6950 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6952 234035bc 2019-06-01 stsp if (error != NULL)
6953 234035bc 2019-06-01 stsp goto done;
6955 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6956 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6957 234035bc 2019-06-01 stsp if (error)
6958 234035bc 2019-06-01 stsp goto done;
6960 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6961 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6962 234035bc 2019-06-01 stsp if (error != NULL) {
6963 234035bc 2019-06-01 stsp struct got_reference *ref;
6964 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6965 234035bc 2019-06-01 stsp goto done;
6966 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6967 234035bc 2019-06-01 stsp if (error != NULL)
6968 234035bc 2019-06-01 stsp goto done;
6969 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6970 234035bc 2019-06-01 stsp got_ref_close(ref);
6971 234035bc 2019-06-01 stsp if (error != NULL)
6972 234035bc 2019-06-01 stsp goto done;
6974 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6975 234035bc 2019-06-01 stsp if (error)
6976 234035bc 2019-06-01 stsp goto done;
6978 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6979 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6980 234035bc 2019-06-01 stsp if (error != NULL)
6981 234035bc 2019-06-01 stsp goto done;
6983 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6984 234035bc 2019-06-01 stsp if (error) {
6985 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6986 234035bc 2019-06-01 stsp goto done;
6987 234035bc 2019-06-01 stsp error = NULL;
6989 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6990 234035bc 2019-06-01 stsp goto done;
6993 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6994 234035bc 2019-06-01 stsp if (error)
6995 234035bc 2019-06-01 stsp goto done;
6996 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6997 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6998 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6999 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
7001 234035bc 2019-06-01 stsp if (error != NULL)
7002 234035bc 2019-06-01 stsp goto done;
7004 9627c110 2020-04-18 stsp if (upa.did_something)
7005 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
7006 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7008 234035bc 2019-06-01 stsp if (commit)
7009 234035bc 2019-06-01 stsp got_object_commit_close(commit);
7010 234035bc 2019-06-01 stsp free(commit_id_str);
7011 234035bc 2019-06-01 stsp if (head_ref)
7012 234035bc 2019-06-01 stsp got_ref_close(head_ref);
7013 234035bc 2019-06-01 stsp if (worktree)
7014 234035bc 2019-06-01 stsp got_worktree_close(worktree);
7016 234035bc 2019-06-01 stsp got_repo_close(repo);
7017 c4296144 2019-05-09 stsp return error;
7020 5ef14e63 2019-06-02 stsp __dead static void
7021 5ef14e63 2019-06-02 stsp usage_backout(void)
7023 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
7027 5ef14e63 2019-06-02 stsp static const struct got_error *
7028 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
7030 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
7031 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
7032 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
7033 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
7034 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
7035 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
7036 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
7037 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
7039 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7041 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7042 5ef14e63 2019-06-02 stsp switch (ch) {
7044 5ef14e63 2019-06-02 stsp usage_backout();
7045 5ef14e63 2019-06-02 stsp /* NOTREACHED */
7049 5ef14e63 2019-06-02 stsp argc -= optind;
7050 5ef14e63 2019-06-02 stsp argv += optind;
7052 43012d58 2019-07-14 stsp #ifndef PROFILE
7053 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7054 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7055 43012d58 2019-07-14 stsp err(1, "pledge");
7057 5ef14e63 2019-06-02 stsp if (argc != 1)
7058 5ef14e63 2019-06-02 stsp usage_backout();
7060 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
7061 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
7062 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
7063 5ef14e63 2019-06-02 stsp goto done;
7065 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
7066 fa51e947 2020-03-27 stsp if (error) {
7067 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7068 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
7069 5ef14e63 2019-06-02 stsp goto done;
7072 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7074 5ef14e63 2019-06-02 stsp if (error != NULL)
7075 5ef14e63 2019-06-02 stsp goto done;
7077 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7078 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7079 5ef14e63 2019-06-02 stsp if (error)
7080 5ef14e63 2019-06-02 stsp goto done;
7082 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7083 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
7084 5ef14e63 2019-06-02 stsp if (error != NULL) {
7085 5ef14e63 2019-06-02 stsp struct got_reference *ref;
7086 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7087 5ef14e63 2019-06-02 stsp goto done;
7088 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
7089 5ef14e63 2019-06-02 stsp if (error != NULL)
7090 5ef14e63 2019-06-02 stsp goto done;
7091 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
7092 5ef14e63 2019-06-02 stsp got_ref_close(ref);
7093 5ef14e63 2019-06-02 stsp if (error != NULL)
7094 5ef14e63 2019-06-02 stsp goto done;
7096 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
7097 5ef14e63 2019-06-02 stsp if (error)
7098 5ef14e63 2019-06-02 stsp goto done;
7100 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
7101 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
7102 5ef14e63 2019-06-02 stsp if (error != NULL)
7103 5ef14e63 2019-06-02 stsp goto done;
7105 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
7106 5ef14e63 2019-06-02 stsp if (error)
7107 5ef14e63 2019-06-02 stsp goto done;
7109 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7110 5ef14e63 2019-06-02 stsp if (error)
7111 5ef14e63 2019-06-02 stsp goto done;
7112 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7113 5ef14e63 2019-06-02 stsp if (pid == NULL) {
7114 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
7115 5ef14e63 2019-06-02 stsp goto done;
7118 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7119 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7120 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7121 5ef14e63 2019-06-02 stsp if (error != NULL)
7122 5ef14e63 2019-06-02 stsp goto done;
7124 9627c110 2020-04-18 stsp if (upa.did_something)
7125 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
7126 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7128 5ef14e63 2019-06-02 stsp if (commit)
7129 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
7130 5ef14e63 2019-06-02 stsp free(commit_id_str);
7131 5ef14e63 2019-06-02 stsp if (head_ref)
7132 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
7133 818c7501 2019-07-11 stsp if (worktree)
7134 818c7501 2019-07-11 stsp got_worktree_close(worktree);
7136 818c7501 2019-07-11 stsp got_repo_close(repo);
7137 818c7501 2019-07-11 stsp return error;
7140 818c7501 2019-07-11 stsp __dead static void
7141 818c7501 2019-07-11 stsp usage_rebase(void)
7143 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7144 af54c8f8 2019-07-11 stsp getprogname());
7149 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
7152 0ebf8283 2019-07-24 stsp size_t len;
7154 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
7155 0ebf8283 2019-07-24 stsp if (len > limit)
7156 0ebf8283 2019-07-24 stsp len = limit;
7157 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
7158 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
7160 0ebf8283 2019-07-24 stsp *nl = '\0';
7163 0ebf8283 2019-07-24 stsp static const struct got_error *
7164 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7166 5943eee2 2019-08-13 stsp const struct got_error *err;
7167 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
7168 5943eee2 2019-08-13 stsp const char *s;
7170 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
7172 5943eee2 2019-08-13 stsp return err;
7174 5943eee2 2019-08-13 stsp s = logmsg0;
7175 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
7178 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
7179 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
7180 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
7181 5943eee2 2019-08-13 stsp goto done;
7184 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
7186 5943eee2 2019-08-13 stsp free(logmsg0);
7187 a0ea4fc0 2020-02-28 stsp return err;
7190 a0ea4fc0 2020-02-28 stsp static const struct got_error *
7191 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7193 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
7194 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
7195 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
7197 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
7199 a0ea4fc0 2020-02-28 stsp return err;
7201 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
7203 a0ea4fc0 2020-02-28 stsp goto done;
7205 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
7207 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
7209 a0ea4fc0 2020-02-28 stsp goto done;
7211 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
7213 a0ea4fc0 2020-02-28 stsp free(id_str);
7214 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
7215 a0ea4fc0 2020-02-28 stsp free(logmsg);
7216 5943eee2 2019-08-13 stsp return err;
7219 818c7501 2019-07-11 stsp static const struct got_error *
7220 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
7221 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
7223 818c7501 2019-07-11 stsp const struct got_error *err;
7224 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7226 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
7228 818c7501 2019-07-11 stsp goto done;
7230 ff0d2220 2019-07-11 stsp if (new_id) {
7231 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
7233 ff0d2220 2019-07-11 stsp goto done;
7236 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
7237 ff0d2220 2019-07-11 stsp if (new_id_str)
7238 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
7240 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7242 0ebf8283 2019-07-24 stsp goto done;
7244 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
7245 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7247 818c7501 2019-07-11 stsp free(old_id_str);
7248 818c7501 2019-07-11 stsp free(new_id_str);
7249 272a1371 2020-02-28 stsp free(logmsg);
7250 818c7501 2019-07-11 stsp return err;
7253 1ee397ad 2019-07-12 stsp static const struct got_error *
7254 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7255 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
7256 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
7258 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
7259 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
7260 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
7263 818c7501 2019-07-11 stsp static const struct got_error *
7264 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
7265 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7266 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
7267 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
7269 ff0d2220 2019-07-11 stsp const struct got_error *error;
7270 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
7271 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
7273 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7274 ff0d2220 2019-07-11 stsp if (error)
7275 ff0d2220 2019-07-11 stsp return error;
7277 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7278 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
7279 ff0d2220 2019-07-11 stsp if (error) {
7280 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7281 ff0d2220 2019-07-11 stsp goto done;
7282 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
7284 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
7285 ff0d2220 2019-07-11 stsp free(new_commit_id);
7288 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
7289 ff0d2220 2019-07-11 stsp return error;
7292 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
7293 64c6d990 2019-07-11 stsp const char *path_prefix;
7294 64c6d990 2019-07-11 stsp size_t len;
7295 8ca9bd68 2019-07-25 stsp int errcode;
7298 64c6d990 2019-07-11 stsp static const struct got_error *
7299 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7300 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
7301 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
7302 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
7304 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
7306 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7307 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7308 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
7310 64c6d990 2019-07-11 stsp return NULL;
7313 64c6d990 2019-07-11 stsp static const struct got_error *
7314 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
7315 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
7316 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
7318 64c6d990 2019-07-11 stsp const struct got_error *err;
7319 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7320 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7321 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7323 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7324 64c6d990 2019-07-11 stsp return NULL;
7326 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7328 64c6d990 2019-07-11 stsp goto done;
7330 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7332 64c6d990 2019-07-11 stsp goto done;
7334 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7335 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7337 64c6d990 2019-07-11 stsp goto done;
7339 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7340 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7342 64c6d990 2019-07-11 stsp goto done;
7344 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7345 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7346 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7347 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7348 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7349 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7350 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7352 64c6d990 2019-07-11 stsp if (tree1)
7353 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7354 64c6d990 2019-07-11 stsp if (tree2)
7355 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7356 64c6d990 2019-07-11 stsp if (commit)
7357 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7358 64c6d990 2019-07-11 stsp if (parent_commit)
7359 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7360 64c6d990 2019-07-11 stsp return err;
7363 ff0d2220 2019-07-11 stsp static const struct got_error *
7364 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7365 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7366 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7367 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7368 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7370 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7371 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7372 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7373 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7374 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7376 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7378 af61c510 2019-07-19 stsp return err;
7380 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7381 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7383 af61c510 2019-07-19 stsp goto done;
7384 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7385 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7386 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7387 af61c510 2019-07-19 stsp if (err) {
7388 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7389 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7390 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7391 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7392 af61c510 2019-07-19 stsp "been reached?!?");
7394 ee780d5c 2020-01-04 stsp goto done;
7396 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7397 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7399 af61c510 2019-07-19 stsp goto done;
7401 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7403 af61c510 2019-07-19 stsp goto done;
7404 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7405 af61c510 2019-07-19 stsp commit_id = parent_id;
7409 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7410 af61c510 2019-07-19 stsp return err;
7413 af61c510 2019-07-19 stsp static const struct got_error *
7414 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7416 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7417 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7418 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7419 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7420 818c7501 2019-07-11 stsp char *cwd = NULL;
7421 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7422 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7423 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7424 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7425 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7426 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7427 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7428 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7429 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7430 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7431 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7432 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7433 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7435 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7436 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7438 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7439 818c7501 2019-07-11 stsp switch (ch) {
7441 818c7501 2019-07-11 stsp abort_rebase = 1;
7444 818c7501 2019-07-11 stsp continue_rebase = 1;
7447 818c7501 2019-07-11 stsp usage_rebase();
7448 818c7501 2019-07-11 stsp /* NOTREACHED */
7452 818c7501 2019-07-11 stsp argc -= optind;
7453 818c7501 2019-07-11 stsp argv += optind;
7455 43012d58 2019-07-14 stsp #ifndef PROFILE
7456 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7457 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7458 43012d58 2019-07-14 stsp err(1, "pledge");
7460 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7461 818c7501 2019-07-11 stsp usage_rebase();
7462 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7463 818c7501 2019-07-11 stsp if (argc != 0)
7464 818c7501 2019-07-11 stsp usage_rebase();
7465 818c7501 2019-07-11 stsp } else if (argc != 1)
7466 818c7501 2019-07-11 stsp usage_rebase();
7468 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7469 818c7501 2019-07-11 stsp if (cwd == NULL) {
7470 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7471 818c7501 2019-07-11 stsp goto done;
7473 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7474 fa51e947 2020-03-27 stsp if (error) {
7475 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7476 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7477 818c7501 2019-07-11 stsp goto done;
7480 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7482 818c7501 2019-07-11 stsp if (error != NULL)
7483 818c7501 2019-07-11 stsp goto done;
7485 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7486 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7487 7ef62c4e 2020-02-24 stsp if (error)
7488 7ef62c4e 2020-02-24 stsp goto done;
7490 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7491 7ef62c4e 2020-02-24 stsp worktree);
7492 818c7501 2019-07-11 stsp if (error)
7493 7ef62c4e 2020-02-24 stsp goto done;
7494 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7495 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7496 818c7501 2019-07-11 stsp goto done;
7499 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7500 818c7501 2019-07-11 stsp if (error)
7501 818c7501 2019-07-11 stsp goto done;
7503 f6794adc 2019-07-23 stsp if (abort_rebase) {
7504 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7505 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7506 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7507 f6794adc 2019-07-23 stsp goto done;
7509 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7510 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7511 3e3a69f1 2019-07-25 stsp worktree, repo);
7512 818c7501 2019-07-11 stsp if (error)
7513 818c7501 2019-07-11 stsp goto done;
7514 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7515 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7516 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7517 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7518 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7519 818c7501 2019-07-11 stsp if (error)
7520 818c7501 2019-07-11 stsp goto done;
7521 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7522 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7523 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7526 818c7501 2019-07-11 stsp if (continue_rebase) {
7527 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7528 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7529 f6794adc 2019-07-23 stsp goto done;
7531 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7532 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7533 3e3a69f1 2019-07-25 stsp worktree, repo);
7534 818c7501 2019-07-11 stsp if (error)
7535 818c7501 2019-07-11 stsp goto done;
7537 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7538 01757395 2019-07-12 stsp resume_commit_id, repo);
7539 818c7501 2019-07-11 stsp if (error)
7540 818c7501 2019-07-11 stsp goto done;
7542 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7543 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7544 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7545 818c7501 2019-07-11 stsp goto done;
7548 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7549 818c7501 2019-07-11 stsp if (error != NULL)
7550 818c7501 2019-07-11 stsp goto done;
7553 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7554 ff0d2220 2019-07-11 stsp if (error)
7555 ff0d2220 2019-07-11 stsp goto done;
7557 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7558 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7560 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7561 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7562 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7563 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7564 818c7501 2019-07-11 stsp if (error)
7565 818c7501 2019-07-11 stsp goto done;
7566 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7567 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7568 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7569 818c7501 2019-07-11 stsp "with work tree's branch");
7570 818c7501 2019-07-11 stsp goto done;
7573 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7574 a51a74b3 2019-07-27 stsp if (error) {
7575 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7576 a51a74b3 2019-07-27 stsp goto done;
7577 a51a74b3 2019-07-27 stsp error = NULL;
7579 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7580 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7581 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7582 a51a74b3 2019-07-27 stsp goto done;
7584 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7585 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7586 818c7501 2019-07-11 stsp if (error)
7587 818c7501 2019-07-11 stsp goto done;
7590 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7591 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7592 818c7501 2019-07-11 stsp if (error)
7593 818c7501 2019-07-11 stsp goto done;
7595 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7596 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7597 fc66b545 2019-08-12 stsp if (pid == NULL) {
7598 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7599 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7600 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7601 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7602 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7603 fc66b545 2019-08-12 stsp if (error)
7604 fc66b545 2019-08-12 stsp goto done;
7605 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7606 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7607 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7610 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7611 fc66b545 2019-08-12 stsp goto done;
7613 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7614 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7615 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7616 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7617 818c7501 2019-07-11 stsp commit = NULL;
7618 818c7501 2019-07-11 stsp if (error)
7619 818c7501 2019-07-11 stsp goto done;
7621 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7622 38b0338b 2019-11-29 stsp if (continue_rebase) {
7623 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7624 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7625 38b0338b 2019-11-29 stsp goto done;
7627 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7628 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7629 38b0338b 2019-11-29 stsp char *id_str;
7630 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7631 38b0338b 2019-11-29 stsp new_base_branch);
7632 38b0338b 2019-11-29 stsp if (error)
7633 38b0338b 2019-11-29 stsp goto done;
7634 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7635 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7636 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7637 38b0338b 2019-11-29 stsp free(id_str);
7638 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7639 38b0338b 2019-11-29 stsp new_head_commit_id);
7640 38b0338b 2019-11-29 stsp if (error)
7641 38b0338b 2019-11-29 stsp goto done;
7645 818c7501 2019-07-11 stsp pid = NULL;
7646 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7647 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7649 818c7501 2019-07-11 stsp commit_id = qid->id;
7650 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7651 818c7501 2019-07-11 stsp pid = qid;
7653 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7654 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7655 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7656 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7657 818c7501 2019-07-11 stsp if (error)
7658 818c7501 2019-07-11 stsp goto done;
7660 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7661 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7662 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7664 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7665 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7666 a0ea4fc0 2020-02-28 stsp if (error)
7667 a0ea4fc0 2020-02-28 stsp goto done;
7668 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7672 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7673 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7674 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7675 818c7501 2019-07-11 stsp if (error)
7676 818c7501 2019-07-11 stsp goto done;
7679 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7680 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7681 818c7501 2019-07-11 stsp if (error)
7682 818c7501 2019-07-11 stsp goto done;
7683 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7684 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7686 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7687 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7689 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7690 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7691 818c7501 2019-07-11 stsp free(resume_commit_id);
7692 818c7501 2019-07-11 stsp free(yca_id);
7693 818c7501 2019-07-11 stsp if (commit)
7694 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7695 818c7501 2019-07-11 stsp if (branch)
7696 818c7501 2019-07-11 stsp got_ref_close(branch);
7697 818c7501 2019-07-11 stsp if (new_base_branch)
7698 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7699 818c7501 2019-07-11 stsp if (tmp_branch)
7700 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7701 5ef14e63 2019-06-02 stsp if (worktree)
7702 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7704 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7705 5ef14e63 2019-06-02 stsp return error;
7708 0ebf8283 2019-07-24 stsp __dead static void
7709 0ebf8283 2019-07-24 stsp usage_histedit(void)
7711 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7712 0ebf8283 2019-07-24 stsp getprogname());
7716 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7717 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7718 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7719 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7720 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7722 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7723 0ebf8283 2019-07-24 stsp unsigned char code;
7724 0ebf8283 2019-07-24 stsp const char *name;
7725 0ebf8283 2019-07-24 stsp const char *desc;
7726 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7727 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7728 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7729 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7730 82997472 2020-01-29 stsp "be used" },
7731 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7732 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7733 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7736 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7737 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7738 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7739 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7740 0ebf8283 2019-07-24 stsp char *logmsg;
7742 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7744 0ebf8283 2019-07-24 stsp static const struct got_error *
7745 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7746 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7748 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7749 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7750 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7753 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7755 0ebf8283 2019-07-24 stsp goto done;
7757 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7759 0ebf8283 2019-07-24 stsp goto done;
7761 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7763 0ebf8283 2019-07-24 stsp goto done;
7765 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7766 0ebf8283 2019-07-24 stsp if (n < 0)
7767 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7769 0ebf8283 2019-07-24 stsp if (commit)
7770 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7771 0ebf8283 2019-07-24 stsp free(id_str);
7772 0ebf8283 2019-07-24 stsp free(logmsg);
7773 0ebf8283 2019-07-24 stsp return err;
7776 0ebf8283 2019-07-24 stsp static const struct got_error *
7777 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7778 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7780 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7781 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7783 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7784 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7786 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7787 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7791 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7792 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7793 083957f4 2020-02-24 stsp if (n < 0) {
7794 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7800 0ebf8283 2019-07-24 stsp return err;
7803 0ebf8283 2019-07-24 stsp static const struct got_error *
7804 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7805 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7807 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7809 514f2ffe 2020-01-29 stsp char *id_str;
7810 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7812 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7813 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7815 514f2ffe 2020-01-29 stsp return err;
7817 514f2ffe 2020-01-29 stsp n = fprintf(f,
7818 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7819 514f2ffe 2020-01-29 stsp "# commit %s\n"
7820 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7821 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7822 514f2ffe 2020-01-29 stsp if (n < 0) {
7823 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7824 514f2ffe 2020-01-29 stsp goto done;
7827 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7828 514f2ffe 2020-01-29 stsp if (n < 0) {
7829 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7830 514f2ffe 2020-01-29 stsp goto done;
7833 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7834 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7835 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7836 0ebf8283 2019-07-24 stsp cmd->desc);
7837 0ebf8283 2019-07-24 stsp if (n < 0) {
7838 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7843 514f2ffe 2020-01-29 stsp free(id_str);
7844 0ebf8283 2019-07-24 stsp return err;
7847 0ebf8283 2019-07-24 stsp static const struct got_error *
7848 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7850 0ebf8283 2019-07-24 stsp static char msg[42];
7853 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7855 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7856 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7858 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7861 0ebf8283 2019-07-24 stsp static const struct got_error *
7862 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7863 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7865 0ebf8283 2019-07-24 stsp const struct got_error *err;
7866 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7867 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7869 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7871 0ebf8283 2019-07-24 stsp return err;
7873 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7875 0ebf8283 2019-07-24 stsp goto done;
7877 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7879 5943eee2 2019-08-13 stsp goto done;
7880 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7881 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7882 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7883 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7886 0ebf8283 2019-07-24 stsp if (folded_commit)
7887 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7888 0ebf8283 2019-07-24 stsp free(id_str);
7889 5943eee2 2019-08-13 stsp free(folded_logmsg);
7890 0ebf8283 2019-07-24 stsp return err;
7893 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7894 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7896 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7898 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7899 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7900 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7901 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7902 3f9de99f 2019-07-24 stsp folded = prev;
7903 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7906 0ebf8283 2019-07-24 stsp return folded;
7909 0ebf8283 2019-07-24 stsp static const struct got_error *
7910 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7911 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7913 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7914 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7915 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7916 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7917 1601cb9f 2020-09-11 naddy int logmsg_len;
7919 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7921 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7923 0ebf8283 2019-07-24 stsp return err;
7925 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7926 0ebf8283 2019-07-24 stsp if (folded) {
7927 0ebf8283 2019-07-24 stsp while (folded != hle) {
7928 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7929 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7932 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7933 0ebf8283 2019-07-24 stsp logmsg, repo);
7935 0ebf8283 2019-07-24 stsp goto done;
7936 0ebf8283 2019-07-24 stsp free(logmsg);
7937 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7938 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7942 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7944 0ebf8283 2019-07-24 stsp goto done;
7945 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7947 5943eee2 2019-08-13 stsp goto done;
7948 1601cb9f 2020-09-11 naddy logmsg_len = asprintf(&new_msg,
7949 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7950 1601cb9f 2020-09-11 naddy logmsg ? logmsg : "", id_str, orig_logmsg);
7951 1601cb9f 2020-09-11 naddy if (logmsg_len == -1) {
7952 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7953 0ebf8283 2019-07-24 stsp goto done;
7955 0ebf8283 2019-07-24 stsp free(logmsg);
7956 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7958 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7960 0ebf8283 2019-07-24 stsp goto done;
7962 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7963 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7965 0ebf8283 2019-07-24 stsp goto done;
7967 1601cb9f 2020-09-11 naddy write(fd, logmsg, logmsg_len);
7968 0ebf8283 2019-07-24 stsp close(fd);
7970 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7972 0ebf8283 2019-07-24 stsp goto done;
7974 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7975 0ebf8283 2019-07-24 stsp if (err) {
7976 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7977 0ebf8283 2019-07-24 stsp goto done;
7978 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7981 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7982 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7983 0ebf8283 2019-07-24 stsp free(logmsg_path);
7984 0ebf8283 2019-07-24 stsp free(logmsg);
7985 5943eee2 2019-08-13 stsp free(orig_logmsg);
7986 0ebf8283 2019-07-24 stsp free(editor);
7987 0ebf8283 2019-07-24 stsp if (commit)
7988 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7989 0ebf8283 2019-07-24 stsp return err;
7992 0ebf8283 2019-07-24 stsp static const struct got_error *
7993 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7994 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7996 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7997 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7998 0ebf8283 2019-07-24 stsp size_t size;
7999 0ebf8283 2019-07-24 stsp ssize_t len;
8000 0ebf8283 2019-07-24 stsp int lineno = 0, i;
8001 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
8002 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
8003 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
8005 0ebf8283 2019-07-24 stsp for (;;) {
8006 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
8007 0ebf8283 2019-07-24 stsp if (len == -1) {
8008 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
8009 0ebf8283 2019-07-24 stsp if (feof(f))
8011 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
8012 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
8017 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8019 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
8020 0ebf8283 2019-07-24 stsp free(line);
8021 0ebf8283 2019-07-24 stsp line = NULL;
8024 0ebf8283 2019-07-24 stsp cmd = NULL;
8025 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
8026 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
8027 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
8028 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
8029 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
8032 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
8037 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
8038 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8041 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
8043 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
8044 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
8045 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
8048 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
8049 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8053 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
8054 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
8055 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8059 0ebf8283 2019-07-24 stsp free(line);
8060 0ebf8283 2019-07-24 stsp line = NULL;
8064 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
8066 0ebf8283 2019-07-24 stsp *end = '\0';
8068 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
8069 0ebf8283 2019-07-24 stsp if (err) {
8070 0ebf8283 2019-07-24 stsp /* override error code */
8071 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
8075 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
8076 0ebf8283 2019-07-24 stsp if (hle == NULL) {
8077 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
8080 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
8081 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
8082 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
8083 0ebf8283 2019-07-24 stsp commit_id = NULL;
8084 0ebf8283 2019-07-24 stsp free(line);
8085 0ebf8283 2019-07-24 stsp line = NULL;
8086 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8089 0ebf8283 2019-07-24 stsp free(line);
8090 0ebf8283 2019-07-24 stsp free(commit_id);
8091 0ebf8283 2019-07-24 stsp return err;
8094 0ebf8283 2019-07-24 stsp static const struct got_error *
8095 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
8096 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
8098 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
8099 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
8100 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8101 5b87815e 2020-03-05 stsp static char msg[92];
8102 bfce7f83 2019-07-27 stsp char *id_str;
8104 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
8105 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8106 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
8107 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
8108 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
8110 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8111 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
8112 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8113 5b87815e 2020-03-05 stsp if (hle == hle2)
8115 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
8116 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
8118 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
8120 5b87815e 2020-03-05 stsp return err;
8121 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
8122 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
8123 5b87815e 2020-03-05 stsp free(id_str);
8124 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8128 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
8129 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8130 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8133 bfce7f83 2019-07-27 stsp if (hle == NULL) {
8134 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
8136 bfce7f83 2019-07-27 stsp return err;
8137 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
8138 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
8139 bfce7f83 2019-07-27 stsp free(id_str);
8140 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8144 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8145 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8146 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8147 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
8149 bfce7f83 2019-07-27 stsp return NULL;
8152 bfce7f83 2019-07-27 stsp static const struct got_error *
8153 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
8154 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
8155 bfce7f83 2019-07-27 stsp struct got_repository *repo)
8157 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8158 0ebf8283 2019-07-24 stsp char *editor;
8159 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8161 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
8163 0ebf8283 2019-07-24 stsp return err;
8165 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
8166 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
8167 0ebf8283 2019-07-24 stsp goto done;
8170 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8171 0ebf8283 2019-07-24 stsp if (f == NULL) {
8172 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
8173 0ebf8283 2019-07-24 stsp goto done;
8175 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8177 bfce7f83 2019-07-27 stsp goto done;
8179 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
8181 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8182 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8183 0ebf8283 2019-07-24 stsp free(editor);
8184 0ebf8283 2019-07-24 stsp return err;
8187 0ebf8283 2019-07-24 stsp static const struct got_error *
8188 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8189 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
8190 514f2ffe 2020-01-29 stsp struct got_repository *);
8192 0ebf8283 2019-07-24 stsp static const struct got_error *
8193 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
8194 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
8195 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
8197 0ebf8283 2019-07-24 stsp const struct got_error *err;
8198 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8199 0ebf8283 2019-07-24 stsp char *path = NULL;
8201 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
8203 0ebf8283 2019-07-24 stsp return err;
8205 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
8207 0ebf8283 2019-07-24 stsp goto done;
8209 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8211 0ebf8283 2019-07-24 stsp goto done;
8213 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
8214 083957f4 2020-02-24 stsp rewind(f);
8215 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8217 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
8218 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
8219 0ebf8283 2019-07-24 stsp goto done;
8222 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
8223 083957f4 2020-02-24 stsp if (err) {
8224 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8225 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8226 083957f4 2020-02-24 stsp goto done;
8227 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
8228 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
8232 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8233 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8234 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
8235 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
8236 0ebf8283 2019-07-24 stsp free(path);
8237 0ebf8283 2019-07-24 stsp return err;
8240 0ebf8283 2019-07-24 stsp static const struct got_error *
8241 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
8242 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8244 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8245 0ebf8283 2019-07-24 stsp char *path = NULL;
8246 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8247 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8248 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8250 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
8252 0ebf8283 2019-07-24 stsp return err;
8254 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
8255 0ebf8283 2019-07-24 stsp if (f == NULL) {
8256 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8257 0ebf8283 2019-07-24 stsp goto done;
8259 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
8260 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8265 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8266 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
8267 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
8268 0ebf8283 2019-07-24 stsp if (n < 0) {
8269 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
8275 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8276 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8277 0ebf8283 2019-07-24 stsp free(path);
8278 0ebf8283 2019-07-24 stsp if (commit)
8279 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8280 0ebf8283 2019-07-24 stsp return err;
8284 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
8286 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
8288 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
8289 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
8290 bfce7f83 2019-07-27 stsp free(hle);
8294 0ebf8283 2019-07-24 stsp static const struct got_error *
8295 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
8296 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
8298 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
8299 0ebf8283 2019-07-24 stsp FILE *f = NULL;
8301 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
8302 0ebf8283 2019-07-24 stsp if (f == NULL) {
8303 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
8304 0ebf8283 2019-07-24 stsp goto done;
8307 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
8309 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
8310 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
8311 0ebf8283 2019-07-24 stsp return err;
8314 0ebf8283 2019-07-24 stsp static const struct got_error *
8315 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8316 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
8317 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
8319 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
8320 0ebf8283 2019-07-24 stsp int resp = ' ';
8322 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8323 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8324 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8325 0ebf8283 2019-07-24 stsp resp = getchar();
8326 a61a4414 2019-08-07 stsp if (resp == '\n')
8327 a61a4414 2019-08-07 stsp resp = getchar();
8328 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8329 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8330 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8332 426ebf2e 2019-07-25 stsp if (err) {
8333 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8334 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8336 bfce7f83 2019-07-27 stsp prev_err = err;
8337 426ebf2e 2019-07-25 stsp resp = ' ';
8341 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8342 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8343 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8344 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8345 426ebf2e 2019-07-25 stsp if (err) {
8346 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8347 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8349 bfce7f83 2019-07-27 stsp prev_err = err;
8350 426ebf2e 2019-07-25 stsp resp = ' ';
8354 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8355 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8358 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8361 0ebf8283 2019-07-24 stsp return err;
8364 0ebf8283 2019-07-24 stsp static const struct got_error *
8365 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8366 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8367 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8369 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8370 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8371 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8372 3e3a69f1 2019-07-25 stsp branch, repo);
8375 0ebf8283 2019-07-24 stsp static const struct got_error *
8376 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8377 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8379 0ebf8283 2019-07-24 stsp const struct got_error *err;
8380 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8382 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8384 0ebf8283 2019-07-24 stsp goto done;
8386 0ebf8283 2019-07-24 stsp if (new_id) {
8387 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8389 0ebf8283 2019-07-24 stsp goto done;
8392 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8393 0ebf8283 2019-07-24 stsp if (new_id_str)
8394 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8396 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8397 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8398 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8399 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8400 0ebf8283 2019-07-24 stsp goto done;
8402 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8404 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8406 0ebf8283 2019-07-24 stsp goto done;
8409 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8410 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8411 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8412 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8413 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8415 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8416 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8417 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8424 0ebf8283 2019-07-24 stsp free(old_id_str);
8425 0ebf8283 2019-07-24 stsp free(new_id_str);
8426 0ebf8283 2019-07-24 stsp return err;
8429 0ebf8283 2019-07-24 stsp static const struct got_error *
8430 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8431 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8432 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8433 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8435 0ebf8283 2019-07-24 stsp const struct got_error *err;
8436 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8437 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8439 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8440 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8441 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8443 0ebf8283 2019-07-24 stsp return err;
8446 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8448 0ebf8283 2019-07-24 stsp return err;
8450 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8451 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8452 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8453 0ebf8283 2019-07-24 stsp if (err) {
8454 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8455 0ebf8283 2019-07-24 stsp goto done;
8456 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8458 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8459 0ebf8283 2019-07-24 stsp free(new_commit_id);
8462 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8463 0ebf8283 2019-07-24 stsp return err;
8466 0ebf8283 2019-07-24 stsp static const struct got_error *
8467 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8468 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8470 0ebf8283 2019-07-24 stsp const struct got_error *error;
8471 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8473 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8475 0ebf8283 2019-07-24 stsp if (error)
8476 0ebf8283 2019-07-24 stsp return error;
8478 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8479 0ebf8283 2019-07-24 stsp if (error)
8480 0ebf8283 2019-07-24 stsp return error;
8482 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8483 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8484 0ebf8283 2019-07-24 stsp return error;
8487 ab20a43a 2020-01-29 stsp static const struct got_error *
8488 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8489 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8490 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8491 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8493 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8495 ab20a43a 2020-01-29 stsp switch (status) {
8496 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8497 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8498 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8499 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8500 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8501 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8506 ab20a43a 2020-01-29 stsp switch (staged_status) {
8507 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8508 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8509 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8510 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8511 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8516 ab20a43a 2020-01-29 stsp return NULL;
8519 0ebf8283 2019-07-24 stsp static const struct got_error *
8520 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8522 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8523 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8524 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8525 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8526 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8527 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8528 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8529 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8530 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8531 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8532 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8533 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8534 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8535 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8536 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8537 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8538 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8539 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8540 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8541 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8542 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8543 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8544 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8546 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8547 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8548 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8549 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8551 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8552 0ebf8283 2019-07-24 stsp switch (ch) {
8554 0ebf8283 2019-07-24 stsp abort_edit = 1;
8557 0ebf8283 2019-07-24 stsp continue_edit = 1;
8560 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8563 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8566 0ebf8283 2019-07-24 stsp usage_histedit();
8567 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8571 0ebf8283 2019-07-24 stsp argc -= optind;
8572 0ebf8283 2019-07-24 stsp argv += optind;
8574 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8575 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8576 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8577 0ebf8283 2019-07-24 stsp err(1, "pledge");
8579 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8580 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8581 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8582 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8583 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8584 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8585 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8586 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8587 0ebf8283 2019-07-24 stsp if (argc != 0)
8588 0ebf8283 2019-07-24 stsp usage_histedit();
8591 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8592 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8593 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8594 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8595 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8596 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8599 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8600 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8601 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8602 0ebf8283 2019-07-24 stsp goto done;
8604 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8605 fa51e947 2020-03-27 stsp if (error) {
8606 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8607 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8608 0ebf8283 2019-07-24 stsp goto done;
8611 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8613 0ebf8283 2019-07-24 stsp if (error != NULL)
8614 0ebf8283 2019-07-24 stsp goto done;
8616 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8617 0ebf8283 2019-07-24 stsp if (error)
8618 0ebf8283 2019-07-24 stsp goto done;
8619 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8620 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8621 0ebf8283 2019-07-24 stsp goto done;
8624 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8625 0ebf8283 2019-07-24 stsp if (error)
8626 0ebf8283 2019-07-24 stsp goto done;
8628 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8629 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8630 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8631 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8632 083957f4 2020-02-24 stsp "before the -m option can be used");
8633 083957f4 2020-02-24 stsp goto done;
8636 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8637 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8638 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8639 3e3a69f1 2019-07-25 stsp worktree, repo);
8640 0ebf8283 2019-07-24 stsp if (error)
8641 0ebf8283 2019-07-24 stsp goto done;
8642 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8643 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8644 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8645 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8646 0ebf8283 2019-07-24 stsp if (error)
8647 0ebf8283 2019-07-24 stsp goto done;
8648 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8649 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8650 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8651 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8652 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8653 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8654 0ebf8283 2019-07-24 stsp goto done;
8657 0ebf8283 2019-07-24 stsp if (continue_edit) {
8658 0ebf8283 2019-07-24 stsp char *path;
8660 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8661 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8662 0ebf8283 2019-07-24 stsp goto done;
8665 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8666 0ebf8283 2019-07-24 stsp if (error)
8667 0ebf8283 2019-07-24 stsp goto done;
8669 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8670 0ebf8283 2019-07-24 stsp free(path);
8671 0ebf8283 2019-07-24 stsp if (error)
8672 0ebf8283 2019-07-24 stsp goto done;
8674 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8675 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8676 3e3a69f1 2019-07-25 stsp worktree, repo);
8677 0ebf8283 2019-07-24 stsp if (error)
8678 0ebf8283 2019-07-24 stsp goto done;
8680 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8681 0ebf8283 2019-07-24 stsp if (error)
8682 0ebf8283 2019-07-24 stsp goto done;
8684 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8685 0ebf8283 2019-07-24 stsp head_commit_id);
8686 0ebf8283 2019-07-24 stsp if (error)
8687 0ebf8283 2019-07-24 stsp goto done;
8688 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8689 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8690 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8691 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8692 3aac7cf7 2019-07-25 stsp goto done;
8694 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8695 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8696 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8697 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8698 0ebf8283 2019-07-24 stsp commit = NULL;
8699 0ebf8283 2019-07-24 stsp if (error)
8700 0ebf8283 2019-07-24 stsp goto done;
8702 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8703 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8704 0ebf8283 2019-07-24 stsp goto done;
8707 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8708 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8709 0ebf8283 2019-07-24 stsp if (error != NULL)
8710 0ebf8283 2019-07-24 stsp goto done;
8712 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8713 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8714 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8715 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8716 c7d20a3f 2019-07-30 stsp goto done;
8719 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8720 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8721 bfce7f83 2019-07-27 stsp branch = NULL;
8722 0ebf8283 2019-07-24 stsp if (error)
8723 0ebf8283 2019-07-24 stsp goto done;
8725 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8726 0ebf8283 2019-07-24 stsp head_commit_id);
8727 0ebf8283 2019-07-24 stsp if (error)
8728 0ebf8283 2019-07-24 stsp goto done;
8729 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8730 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8731 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8732 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8733 3aac7cf7 2019-07-25 stsp goto done;
8735 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8736 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8737 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8738 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8739 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8740 0ebf8283 2019-07-24 stsp commit = NULL;
8741 0ebf8283 2019-07-24 stsp if (error)
8742 0ebf8283 2019-07-24 stsp goto done;
8744 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8745 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8746 c5996fff 2020-01-29 stsp goto done;
8749 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8750 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8751 bfce7f83 2019-07-27 stsp if (error)
8752 bfce7f83 2019-07-27 stsp goto done;
8754 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8755 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8756 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8757 6ba2bea2 2019-07-27 stsp if (error) {
8758 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8759 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8760 9627c110 2020-04-18 stsp update_progress, &upa);
8761 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8762 0ebf8283 2019-07-24 stsp goto done;
8765 514f2ffe 2020-01-29 stsp const char *branch_name;
8766 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8767 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8768 514f2ffe 2020-01-29 stsp branch_name += 11;
8769 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8770 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8771 6ba2bea2 2019-07-27 stsp if (error) {
8772 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8773 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8774 9627c110 2020-04-18 stsp update_progress, &upa);
8775 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8776 0ebf8283 2019-07-24 stsp goto done;
8781 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8783 6ba2bea2 2019-07-27 stsp if (error) {
8784 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8785 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8786 9627c110 2020-04-18 stsp update_progress, &upa);
8787 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8788 0ebf8283 2019-07-24 stsp goto done;
8793 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8794 4ec14e60 2019-08-28 hiltjo if (error)
8795 0ebf8283 2019-07-24 stsp goto done;
8797 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8798 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8799 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8800 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8803 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8804 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8805 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8806 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8808 ab20a43a 2020-01-29 stsp if (error)
8809 ab20a43a 2020-01-29 stsp goto done;
8811 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8812 ab20a43a 2020-01-29 stsp int have_changes = 0;
8814 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8815 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8816 ab20a43a 2020-01-29 stsp if (error)
8817 ab20a43a 2020-01-29 stsp goto done;
8818 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8819 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8820 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8821 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8822 ab20a43a 2020-01-29 stsp if (error) {
8823 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8824 ab20a43a 2020-01-29 stsp goto done;
8825 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8826 ab20a43a 2020-01-29 stsp goto done;
8828 ab20a43a 2020-01-29 stsp if (have_changes) {
8829 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8830 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8831 ab20a43a 2020-01-29 stsp if (error)
8832 ab20a43a 2020-01-29 stsp goto done;
8834 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8835 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8836 ab20a43a 2020-01-29 stsp if (error)
8837 ab20a43a 2020-01-29 stsp goto done;
8838 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8839 ab20a43a 2020-01-29 stsp hle, NULL);
8840 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8841 ab20a43a 2020-01-29 stsp commit = NULL;
8842 ab20a43a 2020-01-29 stsp if (error)
8843 ab20a43a 2020-01-29 stsp goto done;
8849 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8850 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8851 0ebf8283 2019-07-24 stsp if (error)
8852 0ebf8283 2019-07-24 stsp goto done;
8856 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8857 0ebf8283 2019-07-24 stsp hle->commit_id);
8858 0ebf8283 2019-07-24 stsp if (error)
8859 0ebf8283 2019-07-24 stsp goto done;
8860 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8861 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8863 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8864 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8865 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8866 0ebf8283 2019-07-24 stsp if (error)
8867 0ebf8283 2019-07-24 stsp goto done;
8868 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8869 0ebf8283 2019-07-24 stsp commit = NULL;
8871 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8872 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8873 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8875 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8876 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8878 a0ea4fc0 2020-02-28 stsp if (error)
8879 a0ea4fc0 2020-02-28 stsp goto done;
8880 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8884 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8885 0ebf8283 2019-07-24 stsp char *id_str;
8886 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8887 0ebf8283 2019-07-24 stsp if (error)
8888 0ebf8283 2019-07-24 stsp goto done;
8889 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8891 0ebf8283 2019-07-24 stsp free(id_str);
8892 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8893 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8894 3e3a69f1 2019-07-25 stsp fileindex);
8895 0ebf8283 2019-07-24 stsp goto done;
8898 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8899 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8900 0ebf8283 2019-07-24 stsp if (error)
8901 0ebf8283 2019-07-24 stsp goto done;
8905 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8906 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8907 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8908 0ebf8283 2019-07-24 stsp if (error)
8909 0ebf8283 2019-07-24 stsp goto done;
8912 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8913 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8914 0ebf8283 2019-07-24 stsp if (error)
8915 0ebf8283 2019-07-24 stsp goto done;
8916 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8917 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8919 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8920 3e3a69f1 2019-07-25 stsp branch, repo);
8922 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8923 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8924 0ebf8283 2019-07-24 stsp free(head_commit_id);
8925 0ebf8283 2019-07-24 stsp free(base_commit_id);
8926 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8927 0ebf8283 2019-07-24 stsp if (commit)
8928 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8929 0ebf8283 2019-07-24 stsp if (branch)
8930 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8931 0ebf8283 2019-07-24 stsp if (tmp_branch)
8932 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8933 0ebf8283 2019-07-24 stsp if (worktree)
8934 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8936 2822a352 2019-10-15 stsp got_repo_close(repo);
8937 2822a352 2019-10-15 stsp return error;
8940 2822a352 2019-10-15 stsp __dead static void
8941 2822a352 2019-10-15 stsp usage_integrate(void)
8943 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8947 2822a352 2019-10-15 stsp static const struct got_error *
8948 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8950 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8951 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8952 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8953 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8954 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8955 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8956 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8957 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8959 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8961 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8962 2822a352 2019-10-15 stsp switch (ch) {
8964 2822a352 2019-10-15 stsp usage_integrate();
8965 2822a352 2019-10-15 stsp /* NOTREACHED */
8969 2822a352 2019-10-15 stsp argc -= optind;
8970 2822a352 2019-10-15 stsp argv += optind;
8972 2822a352 2019-10-15 stsp if (argc != 1)
8973 2822a352 2019-10-15 stsp usage_integrate();
8974 2822a352 2019-10-15 stsp branch_arg = argv[0];
8975 b08a0ccd 2020-09-24 stsp #ifndef PROFILE
8976 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8977 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8978 2822a352 2019-10-15 stsp err(1, "pledge");
8980 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8981 2822a352 2019-10-15 stsp if (cwd == NULL) {
8982 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8983 2822a352 2019-10-15 stsp goto done;
8986 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8987 fa51e947 2020-03-27 stsp if (error) {
8988 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8989 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8991 2822a352 2019-10-15 stsp goto done;
8994 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8995 2822a352 2019-10-15 stsp if (error)
8996 2822a352 2019-10-15 stsp goto done;
8998 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9000 2822a352 2019-10-15 stsp if (error != NULL)
9001 2822a352 2019-10-15 stsp goto done;
9003 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9004 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
9005 2822a352 2019-10-15 stsp if (error)
9006 2822a352 2019-10-15 stsp goto done;
9008 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
9009 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
9010 2822a352 2019-10-15 stsp goto done;
9013 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
9014 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
9015 2822a352 2019-10-15 stsp if (error)
9016 2822a352 2019-10-15 stsp goto done;
9018 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
9019 2822a352 2019-10-15 stsp if (refname == NULL) {
9020 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9021 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9022 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9023 2822a352 2019-10-15 stsp goto done;
9025 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
9026 2822a352 2019-10-15 stsp if (base_refname == NULL) {
9027 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
9028 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9029 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9030 2822a352 2019-10-15 stsp goto done;
9033 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
9034 2822a352 2019-10-15 stsp if (error)
9035 2822a352 2019-10-15 stsp goto done;
9037 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
9038 2822a352 2019-10-15 stsp if (error)
9039 2822a352 2019-10-15 stsp goto done;
9041 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
9042 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
9043 2822a352 2019-10-15 stsp "specified branch has already been integrated");
9044 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9045 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9046 2822a352 2019-10-15 stsp goto done;
9049 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
9050 2822a352 2019-10-15 stsp if (error) {
9051 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
9052 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
9053 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
9054 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
9055 2822a352 2019-10-15 stsp goto done;
9058 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9059 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
9060 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
9061 2822a352 2019-10-15 stsp check_cancelled, NULL);
9062 2822a352 2019-10-15 stsp if (error)
9063 2822a352 2019-10-15 stsp goto done;
9065 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
9066 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9069 715dc77e 2019-08-03 stsp got_repo_close(repo);
9070 2822a352 2019-10-15 stsp if (worktree)
9071 2822a352 2019-10-15 stsp got_worktree_close(worktree);
9072 2822a352 2019-10-15 stsp free(cwd);
9073 2822a352 2019-10-15 stsp free(base_commit_id);
9074 2822a352 2019-10-15 stsp free(commit_id);
9075 2822a352 2019-10-15 stsp free(refname);
9076 2822a352 2019-10-15 stsp free(base_refname);
9077 715dc77e 2019-08-03 stsp return error;
9080 715dc77e 2019-08-03 stsp __dead static void
9081 715dc77e 2019-08-03 stsp usage_stage(void)
9083 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9084 35213c7c 2020-07-23 stsp "[-S] [file-path ...]\n",
9085 715dc77e 2019-08-03 stsp getprogname());
9089 715dc77e 2019-08-03 stsp static const struct got_error *
9090 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
9091 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
9092 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9093 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
9095 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
9096 408b4ebc 2019-08-03 stsp char *id_str = NULL;
9098 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
9099 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
9100 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
9101 408b4ebc 2019-08-03 stsp return NULL;
9103 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
9104 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
9105 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
9107 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
9109 408b4ebc 2019-08-03 stsp return err;
9111 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
9112 408b4ebc 2019-08-03 stsp free(id_str);
9113 dc424a06 2019-08-07 stsp return NULL;
9116 dc424a06 2019-08-07 stsp static const struct got_error *
9117 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
9119 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
9120 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
9121 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
9122 715dc77e 2019-08-03 stsp char *cwd = NULL;
9123 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
9124 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
9125 35213c7c 2020-07-23 stsp int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9126 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
9127 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9128 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9130 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
9132 35213c7c 2020-07-23 stsp while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9133 715dc77e 2019-08-03 stsp switch (ch) {
9135 408b4ebc 2019-08-03 stsp list_stage = 1;
9138 dc424a06 2019-08-07 stsp pflag = 1;
9141 dc424a06 2019-08-07 stsp patch_script_path = optarg;
9144 35213c7c 2020-07-23 stsp allow_bad_symlinks = 1;
9147 715dc77e 2019-08-03 stsp usage_stage();
9148 715dc77e 2019-08-03 stsp /* NOTREACHED */
9152 715dc77e 2019-08-03 stsp argc -= optind;
9153 715dc77e 2019-08-03 stsp argv += optind;
9155 715dc77e 2019-08-03 stsp #ifndef PROFILE
9156 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9157 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
9158 715dc77e 2019-08-03 stsp err(1, "pledge");
9160 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
9161 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
9162 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
9163 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
9165 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
9166 715dc77e 2019-08-03 stsp if (cwd == NULL) {
9167 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
9168 715dc77e 2019-08-03 stsp goto done;
9171 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9172 fa51e947 2020-03-27 stsp if (error) {
9173 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9174 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
9175 715dc77e 2019-08-03 stsp goto done;
9178 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9180 715dc77e 2019-08-03 stsp if (error != NULL)
9181 715dc77e 2019-08-03 stsp goto done;
9183 dc424a06 2019-08-07 stsp if (patch_script_path) {
9184 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
9185 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
9186 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
9187 dc424a06 2019-08-07 stsp patch_script_path);
9188 dc424a06 2019-08-07 stsp goto done;
9191 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9192 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
9193 715dc77e 2019-08-03 stsp if (error)
9194 715dc77e 2019-08-03 stsp goto done;
9196 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9197 6d022e97 2019-08-04 stsp if (error)
9198 6d022e97 2019-08-04 stsp goto done;
9200 6d022e97 2019-08-04 stsp if (list_stage)
9201 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
9202 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
9204 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9205 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
9206 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
9207 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
9208 35213c7c 2020-07-23 stsp pflag ? choose_patch : NULL, &cpa,
9209 35213c7c 2020-07-23 stsp allow_bad_symlinks, repo);
9212 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9213 2e1f37b0 2019-08-08 stsp error == NULL)
9214 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9216 ad493afc 2019-08-03 stsp got_repo_close(repo);
9217 ad493afc 2019-08-03 stsp if (worktree)
9218 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
9219 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9220 ad493afc 2019-08-03 stsp free((char *)pe->path);
9221 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
9222 ad493afc 2019-08-03 stsp free(cwd);
9223 ad493afc 2019-08-03 stsp return error;
9226 ad493afc 2019-08-03 stsp __dead static void
9227 ad493afc 2019-08-03 stsp usage_unstage(void)
9229 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9230 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
9231 ad493afc 2019-08-03 stsp getprogname());
9236 ad493afc 2019-08-03 stsp static const struct got_error *
9237 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
9239 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
9240 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
9241 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
9242 ad493afc 2019-08-03 stsp char *cwd = NULL;
9243 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
9244 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
9245 9627c110 2020-04-18 stsp int ch, pflag = 0;
9246 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
9247 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
9248 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
9249 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
9251 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
9253 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
9254 ad493afc 2019-08-03 stsp switch (ch) {
9256 2e1f37b0 2019-08-08 stsp pflag = 1;
9259 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
9262 ad493afc 2019-08-03 stsp usage_unstage();
9263 ad493afc 2019-08-03 stsp /* NOTREACHED */
9267 ad493afc 2019-08-03 stsp argc -= optind;
9268 ad493afc 2019-08-03 stsp argv += optind;
9270 ad493afc 2019-08-03 stsp #ifndef PROFILE
9271 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9272 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
9273 ad493afc 2019-08-03 stsp err(1, "pledge");
9275 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
9276 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
9278 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
9279 ad493afc 2019-08-03 stsp if (cwd == NULL) {
9280 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
9281 ad493afc 2019-08-03 stsp goto done;
9284 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
9285 fa51e947 2020-03-27 stsp if (error) {
9286 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9287 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
9288 ad493afc 2019-08-03 stsp goto done;
9291 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9293 ad493afc 2019-08-03 stsp if (error != NULL)
9294 ad493afc 2019-08-03 stsp goto done;
9296 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
9297 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
9298 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
9299 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
9300 2e1f37b0 2019-08-08 stsp patch_script_path);
9301 2e1f37b0 2019-08-08 stsp goto done;
9305 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
9306 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
9307 ad493afc 2019-08-03 stsp if (error)
9308 ad493afc 2019-08-03 stsp goto done;
9310 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9311 ad493afc 2019-08-03 stsp if (error)
9312 ad493afc 2019-08-03 stsp goto done;
9314 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
9315 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
9316 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
9317 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
9318 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
9319 9627c110 2020-04-18 stsp if (!error)
9320 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
9322 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
9323 2e1f37b0 2019-08-08 stsp error == NULL)
9324 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9326 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9327 715dc77e 2019-08-03 stsp if (worktree)
9328 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9329 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9330 715dc77e 2019-08-03 stsp free((char *)pe->path);
9331 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9332 715dc77e 2019-08-03 stsp free(cwd);
9333 01073a5d 2019-08-22 stsp return error;
9336 01073a5d 2019-08-22 stsp __dead static void
9337 01073a5d 2019-08-22 stsp usage_cat(void)
9339 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9340 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9344 01073a5d 2019-08-22 stsp static const struct got_error *
9345 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9347 01073a5d 2019-08-22 stsp const struct got_error *err;
9348 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9350 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9352 01073a5d 2019-08-22 stsp return err;
9354 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9355 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9356 01073a5d 2019-08-22 stsp return err;
9359 01073a5d 2019-08-22 stsp static const struct got_error *
9360 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9362 01073a5d 2019-08-22 stsp const struct got_error *err;
9363 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9364 56e0773d 2019-11-28 stsp int nentries, i;
9366 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9368 01073a5d 2019-08-22 stsp return err;
9370 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9371 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9372 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9373 01073a5d 2019-08-22 stsp char *id_str;
9374 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9376 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9377 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9380 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9381 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9382 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9383 01073a5d 2019-08-22 stsp free(id_str);
9386 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9387 01073a5d 2019-08-22 stsp return err;
9390 01073a5d 2019-08-22 stsp static const struct got_error *
9391 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9393 01073a5d 2019-08-22 stsp const struct got_error *err;
9394 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9395 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9396 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9397 01073a5d 2019-08-22 stsp char *id_str = NULL;
9398 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9400 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9402 01073a5d 2019-08-22 stsp return err;
9404 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9406 01073a5d 2019-08-22 stsp goto done;
9408 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9409 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9410 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9411 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9412 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9413 01073a5d 2019-08-22 stsp char *pid_str;
9414 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9416 01073a5d 2019-08-22 stsp goto done;
9417 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9418 01073a5d 2019-08-22 stsp free(pid_str);
9420 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9421 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9422 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9424 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9425 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9426 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9428 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9429 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9430 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9432 01073a5d 2019-08-22 stsp free(id_str);
9433 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9434 01073a5d 2019-08-22 stsp return err;
9437 01073a5d 2019-08-22 stsp static const struct got_error *
9438 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9440 01073a5d 2019-08-22 stsp const struct got_error *err;
9441 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9442 01073a5d 2019-08-22 stsp char *id_str = NULL;
9443 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9445 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9447 01073a5d 2019-08-22 stsp return err;
9449 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9451 01073a5d 2019-08-22 stsp goto done;
9453 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9455 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9456 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9457 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9458 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9460 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9461 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9462 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9464 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9465 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9466 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9468 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9469 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9470 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9476 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9477 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9479 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9480 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9481 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9483 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9484 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9485 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9487 01073a5d 2019-08-22 stsp free(id_str);
9488 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9489 01073a5d 2019-08-22 stsp return err;
9492 01073a5d 2019-08-22 stsp static const struct got_error *
9493 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9495 01073a5d 2019-08-22 stsp const struct got_error *error;
9496 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9497 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9498 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9499 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9500 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9501 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9503 01073a5d 2019-08-22 stsp #ifndef PROFILE
9504 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9505 01073a5d 2019-08-22 stsp NULL) == -1)
9506 01073a5d 2019-08-22 stsp err(1, "pledge");
9509 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9510 01073a5d 2019-08-22 stsp switch (ch) {
9512 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9515 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9516 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9517 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9519 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9522 896e9b6f 2019-08-26 stsp force_path = 1;
9525 01073a5d 2019-08-22 stsp usage_cat();
9526 01073a5d 2019-08-22 stsp /* NOTREACHED */
9530 01073a5d 2019-08-22 stsp argc -= optind;
9531 01073a5d 2019-08-22 stsp argv += optind;
9533 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9534 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9535 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9536 01073a5d 2019-08-22 stsp goto done;
9538 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9539 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9540 01073a5d 2019-08-22 stsp goto done;
9541 01073a5d 2019-08-22 stsp if (worktree) {
9542 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9543 01073a5d 2019-08-22 stsp repo_path = strdup(
9544 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9545 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9546 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9547 01073a5d 2019-08-22 stsp goto done;
9552 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9553 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9554 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9555 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9558 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9559 01073a5d 2019-08-22 stsp free(repo_path);
9560 01073a5d 2019-08-22 stsp if (error != NULL)
9561 01073a5d 2019-08-22 stsp goto done;
9563 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9564 896e9b6f 2019-08-26 stsp if (error)
9565 896e9b6f 2019-08-26 stsp goto done;
9567 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9568 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9569 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9570 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9571 01073a5d 2019-08-22 stsp if (error)
9572 01073a5d 2019-08-22 stsp goto done;
9574 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9575 896e9b6f 2019-08-26 stsp if (force_path) {
9576 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9578 896e9b6f 2019-08-26 stsp if (error)
9581 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9582 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9583 896e9b6f 2019-08-26 stsp if (error) {
9584 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9585 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9587 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9588 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9589 896e9b6f 2019-08-26 stsp if (error)
9594 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9595 01073a5d 2019-08-22 stsp if (error)
9598 01073a5d 2019-08-22 stsp switch (obj_type) {
9599 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9600 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9602 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9603 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9605 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9606 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9608 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9609 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9612 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9615 01073a5d 2019-08-22 stsp if (error)
9617 01073a5d 2019-08-22 stsp free(label);
9618 01073a5d 2019-08-22 stsp label = NULL;
9620 01073a5d 2019-08-22 stsp id = NULL;
9623 01073a5d 2019-08-22 stsp free(label);
9625 896e9b6f 2019-08-26 stsp free(commit_id);
9626 01073a5d 2019-08-22 stsp if (worktree)
9627 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9628 01073a5d 2019-08-22 stsp if (repo) {
9629 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9630 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9631 01073a5d 2019-08-22 stsp if (error == NULL)
9632 01073a5d 2019-08-22 stsp error = repo_error;
9634 b2118c49 2020-07-28 stsp return error;
9637 b2118c49 2020-07-28 stsp __dead static void
9638 b2118c49 2020-07-28 stsp usage_info(void)
9640 b2118c49 2020-07-28 stsp fprintf(stderr, "usage: %s info [path ...]\n",
9641 b2118c49 2020-07-28 stsp getprogname());
9645 b2118c49 2020-07-28 stsp static const struct got_error *
9646 b2118c49 2020-07-28 stsp print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9647 b2118c49 2020-07-28 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9648 b2118c49 2020-07-28 stsp struct got_object_id *commit_id)
9650 b2118c49 2020-07-28 stsp const struct got_error *err = NULL;
9651 b2118c49 2020-07-28 stsp char *id_str = NULL;
9652 b2118c49 2020-07-28 stsp char datebuf[128];
9653 b2118c49 2020-07-28 stsp struct tm mytm, *tm;
9654 b2118c49 2020-07-28 stsp struct got_pathlist_head *paths = arg;
9655 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9658 b2118c49 2020-07-28 stsp * Clear error indication from any of the path arguments which
9659 b2118c49 2020-07-28 stsp * would cause this file index entry to be displayed.
9661 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, paths, entry) {
9662 b2118c49 2020-07-28 stsp if (got_path_cmp(path, pe->path, strlen(path),
9663 b2118c49 2020-07-28 stsp pe->path_len) == 0 ||
9664 b2118c49 2020-07-28 stsp got_path_is_child(path, pe->path, pe->path_len))
9665 b2118c49 2020-07-28 stsp pe->data = NULL; /* no error */
9668 b2118c49 2020-07-28 stsp printf(GOT_COMMIT_SEP_STR);
9669 b2118c49 2020-07-28 stsp if (S_ISLNK(mode))
9670 b2118c49 2020-07-28 stsp printf("symlink: %s\n", path);
9671 b2118c49 2020-07-28 stsp else if (S_ISREG(mode)) {
9672 b2118c49 2020-07-28 stsp printf("file: %s\n", path);
9673 b2118c49 2020-07-28 stsp printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9674 b2118c49 2020-07-28 stsp } else if (S_ISDIR(mode))
9675 b2118c49 2020-07-28 stsp printf("directory: %s\n", path);
9677 b2118c49 2020-07-28 stsp printf("something: %s\n", path);
9679 b2118c49 2020-07-28 stsp tm = localtime_r(&mtime, &mytm);
9680 b2118c49 2020-07-28 stsp if (tm == NULL)
9681 b2118c49 2020-07-28 stsp return NULL;
9682 b2118c49 2020-07-28 stsp if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9683 b2118c49 2020-07-28 stsp return got_error(GOT_ERR_NO_SPACE);
9684 b2118c49 2020-07-28 stsp printf("timestamp: %s\n", datebuf);
9686 b2118c49 2020-07-28 stsp if (blob_id) {
9687 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, blob_id);
9689 b2118c49 2020-07-28 stsp return err;
9690 b2118c49 2020-07-28 stsp printf("based on blob: %s\n", id_str);
9691 b2118c49 2020-07-28 stsp free(id_str);
9694 b2118c49 2020-07-28 stsp if (staged_blob_id) {
9695 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, staged_blob_id);
9697 b2118c49 2020-07-28 stsp return err;
9698 b2118c49 2020-07-28 stsp printf("based on staged blob: %s\n", id_str);
9699 b2118c49 2020-07-28 stsp free(id_str);
9702 b2118c49 2020-07-28 stsp if (commit_id) {
9703 b2118c49 2020-07-28 stsp err = got_object_id_str(&id_str, commit_id);
9705 b2118c49 2020-07-28 stsp return err;
9706 b2118c49 2020-07-28 stsp printf("based on commit: %s\n", id_str);
9707 b2118c49 2020-07-28 stsp free(id_str);
9710 b2118c49 2020-07-28 stsp return NULL;
9713 b2118c49 2020-07-28 stsp static const struct got_error *
9714 b2118c49 2020-07-28 stsp cmd_info(int argc, char *argv[])
9716 b2118c49 2020-07-28 stsp const struct got_error *error = NULL;
9717 b2118c49 2020-07-28 stsp struct got_worktree *worktree = NULL;
9718 b2118c49 2020-07-28 stsp char *cwd = NULL, *id_str = NULL;
9719 b2118c49 2020-07-28 stsp struct got_pathlist_head paths;
9720 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9721 b2118c49 2020-07-28 stsp char *uuidstr = NULL;
9722 b2118c49 2020-07-28 stsp int ch, show_files = 0;
9724 b2118c49 2020-07-28 stsp TAILQ_INIT(&paths);
9726 b2118c49 2020-07-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
9727 b2118c49 2020-07-28 stsp switch (ch) {
9729 b2118c49 2020-07-28 stsp usage_info();
9730 b2118c49 2020-07-28 stsp /* NOTREACHED */
9734 b2118c49 2020-07-28 stsp argc -= optind;
9735 b2118c49 2020-07-28 stsp argv += optind;
9737 b2118c49 2020-07-28 stsp #ifndef PROFILE
9738 b2118c49 2020-07-28 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9739 b2118c49 2020-07-28 stsp NULL) == -1)
9740 b2118c49 2020-07-28 stsp err(1, "pledge");
9742 b2118c49 2020-07-28 stsp cwd = getcwd(NULL, 0);
9743 b2118c49 2020-07-28 stsp if (cwd == NULL) {
9744 b2118c49 2020-07-28 stsp error = got_error_from_errno("getcwd");
9745 b2118c49 2020-07-28 stsp goto done;
9748 b2118c49 2020-07-28 stsp error = got_worktree_open(&worktree, cwd);
9749 b2118c49 2020-07-28 stsp if (error) {
9750 b2118c49 2020-07-28 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
9751 b2118c49 2020-07-28 stsp error = wrap_not_worktree_error(error, "status", cwd);
9752 b2118c49 2020-07-28 stsp goto done;
9755 b2118c49 2020-07-28 stsp error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9756 b2118c49 2020-07-28 stsp if (error)
9757 b2118c49 2020-07-28 stsp goto done;
9759 b2118c49 2020-07-28 stsp if (argc >= 1) {
9760 b2118c49 2020-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv,
9761 b2118c49 2020-07-28 stsp worktree);
9762 b2118c49 2020-07-28 stsp if (error)
9763 b2118c49 2020-07-28 stsp goto done;
9764 b2118c49 2020-07-28 stsp show_files = 1;
9767 b2118c49 2020-07-28 stsp error = got_object_id_str(&id_str,
9768 b2118c49 2020-07-28 stsp got_worktree_get_base_commit_id(worktree));
9769 b2118c49 2020-07-28 stsp if (error)
9770 b2118c49 2020-07-28 stsp goto done;
9772 b2118c49 2020-07-28 stsp error = got_worktree_get_uuid(&uuidstr, worktree);
9773 b2118c49 2020-07-28 stsp if (error)
9774 b2118c49 2020-07-28 stsp goto done;
9776 b2118c49 2020-07-28 stsp printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9777 b2118c49 2020-07-28 stsp printf("work tree base commit: %s\n", id_str);
9778 b2118c49 2020-07-28 stsp printf("work tree path prefix: %s\n",
9779 b2118c49 2020-07-28 stsp got_worktree_get_path_prefix(worktree));
9780 b2118c49 2020-07-28 stsp printf("work tree branch reference: %s\n",
9781 b2118c49 2020-07-28 stsp got_worktree_get_head_ref_name(worktree));
9782 b2118c49 2020-07-28 stsp printf("work tree UUID: %s\n", uuidstr);
9783 b2118c49 2020-07-28 stsp printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9785 b2118c49 2020-07-28 stsp if (show_files) {
9786 b2118c49 2020-07-28 stsp struct got_pathlist_entry *pe;
9787 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9788 b2118c49 2020-07-28 stsp if (pe->path_len == 0)
9791 b2118c49 2020-07-28 stsp * Assume this path will fail. This will be corrected
9792 b2118c49 2020-07-28 stsp * in print_path_info() in case the path does suceeed.
9794 b2118c49 2020-07-28 stsp pe->data = (void *)got_error_path(pe->path,
9795 b2118c49 2020-07-28 stsp GOT_ERR_BAD_PATH);
9797 b2118c49 2020-07-28 stsp error = got_worktree_path_info(worktree, &paths,
9798 b2118c49 2020-07-28 stsp print_path_info, &paths, check_cancelled, NULL);
9799 b2118c49 2020-07-28 stsp if (error)
9800 b2118c49 2020-07-28 stsp goto done;
9801 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry) {
9802 b2118c49 2020-07-28 stsp if (pe->data != NULL) {
9803 b2118c49 2020-07-28 stsp error = pe->data; /* bad path */
9809 b2118c49 2020-07-28 stsp TAILQ_FOREACH(pe, &paths, entry)
9810 b2118c49 2020-07-28 stsp free((char *)pe->path);
9811 b2118c49 2020-07-28 stsp got_pathlist_free(&paths);
9812 b2118c49 2020-07-28 stsp free(cwd);
9813 b2118c49 2020-07-28 stsp free(id_str);
9814 b2118c49 2020-07-28 stsp free(uuidstr);
9815 0ebf8283 2019-07-24 stsp return error;